Documentation
¶
Overview ¶
Package session stores conversations: each session's metadata and its messages and events on disk, snapshots to branch from, search over a session's history, and the store ADK reads the conversation from (spec_sessions_017).
Index ¶
- Constants
- Variables
- func NewSessionID() string
- func SearchTerms(query string) []string
- func TitleFrom(prompt string) string
- func ValidateID(id string) error
- func ValidateName(name string) error
- type Match
- type Message
- type PersistentService
- func (p *PersistentService) AppendEvent(ctx context.Context, s adksession.Session, ev *adksession.Event) error
- func (p *PersistentService) Create(ctx context.Context, req *adksession.CreateRequest) (*adksession.CreateResponse, error)
- func (p *PersistentService) Delete(ctx context.Context, req *adksession.DeleteRequest) error
- func (p *PersistentService) Get(ctx context.Context, req *adksession.GetRequest) (*adksession.GetResponse, error)
- func (p *PersistentService) HasEvents(id string) bool
- func (p *PersistentService) List(ctx context.Context, req *adksession.ListRequest) (*adksession.ListResponse, error)
- func (p *PersistentService) Truncate(ctx context.Context, appName, userID, id string, keep int) error
- type SessionRecord
- type Storage
- func (s *Storage) Active() *SessionRecord
- func (s *Storage) AddMessage(role, content string) error
- func (s *Storage) Append(msg Message) error
- func (s *Storage) CreateSession(id, title, agent string) (*SessionRecord, error)
- func (s *Storage) FindName(name string) (*SessionRecord, error)
- func (s *Storage) LastTurn(id string) (traceparent string, index int)
- func (s *Storage) List() ([]*SessionRecord, error)
- func (s *Storage) ListWorkspace(workspace string) ([]*SessionRecord, error)
- func (s *Storage) Load(id string) (*SessionRecord, error)
- func (s *Storage) Open(ref string) (rec *SessionRecord, branched bool, err error)
- func (s *Storage) Rename(title string) error
- func (s *Storage) SetLastTurn(id, traceparent string, index int) error
- func (s *Storage) SetWorkspace(dir string)
- func (s *Storage) Snapshot(srcID, name string, replace bool) (*SessionRecord, error)
- func (s *Storage) TranscriptPath(id string) string
- func (s *Storage) Truncate(n int) error
- func (s *Storage) Workspace() string
- type TurnRef
Constants ¶
const ( KindSteer = "steer" KindHook = "hook" KindPlan = "plan" // the approved plan's go-ahead )
Kinds of user message other than prompts.
Variables ¶
var ErrInvalidID = errors.New("invalid session id")
ErrInvalidID is returned for session IDs that could escape the storage directory.
Functions ¶
func NewSessionID ¶
func NewSessionID() string
NewSessionID returns a collision-resistant session identifier.
func SearchTerms ¶
SearchTerms splits a query into words and "quoted phrases".
func TitleFrom ¶
TitleFrom makes a session title from a prompt: its first non-empty line, with whitespace collapsed and long text cut short.
func ValidateID ¶
ValidateID reports whether id is safe to use as a file name component.
func ValidateName ¶
ValidateName reports whether name can label a snapshot. Names follow the ID rules (they never reach a path, but are shown and typed like IDs) and can't start with "session-", so a name is never mistaken for an ID. "latest" is taken by --resume.
Types ¶
type Match ¶
type Match struct {
Index int // position in the transcript
Message Message
Hits int // distinct terms found
Excerpt string // the text around the first hit
}
Match is a transcript message that mentions a search's terms.
type Message ¶
type Message struct {
Role string `json:"role"` // "user", "model", "tool"
Content string `json:"content"`
Timestamp time.Time `json:"timestamp"`
// Kind tells user messages apart: "" for a prompt, KindSteer for a
// message sent while a turn ran, KindHook for a stop hook's request.
// Only prompts are rewind points.
Kind string `json:"kind,omitempty"`
// Events is how many events the session's event log held before this
// prompt, so rewinding the conversation can cut the log there. Unset for
// other messages and for prompts recorded by older versions.
Events *int `json:"events,omitempty"`
}
Message represents a single chat turn.
type PersistentService ¶
type PersistentService struct {
// contains filtered or unexported fields
}
PersistentService is an ADK session service that keeps sessions in memory and appends every final event to <dir>/<session>.events.jsonl, so a conversation (including tool calls and compaction summaries) can be resumed in a later process by using the same session ID.
func NewPersistentService ¶
func NewPersistentService(dir string) (*PersistentService, error)
NewPersistentService stores event logs in dir (created owner-only).
func (*PersistentService) AppendEvent ¶
func (p *PersistentService) AppendEvent(ctx context.Context, s adksession.Session, ev *adksession.Event) error
AppendEvent records the event in memory, then durably appends final events.
func (*PersistentService) Create ¶
func (p *PersistentService) Create(ctx context.Context, req *adksession.CreateRequest) (*adksession.CreateResponse, error)
Create creates a session, replaying stored events if the ID was used before.
func (*PersistentService) Delete ¶
func (p *PersistentService) Delete(ctx context.Context, req *adksession.DeleteRequest) error
Delete removes the session and its stored events.
func (*PersistentService) Get ¶
func (p *PersistentService) Get(ctx context.Context, req *adksession.GetRequest) (*adksession.GetResponse, error)
Get returns a session, loading it from disk on first access after a restart.
func (*PersistentService) HasEvents ¶
func (p *PersistentService) HasEvents(id string) bool
HasEvents reports whether a stored event log exists for id.
func (*PersistentService) List ¶
func (p *PersistentService) List(ctx context.Context, req *adksession.ListRequest) (*adksession.ListResponse, error)
List delegates to the in-memory service.
type SessionRecord ¶
type SessionRecord struct {
ID string `json:"id"`
Title string `json:"title"`
Agent string `json:"agent"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
MessageCount int `json:"message_count"`
// Workspace is the canonical workspace directory the session belongs to;
// empty for sessions created before sessions were workspace-scoped.
Workspace string `json:"workspace,omitempty"`
// LastTurn identifies the trace of the most recent turn so the next one,
// even in a later process, can link to it. Empty with telemetry off.
LastTurn *TurnRef `json:"last_turn,omitempty"`
// Name labels a snapshot saved with /session save; "" otherwise.
Name string `json:"name,omitempty"`
// From is the session this one was copied from: the saved session for
// a snapshot, the snapshot for a session started from one.
From string `json:"from,omitempty"`
// Messages is populated by Load and for the active session; List leaves it
// empty and reports MessageCount instead.
Messages []Message `json:"messages,omitempty"`
}
SessionRecord holds persistent metadata and chat history for a session.
type Storage ¶
type Storage struct {
// contains filtered or unexported fields
}
Storage manages persistent sessions on disk. Metadata lives in <id>.meta.json (small, rewritten atomically) and messages are appended to <id>.jsonl, so adding a message costs O(message) rather than O(session).
func NewStorage ¶
NewStorage creates session storage in the specified directory.
func (*Storage) Active ¶
func (s *Storage) Active() *SessionRecord
Active returns a snapshot of the currently active session, or nil.
func (*Storage) AddMessage ¶
AddMessage appends a message to the active session.
func (*Storage) CreateSession ¶
func (s *Storage) CreateSession(id, title, agent string) (*SessionRecord, error)
CreateSession starts a new session and makes it active. With title "" the session is named after its first prompt (see AddMessage).
func (*Storage) FindName ¶
func (s *Storage) FindName(name string) (*SessionRecord, error)
FindName returns the snapshot called name, or nil. Names are unique across workspaces, like IDs.
func (*Storage) LastTurn ¶
LastTurn returns the latest turn recorded for session id: from memory for the active session, otherwise from its metadata file. It returns ("", 0) when there is none.
func (*Storage) List ¶
func (s *Storage) List() ([]*SessionRecord, error)
List returns saved session metadata sorted by update time descending. Message bodies are not read.
func (*Storage) ListWorkspace ¶
func (s *Storage) ListWorkspace(workspace string) ([]*SessionRecord, error)
ListWorkspace returns sessions belonging to workspace, newest first.
func (*Storage) Load ¶
func (s *Storage) Load(id string) (*SessionRecord, error)
Load loads a session by ID and makes it active.
func (*Storage) Open ¶
func (s *Storage) Open(ref string) (rec *SessionRecord, branched bool, err error)
Open makes a session active by ID or snapshot name. An ID resumes that session in place. A snapshot, by name or by ID, is never continued: a new session copied from it starts instead (branched is true), so the snapshot stays as it was saved.
func (*Storage) Rename ¶
Rename sets the active session's title (shown in /session list and the terminal title). An empty title is refused.
func (*Storage) SetLastTurn ¶
SetLastTurn records the latest turn of session id in its metadata.
func (*Storage) SetWorkspace ¶
SetWorkspace records the workspace new sessions belong to.
func (*Storage) Snapshot ¶
func (s *Storage) Snapshot(srcID, name string, replace bool) (*SessionRecord, error)
Snapshot saves a named copy of session srcID: its transcript, metadata and the model's event log, under a new ID. The source is left as it is and stays active. An existing snapshot with the name is an ErrSnapshotNameTaken error unless replace is set, in which case it is deleted once the new one is written.
func (*Storage) TranscriptPath ¶
TranscriptPath is where session id's messages are kept (JSON lines), for hooks; "" for an invalid ID.