Skip to main content
A conversation handle with one active query at a time. Owns its local control dispatcher and SDK-hosted MCP resources, while persisted server state survives normal close.

Example

Constructors

Constructor

Parameters

ctx
SessionContext

Returns

Session

Properties

directory


id


workspaceId?

Accessors

usage

Get Signature

Live cost/usage accumulated on this session handle. Grows while stream() / send() / command() consume events and reconciles with each turn’s authoritative result (which includes subagent child-session cost). Attached/resumed handles start at zero. Returns a defensive snapshot.
Returns
SessionUsage

Methods

abort()

Alias of interrupt for cancelling the active turn.

Returns

Promise<void> Completion of the abort request.

Throws

Transport errors.

Example


checkpoint()

Capture workspace file state at the current transcript boundary.

Parameters

label?
string Optional human-readable label.

Returns

Promise<CheckpointHandle> A session-owned checkpoint handle.

Throws

Snapshot or transport errors.

Example


close()

Release everything this handle owns, in a deterministic order. In-flight per-call work (stream/command/compact/todos) is torn down first, then the control dispatcher, then SDK-hosted MCP servers. The server-side session is not deleted — reattach later with sessions.resume(id). Bounded by construction, because close() is what you call when you want to stop waiting: a consumer-owned generator’s finally gets a short grace period, and an abandoned turn’s abort request is awaited for at most about a second so an unreachable server cannot wedge teardown. Against a responsive server it settles in well under 100 ms. Idempotent: concurrent and repeated calls share one teardown.

Returns

Promise<void> A promise that settles once every owned resource is handled.

Throws

When one or more teardown stages fail; several are combined in an AggregateError.

Example


command()

Execute a registered slash command in this conversation.

Parameters

name
string
args?
string = ""
options?
Command name, arguments and optional execution overrides.
agent?
string
model?
ModelId
variant?
string

Returns

Promise<ResultMessage> The terminal command result.

Throws

If the command is unavailable or another query is active.

Example


compact()

Summarize conversation history and return the committed compaction boundary.

Parameters

options?
Optional model and additional instructions for the summary.
customInstructions?
string
model?
ModelId

Returns

Promise<CompactResult> Compaction identity, preserved messages, token count, and summary text.

getAppliedSettings()

Read the effective runtime policy and registered capabilities.

Returns

Promise<AppliedSettings> A normalized settings snapshot without callback implementations.

Throws

Transport errors.

Example


getTodos()

Read the current persisted todo list.

Returns

Promise<Todo[]> Todo entries in runtime order.

Throws

Transport errors.

Example


interrupt()

Interrupt the active turn and its pending callback work.

Returns

Promise<void> Completion of the abort request.

Throws

Transport errors.

Example


listCheckpoints()

List checkpoint handles owned by this session.

Returns

Promise<CheckpointHandle[]> Persisted checkpoint metadata.

Throws

Transport errors.

Example


messages()

Read persisted conversation messages normalized to the SDK message union.

Returns

Promise<AgentMessage[]> Stored transcript messages, including supported tool and attachment parts.

Throws

Transport errors.

Example


rename()

Update the persisted conversation title.

Parameters

title
string New title.

Returns

Promise<void> Completion after metadata is updated.

Throws

Transport errors.

Example


rewind()

Restore eligible workspace files to a session checkpoint. The transcript is not rolled back; external side effects are not undone.

Parameters

checkpointId
string Checkpoint owned by this session.

Returns

Promise<RewindResult> The affected files and checkpoint identity.

Throws

Invalid checkpoint, restore or transport errors.

Example


send()

Submit input and wait for its terminal result.

Parameters

prompt
PromptInput Text, typed turn, or asynchronous turns.

Returns

Promise<ResultMessage> The completed result; inspect subtype before using output.

Throws

If closed, another query is active, or transport fails.

Example


setAgents()

Replace the complete session-scoped agent definition map.

Parameters

agents
Record<string, AgentDefinition> Every agent definition that should remain available.

Returns

Promise<void> Completion after the replacement is accepted.

Throws

Invalid definitions or transport errors.

Example


setModel()

Select the model for subsequent turns.

Parameters

model
ModelId Provider/model identifier.

Returns

Promise<void> Completion after runtime configuration is updated.

Throws

Invalid model identifiers or transport errors.

Example


setPermissionMode()

Change authorization mode for subsequent session work.

Parameters

_mode
PermissionMode New runtime permission mode.

Returns

Promise<void> Completion after the runtime accepts the policy.

Throws

Invalid mode or transport errors.

Example


stream()

Submit input and yield normalized messages until the terminal result.

Parameters

prompt
PromptInput Text, typed turn, or asynchronous turns.
options?
StreamOptions Partial-message and cancellation settings.

Returns

AsyncGenerator<AgentMessage, ResultMessage> An async generator of messages with a terminal return value.

Throws

Closed/conflicting session or transport failures.

Example


tag()

Add a tag without duplicating existing tags.

Parameters

tag
string Tag text, trimmed by the SDK.

Returns

Promise<void> Completion after tags are updated.

Throws

Transport errors.

Example


todos()

Watch current todo snapshots until cancellation or stream closure.

Parameters

options?
Optional abort signal.
signal?
AbortSignal

Returns

AsyncGenerator<Todo[]> An async generator of todo lists.

Throws

Transport errors.

Example


untag()

Remove a tag while retaining the others.

Parameters

tag
string Tag to remove.

Returns

Promise<void> Completion after tags are updated.

Throws

Transport errors.

Example