代码提交
This commit is contained in:
1 parent
96bfdacee5
commit
067f602732
1 file changed
+53
-26
@@ -168,33 +168,60 @@ function parseKnowledgeBaseChunk(jsonText: string): KnowledgeBaseChunk {
|
||||
}
|
||||
}
|
||||
|
||||
function flattenDocs(value: unknown): Record<string, unknown>[] {
|
||||
if (Array.isArray(value)) {
|
||||
return value.flatMap((item) => flattenDocs(item));
|
||||
}
|
||||
if (!value || typeof value !== "object") return [];
|
||||
const record = value as Record<string, unknown>;
|
||||
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<string, unknown>;
|
||||
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,
|
||||
},
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user