From 067f6027327634879809c73bab52104b76643f20 Mon Sep 17 00:00:00 2001 From: fengxiangdebaozi <10845145+fengxiangdebaozi@user.noreply.gitee.com> Date: Fri, 14 Aug 2026 19:31:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/core/threads/use-single-round-rag.ts | 79 +++++++++++++------ 1 file changed, 53 insertions(+), 26 deletions(-) diff --git a/frontend-web/src/core/threads/use-single-round-rag.ts b/frontend-web/src/core/threads/use-single-round-rag.ts index e90d5fc8..3d3e925b 100644 --- a/frontend-web/src/core/threads/use-single-round-rag.ts +++ b/frontend-web/src/core/threads/use-single-round-rag.ts @@ -168,33 +168,60 @@ function parseKnowledgeBaseChunk(jsonText: string): KnowledgeBaseChunk { } } -function flattenDocs(value: unknown): Record[] { - if (Array.isArray(value)) { - return value.flatMap((item) => flattenDocs(item)); - } - if (!value || typeof value !== "object") return []; - const record = value as Record; - if ("docs" in record) return flattenDocs(record.docs); - return [record]; -} +/** + * Mirrors GeneralQA's `mapDocsToSources` behavior: `docs` may be an array, + * a single document, or a plain string. The previous adapter only accepted + * objects and consequently dropped the first stream payload whenever the + * knowledge-base service returned a string document. + */ +function normalizeSources(value: KnowledgeBaseChunk["docs"]): RagSource[] { + const docs = Array.isArray(value) ? value : value ? [value] : []; + return docs.flatMap((item, index): RagSource[] => { + if (typeof item === "string") { + return [ + { + title: `参考资料 ${index + 1}`, + type: "知识库", + snippet: item, + content: item, + raw: { content: item }, + }, + ]; + } + if (!item || typeof item !== "object") return []; -function normalizeSources(value: unknown): RagSource[] { - return flattenDocs(value).map((item, index) => { - const title = String(item.m_title ?? item.title ?? item.source ?? `参考资料 ${index + 1}`); - const content = String(item.m_content ?? item.content ?? item.snippet ?? ""); - const source = item.source1 ?? item.source; - const rawUrl = item.url ?? item.link ?? item.href; - return { - id: String(item.recUuid ?? item.rec_uuid ?? item.uuid ?? item.id ?? "") || undefined, - title, - type: String(item.source1 ?? item.type ?? "知识库"), - snippet: content, - content, - source: typeof source === "string" ? source : undefined, - time: typeof item.m_publish === "string" ? item.m_publish : undefined, - url: typeof rawUrl === "string" && /^https?:\/\//i.test(rawUrl) ? rawUrl : undefined, - raw: item, - }; + const record = item as Record; + const title = String( + record.title ?? record.m_title ?? record.source ?? `参考资料 ${index + 1}`, + ); + const content = String( + record.content ?? record.m_content ?? record.snippet ?? record.url ?? "", + ); + const source = record.source ?? record.source1 ?? record.title ?? record.m_title; + const rawUrl = record.url ?? record.link ?? record.href; + return [ + { + id: + String(record.recUuid ?? record.rec_uuid ?? record.uuid ?? record.id ?? "") || + undefined, + title, + type: String(record.source1 ?? record.type ?? "知识库"), + snippet: content, + content, + source: typeof source === "string" ? source : undefined, + time: + typeof record.m_publish === "string" + ? record.m_publish + : typeof record.time === "string" + ? record.time + : undefined, + url: + typeof rawUrl === "string" && /^https?:\/\//i.test(rawUrl) + ? rawUrl + : undefined, + raw: record, + }, + ]; }); }