session

package
v0.3.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const LabelMaxLen = 200

LabelMaxLen is the max length of the label string after sanitization.

View Source
const TailReadBytes int64 = 64 * 1024

TailReadBytes is the upper bound on how many bytes we read from the end of a JSONL file.

Variables

View Source
var ErrSessionEmpty = errors.New("session has no usable content")

ErrSessionEmpty is returned by ParseSessionTail / FindByID when the file exists but has no user message or no extractable label.

View Source
var ErrSessionFileMissing = errors.New("session file missing")

ErrSessionFileMissing is returned by FindByID when no JSONL file matches the session id. Callers distinguish this from a file that exists but contains no usable content (see ErrSessionEmpty).

Functions

func CollectJSONLPaths added in v0.1.1

func CollectJSONLPaths(base string) ([]string, error)

CollectJSONLPaths walks the projects directory and returns every session JSONL path, excluding agent-*.jsonl. Returned nil when projects dir doesn't exist.

func ExtractText added in v0.1.4

func ExtractText(raw json.RawMessage, sep string) string

ExtractText returns the concatenated text of a message Content payload. Content may be a bare JSON string or an array of typed blocks; only "text" blocks contribute, joined by sep.

func IterContent added in v0.1.1

func IterContent(path string, fn func(text string) bool) error

IterContent reads a session JSONL transcript and invokes fn with the extractable text of each user/assistant message. fn can return false to stop early. Non-JSON and non-message lines are skipped silently. Lines longer than 4 MiB are skipped.

func ProjectsDir

func ProjectsDir() (string, error)

ProjectsDir returns the directory where Claude Code stores transcripts. If CLAUDE_CONFIG_DIR is set, it uses $CLAUDE_CONFIG_DIR/projects; otherwise it falls back to ~/.claude/projects.

func SanitizeLabel added in v0.3.0

func SanitizeLabel(s string) string

SanitizeLabel collapses whitespace and neutralizes control characters in a session label so it is safe to emit into the tab-separated fzf row. Exported for reuse by other sources (OpenCode titles come from the DB, not a JSONL).

func Truncate added in v0.1.4

func Truncate(s string, n int) string

Truncate shortens s to at most n runes, appending an ellipsis when cut.

Types

type Message added in v0.3.0

type Message struct {
	Role      string
	Timestamp time.Time
	Body      string
}

Message is one rendered transcript turn, the unit the preview pane displays. Sources that don't store a JSONL transcript (OpenCode) produce these directly.

type Session

type Session struct {
	ID          string
	ProjectDir  string
	JSONLPath   string
	CWD         string
	CWDBasename string
	Label       string
	LastTime    time.Time
	LastEpoch   int64
	CWDExists   bool
	// CWDUnknown is true when the JSONL had no cwd field and the
	// directory-name fallback could not be reconciled with the filesystem.
	// Callers (resume) refuse to act on these sessions.
	CWDUnknown bool
	// Source is the backend that produced this session ("claude"), stamped by
	// the source package; the session package itself leaves it empty.
	Source string
}

Session is a parsed view of one Claude Code session transcript.

func FindByID

func FindByID(id string) (*Session, error)

FindByID scans every project directory looking for a session whose filename matches the given UUID. Returns ErrSessionFileMissing when no JSONL exists for the id, and ErrSessionEmpty when the file is present but has no usable content.

func ParseSessionTail

func ParseSessionTail(path string, maxBytes int64) (*Session, error)

ParseSessionTail reads the tail of one JSONL transcript and returns a Session. Returns ErrSessionEmpty when the file exists but has no user message or no extractable label. Returns ErrSessionFileMissing when the file is absent.

func Scan

func Scan() ([]*Session, error)

Scan walks every project directory under ~/.claude/projects and parses each session JSONL in parallel. Sessions returned are sorted by last activity (newest first).

func ScanFiltered

func ScanFiltered(allow map[string]struct{}) ([]*Session, error)

ScanFiltered behaves like Scan but only parses files whose path is in the allow-set. Empty allow returns nothing.

Jump to

Keyboard shortcuts

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