wiki-graph-core 0.3.1

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 (576) hide show
  1. package/LICENSE +201 -0
  2. package/data/editor/clue_reviewer.jinja +410 -0
  3. package/data/editor/clue_reviewer_generator.jinja +428 -0
  4. package/data/editor/revision_feedback.jinja +5 -0
  5. package/data/editor/text_compressor.jinja +127 -0
  6. package/data/guaranteed/response_intent_classifier.jinja +8 -0
  7. package/data/help/commands/archive/next.jinja +29 -0
  8. package/data/help/commands/gc.jinja +38 -0
  9. package/data/help/commands/legacy/migrate.jinja +20 -0
  10. package/data/help/commands/legacy.jinja +19 -0
  11. package/data/help/commands/maintenance/chapter/add.jinja +41 -0
  12. package/data/help/commands/maintenance/chapter/list.jinja +33 -0
  13. package/data/help/commands/maintenance/chapter/move.jinja +36 -0
  14. package/data/help/commands/maintenance/chapter/remove.jinja +26 -0
  15. package/data/help/commands/maintenance/chapter/reset.jinja +29 -0
  16. package/data/help/commands/maintenance/chapter/set-source.jinja +34 -0
  17. package/data/help/commands/maintenance/chapter/set-summary.jinja +31 -0
  18. package/data/help/commands/maintenance/chapter/set-title.jinja +29 -0
  19. package/data/help/commands/maintenance/chapter/tree.jinja +58 -0
  20. package/data/help/commands/maintenance/chapter.jinja +59 -0
  21. package/data/help/commands/maintenance/cover.jinja +28 -0
  22. package/data/help/commands/maintenance/meta.jinja +55 -0
  23. package/data/help/commands/predicate.jinja +460 -0
  24. package/data/help/commands/root.jinja +83 -0
  25. package/data/help/commands/transform.jinja +34 -0
  26. package/data/help/commands/uri.jinja +274 -0
  27. package/data/help/topics/config.jinja +88 -0
  28. package/data/help/topics/format.jinja +72 -0
  29. package/data/help/topics/readiness.jinja +99 -0
  30. package/data/help/topics/recipe.jinja +151 -0
  31. package/data/help/topics/runtime.jinja +62 -0
  32. package/data/help/topics/uri.jinja +178 -0
  33. package/data/output/epub/cover.xhtml.jinja +13 -0
  34. package/data/output/epub/nav.xhtml.jinja +12 -0
  35. package/data/output/epub/package.opf.xml.jinja +39 -0
  36. package/data/output/epub/section.xhtml.jinja +12 -0
  37. package/data/topologization/book_coherence_extraction.jinja +223 -0
  38. package/data/topologization/chunk_extraction.jinja +233 -0
  39. package/data/topologization/evidence_choice.jinja +38 -0
  40. package/data/topologization/translate_chunks.jinja +138 -0
  41. package/data/topologization/user_focused_extraction.jinja +378 -0
  42. package/dist/archive/query/archive-view.d.ts +26 -0
  43. package/dist/archive/query/archive-view.js +4540 -0
  44. package/dist/archive/query/archive-view.js.map +1 -0
  45. package/dist/archive/query/continuation-cursor.d.ts +61 -0
  46. package/dist/archive/query/continuation-cursor.js +363 -0
  47. package/dist/archive/query/continuation-cursor.js.map +1 -0
  48. package/dist/archive/query/index.d.ts +29 -0
  49. package/dist/archive/query/index.js +63 -0
  50. package/dist/archive/query/index.js.map +1 -0
  51. package/dist/archive/query/lexical-search.d.ts +22 -0
  52. package/dist/archive/query/lexical-search.js +137 -0
  53. package/dist/archive/query/lexical-search.js.map +1 -0
  54. package/dist/archive/query/search-cache.d.ts +192 -0
  55. package/dist/archive/query/search-cache.js +1248 -0
  56. package/dist/archive/query/search-cache.js.map +1 -0
  57. package/dist/archive/search-index/index.d.ts +26 -0
  58. package/dist/archive/search-index/index.js +29 -0
  59. package/dist/archive/search-index/index.js.map +1 -0
  60. package/dist/archive/search-index/search-index.d.ts +26 -0
  61. package/dist/archive/search-index/search-index.js +555 -0
  62. package/dist/archive/search-index/search-index.js.map +1 -0
  63. package/dist/archive/search-index/search-tokenizer.d.ts +16 -0
  64. package/dist/archive/search-index/search-tokenizer.js +225 -0
  65. package/dist/archive/search-index/search-tokenizer.js.map +1 -0
  66. package/dist/common/cli-command.d.ts +4 -0
  67. package/dist/common/cli-command.js +7 -0
  68. package/dist/common/cli-command.js.map +1 -0
  69. package/dist/common/data-dir.d.ts +3 -0
  70. package/dist/common/data-dir.js +48 -0
  71. package/dist/common/data-dir.js.map +1 -0
  72. package/dist/common/language.d.ts +55 -0
  73. package/dist/common/language.js +208 -0
  74. package/dist/common/language.js.map +1 -0
  75. package/dist/common/llm-scope.d.ts +19 -0
  76. package/dist/common/llm-scope.js +31 -0
  77. package/dist/common/llm-scope.js.map +1 -0
  78. package/dist/common/logging.d.ts +22 -0
  79. package/dist/common/logging.js +141 -0
  80. package/dist/common/logging.js.map +1 -0
  81. package/dist/common/template.d.ts +8 -0
  82. package/dist/common/template.js +54 -0
  83. package/dist/common/template.js.map +1 -0
  84. package/dist/common/tinyld-language.d.ts +6 -0
  85. package/dist/common/tinyld-language.js +34 -0
  86. package/dist/common/tinyld-language.js.map +1 -0
  87. package/dist/common/wiki-graph-dir.d.ts +10 -0
  88. package/dist/common/wiki-graph-dir.js +41 -0
  89. package/dist/common/wiki-graph-dir.js.map +1 -0
  90. package/dist/common/wiki-graph-temp.d.ts +6 -0
  91. package/dist/common/wiki-graph-temp.js +21 -0
  92. package/dist/common/wiki-graph-temp.js.map +1 -0
  93. package/dist/common/wiki-graph-uri.d.ts +28 -0
  94. package/dist/common/wiki-graph-uri.js +171 -0
  95. package/dist/common/wiki-graph-uri.js.map +1 -0
  96. package/dist/context/index.d.ts +1 -0
  97. package/dist/context/index.js +13 -0
  98. package/dist/context/index.js.map +1 -0
  99. package/dist/context/task-context.d.ts +41 -0
  100. package/dist/context/task-context.js +111 -0
  101. package/dist/context/task-context.js.map +1 -0
  102. package/dist/document/database.d.ts +30 -0
  103. package/dist/document/database.js +273 -0
  104. package/dist/document/database.js.map +1 -0
  105. package/dist/document/document.d.ts +153 -0
  106. package/dist/document/document.js +615 -0
  107. package/dist/document/document.js.map +1 -0
  108. package/dist/document/fragments.d.ts +63 -0
  109. package/dist/document/fragments.js +371 -0
  110. package/dist/document/fragments.js.map +1 -0
  111. package/dist/document/index.d.ts +13 -0
  112. package/dist/document/index.js +62 -0
  113. package/dist/document/index.js.map +1 -0
  114. package/dist/document/schema.d.ts +8 -0
  115. package/dist/document/schema.js +472 -0
  116. package/dist/document/schema.js.map +1 -0
  117. package/dist/document/shared-state-database.d.ts +9 -0
  118. package/dist/document/shared-state-database.js +193 -0
  119. package/dist/document/shared-state-database.js.map +1 -0
  120. package/dist/document/stores.d.ts +203 -0
  121. package/dist/document/stores.js +1678 -0
  122. package/dist/document/stores.js.map +1 -0
  123. package/dist/document/text-streams.d.ts +86 -0
  124. package/dist/document/text-streams.js +515 -0
  125. package/dist/document/text-streams.js.map +1 -0
  126. package/dist/document/types.d.ts +148 -0
  127. package/dist/document/types.js +69 -0
  128. package/dist/document/types.js.map +1 -0
  129. package/dist/editor/clue.d.ts +27 -0
  130. package/dist/editor/clue.js +158 -0
  131. package/dist/editor/clue.js.map +1 -0
  132. package/dist/editor/compressor.d.ts +18 -0
  133. package/dist/editor/compressor.js +68 -0
  134. package/dist/editor/compressor.js.map +1 -0
  135. package/dist/editor/editor.d.ts +39 -0
  136. package/dist/editor/editor.js +253 -0
  137. package/dist/editor/editor.js.map +1 -0
  138. package/dist/editor/feedback.d.ts +16 -0
  139. package/dist/editor/feedback.js +104 -0
  140. package/dist/editor/feedback.js.map +1 -0
  141. package/dist/editor/index.d.ts +17 -0
  142. package/dist/editor/index.js +5 -0
  143. package/dist/editor/index.js.map +1 -0
  144. package/dist/editor/language-review.d.ts +14 -0
  145. package/dist/editor/language-review.js +35 -0
  146. package/dist/editor/language-review.js.map +1 -0
  147. package/dist/editor/log.d.ts +37 -0
  148. package/dist/editor/log.js +202 -0
  149. package/dist/editor/log.js.map +1 -0
  150. package/dist/editor/markup.d.ts +20 -0
  151. package/dist/editor/markup.js +294 -0
  152. package/dist/editor/markup.js.map +1 -0
  153. package/dist/editor/prompt-templates.d.ts +6 -0
  154. package/dist/editor/prompt-templates.js +11 -0
  155. package/dist/editor/prompt-templates.js.map +1 -0
  156. package/dist/editor/response.d.ts +5 -0
  157. package/dist/editor/response.js +31 -0
  158. package/dist/editor/response.js.map +1 -0
  159. package/dist/editor/review.d.ts +34 -0
  160. package/dist/editor/review.js +189 -0
  161. package/dist/editor/review.js.map +1 -0
  162. package/dist/editor/types.d.ts +31 -0
  163. package/dist/editor/types.js +21 -0
  164. package/dist/editor/types.js.map +1 -0
  165. package/dist/evidence-selection/anchor-resolver.d.ts +43 -0
  166. package/dist/evidence-selection/anchor-resolver.js +783 -0
  167. package/dist/evidence-selection/anchor-resolver.js.map +1 -0
  168. package/dist/evidence-selection/anchor-types.d.ts +27 -0
  169. package/dist/evidence-selection/anchor-types.js +1 -0
  170. package/dist/evidence-selection/anchor-types.js.map +1 -0
  171. package/dist/evidence-selection/index.d.ts +8 -0
  172. package/dist/evidence-selection/index.js +32 -0
  173. package/dist/evidence-selection/index.js.map +1 -0
  174. package/dist/evidence-selection/prompt.d.ts +18 -0
  175. package/dist/evidence-selection/prompt.js +54 -0
  176. package/dist/evidence-selection/prompt.js.map +1 -0
  177. package/dist/evidence-selection/quote-score.d.ts +29 -0
  178. package/dist/evidence-selection/quote-score.js +331 -0
  179. package/dist/evidence-selection/quote-score.js.map +1 -0
  180. package/dist/evidence-selection/selection-resolver.d.ts +19 -0
  181. package/dist/evidence-selection/selection-resolver.js +303 -0
  182. package/dist/evidence-selection/selection-resolver.js.map +1 -0
  183. package/dist/evidence-selection/text.d.ts +4 -0
  184. package/dist/evidence-selection/text.js +66 -0
  185. package/dist/evidence-selection/text.js.map +1 -0
  186. package/dist/evidence-selection/types.d.ts +36 -0
  187. package/dist/evidence-selection/types.js +1 -0
  188. package/dist/evidence-selection/types.js.map +1 -0
  189. package/dist/facade/app.d.ts +75 -0
  190. package/dist/facade/app.js +141 -0
  191. package/dist/facade/app.js.map +1 -0
  192. package/dist/facade/build-queue.d.ts +181 -0
  193. package/dist/facade/build-queue.js +1442 -0
  194. package/dist/facade/build-queue.js.map +1 -0
  195. package/dist/facade/chapter-build.d.ts +60 -0
  196. package/dist/facade/chapter-build.js +1020 -0
  197. package/dist/facade/chapter-build.js.map +1 -0
  198. package/dist/facade/chapter.d.ts +168 -0
  199. package/dist/facade/chapter.js +944 -0
  200. package/dist/facade/chapter.js.map +1 -0
  201. package/dist/facade/digest.d.ts +55 -0
  202. package/dist/facade/digest.js +168 -0
  203. package/dist/facade/digest.js.map +1 -0
  204. package/dist/facade/graph.d.ts +60 -0
  205. package/dist/facade/graph.js +203 -0
  206. package/dist/facade/graph.js.map +1 -0
  207. package/dist/facade/import.d.ts +49 -0
  208. package/dist/facade/import.js +262 -0
  209. package/dist/facade/import.js.map +1 -0
  210. package/dist/facade/index.d.ts +39 -0
  211. package/dist/facade/index.js +171 -0
  212. package/dist/facade/index.js.map +1 -0
  213. package/dist/facade/knowledge-graph-build.d.ts +75 -0
  214. package/dist/facade/knowledge-graph-build.js +897 -0
  215. package/dist/facade/knowledge-graph-build.js.map +1 -0
  216. package/dist/facade/llm-sampling.d.ts +11 -0
  217. package/dist/facade/llm-sampling.js +58 -0
  218. package/dist/facade/llm-sampling.js.map +1 -0
  219. package/dist/facade/types.d.ts +8 -0
  220. package/dist/facade/types.js +1 -0
  221. package/dist/facade/types.js.map +1 -0
  222. package/dist/facade/wiki-graph-archive.d.ts +41 -0
  223. package/dist/facade/wiki-graph-archive.js +130 -0
  224. package/dist/facade/wiki-graph-archive.js.map +1 -0
  225. package/dist/gc/files.d.ts +15 -0
  226. package/dist/gc/files.js +141 -0
  227. package/dist/gc/files.js.map +1 -0
  228. package/dist/gc/index.d.ts +2 -0
  229. package/dist/gc/index.js +5 -0
  230. package/dist/gc/index.js.map +1 -0
  231. package/dist/gc/lock.d.ts +3 -0
  232. package/dist/gc/lock.js +137 -0
  233. package/dist/gc/lock.js.map +1 -0
  234. package/dist/gc/runner.d.ts +9 -0
  235. package/dist/gc/runner.js +129 -0
  236. package/dist/gc/runner.js.map +1 -0
  237. package/dist/gc/tmp-gc.d.ts +5 -0
  238. package/dist/gc/tmp-gc.js +57 -0
  239. package/dist/gc/tmp-gc.js.map +1 -0
  240. package/dist/gc/types.d.ts +24 -0
  241. package/dist/gc/types.js +1 -0
  242. package/dist/gc/types.js.map +1 -0
  243. package/dist/gc.cjs +10041 -0
  244. package/dist/gc.cjs.map +1 -0
  245. package/dist/gc.d.cts +10 -0
  246. package/dist/gc.d.ts +2 -0
  247. package/dist/gc.js +5 -0
  248. package/dist/gc.js.map +1 -0
  249. package/dist/guaranteed/classifier.d.ts +10 -0
  250. package/dist/guaranteed/classifier.js +88 -0
  251. package/dist/guaranteed/classifier.js.map +1 -0
  252. package/dist/guaranteed/errors.d.ts +38 -0
  253. package/dist/guaranteed/errors.js +73 -0
  254. package/dist/guaranteed/errors.js.map +1 -0
  255. package/dist/guaranteed/index.d.ts +7 -0
  256. package/dist/guaranteed/index.js +21 -0
  257. package/dist/guaranteed/index.js.map +1 -0
  258. package/dist/guaranteed/request.d.ts +8 -0
  259. package/dist/guaranteed/request.js +206 -0
  260. package/dist/guaranteed/request.js.map +1 -0
  261. package/dist/guaranteed/response.d.ts +12 -0
  262. package/dist/guaranteed/response.js +98 -0
  263. package/dist/guaranteed/response.js.map +1 -0
  264. package/dist/guaranteed/types.d.ts +20 -0
  265. package/dist/guaranteed/types.js +1 -0
  266. package/dist/guaranteed/types.js.map +1 -0
  267. package/dist/index-B8NNnQ8P.d.ts +424 -0
  268. package/dist/index.cjs +34774 -0
  269. package/dist/index.cjs.map +1 -0
  270. package/dist/index.d.cts +2462 -0
  271. package/dist/index.d.ts +79 -0
  272. package/dist/index.js +168 -0
  273. package/dist/index.js.map +1 -0
  274. package/dist/legacy-sdpub/upgrade.d.ts +7 -0
  275. package/dist/legacy-sdpub/upgrade.js +795 -0
  276. package/dist/legacy-sdpub/upgrade.js.map +1 -0
  277. package/dist/llm/cache.d.ts +13 -0
  278. package/dist/llm/cache.js +35 -0
  279. package/dist/llm/cache.js.map +1 -0
  280. package/dist/llm/client.d.ts +26 -0
  281. package/dist/llm/client.js +572 -0
  282. package/dist/llm/client.js.map +1 -0
  283. package/dist/llm/context.d.ts +22 -0
  284. package/dist/llm/context.js +78 -0
  285. package/dist/llm/context.js.map +1 -0
  286. package/dist/llm/errors.d.ts +7 -0
  287. package/dist/llm/errors.js +12 -0
  288. package/dist/llm/errors.js.map +1 -0
  289. package/dist/llm/index.d.ts +7 -0
  290. package/dist/llm/index.js +17 -0
  291. package/dist/llm/index.js.map +1 -0
  292. package/dist/llm/request-log.d.ts +9 -0
  293. package/dist/llm/request-log.js +35 -0
  294. package/dist/llm/request-log.js.map +1 -0
  295. package/dist/llm/sampling.d.ts +11 -0
  296. package/dist/llm/sampling.js +67 -0
  297. package/dist/llm/sampling.js.map +1 -0
  298. package/dist/llm/types.d.ts +50 -0
  299. package/dist/llm/types.js +1 -0
  300. package/dist/llm/types.js.map +1 -0
  301. package/dist/output/epub/archive.d.ts +9 -0
  302. package/dist/output/epub/archive.js +81 -0
  303. package/dist/output/epub/archive.js.map +1 -0
  304. package/dist/output/epub/book.d.ts +15 -0
  305. package/dist/output/epub/book.js +76 -0
  306. package/dist/output/epub/book.js.map +1 -0
  307. package/dist/output/epub/content.d.ts +9 -0
  308. package/dist/output/epub/content.js +35 -0
  309. package/dist/output/epub/content.js.map +1 -0
  310. package/dist/output/epub/index.d.ts +20 -0
  311. package/dist/output/epub/index.js +14 -0
  312. package/dist/output/epub/index.js.map +1 -0
  313. package/dist/output/epub/model.d.ts +24 -0
  314. package/dist/output/epub/model.js +1 -0
  315. package/dist/output/epub/model.js.map +1 -0
  316. package/dist/output/epub/navigation.d.ts +10 -0
  317. package/dist/output/epub/navigation.js +39 -0
  318. package/dist/output/epub/navigation.js.map +1 -0
  319. package/dist/output/epub/package.d.ts +17 -0
  320. package/dist/output/epub/package.js +29 -0
  321. package/dist/output/epub/package.js.map +1 -0
  322. package/dist/output/epub/shared.d.ts +4 -0
  323. package/dist/output/epub/shared.js +12 -0
  324. package/dist/output/epub/shared.js.map +1 -0
  325. package/dist/output/epub/templates.d.ts +35 -0
  326. package/dist/output/epub/templates.js +26 -0
  327. package/dist/output/epub/templates.js.map +1 -0
  328. package/dist/output/index.d.ts +14 -0
  329. package/dist/output/index.js +11 -0
  330. package/dist/output/index.js.map +1 -0
  331. package/dist/output/plain-text.d.ts +18 -0
  332. package/dist/output/plain-text.js +56 -0
  333. package/dist/output/plain-text.js.map +1 -0
  334. package/dist/progress/index.d.ts +3 -0
  335. package/dist/progress/index.js +14 -0
  336. package/dist/progress/index.js.map +1 -0
  337. package/dist/progress/reporter.d.ts +10 -0
  338. package/dist/progress/reporter.js +79 -0
  339. package/dist/progress/reporter.js.map +1 -0
  340. package/dist/progress/tracker.d.ts +37 -0
  341. package/dist/progress/tracker.js +119 -0
  342. package/dist/progress/tracker.js.map +1 -0
  343. package/dist/progress/types.d.ts +28 -0
  344. package/dist/progress/types.js +1 -0
  345. package/dist/progress/types.js.map +1 -0
  346. package/dist/reader/attention/attention.d.ts +39 -0
  347. package/dist/reader/attention/attention.js +123 -0
  348. package/dist/reader/attention/attention.js.map +1 -0
  349. package/dist/reader/attention/index.d.ts +9 -0
  350. package/dist/reader/attention/index.js +5 -0
  351. package/dist/reader/attention/index.js.map +1 -0
  352. package/dist/reader/attention/wave-reflection.d.ts +21 -0
  353. package/dist/reader/attention/wave-reflection.js +153 -0
  354. package/dist/reader/attention/wave-reflection.js.map +1 -0
  355. package/dist/reader/attention/working-memory.d.ts +28 -0
  356. package/dist/reader/attention/working-memory.js +62 -0
  357. package/dist/reader/attention/working-memory.js.map +1 -0
  358. package/dist/reader/chunk-batch/extract.d.ts +13 -0
  359. package/dist/reader/chunk-batch/extract.js +14 -0
  360. package/dist/reader/chunk-batch/extract.js.map +1 -0
  361. package/dist/reader/chunk-batch/extractor.d.ts +17 -0
  362. package/dist/reader/chunk-batch/extractor.js +337 -0
  363. package/dist/reader/chunk-batch/extractor.js.map +1 -0
  364. package/dist/reader/chunk-batch/fragment-projection.d.ts +34 -0
  365. package/dist/reader/chunk-batch/fragment-projection.js +94 -0
  366. package/dist/reader/chunk-batch/fragment-projection.js.map +1 -0
  367. package/dist/reader/chunk-batch/index.d.ts +12 -0
  368. package/dist/reader/chunk-batch/index.js +13 -0
  369. package/dist/reader/chunk-batch/index.js.map +1 -0
  370. package/dist/reader/chunk-batch/language.d.ts +9 -0
  371. package/dist/reader/chunk-batch/language.js +23 -0
  372. package/dist/reader/chunk-batch/language.js.map +1 -0
  373. package/dist/reader/chunk-batch/parser.d.ts +94 -0
  374. package/dist/reader/chunk-batch/parser.js +640 -0
  375. package/dist/reader/chunk-batch/parser.js.map +1 -0
  376. package/dist/reader/chunk-batch/prompt-templates.d.ts +6 -0
  377. package/dist/reader/chunk-batch/prompt-templates.js +11 -0
  378. package/dist/reader/chunk-batch/prompt-templates.js.map +1 -0
  379. package/dist/reader/chunk-batch/types.d.ts +85 -0
  380. package/dist/reader/chunk-batch/types.js +1 -0
  381. package/dist/reader/chunk-batch/types.js.map +1 -0
  382. package/dist/reader/index.d.ts +13 -0
  383. package/dist/reader/index.js +7 -0
  384. package/dist/reader/index.js.map +1 -0
  385. package/dist/reader/reader.d.ts +37 -0
  386. package/dist/reader/reader.js +82 -0
  387. package/dist/reader/reader.js.map +1 -0
  388. package/dist/reader/segment/index.d.ts +2 -0
  389. package/dist/reader/segment/index.js +5 -0
  390. package/dist/reader/segment/index.js.map +1 -0
  391. package/dist/reader/segment/intl-segmenter.d.ts +5 -0
  392. package/dist/reader/segment/intl-segmenter.js +102 -0
  393. package/dist/reader/segment/intl-segmenter.js.map +1 -0
  394. package/dist/reader/segment/segment.d.ts +5 -0
  395. package/dist/reader/segment/segment.js +9 -0
  396. package/dist/reader/segment/segment.js.map +1 -0
  397. package/dist/reader/segment/types.d.ts +14 -0
  398. package/dist/reader/segment/types.js +1 -0
  399. package/dist/reader/segment/types.js.map +1 -0
  400. package/dist/reader/types.d.ts +29 -0
  401. package/dist/reader/types.js +1 -0
  402. package/dist/reader/types.js.map +1 -0
  403. package/dist/serial.d.ts +89 -0
  404. package/dist/serial.js +476 -0
  405. package/dist/serial.js.map +1 -0
  406. package/dist/source/adapter.d.ts +15 -0
  407. package/dist/source/adapter.js +1 -0
  408. package/dist/source/adapter.js.map +1 -0
  409. package/dist/source/epub/archive.d.ts +27 -0
  410. package/dist/source/epub/archive.js +189 -0
  411. package/dist/source/epub/archive.js.map +1 -0
  412. package/dist/source/epub/content.d.ts +22 -0
  413. package/dist/source/epub/content.js +183 -0
  414. package/dist/source/epub/content.js.map +1 -0
  415. package/dist/source/epub/document.d.ts +55 -0
  416. package/dist/source/epub/document.js +179 -0
  417. package/dist/source/epub/document.js.map +1 -0
  418. package/dist/source/epub/index.d.ts +10 -0
  419. package/dist/source/epub/index.js +6 -0
  420. package/dist/source/epub/index.js.map +1 -0
  421. package/dist/source/epub/navigation.d.ts +16 -0
  422. package/dist/source/epub/navigation.js +215 -0
  423. package/dist/source/epub/navigation.js.map +1 -0
  424. package/dist/source/epub/package.d.ts +33 -0
  425. package/dist/source/epub/package.js +237 -0
  426. package/dist/source/epub/package.js.map +1 -0
  427. package/dist/source/epub/xml.d.ts +16 -0
  428. package/dist/source/epub/xml.js +111 -0
  429. package/dist/source/epub/xml.js.map +1 -0
  430. package/dist/source/index.d.ts +12 -0
  431. package/dist/source/index.js +32 -0
  432. package/dist/source/index.js.map +1 -0
  433. package/dist/source/meta.d.ts +30 -0
  434. package/dist/source/meta.js +22 -0
  435. package/dist/source/meta.js.map +1 -0
  436. package/dist/source/plain-text.d.ts +16 -0
  437. package/dist/source/plain-text.js +86 -0
  438. package/dist/source/plain-text.js.map +1 -0
  439. package/dist/source/toc.d.ts +16 -0
  440. package/dist/source/toc.js +17 -0
  441. package/dist/source/toc.js.map +1 -0
  442. package/dist/source/types.d.ts +16 -0
  443. package/dist/source/types.js +1 -0
  444. package/dist/source/types.js.map +1 -0
  445. package/dist/topology/grouping.d.ts +15 -0
  446. package/dist/topology/grouping.js +18 -0
  447. package/dist/topology/grouping.js.map +1 -0
  448. package/dist/topology/index.d.ts +21 -0
  449. package/dist/topology/index.js +5 -0
  450. package/dist/topology/index.js.map +1 -0
  451. package/dist/topology/resource-segmentation.d.ts +13 -0
  452. package/dist/topology/resource-segmentation.js +111 -0
  453. package/dist/topology/resource-segmentation.js.map +1 -0
  454. package/dist/topology/segment-incision.d.ts +18 -0
  455. package/dist/topology/segment-incision.js +197 -0
  456. package/dist/topology/segment-incision.js.map +1 -0
  457. package/dist/topology/snake-detector.d.ts +13 -0
  458. package/dist/topology/snake-detector.js +520 -0
  459. package/dist/topology/snake-detector.js.map +1 -0
  460. package/dist/topology/snake-graph-builder.d.ts +14 -0
  461. package/dist/topology/snake-graph-builder.js +76 -0
  462. package/dist/topology/snake-graph-builder.js.map +1 -0
  463. package/dist/topology/topology.d.ts +29 -0
  464. package/dist/topology/topology.js +348 -0
  465. package/dist/topology/topology.js.map +1 -0
  466. package/dist/topology/weights.d.ts +10 -0
  467. package/dist/topology/weights.js +84 -0
  468. package/dist/topology/weights.js.map +1 -0
  469. package/dist/types-FVIqiLC9.d.cts +24 -0
  470. package/dist/utils/async-semaphore.d.ts +7 -0
  471. package/dist/utils/async-semaphore.js +40 -0
  472. package/dist/utils/async-semaphore.js.map +1 -0
  473. package/dist/utils/enum.d.ts +5 -0
  474. package/dist/utils/enum.js +18 -0
  475. package/dist/utils/enum.js.map +1 -0
  476. package/dist/utils/hash.d.ts +3 -0
  477. package/dist/utils/hash.js +132 -0
  478. package/dist/utils/hash.js.map +1 -0
  479. package/dist/utils/node-error.d.ts +4 -0
  480. package/dist/utils/node-error.js +59 -0
  481. package/dist/utils/node-error.js.map +1 -0
  482. package/dist/wikg/archive-uri.d.ts +1 -0
  483. package/dist/wikg/archive-uri.js +37 -0
  484. package/dist/wikg/archive-uri.js.map +1 -0
  485. package/dist/wikg/archive.d.ts +28 -0
  486. package/dist/wikg/archive.js +496 -0
  487. package/dist/wikg/archive.js.map +1 -0
  488. package/dist/wikg/index.d.ts +31 -0
  489. package/dist/wikg/index.js +60 -0
  490. package/dist/wikg/index.js.map +1 -0
  491. package/dist/wikg/wikg-coordinator.d.ts +45 -0
  492. package/dist/wikg/wikg-coordinator.js +1687 -0
  493. package/dist/wikg/wikg-coordinator.js.map +1 -0
  494. package/dist/wikg/wiki-graph-archive-file.d.ts +42 -0
  495. package/dist/wikg/wiki-graph-archive-file.js +78 -0
  496. package/dist/wikg/wiki-graph-archive-file.js.map +1 -0
  497. package/dist/wikilink/index.d.ts +10 -0
  498. package/dist/wikilink/index.js +7 -0
  499. package/dist/wikilink/index.js.map +1 -0
  500. package/dist/wikilink/relation-discovery.d.ts +31 -0
  501. package/dist/wikilink/relation-discovery.js +477 -0
  502. package/dist/wikilink/relation-discovery.js.map +1 -0
  503. package/dist/wikilink/types.d.ts +23 -0
  504. package/dist/wikilink/types.js +1 -0
  505. package/dist/wikilink/types.js.map +1 -0
  506. package/dist/wikilink/window.d.ts +7 -0
  507. package/dist/wikilink/window.js +53 -0
  508. package/dist/wikilink/window.js.map +1 -0
  509. package/dist/wikimatch/enrichment.d.ts +10 -0
  510. package/dist/wikimatch/enrichment.js +55 -0
  511. package/dist/wikimatch/enrichment.js.map +1 -0
  512. package/dist/wikimatch/index.d.ts +16 -0
  513. package/dist/wikimatch/index.js +66 -0
  514. package/dist/wikimatch/index.js.map +1 -0
  515. package/dist/wikimatch/option-narrowing.d.ts +17 -0
  516. package/dist/wikimatch/option-narrowing.js +208 -0
  517. package/dist/wikimatch/option-narrowing.js.map +1 -0
  518. package/dist/wikimatch/options.d.ts +14 -0
  519. package/dist/wikimatch/options.js +155 -0
  520. package/dist/wikimatch/options.js.map +1 -0
  521. package/dist/wikimatch/policy-judge.d.ts +17 -0
  522. package/dist/wikimatch/policy-judge.js +430 -0
  523. package/dist/wikimatch/policy-judge.js.map +1 -0
  524. package/dist/wikimatch/range-suppression.d.ts +9 -0
  525. package/dist/wikimatch/range-suppression.js +27 -0
  526. package/dist/wikimatch/range-suppression.js.map +1 -0
  527. package/dist/wikimatch/surface-screening.d.ts +16 -0
  528. package/dist/wikimatch/surface-screening.js +168 -0
  529. package/dist/wikimatch/surface-screening.js.map +1 -0
  530. package/dist/wikimatch/surface-window.d.ts +6 -0
  531. package/dist/wikimatch/surface-window.js +44 -0
  532. package/dist/wikimatch/surface-window.js.map +1 -0
  533. package/dist/wikimatch/types.d.ts +151 -0
  534. package/dist/wikimatch/types.js +1 -0
  535. package/dist/wikimatch/types.js.map +1 -0
  536. package/dist/wikimatch/wikispine.d.ts +43 -0
  537. package/dist/wikimatch/wikispine.js +399 -0
  538. package/dist/wikimatch/wikispine.js.map +1 -0
  539. package/dist/wikimatch/window.d.ts +6 -0
  540. package/dist/wikimatch/window.js +83 -0
  541. package/dist/wikimatch/window.js.map +1 -0
  542. package/dist/wikimatch/words.d.ts +16 -0
  543. package/dist/wikimatch/words.js +66 -0
  544. package/dist/wikimatch/words.js.map +1 -0
  545. package/dist/wikipage/cache.d.ts +18 -0
  546. package/dist/wikipage/cache.js +404 -0
  547. package/dist/wikipage/cache.js.map +1 -0
  548. package/dist/wikipage/fetch-log.d.ts +17 -0
  549. package/dist/wikipage/fetch-log.js +143 -0
  550. package/dist/wikipage/fetch-log.js.map +1 -0
  551. package/dist/wikipage/index.d.ts +14 -0
  552. package/dist/wikipage/index.js +17 -0
  553. package/dist/wikipage/index.js.map +1 -0
  554. package/dist/wikipage/normalizer.d.ts +14 -0
  555. package/dist/wikipage/normalizer.js +147 -0
  556. package/dist/wikipage/normalizer.js.map +1 -0
  557. package/dist/wikipage/rate-limiter.d.ts +13 -0
  558. package/dist/wikipage/rate-limiter.js +84 -0
  559. package/dist/wikipage/rate-limiter.js.map +1 -0
  560. package/dist/wikipage/resolver.d.ts +26 -0
  561. package/dist/wikipage/resolver.js +325 -0
  562. package/dist/wikipage/resolver.js.map +1 -0
  563. package/dist/wikipage/types.d.ts +96 -0
  564. package/dist/wikipage/types.js +1 -0
  565. package/dist/wikipage/types.js.map +1 -0
  566. package/dist/wikipage/wikimedia-client.d.ts +48 -0
  567. package/dist/wikipage/wikimedia-client.js +397 -0
  568. package/dist/wikipage/wikimedia-client.js.map +1 -0
  569. package/dist/worker-BgQ0Xl8T.d.cts +181 -0
  570. package/dist/worker.cjs +5259 -0
  571. package/dist/worker.cjs.map +1 -0
  572. package/dist/worker.d.cts +2 -0
  573. package/dist/worker.d.ts +2 -0
  574. package/dist/worker.js +5 -0
  575. package/dist/worker.js.map +1 -0
  576. package/package.json +78 -0
@@ -0,0 +1,783 @@
1
+ import { normalizeText, splitTextIntoSentences } from "./text.js";
2
+ const MIN_AUTO_RESOLVE_GAP = 0.07;
3
+ const MIN_AUTO_RESOLVE_SCORE = 0.9;
4
+ const MIN_BOUNDARY_BONUS_LENGTH = 12;
5
+ const MIN_CANDIDATE_SCORE = 0.55;
6
+ const VERY_HIGH_CONFIDENCE_SCORE = 0.97;
7
+ const MAX_BOUNDARY_BONUS = 0.08;
8
+ const MAX_CANDIDATE_DISPLAY = 3;
9
+ class EvidenceResolver {
10
+ resolve(evidence, candidateSentenceIds, candidateTexts) {
11
+ const startValue = evidence.start_anchor ?? evidence.start;
12
+ const endValue = evidence.end_anchor ?? evidence.end;
13
+ const [startAnchor, startFailure] = this.parseAnchor(
14
+ startValue,
15
+ "start_anchor"
16
+ );
17
+ if (startFailure !== void 0) {
18
+ return [void 0, startFailure];
19
+ }
20
+ let endAnchor;
21
+ if (endValue !== void 0) {
22
+ const [parsedEndAnchor, endFailure] = this.parseAnchor(
23
+ endValue,
24
+ "end_anchor"
25
+ );
26
+ if (endFailure !== void 0) {
27
+ return [void 0, endFailure];
28
+ }
29
+ endAnchor = parsedEndAnchor;
30
+ }
31
+ const [startCandidate, startStrategy, startResolveFailure] = this.resolveAnchor({
32
+ anchor: startAnchor,
33
+ candidateSentenceIds,
34
+ candidateTexts,
35
+ label: "start_anchor"
36
+ });
37
+ if (startResolveFailure !== void 0) {
38
+ return [void 0, startResolveFailure];
39
+ }
40
+ if (startCandidate === void 0) {
41
+ return [
42
+ void 0,
43
+ {
44
+ candidates: [],
45
+ code: "none",
46
+ fieldName: "start_anchor",
47
+ message: "start_anchor could not be matched: no candidate selected"
48
+ }
49
+ ];
50
+ }
51
+ if (endAnchor === void 0) {
52
+ return [
53
+ {
54
+ confidence: startCandidate.score,
55
+ sentenceIds: [startCandidate.sentenceId],
56
+ strategy: startStrategy
57
+ },
58
+ void 0
59
+ ];
60
+ }
61
+ const [endCandidate, endStrategy, endResolveFailure] = this.resolveAnchor({
62
+ anchor: endAnchor,
63
+ candidateSentenceIds,
64
+ candidateTexts,
65
+ label: "end_anchor",
66
+ minIndex: startCandidate.index
67
+ });
68
+ if (endResolveFailure !== void 0) {
69
+ return [void 0, endResolveFailure];
70
+ }
71
+ if (endCandidate === void 0) {
72
+ return [
73
+ void 0,
74
+ {
75
+ candidates: [],
76
+ code: "none",
77
+ fieldName: "end_anchor",
78
+ message: "end_anchor could not be matched: no candidate selected"
79
+ }
80
+ ];
81
+ }
82
+ if (endCandidate.index < startCandidate.index) {
83
+ return [
84
+ void 0,
85
+ {
86
+ candidates: [],
87
+ code: "invalid_range",
88
+ fieldName: "end_anchor",
89
+ message: `Invalid evidence range: end_anchor resolved before start_anchor (${startCandidate.occurrenceId} -> ${endCandidate.occurrenceId}).`
90
+ }
91
+ ];
92
+ }
93
+ return [
94
+ {
95
+ confidence: Math.min(startCandidate.score, endCandidate.score),
96
+ sentenceIds: candidateSentenceIds.slice(
97
+ startCandidate.index,
98
+ endCandidate.index + 1
99
+ ),
100
+ strategy: `${startStrategy}+${endStrategy}`
101
+ },
102
+ void 0
103
+ ];
104
+ }
105
+ resolveWithOverrides(input) {
106
+ const startValue = input.evidence.start_anchor ?? input.evidence.start;
107
+ const endValue = input.evidence.end_anchor ?? input.evidence.end;
108
+ const [startAnchor, startFailure] = this.parseAnchor(
109
+ startValue,
110
+ "start_anchor"
111
+ );
112
+ if (startFailure !== void 0) {
113
+ return [void 0, startFailure];
114
+ }
115
+ const startCandidateOverride = input.overrides.start_anchor;
116
+ const [startCandidate, startStrategy, startResolveFailure] = startCandidateOverride === void 0 ? this.resolveAnchor({
117
+ anchor: startAnchor,
118
+ candidateSentenceIds: input.candidateSentenceIds,
119
+ candidateTexts: input.candidateTexts,
120
+ label: "start_anchor"
121
+ }) : [startCandidateOverride, "choice_final", void 0];
122
+ if (startResolveFailure !== void 0) {
123
+ return [void 0, startResolveFailure];
124
+ }
125
+ if (startCandidate === void 0) {
126
+ return [
127
+ void 0,
128
+ {
129
+ candidates: [],
130
+ code: "none",
131
+ fieldName: "start_anchor",
132
+ message: "start_anchor could not be matched: no candidate selected"
133
+ }
134
+ ];
135
+ }
136
+ if (endValue === void 0) {
137
+ return [
138
+ {
139
+ confidence: startCandidate.score,
140
+ sentenceIds: [startCandidate.sentenceId],
141
+ strategy: startStrategy
142
+ },
143
+ void 0
144
+ ];
145
+ }
146
+ const [endAnchor, endFailure] = this.parseAnchor(endValue, "end_anchor");
147
+ if (endFailure !== void 0) {
148
+ return [void 0, endFailure];
149
+ }
150
+ const endCandidateOverride = input.overrides.end_anchor;
151
+ const [endCandidate, endStrategy, endResolveFailure] = endCandidateOverride === void 0 ? this.resolveAnchor({
152
+ anchor: endAnchor,
153
+ candidateSentenceIds: input.candidateSentenceIds,
154
+ candidateTexts: input.candidateTexts,
155
+ label: "end_anchor",
156
+ minIndex: startCandidate.index
157
+ }) : [endCandidateOverride, "choice_final", void 0];
158
+ if (endResolveFailure !== void 0) {
159
+ return [void 0, endResolveFailure];
160
+ }
161
+ if (endCandidate === void 0) {
162
+ return [
163
+ void 0,
164
+ {
165
+ candidates: [],
166
+ code: "none",
167
+ fieldName: "end_anchor",
168
+ message: "end_anchor could not be matched: no candidate selected"
169
+ }
170
+ ];
171
+ }
172
+ if (endCandidate.index < startCandidate.index) {
173
+ return [
174
+ void 0,
175
+ {
176
+ candidates: [],
177
+ code: "invalid_range",
178
+ fieldName: "end_anchor",
179
+ message: `Invalid evidence range: end_anchor resolved before start_anchor (${startCandidate.occurrenceId} -> ${endCandidate.occurrenceId}).`
180
+ }
181
+ ];
182
+ }
183
+ return [
184
+ {
185
+ confidence: Math.min(startCandidate.score, endCandidate.score),
186
+ sentenceIds: input.candidateSentenceIds.slice(
187
+ startCandidate.index,
188
+ endCandidate.index + 1
189
+ ),
190
+ strategy: `${startStrategy}+${endStrategy}`
191
+ },
192
+ void 0
193
+ ];
194
+ }
195
+ parseAnchor(value, fieldName) {
196
+ const [anchor, error] = this.#parseAnchor(value);
197
+ if (error !== void 0) {
198
+ return [
199
+ void 0,
200
+ {
201
+ candidates: [],
202
+ code: "invalid_anchor",
203
+ fieldName,
204
+ message: `Invalid evidence.${fieldName}: ${error}`
205
+ }
206
+ ];
207
+ }
208
+ return [this.#normalizeAnchor(anchor, fieldName), void 0];
209
+ }
210
+ resolveAnchor(input) {
211
+ if (input.anchor === void 0) {
212
+ return [
213
+ void 0,
214
+ "invalid",
215
+ {
216
+ candidates: [],
217
+ code: "invalid",
218
+ fieldName: input.label,
219
+ message: `${input.label} is missing`
220
+ }
221
+ ];
222
+ }
223
+ let candidates = this.rankAnchor(
224
+ input.anchor,
225
+ input.candidateSentenceIds,
226
+ input.candidateTexts,
227
+ input.label
228
+ );
229
+ const minIndex = input.minIndex;
230
+ if (minIndex !== void 0) {
231
+ candidates = candidates.filter(
232
+ (candidate) => candidate.index >= minIndex
233
+ );
234
+ }
235
+ if (candidates.length === 0) {
236
+ return [
237
+ void 0,
238
+ "none",
239
+ {
240
+ candidates: [],
241
+ code: "none",
242
+ fieldName: input.label,
243
+ message: `${input.label} could not be matched: no candidates available`
244
+ }
245
+ ];
246
+ }
247
+ const exactRaw = candidates.filter((candidate) => candidate.exactRaw);
248
+ if (exactRaw.length === 1) {
249
+ return [exactRaw[0], "exact_raw", void 0];
250
+ }
251
+ if (exactRaw.length > 1) {
252
+ return [
253
+ void 0,
254
+ "ambiguous_exact_raw",
255
+ this.#buildAmbiguousFailure(
256
+ input.label,
257
+ "ambiguous_exact_raw",
258
+ exactRaw
259
+ )
260
+ ];
261
+ }
262
+ const exactSubstring = candidates.filter(
263
+ (candidate) => candidate.exactSubstring
264
+ );
265
+ if (exactSubstring.length === 1 && this.#anchorLength(input.anchor) >= 8) {
266
+ return [exactSubstring[0], "exact_substring", void 0];
267
+ }
268
+ if (exactSubstring.length > 1 && this.#anchorLength(input.anchor) >= 8) {
269
+ const selected = this.#tryAutoSelectScoredDuplicates(exactSubstring);
270
+ if (selected !== void 0) {
271
+ return [selected, "exact_substring_scored", void 0];
272
+ }
273
+ return [
274
+ void 0,
275
+ "ambiguous_exact_substring",
276
+ this.#buildAmbiguousFailure(
277
+ input.label,
278
+ "ambiguous_exact_substring",
279
+ exactSubstring
280
+ )
281
+ ];
282
+ }
283
+ const exactNormalized = candidates.filter(
284
+ (candidate) => candidate.exactNormalized
285
+ );
286
+ if (exactNormalized.length === 1) {
287
+ return [exactNormalized[0], "exact_normalized", void 0];
288
+ }
289
+ if (exactNormalized.length > 1) {
290
+ const selected = this.#tryAutoSelectScoredDuplicates(exactNormalized);
291
+ if (selected !== void 0) {
292
+ return [selected, "exact_normalized_scored", void 0];
293
+ }
294
+ return [
295
+ void 0,
296
+ "ambiguous_exact_normalized",
297
+ this.#buildAmbiguousFailure(
298
+ input.label,
299
+ "ambiguous_exact_normalized",
300
+ exactNormalized
301
+ )
302
+ ];
303
+ }
304
+ const topCandidate = candidates[0];
305
+ if (topCandidate === void 0) {
306
+ return [
307
+ void 0,
308
+ "none",
309
+ {
310
+ candidates: [],
311
+ code: "none",
312
+ fieldName: input.label,
313
+ message: `${input.label} could not be matched: no candidates available`
314
+ }
315
+ ];
316
+ }
317
+ const secondScore = candidates[1]?.score ?? 0;
318
+ const gap = topCandidate.score - secondScore;
319
+ if (topCandidate.score < MIN_CANDIDATE_SCORE) {
320
+ return [
321
+ void 0,
322
+ "low_confidence",
323
+ {
324
+ candidates: candidates.slice(0, MAX_CANDIDATE_DISPLAY),
325
+ code: "low_confidence",
326
+ fieldName: input.label,
327
+ message: `${input.label} could not be matched confidently. Best candidate score=${topCandidate.score.toFixed(3)}.`
328
+ }
329
+ ];
330
+ }
331
+ if (candidates.length === 1) {
332
+ return [topCandidate, "auto_top1_single_candidate", void 0];
333
+ }
334
+ if (topCandidate.score >= VERY_HIGH_CONFIDENCE_SCORE) {
335
+ return [topCandidate, "auto_top1_very_high", void 0];
336
+ }
337
+ if (topCandidate.score >= MIN_AUTO_RESOLVE_SCORE && gap >= MIN_AUTO_RESOLVE_GAP) {
338
+ return [topCandidate, "auto_top1", void 0];
339
+ }
340
+ return [
341
+ void 0,
342
+ "ambiguous_ranked",
343
+ this.#buildAmbiguousFailure(input.label, "ambiguous_ranked", candidates)
344
+ ];
345
+ }
346
+ rankAnchor(anchor, candidateSentenceIds, candidateTexts, label) {
347
+ const ranked = [];
348
+ for (const [index, sentenceId] of candidateSentenceIds.entries()) {
349
+ const text = candidateTexts[index] ?? "";
350
+ const prevText = index > 0 ? candidateTexts[index - 1] ?? "" : "";
351
+ const nextText = index < candidateTexts.length - 1 ? candidateTexts[index + 1] ?? "" : "";
352
+ const match = this.#scoreAnchor(anchor, text, label);
353
+ ranked.push({
354
+ exactNormalized: match.exactNormalized,
355
+ exactRaw: match.exactRaw,
356
+ exactSubstring: match.exactSubstring,
357
+ index,
358
+ nextText,
359
+ occurrenceId: `S${index + 1}`,
360
+ prevText,
361
+ score: match.score,
362
+ sentenceId,
363
+ text
364
+ });
365
+ }
366
+ ranked.sort((left, right) => {
367
+ if (left.score !== right.score) {
368
+ return right.score - left.score;
369
+ }
370
+ if (left.exactRaw !== right.exactRaw) {
371
+ return left.exactRaw ? -1 : 1;
372
+ }
373
+ if (left.exactSubstring !== right.exactSubstring) {
374
+ return left.exactSubstring ? -1 : 1;
375
+ }
376
+ if (left.exactNormalized !== right.exactNormalized) {
377
+ return left.exactNormalized ? -1 : 1;
378
+ }
379
+ return left.index - right.index;
380
+ });
381
+ return ranked;
382
+ }
383
+ #scoreAnchor(anchor, candidateText, label) {
384
+ if (anchor.mode === "head_tail") {
385
+ return this.#scoreHeadTail(anchor, candidateText);
386
+ }
387
+ return this.#scoreTextQuery(anchor.text ?? "", candidateText, label);
388
+ }
389
+ #scoreHeadTail(anchor, candidateText) {
390
+ const head = anchor.head ?? "";
391
+ const tail = anchor.tail ?? "";
392
+ const candidateNormalized = normalizeText(candidateText);
393
+ const headNormalized = normalizeText(head);
394
+ const tailNormalized = normalizeText(tail);
395
+ if (headNormalized === "" || tailNormalized === "") {
396
+ return createEmptyTextMatchScore();
397
+ }
398
+ const headPosition = candidateNormalized.indexOf(headNormalized);
399
+ const tailPosition = candidateNormalized.lastIndexOf(tailNormalized);
400
+ if (headPosition !== -1 && tailPosition !== -1 && headPosition <= tailPosition) {
401
+ const coveredChars = headNormalized.length + tailNormalized.length;
402
+ const coverage = Math.min(
403
+ 1,
404
+ coveredChars / Math.max(1, candidateNormalized.length)
405
+ );
406
+ return {
407
+ exactNormalized: false,
408
+ exactRaw: false,
409
+ exactSubstring: true,
410
+ matchEnd: tailPosition + tailNormalized.length,
411
+ matchStart: headPosition,
412
+ score: 0.82 + 0.18 * coverage
413
+ };
414
+ }
415
+ const headMatch = this.#scoreTextQuery(head, candidateText, "start_anchor");
416
+ const tailMatch = this.#scoreTextQuery(tail, candidateText, "end_anchor");
417
+ const ordered = headMatch.matchStart <= tailMatch.matchStart ? 1 : 0.85;
418
+ return {
419
+ exactNormalized: false,
420
+ exactRaw: false,
421
+ exactSubstring: false,
422
+ matchEnd: Math.max(headMatch.matchEnd, tailMatch.matchEnd),
423
+ matchStart: Math.min(headMatch.matchStart, tailMatch.matchStart),
424
+ score: (headMatch.score + tailMatch.score) / 2 * ordered
425
+ };
426
+ }
427
+ #scoreTextQuery(queryText, candidateText, label) {
428
+ const queryRaw = queryText.trim();
429
+ const candidateRaw = candidateText.trim();
430
+ const queryNormalized = normalizeText(queryRaw);
431
+ const candidateNormalized = normalizeText(candidateRaw);
432
+ if (queryNormalized === "" || candidateNormalized === "") {
433
+ return createEmptyTextMatchScore();
434
+ }
435
+ if (queryRaw === candidateRaw) {
436
+ return {
437
+ exactNormalized: true,
438
+ exactRaw: true,
439
+ exactSubstring: true,
440
+ matchEnd: candidateNormalized.length,
441
+ matchStart: 0,
442
+ score: 1
443
+ };
444
+ }
445
+ if (queryNormalized === candidateNormalized) {
446
+ return {
447
+ exactNormalized: true,
448
+ exactRaw: false,
449
+ exactSubstring: false,
450
+ matchEnd: candidateNormalized.length,
451
+ matchStart: 0,
452
+ score: 0.995
453
+ };
454
+ }
455
+ if (candidateNormalized.includes(queryNormalized)) {
456
+ const matchStart = candidateNormalized.indexOf(queryNormalized);
457
+ const coverage = queryNormalized.length / Math.max(1, candidateNormalized.length);
458
+ const boundaryBonus = this.#boundaryBonus({
459
+ candidateNormalized,
460
+ label,
461
+ matchEnd: matchStart + queryNormalized.length,
462
+ matchStart,
463
+ queryNormalized
464
+ });
465
+ return {
466
+ exactNormalized: false,
467
+ exactRaw: false,
468
+ exactSubstring: true,
469
+ matchEnd: matchStart + queryNormalized.length,
470
+ matchStart,
471
+ score: Math.min(1, 0.75 + 0.25 * coverage + boundaryBonus)
472
+ };
473
+ }
474
+ if (queryNormalized.includes(candidateNormalized)) {
475
+ const coverage = candidateNormalized.length / Math.max(1, queryNormalized.length);
476
+ return {
477
+ exactNormalized: false,
478
+ exactRaw: false,
479
+ exactSubstring: false,
480
+ matchEnd: candidateNormalized.length,
481
+ matchStart: 0,
482
+ score: Math.min(0.92, 0.78 + 0.14 * coverage)
483
+ };
484
+ }
485
+ let bestScore = 0;
486
+ let bestStart = -1;
487
+ let bestEnd = -1;
488
+ const maxWindow = Math.min(
489
+ candidateNormalized.length,
490
+ Math.max(
491
+ queryNormalized.length,
492
+ Math.floor(queryNormalized.length * 1.2)
493
+ )
494
+ );
495
+ const minWindow = Math.min(
496
+ maxWindow,
497
+ Math.max(1, Math.floor(queryNormalized.length * 0.8))
498
+ );
499
+ const queryChars = new Set(queryNormalized);
500
+ for (let windowSize = minWindow; windowSize <= maxWindow; windowSize += 1) {
501
+ for (let start = 0; start <= candidateNormalized.length - windowSize; start += 1) {
502
+ const window = candidateNormalized.slice(start, start + windowSize);
503
+ const windowChars = new Set(window);
504
+ const unionSize = (/* @__PURE__ */ new Set([...queryChars, ...windowChars])).size;
505
+ const overlapSize = [...queryChars].filter(
506
+ (char) => windowChars.has(char)
507
+ ).length;
508
+ const jaccard = unionSize === 0 ? 0 : overlapSize / unionSize;
509
+ if (jaccard < 0.35) {
510
+ continue;
511
+ }
512
+ const totalScore = 0.35 * charNgramScore(queryNormalized, window) + 0.35 * sequenceSimilarity(queryNormalized, window) + 0.2 * levenshteinSimilarity(queryNormalized, window) + 0.1 * lengthPenalty(queryNormalized, window) + this.#boundaryBonus({
513
+ candidateNormalized,
514
+ label,
515
+ matchEnd: start + windowSize,
516
+ matchStart: start,
517
+ queryNormalized
518
+ });
519
+ if (totalScore > bestScore) {
520
+ bestScore = Math.min(1, totalScore);
521
+ bestStart = start;
522
+ bestEnd = start + windowSize;
523
+ }
524
+ }
525
+ }
526
+ return {
527
+ exactNormalized: false,
528
+ exactRaw: false,
529
+ exactSubstring: false,
530
+ matchEnd: bestEnd,
531
+ matchStart: bestStart,
532
+ score: bestScore
533
+ };
534
+ }
535
+ #parseAnchor(value) {
536
+ if (value === void 0 || value === null) {
537
+ return [void 0, "anchor is required"];
538
+ }
539
+ if (typeof value === "string") {
540
+ const stripped = value.trim();
541
+ if (stripped === "") {
542
+ return [void 0, "anchor string is empty"];
543
+ }
544
+ if (stripped.includes("...")) {
545
+ const parts = stripped.split("...").map((part) => part.trim()).filter((part) => part !== "");
546
+ if (parts.length >= 2) {
547
+ const head2 = parts[0];
548
+ const tail2 = parts[parts.length - 1];
549
+ if (head2 === void 0 || tail2 === void 0) {
550
+ return [void 0, "anchor string is empty"];
551
+ }
552
+ return [
553
+ {
554
+ head: head2,
555
+ mode: "head_tail",
556
+ tail: tail2
557
+ },
558
+ void 0
559
+ ];
560
+ }
561
+ }
562
+ return [{ mode: "full", text: stripped }, void 0];
563
+ }
564
+ if (typeof value !== "object") {
565
+ return [void 0, `expected string or object, got ${typeof value}`];
566
+ }
567
+ const rawValue = value;
568
+ const mode = rawValue.mode === "head_tail" || rawValue.head !== void 0 ? "head_tail" : "full";
569
+ if (mode === "full") {
570
+ const text = typeof rawValue.text === "string" ? rawValue.text.trim() : "";
571
+ if (text === "") {
572
+ return [void 0, "full anchor requires non-empty 'text'"];
573
+ }
574
+ return [{ mode: "full", text }, void 0];
575
+ }
576
+ const head = typeof rawValue.head === "string" ? rawValue.head.trim() : "";
577
+ const tail = typeof rawValue.tail === "string" ? rawValue.tail.trim() : "";
578
+ if (head !== "" && tail === "") {
579
+ return [{ mode: "full", text: head }, void 0];
580
+ }
581
+ if (head === "" && tail !== "") {
582
+ return [{ mode: "full", text: tail }, void 0];
583
+ }
584
+ if (head === "" || tail === "") {
585
+ return [
586
+ void 0,
587
+ "head_tail anchor requires non-empty 'head' and 'tail'"
588
+ ];
589
+ }
590
+ return [{ head, mode: "head_tail", tail }, void 0];
591
+ }
592
+ #normalizeAnchor(anchor, fieldName) {
593
+ if (anchor === void 0) {
594
+ return void 0;
595
+ }
596
+ if (anchor.mode === "head_tail") {
597
+ const boundaryText = fieldName === "end_anchor" ? this.#selectBoundarySentence(anchor.tail ?? "", "end_anchor") : this.#selectBoundarySentence(anchor.head ?? "", "start_anchor");
598
+ return boundaryText === "" ? void 0 : {
599
+ mode: "full",
600
+ text: boundaryText
601
+ };
602
+ }
603
+ const normalizedText = this.#selectBoundarySentence(
604
+ anchor.text ?? "",
605
+ fieldName
606
+ );
607
+ return {
608
+ mode: "full",
609
+ text: normalizedText
610
+ };
611
+ }
612
+ #selectBoundarySentence(text, fieldName) {
613
+ const sentences = splitTextIntoSentences(text);
614
+ if (sentences.length <= 1) {
615
+ return text.trim();
616
+ }
617
+ return fieldName === "end_anchor" ? sentences[sentences.length - 1] ?? text.trim() : sentences[0] ?? text.trim();
618
+ }
619
+ #boundaryBonus(input) {
620
+ if (input.matchStart < 0 || input.matchEnd < 0) {
621
+ return 0;
622
+ }
623
+ if (input.queryNormalized.length < MIN_BOUNDARY_BONUS_LENGTH) {
624
+ return 0;
625
+ }
626
+ if (input.label === "start_anchor" && input.matchStart !== 0) {
627
+ return 0;
628
+ }
629
+ if (input.label === "end_anchor" && input.matchEnd !== input.candidateNormalized.length) {
630
+ return 0;
631
+ }
632
+ const coverage = input.queryNormalized.length / Math.max(1, input.candidateNormalized.length);
633
+ return MAX_BOUNDARY_BONUS * (0.5 + 0.5 * Math.min(1, coverage));
634
+ }
635
+ #tryAutoSelectScoredDuplicates(candidates) {
636
+ if (candidates.length === 0) {
637
+ return void 0;
638
+ }
639
+ if (candidates.length === 1) {
640
+ return candidates[0];
641
+ }
642
+ const topCandidate = candidates[0];
643
+ if (topCandidate === void 0) {
644
+ return void 0;
645
+ }
646
+ const secondScore = candidates[1]?.score ?? 0;
647
+ if (topCandidate.score >= MIN_AUTO_RESOLVE_SCORE && topCandidate.score - secondScore >= MIN_AUTO_RESOLVE_GAP) {
648
+ return topCandidate;
649
+ }
650
+ return void 0;
651
+ }
652
+ #buildAmbiguousFailure(label, code, candidates) {
653
+ const topCandidates = candidates.slice(0, MAX_CANDIDATE_DISPLAY);
654
+ const formatted = topCandidates.map((candidate) => formatCandidate(candidate)).join("\n");
655
+ return {
656
+ candidates: topCandidates,
657
+ code,
658
+ fieldName: label,
659
+ message: `${label} is ambiguous. Top candidates:
660
+ ${formatted}
661
+ Please provide a more specific anchor or surrounding context.`
662
+ };
663
+ }
664
+ #anchorLength(anchor) {
665
+ if (anchor.mode === "head_tail") {
666
+ return normalizeText(anchor.head ?? "").length + normalizeText(anchor.tail ?? "").length;
667
+ }
668
+ return normalizeText(anchor.text ?? "").length;
669
+ }
670
+ }
671
+ function createEmptyTextMatchScore() {
672
+ return {
673
+ exactNormalized: false,
674
+ exactRaw: false,
675
+ exactSubstring: false,
676
+ matchEnd: -1,
677
+ matchStart: -1,
678
+ score: 0
679
+ };
680
+ }
681
+ function charNgramScore(left, right) {
682
+ const leftBigrams = charNgrams(left, 2);
683
+ const rightBigrams = charNgrams(right, 2);
684
+ const leftTrigrams = charNgrams(left, 3);
685
+ const rightTrigrams = charNgrams(right, 3);
686
+ return (diceCoefficient(leftBigrams, rightBigrams) + diceCoefficient(leftTrigrams, rightTrigrams)) / 2;
687
+ }
688
+ function charNgrams(text, size) {
689
+ if (text.length <= size) {
690
+ return text === "" ? /* @__PURE__ */ new Set() : /* @__PURE__ */ new Set([text]);
691
+ }
692
+ const result = /* @__PURE__ */ new Set();
693
+ for (let index = 0; index <= text.length - size; index += 1) {
694
+ result.add(text.slice(index, index + size));
695
+ }
696
+ return result;
697
+ }
698
+ function diceCoefficient(left, right) {
699
+ if (left.size === 0 || right.size === 0) {
700
+ return 0;
701
+ }
702
+ let overlap = 0;
703
+ for (const value of left) {
704
+ if (right.has(value)) {
705
+ overlap += 1;
706
+ }
707
+ }
708
+ return 2 * overlap / (left.size + right.size);
709
+ }
710
+ function levenshteinSimilarity(left, right) {
711
+ if (left === right) {
712
+ return 1;
713
+ }
714
+ if (left === "" || right === "") {
715
+ return 0;
716
+ }
717
+ let previous = Array.from({ length: right.length + 1 }, (_, index) => index);
718
+ let current = new Array(right.length + 1).fill(0);
719
+ for (const [leftIndex, leftChar] of [...left].entries()) {
720
+ current[0] = leftIndex + 1;
721
+ for (const [rightIndex, rightChar] of [...right].entries()) {
722
+ const insertCost = (current[rightIndex] ?? 0) + 1;
723
+ const deleteCost = (previous[rightIndex + 1] ?? 0) + 1;
724
+ const replaceCost = (previous[rightIndex] ?? 0) + (leftChar === rightChar ? 0 : 1);
725
+ current[rightIndex + 1] = Math.min(insertCost, deleteCost, replaceCost);
726
+ }
727
+ [previous, current] = [current, previous];
728
+ }
729
+ const distance = previous[right.length] ?? Math.max(left.length, right.length);
730
+ return 1 - distance / Math.max(left.length, right.length);
731
+ }
732
+ function lengthPenalty(left, right) {
733
+ return 1 - Math.abs(left.length - right.length) / Math.max(left.length, right.length);
734
+ }
735
+ function sequenceSimilarity(left, right) {
736
+ if (left === right) {
737
+ return 1;
738
+ }
739
+ const leftLength = left.length;
740
+ const rightLength = right.length;
741
+ if (leftLength === 0 || rightLength === 0) {
742
+ return 0;
743
+ }
744
+ const matrix = Array.from(
745
+ { length: leftLength + 1 },
746
+ () => new Array(rightLength + 1).fill(0)
747
+ );
748
+ let longest = 0;
749
+ for (let leftIndex = 1; leftIndex <= leftLength; leftIndex += 1) {
750
+ for (let rightIndex = 1; rightIndex <= rightLength; rightIndex += 1) {
751
+ if (left[leftIndex - 1] !== right[rightIndex - 1]) {
752
+ continue;
753
+ }
754
+ const currentRow = matrix[leftIndex];
755
+ if (currentRow === void 0) {
756
+ continue;
757
+ }
758
+ currentRow[rightIndex] = (matrix[leftIndex - 1]?.[rightIndex - 1] ?? 0) + 1;
759
+ longest = Math.max(longest, currentRow[rightIndex] ?? 0);
760
+ }
761
+ }
762
+ return 2 * longest / (leftLength + rightLength);
763
+ }
764
+ function formatCandidate(candidate) {
765
+ return ` - ${candidate.occurrenceId} score=${candidate.score.toFixed(3)}
766
+ prev: ${truncate(candidate.prevText)}
767
+ text: ${truncate(candidate.text, 120)}
768
+ next: ${truncate(candidate.nextText)}`;
769
+ }
770
+ function truncate(text, limit = 80) {
771
+ const stripped = text.replace(/\s+/gu, " ").trim();
772
+ if (stripped === "") {
773
+ return "(none)";
774
+ }
775
+ if (stripped.length <= limit) {
776
+ return stripped;
777
+ }
778
+ return `${stripped.slice(0, limit - 3)}...`;
779
+ }
780
+ export {
781
+ EvidenceResolver
782
+ };
783
+ //# sourceMappingURL=anchor-resolver.js.map