feat: 更新 InChatCommandHandler 接口,添加会话管理器参数;修改命令路由和保存会话逻辑以支持会话管理
This commit is contained in:
parent
08172dcf9c
commit
b17ddd7556
@ -3,6 +3,7 @@ use crate::command::context::CommandContext;
|
|||||||
use crate::command::response::{CommandError, CommandResponse};
|
use crate::command::response::{CommandError, CommandResponse};
|
||||||
use crate::command::Command;
|
use crate::command::Command;
|
||||||
use crate::agent::AgentError;
|
use crate::agent::AgentError;
|
||||||
|
use crate::gateway::session::SessionManager;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
|
||||||
/// 命令处理器 trait
|
/// 命令处理器 trait
|
||||||
@ -33,7 +34,7 @@ pub trait CommandHandler: Send + Sync {
|
|||||||
/// InChat 命令处理器 trait
|
/// InChat 命令处理器 trait
|
||||||
///
|
///
|
||||||
/// 用于处理在聊天中直接输入的命令(如 Feishu/WeChat 等通道)
|
/// 用于处理在聊天中直接输入的命令(如 Feishu/WeChat 等通道)
|
||||||
/// 接收 InboundMessage 而不是 CommandContext
|
/// 接收 InboundMessage 和 SessionManager
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait InChatCommandHandler: Send + Sync {
|
pub trait InChatCommandHandler: Send + Sync {
|
||||||
/// 是否可以处理此命令
|
/// 是否可以处理此命令
|
||||||
@ -44,6 +45,7 @@ pub trait InChatCommandHandler: Send + Sync {
|
|||||||
/// # Arguments
|
/// # Arguments
|
||||||
/// * `cmd` - 要执行的命令
|
/// * `cmd` - 要执行的命令
|
||||||
/// * `inbound` - 入站消息(包含通道信息)
|
/// * `inbound` - 入站消息(包含通道信息)
|
||||||
|
/// * `session_manager` - 会话管理器(用于获取 session)
|
||||||
///
|
///
|
||||||
/// # Returns
|
/// # Returns
|
||||||
/// * `Ok(())` - 命令执行成功
|
/// * `Ok(())` - 命令执行成功
|
||||||
@ -52,6 +54,7 @@ pub trait InChatCommandHandler: Send + Sync {
|
|||||||
&self,
|
&self,
|
||||||
cmd: Command,
|
cmd: Command,
|
||||||
inbound: &InboundMessage,
|
inbound: &InboundMessage,
|
||||||
|
session_manager: &SessionManager,
|
||||||
) -> Result<(), AgentError>;
|
) -> Result<(), AgentError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,6 +160,7 @@ impl InChatCommandRouter {
|
|||||||
/// # Arguments
|
/// # Arguments
|
||||||
/// * `cmd` - 要执行的命令
|
/// * `cmd` - 要执行的命令
|
||||||
/// * `inbound` - 入站消息
|
/// * `inbound` - 入站消息
|
||||||
|
/// * `session_manager` - 会话管理器
|
||||||
///
|
///
|
||||||
/// # Returns
|
/// # Returns
|
||||||
/// * `Ok(true)` - 命令被处理
|
/// * `Ok(true)` - 命令被处理
|
||||||
@ -166,11 +170,12 @@ impl InChatCommandRouter {
|
|||||||
&self,
|
&self,
|
||||||
cmd: Command,
|
cmd: Command,
|
||||||
inbound: &InboundMessage,
|
inbound: &InboundMessage,
|
||||||
|
session_manager: &SessionManager,
|
||||||
) -> Result<bool, AgentError> {
|
) -> Result<bool, AgentError> {
|
||||||
// 查找能处理此命令的处理器
|
// 查找能处理此命令的处理器
|
||||||
for handler in &self.handlers {
|
for handler in &self.handlers {
|
||||||
if handler.can_handle(&cmd) {
|
if handler.can_handle(&cmd) {
|
||||||
handler.handle(cmd, inbound).await?;
|
handler.handle(cmd, inbound, session_manager).await?;
|
||||||
return Ok(true);
|
return Ok(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,6 +6,7 @@ use crate::command::response::{CommandError, CommandResponse, MessageKind};
|
|||||||
use crate::command::Command;
|
use crate::command::Command;
|
||||||
use crate::config::LLMProviderConfig;
|
use crate::config::LLMProviderConfig;
|
||||||
use crate::gateway::agent_prompt_provider::SimpleAgentPromptProvider;
|
use crate::gateway::agent_prompt_provider::SimpleAgentPromptProvider;
|
||||||
|
use crate::gateway::session::SessionManager;
|
||||||
use crate::storage::{SessionRecord, SessionStore};
|
use crate::storage::{SessionRecord, SessionStore};
|
||||||
use crate::agent::AgentError;
|
use crate::agent::AgentError;
|
||||||
use crate::bus::OutboundMessage;
|
use crate::bus::OutboundMessage;
|
||||||
@ -375,17 +376,27 @@ impl InChatCommandHandler for SaveSessionInChatHandler {
|
|||||||
&self,
|
&self,
|
||||||
cmd: Command,
|
cmd: Command,
|
||||||
inbound: &InboundMessage,
|
inbound: &InboundMessage,
|
||||||
|
session_manager: &crate::gateway::session::SessionManager,
|
||||||
) -> Result<(), AgentError> {
|
) -> Result<(), AgentError> {
|
||||||
let Command::SaveSession { filepath } = cmd else {
|
let Command::SaveSession { filepath } = cmd else {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
// 使用聊天ID作为会话ID
|
// 通过 session_manager 获取 session
|
||||||
let session_id = &inbound.chat_id;
|
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(
|
let result = save_session_to_file(
|
||||||
session_id,
|
&session_id,
|
||||||
filepath,
|
filepath,
|
||||||
&*self.store,
|
&*self.store,
|
||||||
&self.provider_config,
|
&self.provider_config,
|
||||||
|
|||||||
@ -94,7 +94,7 @@ impl InboundProcessor {
|
|||||||
// 尝试解析为命令
|
// 尝试解析为命令
|
||||||
if let Some(cmd) = parse_in_chat_command(&inbound.content) {
|
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 {
|
if handled {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user