From a2d4ed91935e14e0fded6db590502e4ea90a2f04 Mon Sep 17 00:00:00 2001 From: oudecheng <13802883547@139.com> Date: Fri, 15 May 2026 17:43:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E8=AF=9D=E9=A2=98=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=8E=B7=E5=8F=96=E5=92=8C=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E6=AF=8F=E4=B8=AA=20chat=20=E7=9A=84=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E8=AF=9D=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gateway/session.rs | 28 ++++++++++++++++++++++++++++ src/gateway/session_history.rs | 12 ++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/gateway/session.rs b/src/gateway/session.rs index de471ce..c7cf4c9 100644 --- a/src/gateway/session.rs +++ b/src/gateway/session.rs @@ -174,6 +174,11 @@ impl Session { self.history.chat_topic(chat_id) } + /// 获取历史所对应的话题 ID(指定 chat) + pub fn history_topic(&self, chat_id: &str) -> Option<&str> { + self.history.history_topic(chat_id) + } + /// 切换话题 - 清除当前历史并加载新话题的历史 pub fn switch_topic(&mut self, chat_id: &str, topic_id: &str) -> Result<(), AgentError> { // 清除当前历史 @@ -201,9 +206,32 @@ impl Session { } pub fn ensure_chat_loaded(&mut self, chat_id: &str) -> Result<(), AgentError> { + // 检查历史是否存在且对应正确的话题 + let current_topic = self.history.chat_topic(chat_id); + let stored_topic = self.history.history_topic(chat_id); + + if self.chat_history_exists(chat_id) { + // 如果历史已存在,但话题不匹配,需要重新加载 + if current_topic != stored_topic { + tracing::info!( + chat_id = %chat_id, + current_topic = ?current_topic, + stored_topic = ?stored_topic, + "Topic changed, reloading history" + ); + self.reload_chat_history(chat_id)?; + } + return Ok(()); + } + + // 历史不存在,正常加载 self.history.ensure_chat_loaded(chat_id) } + fn chat_history_exists(&self, chat_id: &str) -> bool { + self.history.get_history(chat_id).is_some() + } + pub fn ensure_agent_prompt_before_user_message( &mut self, chat_id: &str, diff --git a/src/gateway/session_history.rs b/src/gateway/session_history.rs index 10f7ea1..f235383 100644 --- a/src/gateway/session_history.rs +++ b/src/gateway/session_history.rs @@ -26,6 +26,7 @@ pub(crate) struct SessionHistory { channel_name: String, chat_histories: HashMap>, chat_topic_ids: HashMap, // 每个 chat 的当前 topic + history_topic_ids: HashMap, // 每个 chat 的历史所对应的话题 compression_in_flight: HashSet, conversations: Arc, skill_events: Arc, @@ -43,6 +44,7 @@ impl SessionHistory { channel_name: channel_name.into(), chat_histories: HashMap::new(), chat_topic_ids: HashMap::new(), + history_topic_ids: HashMap::new(), compression_in_flight: HashSet::new(), conversations, skill_events, @@ -117,6 +119,15 @@ impl SessionHistory { pub(crate) fn set_history(&mut self, chat_id: &str, history: Vec) { self.chat_histories.insert(chat_id.to_string(), history); + // 记录历史对应的话题(当前设置的话题) + if let Some(topic_id) = self.chat_topic_ids.get(chat_id) { + self.history_topic_ids.insert(chat_id.to_string(), topic_id.clone()); + } + } + + /// 获取指定 chat 的历史所对应的话题 + pub(crate) fn history_topic(&self, chat_id: &str) -> Option<&str> { + self.history_topic_ids.get(chat_id).map(|s| s.as_str()) } /// 设置指定 chat 的当前 topic @@ -141,6 +152,7 @@ impl SessionHistory { pub(crate) fn remove_history(&mut self, chat_id: &str) { self.chat_histories.remove(chat_id); self.compression_in_flight.remove(chat_id); + self.history_topic_ids.remove(chat_id); } pub(crate) fn clear_chat_history(&mut self, chat_id: &str) -> Result<(), AgentError> {