feat: 更新 InChatCommandHandler 接口,添加会话管理器参数;修改命令路由和保存会话逻辑以支持会话管理

This commit is contained in:
ooodc 2026-05-13 23:11:02 +08:00
parent 08172dcf9c
commit b17ddd7556
3 changed files with 22 additions and 6 deletions

View File

@ -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<bool, AgentError> {
// 查找能处理此命令的处理器
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);
}
}

View File

@ -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,

View File

@ -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(());
}