-
Notifications
You must be signed in to change notification settings - Fork 2
PRD-2007: Edit AI message logic #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,10 +4,12 @@ import { FormValues } from '@open-webui-react-native/mobile/shared/utils/form'; | |
| import { | ||
| chatApi, | ||
| ChatResponse, | ||
| prepareUpdateMessageInChatPayload, | ||
| prepareCompleteChatPayload, | ||
| prepareUpdateMessageToSendPayload, | ||
| prepareEditAssistantMessagePayload, | ||
| prepareCopyEditedMessagePayload, | ||
| } from '@open-webui-react-native/shared/data-access/api'; | ||
| import { Role } from '@open-webui-react-native/shared/data-access/common'; | ||
| import { socketService } from '@open-webui-react-native/shared/data-access/websocket'; | ||
|
|
||
| interface UseEditMessageProps { | ||
|
|
@@ -47,7 +49,7 @@ export const useEditMessage = ({ chat, modelId }: UseEditMessageProps): typeof r | |
| return; | ||
| } | ||
|
|
||
| const preparedChat = prepareUpdateMessageInChatPayload(chat, editingMessageId, message); | ||
| const preparedChat = prepareCopyEditedMessagePayload(chat, editingMessageId, message); | ||
|
|
||
| await updateChat(preparedChat); | ||
| cancelEditing(); | ||
|
|
@@ -60,10 +62,18 @@ export const useEditMessage = ({ chat, modelId }: UseEditMessageProps): typeof r | |
| const chatHistory = chat.chat.history; | ||
| const editedMessage = chatHistory.messages[editingMessageId]; | ||
|
|
||
| const preparedChat = prepareUpdateMessageToSendPayload(chat, message, modelId, editedMessage.parentId); | ||
| let preparedChat: ChatResponse; | ||
|
|
||
| if (editedMessage.role === Role.ASSISTANT) { | ||
| preparedChat = prepareEditAssistantMessagePayload(chat, editingMessageId, message); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Tomass673 Could you please check the logic for “Save as copy”? When the “Save as copy” button is clicked, the Also, in order to update the history branch, the This comment briefly describes the flow for editing an AI message, with links to the relevant code in the web application – https://app.clickup.com/t/24336023/PRD-1965?comment=90150163295336 If implemented correctly, "Save As Copy" should work like this: Simulator.Screen.Recording.-.iPhone.17.-.2025-12-09.at.11.23.25.mov
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| } else { | ||
| preparedChat = prepareUpdateMessageToSendPayload(chat, message, modelId, editedMessage.parentId); | ||
| } | ||
|
|
||
| await updateChat(preparedChat, { | ||
| onSuccess: (data) => { | ||
| if (editedMessage.role === Role.ASSISTANT) return; | ||
|
|
||
| const completePayload = prepareCompleteChatPayload({ | ||
| chatId: data.id, | ||
| messageId: data.chat!.history.currentId, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { Role } from '@open-webui-react-native/shared/data-access/common'; | ||
| import { ChatResponse, Message } from '../models'; | ||
|
|
||
| export function prepareCopyEditedMessagePayload( | ||
| oldData: ChatResponse, | ||
| messageId: string, | ||
| newContent: string, | ||
| ): ChatResponse { | ||
| const history = oldData.chat.history; | ||
| const messagesMap = { ...history.messages }; | ||
|
|
||
| const target = messagesMap[messageId]; | ||
| if (!target) return oldData; | ||
|
|
||
| const isAIMessage = target.role === Role.ASSISTANT; | ||
|
|
||
| const updatedMessage: Message = { | ||
| ...target, | ||
| content: newContent, | ||
| }; | ||
| messagesMap[messageId] = updatedMessage; | ||
|
|
||
| if (!isAIMessage) { | ||
| const patchedMessagesList = oldData.chat.messages.map((msg) => (msg.id === messageId ? updatedMessage : msg)); | ||
| const lastAssistantMessage = oldData.chat.history.lastAssistantMessage; | ||
|
|
||
| return { | ||
| ...oldData, | ||
| chat: { | ||
| ...oldData.chat, | ||
| history: { | ||
| ...history, | ||
| messages: messagesMap, | ||
| lastAssistantMessage, | ||
| }, | ||
| messages: patchedMessagesList, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const chain: Array<Message> = []; | ||
| let pointer: Message | undefined = updatedMessage; | ||
|
|
||
| while (pointer) { | ||
| chain.unshift(pointer); | ||
| pointer = pointer.parentId ? messagesMap[pointer.parentId] : undefined; | ||
| } | ||
|
|
||
| const newCurrentId = updatedMessage.id; | ||
|
|
||
| return { | ||
| ...oldData, | ||
| chat: { | ||
| ...oldData.chat, | ||
| history: { | ||
| ...history, | ||
| messages: messagesMap, | ||
| currentId: newCurrentId, | ||
| lastAssistantMessage: updatedMessage, | ||
| }, | ||
| messages: chain, | ||
| }, | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { Role } from '@open-webui-react-native/shared/data-access/common'; | ||
| import { ChatResponse } from '../models'; | ||
|
|
||
| export function prepareEditAssistantMessagePayload( | ||
| oldData: ChatResponse, | ||
| messageId: string, | ||
| newContent: string, | ||
| ): ChatResponse { | ||
| const history = oldData.chat.history; | ||
| const messagesMap = { ...history.messages }; | ||
|
|
||
| const target = messagesMap[messageId]; | ||
| if (!target || target.role !== Role.ASSISTANT) return oldData; | ||
|
|
||
| const updatedMessage = { | ||
| ...target, | ||
| content: newContent, | ||
| done: true, | ||
| }; | ||
|
|
||
| messagesMap[messageId] = updatedMessage; | ||
|
|
||
| const updatedMessagesList = oldData.chat.messages.map((m) => (m.id === messageId ? updatedMessage : m)); | ||
|
|
||
| const lastAssistantMessage = | ||
| history.lastAssistantMessage?.id === messageId ? updatedMessage : history.lastAssistantMessage; | ||
|
|
||
| return { | ||
| ...oldData, | ||
| chat: { | ||
| ...oldData.chat, | ||
| history: { | ||
| ...history, | ||
| messages: messagesMap, | ||
| lastAssistantMessage, | ||
| }, | ||
| messages: updatedMessagesList, | ||
| }, | ||
| }; | ||
| } |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that due to changes in this logic, we can no longer change the text of the user's message when pressing the "Save" button.
Simulator.Screen.Recording.-.iPhone.17.-.2025-12-15.at.10.50.17.mov
Could you please check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restored this functionality