Commit 550e413
authored
fix(react): preserve chat when id is undefined (#16484)
## Background
Passing `id: undefined` to `useChat` currently recreates the internal
`Chat` instance on every render because the `id` key exists in options
while the auto-generated chat ID never equals `undefined`. That clears
messages and stream state immediately after updates, which makes the
common `id={conversationId ?? undefined}` pattern behave differently
from omitting `id`.
I confirmed the reproduction from #16226 with a focused regression test:
with the old guard, calling `setMessages` while `id` is explicitly
`undefined` immediately rerenders into a fresh chat with a new generated
ID and `messages: []`.
I compared the two open PRs before choosing this implementation:
- #16228 by @Aayush-engineer uses the nullish guard and adds a
changeset, but does not include a regression test.
- #16403 by @zhsj0089944 uses an `undefined` guard, but does not include
a regression test or changeset.
This PR uses the nullish guard from #16228 because it makes explicit
`undefined` behave like an omitted ID and also treats a JavaScript
`null` value defensively as “no provided ID”. It adds the missing
hook-level regression test and a patch changeset.
Credit to @Aayush-engineer and @zhsj0089944 for the prior PRs and
diagnosis.
## Summary
- Only recreate the internal `Chat` from `useChat` when a provided `id`
is non-nullish and different from the current chat ID.
- Add a regression test that verifies `useChat({ id: undefined })`
preserves the generated ID and messages across rerenders.
- Add a patch changeset for `@ai-sdk/react`.
## Manual Verification
Confirmed the reproduction by temporarily restoring the old guard and
running:
```sh
pnpm --filter @ai-sdk/react exec vitest --config vitest.config.js --run src/use-chat.ui.test.tsx -t "should not recreate chat when id is explicitly undefined"
```
With the old guard, the test failed because messages reset to `[]` and
the generated chat ID changed. With this fix, the same test passes.
## Checklist
- [x] All commits are signed (PRs with unsigned commits cannot be
merged)
- [x] Tests have been added / updated (for bug fixes / features)
- [ ] Documentation has been added / updated (for bug fixes / features)
- [x] A _patch_ changeset for relevant packages has been added (for bug
fixes / features - run `pnpm changeset` in the project root)
- [x] I have reviewed this pull request (self-review)
## Related Issues
Fixes #16226.
Supersedes and closes #16228.
Supersedes and closes #16403.1 parent b19977f commit 550e413
3 files changed
Lines changed: 86 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
100 | 102 | | |
101 | 103 | | |
102 | 104 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2424 | 2424 | | |
2425 | 2425 | | |
2426 | 2426 | | |
| 2427 | + | |
| 2428 | + | |
| 2429 | + | |
| 2430 | + | |
| 2431 | + | |
| 2432 | + | |
| 2433 | + | |
| 2434 | + | |
| 2435 | + | |
| 2436 | + | |
| 2437 | + | |
| 2438 | + | |
| 2439 | + | |
| 2440 | + | |
| 2441 | + | |
| 2442 | + | |
| 2443 | + | |
| 2444 | + | |
| 2445 | + | |
| 2446 | + | |
| 2447 | + | |
| 2448 | + | |
| 2449 | + | |
| 2450 | + | |
| 2451 | + | |
| 2452 | + | |
| 2453 | + | |
| 2454 | + | |
| 2455 | + | |
| 2456 | + | |
| 2457 | + | |
| 2458 | + | |
| 2459 | + | |
| 2460 | + | |
| 2461 | + | |
| 2462 | + | |
| 2463 | + | |
| 2464 | + | |
| 2465 | + | |
| 2466 | + | |
| 2467 | + | |
| 2468 | + | |
| 2469 | + | |
| 2470 | + | |
| 2471 | + | |
| 2472 | + | |
| 2473 | + | |
| 2474 | + | |
| 2475 | + | |
| 2476 | + | |
| 2477 | + | |
| 2478 | + | |
| 2479 | + | |
| 2480 | + | |
| 2481 | + | |
| 2482 | + | |
| 2483 | + | |
| 2484 | + | |
| 2485 | + | |
| 2486 | + | |
| 2487 | + | |
| 2488 | + | |
| 2489 | + | |
| 2490 | + | |
| 2491 | + | |
| 2492 | + | |
| 2493 | + | |
| 2494 | + | |
| 2495 | + | |
| 2496 | + | |
| 2497 | + | |
| 2498 | + | |
| 2499 | + | |
| 2500 | + | |
| 2501 | + | |
| 2502 | + | |
| 2503 | + | |
| 2504 | + | |
2427 | 2505 | | |
2428 | 2506 | | |
2429 | 2507 | | |
| |||
0 commit comments