pub struct SessionManager { /* private fields */ }Expand description
Manages conversation sessions as append-only trees in JSONL files.
Each entry has an id and parentId forming a tree structure. Appending creates a child of the current leaf. Branching moves the leaf to an earlier entry, allowing new branches without modifying history.
Implementations§
Source§impl SessionManager
impl SessionManager
Sourcepub fn with_session(
session: Session,
session_dir: PathBuf,
cwd: PathBuf,
persist: bool,
) -> Self
pub fn with_session( session: Session, session_dir: PathBuf, cwd: PathBuf, persist: bool, ) -> Self
Create a SessionManager wrapping an existing Session.
Sourcepub fn new_session(&mut self, options: Option<&NewSessionOptions>)
pub fn new_session(&mut self, options: Option<&NewSessionOptions>)
Create a new session (overwrites current entries). Pi-compatible: defers writing to disk until first assistant message.
Sourcepub fn ensure_flushed(&mut self)
pub fn ensure_flushed(&mut self)
Ensure the session file has been written (lazy write). Migrates from in-memory to file-backed storage, writing header + all entries. Called before first assistant message.
pub fn is_persisted(&self) -> bool
pub fn cwd(&self) -> &Path
pub fn session_dir(&self) -> &Path
Sourcepub fn uses_default_session_dir(&self) -> bool
pub fn uses_default_session_dir(&self) -> bool
Returns true if using the default cwd-encoded session directory.
pub fn session_id(&self) -> String
pub fn session_file(&self) -> Option<PathBuf>
pub fn leaf_id(&self) -> Option<String>
Sourcepub fn session_name(&self) -> Option<String>
pub fn session_name(&self) -> Option<String>
Get the current session name.
Sourcepub fn session_mut(&mut self) -> &mut Session
pub fn session_mut(&mut self) -> &mut Session
Get the underlying Session mutable reference.
Sourcepub fn into_session(self) -> Session
pub fn into_session(self) -> Session
Consume and return the inner Session.
Sourcepub fn get_leaf_entry(&self) -> Option<SessionEntry>
pub fn get_leaf_entry(&self) -> Option<SessionEntry>
Get the current leaf entry (pi-compatible).
Sourcepub fn get_tree(&self) -> Vec<SessionTreeNode>
pub fn get_tree(&self) -> Vec<SessionTreeNode>
Get the session as a tree structure with resolved children and labels (pi-compatible).
Sourcepub fn get_entries(&self) -> Vec<SessionEntry>
pub fn get_entries(&self) -> Vec<SessionEntry>
Get all session entries (excludes header). Pi-compatible.
pub fn append_message(&mut self, message: &AgentMessage) -> String
pub fn append_thinking_level_change(&mut self, thinking_level: &str) -> String
pub fn append_model_change(&mut self, provider: &str, model_id: &str) -> String
pub fn append_session_info(&mut self, name: &str) -> String
pub fn append_compaction( &mut self, summary: &str, first_kept_entry_id: &str, tokens_before: u64, details: Option<Value>, from_hook: Option<bool>, ) -> String
pub fn append_branch_summary( &mut self, from_id: &str, summary: &str, details: Option<Value>, from_hook: Option<bool>, ) -> String
pub fn append_label_change( &mut self, target_id: &str, label: Option<&str>, ) -> String
pub fn append_custom_entry(&mut self, custom_type: &str, data: Value) -> String
pub fn append_active_tools_change( &mut self, active_tool_names: &[String], ) -> String
pub fn append_custom_message_entry( &mut self, custom_type: &str, content: Value, display: bool, details: Option<Value>, ) -> String
Sourcepub fn find_entries_by_type(&self, type_name: &str) -> Vec<SessionEntry>
pub fn find_entries_by_type(&self, type_name: &str) -> Vec<SessionEntry>
Find all entries of a given type (pi-compatible).
Sourcepub fn entries(&self) -> Vec<SessionEntry>
pub fn entries(&self) -> Vec<SessionEntry>
Get all entries (excludes header).
Sourcepub fn entry(&self, id: &str) -> Option<SessionEntry>
pub fn entry(&self, id: &str) -> Option<SessionEntry>
Look up an entry by id.
Sourcepub fn children(&self, parent_id: &str) -> Vec<SessionEntry>
pub fn children(&self, parent_id: &str) -> Vec<SessionEntry>
Get all direct children of an entry.
Sourcepub fn branch(&self, from_id: Option<&str>) -> Vec<SessionEntry>
pub fn branch(&self, from_id: Option<&str>) -> Vec<SessionEntry>
Walk from entry to root, returning all entries in path order.
Sourcepub fn build_session_context(&self) -> SessionContext
pub fn build_session_context(&self) -> SessionContext
Build the session context (messages for LLM with compaction handling). Pi-compatible: delegates to Session::build_context.
Sourcepub fn set_branch(&mut self, branch_from_id: &str) -> Result<(), String>
pub fn set_branch(&mut self, branch_from_id: &str) -> Result<(), String>
Move leaf pointer to an earlier entry (starts a new branch). Pi-compatible: delegates to Session::set_leaf_id.
Sourcepub fn reset_leaf(&mut self)
pub fn reset_leaf(&mut self)
Reset leaf pointer to null (before any entries).
Sourcepub fn branch_with_summary(
&mut self,
branch_from_id: Option<&str>,
summary: &str,
details: Option<Value>,
from_hook: Option<bool>,
) -> Result<String, String>
pub fn branch_with_summary( &mut self, branch_from_id: Option<&str>, summary: &str, details: Option<Value>, from_hook: Option<bool>, ) -> Result<String, String>
Move leaf pointer with a branch summary entry. Pi-compatible: delegates to Session::move_to.
Sourcepub fn create_with_options(
cwd: &Path,
session_dir: Option<&Path>,
options: Option<&NewSessionOptions>,
) -> Self
pub fn create_with_options( cwd: &Path, session_dir: Option<&Path>, options: Option<&NewSessionOptions>, ) -> Self
Create a new session with options (pi-compatible).
Sourcepub fn open(
path: &Path,
session_dir: Option<&Path>,
cwd_override: Option<&Path>,
) -> Self
pub fn open( path: &Path, session_dir: Option<&Path>, cwd_override: Option<&Path>, ) -> Self
Open a specific session file.
Sourcepub fn continue_recent(cwd: &Path, session_dir: Option<&Path>) -> Self
pub fn continue_recent(cwd: &Path, session_dir: Option<&Path>) -> Self
Continue the most recent session, or create new if none.
Sourcepub fn fork_from(
source_path: &Path,
target_cwd: &Path,
session_dir: Option<&Path>,
options: Option<&NewSessionOptions>,
) -> Result<Self>
pub fn fork_from( source_path: &Path, target_cwd: &Path, session_dir: Option<&Path>, options: Option<&NewSessionOptions>, ) -> Result<Self>
Fork a session from another project directory into the current one. Pi-compatible: creates a new session with the full history from the source session.
Sourcepub fn create_branched_session(&mut self, leaf_id: &str) -> Option<PathBuf>
pub fn create_branched_session(&mut self, leaf_id: &str) -> Option<PathBuf>
Create a branched session from a specific leaf path. Extracts the linear path from root to leaf into a new session file. Pi-compatible: creates a new session file, preserving labels.