feat(command): 优化 StopExecution 命令按话题取消逻辑
- StopExecutionCommandHandler 新增 SessionManager 依赖 - 优先使用 ctx.topic_id,若无则从 SessionManager 获取当前话题 - 增加获取话题过程中错误处理和日志打印 - 更新命令处理器构造参数,传入 SessionManager - 在命令路由和 WebSocket 注册时同步修改 StopExecutionCommandHandler 创建方式
This commit is contained in:
parent
9ea5849f22
commit
edc975e6d0
@ -5,15 +5,17 @@ use crate::command::handler::{CommandHandler, CommandMetadata};
|
|||||||
use crate::command::response::{CommandError, CommandResponse, MessageKind};
|
use crate::command::response::{CommandError, CommandResponse, MessageKind};
|
||||||
use crate::command::Command;
|
use crate::command::Command;
|
||||||
use crate::gateway::cancel_manager::CancelManager;
|
use crate::gateway::cancel_manager::CancelManager;
|
||||||
|
use crate::gateway::session::SessionManager;
|
||||||
|
|
||||||
/// 处理 StopExecution 命令:按话题取消当前正在执行的 Agent。
|
/// 处理 StopExecution 命令:按话题取消当前正在执行的 Agent。
|
||||||
pub struct StopExecutionCommandHandler {
|
pub struct StopExecutionCommandHandler {
|
||||||
cancel_manager: CancelManager,
|
cancel_manager: CancelManager,
|
||||||
|
session_manager: SessionManager,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StopExecutionCommandHandler {
|
impl StopExecutionCommandHandler {
|
||||||
pub fn new(cancel_manager: CancelManager) -> Self {
|
pub fn new(cancel_manager: CancelManager, session_manager: SessionManager) -> Self {
|
||||||
Self { cancel_manager }
|
Self { cancel_manager, session_manager }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,15 +38,45 @@ impl CommandHandler for StopExecutionCommandHandler {
|
|||||||
_cmd: Command,
|
_cmd: Command,
|
||||||
ctx: CommandContext,
|
ctx: CommandContext,
|
||||||
) -> Result<CommandResponse, CommandError> {
|
) -> Result<CommandResponse, CommandError> {
|
||||||
|
// 优先使用 ctx.topic_id,如果没有则从 session_manager 获取真实的 topic_id
|
||||||
let topic_id = match ctx.topic_id.as_deref() {
|
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 => {
|
None => {
|
||||||
|
// 从 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)
|
return Ok(CommandResponse::success(ctx.request_id)
|
||||||
.with_message(MessageKind::Notification, "当前没有活跃的话题,无法停止"));
|
.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 {
|
if cancelled {
|
||||||
Ok(CommandResponse::success(ctx.request_id)
|
Ok(CommandResponse::success(ctx.request_id)
|
||||||
|
|||||||
@ -113,6 +113,7 @@ impl InboundProcessor {
|
|||||||
// 注册 stop_execution 处理器
|
// 注册 stop_execution 处理器
|
||||||
command_router.register(Box::new(StopExecutionCommandHandler::new(
|
command_router.register(Box::new(StopExecutionCommandHandler::new(
|
||||||
cancel_manager.clone(),
|
cancel_manager.clone(),
|
||||||
|
session_manager.clone(),
|
||||||
)));
|
)));
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
|
|||||||
@ -432,6 +432,7 @@ async fn handle_inbound(
|
|||||||
// 注册 stop_execution 处理器
|
// 注册 stop_execution 处理器
|
||||||
router.register(Box::new(StopExecutionCommandHandler::new(
|
router.register(Box::new(StopExecutionCommandHandler::new(
|
||||||
state.cancel_manager.clone(),
|
state.cancel_manager.clone(),
|
||||||
|
state.session_manager.clone(),
|
||||||
)));
|
)));
|
||||||
|
|
||||||
// 构建命令上下文
|
// 构建命令上下文
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user