Skip to main content

Session

Struct Session 

Source
pub struct Session {
    pub header: SessionHeader,
    pub entries: Vec<SessionEntry>,
    pub path: Option<PathBuf>,
    pub session_dir: Option<PathBuf>,
    /* private fields */
}
Expand description

A session manages conversation state and persistence.

Fields§

§header: SessionHeader

Session header

§entries: Vec<SessionEntry>

Session entries (messages, changes, etc.)

§path: Option<PathBuf>

Path to the session file (None for in-memory)

§session_dir: Option<PathBuf>

Base directory for session storage (optional override)

Implementations§

Source§

impl Session

Source

pub async fn new(cli: &Cli, config: &Config) -> Result<Self>

Create a new session from CLI args and config.

Source

pub async fn resume_with_picker( override_dir: Option<&Path>, config: &Config, picker_input_override: Option<String>, ) -> Result<Self>

Resume a session by prompting the user to select from recent sessions.

Source

pub fn in_memory() -> Self

Create an in-memory (ephemeral) session.

Source

pub fn create() -> Self

Create a new session.

Source

pub fn create_with_dir(session_dir: Option<PathBuf>) -> Self

Create a new session with an optional base directory override.

Source

pub fn create_with_dir_and_store( session_dir: Option<PathBuf>, store_kind: SessionStoreKind, ) -> Self

Source

pub async fn open(path: &str) -> Result<Self>

Open an existing session.

Source

pub async fn open_with_diagnostics( path: &str, ) -> Result<(Self, SessionOpenDiagnostics)>

Open an existing session and return diagnostics about any recovered corruption.

Source

pub fn open_from_v2( store: &SessionStoreV2, header: SessionHeader, mode: V2OpenMode, ) -> Result<(Self, SessionOpenDiagnostics)>

Open a session from an already-open V2 store with an explicit read mode.

Source

pub async fn continue_recent_in_dir( override_dir: Option<&Path>, config: &Config, ) -> Result<Self>

Continue the most recent session.

Source

pub async fn save(&mut self) -> Result<()>

Save the session to disk.

Source

pub async fn flush_autosave( &mut self, trigger: AutosaveFlushTrigger, ) -> Result<()>

Flush queued autosave mutations using the requested trigger.

This is the write-behind entry point: no-op when there are no pending mutations, and one persistence operation for all coalesced mutations when pending work exists.

Source

pub async fn flush_autosave_on_shutdown(&mut self) -> Result<()>

Final shutdown flush respecting the configured durability mode.

Source

pub const fn autosave_metrics(&self) -> AutosaveQueueMetrics

Current autosave queue and lifecycle counters for observability.

Source

pub const fn autosave_durability_mode(&self) -> AutosaveDurabilityMode

Source

pub const fn set_autosave_durability_mode( &mut self, mode: AutosaveDurabilityMode, )

Source

pub fn effective_model_for_current_path(&self) -> Option<(String, String)>

Source

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

Source

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

Append a session message entry.

Source

pub fn append_model_message(&mut self, message: Message) -> String

Append a message from the model message types.

Source

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

Source

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

Source

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

Source

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

Append a custom entry (extension state, etc).

Source

pub fn append_bash_execution( &mut self, command: String, output: String, exit_code: i32, cancelled: bool, truncated: bool, full_output_path: Option<String>, ) -> String

Source

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

Get the current session name from the cached value (Gap C).

Source

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

Set the session name by appending a SessionInfo entry.

Source

pub fn append_compaction( &mut self, summary: String, first_kept_entry_id: String, tokens_before: u64, details: Option<Value>, from_hook: Option<bool>, ) -> String

Source

pub fn append_branch_summary( &mut self, from_id: String, summary: String, details: Option<Value>, from_hook: Option<bool>, ) -> String

Source

pub fn ensure_entry_ids(&mut self)

Source

pub fn to_messages(&self) -> Vec<Message>

Convert session entries to model messages (for provider context).

Source

pub fn to_html(&self) -> String

Render the session as a standalone HTML document.

Delegates to render_session_html() for the actual rendering. For non-blocking export, prefer export_snapshot().to_html() which avoids cloning internal caches.

Source

pub fn set_model_header( &mut self, provider: Option<String>, model_id: Option<String>, thinking_level: Option<String>, )

Update header model info.

Source

pub fn set_branched_from(&mut self, path: Option<String>)

Source

pub fn export_snapshot(&self) -> ExportSnapshot

Create a lightweight snapshot for non-blocking HTML export.

Captures only the fields needed by to_html() (header, entries, path), avoiding a full Session::clone() which includes caches, autosave queues, persistence state, and other internal bookkeeping.

Source

pub fn plan_fork_from_user_message(&self, entry_id: &str) -> Result<ForkPlan>

Plan a /fork from a user message entry ID.

Returns the entries to copy into a new session (path to the parent of the selected user message), the new leaf id, and the selected user message text for editor pre-fill.

Source

pub fn get_path_to_entry(&self, entry_id: &str) -> Vec<String>

Get the path from an entry back to the root (inclusive). Returns entry IDs in order from root to the specified entry.

Source

pub fn get_children(&self, entry_id: Option<&str>) -> Vec<String>

Get direct children of an entry.

Source

pub fn list_leaves(&self) -> Vec<String>

List all leaf nodes (entries with no children).

Source

pub fn navigate_to(&mut self, entry_id: &str) -> bool

Navigate to a specific entry, making it the current leaf. Returns true if the entry exists.

Source

pub fn leaf_id(&self) -> Option<&str>

Get the current leaf entry ID.

Source

pub fn init_from_fork_plan(&mut self, plan: ForkPlan)

Initialize the session entries and leaf from a ForkPlan.

This safely applies the new entries and leaf, and rebuilds all internal caches (including the is_linear optimization flag).

Source

pub fn _test_set_leaf_id(&mut self, id: Option<String>)

Set the leaf ID directly (for tests only).

Source

pub fn revert_last_user_message(&mut self) -> bool

Revert the last user message on the current path, effectively abandoning it. This is used during API retries to prevent duplicating the user prompt in the session history.

Source

pub fn reset_leaf(&mut self)

Reset the leaf pointer to root (before any entries).

After calling this, the next appended entry will become a new root entry (parent_id = None). This is used by interactive /tree navigation when re-editing the first user message.

Source

pub fn create_branch_from(&mut self, entry_id: &str) -> bool

Create a new branch starting from a specific entry. Sets the leaf_id to the specified entry so new entries branch from there. Returns true if the entry exists.

Source

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

Get the entry at a specific ID (Gap B: O(1) via entry_index).

Source

pub fn get_entry_mut(&mut self, entry_id: &str) -> Option<&mut SessionEntry>

Get the entry at a specific ID, mutable (Gap B: O(1) via entry_index).

Source

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

Entries along the current leaf path, in chronological order.

Gap A: when is_linear is true (the 99% case — no branching has occurred), this returns all entries directly without building a parent map or tracing the path.

Source

pub fn to_messages_for_current_path(&self) -> Vec<Message>

Convert session entries along the current path to model messages. This follows parent_id links from leaf_id back to root.

Source

pub fn sibling_branches(&self) -> Option<(Option<String>, Vec<SiblingBranch>)>

Find the nearest ancestor that is a fork point (has multiple children) and return its children (sibling branch roots). Each sibling is represented by its branch-root entry ID plus the leaf ID reachable from that root.

Returns (fork_point_id, sibling_leaves) where each sibling leaf is a leaf entry ID reachable through the fork point’s children. The current leaf is included in the list.

Source

pub fn branch_summary(&self) -> BranchInfo

Get a summary of branches in this session.

Source

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

Add a label to an entry.

Trait Implementations§

Source§

impl Clone for Session

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Session

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
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> 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<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
Source§

impl<T> ParallelSend for T

Source§

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

Source§

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