Help us shape the future of Go! Take the 2026 Go Developer Survey

session

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package session stores agent conversations and per-session state.

Index

Constants

This section is empty.

Variables

View Source
var ErrConflict = errors.New("session was modified concurrently")

ErrConflict is returned by Save when the stored session changed since the session being saved was loaded or last saved, for example by another Runner or process, or when GetOrCreate made a session that another writer created in the meantime. Load the session again and reapply the change.

MemoryStore detects conflicts for the sessions it returns from Get, sessions made by GetOrCreate and sessions it has saved before. Saving any other Session overwrites the stored one. Stores in other packages do the same with Loaded, ExpectedRevision and MarkSaved.

View Source
var ErrNotFound = errors.New("session not found")

ErrNotFound is returned when a session does not exist.

Functions

func ExpectedRevision

func ExpectedRevision(s Session) (rev int64, ok bool)

ExpectedRevision reports the stored revision a Store must find to save s without ErrConflict. ok is false for sessions not loaded or saved by a store, such as those from New or FromSnapshot, which overwrite the stored session. rev is -1 for a session GetOrCreate made, which conflicts with any stored session. When the session is not stored at all, for example because it was deleted, it is saved whatever rev is, as revision max(rev, 0)+1 so that copies loaded before the deletion conflict.

func MarkSaved

func MarkSaved(s Session, rev int64)

MarkSaved records that a Store saved s as revision rev, which its next save expects.

func NewID

func NewID() string

NewID returns a random session identifier: a version 4 UUID, which carries 122 random bits and, unlike a version 7 UUID, does not reveal when the session was created.

Types

type Info

type Info struct {
	ID        string
	Messages  int
	CreatedAt time.Time
	UpdatedAt time.Time
}

Info summarizes a stored session.

type MemoryStore

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

MemoryStore keeps sessions in process memory as JSON snapshots, with the state representation and session IDs of a persistent store. Later changes to a session are visible only after Save, which returns ErrConflict for concurrent modification. The zero value is ready to use.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore returns an empty store.

func (*MemoryStore) Delete

func (s *MemoryStore) Delete(_ context.Context, id string) error

Delete implements Store.

func (*MemoryStore) Get

func (s *MemoryStore) Get(_ context.Context, id string) (Session, error)

Get implements Store.

func (*MemoryStore) List

func (s *MemoryStore) List(context.Context) ([]Info, error)

List implements Store.

func (*MemoryStore) Save

func (s *MemoryStore) Save(_ context.Context, sess Session) error

Save implements Store.

type Session

type Session interface {
	ID() string
	// Messages returns a copy of the conversation. Values within message
	// metadata may remain shared, as in model.Message.Clone.
	Messages() []*model.Message
	// Append adds messages to the conversation.
	Append(msgs ...*model.Message)
	// Replace swaps the whole conversation, for example after compaction.
	Replace(msgs []*model.Message)
	// State holds arbitrary per-session values.
	State() *State
	CreatedAt() time.Time
	UpdatedAt() time.Time
}

Session is a conversation with an agent.

Implementations must be safe for concurrent use.

func FromSnapshot

func FromSnapshot(snap *Snapshot) Session

FromSnapshot builds an in-memory session from a snapshot. It copies the messages and state map; state values and message metadata values remain shared.

func GetOrCreate

func GetOrCreate(ctx context.Context, store Store, id string) (Session, bool, error)

GetOrCreate loads a session, creating an empty one if it does not exist. The boolean reports whether the session was created.

func Loaded

func Loaded(snap *Snapshot) Session

Loaded returns the session a Store returns from Get for snap, which it loaded at snap.Revision. Saving the session expects that revision; see ExpectedRevision.

func New

func New(id string) Session

New returns an empty in-memory session. An empty id is replaced by NewID.

type Snapshot

type Snapshot struct {
	ID        string           `json:"id"`
	Messages  []*model.Message `json:"messages"`
	State     map[string]any   `json:"state,omitempty"`
	CreatedAt time.Time        `json:"created_at"`
	UpdatedAt time.Time        `json:"updated_at"`
	// Revision counts the saves of the session in its store. Stores use it
	// to detect concurrent modification; see ErrConflict.
	Revision int64 `json:"revision,omitempty"`
}

Snapshot is the serializable form of a session.

func TakeSnapshot

func TakeSnapshot(s Session) *Snapshot

TakeSnapshot captures a session through separate accessor calls. Callers must prevent concurrent changes if they need a consistent snapshot. State values and message metadata values remain shared.

type State

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

State is a key-value map whose operations are safe for concurrent use. Values are stored and returned without copying; callers must treat mutable values as immutable or synchronize access to them. Values should be JSON serializable when the session is persisted.

func (*State) All

func (s *State) All() map[string]any

All returns a shallow copy of the map; its values remain shared.

func (*State) Decode

func (s *State) Decode(key string, dst any) (bool, error)

Decode unmarshals the value for key into dst, converting through JSON. This recovers typed values from sessions loaded from disk.

func (*State) Delete

func (s *State) Delete(key string)

Delete removes a value.

func (*State) Get

func (s *State) Get(key string) (any, bool)

Get returns the value for key.

func (*State) Set

func (s *State) Set(key string, value any)

Set stores a value.

func (*State) Value

func (s *State) Value[T any](key string) (T, bool, error)

Value returns the value of key as a T, converting through JSON as Decode does, so values of sessions loaded from disk, which are plain JSON values, read as the type they were stored as. It reports false if s has no such value.

type Store

type Store interface {
	// Get loads a session or returns ErrNotFound.
	Get(ctx context.Context, id string) (Session, error)
	Save(ctx context.Context, s Session) error
	Delete(ctx context.Context, id string) error
	// List returns sessions, most recently updated first.
	List(ctx context.Context) ([]Info, error)
}

Store persists sessions. Session IDs are 1 to 128 ASCII letters, digits, '_', '-' and '.', not starting with '.'; the built-in stores reject others.

Directories

Path Synopsis
Package sessiontest checks session.Store implementations against the contract the built-in stores follow.
Package sessiontest checks session.Store implementations against the contract the built-in stores follow.

Jump to

Keyboard shortcuts

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