AgentSession class is the central abstraction for agent lifecycle and session management. It provides access to agent state, event subscription with automatic persistence, model management, compaction, and session operations.
Import
Constructor
AgentSessionConfig
required
Configuration object for the agent session
Properties
Agent
Access to the underlying agent instance
SessionManager
Access to the session manager
string
Current working directory
Methods
addEventListener
Subscribe to agent session events.(event: AgentSessionEvent) => void
required
Callback function that receives session events
agent_start- Agent turn beginsagent_end- Agent turn completestool_execution_start- Tool execution beginstool_execution_end- Tool execution completesauto_compaction_start- Automatic compaction triggeredauto_compaction_end- Automatic compaction finished- And more (see
AgentSessionEventtype)
prompt
Send a user message to the agent.string
required
The user’s message text
PromptOptions
executeBash
Execute a bash command.string
required
The bash command to execute
string
Optional description of the command
compact
Manually trigger context compaction.CompactOptions
switchSession
Switch to a different session.string
required
The ID of the session to switch to