fix(streaming): aggregate OpenAI tool-call delta fragments#512
Merged
Conversation
## Priority 2 — Local agent capabilities
OpenAI-compatible SSE streams emit tool calls as *fragments*:
1. {id:"call_1", name:"get_weather", arguments:""}
2. {id:"", name:"", arguments:"{\"lo"}
3. {id:"", name:"", arguments:"cation\":\"Tokyo\"}"}
ChatViewModel.sendMessage and OpenClawProvider.send both appended each
delta to a toolCalls list verbatim, producing three broken tool-call
requests per call — one with the real id but empty arguments, two
with empty ids and partial JSON. The dispatcher then ran the first
with empty args (tool fell back to defaults or errored) and the later
two as "Unknown tool".
StreamingToolCallAggregator glues fragments back together: a delta
with a non-empty id starts a new call (committing the pending one);
an empty-id delta appends its name/arguments fragment to the pending
call. Sequential tool streaming (the OpenAI default) works; true
multi-tool interleaved streaming would need explicit index keys and
is documented as a future extension.
Wired into ChatViewModel streaming and OpenClawProvider.send (the
non-streaming path that still consumes per-event WS messages).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Priority 2 — Local agent capabilities
Summary
OpenAI-compatible SSE streams emit tool calls as fragments:
ChatViewModel.sendMessageandOpenClawProvider.sendboth appended each delta to atoolCallslist verbatim, producing three brokenToolCallRequests per call:The dispatcher then ran the first with empty args (tool fell back to defaults or errored) and the later two as "Unknown tool" — a silent failure where the LLM thought the tool succeeded.
Fix
New
StreamingToolCallAggregatorglues fragments back together. Heuristic:name/argumentsfragment to the pending call.Wired into:
ChatViewModel.sendMessagestreaming consumer.OpenClawProvider.send(the non-streaming path that still consumes per-event WS messages).Scope & limitations
indexkeys. Documented as a future extension.Test plan
StreamingToolCallAggregatorTestcovers: complete single delta, fragment sequence re-assembly, multi-call id boundaries, name-fragment append, orphaned pre-pending fragment, empty chunk no-op, empty aggregator../gradlew :app:testStandardDebugUnitTest— green./gradlew :app:assembleStandardDebug— green