agnt_core/store_trait.rs
1//! The [`MessageStore`] trait — abstract session persistence.
2//!
3//! Implementors live in `agnt-store` (SQLite) or can be user-provided
4//! (in-memory for tests, Postgres, Redis, key-value store, etc.).
5
6use crate::message::Message;
7
8/// Error returned by [`MessageStore`] operations.
9#[derive(Debug, Clone)]
10pub enum StoreError {
11 /// Failed to open or connect to the store.
12 Open(String),
13 /// IO or query failure at runtime.
14 Io(String),
15 /// Serialization failure encoding/decoding a message.
16 Serde(String),
17}
18
19impl std::fmt::Display for StoreError {
20 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21 match self {
22 Self::Open(e) => write!(f, "store open: {}", e),
23 Self::Io(e) => write!(f, "store io: {}", e),
24 Self::Serde(e) => write!(f, "store serde: {}", e),
25 }
26 }
27}
28
29impl std::error::Error for StoreError {}
30
31/// A tool execution record to persist.
32#[derive(Debug, Clone)]
33pub struct ToolLog<'a> {
34 pub name: &'a str,
35 pub args: &'a str,
36 pub result: &'a str,
37 pub duration_us: u64,
38}
39
40/// Abstract session persistence.
41///
42/// Implementors provide durable storage for conversation history and tool
43/// execution logs, keyed by a session identifier.
44pub trait MessageStore: Send + Sync {
45 /// Load all messages for a session in insertion order.
46 fn load(&self, session: &str) -> Result<Vec<Message>, StoreError>;
47
48 /// Append one message to the session log.
49 fn append(&self, session: &str, message: &Message) -> Result<(), StoreError>;
50
51 /// Record a tool execution with its args, result, and duration.
52 fn log_tool(&self, session: &str, log: &ToolLog<'_>) -> Result<(), StoreError>;
53
54 /// Wipe all messages and tool logs for a session.
55 fn clear(&self, session: &str) -> Result<(), StoreError>;
56}