Repository navigation
fix(cloudflare-agents): snapshot every step's assistant message in order - #3192
rudycelekli wants to merge 1 commit into
Conversation
MESSAGES_SNAPSHOT was built from one running text buffer, one tool-call list and one tool-result list, with the assistant message given the id of the LAST text message. In a multi-step run (text, tool call, tool result, more text) the snapshot dropped the first assistant message the client had already rendered. It put all text from every step into the last message with no separator, attached the earlier step's tool calls to it, and placed the tool results after the final answer. The client replaced its history with that, so the next turn sent the model a reordered, merged conversation. Build the snapshot in stream order: one assistant message per text message (holding the calls whose TOOL_CALL_START named it as parent), with the tool results where they occurred. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Hey @rudycelekli, Apologies for the delayed response. Nothing blocking from me on this one. Text, then a tool call, its result, then more text now comes out of the snapshot in that order, with each step's text in its own assistant message. A tool call in a later step with no new text stays attached to the earlier message, matching its @contextablemark, this one looks ready for a maintainer review. |
|
Thanks @rudycelekli, this fix is right. It's included in #3365 as your own commit (authorship kept), which reworks |
|
Thanks for this fix! It landed as part of #3365, which reworks the adapter for the current Cloudflare Agents SDK: your commit is included there with your authorship, and every step's assistant message is now snapshotted in order. Closing this one in favour of #3365; the change ships in the next |
What's wrong
AgentsToAGUIAdapter.adaptStreamToAGUIbuilds its closingMESSAGES_SNAPSHOTfrom three run-wide accumulators: onetextContentstring, onetoolCallslist and onetoolMessageslist. It emits them as a single assistant message, with the id of whichever text message was streamed last, followed by all the tool results.That only works for single-step runs. In a multi-step AI SDK run, for example a server tool with
stopWhen/maxSteps, the stream is:The snapshot on
mainis:Because the client replaces its message list with the snapshot, this causes three problems:
M1, which the client already rendered, disappears.M2shows both steps' text run together.The fix
The snapshot is now built in stream order:
TOOL_CALL_STARTnamed it as parent;For calls streamed through
tool-input-*, the parent is recorded attool-input-start, so it matches the emitted event. A run with no text still gets one assistant message, filled fromstream.textas before. Single-step snapshots are unchanged, and every existing snapshot test passes as is.Test
A new case in
src/adapter.test.ts, "MESSAGES_SNAPSHOT keeps each step's assistant message in stream order", runs the sequence above. It expects[user, assistant M1 ("Let me check.", toolCalls [tc1]), tool tc1, assistant M2 ("It is sunny.")], with ids matching the emittedTEXT_MESSAGE_STARTandTOOL_CALL_RESULTevents.mainit fails.src/adapter.tsreverted and the test kept, the named test fails again (23 others pass).Checks run (
integrations/community/cloudflare-agents/typescript)vitest run: 3 files, 46 tests passedtsc --noEmit: clean🤖 Generated with Claude Code