diff --git a/src/command/handlers/stop_execution.rs b/src/command/handlers/stop_execution.rs index e754db8..fb0d06b 100644 --- a/src/command/handlers/stop_execution.rs +++ b/src/command/handlers/stop_execution.rs @@ -5,15 +5,17 @@ use crate::command::handler::{CommandHandler, CommandMetadata}; use crate::command::response::{CommandError, CommandResponse, MessageKind}; use crate::command::Command; use crate::gateway::cancel_manager::CancelManager; +use crate::gateway::session::SessionManager; /// 处理 StopExecution 命令:按话题取消当前正在执行的 Agent。 pub struct StopExecutionCommandHandler { cancel_manager: CancelManager, + session_manager: SessionManager, } impl StopExecutionCommandHandler { - pub fn new(cancel_manager: CancelManager) -> Self { - Self { cancel_manager } + pub fn new(cancel_manager: CancelManager, session_manager: SessionManager) -> Self { + Self { cancel_manager, session_manager } } } @@ -36,15 +38,45 @@ impl CommandHandler for StopExecutionCommandHandler { _cmd: Command, ctx: CommandContext, ) -> Result { + // 优先使用 ctx.topic_id,如果没有则从 session_manager 获取真实的 topic_id let topic_id = match ctx.topic_id.as_deref() { - Some(id) => id, + Some(id) => { + tracing::info!( + channel = %ctx.channel_name, + chat_id = ?ctx.chat_id, + topic_id = %id, + source = "ctx", + "Stop execution command received" + ); + id.to_string() + } None => { - return Ok(CommandResponse::success(ctx.request_id) - .with_message(MessageKind::Notification, "当前没有活跃的话题,无法停止")); + // 从 SessionManager 获取真实的 current topic + let chat_id = ctx.chat_id.as_deref().unwrap_or(""); + match self.session_manager.get_current_topic(&ctx.channel_name, chat_id).await { + Ok(Some(id)) => { + tracing::info!( + channel = %ctx.channel_name, + chat_id = %chat_id, + topic_id = %id, + source = "session_manager", + "Stop execution command received (resolved from session)" + ); + id + } + Ok(None) => { + return Ok(CommandResponse::success(ctx.request_id) + .with_message(MessageKind::Notification, "当前没有活跃的话题,无法停止")); + } + Err(e) => { + return Ok(CommandResponse::error(ctx.request_id, + CommandError::new("QUERY_TOPIC_ERROR", e.to_string()))); + } + } } }; - let cancelled = self.cancel_manager.cancel_by_topic(topic_id).await; + let cancelled = self.cancel_manager.cancel_by_topic(&topic_id).await; if cancelled { Ok(CommandResponse::success(ctx.request_id) diff --git a/src/gateway/processor.rs b/src/gateway/processor.rs index f8158a5..87d0284 100644 --- a/src/gateway/processor.rs +++ b/src/gateway/processor.rs @@ -113,6 +113,7 @@ impl InboundProcessor { // 注册 stop_execution 处理器 command_router.register(Box::new(StopExecutionCommandHandler::new( cancel_manager.clone(), + session_manager.clone(), ))); Self { diff --git a/src/gateway/ws.rs b/src/gateway/ws.rs index bc7f898..f421fe8 100644 --- a/src/gateway/ws.rs +++ b/src/gateway/ws.rs @@ -432,6 +432,7 @@ async fn handle_inbound( // 注册 stop_execution 处理器 router.register(Box::new(StopExecutionCommandHandler::new( state.cancel_manager.clone(), + state.session_manager.clone(), ))); // 构建命令上下文