Kind: Class
Source: atloria-monorepo/libs/agent-core/src/session/agent-session.ts
Interactive agent session.
Usage:
typescriptconst session = new AgentSession(runtime, agentConfig, { maxTurns: 30 });
session.on('tool_start', (e) => console.log(`Using ${e.toolName}...`));
session.on('agent_message', (e) => console.log(e.text));
const result = await session.send('Build a login page');
const result2 = await session.send('Add password validation');
Extends: EventEmitter
Methods
| Method | Signature | Returns |
|---|---|---|
on | on(event: K, listener: (data: SessionEventMap[K]) => void) | this |
off | off(event: K, listener: (data: SessionEventMap[K]) => void) | this |
emit | emit(event: K, data: SessionEventMap[K]) | boolean |
send | send(message: string) | Promise<RunResult> |
inject | inject(message: string) | void |
cancel | cancel() | void |
getState | getState() | SessionState |
getHistory | getHistory() | AgentHistoryItem[] |
getUsage | getUsage() | TokenUsage |
getSendCount | getSendCount() | number |
getTurnCount | getTurnCount() | number |
getSnapshot | getSnapshot() | { conversationHistory: AgentHistoryItem[]; sdkHistory: unknown[] } |
loadHistory | loadHistory(conversationHistory: AgentHistoryItem[], sdkHistory: unknown[]) | void |
end | end() | void |
setVerbose | setVerbose(enabled: boolean) | void |
isVerbose | isVerbose() | boolean |
isPlanMode | isPlanMode() | boolean |
getActiveModel | getActiveModel() | `string |
approvePlan | approvePlan(feedback: string) | Promise<RunResult> |
revisePlan | revisePlan(feedback: string) | Promise<RunResult> |
fork | fork(branchName: string) | AgentSession<TContext> |
Where it refuses work
AgentSessionstops the work withErrorwhenthis.state === 'running'— “Session is already processing a message. Use inject() for mid-task messages.”.AgentSessionstops the work withErrorwhenresult.blocked.AgentSessionstops the work withErrorwhenthis.state !== 'running'— “Cannot inject: session is not running. Use send() instead.”.AgentSessionstops the work withErrorwhenthis.state !== 'idle'— “Cannot load history: session is not idle.”.AgentSessionstops the work withErrorwhenthis.history.length > 0 || this.conversationHistory.length > 0— “Cannot load history: session already has history.”.AgentSessionstops the work withErrorwhen!this.planMode— “Cannot approve plan: session is not in plan mode.”.
When something fails
AgentSessionhandles failure in 6 places: it lets it reach the caller in 3, and discards it silently in 3. A failure discarded silently leaves no trace for whoever debugs this later.
Was this page helpful?