session

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const ScopeVersionV1 = 1

ScopeVersionV1 is the first structured session-scope schema version.

Variables

This section is empty.

Functions

func BuildLegacyDirectAliases

func BuildLegacyDirectAliases(agentID, channel, account, peerID string) []string

func BuildLegacyMainAlias

func BuildLegacyMainAlias(agentID string) string

func BuildLegacyPeerAlias

func BuildLegacyPeerAlias(agentID, channel, peerKind, peerID string) string

func BuildMainSessionKey

func BuildMainSessionKey(agentID string) string

BuildMainSessionKey returns the canonical opaque main-session key for an agent. The corresponding legacy alias remains available via BuildLegacyMainAlias for compatibility and migration logic.

func BuildOpaqueSessionKey

func BuildOpaqueSessionKey(alias string) string

BuildOpaqueSessionKey returns a stable opaque session key derived from a canonical alias string. The alias remains available through metadata for compatibility and migration purposes.

func BuildSessionKey

func BuildSessionKey(scope SessionScope) string

BuildSessionKey returns the current opaque key for a structured session scope.

func CanonicalScopeSignature

func CanonicalScopeSignature(scope SessionScope) string

CanonicalScopeSignature returns a stable serialized representation of scope.

func CanonicalSessionIdentityID

func CanonicalSessionIdentityID(channel, rawID string, identityLinks map[string][]string) string

CanonicalSessionIdentityID collapses an identity using identity_links when possible, then returns a normalized lowercase identifier.

func IsExplicitSessionKey

func IsExplicitSessionKey(key string) bool

func IsLegacyAgentSessionKey

func IsLegacyAgentSessionKey(key string) bool

func IsOpaqueSessionKey

func IsOpaqueSessionKey(key string) bool

IsOpaqueSessionKey returns true when the key matches the current opaque session-key format.

func ResolveAgentID

func ResolveAgentID(store any, sessionKey string) string

ResolveAgentID returns the routed agent ID associated with a session. It prefers structured session scope metadata when available and falls back to legacy agent-scoped session keys for compatibility.

Types

type Allocation

type Allocation struct {
	Scope          SessionScope
	SessionKey     string
	SessionAliases []string
	MainSessionKey string
	MainAliases    []string
}

Allocation contains the concrete session keys selected for a routed turn. The current implementation intentionally preserves the legacy session-key layout while moving key construction out of the router.

func AllocateRouteSession

func AllocateRouteSession(input AllocationInput) Allocation

AllocateRouteSession maps a route decision onto a structured scope and the current opaque session-key format.

type AllocationInput

type AllocationInput struct {
	AgentID       string
	Context       bus.InboundContext
	SessionPolicy routing.SessionPolicy
}

AllocationInput contains the routing result and peer context needed to derive the session keys for a turn.

type JSONLBackend

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

JSONLBackend adapts a memory.Store into the SessionStore interface. Write errors are logged rather than returned, matching the fire-and-forget contract of SessionManager that the agent loop relies on.

func NewJSONLBackend

func NewJSONLBackend(store memory.Store) *JSONLBackend

NewJSONLBackend wraps a memory.Store for use as a SessionStore.

func (*JSONLBackend) AddFullMessage

func (b *JSONLBackend) AddFullMessage(sessionKey string, msg providers.Message)

func (*JSONLBackend) AddMessage

func (b *JSONLBackend) AddMessage(sessionKey, role, content string)

func (*JSONLBackend) Close

func (b *JSONLBackend) Close() error

Close releases resources held by the underlying store.

func (*JSONLBackend) EnsureSessionMetadata

func (b *JSONLBackend) EnsureSessionMetadata(sessionKey string, scope *SessionScope, aliases []string)

EnsureSessionMetadata persists scope and alias metadata for a session.

func (*JSONLBackend) GetHistory

func (b *JSONLBackend) GetHistory(key string) []providers.Message

func (*JSONLBackend) GetSessionScope

func (b *JSONLBackend) GetSessionScope(sessionKey string) *SessionScope

GetSessionScope reads structured scope metadata for a session key or alias.

func (*JSONLBackend) GetSummary

func (b *JSONLBackend) GetSummary(key string) string

func (*JSONLBackend) ListSessions

func (b *JSONLBackend) ListSessions() []string

ListSessions returns all known session keys.

func (*JSONLBackend) ResolveSessionKey

func (b *JSONLBackend) ResolveSessionKey(sessionKey string) string

ResolveSessionKey maps aliases onto their canonical session key when the underlying store supports structured metadata. Unknown aliases fall back to the original input so existing callers remain compatible.

func (*JSONLBackend) Save

func (b *JSONLBackend) Save(key string) error

Save persists session state. Since the JSONL store fsyncs every write immediately, the data is already durable. Save runs compaction to reclaim space from logically truncated messages (no-op when there are none).

func (*JSONLBackend) SetHistory

func (b *JSONLBackend) SetHistory(key string, history []providers.Message)

func (*JSONLBackend) SetSummary

func (b *JSONLBackend) SetSummary(key, summary string)

func (*JSONLBackend) TruncateHistory

func (b *JSONLBackend) TruncateHistory(key string, keepLast int)

type MetadataAwareSessionStore

type MetadataAwareSessionStore interface {
	EnsureSessionMetadata(sessionKey string, scope *SessionScope, aliases []string)
	ResolveSessionKey(sessionKey string) string
	GetSessionScope(sessionKey string) *SessionScope
}

MetadataAwareSessionStore exposes structured session metadata operations.

type ParsedLegacySessionKey

type ParsedLegacySessionKey struct {
	AgentID string
	Rest    string
}

func ParseLegacyAgentSessionKey

func ParseLegacyAgentSessionKey(sessionKey string) *ParsedLegacySessionKey

type Session

type Session struct {
	Key      string              `json:"key"`
	Messages []providers.Message `json:"messages"`
	Summary  string              `json:"summary,omitempty"`
	Created  time.Time           `json:"created"`
	Updated  time.Time           `json:"updated"`
}

type SessionManager

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

func NewSessionManager

func NewSessionManager(storage string) *SessionManager

func (*SessionManager) AddFullMessage

func (sm *SessionManager) AddFullMessage(sessionKey string, msg providers.Message)

AddFullMessage adds a complete message with tool calls and tool call ID to the session. This is used to save the full conversation flow including tool calls and tool results.

func (*SessionManager) AddMessage

func (sm *SessionManager) AddMessage(sessionKey, role, content string)

func (*SessionManager) Close

func (sm *SessionManager) Close() error

Close is a no-op for the in-memory SessionManager; it satisfies the SessionStore interface so callers can release resources uniformly.

func (*SessionManager) GetHistory

func (sm *SessionManager) GetHistory(key string) []providers.Message

func (*SessionManager) GetOrCreate

func (sm *SessionManager) GetOrCreate(key string) *Session

func (*SessionManager) GetSummary

func (sm *SessionManager) GetSummary(key string) string

func (*SessionManager) ListSessions

func (sm *SessionManager) ListSessions() []string

func (*SessionManager) Save

func (sm *SessionManager) Save(key string) error

func (*SessionManager) SetHistory

func (sm *SessionManager) SetHistory(key string, history []providers.Message)

SetHistory updates the messages of a session.

func (*SessionManager) SetSummary

func (sm *SessionManager) SetSummary(key string, summary string)

func (*SessionManager) TruncateHistory

func (sm *SessionManager) TruncateHistory(key string, keepLast int)

type SessionScope

type SessionScope struct {
	Version    int               `json:"version"`
	AgentID    string            `json:"agent_id"`
	Channel    string            `json:"channel"`
	Account    string            `json:"account"`
	Dimensions []string          `json:"dimensions"`
	Values     map[string]string `json:"values"`
}

SessionScope describes the semantic session partition selected for a turn.

func CloneScope

func CloneScope(scope *SessionScope) *SessionScope

CloneScope returns a deep copy of scope.

type SessionStore

type SessionStore interface {
	// AddMessage appends a simple role/content message to the session.
	AddMessage(sessionKey, role, content string)
	// AddFullMessage appends a complete message including tool calls.
	AddFullMessage(sessionKey string, msg providers.Message)
	// GetHistory returns the full message history for the session.
	GetHistory(key string) []providers.Message
	// GetSummary returns the conversation summary, or "" if none.
	GetSummary(key string) string
	// SetSummary replaces the conversation summary.
	SetSummary(key, summary string)
	// SetHistory replaces the full message history.
	SetHistory(key string, history []providers.Message)
	// TruncateHistory keeps only the last keepLast messages.
	TruncateHistory(key string, keepLast int)
	// Save persists any pending state to durable storage.
	Save(key string) error
	// ListSessions returns all known session keys.
	ListSessions() []string
	// Close releases resources held by the store.
	Close() error
}

SessionStore defines the persistence operations used by the agent loop. Both SessionManager (legacy JSON backend) and JSONLBackend satisfy this interface, allowing the storage layer to be swapped without touching the agent loop code.

Write methods (Add*, Set*, Truncate*) are fire-and-forget: they do not return errors. Implementations should log failures internally. This matches the original SessionManager contract that the agent loop relies on.

Jump to

Keyboard shortcuts

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