|
| 1 | +--- |
| 2 | +title: "AbstractAgent" |
| 3 | +description: "Base agent implementation with core event handling (Java)" |
| 4 | +--- |
| 5 | + |
| 6 | +# AbstractAgent |
| 7 | + |
| 8 | +The `AbstractAgent` class provides the foundation for agent implementations. It handles event stream processing, state management, and message history. Extend it and implement `protected void run(RunAgentInput input, IEventStream<BaseEvent> stream)`. |
| 9 | + |
| 10 | +## Example Implementation |
| 11 | + |
| 12 | +```java |
| 13 | +public class MyAgent extends AbstractAgent { |
| 14 | + public MyAgent() { |
| 15 | + super("my-agent", "A custom agent", "thread-123", List.of(), new State(), false); |
| 16 | + } |
| 17 | + |
| 18 | + @Override |
| 19 | + protected void run(RunAgentInput input, IEventStream<BaseEvent> stream) { |
| 20 | + // Emit events using the stream |
| 21 | + stream.next(new RunStartedEvent(input.getThreadId(), input.getRunId())); |
| 22 | + |
| 23 | + // Your agent logic here... |
| 24 | + |
| 25 | + stream.next(new RunFinishedEvent(input.getThreadId(), input.getRunId())); |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +## Configuration |
| 31 | + |
| 32 | +Constructors take: `agentId`, `description`, `threadId`, `initialMessages`, `state`, `debug`. |
| 33 | + |
| 34 | +## Core Methods |
| 35 | + |
| 36 | +- `CompletableFuture<Void> runAgent(RunAgentParameters parameters, AgentSubscriber subscriber)` — orchestrates an async run and event distribution |
| 37 | +- `Subscription subscribe(AgentSubscriber subscriber)` — persistent subscriber registration |
| 38 | +- `void addMessage(BaseMessage message)` / `void addMessages(List<BaseMessage>)` |
| 39 | +- `void setMessages(List<BaseMessage>)` |
| 40 | +- `void setState(State state)` / `State getState()` |
| 41 | + |
| 42 | +## Protected Methods |
| 43 | + |
| 44 | +- `protected abstract void run(RunAgentInput input, IEventStream<BaseEvent> stream)` — emit events into `stream` |
| 45 | +- `protected RunAgentInput prepareRunAgentInput(RunAgentParameters parameters)` |
| 46 | +- `protected void onInitialize(RunAgentInput input, List<AgentSubscriber> subscribers)` |
| 47 | +- `protected void handleEvent(BaseEvent event, List<AgentSubscriber> subscribers, AtomicReference<IEventStream<BaseEvent>> streamRef)` |
| 48 | +- `protected void handleComplete(List<AgentSubscriber> subscribers, RunAgentInput input, CompletableFuture<Void> future)` |
| 49 | +- `protected void handleError(Throwable error, List<AgentSubscriber> subscribers, CompletableFuture<Void> future)` |
| 50 | + |
| 51 | +### Text Message Helpers |
| 52 | + |
| 53 | +`handleTextMessageStart`, `handleTextMessageContent`, `handleTextMessageChunk`, `handleTextMessageEnd` integrate with `MessageFactory` to assemble streamed messages and notify subscribers. |
| 54 | + |
| 55 | + |
0 commit comments