[v6.0] fix(react): use the latest transport in useChat instead of a stale one#16806
Merged
Conversation
…one (#16546) `useChat()` creates its `Chat` instance once and stores it in a `useRef()`, so as a result, a `transport` passed to `useChat()` was captured and then frozen at its value in the first render. If react state later changes, the `transport` becomes stale. A very similar problem was addressed in #8985 but with callbacks. Rather than passing the transport directly to `Chat`, this passes a proxy object that always reads the latest transport (or falls back to a default one). This uses the same solution as in #8985 for callbacks. Added a route to the ai-e2e-next example at `/chat/stale-transport-demo`. It reproduces the bug without the changes made, and then is fixed with these changes. Fixes #7819 Closes #12330 Closes #13464 Closes #15380 Closes #16326 (cherry picked from commit 8b2dc3c)
Contributor
|
|
Contributor
|
🚀 Published in:
|
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.
Backport of #16546 to
release-v6.0.useChat()creates itsChatinstance once in auseRef, so atransportpassed touseChat()was frozen at its first-render value and became stale after React state changes; the fix extends the existing latest-values ref pattern (already used for callbacks on v6) to also cover the transport, handingChata stable proxy transport whosesendMessages/reconnectToStreamalways delegate to the latest transport (falling back to a lazily createdDefaultChatTransport). Theuse-chat.tsrewrite fromcallbacksReftolatestRefapplied cleanly and matches main exactly, except that v6'sshouldRecreateChatguard is kept as-is (the nullish-id guard from #16484 is backported independently in its own PR); conflict resolution was limited to the import block (v6 does not importfireEventin the test file) and re-indenting the newstale transporttest describe block to v6's flat test file structure (main wraps tests in an outerdescribe('use-chat')). Verified the ported regression test fails on v6 without the fix and passes with it. Part of the v6.0 backport tracking issue #16767.