diff --git a/src/command/handlers/session.rs b/src/command/handlers/session.rs index 9c58d34..0bce329 100644 --- a/src/command/handlers/session.rs +++ b/src/command/handlers/session.rs @@ -2,6 +2,7 @@ use crate::command::context::CommandContext; use crate::command::handler::CommandHandler; use crate::command::response::{CommandError, CommandResponse, MessageKind}; use crate::command::Command; +use crate::gateway::session::SessionManager; use crate::storage::SessionStore; use async_trait::async_trait; use std::sync::Arc; @@ -11,6 +12,7 @@ use std::sync::Arc; /// 处理与会话管理相关的命令 pub struct SessionCommandHandler { store: Arc, + session_manager: Option, } impl SessionCommandHandler { @@ -19,7 +21,16 @@ impl SessionCommandHandler { /// # Arguments /// * `store` - Session 存储 pub(crate) fn new(store: Arc) -> Self { - Self { store } + Self { + store, + session_manager: None, + } + } + + /// 设置 SessionManager(用于 CreateSession 命令自动切换话题) + pub fn with_session_manager(mut self, session_manager: SessionManager) -> Self { + self.session_manager = Some(session_manager); + self } } @@ -62,6 +73,19 @@ async fn handle_create_session( .create_topic(session_id, &topic_title, None) .map_err(|e| CommandError::new("CREATE_TOPIC_ERROR", e.to_string()))?; + // 获取 chat_id + let chat_id = ctx.chat_id.as_deref() + .ok_or_else(|| CommandError::new("NO_CHAT_ID", "No chat_id in context"))?; + + // 如果有 SessionManager,自动切换到新话题 + if let Some(ref session_manager) = handler.session_manager { + if let Some(session) = session_manager.get(&ctx.channel_name).await { + let mut session_guard = session.lock().await; + session_guard.switch_topic(chat_id, &topic.id) + .map_err(|e| CommandError::new("SWITCH_TOPIC_ERROR", e.to_string()))?; + } + } + Ok(CommandResponse::success(ctx.request_id) .with_message(MessageKind::Notification, &topic.title) .with_metadata("topic_id", &topic.id) @@ -88,7 +112,8 @@ mod tests { let session = store.create_session("cli", Some("test session")).unwrap(); let ctx = CommandContext::new("test", "cli") - .with_session_id(&session.id); + .with_session_id(&session.id) + .with_chat_id(&session.id); let cmd = Command::CreateSession { title: Some("my topic".to_string()), }; @@ -108,7 +133,8 @@ mod tests { let session = store.create_session("cli", Some("test session")).unwrap(); let ctx = CommandContext::new("test", "cli") - .with_session_id(&session.id); + .with_session_id(&session.id) + .with_chat_id(&session.id); let cmd = Command::CreateSession { title: None }; let result = handler.handle(cmd, ctx).await; diff --git a/src/gateway/processor.rs b/src/gateway/processor.rs index 5a86971..e9f8b49 100644 --- a/src/gateway/processor.rs +++ b/src/gateway/processor.rs @@ -39,7 +39,9 @@ impl InboundProcessor { let store = session_manager.store(); // 注册 Session 处理器 - command_router.register(Box::new(SessionCommandHandler::new(store.clone()))); + let session_handler = SessionCommandHandler::new(store.clone()) + .with_session_manager(session_manager.clone()); + command_router.register(Box::new(session_handler)); // 注册 session_query 处理器 let session_query_handler = SessionQueryCommandHandler::new(store) diff --git a/src/gateway/ws.rs b/src/gateway/ws.rs index ec4cbb4..81c4a8c 100644 --- a/src/gateway/ws.rs +++ b/src/gateway/ws.rs @@ -224,8 +224,11 @@ async fn handle_inbound( ])); let mut router = CommandRouter::new(); - router.register(Box::new(SessionCommandHandler::new(store.clone()))); - // 修复:添加 SessionManager 到 SessionQueryCommandHandler + // 注册 Session 处理器,添加 SessionManager + let session_handler = SessionCommandHandler::new(store.clone()) + .with_session_manager(state.session_manager.clone()); + router.register(Box::new(session_handler)); + // 注册 SessionQuery 处理器 let session_query_handler = SessionQueryCommandHandler::new(store.clone()) .with_session_manager(state.session_manager.clone()); router.register(Box::new(session_query_handler));