fix: 支持原文件下载,同时将postgres地址由localhost变更为8.152.207.171
This commit is contained in:
1 parent
70f95e9f5c
commit
c380248502
12 files changed
+1273
-273
No files matched your search
@@ -63,6 +63,12 @@ interface SourceFileLink {
|
||||
url: string;
|
||||
}
|
||||
|
||||
const PREFERRED_SOURCE_EXTS = new Set([
|
||||
'pdf', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'csv',
|
||||
'jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp',
|
||||
'txt', 'rtf', 'md', 'markdown',
|
||||
]);
|
||||
|
||||
const safeDecode = (value: string) => {
|
||||
try {
|
||||
return decodeURIComponent(value);
|
||||
@@ -92,6 +98,43 @@ const normalizeUrl = (url: string) => {
|
||||
return '';
|
||||
};
|
||||
|
||||
const getUrlExt = (url: string) => {
|
||||
const clean = (url.split('?')[0] || '').toLowerCase();
|
||||
const idx = clean.lastIndexOf('.');
|
||||
if (idx < 0) return '';
|
||||
return clean.slice(idx + 1);
|
||||
};
|
||||
|
||||
const isChunkLikeLink = (url: string) => {
|
||||
// Keep original Markdown files; only filter explicit chunk/segment-style links.
|
||||
return /segment|chunk|fragments?|split/i.test(url);
|
||||
};
|
||||
|
||||
const isPreferredSourceLink = (url: string) => {
|
||||
const ext = getUrlExt(url);
|
||||
return PREFERRED_SOURCE_EXTS.has(ext);
|
||||
};
|
||||
|
||||
const isNoisySourceUrl = (url: string) => {
|
||||
// Raw signed download links are noisy in UI (e.g. download?version_id=...&token=...).
|
||||
return /(?:^|\/)download\?/i.test(url) || /[?&](token|version_id|kind)=/i.test(url);
|
||||
};
|
||||
|
||||
const isNoisySourceName = (name: string) => {
|
||||
// Guard against names that are actually query strings instead of human-readable filenames.
|
||||
const n = (name || '').trim().toLowerCase();
|
||||
return n.startsWith('download?') || n.includes('token=') || n.includes('version_id=');
|
||||
};
|
||||
|
||||
const looksLikeFileName = (name: string) => {
|
||||
const n = (name || '').trim();
|
||||
if (!n) return false;
|
||||
// Keep Chinese/English file labels such as 员工手册.doc
|
||||
return /\.[A-Za-z0-9]{2,8}$/.test(n);
|
||||
};
|
||||
|
||||
const isMarkdownFileName = (name: string) => /\.(md|markdown)$/i.test((name || '').trim());
|
||||
|
||||
const extractUrlsFromText = (text: string) => {
|
||||
if (!text) return [];
|
||||
const urls: string[] = [];
|
||||
@@ -139,11 +182,13 @@ const getSourceLinks = (resources: any[] = []): SourceFileLink[] => {
|
||||
const map = new Map<string, SourceFileLink>();
|
||||
for (const item of resources) {
|
||||
const directCandidates = [
|
||||
item?.source_download_url,
|
||||
item?.url,
|
||||
item?.file_url,
|
||||
item?.document_url,
|
||||
item?.source_url,
|
||||
item?.download_url,
|
||||
item?.metadata?.source_download_url,
|
||||
item?.metadata?.url,
|
||||
item?.metadata?.file_url,
|
||||
item?.metadata?.document_url,
|
||||
@@ -167,14 +212,21 @@ const getSourceLinks = (resources: any[] = []): SourceFileLink[] => {
|
||||
}
|
||||
|
||||
for (const url of discovered) {
|
||||
const name = item?.document_name || item?.title || decodeURIComponent(url.split('/').pop() || 'source-file');
|
||||
if (isChunkLikeLink(url)) continue;
|
||||
// If URL is noisy and we don't have an explicit filename from metadata, skip it.
|
||||
if (isNoisySourceUrl(url) && !item?.source_filename && !item?.document_name) continue;
|
||||
const name = item?.source_filename || item?.document_name || item?.title || safeDecode(url.split('/').pop() || 'source-file');
|
||||
// Skip links whose display name itself is noisy.
|
||||
if (isNoisySourceName(name)) continue;
|
||||
const key = `${name}|${url}`;
|
||||
if (!map.has(key)) {
|
||||
map.set(key, { name, url });
|
||||
}
|
||||
}
|
||||
}
|
||||
return Array.from(map.values());
|
||||
const links = Array.from(map.values());
|
||||
const preferred = links.filter((l) => isPreferredSourceLink(l.url));
|
||||
return preferred.length > 0 ? preferred : links;
|
||||
};
|
||||
|
||||
const getMessageLinks = (msg: any): SourceFileLink[] => {
|
||||
@@ -182,41 +234,29 @@ const getMessageLinks = (msg: any): SourceFileLink[] => {
|
||||
|
||||
const merge = (links: SourceFileLink[]) => {
|
||||
for (const link of links) {
|
||||
if (isChunkLikeLink(link.url)) continue;
|
||||
// Keep file buttons if name is valid, even when URL is signed/temporary.
|
||||
if (isNoisySourceName(link.name)) continue;
|
||||
if (isMarkdownFileName(link.name)) {
|
||||
const key = `${link.name}|${link.url}`;
|
||||
if (!map.has(key)) map.set(key, link);
|
||||
continue;
|
||||
}
|
||||
if (isNoisySourceUrl(link.url) && !looksLikeFileName(link.name)) continue;
|
||||
const key = `${link.name}|${link.url}`;
|
||||
if (!map.has(key)) map.set(key, link);
|
||||
}
|
||||
};
|
||||
|
||||
if (msg?.retriever_resources) {
|
||||
// Only use structured retriever resources for source buttons,
|
||||
// so we don't accidentally show random URLs from answer text.
|
||||
merge(getSourceLinks(msg.retriever_resources));
|
||||
}
|
||||
|
||||
const contentLinks = extractUrlsFromText(msg?.content || '');
|
||||
for (const url of contentLinks) {
|
||||
const name = safeDecode(url.split('/').pop() || 'source-file');
|
||||
merge([{ name, url }]);
|
||||
}
|
||||
|
||||
if (Array.isArray(msg?.attribution_parts)) {
|
||||
for (const part of msg.attribution_parts) {
|
||||
const urls = extractUrlsFromText(typeof part === 'string' ? part : JSON.stringify(part));
|
||||
for (const url of urls) {
|
||||
const name = safeDecode(url.split('/').pop() || 'source-file');
|
||||
merge([{ name, url }]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 5) 最后兜底:扫描整条 assistant 消息的 JSON 字符串。
|
||||
// 用途:覆盖 Dify 在未知嵌套字段里返回链接但未被上面路径命中的情况。
|
||||
const fullMessageText = JSON.stringify(msg || {});
|
||||
const allUrls = extractUrlsFromText(fullMessageText);
|
||||
for (const url of allUrls) {
|
||||
const name = safeDecode(url.split('/').pop() || 'source-file');
|
||||
merge([{ name, url }]);
|
||||
}
|
||||
|
||||
return Array.from(map.values());
|
||||
const links = Array.from(map.values());
|
||||
const preferred = links.filter((l) => isPreferredSourceLink(l.url));
|
||||
return preferred.length > 0 ? preferred : links;
|
||||
};
|
||||
|
||||
const filePreviewVisible = ref(false);
|
||||
|
||||
Reference in new issue
Block a user