fix: prevent rare duplicate tool call IDs in long running claude prompts

This commit is contained in:
2026-08-05 16:01:40 -06:00
parent 9540345ec7
commit 1f7b8417fa
2 changed files with 17 additions and 7 deletions
+10 -3
View File
@@ -1,3 +1,5 @@
use std::mem;
use super::*; use super::*;
use crate::utils::{base64_decode, encode_uri, hex_encode, hmac_sha256, sha256, strip_think_tag}; use crate::utils::{base64_decode, encode_uri, hex_encode, hmac_sha256, sha256, strip_think_tag};
@@ -275,10 +277,11 @@ async fn chat_completions_streaming(
format!("Tool call '{function_name}' has non-JSON arguments '{function_arguments}'") format!("Tool call '{function_name}' has non-JSON arguments '{function_arguments}'")
})?; })?;
handler.tool_call(ToolCall::new( handler.tool_call(ToolCall::new(
function_name.clone(), mem::take(&mut function_name),
arguments, arguments,
Some(function_id.clone()), Some(mem::take(&mut function_id)),
))?; ))?;
function_arguments.clear();
} }
} }
_ => {} _ => {}
@@ -529,7 +532,11 @@ fn extract_chat_completions(data: &Value) -> Result<ChatCompletionsOutput> {
bail!("Invalid response data: {data}"); bail!("Invalid response data: {data}");
} }
let output = ChatCompletionsOutput { text, tool_calls, ..Default::default() }; let output = ChatCompletionsOutput {
text,
tool_calls,
..Default::default()
};
Ok(output) Ok(output)
} }
+7 -4
View File
@@ -1,3 +1,5 @@
use std::mem;
use super::access_token::get_access_token; use super::access_token::get_access_token;
use super::claude_oauth::ClaudeOAuthProvider; use super::claude_oauth::ClaudeOAuthProvider;
use super::oauth::{self, OAuthProvider}; use super::oauth::{self, OAuthProvider};
@@ -232,8 +234,8 @@ pub async fn claude_chat_completions_streaming(
handler.text("\n</think>\n\n")?; handler.text("\n</think>\n\n")?;
reasoning_state = 0; reasoning_state = 0;
handler.thinking_block(ThinkingBlock::Thinking { handler.thinking_block(ThinkingBlock::Thinking {
thinking: std::mem::take(&mut thinking_text), thinking: mem::take(&mut thinking_text),
signature: std::mem::take(&mut thinking_signature), signature: mem::take(&mut thinking_signature),
}); });
} }
if !function_name.is_empty() { if !function_name.is_empty() {
@@ -245,10 +247,11 @@ pub async fn claude_chat_completions_streaming(
})? })?
}; };
handler.tool_call(ToolCall::new( handler.tool_call(ToolCall::new(
function_name.clone(), mem::take(&mut function_name),
arguments, arguments,
Some(function_id.clone()), Some(mem::take(&mut function_id)),
))?; ))?;
function_arguments.clear();
} }
} }
_ => {} _ => {}