mod context;
mod lock;
mod manager;
mod session;
pub use context::ExecutionContext;
pub use lock::ReadWriteLock;
pub use manager::ConcurrentSessionManager;
pub use session::{Session, SessionConfig};
use async_trait::async_trait;
use crate::types::{AgentState, Layer2Result, Message, SessionId, SessionMeta};
#[async_trait]
pub trait SessionManagerTrait: Send + Sync {
async fn create(&self, config: SessionConfig) -> Layer2Result<SessionId>;
async fn get(&self, id: &SessionId) -> Layer2Result<Option<Session>>;
async fn get_or_create(
&self,
id: Option<&SessionId>,
config: SessionConfig,
) -> Layer2Result<SessionId>;
async fn save(&self, session: &Session) -> Layer2Result<()>;
async fn delete(&self, id: &SessionId) -> Layer2Result<bool>;
async fn list(&self) -> Layer2Result<Vec<SessionMeta>>;
async fn update<F>(&self, id: &SessionId, update_fn: F) -> Layer2Result<bool>
where
F: FnOnce(&mut Session) + Send;
async fn read<F, T>(&self, id: &SessionId, read_fn: F) -> Layer2Result<Option<T>>
where
F: FnOnce(&Session) -> T + Send,
T: Send;
async fn get_state(&self, id: &SessionId) -> Layer2Result<Option<AgentState>>;
async fn set_state(&self, id: &SessionId, state: AgentState) -> Layer2Result<bool>;
async fn add_message(&self, id: &SessionId, message: Message) -> Layer2Result<bool>;
async fn get_messages(&self, id: &SessionId) -> Layer2Result<Option<Vec<Message>>>;
fn stats(&self) -> SessionStats;
}
#[derive(Debug, Clone, Default)]
pub struct SessionStats {
pub total_sessions: usize,
pub max_sessions: usize,
pub active_sessions: usize,
}
pub trait StateLockTrait: Send + Sync {
fn read_lock<F, T>(&self, f: F) -> T
where
F: FnOnce() -> T;
fn write_lock<F, T>(&self, f: F) -> T
where
F: FnOnce() -> T;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_session_stats_default() {
let stats = SessionStats::default();
assert_eq!(stats.total_sessions, 0);
assert_eq!(stats.active_sessions, 0);
}
}