fix(ai-react): update messagesRef synchronously during render#373
fix(ai-react): update messagesRef synchronously during render#373DiegoGBrisa wants to merge 3 commits intoTanStack:mainfrom
Conversation
When setMessages and id change are batched into a single React render, the useEffect that updates messagesRef hasn't run yet. The new ChatClient is created with stale (empty) initialMessages, losing the conversation history. The test verifies that the adapter receives all previous messages when sending through the recreated client.
Replace useEffect ref update with synchronous render-time assignment so messagesRef is always current when useMemo creates a new ChatClient.
🦋 Changeset detectedLatest commit: c1176b2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
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 |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThe changes fix a staleness issue in the useChat hook by updating messagesRef synchronously during render instead of asynchronously in useEffect. This ensures the reference is current when ChatClient is recreated, with accompanying test coverage validating the behavior during client recreation with batched updates. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
View your CI Pipeline Execution ↗ for commit c1176b2
☁️ Nx Cloud last updated this comment at |
@tanstack/ai
@tanstack/ai-anthropic
@tanstack/ai-client
@tanstack/ai-devtools-core
@tanstack/ai-elevenlabs
@tanstack/ai-event-client
@tanstack/ai-fal
@tanstack/ai-gemini
@tanstack/ai-grok
@tanstack/ai-groq
@tanstack/ai-ollama
@tanstack/ai-openai
@tanstack/ai-openrouter
@tanstack/ai-preact
@tanstack/ai-react
@tanstack/ai-react-ui
@tanstack/ai-solid
@tanstack/ai-solid-ui
@tanstack/ai-svelte
@tanstack/ai-vue
@tanstack/ai-vue-ui
@tanstack/preact-ai-devtools
@tanstack/react-ai-devtools
@tanstack/solid-ai-devtools
commit: |
Summary
messagesRefinuseChatis updated viauseEffect(async), butuseMemoreads it during render (sync) to passinitialMessageswhen creating a newChatClient. WhensetMessagesand aclientIdchange are batched into a single React 18 render, the ref is stale — the new client is created with empty messages, losing the conversation history.The fix replaces the
useEffectref update with a synchronous render-time assignment somessagesRefis always current beforeuseMemoruns.Test plan
setMessages+idchange, then sends a message and verifies the adapter receives the full conversationSummary by CodeRabbit
Bug Fixes