sh_layer2/session_manager/
mod.rs1mod context;
12mod lock;
13mod manager;
14mod session;
15
16pub use context::ExecutionContext;
17pub use lock::ReadWriteLock;
18pub use manager::ConcurrentSessionManager;
19pub use session::{Session, SessionConfig};
20
21use async_trait::async_trait;
22
23use crate::types::{AgentState, Layer2Result, Message, SessionId, SessionMeta};
24
25#[async_trait]
29pub trait SessionManagerTrait: Send + Sync {
30 async fn create(&self, config: SessionConfig) -> Layer2Result<SessionId>;
38
39 async fn get(&self, id: &SessionId) -> Layer2Result<Option<Session>>;
47
48 async fn get_or_create(
56 &self,
57 id: Option<&SessionId>,
58 config: SessionConfig,
59 ) -> Layer2Result<SessionId>;
60
61 async fn save(&self, session: &Session) -> Layer2Result<()>;
66
67 async fn delete(&self, id: &SessionId) -> Layer2Result<bool>;
72
73 async fn list(&self) -> Layer2Result<Vec<SessionMeta>>;
78
79 async fn update<F>(&self, id: &SessionId, update_fn: F) -> Layer2Result<bool>
85 where
86 F: FnOnce(&mut Session) + Send;
87
88 async fn read<F, T>(&self, id: &SessionId, read_fn: F) -> Layer2Result<Option<T>>
94 where
95 F: FnOnce(&Session) -> T + Send,
96 T: Send;
97
98 async fn get_state(&self, id: &SessionId) -> Layer2Result<Option<AgentState>>;
100
101 async fn set_state(&self, id: &SessionId, state: AgentState) -> Layer2Result<bool>;
103
104 async fn add_message(&self, id: &SessionId, message: Message) -> Layer2Result<bool>;
106
107 async fn get_messages(&self, id: &SessionId) -> Layer2Result<Option<Vec<Message>>>;
109
110 fn stats(&self) -> SessionStats;
112}
113
114#[derive(Debug, Clone, Default)]
116pub struct SessionStats {
117 pub total_sessions: usize,
118 pub max_sessions: usize,
119 pub active_sessions: usize,
120}
121
122pub trait StateLockTrait: Send + Sync {
124 fn read_lock<F, T>(&self, f: F) -> T
126 where
127 F: FnOnce() -> T;
128
129 fn write_lock<F, T>(&self, f: F) -> T
131 where
132 F: FnOnce() -> T;
133}
134
135#[cfg(test)]
136mod tests {
137 use super::*;
138
139 #[test]
140 fn test_session_stats_default() {
141 let stats = SessionStats::default();
142 assert_eq!(stats.total_sessions, 0);
143 assert_eq!(stats.active_sessions, 0);
144 }
145}