Commit 013d15d
fix: restore chat history loading in playground (#999)
## Summary
Fixes chat history loading in the Playground after the AI SDK 5 upgrade
broke the functionality.
## Problem
The AI SDK 5 upgrade (commit c213781) introduced a regression where
chat history was being fetched from the API but never passed to the
`useChat()` hook, causing previously saved messages to be lost when
users loaded existing chats from the dropdown.
## Root Cause
- `useChatHistory()` was fetching chat history correctly via React Query
- The result was stored in `_chatHistory` (underscore indicating
intentionally unused)
- AI SDK 5's `useChat()` hook was called without `initialMessages`
parameter
- Chat always started empty, ignoring saved history
## Solution
Created a bridge between React Query (server state) and AI SDK (local
state):
1. **Extract chat history** from `useChatHistory()` (remove underscore
prefix)
2. **Get `setMessages`** from `useChat()` hook for message
initialization
3. **Add `useEffect`** to load history once per chat ID after React
Query finishes
4. **Use `isLoading` state** from React Query instead of arbitrary
timeouts
## Technical Details
The `useEffect` is necessary because:
- React Query manages server-side chat history fetching
- AI SDK 5 manages local chat interaction state
- These are separate state systems that need synchronization
- The effect only runs once per unique chat ID (tracked via ref)
## Changes
**File**: `client/dashboard/src/pages/playground/ChatWindow.tsx`
- Line 157-158: Destructure `chatHistory` and `isLoading` from
`useChatHistory()`
- Line 166: Add `loadedChatIdRef` to track loaded chat IDs
- Line 415: Extract `setMessages` from `useChat()` return value
- Lines 466-485: Add `useEffect` to bridge React Query → AI SDK state
## Testing
✅ Chat history loads correctly when selecting from dropdown
✅ No infinite loops or excessive re-renders
✅ Server requests are not canceled repeatedly
✅ React Query loading state properly synchronized
## Follows Best Practices
✅ All data fetching uses React Query (not manual `useEffect`)
✅ The `useEffect` is for state synchronization, not data fetching
✅ Uses React Query's `isLoading` state declaratively
✅ Minimal, focused bridge between two state management systems
Fixes AGE-969
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>1 parent 2667ecf commit 013d15d
File tree
2 files changed
+29
-1
lines changed- .changeset
- client/dashboard/src/pages/playground
2 files changed
+29
-1
lines changed| 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 | |
|---|---|---|---|
| |||
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
157 | | - | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
158 | 160 | | |
159 | 161 | | |
160 | 162 | | |
161 | 163 | | |
162 | 164 | | |
163 | 165 | | |
164 | 166 | | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
165 | 170 | | |
166 | 171 | | |
167 | 172 | | |
| |||
412 | 417 | | |
413 | 418 | | |
414 | 419 | | |
| 420 | + | |
415 | 421 | | |
416 | 422 | | |
417 | 423 | | |
| |||
458 | 464 | | |
459 | 465 | | |
460 | 466 | | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
461 | 484 | | |
462 | 485 | | |
463 | 486 | | |
| |||
0 commit comments