Agent class

Stateful wrapper around the low-level agent loop.

Agent owns the current transcript, emits lifecycle events, executes tools, and exposes queueing APIs for steering and follow-up messages. Port of pi's Agent.

Constructors

Agent({Model? model, String? systemPrompt, List<Tool>? tools, List<Message>? messages, required StreamFunction streamFunction, ToolExecutor? toolExecutor, ToolRegistry? toolRegistry, BeforeToolCallHook? beforeToolCall, AfterToolCallHook? afterToolCall, TransformContextHook? transformContext, PrepareNextTurnHook? prepareNextTurn, QueueMode steeringMode = QueueMode.oneAtATime, QueueMode followUpMode = QueueMode.oneAtATime, ToolExecutionMode toolExecution = ToolExecutionMode.parallel})
Creates an agent. See the library doc for the pi mapping.

Properties

afterToolCall AfterToolCallHook?
Called after a tool finishes; can override the result. See AfterToolCallHook.
getter/setter pair
beforeToolCall BeforeToolCallHook?
Called before a tool is executed; can block it. See BeforeToolCallHook.
getter/setter pair
cancelToken CancelToken?
Active cancel token for the current run, if any.
no setter
followUpMode QueueMode
Controls how queued follow-up messages are drained.
getter/setter pair
hashCode int
The hash code for this object.
no setterinherited
prepareNextTurn PrepareNextTurnHook?
Adjusts context/model between turns. See PrepareNextTurnHook.
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
state AgentState
Current agent state.
no setter
steeringMode QueueMode
Controls how queued steering messages are drained.
getter/setter pair
streamFunction StreamFunction
Provider adapter used for every model call. See StreamFunction.
getter/setter pair
toolExecution ToolExecutionMode
Tool execution strategy for assistant messages that contain multiple tool calls.
getter/setter pair
toolExecutor ToolExecutor
Executes tool calls requested by the model. See ToolExecutor.
getter/setter pair
transformContext TransformContextHook?
Rewrites the message list sent to the provider before each call. See TransformContextHook.
getter/setter pair

Methods

abort() → void
Abort the current run, if one is active.
clearAllQueues() → void
Remove all queued steering and follow-up messages.
clearFollowUpQueue() → void
Remove all queued follow-up messages.
clearSteeringQueue() → void
Remove all queued steering messages.
continueRun() Future<void>
Continue from the current transcript. The last message must be a user or tool-result message, unless queued steering/follow-up messages can start a fresh prompt run (pi's continue()).
drainSteeringQueue() List<Message>
Drain the steering queue and return its messages (abort path: the queued steers must land in the transcript, not vanish).
followUp(Message message) → void
Queue a message to run only after the agent would otherwise stop.
hasQueuedMessages() bool
Returns true when either queue still contains pending messages.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
prompt(String text) Future<void>
Start a new prompt from plain text.
promptMessage(Message message) Future<void>
Start a new prompt from a single message.
promptMessages(List<Message> messages) Future<void>
Start a new prompt from a batch of messages.
reset() → void
Clear transcript state, runtime state, and queued messages.
steer(Message message) → void
Queue a message to be injected after the current assistant turn finishes.
subscribe(AgentListener listener) → void Function()
Subscribe to agent lifecycle events. Returns an unsubscribe function.
toString() String
A string representation of this object.
inherited
waitForIdle() Future<void>
Resolves when the current run and all awaited event listeners have finished (after agent_end listeners settle).

Operators

operator ==(Object other) bool
The equality operator.
inherited