fix(parser): prefer anydoc for ppt/pptx when the binding is linked
Type-level defaults still fall back to markitdown, but a linked anydoc build should use the in-process converter first instead of always shipping Office decks through docreader.main
parent
52182385ec
commit
6020799792
|
|
@ -199,7 +199,7 @@ function getEngineOptions(extensions: string[]): EngineOption[] {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const defaultName = raw.find(e => e.available)?.name ?? ''
|
const defaultName = pickDefaultEngineName(raw, extensions)
|
||||||
return raw
|
return raw
|
||||||
.filter(e => e.available)
|
.filter(e => e.available)
|
||||||
.map(e => ({
|
.map(e => ({
|
||||||
|
|
@ -209,6 +209,19 @@ function getEngineOptions(extensions: string[]): EngineOption[] {
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function pickDefaultEngineName(
|
||||||
|
engines: { name: string; available: boolean }[],
|
||||||
|
extensions: string[],
|
||||||
|
): string {
|
||||||
|
const available = engines.filter(e => e.available)
|
||||||
|
const prefersAnydoc = extensions.some(ext => ext === 'ppt' || ext === 'pptx')
|
||||||
|
if (prefersAnydoc) {
|
||||||
|
const anydoc = available.find(e => e.name === 'anydoc')
|
||||||
|
if (anydoc) return anydoc.name
|
||||||
|
}
|
||||||
|
return available[0]?.name ?? ''
|
||||||
|
}
|
||||||
|
|
||||||
function hasAvailableEngine(extensions: string[]): boolean {
|
function hasAvailableEngine(extensions: string[]): boolean {
|
||||||
return getEngineOptions(extensions).length > 0
|
return getEngineOptions(extensions).length > 0
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,11 @@
|
||||||
package docparser
|
package docparser
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/infrastructure/docparser/anydoc"
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
func TestListAllEnginesBuiltinIncludesDocumentFormats(t *testing.T) {
|
func TestListAllEnginesBuiltinIncludesDocumentFormats(t *testing.T) {
|
||||||
engines := ListAllEngines(true, nil, nil)
|
engines := ListAllEngines(true, nil, nil)
|
||||||
|
|
@ -26,3 +31,22 @@ func TestListAllEnginesBuiltinIncludesDocumentFormats(t *testing.T) {
|
||||||
|
|
||||||
t.Fatal("builtin engine not found")
|
t.Fatal("builtin engine not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDefaultParserEnginePrefersAnydocWhenLinked(t *testing.T) {
|
||||||
|
if types.DefaultParserEngine("pdf") != "" {
|
||||||
|
t.Fatalf("DefaultParserEngine(pdf) should stay empty so builtin routing is unchanged")
|
||||||
|
}
|
||||||
|
got := types.DefaultParserEngine("pptx")
|
||||||
|
if anydoc.Available() {
|
||||||
|
if got != AnydocEngineName {
|
||||||
|
t.Fatalf("DefaultParserEngine(pptx) = %q, want anydoc when the binding is linked", got)
|
||||||
|
}
|
||||||
|
if types.DefaultParserEngine("ppt") != AnydocEngineName {
|
||||||
|
t.Fatalf("DefaultParserEngine(ppt) = %q, want anydoc", types.DefaultParserEngine("ppt"))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if got != "markitdown" {
|
||||||
|
t.Fatalf("DefaultParserEngine(pptx) = %q, want markitdown when anydoc is unavailable", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Tencent/WeKnora/internal/infrastructure/docparser/anydoc"
|
"github.com/Tencent/WeKnora/internal/infrastructure/docparser/anydoc"
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -29,6 +30,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
types.SetPreferParserEngine(preferAnydocWhenAvailable)
|
||||||
RegisterEngine(&builtinEngine{})
|
RegisterEngine(&builtinEngine{})
|
||||||
RegisterEngine(&simpleEngine{})
|
RegisterEngine(&simpleEngine{})
|
||||||
RegisterEngine(&anydocEngine{})
|
RegisterEngine(&anydocEngine{})
|
||||||
|
|
@ -39,6 +41,17 @@ func init() {
|
||||||
RegisterEngine(&paddleOCRVLCloudEngine{})
|
RegisterEngine(&paddleOCRVLCloudEngine{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// preferAnydocWhenAvailable is the type-level default override: when the
|
||||||
|
// anydoc binding is linked and converts this file type, use it instead of
|
||||||
|
// the markitdown fallback. Types without a fallback (pdf, docx, …) never
|
||||||
|
// consult this — DefaultParserEngine short-circuits first.
|
||||||
|
func preferAnydocWhenAvailable(fileType string) string {
|
||||||
|
if anydoc.Available() && anydoc.Supports(fileType, "") {
|
||||||
|
return AnydocEngineName
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// builtin — DocReader-backed parser for complex document formats.
|
// builtin — DocReader-backed parser for complex document formats.
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
|
@ -250,7 +250,8 @@ type ChunkingConfig struct {
|
||||||
Separators []string `yaml:"separators" json:"separators"`
|
Separators []string `yaml:"separators" json:"separators"`
|
||||||
// ParserEngineRules configures which parser engine to use for each file type.
|
// ParserEngineRules configures which parser engine to use for each file type.
|
||||||
// When empty, DefaultParserEngine is used (builtin/simple routing, except
|
// When empty, DefaultParserEngine is used (builtin/simple routing, except
|
||||||
// types that only a specific engine can parse, e.g. ppt/pptx → markitdown).
|
// types that only a specific engine can parse: ppt/pptx prefer anydoc
|
||||||
|
// when that binding is linked, otherwise markitdown).
|
||||||
ParserEngineRules []ParserEngineRule `yaml:"parser_engine_rules,omitempty" json:"parser_engine_rules,omitempty"`
|
ParserEngineRules []ParserEngineRule `yaml:"parser_engine_rules,omitempty" json:"parser_engine_rules,omitempty"`
|
||||||
// EnableParentChild enables two-level parent-child chunking strategy.
|
// EnableParentChild enables two-level parent-child chunking strategy.
|
||||||
// When enabled, large parent chunks provide context while small child chunks
|
// When enabled, large parent chunks provide context while small child chunks
|
||||||
|
|
@ -280,17 +281,42 @@ type ChunkingConfig struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// defaultParserEngineByType maps file types the builtin/simple engines cannot
|
// defaultParserEngineByType maps file types the builtin/simple engines cannot
|
||||||
// parse to the only engine that can. Types omitted here keep empty-string
|
// parse to a docreader engine that can. Types omitted here keep empty-string
|
||||||
// routing (Go simple formats, otherwise docreader builtin).
|
// routing (Go simple formats, otherwise docreader builtin). Anydoc, when
|
||||||
|
// linked into the binary, is preferred over these fallbacks — see
|
||||||
|
// SetPreferParserEngine.
|
||||||
var defaultParserEngineByType = map[string]string{
|
var defaultParserEngineByType = map[string]string{
|
||||||
"ppt": "markitdown",
|
"ppt": "markitdown",
|
||||||
"pptx": "markitdown",
|
"pptx": "markitdown",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// preferParserEngine, if set, may override DefaultParserEngine for types that
|
||||||
|
// have a type-level fallback. The docparser package registers anydoc here so
|
||||||
|
// types does not import the engine catalog.
|
||||||
|
var preferParserEngine func(fileType string) string
|
||||||
|
|
||||||
|
// SetPreferParserEngine registers a build-time preference used by
|
||||||
|
// DefaultParserEngine. Pass nil to clear. Intended to be called from init().
|
||||||
|
func SetPreferParserEngine(fn func(fileType string) string) {
|
||||||
|
preferParserEngine = fn
|
||||||
|
}
|
||||||
|
|
||||||
// DefaultParserEngine returns the engine used when no parser_engine_rules
|
// DefaultParserEngine returns the engine used when no parser_engine_rules
|
||||||
// match. Empty string means builtin (docreader) or Go simple-format routing.
|
// match. Empty string means builtin (docreader) or Go simple-format routing.
|
||||||
|
// For ppt/pptx the preference is anydoc when that binding is available,
|
||||||
|
// otherwise markitdown.
|
||||||
func DefaultParserEngine(fileType string) string {
|
func DefaultParserEngine(fileType string) string {
|
||||||
return defaultParserEngineByType[normalizeParserFileType(fileType)]
|
ft := normalizeParserFileType(fileType)
|
||||||
|
fallback := defaultParserEngineByType[ft]
|
||||||
|
if fallback == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if preferParserEngine != nil {
|
||||||
|
if engine := preferParserEngine(ft); engine != "" {
|
||||||
|
return engine
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fallback
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolveParserEngine returns the engine name for the given file type
|
// ResolveParserEngine returns the engine name for the given file type
|
||||||
|
|
|
||||||
|
|
@ -327,6 +327,33 @@ func TestDefaultParserEngine(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDefaultParserEnginePrefersRegisteredEngineForFallbackTypes(t *testing.T) {
|
||||||
|
t.Cleanup(func() { SetPreferParserEngine(nil) })
|
||||||
|
SetPreferParserEngine(func(fileType string) string {
|
||||||
|
if fileType == "pptx" || fileType == "ppt" {
|
||||||
|
return "anydoc"
|
||||||
|
}
|
||||||
|
return "should-not-apply"
|
||||||
|
})
|
||||||
|
if got := DefaultParserEngine("pptx"); got != "anydoc" {
|
||||||
|
t.Fatalf("DefaultParserEngine(pptx) = %q, want anydoc", got)
|
||||||
|
}
|
||||||
|
if got := DefaultParserEngine("ppt"); got != "anydoc" {
|
||||||
|
t.Fatalf("DefaultParserEngine(ppt) = %q, want anydoc", got)
|
||||||
|
}
|
||||||
|
if got := DefaultParserEngine("pdf"); got != "" {
|
||||||
|
t.Fatalf("DefaultParserEngine(pdf) = %q, want empty (no type-level fallback)", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDefaultParserEngineIgnoresEmptyPreference(t *testing.T) {
|
||||||
|
t.Cleanup(func() { SetPreferParserEngine(nil) })
|
||||||
|
SetPreferParserEngine(func(string) string { return "" })
|
||||||
|
if got := DefaultParserEngine("pptx"); got != "markitdown" {
|
||||||
|
t.Fatalf("DefaultParserEngine(pptx) = %q, want markitdown when preference is empty", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestChunkingConfigResolveParserEngineDefaults(t *testing.T) {
|
func TestChunkingConfigResolveParserEngineDefaults(t *testing.T) {
|
||||||
var empty ChunkingConfig
|
var empty ChunkingConfig
|
||||||
if got := empty.ResolveParserEngine("pptx"); got != "markitdown" {
|
if got := empty.ResolveParserEngine("pptx"); got != "markitdown" {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue