session

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package session implements conversation persistence.

Storage layout mirrors the real Claude Code (src/utils/sessionStorage.ts):

~/.claude/projects/<sanitized-cwd>/<session-id>.jsonl

Each line of the JSONL file is one Entry. On startup we can read any previous session's JSONL and restore its message history, enabling --continue and /resume.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractTitle

func ExtractTitle(path string) string

ExtractTitle returns the best title for a session: 1. The last custom-title entry if present. 2. The first user message text (truncated to 60 runes). 3. Empty string.

func FilterUnresolvedToolUses

func FilterUnresolvedToolUses(msgs []api.Message) []api.Message

FilterUnresolvedToolUses drops orphan tool_use blocks from assistant messages — i.e. tool_use blocks whose ID has no matching tool_result in any subsequent user message. Anthropic's API rejects history with such orphans; they appear when the stream errors mid-turn before tools could run. Mirrors src/utils/messages.ts filterUnresolvedToolUses.

If filtering empties an assistant message entirely (every block was an orphan tool_use), the message is dropped to avoid sending an empty content array.

func LoadMessages

func LoadMessages(path string) ([]api.Message, error)

LoadMessages reads a JSONL transcript at path and returns its messages. Output passes through FilterUnresolvedToolUses so a partial assistant message persisted by conversation recovery (a tool_use with no matching tool_result) doesn't poison the next API call on /resume.

func LoadTag

func LoadTag(path string) (string, error)

LoadTag returns the current tag for a session, or "" if untagged.

func ProjectDir

func ProjectDir(cwd, home string) string

New creates a new session rooted at cwd, using sessionID as the file name. ProjectDir returns the directory where session files for cwd are stored.

Types

type Activity

type Activity struct {
	FirstActivity time.Time
	LastActivity  time.Time
	MessageCount  int
}

Activity summarizes a session's temporal footprint.

func LoadActivity

func LoadActivity(path string) (Activity, error)

LoadActivity walks the JSONL and returns first/last entry timestamps and message count. Mirrors the temporal half of src/utils/sessionActivity.ts — we do not run the heartbeat timer (that's a remote/bridge feature) but we expose enough for /session to display "active for X" / "idle for Y".

func (Activity) IdleSince

func (a Activity) IdleSince(now time.Time) time.Duration

IdleSince returns how long since the last recorded activity, or 0 if none.

type CostSnapshot

type CostSnapshot struct {
	InputTokens  int     `json:"inputTokens"`
	OutputTokens int     `json:"outputTokens"`
	CostUSD      float64 `json:"costUSD"`
}

CostSnapshot is the cost entry written to JSONL on each turn.

func LoadCost

func LoadCost(path string) (CostSnapshot, error)

LoadCost reads the last cost entry from a JSONL file. Returns zero CostSnapshot if none found (not an error).

type Entry

type Entry struct {
	Type      string          `json:"type"`
	SessionID string          `json:"sessionId"`
	Timestamp int64           `json:"ts,omitempty"`
	Message   json.RawMessage `json:"message,omitempty"`
	Summary   string          `json:"summary,omitempty"`
	Title     string          `json:"customTitle,omitempty"`
	// Mode is set on type="session_settings" entries to persist session-level
	// settings (e.g. "coordinator" or "normal") across resume.
	Mode string `json:"mode,omitempty"`
}

Entry is one line in the JSONL transcript file.

type FileAccessEntry

type FileAccessEntry struct {
	Op   string `json:"op"` // "read" | "write"
	Path string `json:"path"`
}

FileAccessEntry records a file read or write.

func LoadFileAccess

func LoadFileAccess(path string) ([]FileAccessEntry, error)

LoadFileAccess reads all file access entries from a JSONL file.

type SearchResult

type SearchResult struct {
	Role string
	Text string
}

SearchResult is one matching turn from a transcript search.

func Search(path string, term string) ([]SearchResult, error)

Search scans a JSONL transcript for messages containing term (case-insensitive).

type Session

type Session struct {
	ID         string
	ProjectDir string
	FilePath   string
}

Session manages the JSONL transcript for one conversation.

func FromFile

func FromFile(filePath string) *Session

FromFile wraps an existing JSONL file as a Session so new turns can be appended to it.

func New

func New(cwd, sessionID string) (*Session, error)

func (*Session) Append

func (s *Session) Append(entry Entry) error

Append writes one entry to the JSONL file.

func (*Session) AppendCost

func (s *Session) AppendCost(inputTokens, outputTokens int, costUSD float64) error

AppendCost writes a cost snapshot entry to the session JSONL.

func (*Session) AppendFileAccess

func (s *Session) AppendFileAccess(op, path string) error

AppendFileAccess records that a file was read or written.

func (*Session) AppendMessage

func (s *Session) AppendMessage(msg api.Message) error

AppendMessage serializes an api.Message and appends it.

func (*Session) AppendTag

func (s *Session) AppendTag(tag string) error

AppendTag persists a tag label for the session. Empty tag clears it. Mirrors src/utils/sessionStorage.ts saveTag — tag entries are appended to the JSONL transcript and the most recent value wins on read.

func (*Session) LoadMessages

func (s *Session) LoadMessages() ([]api.Message, error)

LoadMessages reads the JSONL file and returns the message history.

func (*Session) ReadMode

func (s *Session) ReadMode() string

ReadMode scans the session JSONL and returns the most recent mode value from a "session_settings" entry. Returns "" if no mode has been set.

func (*Session) SetMode

func (s *Session) SetMode(mode string) error

SetMode persists a session-level mode setting (e.g. "coordinator" or "normal").

func (*Session) SetSummary

func (s *Session) SetSummary(summary string) error

SetSummary persists a compaction summary.

func (*Session) SetTitle

func (s *Session) SetTitle(title string) error

SetTitle persists a human-readable conversation title.

func (*Session) Snapshot

func (s *Session) Snapshot() int

Snapshot returns the current message count — used as a rewind point. We use the file line count as a proxy. Returns 0 if unavailable.

type SessionMeta

type SessionMeta struct {
	ID       string
	FilePath string
	Modified time.Time
	Title    string
}

SessionMeta is a lightweight descriptor for a past session.

func List

func List(cwd string) ([]SessionMeta, error)

List returns all session IDs for the given cwd, newest first.

Jump to

Keyboard shortcuts

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