feat(stream): add method to detect incomplete tool calls and handle parsing errors
This commit is contained in:
@@ -318,6 +318,19 @@ impl LlmClient {
|
||||
}
|
||||
}
|
||||
|
||||
// The connection closed without an explicit `[DONE]` event. Some
|
||||
// providers legitimately omit it, so EOF alone isn't an error —
|
||||
// but if it leaves a tool call's arguments as unparsable JSON, the
|
||||
// response was truncated mid-generation, not finished. Report that
|
||||
// honestly instead of silently double-stringifying the fragment
|
||||
// into a tool call that will misbehave (e.g. a `write` call with a
|
||||
// half-written file body).
|
||||
if let Some((name, err)) = turn.incomplete_tool_call() {
|
||||
anyhow::bail!(
|
||||
"stream ended before tool call '{name}' arguments were complete: {err}"
|
||||
);
|
||||
}
|
||||
|
||||
turn.is_complete = true;
|
||||
Ok((turn.build_assistant_message(), usage))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user