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.
- package/LICENSE +201 -0
- package/data/editor/clue_reviewer.jinja +410 -0
- package/data/editor/clue_reviewer_generator.jinja +428 -0
- package/data/editor/revision_feedback.jinja +5 -0
- package/data/editor/text_compressor.jinja +127 -0
- package/data/guaranteed/response_intent_classifier.jinja +8 -0
- package/data/help/commands/archive/next.jinja +29 -0
- package/data/help/commands/gc.jinja +38 -0
- package/data/help/commands/legacy/migrate.jinja +20 -0
- package/data/help/commands/legacy.jinja +19 -0
- package/data/help/commands/maintenance/chapter/add.jinja +41 -0
- package/data/help/commands/maintenance/chapter/list.jinja +33 -0
- package/data/help/commands/maintenance/chapter/move.jinja +36 -0
- package/data/help/commands/maintenance/chapter/remove.jinja +26 -0
- package/data/help/commands/maintenance/chapter/reset.jinja +29 -0
- package/data/help/commands/maintenance/chapter/set-source.jinja +34 -0
- package/data/help/commands/maintenance/chapter/set-summary.jinja +31 -0
- package/data/help/commands/maintenance/chapter/set-title.jinja +29 -0
- package/data/help/commands/maintenance/chapter/tree.jinja +58 -0
- package/data/help/commands/maintenance/chapter.jinja +59 -0
- package/data/help/commands/maintenance/cover.jinja +28 -0
- package/data/help/commands/maintenance/meta.jinja +55 -0
- package/data/help/commands/predicate.jinja +460 -0
- package/data/help/commands/root.jinja +83 -0
- package/data/help/commands/transform.jinja +34 -0
- package/data/help/commands/uri.jinja +274 -0
- package/data/help/topics/config.jinja +88 -0
- package/data/help/topics/format.jinja +72 -0
- package/data/help/topics/readiness.jinja +99 -0
- package/data/help/topics/recipe.jinja +151 -0
- package/data/help/topics/runtime.jinja +62 -0
- package/data/help/topics/uri.jinja +178 -0
- package/data/output/epub/cover.xhtml.jinja +13 -0
- package/data/output/epub/nav.xhtml.jinja +12 -0
- package/data/output/epub/package.opf.xml.jinja +39 -0
- package/data/output/epub/section.xhtml.jinja +12 -0
- package/data/topologization/book_coherence_extraction.jinja +223 -0
- package/data/topologization/chunk_extraction.jinja +233 -0
- package/data/topologization/evidence_choice.jinja +38 -0
- package/data/topologization/translate_chunks.jinja +138 -0
- package/data/topologization/user_focused_extraction.jinja +378 -0
- package/dist/archive/query/archive-view.d.ts +26 -0
- package/dist/archive/query/archive-view.js +4540 -0
- package/dist/archive/query/archive-view.js.map +1 -0
- package/dist/archive/query/continuation-cursor.d.ts +61 -0
- package/dist/archive/query/continuation-cursor.js +363 -0
- package/dist/archive/query/continuation-cursor.js.map +1 -0
- package/dist/archive/query/index.d.ts +29 -0
- package/dist/archive/query/index.js +63 -0
- package/dist/archive/query/index.js.map +1 -0
- package/dist/archive/query/lexical-search.d.ts +22 -0
- package/dist/archive/query/lexical-search.js +137 -0
- package/dist/archive/query/lexical-search.js.map +1 -0
- package/dist/archive/query/search-cache.d.ts +192 -0
- package/dist/archive/query/search-cache.js +1248 -0
- package/dist/archive/query/search-cache.js.map +1 -0
- package/dist/archive/search-index/index.d.ts +26 -0
- package/dist/archive/search-index/index.js +29 -0
- package/dist/archive/search-index/index.js.map +1 -0
- package/dist/archive/search-index/search-index.d.ts +26 -0
- package/dist/archive/search-index/search-index.js +555 -0
- package/dist/archive/search-index/search-index.js.map +1 -0
- package/dist/archive/search-index/search-tokenizer.d.ts +16 -0
- package/dist/archive/search-index/search-tokenizer.js +225 -0
- package/dist/archive/search-index/search-tokenizer.js.map +1 -0
- package/dist/common/cli-command.d.ts +4 -0
- package/dist/common/cli-command.js +7 -0
- package/dist/common/cli-command.js.map +1 -0
- package/dist/common/data-dir.d.ts +3 -0
- package/dist/common/data-dir.js +48 -0
- package/dist/common/data-dir.js.map +1 -0
- package/dist/common/language.d.ts +55 -0
- package/dist/common/language.js +208 -0
- package/dist/common/language.js.map +1 -0
- package/dist/common/llm-scope.d.ts +19 -0
- package/dist/common/llm-scope.js +31 -0
- package/dist/common/llm-scope.js.map +1 -0
- package/dist/common/logging.d.ts +22 -0
- package/dist/common/logging.js +141 -0
- package/dist/common/logging.js.map +1 -0
- package/dist/common/template.d.ts +8 -0
- package/dist/common/template.js +54 -0
- package/dist/common/template.js.map +1 -0
- package/dist/common/tinyld-language.d.ts +6 -0
- package/dist/common/tinyld-language.js +34 -0
- package/dist/common/tinyld-language.js.map +1 -0
- package/dist/common/wiki-graph-dir.d.ts +10 -0
- package/dist/common/wiki-graph-dir.js +41 -0
- package/dist/common/wiki-graph-dir.js.map +1 -0
- package/dist/common/wiki-graph-temp.d.ts +6 -0
- package/dist/common/wiki-graph-temp.js +21 -0
- package/dist/common/wiki-graph-temp.js.map +1 -0
- package/dist/common/wiki-graph-uri.d.ts +28 -0
- package/dist/common/wiki-graph-uri.js +171 -0
- package/dist/common/wiki-graph-uri.js.map +1 -0
- package/dist/context/index.d.ts +1 -0
- package/dist/context/index.js +13 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/task-context.d.ts +41 -0
- package/dist/context/task-context.js +111 -0
- package/dist/context/task-context.js.map +1 -0
- package/dist/document/database.d.ts +30 -0
- package/dist/document/database.js +273 -0
- package/dist/document/database.js.map +1 -0
- package/dist/document/document.d.ts +153 -0
- package/dist/document/document.js +615 -0
- package/dist/document/document.js.map +1 -0
- package/dist/document/fragments.d.ts +63 -0
- package/dist/document/fragments.js +371 -0
- package/dist/document/fragments.js.map +1 -0
- package/dist/document/index.d.ts +13 -0
- package/dist/document/index.js +62 -0
- package/dist/document/index.js.map +1 -0
- package/dist/document/schema.d.ts +8 -0
- package/dist/document/schema.js +472 -0
- package/dist/document/schema.js.map +1 -0
- package/dist/document/shared-state-database.d.ts +9 -0
- package/dist/document/shared-state-database.js +193 -0
- package/dist/document/shared-state-database.js.map +1 -0
- package/dist/document/stores.d.ts +203 -0
- package/dist/document/stores.js +1678 -0
- package/dist/document/stores.js.map +1 -0
- package/dist/document/text-streams.d.ts +86 -0
- package/dist/document/text-streams.js +515 -0
- package/dist/document/text-streams.js.map +1 -0
- package/dist/document/types.d.ts +148 -0
- package/dist/document/types.js +69 -0
- package/dist/document/types.js.map +1 -0
- package/dist/editor/clue.d.ts +27 -0
- package/dist/editor/clue.js +158 -0
- package/dist/editor/clue.js.map +1 -0
- package/dist/editor/compressor.d.ts +18 -0
- package/dist/editor/compressor.js +68 -0
- package/dist/editor/compressor.js.map +1 -0
- package/dist/editor/editor.d.ts +39 -0
- package/dist/editor/editor.js +253 -0
- package/dist/editor/editor.js.map +1 -0
- package/dist/editor/feedback.d.ts +16 -0
- package/dist/editor/feedback.js +104 -0
- package/dist/editor/feedback.js.map +1 -0
- package/dist/editor/index.d.ts +17 -0
- package/dist/editor/index.js +5 -0
- package/dist/editor/index.js.map +1 -0
- package/dist/editor/language-review.d.ts +14 -0
- package/dist/editor/language-review.js +35 -0
- package/dist/editor/language-review.js.map +1 -0
- package/dist/editor/log.d.ts +37 -0
- package/dist/editor/log.js +202 -0
- package/dist/editor/log.js.map +1 -0
- package/dist/editor/markup.d.ts +20 -0
- package/dist/editor/markup.js +294 -0
- package/dist/editor/markup.js.map +1 -0
- package/dist/editor/prompt-templates.d.ts +6 -0
- package/dist/editor/prompt-templates.js +11 -0
- package/dist/editor/prompt-templates.js.map +1 -0
- package/dist/editor/response.d.ts +5 -0
- package/dist/editor/response.js +31 -0
- package/dist/editor/response.js.map +1 -0
- package/dist/editor/review.d.ts +34 -0
- package/dist/editor/review.js +189 -0
- package/dist/editor/review.js.map +1 -0
- package/dist/editor/types.d.ts +31 -0
- package/dist/editor/types.js +21 -0
- package/dist/editor/types.js.map +1 -0
- package/dist/evidence-selection/anchor-resolver.d.ts +43 -0
- package/dist/evidence-selection/anchor-resolver.js +783 -0
- package/dist/evidence-selection/anchor-resolver.js.map +1 -0
- package/dist/evidence-selection/anchor-types.d.ts +27 -0
- package/dist/evidence-selection/anchor-types.js +1 -0
- package/dist/evidence-selection/anchor-types.js.map +1 -0
- package/dist/evidence-selection/index.d.ts +8 -0
- package/dist/evidence-selection/index.js +32 -0
- package/dist/evidence-selection/index.js.map +1 -0
- package/dist/evidence-selection/prompt.d.ts +18 -0
- package/dist/evidence-selection/prompt.js +54 -0
- package/dist/evidence-selection/prompt.js.map +1 -0
- package/dist/evidence-selection/quote-score.d.ts +29 -0
- package/dist/evidence-selection/quote-score.js +331 -0
- package/dist/evidence-selection/quote-score.js.map +1 -0
- package/dist/evidence-selection/selection-resolver.d.ts +19 -0
- package/dist/evidence-selection/selection-resolver.js +303 -0
- package/dist/evidence-selection/selection-resolver.js.map +1 -0
- package/dist/evidence-selection/text.d.ts +4 -0
- package/dist/evidence-selection/text.js +66 -0
- package/dist/evidence-selection/text.js.map +1 -0
- package/dist/evidence-selection/types.d.ts +36 -0
- package/dist/evidence-selection/types.js +1 -0
- package/dist/evidence-selection/types.js.map +1 -0
- package/dist/facade/app.d.ts +75 -0
- package/dist/facade/app.js +141 -0
- package/dist/facade/app.js.map +1 -0
- package/dist/facade/build-queue.d.ts +181 -0
- package/dist/facade/build-queue.js +1442 -0
- package/dist/facade/build-queue.js.map +1 -0
- package/dist/facade/chapter-build.d.ts +60 -0
- package/dist/facade/chapter-build.js +1020 -0
- package/dist/facade/chapter-build.js.map +1 -0
- package/dist/facade/chapter.d.ts +168 -0
- package/dist/facade/chapter.js +944 -0
- package/dist/facade/chapter.js.map +1 -0
- package/dist/facade/digest.d.ts +55 -0
- package/dist/facade/digest.js +168 -0
- package/dist/facade/digest.js.map +1 -0
- package/dist/facade/graph.d.ts +60 -0
- package/dist/facade/graph.js +203 -0
- package/dist/facade/graph.js.map +1 -0
- package/dist/facade/import.d.ts +49 -0
- package/dist/facade/import.js +262 -0
- package/dist/facade/import.js.map +1 -0
- package/dist/facade/index.d.ts +39 -0
- package/dist/facade/index.js +171 -0
- package/dist/facade/index.js.map +1 -0
- package/dist/facade/knowledge-graph-build.d.ts +75 -0
- package/dist/facade/knowledge-graph-build.js +897 -0
- package/dist/facade/knowledge-graph-build.js.map +1 -0
- package/dist/facade/llm-sampling.d.ts +11 -0
- package/dist/facade/llm-sampling.js +58 -0
- package/dist/facade/llm-sampling.js.map +1 -0
- package/dist/facade/types.d.ts +8 -0
- package/dist/facade/types.js +1 -0
- package/dist/facade/types.js.map +1 -0
- package/dist/facade/wiki-graph-archive.d.ts +41 -0
- package/dist/facade/wiki-graph-archive.js +130 -0
- package/dist/facade/wiki-graph-archive.js.map +1 -0
- package/dist/gc/files.d.ts +15 -0
- package/dist/gc/files.js +141 -0
- package/dist/gc/files.js.map +1 -0
- package/dist/gc/index.d.ts +2 -0
- package/dist/gc/index.js +5 -0
- package/dist/gc/index.js.map +1 -0
- package/dist/gc/lock.d.ts +3 -0
- package/dist/gc/lock.js +137 -0
- package/dist/gc/lock.js.map +1 -0
- package/dist/gc/runner.d.ts +9 -0
- package/dist/gc/runner.js +129 -0
- package/dist/gc/runner.js.map +1 -0
- package/dist/gc/tmp-gc.d.ts +5 -0
- package/dist/gc/tmp-gc.js +57 -0
- package/dist/gc/tmp-gc.js.map +1 -0
- package/dist/gc/types.d.ts +24 -0
- package/dist/gc/types.js +1 -0
- package/dist/gc/types.js.map +1 -0
- package/dist/gc.cjs +10041 -0
- package/dist/gc.cjs.map +1 -0
- package/dist/gc.d.cts +10 -0
- package/dist/gc.d.ts +2 -0
- package/dist/gc.js +5 -0
- package/dist/gc.js.map +1 -0
- package/dist/guaranteed/classifier.d.ts +10 -0
- package/dist/guaranteed/classifier.js +88 -0
- package/dist/guaranteed/classifier.js.map +1 -0
- package/dist/guaranteed/errors.d.ts +38 -0
- package/dist/guaranteed/errors.js +73 -0
- package/dist/guaranteed/errors.js.map +1 -0
- package/dist/guaranteed/index.d.ts +7 -0
- package/dist/guaranteed/index.js +21 -0
- package/dist/guaranteed/index.js.map +1 -0
- package/dist/guaranteed/request.d.ts +8 -0
- package/dist/guaranteed/request.js +206 -0
- package/dist/guaranteed/request.js.map +1 -0
- package/dist/guaranteed/response.d.ts +12 -0
- package/dist/guaranteed/response.js +98 -0
- package/dist/guaranteed/response.js.map +1 -0
- package/dist/guaranteed/types.d.ts +20 -0
- package/dist/guaranteed/types.js +1 -0
- package/dist/guaranteed/types.js.map +1 -0
- package/dist/index-B8NNnQ8P.d.ts +424 -0
- package/dist/index.cjs +34774 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2462 -0
- package/dist/index.d.ts +79 -0
- package/dist/index.js +168 -0
- package/dist/index.js.map +1 -0
- package/dist/legacy-sdpub/upgrade.d.ts +7 -0
- package/dist/legacy-sdpub/upgrade.js +795 -0
- package/dist/legacy-sdpub/upgrade.js.map +1 -0
- package/dist/llm/cache.d.ts +13 -0
- package/dist/llm/cache.js +35 -0
- package/dist/llm/cache.js.map +1 -0
- package/dist/llm/client.d.ts +26 -0
- package/dist/llm/client.js +572 -0
- package/dist/llm/client.js.map +1 -0
- package/dist/llm/context.d.ts +22 -0
- package/dist/llm/context.js +78 -0
- package/dist/llm/context.js.map +1 -0
- package/dist/llm/errors.d.ts +7 -0
- package/dist/llm/errors.js +12 -0
- package/dist/llm/errors.js.map +1 -0
- package/dist/llm/index.d.ts +7 -0
- package/dist/llm/index.js +17 -0
- package/dist/llm/index.js.map +1 -0
- package/dist/llm/request-log.d.ts +9 -0
- package/dist/llm/request-log.js +35 -0
- package/dist/llm/request-log.js.map +1 -0
- package/dist/llm/sampling.d.ts +11 -0
- package/dist/llm/sampling.js +67 -0
- package/dist/llm/sampling.js.map +1 -0
- package/dist/llm/types.d.ts +50 -0
- package/dist/llm/types.js +1 -0
- package/dist/llm/types.js.map +1 -0
- package/dist/output/epub/archive.d.ts +9 -0
- package/dist/output/epub/archive.js +81 -0
- package/dist/output/epub/archive.js.map +1 -0
- package/dist/output/epub/book.d.ts +15 -0
- package/dist/output/epub/book.js +76 -0
- package/dist/output/epub/book.js.map +1 -0
- package/dist/output/epub/content.d.ts +9 -0
- package/dist/output/epub/content.js +35 -0
- package/dist/output/epub/content.js.map +1 -0
- package/dist/output/epub/index.d.ts +20 -0
- package/dist/output/epub/index.js +14 -0
- package/dist/output/epub/index.js.map +1 -0
- package/dist/output/epub/model.d.ts +24 -0
- package/dist/output/epub/model.js +1 -0
- package/dist/output/epub/model.js.map +1 -0
- package/dist/output/epub/navigation.d.ts +10 -0
- package/dist/output/epub/navigation.js +39 -0
- package/dist/output/epub/navigation.js.map +1 -0
- package/dist/output/epub/package.d.ts +17 -0
- package/dist/output/epub/package.js +29 -0
- package/dist/output/epub/package.js.map +1 -0
- package/dist/output/epub/shared.d.ts +4 -0
- package/dist/output/epub/shared.js +12 -0
- package/dist/output/epub/shared.js.map +1 -0
- package/dist/output/epub/templates.d.ts +35 -0
- package/dist/output/epub/templates.js +26 -0
- package/dist/output/epub/templates.js.map +1 -0
- package/dist/output/index.d.ts +14 -0
- package/dist/output/index.js +11 -0
- package/dist/output/index.js.map +1 -0
- package/dist/output/plain-text.d.ts +18 -0
- package/dist/output/plain-text.js +56 -0
- package/dist/output/plain-text.js.map +1 -0
- package/dist/progress/index.d.ts +3 -0
- package/dist/progress/index.js +14 -0
- package/dist/progress/index.js.map +1 -0
- package/dist/progress/reporter.d.ts +10 -0
- package/dist/progress/reporter.js +79 -0
- package/dist/progress/reporter.js.map +1 -0
- package/dist/progress/tracker.d.ts +37 -0
- package/dist/progress/tracker.js +119 -0
- package/dist/progress/tracker.js.map +1 -0
- package/dist/progress/types.d.ts +28 -0
- package/dist/progress/types.js +1 -0
- package/dist/progress/types.js.map +1 -0
- package/dist/reader/attention/attention.d.ts +39 -0
- package/dist/reader/attention/attention.js +123 -0
- package/dist/reader/attention/attention.js.map +1 -0
- package/dist/reader/attention/index.d.ts +9 -0
- package/dist/reader/attention/index.js +5 -0
- package/dist/reader/attention/index.js.map +1 -0
- package/dist/reader/attention/wave-reflection.d.ts +21 -0
- package/dist/reader/attention/wave-reflection.js +153 -0
- package/dist/reader/attention/wave-reflection.js.map +1 -0
- package/dist/reader/attention/working-memory.d.ts +28 -0
- package/dist/reader/attention/working-memory.js +62 -0
- package/dist/reader/attention/working-memory.js.map +1 -0
- package/dist/reader/chunk-batch/extract.d.ts +13 -0
- package/dist/reader/chunk-batch/extract.js +14 -0
- package/dist/reader/chunk-batch/extract.js.map +1 -0
- package/dist/reader/chunk-batch/extractor.d.ts +17 -0
- package/dist/reader/chunk-batch/extractor.js +337 -0
- package/dist/reader/chunk-batch/extractor.js.map +1 -0
- package/dist/reader/chunk-batch/fragment-projection.d.ts +34 -0
- package/dist/reader/chunk-batch/fragment-projection.js +94 -0
- package/dist/reader/chunk-batch/fragment-projection.js.map +1 -0
- package/dist/reader/chunk-batch/index.d.ts +12 -0
- package/dist/reader/chunk-batch/index.js +13 -0
- package/dist/reader/chunk-batch/index.js.map +1 -0
- package/dist/reader/chunk-batch/language.d.ts +9 -0
- package/dist/reader/chunk-batch/language.js +23 -0
- package/dist/reader/chunk-batch/language.js.map +1 -0
- package/dist/reader/chunk-batch/parser.d.ts +94 -0
- package/dist/reader/chunk-batch/parser.js +640 -0
- package/dist/reader/chunk-batch/parser.js.map +1 -0
- package/dist/reader/chunk-batch/prompt-templates.d.ts +6 -0
- package/dist/reader/chunk-batch/prompt-templates.js +11 -0
- package/dist/reader/chunk-batch/prompt-templates.js.map +1 -0
- package/dist/reader/chunk-batch/types.d.ts +85 -0
- package/dist/reader/chunk-batch/types.js +1 -0
- package/dist/reader/chunk-batch/types.js.map +1 -0
- package/dist/reader/index.d.ts +13 -0
- package/dist/reader/index.js +7 -0
- package/dist/reader/index.js.map +1 -0
- package/dist/reader/reader.d.ts +37 -0
- package/dist/reader/reader.js +82 -0
- package/dist/reader/reader.js.map +1 -0
- package/dist/reader/segment/index.d.ts +2 -0
- package/dist/reader/segment/index.js +5 -0
- package/dist/reader/segment/index.js.map +1 -0
- package/dist/reader/segment/intl-segmenter.d.ts +5 -0
- package/dist/reader/segment/intl-segmenter.js +102 -0
- package/dist/reader/segment/intl-segmenter.js.map +1 -0
- package/dist/reader/segment/segment.d.ts +5 -0
- package/dist/reader/segment/segment.js +9 -0
- package/dist/reader/segment/segment.js.map +1 -0
- package/dist/reader/segment/types.d.ts +14 -0
- package/dist/reader/segment/types.js +1 -0
- package/dist/reader/segment/types.js.map +1 -0
- package/dist/reader/types.d.ts +29 -0
- package/dist/reader/types.js +1 -0
- package/dist/reader/types.js.map +1 -0
- package/dist/serial.d.ts +89 -0
- package/dist/serial.js +476 -0
- package/dist/serial.js.map +1 -0
- package/dist/source/adapter.d.ts +15 -0
- package/dist/source/adapter.js +1 -0
- package/dist/source/adapter.js.map +1 -0
- package/dist/source/epub/archive.d.ts +27 -0
- package/dist/source/epub/archive.js +189 -0
- package/dist/source/epub/archive.js.map +1 -0
- package/dist/source/epub/content.d.ts +22 -0
- package/dist/source/epub/content.js +183 -0
- package/dist/source/epub/content.js.map +1 -0
- package/dist/source/epub/document.d.ts +55 -0
- package/dist/source/epub/document.js +179 -0
- package/dist/source/epub/document.js.map +1 -0
- package/dist/source/epub/index.d.ts +10 -0
- package/dist/source/epub/index.js +6 -0
- package/dist/source/epub/index.js.map +1 -0
- package/dist/source/epub/navigation.d.ts +16 -0
- package/dist/source/epub/navigation.js +215 -0
- package/dist/source/epub/navigation.js.map +1 -0
- package/dist/source/epub/package.d.ts +33 -0
- package/dist/source/epub/package.js +237 -0
- package/dist/source/epub/package.js.map +1 -0
- package/dist/source/epub/xml.d.ts +16 -0
- package/dist/source/epub/xml.js +111 -0
- package/dist/source/epub/xml.js.map +1 -0
- package/dist/source/index.d.ts +12 -0
- package/dist/source/index.js +32 -0
- package/dist/source/index.js.map +1 -0
- package/dist/source/meta.d.ts +30 -0
- package/dist/source/meta.js +22 -0
- package/dist/source/meta.js.map +1 -0
- package/dist/source/plain-text.d.ts +16 -0
- package/dist/source/plain-text.js +86 -0
- package/dist/source/plain-text.js.map +1 -0
- package/dist/source/toc.d.ts +16 -0
- package/dist/source/toc.js +17 -0
- package/dist/source/toc.js.map +1 -0
- package/dist/source/types.d.ts +16 -0
- package/dist/source/types.js +1 -0
- package/dist/source/types.js.map +1 -0
- package/dist/topology/grouping.d.ts +15 -0
- package/dist/topology/grouping.js +18 -0
- package/dist/topology/grouping.js.map +1 -0
- package/dist/topology/index.d.ts +21 -0
- package/dist/topology/index.js +5 -0
- package/dist/topology/index.js.map +1 -0
- package/dist/topology/resource-segmentation.d.ts +13 -0
- package/dist/topology/resource-segmentation.js +111 -0
- package/dist/topology/resource-segmentation.js.map +1 -0
- package/dist/topology/segment-incision.d.ts +18 -0
- package/dist/topology/segment-incision.js +197 -0
- package/dist/topology/segment-incision.js.map +1 -0
- package/dist/topology/snake-detector.d.ts +13 -0
- package/dist/topology/snake-detector.js +520 -0
- package/dist/topology/snake-detector.js.map +1 -0
- package/dist/topology/snake-graph-builder.d.ts +14 -0
- package/dist/topology/snake-graph-builder.js +76 -0
- package/dist/topology/snake-graph-builder.js.map +1 -0
- package/dist/topology/topology.d.ts +29 -0
- package/dist/topology/topology.js +348 -0
- package/dist/topology/topology.js.map +1 -0
- package/dist/topology/weights.d.ts +10 -0
- package/dist/topology/weights.js +84 -0
- package/dist/topology/weights.js.map +1 -0
- package/dist/types-FVIqiLC9.d.cts +24 -0
- package/dist/utils/async-semaphore.d.ts +7 -0
- package/dist/utils/async-semaphore.js +40 -0
- package/dist/utils/async-semaphore.js.map +1 -0
- package/dist/utils/enum.d.ts +5 -0
- package/dist/utils/enum.js +18 -0
- package/dist/utils/enum.js.map +1 -0
- package/dist/utils/hash.d.ts +3 -0
- package/dist/utils/hash.js +132 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/node-error.d.ts +4 -0
- package/dist/utils/node-error.js +59 -0
- package/dist/utils/node-error.js.map +1 -0
- package/dist/wikg/archive-uri.d.ts +1 -0
- package/dist/wikg/archive-uri.js +37 -0
- package/dist/wikg/archive-uri.js.map +1 -0
- package/dist/wikg/archive.d.ts +28 -0
- package/dist/wikg/archive.js +496 -0
- package/dist/wikg/archive.js.map +1 -0
- package/dist/wikg/index.d.ts +31 -0
- package/dist/wikg/index.js +60 -0
- package/dist/wikg/index.js.map +1 -0
- package/dist/wikg/wikg-coordinator.d.ts +45 -0
- package/dist/wikg/wikg-coordinator.js +1687 -0
- package/dist/wikg/wikg-coordinator.js.map +1 -0
- package/dist/wikg/wiki-graph-archive-file.d.ts +42 -0
- package/dist/wikg/wiki-graph-archive-file.js +78 -0
- package/dist/wikg/wiki-graph-archive-file.js.map +1 -0
- package/dist/wikilink/index.d.ts +10 -0
- package/dist/wikilink/index.js +7 -0
- package/dist/wikilink/index.js.map +1 -0
- package/dist/wikilink/relation-discovery.d.ts +31 -0
- package/dist/wikilink/relation-discovery.js +477 -0
- package/dist/wikilink/relation-discovery.js.map +1 -0
- package/dist/wikilink/types.d.ts +23 -0
- package/dist/wikilink/types.js +1 -0
- package/dist/wikilink/types.js.map +1 -0
- package/dist/wikilink/window.d.ts +7 -0
- package/dist/wikilink/window.js +53 -0
- package/dist/wikilink/window.js.map +1 -0
- package/dist/wikimatch/enrichment.d.ts +10 -0
- package/dist/wikimatch/enrichment.js +55 -0
- package/dist/wikimatch/enrichment.js.map +1 -0
- package/dist/wikimatch/index.d.ts +16 -0
- package/dist/wikimatch/index.js +66 -0
- package/dist/wikimatch/index.js.map +1 -0
- package/dist/wikimatch/option-narrowing.d.ts +17 -0
- package/dist/wikimatch/option-narrowing.js +208 -0
- package/dist/wikimatch/option-narrowing.js.map +1 -0
- package/dist/wikimatch/options.d.ts +14 -0
- package/dist/wikimatch/options.js +155 -0
- package/dist/wikimatch/options.js.map +1 -0
- package/dist/wikimatch/policy-judge.d.ts +17 -0
- package/dist/wikimatch/policy-judge.js +430 -0
- package/dist/wikimatch/policy-judge.js.map +1 -0
- package/dist/wikimatch/range-suppression.d.ts +9 -0
- package/dist/wikimatch/range-suppression.js +27 -0
- package/dist/wikimatch/range-suppression.js.map +1 -0
- package/dist/wikimatch/surface-screening.d.ts +16 -0
- package/dist/wikimatch/surface-screening.js +168 -0
- package/dist/wikimatch/surface-screening.js.map +1 -0
- package/dist/wikimatch/surface-window.d.ts +6 -0
- package/dist/wikimatch/surface-window.js +44 -0
- package/dist/wikimatch/surface-window.js.map +1 -0
- package/dist/wikimatch/types.d.ts +151 -0
- package/dist/wikimatch/types.js +1 -0
- package/dist/wikimatch/types.js.map +1 -0
- package/dist/wikimatch/wikispine.d.ts +43 -0
- package/dist/wikimatch/wikispine.js +399 -0
- package/dist/wikimatch/wikispine.js.map +1 -0
- package/dist/wikimatch/window.d.ts +6 -0
- package/dist/wikimatch/window.js +83 -0
- package/dist/wikimatch/window.js.map +1 -0
- package/dist/wikimatch/words.d.ts +16 -0
- package/dist/wikimatch/words.js +66 -0
- package/dist/wikimatch/words.js.map +1 -0
- package/dist/wikipage/cache.d.ts +18 -0
- package/dist/wikipage/cache.js +404 -0
- package/dist/wikipage/cache.js.map +1 -0
- package/dist/wikipage/fetch-log.d.ts +17 -0
- package/dist/wikipage/fetch-log.js +143 -0
- package/dist/wikipage/fetch-log.js.map +1 -0
- package/dist/wikipage/index.d.ts +14 -0
- package/dist/wikipage/index.js +17 -0
- package/dist/wikipage/index.js.map +1 -0
- package/dist/wikipage/normalizer.d.ts +14 -0
- package/dist/wikipage/normalizer.js +147 -0
- package/dist/wikipage/normalizer.js.map +1 -0
- package/dist/wikipage/rate-limiter.d.ts +13 -0
- package/dist/wikipage/rate-limiter.js +84 -0
- package/dist/wikipage/rate-limiter.js.map +1 -0
- package/dist/wikipage/resolver.d.ts +26 -0
- package/dist/wikipage/resolver.js +325 -0
- package/dist/wikipage/resolver.js.map +1 -0
- package/dist/wikipage/types.d.ts +96 -0
- package/dist/wikipage/types.js +1 -0
- package/dist/wikipage/types.js.map +1 -0
- package/dist/wikipage/wikimedia-client.d.ts +48 -0
- package/dist/wikipage/wikimedia-client.js +397 -0
- package/dist/wikipage/wikimedia-client.js.map +1 -0
- package/dist/worker-BgQ0Xl8T.d.cts +181 -0
- package/dist/worker.cjs +5259 -0
- package/dist/worker.cjs.map +1 -0
- package/dist/worker.d.cts +2 -0
- package/dist/worker.d.ts +2 -0
- package/dist/worker.js +5 -0
- package/dist/worker.js.map +1 -0
- package/package.json +78 -0
|
@@ -0,0 +1,897 @@
|
|
|
1
|
+
import { createReadStream, createWriteStream } from "fs";
|
|
2
|
+
import { mkdir, rm } from "fs/promises";
|
|
3
|
+
import { createInterface } from "readline";
|
|
4
|
+
import { join } from "path";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { LanguageCode, normalizeLanguageCode } from "../common/language.js";
|
|
7
|
+
import {
|
|
8
|
+
buildWikimatchSurfaceProtectionInput,
|
|
9
|
+
buildWikimatchWindows,
|
|
10
|
+
enrichWikimatchCandidates,
|
|
11
|
+
filterCandidateQidOptions,
|
|
12
|
+
judgeWikimatchPolicy,
|
|
13
|
+
judgeWikimatchSurfaceProtection,
|
|
14
|
+
listCandidateSelectableQids,
|
|
15
|
+
matchWikispineSentenceCandidates
|
|
16
|
+
} from "../wikimatch/index.js";
|
|
17
|
+
import {
|
|
18
|
+
buildWikilinkEvidenceWindows,
|
|
19
|
+
discoverWikilinkRelations
|
|
20
|
+
} from "../wikilink/index.js";
|
|
21
|
+
import { getChapterDetails } from "./chapter.js";
|
|
22
|
+
const mentionRecordSchema = z.object({
|
|
23
|
+
id: z.string().min(1),
|
|
24
|
+
chapterId: z.number().int(),
|
|
25
|
+
sentenceIndex: z.number().int().nonnegative().optional(),
|
|
26
|
+
rangeStart: z.number().int().nonnegative(),
|
|
27
|
+
rangeEnd: z.number().int().nonnegative(),
|
|
28
|
+
surface: z.string().min(1),
|
|
29
|
+
qid: z.string().regex(/^Q[1-9][0-9]*$/),
|
|
30
|
+
confidence: z.number().min(0).max(1).optional(),
|
|
31
|
+
note: z.string().optional()
|
|
32
|
+
});
|
|
33
|
+
const sentenceIdSchema = z.tuple([z.number().int().nonnegative(), z.number().int().nonnegative()]).readonly();
|
|
34
|
+
const mentionLinkRecordSchema = z.object({
|
|
35
|
+
id: z.string().min(1),
|
|
36
|
+
sourceMentionId: z.string().min(1),
|
|
37
|
+
targetMentionId: z.string().min(1),
|
|
38
|
+
predicate: z.string().min(1),
|
|
39
|
+
evidenceSentenceIds: z.array(sentenceIdSchema).min(1),
|
|
40
|
+
confidence: z.number().min(0).max(1).optional(),
|
|
41
|
+
note: z.string().optional()
|
|
42
|
+
});
|
|
43
|
+
const WIKIMATCH_GROUNDING_DEFAULT_OPTION_BUDGETS = [5, 10, 20, 35];
|
|
44
|
+
const WIKIMATCH_GROUNDING_PRIOR_OPTION_BUDGETS = [3, 5, 10, 20, 35];
|
|
45
|
+
const WIKIMATCH_GROUNDING_MAX_OPTION_BUDGET = 50;
|
|
46
|
+
const WIKIMATCH_GROUNDING_SURFACE_PRIOR_THRESHOLD = 3;
|
|
47
|
+
const WIKIMATCH_SURFACE_PROTECTION_PERCENTILE = 0.1;
|
|
48
|
+
const WIKILINK_EVIDENCE_DISTANCE = 700;
|
|
49
|
+
const WIKILINK_WINDOW_LENGTH = 1800;
|
|
50
|
+
async function generateChapterKnowledgeGraphArtifact(document, chapterId, options) {
|
|
51
|
+
return await generateChapterKnowledgeGraphArtifactFromSnapshot(
|
|
52
|
+
chapterId,
|
|
53
|
+
await snapshotChapterKnowledgeGraphInput(document, chapterId),
|
|
54
|
+
options
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
async function snapshotChapterKnowledgeGraphInput(document, chapterId) {
|
|
58
|
+
const details = await getChapterDetails(document, chapterId);
|
|
59
|
+
if (details.stage === "planned") {
|
|
60
|
+
throw new Error(
|
|
61
|
+
`Chapter ${chapterId} is planned. Set source before generating Knowledge Graph.`
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
details,
|
|
66
|
+
fragments: await readChapterFragments(document, chapterId)
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
async function generateChapterKnowledgeGraphArtifactFromSnapshot(chapterId, snapshot, options) {
|
|
70
|
+
if (snapshot.details.chapterId !== chapterId) {
|
|
71
|
+
throw new Error(
|
|
72
|
+
`Knowledge Graph snapshot belongs to chapter ${snapshot.details.chapterId}, not chapter ${chapterId}.`
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
if (snapshot.details.stage === "planned") {
|
|
76
|
+
throw new Error(
|
|
77
|
+
`Chapter ${chapterId} is planned. Set source before generating Knowledge Graph.`
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
const fragments = snapshot.fragments;
|
|
81
|
+
const text = joinFragmentText(fragments);
|
|
82
|
+
const sentences = createWikimatchSentences(fragments);
|
|
83
|
+
await options.progressTracker?.updatePhase({
|
|
84
|
+
done: 0,
|
|
85
|
+
phase: "matching",
|
|
86
|
+
phaseDetail: "text",
|
|
87
|
+
total: text.length,
|
|
88
|
+
unit: "char"
|
|
89
|
+
});
|
|
90
|
+
const rawCandidates = await matchWikispineSentenceCandidates({
|
|
91
|
+
includeDisambiguation: true,
|
|
92
|
+
onProgress: async (progress) => {
|
|
93
|
+
await options.progressTracker?.updatePhase({
|
|
94
|
+
done: progress.coveredRangeEnd,
|
|
95
|
+
force: false,
|
|
96
|
+
phase: "matching",
|
|
97
|
+
phaseDetail: "text",
|
|
98
|
+
total: text.length,
|
|
99
|
+
unit: "char"
|
|
100
|
+
});
|
|
101
|
+
},
|
|
102
|
+
...options.wikispine ?? {},
|
|
103
|
+
sentences
|
|
104
|
+
});
|
|
105
|
+
await options.progressTracker?.throwIfStopped();
|
|
106
|
+
await options.progressTracker?.updatePhase({
|
|
107
|
+
done: text.length,
|
|
108
|
+
phase: "matching",
|
|
109
|
+
phaseDetail: "text",
|
|
110
|
+
total: text.length,
|
|
111
|
+
unit: "char"
|
|
112
|
+
});
|
|
113
|
+
const screenedCandidates = await screenCandidates({
|
|
114
|
+
candidates: rawCandidates,
|
|
115
|
+
policyPrompt: options.policyPrompt,
|
|
116
|
+
...options.progressTracker === void 0 ? {} : { progressTracker: options.progressTracker },
|
|
117
|
+
request: options.request,
|
|
118
|
+
text
|
|
119
|
+
});
|
|
120
|
+
await options.progressTracker?.throwIfStopped();
|
|
121
|
+
const qidCount = countUniqueQids(screenedCandidates);
|
|
122
|
+
await options.progressTracker?.updatePhase({
|
|
123
|
+
done: 0,
|
|
124
|
+
phase: "enrichment",
|
|
125
|
+
total: qidCount,
|
|
126
|
+
unit: "qid"
|
|
127
|
+
});
|
|
128
|
+
const enrichedCandidates = await enrichWikimatchCandidates(
|
|
129
|
+
screenedCandidates,
|
|
130
|
+
{
|
|
131
|
+
...options.progressTracker === void 0 ? {} : {
|
|
132
|
+
progress: createEnrichmentProgressReporter(options.progressTracker)
|
|
133
|
+
},
|
|
134
|
+
...options.resolverOptions === void 0 ? {} : { resolverOptions: options.resolverOptions }
|
|
135
|
+
}
|
|
136
|
+
);
|
|
137
|
+
await options.progressTracker?.throwIfStopped();
|
|
138
|
+
await options.progressTracker?.updatePhase({
|
|
139
|
+
done: qidCount,
|
|
140
|
+
phase: "enrichment",
|
|
141
|
+
total: qidCount,
|
|
142
|
+
unit: "qid"
|
|
143
|
+
});
|
|
144
|
+
const mentions = await judgeCandidates({
|
|
145
|
+
candidates: enrichedCandidates,
|
|
146
|
+
chapterId,
|
|
147
|
+
fragments,
|
|
148
|
+
policyPrompt: options.policyPrompt,
|
|
149
|
+
...options.progressTracker === void 0 ? {} : { progressTracker: options.progressTracker },
|
|
150
|
+
request: options.request,
|
|
151
|
+
text
|
|
152
|
+
});
|
|
153
|
+
await options.progressTracker?.throwIfStopped();
|
|
154
|
+
const mentionLinks = await discoverMentionLinks({
|
|
155
|
+
fragments,
|
|
156
|
+
mentions,
|
|
157
|
+
...options.progressTracker === void 0 ? {} : { progressTracker: options.progressTracker },
|
|
158
|
+
request: options.request
|
|
159
|
+
});
|
|
160
|
+
await options.progressTracker?.throwIfStopped();
|
|
161
|
+
const artifact = await buildChapterKnowledgeGraphArtifact(chapterId, {
|
|
162
|
+
mentionLinks,
|
|
163
|
+
mentions,
|
|
164
|
+
parameter: createKnowledgeGraphParameterInput(options),
|
|
165
|
+
workspacePath: options.workspacePath
|
|
166
|
+
});
|
|
167
|
+
return artifact;
|
|
168
|
+
}
|
|
169
|
+
async function buildChapterKnowledgeGraphArtifact(chapterId, options) {
|
|
170
|
+
const workspacePath = join(
|
|
171
|
+
options.workspacePath,
|
|
172
|
+
"knowledge-graph",
|
|
173
|
+
`chapter-${chapterId}`
|
|
174
|
+
);
|
|
175
|
+
const mentionsPath = join(workspacePath, "mentions.jsonl");
|
|
176
|
+
const mentionLinksPath = join(workspacePath, "mention-links.jsonl");
|
|
177
|
+
await rm(workspacePath, { force: true, recursive: true });
|
|
178
|
+
await mkdir(workspacePath, { recursive: true });
|
|
179
|
+
await writeJsonl(mentionsPath, options.mentions, parseMentionRecord);
|
|
180
|
+
await writeJsonl(
|
|
181
|
+
mentionLinksPath,
|
|
182
|
+
options.mentionLinks,
|
|
183
|
+
parseMentionLinkRecord
|
|
184
|
+
);
|
|
185
|
+
return {
|
|
186
|
+
chapterId,
|
|
187
|
+
mentionLinksPath,
|
|
188
|
+
mentionsPath,
|
|
189
|
+
parameter: options.parameter ?? {
|
|
190
|
+
language: LanguageCode.Chinese,
|
|
191
|
+
prompt: ""
|
|
192
|
+
},
|
|
193
|
+
workspacePath
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
function createKnowledgeGraphParameterInput(options) {
|
|
197
|
+
return {
|
|
198
|
+
language: normalizeLanguageCode(options.resolverOptions?.language) ?? LanguageCode.Chinese,
|
|
199
|
+
prompt: options.policyPrompt
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
async function screenCandidates(input) {
|
|
203
|
+
if (input.candidates.length === 0) {
|
|
204
|
+
return [];
|
|
205
|
+
}
|
|
206
|
+
const protectionInput = buildWikimatchSurfaceProtectionInput({
|
|
207
|
+
candidates: input.candidates,
|
|
208
|
+
percentile: WIKIMATCH_SURFACE_PROTECTION_PERCENTILE,
|
|
209
|
+
text: input.text
|
|
210
|
+
});
|
|
211
|
+
await input.progressTracker?.updatePhase({
|
|
212
|
+
done: 0,
|
|
213
|
+
phase: "screening",
|
|
214
|
+
phaseDetail: `${protectionInput.suspiciousSurfaces.length} high-frequency surfaces`,
|
|
215
|
+
total: 1,
|
|
216
|
+
unit: "window"
|
|
217
|
+
});
|
|
218
|
+
await input.progressTracker?.throwIfStopped();
|
|
219
|
+
const protection = await judgeWikimatchSurfaceProtection({
|
|
220
|
+
policyPrompt: input.policyPrompt,
|
|
221
|
+
request: input.request,
|
|
222
|
+
suspiciousSurfaces: protectionInput.suspiciousSurfaces
|
|
223
|
+
});
|
|
224
|
+
await input.progressTracker?.throwIfStopped();
|
|
225
|
+
const protectedSurfaces = new Set(
|
|
226
|
+
protection.protectedSurfaces.map((surface) => surface.text)
|
|
227
|
+
);
|
|
228
|
+
const allowedCandidateKeys = new Set(
|
|
229
|
+
protectionInput.candidates.map(createCandidateRangeKey)
|
|
230
|
+
);
|
|
231
|
+
await input.progressTracker?.updatePhase({
|
|
232
|
+
done: 1,
|
|
233
|
+
phase: "screening",
|
|
234
|
+
phaseDetail: `${protectedSurfaces.size} protected surfaces`,
|
|
235
|
+
total: 1,
|
|
236
|
+
unit: "window"
|
|
237
|
+
});
|
|
238
|
+
return protectionInput.suppressedCandidates.filter(
|
|
239
|
+
(candidate) => allowedCandidateKeys.has(createCandidateRangeKey(candidate)) || protectedSurfaces.has(candidate.surface)
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
function createCandidateRangeKey(candidate) {
|
|
243
|
+
return `${candidate.range.start}\0${candidate.range.end}\0${candidate.surface}`;
|
|
244
|
+
}
|
|
245
|
+
async function judgeCandidates(input) {
|
|
246
|
+
const mentions = [];
|
|
247
|
+
const sentenceLocations = buildSentenceLocations(input.fragments);
|
|
248
|
+
const acceptedMentions = await groundWikimatchCandidates({
|
|
249
|
+
candidates: input.candidates,
|
|
250
|
+
policyPrompt: input.policyPrompt,
|
|
251
|
+
...input.progressTracker === void 0 ? {} : { progressTracker: input.progressTracker },
|
|
252
|
+
request: input.request,
|
|
253
|
+
text: input.text
|
|
254
|
+
});
|
|
255
|
+
let mentionIndex = 1;
|
|
256
|
+
for (const mention of acceptedMentions) {
|
|
257
|
+
const location = locateMention(sentenceLocations, mention.range.start);
|
|
258
|
+
mentions.push(
|
|
259
|
+
toMentionRecord(input.chapterId, mention, location, mentionIndex)
|
|
260
|
+
);
|
|
261
|
+
mentionIndex += 1;
|
|
262
|
+
}
|
|
263
|
+
return mentions;
|
|
264
|
+
}
|
|
265
|
+
async function groundWikimatchCandidates(input) {
|
|
266
|
+
const mentions = [];
|
|
267
|
+
const candidatePages = createGroundingCandidatePages(input.candidates);
|
|
268
|
+
let completedWindows = 0;
|
|
269
|
+
let totalWindows = 0;
|
|
270
|
+
let activeCandidates = candidatePages.nextPage();
|
|
271
|
+
while (activeCandidates.length > 0) {
|
|
272
|
+
const windows = buildWikimatchWindows({
|
|
273
|
+
candidates: activeCandidates,
|
|
274
|
+
contextWords: 220,
|
|
275
|
+
optionBudget: WIKIMATCH_GROUNDING_MAX_OPTION_BUDGET,
|
|
276
|
+
text: input.text
|
|
277
|
+
});
|
|
278
|
+
totalWindows += windows.length;
|
|
279
|
+
if (windows.length === 0) {
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
await input.progressTracker?.updatePhase({
|
|
283
|
+
done: completedWindows,
|
|
284
|
+
phase: "grounding",
|
|
285
|
+
total: totalWindows,
|
|
286
|
+
unit: "window"
|
|
287
|
+
});
|
|
288
|
+
const results = await mapLazyGuaranteedRequests(
|
|
289
|
+
input.request,
|
|
290
|
+
windows,
|
|
291
|
+
async (window, request) => {
|
|
292
|
+
try {
|
|
293
|
+
await input.progressTracker?.throwIfStopped();
|
|
294
|
+
return await judgeWikimatchPolicy({
|
|
295
|
+
candidates: window.candidates,
|
|
296
|
+
policyPrompt: input.policyPrompt,
|
|
297
|
+
request,
|
|
298
|
+
window
|
|
299
|
+
});
|
|
300
|
+
} finally {
|
|
301
|
+
completedWindows += 1;
|
|
302
|
+
await input.progressTracker?.updatePhase({
|
|
303
|
+
done: completedWindows,
|
|
304
|
+
phase: "grounding",
|
|
305
|
+
total: totalWindows,
|
|
306
|
+
unit: "window"
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
);
|
|
311
|
+
const continuedCandidateIds = /* @__PURE__ */ new Set();
|
|
312
|
+
for (const result of results) {
|
|
313
|
+
for (const mention of result.mentions) {
|
|
314
|
+
mentions.push(mention);
|
|
315
|
+
candidatePages.accept(mention);
|
|
316
|
+
}
|
|
317
|
+
for (const update of result.policyUpdates) {
|
|
318
|
+
candidatePages.close(update.candidateId, update.decision);
|
|
319
|
+
}
|
|
320
|
+
for (const continuation of result.continuations) {
|
|
321
|
+
for (const candidateId of continuation.candidateIds) {
|
|
322
|
+
continuedCandidateIds.add(candidateId);
|
|
323
|
+
candidatePages.continue(candidateId);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
activeCandidates = candidatePages.nextPage(continuedCandidateIds);
|
|
328
|
+
}
|
|
329
|
+
await input.progressTracker?.updatePhase({
|
|
330
|
+
done: completedWindows,
|
|
331
|
+
phase: "grounding",
|
|
332
|
+
phaseDetail: formatGroundingEfficiency(
|
|
333
|
+
candidatePages.getStats(),
|
|
334
|
+
mentions.length
|
|
335
|
+
),
|
|
336
|
+
total: totalWindows,
|
|
337
|
+
unit: "window"
|
|
338
|
+
});
|
|
339
|
+
return mentions;
|
|
340
|
+
}
|
|
341
|
+
function createGroundingCandidatePages(candidates) {
|
|
342
|
+
const candidatesById = new Map(
|
|
343
|
+
candidates.map((candidate) => [candidate.id, candidate])
|
|
344
|
+
);
|
|
345
|
+
const shownQidsByCandidateId = new Map(
|
|
346
|
+
candidates.map((candidate) => [candidate.id, /* @__PURE__ */ new Set()])
|
|
347
|
+
);
|
|
348
|
+
const pageIndexesByCandidateId = new Map(
|
|
349
|
+
candidates.map((candidate) => [candidate.id, 0])
|
|
350
|
+
);
|
|
351
|
+
const closedCandidateIds = /* @__PURE__ */ new Set();
|
|
352
|
+
const recallCounts = /* @__PURE__ */ new Map();
|
|
353
|
+
const surfaceStats = /* @__PURE__ */ new Map();
|
|
354
|
+
const stats = {
|
|
355
|
+
candidatePageCount: 0,
|
|
356
|
+
qidAppearanceCount: 0
|
|
357
|
+
};
|
|
358
|
+
return {
|
|
359
|
+
accept(mention) {
|
|
360
|
+
closedCandidateIds.add(mention.candidateId);
|
|
361
|
+
getSurfaceStats(surfaceStats, mention.surface).recallCount += 1;
|
|
362
|
+
recallCounts.set(
|
|
363
|
+
createSurfaceQidKey(mention.surface, mention.qid),
|
|
364
|
+
(recallCounts.get(createSurfaceQidKey(mention.surface, mention.qid)) ?? 0) + 1
|
|
365
|
+
);
|
|
366
|
+
},
|
|
367
|
+
close(candidateId, decision) {
|
|
368
|
+
closedCandidateIds.add(candidateId);
|
|
369
|
+
const candidate = candidatesById.get(candidateId);
|
|
370
|
+
if (candidate !== void 0 && (decision === "skip_this_time" || decision === "never_recall")) {
|
|
371
|
+
getSurfaceStats(surfaceStats, candidate.surface).rejectCount += 1;
|
|
372
|
+
}
|
|
373
|
+
},
|
|
374
|
+
continue(candidateId) {
|
|
375
|
+
const candidate = candidatesById.get(candidateId);
|
|
376
|
+
if (candidate !== void 0) {
|
|
377
|
+
getSurfaceStats(surfaceStats, candidate.surface).continueCount += 1;
|
|
378
|
+
}
|
|
379
|
+
},
|
|
380
|
+
getStats() {
|
|
381
|
+
return { ...stats };
|
|
382
|
+
},
|
|
383
|
+
nextPage(continuedCandidateIds) {
|
|
384
|
+
const pageCandidates = [];
|
|
385
|
+
const candidateIds = continuedCandidateIds === void 0 ? candidates.map((candidate) => candidate.id) : [...continuedCandidateIds];
|
|
386
|
+
for (const candidateId of candidateIds) {
|
|
387
|
+
if (closedCandidateIds.has(candidateId)) {
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
const candidate = candidatesById.get(candidateId);
|
|
391
|
+
const shownQids = shownQidsByCandidateId.get(candidateId);
|
|
392
|
+
if (candidate === void 0 || shownQids === void 0) {
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
const sortedCandidate = sortCandidateOptionsByRecall(
|
|
396
|
+
candidate,
|
|
397
|
+
recallCounts
|
|
398
|
+
);
|
|
399
|
+
const selectableQids = listCandidateSelectableQids(sortedCandidate);
|
|
400
|
+
const pageIndex = pageIndexesByCandidateId.get(candidateId) ?? 0;
|
|
401
|
+
const optionBudget = getGroundingCandidateOptionBudget(
|
|
402
|
+
getSurfaceStats(surfaceStats, candidate.surface),
|
|
403
|
+
pageIndex
|
|
404
|
+
);
|
|
405
|
+
const selectedQids = selectableQids.filter((qid) => !shownQids.has(qid)).slice(0, optionBudget);
|
|
406
|
+
if (selectedQids.length === 0) {
|
|
407
|
+
closedCandidateIds.add(candidateId);
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
for (const qid of selectedQids) {
|
|
411
|
+
shownQids.add(qid);
|
|
412
|
+
}
|
|
413
|
+
const hasMoreOptions = selectableQids.some(
|
|
414
|
+
(qid) => !shownQids.has(qid)
|
|
415
|
+
);
|
|
416
|
+
const pageCandidate = filterCandidateQidOptions(
|
|
417
|
+
sortedCandidate,
|
|
418
|
+
new Set(selectedQids)
|
|
419
|
+
);
|
|
420
|
+
if (!hasMoreOptions) {
|
|
421
|
+
closedCandidateIds.add(candidateId);
|
|
422
|
+
}
|
|
423
|
+
getSurfaceStats(surfaceStats, candidate.surface).seenCount += 1;
|
|
424
|
+
pageIndexesByCandidateId.set(candidateId, pageIndex + 1);
|
|
425
|
+
stats.candidatePageCount += 1;
|
|
426
|
+
stats.qidAppearanceCount += selectedQids.length;
|
|
427
|
+
pageCandidates.push({
|
|
428
|
+
...pageCandidate,
|
|
429
|
+
...hasMoreOptions ? { hasMoreOptions: true } : {}
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
return pageCandidates;
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
function getSurfaceStats(surfaceStats, surface) {
|
|
437
|
+
const existing = surfaceStats.get(surface);
|
|
438
|
+
if (existing !== void 0) {
|
|
439
|
+
return existing;
|
|
440
|
+
}
|
|
441
|
+
const created = {
|
|
442
|
+
continueCount: 0,
|
|
443
|
+
recallCount: 0,
|
|
444
|
+
rejectCount: 0,
|
|
445
|
+
seenCount: 0
|
|
446
|
+
};
|
|
447
|
+
surfaceStats.set(surface, created);
|
|
448
|
+
return created;
|
|
449
|
+
}
|
|
450
|
+
function getGroundingCandidateOptionBudget(stats, pageIndex) {
|
|
451
|
+
const budgets = hasStrongGroundingSurfacePrior(stats) ? WIKIMATCH_GROUNDING_PRIOR_OPTION_BUDGETS : WIKIMATCH_GROUNDING_DEFAULT_OPTION_BUDGETS;
|
|
452
|
+
return budgets[Math.min(pageIndex, budgets.length - 1)];
|
|
453
|
+
}
|
|
454
|
+
function hasStrongGroundingSurfacePrior(stats) {
|
|
455
|
+
if (stats.seenCount < WIKIMATCH_GROUNDING_SURFACE_PRIOR_THRESHOLD) {
|
|
456
|
+
return false;
|
|
457
|
+
}
|
|
458
|
+
return stats.recallCount >= 2 && stats.continueCount === 0 || stats.rejectCount >= 2 && stats.recallCount === 0 || stats.continueCount >= WIKIMATCH_GROUNDING_SURFACE_PRIOR_THRESHOLD;
|
|
459
|
+
}
|
|
460
|
+
function formatGroundingEfficiency(stats, mentionCount) {
|
|
461
|
+
const qidPerMention = mentionCount === 0 ? "n/a" : (stats.qidAppearanceCount / mentionCount).toFixed(1);
|
|
462
|
+
return `efficiency qid/mention=${qidPerMention} qids=${stats.qidAppearanceCount} mentions=${mentionCount} pages=${stats.candidatePageCount}`;
|
|
463
|
+
}
|
|
464
|
+
function sortCandidateOptionsByRecall(candidate, recallCounts) {
|
|
465
|
+
return {
|
|
466
|
+
...candidate,
|
|
467
|
+
qidOptions: [...candidate.qidOptions].sort((left, right) => {
|
|
468
|
+
return getOptionRecallScore(candidate.surface, right, recallCounts) - getOptionRecallScore(candidate.surface, left, recallCounts);
|
|
469
|
+
})
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
function getOptionRecallScore(surface, option, recallCounts) {
|
|
473
|
+
const directScore = recallCounts.get(createSurfaceQidKey(surface, option.qid)) ?? 0;
|
|
474
|
+
const disambiguationScore = option.disambiguation?.linkedQids.reduce(
|
|
475
|
+
(total, item) => total + (recallCounts.get(createSurfaceQidKey(surface, item.qid)) ?? 0),
|
|
476
|
+
0
|
|
477
|
+
) ?? 0;
|
|
478
|
+
const profileScore = option.disambiguation?.profile?.meanings.reduce(
|
|
479
|
+
(total, item) => total + (recallCounts.get(createSurfaceQidKey(surface, item.qid)) ?? 0),
|
|
480
|
+
0
|
|
481
|
+
) ?? 0;
|
|
482
|
+
return directScore + disambiguationScore + profileScore;
|
|
483
|
+
}
|
|
484
|
+
function createSurfaceQidKey(surface, qid) {
|
|
485
|
+
return `${surface}\0${qid}`;
|
|
486
|
+
}
|
|
487
|
+
function createEnrichmentProgressReporter(progressTracker) {
|
|
488
|
+
return async (event) => {
|
|
489
|
+
await progressTracker.throwIfStopped();
|
|
490
|
+
const phase = formatEnrichmentProgressPhase(event);
|
|
491
|
+
await progressTracker.updatePhase({
|
|
492
|
+
done: event.done,
|
|
493
|
+
...phase.detail === void 0 ? {} : { phaseDetail: phase.detail },
|
|
494
|
+
phase: "enrichment",
|
|
495
|
+
total: event.total,
|
|
496
|
+
unit: phase.unit
|
|
497
|
+
});
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
function formatEnrichmentProgressPhase(event) {
|
|
501
|
+
switch (event.detail) {
|
|
502
|
+
case "disambiguation-page":
|
|
503
|
+
return {
|
|
504
|
+
detail: "disambiguation",
|
|
505
|
+
unit: "page"
|
|
506
|
+
};
|
|
507
|
+
case "entity":
|
|
508
|
+
return {
|
|
509
|
+
detail: "entity",
|
|
510
|
+
unit: "record"
|
|
511
|
+
};
|
|
512
|
+
case "linked-page":
|
|
513
|
+
return {
|
|
514
|
+
detail: "linked",
|
|
515
|
+
unit: "page"
|
|
516
|
+
};
|
|
517
|
+
case "page":
|
|
518
|
+
return {
|
|
519
|
+
detail: "page",
|
|
520
|
+
unit: "page"
|
|
521
|
+
};
|
|
522
|
+
case "qid":
|
|
523
|
+
return {
|
|
524
|
+
unit: "qid"
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
async function mapLazyGuaranteedRequests(request, items, operation) {
|
|
529
|
+
const lazy = request.lazy;
|
|
530
|
+
if (lazy !== void 0) {
|
|
531
|
+
return await Promise.all(
|
|
532
|
+
items.map(
|
|
533
|
+
async (item) => await lazy(async (request2) => await operation(item, request2))
|
|
534
|
+
)
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
const results = [];
|
|
538
|
+
for (const item of items) {
|
|
539
|
+
results.push(await operation(item, request));
|
|
540
|
+
}
|
|
541
|
+
return results;
|
|
542
|
+
}
|
|
543
|
+
async function discoverMentionLinks(input) {
|
|
544
|
+
const fragmentWindows = buildMentionLinkWindows(
|
|
545
|
+
input.fragments,
|
|
546
|
+
input.mentions
|
|
547
|
+
);
|
|
548
|
+
let completedWindows = 0;
|
|
549
|
+
await input.progressTracker?.updatePhase({
|
|
550
|
+
done: 0,
|
|
551
|
+
phase: "relation-discovery",
|
|
552
|
+
total: fragmentWindows.length,
|
|
553
|
+
unit: "window"
|
|
554
|
+
});
|
|
555
|
+
const discoveredLinks = (await mapLazyGuaranteedRequests(
|
|
556
|
+
input.request,
|
|
557
|
+
fragmentWindows,
|
|
558
|
+
async (item, request) => {
|
|
559
|
+
try {
|
|
560
|
+
await input.progressTracker?.throwIfStopped();
|
|
561
|
+
return await discoverWikilinkRelations({
|
|
562
|
+
chapterId: item.fragment.serialId,
|
|
563
|
+
fragmentId: item.fragment.fragmentId,
|
|
564
|
+
request,
|
|
565
|
+
sentences: item.fragment.sentences,
|
|
566
|
+
window: item.window
|
|
567
|
+
});
|
|
568
|
+
} finally {
|
|
569
|
+
completedWindows += 1;
|
|
570
|
+
await input.progressTracker?.updatePhase({
|
|
571
|
+
done: completedWindows,
|
|
572
|
+
phase: "relation-discovery",
|
|
573
|
+
total: fragmentWindows.length,
|
|
574
|
+
unit: "window"
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
)).flat();
|
|
579
|
+
return discoveredLinks.map((link, index) => ({
|
|
580
|
+
...link.confidence === void 0 ? {} : { confidence: link.confidence },
|
|
581
|
+
evidenceSentenceIds: link.evidenceSentenceIds,
|
|
582
|
+
id: `l${input.fragments[0]?.serialId ?? 0}-${index + 1}`,
|
|
583
|
+
predicate: link.predicate,
|
|
584
|
+
sourceMentionId: link.sourceMentionId,
|
|
585
|
+
targetMentionId: link.targetMentionId
|
|
586
|
+
}));
|
|
587
|
+
}
|
|
588
|
+
function buildMentionLinkWindows(fragments, mentions) {
|
|
589
|
+
return fragments.flatMap((fragment) => {
|
|
590
|
+
const startSentenceIndex = fragment.fragmentId;
|
|
591
|
+
const endSentenceIndex = startSentenceIndex + fragment.sentences.length - 1;
|
|
592
|
+
const fragmentMentions = toWikilinkMentions(
|
|
593
|
+
fragment.sentences,
|
|
594
|
+
mentions.filter(
|
|
595
|
+
(mention) => mention.sentenceIndex !== void 0 && mention.sentenceIndex >= startSentenceIndex && mention.sentenceIndex <= endSentenceIndex
|
|
596
|
+
),
|
|
597
|
+
startSentenceIndex
|
|
598
|
+
);
|
|
599
|
+
const windows = buildWikilinkEvidenceWindows({
|
|
600
|
+
maxEvidenceDistance: WIKILINK_EVIDENCE_DISTANCE,
|
|
601
|
+
mentions: fragmentMentions,
|
|
602
|
+
text: joinSentences(fragment.sentences),
|
|
603
|
+
windowLength: WIKILINK_WINDOW_LENGTH
|
|
604
|
+
});
|
|
605
|
+
return windows.map((window) => ({
|
|
606
|
+
fragment,
|
|
607
|
+
window
|
|
608
|
+
}));
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
function toWikilinkMentions(sentences, mentions, startSentenceIndex) {
|
|
612
|
+
const sentenceOffsets = buildFragmentSentenceOffsets(sentences);
|
|
613
|
+
return mentions.flatMap((mention) => {
|
|
614
|
+
if (mention.sentenceIndex === void 0) {
|
|
615
|
+
return [];
|
|
616
|
+
}
|
|
617
|
+
const sentenceOffset = sentenceOffsets[mention.sentenceIndex - startSentenceIndex];
|
|
618
|
+
if (sentenceOffset === void 0) {
|
|
619
|
+
return [];
|
|
620
|
+
}
|
|
621
|
+
return [
|
|
622
|
+
{
|
|
623
|
+
id: mention.id,
|
|
624
|
+
qid: mention.qid,
|
|
625
|
+
range: {
|
|
626
|
+
end: sentenceOffset.start + mention.rangeEnd,
|
|
627
|
+
start: sentenceOffset.start + mention.rangeStart
|
|
628
|
+
},
|
|
629
|
+
surface: mention.surface
|
|
630
|
+
}
|
|
631
|
+
];
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
function buildFragmentSentenceOffsets(sentences) {
|
|
635
|
+
const offsets = [];
|
|
636
|
+
let cursor = 0;
|
|
637
|
+
for (const sentence of sentences) {
|
|
638
|
+
const start = cursor;
|
|
639
|
+
const end = start + sentence.text.length;
|
|
640
|
+
offsets.push({ end, start });
|
|
641
|
+
cursor = end + 1;
|
|
642
|
+
}
|
|
643
|
+
return offsets;
|
|
644
|
+
}
|
|
645
|
+
function joinSentences(sentences) {
|
|
646
|
+
return sentences.map((sentence) => sentence.text).join(" ");
|
|
647
|
+
}
|
|
648
|
+
function countUniqueQids(candidates) {
|
|
649
|
+
return new Set(
|
|
650
|
+
candidates.flatMap(
|
|
651
|
+
(candidate) => candidate.qidOptions.map((option) => option.qid)
|
|
652
|
+
)
|
|
653
|
+
).size;
|
|
654
|
+
}
|
|
655
|
+
function toMentionRecord(chapterId, mention, location, index) {
|
|
656
|
+
return {
|
|
657
|
+
chapterId,
|
|
658
|
+
...mention.confidence === void 0 ? {} : { confidence: mention.confidence },
|
|
659
|
+
id: `m${chapterId}-${index}`,
|
|
660
|
+
...mention.note === void 0 ? {} : { note: mention.note },
|
|
661
|
+
qid: mention.qid,
|
|
662
|
+
rangeEnd: location.rangeStart + mention.surface.length,
|
|
663
|
+
rangeStart: location.rangeStart,
|
|
664
|
+
sentenceIndex: location.sentenceIndex,
|
|
665
|
+
surface: mention.surface
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
function buildSentenceLocations(fragments) {
|
|
669
|
+
const locations = [];
|
|
670
|
+
let offset = 0;
|
|
671
|
+
for (const fragment of fragments) {
|
|
672
|
+
for (let sentenceIndex = 0; sentenceIndex < fragment.sentences.length; sentenceIndex += 1) {
|
|
673
|
+
const length = fragment.sentences[sentenceIndex].text.length;
|
|
674
|
+
locations.push({
|
|
675
|
+
absoluteStart: offset,
|
|
676
|
+
length,
|
|
677
|
+
sentenceIndex: fragment.fragmentId + sentenceIndex
|
|
678
|
+
});
|
|
679
|
+
offset += length + 1;
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
return locations;
|
|
683
|
+
}
|
|
684
|
+
function locateMention(locations, absoluteOffset) {
|
|
685
|
+
for (const location of locations) {
|
|
686
|
+
const rangeEnd = location.absoluteStart + location.length;
|
|
687
|
+
if (absoluteOffset >= location.absoluteStart && absoluteOffset < rangeEnd) {
|
|
688
|
+
return {
|
|
689
|
+
rangeStart: absoluteOffset - location.absoluteStart,
|
|
690
|
+
sentenceIndex: location.sentenceIndex
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
throw new Error(`Mention offset ${absoluteOffset} is outside chapter text.`);
|
|
695
|
+
}
|
|
696
|
+
async function readChapterFragments(document, chapterId) {
|
|
697
|
+
const serialFragments = document.getSerialFragments(chapterId);
|
|
698
|
+
return await Promise.all(
|
|
699
|
+
(await serialFragments.listFragmentIds()).map(
|
|
700
|
+
async (fragmentId) => await serialFragments.getFragment(fragmentId)
|
|
701
|
+
)
|
|
702
|
+
);
|
|
703
|
+
}
|
|
704
|
+
function joinFragmentText(fragments) {
|
|
705
|
+
return fragments.flatMap((fragment) => fragment.sentences.map((sentence) => sentence.text)).join(" ");
|
|
706
|
+
}
|
|
707
|
+
function createWikimatchSentences(fragments) {
|
|
708
|
+
const sentences = [];
|
|
709
|
+
let offset = 0;
|
|
710
|
+
for (const fragment of fragments) {
|
|
711
|
+
for (let index = 0; index < fragment.sentences.length; index += 1) {
|
|
712
|
+
const sentence = fragment.sentences[index];
|
|
713
|
+
sentences.push({
|
|
714
|
+
id: `${fragment.serialId}:${fragment.fragmentId + index}`,
|
|
715
|
+
range: {
|
|
716
|
+
end: offset + sentence.text.length,
|
|
717
|
+
start: offset
|
|
718
|
+
},
|
|
719
|
+
text: sentence.text
|
|
720
|
+
});
|
|
721
|
+
offset += sentence.text.length + 1;
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
return sentences;
|
|
725
|
+
}
|
|
726
|
+
async function commitChapterKnowledgeGraphArtifact(document, artifact) {
|
|
727
|
+
const mentions = await readJsonl(artifact.mentionsPath, parseMentionRecord);
|
|
728
|
+
const mentionLinks = await readJsonl(
|
|
729
|
+
artifact.mentionLinksPath,
|
|
730
|
+
parseMentionLinkRecord
|
|
731
|
+
);
|
|
732
|
+
validateChapterKnowledgeGraphArtifact(artifact.chapterId, {
|
|
733
|
+
mentionLinks,
|
|
734
|
+
mentions
|
|
735
|
+
});
|
|
736
|
+
await document.openSession(async (openedDocument) => {
|
|
737
|
+
await openedDocument.serials.ensure(artifact.chapterId);
|
|
738
|
+
const existingLinks = await openedDocument.mentionLinks.listByChapter(
|
|
739
|
+
artifact.chapterId
|
|
740
|
+
);
|
|
741
|
+
if (existingLinks.length > 0 && mentionLinks.length === 0) {
|
|
742
|
+
throw new Error(
|
|
743
|
+
`Refusing to replace chapter ${artifact.chapterId} knowledge graph with an artifact that contains no mention links.`
|
|
744
|
+
);
|
|
745
|
+
}
|
|
746
|
+
await openedDocument.mentionLinks.deleteByChapter(artifact.chapterId);
|
|
747
|
+
await openedDocument.mentions.deleteByChapter(artifact.chapterId);
|
|
748
|
+
await openedDocument.mentions.saveMany(mentions);
|
|
749
|
+
await openedDocument.mentionLinks.saveMany(mentionLinks);
|
|
750
|
+
const parameter = await openedDocument.graphBuildParameters.save(
|
|
751
|
+
artifact.parameter
|
|
752
|
+
);
|
|
753
|
+
await openedDocument.serials.setKnowledgeGraphReady(
|
|
754
|
+
artifact.chapterId,
|
|
755
|
+
true,
|
|
756
|
+
parameter.hash
|
|
757
|
+
);
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
async function clearChapterKnowledgeGraph(document, chapterId) {
|
|
761
|
+
await document.openSession(async (openedDocument) => {
|
|
762
|
+
await openedDocument.mentionLinks.deleteByChapter(chapterId);
|
|
763
|
+
await openedDocument.mentions.deleteByChapter(chapterId);
|
|
764
|
+
await openedDocument.serials.setKnowledgeGraphReady(chapterId, false);
|
|
765
|
+
await openedDocument.graphBuildParameters.deleteUnreferenced();
|
|
766
|
+
});
|
|
767
|
+
}
|
|
768
|
+
function validateChapterKnowledgeGraphArtifact(chapterId, records) {
|
|
769
|
+
const mentionIds = /* @__PURE__ */ new Set();
|
|
770
|
+
for (const mention of records.mentions) {
|
|
771
|
+
if (mention.chapterId !== chapterId) {
|
|
772
|
+
throw new Error(
|
|
773
|
+
`Mention ${mention.id} belongs to chapter ${mention.chapterId}, expected chapter ${chapterId}.`
|
|
774
|
+
);
|
|
775
|
+
}
|
|
776
|
+
if (mention.rangeEnd <= mention.rangeStart) {
|
|
777
|
+
throw new Error(
|
|
778
|
+
`Mention ${mention.id} has invalid range [${mention.rangeStart}, ${mention.rangeEnd}).`
|
|
779
|
+
);
|
|
780
|
+
}
|
|
781
|
+
if (mentionIds.has(mention.id)) {
|
|
782
|
+
throw new Error(`Duplicate mention id ${mention.id}.`);
|
|
783
|
+
}
|
|
784
|
+
mentionIds.add(mention.id);
|
|
785
|
+
}
|
|
786
|
+
const linkIds = /* @__PURE__ */ new Set();
|
|
787
|
+
for (const link of records.mentionLinks) {
|
|
788
|
+
if (linkIds.has(link.id)) {
|
|
789
|
+
throw new Error(`Duplicate mention link id ${link.id}.`);
|
|
790
|
+
}
|
|
791
|
+
if (!mentionIds.has(link.sourceMentionId)) {
|
|
792
|
+
throw new Error(
|
|
793
|
+
`Mention link ${link.id} references unknown source mention ${link.sourceMentionId}.`
|
|
794
|
+
);
|
|
795
|
+
}
|
|
796
|
+
if (!mentionIds.has(link.targetMentionId)) {
|
|
797
|
+
throw new Error(
|
|
798
|
+
`Mention link ${link.id} references unknown target mention ${link.targetMentionId}.`
|
|
799
|
+
);
|
|
800
|
+
}
|
|
801
|
+
if (link.evidenceSentenceIds.length === 0) {
|
|
802
|
+
throw new Error(`Mention link ${link.id} has no evidence sentences.`);
|
|
803
|
+
}
|
|
804
|
+
for (const sentenceId of link.evidenceSentenceIds) {
|
|
805
|
+
if (sentenceId[0] !== chapterId) {
|
|
806
|
+
throw new Error(
|
|
807
|
+
`Mention link ${link.id} evidence sentence ${formatSentenceId(sentenceId)} is outside chapter ${chapterId}.`
|
|
808
|
+
);
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
linkIds.add(link.id);
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
function formatSentenceId(sentenceId) {
|
|
815
|
+
return sentenceId.join(":");
|
|
816
|
+
}
|
|
817
|
+
async function writeJsonl(path, records, parseRecord) {
|
|
818
|
+
const stream = createWriteStream(path, { encoding: "utf8", flags: "wx" });
|
|
819
|
+
try {
|
|
820
|
+
for await (const record of records) {
|
|
821
|
+
stream.write(`${JSON.stringify(parseRecord(record))}
|
|
822
|
+
`);
|
|
823
|
+
}
|
|
824
|
+
} finally {
|
|
825
|
+
await closeWritableStream(stream);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
async function readJsonl(path, parseRecord) {
|
|
829
|
+
const records = [];
|
|
830
|
+
const lines = createInterface({
|
|
831
|
+
crlfDelay: Infinity,
|
|
832
|
+
input: createReadStream(path, { encoding: "utf8" })
|
|
833
|
+
});
|
|
834
|
+
let lineNumber = 0;
|
|
835
|
+
for await (const line of lines) {
|
|
836
|
+
lineNumber += 1;
|
|
837
|
+
if (line.trim() === "") {
|
|
838
|
+
continue;
|
|
839
|
+
}
|
|
840
|
+
try {
|
|
841
|
+
records.push(parseRecord(JSON.parse(line)));
|
|
842
|
+
} catch (error) {
|
|
843
|
+
throw new Error(`Invalid JSONL record at ${path}:${lineNumber}`, {
|
|
844
|
+
cause: error
|
|
845
|
+
});
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
return records;
|
|
849
|
+
}
|
|
850
|
+
function parseMentionRecord(record) {
|
|
851
|
+
const parsed = mentionRecordSchema.parse(record);
|
|
852
|
+
return {
|
|
853
|
+
chapterId: parsed.chapterId,
|
|
854
|
+
...parsed.confidence === void 0 ? {} : { confidence: parsed.confidence },
|
|
855
|
+
id: parsed.id,
|
|
856
|
+
...parsed.note === void 0 ? {} : { note: parsed.note },
|
|
857
|
+
qid: parsed.qid,
|
|
858
|
+
rangeEnd: parsed.rangeEnd,
|
|
859
|
+
rangeStart: parsed.rangeStart,
|
|
860
|
+
...parsed.sentenceIndex === void 0 ? {} : { sentenceIndex: parsed.sentenceIndex },
|
|
861
|
+
surface: parsed.surface
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
function parseMentionLinkRecord(record) {
|
|
865
|
+
const parsed = mentionLinkRecordSchema.parse(record);
|
|
866
|
+
return {
|
|
867
|
+
...parsed.confidence === void 0 ? {} : { confidence: parsed.confidence },
|
|
868
|
+
evidenceSentenceIds: parsed.evidenceSentenceIds,
|
|
869
|
+
id: parsed.id,
|
|
870
|
+
...parsed.note === void 0 ? {} : { note: parsed.note },
|
|
871
|
+
predicate: parsed.predicate,
|
|
872
|
+
sourceMentionId: parsed.sourceMentionId,
|
|
873
|
+
targetMentionId: parsed.targetMentionId
|
|
874
|
+
};
|
|
875
|
+
}
|
|
876
|
+
async function closeWritableStream(stream) {
|
|
877
|
+
await new Promise((resolveClose, rejectClose) => {
|
|
878
|
+
stream.end((error) => {
|
|
879
|
+
if (error !== void 0 && error !== null) {
|
|
880
|
+
rejectClose(error);
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
resolveClose();
|
|
884
|
+
});
|
|
885
|
+
});
|
|
886
|
+
}
|
|
887
|
+
export {
|
|
888
|
+
buildChapterKnowledgeGraphArtifact,
|
|
889
|
+
clearChapterKnowledgeGraph,
|
|
890
|
+
commitChapterKnowledgeGraphArtifact,
|
|
891
|
+
createEnrichmentProgressReporter,
|
|
892
|
+
generateChapterKnowledgeGraphArtifact,
|
|
893
|
+
generateChapterKnowledgeGraphArtifactFromSnapshot,
|
|
894
|
+
groundWikimatchCandidates,
|
|
895
|
+
snapshotChapterKnowledgeGraphInput
|
|
896
|
+
};
|
|
897
|
+
//# sourceMappingURL=knowledge-graph-build.js.map
|