feat: 优化会话消息处理逻辑,支持在话题切换时正确保存消息并更新内存历史

This commit is contained in:
oudecheng 2026-05-21 18:04:11 +08:00
parent 159c1bbb7a
commit c817b1dde1
2 changed files with 34 additions and 6 deletions

View File

@ -110,6 +110,20 @@ impl AgentExecutionService {
// 将结果消息保存到确定的话题
if let Some(topic_id) = target_topic_id {
if is_current_turn {
// 如果是最新回合,使用 append_persisted_messages 保存到数据库并更新内存历史
if let Err(err) = session.append_persisted_messages(
request.chat_id,
request.result.emitted_messages.clone(),
) {
tracing::error!(
error = %err,
chat_id = %request.chat_id,
"Failed to append messages to session history"
);
}
} else {
// 如果用户已切换话题,只保存到原始话题(不更新内存历史)
if let Err(err) = session.append_messages_to_topic(
request.chat_id,
topic_id,
@ -122,6 +136,19 @@ impl AgentExecutionService {
);
}
}
} else if is_current_turn {
// 如果没有话题直接更新内存历史append_persisted_messages 会处理持久化)
if let Err(err) = session.append_persisted_messages(
request.chat_id,
request.result.emitted_messages.clone(),
) {
tracing::error!(
error = %err,
chat_id = %request.chat_id,
"Failed to append messages to session history"
);
}
}
// 只有当是最新回合时才发送 outbound 消息给用户
// 如果用户已经切换到其他话题,只保存结果,不发送消息(避免打扰)

View File

@ -235,12 +235,13 @@ impl OpenAIProvider {
let normalized = self.normalize_tool_arguments(arguments);
if self.uses_json_tool_arguments() {
// Model expects JSON object format
// Model expects JSON object format (e.g., some code models)
normalized
} else {
// Standard OpenAI format: arguments as JSON string
// But ensure we serialize valid JSON, not null
match normalized {
Value::Null => Value::String("{}".to_string()),
Value::String(raw) => {
// If the string is already valid JSON, keep it as-is
// Otherwise, ensure it's a proper JSON string