From c817b1dde130e0f028d51d31db387c8cca19f7e4 Mon Sep 17 00:00:00 2001 From: oudecheng <13802883547@139.com> Date: Thu, 21 May 2026 18:04:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BC=9A=E8=AF=9D?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=9C=A8=E8=AF=9D=E9=A2=98=E5=88=87=E6=8D=A2?= =?UTF-8?q?=E6=97=B6=E6=AD=A3=E7=A1=AE=E4=BF=9D=E5=AD=98=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E5=B9=B6=E6=9B=B4=E6=96=B0=E5=86=85=E5=AD=98=E5=8E=86=E5=8F=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gateway/execution.rs | 37 ++++++++++++++++++++++++++++++++----- src/providers/openai.rs | 3 ++- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/gateway/execution.rs b/src/gateway/execution.rs index b58792f..24e8bcc 100644 --- a/src/gateway/execution.rs +++ b/src/gateway/execution.rs @@ -110,15 +110,42 @@ impl AgentExecutionService { // 将结果消息保存到确定的话题 if let Some(topic_id) = target_topic_id { - if let Err(err) = session.append_messages_to_topic( + 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, + &request.result.emitted_messages, + ) { + tracing::error!( + error = %err, + topic_id = %topic_id, + "Failed to append messages to topic" + ); + } + } + } else if is_current_turn { + // 如果没有话题,直接更新内存历史(append_persisted_messages 会处理持久化) + if let Err(err) = session.append_persisted_messages( request.chat_id, - topic_id, - &request.result.emitted_messages, + request.result.emitted_messages.clone(), ) { tracing::error!( error = %err, - topic_id = %topic_id, - "Failed to append messages to topic" + chat_id = %request.chat_id, + "Failed to append messages to session history" ); } } diff --git a/src/providers/openai.rs b/src/providers/openai.rs index 1766a82..35e93a7 100644 --- a/src/providers/openai.rs +++ b/src/providers/openai.rs @@ -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