fix(frontend): label the parser engine the backend will actually pick (#4354)

pickDefaultEngineName guessed the default from engine availability order,
so it badges anydoc as the PDF default while preferAnydocWhenAvailable
deliberately excludes PDF, and it labels builtin as the default for the
simple formats the Go reader actually handles.

Mirror the three backend routing tables instead of guessing, and pin the
mirror with a per-group assertion table.
main
The one -Niu 2026-10-16 23:50:15 +08:00 committed by GitHub
parent 86767d77e9
commit ad63fc4b8c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 99 additions and 10 deletions

View File

@ -182,7 +182,7 @@ for (const savedRules of [[], [{ file_types: ['xlsx'], engine: 'builtin', xlsx_f
try {
release()
await settle()
assert.equal(vm.getEngineForGroup(['pdf']), 'anydoc', 'defaults are still displayed')
assert.equal(vm.getEngineForGroup(['pdf']), 'builtin', 'defaults are still displayed')
assert.equal(updates.length, 0, 'loading must not mutate the parent form')
f.ui.showSettingsModal = true
await vue.nextTick()
@ -205,6 +205,62 @@ for (const savedRules of [[], [{ file_types: ['xlsx'], engine: 'builtin', xlsx_f
})
}
// Engine FileTypes copied from internal/infrastructure/docparser/engines.go, so
// a drift between the registry and the frontend default routing shows up here
// rather than as a "(default)" badge that fails on upload.
const registryEngines = [
{
Name: 'builtin', Available: true,
FileTypes: ['docx', 'doc', 'pdf', 'md', 'markdown', 'xlsx', 'xls', 'pptx', 'ppt', 'epub',
'html', 'htm', 'mhtml', 'xmind', 'jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff', 'webp',
'mp3', 'wav', 'm4a', 'flac', 'ogg'],
},
{
Name: 'simple', Available: true,
FileTypes: ['md', 'markdown', 'txt', 'csv', 'json', 'jpg', 'jpeg', 'png', 'gif', 'bmp',
'tiff', 'webp', 'mp3', 'wav', 'm4a', 'flac', 'ogg'],
},
{ Name: 'anydoc', Available: true, FileTypes: ['docx', 'doc', 'xlsx', 'xls', 'pptx', 'ppt', 'pdf'] },
{ Name: 'markitdown', Available: true, FileTypes: ['docx', 'pptx', 'ppt'] },
]
async function mountParserSettings(engines: any[]) {
const f = fixture()
f.resources.parserEngines = engines
const props = vue.reactive({ parserEngineRules: [], 'onUpdate:parserEngineRules': () => {} })
const { vm, unmount } = f.mount('../views/knowledge/settings/KBParserSettings.vue', props)
await settle()
return { vm, unmount }
}
test('the default engine badge follows the backend per-type routing', async () => {
const { vm, unmount } = await mountParserSettings(registryEngines)
try {
// PDF is the case anydoc cannot model: engines.go keeps it on builtin.
assert.equal(vm.getEngineForGroup(['pdf']), 'builtin')
for (const group of [['md', 'markdown'], ['txt'], ['csv'], ['json'], ['jpg', 'jpeg', 'png']]) {
assert.equal(vm.getEngineForGroup(group), 'simple', `${group} stays on the Go reader`)
}
for (const group of [['docx', 'doc'], ['xlsx', 'xls'], ['pptx', 'ppt']]) {
assert.equal(vm.getEngineForGroup(group), 'anydoc', `${group} prefers anydoc`)
}
assert.equal(vm.getEngineForGroup(['mhtml']), 'builtin', 'anydoc does not claim mhtml')
} finally { unmount() }
})
test('with anydoc unavailable only ppt/pptx leave builtin for a pinned engine', async () => {
const { vm, unmount } = await mountParserSettings(
registryEngines.map(e => (e.Name === 'anydoc' ? { ...e, Available: false } : e)),
)
try {
assert.equal(vm.getEngineForGroup(['pdf']), 'builtin')
assert.equal(vm.getEngineForGroup(['pptx', 'ppt']), 'markitdown', 'knowledgebase.go pins ppt/pptx')
assert.equal(vm.getEngineForGroup(['docx', 'doc']), 'builtin')
assert.equal(vm.getEngineForGroup(['xlsx', 'xls']), 'builtin')
assert.equal(vm.getEngineForGroup(['md', 'markdown']), 'simple')
} finally { unmount() }
})
test('assistant changes warn without overwriting unsaved agent settings', async t => {
t.mock.timers.enable({ apis: ['setTimeout'] })
const f = fixture()

View File

@ -209,18 +209,51 @@ function getEngineOptions(extensions: string[]): EngineOption[] {
}))
}
// The "(default)" badge has to name the engine ingestion will actually pick, so
// it is computed from the backend routing rather than from availability order:
// preferAnydocWhenAvailable in internal/infrastructure/docparser/engines.go
// simpleFormats in internal/infrastructure/docparser/builtin_converter.go
// defaultParserEngineByType in internal/types/knowledgebase.go
// DefaultParserEngine returns "" when nothing is pinned for a type, which sends
// simple formats to the Go reader and everything else to docreader builtin.
const simpleFileTypes = new Set([
'md', 'markdown', 'txt', 'text', 'csv', 'json',
'jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff', 'webp',
'mp3', 'wav', 'm4a', 'flac', 'ogg',
])
// anydoc has no document model for PDF, so the backend never defaults to it.
const noAnydocFileTypes = new Set(['pdf'])
const pinnedEngineByFileType: Record<string, string> = {
ppt: 'markitdown',
pptx: 'markitdown',
}
function defaultEngineForFileType(
engines: { name: string; available: boolean; fileTypes: string[] }[],
ext: string,
): string {
const usable = (name: string) =>
engines.some(e => e.available && e.name === name && e.fileTypes.includes(ext))
if (!simpleFileTypes.has(ext) && !noAnydocFileTypes.has(ext) && usable('anydoc')) {
return 'anydoc'
}
const pinned = pinnedEngineByFileType[ext]
if (pinned && usable(pinned)) return pinned
const unpinned = simpleFileTypes.has(ext) ? 'simple' : 'builtin'
return usable(unpinned) ? unpinned : ''
}
function pickDefaultEngineName(
engines: { name: string; available: boolean }[],
engines: { name: string; available: boolean; fileTypes: string[] }[],
extensions: string[],
): string {
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
}
return available[0]?.name ?? ''
const perFileType = extensions.map(ext => defaultEngineForFileType(engines, ext))
const first = perFileType[0] ?? ''
// One dropdown serves the whole group, so only badge a default when every
// member of the group routes to the same engine.
return perFileType.every(name => name === first) ? first : ''
}
function hasAvailableEngine(extensions: string[]): boolean {