fix: 修复 persistent_session_id 前缀重复累积问题
This commit is contained in:
parent
a78f5c5512
commit
7e24e57af4
@ -6,7 +6,7 @@ use crate::agent::{AgentError, AgentProcessResult, EmittedMessageHandler, Persis
|
||||
use crate::bus::message::ToolMessageState;
|
||||
use crate::bus::{ChatMessage, MediaItem, OutboundMessage, SYSTEM_CONTEXT_SCHEDULED_PROMPT};
|
||||
use crate::config::LLMProviderConfig;
|
||||
use crate::storage::ConversationRepository;
|
||||
use crate::storage::{persistent_session_id, ConversationRepository};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use super::compaction::schedule_background_history_compaction;
|
||||
@ -252,7 +252,7 @@ impl AgentExecutionService {
|
||||
|
||||
// 构建系统提示词上下文
|
||||
let system_prompt_context = SystemPromptContext {
|
||||
session_id: Some(format!("{}:{}", request.channel_name, request.chat_id)),
|
||||
session_id: Some(persistent_session_id(request.channel_name, request.chat_id)),
|
||||
chat_id: request.chat_id.to_string(),
|
||||
user_message_count,
|
||||
};
|
||||
@ -356,7 +356,7 @@ impl AgentExecutionService {
|
||||
|
||||
// 构建系统提示词上下文
|
||||
let system_prompt_context = SystemPromptContext {
|
||||
session_id: Some(format!("{}:{}", request.channel_name, request.chat_id)),
|
||||
session_id: Some(persistent_session_id(request.channel_name, request.chat_id)),
|
||||
chat_id: request.chat_id.to_string(),
|
||||
user_message_count,
|
||||
};
|
||||
|
||||
@ -1601,6 +1601,13 @@ impl SessionStore {
|
||||
}
|
||||
|
||||
pub fn persistent_session_id(channel_name: &str, chat_id: &str) -> String {
|
||||
// 幂等:循环去除已存在的 "{channel_name}:" 前缀,防止前缀累积
|
||||
let prefix = format!("{}:", channel_name);
|
||||
let mut chat_id = chat_id;
|
||||
while chat_id.starts_with(&prefix) {
|
||||
chat_id = &chat_id[prefix.len()..];
|
||||
}
|
||||
|
||||
if channel_name == "cli" || channel_name == "websocket" {
|
||||
chat_id.to_string()
|
||||
} else {
|
||||
@ -2288,8 +2295,12 @@ mod tests {
|
||||
#[test]
|
||||
fn test_persistent_session_id_for_cli_and_channel() {
|
||||
assert_eq!(persistent_session_id("cli", "abc"), "abc");
|
||||
assert_eq!(persistent_session_id("websocket", "websocket:abc"), "websocket:abc");
|
||||
// 幂等:已带前缀的 chat_id 会被清理,不会累积前缀
|
||||
assert_eq!(persistent_session_id("websocket", "websocket:abc"), "abc");
|
||||
assert_eq!(persistent_session_id("websocket", "websocket:websocket:abc"), "abc");
|
||||
assert_eq!(persistent_session_id(TEST_CHANNEL, "abc"), "test-channel:abc");
|
||||
// 其他通道也幂等
|
||||
assert_eq!(persistent_session_id(TEST_CHANNEL, "test-channel:abc"), "test-channel:abc");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user