fix(chat): 修复源文件显示了兜底文件名uploaded_file

This commit is contained in:
sophie committed 2026-06-23 11:58:57 +08:00
1 parent 0c5ee4aec1
commit 1bc938155a
2 files changed
+89 -4

No files matched your search

@@ -62,6 +62,28 @@ const looksLikeFileName = (name: string) => {
return /\.[A-Za-z0-9]{2,8}$/.test(n);
};
/**
* 判断源文件名是否为知识库上传链路生成的兜底名。
* AI 触发时机:当 Dify 文档名与 HXOPS 源文件名不一致时,用它识别 `uploaded_file.*`
* 这类无业务含义的占位名,避免聊天源文件按钮展示错误名称。
*/
const isFallbackUploadedFileName = (name: string) => {
const base = String(name || '').trim().split(/[\\/]/).pop() || '';
return /^uploaded_file(?:\.[A-Za-z0-9]{1,8})?$/i.test(base);
};
/**
* 选择聊天源文件按钮/预览弹窗的展示名称。
* AI 触发时机:构建 source file link 时调用;若本地源文件名是兜底名,
* 优先使用 Dify 检索返回的 document_name/title,下载链接仍沿用后端解析结果。
*/
const pickSourceDisplayName = (item: Record<string, unknown>, fallback = '') => {
const sourceFilename = String(item?.source_filename || '').trim();
const documentName = String(item?.document_name || item?.title || '').trim();
if (sourceFilename && !isFallbackUploadedFileName(sourceFilename)) return sourceFilename;
return documentName || sourceFilename || fallback;
};
const findFirstByKeys = (payload: unknown, keyCandidates: string[]): string => {
if (payload == null) return '';
if (Array.isArray(payload)) {
@@ -234,7 +256,7 @@ export function useChatSourceFiles({ agentId, t, msg, isMobile }: UseChatSourceF
].filter(Boolean) as string[];
const content = getSourceContent(item);
const forceName = String(item?.source_filename || item?.document_name || item?.title || '');
const forceName = pickSourceDisplayName(item);
const forceUrl = normalizeUrl(String(item?.source_download_url || item?.url || ''));
if (forceName && (forceUrl || (datasetId && remoteDocumentId))) {
@@ -263,7 +285,7 @@ export function useChatSourceFiles({ agentId, t, msg, isMobile }: UseChatSourceF
for (const url of discovered) {
if (isChunkLikeLink(url)) continue;
const name = String(item?.source_filename || item?.document_name || item?.title || safeDecode(url.split('/').pop() || 'source-file'));
const name = pickSourceDisplayName(item, safeDecode(url.split('/').pop() || 'source-file'));
if (isNoisySourceName(name)) continue;
if (isMarkdownFileName(name) || isMarkdownByUrl(url)) continue;
const key = buildSourceLinkKey(name, datasetId, remoteDocumentId, url);
@@ -316,7 +338,7 @@ export function useChatSourceFiles({ agentId, t, msg, isMobile }: UseChatSourceF
const getDisplayRetrieverResources = (message: Record<string, unknown>): Record<string, unknown>[] => {
const list = Array.isArray(message?.retriever_resources) ? message.retriever_resources as Record<string, unknown>[] : [];
return list.filter((doc) => {
const name = String(doc?.source_filename || doc?.document_name || doc?.title || '');
const name = pickSourceDisplayName(doc);
const url = String(doc?.source_download_url || doc?.url || doc?.file_url || doc?.document_url || '');
return !(isMarkdownFileName(name) || isMarkdownByUrl(url));
});
@@ -358,7 +380,9 @@ export function useChatSourceFiles({ agentId, t, msg, isMobile }: UseChatSourceF
return {
...file,
url: resolvedUrl,
name: data?.source_filename || file.name,
name: data?.source_filename && !isFallbackUploadedFileName(data.source_filename)
? data.source_filename
: file.name,
actualExt: String(data?.source_ext || '').trim().toLowerCase() || undefined,
};
}