fix(parser): prefer anydoc for every type it converts when linked
ppt/pptx still fall back to markitdown when anydoc is not in the binary. pdf/docx/xlsx and other anydoc formats now default to the in-process converter as well; csv/md stay on the Go simple reader.main
parent
6020799792
commit
35275d5613
|
|
@ -214,8 +214,9 @@ function pickDefaultEngineName(
|
|||
extensions: string[],
|
||||
): string {
|
||||
const available = engines.filter(e => e.available)
|
||||
const prefersAnydoc = extensions.some(ext => ext === 'ppt' || ext === 'pptx')
|
||||
if (prefersAnydoc) {
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,20 +33,25 @@ func TestListAllEnginesBuiltinIncludesDocumentFormats(t *testing.T) {
|
|||
}
|
||||
|
||||
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")
|
||||
cases := []string{"pptx", "ppt", "pdf", "docx"}
|
||||
if anydoc.Available() {
|
||||
if got != AnydocEngineName {
|
||||
t.Fatalf("DefaultParserEngine(pptx) = %q, want anydoc when the binding is linked", got)
|
||||
for _, ft := range cases {
|
||||
if got := types.DefaultParserEngine(ft); got != AnydocEngineName {
|
||||
t.Errorf("DefaultParserEngine(%s) = %q, want anydoc when the binding is linked", ft, got)
|
||||
}
|
||||
}
|
||||
if types.DefaultParserEngine("ppt") != AnydocEngineName {
|
||||
t.Fatalf("DefaultParserEngine(ppt) = %q, want anydoc", types.DefaultParserEngine("ppt"))
|
||||
if got := types.DefaultParserEngine("csv"); got != "" {
|
||||
t.Errorf("DefaultParserEngine(csv) = %q, want empty so the Go simple reader stays default", got)
|
||||
}
|
||||
return
|
||||
}
|
||||
if got != "markitdown" {
|
||||
if got := types.DefaultParserEngine("pptx"); got != "markitdown" {
|
||||
t.Fatalf("DefaultParserEngine(pptx) = %q, want markitdown when anydoc is unavailable", got)
|
||||
}
|
||||
if got := types.DefaultParserEngine("pdf"); got != "" {
|
||||
t.Fatalf("DefaultParserEngine(pdf) = %q, want empty when anydoc is unavailable", got)
|
||||
}
|
||||
if got := types.DefaultParserEngine("docx"); got != "" {
|
||||
t.Fatalf("DefaultParserEngine(docx) = %q, want empty when anydoc is unavailable", got)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,9 +43,11 @@ func init() {
|
|||
|
||||
// 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.
|
||||
// builtin or the markitdown fallback. Simple formats stay on the Go reader.
|
||||
func preferAnydocWhenAvailable(fileType string) string {
|
||||
if IsSimpleFormat(fileType) {
|
||||
return ""
|
||||
}
|
||||
if anydoc.Available() && anydoc.Supports(fileType, "") {
|
||||
return AnydocEngineName
|
||||
}
|
||||
|
|
|
|||
|
|
@ -250,8 +250,9 @@ type ChunkingConfig struct {
|
|||
Separators []string `yaml:"separators" json:"separators"`
|
||||
// ParserEngineRules configures which parser engine to use for each file type.
|
||||
// When empty, DefaultParserEngine is used (builtin/simple routing, except
|
||||
// types that only a specific engine can parse: ppt/pptx prefer anydoc
|
||||
// when that binding is linked, otherwise markitdown).
|
||||
// types that only a specific engine can parse: ppt/pptx fall back to
|
||||
// markitdown). A linked anydoc binding is preferred for every type it
|
||||
// converts.
|
||||
ParserEngineRules []ParserEngineRule `yaml:"parser_engine_rules,omitempty" json:"parser_engine_rules,omitempty"`
|
||||
// EnableParentChild enables two-level parent-child chunking strategy.
|
||||
// When enabled, large parent chunks provide context while small child chunks
|
||||
|
|
@ -282,17 +283,16 @@ type ChunkingConfig struct {
|
|||
|
||||
// defaultParserEngineByType maps file types the builtin/simple engines cannot
|
||||
// parse to a docreader engine that can. Types omitted here keep empty-string
|
||||
// routing (Go simple formats, otherwise docreader builtin). Anydoc, when
|
||||
// linked into the binary, is preferred over these fallbacks — see
|
||||
// SetPreferParserEngine.
|
||||
// routing (Go simple formats, otherwise docreader builtin) unless
|
||||
// SetPreferParserEngine selects a linked in-process engine such as anydoc.
|
||||
var defaultParserEngineByType = map[string]string{
|
||||
"ppt": "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.
|
||||
// preferParserEngine, if set, may override DefaultParserEngine. 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
|
||||
|
|
@ -303,20 +303,17 @@ func SetPreferParserEngine(fn func(fileType string) string) {
|
|||
|
||||
// DefaultParserEngine returns the engine used when no parser_engine_rules
|
||||
// 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.
|
||||
// When the anydoc binding is linked it is preferred for every type it
|
||||
// converts (except Go simple formats). ppt/pptx otherwise fall back to
|
||||
// markitdown.
|
||||
func DefaultParserEngine(fileType string) string {
|
||||
ft := normalizeParserFileType(fileType)
|
||||
fallback := defaultParserEngineByType[ft]
|
||||
if fallback == "" {
|
||||
return ""
|
||||
}
|
||||
if preferParserEngine != nil {
|
||||
if engine := preferParserEngine(ft); engine != "" {
|
||||
return engine
|
||||
}
|
||||
}
|
||||
return fallback
|
||||
return defaultParserEngineByType[ft]
|
||||
}
|
||||
|
||||
// ResolveParserEngine returns the engine name for the given file type
|
||||
|
|
|
|||
|
|
@ -330,10 +330,10 @@ 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" {
|
||||
if fileType == "pptx" || fileType == "ppt" || fileType == "pdf" || fileType == "docx" {
|
||||
return "anydoc"
|
||||
}
|
||||
return "should-not-apply"
|
||||
return ""
|
||||
})
|
||||
if got := DefaultParserEngine("pptx"); got != "anydoc" {
|
||||
t.Fatalf("DefaultParserEngine(pptx) = %q, want anydoc", got)
|
||||
|
|
@ -341,8 +341,14 @@ func TestDefaultParserEnginePrefersRegisteredEngineForFallbackTypes(t *testing.T
|
|||
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)
|
||||
if got := DefaultParserEngine("pdf"); got != "anydoc" {
|
||||
t.Fatalf("DefaultParserEngine(pdf) = %q, want anydoc", got)
|
||||
}
|
||||
if got := DefaultParserEngine("docx"); got != "anydoc" {
|
||||
t.Fatalf("DefaultParserEngine(docx) = %q, want anydoc", got)
|
||||
}
|
||||
if got := DefaultParserEngine("txt"); got != "" {
|
||||
t.Fatalf("DefaultParserEngine(txt) = %q, want empty", got)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue