AgentSubscriber
TheAgentSubscriber interface provides a comprehensive event-driven system for
handling agent lifecycle events, message updates, and state mutations during
agent execution. It allows you to hook into various stages of the agent’s
operation and modify its behavior.
Overview
AgentSubscriber defines a collection of optional event handlers and lifecycle
hooks that can respond to different stages of agent execution. All methods in
the interface are optional, allowing you to implement only the events you need
to handle.
All subscriber methods can be either synchronous or asynchronous - if they
return a Promise, the agent will await their completion before proceeding.
Adding Subscribers to Agents
Subscribers can be added to agents in two ways:Permanent Subscription
Use thesubscribe() method to add a subscriber that will persist across
multiple agent runs:
Temporary Subscription
Pass a subscriber directly torunAgent() for one-time use:
Core Interfaces
AgentSubscriber
The main interface that defines all available event handlers and lifecycle hooks. All methods in the interface are optional, allowing you to implement only the events you need to handle.AgentStateMutation
Event handlers can return anAgentStateMutation object to modify the agent’s
state and control event processing:
- messages: Replaces the current message history
- state: Replaces the current agent state
- stopPropagation: If
true, prevents subsequent subscribers from handling the event (useful for overriding default behavior)
AgentSubscriberParams
Common parameters passed to most subscriber methods:Event Handlers
Lifecycle Events
onRunInitialized()
Called when the agent run is first initialized, before any processing begins.onRunFailed()
Called when the agent run encounters an error.onRunFinalized()
Called when the agent run completes, regardless of success or failure.Event Handlers
onEvent()
General event handler that receives all events during agent execution.onRunStartedEvent()
Triggered when an agent run begins execution.onRunFinishedEvent()
Called when an agent run completes successfully.onRunErrorEvent()
Triggered when an agent run encounters an error.onStepStartedEvent()
Called when a step within an agent run begins.onStepFinishedEvent()
Triggered when a step within an agent run completes.Message Events
onTextMessageStartEvent()
Triggered when a text message starts being generated.onTextMessageContentEvent()
Called for each chunk of text content as it’s generated.onTextMessageEndEvent()
Called when a text message generation is complete.Tool Call Events
onToolCallStartEvent()
Triggered when a tool call begins.onToolCallArgsEvent()
Called as tool call arguments are being parsed, providing both raw and parsed argument data.onToolCallEndEvent()
Called when a tool call is complete with final arguments.onToolCallResultEvent()
Triggered when a tool call result is received.State Events
onStateSnapshotEvent()
Called when a complete state snapshot is provided.onStateDeltaEvent()
Triggered when partial state changes are applied.onMessagesSnapshotEvent()
Called when a complete message history snapshot is provided.onActivitySnapshotEvent()
Called when an activity snapshot is received. The handler receives both the raw event and any existingActivityMessage (if present) so you can inspect or
replace it before the default client logic runs.