feat(ai-chat): expose requestId in OnChatMessageOptions#977
Merged
threepointone merged 2 commits intoFeb 24, 2026
Merged
Conversation
onChatMessage handlers cannot send error responses for pre-stream failures (e.g. body validation) because they lack the request ID needed to tag CF_AGENT_USE_CHAT_RESPONSE messages. Without a matching ID the client transport silently drops the message. Add requestId to OnChatMessageOptions and pass it at all three call sites: initial chat request, tool continuation, and approval continuation.
🦋 Changeset detectedLatest commit: e3ee2fb 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 |
commit: |
Add requestId to OnChatMessageOptions and ensure saveMessages generates a consistent requestId, obtains the abortSignal, and passes the full options (requestId, abortSignal, clientTools, body) to onChatMessage; reply messages now use the same requestId. Update TestChatAgent to capture requestId and add comprehensive tests (packages/ai-chat/src/tests/request-id.test.ts) covering client-provided and server-generated request IDs, continuation flows, response tagging, and pre-stream error handling. Include a changeset describing the patch.
threepointone
approved these changes
Feb 24, 2026
Merged
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
requestId: stringtoOnChatMessageOptionsonChatMessagecall sites (initial request, tool continuation, approval continuation)Problem
onChatMessagehandlers cannot send error responses for pre-stream failures (e.g. body validation, auth checks) because they lack the request ID needed to tagCF_AGENT_USE_CHAT_RESPONSEmessages. The client transport filters bydata.id !== requestId, so messages without the correct ID are silently dropped.The connection is accessible via
getCurrentAgent().connection, but without the request ID there is no way to construct a message the client will accept.Current workarounds are all bad:
_tryCatchChat→onError→ PartyServer swallows it. Client stream hangs forever._replysends it as a normal assistant text message (noerror: trueflag). Client renders a Zod error as if the assistant said it.safeParse+ silent fallback → Bad input is invisible. Works but masks client bugs.Solution
Expose the request ID that already exists at each call site:
data.id(client-generated)continuationId(server-generated viananoid())continuationId(server-generated viananoid())This lets
onChatMessagesend properly-tagged error responses:Non-breaking:
requestIdis a new required field onOnChatMessageOptions, but this type is only constructed internally byAIChatAgent— user code receives it, never constructs it.