Skip to content

DefaultAgentStateController

Default agent state controller extracted from Runtime.

Handles:

  • Active agent resolution with fallback
  • Handoff recording in session.handoffHistory + session.metadata.handoffHistory
  • Per-agent state updates in session.agentStates
  • Circular handoff detection
new DefaultAgentStateController(): DefaultAgentStateController;

DefaultAgentStateController

clearAgent(session, agentId): void;

Remove per-agent state from session.agentStates. No-op if absent.

Parameter Type

session

Session

agentId

string

void

AgentStateController.clearAgent


getActiveAgent(session, fallbackAgentId): string;

Resolve the active agent ID, falling back to the provided default.

Parameter Type

session

Session

fallbackAgentId

string

string

AgentStateController.getActiveAgent


getAgentState<T>(session, agentId): T | undefined;

Read per-agent state with optional type assertion. Returns undefined if absent or empty.

Type Parameter Default type

T

Record<string, unknown>

Parameter Type

session

Session

agentId

string

T | undefined

AgentStateController.getAgentState


isCircularHandoff(
handoffStack,
agentId,
maxVisits?): boolean;

Check whether visiting the given agent would constitute a circular handoff. Returns true if the agent has been visited >= maxVisits times in the handoff stack.

Parameter Type Default value

handoffStack

string[]

undefined

agentId

string

undefined

maxVisits

number

2

boolean

AgentStateController.isCircularHandoff


recordHandoff(args): void;

Record a handoff in the session’s handoff history.

Parameter Type

args

{ fromAgentId: string; reason: string; session: Session; toAgentId: string; }

args.fromAgentId

string

args.reason

string

args.session

Session

args.toAgentId

string

void

AgentStateController.recordHandoff


setActiveAgent(session, agentId): void;

Set the active agent on the session.

Parameter Type

session

Session

agentId

string

void

AgentStateController.setActiveAgent


setAgentState(
session,
agentId,
state): void;

Replace per-agent state fully (no merge). Use when the state is a single opaque blob.

Parameter Type

session

Session

agentId

string

state

Record<string, unknown>

void

AgentStateController.setAgentState


updateAgentState(
session,
agentId,
state): void;

Update per-agent state stored in session.agentStates. Merges with existing state.

Parameter Type

session

Session

agentId

string

state

Record<string, unknown>

void

AgentStateController.updateAgentState