use crate::checkpoint::CheckpointEnvelope;
use crate::error::SwarmResult;
use crate::event::AgentEvent;
use crate::types::Message;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SessionRecord {
pub session_id: String,
pub agent_name: String,
pub trace_id: String,
pub started_at: DateTime<Utc>,
pub ended_at: Option<DateTime<Utc>>,
pub outcome: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CheckpointSummary {
pub session_id: String,
pub version: u32,
pub created_at: DateTime<Utc>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MemoryRecord {
pub session_id: String,
pub key: String,
pub value: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
#[async_trait]
pub trait SessionStore: Send + Sync {
async fn create_session(
&self,
session_id: &str,
agent_name: &str,
trace_id: &str,
) -> SwarmResult<()>;
async fn get_session(&self, session_id: &str) -> SwarmResult<Option<SessionRecord>>;
async fn list_sessions(&self, limit: usize, offset: usize) -> SwarmResult<Vec<SessionRecord>>;
async fn list_sessions_by_trace(&self, trace_id: &str) -> SwarmResult<Vec<SessionRecord>>;
async fn complete_session(&self, session_id: &str, outcome: &str) -> SwarmResult<()>;
async fn store_messages(&self, session_id: &str, messages: &[Message]) -> SwarmResult<()>;
async fn load_messages(&self, session_id: &str) -> SwarmResult<Vec<Message>>;
}
#[async_trait]
pub trait EventStore: Send + Sync {
async fn append_event(&self, session_id: &str, event: &AgentEvent) -> SwarmResult<()>;
async fn read_events(&self, session_id: &str) -> SwarmResult<Vec<AgentEvent>>;
async fn read_events_since(
&self,
session_id: &str,
after: DateTime<Utc>,
) -> SwarmResult<Vec<AgentEvent>>;
async fn count_events(&self, session_id: &str) -> SwarmResult<u64>;
}
#[async_trait]
pub trait CheckpointStore: Send + Sync {
async fn save_checkpoint(&self, envelope: &CheckpointEnvelope) -> SwarmResult<()>;
async fn load_checkpoint(&self, session_id: &str) -> SwarmResult<Option<CheckpointEnvelope>>;
async fn load_checkpoint_at_version(
&self,
session_id: &str,
version: u32,
) -> SwarmResult<Option<CheckpointEnvelope>>;
async fn list_checkpoints(&self, session_id: &str) -> SwarmResult<Vec<CheckpointSummary>>;
async fn delete_checkpoints(&self, session_id: &str) -> SwarmResult<()>;
}
#[async_trait]
pub trait MemoryStore: Send + Sync {
async fn persist_memory(&self, session_id: &str, key: &str, value: &str) -> SwarmResult<()>;
async fn restore_memory(&self, session_id: &str) -> SwarmResult<Vec<MemoryRecord>>;
async fn delete_memory(&self, session_id: &str) -> SwarmResult<()>;
}
pub trait PersistenceBackend:
SessionStore + EventStore + CheckpointStore + MemoryStore + Send + Sync
{
}
impl<T> PersistenceBackend for T where
T: SessionStore + EventStore + CheckpointStore + MemoryStore + Send + Sync
{
}
#[cfg(feature = "postgres")]
pub mod postgres;
pub mod sqlite;