Skip to main content

Session

Struct Session 

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

A conversation session with full history

Uses Arc<Vec> internally for efficient forking (copy-on-write).

Implementations§

Source§

impl Session

Source

pub fn new() -> Self

Create a new empty session

Source

pub fn with_id(id: SessionId) -> Self

Create a session with a specific ID (for loading)

Source

pub fn id(&self) -> &SessionId

Get the session ID

Source

pub fn version(&self) -> u32

Get the session version

Source

pub fn messages(&self) -> &[Message]

Get all messages

Source

pub fn messages_mut(&mut self) -> &mut Vec<Message>

Get mutable access to messages (triggers CoW if Arc is shared)

Source

pub fn created_at(&self) -> SystemTime

Get creation time

Source

pub fn updated_at(&self) -> SystemTime

Get last update time

Source

pub fn push(&mut self, message: Message)

Add a message to the session

Updates the timestamp. For adding multiple messages, prefer push_batch.

Source

pub fn push_batch(&mut self, messages: Vec<Message>)

Add multiple messages in one operation (single timestamp update)

More efficient than multiple push calls when adding many messages.

Source

pub fn touch(&mut self)

Explicitly update the timestamp

Call this after bulk operations that don’t update timestamps automatically.

Source

pub fn has_pending_boundary(&self) -> bool

Whether the conversation has a pending turn boundary.

Returns true if the last message is User or ToolResults, meaning the conversation is waiting for an assistant turn and run_pending can resume without a new user message.

Source

pub fn last_n(&self, n: usize) -> &[Message]

Get the last N messages

Source

pub fn total_tokens(&self) -> u64

Count total tokens used.

Source

pub fn total_usage(&self) -> Usage

Get total usage statistics for the session.

Source

pub fn record_usage(&mut self, turn_usage: Usage)

Update cumulative usage after an LLM call.

Source

pub fn set_system_prompt(&mut self, prompt: String)

Set a system prompt (adds or replaces System message at start)

Source

pub fn append_system_context_blocks( &mut self, appends: &[PendingSystemContextAppend], )

Append one or more runtime system-context blocks to the canonical system prompt.

Source

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

Get the last assistant message text content.

Source

pub fn tool_call_count(&self) -> usize

Count tool calls made

Source

pub fn metadata(&self) -> &Map<String, Value>

Get metadata

Source

pub fn set_metadata(&mut self, key: &str, value: Value)

Set a metadata value

Source

pub fn remove_metadata(&mut self, key: &str)

Remove a metadata value.

Source

pub fn set_session_metadata( &mut self, metadata: SessionMetadata, ) -> Result<(), Error>

Store SessionMetadata in the session metadata map.

Source

pub fn session_metadata(&self) -> Option<SessionMetadata>

Load SessionMetadata from the session metadata map.

Source

pub fn set_system_context_state( &mut self, state: SessionSystemContextState, ) -> Result<(), Error>

Store durable system-context control state in the session metadata map.

Source

pub fn system_context_state(&self) -> Option<SessionSystemContextState>

Load durable system-context control state from the session metadata map.

Source

pub fn set_deferred_turn_state( &mut self, state: SessionDeferredTurnState, ) -> Result<(), Error>

Store durable deferred-turn control state in the session metadata map.

Source

pub fn deferred_turn_state(&self) -> Option<SessionDeferredTurnState>

Load durable deferred-turn control state from the session metadata map.

Source

pub fn set_build_state(&mut self, state: SessionBuildState) -> Result<(), Error>

Store recoverable build-only session state in the session metadata map.

Source

pub fn build_state(&self) -> Option<SessionBuildState>

Load recoverable build-only session state from the session metadata map.

Source

pub fn set_tool_visibility_state( &mut self, state: SessionToolVisibilityState, ) -> Result<(), Error>

Store durable tool-visibility control state in the session metadata map.

Source

pub fn tool_visibility_state(&self) -> Option<SessionToolVisibilityState>

Load durable tool-visibility control state from the session metadata map.

Source

pub fn set_mob_tool_authority_context( &mut self, authority_context: Option<MobToolAuthorityContext>, ) -> Result<(), Error>

Store typed mob operator authority inside canonical build-state metadata.

Source

pub fn mob_tool_authority_context(&self) -> Option<MobToolAuthorityContext>

Load typed mob operator authority from canonical build-state metadata.

Source

pub fn fork_at(&self, index: usize) -> Self

Fork the session at a specific message index

Creates a new session with a subset of messages. The messages are copied (not shared) since the new session has a different prefix.

Source

pub fn fork(&self) -> Self

Fork the entire session (full history)

This is O(1) - the new session shares the message buffer via Arc. Copy-on-write occurs when either session mutates its messages.

Trait Implementations§

Source§

impl Clone for Session

Source§

fn clone(&self) -> Session

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
Source§

impl Default for Session

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Session

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<&Session> for SessionMeta

Source§

fn from(session: &Session) -> Self

Converts to this type from the input type.
Source§

impl Serialize for Session

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. 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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,