|
| 1 | +<template> |
| 2 | + <ScrollPanel |
| 3 | + ref="scrollPanelRef" |
| 4 | + class="w-full min-h-[400px] rounded-lg px-2 py-2 text-xs" |
| 5 | + :pt="{ content: { id: 'chat-scroll-content' } }" |
| 6 | + > |
| 7 | + <div v-for="(item, i) in parsedHistory" :key="i" class="mb-4"> |
| 8 | + <!-- Prompt (user, right) --> |
| 9 | + <span |
| 10 | + :class="{ |
| 11 | + 'opacity-40 pointer-events-none': editIndex !== null && i > editIndex |
| 12 | + }" |
| 13 | + > |
| 14 | + <div class="flex justify-end mb-1"> |
| 15 | + <div |
| 16 | + class="bg-gray-300 dark-theme:bg-gray-800 rounded-xl px-4 py-1 max-w-[80%] text-right" |
| 17 | + > |
| 18 | + <div class="break-words text-[12px]">{{ item.prompt }}</div> |
| 19 | + </div> |
| 20 | + </div> |
| 21 | + <div class="flex justify-end mb-2 mr-1"> |
| 22 | + <CopyButton :text="item.prompt" /> |
| 23 | + <Button |
| 24 | + v-tooltip=" |
| 25 | + editIndex === i ? $t('chatHistory.cancelEditTooltip') : null |
| 26 | + " |
| 27 | + text |
| 28 | + rounded |
| 29 | + class="!p-1 !h-4 !w-4 text-gray-400 hover:text-gray-600 dark-theme:hover:text-gray-200 transition" |
| 30 | + pt:icon:class="!text-xs" |
| 31 | + :icon="editIndex === i ? 'pi pi-times' : 'pi pi-pencil'" |
| 32 | + :aria-label=" |
| 33 | + editIndex === i ? $t('chatHistory.cancelEdit') : $t('g.edit') |
| 34 | + " |
| 35 | + @click="editIndex === i ? handleCancelEdit() : handleEdit(i)" |
| 36 | + /> |
| 37 | + </div> |
| 38 | + </span> |
| 39 | + <!-- Response (LLM, left) --> |
| 40 | + <ResponseBlurb |
| 41 | + :text="item.response" |
| 42 | + :class="{ |
| 43 | + 'opacity-25 pointer-events-none': editIndex !== null && i >= editIndex |
| 44 | + }" |
| 45 | + > |
| 46 | + <div v-html="nl2br(linkifyHtml(item.response))" /> |
| 47 | + </ResponseBlurb> |
| 48 | + </div> |
| 49 | + </ScrollPanel> |
| 50 | +</template> |
| 51 | + |
| 52 | +<script setup lang="ts"> |
| 53 | +import Button from 'primevue/button' |
| 54 | +import ScrollPanel from 'primevue/scrollpanel' |
| 55 | +import { computed, nextTick, ref, watch } from 'vue' |
| 56 | +
|
| 57 | +import CopyButton from '@/components/graph/widgets/chatHistory/CopyButton.vue' |
| 58 | +import ResponseBlurb from '@/components/graph/widgets/chatHistory/ResponseBlurb.vue' |
| 59 | +import { ComponentWidget } from '@/scripts/domWidget' |
| 60 | +import { linkifyHtml, nl2br } from '@/utils/formatUtil' |
| 61 | +
|
| 62 | +const { widget, history = '[]' } = defineProps<{ |
| 63 | + widget?: ComponentWidget<string> |
| 64 | + history: string |
| 65 | +}>() |
| 66 | +
|
| 67 | +const editIndex = ref<number | null>(null) |
| 68 | +const scrollPanelRef = ref<InstanceType<typeof ScrollPanel> | null>(null) |
| 69 | +
|
| 70 | +const parsedHistory = computed(() => JSON.parse(history || '[]')) |
| 71 | +
|
| 72 | +const findPromptInput = () => |
| 73 | + widget?.node.widgets?.find((w) => w.name === 'prompt') |
| 74 | +let promptInput = findPromptInput() |
| 75 | +const previousPromptInput = ref<string | null>(null) |
| 76 | +
|
| 77 | +const getPreviousResponseId = (index: number) => |
| 78 | + index > 0 ? parsedHistory.value[index - 1]?.response_id ?? '' : '' |
| 79 | +
|
| 80 | +const storePromptInput = () => { |
| 81 | + promptInput ??= widget?.node.widgets?.find((w) => w.name === 'prompt') |
| 82 | + if (!promptInput) return |
| 83 | +
|
| 84 | + previousPromptInput.value = String(promptInput.value) |
| 85 | +} |
| 86 | +
|
| 87 | +const setPromptInput = (text: string, previousResponseId?: string | null) => { |
| 88 | + promptInput ??= widget?.node.widgets?.find((w) => w.name === 'prompt') |
| 89 | + if (!promptInput) return |
| 90 | +
|
| 91 | + if (previousResponseId !== null) { |
| 92 | + promptInput.value = `<starting_point_id:${previousResponseId}>\n\n${text}` |
| 93 | + } else { |
| 94 | + promptInput.value = text |
| 95 | + } |
| 96 | +} |
| 97 | +
|
| 98 | +const handleEdit = (index: number) => { |
| 99 | + if (!promptInput) return |
| 100 | +
|
| 101 | + editIndex.value = index |
| 102 | + const prevResponseId = index === 0 ? 'start' : getPreviousResponseId(index) |
| 103 | + const promptText = parsedHistory.value[index]?.prompt ?? '' |
| 104 | +
|
| 105 | + storePromptInput() |
| 106 | + setPromptInput(promptText, prevResponseId) |
| 107 | +} |
| 108 | +
|
| 109 | +const resetEditingState = () => { |
| 110 | + editIndex.value = null |
| 111 | +} |
| 112 | +const handleCancelEdit = () => { |
| 113 | + resetEditingState() |
| 114 | + if (promptInput) { |
| 115 | + promptInput.value = previousPromptInput.value ?? '' |
| 116 | + } |
| 117 | +} |
| 118 | +
|
| 119 | +const scrollChatToBottom = () => { |
| 120 | + const content = document.getElementById('chat-scroll-content') |
| 121 | + if (content) { |
| 122 | + content.scrollTo({ top: content.scrollHeight, behavior: 'smooth' }) |
| 123 | + } |
| 124 | +} |
| 125 | +
|
| 126 | +const onHistoryChanged = () => { |
| 127 | + resetEditingState() |
| 128 | + void nextTick(() => scrollChatToBottom()) |
| 129 | +} |
| 130 | +
|
| 131 | +watch(() => parsedHistory.value, onHistoryChanged, { |
| 132 | + immediate: true, |
| 133 | + deep: true |
| 134 | +}) |
| 135 | +</script> |
0 commit comments