Skip to main content

SessionManager

Struct SessionManager 

Source
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

Source

pub fn create(cwd: &str, session_dir: Option<&str>) -> SessionManager

Create a new session and persist it to disk.

Source

pub fn open( path: &str, session_dir: Option<&str>, cwd_override: Option<&str>, ) -> SessionManager

Open an existing session from a file path.

Source

pub fn continue_recent(cwd: &str, session_dir: Option<&str>) -> SessionManager

Continue the most recent session, or create a new one if none exists.

Source

pub fn in_memory(cwd: &str) -> SessionManager

Create an in-memory session without file persistence.

Source

pub fn set_session_file(&mut self, session_file: &str)

Switch to a different session file

Source

pub fn new_session(&mut self, options: Option<NewSessionOptions>)

Create a new session with optional ID and parent

Source

pub fn is_persisted(&self) -> bool

Check if session is persisted to disk

Source

pub fn persisted_count(&self) -> usize

Get the number of agent messages that have already been persisted.

Source

pub fn set_persisted_count(&self, count: usize)

Set the number of agent messages that have been persisted.

Source

pub fn get_cwd(&self) -> String

Get working directory

Source

pub fn get_session_dir(&self) -> String

Get session directory

Source

pub fn get_session_id(&self) -> String

Get session ID

Source

pub fn get_session_file(&self) -> Option<String>

Get session file path

Source

pub fn append_message(&mut self, message: AgentMessage) -> String

Append a message as child of current leaf

Source

pub fn append_thinking_level_change(&mut self, thinking_level: &str) -> String

Append a thinking level change

Source

pub fn append_model_change(&mut self, provider: &str, model_id: &str) -> String

Append a model change

Source

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

Source

pub fn append_custom_entry( &mut self, custom_type: &str, data: Option<Value>, ) -> String

Append a custom entry (for extensions)

Source

pub fn append_session_info(&mut self, name: &str) -> String

Append a session info entry (e.g., display name)

Source

pub fn get_session_name(&self) -> Option<String>

Get the current session name from the latest session_info entry

Source

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

Source

pub fn get_leaf_id(&self) -> Option<String>

Get the current leaf ID

Source

pub fn get_leaf_entry(&self) -> Option<SessionEntry>

Get the current leaf entry

Source

pub fn get_entry(&self, id: &str) -> Option<SessionEntry>

Get an entry by ID

Source

pub fn get_children(&self, parent_id: &str) -> Vec<SessionEntry>

Get all direct children of an entry

Source

pub fn get_parent(&self, id: &str) -> Option<SessionEntry>

Get the parent of an entry

Source

pub fn get_label(&self, id: &str) -> Option<String>

Get the label for an entry

Source

pub fn append_label_change( &mut self, target_id: &str, label: Option<&str>, ) -> Result<String, String>

Set or clear a label on an entry

Source

pub fn get_branch(&self, from_id: Option<&str>) -> Vec<SessionEntry>

Walk from entry to root, returning all entries in path order

Source

pub fn get_path_to_root(&self, from_id: &str) -> Vec<SessionEntry>

Get path to root for a given entry

Source

pub fn get_ancestry(&self, from_id: &str) -> Vec<SessionEntry>

Get ancestry (same as path to root)

Source

pub fn get_depth(&self, id: &str) -> i64

Get depth of an entry

Source

pub fn build_session_context(&self) -> SessionContext

Build the session context (what gets sent to the LLM)

Source

pub fn get_header(&self) -> Option<SessionHeader>

Get session header

Source

pub fn get_entries(&self) -> Vec<SessionEntry>

Get all session entries (excludes header)

Source

pub fn get_tree(&self, _id: Uuid) -> Result<Vec<SessionTreeNode>, Error>

Get the session as a tree structure If id is provided, returns tree for that session (backward compat)

Source

pub fn branch(&mut self, branch_from_id: &str) -> Result<(), String>

Start a new branch from an earlier entry

Source

pub fn reset_leaf(&mut self)

Reset the leaf pointer to null (before any entries)

Source

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

Source

pub fn add_label( &mut self, target_id: &str, label: &str, ) -> Result<String, String>

Add a label to the session

Source

pub fn remove_label(&mut self, target_id: &str) -> Result<String, String>

Remove a label from an entry

Source

pub fn get_latest_compaction_entry(&self) -> Option<SessionEntry>

Get the latest compaction entry

Source

pub fn get_compaction_entries(&self) -> Vec<SessionEntry>

Get all compaction entries

Source

pub fn get_session_stats(&self) -> SessionStats

Get session statistics

Source

pub async fn list( cwd: &str, session_dir: Option<&str>, ) -> Result<Vec<SessionInfo>, Error>

List all sessions for a directory

Source

pub async fn list_all() -> Result<Vec<SessionInfo>, Error>

List all sessions across all project directories

Source

pub fn fork_from( source_path: &str, target_cwd: &str, session_dir: Option<&str>, ) -> Result<SessionManager, String>

Fork a session from another project directory into the current project

Source

pub fn delete_session(path: &str) -> Result<(), Error>

Delete a session

Source

pub fn rename_session(&mut self, name: &str) -> String

Rename a session (set its display name)

Source

pub async fn new() -> Result<SessionManager, Error>

Create a new SessionManager (async for backward compatibility)

Source

pub async fn new_async() -> Result<SessionManager, Error>

Create a new SessionManager (async for backward compatibility)

Source

pub fn session_path(&self, id: &Uuid) -> PathBuf

Get the session file path for a given session ID

Source

pub async fn list_sessions(&self) -> Result<Vec<SessionMeta>, Error>

List all sessions (backward compat)

Source

pub async fn save( &self, _id: Uuid, _entries: &[SessionEntry], ) -> Result<(), Error>

Save entries (backward compat)

Source

pub async fn load(&self, _id: Uuid) -> Result<Vec<SessionEntry>, Error>

Load entries (backward compat)

Source

pub async fn delete(&self, id: Uuid) -> Result<(), Error>

Delete a session (backward compat)

Source

pub async fn branch_from( &self, parent_id: Uuid, entry_id: Uuid, ) -> Result<(Uuid, Vec<SessionEntry>), Error>

Create a branch from an existing session at a given entry

Source

pub async fn get_branch_info( &self, _id: Uuid, ) -> Result<Option<BranchInfo>, Error>

Get branch info for a session

Source

pub async fn get_tree_async( &self, _id: Uuid, ) -> Result<Vec<SessionTreeNode>, Error>

Get tree for a specific session (backward compat)

Source

pub async fn save_meta(&self, _meta: &SessionMeta) -> Result<(), Error>

Save metadata (backward compat)

Source

pub async fn load_meta(&self, _id: Uuid) -> Result<Option<SessionMeta>, Error>

Load metadata (backward compat)

Source

pub async fn create_session(&mut self) -> Result<SessionMeta, Error>

Create a new session (backward compat)

Source

pub fn branch_from_entry(&self, entry_id: &str) -> Result<String, String>

Fork from current session at a specific entry, creating a new session file. Synchronous.

Trait Implementations§

Source§

impl Clone for SessionManager

Source§

fn clone(&self) -> SessionManager

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> GetSetFdFlags for T

Source§

fn get_fd_flags(&self) -> Result<FdFlags, Error>
where T: AsFilelike,

Query the “status” flags for the self file descriptor.
Source§

fn new_set_fd_flags(&self, fd_flags: FdFlags) -> Result<SetFdFlags<T>, Error>
where T: AsFilelike,

Create a new SetFdFlags value for use with set_fd_flags. Read more
Source§

fn set_fd_flags(&mut self, set_fd_flags: SetFdFlags<T>) -> Result<(), Error>
where T: AsFilelike,

Set the “status” flags for the self file descriptor. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Pointee for T

Source§

type Pointer = u32

Source§

fn debug( pointer: <T as Pointee>::Pointer, f: &mut Formatter<'_>, ) -> Result<(), Error>

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more