Expand description
§SQLite-Based Session Storage
This module provides a persistent session storage implementation using SQLite,
enabling agents to maintain conversation history across multiple runs and
application restarts. The SqliteSession struct implements the Session
trait, ensuring it can be used interchangeably with other session stores.
§Features
- Persistence: Conversation history is stored in a SQLite database file, ensuring data is not lost when the application shuts down.
- Asynchronous: All database operations are non-blocking, making it suitable for high-concurrency applications.
- Automatic Migrations: The necessary database schema is automatically created and maintained.
§Usage
To use SqliteSession, you need to create an instance by providing a session
ID and a path to the database file. If the database file does not exist, it
will be created.
§Example: Creating and Using a Persistent Session
use tower_llm::sqlite_session::SqliteSession;
use tower_llm::memory::Session;
use tower_llm::items::{RunItem, MessageItem, Role};
use chrono::Utc;
// Create a new session that will be stored in "test_chat.db".
let session = SqliteSession::new("user_123", "test_chat.db").await?;
// Clear any previous history for this session.
session.clear_session().await?;
// Add a new message to the session.
let message = MessageItem {
id: "msg_1".to_string(),
role: Role::User,
content: "Hello, persistent world!".to_string(),
created_at: Utc::now(),
};
session.add_items(vec![RunItem::Message(message)]).await?;
// Retrieve the messages.
let messages = session.get_messages(None).await?;
assert_eq!(messages.len(), 1);
assert_eq!(messages[0].content, "Hello, persistent world!");
// The conversation is saved in "test_chat.db" and will be available
// in subsequent runs.For testing purposes, you can also create an in-memory session using
SqliteSession::new_in_memory, which does not write to the filesystem.
Structs§
- Sqlite
Session - A
Sessionimplementation that uses SQLite for persistent storage. - Sqlite
Session Store - Tower-native SQLite session store that implements Load/Save services used by MemoryLayer.