session.go

Overview

The session.go file defines core data structures and helper functions for managing an agent's session within the system. A session encapsulates the interaction state of an agent with a user, including session metadata, persistent state, and event history. This file primarily focuses on the representation, validation, and transformation of session data between internal models and external session entities.

This file supports the session lifecycle by enabling:

It plays a key role in the Session Management topic by providing the foundational data structures and conversion logic used throughout the session handling and persistence layers.


Data Structures

Session

type Session struct {
    ID        string         `json:"id"`
    AppName   string         `json:"appName"`
    UserID    string         `json:"userId"`
    UpdatedAt int64          `json:"lastUpdateTime"`
    Events    []Event        `json:"events"`
    State     map[string]any `json:"state"`
}

Represents the state of an agent's session with the following fields:

This struct is the core model for session data passed between internal components and external APIs.


CreateSessionRequest

type CreateSessionRequest struct {
    State  map[string]any `json:"state"`
    Events []Event        `json:"events"`
}

Defines the payload structure for creating a new session. It includes:

This type facilitates session creation requests likely used by REST handlers or service layers.


SessionID

type SessionID struct {
    ID      string `mapstructure:"session_id,optional"`
    AppName string `mapstructure:"app_name,required"`
    UserID  string `mapstructure:"user_id,required"`
}

Encapsulates the identifying parameters of a session. Fields:

Used primarily for decoding and validating session identification data from HTTP route variables or other map-based data sources.


Functions and Methods

SessionIDFromHTTPParameters

func SessionIDFromHTTPParameters(vars map[string]string) (SessionID, error)

Parses and validates session identification parameters from a map of HTTP variables.

Usage Example:

vars := map[string]string{
    "app_name": "chatbot",
    "user_id": "user123",
}
sessionID, err := SessionIDFromHTTPParameters(vars)
if err != nil {
    // handle error
}

Details:


FromSession

func FromSession(session session.Session) (Session, error)

Converts an external session.Session interface object to an internal Session model.

Implementation Details:

This function bridges the external session abstraction with this package's internal representation critical for storage, manipulation, or API response.


(Session) Validate

func (s Session) Validate() error

Validates the integrity and completeness of a Session instance.

Validation rules:

This method ensures that session data conforms to expected invariants before further processing or storage.


Important Implementation Details


Interactions with Other Components


Diagram: Session Structure and Function Relationships

classDiagram
class Session {
+string ID
+string AppName
+string UserID
+int64 UpdatedAt
+[]Event Events
+map[string]any State
+Validate() error
}
class CreateSessionRequest {
+map[string]any State
+[]Event Events
}
class SessionID {
+string ID
+string AppName
+string UserID
}
SessionID ..> SessionIDFromHTTPParameters
SessionIDFromHTTPParameters : returns SessionID, error
Session ..> FromSession
FromSession : returns Session, error

This diagram illustrates the primary data structures (Session, CreateSessionRequest, SessionID) and their associated key functions (SessionIDFromHTTPParameters, FromSession, and Validate). It highlights relationships such as conversion and validation centered around the Session struct.


This file forms a critical model and utility layer for managing agent sessions, validating their state, and adapting external session objects into the internal domain used across the agent framework. It directly supports the session lifecycle by defining core session data and ensuring its correctness.