Repository navigation
fix(agents): fire useAgentChat onToolCall only after the stream ends - #2361
Merged
Merged
Conversation
…2195) Co-authored-by: Cursor <cursoragent@cursor.com>
🦋 Changeset detectedLatest commit: 22dbebb The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
🟡 agents import sizesMeasured 344 runtime imports as minified bundles. The primary size is gzip; raw minified size is included for diagnosis. An existing import growing by more than 10% is marked red. This report is informational.
Compared Changed imports (3)
All 344 current runtime imports
Reported by agent-think[bot]. |
agents
@cloudflare/ai-chat
@cloudflare/codemode
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
…on close Co-authored-by: Cursor <cursoragent@cursor.com>
…le or a local turn Co-authored-by: Cursor <cursoragent@cursor.com>
|
|
||
| When the LLM invokes `getLocation`, the stream pauses. The `onToolCall` callback fires, your code provides the output, and the conversation continues. | ||
|
|
||
| `onToolCall` fires once the response stream ends, for each tool call that is still waiting for a result. A server tool runs and resolves in the same stream, so it never reaches `onToolCall`. |
Contributor
Contributor
Author
There was a problem hiding this comment.
Docs here are ported to cloudflare-docs by hand. I will open the matching cloudflare-docs change once this lands; leaving this open until then.
…-turn Co-authored-by: Cursor <cursoragent@cursor.com>
Merged
tombeckenham
added a commit
to tombeckenham/agents-tom
that referenced
this pull request
Oct 7, 2026
…port - appliedChunks ledger: a continuation replayed after a reconnect skips the frames this client already applied, decided as each frame arrives and applied to the projected chunks so the projector still sees the whole run (cloudflare#1951, upstream cloudflare#2348) - a socket close before the terminal frame marks the stream interrupted, and the AI SDK transport ends it with an error chunk behind the unread chunks (cloudflare#2013, upstream cloudflare#2019) - activeServerTurnId getter for the hook's held-turn tracking (cloudflare#2361) - onBuffered / onRequestBuffered when the request frame was buffered, and a close while the body is still being prepared no longer drops the request (cloudflare#1983, upstream cloudflare#2039) - the chunk stream keeps pulling past events that render nothing, which stalled a waiting reader on RUN_STARTED followed by STEP_STARTED The two stack tests that pinned a clean close mid-stream now expect the error chunk, as upstream changed its own in cloudflare#2019.
tombeckenham
added a commit
to tombeckenham/agents-tom
that referenced
this pull request
Oct 7, 2026
Brings react-agui.tsx level with the legacy hook in agents/chat/react, which received these through the upstream merge: - cloudflare#2344 the server snapshot heals a diverged observed assistant - cloudflare#2348 an observer skips replayed continuation frames it already applied - cloudflare#2361 onToolCall fires only after the stream ends - cloudflare#2378 onTurnEnd fires once per ended request - cloudflare#2383 turns settle across replay, resume and observers (held and recovering requests, the resume:false idle probe) - cloudflare#2496 streaming protection is released when the socket closes - cloudflare#2394 follow the socket when the agent name changes - cloudflare#2403 the tool-result cleanup no longer dispatches per chunk - cloudflare#2039 keep an optimistic send the reconnect transcript omits The last three had no failing test: their upstream tests are ported into react-tests (the address-change one against a fake of useAgent's pending socket, since this package has no test worker) and failed before the fix. restoreBufferedSends, MAX_REMEMBERED_ENDED_TURNS and isSocketAddressPending are exported from agents/chat/react so the two hooks share them.
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.
Fixes #2195.
Summary
useAgentChatfiredonToolCallfor tool calls while the response was still streaming, including server tools the server was about to run. An app that answered an unknown tool with an error sent the server a false failure for that call, and that turned off auto-continuation for the turn.onToolCallnow runs only once the response stream has ended (the status is neitherstreamingnorsubmitted, and no server stream is being observed). It fires for each tool call still waiting for a result. By then, server tools have their outputs, so only client tools are left.Tests
packages/ai-chat/src/react-tests/use-agent-chat.test.tsx: a server tool streamed mid-turn does not reachonToolCall, and a client tool still does after the stream ends.statusremainsreadyafter initial tool call. #1157 tests adapted to the new timing.docs/agents/chat-agents.md.