The type of the property being accessed. Can be any serializable type.
Creates a new instance of AgentStatePropertyAccessor.
The type of the property being accessed. Can be any serializable type.
The agent state object that manages the backing storage for this property
The unique name of the property within the state object. This name is used as the key in the state storage.
This constructor is typically not called directly. Instead, use AgentState.createProperty to create property accessors, which ensures proper integration with the state management system.
ReadonlynameThe unique name of the property within the state object. This name is used as the key in the state storage.
Protected ReadonlystateThe agent state object that manages the backing storage for this property
Deletes the property from the state storage.
The turn context for the current conversation turn
OptionalcustomKey: CustomKeyOptional custom key for accessing state in a specific storage location. Useful for multi-tenant scenarios or when state needs to be partitioned.
A promise that resolves when the delete operation is complete
This operation removes the property from the in-memory state object but does not
automatically persist the change to the underlying storage. You must call
state.saveChanges(context) afterwards to persist the deletion.
saveChanges() is calledget() calls will return undefined (or the default value if provided)const userSettings = userState.createProperty<UserSettings>("settings");
// Delete the user settings
await userSettings.delete(context);
// Persist the deletion to storage
await userState.saveChanges(context);
// Verify deletion
const settings = await userSettings.get(context); // Returns undefined
Retrieves the value of the property from state storage.
The turn context for the current conversation turn
OptionaldefaultValue: TOptional default value to use if the property doesn't exist. When provided, this value is deep cloned and stored in state.
OptionalcustomKey: CustomKeyOptional custom key for accessing state in a specific storage location. Useful for multi-tenant scenarios or when state needs to be partitioned.
A promise that resolves to the property value, the cloned default value, or undefined
This method provides intelligent default value handling:
undefined is returnedDeep Cloning: Default values are deep cloned using JSON serialization to prevent reference sharing issues. This means:
Performance: The first access loads state from storage; subsequent accesses use the in-memory cached version until the context is disposed.
const counterProperty = userState.createProperty<number>("counter");
// Get with default value
const count = await counterProperty.get(context, 0);
console.log(count); // 0 if property doesn't exist, otherwise the stored value
interface UserProfile {
name: string;
preferences: { theme: string; notifications: boolean };
}
const userProfile = userState.createProperty<UserProfile>("profile");
const profile = await userProfile.get(context, {
name: "Anonymous",
preferences: { theme: "light", notifications: true }
});
Sets the value of the property in state storage.
The turn context for the current conversation turn
The value to assign to the property. Can be any serializable value.
OptionalcustomKey: CustomKeyOptional custom key for accessing state in a specific storage location. Useful for multi-tenant scenarios or when state needs to be partitioned.
A promise that resolves when the set operation is complete
This operation updates the property in the in-memory state object but does not
automatically persist the change to the underlying storage. You must call
state.saveChanges(context) afterwards to persist the changes.
Memory vs Storage: Changes are immediately reflected in memory and will be
available to subsequent get() calls within the same context, but are not
persisted to storage until saveChanges() is called.
Value References: The exact value reference is stored (no cloning occurs). Ensure you don't modify objects after setting them unless you intend for those changes to be reflected in state.
Type Safety: When using TypeScript, the value must match the property's declared type parameter.
const counterProperty = userState.createProperty<number>("counter");
// Set a new value
await counterProperty.set(context, 42);
// Persist to storage
await userState.saveChanges(context);
const userProfile = userState.createProperty<UserProfile>("profile");
const newProfile: UserProfile = {
name: "John Doe",
preferences: { theme: "dark", notifications: false }
};
await userProfile.set(context, newProfile);
await userState.saveChanges(context);
Provides typed access to an Agent state property with automatic state loading and persistence management.
Remarks
AgentStatePropertyAccessorsimplifies working with persisted state by abstracting the complexity of loading state from storage and manipulating specific properties. It provides a type-safe interface for state management with automatic handling of:Key Features
Key features of
AgentStatePropertyAccessorinclude:Type Safety
The accessor provides compile-time type checking when using TypeScript:
Automatic Default Value Handling
When a property doesn't exist, default values are automatically cloned and stored:
Explicit Persistence Control
Changes are kept in memory until explicitly persisted:
Usage Examples
Example: Basic Usage
Example: Working with Primitive Types
Example: Conditional Logic
Example: Custom Storage Keys
Important Notes
state.saveChanges(context)to persist changes to storage.See