session

package
v0.98.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2026 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package session manages branchable conversation trees and JSONL serialization.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalEntry

func MarshalEntry(entry TreeEntry) ([]byte, error)

MarshalEntry serializes one session tree entry to JSON.

func SerializeSession

func SerializeSession(entries []TreeEntry) ([]byte, error)

SerializeSession writes session entries as JSONL.

func ToAgentContext

func ToAgentContext(sessionCtx Context, systemPrompt string) *msg.Context

ToAgentContext converts a session context into an agent loop context.

Types

type CompactionResult

type CompactionResult struct {
	EntryID          string
	Summary          string
	FirstKeptEntryID string
	TokensBefore     int
	ReadFiles        []string
	ModifiedFiles    []string
}

CompactionResult carries data needed to append a compaction tree node.

type Context

type Context struct {
	Messages    []msg.AgentMessage
	ModelName   string
	ActiveTools []string
}

Context is the reconstructed runtime view of a branch path.

func BuildContext

func BuildContext(path []TreeEntry) Context

BuildContext reconstructs loop context from a branch path with compaction boundaries.

type EntryType

type EntryType string

EntryType identifies a persisted session tree node.

const (
	EntryMessage           EntryType = "message"
	EntryModelChange       EntryType = "model_change"
	EntryActiveToolsChange EntryType = "active_tools_change"
	EntryBranchSummary     EntryType = "branch_summary"
	EntryCompaction        EntryType = "compaction"
	EntryCustom            EntryType = "custom"
)

type MemoryStorage

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

MemoryStorage is an in-memory Storage implementation for tests and ephemeral runs.

func NewMemoryStorage

func NewMemoryStorage() *MemoryStorage

NewMemoryStorage creates an empty in-memory session store.

func (*MemoryStorage) Append

func (m *MemoryStorage) Append(_ context.Context, entry TreeEntry) error

Append stores a tree entry in insertion order.

func (*MemoryStorage) Entries

func (m *MemoryStorage) Entries() []TreeEntry

Entries returns a snapshot of stored entries for assertions.

func (*MemoryStorage) GetBranch

func (m *MemoryStorage) GetBranch(_ context.Context, leafID string) ([]TreeEntry, error)

GetBranch returns the ordered path to the requested leaf.

func (*MemoryStorage) GetLeafID

func (m *MemoryStorage) GetLeafID(_ context.Context) (string, error)

GetLeafID returns the current leaf pointer.

func (*MemoryStorage) ListEntries

func (m *MemoryStorage) ListEntries(_ context.Context) ([]TreeEntry, error)

ListEntries returns all stored entries in append order.

func (*MemoryStorage) SetLeafID

func (m *MemoryStorage) SetLeafID(_ context.Context, id string) error

SetLeafID updates the current leaf pointer.

type Session

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

Session manages a branchable conversation tree backed by storage.

func New

func New(storage Storage) *Session

New creates a session backed by the given storage implementation.

func (*Session) Append

func (s *Session) Append(ctx context.Context, entry TreeEntry) error

Append stores a new tree entry and moves the leaf pointer to it.

func (*Session) AppendCompaction

func (s *Session) AppendCompaction(ctx context.Context, result CompactionResult) error

AppendCompaction stores a compaction node and moves the leaf pointer to it.

func (*Session) GetBranch

func (s *Session) GetBranch(ctx context.Context, leafID string) ([]TreeEntry, error)

GetBranch returns the path from root to the given leaf, inclusive.

func (*Session) LeafID

func (s *Session) LeafID(ctx context.Context) (string, error)

LeafID returns the current leaf pointer without loading the full branch.

func (*Session) ListEntries

func (s *Session) ListEntries(ctx context.Context) ([]TreeEntry, error)

ListEntries returns all persisted session entries when supported by storage.

func (*Session) MoveTo

func (s *Session) MoveTo(ctx context.Context, entryID, summary string) error

MoveTo switches the active leaf and optionally appends a branch summary node.

type Storage

type Storage interface {
	Append(ctx context.Context, entry TreeEntry) error
	GetBranch(ctx context.Context, leafID string) ([]TreeEntry, error)
	GetLeafID(ctx context.Context) (string, error)
	SetLeafID(ctx context.Context, id string) error
}

Storage persists session tree entries without prescribing a backend.

type TreeEntry

type TreeEntry struct {
	ID       string    `json:"id"`
	ParentID string    `json:"parent_id"`
	Type     EntryType `json:"type"`

	Message          msg.AgentMessage `json:"-"`
	MessageRaw       any              `json:"message,omitempty"`
	ModelName        string           `json:"model_name,omitempty"`
	ActiveToolNames  []string         `json:"active_tool_names,omitempty"`
	Summary          string           `json:"summary,omitempty"`
	FromID           string           `json:"from_id,omitempty"`
	FirstKeptEntryID string           `json:"first_kept_entry_id,omitempty"`
	TokensBefore     int              `json:"tokens_before,omitempty"`
	ReadFiles        []string         `json:"read_files,omitempty"`
	ModifiedFiles    []string         `json:"modified_files,omitempty"`
}

TreeEntry is one node in the append-only session tree.

func DeserializeSession

func DeserializeSession(data []byte) ([]TreeEntry, error)

DeserializeSession reads JSONL session entries.

func LoadJSONL

func LoadJSONL(r io.Reader) ([]TreeEntry, error)

LoadJSONL deserializes session entries from a reader.

func UnmarshalEntry

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

UnmarshalEntry deserializes one session tree entry from JSON.

Jump to

Keyboard shortcuts

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