From f9f3fe5ec02ef63259e1ad3ac2fa1de7335f9e54 Mon Sep 17 00:00:00 2001 From: Danny Liu Date: Sat, 2 Mar 2024 23:23:49 -0800 Subject: [PATCH 1/6] refac delete logic --- src/lib/components/chat/Messages.svelte | 132 +++++++++++------------- src/routes/(app)/+page.svelte | 4 +- src/routes/(app)/c/[id]/+page.svelte | 4 +- 3 files changed, 64 insertions(+), 76 deletions(-) diff --git a/src/lib/components/chat/Messages.svelte b/src/lib/components/chat/Messages.svelte index fd0a6e83..77e6cad0 100644 --- a/src/lib/components/chat/Messages.svelte +++ b/src/lib/components/chat/Messages.svelte @@ -223,32 +223,22 @@ }, 100); }; - // TODO: change delete behaviour - // const deleteMessageAndDescendants = async (messageId: string) => { - // if (history.messages[messageId]) { - // history.messages[messageId].deleted = true; - - // for (const childId of history.messages[messageId].childrenIds) { - // await deleteMessageAndDescendants(childId); - // } - // } - // }; - - // const triggerDeleteMessageRecursive = async (messageId: string) => { - // await deleteMessageAndDescendants(messageId); - // await updateChatById(localStorage.token, chatId, { history }); - // await chats.set(await getChatList(localStorage.token)); - // }; - const messageDeleteHandler = async (messageId) => { - if (history.messages[messageId]) { - history.messages[messageId].deleted = true; + const messageParentId = history.messages[messageId]?.parentId; - for (const childId of history.messages[messageId].childrenIds) { - history.messages[childId].deleted = true; - } + if (messageParentId !== null) { + history.messages[messageParentId].childrenIds = [] } - await updateChatById(localStorage.token, chatId, { history }); + + delete history.messages[messageId]; + history.currentId = messageParentId; + + await tick(); + + await updateChatById(localStorage.token, chatId, { + messages: messages, + history: history + }); }; @@ -258,57 +248,55 @@
{#key chatId} {#each messages as message, messageIdx} - {#if !message.deleted} -
-
- {#if message.role === 'user'} - messageDeleteHandler(message.id)} - user={$user} - {message} - isFirstMessage={messageIdx === 0} - siblings={message.parentId !== null - ? history.messages[message.parentId]?.childrenIds ?? [] - : Object.values(history.messages) - .filter((message) => message.parentId === null) - .map((message) => message.id) ?? []} - {confirmEditMessage} - {showPreviousMessage} - {showNextMessage} - {copyToClipboard} - /> - {:else} - { - console.log('save', e); +
+
+ {#if message.role === 'user'} + messageDeleteHandler(message.id)} + user={$user} + {message} + isFirstMessage={messageIdx === 0} + siblings={message.parentId !== null + ? history.messages[message.parentId]?.childrenIds ?? [] + : Object.values(history.messages) + .filter((message) => message.parentId === null) + .map((message) => message.id) ?? []} + {confirmEditMessage} + {showPreviousMessage} + {showNextMessage} + {copyToClipboard} + /> + {:else} + { + console.log('save', e); - const message = e.detail; - history.messages[message.id] = message; - await updateChatById(localStorage.token, chatId, { - messages: messages, - history: history - }); - }} - /> - {/if} -
+ const message = e.detail; + history.messages[message.id] = message; + await updateChatById(localStorage.token, chatId, { + messages: messages, + history: history + }); + }} + /> + {/if}
- {/if} +
{/each} {#if bottomPadding} diff --git a/src/routes/(app)/+page.svelte b/src/routes/(app)/+page.svelte index 04dc922c..da3aff23 100644 --- a/src/routes/(app)/+page.svelte +++ b/src/routes/(app)/+page.svelte @@ -342,7 +342,7 @@ content: $settings.system } : undefined, - ...messages.filter((message) => !message.deleted) + ...messages ] .filter((message) => message) .map((message, idx, arr) => ({ @@ -550,7 +550,7 @@ content: $settings.system } : undefined, - ...messages.filter((message) => !message.deleted) + ...messages ] .filter((message) => message) .map((message, idx, arr) => ({ diff --git a/src/routes/(app)/c/[id]/+page.svelte b/src/routes/(app)/c/[id]/+page.svelte index 69ebfde5..85638c40 100644 --- a/src/routes/(app)/c/[id]/+page.svelte +++ b/src/routes/(app)/c/[id]/+page.svelte @@ -355,7 +355,7 @@ content: $settings.system } : undefined, - ...messages.filter((message) => !message.deleted) + ...messages ] .filter((message) => message) .map((message, idx, arr) => ({ @@ -563,7 +563,7 @@ content: $settings.system } : undefined, - ...messages.filter((message) => !message.deleted) + ...messages ] .filter((message) => message) .map((message, idx, arr) => ({ From 225038738294f35228003ba497d3378c0aaa3a8e Mon Sep 17 00:00:00 2001 From: Danny Liu Date: Sat, 2 Mar 2024 23:31:03 -0800 Subject: [PATCH 2/6] retain the message in history object --- src/lib/components/chat/Messages.svelte | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/components/chat/Messages.svelte b/src/lib/components/chat/Messages.svelte index 77e6cad0..5711fb17 100644 --- a/src/lib/components/chat/Messages.svelte +++ b/src/lib/components/chat/Messages.svelte @@ -230,7 +230,6 @@ history.messages[messageParentId].childrenIds = [] } - delete history.messages[messageId]; history.currentId = messageParentId; await tick(); From 8c011974834dcb2f5b4d72bc6e22843e647bc3b5 Mon Sep 17 00:00:00 2001 From: Danny Liu Date: Mon, 4 Mar 2024 12:34:14 -0800 Subject: [PATCH 3/6] 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, From 03907f9a8adde951a855e665bc61e6fbc265a42a Mon Sep 17 00:00:00 2001 From: Danny Liu Date: Mon, 4 Mar 2024 21:23:41 -0800 Subject: [PATCH 4/6] check if deleted message response has sibling responses for multi model chats --- src/lib/components/chat/Messages.svelte | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib/components/chat/Messages.svelte b/src/lib/components/chat/Messages.svelte index 3f1ee28b..2ce107bb 100644 --- a/src/lib/components/chat/Messages.svelte +++ b/src/lib/components/chat/Messages.svelte @@ -228,10 +228,12 @@ const messageParentId = messageToDelete.parentId; const messageChildrenIds = messageToDelete.childrenIds ?? []; + const hasSibling = messageChildrenIds.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) { // if last prompt/response pair + if (child.childrenIds.length === 0 && !hasSibling) { // if last prompt/response pair history.messages[messageParentId].childrenIds = [] history.currentId = messageParentId; } From a9d74b669e71d5db989f8766f9535620f20238c5 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Wed, 6 Mar 2024 00:33:47 -0800 Subject: [PATCH 5/6] refac --- src/lib/components/chat/Messages.svelte | 48 ++++++++----------------- 1 file changed, 15 insertions(+), 33 deletions(-) diff --git a/src/lib/components/chat/Messages.svelte b/src/lib/components/chat/Messages.svelte index 2ce107bb..481c8967 100644 --- a/src/lib/components/chat/Messages.svelte +++ b/src/lib/components/chat/Messages.svelte @@ -224,42 +224,24 @@ }; const messageDeleteHandler = async (messageId) => { - const messageToDelete = history.messages[messageId]; - const messageParentId = messageToDelete.parentId; - const messageChildrenIds = messageToDelete.childrenIds ?? []; + const message = history.messages[messageId]; + const parentId = message.parentId; + const childrenIds = message.childrenIds ?? []; + const grandchildrenIds = []; - const hasSibling = messageChildrenIds.some(childId => history.messages[childId]?.childrenIds?.length > 0); + // Iterate through childrenIds to find grandchildrenIds + for (const childId of childrenIds) { + const childMessage = history.messages[childId]; + const grandChildrenIds = childMessage.childrenIds ?? []; + grandchildrenIds.push(...grandChildrenIds); + } - 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; - } - else { - child.childrenIds.forEach((grandChildId) => { - if (history.messages[grandChildId]) { - history.messages[grandChildId].parentId = messageParentId; - history.messages[messageParentId].childrenIds.push(grandChildId); - } - }); - } - } + history.messages[parentId].childrenIds.push(...grandchildrenIds); + history.messages[parentId].childrenIds = history.messages[parentId].childrenIds.filter( + (id) => id !== messageId + ); - // remove response - history.messages[messageParentId].childrenIds = history.messages[messageParentId].childrenIds - .filter((id) => id !== childId); - }); - - // remove prompt - history.messages[messageParentId].childrenIds = history.messages[messageParentId].childrenIds - .filter((id) => id !== messageId); - - await updateChatById(localStorage.token, chatId, { - messages: messages, - history: history - }); + await updateChatById(localStorage.token, chatId, { messages, history }); }; From 2b016adc6e27913108a3de5591c9c054f1fb61c4 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Wed, 6 Mar 2024 00:55:51 -0800 Subject: [PATCH 6/6] revert --- src/lib/components/chat/Messages.svelte | 88 ++++++++++++++++++++----- 1 file changed, 71 insertions(+), 17 deletions(-) diff --git a/src/lib/components/chat/Messages.svelte b/src/lib/components/chat/Messages.svelte index 481c8967..339d8860 100644 --- a/src/lib/components/chat/Messages.svelte +++ b/src/lib/components/chat/Messages.svelte @@ -224,25 +224,79 @@ }; const messageDeleteHandler = async (messageId) => { - const message = history.messages[messageId]; - const parentId = message.parentId; - const childrenIds = message.childrenIds ?? []; - const grandchildrenIds = []; - - // Iterate through childrenIds to find grandchildrenIds - for (const childId of childrenIds) { - const childMessage = history.messages[childId]; - const grandChildrenIds = childMessage.childrenIds ?? []; - grandchildrenIds.push(...grandChildrenIds); - } - - history.messages[parentId].childrenIds.push(...grandchildrenIds); - history.messages[parentId].childrenIds = history.messages[parentId].childrenIds.filter( - (id) => id !== messageId + const messageToDelete = history.messages[messageId]; + const messageParentId = messageToDelete.parentId; + const messageChildrenIds = messageToDelete.childrenIds ?? []; + const hasSibling = messageChildrenIds.some( + (childId) => history.messages[childId]?.childrenIds?.length > 0 ); - - await updateChatById(localStorage.token, chatId, { messages, history }); + 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; + } else { + child.childrenIds.forEach((grandChildId) => { + if (history.messages[grandChildId]) { + history.messages[grandChildId].parentId = messageParentId; + history.messages[messageParentId].childrenIds.push(grandChildId); + } + }); + } + } + // remove response + history.messages[messageParentId].childrenIds = history.messages[ + messageParentId + ].childrenIds.filter((id) => id !== childId); + }); + // remove prompt + history.messages[messageParentId].childrenIds = history.messages[ + messageParentId + ].childrenIds.filter((id) => id !== messageId); + await updateChatById(localStorage.token, chatId, { + messages: messages, + history: history + }); }; + + // const messageDeleteHandler = async (messageId) => { + // const message = history.messages[messageId]; + // const parentId = message.parentId; + // const childrenIds = message.childrenIds ?? []; + // const grandchildrenIds = []; + + // // Iterate through childrenIds to find grandchildrenIds + // for (const childId of childrenIds) { + // const childMessage = history.messages[childId]; + // const grandChildrenIds = childMessage.childrenIds ?? []; + + // for (const grandchildId of grandchildrenIds) { + // const childMessage = history.messages[grandchildId]; + // childMessage.parentId = parentId; + // } + // grandchildrenIds.push(...grandChildrenIds); + // } + + // history.messages[parentId].childrenIds.push(...grandchildrenIds); + // history.messages[parentId].childrenIds = history.messages[parentId].childrenIds.filter( + // (id) => id !== messageId + // ); + + // // Select latest message + // let currentMessageId = grandchildrenIds.at(-1); + // if (currentMessageId) { + // let messageChildrenIds = history.messages[currentMessageId].childrenIds; + // while (messageChildrenIds.length !== 0) { + // currentMessageId = messageChildrenIds.at(-1); + // messageChildrenIds = history.messages[currentMessageId].childrenIds; + // } + // history.currentId = currentMessageId; + // } + + // await updateChatById(localStorage.token, chatId, { messages, history }); + // }; {#if messages.length == 0}