fix: 用户隔离 — 切换用户时清空上下文 + 加载历史
listen_loop 新增用户切换逻辑: - 跟踪 last_user_id - 用户变化时清空 ChatSession (messages/checkpoint/summaries) - 加载新用户最近 20 条聊天记录 - 隔离对话上下文、摘要、工作记忆 现在每个用户拥有独立的会话视图
This commit is contained in:
+19
-5
@@ -468,6 +468,7 @@ async fn listen_loop(
|
||||
approval_manager: &ApprovalManager,
|
||||
current_user_id: Arc<tokio::sync::Mutex<String>>,
|
||||
) {
|
||||
let last_user_id = Arc::new(tokio::sync::Mutex::new(String::new()));
|
||||
loop {
|
||||
match client.receive_messages().await {
|
||||
Ok(resp) => {
|
||||
@@ -535,11 +536,24 @@ async fn listen_loop(
|
||||
continue;
|
||||
}
|
||||
|
||||
// 更新当前用户(审批流程需要)
|
||||
*current_user_id.lock().await = from.to_string();
|
||||
// 同步到 session(executor 从 session 读取 user_id)
|
||||
if let Some(conv) = &conversation {
|
||||
conv.session().lock().await.current_user_id = from.to_string();
|
||||
// 用户切换:清空上下文,加载新用户历史
|
||||
{
|
||||
let mut last_user = last_user_id.lock().await;
|
||||
if *last_user != from.to_string() && !last_user.is_empty() {
|
||||
info!("用户切换: {} → {}", *last_user, from);
|
||||
if let Some(conv) = &conversation {
|
||||
let session = conv.session();
|
||||
let mut s = session.lock().await;
|
||||
s.messages.clear();
|
||||
s.checkpoint = 0;
|
||||
s.summaries.clear();
|
||||
s.last_user_at = None;
|
||||
s.current_user_id = from.to_string();
|
||||
// 加载该用户最近 20 条历史
|
||||
s.load_recent_messages(20).await;
|
||||
}
|
||||
}
|
||||
*last_user = from.to_string();
|
||||
}
|
||||
|
||||
// LLM 回复(异步执行,避免审批阻塞主循环)
|
||||
|
||||
Reference in New Issue
Block a user