Skip to content

AgentSession

reference
1 min readUpdated

Kind: Class

Source: atloria-monorepo/libs/agent-core/src/session/agent-session.ts

Interactive agent session.

Usage:

typescript
const 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

MethodSignatureReturns
onon(event: K, listener: (data: SessionEventMap[K]) => void)this
offoff(event: K, listener: (data: SessionEventMap[K]) => void)this
emitemit(event: K, data: SessionEventMap[K])boolean
sendsend(message: string)Promise<RunResult>
injectinject(message: string)void
cancelcancel()void
getStategetState()SessionState
getHistorygetHistory()AgentHistoryItem[]
getUsagegetUsage()TokenUsage
getSendCountgetSendCount()number
getTurnCountgetTurnCount()number
getSnapshotgetSnapshot(){ conversationHistory: AgentHistoryItem[]; sdkHistory: unknown[] }
loadHistoryloadHistory(conversationHistory: AgentHistoryItem[], sdkHistory: unknown[])void
endend()void
setVerbosesetVerbose(enabled: boolean)void
isVerboseisVerbose()boolean
isPlanModeisPlanMode()boolean
getActiveModelgetActiveModel()`string
approvePlanapprovePlan(feedback: string)Promise<RunResult>
revisePlanrevisePlan(feedback: string)Promise<RunResult>
forkfork(branchName: string)AgentSession<TContext>

Where it refuses work

  • AgentSession stops the work with Error when this.state === 'running' — “Session is already processing a message. Use inject() for mid-task messages.”.
  • AgentSession stops the work with Error when result.blocked.
  • AgentSession stops the work with Error when this.state !== 'running' — “Cannot inject: session is not running. Use send() instead.”.
  • AgentSession stops the work with Error when this.state !== 'idle' — “Cannot load history: session is not idle.”.
  • AgentSession stops the work with Error when this.history.length > 0 || this.conversationHistory.length > 0 — “Cannot load history: session already has history.”.
  • AgentSession stops the work with Error when !this.planMode — “Cannot approve plan: session is not in plan mode.”.

When something fails

  • AgentSession handles 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?

Download as PDF