diff --git a/src/gateway/session.rs b/src/gateway/session.rs index c7cf4c9..e454608 100644 --- a/src/gateway/session.rs +++ b/src/gateway/session.rs @@ -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, diff --git a/src/gateway/ws.rs b/src/gateway/ws.rs index 81c4a8c..3cb70be 100644 --- a/src/gateway/ws.rs +++ b/src/gateway/ws.rs @@ -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;