AppState
Defined in: src/app-state.ts:19
App 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 AppState({ userId: 'user-123' })state.set('sessionId', 'session-456')const userId = state.get('userId') // 'user-123'Implements
Section titled “Implements”StateSerializable
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new AppState(initialState?): AppState;Defined in: src/app-state.ts:28
Creates a new AppState instance.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
initialState? | Record<string, JSONValue> | Optional initial state values |
Returns
Section titled “Returns”AppState
Throws
Section titled “Throws”Error if initialState is not JSON serializable
Methods
Section titled “Methods”Call Signature
Section titled “Call Signature”get<TState, K>(key): TState[K];Defined in: src/app-state.ts:54
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/app-state.ts:55
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/app-state.ts:89
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/app-state.ts:90
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/app-state.ts:111
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/app-state.ts:112
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/app-state.ts:120
Clear all state values.
Returns
Section titled “Returns”void
getAll()
Section titled “getAll()”getAll(): Record<string, JSONValue>;Defined in: src/app-state.ts:129
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/app-state.ts:138
Get all state keys.
Returns
Section titled “Returns”string[]
Array of state keys
toJSON()
Section titled “toJSON()”toJSON(): JSONValue;Defined in: src/app-state.ts:147
Returns the serialized state as JSON value.
Returns
Section titled “Returns”Deep copy of all state
Implementation of
Section titled “Implementation of”StateSerializable.toJSONloadStateFromJson()
Section titled “loadStateFromJson()”loadStateFromJson(json): void;Defined in: src/app-state.ts:156
Loads state from a previously serialized JSON value.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
json | JSONValue | The serialized state to load |
Returns
Section titled “Returns”void
Implementation of
Section titled “Implementation of”StateSerializable.loadStateFromJson