From 4a67ae119502f61b933ff14eade9f9743808c119 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 16 Jun 2024 22:28:26 -0700 Subject: [PATCH] fix: message delete issue --- src/lib/components/chat/Messages.svelte | 59 +++++---- .../chat/Messages/ResponseMessage.svelte | 116 +++++++++--------- 2 files changed, 94 insertions(+), 81 deletions(-) diff --git a/src/lib/components/chat/Messages.svelte b/src/lib/components/chat/Messages.svelte index f633b5774..3cab9a584 100644 --- a/src/lib/components/chat/Messages.svelte +++ b/src/lib/components/chat/Messages.svelte @@ -202,38 +202,51 @@ }, 100); }; - const messageDeleteHandler = async (messageId) => { + const deleteMessageHandler = async (messageId) => { const messageToDelete = history.messages[messageId]; - const messageParentId = messageToDelete.parentId; - const messageChildrenIds = messageToDelete.childrenIds ?? []; - const hasSibling = messageChildrenIds.some( + + const parentMessageId = messageToDelete.parentId; + const childMessageIds = messageToDelete.childrenIds ?? []; + + const hasDescendantMessages = childMessageIds.some( (childId) => history.messages[childId]?.childrenIds?.length > 0 ); - messageChildrenIds.forEach((childId) => { - const child = history.messages[childId]; - if (child && child.childrenIds) { - if (child.childrenIds.length === 0 && !hasSibling) { - // if last prompt/response pair - history.messages[messageParentId].childrenIds = []; - history.currentId = messageParentId; + + history.currentId = parentMessageId; + await tick(); + + // Remove the message itself from the parent message's children array + history.messages[parentMessageId].childrenIds = history.messages[ + parentMessageId + ].childrenIds.filter((id) => id !== messageId); + + await tick(); + + childMessageIds.forEach((childId) => { + const childMessage = history.messages[childId]; + + if (childMessage && childMessage.childrenIds) { + if (childMessage.childrenIds.length === 0 && !hasDescendantMessages) { + // If there are no other responses/prompts + history.messages[parentMessageId].childrenIds = []; } else { - child.childrenIds.forEach((grandChildId) => { + childMessage.childrenIds.forEach((grandChildId) => { if (history.messages[grandChildId]) { - history.messages[grandChildId].parentId = messageParentId; - history.messages[messageParentId].childrenIds.push(grandChildId); + history.messages[grandChildId].parentId = parentMessageId; + history.messages[parentMessageId].childrenIds.push(grandChildId); } }); } } - // remove response - history.messages[messageParentId].childrenIds = history.messages[ - messageParentId + + // Remove child message id from the parent message's children array + history.messages[parentMessageId].childrenIds = history.messages[ + parentMessageId ].childrenIds.filter((id) => id !== childId); }); - // remove prompt - history.messages[messageParentId].childrenIds = history.messages[ - messageParentId - ].childrenIds.filter((id) => id !== messageId); + + await tick(); + await updateChatById(localStorage.token, chatId, { messages: messages, history: history @@ -292,7 +305,7 @@ > {#if message.role === 'user'} messageDeleteHandler(message.id)} + on:delete={() => deleteMessageHandler(message.id)} {user} {readOnly} {message} @@ -308,7 +321,7 @@ copyToClipboard={copyToClipboardWithToast} /> {:else if $mobile || (history.messages[message.parentId]?.models?.length ?? 1) === 1} - {#key message.id} + {#key message.id && history.currentId} - {/if} - {#if isLastMessage && !readOnly} - - - + + + + + + - - - + + + + + + {/if} {/if} {/if}