withPersistence

suspend fun <T> AIAgentContext.withPersistence(action: suspend Persistence.(AIAgentContext) -> T): T(source)

Executes the provided action within the context of the AI agent's persistence layer.

This function enhances agents with persistent state management capabilities by leveraging the [Persistence component within the current AIAgentContext. The supplied action is executed with the persistence layer, enabling operations that require consistent and reliable state management across the lifecycle of the agent.

Return

A result of type T produced by the execution of the provided action.

Parameters

action

A suspendable lambda function that receives the Persistence instance and the current AIAgentContext as its parameters. This allows custom logic that interacts with the persistence layer to be executed.


suspend fun <T> StatefulSingleUseAIAgent<*, *, *>.withPersistence(action: suspend Persistence.(AIAgentContext) -> T): T(source)

Executes the provided action within the context of the agent's persistence layer if the agent is in a running state.

This function allows interaction with the persistence mechanism associated with the agent, ensuring that the operation is carried out in the correct execution context.

Return

The result of the execution of the provided action.

Parameters

action

A suspending function defining operations to perform using the agent's persistence mechanism and the current agent context.

Throws

If the agent is not in a running state when this function is called.