72dfb9ce75
- Updated context template to include runtime metadata such as current time and week, improving contextual awareness in user queries. - Enhanced rewrite template with critical instructions for intent classification, ensuring that rewritten questions preserve essential entities and keywords. - Refined intent classification logic to prioritize user intents more effectively, improving the accuracy of responses based on user queries. - Added examples to clarify expected input and output formats for intent classification, enhancing usability for developers.
615 lines
16 KiB
Vue
615 lines
16 KiB
Vue
<template>
|
||
<div class="bot_msg">
|
||
<div style="display: flex;flex-direction: column; gap:8px">
|
||
<!-- 显示@的知识库和文件(非 Agent 模式下显示) -->
|
||
<div v-if="!session.isAgentMode && mentionedItems && mentionedItems.length > 0" class="mentioned_items">
|
||
<span
|
||
v-for="item in mentionedItems"
|
||
:key="item.id"
|
||
class="mentioned_tag"
|
||
:class="[
|
||
item.type === 'kb' ? (item.kb_type === 'faq' ? 'faq-tag' : 'kb-tag') : 'file-tag'
|
||
]"
|
||
>
|
||
<span class="tag_icon">
|
||
<t-icon v-if="item.type === 'kb'" :name="item.kb_type === 'faq' ? 'chat-bubble-help' : 'folder'" />
|
||
<t-icon v-else name="file" />
|
||
</span>
|
||
<span class="tag_name">{{ item.name }}</span>
|
||
</span>
|
||
</div>
|
||
<docInfo :session="session"></docInfo>
|
||
<AgentStreamDisplay :session="session" :user-query="userQuery" v-if="session.isAgentMode"></AgentStreamDisplay>
|
||
<deepThink :deepSession="session" v-if="session.showThink && !session.isAgentMode"></deepThink>
|
||
</div>
|
||
<!-- 非 Agent 模式下才显示传统的 markdown 渲染 -->
|
||
<div ref="parentMd" v-if="!session.hideContent && !session.isAgentMode">
|
||
<!-- 直接渲染完整内容,避免切分导致的问题,样式与 thinking 一致 -->
|
||
<!-- 只有当有实际内容时才显示包围框 -->
|
||
<div class="content-wrapper" v-if="hasActualContent">
|
||
<div class="ai-markdown-template markdown-content">
|
||
<div v-for="(token, index) in markdownTokens" :key="index" v-html="renderToken(token)"></div>
|
||
</div>
|
||
</div>
|
||
<!-- Streaming indicator (non-Agent mode) -->
|
||
<div v-if="hasActualContent && !session.is_completed" class="loading-indicator">
|
||
<div class="loading-typing">
|
||
<span></span>
|
||
<span></span>
|
||
<span></span>
|
||
</div>
|
||
</div>
|
||
<!-- 复制和添加到知识库按钮 - 非 Agent 模式下显示 -->
|
||
<div v-if="session.is_completed && (content || session.content)" class="answer-toolbar">
|
||
<t-button size="small" variant="outline" shape="round" @click.stop="handleCopyAnswer" :title="$t('agent.copy')">
|
||
<t-icon name="copy" />
|
||
</t-button>
|
||
<t-button size="small" variant="outline" shape="round" @click.stop="handleAddToKnowledge" :title="$t('agent.addToKnowledgeBase')">
|
||
<t-icon name="add" />
|
||
</t-button>
|
||
<!-- Fallback 提示图标 -->
|
||
<t-tooltip v-if="session.is_fallback" :content="$t('chat.fallbackHint')" placement="top">
|
||
<t-button size="small" variant="outline" shape="round" class="fallback-icon-btn">
|
||
<t-icon name="info-circle" />
|
||
</t-button>
|
||
</t-tooltip>
|
||
</div>
|
||
<div v-if="isImgLoading" class="img_loading"><t-loading size="small"></t-loading><span>{{ $t('common.loading') }}</span></div>
|
||
</div>
|
||
<picturePreview :reviewImg="reviewImg" :reviewUrl="reviewUrl" @closePreImg="closePreImg"></picturePreview>
|
||
</div>
|
||
</template>
|
||
<script setup>
|
||
import { onMounted, onBeforeUnmount, watch, computed, ref, reactive, defineProps, nextTick } from 'vue';
|
||
import { marked } from 'marked';
|
||
import docInfo from './docInfo.vue';
|
||
import deepThink from './deepThink.vue';
|
||
import AgentStreamDisplay from './AgentStreamDisplay.vue';
|
||
import picturePreview from '@/components/picture-preview.vue';
|
||
import { sanitizeHTML, safeMarkdownToHTML, createSafeImage, isValidImageURL, hydrateProtectedFileImages } from '@/utils/security';
|
||
import { openMermaidFullscreen } from '@/utils/mermaidViewer';
|
||
import { useI18n } from 'vue-i18n';
|
||
import { MessagePlugin } from 'tdesign-vue-next';
|
||
import { useUIStore } from '@/stores/ui';
|
||
import {
|
||
buildManualMarkdown,
|
||
copyTextToClipboard,
|
||
formatManualTitle,
|
||
replaceIncompleteImageWithPlaceholder
|
||
} from '@/utils/chatMessageShared';
|
||
import {
|
||
bindMermaidFullscreenEvents,
|
||
createMermaidCodeRenderer,
|
||
ensureMermaidInitialized,
|
||
renderMermaidInContainer
|
||
} from '@/utils/mermaidShared';
|
||
|
||
marked.use({
|
||
mangle: false,
|
||
headerIds: false,
|
||
breaks: true, // 全局启用单个换行支持
|
||
});
|
||
|
||
ensureMermaidInitialized();
|
||
|
||
const emit = defineEmits(['scroll-bottom'])
|
||
const { t } = useI18n()
|
||
const uiStore = useUIStore();
|
||
const renderer = new marked.Renderer();
|
||
let parentMd = ref()
|
||
let reviewUrl = ref('')
|
||
let reviewImg = ref(false)
|
||
let isImgLoading = ref(false);
|
||
const props = defineProps({
|
||
// 必填项
|
||
content: {
|
||
type: String,
|
||
required: false
|
||
},
|
||
session: {
|
||
type: Object,
|
||
required: false
|
||
},
|
||
userQuery: {
|
||
type: String,
|
||
required: false,
|
||
default: ''
|
||
},
|
||
isFirstEnter: {
|
||
type: Boolean,
|
||
required: false
|
||
}
|
||
});
|
||
|
||
const preview = (url) => {
|
||
nextTick(() => {
|
||
reviewUrl.value = url;
|
||
reviewImg.value = true
|
||
})
|
||
}
|
||
|
||
const closePreImg = () => {
|
||
reviewImg.value = false
|
||
reviewUrl.value = '';
|
||
}
|
||
|
||
// 创建自定义渲染器实例
|
||
const customRenderer = new marked.Renderer();
|
||
// 覆盖图片渲染方法
|
||
customRenderer.image = function(href, title, text) {
|
||
if (!isValidImageURL(href)) {
|
||
return `<p>${t('error.invalidImageLink')}</p>`;
|
||
}
|
||
return createSafeImage(href, text || '', title || '');
|
||
};
|
||
|
||
// 覆盖代码块渲染方法,支持 Mermaid
|
||
customRenderer.code = createMermaidCodeRenderer('mermaid-botmsg');
|
||
|
||
// 计算属性:将 Markdown 文本转换为 tokens
|
||
const mentionedItems = computed(() => {
|
||
return props.session?.mentioned_items || [];
|
||
});
|
||
|
||
const markdownTokens = computed(() => {
|
||
const text = props.content || props.session?.content || '';
|
||
if (!text || typeof text !== 'string') {
|
||
return [];
|
||
}
|
||
|
||
const processed = replaceIncompleteImageWithPlaceholder(text);
|
||
|
||
// 首先对 Markdown 内容进行安全处理
|
||
const safeMarkdown = safeMarkdownToHTML(processed);
|
||
|
||
// 使用 marked.lexer 分词
|
||
return marked.lexer(safeMarkdown);
|
||
});
|
||
|
||
// 计算属性:判断是否有实际内容(非空且不只是空白)
|
||
const hasActualContent = computed(() => {
|
||
const text = props.content || props.session?.content || '';
|
||
return text && text.trim().length > 0;
|
||
});
|
||
|
||
// 渲染单个 token 为 HTML
|
||
const renderToken = (token) => {
|
||
try {
|
||
// 创建临时的 marked 配置
|
||
const markedOptions = {
|
||
renderer: customRenderer,
|
||
breaks: true
|
||
};
|
||
|
||
// 解析单个 token
|
||
// marked.parser 接受 token 数组
|
||
let html = marked.parser([token], markedOptions);
|
||
|
||
// 使用 DOMPurify 进行最终的安全清理
|
||
return sanitizeHTML(html);
|
||
} catch (e) {
|
||
console.error('Token rendering error:', e);
|
||
return '';
|
||
}
|
||
};
|
||
|
||
const myMarkdown = (res) => {
|
||
return marked.parse(res, { renderer })
|
||
}
|
||
|
||
// 获取实际内容
|
||
const getActualContent = () => {
|
||
return (props.content || props.session?.content || '').trim();
|
||
};
|
||
|
||
// 复制回答内容
|
||
const handleCopyAnswer = async () => {
|
||
const content = getActualContent();
|
||
if (!content) {
|
||
MessagePlugin.warning(t('chat.emptyContentWarning'));
|
||
return;
|
||
}
|
||
|
||
try {
|
||
await copyTextToClipboard(content);
|
||
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'));
|
||
};
|
||
|
||
// 处理 markdown-content 中图片的点击事件
|
||
const handleMarkdownImageClick = (e) => {
|
||
const target = e.target;
|
||
if (target && target.tagName === 'IMG') {
|
||
const src = target.getAttribute('src');
|
||
if (src) {
|
||
e.preventDefault();
|
||
e.stopPropagation();
|
||
preview(src);
|
||
}
|
||
}
|
||
};
|
||
|
||
// 渲染 Mermaid 图表的函数
|
||
const renderMermaidDiagrams = async () => {
|
||
try {
|
||
const renderedCount = await renderMermaidInContainer(parentMd.value, renderedMermaidIds);
|
||
if (renderedCount > 0) {
|
||
nextTick(() => {
|
||
bindMermaidClickEvents();
|
||
});
|
||
}
|
||
} catch (error) {
|
||
console.error('Mermaid rendering error:', error);
|
||
}
|
||
};
|
||
|
||
// 已渲染的 mermaid 元素 ID 集合
|
||
const renderedMermaidIds = new Set();
|
||
|
||
// 为 Mermaid 容器绑定点击全屏事件(绑定在 div 上,不是 SVG 上)
|
||
const bindMermaidClickEvents = () => {
|
||
bindMermaidFullscreenEvents(parentMd.value, (svgOuterHTML) => {
|
||
openMermaidFullscreen(svgOuterHTML);
|
||
});
|
||
};
|
||
|
||
// 监听内容变化并渲染 Mermaid - 只在会话完成后渲染
|
||
watch(() => [props.content, props.session?.content, props.session?.is_completed], () => {
|
||
nextTick(async () => {
|
||
await hydrateProtectedFileImages(parentMd.value);
|
||
// 只在会话完成后渲染 mermaid
|
||
if (props.session?.is_completed) {
|
||
renderMermaidDiagrams();
|
||
}
|
||
});
|
||
}, { immediate: true });
|
||
|
||
onMounted(async () => {
|
||
// 为 markdown-content 中的图片添加点击事件
|
||
nextTick(async () => {
|
||
if (parentMd.value) {
|
||
parentMd.value.addEventListener('click', handleMarkdownImageClick, true);
|
||
}
|
||
await hydrateProtectedFileImages(parentMd.value);
|
||
// 初始渲染 Mermaid 图表
|
||
renderMermaidDiagrams();
|
||
});
|
||
});
|
||
|
||
onBeforeUnmount(() => {
|
||
if (parentMd.value) {
|
||
parentMd.value.removeEventListener('click', handleMarkdownImageClick, true);
|
||
}
|
||
});
|
||
</script>
|
||
<style lang="less" scoped>
|
||
@import '../../../components/css/markdown.less';
|
||
@import '../../../components/css/chat-message-shared.less';
|
||
|
||
// 内容包装器 - 与 Agent 模式的 answer 样式一致
|
||
.content-wrapper {
|
||
background: var(--td-bg-color-container);
|
||
border-radius: 6px;
|
||
padding: 8px 0px;
|
||
transition: all 0.2s ease;
|
||
}
|
||
|
||
.mentioned_items {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 6px;
|
||
justify-content: flex-start;
|
||
max-width: 100%;
|
||
margin-bottom: 2px;
|
||
}
|
||
|
||
.mentioned_tag {
|
||
display: inline-flex;
|
||
align-items: center;
|
||
gap: 4px;
|
||
padding: 3px 8px;
|
||
border-radius: 4px;
|
||
font-size: 12px;
|
||
font-weight: 500;
|
||
max-width: 200px;
|
||
cursor: default;
|
||
transition: all 0.15s;
|
||
background: rgba(7, 192, 95, 0.06);
|
||
border: 1px solid rgba(7, 192, 95, 0.2);
|
||
color: var(--td-text-color-primary);
|
||
|
||
&.kb-tag {
|
||
.tag_icon {
|
||
color: var(--td-brand-color);
|
||
}
|
||
}
|
||
|
||
&.faq-tag {
|
||
.tag_icon {
|
||
color: var(--td-warning-color);
|
||
}
|
||
}
|
||
|
||
&.file-tag {
|
||
.tag_icon {
|
||
color: var(--td-text-color-secondary);
|
||
}
|
||
}
|
||
|
||
.tag_icon {
|
||
font-size: 13px;
|
||
display: flex;
|
||
align-items: center;
|
||
}
|
||
|
||
.tag_name {
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
color: currentColor;
|
||
}
|
||
}
|
||
|
||
.fallback-icon-btn {
|
||
color: var(--td-text-color-disabled) !important;
|
||
border-color: var(--td-component-stroke) !important;
|
||
|
||
&:hover {
|
||
color: var(--td-text-color-placeholder) !important;
|
||
border-color: var(--td-component-border) !important;
|
||
}
|
||
}
|
||
|
||
@keyframes fadeInUp {
|
||
from {
|
||
opacity: 0;
|
||
transform: translateY(8px);
|
||
}
|
||
to {
|
||
opacity: 1;
|
||
transform: translateY(0);
|
||
}
|
||
}
|
||
|
||
.ai-markdown-template {
|
||
font-size: 15px;
|
||
color: var(--td-text-color-primary);
|
||
line-height: 1.6;
|
||
}
|
||
|
||
.markdown-content {
|
||
:deep(p) {
|
||
margin: 6px 0;
|
||
line-height: 1.6;
|
||
}
|
||
|
||
:deep(code) {
|
||
background: var(--td-bg-color-secondarycontainer);
|
||
padding: 2px 5px;
|
||
border-radius: 3px;
|
||
font-family: 'Monaco', 'Menlo', 'Courier New', monospace;
|
||
font-size: 11px;
|
||
}
|
||
|
||
:deep(pre) {
|
||
background: var(--td-bg-color-secondarycontainer);
|
||
padding: 10px;
|
||
border-radius: 4px;
|
||
overflow-x: auto;
|
||
margin: 6px 0;
|
||
|
||
code {
|
||
background: none;
|
||
padding: 0;
|
||
}
|
||
}
|
||
|
||
:deep(ul), :deep(ol) {
|
||
margin: 6px 0;
|
||
padding-left: 20px;
|
||
}
|
||
|
||
:deep(li) {
|
||
margin: 3px 0;
|
||
}
|
||
|
||
:deep(blockquote) {
|
||
border-left: 2px solid var(--td-brand-color);
|
||
padding-left: 10px;
|
||
margin: 6px 0;
|
||
color: var(--td-text-color-secondary);
|
||
}
|
||
|
||
:deep(h1), :deep(h2), :deep(h3), :deep(h4), :deep(h5), :deep(h6) {
|
||
margin: 10px 0 6px 0;
|
||
font-weight: 600;
|
||
color: var(--td-text-color-primary);
|
||
}
|
||
|
||
:deep(a) {
|
||
color: var(--td-brand-color);
|
||
text-decoration: none;
|
||
|
||
&:hover {
|
||
text-decoration: underline;
|
||
}
|
||
}
|
||
|
||
:deep(table) {
|
||
border-collapse: collapse;
|
||
margin: 6px 0;
|
||
font-size: 11px;
|
||
width: 100%;
|
||
|
||
th, td {
|
||
border: 1px solid var(--td-component-stroke);
|
||
padding: 5px 8px;
|
||
text-align: left;
|
||
}
|
||
|
||
th {
|
||
background: var(--td-bg-color-secondarycontainer);
|
||
font-weight: 600;
|
||
}
|
||
|
||
tbody tr:nth-child(even) {
|
||
background: var(--td-bg-color-secondarycontainer);
|
||
}
|
||
}
|
||
|
||
:deep(img) {
|
||
max-width: 80%;
|
||
max-height: 300px;
|
||
width: auto;
|
||
height: auto;
|
||
border-radius: 8px;
|
||
display: block;
|
||
margin: 8px 0;
|
||
border: 0.5px solid var(--td-component-stroke);
|
||
object-fit: contain;
|
||
cursor: pointer;
|
||
transition: transform 0.2s ease;
|
||
|
||
&:hover {
|
||
}
|
||
}
|
||
|
||
// Mermaid 图表样式
|
||
:deep(.mermaid) {
|
||
margin: 16px 0;
|
||
padding: 16px;
|
||
background: var(--td-bg-color-secondarycontainer);
|
||
border-radius: 8px;
|
||
overflow-x: auto;
|
||
text-align: center;
|
||
|
||
svg {
|
||
max-width: 100%;
|
||
height: auto;
|
||
}
|
||
}
|
||
}
|
||
|
||
.ai-markdown-img {
|
||
max-width: 80%;
|
||
max-height: 300px;
|
||
width: auto;
|
||
height: auto;
|
||
border-radius: 8px;
|
||
display: block;
|
||
cursor: pointer;
|
||
object-fit: contain;
|
||
margin: 8px 0 8px 16px;
|
||
border: 0.5px solid var(--td-component-stroke);
|
||
transition: transform 0.2s ease;
|
||
|
||
&:hover {
|
||
transform: scale(1.02);
|
||
}
|
||
}
|
||
|
||
.bot_msg {
|
||
// background: var(--td-bg-color-container);
|
||
border-radius: 4px;
|
||
color: var(--td-text-color-primary);
|
||
font-size: 16px;
|
||
// padding: 10px 12px;
|
||
margin-right: auto;
|
||
max-width: 100%;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
.botanswer_laoding_gif {
|
||
width: 24px;
|
||
height: 18px;
|
||
margin-left: 16px;
|
||
}
|
||
|
||
.thinking-loading {
|
||
padding: 8px 0;
|
||
}
|
||
|
||
.loading-indicator {
|
||
padding: 8px 0;
|
||
}
|
||
|
||
.loading-typing {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 4px;
|
||
|
||
span {
|
||
width: 6px;
|
||
height: 6px;
|
||
border-radius: 50%;
|
||
background: var(--td-brand-color);
|
||
animation: typingBounce 1.4s ease-in-out infinite;
|
||
|
||
&:nth-child(1) {
|
||
animation-delay: 0s;
|
||
}
|
||
|
||
&:nth-child(2) {
|
||
animation-delay: 0.2s;
|
||
}
|
||
|
||
&:nth-child(3) {
|
||
animation-delay: 0.4s;
|
||
}
|
||
}
|
||
}
|
||
|
||
@keyframes typingBounce {
|
||
0%, 60%, 100% {
|
||
transform: translateY(0);
|
||
}
|
||
30% {
|
||
transform: translateY(-8px);
|
||
}
|
||
}
|
||
|
||
.img_loading {
|
||
background: var(--td-bg-color-container-hover);
|
||
height: 230px;
|
||
width: 230px;
|
||
color: var(--td-text-color-placeholder);
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
flex-direction: column;
|
||
font-size: 12px;
|
||
gap: 4px;
|
||
margin-left: 16px;
|
||
border-radius: 8px;
|
||
}
|
||
|
||
:deep(.t-loading__gradient-conic) {
|
||
background: conic-gradient(from 90deg at 50% 50%, #fff 0deg, #676767 360deg) !important;
|
||
|
||
}
|
||
</style> |