Skip to content

feat(ai-chat): expose requestId in OnChatMessageOptions#977

Merged
threepointone merged 2 commits into
cloudflare:mainfrom
dmmulroy:feat/expose-request-id-in-onchatmessage
Feb 24, 2026
Merged

feat(ai-chat): expose requestId in OnChatMessageOptions#977
threepointone merged 2 commits into
cloudflare:mainfrom
dmmulroy:feat/expose-request-id-in-onchatmessage

Conversation

@dmmulroy

Copy link
Copy Markdown
Collaborator

Summary

  • Add requestId: string to OnChatMessageOptions
  • Pass the ID at all 3 onChatMessage call sites (initial request, tool continuation, approval continuation)

Problem

onChatMessage handlers cannot send error responses for pre-stream failures (e.g. body validation, auth checks) because they lack the request ID needed to tag CF_AGENT_USE_CHAT_RESPONSE messages. The client transport filters by data.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:

  • Throw_tryCatchChatonError → PartyServer swallows it. Client stream hangs forever.
  • Return error Response_reply sends it as a normal assistant text message (no error: true flag). 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:

Call site ID source
Initial chat request data.id (client-generated)
Tool continuation continuationId (server-generated via nanoid())
Approval continuation continuationId (server-generated via nanoid())

This lets onChatMessage send properly-tagged error responses:

async onChatMessage(onFinish, options) {
  const result = MySchema.safeParse(options?.body);
  if (!result.success) {
    const { connection } = getCurrentAgent();
    connection?.send(JSON.stringify({
      type: "cf_agent_use_chat_response",
      id: options.requestId,
      body: result.error.message,
      done: true,
      error: true,
    }));
    return; // no Response needed
  }
  // ... normal flow
}

Non-breaking: requestId is a new required field on OnChatMessageOptions, but this type is only constructed internally by AIChatAgent — user code receives it, never constructs it.

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-bot

changeset-bot Bot commented Feb 24, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e3ee2fb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@cloudflare/ai-chat Patch

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

@pkg-pr-new

pkg-pr-new Bot commented Feb 24, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@977
npm i https://pkg.pr.new/cloudflare/agents/@cloudflare/ai-chat@977
npm i https://pkg.pr.new/cloudflare/agents/@cloudflare/codemode@977
npm i https://pkg.pr.new/cloudflare/agents/hono-agents@977

commit: e3ee2fb

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
threepointone merged commit 5426b6f into cloudflare:main Feb 24, 2026
4 checks passed
@github-actions github-actions Bot mentioned this pull request Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants