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 vecset_title()- changes the title
Methods that only read take &self:
last_message()- returns a referencemessage_count()- returns a count
Fields§
§id: SessionIdUnique identifier for this session
title: StringHuman-readable title (can be auto-generated)
status: SessionStatusCurrent 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
impl Session
Sourcepub fn with_id(id: impl Into<String>) -> Self
pub fn with_id(id: impl Into<String>) -> Self
Create a session with a specific ID (for restoration)
Sourcepub fn with_title(self, title: impl Into<String>) -> Self
pub fn with_title(self, title: impl Into<String>) -> Self
Set the session title
Sourcepub fn add_message(&mut self, message: Message)
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.
Sourcepub fn add_messages(&mut self, messages: impl IntoIterator<Item = Message>)
pub fn add_messages(&mut self, messages: impl IntoIterator<Item = Message>)
Add multiple messages at once
Sourcepub fn last_message(&self) -> Option<&Message>
pub fn last_message(&self) -> Option<&Message>
Get the last message, if any
Sourcepub fn last_n_messages(&self, n: usize) -> &[Message]
pub fn last_n_messages(&self, n: usize) -> &[Message]
Get the last N messages
Sourcepub fn messages_by_role(&self, role: Role) -> Vec<&Message>
pub fn messages_by_role(&self, role: Role) -> Vec<&Message>
Get all messages from a specific role
Sourcepub fn message_count(&self) -> usize
pub fn message_count(&self) -> usize
Get the number of messages
Sourcepub fn set_status(&mut self, status: SessionStatus)
pub fn set_status(&mut self, status: SessionStatus)
Set the session status
Sourcepub fn start_processing(&mut self)
pub fn start_processing(&mut self)
Mark session as processing
Sourcepub fn finish_processing(&mut self)
pub fn finish_processing(&mut self)
Mark session as active (done processing)
Sourcepub fn mark_error(&mut self)
pub fn mark_error(&mut self)
Mark session as errored
Sourcepub fn can_accept_messages(&self) -> bool
pub fn can_accept_messages(&self) -> bool
Check if the session can accept new messages
Sourcepub fn set_metadata(&mut self, key: impl Into<String>, value: Value)
pub fn set_metadata(&mut self, key: impl Into<String>, value: Value)
Add metadata to the session
Sourcepub fn get_metadata(&self, key: &str) -> Option<&Value>
pub fn get_metadata(&self, key: &str) -> Option<&Value>
Get metadata value
Sourcepub fn auto_title(&mut self)
pub fn auto_title(&mut self)
Generate a title from the first user message
Useful for auto-generating session titles from conversation content.