原始文件气泡映射错误 + 多版本下载错版本
This commit is contained in:
1 parent
f877f61efd
commit
854bc8e4db
7 files changed
+203
-80
No files matched your search
@@ -311,18 +311,57 @@ const findFirstByKeys = (payload: any, keyCandidates: string[]): string => {
|
||||
return '';
|
||||
};
|
||||
|
||||
const findDirectByKeys = (payloads: any[], keyCandidates: string[]): string => {
|
||||
for (const payload of payloads) {
|
||||
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) continue;
|
||||
for (const key of keyCandidates) {
|
||||
const value = payload?.[key];
|
||||
if (value != null && String(value).trim()) {
|
||||
return String(value).trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
return '';
|
||||
};
|
||||
|
||||
const getResourceIds = (item: any) => {
|
||||
const metadata = item?.metadata && typeof item.metadata === 'object' && !Array.isArray(item.metadata)
|
||||
? item.metadata
|
||||
: null;
|
||||
const datasetId = findFirstByKeys(item, ['dataset_id', 'dify_dataset_id', 'knowledge_base_id', 'kb_id']);
|
||||
const remoteDocumentId = findFirstByKeys(item, [
|
||||
const remoteDocumentId = findDirectByKeys([item, metadata], [
|
||||
'remote_document_id',
|
||||
'dify_document_id',
|
||||
'document_id',
|
||||
'doc_id',
|
||||
'source_document_id',
|
||||
]);
|
||||
'doc_id',
|
||||
]) || findFirstByKeys(item, [
|
||||
'remote_document_id',
|
||||
'dify_document_id',
|
||||
'source_document_id',
|
||||
'doc_id',
|
||||
]) || findDirectByKeys([item, metadata], ['document_id']);
|
||||
return { datasetId, remoteDocumentId };
|
||||
};
|
||||
|
||||
const canPreviewSourceLink = (link: Partial<SourceFileLink> | null | undefined) => {
|
||||
if (!link) return false;
|
||||
return Boolean(link.url || (link.datasetId && link.remoteDocumentId));
|
||||
};
|
||||
|
||||
const buildSourceLinkKey = (name: string, datasetId?: string, remoteDocumentId?: string, url?: string) => {
|
||||
const normalizedName = String(name || '').trim();
|
||||
const normalizedDatasetId = String(datasetId || '').trim();
|
||||
const normalizedRemoteDocumentId = String(remoteDocumentId || '').trim();
|
||||
const normalizedUrl = String(url || '').split('?')[0];
|
||||
if (normalizedDatasetId && normalizedRemoteDocumentId) {
|
||||
return `${normalizedName}|${normalizedDatasetId}|${normalizedRemoteDocumentId}`;
|
||||
}
|
||||
if (looksLikeFileName(normalizedName)) {
|
||||
return normalizedUrl ? `${normalizedName}|${normalizedUrl}` : normalizedName;
|
||||
}
|
||||
return `${normalizedName}|${normalizedUrl}`;
|
||||
};
|
||||
|
||||
const isMarkdownFileName = (name: string) => /\.(md|markdown)$/i.test((name || '').trim());
|
||||
|
||||
const isMarkdownByUrl = (url: string) => /\.(md|markdown)(?:$|\?)/i.test((url || '').toLowerCase());
|
||||
@@ -405,8 +444,8 @@ const getSourceLinks = (resources: any[] = []): SourceFileLink[] => {
|
||||
const forceName = item?.source_filename || item?.document_name || item?.title;
|
||||
const forceUrl = normalizeUrl(item?.source_download_url || item?.url || '');
|
||||
|
||||
if (forceName) {
|
||||
const key = forceName;
|
||||
if (forceName && (forceUrl || (datasetId && remoteDocumentId))) {
|
||||
const key = buildSourceLinkKey(forceName, datasetId, remoteDocumentId, forceUrl);
|
||||
mergeSourceLink(key, {
|
||||
name: forceName,
|
||||
...(forceUrl ? { url: forceUrl } : {}),
|
||||
@@ -442,8 +481,7 @@ const getSourceLinks = (resources: any[] = []): SourceFileLink[] => {
|
||||
// 根据配置决定是否显示 MD 文件
|
||||
if (isMarkdownFileName(name) || isMarkdownByUrl(url)) continue;
|
||||
|
||||
// Deduplicate by name if it looks like a filename, else by name + base URL
|
||||
const key = looksLikeFileName(name) ? name : `${name}|${url.split('?')[0]}`;
|
||||
const key = buildSourceLinkKey(name, datasetId, remoteDocumentId, url);
|
||||
mergeSourceLink(key, {
|
||||
name,
|
||||
url,
|
||||
@@ -452,7 +490,7 @@ const getSourceLinks = (resources: any[] = []): SourceFileLink[] => {
|
||||
});
|
||||
}
|
||||
}
|
||||
const links = Array.from(map.values());
|
||||
const links = Array.from(map.values()).filter((link) => canPreviewSourceLink(link));
|
||||
// 暂时注释掉 isPreferredSourceLink 的过滤,让所有解析出来的气泡都能显示
|
||||
// const preferred = links.filter((l) => isPreferredSourceLink(l.url));
|
||||
// return preferred.length > 0 ? preferred : links;
|
||||
@@ -481,18 +519,19 @@ const getMessageLinks = (msg: any): SourceFileLink[] => {
|
||||
});
|
||||
};
|
||||
for (const link of links) {
|
||||
if (!canPreviewSourceLink(link)) continue;
|
||||
const linkUrl = link.url || '';
|
||||
if (linkUrl && isChunkLikeLink(linkUrl)) continue;
|
||||
// 文件名有效时,保留文件按钮(即便 URL 是临时签名地址)。
|
||||
if (isNoisySourceName(link.name)) continue;
|
||||
if (isMarkdownFileName(link.name) || isMarkdownByUrl(linkUrl)) continue;
|
||||
if (isMarkdownFileName(link.name)) {
|
||||
const key = looksLikeFileName(link.name) ? link.name : `${link.name}|${linkUrl.split('?')[0]}`;
|
||||
const key = buildSourceLinkKey(link.name, link.datasetId, link.remoteDocumentId, linkUrl);
|
||||
mergeMessageLink(key, link);
|
||||
continue;
|
||||
}
|
||||
if (linkUrl && isNoisySourceUrl(linkUrl) && !looksLikeFileName(link.name)) continue;
|
||||
const key = looksLikeFileName(link.name) ? link.name : `${link.name}|${linkUrl.split('?')[0]}`;
|
||||
const key = buildSourceLinkKey(link.name, link.datasetId, link.remoteDocumentId, linkUrl);
|
||||
mergeMessageLink(key, link);
|
||||
}
|
||||
};
|
||||
@@ -561,33 +600,32 @@ const filePreviewVisible = ref(false);
|
||||
const previewFile = ref<{ name: string; url: string; actualExt?: string } | null>(null);
|
||||
|
||||
const resolveSourceLink = async (file: SourceFileLink): Promise<{ name: string; url: string; actualExt?: string }> => {
|
||||
if (file.url) {
|
||||
return {
|
||||
name: file.name,
|
||||
url: file.url,
|
||||
actualExt: file.actualExt,
|
||||
};
|
||||
if (file.datasetId && file.remoteDocumentId) {
|
||||
const data = await resolveChatSourceFile({
|
||||
agent_id: agentId,
|
||||
dataset_id: file.datasetId,
|
||||
remote_document_id: file.remoteDocumentId,
|
||||
});
|
||||
|
||||
const resolvedUrl = normalizeUrl(data?.source_download_url || '');
|
||||
if (resolvedUrl) {
|
||||
return {
|
||||
...file,
|
||||
url: resolvedUrl,
|
||||
name: data?.source_filename || file.name,
|
||||
actualExt: String(data?.source_ext || '').trim().toLowerCase() || undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
if (!file.datasetId || !file.remoteDocumentId) {
|
||||
|
||||
if (!file.url) {
|
||||
throw new Error('当前源文件暂未返回可预览链接');
|
||||
}
|
||||
|
||||
const data = await resolveChatSourceFile({
|
||||
agent_id: agentId,
|
||||
dataset_id: file.datasetId,
|
||||
remote_document_id: file.remoteDocumentId,
|
||||
});
|
||||
|
||||
const resolvedUrl = normalizeUrl(data?.source_download_url || '');
|
||||
if (!resolvedUrl) {
|
||||
throw new Error('未查询到源文件下载地址');
|
||||
}
|
||||
|
||||
return {
|
||||
...file,
|
||||
url: resolvedUrl,
|
||||
name: data?.source_filename || file.name,
|
||||
actualExt: String(data?.source_ext || '').trim().toLowerCase() || undefined,
|
||||
name: file.name,
|
||||
url: file.url,
|
||||
actualExt: file.actualExt,
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user