session

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Transcript of one conversation with usage totals and pluggable persistence

Index

Constants

View Source
const Ack = "Understood. I have the summary of the earlier conversation and will continue from here."

Assistant turn written after a summary so the kept tail still follows an assistant message

View Source
const MaxHeaders = 32

Header lines appended between rewrites before the log is compacted into a fresh file

View Source
const ProjectLabel = "project"

Label naming the project root a session was started under, by which stores list the sessions of a project

Variables

View Source
var ErrBusy = errors.New("session: open in another icebath")

Error of Lease when another process has the session open

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

Error for a session a store does not hold

Functions

func InvalidID

func InvalidID(id string) error

Error for an id a store cannot use

func MarshalEntry

func MarshalEntry(e Entry) ([]byte, error)

Serializes one entry

func MarshalParts

func MarshalParts(parts []protocol.Part) ([]byte, error)

Serializes parts as the tagged JSON array a stored message carries

func MarshalRecord

func MarshalRecord(r Record) ([]byte, error)

Serializes one record

func SortInfos

func SortInfos(infos []Info)

Orders newest first by update time, then by id

func ValidID

func ValidID(id string) bool

Reports whether an id is safe to use as a file name

Types

type Entry

type Entry struct {
	// Position on the shared timeline of entries and records
	Seq   int
	Time  time.Time
	Agent string
	Model string
	Usage protocol.Usage
	protocol.Message
}

One stored message with when it was added, who produced it and what it cost

func UnmarshalEntry

func UnmarshalEntry(data []byte) (Entry, error)

Rebuilds one entry written by MarshalEntry

type Header struct {
	ID       string            `json:"id"`
	Created  time.Time         `json:"created"`
	Updated  time.Time         `json:"updated"`
	Title    string            `json:"title,omitempty"`
	Named    bool              `json:"named,omitempty"`
	Model    string            `json:"model,omitempty"`
	Parent   string            `json:"parent,omitempty"`
	Labels   map[string]string `json:"labels,omitempty"`
	Usage    protocol.Usage    `json:"usage"`
	Last     protocol.Usage    `json:"last"`
	Messages int               `json:"messages"`
	Records  int               `json:"records"`
}

Metadata and usage of a session as stored

func (Header) Info

func (h Header) Info() Info

Listing entry built from a header alone

type Info

type Info struct {
	ID       string            `json:"id"`
	Created  time.Time         `json:"created"`
	Updated  time.Time         `json:"updated"`
	Title    string            `json:"title,omitempty"`
	Model    string            `json:"model,omitempty"`
	Parent   string            `json:"parent,omitempty"`
	Labels   map[string]string `json:"labels,omitempty"`
	Messages int               `json:"messages"`
	Records  int               `json:"records"`
	Usage    protocol.Usage    `json:"usage"`
	// Whether another process has the session open right now
	Busy bool `json:"busy,omitempty"`
	// Why a store could not read the session, empty when it loaded cleanly
	Error string `json:"error,omitempty"`
}

Metadata of a session without its messages

func OfProject

func OfProject(infos []Info, root string) []Info

Narrows a listing to the sessions of a project, keeping unreadable ones out

type MarkdownOptions

type MarkdownOptions struct {
	// Adds the time and producing agent under each heading
	Timestamps bool
	// Appends the record log after the transcript
	Records bool
}

Controls which parts of a transcript the Markdown export includes

type Record

type Record struct {
	Seq   int
	Time  time.Time
	Agent string
	Kind  string
	Data  json.RawMessage
}

Event worth replaying on the timeline that is not a message, such as an approval or a compaction

func UnmarshalRecord

func UnmarshalRecord(data []byte) (Record, error)

Rebuilds one record written by MarshalRecord

type Session

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

Ordered messages of a conversation and the token usage its responses have cost so far

func FromHeader

func FromHeader(h Header, entries []Entry, records []Record) *Session

Session rebuilt from a header, its entries and its records

func New

func New() *Session

Starts an empty session with a random id

func (*Session) AddResponse

func (s *Session) AddResponse(agent string, r protocol.Response) Entry

Appends the assistant turn of a response produced by agent and adds its usage to the totals

func (*Session) AddResults

func (s *Session) AddResults(agent string, results []protocol.ToolResult) Entry

Appends one tool message carrying every result of a tool round run by agent

func (*Session) Append

func (s *Session) Append(m protocol.Message) Entry

Appends input from the user as an entry with no agent

func (*Session) Compact

func (s *Session) Compact(summary string, keep int) error

Replaces everything before the last keep messages with a summary ahead of a user turn and resets Last

func (*Session) Context

func (s *Session) Context() int

Tokens the next request will carry, as measured by the last response

func (*Session) Created

func (s *Session) Created() time.Time

func (*Session) Empty

func (s *Session) Empty() bool

Reports whether the session holds no messages, records or title

func (*Session) Entries

func (s *Session) Entries() []Entry

Copy of every entry in order

func (*Session) Fork

func (s *Session) Fork() *Session

Starts a new session carrying a copy of the transcript, usage reset and parent recorded

func (*Session) Head

func (s *Session) Head(keep int) []Entry

Entries a compaction keeping that many messages replaces with its summary

func (*Session) Header

func (s *Session) Header() Header

Metadata of the session as a store writes it

func (*Session) ID

func (s *Session) ID() string

func (*Session) Info

func (s *Session) Info() Info

func (*Session) Label

func (s *Session) Label(key, value string)

Sets a label, removing it when the value is empty

func (*Session) Labels

func (s *Session) Labels() map[string]string

Copy of the labels

func (*Session) Last

func (s *Session) Last() protocol.Usage

Usage of the most recent response, whose input side measures the current context

func (*Session) Len

func (s *Session) Len() int

func (*Session) Log

func (s *Session) Log(agent, kind string, data any) (Record, error)

Appends a timeline record of kind with data serialized as JSON

func (*Session) Markdown

func (s *Session) Markdown() string

Renders the full transcript as Markdown with a heading per turn

func (*Session) MarkdownWith

func (s *Session) MarkdownWith(o MarkdownOptions) string

Renders the transcript as Markdown honoring the options

func (*Session) Model

func (s *Session) Model() string

func (*Session) Named

func (s *Session) Named() bool

Reports whether a person named the session

func (*Session) Parent

func (s *Session) Parent() string

Id of the session this one was forked from

func (*Session) Project

func (s *Session) Project() string

Project root the session was started under, empty for one started before projects were recorded

func (*Session) Prune

func (s *Session) Prune(callID, stub string)

Remembers the stub that stands in for the tool result in requests from now on

func (*Session) Pruned

func (s *Session) Pruned(callID string) (string, bool)

Stub standing in for the tool result, false while the result still goes out whole

func (*Session) Records

func (s *Session) Records() []Record

Copy of every record in order

func (*Session) Rename

func (s *Session) Rename(title string)

Names the session as a person chose, an empty name handing the title back to derivation

func (*Session) SetModel

func (s *Session) SetModel(model string)

func (*Session) SetTitle

func (s *Session) SetTitle(title string)

Sets a derived title, left alone once a person named the session

func (*Session) Snapshot

func (s *Session) Snapshot() []protocol.Message

Copies the messages so callers can build a request without holding the lock

func (*Session) Title

func (s *Session) Title() string

func (*Session) Truncate

func (s *Session) Truncate(n int)

Drops every entry after the first n and every record newer than the last kept entry

func (*Session) Updated

func (s *Session) Updated() time.Time

func (*Session) Usage

func (s *Session) Usage() protocol.Usage

Token usage summed over every response

type Store

type Store struct {
	Dir string
	// contains filtered or unexported fields
}

Store appending one JSON line per message to a log per session under a directory, so a crash loses at most the last line

Several processes may share the directory. Each write and read holds the lock of the session for its duration, and a process that works on a session holds its lease for as long as it has the session open, so another process sees the session as busy rather than silently writing a diverging copy of it

func (*Store) Busy

func (st *Store) Busy(id string) bool

Reports whether another process holds the lease of the session

func (*Store) Close

func (st *Store) Close() error

Releases every lease this process holds

func (*Store) Delete

func (st *Store) Delete(ctx context.Context, id string) error

func (*Store) Exists

func (st *Store) Exists(ctx context.Context, id string) (bool, error)

func (*Store) Lease

func (st *Store) Lease(id string) error

Takes the lease of a session for this process, a no-op when it already holds it and ErrBusy when another process does

func (*Store) Leased

func (st *Store) Leased(id string) bool

Reports whether this process holds the lease of the session

func (*Store) List

func (st *Store) List(ctx context.Context) ([]Info, error)

Lists stored sessions newest first from the last complete header line of each log, marking those another process has open

func (*Store) ListProject

func (st *Store) ListProject(ctx context.Context, root string) ([]Info, error)

Lists the sessions that belong to a project, by the label stores and apps set at creation

func (*Store) Load

func (st *Store) Load(ctx context.Context, id string) (*Session, error)

Replays the log, the latest header winning, ignoring an unterminated final line without touching the file

func (*Store) Path

func (st *Store) Path(id string) (string, error)

Path of the log for an id

func (*Store) Save

func (st *Store) Save(ctx context.Context, s *Session) error

Appends the new lines and a header when it changed, rewriting the log when history was altered

func (*Store) Unlease

func (st *Store) Unlease(id string) error

Gives the lease of a session back so another process may open it, dropping the lease file of a session never written

Jump to

Keyboard shortcuts

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