Sessions

Sessions represent conversations with an agent. They store conversation history, track resources and variables, and enable stateful interactions.

Creating Sessions

Create a session by specifying the agent ID and initial input variables:

typescript

Getting Session Messages

To restore a conversation on page load, use getMessages() to retrieve UI-ready messages:

typescript

The returned messages can be passed directly to the client SDK's initialMessages option.

UISessionState Interface

typescript

Full Session State (Debug)

For debugging or internal use, you can retrieve the complete session state including all variables and internal message format:

typescript

Note: Use getMessages() for client-facing code. The get() method returns internal message format that includes hidden content not intended for end users.

Attaching to Sessions

To trigger actions on a session, you need to attach to it first:

typescript

Triggering Actions

Once attached, trigger actions on the session:

typescript

Session Lifecycle

Rendering diagram...

Restoring Sessions

When a user returns to your app, restore their session:

typescript

The messages array contains UIMessage objects with properly structured parts, enabling direct use with the client SDK's initialMessages option.

Error Handling

typescript