Skip to main content
The SessionManager class handles session persistence, history tracking, and session tree operations. Each session is stored as an append-only log of entries.

Import

Constructor

string
Directory for session storage. Default: ~/.pi/sessions/

Methods

createSession

Create a new session.
string
required
Working directory for the session
NewSessionOptions
object

loadSession

Load an existing session.
string
required
The session ID to load

appendEntry

Append an entry to the current session.
SessionEntry
required
Entry to append (message, model change, compaction, etc.)
Entry Types:
  • SessionMessageEntry - Agent message
  • ModelChangeEntry - Model switch
  • ThinkingLevelChangeEntry - Thinking level change
  • CompactionEntry - Context compaction
  • BranchSummaryEntry - Branch summary
  • CustomEntry - Extension data (not sent to LLM)
  • CustomMessageEntry - Extension message (sent to LLM)

readEntries

Read entries from a specific point in the session.
string
Entry ID to start from. If omitted, returns all entries.
Returns entries in chronological order with proper parent-child relationships.

getTree

Get the session tree structure.
Returns a defensive copy of the session tree showing the branching structure.

listSessions

List all available sessions.
Returns metadata for all sessions, sorted by creation time.

deleteSession

Delete a session.
string
required
The session ID to delete

Session Context Building

buildSessionContext

Build LLM context from session entries.
SessionEntry[]
required
Session entries to process
string
required
System prompt for the agent
Model
required
Current model (for model-specific formatting)
object

Entry Types

SessionMessageEntry

Stores an agent message (user, assistant, or tool result).

CompactionEntry

Records a context compaction operation.

CustomEntry

Extension-specific data (not sent to LLM).

CustomMessageEntry

Extension message sent to LLM.

Example

Session File Format

Sessions are stored as newline-delimited JSON (NDJSON) in ~/.pi/sessions/:
Each line in a session file is a JSON object: