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
package/dist/gc.d.cts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { G as GcRunReport } from './types-FVIqiLC9.cjs';
|
|
2
|
+
export { a as GcContext, b as GcJob, c as GcJobReport, d as GcJobResult } from './types-FVIqiLC9.cjs';
|
|
3
|
+
|
|
4
|
+
declare function tryRunWikiGraphGc(options?: {
|
|
5
|
+
readonly dryRun?: boolean;
|
|
6
|
+
readonly force?: boolean;
|
|
7
|
+
readonly opportunistic?: boolean;
|
|
8
|
+
}): Promise<GcRunReport>;
|
|
9
|
+
|
|
10
|
+
export { GcRunReport, tryRunWikiGraphGc };
|
package/dist/gc.d.ts
ADDED
package/dist/gc.js
ADDED
package/dist/gc.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/gc.ts"],"sourcesContent":["export { tryRunWikiGraphGc } from \"./gc/index.js\";\nexport type {\n GcContext,\n GcJob,\n GcJobReport,\n GcJobResult,\n GcRunReport,\n} from \"./gc/index.js\";\n"],"mappings":"AAAA,SAAS,yBAAyB;","names":[]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { LLMessage } from '../llm/types.js';
|
|
2
|
+
import 'ai';
|
|
3
|
+
|
|
4
|
+
declare const RESPONSE_INTENT_CLASSIFIER_PROMPT_TEMPLATE = "guaranteed/response_intent_classifier";
|
|
5
|
+
type GuaranteedResponseIntent = "ambiguous" | "malformed_json" | "natural_language";
|
|
6
|
+
declare function buildResponseIntentClassificationMessages(systemPrompt: string, response: string): readonly LLMessage[];
|
|
7
|
+
declare function classifyResponseIntentLocally(response: string): GuaranteedResponseIntent;
|
|
8
|
+
declare function parseResponseIntentClassification(response: string): GuaranteedResponseIntent;
|
|
9
|
+
|
|
10
|
+
export { type GuaranteedResponseIntent, RESPONSE_INTENT_CLASSIFIER_PROMPT_TEMPLATE, buildResponseIntentClassificationMessages, classifyResponseIntentLocally, parseResponseIntentClassification };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
const RESPONSE_INTENT_CLASSIFIER_PROMPT_TEMPLATE = "guaranteed/response_intent_classifier";
|
|
2
|
+
const NATURAL_LANGUAGE_PATTERNS = [
|
|
3
|
+
/\b(?:sorry|apologies)\b/i,
|
|
4
|
+
/\b(?:i\s+can(?:not|'t)|unable\s+to|do\s+not\s+understand)\b/i,
|
|
5
|
+
/\b(?:cannot\s+answer|can't\s+answer|can't\s+help)\b/i,
|
|
6
|
+
/\bno\s+relevant\s+result/i,
|
|
7
|
+
/抱歉/,
|
|
8
|
+
/无法回答/,
|
|
9
|
+
/无法识别/,
|
|
10
|
+
/无法给到/,
|
|
11
|
+
/没有找到相关的结果/
|
|
12
|
+
];
|
|
13
|
+
function buildResponseIntentClassificationMessages(systemPrompt, response) {
|
|
14
|
+
return [
|
|
15
|
+
{
|
|
16
|
+
role: "system",
|
|
17
|
+
content: systemPrompt
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
role: "user",
|
|
21
|
+
content: response
|
|
22
|
+
}
|
|
23
|
+
];
|
|
24
|
+
}
|
|
25
|
+
function classifyResponseIntentLocally(response) {
|
|
26
|
+
const trimmed = response.trim();
|
|
27
|
+
if (trimmed === "") {
|
|
28
|
+
return "ambiguous";
|
|
29
|
+
}
|
|
30
|
+
if (hasStrongMalformedJsonSignal(trimmed)) {
|
|
31
|
+
return "malformed_json";
|
|
32
|
+
}
|
|
33
|
+
if (looksLikeNaturalLanguage(trimmed)) {
|
|
34
|
+
return "natural_language";
|
|
35
|
+
}
|
|
36
|
+
if (hasWeakMalformedJsonSignal(trimmed)) {
|
|
37
|
+
return "ambiguous";
|
|
38
|
+
}
|
|
39
|
+
if (looksLikePlainSentence(trimmed)) {
|
|
40
|
+
return "natural_language";
|
|
41
|
+
}
|
|
42
|
+
return "ambiguous";
|
|
43
|
+
}
|
|
44
|
+
function parseResponseIntentClassification(response) {
|
|
45
|
+
const normalized = response.trim().toLowerCase();
|
|
46
|
+
if (normalized.includes("malformed_json")) {
|
|
47
|
+
return "malformed_json";
|
|
48
|
+
}
|
|
49
|
+
if (normalized.includes("natural_language")) {
|
|
50
|
+
return "natural_language";
|
|
51
|
+
}
|
|
52
|
+
if (normalized.includes("ambiguous")) {
|
|
53
|
+
return "ambiguous";
|
|
54
|
+
}
|
|
55
|
+
return "ambiguous";
|
|
56
|
+
}
|
|
57
|
+
function hasStrongMalformedJsonSignal(text) {
|
|
58
|
+
return /^(?:```json\b|```|{|\[)/i.test(text) || /"[^"\r\n]+"\s*:/.test(text) || hasWeakMalformedJsonSignal(text) && endsWithLikelyTruncationToken(text);
|
|
59
|
+
}
|
|
60
|
+
function hasWeakMalformedJsonSignal(text) {
|
|
61
|
+
const punctuationCount = (text.match(/[{}[\]:,]/g) ?? []).length + (text.match(/"/g) ?? []).length;
|
|
62
|
+
return /:\s/.test(text) || punctuationCount >= 3 || hasUnbalancedBrackets(text) || /[:,]\s*$/.test(text);
|
|
63
|
+
}
|
|
64
|
+
function hasUnbalancedBrackets(text) {
|
|
65
|
+
return count(text, "{") !== count(text, "}") || count(text, "[") !== count(text, "]") ? true : false;
|
|
66
|
+
}
|
|
67
|
+
function endsWithLikelyTruncationToken(text) {
|
|
68
|
+
return /[:, "{[]\s*$/.test(text);
|
|
69
|
+
}
|
|
70
|
+
function looksLikeNaturalLanguage(text) {
|
|
71
|
+
if (NATURAL_LANGUAGE_PATTERNS.some((pattern) => pattern.test(text))) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
return !hasWeakMalformedJsonSignal(text) && looksLikePlainSentence(text);
|
|
75
|
+
}
|
|
76
|
+
function looksLikePlainSentence(text) {
|
|
77
|
+
return /[\p{Script=Han}A-Za-z]/u.test(text) && !/^[{[]/.test(text) && !/"[^"\r\n]+"\s*:/.test(text);
|
|
78
|
+
}
|
|
79
|
+
function count(text, character) {
|
|
80
|
+
return text.split(character).length - 1;
|
|
81
|
+
}
|
|
82
|
+
export {
|
|
83
|
+
RESPONSE_INTENT_CLASSIFIER_PROMPT_TEMPLATE,
|
|
84
|
+
buildResponseIntentClassificationMessages,
|
|
85
|
+
classifyResponseIntentLocally,
|
|
86
|
+
parseResponseIntentClassification
|
|
87
|
+
};
|
|
88
|
+
//# sourceMappingURL=classifier.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/guaranteed/classifier.ts"],"sourcesContent":["import type { LLMessage } from \"../llm/index.js\";\n\nexport const RESPONSE_INTENT_CLASSIFIER_PROMPT_TEMPLATE =\n \"guaranteed/response_intent_classifier\";\n\nexport type GuaranteedResponseIntent =\n | \"ambiguous\"\n | \"malformed_json\"\n | \"natural_language\";\n\nconst NATURAL_LANGUAGE_PATTERNS = [\n /\\b(?:sorry|apologies)\\b/i,\n /\\b(?:i\\s+can(?:not|'t)|unable\\s+to|do\\s+not\\s+understand)\\b/i,\n /\\b(?:cannot\\s+answer|can't\\s+answer|can't\\s+help)\\b/i,\n /\\bno\\s+relevant\\s+result/i,\n /抱歉/,\n /无法回答/,\n /无法识别/,\n /无法给到/,\n /没有找到相关的结果/,\n];\n\nexport function buildResponseIntentClassificationMessages(\n systemPrompt: string,\n response: string,\n): readonly LLMessage[] {\n return [\n {\n role: \"system\",\n content: systemPrompt,\n },\n {\n role: \"user\",\n content: response,\n },\n ];\n}\n\nexport function classifyResponseIntentLocally(\n response: string,\n): GuaranteedResponseIntent {\n const trimmed = response.trim();\n\n if (trimmed === \"\") {\n return \"ambiguous\";\n }\n\n if (hasStrongMalformedJsonSignal(trimmed)) {\n return \"malformed_json\";\n }\n\n if (looksLikeNaturalLanguage(trimmed)) {\n return \"natural_language\";\n }\n\n if (hasWeakMalformedJsonSignal(trimmed)) {\n return \"ambiguous\";\n }\n\n if (looksLikePlainSentence(trimmed)) {\n return \"natural_language\";\n }\n\n return \"ambiguous\";\n}\n\nexport function parseResponseIntentClassification(\n response: string,\n): GuaranteedResponseIntent {\n const normalized = response.trim().toLowerCase();\n\n if (normalized.includes(\"malformed_json\")) {\n return \"malformed_json\";\n }\n\n if (normalized.includes(\"natural_language\")) {\n return \"natural_language\";\n }\n\n if (normalized.includes(\"ambiguous\")) {\n return \"ambiguous\";\n }\n\n return \"ambiguous\";\n}\n\nfunction hasStrongMalformedJsonSignal(text: string): boolean {\n return (\n /^(?:```json\\b|```|{|\\[)/i.test(text) ||\n /\"[^\"\\r\\n]+\"\\s*:/.test(text) ||\n (hasWeakMalformedJsonSignal(text) && endsWithLikelyTruncationToken(text))\n );\n}\n\nfunction hasWeakMalformedJsonSignal(text: string): boolean {\n const punctuationCount =\n (text.match(/[{}[\\]:,]/g) ?? []).length + (text.match(/\"/g) ?? []).length;\n\n return (\n /:\\s/.test(text) ||\n punctuationCount >= 3 ||\n hasUnbalancedBrackets(text) ||\n /[:,]\\s*$/.test(text)\n );\n}\n\nfunction hasUnbalancedBrackets(text: string): boolean {\n return count(text, \"{\") !== count(text, \"}\") ||\n count(text, \"[\") !== count(text, \"]\")\n ? true\n : false;\n}\n\nfunction endsWithLikelyTruncationToken(text: string): boolean {\n return /[:, \"{[]\\s*$/.test(text);\n}\n\nfunction looksLikeNaturalLanguage(text: string): boolean {\n if (NATURAL_LANGUAGE_PATTERNS.some((pattern) => pattern.test(text))) {\n return true;\n }\n\n return !hasWeakMalformedJsonSignal(text) && looksLikePlainSentence(text);\n}\n\nfunction looksLikePlainSentence(text: string): boolean {\n return (\n /[\\p{Script=Han}A-Za-z]/u.test(text) &&\n !/^[{[]/.test(text) &&\n !/\"[^\"\\r\\n]+\"\\s*:/.test(text)\n );\n}\n\nfunction count(text: string, character: string): number {\n return text.split(character).length - 1;\n}\n"],"mappings":"AAEO,MAAM,6CACX;AAOF,MAAM,4BAA4B;AAAA,EAChC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEO,SAAS,0CACd,cACA,UACsB;AACtB,SAAO;AAAA,IACL;AAAA,MACE,MAAM;AAAA,MACN,SAAS;AAAA,IACX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AAK,IAAI;AACjC;AAEA,SAAS,yBAAyB,MAAuB;AACvD,MAAI,0BAA0B,KAAK,CAAC,YAAY,QAAQ,KAAK,IAAI,CAAC,GAAG;AACnE,WAAO;AAAA,EACT;AAEA,SAAO,CAAC,2BAA2B,IAAI,KAAK,uBAAuB,IAAI;AACzE;AAEA,SAAS,uBAAuB,MAAuB;AACrD,SACE,0BAA0B,KAAK,IAAI,KACnC,CAAC,QAAQ,KAAK,IAAI,KAClB,CAAC,kBAAkB,KAAK,IAAI;AAEhC;AAEA,SAAS,MAAM,MAAc,WAA2B;AACtD,SAAO,KAAK,MAAM,SAAS,EAAE,SAAS;AACxC;","names":[]}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
declare class ParsedJsonError extends Error {
|
|
2
|
+
readonly issues: readonly string[];
|
|
3
|
+
constructor(issues: readonly string[]);
|
|
4
|
+
}
|
|
5
|
+
declare abstract class GuaranteedRequestFailureError extends Error {
|
|
6
|
+
readonly attempts: number;
|
|
7
|
+
readonly maxRetries: number;
|
|
8
|
+
protected constructor(message: string, attempts: number, maxRetries: number);
|
|
9
|
+
}
|
|
10
|
+
declare class GuaranteedEmptyResponseError extends GuaranteedRequestFailureError {
|
|
11
|
+
constructor(attempts: number, maxRetries: number);
|
|
12
|
+
}
|
|
13
|
+
declare class SuspectedModelRefusalError extends GuaranteedRequestFailureError {
|
|
14
|
+
readonly response: string;
|
|
15
|
+
readonly reason: string;
|
|
16
|
+
constructor(attempts: number, maxRetries: number, input: {
|
|
17
|
+
response: string;
|
|
18
|
+
reason: string;
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
declare class GuaranteedSchemaValidationError extends GuaranteedRequestFailureError {
|
|
22
|
+
readonly issues: readonly string[];
|
|
23
|
+
readonly response: string;
|
|
24
|
+
constructor(attempts: number, maxRetries: number, input: {
|
|
25
|
+
issues: readonly string[];
|
|
26
|
+
response: string;
|
|
27
|
+
}, cause: unknown);
|
|
28
|
+
}
|
|
29
|
+
declare class GuaranteedParseValidationError extends GuaranteedRequestFailureError {
|
|
30
|
+
readonly issues: readonly string[];
|
|
31
|
+
readonly response: string;
|
|
32
|
+
constructor(attempts: number, maxRetries: number, input: {
|
|
33
|
+
issues: readonly string[];
|
|
34
|
+
response: string;
|
|
35
|
+
}, cause: unknown);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { GuaranteedEmptyResponseError, GuaranteedParseValidationError, GuaranteedRequestFailureError, GuaranteedSchemaValidationError, ParsedJsonError, SuspectedModelRefusalError };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
class ParsedJsonError extends Error {
|
|
2
|
+
issues;
|
|
3
|
+
constructor(issues) {
|
|
4
|
+
super(`Parse failed with ${issues.length} issue(s)`);
|
|
5
|
+
this.name = "ParsedJsonError";
|
|
6
|
+
this.issues = [...issues];
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
class GuaranteedRequestFailureError extends Error {
|
|
10
|
+
attempts;
|
|
11
|
+
maxRetries;
|
|
12
|
+
constructor(message, attempts, maxRetries) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = "GuaranteedRequestFailureError";
|
|
15
|
+
this.attempts = attempts;
|
|
16
|
+
this.maxRetries = maxRetries;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
class GuaranteedEmptyResponseError extends GuaranteedRequestFailureError {
|
|
20
|
+
constructor(attempts, maxRetries) {
|
|
21
|
+
super(
|
|
22
|
+
"LLM returned empty response after all retries",
|
|
23
|
+
attempts,
|
|
24
|
+
maxRetries
|
|
25
|
+
);
|
|
26
|
+
this.name = "GuaranteedEmptyResponseError";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
class SuspectedModelRefusalError extends GuaranteedRequestFailureError {
|
|
30
|
+
response;
|
|
31
|
+
reason;
|
|
32
|
+
constructor(attempts, maxRetries, input) {
|
|
33
|
+
super(
|
|
34
|
+
`Suspected model refusal after ${attempts} JSON syntax error attempt(s): ${input.reason}. Last response: ${JSON.stringify(input.response)}`,
|
|
35
|
+
attempts,
|
|
36
|
+
maxRetries
|
|
37
|
+
);
|
|
38
|
+
this.name = "SuspectedModelRefusalError";
|
|
39
|
+
this.response = input.response;
|
|
40
|
+
this.reason = input.reason;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
class GuaranteedSchemaValidationError extends GuaranteedRequestFailureError {
|
|
44
|
+
issues;
|
|
45
|
+
response;
|
|
46
|
+
constructor(attempts, maxRetries, input, cause) {
|
|
47
|
+
super("Schema validation failed after all retries", attempts, maxRetries);
|
|
48
|
+
this.name = "GuaranteedSchemaValidationError";
|
|
49
|
+
this.issues = [...input.issues];
|
|
50
|
+
this.response = input.response;
|
|
51
|
+
this.cause = cause;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
class GuaranteedParseValidationError extends GuaranteedRequestFailureError {
|
|
55
|
+
issues;
|
|
56
|
+
response;
|
|
57
|
+
constructor(attempts, maxRetries, input, cause) {
|
|
58
|
+
super("Parse validation failed after all retries", attempts, maxRetries);
|
|
59
|
+
this.name = "GuaranteedParseValidationError";
|
|
60
|
+
this.issues = [...input.issues];
|
|
61
|
+
this.response = input.response;
|
|
62
|
+
this.cause = cause;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
export {
|
|
66
|
+
GuaranteedEmptyResponseError,
|
|
67
|
+
GuaranteedParseValidationError,
|
|
68
|
+
GuaranteedRequestFailureError,
|
|
69
|
+
GuaranteedSchemaValidationError,
|
|
70
|
+
ParsedJsonError,
|
|
71
|
+
SuspectedModelRefusalError
|
|
72
|
+
};
|
|
73
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/guaranteed/errors.ts"],"sourcesContent":["export class ParsedJsonError extends Error {\n public readonly issues: readonly string[];\n\n public constructor(issues: readonly string[]) {\n super(`Parse failed with ${issues.length} issue(s)`);\n this.name = \"ParsedJsonError\";\n this.issues = [...issues];\n }\n}\n\nexport abstract class GuaranteedRequestFailureError extends Error {\n public readonly attempts: number;\n public readonly maxRetries: number;\n\n protected constructor(message: string, attempts: number, maxRetries: number) {\n super(message);\n this.name = \"GuaranteedRequestFailureError\";\n this.attempts = attempts;\n this.maxRetries = maxRetries;\n }\n}\n\nexport class GuaranteedEmptyResponseError extends GuaranteedRequestFailureError {\n public constructor(attempts: number, maxRetries: number) {\n super(\n \"LLM returned empty response after all retries\",\n attempts,\n maxRetries,\n );\n this.name = \"GuaranteedEmptyResponseError\";\n }\n}\n\nexport class SuspectedModelRefusalError extends GuaranteedRequestFailureError {\n public readonly response: string;\n public readonly reason: string;\n\n public constructor(\n attempts: number,\n maxRetries: number,\n input: {\n response: string;\n reason: string;\n },\n ) {\n super(\n `Suspected model refusal after ${attempts} JSON syntax error attempt(s): ${input.reason}. Last response: ${JSON.stringify(input.response)}`,\n attempts,\n maxRetries,\n );\n this.name = \"SuspectedModelRefusalError\";\n this.response = input.response;\n this.reason = input.reason;\n }\n}\n\nexport class GuaranteedSchemaValidationError extends GuaranteedRequestFailureError {\n public readonly issues: readonly string[];\n public readonly response: string;\n\n public constructor(\n attempts: number,\n maxRetries: number,\n input: {\n issues: readonly string[];\n response: string;\n },\n cause: unknown,\n ) {\n super(\"Schema validation failed after all retries\", attempts, maxRetries);\n this.name = \"GuaranteedSchemaValidationError\";\n this.issues = [...input.issues];\n this.response = input.response;\n this.cause = cause;\n }\n}\n\nexport class GuaranteedParseValidationError extends GuaranteedRequestFailureError {\n public readonly issues: readonly string[];\n public readonly response: string;\n\n public constructor(\n attempts: number,\n maxRetries: number,\n input: {\n issues: readonly string[];\n response: string;\n },\n cause: unknown,\n ) {\n super(\"Parse validation failed after all retries\", attempts, maxRetries);\n this.name = \"GuaranteedParseValidationError\";\n this.issues = [...input.issues];\n this.response = input.response;\n this.cause = cause;\n }\n}\n"],"mappings":"AAAO,MAAM,wBAAwB,MAAM;AAAA,EACzB;AAAA,EAET,YAAY,QAA2B;AAC5C,U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|
1
|
+
export { GuaranteedEmptyResponseError, GuaranteedParseValidationError, GuaranteedRequestFailureError, GuaranteedSchemaValidationError, ParsedJsonError, SuspectedModelRefusalError } from './errors.js';
|
|
2
|
+
export { RESPONSE_INTENT_CLASSIFIER_PROMPT_TEMPLATE } from './classifier.js';
|
|
3
|
+
export { requestGuaranteedJson } from './request.js';
|
|
4
|
+
export { GuaranteedLazyRequest, GuaranteedParser, GuaranteedRequest, GuaranteedRequestController, GuaranteedRequestOptions } from './types.js';
|
|
5
|
+
import '../llm/types.js';
|
|
6
|
+
import 'ai';
|
|
7
|
+
import 'zod';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {
|
|
2
|
+
GuaranteedEmptyResponseError,
|
|
3
|
+
GuaranteedRequestFailureError,
|
|
4
|
+
GuaranteedParseValidationError,
|
|
5
|
+
GuaranteedSchemaValidationError,
|
|
6
|
+
ParsedJsonError,
|
|
7
|
+
SuspectedModelRefusalError
|
|
8
|
+
} from "./errors.js";
|
|
9
|
+
import { RESPONSE_INTENT_CLASSIFIER_PROMPT_TEMPLATE } from "./classifier.js";
|
|
10
|
+
import { requestGuaranteedJson } from "./request.js";
|
|
11
|
+
export {
|
|
12
|
+
GuaranteedEmptyResponseError,
|
|
13
|
+
GuaranteedParseValidationError,
|
|
14
|
+
GuaranteedRequestFailureError,
|
|
15
|
+
GuaranteedSchemaValidationError,
|
|
16
|
+
ParsedJsonError,
|
|
17
|
+
RESPONSE_INTENT_CLASSIFIER_PROMPT_TEMPLATE,
|
|
18
|
+
SuspectedModelRefusalError,
|
|
19
|
+
requestGuaranteedJson
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/guaranteed/index.ts"],"sourcesContent":["export {\n GuaranteedEmptyResponseError,\n GuaranteedRequestFailureError,\n GuaranteedParseValidationError,\n GuaranteedSchemaValidationError,\n ParsedJsonError,\n SuspectedModelRefusalError,\n} from \"./errors.js\";\nexport { RESPONSE_INTENT_CLASSIFIER_PROMPT_TEMPLATE } from \"./classifier.js\";\nexport { requestGuaranteedJson } from \"./request.js\";\nexport type {\n GuaranteedLazyRequest,\n GuaranteedParser,\n GuaranteedRequest,\n GuaranteedRequestController,\n GuaranteedRequestOptions,\n} from \"./types.js\";\n"],"mappings":"AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,kDAAkD;AAC3D,SAAS,6BAA6B;","names":[]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { GuaranteedRequestOptions } from './types.js';
|
|
2
|
+
import '../llm/types.js';
|
|
3
|
+
import 'ai';
|
|
4
|
+
import 'zod';
|
|
5
|
+
|
|
6
|
+
declare function requestGuaranteedJson<TData, TResult>(options: GuaranteedRequestOptions<TData, TResult>): Promise<TResult>;
|
|
7
|
+
|
|
8
|
+
export { requestGuaranteedJson };
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildResponseIntentClassificationMessages,
|
|
3
|
+
classifyResponseIntentLocally,
|
|
4
|
+
parseResponseIntentClassification
|
|
5
|
+
} from "./classifier.js";
|
|
6
|
+
import {
|
|
7
|
+
GuaranteedEmptyResponseError,
|
|
8
|
+
GuaranteedParseValidationError,
|
|
9
|
+
GuaranteedSchemaValidationError,
|
|
10
|
+
ParsedJsonError,
|
|
11
|
+
SuspectedModelRefusalError
|
|
12
|
+
} from "./errors.js";
|
|
13
|
+
import {
|
|
14
|
+
buildMalformedJsonMessage,
|
|
15
|
+
buildNaturalLanguageMessage,
|
|
16
|
+
buildBusinessErrorMessage,
|
|
17
|
+
buildSchemaErrorMessage,
|
|
18
|
+
buildSyntaxErrorMessage,
|
|
19
|
+
extractJsonText,
|
|
20
|
+
listSchemaIssues,
|
|
21
|
+
repairJsonText
|
|
22
|
+
} from "./response.js";
|
|
23
|
+
const DEFAULT_MAX_RETRIES = 12;
|
|
24
|
+
const MAX_MALFORMED_JSON_REPAIR_HISTORY_ATTEMPTS = 3;
|
|
25
|
+
async function requestGuaranteedJson(options) {
|
|
26
|
+
const initialMessages = [...options.messages];
|
|
27
|
+
let currentMessages = [...options.messages];
|
|
28
|
+
let consecutiveProtocolDerailments = 0;
|
|
29
|
+
let consecutiveMalformedJsonFailures = 0;
|
|
30
|
+
const maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
31
|
+
for (let index = 0; index <= maxRetries; index += 1) {
|
|
32
|
+
const response = await options.request(currentMessages, index, maxRetries);
|
|
33
|
+
if (response === void 0 || response.trim() === "") {
|
|
34
|
+
if (index >= maxRetries) {
|
|
35
|
+
throw new GuaranteedEmptyResponseError(index + 1, maxRetries);
|
|
36
|
+
}
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
let parsedData;
|
|
40
|
+
try {
|
|
41
|
+
const extractedJsonText = extractJsonText(response);
|
|
42
|
+
const repairedJsonText = repairJsonText(extractedJsonText);
|
|
43
|
+
parsedData = JSON.parse(repairedJsonText);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
const intent = await classifyResponseIntent(options, response, index);
|
|
46
|
+
if (intent === "natural_language") {
|
|
47
|
+
consecutiveProtocolDerailments += 1;
|
|
48
|
+
consecutiveMalformedJsonFailures = 0;
|
|
49
|
+
if (consecutiveProtocolDerailments >= 2 || index >= maxRetries) {
|
|
50
|
+
const reason = index >= maxRetries ? "last retry still returned natural-language content" : "two consecutive retries returned natural-language content";
|
|
51
|
+
throw new SuspectedModelRefusalError(index + 1, maxRetries, {
|
|
52
|
+
response,
|
|
53
|
+
reason
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
currentMessages = buildRetryMessages(
|
|
57
|
+
initialMessages,
|
|
58
|
+
response,
|
|
59
|
+
buildNaturalLanguageMessage(),
|
|
60
|
+
false
|
|
61
|
+
);
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
consecutiveProtocolDerailments = 0;
|
|
65
|
+
consecutiveMalformedJsonFailures += 1;
|
|
66
|
+
currentMessages = buildRetryMessages(
|
|
67
|
+
initialMessages,
|
|
68
|
+
response,
|
|
69
|
+
intent === "malformed_json" ? buildMalformedJsonMessage(asSyntaxError(error)) : buildSyntaxErrorMessage(asSyntaxError(error)),
|
|
70
|
+
consecutiveMalformedJsonFailures < MAX_MALFORMED_JSON_REPAIR_HISTORY_ATTEMPTS
|
|
71
|
+
);
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
consecutiveProtocolDerailments = 0;
|
|
75
|
+
consecutiveMalformedJsonFailures = 0;
|
|
76
|
+
const validation = await options.schema.safeParseAsync(parsedData);
|
|
77
|
+
if (!validation.success) {
|
|
78
|
+
if (shouldTreatSchemaFailureAsNaturalLanguage(validation.error, parsedData)) {
|
|
79
|
+
const intent = await classifyResponseIntent(options, response, index);
|
|
80
|
+
if (intent === "natural_language") {
|
|
81
|
+
consecutiveProtocolDerailments += 1;
|
|
82
|
+
consecutiveMalformedJsonFailures = 0;
|
|
83
|
+
if (consecutiveProtocolDerailments >= 2 || index >= maxRetries) {
|
|
84
|
+
const reason = index >= maxRetries ? "last retry still returned natural-language content" : "two consecutive retries returned natural-language content";
|
|
85
|
+
throw new SuspectedModelRefusalError(index + 1, maxRetries, {
|
|
86
|
+
response,
|
|
87
|
+
reason
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
currentMessages = buildRetryMessages(
|
|
91
|
+
initialMessages,
|
|
92
|
+
response,
|
|
93
|
+
buildNaturalLanguageMessage(),
|
|
94
|
+
false
|
|
95
|
+
);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
consecutiveProtocolDerailments = 0;
|
|
100
|
+
const feedback = buildSchemaErrorMessage(validation.error);
|
|
101
|
+
if (index >= maxRetries) {
|
|
102
|
+
throw new GuaranteedSchemaValidationError(
|
|
103
|
+
index + 1,
|
|
104
|
+
maxRetries,
|
|
105
|
+
{
|
|
106
|
+
issues: listSchemaIssues(validation.error),
|
|
107
|
+
response
|
|
108
|
+
},
|
|
109
|
+
validation.error
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
currentMessages = buildRetryMessages(
|
|
113
|
+
initialMessages,
|
|
114
|
+
response,
|
|
115
|
+
feedback,
|
|
116
|
+
true
|
|
117
|
+
);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
try {
|
|
121
|
+
return await options.parse(validation.data, index, maxRetries);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
if (!(error instanceof ParsedJsonError)) {
|
|
124
|
+
throw error;
|
|
125
|
+
}
|
|
126
|
+
const feedback = buildBusinessErrorMessage(error.issues);
|
|
127
|
+
if (index >= maxRetries) {
|
|
128
|
+
throw new GuaranteedParseValidationError(
|
|
129
|
+
index + 1,
|
|
130
|
+
maxRetries,
|
|
131
|
+
{
|
|
132
|
+
issues: error.issues,
|
|
133
|
+
response
|
|
134
|
+
},
|
|
135
|
+
error
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
currentMessages = buildRetryMessages(
|
|
139
|
+
initialMessages,
|
|
140
|
+
response,
|
|
141
|
+
feedback,
|
|
142
|
+
true
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
throw new Error("requestGuaranteedJson failed unexpectedly");
|
|
147
|
+
}
|
|
148
|
+
function buildRetryMessages(initialMessages, response, feedback, includeAssistantResponse) {
|
|
149
|
+
return [
|
|
150
|
+
...initialMessages,
|
|
151
|
+
...includeAssistantResponse ? [
|
|
152
|
+
{
|
|
153
|
+
role: "assistant",
|
|
154
|
+
content: response
|
|
155
|
+
}
|
|
156
|
+
] : [],
|
|
157
|
+
{
|
|
158
|
+
role: "user",
|
|
159
|
+
content: feedback
|
|
160
|
+
}
|
|
161
|
+
];
|
|
162
|
+
}
|
|
163
|
+
function asSyntaxError(error) {
|
|
164
|
+
if (error instanceof SyntaxError) {
|
|
165
|
+
return error;
|
|
166
|
+
}
|
|
167
|
+
return new SyntaxError(String(error));
|
|
168
|
+
}
|
|
169
|
+
async function classifyResponseIntent(options, response, index) {
|
|
170
|
+
const localIntent = classifyResponseIntentLocally(response);
|
|
171
|
+
if (localIntent !== "ambiguous") {
|
|
172
|
+
return localIntent;
|
|
173
|
+
}
|
|
174
|
+
try {
|
|
175
|
+
const classifierResponse = await options.request(
|
|
176
|
+
buildResponseIntentClassificationMessages(
|
|
177
|
+
options.responseIntentClassifierPrompt,
|
|
178
|
+
response
|
|
179
|
+
),
|
|
180
|
+
index,
|
|
181
|
+
options.maxRetries ?? DEFAULT_MAX_RETRIES
|
|
182
|
+
);
|
|
183
|
+
if (classifierResponse === void 0 || classifierResponse.trim() === "") {
|
|
184
|
+
return "natural_language";
|
|
185
|
+
}
|
|
186
|
+
const classifierIntent = parseResponseIntentClassification(classifierResponse);
|
|
187
|
+
return classifierIntent === "ambiguous" ? "natural_language" : classifierIntent;
|
|
188
|
+
} catch {
|
|
189
|
+
return "natural_language";
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
function shouldTreatSchemaFailureAsNaturalLanguage(error, parsedData) {
|
|
193
|
+
if (!isPrimitiveJsonValue(parsedData)) {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
return error.issues.some(
|
|
197
|
+
(issue) => issue.code === "invalid_type" && issue.path.length === 0 && (issue.expected === "object" || issue.expected === "array")
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
function isPrimitiveJsonValue(value) {
|
|
201
|
+
return value === null || typeof value !== "object" && typeof value !== "function";
|
|
202
|
+
}
|
|
203
|
+
export {
|
|
204
|
+
requestGuaranteedJson
|
|
205
|
+
};
|
|
206
|
+
//# sourceMappingURL=request.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/guaranteed/request.ts"],"sourcesContent":["import type { LLMessage } from \"../llm/index.js\";\nimport {\n buildResponseIntentClassificationMessages,\n classifyResponseIntentLocally,\n parseResponseIntentClassification,\n type GuaranteedResponseIntent,\n} from \"./classifier.js\";\nimport {\n GuaranteedEmptyResponseError,\n GuaranteedParseValidationError,\n GuaranteedSchemaValidationError,\n ParsedJsonError,\n SuspectedModelRefusalError,\n} from \"./errors.js\";\nimport {\n buildMalformedJsonMessage,\n buildNaturalLanguageMessage,\n buildBusinessErrorMessage,\n buildSchemaErrorMessage,\n buildSyntaxErrorMessage,\n extractJsonText,\n listSchemaIssues,\n repairJsonText,\n} from \"./response.js\";\nimport type { GuaranteedRequestOptions } from \"./types.js\";\n\nconst DEFAULT_MAX_RETRIES = 12;\nconst MAX_MALFORMED_JSON_REPAIR_HISTORY_ATTEMPTS = 3;\n\nexport async function requestGuaranteedJson<TData, TResult>(\n options: GuaranteedRequestOptions<TData, TResult>,\n): Promise<TResult> {\n const initialMessages = [...options.messages];\n let currentMessages = [...options.messages];\n let consecutiveProtocolDerailments = 0;\n let consecutiveMalformedJsonFailures = 0;\n const maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;\n\n for (let index = 0; index <= maxRetries; index += 1) {\n const response = await options.request(currentMessages, index, maxRetries);\n if (response === undefined || response.trim() === \"\") {\n if (index >= maxRetries) {\n throw new GuaranteedEmptyResponseError(index + 1, maxRetries);\n }\n continue;\n }\n let parsedData: unknown;\n\n try {\n const extractedJsonText = extractJsonText(response);\n const repairedJsonText = repairJsonText(extractedJsonText);\n\n parsedData = JSON.parse(repairedJsonText);\n } catch (error) {\n const intent = await classifyResponseIntent(options, response, index);\n\n if (intent === \"natural_language\") {\n consecutiveProtocolDerailments += 1;\n consecutiveMalformedJsonFailures = 0;\n\n if (consecutiveProtocolDerailments >= 2 || index >= maxRetries) {\n const reason =\n index >= maxRetries\n ? \"last retry still returned natural-language content\"\n : \"two consecutive retries returned natural-language content\";\n throw new SuspectedModelRefusalError(index + 1, maxRetries, {\n response,\n reason,\n });\n }\n\n currentMessages = buildRetryMessages(\n initialMessages,\n response,\n buildNaturalLanguageMessage(),\n false,\n );\n continue;\n }\n\n consecutiveProtocolDerailments = 0;\n consecutiveMalformedJsonFailures += 1;\n currentMessages = buildRetryMessages(\n initialMessages,\n response,\n intent === \"malformed_json\"\n ? buildMalformedJsonMessage(asSyntaxError(error))\n : buildSyntaxErrorMessage(asSyntaxError(error)),\n consecutiveMalformedJsonFailures <\n MAX_MALFORMED_JSON_REPAIR_HISTORY_ATTEMPTS,\n );\n continue;\n }\n consecutiveProtocolDerailments = 0;\n consecutiveMalformedJsonFailures = 0;\n\n const validation = await options.schema.safeParseAsync(parsedData);\n\n if (!validation.success) {\n if (\n shouldTreatSchemaFailureAsNaturalLanguage(validation.error, parsedData)\n ) {\n const intent = await classifyResponseIntent(options, response, index);\n\n if (intent === \"natural_language\") {\n consecutiveProtocolDerailments += 1;\n consecutiveMalformedJsonFailures = 0;\n\n if (consecutiveProtocolDerailments >= 2 || index >= maxRetries) {\n const reason =\n index >= maxRetries\n ? \"last retry still returned natural-language content\"\n : \"two consecutive retries returned natural-language content\";\n throw new SuspectedModelRefusalError(index + 1, maxRetries, {\n response,\n reason,\n });\n }\n\n currentMessages = buildRetryMessages(\n initialMessages,\n response,\n buildNaturalLanguageMessage(),\n false,\n );\n continue;\n }\n }\n\n consecutiveProtocolDerailments = 0;\n const feedback = buildSchemaErrorMessage(validation.error);\n\n if (index >= maxRetries) {\n throw new GuaranteedSchemaValidationError(\n index + 1,\n maxRetries,\n {\n issues: listSchemaIssues(validation.error),\n response,\n },\n validation.error,\n );\n }\n currentMessages = buildRetryMessages(\n initialMessages,\n response,\n feedback,\n true,\n );\n continue;\n }\n\n try {\n return await options.parse(validation.data, index, maxRetries);\n } catch (error) {\n if (!(error instanceof ParsedJsonError)) {\n throw error;\n }\n const feedback = buildBusinessErrorMessage(error.issues);\n\n if (index >= maxRetries) {\n throw new GuaranteedParseValidationError(\n index + 1,\n maxRetries,\n {\n issues: error.issues,\n response,\n },\n error,\n );\n }\n currentMessages = buildRetryMessages(\n initialMessages,\n response,\n feedback,\n true,\n );\n }\n }\n throw new Error(\"requestGuaranteedJson failed unexpectedly\");\n}\n\nfunction buildRetryMessages(\n initialMessages: readonly LLMessage[],\n response: string,\n feedback: string,\n includeAssistantResponse: boolean,\n): LLMessage[] {\n return [\n ...initialMessages,\n ...(includeAssistantResponse\n ? [\n {\n role: \"assistant\" as const,\n content: response,\n },\n ]\n : []),\n {\n role: \"user\",\n content: feedback,\n },\n ];\n}\n\nfunction asSyntaxError(error: unknown): SyntaxError {\n if (error instanceof SyntaxError) {\n return error;\n }\n\n return new SyntaxError(String(error));\n}\n\nasync function classifyResponseIntent<TData, TResult>(\n options: GuaranteedRequestOptions<TData, TResult>,\n response: string,\n index: number,\n): Promise<GuaranteedResponseIntent> {\n const localIntent = classifyResponseIntentLocally(response);\n\n if (localIntent !== \"ambiguous\") {\n return localIntent;\n }\n\n try {\n const classifierResponse = await options.request(\n buildResponseIntentClassificationMessages(\n options.responseIntentClassifierPrompt,\n response,\n ),\n index,\n options.maxRetries ?? DEFAULT_MAX_RETRIES,\n );\n\n if (classifierResponse === undefined || classifierResponse.trim() === \"\") {\n return \"natural_language\";\n }\n\n const classifierIntent =\n parseResponseIntentClassification(classifierResponse);\n\n return classifierIntent === \"ambiguous\"\n ? \"natural_language\"\n : classifierIntent;\n } catch {\n return \"natural_language\";\n }\n}\n\nfunction shouldTreatSchemaFailureAsNaturalLanguage(\n error: {\n issues: readonly {\n code?: string;\n path: readonly PropertyKey[];\n expected?: unknown;\n }[];\n },\n parsedData: unknown,\n): boolean {\n if (!isPrimitiveJsonValue(parsedData)) {\n return false;\n }\n\n return error.issues.some(\n (issue) =>\n issue.code === \"invalid_type\" &&\n issue.path.length === 0 &&\n (issue.expected === \"object\" || issue.expected === \"array\"),\n );\n}\n\nfunction isPrimitiveJsonValue(value: unknown): boolean {\n return (\n value === null || (typeof value !== \"object\" && typeof value !== \"function\")\n );\n}\n"],"mappings":"AACA;AAAA,EACE;AAAA,EACA;AAAA,EACA;A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AS,kBACf,MAAM,KAAK,WAAW,MACrB,MAAM,aAAa,YAAY,MAAM,aAAa;AAAA,EACvD;AACF;AAEA,SAAS,qBAAqB,OAAyB;AACrD,SACE,UAAU,QAAS,OAAO,UAAU,YAAY,OAAO,UAAU;AAErE;","names":[]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ZodError } from 'zod';
|
|
2
|
+
|
|
3
|
+
declare function extractJsonText(response: string): string;
|
|
4
|
+
declare function repairJsonText(jsonText: string): string;
|
|
5
|
+
declare function buildSyntaxErrorMessage(error: SyntaxError): string;
|
|
6
|
+
declare function buildMalformedJsonMessage(error: SyntaxError): string;
|
|
7
|
+
declare function buildNaturalLanguageMessage(): string;
|
|
8
|
+
declare function buildSchemaErrorMessage(error: ZodError): string;
|
|
9
|
+
declare function listSchemaIssues(error: ZodError): readonly string[];
|
|
10
|
+
declare function buildBusinessErrorMessage(issues: readonly string[]): string;
|
|
11
|
+
|
|
12
|
+
export { buildBusinessErrorMessage, buildMalformedJsonMessage, buildNaturalLanguageMessage, buildSchemaErrorMessage, buildSyntaxErrorMessage, extractJsonText, listSchemaIssues, repairJsonText };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { jsonrepair } from "jsonrepair";
|
|
2
|
+
function extractJsonText(response) {
|
|
3
|
+
const withoutCodeFence = response.replace(
|
|
4
|
+
/```(?:json)?\s*\n(.*?)\n```/gs,
|
|
5
|
+
"$1"
|
|
6
|
+
);
|
|
7
|
+
const matchedArray = withoutCodeFence.match(/\[[\s\S]*\]/);
|
|
8
|
+
const matchedObject = withoutCodeFence.match(/\{[\s\S]*\}/);
|
|
9
|
+
if (matchedArray?.index !== void 0 && matchedArray[0] !== void 0 && (matchedObject?.index === void 0 || matchedArray.index < matchedObject.index)) {
|
|
10
|
+
return matchedArray[0];
|
|
11
|
+
}
|
|
12
|
+
if (matchedObject?.[0] !== void 0) {
|
|
13
|
+
return matchedObject[0];
|
|
14
|
+
}
|
|
15
|
+
return withoutCodeFence.trim();
|
|
16
|
+
}
|
|
17
|
+
function repairJsonText(jsonText) {
|
|
18
|
+
try {
|
|
19
|
+
return jsonrepair(jsonText);
|
|
20
|
+
} catch {
|
|
21
|
+
return jsonText;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function buildSyntaxErrorMessage(error) {
|
|
25
|
+
const lines = [
|
|
26
|
+
"Your JSON has a syntax error.",
|
|
27
|
+
`Error: ${error.message}`,
|
|
28
|
+
"",
|
|
29
|
+
"Common issues:",
|
|
30
|
+
"- Trailing commas in arrays or objects",
|
|
31
|
+
'- Unescaped quotes in strings, for example: "He said \\"hi\\""',
|
|
32
|
+
'- Single quotes instead of double quotes, for example: {"key": "value"}',
|
|
33
|
+
"- Missing commas between items",
|
|
34
|
+
"",
|
|
35
|
+
"Regenerate the complete and valid JSON directly. Do not add explanations or markdown fences."
|
|
36
|
+
];
|
|
37
|
+
return lines.join("\n");
|
|
38
|
+
}
|
|
39
|
+
function buildMalformedJsonMessage(error) {
|
|
40
|
+
const lines = [
|
|
41
|
+
"Your previous reply looked like malformed JSON.",
|
|
42
|
+
`Error: ${error.message}`,
|
|
43
|
+
"",
|
|
44
|
+
"Return complete and valid JSON directly.",
|
|
45
|
+
"Do not answer conversationally.",
|
|
46
|
+
"Do not add explanations or markdown fences."
|
|
47
|
+
];
|
|
48
|
+
return lines.join("\n");
|
|
49
|
+
}
|
|
50
|
+
function buildNaturalLanguageMessage() {
|
|
51
|
+
const lines = [
|
|
52
|
+
"Your previous reply was plain natural language, not a JSON object or array.",
|
|
53
|
+
"",
|
|
54
|
+
"Do not apologize or explain.",
|
|
55
|
+
"Do not answer conversationally.",
|
|
56
|
+
"Return complete and valid JSON directly."
|
|
57
|
+
];
|
|
58
|
+
return lines.join("\n");
|
|
59
|
+
}
|
|
60
|
+
function buildSchemaErrorMessage(error) {
|
|
61
|
+
const issues = error.issues.map(formatZodIssue);
|
|
62
|
+
const lines = [
|
|
63
|
+
"Your JSON has structural issues.",
|
|
64
|
+
"",
|
|
65
|
+
...issues,
|
|
66
|
+
"",
|
|
67
|
+
"Regenerate the complete and valid JSON directly. Do not add explanations."
|
|
68
|
+
];
|
|
69
|
+
return lines.join("\n");
|
|
70
|
+
}
|
|
71
|
+
function listSchemaIssues(error) {
|
|
72
|
+
return error.issues.map(formatZodIssue);
|
|
73
|
+
}
|
|
74
|
+
function buildBusinessErrorMessage(issues) {
|
|
75
|
+
const lines = [
|
|
76
|
+
"Your JSON has the following issues.",
|
|
77
|
+
"",
|
|
78
|
+
...issues.map((issue) => `- ${issue}`),
|
|
79
|
+
"",
|
|
80
|
+
"Regenerate the complete and valid JSON directly. Do not add explanations."
|
|
81
|
+
];
|
|
82
|
+
return lines.join("\n");
|
|
83
|
+
}
|
|
84
|
+
function formatZodIssue(issue) {
|
|
85
|
+
const location = issue.path.length === 0 ? "<root>" : issue.path.join(".");
|
|
86
|
+
return `- **${location}**: ${issue.message}`;
|
|
87
|
+
}
|
|
88
|
+
export {
|
|
89
|
+
buildBusinessErrorMessage,
|
|
90
|
+
buildMalformedJsonMessage,
|
|
91
|
+
buildNaturalLanguageMessage,
|
|
92
|
+
buildSchemaErrorMessage,
|
|
93
|
+
buildSyntaxErrorMessage,
|
|
94
|
+
extractJsonText,
|
|
95
|
+
listSchemaIssues,
|
|
96
|
+
repairJsonText
|
|
97
|
+
};
|
|
98
|
+
//# sourceMappingURL=response.js.map
|