sessions

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package sessions provides JSONL-based conversation logging for session persistence and resume. It operates alongside the existing JSON session system in config/session.go — that system tracks config state (endpoint, model, NSFW mode) while this package handles append-only conversation replay.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LogEntry

type LogEntry struct {
	Type      string    `json:"type"` // "user", "assistant", "tool_call", "tool_result", "system"
	Content   string    `json:"content"`
	Role      string    `json:"role,omitempty"`
	ToolName  string    `json:"tool_name,omitempty"`
	ToolID    string    `json:"tool_id,omitempty"`
	Timestamp time.Time `json:"timestamp"`
}

LogEntry represents a single line in the JSONL session log.

func ReadSession

func ReadSession(path string) ([]LogEntry, error)

ReadSession reads all entries from a JSONL file. Malformed lines are silently skipped so that a partially-corrupt file can still be recovered.

type Manager

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

Manager coordinates session writing, listing, and auto-resume for a project.

func NewManager

func NewManager(cwd string) (*Manager, error)

NewManager creates a Manager scoped to the project that contains cwd. The projectID is derived from the git repository root (if available) or the cwd itself, hashed to a short hex string.

func (*Manager) Close

func (m *Manager) Close() error

Close flushes and closes the writer.

func (*Manager) GetRecentSession

func (m *Manager) GetRecentSession(maxAge time.Duration) (*SessionInfo, error)

GetRecentSession returns the most recently modified session that is younger than maxAge, or nil if none qualifies.

func (*Manager) ListSessions

func (m *Manager) ListSessions() ([]SessionInfo, error)

ListSessions returns all sessions for this project.

func (*Manager) LogTurn

func (m *Manager) LogTurn(role, content string) error

LogTurn writes a conversation turn. role should be one of the LogEntry.Type values: "user", "assistant", "tool_call", "tool_result", "system".

func (*Manager) ProjectID

func (m *Manager) ProjectID() string

ProjectID returns the computed project hash.

func (*Manager) ResumeSession

func (m *Manager) ResumeSession(id string) ([]LogEntry, error)

ResumeSession loads entries from an existing JSONL session and opens a new writer that appends to it.

func (*Manager) SessionDir

func (m *Manager) SessionDir() string

SessionDir returns the project-specific session directory.

func (*Manager) SessionID

func (m *Manager) SessionID() string

SessionID returns the current session ID, or empty if none is active.

func (*Manager) StartSession

func (m *Manager) StartSession() error

StartSession creates a new JSONL log file and sets up the writer.

type SessionInfo

type SessionInfo struct {
	ID         string
	Path       string
	Title      string // derived from first user message
	EntryCount int
	CreatedAt  time.Time
	UpdatedAt  time.Time // file mtime
}

SessionInfo summarises a JSONL session file without loading every entry.

func ListSessions

func ListSessions(sessionDir string) ([]SessionInfo, error)

ListSessions scans sessionDir for .jsonl files and returns metadata sorted by modification time (newest first).

type SessionWriter

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

SessionWriter appends LogEntry records to a JSONL file.

func NewSessionWriter

func NewSessionWriter(sessionDir string, sessionID string) (*SessionWriter, error)

NewSessionWriter creates a writer that appends to <sessionDir>/<sessionID>.jsonl, creating parent directories as needed.

func (*SessionWriter) Close

func (w *SessionWriter) Close() error

Close flushes and closes the underlying file.

func (*SessionWriter) Path

func (w *SessionWriter) Path() string

Path returns the absolute path of the JSONL file.

func (*SessionWriter) WriteEntry

func (w *SessionWriter) WriteEntry(entry LogEntry) error

WriteEntry appends a single JSON line. Ephemeral types (progress, typing, ping) are silently dropped.

Jump to

Keyboard shortcuts

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