From 591962d906443b9cb43f768c25070dcfe8b98e31 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Fri, 23 Aug 2024 14:31:39 +0200 Subject: [PATCH] refac: input commands --- src/lib/components/chat/MessageInput.svelte | 160 ++++------------ .../chat/MessageInput/Commands.svelte | 131 +++++++++++++ .../{ => Commands}/Documents.svelte | 16 +- .../chat/MessageInput/Commands/Models.svelte | 90 +++++++++ .../Prompts.svelte} | 43 +++-- .../chat/MessageInput/Models.svelte | 181 ------------------ src/lib/utils/index.ts | 14 ++ 7 files changed, 304 insertions(+), 331 deletions(-) create mode 100644 src/lib/components/chat/MessageInput/Commands.svelte rename src/lib/components/chat/MessageInput/{ => Commands}/Documents.svelte (93%) create mode 100644 src/lib/components/chat/MessageInput/Commands/Models.svelte rename src/lib/components/chat/MessageInput/{PromptCommands.svelte => Commands/Prompts.svelte} (80%) delete mode 100644 src/lib/components/chat/MessageInput/Models.svelte diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index 91b51acb8..a71578513 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -1,6 +1,7 @@ + +{#if ['/', '#', '@'].includes(command?.charAt(0))} + {#if command?.charAt(0) === '/'} + + {:else if command?.charAt(0) === '#'} + { + console.log(e); + uploadYoutubeTranscription(e.detail); + }} + on:url={(e) => { + console.log(e); + uploadWeb(e.detail); + }} + on:select={(e) => { + console.log(e); + files = [ + ...files, + { + type: e?.detail?.type ?? 'file', + ...e.detail, + status: 'processed' + } + ]; + + dispatch('select'); + }} + /> + {:else if command?.charAt(0) === '@'} + { + prompt = removeLastWordFromString(prompt, command); + + dispatch('select', { + type: 'model', + data: e.detail + }); + }} + /> + {/if} +{/if} diff --git a/src/lib/components/chat/MessageInput/Documents.svelte b/src/lib/components/chat/MessageInput/Commands/Documents.svelte similarity index 93% rename from src/lib/components/chat/MessageInput/Documents.svelte rename to src/lib/components/chat/MessageInput/Commands/Documents.svelte index 50956e4c0..04275be4e 100644 --- a/src/lib/components/chat/MessageInput/Documents.svelte +++ b/src/lib/components/chat/MessageInput/Commands/Documents.svelte @@ -9,6 +9,7 @@ const i18n = getContext('i18n'); export let prompt = ''; + export let command = ''; const dispatch = createEventDispatcher(); let selectedIdx = 0; @@ -43,16 +44,16 @@ ]; $: filteredCollections = collections - .filter((collection) => findByName(collection, prompt)) + .filter((collection) => findByName(collection, command)) .sort((a, b) => a.name.localeCompare(b.name)); $: filteredDocs = $documents - .filter((doc) => findByName(doc, prompt)) + .filter((doc) => findByName(doc, command)) .sort((a, b) => a.title.localeCompare(b.title)); $: filteredItems = [...filteredCollections, ...filteredDocs]; - $: if (prompt) { + $: if (command) { selectedIdx = 0; console.log(filteredCollections); @@ -62,9 +63,9 @@ name: string; }; - const findByName = (obj: ObjectWithName, prompt: string) => { + const findByName = (obj: ObjectWithName, command: string) => { const name = obj.name.toLowerCase(); - return name.includes(prompt.toLowerCase().split(' ')?.at(0)?.substring(1) ?? ''); + return name.includes(command.toLowerCase().split(' ')?.at(0)?.substring(1) ?? ''); }; export const selectUp = () => { @@ -110,7 +111,10 @@ {#if filteredItems.length > 0 || prompt.split(' ')?.at(0)?.substring(1).startsWith('http')} -
+
#
diff --git a/src/lib/components/chat/MessageInput/Commands/Models.svelte b/src/lib/components/chat/MessageInput/Commands/Models.svelte new file mode 100644 index 000000000..4b660a62c --- /dev/null +++ b/src/lib/components/chat/MessageInput/Commands/Models.svelte @@ -0,0 +1,90 @@ + + +{#if filteredModels.length > 0} +
+
+
+
@
+
+ +
+
+ {#each filteredModels as model, modelIdx} + + {/each} +
+
+
+
+{/if} diff --git a/src/lib/components/chat/MessageInput/PromptCommands.svelte b/src/lib/components/chat/MessageInput/Commands/Prompts.svelte similarity index 80% rename from src/lib/components/chat/MessageInput/PromptCommands.svelte rename to src/lib/components/chat/MessageInput/Commands/Prompts.svelte index 4dd8d3302..9fd48c749 100644 --- a/src/lib/components/chat/MessageInput/PromptCommands.svelte +++ b/src/lib/components/chat/MessageInput/Commands/Prompts.svelte @@ -7,27 +7,30 @@ const i18n = getContext('i18n'); export let files; - export let prompt = ''; - let selectedCommandIdx = 0; - let filteredPromptCommands = []; - $: filteredPromptCommands = $prompts - .filter((p) => p.command.toLowerCase().includes(prompt.toLowerCase())) + export let prompt = ''; + export let command = ''; + + let selectedPromptIdx = 0; + let filteredPrompts = []; + + $: filteredPrompts = $prompts + .filter((p) => p.command.toLowerCase().includes(command.toLowerCase())) .sort((a, b) => a.title.localeCompare(b.title)); - $: if (prompt) { - selectedCommandIdx = 0; + $: if (command) { + selectedPromptIdx = 0; } export const selectUp = () => { - selectedCommandIdx = Math.max(0, selectedCommandIdx - 1); + selectedPromptIdx = Math.max(0, selectedPromptIdx - 1); }; export const selectDown = () => { - selectedCommandIdx = Math.min(selectedCommandIdx + 1, filteredPromptCommands.length - 1); + selectedPromptIdx = Math.min(selectedPromptIdx + 1, filteredPrompts.length - 1); }; - const confirmCommand = async (command) => { + const confirmPrompt = async (command) => { let text = command.content; if (command.content.includes('{{CLIPBOARD}}')) { @@ -79,7 +82,6 @@ await tick(); const words = findWordIndices(prompt); - if (words.length > 0) { const word = words.at(0); chatInputElement.setSelectionRange(word?.startIndex, word.endIndex + 1); @@ -87,8 +89,11 @@ }; -{#if filteredPromptCommands.length > 0} -
+{#if filteredPrompts.length > 0} +
/
@@ -98,26 +103,26 @@ class="max-h-60 flex flex-col w-full rounded-r-lg bg-white dark:bg-gray-900 dark:text-gray-100" >
- {#each filteredPromptCommands as command, commandIdx} + {#each filteredPrompts as prompt, promptIdx} {/each} diff --git a/src/lib/components/chat/MessageInput/Models.svelte b/src/lib/components/chat/MessageInput/Models.svelte deleted file mode 100644 index c4655991f..000000000 --- a/src/lib/components/chat/MessageInput/Models.svelte +++ /dev/null @@ -1,181 +0,0 @@ - - -{#if prompt.charAt(0) === '@'} - {#if filteredModels.length > 0} -
-
-
-
@
-
- -
-
- {#each filteredModels as model, modelIdx} - - {/each} -
-
-
-
- {/if} -{/if} diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index f3532773c..56922f03f 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -288,6 +288,20 @@ export const findWordIndices = (text) => { return matches; }; +export const removeLastWordFromString = (inputString, wordString) => { + // Split the string into an array of words + const words = inputString.split(' '); + + if (words.at(-1) === wordString) { + words.pop(); + } + + // Join the remaining words back into a string + const resultString = words.join(' '); + + return resultString; +}; + export const removeFirstHashWord = (inputString) => { // Split the string into an array of words const words = inputString.split(' ');