fix for duplicate messages at end

This commit is contained in:
Dhanji Prasanna
2025-10-31 13:34:36 +11:00
parent 92ae776510
commit bdaacfd051

View File

@@ -2596,7 +2596,8 @@ Template:
String::new() String::new()
}; };
if !new_content.trim().is_empty() { // Don't display text before final_output - it will be in the summary
if !new_content.trim().is_empty() && tool_call.tool != "final_output" {
#[allow(unused_assignments)] #[allow(unused_assignments)]
if !response_started { if !response_started {
self.ui_writer.print_agent_prompt(); self.ui_writer.print_agent_prompt();
@@ -2722,13 +2723,8 @@ Template:
// Check if this was a final_output tool call // Check if this was a final_output tool call
if tool_call.tool == "final_output" { if tool_call.tool == "final_output" {
// Don't add final_display_content here - it was already added before tool execution // Don't return anything - the summary was already displayed during streaming
// Adding it again would duplicate the output // Returning it again would cause duplication
if let Some(summary) = tool_call.args.get("summary") {
if let Some(summary_str) = summary.as_str() {
full_response.push_str(&format!("\n\n{}", summary_str));
}
}
self.ui_writer.println(""); self.ui_writer.println("");
let _ttft = let _ttft =
first_token_time.unwrap_or_else(|| stream_start.elapsed()); first_token_time.unwrap_or_else(|| stream_start.elapsed());
@@ -2760,7 +2756,7 @@ Template:
// Add the tool call and result to the context window using RAW unfiltered content // Add the tool call and result to the context window using RAW unfiltered content
// This ensures the log file contains the true raw content including JSON tool calls // This ensures the log file contains the true raw content including JSON tool calls
let tool_message = if !full_response.contains(final_display_content) && !raw_content_for_log.trim().is_empty() { let tool_message = if !raw_content_for_log.trim().is_empty() {
Message { Message {
role: MessageRole::Assistant, role: MessageRole::Assistant,
content: format!( content: format!(
@@ -2771,7 +2767,7 @@ Template:
), ),
} }
} else { } else {
// If we've already added the text or there's no text, just include the tool call // No text content before tool call, just include the tool call
Message { Message {
role: MessageRole::Assistant, role: MessageRole::Assistant,
content: format!( content: format!(
@@ -2796,18 +2792,22 @@ Template:
request.tools = Some(Self::create_tool_definitions(self.config.webdriver.enabled, self.config.macax.enabled, self.config.computer_control.enabled)); request.tools = Some(Self::create_tool_definitions(self.config.webdriver.enabled, self.config.macax.enabled, self.config.computer_control.enabled));
} }
// Only add to full_response if we haven't already added it // DO NOT add final_display_content to full_response here!
if !full_response.contains(final_display_content) { // The content was already displayed during streaming and added to current_response.
full_response.push_str(final_display_content); // Adding it again would cause duplication when the agent message is printed.
} // The only time we should add to full_response is:
// 1. For final_output tool (handled separately)
// 2. At the end when no tools were executed (handled in the "no tool executed" branch)
tool_executed = true; tool_executed = true;
// Reset the JSON tool call filter state after each tool execution // Reset the JSON tool call filter state after each tool execution
// This ensures the filter doesn't stay in suppression mode for subsequent streaming content // This ensures the filter doesn't stay in suppression mode for subsequent streaming content
fixed_filter_json::reset_fixed_json_tool_state(); fixed_filter_json::reset_fixed_json_tool_state();
// Reset parser for next iteration // Reset parser for next iteration - this clears the text buffer
parser.reset(); parser.reset();
// Clear current_response for next iteration to prevent buffered text // Clear current_response for next iteration to prevent buffered text
// from being incorrectly displayed after tool execution // from being incorrectly displayed after tool execution
current_response.clear(); current_response.clear();
@@ -2985,11 +2985,10 @@ Template:
// Set full_response to current_response (don't append) // Set full_response to current_response (don't append)
// current_response already contains everything that was displayed // current_response already contains everything that was displayed
// Appending would duplicate the output // Don't set full_response here - it would duplicate the output
if !current_response.is_empty() && full_response.is_empty() { // The text was already displayed during streaming
full_response = current_response.clone(); // Return empty string to avoid duplication
debug!("Set full_response from current_response (no tool): {} chars", full_response.len()); full_response = String::new();
}
self.ui_writer.println(""); self.ui_writer.println("");
let _ttft = let _ttft =