Add durable cancellation mode for useAgentChat#1484
Open
whoiskatrin wants to merge 1 commit intomainfrom
Open
Conversation
🦋 Changeset detectedLatest commit: d91facd The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
agents
@cloudflare/ai-chat
@cloudflare/codemode
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
|
@whoiskatrin LGTM thank you! |
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.
Summary
Adds an opt-in durable cancellation mode for
useAgentChat:durable: truemeans the browser is treated as a reconnectable observer of a server turn. Generic client-side stream abort/cleanup is local-only, but explicitstop()still sendscf_agent_chat_request_canceland cancels the server turn.For lower-level control, this also adds:
The default remains
"on-client-abort"for backwards compatibility.What the issue actually was
This is not about a raw WebSocket close directly calling server-side abort.
The problematic path is one layer higher:
useAgentChatwraps AI SDKuseChat.useChatowns a local active response with anAbortController.WebSocketChatTransport.sendMessages()receives that signal asoptions.abortSignal, and also exposes aReadableStreamwhosecancel()method can be called by the client stream lifecycle.onAbort()handler.cf_agent_chat_request_cancel.So the server could not distinguish these two cases:
For Durable Object backed agents, the second case should often be recoverable via stream resumption. The browser can disappear or detach while the durable server turn continues and buffers chunks for a later reconnect.
What changed
durable?: booleantouseAgentChat.durable: truemaps toserverTurnCancellation: "explicit-only".serverTurnCancellationpolicy."on-client-abort"preserves current behavior."explicit-only"makes generic client abort/stream cancel local-only.WebSocketChatTransport.cancelActiveServerTurn()for explicit server cancellation.useAgentChat().stop()so explicit stop still cancels the server turn even in durable mode.Tests
npm run checkcd packages/ai-chat && npm run testRelated
Fixes #1471