From a75b68c19c6d629d08bcbbe617a22a821fa5718f Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Mon, 10 Jun 2024 11:40:58 -0700 Subject: [PATCH] refac: message status history --- src/lib/components/chat/Chat.svelte | 41 +++++++++++-------- .../chat/Messages/ResponseMessage.svelte | 15 ++++--- .../ResponseMessage/WebSearchResults.svelte | 41 +++++++++++++++++-- .../components/icons/MagnifyingGlass.svelte | 19 +++++++++ 4 files changed, 89 insertions(+), 27 deletions(-) create mode 100644 src/lib/components/icons/MagnifyingGlass.svelte diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 861b985a5..47e1075df 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -515,11 +515,13 @@ const getWebSearchResults = async (model: string, parentId: string, responseId: string) => { const responseMessage = history.messages[responseId]; - responseMessage.status = { - done: false, - action: 'web_search', - description: $i18n.t('Generating search query') - }; + responseMessage.statusHistory = [ + { + done: false, + action: 'web_search', + description: $i18n.t('Generating search query') + } + ]; messages = messages; const prompt = history.messages[parentId].content; @@ -532,19 +534,21 @@ if (!searchQuery) { toast.warning($i18n.t('No search query generated')); - responseMessage.status = { - ...responseMessage.status, + responseMessage.statusHistory.push({ done: true, error: true, + action: 'web_search', description: 'No search query generated' - }; + }); + messages = messages; } - responseMessage.status = { - ...responseMessage.status, - description: $i18n.t("Searching the web for '{{searchQuery}}'", { searchQuery }) - }; + responseMessage.statusHistory.push({ + done: false, + action: 'web_search', + description: $i18n.t(`Searching "{{searchQuery}}"`, { searchQuery }) + }); messages = messages; const results = await runWebSearch(localStorage.token, searchQuery).catch((error) => { @@ -555,12 +559,13 @@ }); if (results) { - responseMessage.status = { - ...responseMessage.status, + responseMessage.statusHistory.push({ done: true, + action: 'web_search', description: $i18n.t('Searched {{count}} sites', { count: results.filenames.length }), + query: searchQuery, urls: results.filenames - }; + }); if (responseMessage?.files ?? undefined === undefined) { responseMessage.files = []; @@ -575,12 +580,12 @@ messages = messages; } else { - responseMessage.status = { - ...responseMessage.status, + responseMessage.statusHistory.push({ done: true, error: true, + action: 'web_search', description: 'No search results found' - }; + }); messages = messages; } }; diff --git a/src/lib/components/chat/Messages/ResponseMessage.svelte b/src/lib/components/chat/Messages/ResponseMessage.svelte index 7933c0215..281f7671f 100644 --- a/src/lib/components/chat/Messages/ResponseMessage.svelte +++ b/src/lib/components/chat/Messages/ResponseMessage.svelte @@ -420,26 +420,29 @@ class="prose chat-{message.role} w-full max-w-full dark:prose-invert prose-headings:my-0 prose-headings:-mb-4 prose-p:m-0 prose-p:-mb-6 prose-pre:my-0 prose-table:my-0 prose-blockquote:my-0 prose-img:my-0 prose-ul:-my-4 prose-ol:-my-4 prose-li:-my-3 prose-ul:-mb-6 prose-ol:-mb-8 prose-ol:p-0 prose-li:-mb-4 whitespace-pre-line" >
- {#if message?.status} + {#if (message?.statusHistory ?? [...(message?.status ? [message?.status] : [])]).length > 0} + {@const status = ( + message?.statusHistory ?? [...(message?.status ? [message?.status] : [])] + ).at(-1)}
- {#if message?.status?.done === false} + {#if status.done === false}
{/if} - {#if message?.status?.action === 'web_search' && message?.status?.urls} - + {#if status?.action === 'web_search' && status?.urls} +
- {message.status.description} + {status?.description}
{:else}
- {message.status.description} + {status?.description}
{/if} diff --git a/src/lib/components/chat/Messages/ResponseMessage/WebSearchResults.svelte b/src/lib/components/chat/Messages/ResponseMessage/WebSearchResults.svelte index dc233c6d4..528108036 100644 --- a/src/lib/components/chat/Messages/ResponseMessage/WebSearchResults.svelte +++ b/src/lib/components/chat/Messages/ResponseMessage/WebSearchResults.svelte @@ -1,10 +1,11 @@ @@ -27,11 +28,45 @@ class=" text-sm border border-gray-300/30 dark:border-gray-700/50 rounded-xl" transition={slide} > - {#each urls as url, urlIdx} + {#if status?.query} + +
+ + +
+ {status.query} +
+
+ +
+ + + + +
+
+ {/if} + + {#each status.urls as url, urlIdx} diff --git a/src/lib/components/icons/MagnifyingGlass.svelte b/src/lib/components/icons/MagnifyingGlass.svelte new file mode 100644 index 000000000..a61186b5d --- /dev/null +++ b/src/lib/components/icons/MagnifyingGlass.svelte @@ -0,0 +1,19 @@ + + + + +