增加是否显示源文件配置

This commit is contained in:
sophie committed 2026-04-13 10:10:58 +08:00
1 parent fdbfb28ee8
commit a3f6ada815
2 files changed
+56 -13

No files matched your search

+9
View File
@@ -10,6 +10,15 @@ export const config = {
},
workspace: {
enableFeedback: true,
// 聊天窗口“源文件展示”开关配置
sourceFiles: {
// 是否显示源文件区域(包含上方文件按钮 + 下方“源文档”详情)
showSourceFiles: true,
// 是否显示 MD 源文件(文件名后缀为 .md/.markdown)
showMdFiles: true,
// 当 showMdFiles=true 时,MD 源文件是否默认展开“源文档”详情
expandMdByDefault: false,
},
modules: {
announcements: true,
favorites: true,
+47 -13
View File
@@ -64,6 +64,16 @@ interface SourceFileLink {
url: string;
}
// 源文件展示配置(来自 config.ts)
const sourceFileConfig = {
// 是否显示整个“源文件”区域
showSourceFiles: config.workspace?.sourceFiles?.showSourceFiles !== false,
// 是否显示 MD 源文件(.md/.markdown)
showMdFiles: config.workspace?.sourceFiles?.showMdFiles !== false,
// MD 源文件是否默认展开“源文档”详情
expandMdByDefault: config.workspace?.sourceFiles?.expandMdByDefault === true,
};
const PREFERRED_SOURCE_EXTS = new Set([
'pdf', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'csv',
'jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp',
@@ -107,7 +117,7 @@ const getUrlExt = (url: string) => {
};
const isChunkLikeLink = (url: string) => {
// Keep original Markdown files; only filter explicit chunk/segment-style links.
// 保留原始 Markdown 文件链接;仅过滤明显的分片/切片类链接。
return /segment|chunk|fragments?|split/i.test(url);
};
@@ -117,12 +127,12 @@ const isPreferredSourceLink = (url: string) => {
};
const isNoisySourceUrl = (url: string) => {
// Raw signed download links are noisy in UI (e.g. download?version_id=...&token=...).
// 带签名参数的下载链接在 UI 中可读性差(如 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=');
};
@@ -130,12 +140,14 @@ const isNoisySourceName = (name: string) => {
const looksLikeFileName = (name: string) => {
const n = (name || '').trim();
if (!n) return false;
// Keep Chinese/English file labels such as 员工手册.doc
// 保留中英文文件名,如 员工手册.doc
return /\.[A-Za-z0-9]{2,8}$/.test(n);
};
const isMarkdownFileName = (name: string) => /\.(md|markdown)$/i.test((name || '').trim());
const isMarkdownByUrl = (url: string) => /\.(md|markdown)(?:$|\?)/i.test((url || '').toLowerCase());
const extractUrlsFromText = (text: string) => {
if (!text) return [];
const urls: string[] = [];
@@ -203,7 +215,7 @@ const getSourceLinks = (resources: any[] = []): SourceFileLink[] => {
...extractUrlsFromText(serialized),
];
// Parse explicit file URL array if Dify provides it.
// 若 Dify 返回了 files 数组,额外补充其显式文件链接。
const files = Array.isArray(item?.files) ? item.files : [];
for (const f of files) {
const fu = normalizeUrl(f?.url || f?.download_url || f?.file_url || '');
@@ -214,11 +226,13 @@ const getSourceLinks = (resources: any[] = []): SourceFileLink[] => {
for (const url of discovered) {
if (isChunkLikeLink(url)) continue;
// If URL is noisy and we don't have an explicit filename from metadata, skip it.
// URL 若为噪声签名链接,且没有可用文件名元数据,则跳过。
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;
// 根据配置决定是否显示 MD 文件
if (!sourceFileConfig.showMdFiles && (isMarkdownFileName(name) || isMarkdownByUrl(url))) continue;
const key = `${name}|${url}`;
if (!map.has(key)) {
map.set(key, { name, url });
@@ -231,13 +245,15 @@ const getSourceLinks = (resources: any[] = []): SourceFileLink[] => {
};
const getMessageLinks = (msg: any): SourceFileLink[] => {
if (!sourceFileConfig.showSourceFiles) return [];
const map = new Map<string, 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.
// 文件名有效时,保留文件按钮(即便 URL 是临时签名地址)。
if (isNoisySourceName(link.name)) continue;
if (!sourceFileConfig.showMdFiles && (isMarkdownFileName(link.name) || isMarkdownByUrl(link.url))) continue;
if (isMarkdownFileName(link.name)) {
const key = `${link.name}|${link.url}`;
if (!map.has(key)) map.set(key, link);
@@ -250,8 +266,8 @@ const getMessageLinks = (msg: any): SourceFileLink[] => {
};
if (msg?.retriever_resources) {
// Only use structured retriever resources for source buttons,
// so we don't accidentally show random URLs from answer text.
// 仅使用结构化 retriever_resources 生成源文件按钮,
// 避免把回答正文里的普通 URL 误显示为“源文件”。
merge(getSourceLinks(msg.retriever_resources));
}
@@ -260,6 +276,23 @@ const getMessageLinks = (msg: any): SourceFileLink[] => {
return preferred.length > 0 ? preferred : links;
};
const getDisplayRetrieverResources = (msg: any): any[] => {
if (!sourceFileConfig.showSourceFiles) return [];
const list = Array.isArray(msg?.retriever_resources) ? msg.retriever_resources : [];
if (sourceFileConfig.showMdFiles) return list;
return list.filter((doc: any) => {
const name = doc?.source_filename || doc?.document_name || doc?.title || '';
const url = doc?.source_download_url || doc?.url || doc?.file_url || doc?.document_url || '';
return !(isMarkdownFileName(String(name)) || isMarkdownByUrl(String(url)));
});
};
const shouldExpandSourceDetails = (msg: any): boolean => {
if (!sourceFileConfig.showSourceFiles) return false;
if (!sourceFileConfig.showMdFiles || !sourceFileConfig.expandMdByDefault) return false;
return getMessageLinks(msg).some((link) => isMarkdownFileName(link.name) || isMarkdownByUrl(link.url));
};
const filePreviewVisible = ref(false);
const previewFile = ref<SourceFileLink | null>(null);
@@ -626,7 +659,7 @@ const submitFeedback = async () => {
</div>
<div
v-if="msg.role === 'assistant' && ((msg.retriever_resources && msg.retriever_resources.length > 0) || (msg.attribution_parts && msg.attribution_parts.length > 0) || getMessageLinks(msg).length > 0)"
v-if="sourceFileConfig.showSourceFiles && msg.role === 'assistant' && ((getDisplayRetrieverResources(msg).length > 0) || (msg.attribution_parts && msg.attribution_parts.length > 0) || getMessageLinks(msg).length > 0)"
class="mt-2 space-y-2"
>
<!-- 带有URL链接的源文件名称,支持预览 -->
@@ -642,15 +675,16 @@ const submitFeedback = async () => {
</div>
<details
v-if="msg.retriever_resources && msg.retriever_resources.length > 0"
v-if="getDisplayRetrieverResources(msg).length > 0"
class="rounded-xl border border-slate-200 bg-slate-50/90 px-4 py-3 dark:border-slate-700 dark:bg-slate-800/65"
:open="shouldExpandSourceDetails(msg)"
>
<summary class="cursor-pointer text-xs font-semibold uppercase tracking-wide text-slate-600 dark:text-slate-300">
源文档 ({{ msg.retriever_resources.length }})
源文档 ({{ getDisplayRetrieverResources(msg).length }})
</summary>
<div class="mt-3 space-y-2">
<div
v-for="(doc, docIndex) in msg.retriever_resources"
v-for="(doc, docIndex) in getDisplayRetrieverResources(msg)"
:key="`${doc.document_name || 'doc'}-${doc.segment_id || docIndex}`"
class="rounded-lg border border-slate-200 bg-white p-3 dark:border-slate-700 dark:bg-slate-900"
>