feat(command): 优化 StopExecution 命令按话题取消逻辑

- StopExecutionCommandHandler 新增 SessionManager 依赖
- 优先使用 ctx.topic_id,若无则从 SessionManager 获取当前话题
- 增加获取话题过程中错误处理和日志打印
- 更新命令处理器构造参数,传入 SessionManager
- 在命令路由和 WebSocket 注册时同步修改 StopExecutionCommandHandler 创建方式
This commit is contained in:
oudecheng 2026-06-18 17:17:26 +08:00
parent 9ea5849f22
commit edc975e6d0
3 changed files with 40 additions and 6 deletions

View File

@ -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<CommandResponse, CommandError> {
// 优先使用 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)

View File

@ -113,6 +113,7 @@ impl InboundProcessor {
// 注册 stop_execution 处理器
command_router.register(Box::new(StopExecutionCommandHandler::new(
cancel_manager.clone(),
session_manager.clone(),
)));
Self {

View File

@ -432,6 +432,7 @@ async fn handle_inbound(
// 注册 stop_execution 处理器
router.register(Box::new(StopExecutionCommandHandler::new(
state.cancel_manager.clone(),
state.session_manager.clone(),
)));
// 构建命令上下文