kreuzberg 4.0.8 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (308) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +2 -2
  3. data/README.md +1 -1
  4. data/ext/kreuzberg_rb/native/Cargo.lock +94 -98
  5. data/ext/kreuzberg_rb/native/Cargo.toml +4 -2
  6. data/ext/kreuzberg_rb/native/src/batch.rs +139 -0
  7. data/ext/kreuzberg_rb/native/src/config/mod.rs +10 -0
  8. data/ext/kreuzberg_rb/native/src/config/types.rs +1058 -0
  9. data/ext/kreuzberg_rb/native/src/error_handling.rs +125 -0
  10. data/ext/kreuzberg_rb/native/src/extraction.rs +79 -0
  11. data/ext/kreuzberg_rb/native/src/gc_guarded_value.rs +35 -0
  12. data/ext/kreuzberg_rb/native/src/helpers.rs +176 -0
  13. data/ext/kreuzberg_rb/native/src/lib.rs +342 -3622
  14. data/ext/kreuzberg_rb/native/src/metadata.rs +34 -0
  15. data/ext/kreuzberg_rb/native/src/plugins/mod.rs +92 -0
  16. data/ext/kreuzberg_rb/native/src/plugins/ocr_backend.rs +159 -0
  17. data/ext/kreuzberg_rb/native/src/plugins/post_processor.rs +126 -0
  18. data/ext/kreuzberg_rb/native/src/plugins/validator.rs +99 -0
  19. data/ext/kreuzberg_rb/native/src/result.rs +326 -0
  20. data/ext/kreuzberg_rb/native/src/validation.rs +4 -0
  21. data/lib/kreuzberg/config.rb +66 -0
  22. data/lib/kreuzberg/result.rb +107 -2
  23. data/lib/kreuzberg/types.rb +104 -0
  24. data/lib/kreuzberg/version.rb +1 -1
  25. data/lib/kreuzberg.rb +0 -4
  26. data/sig/kreuzberg.rbs +105 -1
  27. data/vendor/Cargo.toml +3 -3
  28. data/vendor/kreuzberg/Cargo.toml +4 -3
  29. data/vendor/kreuzberg/README.md +1 -1
  30. data/vendor/kreuzberg/src/api/config.rs +69 -0
  31. data/vendor/kreuzberg/src/api/handlers.rs +99 -2
  32. data/vendor/kreuzberg/src/api/mod.rs +14 -7
  33. data/vendor/kreuzberg/src/api/router.rs +214 -0
  34. data/vendor/kreuzberg/src/api/startup.rs +243 -0
  35. data/vendor/kreuzberg/src/api/types.rs +78 -0
  36. data/vendor/kreuzberg/src/cache/cleanup.rs +277 -0
  37. data/vendor/kreuzberg/src/cache/core.rs +428 -0
  38. data/vendor/kreuzberg/src/cache/mod.rs +21 -843
  39. data/vendor/kreuzberg/src/cache/utilities.rs +156 -0
  40. data/vendor/kreuzberg/src/chunking/boundaries.rs +301 -0
  41. data/vendor/kreuzberg/src/chunking/builder.rs +294 -0
  42. data/vendor/kreuzberg/src/chunking/config.rs +52 -0
  43. data/vendor/kreuzberg/src/chunking/core.rs +1017 -0
  44. data/vendor/kreuzberg/src/chunking/mod.rs +14 -2211
  45. data/vendor/kreuzberg/src/chunking/processor.rs +10 -0
  46. data/vendor/kreuzberg/src/chunking/validation.rs +686 -0
  47. data/vendor/kreuzberg/src/core/config/extraction/core.rs +169 -0
  48. data/vendor/kreuzberg/src/core/config/extraction/env.rs +179 -0
  49. data/vendor/kreuzberg/src/core/config/extraction/loaders.rs +204 -0
  50. data/vendor/kreuzberg/src/core/config/extraction/mod.rs +42 -0
  51. data/vendor/kreuzberg/src/core/config/extraction/types.rs +93 -0
  52. data/vendor/kreuzberg/src/core/config/formats.rs +135 -0
  53. data/vendor/kreuzberg/src/core/config/mod.rs +20 -0
  54. data/vendor/kreuzberg/src/core/config/ocr.rs +73 -0
  55. data/vendor/kreuzberg/src/core/config/page.rs +57 -0
  56. data/vendor/kreuzberg/src/core/config/pdf.rs +111 -0
  57. data/vendor/kreuzberg/src/core/config/processing.rs +312 -0
  58. data/vendor/kreuzberg/src/core/config_validation/dependencies.rs +187 -0
  59. data/vendor/kreuzberg/src/core/config_validation/mod.rs +386 -0
  60. data/vendor/kreuzberg/src/core/config_validation/sections.rs +401 -0
  61. data/vendor/kreuzberg/src/core/extractor/batch.rs +246 -0
  62. data/vendor/kreuzberg/src/core/extractor/bytes.rs +116 -0
  63. data/vendor/kreuzberg/src/core/extractor/file.rs +240 -0
  64. data/vendor/kreuzberg/src/core/extractor/helpers.rs +71 -0
  65. data/vendor/kreuzberg/src/core/extractor/legacy.rs +62 -0
  66. data/vendor/kreuzberg/src/core/extractor/mod.rs +490 -0
  67. data/vendor/kreuzberg/src/core/extractor/sync.rs +208 -0
  68. data/vendor/kreuzberg/src/core/mod.rs +4 -1
  69. data/vendor/kreuzberg/src/core/pipeline/cache.rs +60 -0
  70. data/vendor/kreuzberg/src/core/pipeline/execution.rs +89 -0
  71. data/vendor/kreuzberg/src/core/pipeline/features.rs +108 -0
  72. data/vendor/kreuzberg/src/core/pipeline/format.rs +392 -0
  73. data/vendor/kreuzberg/src/core/pipeline/initialization.rs +67 -0
  74. data/vendor/kreuzberg/src/core/pipeline/mod.rs +135 -0
  75. data/vendor/kreuzberg/src/core/pipeline/tests.rs +975 -0
  76. data/vendor/kreuzberg/src/core/server_config/env.rs +90 -0
  77. data/vendor/kreuzberg/src/core/server_config/loader.rs +202 -0
  78. data/vendor/kreuzberg/src/core/server_config/mod.rs +380 -0
  79. data/vendor/kreuzberg/src/core/server_config/tests/basic_tests.rs +124 -0
  80. data/vendor/kreuzberg/src/core/server_config/tests/env_tests.rs +216 -0
  81. data/vendor/kreuzberg/src/core/server_config/tests/file_loading_tests.rs +341 -0
  82. data/vendor/kreuzberg/src/core/server_config/tests/mod.rs +5 -0
  83. data/vendor/kreuzberg/src/core/server_config/validation.rs +17 -0
  84. data/vendor/kreuzberg/src/embeddings.rs +136 -13
  85. data/vendor/kreuzberg/src/extraction/{archive.rs → archive/mod.rs} +45 -239
  86. data/vendor/kreuzberg/src/extraction/archive/sevenz.rs +98 -0
  87. data/vendor/kreuzberg/src/extraction/archive/tar.rs +118 -0
  88. data/vendor/kreuzberg/src/extraction/archive/zip.rs +101 -0
  89. data/vendor/kreuzberg/src/extraction/html/converter.rs +592 -0
  90. data/vendor/kreuzberg/src/extraction/html/image_handling.rs +95 -0
  91. data/vendor/kreuzberg/src/extraction/html/mod.rs +53 -0
  92. data/vendor/kreuzberg/src/extraction/html/processor.rs +659 -0
  93. data/vendor/kreuzberg/src/extraction/html/stack_management.rs +103 -0
  94. data/vendor/kreuzberg/src/extraction/html/types.rs +28 -0
  95. data/vendor/kreuzberg/src/extraction/mod.rs +6 -2
  96. data/vendor/kreuzberg/src/extraction/pptx/container.rs +159 -0
  97. data/vendor/kreuzberg/src/extraction/pptx/content_builder.rs +168 -0
  98. data/vendor/kreuzberg/src/extraction/pptx/elements.rs +132 -0
  99. data/vendor/kreuzberg/src/extraction/pptx/image_handling.rs +57 -0
  100. data/vendor/kreuzberg/src/extraction/pptx/metadata.rs +160 -0
  101. data/vendor/kreuzberg/src/extraction/pptx/mod.rs +558 -0
  102. data/vendor/kreuzberg/src/extraction/pptx/parser.rs +379 -0
  103. data/vendor/kreuzberg/src/extraction/transform/content.rs +205 -0
  104. data/vendor/kreuzberg/src/extraction/transform/elements.rs +211 -0
  105. data/vendor/kreuzberg/src/extraction/transform/mod.rs +480 -0
  106. data/vendor/kreuzberg/src/extraction/transform/types.rs +27 -0
  107. data/vendor/kreuzberg/src/extractors/archive.rs +2 -0
  108. data/vendor/kreuzberg/src/extractors/bibtex.rs +2 -0
  109. data/vendor/kreuzberg/src/extractors/djot_format/attributes.rs +134 -0
  110. data/vendor/kreuzberg/src/extractors/djot_format/conversion.rs +223 -0
  111. data/vendor/kreuzberg/src/extractors/djot_format/extractor.rs +172 -0
  112. data/vendor/kreuzberg/src/extractors/djot_format/mod.rs +24 -0
  113. data/vendor/kreuzberg/src/extractors/djot_format/parsing/block_handlers.rs +271 -0
  114. data/vendor/kreuzberg/src/extractors/djot_format/parsing/content_extraction.rs +257 -0
  115. data/vendor/kreuzberg/src/extractors/djot_format/parsing/event_handlers.rs +101 -0
  116. data/vendor/kreuzberg/src/extractors/djot_format/parsing/inline_handlers.rs +201 -0
  117. data/vendor/kreuzberg/src/extractors/djot_format/parsing/mod.rs +16 -0
  118. data/vendor/kreuzberg/src/extractors/djot_format/parsing/state.rs +78 -0
  119. data/vendor/kreuzberg/src/extractors/djot_format/parsing/table_extraction.rs +68 -0
  120. data/vendor/kreuzberg/src/extractors/djot_format/parsing/text_extraction.rs +61 -0
  121. data/vendor/kreuzberg/src/extractors/djot_format/rendering.rs +452 -0
  122. data/vendor/kreuzberg/src/extractors/docbook.rs +2 -0
  123. data/vendor/kreuzberg/src/extractors/docx.rs +12 -1
  124. data/vendor/kreuzberg/src/extractors/email.rs +2 -0
  125. data/vendor/kreuzberg/src/extractors/epub/content.rs +333 -0
  126. data/vendor/kreuzberg/src/extractors/epub/metadata.rs +137 -0
  127. data/vendor/kreuzberg/src/extractors/epub/mod.rs +186 -0
  128. data/vendor/kreuzberg/src/extractors/epub/parsing.rs +86 -0
  129. data/vendor/kreuzberg/src/extractors/excel.rs +4 -0
  130. data/vendor/kreuzberg/src/extractors/fictionbook.rs +2 -0
  131. data/vendor/kreuzberg/src/extractors/frontmatter_utils.rs +466 -0
  132. data/vendor/kreuzberg/src/extractors/html.rs +80 -8
  133. data/vendor/kreuzberg/src/extractors/image.rs +8 -1
  134. data/vendor/kreuzberg/src/extractors/jats/elements.rs +350 -0
  135. data/vendor/kreuzberg/src/extractors/jats/metadata.rs +21 -0
  136. data/vendor/kreuzberg/src/extractors/{jats.rs → jats/mod.rs} +10 -412
  137. data/vendor/kreuzberg/src/extractors/jats/parser.rs +52 -0
  138. data/vendor/kreuzberg/src/extractors/jupyter.rs +2 -0
  139. data/vendor/kreuzberg/src/extractors/latex/commands.rs +93 -0
  140. data/vendor/kreuzberg/src/extractors/latex/environments.rs +157 -0
  141. data/vendor/kreuzberg/src/extractors/latex/metadata.rs +27 -0
  142. data/vendor/kreuzberg/src/extractors/latex/mod.rs +146 -0
  143. data/vendor/kreuzberg/src/extractors/latex/parser.rs +231 -0
  144. data/vendor/kreuzberg/src/extractors/latex/utilities.rs +126 -0
  145. data/vendor/kreuzberg/src/extractors/markdown.rs +39 -162
  146. data/vendor/kreuzberg/src/extractors/mod.rs +9 -1
  147. data/vendor/kreuzberg/src/extractors/odt.rs +2 -0
  148. data/vendor/kreuzberg/src/extractors/opml/core.rs +165 -0
  149. data/vendor/kreuzberg/src/extractors/opml/mod.rs +31 -0
  150. data/vendor/kreuzberg/src/extractors/opml/parser.rs +479 -0
  151. data/vendor/kreuzberg/src/extractors/orgmode.rs +2 -0
  152. data/vendor/kreuzberg/src/extractors/pdf/extraction.rs +106 -0
  153. data/vendor/kreuzberg/src/extractors/{pdf.rs → pdf/mod.rs} +25 -324
  154. data/vendor/kreuzberg/src/extractors/pdf/ocr.rs +214 -0
  155. data/vendor/kreuzberg/src/extractors/pdf/pages.rs +51 -0
  156. data/vendor/kreuzberg/src/extractors/pptx.rs +9 -2
  157. data/vendor/kreuzberg/src/extractors/rst.rs +2 -0
  158. data/vendor/kreuzberg/src/extractors/rtf/encoding.rs +116 -0
  159. data/vendor/kreuzberg/src/extractors/rtf/formatting.rs +24 -0
  160. data/vendor/kreuzberg/src/extractors/rtf/images.rs +72 -0
  161. data/vendor/kreuzberg/src/extractors/rtf/metadata.rs +216 -0
  162. data/vendor/kreuzberg/src/extractors/rtf/mod.rs +142 -0
  163. data/vendor/kreuzberg/src/extractors/rtf/parser.rs +259 -0
  164. data/vendor/kreuzberg/src/extractors/rtf/tables.rs +83 -0
  165. data/vendor/kreuzberg/src/extractors/structured.rs +2 -0
  166. data/vendor/kreuzberg/src/extractors/text.rs +4 -0
  167. data/vendor/kreuzberg/src/extractors/typst.rs +2 -0
  168. data/vendor/kreuzberg/src/extractors/xml.rs +2 -0
  169. data/vendor/kreuzberg/src/keywords/processor.rs +14 -0
  170. data/vendor/kreuzberg/src/language_detection/processor.rs +10 -0
  171. data/vendor/kreuzberg/src/lib.rs +2 -2
  172. data/vendor/kreuzberg/src/mcp/errors.rs +312 -0
  173. data/vendor/kreuzberg/src/mcp/format.rs +211 -0
  174. data/vendor/kreuzberg/src/mcp/mod.rs +9 -3
  175. data/vendor/kreuzberg/src/mcp/params.rs +196 -0
  176. data/vendor/kreuzberg/src/mcp/server.rs +39 -1438
  177. data/vendor/kreuzberg/src/mcp/tools/cache.rs +179 -0
  178. data/vendor/kreuzberg/src/mcp/tools/extraction.rs +403 -0
  179. data/vendor/kreuzberg/src/mcp/tools/mime.rs +150 -0
  180. data/vendor/kreuzberg/src/mcp/tools/mod.rs +11 -0
  181. data/vendor/kreuzberg/src/ocr/backends/easyocr.rs +96 -0
  182. data/vendor/kreuzberg/src/ocr/backends/mod.rs +7 -0
  183. data/vendor/kreuzberg/src/ocr/backends/paddleocr.rs +27 -0
  184. data/vendor/kreuzberg/src/ocr/backends/tesseract.rs +134 -0
  185. data/vendor/kreuzberg/src/ocr/hocr.rs +60 -16
  186. data/vendor/kreuzberg/src/ocr/language_registry.rs +11 -235
  187. data/vendor/kreuzberg/src/ocr/mod.rs +1 -0
  188. data/vendor/kreuzberg/src/ocr/processor/config.rs +203 -0
  189. data/vendor/kreuzberg/src/ocr/processor/execution.rs +494 -0
  190. data/vendor/kreuzberg/src/ocr/processor/mod.rs +265 -0
  191. data/vendor/kreuzberg/src/ocr/processor/validation.rs +145 -0
  192. data/vendor/kreuzberg/src/ocr/tesseract_backend.rs +41 -24
  193. data/vendor/kreuzberg/src/pdf/bindings.rs +21 -8
  194. data/vendor/kreuzberg/src/pdf/hierarchy/bounding_box.rs +289 -0
  195. data/vendor/kreuzberg/src/pdf/hierarchy/clustering.rs +199 -0
  196. data/vendor/kreuzberg/src/pdf/{hierarchy.rs → hierarchy/extraction.rs} +6 -346
  197. data/vendor/kreuzberg/src/pdf/hierarchy/mod.rs +18 -0
  198. data/vendor/kreuzberg/src/plugins/extractor/mod.rs +319 -0
  199. data/vendor/kreuzberg/src/plugins/extractor/registry.rs +434 -0
  200. data/vendor/kreuzberg/src/plugins/extractor/trait.rs +391 -0
  201. data/vendor/kreuzberg/src/plugins/mod.rs +13 -0
  202. data/vendor/kreuzberg/src/plugins/ocr.rs +11 -0
  203. data/vendor/kreuzberg/src/plugins/processor/mod.rs +365 -0
  204. data/vendor/kreuzberg/src/plugins/processor/registry.rs +37 -0
  205. data/vendor/kreuzberg/src/plugins/processor/trait.rs +284 -0
  206. data/vendor/kreuzberg/src/plugins/registry/extractor.rs +416 -0
  207. data/vendor/kreuzberg/src/plugins/registry/mod.rs +116 -0
  208. data/vendor/kreuzberg/src/plugins/registry/ocr.rs +293 -0
  209. data/vendor/kreuzberg/src/plugins/registry/processor.rs +304 -0
  210. data/vendor/kreuzberg/src/plugins/registry/validator.rs +238 -0
  211. data/vendor/kreuzberg/src/plugins/validator/mod.rs +424 -0
  212. data/vendor/kreuzberg/src/plugins/validator/registry.rs +355 -0
  213. data/vendor/kreuzberg/src/plugins/validator/trait.rs +276 -0
  214. data/vendor/kreuzberg/src/stopwords/languages/asian.rs +40 -0
  215. data/vendor/kreuzberg/src/stopwords/languages/germanic.rs +36 -0
  216. data/vendor/kreuzberg/src/stopwords/languages/mod.rs +10 -0
  217. data/vendor/kreuzberg/src/stopwords/languages/other.rs +44 -0
  218. data/vendor/kreuzberg/src/stopwords/languages/romance.rs +36 -0
  219. data/vendor/kreuzberg/src/stopwords/languages/slavic.rs +36 -0
  220. data/vendor/kreuzberg/src/stopwords/mod.rs +7 -33
  221. data/vendor/kreuzberg/src/text/quality.rs +1 -1
  222. data/vendor/kreuzberg/src/text/quality_processor.rs +10 -0
  223. data/vendor/kreuzberg/src/text/token_reduction/core/analysis.rs +238 -0
  224. data/vendor/kreuzberg/src/text/token_reduction/core/mod.rs +8 -0
  225. data/vendor/kreuzberg/src/text/token_reduction/core/punctuation.rs +54 -0
  226. data/vendor/kreuzberg/src/text/token_reduction/core/reducer.rs +384 -0
  227. data/vendor/kreuzberg/src/text/token_reduction/core/sentence_selection.rs +68 -0
  228. data/vendor/kreuzberg/src/text/token_reduction/core/word_filtering.rs +156 -0
  229. data/vendor/kreuzberg/src/text/token_reduction/filters/general.rs +377 -0
  230. data/vendor/kreuzberg/src/text/token_reduction/filters/html.rs +51 -0
  231. data/vendor/kreuzberg/src/text/token_reduction/filters/markdown.rs +285 -0
  232. data/vendor/kreuzberg/src/text/token_reduction/filters.rs +131 -246
  233. data/vendor/kreuzberg/src/types/djot.rs +209 -0
  234. data/vendor/kreuzberg/src/types/extraction.rs +301 -0
  235. data/vendor/kreuzberg/src/types/formats.rs +443 -0
  236. data/vendor/kreuzberg/src/types/metadata.rs +560 -0
  237. data/vendor/kreuzberg/src/types/mod.rs +281 -0
  238. data/vendor/kreuzberg/src/types/page.rs +182 -0
  239. data/vendor/kreuzberg/src/types/serde_helpers.rs +132 -0
  240. data/vendor/kreuzberg/src/types/tables.rs +39 -0
  241. data/vendor/kreuzberg/src/utils/quality/heuristics.rs +58 -0
  242. data/vendor/kreuzberg/src/utils/{quality.rs → quality/mod.rs} +168 -489
  243. data/vendor/kreuzberg/src/utils/quality/patterns.rs +117 -0
  244. data/vendor/kreuzberg/src/utils/quality/scoring.rs +178 -0
  245. data/vendor/kreuzberg/src/utils/string_pool/buffer_pool.rs +325 -0
  246. data/vendor/kreuzberg/src/utils/string_pool/interned.rs +102 -0
  247. data/vendor/kreuzberg/src/utils/string_pool/language_pool.rs +119 -0
  248. data/vendor/kreuzberg/src/utils/string_pool/mime_pool.rs +235 -0
  249. data/vendor/kreuzberg/src/utils/string_pool/mod.rs +41 -0
  250. data/vendor/kreuzberg/tests/api_chunk.rs +313 -0
  251. data/vendor/kreuzberg/tests/api_embed.rs +6 -9
  252. data/vendor/kreuzberg/tests/batch_orchestration.rs +1 -0
  253. data/vendor/kreuzberg/tests/concurrency_stress.rs +7 -0
  254. data/vendor/kreuzberg/tests/core_integration.rs +1 -0
  255. data/vendor/kreuzberg/tests/docx_metadata_extraction_test.rs +130 -0
  256. data/vendor/kreuzberg/tests/epub_native_extractor_tests.rs +5 -14
  257. data/vendor/kreuzberg/tests/format_integration.rs +2 -0
  258. data/vendor/kreuzberg/tests/helpers/mod.rs +1 -0
  259. data/vendor/kreuzberg/tests/html_table_test.rs +11 -11
  260. data/vendor/kreuzberg/tests/ocr_configuration.rs +16 -0
  261. data/vendor/kreuzberg/tests/ocr_errors.rs +18 -0
  262. data/vendor/kreuzberg/tests/ocr_quality.rs +9 -0
  263. data/vendor/kreuzberg/tests/ocr_stress.rs +1 -0
  264. data/vendor/kreuzberg/tests/pipeline_integration.rs +50 -0
  265. data/vendor/kreuzberg/tests/plugin_ocr_backend_test.rs +13 -0
  266. data/vendor/kreuzberg/tests/plugin_system.rs +12 -0
  267. data/vendor/kreuzberg/tests/registry_integration_tests.rs +2 -0
  268. data/vendor/kreuzberg-ffi/Cargo.toml +2 -1
  269. data/vendor/kreuzberg-ffi/benches/result_view_benchmark.rs +2 -0
  270. data/vendor/kreuzberg-ffi/kreuzberg.h +347 -178
  271. data/vendor/kreuzberg-ffi/src/config/html.rs +318 -0
  272. data/vendor/kreuzberg-ffi/src/config/loader.rs +154 -0
  273. data/vendor/kreuzberg-ffi/src/config/merge.rs +104 -0
  274. data/vendor/kreuzberg-ffi/src/config/mod.rs +385 -0
  275. data/vendor/kreuzberg-ffi/src/config/parse.rs +91 -0
  276. data/vendor/kreuzberg-ffi/src/config/serialize.rs +118 -0
  277. data/vendor/kreuzberg-ffi/src/config_builder.rs +598 -0
  278. data/vendor/kreuzberg-ffi/src/error.rs +46 -14
  279. data/vendor/kreuzberg-ffi/src/helpers.rs +10 -0
  280. data/vendor/kreuzberg-ffi/src/html_options.rs +421 -0
  281. data/vendor/kreuzberg-ffi/src/lib.rs +16 -0
  282. data/vendor/kreuzberg-ffi/src/panic_shield.rs +11 -0
  283. data/vendor/kreuzberg-ffi/src/plugins/ocr_backend.rs +2 -0
  284. data/vendor/kreuzberg-ffi/src/result.rs +148 -122
  285. data/vendor/kreuzberg-ffi/src/result_view.rs +4 -0
  286. data/vendor/kreuzberg-tesseract/Cargo.toml +2 -2
  287. metadata +200 -28
  288. data/vendor/kreuzberg/src/api/server.rs +0 -518
  289. data/vendor/kreuzberg/src/core/config.rs +0 -1914
  290. data/vendor/kreuzberg/src/core/config_validation.rs +0 -949
  291. data/vendor/kreuzberg/src/core/extractor.rs +0 -1200
  292. data/vendor/kreuzberg/src/core/pipeline.rs +0 -1223
  293. data/vendor/kreuzberg/src/core/server_config.rs +0 -1220
  294. data/vendor/kreuzberg/src/extraction/html.rs +0 -1830
  295. data/vendor/kreuzberg/src/extraction/pptx.rs +0 -3102
  296. data/vendor/kreuzberg/src/extractors/epub.rs +0 -696
  297. data/vendor/kreuzberg/src/extractors/latex.rs +0 -653
  298. data/vendor/kreuzberg/src/extractors/opml.rs +0 -635
  299. data/vendor/kreuzberg/src/extractors/rtf.rs +0 -809
  300. data/vendor/kreuzberg/src/ocr/processor.rs +0 -858
  301. data/vendor/kreuzberg/src/plugins/extractor.rs +0 -1042
  302. data/vendor/kreuzberg/src/plugins/processor.rs +0 -650
  303. data/vendor/kreuzberg/src/plugins/registry.rs +0 -1339
  304. data/vendor/kreuzberg/src/plugins/validator.rs +0 -967
  305. data/vendor/kreuzberg/src/text/token_reduction/core.rs +0 -832
  306. data/vendor/kreuzberg/src/types.rs +0 -1713
  307. data/vendor/kreuzberg/src/utils/string_pool.rs +0 -762
  308. data/vendor/kreuzberg-ffi/src/config.rs +0 -1341
@@ -1,1223 +0,0 @@
1
- //! Post-processing pipeline orchestration.
2
- //!
3
- //! This module orchestrates the post-processing pipeline, executing validators,
4
- //! quality processing, chunking, and custom hooks in the correct order.
5
-
6
- use crate::core::config::ExtractionConfig;
7
- use crate::plugins::{PostProcessor, ProcessingStage};
8
- use crate::types::ExtractionResult;
9
- use crate::{KreuzbergError, Result};
10
- use once_cell::sync::Lazy;
11
- use std::sync::Arc;
12
- use std::sync::RwLock as StdRwLock;
13
-
14
- /// Cached post-processors for each stage to reduce lock contention.
15
- ///
16
- /// This cache is populated once during the first pipeline run and reused
17
- /// for all subsequent extractions, eliminating 3 of 4 registry lock acquisitions
18
- /// per extraction.
19
- struct ProcessorCache {
20
- early: Arc<Vec<Arc<dyn PostProcessor>>>,
21
- middle: Arc<Vec<Arc<dyn PostProcessor>>>,
22
- late: Arc<Vec<Arc<dyn PostProcessor>>>,
23
- }
24
-
25
- impl ProcessorCache {
26
- /// Create a new processor cache by fetching from the registry.
27
- fn new() -> Result<Self> {
28
- let processor_registry = crate::plugins::registry::get_post_processor_registry();
29
- let registry = processor_registry
30
- .read()
31
- .map_err(|e| crate::KreuzbergError::Other(format!("Post-processor registry lock poisoned: {}", e)))?;
32
-
33
- Ok(Self {
34
- early: Arc::new(registry.get_for_stage(ProcessingStage::Early)),
35
- middle: Arc::new(registry.get_for_stage(ProcessingStage::Middle)),
36
- late: Arc::new(registry.get_for_stage(ProcessingStage::Late)),
37
- })
38
- }
39
-
40
- /// Get processors for a specific stage from cache.
41
- #[allow(dead_code)]
42
- fn get_for_stage(&self, stage: ProcessingStage) -> Arc<Vec<Arc<dyn PostProcessor>>> {
43
- match stage {
44
- ProcessingStage::Early => Arc::clone(&self.early),
45
- ProcessingStage::Middle => Arc::clone(&self.middle),
46
- ProcessingStage::Late => Arc::clone(&self.late),
47
- }
48
- }
49
- }
50
-
51
- /// Lazy processor cache - initialized on first use, then cached.
52
- static PROCESSOR_CACHE: Lazy<StdRwLock<Option<ProcessorCache>>> = Lazy::new(|| StdRwLock::new(None));
53
-
54
- /// Clear the processor cache (primarily for testing when registry changes).
55
- #[allow(dead_code)]
56
- pub fn clear_processor_cache() -> Result<()> {
57
- let mut cache = PROCESSOR_CACHE
58
- .write()
59
- .map_err(|e| crate::KreuzbergError::Other(format!("Processor cache lock poisoned: {}", e)))?;
60
- *cache = None;
61
- Ok(())
62
- }
63
-
64
- /// Run the post-processing pipeline on an extraction result.
65
- ///
66
- /// Executes post-processing in the following order:
67
- /// 1. Post-Processors - Execute by stage (Early, Middle, Late) to modify/enhance the result
68
- /// 2. Quality Processing - Text cleaning and quality scoring
69
- /// 3. Chunking - Text splitting if enabled
70
- /// 4. Validators - Run validation hooks on the processed result (can fail fast)
71
- ///
72
- /// # Arguments
73
- ///
74
- /// * `result` - The extraction result to process
75
- /// * `config` - Extraction configuration
76
- ///
77
- /// # Returns
78
- ///
79
- /// The processed extraction result.
80
- ///
81
- /// # Errors
82
- ///
83
- /// - Validator errors bubble up immediately
84
- /// - Post-processor errors are caught and recorded in metadata
85
- /// - System errors (IO, RuntimeError equivalents) always bubble up
86
- #[cfg_attr(feature = "otel", tracing::instrument(
87
- skip(result, config),
88
- fields(
89
- pipeline.stage = "post_processing",
90
- content.length = result.content.len(),
91
- )
92
- ))]
93
- pub async fn run_pipeline(mut result: ExtractionResult, config: &ExtractionConfig) -> Result<ExtractionResult> {
94
- let pp_config = config.postprocessor.as_ref();
95
- let postprocessing_enabled = pp_config.is_none_or(|c| c.enabled);
96
-
97
- if postprocessing_enabled {
98
- #[cfg(any(feature = "keywords-yake", feature = "keywords-rake"))]
99
- {
100
- let _ = crate::keywords::ensure_initialized();
101
- }
102
-
103
- #[cfg(feature = "language-detection")]
104
- {
105
- let _ = crate::language_detection::ensure_initialized();
106
- }
107
-
108
- #[cfg(feature = "chunking")]
109
- {
110
- let _ = crate::chunking::ensure_initialized();
111
- }
112
-
113
- #[cfg(feature = "quality")]
114
- {
115
- let registry = crate::plugins::registry::get_post_processor_registry();
116
- if let Ok(mut reg) = registry.write() {
117
- let _ = reg.register(std::sync::Arc::new(crate::text::QualityProcessor), 30);
118
- }
119
- }
120
-
121
- {
122
- let mut cache_lock = PROCESSOR_CACHE
123
- .write()
124
- .map_err(|e| crate::KreuzbergError::Other(format!("Processor cache lock poisoned: {}", e)))?;
125
- if cache_lock.is_none() {
126
- *cache_lock = Some(ProcessorCache::new()?);
127
- }
128
- }
129
-
130
- let (early_processors, middle_processors, late_processors) = {
131
- let cache_lock = PROCESSOR_CACHE
132
- .read()
133
- .map_err(|e| crate::KreuzbergError::Other(format!("Processor cache lock poisoned: {}", e)))?;
134
- let cache = cache_lock
135
- .as_ref()
136
- .ok_or_else(|| crate::KreuzbergError::Other("Processor cache not initialized".to_string()))?;
137
- (
138
- Arc::clone(&cache.early),
139
- Arc::clone(&cache.middle),
140
- Arc::clone(&cache.late),
141
- )
142
- };
143
-
144
- for (_stage, processors_arc) in [
145
- (ProcessingStage::Early, early_processors),
146
- (ProcessingStage::Middle, middle_processors),
147
- (ProcessingStage::Late, late_processors),
148
- ] {
149
- for processor in processors_arc.iter() {
150
- let processor_name = processor.name();
151
-
152
- let should_run = if let Some(config) = pp_config {
153
- if let Some(ref enabled_set) = config.enabled_set {
154
- enabled_set.contains(processor_name)
155
- } else if let Some(ref disabled_set) = config.disabled_set {
156
- !disabled_set.contains(processor_name)
157
- } else if let Some(ref enabled) = config.enabled_processors {
158
- enabled.iter().any(|name| name == processor_name)
159
- } else if let Some(ref disabled) = config.disabled_processors {
160
- !disabled.iter().any(|name| name == processor_name)
161
- } else {
162
- true
163
- }
164
- } else {
165
- true
166
- };
167
-
168
- if should_run && processor.should_process(&result, config) {
169
- match processor.process(&mut result, config).await {
170
- Ok(_) => {}
171
- Err(err @ KreuzbergError::Io(_))
172
- | Err(err @ KreuzbergError::LockPoisoned(_))
173
- | Err(err @ KreuzbergError::Plugin { .. }) => {
174
- return Err(err);
175
- }
176
- Err(err) => {
177
- result.metadata.additional.insert(
178
- format!("processing_error_{processor_name}"),
179
- serde_json::Value::String(err.to_string()),
180
- );
181
- }
182
- }
183
- }
184
- }
185
- }
186
- }
187
-
188
- #[cfg(feature = "chunking")]
189
- if let Some(ref chunking_config) = config.chunking {
190
- let chunk_config = crate::chunking::ChunkingConfig {
191
- max_characters: chunking_config.max_chars,
192
- overlap: chunking_config.max_overlap,
193
- trim: true,
194
- chunker_type: crate::chunking::ChunkerType::Text,
195
- };
196
-
197
- let page_boundaries = result.metadata.pages.as_ref().and_then(|ps| ps.boundaries.as_deref());
198
-
199
- match crate::chunking::chunk_text(&result.content, &chunk_config, page_boundaries) {
200
- Ok(chunking_result) => {
201
- result.chunks = Some(chunking_result.chunks);
202
-
203
- if let Some(ref chunks) = result.chunks {
204
- result.metadata.additional.insert(
205
- "chunk_count".to_string(),
206
- serde_json::Value::Number(serde_json::Number::from(chunks.len())),
207
- );
208
- }
209
-
210
- #[cfg(feature = "embeddings")]
211
- if let Some(ref embedding_config) = chunking_config.embedding
212
- && let Some(ref mut chunks) = result.chunks
213
- {
214
- match crate::embeddings::generate_embeddings_for_chunks(chunks, embedding_config) {
215
- Ok(()) => {
216
- result
217
- .metadata
218
- .additional
219
- .insert("embeddings_generated".to_string(), serde_json::Value::Bool(true));
220
- }
221
- Err(e) => {
222
- result
223
- .metadata
224
- .additional
225
- .insert("embedding_error".to_string(), serde_json::Value::String(e.to_string()));
226
- }
227
- }
228
- }
229
-
230
- #[cfg(not(feature = "embeddings"))]
231
- if chunking_config.embedding.is_some() {
232
- result.metadata.additional.insert(
233
- "embedding_error".to_string(),
234
- serde_json::Value::String("Embeddings feature not enabled".to_string()),
235
- );
236
- }
237
- }
238
- Err(e) => {
239
- result
240
- .metadata
241
- .additional
242
- .insert("chunking_error".to_string(), serde_json::Value::String(e.to_string()));
243
- }
244
- }
245
- }
246
-
247
- #[cfg(not(feature = "chunking"))]
248
- if config.chunking.is_some() {
249
- result.metadata.additional.insert(
250
- "chunking_error".to_string(),
251
- serde_json::Value::String("Chunking feature not enabled".to_string()),
252
- );
253
- }
254
-
255
- #[cfg(feature = "language-detection")]
256
- if let Some(ref lang_config) = config.language_detection {
257
- match crate::language_detection::detect_languages(&result.content, lang_config) {
258
- Ok(detected) => {
259
- result.detected_languages = detected;
260
- }
261
- Err(e) => {
262
- result.metadata.additional.insert(
263
- "language_detection_error".to_string(),
264
- serde_json::Value::String(e.to_string()),
265
- );
266
- }
267
- }
268
- }
269
-
270
- #[cfg(not(feature = "language-detection"))]
271
- if config.language_detection.is_some() {
272
- result.metadata.additional.insert(
273
- "language_detection_error".to_string(),
274
- serde_json::Value::String("Language detection feature not enabled".to_string()),
275
- );
276
- }
277
-
278
- {
279
- let validator_registry = crate::plugins::registry::get_validator_registry();
280
- let validators = {
281
- let registry = validator_registry
282
- .read()
283
- .map_err(|e| crate::KreuzbergError::Other(format!("Validator registry lock poisoned: {}", e)))?;
284
- registry.get_all()
285
- };
286
-
287
- if !validators.is_empty() {
288
- for validator in validators {
289
- if validator.should_validate(&result, config) {
290
- validator.validate(&result, config).await?;
291
- }
292
- }
293
- }
294
- }
295
-
296
- Ok(result)
297
- }
298
-
299
- /// Run the post-processing pipeline synchronously (WASM-compatible version).
300
- ///
301
- /// This is a synchronous implementation for WASM and non-async contexts.
302
- /// It performs a subset of the full async pipeline, excluding async post-processors
303
- /// and validators.
304
- ///
305
- /// # Arguments
306
- ///
307
- /// * `result` - The extraction result to process
308
- /// * `config` - Extraction configuration
309
- ///
310
- /// # Returns
311
- ///
312
- /// The processed extraction result.
313
- ///
314
- /// # Notes
315
- ///
316
- /// This function is only available when the `tokio-runtime` feature is disabled.
317
- /// It handles:
318
- /// - Quality processing (if enabled)
319
- /// - Chunking (if enabled)
320
- /// - Language detection (if enabled)
321
- ///
322
- /// It does NOT handle:
323
- /// - Async post-processors
324
- /// - Async validators
325
- #[cfg(not(feature = "tokio-runtime"))]
326
- pub fn run_pipeline_sync(mut result: ExtractionResult, config: &ExtractionConfig) -> Result<ExtractionResult> {
327
- #[cfg(feature = "chunking")]
328
- if let Some(ref chunking_config) = config.chunking {
329
- let chunk_config = crate::chunking::ChunkingConfig {
330
- max_characters: chunking_config.max_chars,
331
- overlap: chunking_config.max_overlap,
332
- trim: true,
333
- chunker_type: crate::chunking::ChunkerType::Text,
334
- };
335
-
336
- match crate::chunking::chunk_text(&result.content, &chunk_config, None) {
337
- Ok(chunking_result) => {
338
- result.chunks = Some(chunking_result.chunks);
339
-
340
- if let Some(ref chunks) = result.chunks {
341
- result.metadata.additional.insert(
342
- "chunk_count".to_string(),
343
- serde_json::Value::Number(serde_json::Number::from(chunks.len())),
344
- );
345
- }
346
-
347
- #[cfg(feature = "embeddings")]
348
- if let Some(ref embedding_config) = chunking_config.embedding
349
- && let Some(ref mut chunks) = result.chunks
350
- {
351
- match crate::embeddings::generate_embeddings_for_chunks(chunks, embedding_config) {
352
- Ok(()) => {
353
- result
354
- .metadata
355
- .additional
356
- .insert("embeddings_generated".to_string(), serde_json::Value::Bool(true));
357
- }
358
- Err(e) => {
359
- result
360
- .metadata
361
- .additional
362
- .insert("embedding_error".to_string(), serde_json::Value::String(e.to_string()));
363
- }
364
- }
365
- }
366
-
367
- #[cfg(not(feature = "embeddings"))]
368
- if chunking_config.embedding.is_some() {
369
- result.metadata.additional.insert(
370
- "embedding_error".to_string(),
371
- serde_json::Value::String("Embeddings feature not enabled".to_string()),
372
- );
373
- }
374
- }
375
- Err(e) => {
376
- result
377
- .metadata
378
- .additional
379
- .insert("chunking_error".to_string(), serde_json::Value::String(e.to_string()));
380
- }
381
- }
382
- }
383
-
384
- #[cfg(not(feature = "chunking"))]
385
- if config.chunking.is_some() {
386
- result.metadata.additional.insert(
387
- "chunking_error".to_string(),
388
- serde_json::Value::String("Chunking feature not enabled".to_string()),
389
- );
390
- }
391
-
392
- #[cfg(feature = "language-detection")]
393
- if let Some(ref lang_config) = config.language_detection {
394
- match crate::language_detection::detect_languages(&result.content, lang_config) {
395
- Ok(detected) => {
396
- result.detected_languages = detected;
397
- }
398
- Err(e) => {
399
- result.metadata.additional.insert(
400
- "language_detection_error".to_string(),
401
- serde_json::Value::String(e.to_string()),
402
- );
403
- }
404
- }
405
- }
406
-
407
- #[cfg(not(feature = "language-detection"))]
408
- if config.language_detection.is_some() {
409
- result.metadata.additional.insert(
410
- "language_detection_error".to_string(),
411
- serde_json::Value::String("Language detection feature not enabled".to_string()),
412
- );
413
- }
414
-
415
- Ok(result)
416
- }
417
-
418
- #[cfg(test)]
419
- mod tests {
420
- use super::*;
421
- use crate::types::Metadata;
422
- use lazy_static::lazy_static;
423
-
424
- const VALIDATION_MARKER_KEY: &str = "registry_validation_marker";
425
- #[cfg(feature = "quality")]
426
- const QUALITY_VALIDATION_MARKER: &str = "quality_validation_test";
427
- const POSTPROCESSOR_VALIDATION_MARKER: &str = "postprocessor_validation_test";
428
- const ORDER_VALIDATION_MARKER: &str = "order_validation_test";
429
-
430
- lazy_static! {
431
- static ref REGISTRY_TEST_GUARD: std::sync::Mutex<()> = std::sync::Mutex::new(());
432
- }
433
-
434
- #[tokio::test]
435
- async fn test_run_pipeline_basic() {
436
- let mut result = ExtractionResult {
437
- content: "test".to_string(),
438
- mime_type: "text/plain".to_string(),
439
- metadata: Metadata::default(),
440
- tables: vec![],
441
- detected_languages: None,
442
- chunks: None,
443
- images: None,
444
- pages: None,
445
- };
446
- result.metadata.additional.insert(
447
- VALIDATION_MARKER_KEY.to_string(),
448
- serde_json::json!(ORDER_VALIDATION_MARKER),
449
- );
450
- let config = ExtractionConfig::default();
451
-
452
- let processed = run_pipeline(result, &config).await.unwrap();
453
- assert_eq!(processed.content, "test");
454
- }
455
-
456
- #[tokio::test]
457
- #[cfg(feature = "quality")]
458
- async fn test_pipeline_with_quality_processing() {
459
- let result = ExtractionResult {
460
- content: "This is a test document with some meaningful content.".to_string(),
461
- mime_type: "text/plain".to_string(),
462
- metadata: Metadata::default(),
463
- tables: vec![],
464
- detected_languages: None,
465
- chunks: None,
466
- images: None,
467
- pages: None,
468
- };
469
- let config = ExtractionConfig {
470
- enable_quality_processing: true,
471
- ..Default::default()
472
- };
473
-
474
- let processed = run_pipeline(result, &config).await.unwrap();
475
- assert!(processed.metadata.additional.contains_key("quality_score"));
476
- }
477
-
478
- #[tokio::test]
479
- async fn test_pipeline_without_quality_processing() {
480
- let result = ExtractionResult {
481
- content: "test".to_string(),
482
- mime_type: "text/plain".to_string(),
483
- metadata: Metadata::default(),
484
- tables: vec![],
485
- detected_languages: None,
486
- chunks: None,
487
- images: None,
488
- pages: None,
489
- };
490
- let config = ExtractionConfig {
491
- enable_quality_processing: false,
492
- ..Default::default()
493
- };
494
-
495
- let processed = run_pipeline(result, &config).await.unwrap();
496
- assert!(!processed.metadata.additional.contains_key("quality_score"));
497
- }
498
-
499
- #[tokio::test]
500
- #[cfg(feature = "chunking")]
501
- async fn test_pipeline_with_chunking() {
502
- let result = ExtractionResult {
503
- content: "This is a long text that should be chunked. ".repeat(100),
504
- mime_type: "text/plain".to_string(),
505
- metadata: Metadata::default(),
506
- tables: vec![],
507
- detected_languages: None,
508
- chunks: None,
509
- images: None,
510
- pages: None,
511
- };
512
- let config = ExtractionConfig {
513
- chunking: Some(crate::ChunkingConfig {
514
- max_chars: 500,
515
- max_overlap: 50,
516
- embedding: None,
517
- preset: None,
518
- }),
519
- ..Default::default()
520
- };
521
-
522
- let processed = run_pipeline(result, &config).await.unwrap();
523
- assert!(processed.metadata.additional.contains_key("chunk_count"));
524
- let chunk_count = processed.metadata.additional.get("chunk_count").unwrap();
525
- assert!(chunk_count.as_u64().unwrap() > 1);
526
- }
527
-
528
- #[tokio::test]
529
- async fn test_pipeline_without_chunking() {
530
- let result = ExtractionResult {
531
- content: "test".to_string(),
532
- mime_type: "text/plain".to_string(),
533
- metadata: Metadata::default(),
534
- tables: vec![],
535
- detected_languages: None,
536
- chunks: None,
537
- images: None,
538
- pages: None,
539
- };
540
- let config = ExtractionConfig {
541
- chunking: None,
542
- ..Default::default()
543
- };
544
-
545
- let processed = run_pipeline(result, &config).await.unwrap();
546
- assert!(!processed.metadata.additional.contains_key("chunk_count"));
547
- }
548
-
549
- #[tokio::test]
550
- async fn test_pipeline_preserves_metadata() {
551
- use std::collections::HashMap;
552
- let mut additional = HashMap::new();
553
- additional.insert("source".to_string(), serde_json::json!("test"));
554
- additional.insert("page".to_string(), serde_json::json!(1));
555
-
556
- let result = ExtractionResult {
557
- content: "test".to_string(),
558
- mime_type: "text/plain".to_string(),
559
- metadata: Metadata {
560
- additional,
561
- ..Default::default()
562
- },
563
- pages: None,
564
- tables: vec![],
565
- detected_languages: None,
566
- chunks: None,
567
- images: None,
568
- };
569
- let config = ExtractionConfig::default();
570
-
571
- let processed = run_pipeline(result, &config).await.unwrap();
572
- assert_eq!(
573
- processed.metadata.additional.get("source").unwrap(),
574
- &serde_json::json!("test")
575
- );
576
- assert_eq!(
577
- processed.metadata.additional.get("page").unwrap(),
578
- &serde_json::json!(1)
579
- );
580
- }
581
-
582
- #[tokio::test]
583
- async fn test_pipeline_preserves_tables() {
584
- use crate::types::Table;
585
-
586
- let table = Table {
587
- cells: vec![vec!["A".to_string(), "B".to_string()]],
588
- markdown: "| A | B |".to_string(),
589
- page_number: 0,
590
- };
591
-
592
- let result = ExtractionResult {
593
- content: "test".to_string(),
594
- mime_type: "text/plain".to_string(),
595
- metadata: Metadata::default(),
596
- tables: vec![table],
597
- detected_languages: None,
598
- chunks: None,
599
- images: None,
600
- pages: None,
601
- };
602
- let config = ExtractionConfig::default();
603
-
604
- let processed = run_pipeline(result, &config).await.unwrap();
605
- assert_eq!(processed.tables.len(), 1);
606
- assert_eq!(processed.tables[0].cells.len(), 1);
607
- }
608
-
609
- #[tokio::test]
610
- async fn test_pipeline_empty_content() {
611
- let _guard = REGISTRY_TEST_GUARD.lock().unwrap();
612
-
613
- {
614
- let registry = crate::plugins::registry::get_post_processor_registry();
615
- registry.write().unwrap().shutdown_all().unwrap();
616
- }
617
- {
618
- let registry = crate::plugins::registry::get_validator_registry();
619
- registry.write().unwrap().shutdown_all().unwrap();
620
- }
621
-
622
- let result = ExtractionResult {
623
- content: String::new(),
624
- mime_type: "text/plain".to_string(),
625
- metadata: Metadata::default(),
626
- tables: vec![],
627
- detected_languages: None,
628
- chunks: None,
629
- images: None,
630
- pages: None,
631
- };
632
- let config = ExtractionConfig::default();
633
-
634
- drop(_guard);
635
-
636
- let processed = run_pipeline(result, &config).await.unwrap();
637
- assert_eq!(processed.content, "");
638
- }
639
-
640
- #[tokio::test]
641
- #[cfg(feature = "chunking")]
642
- async fn test_pipeline_with_all_features() {
643
- let result = ExtractionResult {
644
- content: "This is a comprehensive test document. ".repeat(50),
645
- mime_type: "text/plain".to_string(),
646
- metadata: Metadata::default(),
647
- tables: vec![],
648
- detected_languages: None,
649
- chunks: None,
650
- images: None,
651
- pages: None,
652
- };
653
- let config = ExtractionConfig {
654
- enable_quality_processing: true,
655
- chunking: Some(crate::ChunkingConfig {
656
- max_chars: 500,
657
- max_overlap: 50,
658
- embedding: None,
659
- preset: None,
660
- }),
661
- ..Default::default()
662
- };
663
-
664
- let processed = run_pipeline(result, &config).await.unwrap();
665
- assert!(processed.metadata.additional.contains_key("quality_score"));
666
- assert!(processed.metadata.additional.contains_key("chunk_count"));
667
- }
668
-
669
- #[tokio::test]
670
- #[cfg(any(feature = "keywords-yake", feature = "keywords-rake"))]
671
- async fn test_pipeline_with_keyword_extraction() {
672
- {
673
- let _guard = REGISTRY_TEST_GUARD.lock().unwrap();
674
- crate::plugins::registry::get_validator_registry()
675
- .write()
676
- .unwrap()
677
- .shutdown_all()
678
- .unwrap();
679
- crate::plugins::registry::get_post_processor_registry()
680
- .write()
681
- .unwrap()
682
- .shutdown_all()
683
- .unwrap();
684
-
685
- let _ = crate::keywords::register_keyword_processor();
686
- }
687
-
688
- let result = ExtractionResult {
689
- content: r#"
690
- Machine learning is a branch of artificial intelligence that focuses on
691
- building systems that can learn from data. Deep learning is a subset of
692
- machine learning that uses neural networks with multiple layers.
693
- Natural language processing enables computers to understand human language.
694
- "#
695
- .to_string(),
696
- mime_type: "text/plain".to_string(),
697
- metadata: Metadata::default(),
698
- tables: vec![],
699
- detected_languages: None,
700
- chunks: None,
701
- images: None,
702
- pages: None,
703
- };
704
-
705
- #[cfg(feature = "keywords-yake")]
706
- let keyword_config = crate::keywords::KeywordConfig::yake();
707
-
708
- #[cfg(all(feature = "keywords-rake", not(feature = "keywords-yake")))]
709
- let keyword_config = crate::keywords::KeywordConfig::rake();
710
-
711
- let config = ExtractionConfig {
712
- keywords: Some(keyword_config),
713
- ..Default::default()
714
- };
715
-
716
- let processed = run_pipeline(result, &config).await.unwrap();
717
-
718
- assert!(processed.metadata.additional.contains_key("keywords"));
719
-
720
- let keywords_value = processed.metadata.additional.get("keywords").unwrap();
721
- assert!(keywords_value.is_array());
722
-
723
- let keywords = keywords_value.as_array().unwrap();
724
- assert!(!keywords.is_empty(), "Should have extracted keywords");
725
-
726
- let first_keyword = &keywords[0];
727
- assert!(first_keyword.is_object());
728
- assert!(first_keyword.get("text").is_some());
729
- assert!(first_keyword.get("score").is_some());
730
- assert!(first_keyword.get("algorithm").is_some());
731
- }
732
-
733
- #[tokio::test]
734
- #[cfg(any(feature = "keywords-yake", feature = "keywords-rake"))]
735
- async fn test_pipeline_without_keyword_config() {
736
- {
737
- let _guard = REGISTRY_TEST_GUARD.lock().unwrap();
738
- }
739
- let result = ExtractionResult {
740
- content: "Machine learning and artificial intelligence.".to_string(),
741
- mime_type: "text/plain".to_string(),
742
- metadata: Metadata::default(),
743
- tables: vec![],
744
- detected_languages: None,
745
- chunks: None,
746
- images: None,
747
- pages: None,
748
- };
749
-
750
- let config = ExtractionConfig {
751
- keywords: None,
752
- ..Default::default()
753
- };
754
-
755
- let processed = run_pipeline(result, &config).await.unwrap();
756
-
757
- assert!(!processed.metadata.additional.contains_key("keywords"));
758
- }
759
-
760
- #[tokio::test]
761
- #[cfg(any(feature = "keywords-yake", feature = "keywords-rake"))]
762
- async fn test_pipeline_keyword_extraction_short_content() {
763
- let _guard = REGISTRY_TEST_GUARD.lock().unwrap();
764
- crate::plugins::registry::get_validator_registry()
765
- .write()
766
- .unwrap()
767
- .shutdown_all()
768
- .unwrap();
769
- crate::plugins::registry::get_post_processor_registry()
770
- .write()
771
- .unwrap()
772
- .shutdown_all()
773
- .unwrap();
774
-
775
- let result = ExtractionResult {
776
- content: "Short text".to_string(),
777
- mime_type: "text/plain".to_string(),
778
- metadata: Metadata::default(),
779
- tables: vec![],
780
- detected_languages: None,
781
- chunks: None,
782
- images: None,
783
- pages: None,
784
- };
785
-
786
- #[cfg(feature = "keywords-yake")]
787
- let keyword_config = crate::keywords::KeywordConfig::yake();
788
-
789
- #[cfg(all(feature = "keywords-rake", not(feature = "keywords-yake")))]
790
- let keyword_config = crate::keywords::KeywordConfig::rake();
791
-
792
- let config = ExtractionConfig {
793
- keywords: Some(keyword_config),
794
- ..Default::default()
795
- };
796
-
797
- drop(_guard);
798
-
799
- let processed = run_pipeline(result, &config).await.unwrap();
800
-
801
- assert!(!processed.metadata.additional.contains_key("keywords"));
802
- }
803
-
804
- #[tokio::test]
805
- async fn test_postprocessor_runs_before_validator() {
806
- use crate::plugins::{Plugin, PostProcessor, ProcessingStage, Validator};
807
- use async_trait::async_trait;
808
- use std::sync::Arc;
809
-
810
- struct TestPostProcessor;
811
- impl Plugin for TestPostProcessor {
812
- fn name(&self) -> &str {
813
- "test-processor"
814
- }
815
- fn version(&self) -> String {
816
- "1.0.0".to_string()
817
- }
818
- fn initialize(&self) -> Result<()> {
819
- Ok(())
820
- }
821
- fn shutdown(&self) -> Result<()> {
822
- Ok(())
823
- }
824
- }
825
-
826
- #[async_trait]
827
- impl PostProcessor for TestPostProcessor {
828
- async fn process(&self, result: &mut ExtractionResult, _config: &ExtractionConfig) -> Result<()> {
829
- result
830
- .metadata
831
- .additional
832
- .insert("processed".to_string(), serde_json::json!(true));
833
- Ok(())
834
- }
835
-
836
- fn processing_stage(&self) -> ProcessingStage {
837
- ProcessingStage::Middle
838
- }
839
- }
840
-
841
- struct TestValidator;
842
- impl Plugin for TestValidator {
843
- fn name(&self) -> &str {
844
- "test-validator"
845
- }
846
- fn version(&self) -> String {
847
- "1.0.0".to_string()
848
- }
849
- fn initialize(&self) -> Result<()> {
850
- Ok(())
851
- }
852
- fn shutdown(&self) -> Result<()> {
853
- Ok(())
854
- }
855
- }
856
-
857
- #[async_trait]
858
- impl Validator for TestValidator {
859
- async fn validate(&self, result: &ExtractionResult, _config: &ExtractionConfig) -> Result<()> {
860
- let should_validate = result
861
- .metadata
862
- .additional
863
- .get(VALIDATION_MARKER_KEY)
864
- .and_then(|v| v.as_str())
865
- == Some(POSTPROCESSOR_VALIDATION_MARKER);
866
-
867
- if !should_validate {
868
- return Ok(());
869
- }
870
-
871
- let processed = result
872
- .metadata
873
- .additional
874
- .get("processed")
875
- .and_then(|v| v.as_bool())
876
- .unwrap_or(false);
877
-
878
- if !processed {
879
- return Err(crate::KreuzbergError::Validation {
880
- message: "Post-processor did not run before validator".to_string(),
881
- source: None,
882
- });
883
- }
884
- Ok(())
885
- }
886
- }
887
-
888
- let pp_registry = crate::plugins::registry::get_post_processor_registry();
889
- let val_registry = crate::plugins::registry::get_validator_registry();
890
-
891
- let _guard = REGISTRY_TEST_GUARD.lock().unwrap();
892
- clear_processor_cache().unwrap();
893
- pp_registry.write().unwrap().shutdown_all().unwrap();
894
- val_registry.write().unwrap().shutdown_all().unwrap();
895
- clear_processor_cache().unwrap();
896
-
897
- {
898
- let mut registry = pp_registry.write().unwrap();
899
- registry.register(Arc::new(TestPostProcessor), 0).unwrap();
900
- }
901
-
902
- {
903
- let mut registry = val_registry.write().unwrap();
904
- registry.register(Arc::new(TestValidator)).unwrap();
905
- }
906
-
907
- // Clear the cache after registering new processors so it rebuilds with the test processors
908
- clear_processor_cache().unwrap();
909
-
910
- let mut result = ExtractionResult {
911
- content: "test".to_string(),
912
- mime_type: "text/plain".to_string(),
913
- metadata: Metadata::default(),
914
- tables: vec![],
915
- detected_languages: None,
916
- chunks: None,
917
- images: None,
918
- pages: None,
919
- };
920
- result.metadata.additional.insert(
921
- VALIDATION_MARKER_KEY.to_string(),
922
- serde_json::json!(POSTPROCESSOR_VALIDATION_MARKER),
923
- );
924
-
925
- let config = ExtractionConfig {
926
- postprocessor: Some(crate::core::config::PostProcessorConfig {
927
- enabled: true,
928
- enabled_set: None,
929
- disabled_set: None,
930
- enabled_processors: None,
931
- disabled_processors: None,
932
- }),
933
- ..Default::default()
934
- };
935
- drop(_guard);
936
-
937
- let processed = run_pipeline(result, &config).await;
938
-
939
- pp_registry.write().unwrap().shutdown_all().unwrap();
940
- val_registry.write().unwrap().shutdown_all().unwrap();
941
-
942
- assert!(processed.is_ok(), "Validator should have seen post-processor metadata");
943
- let processed = processed.unwrap();
944
- assert_eq!(
945
- processed.metadata.additional.get("processed"),
946
- Some(&serde_json::json!(true)),
947
- "Post-processor metadata should be present"
948
- );
949
- }
950
-
951
- #[tokio::test]
952
- #[cfg(feature = "quality")]
953
- async fn test_quality_processing_runs_before_validator() {
954
- let _guard = REGISTRY_TEST_GUARD.lock().unwrap();
955
- use crate::plugins::{Plugin, Validator};
956
- use async_trait::async_trait;
957
- use std::sync::Arc;
958
-
959
- struct QualityValidator;
960
- impl Plugin for QualityValidator {
961
- fn name(&self) -> &str {
962
- "quality-validator"
963
- }
964
- fn version(&self) -> String {
965
- "1.0.0".to_string()
966
- }
967
- fn initialize(&self) -> Result<()> {
968
- Ok(())
969
- }
970
- fn shutdown(&self) -> Result<()> {
971
- Ok(())
972
- }
973
- }
974
-
975
- #[async_trait]
976
- impl Validator for QualityValidator {
977
- async fn validate(&self, result: &ExtractionResult, _config: &ExtractionConfig) -> Result<()> {
978
- let should_validate = result
979
- .metadata
980
- .additional
981
- .get(VALIDATION_MARKER_KEY)
982
- .and_then(|v| v.as_str())
983
- == Some(QUALITY_VALIDATION_MARKER);
984
-
985
- if !should_validate {
986
- return Ok(());
987
- }
988
-
989
- if !result.metadata.additional.contains_key("quality_score") {
990
- return Err(crate::KreuzbergError::Validation {
991
- message: "Quality processing did not run before validator".to_string(),
992
- source: None,
993
- });
994
- }
995
- Ok(())
996
- }
997
- }
998
-
999
- let val_registry = crate::plugins::registry::get_validator_registry();
1000
- {
1001
- let mut registry = val_registry.write().unwrap();
1002
- registry.register(Arc::new(QualityValidator)).unwrap();
1003
- }
1004
-
1005
- let mut result = ExtractionResult {
1006
- content: "This is meaningful test content for quality scoring.".to_string(),
1007
- mime_type: "text/plain".to_string(),
1008
- metadata: Metadata::default(),
1009
- tables: vec![],
1010
- detected_languages: None,
1011
- chunks: None,
1012
- images: None,
1013
- pages: None,
1014
- };
1015
- result.metadata.additional.insert(
1016
- VALIDATION_MARKER_KEY.to_string(),
1017
- serde_json::json!(QUALITY_VALIDATION_MARKER),
1018
- );
1019
-
1020
- let config = ExtractionConfig {
1021
- enable_quality_processing: true,
1022
- ..Default::default()
1023
- };
1024
-
1025
- drop(_guard);
1026
-
1027
- let processed = run_pipeline(result, &config).await;
1028
-
1029
- {
1030
- let mut registry = val_registry.write().unwrap();
1031
- registry.remove("quality-validator").unwrap();
1032
- }
1033
-
1034
- assert!(processed.is_ok(), "Validator should have seen quality_score");
1035
- }
1036
-
1037
- #[tokio::test]
1038
- async fn test_multiple_postprocessors_run_before_validator() {
1039
- use crate::plugins::{Plugin, PostProcessor, ProcessingStage, Validator};
1040
- use async_trait::async_trait;
1041
- use std::sync::Arc;
1042
-
1043
- struct EarlyProcessor;
1044
- impl Plugin for EarlyProcessor {
1045
- fn name(&self) -> &str {
1046
- "early-proc"
1047
- }
1048
- fn version(&self) -> String {
1049
- "1.0.0".to_string()
1050
- }
1051
- fn initialize(&self) -> Result<()> {
1052
- Ok(())
1053
- }
1054
- fn shutdown(&self) -> Result<()> {
1055
- Ok(())
1056
- }
1057
- }
1058
-
1059
- #[async_trait]
1060
- impl PostProcessor for EarlyProcessor {
1061
- async fn process(&self, result: &mut ExtractionResult, _config: &ExtractionConfig) -> Result<()> {
1062
- let mut order = result
1063
- .metadata
1064
- .additional
1065
- .get("execution_order")
1066
- .and_then(|v| v.as_array())
1067
- .cloned()
1068
- .unwrap_or_default();
1069
- order.push(serde_json::json!("early"));
1070
- result
1071
- .metadata
1072
- .additional
1073
- .insert("execution_order".to_string(), serde_json::json!(order));
1074
- Ok(())
1075
- }
1076
-
1077
- fn processing_stage(&self) -> ProcessingStage {
1078
- ProcessingStage::Early
1079
- }
1080
- }
1081
-
1082
- struct LateProcessor;
1083
- impl Plugin for LateProcessor {
1084
- fn name(&self) -> &str {
1085
- "late-proc"
1086
- }
1087
- fn version(&self) -> String {
1088
- "1.0.0".to_string()
1089
- }
1090
- fn initialize(&self) -> Result<()> {
1091
- Ok(())
1092
- }
1093
- fn shutdown(&self) -> Result<()> {
1094
- Ok(())
1095
- }
1096
- }
1097
-
1098
- #[async_trait]
1099
- impl PostProcessor for LateProcessor {
1100
- async fn process(&self, result: &mut ExtractionResult, _config: &ExtractionConfig) -> Result<()> {
1101
- let mut order = result
1102
- .metadata
1103
- .additional
1104
- .get("execution_order")
1105
- .and_then(|v| v.as_array())
1106
- .cloned()
1107
- .unwrap_or_default();
1108
- order.push(serde_json::json!("late"));
1109
- result
1110
- .metadata
1111
- .additional
1112
- .insert("execution_order".to_string(), serde_json::json!(order));
1113
- Ok(())
1114
- }
1115
-
1116
- fn processing_stage(&self) -> ProcessingStage {
1117
- ProcessingStage::Late
1118
- }
1119
- }
1120
-
1121
- struct OrderValidator;
1122
- impl Plugin for OrderValidator {
1123
- fn name(&self) -> &str {
1124
- "order-validator"
1125
- }
1126
- fn version(&self) -> String {
1127
- "1.0.0".to_string()
1128
- }
1129
- fn initialize(&self) -> Result<()> {
1130
- Ok(())
1131
- }
1132
- fn shutdown(&self) -> Result<()> {
1133
- Ok(())
1134
- }
1135
- }
1136
-
1137
- #[async_trait]
1138
- impl Validator for OrderValidator {
1139
- async fn validate(&self, result: &ExtractionResult, _config: &ExtractionConfig) -> Result<()> {
1140
- let should_validate = result
1141
- .metadata
1142
- .additional
1143
- .get(VALIDATION_MARKER_KEY)
1144
- .and_then(|v| v.as_str())
1145
- == Some(ORDER_VALIDATION_MARKER);
1146
-
1147
- if !should_validate {
1148
- return Ok(());
1149
- }
1150
-
1151
- let order = result
1152
- .metadata
1153
- .additional
1154
- .get("execution_order")
1155
- .and_then(|v| v.as_array())
1156
- .ok_or_else(|| crate::KreuzbergError::Validation {
1157
- message: "No execution order found".to_string(),
1158
- source: None,
1159
- })?;
1160
-
1161
- if order.len() != 2 {
1162
- return Err(crate::KreuzbergError::Validation {
1163
- message: format!("Expected 2 processors to run, got {}", order.len()),
1164
- source: None,
1165
- });
1166
- }
1167
-
1168
- if order[0] != "early" || order[1] != "late" {
1169
- return Err(crate::KreuzbergError::Validation {
1170
- message: format!("Wrong execution order: {:?}", order),
1171
- source: None,
1172
- });
1173
- }
1174
-
1175
- Ok(())
1176
- }
1177
- }
1178
-
1179
- let pp_registry = crate::plugins::registry::get_post_processor_registry();
1180
- let val_registry = crate::plugins::registry::get_validator_registry();
1181
- let _guard = REGISTRY_TEST_GUARD.lock().unwrap();
1182
-
1183
- pp_registry.write().unwrap().shutdown_all().unwrap();
1184
- val_registry.write().unwrap().shutdown_all().unwrap();
1185
- clear_processor_cache().unwrap();
1186
-
1187
- {
1188
- let mut registry = pp_registry.write().unwrap();
1189
- registry.register(Arc::new(EarlyProcessor), 0).unwrap();
1190
- registry.register(Arc::new(LateProcessor), 0).unwrap();
1191
- }
1192
-
1193
- {
1194
- let mut registry = val_registry.write().unwrap();
1195
- registry.register(Arc::new(OrderValidator)).unwrap();
1196
- }
1197
-
1198
- // Clear the cache after registering new processors so it rebuilds with the test processors
1199
- clear_processor_cache().unwrap();
1200
-
1201
- let result = ExtractionResult {
1202
- content: "test".to_string(),
1203
- mime_type: "text/plain".to_string(),
1204
- metadata: Metadata::default(),
1205
- tables: vec![],
1206
- detected_languages: None,
1207
- chunks: None,
1208
- images: None,
1209
- pages: None,
1210
- };
1211
-
1212
- let config = ExtractionConfig::default();
1213
- drop(_guard);
1214
-
1215
- let processed = run_pipeline(result, &config).await;
1216
-
1217
- pp_registry.write().unwrap().shutdown_all().unwrap();
1218
- val_registry.write().unwrap().shutdown_all().unwrap();
1219
- clear_processor_cache().unwrap();
1220
-
1221
- assert!(processed.is_ok(), "All processors should run before validator");
1222
- }
1223
- }