storage_session.go

Overview

The storage_session.go file defines the database schema representations and conversion utilities for persisting session and event data in a relational database using GORM ORM. It is part of the Database Session Service which implements the durable storage backend for session management within the system.

This file contains Go struct definitions mapping to the sessions, events, app_states, and user_states tables, with primary keys, foreign key relations, and JSON serialization of complex fields. It also provides helper functions to convert between internal application-level session and event models and their corresponding database storage representations.

By encapsulating the storage models and conversion logic, this file enables consistent and transactional persistence of session state and event histories, facilitating multi-turn user-agent interactions with durable state and event tracking.


Structs and Their Usage

storageSession

Represents a row in the sessions table.


storageEvent

Represents a row in the events table.


storageAppState

Represents a row in the app_states table for storing application-scoped state.


storageUserState

Represents a row in the user_states table for storing user-scoped state.


Functions and Methods

createStorageSession(s *localSession) (*storageSession, error)

Parameters:

Returns:

Usage Example:

storageSess, err := createStorageSession(localSess)
if err != nil {
    // handle error
}

createSessionFromStorageSession(storage *storageSession) (*localSession, error)

Parameters:

Returns:


createStorageEvent(session session.Session, event *session.Event) (*storageEvent, error)

Parameters:

Returns:

Important Implementation Details:


derefOrZero[T any](p *T) T

Parameters:

Returns:


createEventFromStorageEvent(se *storageEvent) (*session.Event, error)

Parameters:

Returns:

Usage Notes:


Implementation Details and Algorithms


Interaction with Other Components


Visual Diagram: Class Diagram of Main Structs and Relationships

classDiagram
class storageSession {
+string AppName
+string UserID
+string ID
+stateMap State
+time.Time CreateTime
+time.Time UpdateTime
+[]storageEvent Events
+TableName()
}
class storageEvent {
+string ID
+string AppName
+string UserID
+string SessionID
+string InvocationID
+string Author
+[]byte Actions
+dynamicJSON LongRunningToolIDsJSON
+*string Branch
+time.Time Timestamp
+dynamicJSON Content
+dynamicJSON GroundingMetadata
+dynamicJSON CustomMetadata
+dynamicJSON UsageMetadata
+dynamicJSON CitationMetadata
+*bool Partial
+*bool TurnComplete
+*string ErrorCode
+*string ErrorMessage
+*bool Interrupted
+TableName()
}
class storageAppState {
+string AppName
+stateMap State
+time.Time UpdateTime
+TableName()
}
class storageUserState {
+string AppName
+string UserID
+stateMap State
+time.Time UpdateTime
+TableName()
}
storageSession "1" -- "many" storageEvent : has many
storageEvent "many" -- "1" storageSession : belongs to

Summary of Key Points

For detailed usage, lifecycle, and transactional workflows involving these models, see the Database Session Service and Session Management topics.