Skip to main content

SessionManager

Struct SessionManager 

Source
pub struct SessionManager { /* private fields */ }
Expand description

A session: its entries, where it is stored, and the current leaf.

Implementations§

Source§

impl SessionManager

Source

pub fn create( cwd: &Path, dir: &Path, id: Option<String>, ) -> Result<SessionManager, SessionError>

A new session stored in dir, created on disk once it has a conversation. id must be a valid session id.

Source

pub fn preview(&self) -> SessionManager

A copy at the same leaf that is never written, to try appends on.

Source

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

A session that is never written.

Source

pub fn open( path: &Path, dir: Option<&Path>, cwd: Option<&Path>, ) -> Result<SessionManager, SessionError>

Opens a session file. A missing file starts a new session at that path. The working directory comes from the header unless given, and new sessions go to dir, else the file’s directory.

Source

pub fn continue_recent( cwd: &Path, dir: &Path, filter_cwd: bool, ) -> Result<SessionManager, SessionError>

The most recently modified session file in dir (one from cwd when filtering), or a new session.

Source

pub fn new_session( &mut self, id: Option<String>, parent: Option<String>, ) -> Option<PathBuf>

Starts over with a fresh header and returns the new file path when persisted.

Source

pub fn take_write_error(&mut self) -> Option<String>

The error of the session’s first failed write, returned once.

Source

pub fn append_message( &mut self, message: Message, ) -> Result<String, SessionError>

Appends a message on the current branch and returns its entry id.

Source

pub fn append_thinking_level_change( &mut self, level: &str, ) -> Result<String, SessionError>

Records a thinking level change.

Source

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

Records a model change.

Source

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

Records a compaction with the current system message, so the prompt survives when the messages before firstKeptEntryId drop out.

Source

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

Stores extension state outside the model context.

Source

pub fn append_custom_message( &mut self, custom_type: &str, content: Content, display: bool, details: Option<Value>, ) -> Result<String, SessionError>

Stores an extension message that is part of the model context.

Source

pub fn append_usage( &mut self, kind: &str, provider: &str, model: &str, usage: Usage, note: Option<String>, ) -> Result<String, SessionError>

Records usage outside the model context, such as a cache warming request’s, attributed to provider and model.

Source

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

Names the session, with line breaks as spaces.

Source

pub fn append_context_edit( &mut self, target_id: &str, replacement: Option<Content>, ) -> Result<String, SessionError>

Edits an earlier model-visible entry on the active branch: None drops it from the context, a replacement swaps its content. Text replacing an assistant or tool result becomes a text block.

Source

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

Sets or clears the label of an entry.

Source

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

The latest session name.

Source

pub fn id(&self) -> &str

The session id.

Source

pub fn file(&self) -> Option<&Path>

The session file, when persisted.

Source

pub fn is_persisted(&self) -> bool

Whether the session is written to disk.

Source

pub fn dir(&self) -> &Path

The directory new session files go to.

Source

pub fn cwd(&self) -> &Path

The session’s working directory.

Source

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

The header.

Source

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

The current leaf entry id.

Source

pub fn entry(&self, id: &str) -> Option<&FileEntry>

An entry by id.

Source

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

The label of an entry.

Source

pub fn documents(&self) -> (Option<&Value>, Vec<&Value>)

The header and the other entries as stored, in file order: what pi’s HTML export embeds.

Source

pub fn entries(&self) -> impl Iterator<Item = &FileEntry>

Every entry except the header, in file order.

Source

pub fn tree(&self) -> SessionTree

pi’s getTree: entries under their parents, children oldest first. Entries whose parent is missing become roots.

Source

pub fn branch_path(&self, from: Option<&str>) -> Vec<&FileEntry>

The path from the root to from (default: the leaf).

Source

pub fn serialize_branch(&self) -> String

pi’s serializeSessionBranch: a fresh header and the current branch with parent ids rechained, as JSONL.

Source

pub fn branch(&mut self, id: &str) -> Result<(), SessionError>

Moves the leaf to an existing entry. The next append starts a branch there.

Source

pub fn reset_leaf(&mut self)

Moves the leaf before the first entry.

Source

pub fn branch_with_summary( &mut self, from: Option<&str>, summary: String, details: Option<Value>, from_hook: Option<bool>, usage: Option<Usage>, ) -> Result<String, SessionError>

Branches to from (or the root) and records a summary of the branch left.

Source

pub fn build_context(&self) -> SessionContext

The model context of the current branch.

Source

pub fn build_projection(&self) -> Projection<'_>

The current branch’s context, entry by entry.

Source

pub fn create_branched_session( &mut self, leaf_id: &str, ) -> Result<Option<PathBuf>, SessionError>

Starts a new session file holding the branch up to leaf_id, with labels on it, and switches to it. Returns the new file when persisted.

Source

pub fn fork_from( source: &Path, cwd: &Path, dir: &Path, id: Option<String>, ) -> Result<SessionManager, SessionError>

Copies every entry of source into a new session file for cwd in dir, with parentSession pointing at the source.

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> 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: 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + 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: Sized + 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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