Documentation
¶
Overview ¶
Package session provides in-memory session persistence with timeout-based expiry, three-strike escalation, and per-session cost tracking.
Index ¶
- func DeriveSessionID(firstUserMessage string) string
- func GetSessionID(header http.Header, headerName string) string
- func HashRequestContent(content string, toolNames []string) string
- type Config
- type Entry
- type SessionInfo
- type Stats
- type Store
- func (s *Store) AddSessionCost(sessionID string, micros int64)
- func (s *Store) ClearAll()
- func (s *Store) ClearSession(sessionID string)
- func (s *Store) Close()
- func (s *Store) EscalateSession(sessionID string)
- func (s *Store) GetSession(sessionID string) *Entry
- func (s *Store) GetSessionCostUSD(sessionID string) float64
- func (s *Store) GetStats() Stats
- func (s *Store) RecordRequestHash(sessionID, hash string) int
- func (s *Store) SetSession(sessionID, model string, tier router.Tier, userExplicit bool)
- func (s *Store) TouchSession(sessionID string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeriveSessionID ¶
DeriveSessionID derives a session ID from the first user message via SHA-256.
func GetSessionID ¶
GetSessionID extracts a session ID from HTTP headers.
func HashRequestContent ¶
HashRequestContent produces a short hash from request content. It normalizes whitespace, truncates to 500 chars, and includes tool names.
Types ¶
type Config ¶
Config controls session store behavior.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config with sensible defaults.
type Entry ¶
type Entry struct {
Model string
Tier router.Tier
CreatedAt int64 // unix millis
LastUsedAt int64 // unix millis
RequestCount int
RecentHashes []string // sliding window of recent request hashes
Strikes int
Escalated bool
UserExplicit bool // true when user explicitly selected a model (not a routing profile)
SessionCostMicros int64 // cost in micro-USD (1 USD = 1_000_000 micros)
}
Entry holds the state of a single session.
type SessionInfo ¶
type SessionInfo struct {
ID string `json:"id"`
Model string `json:"model"`
Age int64 `json:"age"` // seconds
}
SessionInfo is a summary of a single session.
type Stats ¶
type Stats struct {
Count int `json:"count"`
Sessions []SessionInfo `json:"sessions"`
}
Stats is a snapshot of session store state for debugging.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is a concurrent-safe in-memory session store with automatic cleanup.
func NewStore ¶
NewStore creates a session store. If config.Enabled is true, a background goroutine periodically evicts expired sessions.
func (*Store) AddSessionCost ¶
AddSessionCost adds cost in micro-USD to the session.
func (*Store) ClearSession ¶
ClearSession removes a specific session.
func (*Store) EscalateSession ¶
EscalateSession bumps the session tier one level up. If the session has UserExplicit set, escalation is skipped entirely - the user's explicit model choice wins unconditionally.
func (*Store) GetSession ¶
GetSession returns the session entry for the given ID, or nil if expired.
func (*Store) GetSessionCostUSD ¶
GetSessionCostUSD returns the session cost in USD.
func (*Store) RecordRequestHash ¶
RecordRequestHash tracks a request content hash in the session sliding window. If 2+ consecutive identical hashes, increment strikes. Returns strike count.
func (*Store) SetSession ¶
SetSession pins a model to a session. Creates or updates. If userExplicit is true, the user explicitly chose this model (not a routing profile), and session pinning will skip tier escalation and profile-based routing for the lifetime of the session.
func (*Store) TouchSession ¶
TouchSession extends a session timeout and increments its request count.