Documentation
¶
Overview ¶
Package session provides the session persistence service interface for AI agents. This interface is consumed by Team B (Assistant+Schedule).
Index ¶
- Constants
- type CleanupConfig
- type ConversationContext
- type Message
- type MockSessionService
- func (m *MockSessionService) CleanupExpired(ctx context.Context, retentionDays int) (int64, error)
- func (m *MockSessionService) Clear()
- func (m *MockSessionService) DeleteSession(ctx context.Context, sessionID string) error
- func (m *MockSessionService) ListSessions(ctx context.Context, userID int32, limit int) ([]SessionSummary, error)
- func (m *MockSessionService) LoadContext(ctx context.Context, sessionID string) (*ConversationContext, error)
- func (m *MockSessionService) SaveContext(ctx context.Context, sessionID string, context *ConversationContext) error
- func (m *MockSessionService) SetSessionDirectly(sessionID string, context *ConversationContext)
- type SessionCleanupJob
- type SessionRecovery
- func (r *SessionRecovery) AppendMessage(ctx context.Context, sessionID string, msg *Message) error
- func (r *SessionRecovery) AppendTurn(ctx context.Context, sessionID string, userMsg, assistantMsg string, ...) error
- func (r *SessionRecovery) ClearMessages(ctx context.Context, sessionID string) error
- func (r *SessionRecovery) GetRecentMessages(ctx context.Context, sessionID string, n int) ([]Message, error)
- func (r *SessionRecovery) RecoverSession(ctx context.Context, sessionID string, userID int32) (*ConversationContext, error)
- func (r *SessionRecovery) UpdateMetadata(ctx context.Context, sessionID string, key string, value any) error
- type SessionService
- type SessionSummary
Constants ¶
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 )
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 {
SessionID string `json:"session_id"`
UserID int32 `json:"user_id"`
AgentType string `json:"agent_type"`
Messages []Message `json:"messages"`
Metadata map[string]any `json:"metadata"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}
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 ¶
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.
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 ¶
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)
}
SessionService defines the session persistence service interface. 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.