Skip to content

Fix reload and edit functionality with messageId-based operations#583

Merged
miurla merged 3 commits intov0.5from
fix/reload-edit-functionality
Jul 27, 2025
Merged

Fix reload and edit functionality with messageId-based operations#583
miurla merged 3 commits intov0.5from
fix/reload-edit-functionality

Conversation

@miurla
Copy link
Copy Markdown
Owner

@miurla miurla commented Jul 27, 2025

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)

  • Updated handleUpdateAndReloadMessage to edit messages in-place without creating duplicates
  • Simplified handleReloadFrom to use the SDK's built-in regenerate function
  • Modified prepareSendMessagesRequest to include edited user messages during regeneration

Server-side

  • Updated API route to properly handle message objects instead of arrays
  • Enhanced stream handler to save edited messages before regeneration
  • Improved regeneration logic to handle both assistant and user message regeneration

Key Improvements

  • No more createdAt dependency: All operations now use messageId for precision
  • Fixed message duplication: Edit functionality now updates existing messages instead of creating new ones
  • Cleaner implementation: Leverages SDK's built-in functionality for regeneration
  • Better error handling: More robust error messages and validation

Testing

  • Tested reload functionality for both user and assistant messages
  • Verified edit functionality properly updates messages without duplication
  • Confirmed messageId-based deletion works correctly

- 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
@vercel
Copy link
Copy Markdown

vercel bot commented Jul 27, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
morphic ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 27, 2025 7:25am

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
messagesToModel = updatedChat?.messages || currentChat.messages.slice(0, messageIndex + 1)
messagesToModel = updatedChat?.messages || [
...currentChat.messages.slice(0, messageIndex),
message
]

Copilot uses AI. Check for mistakes.
Comment on lines +67 to +68
// Include the message if it's a user message (for edit cases)
message: messageToRegenerate?.role === 'user' ? messageToRegenerate : undefined
Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Suggested change
// 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)

Copilot uses AI. Check for mistakes.
} else {
// Get the last message from the messages array (the new user message)
message = messages?.[messages.length - 1]
if (trigger === 'regenerate-assistant-message' && !messageId) {
Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
- 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
@miurla miurla merged commit 9184609 into v0.5 Jul 27, 2025
3 checks passed
@miurla miurla deleted the fix/reload-edit-functionality branch July 27, 2025 07:25
byte-rose pushed a commit to Bastsec/morphic that referenced this pull request Sep 23, 2025
Fix reload and edit functionality with messageId-based operations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants