session

package
v0.93.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 3, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package session provides the session persistence service interface for AI agents. This interface is consumed by Team B (Assistant+Schedule).

Index

Constants

View Source
const (
	// DefaultRetentionDays is the default number of days to retain sessions.
	DefaultRetentionDays = 30
	// DefaultCleanupInterval is the default interval between cleanup runs.
	DefaultCleanupInterval = 24 * time.Hour
)
View Source
const (
	// MaxMessagesPerSession is the maximum number of messages to keep in a session.
	// This implements a sliding window to prevent unbounded growth.
	MaxMessagesPerSession = 20
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CleanupConfig

type CleanupConfig struct {
	RetentionDays   int           // Number of days to retain sessions (default: 30)
	CleanupInterval time.Duration // Interval between cleanup runs (default: 24h)
}

CleanupConfig holds configuration for the cleanup job.

func DefaultCleanupConfig

func DefaultCleanupConfig() CleanupConfig

DefaultCleanupConfig returns the default cleanup configuration.

type ConversationContext

type ConversationContext struct {
	Metadata  map[string]any `json:"metadata"`
	SessionID string         `json:"session_id"`
	AgentType string         `json:"agent_type"`
	Messages  []Message      `json:"messages"`
	CreatedAt int64          `json:"created_at"`
	UpdatedAt int64          `json:"updated_at"`
	UserID    int32          `json:"user_id"`
}

ConversationContext represents the conversation context.

type Message

type Message struct {
	Role    string `json:"role"` // "user" | "assistant" | "system"
	Content string `json:"content"`
}

Message represents a conversation message (reused from memory package).

type MockSessionService

type MockSessionService struct {
	// contains filtered or unexported fields
}

MockSessionService is a mock implementation of SessionService for testing.

func NewMockSessionService

func NewMockSessionService() *MockSessionService

NewMockSessionService creates a new MockSessionService with sample data.

func (*MockSessionService) CleanupExpired

func (m *MockSessionService) CleanupExpired(ctx context.Context, retentionDays int) (int64, error)

CleanupExpired removes sessions older than retentionDays.

func (*MockSessionService) Clear

func (m *MockSessionService) Clear()

Clear removes all sessions (for testing).

func (*MockSessionService) DeleteSession

func (m *MockSessionService) DeleteSession(ctx context.Context, sessionID string) error

DeleteSession deletes a session.

func (*MockSessionService) ListSessions

func (m *MockSessionService) ListSessions(ctx context.Context, userID int32, limit int) ([]SessionSummary, error)

ListSessions lists user sessions.

func (*MockSessionService) LoadContext

func (m *MockSessionService) LoadContext(ctx context.Context, sessionID string) (*ConversationContext, error)

LoadContext loads the conversation context.

func (*MockSessionService) SaveContext

func (m *MockSessionService) SaveContext(ctx context.Context, sessionID string, context *ConversationContext) error

SaveContext saves the conversation context.

func (*MockSessionService) SetSessionDirectly

func (m *MockSessionService) SetSessionDirectly(sessionID string, context *ConversationContext)

SetSessionDirectly sets a session directly without updating timestamps (for testing).

type SessionCleanupJob

type SessionCleanupJob struct {
	// contains filtered or unexported fields
}

SessionCleanupJob handles periodic cleanup of expired sessions.

func NewSessionCleanupJob

func NewSessionCleanupJob(svc SessionService, config CleanupConfig) *SessionCleanupJob

NewSessionCleanupJob creates a new cleanup job.

func (*SessionCleanupJob) IsRunning

func (j *SessionCleanupJob) IsRunning() bool

IsRunning returns whether the cleanup job is currently running.

func (*SessionCleanupJob) RunOnce

func (j *SessionCleanupJob) RunOnce(ctx context.Context) (int64, error)

RunOnce executes a single cleanup run immediately. Useful for testing or manual cleanup.

func (*SessionCleanupJob) Start

func (j *SessionCleanupJob) Start(ctx context.Context) error

Start begins the periodic cleanup job. This method is non-blocking and starts the cleanup in a goroutine.

func (*SessionCleanupJob) Stop

func (j *SessionCleanupJob) Stop()

Stop stops the cleanup job.

type SessionRecovery

type SessionRecovery struct {
	// contains filtered or unexported fields
}

SessionRecovery handles session recovery and message management.

func NewSessionRecovery

func NewSessionRecovery(sessionSvc SessionService) *SessionRecovery

NewSessionRecovery creates a new session recovery handler.

func (*SessionRecovery) AppendMessage

func (r *SessionRecovery) AppendMessage(ctx context.Context, sessionID string, msg *Message) error

AppendMessage adds a message to the session and saves it. Implements sliding window to keep only the last MaxMessagesPerSession messages.

func (*SessionRecovery) AppendTurn

func (r *SessionRecovery) AppendTurn(ctx context.Context, sessionID string, userMsg, assistantMsg string, agentType string) error

AppendTurn adds a user-assistant turn to the session. This is a convenience method for the common case of adding both messages.

func (*SessionRecovery) ClearMessages

func (r *SessionRecovery) ClearMessages(ctx context.Context, sessionID string) error

ClearMessages clears all messages from the session while preserving metadata.

func (*SessionRecovery) GetRecentMessages

func (r *SessionRecovery) GetRecentMessages(ctx context.Context, sessionID string, n int) ([]Message, error)

GetRecentMessages returns the last N messages from the session.

func (*SessionRecovery) RecoverSession

func (r *SessionRecovery) RecoverSession(ctx context.Context, sessionID string, userID int32) (*ConversationContext, error)

RecoverSession recovers or creates a session for the given user. If the session exists, it returns the existing context. If not, it creates a new empty context.

func (*SessionRecovery) UpdateMetadata

func (r *SessionRecovery) UpdateMetadata(ctx context.Context, sessionID string, key string, value any) error

UpdateMetadata updates session metadata without modifying messages.

type SessionService

type SessionService interface {
	// SaveContext saves the conversation context.
	SaveContext(ctx context.Context, sessionID string, context *ConversationContext) error

	// LoadContext loads the conversation context.
	LoadContext(ctx context.Context, sessionID string) (*ConversationContext, error)

	// ListSessions lists user sessions.
	ListSessions(ctx context.Context, userID int32, limit int) ([]SessionSummary, error)

	// DeleteSession deletes a session (user privacy control).
	DeleteSession(ctx context.Context, sessionID string) error

	// CleanupExpired removes sessions older than retentionDays.
	// Returns the number of deleted sessions.
	CleanupExpired(ctx context.Context, retentionDays int) (int64, error)
}

Consumers: Team B (Assistant+Schedule).

func NewSessionStore

func NewSessionStore(db *sql.DB, cache cache.CacheService) SessionService

NewSessionStore creates a new session store with database and cache.

type SessionSummary

type SessionSummary struct {
	SessionID   string `json:"session_id"`
	AgentType   string `json:"agent_type"`
	LastMessage string `json:"last_message"`
	UpdatedAt   int64  `json:"updated_at"`
}

SessionSummary represents a session summary.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL