Skip to content

fix(ai-chat): preserve first useAgentChat message during initial URL resolution#1541

Merged
threepointone merged 4 commits into
cloudflare:mainfrom
whoiskatrin:fix/use-agent-chat-initial-send-race
May 19, 2026
Merged

fix(ai-chat): preserve first useAgentChat message during initial URL resolution#1541
threepointone merged 4 commits into
cloudflare:mainfrom
whoiskatrin:fix/use-agent-chat-initial-send-race

Conversation

@whoiskatrin
Copy link
Copy Markdown
Contributor

@whoiskatrin whoiskatrin commented May 16, 2026

Summary

Fix a mount-time race in useAgentChat where a message sent immediately after first render could disappear from the hook's messages array.

Problem

When useAgentChat mounts before the Agent HTTP URL is available:

  1. The hook creates its initial AI SDK Chat instance using a fallback chat ID.
  2. The user sends a message immediately, so the message is optimistically added locally.
  3. The Agent HTTP URL becomes available on a later render.
  4. useAgentChat upgrades the chat ID to a URL-derived ID.
  5. The AI SDK recreates the underlying Chat instance, which drops the optimistic message state.

Depending on timing, the first sent message would not remain visible in messages.

Fix

Keep the initial Chat ID stable across the HTTP URL transition for the same mounted Agent instance.

The hook still recreates the chat when the consumer genuinely switches to a different Agent/chat, but it no longer recreates the chat just because the Agent URL becomes available after mount.

When initial persisted history resolves after an optimistic first send, late hydration now prepends missing hydrated messages instead of overwriting or discarding either side. Existing in-memory messages win by ID so newer local/stream state is not replaced by the fetch response.

Regression tests

Added React regression coverage for:

  • mounting useAgentChat before getHttpUrl() is ready
  • immediately calling sendMessage()
  • making the HTTP URL become available
  • verifying the optimistic first message remains present
  • verifying late persisted history and the optimistic first send are both retained

Validation

Passed:

  • npm run test:react --workspace packages/ai-chat
  • npx oxfmt --check packages/ai-chat/src/react.tsx packages/ai-chat/src/react-tests/use-agent-chat.test.tsx

Also ran npm run check. Sherif, export validation, repository formatting, and Oxlint completed successfully; the typecheck stage could not execute in this local environment because tsgo was unavailable (/bin/sh: tsgo: command not found).

Release note

Includes a patch changeset for @cloudflare/ai-chat.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 16, 2026

🦋 Changeset detected

Latest commit: 8677a96

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
Copy link
Copy Markdown

pkg-pr-new Bot commented May 16, 2026

Open in StackBlitz

agents

npm i https://pkg.pr.new/agents@1541

@cloudflare/ai-chat

npm i https://pkg.pr.new/@cloudflare/ai-chat@1541

@cloudflare/codemode

npm i https://pkg.pr.new/@cloudflare/codemode@1541

hono-agents

npm i https://pkg.pr.new/hono-agents@1541

@cloudflare/shell

npm i https://pkg.pr.new/@cloudflare/shell@1541

@cloudflare/think

npm i https://pkg.pr.new/@cloudflare/think@1541

@cloudflare/voice

npm i https://pkg.pr.new/@cloudflare/voice@1541

@cloudflare/worker-bundler

npm i https://pkg.pr.new/@cloudflare/worker-bundler@1541

commit: 8677a96

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

@whoiskatrin whoiskatrin marked this pull request as ready for review May 19, 2026 14:44
@threepointone threepointone merged commit bc2b1d3 into cloudflare:main May 19, 2026
4 checks passed
@github-actions github-actions Bot mentioned this pull request May 19, 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