From 052f5299a0a7ae4f7fbe574600610f5d394b02fb Mon Sep 17 00:00:00 2001 From: Rafi Date: Thu, 9 Mar 2023 17:39:45 +0800 Subject: [PATCH] Add frequently used prompt function. --- components/MsgContent.vue | 1 - components/MsgEditor.vue | 8 +- components/Prompt.vue | 224 ++++++++++++++++++++++++++++++++++++++ pages/index.vue | 9 +- 4 files changed, 237 insertions(+), 5 deletions(-) create mode 100644 components/Prompt.vue diff --git a/components/MsgContent.vue b/components/MsgContent.vue index ec0036e..4a5eaf8 100644 --- a/components/MsgContent.vue +++ b/components/MsgContent.vue @@ -65,7 +65,6 @@ const highlightCode = () => { } watchEffect(() => { - console.log('content changed', props.content) contentHtml.value = props.content ? marked(props.content) : '' if (props.content && props.content.endsWith('```')) { nextTick(() => { diff --git a/components/MsgEditor.vue b/components/MsgEditor.vue index 3648c79..ef3decf 100644 --- a/components/MsgEditor.vue +++ b/components/MsgEditor.vue @@ -2,13 +2,12 @@ +const menu = ref(false) +const prompts = ref([]) +const editingPrompt = ref(null) +const newPrompt = ref('') +const submittingNewPrompt = ref(false) +const promptInputErrorMessage = ref('') +const loadingPrompts = ref(false) +const deletingPromptIndex = ref(null) + +const props = defineProps({ + usePrompt: { + type: Function, + required: true + } +}) + +const addPrompt = async () => { + if (!newPrompt.value) { + promptInputErrorMessage.value = 'Please enter a prompt' + return + } + submittingNewPrompt.value = true + const { data, error } = await useAuthFetch('/api/chat/prompts/', { + method: 'POST', + body: JSON.stringify({ + prompt: newPrompt.value + }) + }) + if (!error.value) { + prompts.value.push(data.value) + newPrompt.value = '' + } + submittingNewPrompt.value = false +} + +const editPrompt = (index) => { + editingPrompt.value = Object.assign({}, prompts.value[index]) +} + +const updatePrompt = async (index) => { + editingPrompt.value.updating = true + const { data, error } = await useAuthFetch(`/api/chat/prompts/${editingPrompt.value.id}/`, { + method: 'PUT', + body: JSON.stringify({ + prompt: editingPrompt.value.prompt + }) + }) + if (!error.value) { + prompts.value[index] = editingPrompt.value + } + editingPrompt.value.updating = false + editingPrompt.value = null +} + +const cancelEditPrompt = () => { + editingPrompt.value = null +} + +const deletePrompt = async (index) => { + deletingPromptIndex.value = index + const { data, error } = await useAuthFetch(`/api/chat/prompts/${prompts.value[index].id}/`, { + method: 'DELETE' + }) + deletingPromptIndex.value = null + if (!error.value) { + prompts.value.splice(index, 1) + } +} + +const loadPrompts = async () => { + loadingPrompts.value = true + const { data, error } = await useAuthFetch('/api/chat/prompts/') + if (!error.value) { + prompts.value = data.value + } + loadingPrompts.value = false +} + +const selectPrompt = (prompt) => { + props.usePrompt(prompt.prompt) + menu.value = false +} + +onMounted( () => { + loadPrompts() +}) + + + + + \ No newline at end of file diff --git a/pages/index.vue b/pages/index.vue index baf1867..3ad3398 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -1,4 +1,6 @@ @@ -188,6 +194,7 @@ const showSnackbar = (text) => {
+ { class="mr-3" @click="stop" > - +