#![deny(missing_docs)]
pub mod broker;
mod error;
mod in_memory;
#[cfg(feature = "sqlite")]
mod sqlite;
mod types;
use std::sync::Arc;
use anyhow::Result;
use async_trait::async_trait;
pub use brainwires_core::Message;
pub use broker::{SessionBroker, SessionMessage, SessionSummary, SpawnRequest, SpawnedSession};
pub use error::SessionError;
pub use in_memory::InMemorySessionStore;
#[cfg(feature = "sqlite")]
pub use sqlite::SqliteSessionStore;
pub use types::{SessionId, SessionRecord};
#[derive(Debug, Clone, Copy, Default)]
pub struct ListOptions {
pub offset: usize,
pub limit: Option<usize>,
}
impl ListOptions {
pub fn new(offset: usize, limit: Option<usize>) -> Self {
Self { offset, limit }
}
}
#[async_trait]
pub trait SessionStore: Send + Sync {
async fn load(&self, id: &SessionId) -> Result<Option<Vec<Message>>>;
async fn save(&self, id: &SessionId, messages: &[Message]) -> Result<()>;
async fn list(&self) -> Result<Vec<SessionRecord>>;
async fn list_paginated(&self, opts: ListOptions) -> Result<Vec<SessionRecord>> {
let all = self.list().await?;
let start = opts.offset.min(all.len());
let end = match opts.limit {
Some(limit) => start.saturating_add(limit).min(all.len()),
None => all.len(),
};
Ok(all[start..end].to_vec())
}
async fn delete(&self, id: &SessionId) -> Result<()>;
}
pub type ArcSessionStore = Arc<dyn SessionStore>;