From 70b35d2cc14f96eb87bfeb978f1487735fa36e78 Mon Sep 17 00:00:00 2001 From: oudecheng <13802883547@139.com> Date: Fri, 22 May 2026 10:47:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E8=81=8A=E5=A4=A9?= =?UTF-8?q?=E5=8E=86=E5=8F=B2=E5=8A=A0=E8=BD=BD=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=8C=89=E8=AF=9D=E9=A2=98=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E5=8E=86=E5=8F=B2=E8=AE=B0=E5=BD=95=E5=B9=B6=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E5=80=9F=E7=94=A8=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gateway/session.rs | 9 +++++---- src/gateway/session_history.rs | 20 +++++++++++++++----- src/storage/ports.rs | 13 +++++++------ vendor/wechatbot/src/bot.rs | 12 +++++++----- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/gateway/session.rs b/src/gateway/session.rs index 2c9a71e..e13e51a 100644 --- a/src/gateway/session.rs +++ b/src/gateway/session.rs @@ -210,12 +210,13 @@ impl Session { pub fn ensure_chat_loaded(&mut self, chat_id: &str) -> Result<(), AgentError> { // 检查历史是否存在且对应正确的话题 - let current_topic = self.history.chat_topic(chat_id); + // 先获取 topic 信息并转换为 owned String,避免借用冲突 + let current_topic: Option = self.history.chat_topic(chat_id).map(|s| s.to_string()); let stored_topic = self.history.history_topic(chat_id); if self.chat_history_exists(chat_id) { // 如果历史已存在,但话题不匹配,需要重新加载 - if current_topic != stored_topic { + if current_topic.as_deref() != stored_topic { tracing::info!( chat_id = %chat_id, current_topic = ?current_topic, @@ -227,8 +228,8 @@ impl Session { return Ok(()); } - // 历史不存在,正常加载 - self.history.ensure_chat_loaded(chat_id) + // 历史不存在,按 topic 加载(如果设置了 topic) + self.history.ensure_chat_loaded(chat_id, current_topic.as_deref()) } fn chat_history_exists(&self, chat_id: &str) -> bool { diff --git a/src/gateway/session_history.rs b/src/gateway/session_history.rs index dccc53e..2a8b61e 100644 --- a/src/gateway/session_history.rs +++ b/src/gateway/session_history.rs @@ -65,7 +65,11 @@ impl SessionHistory { .map_err(|err| AgentError::Other(format!("session persistence error: {}", err))) } - pub(crate) fn ensure_chat_loaded(&mut self, chat_id: &str) -> Result<(), AgentError> { + pub(crate) fn ensure_chat_loaded( + &mut self, + chat_id: &str, + topic_id: Option<&str>, + ) -> Result<(), AgentError> { // 获取 session 记录(用于检查最后活跃时间) let session_record = self.ensure_persistent_session(chat_id)?; @@ -93,10 +97,16 @@ impl SessionHistory { return Ok(()); } - let history = self - .conversations - .load_messages(&self.persistent_session_id(chat_id)) - .map_err(|err| AgentError::Other(format!("session history load error: {}", err)))?; + // 如果提供了 topic_id,按 topic 加载;否则按 session 加载 + let history = if let Some(tid) = topic_id { + self.conversations + .load_messages_for_topic(tid) + .map_err(|err| AgentError::Other(format!("session history load error: {}", err)))? + } else { + self.conversations + .load_messages(&self.persistent_session_id(chat_id)) + .map_err(|err| AgentError::Other(format!("session history load error: {}", err)))? + }; self.chat_histories.insert(chat_id.to_string(), history); Ok(()) } diff --git a/src/storage/ports.rs b/src/storage/ports.rs index 76a2c68..f236554 100644 --- a/src/storage/ports.rs +++ b/src/storage/ports.rs @@ -22,6 +22,8 @@ pub trait ConversationRepository: Send + Sync + 'static { fn load_messages(&self, session_id: &str) -> Result, StorageError>; + fn load_messages_for_topic(&self, topic_id: &str) -> Result, StorageError>; + fn append_message(&self, session_id: &str, message: &ChatMessage) -> Result<(), StorageError>; fn append_message_with_topic( @@ -162,6 +164,10 @@ impl ConversationRepository for super::SessionStore { super::SessionStore::load_messages(self, session_id) } + fn load_messages_for_topic(&self, topic_id: &str) -> Result, StorageError> { + super::SessionStore::load_messages_for_topic(self, topic_id) + } + fn append_message(&self, session_id: &str, message: &ChatMessage) -> Result<(), StorageError> { super::SessionStore::append_message(self, session_id, message) } @@ -337,9 +343,4 @@ impl SkillEventRepository for super::SessionStore { } fn list_skill_events( - &self, - session_id: Option<&str>, - ) -> Result, StorageError> { - super::SessionStore::list_skill_events(self, session_id) - } -} + \ No newline at end of file diff --git a/vendor/wechatbot/src/bot.rs b/vendor/wechatbot/src/bot.rs index 28d949e..a4a4027 100644 --- a/vendor/wechatbot/src/bot.rs +++ b/vendor/wechatbot/src/bot.rs @@ -602,24 +602,26 @@ fn chunk_text(text: &str, limit: usize) -> Vec { chunks.push(remaining.to_string()); break; } - let window = &remaining[..limit]; + // Find the nearest valid character boundary + let end = remaining.floor_char_boundary(limit); + let window = &remaining[..end]; let cut = window .rfind("\n\n") - .filter(|&i| i > limit * 3 / 10) + .filter(|&i| i > end * 3 / 10) .map(|i| i + 2) .or_else(|| { window .rfind('\n') - .filter(|&i| i > limit * 3 / 10) + .filter(|&i| i > end * 3 / 10) .map(|i| i + 1) }) .or_else(|| { window .rfind(' ') - .filter(|&i| i > limit * 3 / 10) + .filter(|&i| i > end * 3 / 10) .map(|i| i + 1) }) - .unwrap_or(limit); + .unwrap_or(end); chunks.push(remaining[..cut].to_string()); remaining = &remaining[cut..]; }