agent_core_runtime/controller/session/
manager.rs1use std::collections::HashMap;
4use std::sync::Arc;
5
6use tokio::sync::{mpsc, RwLock};
7use tokio_util::sync::CancellationToken;
8
9use super::config::LLMSessionConfig;
10use super::LLMSession;
11use crate::client::error::LlmError;
12use crate::controller::types::FromLLMPayload;
13
14pub struct LLMSessionManager {
16 sessions: RwLock<HashMap<i64, Arc<LLMSession>>>,
18}
19
20impl LLMSessionManager {
21 pub fn new() -> Self {
23 Self {
24 sessions: RwLock::new(HashMap::new()),
25 }
26 }
27
28 pub async fn create_session(
41 &self,
42 config: LLMSessionConfig,
43 from_llm: mpsc::Sender<FromLLMPayload>,
44 channel_size: usize,
45 ) -> Result<i64, LlmError> {
46 let cancel_token = CancellationToken::new();
47 let session = Arc::new(LLMSession::new(config, from_llm, cancel_token, channel_size)?);
48 let session_id = session.id();
49
50 {
52 let mut sessions = self.sessions.write().await;
53 sessions.insert(session_id, Arc::clone(&session));
54 }
55
56 let session_clone = Arc::clone(&session);
58 tokio::spawn(async move {
59 session_clone.start().await;
60 });
61
62 tracing::info!(session_id, "Session created and started");
63 Ok(session_id)
64 }
65
66 pub async fn get_session_by_id(&self, session_id: i64) -> Option<Arc<LLMSession>> {
71 let sessions = self.sessions.read().await;
72 sessions.get(&session_id).cloned()
73 }
74
75 pub async fn remove_session(&self, session_id: i64) -> bool {
83 let session = {
84 let mut sessions = self.sessions.write().await;
85 sessions.remove(&session_id)
86 };
87
88 if let Some(session) = session {
89 session.shutdown();
90 tracing::info!(session_id, "Session removed");
91 true
92 } else {
93 false
94 }
95 }
96
97 pub async fn shutdown(&self) {
100 let sessions: Vec<Arc<LLMSession>> = {
101 let mut sessions = self.sessions.write().await;
102 sessions.drain().map(|(_, s)| s).collect()
103 };
104
105 for session in sessions {
106 session.shutdown();
107 }
108
109 tracing::info!("Session manager shutdown complete");
110 }
111
112 pub async fn session_count(&self) -> usize {
114 let sessions = self.sessions.read().await;
115 sessions.len()
116 }
117}
118
119impl Default for LLMSessionManager {
120 fn default() -> Self {
121 Self::new()
122 }
123}