From 572cd22b4d2131d4cce8fcc38961f27c86efc967 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Thu, 2 May 2024 00:45:04 -0700 Subject: [PATCH] feat: prompt variables from suggestions --- backend/data/config.json | 4 +++ src/lib/components/chat/Messages.svelte | 36 ++++++++++++++++++++----- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/backend/data/config.json b/backend/data/config.json index 428fc6df8..6c0ad2b9f 100644 --- a/backend/data/config.json +++ b/backend/data/config.json @@ -26,6 +26,10 @@ { "title": ["Overcome procrastination", "give me tips"], "content": "Could you start by asking me about instances when I procrastinate the most and then give me some suggestions to overcome it?" + }, + { + "title": ["Grammar check", "rewrite it for better readability "], + "content": "Check the following sentence for grammar and clarity: \"[sentence]\". Rewrite it for better readability while maintaining its original meaning." } ] } diff --git a/src/lib/components/chat/Messages.svelte b/src/lib/components/chat/Messages.svelte index 4e8c56dcc..3ef99594e 100644 --- a/src/lib/components/chat/Messages.svelte +++ b/src/lib/components/chat/Messages.svelte @@ -12,7 +12,7 @@ import Placeholder from './Messages/Placeholder.svelte'; import Spinner from '../common/Spinner.svelte'; import { imageGenerations } from '$lib/apis/images'; - import { copyToClipboard } from '$lib/utils'; + import { copyToClipboard, findWordIndices } from '$lib/utils'; const i18n = getContext('i18n'); @@ -247,16 +247,38 @@ modelfiles={selectedModelfiles} {suggestionPrompts} submitPrompt={async (p) => { - const chatTextAreaElement = document.getElementById('chat-textarea'); - if (chatTextAreaElement) { + let text = p; + + if (p.includes('{{CLIPBOARD}}')) { + const clipboardText = await navigator.clipboard.readText().catch((err) => { + toast.error($i18n.t('Failed to read clipboard contents')); + return '{{CLIPBOARD}}'; + }); + + text = p.replaceAll('{{CLIPBOARD}}', clipboardText); + } + + prompt = text; + + await tick(); + + const chatInputElement = document.getElementById('chat-textarea'); + if (chatInputElement) { prompt = p; - await tick(); + chatInputElement.style.height = ''; + chatInputElement.style.height = Math.min(chatInputElement.scrollHeight, 200) + 'px'; + chatInputElement.focus(); - chatTextAreaElement.style.height = ''; - chatTextAreaElement.style.height = Math.min(chatTextAreaElement.scrollHeight, 200) + 'px'; - chatTextAreaElement.focus(); + const words = findWordIndices(prompt); + + if (words.length > 0) { + const word = words.at(0); + chatInputElement.setSelectionRange(word?.startIndex, word.endIndex + 1); + } } + + await tick(); }} /> {:else}