pub struct SessionManager { /* private fields */ }Expand description
Manages conversation sessions as append-only trees stored in JSONL files.
SessionManager handles session persistence, branching, and tree traversal. Each session is stored as a JSONL file where each line is a session entry. Entries form a tree structure allowing for session branching and history.
Implementations§
Source§impl SessionManager
impl SessionManager
Sourcepub fn create(cwd: &str, session_dir: Option<&str>) -> Self
pub fn create(cwd: &str, session_dir: Option<&str>) -> Self
Create a new session and persist it to disk.
Sourcepub fn open(
path: &str,
session_dir: Option<&str>,
cwd_override: Option<&str>,
) -> Self
pub fn open( path: &str, session_dir: Option<&str>, cwd_override: Option<&str>, ) -> Self
Open an existing session from a file path.
Sourcepub fn continue_recent(cwd: &str, session_dir: Option<&str>) -> Self
pub fn continue_recent(cwd: &str, session_dir: Option<&str>) -> Self
Continue the most recent session, or create a new one if none exists.
Sourcepub fn set_session_file(&mut self, session_file: &str)
pub fn set_session_file(&mut self, session_file: &str)
Switch to a different session file
Sourcepub fn new_session(&mut self, options: Option<NewSessionOptions>)
pub fn new_session(&mut self, options: Option<NewSessionOptions>)
Create a new session with optional ID and parent
Sourcepub fn is_persisted(&self) -> bool
pub fn is_persisted(&self) -> bool
Check if session is persisted to disk
Sourcepub fn validate_session_id(id: &str) -> bool
pub fn validate_session_id(id: &str) -> bool
Validate a session ID format.
Checks that the session_id conforms to the expected UUID format.
Returns true if valid.
Sourcepub fn is_readonly(&self) -> bool
pub fn is_readonly(&self) -> bool
Returns true if this session is in read-only mode.
A session is read-only when:
- It was opened without write permissions
- Its underlying file is set to read-only on the filesystem
Read-only sessions reject any append/branch operations.
Sourcepub fn can_append(&self) -> bool
pub fn can_append(&self) -> bool
Check if appending to this session is allowed.
Combination of !is_readonly() + in-memory or writable backing file.
Sourcepub fn persisted_count(&self) -> usize
pub fn persisted_count(&self) -> usize
Get the number of agent messages that have already been persisted.
Sourcepub fn set_persisted_count(&self, count: usize)
pub fn set_persisted_count(&self, count: usize)
Set the number of agent messages that have been persisted.
Sourcepub fn get_session_dir(&self) -> String
pub fn get_session_dir(&self) -> String
Get session directory
Sourcepub fn get_session_id(&self) -> String
pub fn get_session_id(&self) -> String
Get session ID
Sourcepub fn get_session_file(&self) -> Option<String>
pub fn get_session_file(&self) -> Option<String>
Get session file path
Sourcepub fn cleanup_if_empty(&self)
pub fn cleanup_if_empty(&self)
Remove the session file from disk if the session has no real conversation (i.e., no user message was ever persisted). Called before switching to a new session or quitting.
Sourcepub fn append_message(&mut self, message: AgentMessage) -> String
pub fn append_message(&mut self, message: AgentMessage) -> String
Append a message as child of current leaf
Sourcepub fn append_thinking_level_change(&mut self, thinking_level: &str) -> String
pub fn append_thinking_level_change(&mut self, thinking_level: &str) -> String
Append a thinking level change
Sourcepub fn append_model_change(&mut self, provider: &str, model_id: &str) -> String
pub fn append_model_change(&mut self, provider: &str, model_id: &str) -> String
Append a model change
Sourcepub fn append_compaction(
&mut self,
summary: &str,
_first_kept_entry_id: &str,
tokens_before: i64,
_details: Option<Value>,
_from_hook: Option<bool>,
) -> String
pub fn append_compaction( &mut self, summary: &str, _first_kept_entry_id: &str, tokens_before: i64, _details: Option<Value>, _from_hook: Option<bool>, ) -> String
Append a compaction summary
Sourcepub fn append_custom_entry(
&mut self,
custom_type: &str,
data: Option<Value>,
) -> String
pub fn append_custom_entry( &mut self, custom_type: &str, data: Option<Value>, ) -> String
Append a custom entry (for extensions)
Sourcepub fn append_session_info(&mut self, name: &str) -> String
pub fn append_session_info(&mut self, name: &str) -> String
Append a session info entry (e.g., display name)
Sourcepub fn get_session_name(&self) -> Option<String>
pub fn get_session_name(&self) -> Option<String>
Get the current session name from the latest session_info entry
Sourcepub fn append_custom_message_entry(
&mut self,
custom_type: &str,
content: ContentValue,
display: bool,
details: Option<Value>,
) -> String
pub fn append_custom_message_entry( &mut self, custom_type: &str, content: ContentValue, display: bool, details: Option<Value>, ) -> String
Append a custom message entry (for extensions) that participates in LLM context
Sourcepub fn get_leaf_id(&self) -> Option<String>
pub fn get_leaf_id(&self) -> Option<String>
Get the current leaf ID
Sourcepub fn get_leaf_entry(&self) -> Option<SessionEntry>
pub fn get_leaf_entry(&self) -> Option<SessionEntry>
Get the current leaf entry
Sourcepub fn get_entry(&self, id: &str) -> Option<SessionEntry>
pub fn get_entry(&self, id: &str) -> Option<SessionEntry>
Get an entry by ID
Sourcepub fn get_children(&self, parent_id: &str) -> Vec<SessionEntry>
pub fn get_children(&self, parent_id: &str) -> Vec<SessionEntry>
Get all direct children of an entry
Sourcepub fn get_parent(&self, id: &str) -> Option<SessionEntry>
pub fn get_parent(&self, id: &str) -> Option<SessionEntry>
Get the parent of an entry
Sourcepub fn append_label_change(
&mut self,
target_id: &str,
label: Option<&str>,
) -> Result<String, String>
pub fn append_label_change( &mut self, target_id: &str, label: Option<&str>, ) -> Result<String, String>
Set or clear a label on an entry
Sourcepub fn get_branch(&self, from_id: Option<&str>) -> Vec<SessionEntry>
pub fn get_branch(&self, from_id: Option<&str>) -> Vec<SessionEntry>
Walk from entry to root, returning all entries in path order
Sourcepub fn get_path_to_root(&self, from_id: &str) -> Vec<SessionEntry>
pub fn get_path_to_root(&self, from_id: &str) -> Vec<SessionEntry>
Get path to root for a given entry
Sourcepub fn get_ancestry(&self, from_id: &str) -> Vec<SessionEntry>
pub fn get_ancestry(&self, from_id: &str) -> Vec<SessionEntry>
Get ancestry (same as path to root)
Sourcepub fn build_session_context(&self) -> SessionContext
pub fn build_session_context(&self) -> SessionContext
Build the session context (what gets sent to the LLM)
Sourcepub fn get_header(&self) -> Option<SessionHeader>
pub fn get_header(&self) -> Option<SessionHeader>
Get session header
Sourcepub fn get_entries(&self) -> Vec<SessionEntry>
pub fn get_entries(&self) -> Vec<SessionEntry>
Get all session entries (excludes header)
Sourcepub fn get_tree(&self, _id: Uuid) -> Result<Vec<SessionTreeNode>>
pub fn get_tree(&self, _id: Uuid) -> Result<Vec<SessionTreeNode>>
Get the session as a tree structure If id is provided, returns tree for that session (backward compat)
Sourcepub fn branch(&mut self, branch_from_id: &str) -> Result<(), String>
pub fn branch(&mut self, branch_from_id: &str) -> Result<(), String>
Start a new branch from an earlier entry
Sourcepub fn reset_leaf(&mut self)
pub fn reset_leaf(&mut self)
Reset the 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>,
) -> String
pub fn branch_with_summary( &mut self, branch_from_id: Option<&str>, summary: &str, _details: Option<Value>, _from_hook: Option<bool>, ) -> String
Start a new branch with a summary of the abandoned path
Sourcepub fn add_label(
&mut self,
target_id: &str,
label: &str,
) -> Result<String, String>
pub fn add_label( &mut self, target_id: &str, label: &str, ) -> Result<String, String>
Add a label to the session
Sourcepub fn remove_label(&mut self, target_id: &str) -> Result<String, String>
pub fn remove_label(&mut self, target_id: &str) -> Result<String, String>
Remove a label from an entry
Sourcepub fn get_latest_compaction_entry(&self) -> Option<SessionEntry>
pub fn get_latest_compaction_entry(&self) -> Option<SessionEntry>
Get the latest compaction entry
Sourcepub fn get_compaction_entries(&self) -> Vec<SessionEntry>
pub fn get_compaction_entries(&self) -> Vec<SessionEntry>
Get all compaction entries
Sourcepub fn get_session_stats(&self) -> SessionStats
pub fn get_session_stats(&self) -> SessionStats
Get session statistics
Sourcepub async fn list(
cwd: &str,
session_dir: Option<&str>,
) -> Result<Vec<SessionInfo>>
pub async fn list( cwd: &str, session_dir: Option<&str>, ) -> Result<Vec<SessionInfo>>
List all sessions for a directory
Sourcepub async fn list_all() -> Result<Vec<SessionInfo>>
pub async fn list_all() -> Result<Vec<SessionInfo>>
List all sessions across all project directories
Sourcepub fn fork_from(
source_path: &str,
target_cwd: &str,
session_dir: Option<&str>,
) -> Result<Self, String>
pub fn fork_from( source_path: &str, target_cwd: &str, session_dir: Option<&str>, ) -> Result<Self, String>
Fork a session from another project directory into the current project
Sourcepub fn delete_session(path: &str) -> Result<()>
pub fn delete_session(path: &str) -> Result<()>
Delete a session
Sourcepub fn rename_session(&mut self, name: &str) -> String
pub fn rename_session(&mut self, name: &str) -> String
Rename a session (set its display name)
Sourcepub async fn new() -> Result<Self>
pub async fn new() -> Result<Self>
Create a new SessionManager (async for backward compatibility)
Sourcepub async fn new_async() -> Result<Self>
pub async fn new_async() -> Result<Self>
Create a new SessionManager (async for backward compatibility)
Sourcepub fn session_path(&self, id: &Uuid) -> PathBuf
pub fn session_path(&self, id: &Uuid) -> PathBuf
Get the session file path for a given session ID
Sourcepub async fn list_sessions(&self) -> Result<Vec<SessionMeta>>
pub async fn list_sessions(&self) -> Result<Vec<SessionMeta>>
List all sessions (backward compat)
Sourcepub async fn save(&self, _id: Uuid, _entries: &[SessionEntry]) -> Result<()>
pub async fn save(&self, _id: Uuid, _entries: &[SessionEntry]) -> Result<()>
Save entries (backward compat)
Sourcepub async fn load(&self, _id: Uuid) -> Result<Vec<SessionEntry>>
pub async fn load(&self, _id: Uuid) -> Result<Vec<SessionEntry>>
Load entries (backward compat)
Sourcepub async fn branch_from(
&self,
parent_id: Uuid,
entry_id: Uuid,
) -> Result<(Uuid, Vec<SessionEntry>)>
pub async fn branch_from( &self, parent_id: Uuid, entry_id: Uuid, ) -> Result<(Uuid, Vec<SessionEntry>)>
Create a branch from an existing session at a given entry
Sourcepub async fn get_branch_info(&self, _id: Uuid) -> Result<Option<BranchInfo>>
pub async fn get_branch_info(&self, _id: Uuid) -> Result<Option<BranchInfo>>
Get branch info for a session
Sourcepub async fn get_tree_async(&self, _id: Uuid) -> Result<Vec<SessionTreeNode>>
pub async fn get_tree_async(&self, _id: Uuid) -> Result<Vec<SessionTreeNode>>
Get tree for a specific session (backward compat)
Sourcepub async fn save_meta(&self, _meta: &SessionMeta) -> Result<()>
pub async fn save_meta(&self, _meta: &SessionMeta) -> Result<()>
Save metadata (backward compat)
Sourcepub async fn load_meta(&self, _id: Uuid) -> Result<Option<SessionMeta>>
pub async fn load_meta(&self, _id: Uuid) -> Result<Option<SessionMeta>>
Load metadata (backward compat)
Sourcepub async fn create_session(&mut self) -> Result<SessionMeta>
pub async fn create_session(&mut self) -> Result<SessionMeta>
Create a new session (backward compat)