feat(llm): improve tool call handling by dynamically resizing tool_calls and updating arguments
This commit is contained in:
@@ -346,33 +346,32 @@ impl LlmClient {
|
|||||||
StreamEvent::Token(t) => self.content.push_str(t),
|
StreamEvent::Token(t) => self.content.push_str(t),
|
||||||
StreamEvent::Reasoning(_) => {}
|
StreamEvent::Reasoning(_) => {}
|
||||||
StreamEvent::ToolCallDelta {
|
StreamEvent::ToolCallDelta {
|
||||||
index: _,
|
index,
|
||||||
id,
|
id,
|
||||||
name,
|
name,
|
||||||
arguments_delta,
|
arguments_delta,
|
||||||
} => {
|
} => {
|
||||||
let existing = self.tool_calls.iter_mut().find(|tc| {
|
while self.tool_calls.len() <= *index {
|
||||||
if let Some(ref id_val) = id {
|
self.tool_calls.push(zesdex_domain::core::ToolCall {
|
||||||
tc.id == *id_val
|
id: String::new(),
|
||||||
} else {
|
type_: "function".to_string(),
|
||||||
false
|
function: zesdex_domain::core::ToolFunction {
|
||||||
}
|
name: String::new(),
|
||||||
});
|
arguments: serde_json::Value::String(String::new()),
|
||||||
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()),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
);
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
|||||||
Reference in New Issue
Block a user