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

+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,