Skip to content

AgentState

Defined in: src/agent/state.ts:18

Agent state provides key-value storage outside conversation context. State is not passed to the model during inference but is accessible by tools (via ToolContext) and application logic.

All values are deep copied on get/set operations to prevent reference mutations. Values must be JSON serializable.

const state = new AgentState({ userId: 'user-123' })
state.set('sessionId', 'session-456')
const userId = state.get('userId') // 'user-123'
get<TState, K>(key): TState[K];

Defined in: src/agent/state.ts:53

Get a state value by key with optional type-safe property lookup. Returns a deep copy to prevent mutations.

Type ParameterDefault typeDescription
TState-The complete state interface type
K extends string | number | symbolkeyof TStateThe property key (inferred from argument)
ParameterTypeDescription
keyKKey to retrieve specific value

TState[K]

The value for the key, or undefined if key doesn’t exist

// Typed usage
const user = state.get<AppState>('user') // { name: string; age: number } | undefined
// Untyped usage
const value = state.get('someKey') // JSONValue | undefined
get(key): JSONValue;

Defined in: src/agent/state.ts:54

Get a state value by key with optional type-safe property lookup. Returns a deep copy to prevent mutations.

ParameterTypeDescription
keystringKey to retrieve specific value

JSONValue

The value for the key, or undefined if key doesn’t exist

// Typed usage
const user = state.get<AppState>('user') // { name: string; age: number } | undefined
// Untyped usage
const value = state.get('someKey') // JSONValue | undefined

set<TState, K>(key, value): void;

Defined in: src/agent/state.ts:88

Set a state value with optional type-safe property validation. Validates JSON serializability and stores a deep copy.

Type ParameterDefault typeDescription
TState-The complete state interface type
K extends string | number | symbolkeyof TStateThe property key (inferred from argument)
ParameterTypeDescription
keyKThe key to set
valueTState[K]The value to store (must be JSON serializable)

void

Error if value is not JSON serializable

// Typed usage
state.set<AppState>('user', { name: 'Alice', age: 25 })
// Untyped usage
state.set('someKey', { any: 'value' })
set(key, value): void;

Defined in: src/agent/state.ts:89

Set a state value with optional type-safe property validation. Validates JSON serializability and stores a deep copy.

ParameterTypeDescription
keystringThe key to set
valueunknownThe value to store (must be JSON serializable)

void

Error if value is not JSON serializable

// Typed usage
state.set<AppState>('user', { name: 'Alice', age: 25 })
// Untyped usage
state.set('someKey', { any: 'value' })

delete<TState, K>(key): void;

Defined in: src/agent/state.ts:110

Delete a state value by key with optional type-safe property validation.

Type ParameterDefault typeDescription
TState-The complete state interface type
K extends string | number | symbolkeyof TStateThe property key (inferred from argument)
ParameterTypeDescription
keyKThe key to delete

void

// Typed usage
state.delete<AppState>('user')
// Untyped usage
state.delete('someKey')
delete(key): void;

Defined in: src/agent/state.ts:111

Delete a state value by key with optional type-safe property validation.

ParameterTypeDescription
keystringThe key to delete

void

// Typed usage
state.delete<AppState>('user')
// Untyped usage
state.delete('someKey')

clear(): void;

Defined in: src/agent/state.ts:119

Clear all state values.

void


getAll(): Record<string, JSONValue>;

Defined in: src/agent/state.ts:128

Get a copy of all state as an object.

Record<string, JSONValue>

Deep copy of all state


keys(): string[];

Defined in: src/agent/state.ts:137

Get all state keys.

string[]

Array of state keys