From 740974c53e655366b5654524272197d6f430e79c Mon Sep 17 00:00:00 2001 From: wizardchen Date: Thu, 4 Dec 2025 00:49:20 +0800 Subject: [PATCH] feat: Add completion status tracking and toolbar for copying and saving responses in chat component --- frontend/src/views/chat/components/botmsg.vue | 223 +++++++++++++++--- frontend/src/views/chat/index.vue | 8 +- 2 files changed, 198 insertions(+), 33 deletions(-) diff --git a/frontend/src/views/chat/components/botmsg.vue b/frontend/src/views/chat/components/botmsg.vue index e1423498..2392fcce 100644 --- a/frontend/src/views/chat/components/botmsg.vue +++ b/frontend/src/views/chat/components/botmsg.vue @@ -19,6 +19,15 @@
+ +
+ + + + + + +
{{ $t('common.loading') }}
@@ -33,6 +42,8 @@ import AgentStreamDisplay from './AgentStreamDisplay.vue'; import picturePreview from '@/components/picture-preview.vue'; import { sanitizeHTML, safeMarkdownToHTML, createSafeImage, isValidImageURL } from '@/utils/security'; import { useI18n } from 'vue-i18n'; +import { MessagePlugin } from 'tdesign-vue-next'; +import { useUIStore } from '@/stores/ui'; marked.use({ mangle: false, @@ -41,6 +52,7 @@ marked.use({ }); const emit = defineEmits(['scroll-bottom']) const { t } = useI18n() +const uiStore = useUIStore(); const renderer = new marked.Renderer(); let parentMd = ref() let reviewUrl = ref('') @@ -196,6 +208,80 @@ const myMarkdown = (res) => { return marked.parse(res, { renderer }) } +// 获取实际内容 +const getActualContent = () => { + return (props.content || props.session?.content || '').trim(); +}; + +// 格式化标题 +const formatManualTitle = (question) => { + if (!question) { + return '会话摘录'; + } + const condensed = question.replace(/\s+/g, ' ').trim(); + if (!condensed) { + return '会话摘录'; + } + return condensed.length > 40 ? `${condensed.slice(0, 40)}...` : condensed; +}; + +// 构建手动添加的 Markdown 内容 +const buildManualMarkdown = (question, answer) => { + const safeAnswer = answer?.trim() || '(无回答内容)'; + return `${safeAnswer}`; +}; + +// 复制回答内容 +const handleCopyAnswer = async () => { + const content = getActualContent(); + if (!content) { + MessagePlugin.warning(t('chat.emptyContentWarning') || '当前回答为空,无法复制'); + return; + } + + try { + if (navigator.clipboard && navigator.clipboard.writeText) { + await navigator.clipboard.writeText(content); + MessagePlugin.success(t('chat.copySuccess') || '已复制到剪贴板'); + } else { + const textArea = document.createElement('textarea'); + textArea.value = content; + textArea.style.position = 'fixed'; + textArea.style.opacity = '0'; + document.body.appendChild(textArea); + textArea.select(); + document.execCommand('copy'); + document.body.removeChild(textArea); + MessagePlugin.success(t('chat.copySuccess') || '已复制到剪贴板'); + } + } catch (err) { + console.error('复制失败:', err); + MessagePlugin.error(t('chat.copyFailed') || '复制失败,请手动复制'); + } +}; + +// 添加到知识库 +const handleAddToKnowledge = () => { + const content = getActualContent(); + if (!content) { + MessagePlugin.warning(t('chat.emptyContentWarning') || '当前回答为空,无法保存到知识库'); + return; + } + + const question = (props.userQuery || '').trim(); + const manualContent = buildManualMarkdown(question, content); + const manualTitle = formatManualTitle(question); + + uiStore.openManualEditor({ + mode: 'create', + title: manualTitle, + content: manualContent, + status: 'draft', + }); + + MessagePlugin.info(t('chat.editorOpened') || '已打开编辑器,请选择知识库后保存'); +}; + onMounted(async () => { processedMarkdown.value = splitMarkdownByImages(props.content); removeImg() @@ -204,14 +290,14 @@ onMounted(async () => {