Skip to main content

Session

Struct Session 

Source
pub struct Session {
    pub id: SessionId,
    pub title: String,
    pub status: SessionStatus,
    pub messages: Vec<Message>,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub metadata: Map<String, Value>,
}
Expand description

A conversation session between user and agent

Sessions track the full conversation history and metadata. They are the primary unit of state in the agent system.

§Ownership

The session owns its messages (Vec<Message>). When you add a message, ownership transfers to the session. This is Rust’s ownership model in action.

§Mutability

Methods that modify the session take &mut self:

  • add_message() - adds to the messages vec
  • set_title() - changes the title

Methods that only read take &self:

  • last_message() - returns a reference
  • message_count() - returns a count

Fields§

§id: SessionId

Unique identifier for this session

§title: String

Human-readable title (can be auto-generated)

§status: SessionStatus

Current status of the session

§messages: Vec<Message>

The conversation messages

§created_at: DateTime<Utc>

When the session was created

§updated_at: DateTime<Utc>

When the session was last modified

§metadata: Map<String, Value>

Optional metadata (for extensions)

Implementations§

Source§

impl Session

Source

pub fn new() -> Self

Create a new empty session

Source

pub fn with_id(id: impl Into<String>) -> Self

Create a session with a specific ID (for restoration)

Source

pub fn with_title(self, title: impl Into<String>) -> Self

Set the session title

Source

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

Add a message to the session

This transfers ownership of the message to the session. The session’s updated_at timestamp is also updated.

Source

pub fn add_messages(&mut self, messages: impl IntoIterator<Item = Message>)

Add multiple messages at once

Source

pub fn last_message(&self) -> Option<&Message>

Get the last message, if any

Source

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

Get the last N messages

Source

pub fn messages_by_role(&self, role: Role) -> Vec<&Message>

Get all messages from a specific role

Source

pub fn message_count(&self) -> usize

Get the number of messages

Source

pub fn is_empty(&self) -> bool

Check if the session is empty (no messages)

Source

pub fn set_title(&mut self, title: impl Into<String>)

Set the session title

Source

pub fn set_status(&mut self, status: SessionStatus)

Set the session status

Source

pub fn start_processing(&mut self)

Mark session as processing

Source

pub fn finish_processing(&mut self)

Mark session as active (done processing)

Source

pub fn complete(&mut self)

Mark session as completed

Source

pub fn mark_error(&mut self)

Mark session as errored

Source

pub fn can_accept_messages(&self) -> bool

Check if the session can accept new messages

Source

pub fn set_metadata(&mut self, key: impl Into<String>, value: Value)

Add metadata to the session

Source

pub fn get_metadata(&self, key: &str) -> Option<&Value>

Get metadata value

Source

pub fn auto_title(&mut self)

Generate a title from the first user message

Useful for auto-generating session titles from conversation content.

Source

pub fn duration(&self) -> Duration

Get conversation duration

Trait Implementations§

Source§

impl Clone for Session

Source§

fn clone(&self) -> Self

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
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 SessionSummary

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

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 = !

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