session.go

Overview

The session.go file defines core interfaces and data structures for managing user-agent interaction sessions within the system. A session represents a continuous conversation or interaction thread between a user and one or more agents, encapsulating the conversation history, session state, and metadata. This file provides abstractions for session identity, persistent session state as a key-value store, and the sequence of interaction events that make up the conversation.

It serves as the foundation for tracking conversation progress, storing user or app-specific state, and managing event histories such as user inputs, agent responses, and function call activities. The interfaces and types defined here are designed to be implemented by different storage backends, enabling flexible and pluggable session persistence solutions.

This file is a key component of the Session Management topic, where session lifecycle, state management, and event history are handled, supporting workflows involving agents, LLM responses, and function executions.


Key Interfaces and Types

Session Interface

Represents a single user-agent interaction thread. It provides accessors for session metadata, state, and event history.

Usage Example:

var sess Session
sessionID := sess.ID()
userID := sess.UserID()
state := sess.State()
events := sess.Events()

State Interface

Defines a mutable key-value store used to store session state data scoped to the session.

The state keys typically use prefixes such as app:, user:, or temp: (defined as constants) to specify scope.

ReadonlyState Interface

A read-only variant of the State interface, exposing only Get and All methods. Useful for contexts where mutation is disallowed.

Events Interface

Represents an ordered list of Event objects that track the sequence of interactions in a session.

Event Struct

Encapsulates an individual interaction or message in the session conversation. It embeds model.LLMResponse to include LLM-generated content.

EventActions Struct

Describes side effects or control actions attached to an event.


Constants

Defines key prefixes for scoping session state keys:


Errors


Helper Functions (Internal)

These functions inspect an LLMResponse embedded in an event to detect special content types:

These checks are used mainly in Event.IsFinalResponse() to determine whether the event marks a final agent output or if further processing is expected.


Interactions with Other System Components


Mermaid Class Diagram

classDiagram
class Session {
+ID() string
+AppName() string
+UserID() string
+State() State
+Events() Events
+LastUpdateTime() time.Time
}
class State {
+Get(string) (any, error)
+Set(string, any) error
+All() iter.Seq2
}
class ReadonlyState {
+Get(string) (any, error)
+All() iter.Seq2
}
class Events {
+All() iter.Seq
+Len() int
+At(int) *Event
}
class Event {
+ID string
+Timestamp time.Time
+InvocationID string
+Branch string
+Author string
+Actions EventActions
+LongRunningToolIDs []string
+IsFinalResponse() bool
}
class EventActions {
+StateDelta map[string]any
+ArtifactDelta map[string]int64
+SkipSummarization bool
+TransferToAgent string
+Escalate bool
}
Session --> State
Session --> Events
Events --> Event
Event --> EventActions
State <|-- ReadonlyState

Detailed Explanation of Important Implementation Details

Session State and Scoping

The session state is designed as a generic key-value store accessible via the State interface. Keys are prefixed to indicate their scope:

This scoping mechanism allows fine-grained control over state visibility and lifecycle, critical for supporting features like multi-user applications, session persistence, and temporary invocation data.

Event Model and Finality Detection

Each Event embeds an LLM response and metadata about the conversation turn or action. The IsFinalResponse method uses a combination of heuristics to determine if the event represents a completed agent response:

This logic ensures that downstream components can recognize when an agent's turn is complete and ready for user consumption or further processing.

Use of UUIDs and Timestamps

Events are assigned unique UUIDs at creation (NewEvent), ensuring traceability and uniqueness in distributed environments. Timestamps are recorded using time.Now() to mark event occurrence, important for ordering and auditing.


Summary of File Role in the System

The session.go file establishes the foundational abstractions for session identity, state management, and event tracking in user-agent conversations. It plays a critical role in the lifecycle of agent interactions by enabling persistent, scoped state management and detailed event histories. The abstractions here allow the rest of the system to build sophisticated agent workflows, stateful memory, and event-driven logic over a consistent and extensible session model.

This file's constructs are utilized by other components such as agents (LLM Integration and Agents), session service implementations (In-Memory Session Service, Database Session Service), and agent runners (Agent Execution Runner) to support complex conversational AI scenarios.