only emit final response once

This commit is contained in:
Dhanji Prasanna
2025-09-29 08:22:26 +10:00
parent 39ef13e317
commit 315596e316

View File

@@ -1027,14 +1027,27 @@ The tool will execute immediately and you'll receive the result (success or erro
io::stdout().flush()?; io::stdout().flush()?;
// Add the tool call and result to the context window // Add the tool call and result to the context window
let tool_message = Message { // Only include the text content if it's not already in full_response
role: MessageRole::Assistant, let tool_message = if !full_response.contains(final_display_content) {
content: format!( Message {
"{}\n\n{{\"tool\": \"{}\", \"args\": {}}}", role: MessageRole::Assistant,
final_display_content.trim(), content: format!(
tool_call.tool, "{}\n\n{{\"tool\": \"{}\", \"args\": {}}}",
tool_call.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 { let result_message = Message {
role: MessageRole::User, 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()); 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; tool_executed = true;
// Reset parser for next iteration // Reset parser for next iteration