feat(llm): improve tool call handling by dynamically resizing tool_calls and updating arguments

This commit is contained in:
asepharyana
2026-07-20 14:58:39 +07:00
parent 285dbb14cc
commit 9bfb95d795
+21 -22
View File
@@ -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);
}
}
_ => {