use async_trait::async_trait;
use serde_json::Value;
use crate::types::message::Message;
use crate::Result;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct MemoryEntry {
pub id: String,
pub content: String,
pub metadata: Option<Value>,
pub created_at: u64,
}
impl MemoryEntry {
#[must_use]
pub fn now(id: impl Into<String>, content: impl Into<String>) -> Self {
let created_at = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
Self {
id: id.into(),
content: content.into(),
metadata: None,
created_at,
}
}
}
#[async_trait]
pub trait Memory: Send + Sync + 'static {
async fn messages(&self, session_id: &str) -> Result<Vec<Message>>;
async fn append(&self, session_id: &str, message: Message) -> Result<()>;
async fn get_context(&self, session_id: &str, key: &str) -> Result<Option<Value>>;
async fn set_context(&self, session_id: &str, key: &str, value: Value) -> Result<()>;
async fn recall(&self, query: &str, limit: usize) -> Result<Vec<MemoryEntry>>;
async fn store(&self, entry: MemoryEntry) -> Result<()>;
async fn create_session(&self) -> Result<String> {
Ok(uuid::Uuid::new_v4().to_string())
}
async fn list_sessions(&self) -> Result<Vec<String>> {
Ok(vec![])
}
async fn delete_session(&self, _session_id: &str) -> Result<()> {
Ok(())
}
}