Share via


UserState class

Manages the state of a user.

Extends

Constructors

UserState(Storage, string)

Creates a new instance of UserState.

Inherited Methods

clear(TurnContext)

Clears the state by setting it to an empty object in the turn context. Note: This does not remove the state from storage, it only clears the in-memory representation. Call saveChanges() after this to persist the empty state to storage.

createProperty<T>(string)

Creates a property accessor for the specified property. Property accessors provide typed access to properties within the state object.

delete(TurnContext, CustomKey)

Deletes the state from both the turn context and storage.

get(TurnContext)

Gets the state from the turn context without loading it from storage.

load(TurnContext, boolean, CustomKey)

Loads the state from storage into the turn context. If state is already cached in the turn context and force is not set, the cached version will be used.

saveChanges(TurnContext, boolean, CustomKey)

Saves the state to storage if it has changed since it was loaded. Change detection uses a hash of the state object to determine if saving is necessary.

Constructor Details

UserState(Storage, string)

Creates a new instance of UserState.

new UserState(storage: Storage, namespace?: string)

Parameters

storage
Storage

The storage provider.

namespace

string

Inherited Method Details

clear(TurnContext)

Clears the state by setting it to an empty object in the turn context. Note: This does not remove the state from storage, it only clears the in-memory representation. Call saveChanges() after this to persist the empty state to storage.

function clear(context: TurnContext): Promise<void>

Parameters

context
TurnContext

The turn context containing the state to clear

Returns

Promise<void>

A promise that resolves when the clear operation is complete

Inherited From AgentState.clear

createProperty<T>(string)

Creates a property accessor for the specified property. Property accessors provide typed access to properties within the state object.

function createProperty<T>(name: string): AgentStatePropertyAccessor<T>

Parameters

name

string

The name of the property to access

Returns

A property accessor for the specified property

Inherited From AgentState.createProperty

delete(TurnContext, CustomKey)

Deletes the state from both the turn context and storage.

function delete(context: TurnContext, customKey?: CustomKey): Promise<void>

Parameters

context
TurnContext

The turn context containing the state to delete

customKey
CustomKey

Optional custom storage key to use instead of the default

Returns

Promise<void>

A promise that resolves when the delete operation is complete

Inherited From AgentState.delete

get(TurnContext)

Gets the state from the turn context without loading it from storage.

function get(context: TurnContext): any

Parameters

context
TurnContext

The turn context containing the state to get

Returns

any

The state object, or undefined if no state is found in the turn context

Inherited From AgentState.get

load(TurnContext, boolean, CustomKey)

Loads the state from storage into the turn context. If state is already cached in the turn context and force is not set, the cached version will be used.

function load(context: TurnContext, force?: boolean, customKey?: CustomKey): Promise<any>

Parameters

context
TurnContext

The turn context to load state into

force

boolean

If true, forces a reload from storage even if state is cached

customKey
CustomKey

Optional custom storage key to use instead of the default

Returns

Promise<any>

A promise that resolves to the loaded state object

Inherited From AgentState.load

saveChanges(TurnContext, boolean, CustomKey)

Saves the state to storage if it has changed since it was loaded. Change detection uses a hash of the state object to determine if saving is necessary.

function saveChanges(context: TurnContext, force?: boolean, customKey?: CustomKey): Promise<void>

Parameters

context
TurnContext

The turn context containing the state to save

force

boolean

If true, forces a save to storage even if no changes are detected

customKey
CustomKey

Optional custom storage key to use instead of the default

Returns

Promise<void>

A promise that resolves when the save operation is complete

Inherited From AgentState.saveChanges