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)