Skip to main content

tower_llm/
memory.rs

1//! Session trait for compatibility with SqliteSession
2
3use async_trait::async_trait;
4use std::fmt::Debug;
5
6use crate::error::Result;
7use crate::items::{Message, RunItem};
8
9/// Defines the interface for session storage implementations.
10#[async_trait]
11pub trait Session: Send + Sync + Debug {
12    /// Returns the unique identifier for the session.
13    fn session_id(&self) -> &str;
14
15    /// Retrieves a list of `RunItem`s from the session.
16    async fn get_items(&self, limit: Option<usize>) -> Result<Vec<RunItem>>;
17
18    /// Adds a vector of `RunItem`s to the session.
19    async fn add_items(&self, items: Vec<RunItem>) -> Result<()>;
20
21    /// Removes and returns the most recent `RunItem` from the session.
22    async fn pop_item(&self) -> Result<Option<RunItem>>;
23
24    /// Clears all items from the session, effectively resetting the conversation.
25    async fn clear_session(&self) -> Result<()>;
26
27    /// Retrieves the conversation history as a vector of `Message`s.
28    async fn get_messages(&self, limit: Option<usize>) -> Result<Vec<Message>> {
29        let items = self.get_items(limit).await?;
30        Ok(crate::items::ItemHelpers::to_messages(&items))
31    }
32}