update gitignore and add comments

This commit is contained in:
sophie committed 2026-04-13 10:07:41 +08:00
1 parent 3d22e050f3
commit fdbfb28ee8
3 files changed
+15

No files matched your search

+3
View File
@@ -36,6 +36,9 @@ frontend/node_modules/
frontend/.DS_Store
frontend/.env.local
frontend/.env.*.local
frontend/.npm-cache
frontend/public/WW_verify_46MjWA55y3PBRLid.txt
frontend/package-lock.json
# IDEs
.vscode/
+10
View File
@@ -8,6 +8,7 @@
import { ref } from 'vue';
interface RetrieverResource {
// 知识库/检索来源的基础字段,具体键可能因后端返回而扩展
dataset_name?: string;
document_name?: string;
segment_id?: string;
@@ -17,12 +18,14 @@ interface RetrieverResource {
}
interface ChatMessage {
// 对话消息结构:用户/助手文本 + 可选的来源与归因信息
role: 'user' | 'assistant';
content: string;
retriever_resources?: RetrieverResource[];
attribution_parts?: string[];
}
// 统一资源格式为数组,兼容 list 或 { data: list } 两种结构
const normalizeResources = (resources: any): RetrieverResource[] => {
if (!resources) return [];
if (Array.isArray(resources)) return resources;
@@ -30,11 +33,13 @@ const normalizeResources = (resources: any): RetrieverResource[] => {
return [];
};
// 将检索来源资源合并到消息中,并按文档名+片段ID+内容做去重
const addRetrieverResources = (msg: ChatMessage, resources: any) => {
const list = normalizeResources(resources);
if (list.length === 0) return;
if (!msg.retriever_resources) msg.retriever_resources = [];
for (const item of list) {
// 使用稳定键进行去重,避免同一条来源在多事件中重复展示
const key = `${item.document_name || ''}|${item.segment_id || ''}|${item.content || ''}`;
const exists = msg.retriever_resources.some(
(r) => `${r.document_name || ''}|${r.segment_id || ''}|${r.content || ''}` === key
@@ -45,10 +50,12 @@ const addRetrieverResources = (msg: ChatMessage, resources: any) => {
}
};
// 归一化并合并归因内容(支持 string/array/object),按文本去重
const addAttributionParts = (msg: ChatMessage, value: any) => {
if (!value) return;
if (!msg.attribution_parts) msg.attribution_parts = [];
// 只收集非空文本,且保证唯一
const pushUnique = (text: string) => {
const v = text.trim();
if (!v) return;
@@ -78,6 +85,7 @@ const addAttributionParts = (msg: ChatMessage, value: any) => {
}
};
// 从事件顶层与 metadata 中提取来源/归因信息并写回当前消息
const applyEventMetadata = (msg: ChatMessage, data: any) => {
const metadata = data?.metadata || {};
addRetrieverResources(msg, data?.retriever_resources);
@@ -234,7 +242,9 @@ export function useChat() {
});
if (data.event === 'message' || data.event === 'agent_message') {
// event: message LLM 返回文本块事件,即:完整的文本以分块的方式输出。
if (typeof data.answer === 'string' && data.answer.length > 0) {
// answer: LLM 返回文本块内容
botMsg.content += data.answer;
console.debug('[chat][sse] append answer chunk', {
event: data.event,
+2
View File
@@ -25,6 +25,7 @@ const { t, locale } = useI18n();
const renderMarkdown = (text: string) => {
if (!text) return '';
try {
// 把 # 标题、代码块、[链接](url) 等 Markdown 解析成 HTML
return marked.parse(text);
} catch (e) {
return text;
@@ -628,6 +629,7 @@ const submitFeedback = async () => {
v-if="msg.role === 'assistant' && ((msg.retriever_resources && msg.retriever_resources.length > 0) || (msg.attribution_parts && msg.attribution_parts.length > 0) || getMessageLinks(msg).length > 0)"
class="mt-2 space-y-2"
>
<!-- 带有URL链接的源文件名称,支持预览 -->
<div v-if="getMessageLinks(msg).length > 0" class="flex flex-wrap gap-2">
<button
v-for="(file, linkIndex) in getMessageLinks(msg)"