use std::sync::Arc; use tokio::sync::{Mutex, mpsc}; use uuid::Uuid; use crate::config::LLMProviderConfig; use crate::agent::AgentLoop; use crate::protocol::WsOutbound; pub struct Session { pub id: Uuid, pub agent_loop: Arc>, pub user_tx: mpsc::Sender, } impl Session { pub async fn new( provider_config: LLMProviderConfig, user_tx: mpsc::Sender, ) -> Result { let agent_loop = AgentLoop::new(provider_config)?; Ok(Self { id: Uuid::new_v4(), agent_loop: Arc::new(Mutex::new(agent_loop)), user_tx, }) } pub async fn send(&self, msg: WsOutbound) { let _ = self.user_tx.send(msg).await; } } use std::collections::HashMap; use std::sync::RwLock; pub struct SessionManager { sessions: RwLock>>, } impl SessionManager { pub fn new() -> Self { Self { sessions: RwLock::new(HashMap::new()), } } pub fn add(&self, session: Arc) { self.sessions.write().unwrap().insert(session.id, session); } pub fn remove(&self, id: &Uuid) { self.sessions.write().unwrap().remove(id); } pub fn get(&self, id: &Uuid) -> Option> { self.sessions.read().unwrap().get(id).cloned() } pub fn len(&self) -> usize { self.sessions.read().unwrap().len() } } impl Default for SessionManager { fn default() -> Self { Self::new() } }