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.)
SessionMessageEntry- Agent messageModelChangeEntry- Model switchThinkingLevelChangeEntry- Thinking level changeCompactionEntry- Context compactionBranchSummaryEntry- Branch summaryCustomEntry- 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.
getTree
Get the session tree structure.listSessions
List all available sessions.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/: