fix(agents): dedupe ReAct draft text per-round while preserving live streaming (#421)#446
Draft
MarioCadenas wants to merge 1 commit into
Draft
fix(agents): dedupe ReAct draft text per-round while preserving live streaming (#421)#446MarioCadenas wants to merge 1 commit into
MarioCadenas wants to merge 1 commit into
Conversation
…streaming (#421) With OpenAI-compatible Claude on Databricks Model Serving, the model emits a full draft answer ALONGSIDE its tool calls on every ReAct round, so the answer appeared 3-4x in the output. An earlier approach buffered text and flushed it only on the terminal turn, but that made the final answer arrive all-at-once instead of streaming. Root cause: the duplication was never in the adapter or wire protocol. The AgentEventTranslator already closes the current message item and opens a new one when a tool_call/tool_result arrives, so each round's text is a distinct Responses-API output item. Two CONSUMERS flattened those items by concatenating deltas across item boundaries. Three-layer fix that respects the existing item boundaries: - The Databricks adapter streams every round's text live again (this already matched main; no change needed). - consumeAdapterStream keeps only the terminal round: a draft followed by a tool_call is set aside as superseded, and the open message at end-of-stream (or the last draft if maxSteps exhausted mid-tool-calling) is returned. This centrally dedupes thread history, the non-streaming JSON fullContent, runAgent, and sub-agents. - use-agent-chat reduces the wire events into an ordered, per-output-item AgentTurnItem list while streaming live; `content` now tracks only the LAST message item (the terminal answer), so flat-API consumers dedupe too. New public hook API: the AgentTurnItem discriminated union (message / tool_call / tool_result) plus `items` on UseAgentChatResult. The dev-playground agent route uses it to render a collapsible "Steps" disclosure (intermediate drafts + tool-call/result chips) above the prominent, live-streaming answer. Supersedes the buffer-until-terminal approach in branch fix/421-adapter-text-dup. Co-authored-by: Isaac Signed-off-by: MarioCadenas <MarioCadenas@users.noreply.github.com>
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.
Problem
With OpenAI-compatible Claude on Databricks Model Serving, the model emits a full draft answer ALONGSIDE its tool calls on every ReAct round, so the answer appeared 3-4x in the output. The earlier approach (branch
fix/421-adapter-text-dup) buffered text and flushed it only on the terminal turn — but that made the final answer arrive all-at-once instead of streaming, which is unacceptable. This PR closes/supersedes that approach and references issue #421.Root cause
The duplication was never in the adapter or the wire protocol.
AgentEventTranslatoralready closes the current message item (response.output_item.done) when atool_call/tool_resultarrives and opens a NEW message item for the next round's text — so each round's text is already a distinct Responses-API output item with its ownoutput_index/item_id. The duplication came from two consumers that concatenated deltas across those item boundaries.The three-layer fix (respect the item boundaries)
databricks.ts) streams every round's text live (already matchedmain— no change needed).consumeAdapterStreamkeeps only the terminal round: a draft followed by atool_call/tool_resultis set aside as superseded, and the message open at end-of-stream (or the last draft ifmaxStepsis exhausted mid-tool-calling) is returned. This centrally dedupes thread history, the non-streaming JSONfullContent,runAgent, and sub-agents.useAgentChatreduces the wire events into an ordered, per-output-item list while streaming live.contentnow tracks only the LAST message item (the terminal answer), so flat-API consumers that only readcontentdedupe too.New public hook API
UseAgentChatResultgainsitems: AgentTurnItem[](ordered byoutput_index). The dev-playground agent route uses it to render a collapsible "Steps" disclosure (intermediate drafts + tool-call/result chips) above the prominent, live-streaming answer.Tests
consume-adapter-stream.test.ts: multi-round draft+tool_call returns the terminal text once; maxSteps-exhaustion returns the last draft; LangChain singlemessagestill replaces; mixed.use-agent-chat.test.ts: two message items separated by a function_call →itemsis[message, tool_call, tool_result, message],content=== the LAST message text (not concatenated), deltas stream into the right item live.All
appkit(2147) andappkit-ui(319) tests pass; build, typecheck, biome, and knip clean.This pull request and its description were written by Isaac.