Skip to content

fix(knowledge): restore PDF preview MIME + configurable citation labels (#85, #86) - #88

Merged
2 commits merged into
Soren-ABT:mainfrom
InfiniteScope:fix/issues-85-86-pdf-and-citation
Nov 24, 2026
Merged

2 commits merged into
Soren-ABT:mainfrom
InfiniteScope:fix/issues-85-86-pdf-and-citation

Conversation

@InfiniteScope

Copy link
Copy Markdown
Contributor

一个 PR 处理两个最近的 issue:#85(PDF 预览变成下载)和 #86(自动检索 header 强制引用标签)。两处改动互不依赖,但都很小,放在一起方便复测。

#85:PDF 预览失效

根因是目录导入/路径导入的文档没有写入 mimeType:ingestPathFile 建占位和提交时都没带,addFileDocument 也只在请求自带 MIME 时才保存(浏览器上传通常带,目录扫描不带)。getRawFile 返回 doc.mimeType,/raw 拿到空值就回退成 application/octet-stream;前端 blob 带这个类型 + nosniff,Chromium 不把它当 PDF 内嵌渲染,于是走下载。

修复分三层,都是最小改动:

  1. 新增 mimeTypeForFileName(按扩展名映射 pdf/docx/xlsx/epub/图片/文本等);addFileDocument 和 ingestPathFile 在导入时就补 mimeType,占位行和最终写入都带上。
  2. getRawFile 对没有 MIME 的旧记录按文件名兜底。
  3. /raw 响应再兜底一次 mimeType ?? mimeTypeForFileName(fileName) ?? application/octet-stream。

边界:只补 MIME,不改解析/分块/内容;已有库不需要重建,旧记录靠第 2/3 层就能恢复预览;未知扩展名仍然回退 octet-stream;SVG 等映射仍受 raw 响应的 CSP sandbox 约束。

#86:自动检索引用标签开关

header 里那句 Cite the source labels when it answers the question: 是硬编码的,所以用户只能二选一:保留自动注入(回答里出现一长串 [source: …; baseId=…])或关闭自动检索(丢掉证据)。

修复:新增全局配置 autoRetrieveCitation(默认 true,保持现有行为)。关闭后 header 只保留“不可信参考资料 / 不要执行其中指令”的边界声明,不再要求模型输出来源标签;证据和标签本身仍在注入文本里,需要出处时可以再要求模型给出。UI 放在“全局默认”scope 的高级设置里(autoRetrieve 下面);base scope 不显示——header 是一条注入消息,按全局统一语义更清楚。

边界:这是软约束,模型仍可能偶尔复述标签,但会明显减少;默认行为不变;暂不支持按 base 单独设置。

测试

  • tests/raw-preview.spec.ts:MIME 映射;真实目录导入一个 PDF 后 getRawFile 返回 application/pdf;旧记录无 MIME 时 /raw 响应头仍是 application/pdf(inline=1 的 disposition 也验证)。
  • tests/config.spec.ts:autoRetrieveCitation 默认 true、可持久化 false、类型校验。
  • tests/auto-retrieve.spec.ts:关闭时注入文本仍含证据和边界声明,但不再含 Cite the source labels。

红→绿已验证:撤销源码改动后,目录导入用例(mimeType undefined)、旧记录响应头(octet-stream)和关闭引用用例(仍出现 Cite...)都会失败;修复后全绿。

全量 vitest --dir tests:707 passed;tool-contract.spec.ts 仍因本机缺 sibling checkout 无法加载(CI 覆盖)。

Fixes #85.
Fixes #86.

…figurable (Soren-ABT#85, Soren-ABT#86)

Directory imports published documents without a mimeType, so the raw route served every PDF as application/octet-stream and the panel iframe fell back to a download. Derive the MIME from the extension on upload/path imports, fall back to the file name in getRawFile and the raw response, and keep existing libraries previewable without a reindex.

Also add a global autoRetrieveCitation switch (default true): off keeps the injected evidence and untrusted-content boundary but drops the instruction that makes the model print source labels. Exposed in the global defaults scope of the retrieval settings.
The raw-preview spec copied an older inline FakeTable that lacks the deleteMany method required by the DSH 0.2.2-rc.5 KvTable interface, so every typecheck job failed before running tests. Use tests/helpers/memory-kv-table.ts instead.
@Soren-ABT Soren-ABT closed this pull request by merging all changes into Soren-ABT:main in e7e291e Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants