From b17ddd7556e4822fe7278375f9be8d1b431cb4bd Mon Sep 17 00:00:00 2001 From: ooodc <549496103@qq.com> Date: Wed, 13 May 2026 23:11:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=20InChatCommandHandl?= =?UTF-8?q?er=20=E6=8E=A5=E5=8F=A3=EF=BC=8C=E6=B7=BB=E5=8A=A0=E4=BC=9A?= =?UTF-8?q?=E8=AF=9D=E7=AE=A1=E7=90=86=E5=99=A8=E5=8F=82=E6=95=B0=EF=BC=9B?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=91=BD=E4=BB=A4=E8=B7=AF=E7=94=B1=E5=92=8C?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E4=BC=9A=E8=AF=9D=E9=80=BB=E8=BE=91=E4=BB=A5?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BC=9A=E8=AF=9D=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/command/handler.rs | 9 +++++++-- src/command/handlers/save_session.rs | 17 ++++++++++++++--- src/gateway/processor.rs | 2 +- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/command/handler.rs b/src/command/handler.rs index 75c48d2..d6b3572 100644 --- a/src/command/handler.rs +++ b/src/command/handler.rs @@ -3,6 +3,7 @@ use crate::command::context::CommandContext; use crate::command::response::{CommandError, CommandResponse}; use crate::command::Command; use crate::agent::AgentError; +use crate::gateway::session::SessionManager; use async_trait::async_trait; /// 命令处理器 trait @@ -33,7 +34,7 @@ pub trait CommandHandler: Send + Sync { /// InChat 命令处理器 trait /// /// 用于处理在聊天中直接输入的命令(如 Feishu/WeChat 等通道) -/// 接收 InboundMessage 而不是 CommandContext +/// 接收 InboundMessage 和 SessionManager #[async_trait] pub trait InChatCommandHandler: Send + Sync { /// 是否可以处理此命令 @@ -44,6 +45,7 @@ pub trait InChatCommandHandler: Send + Sync { /// # Arguments /// * `cmd` - 要执行的命令 /// * `inbound` - 入站消息(包含通道信息) + /// * `session_manager` - 会话管理器(用于获取 session) /// /// # Returns /// * `Ok(())` - 命令执行成功 @@ -52,6 +54,7 @@ pub trait InChatCommandHandler: Send + Sync { &self, cmd: Command, inbound: &InboundMessage, + session_manager: &SessionManager, ) -> Result<(), AgentError>; } @@ -157,6 +160,7 @@ impl InChatCommandRouter { /// # Arguments /// * `cmd` - 要执行的命令 /// * `inbound` - 入站消息 + /// * `session_manager` - 会话管理器 /// /// # Returns /// * `Ok(true)` - 命令被处理 @@ -166,11 +170,12 @@ impl InChatCommandRouter { &self, cmd: Command, inbound: &InboundMessage, + session_manager: &SessionManager, ) -> Result { // 查找能处理此命令的处理器 for handler in &self.handlers { if handler.can_handle(&cmd) { - handler.handle(cmd, inbound).await?; + handler.handle(cmd, inbound, session_manager).await?; return Ok(true); } } diff --git a/src/command/handlers/save_session.rs b/src/command/handlers/save_session.rs index f56f1f9..72cd7e1 100644 --- a/src/command/handlers/save_session.rs +++ b/src/command/handlers/save_session.rs @@ -6,6 +6,7 @@ use crate::command::response::{CommandError, CommandResponse, MessageKind}; use crate::command::Command; use crate::config::LLMProviderConfig; use crate::gateway::agent_prompt_provider::SimpleAgentPromptProvider; +use crate::gateway::session::SessionManager; use crate::storage::{SessionRecord, SessionStore}; use crate::agent::AgentError; use crate::bus::OutboundMessage; @@ -375,17 +376,27 @@ impl InChatCommandHandler for SaveSessionInChatHandler { &self, cmd: Command, inbound: &InboundMessage, + session_manager: &crate::gateway::session::SessionManager, ) -> Result<(), AgentError> { let Command::SaveSession { filepath } = cmd else { return Ok(()); }; - // 使用聊天ID作为会话ID - let session_id = &inbound.chat_id; + // 通过 session_manager 获取 session + let session = match session_manager.get(&inbound.channel).await { + Some(s) => s, + None => { + tracing::error!("Session not found for channel: {}", inbound.channel); + return Ok(()); + } + }; + + let session_guard = session.lock().await; + let session_id = session_guard.persistent_session_id(&inbound.chat_id); // 调用公共函数 let result = save_session_to_file( - session_id, + &session_id, filepath, &*self.store, &self.provider_config, diff --git a/src/gateway/processor.rs b/src/gateway/processor.rs index c2df86d..4f362ed 100644 --- a/src/gateway/processor.rs +++ b/src/gateway/processor.rs @@ -94,7 +94,7 @@ impl InboundProcessor { // 尝试解析为命令 if let Some(cmd) = parse_in_chat_command(&inbound.content) { // 使用命令路由器处理 - let handled = self.command_router.dispatch(cmd, &inbound).await?; + let handled = self.command_router.dispatch(cmd, &inbound, &self.session_manager).await?; if handled { return Ok(()); }