From 315596e316902a3b9afc3df3f6c1e48af8365939 Mon Sep 17 00:00:00 2001 From: Dhanji Prasanna Date: Mon, 29 Sep 2025 08:22:26 +1000 Subject: [PATCH] only emit final response once --- crates/g3-core/src/lib.rs | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/crates/g3-core/src/lib.rs b/crates/g3-core/src/lib.rs index beabac2..c577844 100644 --- a/crates/g3-core/src/lib.rs +++ b/crates/g3-core/src/lib.rs @@ -1027,14 +1027,27 @@ The tool will execute immediately and you'll receive the result (success or erro io::stdout().flush()?; // Add the tool call and result to the context window - let tool_message = Message { - role: MessageRole::Assistant, - content: format!( - "{}\n\n{{\"tool\": \"{}\", \"args\": {}}}", - final_display_content.trim(), - tool_call.tool, - tool_call.args - ), + // Only include the text content if it's not already in full_response + let tool_message = if !full_response.contains(final_display_content) { + Message { + role: MessageRole::Assistant, + content: format!( + "{}\n\n{{\"tool\": \"{}\", \"args\": {}}}", + final_display_content.trim(), + tool_call.tool, + tool_call.args + ), + } + } else { + // If we've already added the text, just include the tool call + Message { + role: MessageRole::Assistant, + content: format!( + "{{\"tool\": \"{}\", \"args\": {}}}", + tool_call.tool, + tool_call.args + ), + } }; let result_message = Message { role: MessageRole::User, @@ -1052,7 +1065,10 @@ The tool will execute immediately and you'll receive the result (success or erro request.tools = Some(Self::create_tool_definitions()); } - full_response.push_str(final_display_content); + // Only add to full_response if we haven't already added it + if !full_response.contains(final_display_content) { + full_response.push_str(final_display_content); + } tool_executed = true; // Reset parser for next iteration