Compare commits

..

2 Commits

3 changed files with 22 additions and 9 deletions

View File

@ -10,7 +10,6 @@ use crate::command::handler::CommandRouter;
use crate::command::handlers::save_session::SaveSessionCommandHandler;
use crate::command::handlers::session::SessionCommandHandler;
use crate::command::handlers::session_query::SessionQueryCommandHandler;
use crate::command::Command;
use crate::config::LLMProviderConfig;
use crate::gateway::agent_prompt_provider::AgentPromptProvider;
use crate::skills::SkillPromptProvider;
@ -123,6 +122,14 @@ impl InboundProcessor {
// 计算正确的 session_id根据 channel_name 和 chat_id
let session_id = persistent_session_id(&inbound.channel, &inbound.chat_id);
// 获取当前话题(如果有 Session
let current_topic = if let Some(session) = self.session_manager.get(&inbound.channel).await {
let guard = session.lock().await;
guard.current_topic(&inbound.chat_id).map(|s| s.to_string())
} else {
None
};
// 使用 ChannelInputAdapter 尝试解析命令
let adapter = ChannelInputAdapter::new();
let ctx = crate::command::context::AdapterContext::new(&inbound.channel)
@ -130,12 +137,13 @@ impl InboundProcessor {
if let Ok(Some(cmd)) = adapter.try_parse(&inbound.content, ctx) {
// 使用命令路由器处理
let cmd_ctx = crate::command::context::CommandContext::new(&inbound.channel, &inbound.channel)
let mut cmd_ctx = crate::command::context::CommandContext::new(&inbound.channel, &inbound.channel)
.with_session_id(&session_id)
.with_chat_id(&inbound.chat_id);
// 记录是否是创建会话命令(用于后续处理)
let _is_create_session = matches!(cmd, Command::CreateSession { .. });
// 只在有话题时才设置 topic_id
if let Some(ref topic_id) = current_topic {
cmd_ctx = cmd_ctx.with_topic_id(topic_id.as_str());
}
let response = self.command_router.dispatch_with_response(cmd, cmd_ctx).await;

View File

@ -184,6 +184,9 @@ impl Session {
// 清除当前历史
self.history.remove_history(chat_id);
// 先设置当前话题set_history 需要这个)
self.history.set_chat_topic(chat_id, topic_id.to_string());
// 加载新话题的历史
let messages = self
.store
@ -191,7 +194,6 @@ impl Session {
.map_err(|e| AgentError::Other(format!("load topic messages error: {}", e)))?;
self.history.set_history(chat_id, messages);
self.history.set_chat_topic(chat_id, topic_id.to_string());
tracing::info!(
topic_id = %topic_id,

View File

@ -243,10 +243,13 @@ async fn handle_inbound(
current_topic_id = ?current_topic_id,
"Building CommandContext for WebSocket command"
);
let cmd_ctx = CommandContext::new("websocket", "cli")
let mut cmd_ctx = CommandContext::new("websocket", "cli")
.with_session_id(current_session_id.as_str())
.with_chat_id(current_session_id.as_str())
.with_topic_id(current_topic_id.as_deref().unwrap_or(""));
.with_chat_id(current_session_id.as_str());
// 只在有 topic_id 时才设置
if let Some(ref topic_id) = *current_topic_id {
cmd_ctx = cmd_ctx.with_topic_id(topic_id.as_str());
}
// 执行命令
let response = router.dispatch_with_response(cmd, cmd_ctx).await;