Exit agent loop when tool call JSON fails to parse (#7840)
Co-authored-by: Douwe Osinga <douwe@squareup.com>
This commit is contained in:
@@ -1181,9 +1181,10 @@ impl Agent {
|
|||||||
let mut messages_to_add = Conversation::default();
|
let mut messages_to_add = Conversation::default();
|
||||||
let mut tools_updated = false;
|
let mut tools_updated = false;
|
||||||
let mut did_recovery_compact_this_iteration = false;
|
let mut did_recovery_compact_this_iteration = false;
|
||||||
|
let mut exit_chat = false;
|
||||||
|
|
||||||
while let Some(next) = stream.next().await {
|
while let Some(next) = stream.next().await {
|
||||||
if is_token_cancelled(&cancel_token) {
|
if is_token_cancelled(&cancel_token) || exit_chat {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1462,15 +1463,17 @@ impl Agent {
|
|||||||
yield AgentEvent::Message(final_response.clone());
|
yield AgentEvent::Message(final_response.clone());
|
||||||
messages_to_add.push(final_response);
|
messages_to_add.push(final_response);
|
||||||
} else {
|
} else {
|
||||||
let error_msg = format!(
|
error!(
|
||||||
"[system: Tool call could not be parsed: {}. The response may have been truncated. Try breaking the task into smaller steps.]",
|
"Tool call could not be parsed: {}",
|
||||||
request.tool_call.as_ref().unwrap_err(),
|
request.tool_call.as_ref().unwrap_err(),
|
||||||
);
|
);
|
||||||
let error_response = Message::user()
|
yield AgentEvent::Message(
|
||||||
.with_generated_id()
|
Message::assistant().with_text(
|
||||||
.with_text(&error_msg);
|
"A tool call could not be parsed — the response may have been truncated. Try breaking the task into smaller steps or resending your message."
|
||||||
yield AgentEvent::Message(error_response.clone());
|
)
|
||||||
messages_to_add.push(error_response);
|
);
|
||||||
|
exit_chat = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1590,7 +1593,6 @@ impl Agent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut exit_chat = false;
|
|
||||||
if no_tools_called {
|
if no_tools_called {
|
||||||
if let Some(final_output_tool) = self.final_output_tool.lock().await.as_ref() {
|
if let Some(final_output_tool) = self.final_output_tool.lock().await.as_ref() {
|
||||||
if final_output_tool.final_output.is_none() {
|
if final_output_tool.final_output.is_none() {
|
||||||
|
|||||||
Reference in New Issue
Block a user