From 9bfb95d795d6c7cc10cfcb907642bba7b8bb7f19 Mon Sep 17 00:00:00 2001 From: asepharyana Date: Mon, 20 Jul 2026 14:58:39 +0700 Subject: [PATCH] feat(llm): improve tool call handling by dynamically resizing tool_calls and updating arguments --- apps/infrastructure/src/llm/provider.rs | 43 ++++++++++++------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/apps/infrastructure/src/llm/provider.rs b/apps/infrastructure/src/llm/provider.rs index 8430f8a..2b6bbf5 100644 --- a/apps/infrastructure/src/llm/provider.rs +++ b/apps/infrastructure/src/llm/provider.rs @@ -346,33 +346,32 @@ impl LlmClient { StreamEvent::Token(t) => self.content.push_str(t), StreamEvent::Reasoning(_) => {} StreamEvent::ToolCallDelta { - index: _, + index, id, name, arguments_delta, } => { - let existing = self.tool_calls.iter_mut().find(|tc| { - if let Some(ref id_val) = id { - tc.id == *id_val - } else { - false - } - }); - if let Some(tc) = existing { - if let Some(ref n) = name { - tc.function.name = n.clone(); - } - } else { - self.tool_calls.push( - zesdex_domain::core::ToolCall { - id: id.clone().unwrap_or_default(), - type_: "function".to_string(), - function: zesdex_domain::core::ToolFunction { - name: name.clone().unwrap_or_default(), - arguments: serde_json::Value::String(arguments_delta.clone()), - }, + while self.tool_calls.len() <= *index { + self.tool_calls.push(zesdex_domain::core::ToolCall { + id: String::new(), + type_: "function".to_string(), + function: zesdex_domain::core::ToolFunction { + name: String::new(), + arguments: serde_json::Value::String(String::new()), }, - ); + }); + } + + let tc = &mut self.tool_calls[*index]; + + if let Some(ref id_val) = id { + tc.id = id_val.clone(); + } + if let Some(ref name_val) = name { + tc.function.name = name_val.clone(); + } + if let serde_json::Value::String(ref mut args) = tc.function.arguments { + args.push_str(arguments_delta); } } _ => {