Skip to content

TurnContext

Context available during the turn hook.

Extends SessionContext with turn-specific data: input messages, output, turn ID, and the abort() method for hard-stopping.

agent: object

Agent definition: name, model, instructions.

instructions: string

model: string

name: string

SessionContext.agent


config: Record<string, unknown>

Middleware-specific configuration from the agent definition.

SessionContext.config


history: Message[]

Canonical conversation history (append-only).

SessionContext.history


input: Message[]

Input messages for this turn.


output: string | null

Assistant’s final text output for this turn. null until the turn completes.


sessionId: string

Unique session identifier.

SessionContext.sessionId


startedAt: number

Timestamp when this turn started.


state: Record<string, unknown>

Session state — typed fields with optional reducers, shared across all turns.

SessionContext.state


turnId: string

Unique turn identifier.


turnIndex: number

Turn number within this session (0-based).

abort(reason): never

Hard-stop the turn. Throws AbortError that unwinds the entire onion stack.

string

never


emit(event): void

Emit a stream event to the consumer.

StreamEvent

void

SessionContext.emit


registerTool(tool): void

Register a tool on the agent. Call in the agent hook before next().

Tool

void

SessionContext.registerTool