session

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSessionNotFound  = errors.New("session not found")
	ErrSessionExpired   = errors.New("session expired")
	ErrDuplicateSession = errors.New("duplicate session")
)

Functions

func SessionKeyFromContext

func SessionKeyFromContext(ctx context.Context) string

SessionKeyFromContext extracts the session key from context.

func WithSessionKey

func WithSessionKey(ctx context.Context, key string) context.Context

WithSessionKey adds a session key to the context.

Types

type ChildSession added in v0.4.0

type ChildSession struct {
	// Key is the unique identifier for this child session.
	Key string

	// ParentKey is the key of the parent session.
	ParentKey string

	// AgentName is the sub-agent that owns this child session.
	AgentName string

	// History contains messages added during this child session.
	History []Message

	// Config holds the child session settings.
	Config ChildSessionConfig

	// CreatedAt is when this child session was forked.
	CreatedAt time.Time

	// MergedAt is set when the child session is merged back to parent.
	// Zero value means not yet merged.
	MergedAt time.Time
}

ChildSession represents an isolated sub-session forked from a parent. It follows "read parent, write child" semantics: the child session has its own message history that does not pollute the parent until explicitly merged.

func NewChildSession added in v0.4.0

func NewChildSession(parentKey, agentName string, cfg ChildSessionConfig) *ChildSession

NewChildSession creates a new child session forked from a parent.

func (*ChildSession) AppendMessage added in v0.4.0

func (cs *ChildSession) AppendMessage(msg Message)

AppendMessage adds a message to the child session's history.

func (*ChildSession) IsMerged added in v0.4.0

func (cs *ChildSession) IsMerged() bool

IsMerged returns true if this child session has been merged back to parent.

type ChildSessionConfig added in v0.4.0

type ChildSessionConfig struct {
	// MaxMessages limits the child session's message history.
	// Zero means unlimited (inherits parent limit).
	MaxMessages int

	// InheritHistory copies the last N messages from parent.
	// Zero means start with empty history.
	InheritHistory int

	// SummarizeOnMerge applies a summarizer when merging back to parent.
	SummarizeOnMerge bool
}

ChildSessionConfig configures how a child session behaves.

type ChildSessionStore added in v0.4.0

type ChildSessionStore interface {
	// ForkChild creates a new child session from a parent session.
	// If cfg.InheritHistory > 0, the last N messages are copied from parent.
	ForkChild(parentKey, agentName string, cfg ChildSessionConfig) (*ChildSession, error)

	// MergeChild merges a child session's messages back into the parent.
	// The summary parameter, if non-empty, replaces the full child history
	// with a single assistant message containing the summary.
	MergeChild(childKey string, summary string) error

	// DiscardChild removes a child session without merging.
	DiscardChild(childKey string) error

	// GetChild retrieves a child session by key.
	GetChild(childKey string) (*ChildSession, error)

	// ChildrenOf returns all child sessions for a parent.
	ChildrenOf(parentKey string) ([]*ChildSession, error)
}

ChildSessionStore extends Store with child session operations.

type EntStore

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

EntStore implements Store using entgo.io

func NewEntStore

func NewEntStore(dbPath string, opts ...StoreOption) (*EntStore, error)

NewEntStore creates a new ent-backed session store

func NewEntStoreWithClient

func NewEntStoreWithClient(client *ent.Client, opts ...StoreOption) *EntStore

NewEntStoreWithClient creates a new ent-backed session store using an existing ent.Client. This avoids opening a second database connection when the client is already available (e.g., from the bootstrap process). Schema migration is assumed to be already complete.

func (*EntStore) AppendMessage

func (s *EntStore) AppendMessage(key string, msg Message) error

AppendMessage adds a message to session history

func (*EntStore) Client

func (s *EntStore) Client() *ent.Client

Client returns the ent client

func (*EntStore) Close

func (s *EntStore) Close() error

Close closes the ent client and underlying database connection. When the client was provided externally via NewEntStoreWithClient, only the ent client is closed; the raw DB connection is managed by the caller.

func (*EntStore) CompactMessages

func (s *EntStore) CompactMessages(key string, upToIndex int, summary string) error

CompactMessages replaces messages up to (and including) upToIndex with a single summary message. This achieves compaction: the original messages are removed and replaced by a condensed version, preserving recent context.

func (*EntStore) Create

func (s *EntStore) Create(session *Session) error

Create creates a new session

func (*EntStore) Delete

func (s *EntStore) Delete(key string) error

Delete removes a session

func (*EntStore) Get

func (s *EntStore) Get(key string) (*Session, error)

Get retrieves a session by key

func (*EntStore) GetChecksum

func (s *EntStore) GetChecksum(name string) ([]byte, error)

GetChecksum retrieves the passphrase checksum by name

func (*EntStore) GetSalt

func (s *EntStore) GetSalt(name string) ([]byte, error)

GetSalt retrieves the encryption salt by name

func (*EntStore) MigrateSecrets

func (s *EntStore) MigrateSecrets(ctx context.Context, reencryptFn func([]byte) ([]byte, error), newSalt, newChecksum []byte) (err error)

MigrateSecrets performs the secret migration using callbacks to avoid import cycles. reencryptFn typically decrypts with old key and encrypts with new key.

func (*EntStore) SetChecksum

func (s *EntStore) SetChecksum(name string, checksum []byte) error

SetChecksum stores the passphrase checksum by name

func (*EntStore) SetSalt

func (s *EntStore) SetSalt(name string, salt []byte) error

SetSalt stores the encryption salt by name

func (*EntStore) Update

func (s *EntStore) Update(session *Session) error

Update updates an existing session

type InMemoryChildStore added in v0.4.0

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

InMemoryChildStore implements ChildSessionStore using an in-memory map. It wraps an existing Store for parent session access.

func NewInMemoryChildStore added in v0.4.0

func NewInMemoryChildStore(parent Store) *InMemoryChildStore

NewInMemoryChildStore creates a new in-memory child session store.

func (*InMemoryChildStore) ChildrenOf added in v0.4.0

func (s *InMemoryChildStore) ChildrenOf(parentKey string) ([]*ChildSession, error)

ChildrenOf returns all child sessions for a parent using the parent index.

func (*InMemoryChildStore) DiscardChild added in v0.4.0

func (s *InMemoryChildStore) DiscardChild(childKey string) error

DiscardChild removes a child session without merging.

func (*InMemoryChildStore) ForkChild added in v0.4.0

func (s *InMemoryChildStore) ForkChild(parentKey, agentName string, cfg ChildSessionConfig) (*ChildSession, error)

ForkChild creates a new child session from a parent session.

func (*InMemoryChildStore) GetChild added in v0.4.0

func (s *InMemoryChildStore) GetChild(childKey string) (*ChildSession, error)

GetChild retrieves a child session by key.

func (*InMemoryChildStore) MergeChild added in v0.4.0

func (s *InMemoryChildStore) MergeChild(childKey string, summary string) error

MergeChild merges a child session's messages back into the parent.

type Message

type Message struct {
	Role      types.MessageRole `json:"role"` // "user", "assistant", "tool"
	Content   string            `json:"content"`
	Timestamp time.Time         `json:"timestamp"`
	ToolCalls []ToolCall        `json:"toolCalls,omitempty"`
	Author    string            `json:"author,omitempty"` // ADK agent name for multi-agent routing
}

Message represents a single message in conversation history

type Session

type Session struct {
	Key         string            `json:"key"`
	AgentID     string            `json:"agentId,omitempty"`
	ChannelType string            `json:"channelType,omitempty"`
	ChannelID   string            `json:"channelId,omitempty"`
	History     []Message         `json:"history"`
	Metadata    map[string]string `json:"metadata,omitempty"`
	Model       string            `json:"model,omitempty"`
	CreatedAt   time.Time         `json:"createdAt"`
	UpdatedAt   time.Time         `json:"updatedAt"`
}

Session represents a conversation session

type Store

type Store interface {
	// Create creates a new session
	Create(session *Session) error
	// Get retrieves a session by key
	Get(key string) (*Session, error)
	// Update updates an existing session
	Update(session *Session) error
	// Delete removes a session
	Delete(key string) error
	// AppendMessage adds a message to session history
	AppendMessage(key string, msg Message) error
	// Close closes the store
	Close() error

	// GetSalt retrieves the encryption salt for LocalCryptoProvider
	GetSalt(name string) ([]byte, error)
	// SetSalt stores the encryption salt for LocalCryptoProvider
	SetSalt(name string, salt []byte) error
}

Store defines the interface for session storage

type StoreOption

type StoreOption func(*EntStore)

StoreOption defines the functional option pattern for EntStore

func WithMaxHistoryTurns

func WithMaxHistoryTurns(n int) StoreOption

WithMaxHistoryTurns limits the number of messages kept per session.

func WithPassphrase

func WithPassphrase(passphrase string) StoreOption

WithPassphrase sets the encryption passphrase for the database.

func WithTTL

func WithTTL(d time.Duration) StoreOption

WithTTL sets the session time-to-live.

type ToolCall

type ToolCall struct {
	ID               string `json:"id"`
	Name             string `json:"name"`
	Input            string `json:"input"`
	Output           string `json:"output,omitempty"`
	Thought          bool   `json:"thought,omitempty"`
	ThoughtSignature []byte `json:"thoughtSignature,omitempty"`
}

ToolCall represents a tool invocation

Jump to

Keyboard shortcuts

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