WeKnora icon indicating copy to clipboard operation
WeKnora copied to clipboard

[Bug]: 前端默认解析引擎标注与后端 DefaultParserEngine 不一致,PDF 被错误标为 anydoc 默认

Open JSJ189 opened this issue 1 month ago • 0 comments

Affected Component

Frontend UI

Bug Description

前端的默认引擎判定与后端 DefaultParserEngine 不一致,导致 PDF 被错误标注 anydoc 为默认。

用户照着「(默认)」选 anydoc 解析 PDF,必然报:

anydoc conversion failed for "...pdf":
  anydoc: markdown conversion failed: anydoc: malformed:
  malformed document: couldn't parse input: invalid content stream

后端代码注释已明确说明 PDF 不能走 anydoc:

// PDF is excluded. anydoc has no document model for it, so Convert force-
// disables asset extraction, and AnydocReader only falls back to builtin
// when the whole file yields no text at all.
func preferAnydocWhenAvailable(fileType string) string {
	if IsSimpleFormat(ft) || ft == "pdf" {
		return ""
	}

前后端行为对照

后端 types.DefaultParserEngine(fileType)

func DefaultParserEngine(fileType string) string {
	ft := normalizeParserFileType(fileType)
	if preferParserEngine != nil {
		if engine := preferParserEngine(ft); engine != "" {
			return engine
		}
	}
	return defaultParserEngineByType[ft]
}

var defaultParserEngineByType = map[string]string{
	"ppt":  "markitdown",
	"pptx": "markitdown",
}

前端 KBParserSettings.vue:pickDefaultEngineName

function pickDefaultEngineName(engines, extensions) {
  const available = engines.filter(e => e.available)
  const simpleExts = new Set(['md', 'markdown', 'txt', 'csv', 'json'])
  const allSimple = extensions.length > 0 && extensions.every(ext => simpleExts.has(ext))
  if (!allSimple) {
    const anydoc = available.find(e => e.name === 'anydoc')
    if (anydoc) return anydoc.name      // ← 只要 anydoc 可用就返回,不排除 pdf
  }
  return available[0]?.name ?? ''
}

不一致矩阵

文件类型 anydoc 可用 前端标注「默认」 后端实际默认 一致
pdf 是 / 否 anydoc(是)/ available[0] builtin ❌
ppt / pptx 否 available[0](builtin) markitdown ❌
ppt / pptx 是 anydoc anydoc ✅
docx / doc / xlsx / xls / epub 等 是 anydoc anydoc ✅
md / markdown / txt / csv / json — available[0] simple 路由 基本一致

三处具体缺陷

缺陷 1(主):PDF 被标为 anydoc 默认

位置:frontend/src/views/knowledge/settings/KBParserSettings.vue:212-224

前端的 pickDefaultEngineName 只区分「是否全部为简单格式」,没有排除 pdf。后端 preferAnydocWhenAvailable 显式排除 pdf。

缺陷 2(连带):ppt / pptx 在 anydoc 不可用时标注错误

位置:同上

anydoc 不可用时,前端回退到 available[0]?.name(通常是 builtin),但后端 defaultParserEngineByType 指定 ppt/pptx → markitdown。

缺陷 3(测试固化错误行为)

位置:frontend/src/composables/useModalShell.test.ts:181

assert.equal(vm.getEngineForGroup(['pdf']), 'anydoc', 'defaults are still displayed')

该断言把 PDF 默认为 anydoc 固化进测试。修正缺陷 1 后此测试会失败,需同步改为 'builtin'。


Steps to Reproduce

  1. 打开知识库设置 → 解析设置(KBParserSettings.vue),或上传文档时的解析分栏。
  2. 找到 PDF 文件类型分组。
  3. 展开解析引擎下拉。
  4. 观察 anydoc 选项后标注了「(默认)」。
  5. 保持默认上传一份 PDF。
  6. 解析失败,报 anydoc: malformed: malformed document: couldn't parse input: invalid content stream。

对照:查询 GET /api/v1/system/parser-engines,anydoc 的 FileTypes 包含 pdf,说明前端据此把 anydoc 当作候选;但后端路由层并不把它作为 PDF 默认。


Expected Behavior

前端的默认引擎标注应与后端 DefaultParserEngine(fileType) 完全一致。

两种修法:

  • A:前端复刻后端规则 —— pickDefaultEngineName 排除 pdf(以及 ppt/pptx 的 markitdown 回退),把默认值计算对齐 types.DefaultParserEngine;
  • B:后端在 GET /api/v1/system/parser-engines 响应里为每个文件类型返回 default_engine 字段,前端只做展示,不再自行推断。

推荐 B,单一事实来源,避免以后再次漂移。


Actual Behavior

  • PDF 分组的下拉里 anydoc 后面带「(默认)」;
  • 用户照默认选 anydoc,PDF 解析必然失败;
  • 后端 DefaultParserEngine("pdf") 实际返回 builtin(DocReader 内置解析器),但界面没有任何提示;
  • anydoc 失败后不回退:PDFNeedsOCR 只匹配 ocr is required / no extractable text,pdfMarkdownIsEmpty 在 err != nil 时返回 false,malformed 错误直接硬抛。

WeKnora Version

App Version: v0.8.3

Deployment Method

Docker Compose

Operating System

Windows 11 + Docker Desktop

Relevant Logs

[convert] kb=c384c4d3-... fileType=pdf isURL=false engine="anydoc" rules=[{FileTypes:[pdf] Engine:anydoc}]
[convert] DocReader failed code=DOCREADER_PARSE_FAILED:
  anydoc conversion failed for "小八音沟河大桥_第3-12页_Hybrid测试.pdf":
  anydoc: markdown conversion failed: anydoc: malformed:
  malformed document: couldn't parse input: invalid content stream

Related

  • #4326 同一场景下的另一个缺陷:自动重试成功后根 span 仍为 failed。

Confirmation

  • [x] I have searched existing issues and confirmed this is a new one

JSJ189 avatar Oct 15 '26 07:10 JSJ189