Skip to main content
The 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
Events:
  • agent_start - Agent turn begins
  • agent_end - Agent turn completes
  • tool_execution_start - Tool execution begins
  • tool_execution_end - Tool execution completes
  • auto_compaction_start - Automatic compaction triggered
  • auto_compaction_end - Automatic compaction finished
  • And more (see AgentSessionEvent type)

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

getState

Get current agent state.
Returns the current agent state including messages, model, thinking level, and streaming status.

Example

Type Definitions

SessionStats

Statistics about the current session.

ModelCycleResult

Result from a prompt operation.