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.
Example
Section titled “Example”const state = new AgentState({ userId: 'user-123' })state.set('sessionId', 'session-456')const userId = state.get('userId') // 'user-123'Methods
Section titled “Methods”Call Signature
Section titled “Call Signature”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 Parameters
Section titled “Type Parameters”| Type Parameter | Default type | Description |
|---|---|---|
TState | - | The complete state interface type |
K extends string | number | symbol | keyof TState | The property key (inferred from argument) |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
key | K | Key to retrieve specific value |
Returns
Section titled “Returns”TState[K]
The value for the key, or undefined if key doesn’t exist
Example
Section titled “Example”// Typed usageconst user = state.get<AppState>('user') // { name: string; age: number } | undefined
// Untyped usageconst value = state.get('someKey') // JSONValue | undefinedCall Signature
Section titled “Call Signature”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.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
key | string | Key to retrieve specific value |
Returns
Section titled “Returns”The value for the key, or undefined if key doesn’t exist
Example
Section titled “Example”// Typed usageconst user = state.get<AppState>('user') // { name: string; age: number } | undefined
// Untyped usageconst value = state.get('someKey') // JSONValue | undefinedCall Signature
Section titled “Call Signature”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 Parameters
Section titled “Type Parameters”| Type Parameter | Default type | Description |
|---|---|---|
TState | - | The complete state interface type |
K extends string | number | symbol | keyof TState | The property key (inferred from argument) |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
key | K | The key to set |
value | TState[K] | The value to store (must be JSON serializable) |
Returns
Section titled “Returns”void
Throws
Section titled “Throws”Error if value is not JSON serializable
Example
Section titled “Example”// Typed usagestate.set<AppState>('user', { name: 'Alice', age: 25 })
// Untyped usagestate.set('someKey', { any: 'value' })Call Signature
Section titled “Call Signature”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.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
key | string | The key to set |
value | unknown | The value to store (must be JSON serializable) |
Returns
Section titled “Returns”void
Throws
Section titled “Throws”Error if value is not JSON serializable
Example
Section titled “Example”// Typed usagestate.set<AppState>('user', { name: 'Alice', age: 25 })
// Untyped usagestate.set('someKey', { any: 'value' })delete()
Section titled “delete()”Call Signature
Section titled “Call Signature”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 Parameters
Section titled “Type Parameters”| Type Parameter | Default type | Description |
|---|---|---|
TState | - | The complete state interface type |
K extends string | number | symbol | keyof TState | The property key (inferred from argument) |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
key | K | The key to delete |
Returns
Section titled “Returns”void
Example
Section titled “Example”// Typed usagestate.delete<AppState>('user')
// Untyped usagestate.delete('someKey')Call Signature
Section titled “Call Signature”delete(key): void;Defined in: src/agent/state.ts:111
Delete a state value by key with optional type-safe property validation.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
key | string | The key to delete |
Returns
Section titled “Returns”void
Example
Section titled “Example”// Typed usagestate.delete<AppState>('user')
// Untyped usagestate.delete('someKey')clear()
Section titled “clear()”clear(): void;Defined in: src/agent/state.ts:119
Clear all state values.
Returns
Section titled “Returns”void
getAll()
Section titled “getAll()”getAll(): Record<string, JSONValue>;Defined in: src/agent/state.ts:128
Get a copy of all state as an object.
Returns
Section titled “Returns”Record<string, JSONValue>
Deep copy of all state
keys()
Section titled “keys()”keys(): string[];Defined in: src/agent/state.ts:137
Get all state keys.
Returns
Section titled “Returns”string[]
Array of state keys