From 8c011974834dcb2f5b4d72bc6e22843e647bc3b5 Mon Sep 17 00:00:00 2001 From: Danny Liu Date: Mon, 4 Mar 2024 12:34:14 -0800 Subject: [PATCH] refac: reassign grandchildren of deleted message as children of parent message --- src/lib/components/chat/Messages.svelte | 32 ++++++++++++++++++++----- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/lib/components/chat/Messages.svelte b/src/lib/components/chat/Messages.svelte index 5711fb17..3f1ee28b 100644 --- a/src/lib/components/chat/Messages.svelte +++ b/src/lib/components/chat/Messages.svelte @@ -224,15 +224,35 @@ }; const messageDeleteHandler = async (messageId) => { - const messageParentId = history.messages[messageId]?.parentId; + const messageToDelete = history.messages[messageId]; + const messageParentId = messageToDelete.parentId; + const messageChildrenIds = messageToDelete.childrenIds ?? []; - if (messageParentId !== null) { - history.messages[messageParentId].childrenIds = [] - } + messageChildrenIds.forEach((childId) => { + const child = history.messages[childId]; + if (child && child.childrenIds) { + if (child.childrenIds.length == 0) { // if last prompt/response pair + history.messages[messageParentId].childrenIds = [] + history.currentId = messageParentId; + } + else { + child.childrenIds.forEach((grandChildId) => { + if (history.messages[grandChildId]) { + history.messages[grandChildId].parentId = messageParentId; + history.messages[messageParentId].childrenIds.push(grandChildId); + } + }); + } + } - history.currentId = messageParentId; + // remove response + history.messages[messageParentId].childrenIds = history.messages[messageParentId].childrenIds + .filter((id) => id !== childId); + }); - await tick(); + // remove prompt + history.messages[messageParentId].childrenIds = history.messages[messageParentId].childrenIds + .filter((id) => id !== messageId); await updateChatById(localStorage.token, chatId, { messages: messages,