From 428df8da591af69e9a511e3fea167fbd16329d6c Mon Sep 17 00:00:00 2001 From: ooodc <549496103@qq.com> Date: Sat, 16 May 2026 20:45:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=87=AA=E5=8A=A8=E4=BB=8E=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E6=81=A2=E5=A4=8D=E5=BD=93=E5=89=8D=E8=AF=9D?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E5=A2=9E=E5=BC=BA=E4=BC=9A=E8=AF=9D=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gateway/session.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/gateway/session.rs b/src/gateway/session.rs index 63c8248..c7784d4 100644 --- a/src/gateway/session.rs +++ b/src/gateway/session.rs @@ -547,11 +547,30 @@ impl SessionManager { self.lifecycle.get(channel_name).await } - /// 获取指定 chat 的当前话题(确保 session 存在) + /// 获取指定 chat 的当前话题(确保 session 存在,自动从数据库恢复) pub async fn get_current_topic(&self, channel_name: &str, chat_id: &str) -> Result, AgentError> { self.ensure_session(channel_name).await?; if let Some(session) = self.get(channel_name).await { - let guard = session.lock().await; + let mut guard = session.lock().await; + + // 如果内存中没有当前话题,从数据库恢复最近活跃的话题 + if guard.current_topic(chat_id).is_none() { + let session_id = guard.persistent_session_id(chat_id); + let topics = self.store.list_topics(&session_id) + .map_err(|e| AgentError::Other(format!("Failed to list topics: {}", e)))?; + + if let Some(latest_topic) = topics.first() { + // 设置最近活跃的话题为当前话题 + guard.set_current_topic(chat_id, Some(latest_topic.id.clone())); + tracing::info!( + chat_id = %chat_id, + topic_id = %latest_topic.id, + topic_title = %latest_topic.title, + "Restored current topic from database" + ); + } + } + Ok(guard.current_topic(chat_id).map(|s| s.to_string())) } else { Ok(None)