Fix reload and edit functionality with messageId-based operations#583
Fix reload and edit functionality with messageId-based operations#583
Conversation
- Update chat.tsx to properly handle message editing without creating duplicates - Simplify reload functionality to use SDK's regenerate function with messageId - Fix server-side handling to properly save edited messages before regeneration - Remove dependency on createdAt timestamps in favor of messageId-based operations - Ensure edited messages maintain their original ID for proper history tracking
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the reload and edit functionality to use messageId-based operations instead of timestamp-based operations, improving reliability and aligning with Vercel AI SDK v5 patterns.
Key changes include:
- Replaced timestamp-based message operations with messageId-based operations for better precision
- Updated edit functionality to modify messages in-place rather than creating duplicates
- Simplified reload logic to leverage SDK's built-in regenerate function
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
lib/streaming/create-chat-stream-response.ts |
Enhanced regeneration logic to handle both assistant and user message regeneration with messageId-based operations |
components/chat.tsx |
Simplified edit and reload handlers to use SDK's regenerate function and in-place message updates |
app/search/loading.tsx |
Minor CSS class update removing h-full from container |
app/api/chat/route.ts |
Updated API route to handle single message objects instead of arrays with improved validation |
| } | ||
| // Get updated messages including the edited one | ||
| const updatedChat = await getChatAction(chatId, userId) | ||
| messagesToModel = updatedChat?.messages || currentChat.messages.slice(0, messageIndex + 1) |
There was a problem hiding this comment.
The fallback logic is inconsistent. If updatedChat is null, using currentChat.messages.slice(0, messageIndex + 1) may not include the edited message that was just saved. Consider handling this case more explicitly or ensuring the edited message is properly included.
| messagesToModel = updatedChat?.messages || currentChat.messages.slice(0, messageIndex + 1) | |
| messagesToModel = updatedChat?.messages || [ | |
| ...currentChat.messages.slice(0, messageIndex), | |
| message | |
| ] |
components/chat.tsx
Outdated
| // Include the message if it's a user message (for edit cases) | ||
| message: messageToRegenerate?.role === 'user' ? messageToRegenerate : undefined |
There was a problem hiding this comment.
[nitpick] The conditional logic for including the message could be clearer. Consider adding a comment explaining why only user messages are included during regeneration, or extract this logic into a helper function for better readability.
| // Include the message if it's a user message (for edit cases) | |
| message: messageToRegenerate?.role === 'user' ? messageToRegenerate : undefined | |
| // Use helper function to determine if the message should be included | |
| message: shouldIncludeMessageForRegeneration(messageToRegenerate) |
app/api/chat/route.ts
Outdated
| } else { | ||
| // Get the last message from the messages array (the new user message) | ||
| message = messages?.[messages.length - 1] | ||
| if (trigger === 'regenerate-assistant-message' && !messageId) { |
There was a problem hiding this comment.
The validation logic only checks for messageId when the trigger is 'regenerate-assistant-message', but based on the server-side logic, messageId appears to be required for all regeneration operations. Consider validating messageId for all regeneration triggers or clarifying the API contract.
- Fix fallback logic for edited messages when updatedChat is null - Improve validation structure for different triggers - Ensure edited messages are properly handled in all cases
Fix reload and edit functionality with messageId-based operations
Overview
This PR fixes the reload and edit functionality to use messageId-based operations instead of timestamp-based operations, making the implementation more reliable and aligned with Vercel AI SDK v5 patterns.
Changes
Client-side (chat.tsx)
handleUpdateAndReloadMessageto edit messages in-place without creating duplicateshandleReloadFromto use the SDK's built-inregeneratefunctionprepareSendMessagesRequestto include edited user messages during regenerationServer-side
Key Improvements
Testing