assistant2: Skip tool uses without a matching tool result (#27082)

Agus Zubiaga created

Anthropic API doesn't allow `tool_use` messages without a corresponding
`tool_result`, so we'll skip those when building a request. I'll
separately investigate why we are sending request before the tool result
as that might lead to separate issues, but that might take a while and
this is currently very frustrating.

Release Notes:

- N/A

Change summary

crates/assistant2/src/tool_use.rs | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)

Detailed changes

crates/assistant2/src/tool_use.rs 🔗

@@ -286,9 +286,17 @@ impl ToolUseState {
     ) {
         if let Some(tool_uses) = self.tool_uses_by_assistant_message.get(&message_id) {
             for tool_use in tool_uses {
-                request_message
-                    .content
-                    .push(MessageContent::ToolUse(tool_use.clone()));
+                if self.tool_results.contains_key(&tool_use.id) {
+                    // Do not send tool uses until they are completed
+                    request_message
+                        .content
+                        .push(MessageContent::ToolUse(tool_use.clone()));
+                } else {
+                    log::debug!(
+                        "skipped tool use {:?} because it is still pending",
+                        tool_use
+                    );
+                }
             }
         }
     }