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.
onActivityDeltaEvent()
Triggered for each activity delta. Use this hook to transform or debounce the incoming JSON Patch operations before they update the conversation transcript.onRawEvent()
Handler for raw, unprocessed events.onCustomEvent()
Handler for custom application-specific events.State Change Handlers
onMessagesChanged()
Called when the agent’s message history is updated.onStateChanged()
Triggered when the agent’s state is modified.onNewMessage()
Called when a new message is added to the conversation.onNewToolCall()
Triggered when a new tool call is added to a message.Messages and state are read-only
Treat the messages and state that handlers receive, andagent.messages, as
read-only. Copy a value before you change it.
- A message that did not change keeps the same object from one update to the next, so it can be compared by identity. Editing one in place also changes every earlier update that holds it.
- A changed message shares its unchanged nested values, such as metadata and multimodal content, with the reducer’s working copy. Editing those in place can change what later updates contain.
agent.stateand activity messagecontentare copied when emitted, so an edit to them does not reach later updates. An earlier update that holds the same object still sees the edit.