@nhtio/adk 1.20260625.1 → 1.20260626.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +67 -0
- package/batteries/embeddings/openai/adapter.cjs +1 -1
- package/batteries/embeddings/openai/adapter.mjs +1 -1
- package/batteries/embeddings/transformers_js/adapter.cjs +65 -18
- package/batteries/embeddings/transformers_js/adapter.cjs.map +1 -1
- package/batteries/embeddings/transformers_js/adapter.d.ts +11 -0
- package/batteries/embeddings/transformers_js/adapter.mjs +65 -18
- package/batteries/embeddings/transformers_js/adapter.mjs.map +1 -1
- package/batteries/embeddings/transformers_js/index.d.ts +3 -1
- package/batteries/embeddings/transformers_js/pooling.cjs +67 -0
- package/batteries/embeddings/transformers_js/pooling.cjs.map +1 -0
- package/batteries/embeddings/transformers_js/pooling.d.ts +41 -0
- package/batteries/embeddings/transformers_js/pooling.mjs +63 -0
- package/batteries/embeddings/transformers_js/pooling.mjs.map +1 -0
- package/batteries/embeddings/transformers_js/types.d.ts +34 -1
- package/batteries/embeddings/transformers_js/validation.cjs +11 -2
- package/batteries/embeddings/transformers_js/validation.cjs.map +1 -1
- package/batteries/embeddings/transformers_js/validation.mjs +11 -2
- package/batteries/embeddings/transformers_js/validation.mjs.map +1 -1
- package/batteries/embeddings/transformers_js.cjs +4 -0
- package/batteries/embeddings/transformers_js.mjs +2 -1
- package/batteries/embeddings/webllm/adapter.cjs +1 -1
- package/batteries/embeddings/webllm/adapter.mjs +1 -1
- package/batteries/encoding.cjs +7 -7
- package/batteries/encoding.mjs +7 -7
- package/batteries/llm/chat_common/generation.d.ts +122 -0
- package/batteries/llm/chat_common/helpers.d.ts +68 -0
- package/batteries/llm/chat_common/index.d.ts +2 -0
- package/batteries/llm/chat_common/lifecycle.d.ts +85 -0
- package/batteries/llm/chat_common/reasoning_parsers.d.ts +36 -7
- package/batteries/llm/chat_common/tool_parsers.d.ts +60 -11
- package/batteries/llm/chat_common/types.d.ts +36 -1
- package/batteries/llm/litert_lm/adapter.cjs +160 -32
- package/batteries/llm/litert_lm/adapter.cjs.map +1 -1
- package/batteries/llm/litert_lm/adapter.d.ts +11 -0
- package/batteries/llm/litert_lm/adapter.mjs +161 -33
- package/batteries/llm/litert_lm/adapter.mjs.map +1 -1
- package/batteries/llm/litert_lm/helpers.cjs +89 -12
- package/batteries/llm/litert_lm/helpers.cjs.map +1 -1
- package/batteries/llm/litert_lm/helpers.d.ts +61 -0
- package/batteries/llm/litert_lm/helpers.mjs +77 -13
- package/batteries/llm/litert_lm/helpers.mjs.map +1 -1
- package/batteries/llm/litert_lm/index.d.ts +4 -2
- package/batteries/llm/litert_lm/types.d.ts +82 -7
- package/batteries/llm/litert_lm/validation.cjs +34 -9
- package/batteries/llm/litert_lm/validation.cjs.map +1 -1
- package/batteries/llm/litert_lm/validation.mjs +34 -9
- package/batteries/llm/litert_lm/validation.mjs.map +1 -1
- package/batteries/llm/litert_lm.cjs +15 -2
- package/batteries/llm/litert_lm.mjs +5 -4
- package/batteries/llm/ollama/adapter.cjs +6 -6
- package/batteries/llm/ollama/adapter.mjs +6 -6
- package/batteries/llm/ollama/helpers.cjs +7 -7
- package/batteries/llm/ollama/helpers.cjs.map +1 -1
- package/batteries/llm/ollama/helpers.mjs +7 -7
- package/batteries/llm/ollama/helpers.mjs.map +1 -1
- package/batteries/llm/ollama/validation.cjs +1 -1
- package/batteries/llm/ollama/validation.mjs +1 -1
- package/batteries/llm/ollama.cjs +1 -1
- package/batteries/llm/ollama.mjs +1 -1
- package/batteries/llm/openai_chat_completions/adapter.cjs +6 -6
- package/batteries/llm/openai_chat_completions/adapter.mjs +6 -6
- package/batteries/llm/openai_chat_completions/helpers.cjs +16 -10
- package/batteries/llm/openai_chat_completions/helpers.cjs.map +1 -1
- package/batteries/llm/openai_chat_completions/helpers.d.ts +1 -1
- package/batteries/llm/openai_chat_completions/helpers.mjs +12 -11
- package/batteries/llm/openai_chat_completions/helpers.mjs.map +1 -1
- package/batteries/llm/openai_chat_completions/index.d.ts +1 -1
- package/batteries/llm/openai_chat_completions/validation.cjs +1 -1
- package/batteries/llm/openai_chat_completions/validation.mjs +1 -1
- package/batteries/llm/openai_chat_completions.cjs +6 -1
- package/batteries/llm/openai_chat_completions.mjs +2 -2
- package/batteries/llm/transformers_js/adapter.cjs +266 -48
- package/batteries/llm/transformers_js/adapter.cjs.map +1 -1
- package/batteries/llm/transformers_js/adapter.d.ts +17 -2
- package/batteries/llm/transformers_js/adapter.mjs +267 -49
- package/batteries/llm/transformers_js/adapter.mjs.map +1 -1
- package/batteries/llm/transformers_js/helpers.cjs +175 -8
- package/batteries/llm/transformers_js/helpers.cjs.map +1 -1
- package/batteries/llm/transformers_js/helpers.d.ts +59 -0
- package/batteries/llm/transformers_js/helpers.mjs +163 -9
- package/batteries/llm/transformers_js/helpers.mjs.map +1 -1
- package/batteries/llm/transformers_js/index.d.ts +7 -3
- package/batteries/llm/transformers_js/model_source.cjs +144 -0
- package/batteries/llm/transformers_js/model_source.cjs.map +1 -0
- package/batteries/llm/transformers_js/model_source.d.ts +95 -0
- package/batteries/llm/transformers_js/model_source.mjs +139 -0
- package/batteries/llm/transformers_js/model_source.mjs.map +1 -0
- package/batteries/llm/transformers_js/types.d.ts +109 -17
- package/batteries/llm/transformers_js/validation.cjs +31 -11
- package/batteries/llm/transformers_js/validation.cjs.map +1 -1
- package/batteries/llm/transformers_js/validation.mjs +31 -11
- package/batteries/llm/transformers_js/validation.mjs.map +1 -1
- package/batteries/llm/transformers_js.cjs +21 -2
- package/batteries/llm/transformers_js.mjs +6 -4
- package/batteries/llm/webllm_chat_completions/adapter.cjs +56 -22
- package/batteries/llm/webllm_chat_completions/adapter.cjs.map +1 -1
- package/batteries/llm/webllm_chat_completions/adapter.mjs +56 -22
- package/batteries/llm/webllm_chat_completions/adapter.mjs.map +1 -1
- package/batteries/llm/webllm_chat_completions/helpers.cjs +1 -1
- package/batteries/llm/webllm_chat_completions/helpers.mjs +1 -1
- package/batteries/llm/webllm_chat_completions/index.d.ts +1 -1
- package/batteries/llm/webllm_chat_completions/types.d.ts +11 -1
- package/batteries/llm/webllm_chat_completions/validation.cjs +14 -6
- package/batteries/llm/webllm_chat_completions/validation.cjs.map +1 -1
- package/batteries/llm/webllm_chat_completions/validation.mjs +14 -6
- package/batteries/llm/webllm_chat_completions/validation.mjs.map +1 -1
- package/batteries/llm/webllm_chat_completions.cjs +1 -1
- package/batteries/llm/webllm_chat_completions.mjs +1 -1
- package/batteries/llm.cjs +6 -1
- package/batteries/llm.mjs +2 -2
- package/batteries/media/contracts.cjs +1 -1
- package/batteries/media/contracts.mjs +1 -1
- package/batteries/media/engines/audio_decode.cjs +1 -1
- package/batteries/media/engines/audio_decode.mjs +1 -1
- package/batteries/media/engines/data.cjs +1 -1
- package/batteries/media/engines/data.mjs +1 -1
- package/batteries/media/engines/exceljs.cjs +1 -1
- package/batteries/media/engines/exceljs.mjs +1 -1
- package/batteries/media/engines/execa_executor.cjs +1 -1
- package/batteries/media/engines/execa_executor.mjs +1 -1
- package/batteries/media/engines/fs_workspace.cjs +1 -1
- package/batteries/media/engines/fs_workspace.mjs +1 -1
- package/batteries/media/engines/jimp.cjs +1 -1
- package/batteries/media/engines/jimp.mjs +1 -1
- package/batteries/media/engines/sharp.cjs +1 -1
- package/batteries/media/engines/sharp.mjs +1 -1
- package/batteries/media/engines/sheetjs.cjs +1 -1
- package/batteries/media/engines/sheetjs.mjs +1 -1
- package/batteries/media/engines/tesseract_js.cjs +1 -1
- package/batteries/media/engines/tesseract_js.mjs +1 -1
- package/batteries/media/engines/transformers_asr.cjs +1 -1
- package/batteries/media/engines/transformers_asr.mjs +1 -1
- package/batteries/media/forge.cjs +6 -6
- package/batteries/media/forge.mjs +6 -6
- package/batteries/media/lint.cjs +1 -1
- package/batteries/media/lint.mjs +1 -1
- package/batteries/media.cjs +2 -2
- package/batteries/media.mjs +2 -2
- package/batteries/storage/flydrive.cjs +1 -1
- package/batteries/storage/flydrive.mjs +1 -1
- package/batteries/storage/in_memory.cjs +1 -1
- package/batteries/storage/in_memory.mjs +1 -1
- package/batteries/storage/opfs.cjs +1 -1
- package/batteries/storage/opfs.mjs +1 -1
- package/batteries/tools/_shared.cjs +2 -2
- package/batteries/tools/_shared.mjs +2 -2
- package/batteries/tools/color.cjs +2 -2
- package/batteries/tools/color.mjs +2 -2
- package/batteries/tools/comparison.cjs +3 -3
- package/batteries/tools/comparison.mjs +3 -3
- package/batteries/tools/data_structure.cjs +3 -3
- package/batteries/tools/data_structure.mjs +3 -3
- package/batteries/tools/datetime_extended.cjs +3 -3
- package/batteries/tools/datetime_extended.mjs +3 -3
- package/batteries/tools/datetime_math.cjs +3 -3
- package/batteries/tools/datetime_math.mjs +3 -3
- package/batteries/tools/encoding.cjs +3 -3
- package/batteries/tools/encoding.mjs +3 -3
- package/batteries/tools/formatting.cjs +3 -3
- package/batteries/tools/formatting.mjs +3 -3
- package/batteries/tools/geo_basics.cjs +2 -2
- package/batteries/tools/geo_basics.mjs +2 -2
- package/batteries/tools/math.cjs +3 -3
- package/batteries/tools/math.mjs +3 -3
- package/batteries/tools/memory.cjs +6 -6
- package/batteries/tools/memory.mjs +6 -6
- package/batteries/tools/parsing.cjs +4 -4
- package/batteries/tools/parsing.mjs +4 -4
- package/batteries/tools/retrievables.cjs +5 -5
- package/batteries/tools/retrievables.mjs +5 -5
- package/batteries/tools/scrapper.cjs +1 -1
- package/batteries/tools/scrapper.mjs +1 -1
- package/batteries/tools/searxng.cjs +1 -1
- package/batteries/tools/searxng.mjs +1 -1
- package/batteries/tools/standing_instructions.cjs +4 -4
- package/batteries/tools/standing_instructions.mjs +4 -4
- package/batteries/tools/statistics.cjs +4 -4
- package/batteries/tools/statistics.mjs +4 -4
- package/batteries/tools/string_processing.cjs +3 -3
- package/batteries/tools/string_processing.mjs +3 -3
- package/batteries/tools/structured_data.cjs +3 -3
- package/batteries/tools/structured_data.mjs +3 -3
- package/batteries/tools/text_analysis.cjs +3 -3
- package/batteries/tools/text_analysis.mjs +3 -3
- package/batteries/tools/text_comparison.cjs +2 -2
- package/batteries/tools/text_comparison.mjs +2 -2
- package/batteries/tools/time.cjs +3 -3
- package/batteries/tools/time.mjs +3 -3
- package/batteries/tools/unit_conversion.cjs +2 -2
- package/batteries/tools/unit_conversion.mjs +2 -2
- package/batteries/tools/web_retrieval.cjs +1 -1
- package/batteries/tools/web_retrieval.mjs +1 -1
- package/batteries/tools.cjs +2 -2
- package/batteries/tools.mjs +2 -2
- package/batteries/vector/arangodb.cjs +1 -1
- package/batteries/vector/arangodb.mjs +1 -1
- package/batteries/vector/clickhouse.cjs +1 -1
- package/batteries/vector/clickhouse.mjs +1 -1
- package/batteries/vector/cloudflare.cjs +1 -1
- package/batteries/vector/cloudflare.mjs +1 -1
- package/batteries/vector/couchbase.cjs +1 -1
- package/batteries/vector/couchbase.mjs +1 -1
- package/batteries/vector/duckdb.cjs +1 -1
- package/batteries/vector/duckdb.mjs +1 -1
- package/batteries/vector/elasticsearch.cjs +1 -1
- package/batteries/vector/elasticsearch.mjs +1 -1
- package/batteries/vector/filters.cjs +1 -1
- package/batteries/vector/filters.mjs +1 -1
- package/batteries/vector/hnswlib.cjs +1 -1
- package/batteries/vector/hnswlib.mjs +1 -1
- package/batteries/vector/lancedb.cjs +1 -1
- package/batteries/vector/lancedb.mjs +1 -1
- package/batteries/vector/mariadb.cjs +1 -1
- package/batteries/vector/mariadb.mjs +1 -1
- package/batteries/vector/meilisearch.cjs +1 -1
- package/batteries/vector/meilisearch.mjs +1 -1
- package/batteries/vector/migrate.cjs +1 -1
- package/batteries/vector/migrate.mjs +1 -1
- package/batteries/vector/mongodb.cjs +1 -1
- package/batteries/vector/mongodb.mjs +1 -1
- package/batteries/vector/neo4j.cjs +1 -1
- package/batteries/vector/neo4j.mjs +1 -1
- package/batteries/vector/opensearch.cjs +1 -1
- package/batteries/vector/opensearch.mjs +1 -1
- package/batteries/vector/oracle23ai.cjs +1 -1
- package/batteries/vector/oracle23ai.mjs +1 -1
- package/batteries/vector/pinecone.cjs +1 -1
- package/batteries/vector/pinecone.mjs +1 -1
- package/batteries/vector/redis.cjs +1 -1
- package/batteries/vector/redis.mjs +1 -1
- package/batteries/vector/retrievable.cjs +1 -1
- package/batteries/vector/retrievable.mjs +1 -1
- package/batteries/vector/s3vectors.cjs +1 -1
- package/batteries/vector/s3vectors.mjs +1 -1
- package/batteries/vector/solr.cjs +1 -1
- package/batteries/vector/solr.mjs +1 -1
- package/batteries/vector/surrealdb.cjs +1 -1
- package/batteries/vector/surrealdb.mjs +1 -1
- package/batteries/vector/typesense.cjs +1 -1
- package/batteries/vector/typesense.mjs +1 -1
- package/batteries/vector/vespa.cjs +1 -1
- package/batteries/vector/vespa.mjs +1 -1
- package/batteries/vector/weaviate.cjs +1 -1
- package/batteries/vector/weaviate.mjs +1 -1
- package/batteries.cjs +8 -3
- package/batteries.mjs +4 -4
- package/{common-0CmrSKNb.mjs → common-DJH3UemH.mjs} +7 -7
- package/{common-0CmrSKNb.mjs.map → common-DJH3UemH.mjs.map} +1 -1
- package/{common-CpcsEqPY.js → common-ax3vKu6O.js} +7 -7
- package/{common-CpcsEqPY.js.map → common-ax3vKu6O.js.map} +1 -1
- package/common.cjs +7 -7
- package/common.mjs +7 -7
- package/{dispatch_runner-C2ZpFfw4.mjs → dispatch_runner-3anlRk3F.mjs} +3 -3
- package/{dispatch_runner-C2ZpFfw4.mjs.map → dispatch_runner-3anlRk3F.mjs.map} +1 -1
- package/{dispatch_runner-DMTkGHQI.js → dispatch_runner-CTkL_cMm.js} +3 -3
- package/{dispatch_runner-DMTkGHQI.js.map → dispatch_runner-CTkL_cMm.js.map} +1 -1
- package/dispatch_runner.cjs +1 -1
- package/dispatch_runner.mjs +1 -1
- package/eslint/rules.cjs +1 -1
- package/eslint/rules.mjs +1 -1
- package/eslint.cjs +2 -2
- package/eslint.mjs +2 -2
- package/forge.cjs +4 -4
- package/forge.mjs +4 -4
- package/guards.cjs +8 -8
- package/guards.mjs +8 -8
- package/{helpers-1okZzNCa.js → helpers-CJcqikes.js} +131 -1
- package/helpers-CJcqikes.js.map +1 -0
- package/{helpers-BCyDAn_R.mjs → helpers-DlrRbLVn.mjs} +102 -2
- package/helpers-DlrRbLVn.mjs.map +1 -0
- package/index.cjs +11 -11
- package/index.mjs +11 -11
- package/lifecycle-C4kwYJ-N.mjs +68 -0
- package/lifecycle-C4kwYJ-N.mjs.map +1 -0
- package/lifecycle-vYQJ2rmf.js +79 -0
- package/lifecycle-vYQJ2rmf.js.map +1 -0
- package/mcp/adk-docs-corpus.json +1 -1
- package/package.json +249 -239
- package/reasoning_parsers-CFNoFf6x.js +1016 -0
- package/reasoning_parsers-CFNoFf6x.js.map +1 -0
- package/reasoning_parsers-Dp66LFBt.mjs +747 -0
- package/reasoning_parsers-Dp66LFBt.mjs.map +1 -0
- package/{scrapper-CxmS0VaD.mjs → scrapper-BrViMDPI.mjs} +5 -5
- package/{scrapper-CxmS0VaD.mjs.map → scrapper-BrViMDPI.mjs.map} +1 -1
- package/{scrapper-BUCxsVVi.js → scrapper-CzgdX1NN.js} +5 -5
- package/{scrapper-BUCxsVVi.js.map → scrapper-CzgdX1NN.js.map} +1 -1
- package/{searxng-CgxQluyF.js → searxng-B5I1KmQj.js} +5 -5
- package/{searxng-CgxQluyF.js.map → searxng-B5I1KmQj.js.map} +1 -1
- package/{searxng-Dgh-4RpS.mjs → searxng-BvjwwZav.mjs} +5 -5
- package/{searxng-Dgh-4RpS.mjs.map → searxng-BvjwwZav.mjs.map} +1 -1
- package/server.json +2 -2
- package/skills/adk-assembly/SKILL.md +2 -2
- package/{spooled_artifact-fgc1lutm.mjs → spooled_artifact-BnbqIZ3a.mjs} +3 -3
- package/{spooled_artifact-fgc1lutm.mjs.map → spooled_artifact-BnbqIZ3a.mjs.map} +1 -1
- package/{spooled_artifact-MbMz6NXX.js → spooled_artifact-DrIR90nV.js} +3 -3
- package/{spooled_artifact-MbMz6NXX.js.map → spooled_artifact-DrIR90nV.js.map} +1 -1
- package/spooled_artifact.cjs +2 -2
- package/spooled_artifact.mjs +2 -2
- package/{spooled_markdown_artifact-3sK6HFRw.mjs → spooled_markdown_artifact-BJH_JZFm.mjs} +3 -3
- package/{spooled_markdown_artifact-3sK6HFRw.mjs.map → spooled_markdown_artifact-BJH_JZFm.mjs.map} +1 -1
- package/{spooled_markdown_artifact-Gj0ruiLJ.js → spooled_markdown_artifact-rg_WCNWq.js} +3 -3
- package/{spooled_markdown_artifact-Gj0ruiLJ.js.map → spooled_markdown_artifact-rg_WCNWq.js.map} +1 -1
- package/{thought-CU7PYkB1.js → thought-CE88bddw.js} +3 -3
- package/{thought-CU7PYkB1.js.map → thought-CE88bddw.js.map} +1 -1
- package/{thought-DmcfCRAw.mjs → thought-DdiQCpgX.mjs} +3 -3
- package/{thought-DmcfCRAw.mjs.map → thought-DdiQCpgX.mjs.map} +1 -1
- package/{tool-Bt6tmFXT.js → tool-BngpaOWb.js} +3 -3
- package/{tool-Bt6tmFXT.js.map → tool-BngpaOWb.js.map} +1 -1
- package/{tool-C_uxdxjp.mjs → tool-asIfCjxb.mjs} +3 -3
- package/{tool-C_uxdxjp.mjs.map → tool-asIfCjxb.mjs.map} +1 -1
- package/{tool_call-BixyvnWZ.js → tool_call-CZVmISyc.js} +4 -4
- package/{tool_call-BixyvnWZ.js.map → tool_call-CZVmISyc.js.map} +1 -1
- package/{tool_call-pfvAsgkY.mjs → tool_call-TtzdXxcb.mjs} +4 -4
- package/{tool_call-pfvAsgkY.mjs.map → tool_call-TtzdXxcb.mjs.map} +1 -1
- package/{tool_registry-FeMMz993.mjs → tool_registry-Bj0JhXCv.mjs} +164 -164
- package/tool_registry-Bj0JhXCv.mjs.map +1 -0
- package/{tool_registry-CIIfbY_w.js → tool_registry-DbS8Pem9.js} +163 -163
- package/tool_registry-DbS8Pem9.js.map +1 -0
- package/{turn_runner-Brz6Sd0F.mjs → turn_runner-305dGa72.mjs} +5 -5
- package/{turn_runner-Brz6Sd0F.mjs.map → turn_runner-305dGa72.mjs.map} +1 -1
- package/{turn_runner-DB-WWls8.js → turn_runner-DqJBbqeU.js} +5 -5
- package/{turn_runner-DB-WWls8.js.map → turn_runner-DqJBbqeU.js.map} +1 -1
- package/turn_runner.cjs +1 -1
- package/turn_runner.mjs +1 -1
- package/{validate-MBEmBzMl.mjs → validate-B8IGAERs.mjs} +2 -2
- package/{validate-MBEmBzMl.mjs.map → validate-B8IGAERs.mjs.map} +1 -1
- package/{validate-B4RXgYJC.js → validate-Cq8xhZrZ.js} +2 -2
- package/{validate-B4RXgYJC.js.map → validate-Cq8xhZrZ.js.map} +1 -1
- package/helpers-1okZzNCa.js.map +0 -1
- package/helpers-BCyDAn_R.mjs.map +0 -1
- package/reasoning_parsers-B6W-1twu.mjs +0 -439
- package/reasoning_parsers-B6W-1twu.mjs.map +0 -1
- package/reasoning_parsers-CdjuWEfx.js +0 -654
- package/reasoning_parsers-CdjuWEfx.js.map +0 -1
- package/tool_registry-CIIfbY_w.js.map +0 -1
- package/tool_registry-FeMMz993.mjs.map +0 -1
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic, battery-owned pooling + L2-normalization for the transformers.js embeddings battery.
|
|
3
|
+
*
|
|
4
|
+
* @module @nhtio/adk/batteries/embeddings/transformers_js/pooling
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* Used when `poolingOwner: 'battery'`. The pipeline is asked for raw `pooling:'none'` token states —
|
|
8
|
+
* a `[batch, seq, hidden]` tensor — and this module pools them into one vector per input in pure JS,
|
|
9
|
+
* identically across Node and the browser. That removes the per-runtime post-processing variance that
|
|
10
|
+
* widens the node↔browser cosine gap; only the irreducible ONNX-Runtime kernel floor remains.
|
|
11
|
+
*
|
|
12
|
+
* Pooling strategies mirror the transformers.js feature-extraction options:
|
|
13
|
+
* - `mean` — attention-unaware mean over the sequence axis (matches the pipeline's `'mean'`, which
|
|
14
|
+
* also means over all positions for a single ungrouped sequence).
|
|
15
|
+
* - `cls` / `first_token` — the first token's hidden state.
|
|
16
|
+
* - `eos` / `last_token` — the last token's hidden state.
|
|
17
|
+
* - `none` — no pooling (caller already has the vector); returned unchanged per row.
|
|
18
|
+
*/
|
|
19
|
+
/** A 3-D `[batch, seq, hidden]` nested array (raw token states from `pooling:'none'`). */
|
|
20
|
+
export type TokenStates3D = number[][][];
|
|
21
|
+
/** A 2-D `[batch, hidden]` nested array (already-pooled vectors). */
|
|
22
|
+
export type Pooled2D = number[][];
|
|
23
|
+
/**
|
|
24
|
+
* L2-normalize a vector in place-safe fashion (returns a new array). A zero vector is returned
|
|
25
|
+
* unchanged (no divide-by-zero).
|
|
26
|
+
*
|
|
27
|
+
* @param vec - The vector to normalize.
|
|
28
|
+
*/
|
|
29
|
+
export declare const l2Normalize: (vec: number[]) => number[];
|
|
30
|
+
/**
|
|
31
|
+
* Pool raw `[batch, seq, hidden]` token states into `[batch, hidden]` vectors and optionally
|
|
32
|
+
* L2-normalize — the deterministic battery-owned path (`poolingOwner: 'battery'`).
|
|
33
|
+
*
|
|
34
|
+
* @param tokenStates - The `[batch, seq, hidden]` raw states from `pooling:'none'`.
|
|
35
|
+
* @param pooling - The pooling strategy (`'mean'` default).
|
|
36
|
+
* @param normalize - Whether to L2-normalize each pooled vector (default `true`).
|
|
37
|
+
* @returns One pooled (and optionally normalized) vector per batch row.
|
|
38
|
+
*/
|
|
39
|
+
export declare const poolAndNormalize: (tokenStates: TokenStates3D, pooling: string, normalize: boolean) => Pooled2D;
|
|
40
|
+
/** Default {@link poolAndNormalize}. */
|
|
41
|
+
export declare const defaultPoolAndNormalize: (tokenStates: TokenStates3D, pooling: string, normalize: boolean) => Pooled2D;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
//#region src/batteries/embeddings/transformers_js/pooling.ts
|
|
2
|
+
/**
|
|
3
|
+
* Mean-pool a `[seq, hidden]` matrix into a `[hidden]` vector.
|
|
4
|
+
*
|
|
5
|
+
* @param rows - The per-token hidden-state rows.
|
|
6
|
+
*/
|
|
7
|
+
var meanPool = (rows) => {
|
|
8
|
+
const seq = rows.length;
|
|
9
|
+
if (seq === 0) return [];
|
|
10
|
+
const hidden = rows[0].length;
|
|
11
|
+
const out = new Array(hidden).fill(0);
|
|
12
|
+
for (const row of rows) for (let h = 0; h < hidden; h++) out[h] += row[h];
|
|
13
|
+
for (let h = 0; h < hidden; h++) out[h] /= seq;
|
|
14
|
+
return out;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* L2-normalize a vector in place-safe fashion (returns a new array). A zero vector is returned
|
|
18
|
+
* unchanged (no divide-by-zero).
|
|
19
|
+
*
|
|
20
|
+
* @param vec - The vector to normalize.
|
|
21
|
+
*/
|
|
22
|
+
var l2Normalize = (vec) => {
|
|
23
|
+
let sumSq = 0;
|
|
24
|
+
for (const v of vec) sumSq += v * v;
|
|
25
|
+
const norm = Math.sqrt(sumSq);
|
|
26
|
+
if (norm === 0) return vec.slice();
|
|
27
|
+
return vec.map((v) => v / norm);
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Pool a single `[seq, hidden]` matrix into one `[hidden]` vector per the strategy.
|
|
31
|
+
*
|
|
32
|
+
* @param rows - The per-token hidden-state rows for one input.
|
|
33
|
+
* @param pooling - The pooling strategy.
|
|
34
|
+
*/
|
|
35
|
+
var poolOne = (rows, pooling) => {
|
|
36
|
+
switch (pooling) {
|
|
37
|
+
case "cls":
|
|
38
|
+
case "first_token": return (rows[0] ?? []).slice();
|
|
39
|
+
case "eos":
|
|
40
|
+
case "last_token": return (rows[rows.length - 1] ?? []).slice();
|
|
41
|
+
case "none": return meanPool(rows);
|
|
42
|
+
default: return meanPool(rows);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Pool raw `[batch, seq, hidden]` token states into `[batch, hidden]` vectors and optionally
|
|
47
|
+
* L2-normalize — the deterministic battery-owned path (`poolingOwner: 'battery'`).
|
|
48
|
+
*
|
|
49
|
+
* @param tokenStates - The `[batch, seq, hidden]` raw states from `pooling:'none'`.
|
|
50
|
+
* @param pooling - The pooling strategy (`'mean'` default).
|
|
51
|
+
* @param normalize - Whether to L2-normalize each pooled vector (default `true`).
|
|
52
|
+
* @returns One pooled (and optionally normalized) vector per batch row.
|
|
53
|
+
*/
|
|
54
|
+
var poolAndNormalize = (tokenStates, pooling, normalize) => tokenStates.map((rows) => {
|
|
55
|
+
const pooled = poolOne(rows, pooling);
|
|
56
|
+
return normalize ? l2Normalize(pooled) : pooled;
|
|
57
|
+
});
|
|
58
|
+
/** Default {@link poolAndNormalize}. */
|
|
59
|
+
var defaultPoolAndNormalize = poolAndNormalize;
|
|
60
|
+
//#endregion
|
|
61
|
+
export { defaultPoolAndNormalize, l2Normalize, poolAndNormalize };
|
|
62
|
+
|
|
63
|
+
//# sourceMappingURL=pooling.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pooling.mjs","names":[],"sources":["../../../../src/batteries/embeddings/transformers_js/pooling.ts"],"sourcesContent":["/**\n * Deterministic, battery-owned pooling + L2-normalization for the transformers.js embeddings battery.\n *\n * @module @nhtio/adk/batteries/embeddings/transformers_js/pooling\n *\n * @remarks\n * Used when `poolingOwner: 'battery'`. The pipeline is asked for raw `pooling:'none'` token states —\n * a `[batch, seq, hidden]` tensor — and this module pools them into one vector per input in pure JS,\n * identically across Node and the browser. That removes the per-runtime post-processing variance that\n * widens the node↔browser cosine gap; only the irreducible ONNX-Runtime kernel floor remains.\n *\n * Pooling strategies mirror the transformers.js feature-extraction options:\n * - `mean` — attention-unaware mean over the sequence axis (matches the pipeline's `'mean'`, which\n * also means over all positions for a single ungrouped sequence).\n * - `cls` / `first_token` — the first token's hidden state.\n * - `eos` / `last_token` — the last token's hidden state.\n * - `none` — no pooling (caller already has the vector); returned unchanged per row.\n */\n\n/** A 3-D `[batch, seq, hidden]` nested array (raw token states from `pooling:'none'`). */\nexport type TokenStates3D = number[][][]\n/** A 2-D `[batch, hidden]` nested array (already-pooled vectors). */\nexport type Pooled2D = number[][]\n\n/**\n * Mean-pool a `[seq, hidden]` matrix into a `[hidden]` vector.\n *\n * @param rows - The per-token hidden-state rows.\n */\nconst meanPool = (rows: number[][]): number[] => {\n const seq = rows.length\n if (seq === 0) return []\n const hidden = rows[0].length\n const out = new Array<number>(hidden).fill(0)\n for (const row of rows) {\n for (let h = 0; h < hidden; h++) out[h] += row[h]\n }\n for (let h = 0; h < hidden; h++) out[h] /= seq\n return out\n}\n\n/**\n * L2-normalize a vector in place-safe fashion (returns a new array). A zero vector is returned\n * unchanged (no divide-by-zero).\n *\n * @param vec - The vector to normalize.\n */\nexport const l2Normalize = (vec: number[]): number[] => {\n let sumSq = 0\n for (const v of vec) sumSq += v * v\n const norm = Math.sqrt(sumSq)\n if (norm === 0) return vec.slice()\n return vec.map((v) => v / norm)\n}\n\n/**\n * Pool a single `[seq, hidden]` matrix into one `[hidden]` vector per the strategy.\n *\n * @param rows - The per-token hidden-state rows for one input.\n * @param pooling - The pooling strategy.\n */\nconst poolOne = (rows: number[][], pooling: string): number[] => {\n switch (pooling) {\n case 'cls':\n case 'first_token':\n return (rows[0] ?? []).slice()\n case 'eos':\n case 'last_token':\n return (rows[rows.length - 1] ?? []).slice()\n case 'none':\n // No pooling requested but we have token states — fall back to mean so a vector still results.\n return meanPool(rows)\n case 'mean':\n default:\n return meanPool(rows)\n }\n}\n\n/**\n * Pool raw `[batch, seq, hidden]` token states into `[batch, hidden]` vectors and optionally\n * L2-normalize — the deterministic battery-owned path (`poolingOwner: 'battery'`).\n *\n * @param tokenStates - The `[batch, seq, hidden]` raw states from `pooling:'none'`.\n * @param pooling - The pooling strategy (`'mean'` default).\n * @param normalize - Whether to L2-normalize each pooled vector (default `true`).\n * @returns One pooled (and optionally normalized) vector per batch row.\n */\nexport const poolAndNormalize = (\n tokenStates: TokenStates3D,\n pooling: string,\n normalize: boolean\n): Pooled2D =>\n tokenStates.map((rows) => {\n const pooled = poolOne(rows, pooling)\n return normalize ? l2Normalize(pooled) : pooled\n })\n\n/** Default {@link poolAndNormalize}. */\nexport const defaultPoolAndNormalize = poolAndNormalize\n"],"mappings":";;;;;;AA6BA,IAAM,YAAY,SAA+B;CAC/C,MAAM,MAAM,KAAK;CACjB,IAAI,QAAQ,GAAG,OAAO,CAAC;CACvB,MAAM,SAAS,KAAK,GAAG;CACvB,MAAM,MAAM,IAAI,MAAc,MAAM,EAAE,KAAK,CAAC;CAC5C,KAAK,MAAM,OAAO,MAChB,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,KAAK,IAAI,MAAM,IAAI;CAEjD,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,KAAK,IAAI,MAAM;CAC3C,OAAO;AACT;;;;;;;AAQA,IAAa,eAAe,QAA4B;CACtD,IAAI,QAAQ;CACZ,KAAK,MAAM,KAAK,KAAK,SAAS,IAAI;CAClC,MAAM,OAAO,KAAK,KAAK,KAAK;CAC5B,IAAI,SAAS,GAAG,OAAO,IAAI,MAAM;CACjC,OAAO,IAAI,KAAK,MAAM,IAAI,IAAI;AAChC;;;;;;;AAQA,IAAM,WAAW,MAAkB,YAA8B;CAC/D,QAAQ,SAAR;EACE,KAAK;EACL,KAAK,eACH,QAAQ,KAAK,MAAM,CAAC,GAAG,MAAM;EAC/B,KAAK;EACL,KAAK,cACH,QAAQ,KAAK,KAAK,SAAS,MAAM,CAAC,GAAG,MAAM;EAC7C,KAAK,QAEH,OAAO,SAAS,IAAI;EAEtB,SACE,OAAO,SAAS,IAAI;CACxB;AACF;;;;;;;;;;AAWA,IAAa,oBACX,aACA,SACA,cAEA,YAAY,KAAK,SAAS;CACxB,MAAM,SAAS,QAAQ,MAAM,OAAO;CACpC,OAAO,YAAY,YAAY,MAAM,IAAI;AAC3C,CAAC;;AAGH,IAAa,0BAA0B"}
|
|
@@ -12,9 +12,11 @@
|
|
|
12
12
|
* Pipeline/dtype/device shapes are **local aliases** of `@huggingface/transformers` types, never
|
|
13
13
|
* direct re-exports of the externalized peer.
|
|
14
14
|
*/
|
|
15
|
+
import type { BatteryLifecycleHooks } from "../../llm/chat_common/index";
|
|
15
16
|
import type { BaseEmbeddingsAdapterOptions } from "../openai/types";
|
|
16
17
|
import type { FeatureExtractionPipeline, DataType as TransformersDataType, DeviceType as TransformersDeviceType, ProgressCallback } from '@huggingface/transformers';
|
|
17
18
|
export type { EmbeddingKind, EmbedOptions, BaseEmbeddingsAdapterOptions } from "../openai/types";
|
|
19
|
+
export type { BatteryLifecyclePhase, BatteryLifecycleBattery, BatteryLifecycleReport, BatteryLifecycleCallback, BatteryLifecycleHooks, } from "../../llm/chat_common/index";
|
|
18
20
|
/** The transformers.js feature-extraction pipeline this battery drives. */
|
|
19
21
|
export type TransformersJsEmbeddingsPipeline = FeatureExtractionPipeline;
|
|
20
22
|
/** Quantization/precision dtype: `'auto'|'fp32'|'fp16'|'q8'|'q4'|…`. */
|
|
@@ -25,6 +27,25 @@ export type TransformersJsEmbeddingsDeviceType = TransformersDeviceType;
|
|
|
25
27
|
export type TransformersJsEmbeddingsProgressCallback = ProgressCallback;
|
|
26
28
|
/** The pooling method applied to the model's per-token hidden states to produce one sentence vector. */
|
|
27
29
|
export type TransformersJsPooling = 'none' | 'mean' | 'cls' | 'first_token' | 'eos' | 'last_token';
|
|
30
|
+
/**
|
|
31
|
+
* Who applies pooling + L2-normalization:
|
|
32
|
+
* - `'engine'` (default) — the transformers.js pipeline pools internally (today's exact behavior). Each
|
|
33
|
+
* runtime (node-ONNX vs web-ONNX/WebGPU) pools its own way, contributing to the node↔browser vector
|
|
34
|
+
* gap.
|
|
35
|
+
* - `'battery'` — request raw `pooling:'none'` token states and pool + normalize in deterministic shared
|
|
36
|
+
* JS (one implementation, identical across node + browser), removing post-processing variance and
|
|
37
|
+
* leaving only the irreducible ORT-kernel floor. `pooling`/`normalize` then take effect in OUR code.
|
|
38
|
+
*/
|
|
39
|
+
export type TransformersJsPoolingOwner = 'engine' | 'battery';
|
|
40
|
+
/**
|
|
41
|
+
* Custom model-source resolver — the dual-environment seam for serving model files from OPFS, a
|
|
42
|
+
* different source, or bundled bytes (see the LLM battery's `model_source` module). Called once per
|
|
43
|
+
* file; return bytes / a path-or-URL string / a `Response`, or `undefined` to fall through to HF.
|
|
44
|
+
*/
|
|
45
|
+
export type TransformersJsEmbeddingsModelSource = (req: {
|
|
46
|
+
repo: string;
|
|
47
|
+
filename: string;
|
|
48
|
+
}) => Promise<Uint8Array | string | Response | undefined> | Uint8Array | string | Response | undefined;
|
|
28
49
|
/**
|
|
29
50
|
* Factory for lazily creating a feature-extraction pipeline. Defaults to a dynamic import of
|
|
30
51
|
* `@huggingface/transformers` + `pipeline('feature-extraction', …)`; override to inject a pre-built
|
|
@@ -44,7 +65,7 @@ export type CreateTransformersJsEmbeddingsPipeline = (input: {
|
|
|
44
65
|
* transformers.js pipeline fields. `model` accepts any ONNX feature-extraction model id (e.g.
|
|
45
66
|
* `onnx-community/all-MiniLM-L6-v2-ONNX`) — there is no default.
|
|
46
67
|
*/
|
|
47
|
-
export interface TransformersJsEmbeddingsAdapterOptions extends BaseEmbeddingsAdapterOptions {
|
|
68
|
+
export interface TransformersJsEmbeddingsAdapterOptions extends BaseEmbeddingsAdapterOptions, BatteryLifecycleHooks {
|
|
48
69
|
/** A pre-built pipeline. When provided, the battery uses it directly and skips lazy creation. */
|
|
49
70
|
pipeline?: TransformersJsEmbeddingsPipeline;
|
|
50
71
|
/** Override the pipeline factory. Default: `pipeline('feature-extraction', …)` via dynamic import. */
|
|
@@ -57,6 +78,18 @@ export interface TransformersJsEmbeddingsAdapterOptions extends BaseEmbeddingsAd
|
|
|
57
78
|
pooling?: TransformersJsPooling;
|
|
58
79
|
/** L2-normalise the embeddings (default `true`). */
|
|
59
80
|
normalize?: boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Who pools + normalizes (default `'engine'` — today's behavior, byte-identical). Set `'battery'` to
|
|
83
|
+
* pool + L2-normalize in deterministic shared JS for tighter node↔browser parity. See
|
|
84
|
+
* {@link TransformersJsPoolingOwner}.
|
|
85
|
+
*/
|
|
86
|
+
poolingOwner?: TransformersJsPoolingOwner;
|
|
87
|
+
/**
|
|
88
|
+
* Custom model-source resolver (OPFS / separate source / bundled bytes). When set, model files load
|
|
89
|
+
* through it behind the global-`env` mutex; otherwise straight from HF (unchanged). See
|
|
90
|
+
* {@link TransformersJsEmbeddingsModelSource}.
|
|
91
|
+
*/
|
|
92
|
+
modelSource?: TransformersJsEmbeddingsModelSource;
|
|
60
93
|
/** Called with model-load progress reports while weights download/compile. */
|
|
61
94
|
onInitProgress?: TransformersJsEmbeddingsProgressCallback;
|
|
62
95
|
/** Override the availability probe. Default: `true` whenever the peer is importable (env-neutral). */
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
require("../../../chunk-Cek0wNdY.js");
|
|
3
|
-
const require_tool_registry = require("../../../tool_registry-
|
|
3
|
+
const require_tool_registry = require("../../../tool_registry-DbS8Pem9.js");
|
|
4
4
|
require("../../../guards.cjs");
|
|
5
5
|
const require_batteries_embeddings_transformers_js_exceptions = require("./exceptions.cjs");
|
|
6
6
|
let _nhtio_validation = require("@nhtio/validation");
|
|
@@ -27,8 +27,17 @@ var transformersJsEmbeddingsOptionsSchema = _nhtio_validation.validator.object({
|
|
|
27
27
|
dtype: _nhtio_validation.validator.string().optional(),
|
|
28
28
|
pooling: _nhtio_validation.validator.string().valid("none", "mean", "cls", "first_token", "eos", "last_token").default("mean"),
|
|
29
29
|
normalize: _nhtio_validation.validator.boolean().default(true),
|
|
30
|
+
poolingOwner: _nhtio_validation.validator.string().valid("engine", "battery").default("engine"),
|
|
31
|
+
modelSource: _nhtio_validation.validator.function().optional(),
|
|
30
32
|
onInitProgress: _nhtio_validation.validator.function().optional(),
|
|
31
|
-
isAvailable: _nhtio_validation.validator.function().optional()
|
|
33
|
+
isAvailable: _nhtio_validation.validator.function().optional(),
|
|
34
|
+
onLifecycle: _nhtio_validation.validator.function().optional(),
|
|
35
|
+
onLoading: _nhtio_validation.validator.function().optional(),
|
|
36
|
+
onCompiling: _nhtio_validation.validator.function().optional(),
|
|
37
|
+
onReady: _nhtio_validation.validator.function().optional(),
|
|
38
|
+
onGenerating: _nhtio_validation.validator.function().optional(),
|
|
39
|
+
onComplete: _nhtio_validation.validator.function().optional(),
|
|
40
|
+
onError: _nhtio_validation.validator.function().optional()
|
|
32
41
|
}).unknown(false);
|
|
33
42
|
var isValidationError = (value) => require_tool_registry.isError(value) && Array.isArray(value.details);
|
|
34
43
|
var formatValidationDetails = (err) => err.details.map((d) => d.message).join(" and ");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.cjs","names":[],"sources":["../../../../src/batteries/embeddings/transformers_js/validation.ts"],"sourcesContent":["/**\n * Runtime validation schema and wrapper for transformers.js Embeddings adapter options.\n *\n * @module @nhtio/adk/batteries/embeddings/transformers_js/validation\n *\n * @remarks\n * Validates `TransformersJsEmbeddingsAdapterOptions` at construction time. Throws\n * `E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS` on failure. `model` is required; unknown top-level\n * keys are rejected so typos fail loud.\n */\n\nimport { isError } from '@nhtio/adk/guards'\nimport { validator, ValidationError } from '@nhtio/validation'\nimport { E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS } from './exceptions'\nimport type { TransformersJsEmbeddingsAdapterOptions } from './types'\n\n/** Validator schema for `TransformersJsEmbeddingsAdapterOptions`. Rejects unknown top-level keys. */\nexport const transformersJsEmbeddingsOptionsSchema = validator\n .object<TransformersJsEmbeddingsAdapterOptions>({\n // Shared base\n model: validator.string().min(1).required(),\n queryPrefix: validator.string().optional(),\n documentPrefix: validator.string().optional(),\n dimensions: validator.number().integer().min(1).optional(),\n // Pipeline knobs (the transformers.js pipeline is a callable object — accept function or object)\n pipeline: validator\n .alternatives(validator.function(), validator.object().unknown(true))\n .optional(),\n createPipeline: validator.function().optional(),\n device: validator.string().optional(),\n dtype: validator.string().optional(),\n pooling: validator\n .string()\n .valid('none', 'mean', 'cls', 'first_token', 'eos', 'last_token')\n .default('mean'),\n normalize: validator.boolean().default(true),\n onInitProgress: validator.function().optional(),\n isAvailable: validator.function().optional(),\n })\n .unknown(false)\n\nconst isValidationError = (value: unknown): value is ValidationError =>\n isError(value) && Array.isArray((value as ValidationError).details)\n\nconst formatValidationDetails = (err: ValidationError): string =>\n err.details.map((d) => d.message).join(' and ')\n\n/**\n * Validates an arbitrary input against `transformersJsEmbeddingsOptionsSchema`.\n *\n * @param input - The raw options object to validate.\n * @returns The resolved options object with defaults filled in.\n * @throws {@link @nhtio/adk/batteries!E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS} when invalid.\n */\nexport const validateOptions = (input: unknown): TransformersJsEmbeddingsAdapterOptions => {\n const { value, error } = transformersJsEmbeddingsOptionsSchema.validate(input, {\n abortEarly: false,\n convert: false,\n })\n if (error && isValidationError(error)) {\n throw new E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS([formatValidationDetails(error)], {\n cause: error,\n })\n }\n return value as TransformersJsEmbeddingsAdapterOptions\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAiBA,IAAa,wCAAwC,kBAAA,UAClD,OAA+C;CAE9C,OAAO,kBAAA,UAAU,OAAO,EAAE,IAAI,CAAC,EAAE,SAAS;CAC1C,aAAa,kBAAA,UAAU,OAAO,EAAE,SAAS;CACzC,gBAAgB,kBAAA,UAAU,OAAO,EAAE,SAAS;CAC5C,YAAY,kBAAA,UAAU,OAAO,EAAE,QAAQ,EAAE,IAAI,CAAC,EAAE,SAAS;CAEzD,UAAU,kBAAA,UACP,aAAa,kBAAA,UAAU,SAAS,GAAG,kBAAA,UAAU,OAAO,EAAE,QAAQ,IAAI,CAAC,EACnE,SAAS;CACZ,gBAAgB,kBAAA,UAAU,SAAS,EAAE,SAAS;CAC9C,QAAQ,kBAAA,UAAU,OAAO,EAAE,SAAS;CACpC,OAAO,kBAAA,UAAU,OAAO,EAAE,SAAS;CACnC,SAAS,kBAAA,UACN,OAAO,EACP,MAAM,QAAQ,QAAQ,OAAO,eAAe,OAAO,YAAY,EAC/D,QAAQ,MAAM;CACjB,WAAW,kBAAA,UAAU,QAAQ,EAAE,QAAQ,IAAI;CAC3C,gBAAgB,kBAAA,UAAU,SAAS,EAAE,SAAS;CAC9C,aAAa,kBAAA,UAAU,SAAS,EAAE,SAAS;
|
|
1
|
+
{"version":3,"file":"validation.cjs","names":[],"sources":["../../../../src/batteries/embeddings/transformers_js/validation.ts"],"sourcesContent":["/**\n * Runtime validation schema and wrapper for transformers.js Embeddings adapter options.\n *\n * @module @nhtio/adk/batteries/embeddings/transformers_js/validation\n *\n * @remarks\n * Validates `TransformersJsEmbeddingsAdapterOptions` at construction time. Throws\n * `E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS` on failure. `model` is required; unknown top-level\n * keys are rejected so typos fail loud.\n */\n\nimport { isError } from '@nhtio/adk/guards'\nimport { validator, ValidationError } from '@nhtio/validation'\nimport { E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS } from './exceptions'\nimport type { TransformersJsEmbeddingsAdapterOptions } from './types'\n\n/** Validator schema for `TransformersJsEmbeddingsAdapterOptions`. Rejects unknown top-level keys. */\nexport const transformersJsEmbeddingsOptionsSchema = validator\n .object<TransformersJsEmbeddingsAdapterOptions>({\n // Shared base\n model: validator.string().min(1).required(),\n queryPrefix: validator.string().optional(),\n documentPrefix: validator.string().optional(),\n dimensions: validator.number().integer().min(1).optional(),\n // Pipeline knobs (the transformers.js pipeline is a callable object — accept function or object)\n pipeline: validator\n .alternatives(validator.function(), validator.object().unknown(true))\n .optional(),\n createPipeline: validator.function().optional(),\n device: validator.string().optional(),\n dtype: validator.string().optional(),\n pooling: validator\n .string()\n .valid('none', 'mean', 'cls', 'first_token', 'eos', 'last_token')\n .default('mean'),\n normalize: validator.boolean().default(true),\n poolingOwner: validator.string().valid('engine', 'battery').default('engine'),\n modelSource: validator.function().optional(),\n onInitProgress: validator.function().optional(),\n isAvailable: validator.function().optional(),\n // ── Lifecycle hooks (opt-in, normalized phase machine; additive over onInitProgress) ──\n onLifecycle: validator.function().optional(),\n onLoading: validator.function().optional(),\n onCompiling: validator.function().optional(),\n onReady: validator.function().optional(),\n onGenerating: validator.function().optional(),\n onComplete: validator.function().optional(),\n onError: validator.function().optional(),\n })\n .unknown(false)\n\nconst isValidationError = (value: unknown): value is ValidationError =>\n isError(value) && Array.isArray((value as ValidationError).details)\n\nconst formatValidationDetails = (err: ValidationError): string =>\n err.details.map((d) => d.message).join(' and ')\n\n/**\n * Validates an arbitrary input against `transformersJsEmbeddingsOptionsSchema`.\n *\n * @param input - The raw options object to validate.\n * @returns The resolved options object with defaults filled in.\n * @throws {@link @nhtio/adk/batteries!E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS} when invalid.\n */\nexport const validateOptions = (input: unknown): TransformersJsEmbeddingsAdapterOptions => {\n const { value, error } = transformersJsEmbeddingsOptionsSchema.validate(input, {\n abortEarly: false,\n convert: false,\n })\n if (error && isValidationError(error)) {\n throw new E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS([formatValidationDetails(error)], {\n cause: error,\n })\n }\n return value as TransformersJsEmbeddingsAdapterOptions\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAiBA,IAAa,wCAAwC,kBAAA,UAClD,OAA+C;CAE9C,OAAO,kBAAA,UAAU,OAAO,EAAE,IAAI,CAAC,EAAE,SAAS;CAC1C,aAAa,kBAAA,UAAU,OAAO,EAAE,SAAS;CACzC,gBAAgB,kBAAA,UAAU,OAAO,EAAE,SAAS;CAC5C,YAAY,kBAAA,UAAU,OAAO,EAAE,QAAQ,EAAE,IAAI,CAAC,EAAE,SAAS;CAEzD,UAAU,kBAAA,UACP,aAAa,kBAAA,UAAU,SAAS,GAAG,kBAAA,UAAU,OAAO,EAAE,QAAQ,IAAI,CAAC,EACnE,SAAS;CACZ,gBAAgB,kBAAA,UAAU,SAAS,EAAE,SAAS;CAC9C,QAAQ,kBAAA,UAAU,OAAO,EAAE,SAAS;CACpC,OAAO,kBAAA,UAAU,OAAO,EAAE,SAAS;CACnC,SAAS,kBAAA,UACN,OAAO,EACP,MAAM,QAAQ,QAAQ,OAAO,eAAe,OAAO,YAAY,EAC/D,QAAQ,MAAM;CACjB,WAAW,kBAAA,UAAU,QAAQ,EAAE,QAAQ,IAAI;CAC3C,cAAc,kBAAA,UAAU,OAAO,EAAE,MAAM,UAAU,SAAS,EAAE,QAAQ,QAAQ;CAC5E,aAAa,kBAAA,UAAU,SAAS,EAAE,SAAS;CAC3C,gBAAgB,kBAAA,UAAU,SAAS,EAAE,SAAS;CAC9C,aAAa,kBAAA,UAAU,SAAS,EAAE,SAAS;CAE3C,aAAa,kBAAA,UAAU,SAAS,EAAE,SAAS;CAC3C,WAAW,kBAAA,UAAU,SAAS,EAAE,SAAS;CACzC,aAAa,kBAAA,UAAU,SAAS,EAAE,SAAS;CAC3C,SAAS,kBAAA,UAAU,SAAS,EAAE,SAAS;CACvC,cAAc,kBAAA,UAAU,SAAS,EAAE,SAAS;CAC5C,YAAY,kBAAA,UAAU,SAAS,EAAE,SAAS;CAC1C,SAAS,kBAAA,UAAU,SAAS,EAAE,SAAS;AACzC,CAAC,EACA,QAAQ,KAAK;AAEhB,IAAM,qBAAqB,UACzB,sBAAA,QAAQ,KAAK,KAAK,MAAM,QAAS,MAA0B,OAAO;AAEpE,IAAM,2BAA2B,QAC/B,IAAI,QAAQ,KAAK,MAAM,EAAE,OAAO,EAAE,KAAK,OAAO;;;;;;;;AAShD,IAAa,mBAAmB,UAA2D;CACzF,MAAM,EAAE,OAAO,UAAU,sCAAsC,SAAS,OAAO;EAC7E,YAAY;EACZ,SAAS;CACX,CAAC;CACD,IAAI,SAAS,kBAAkB,KAAK,GAClC,MAAM,IAAI,wDAAA,6CAA6C,CAAC,wBAAwB,KAAK,CAAC,GAAG,EACvF,OAAO,MACT,CAAC;CAEH,OAAO;AACT"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { c as isError } from "../../../tool_registry-
|
|
1
|
+
import { c as isError } from "../../../tool_registry-Bj0JhXCv.mjs";
|
|
2
2
|
import "../../../guards.mjs";
|
|
3
3
|
import { E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS } from "./exceptions.mjs";
|
|
4
4
|
import { validator } from "@nhtio/validation";
|
|
@@ -25,8 +25,17 @@ var transformersJsEmbeddingsOptionsSchema = validator.object({
|
|
|
25
25
|
dtype: validator.string().optional(),
|
|
26
26
|
pooling: validator.string().valid("none", "mean", "cls", "first_token", "eos", "last_token").default("mean"),
|
|
27
27
|
normalize: validator.boolean().default(true),
|
|
28
|
+
poolingOwner: validator.string().valid("engine", "battery").default("engine"),
|
|
29
|
+
modelSource: validator.function().optional(),
|
|
28
30
|
onInitProgress: validator.function().optional(),
|
|
29
|
-
isAvailable: validator.function().optional()
|
|
31
|
+
isAvailable: validator.function().optional(),
|
|
32
|
+
onLifecycle: validator.function().optional(),
|
|
33
|
+
onLoading: validator.function().optional(),
|
|
34
|
+
onCompiling: validator.function().optional(),
|
|
35
|
+
onReady: validator.function().optional(),
|
|
36
|
+
onGenerating: validator.function().optional(),
|
|
37
|
+
onComplete: validator.function().optional(),
|
|
38
|
+
onError: validator.function().optional()
|
|
30
39
|
}).unknown(false);
|
|
31
40
|
var isValidationError = (value) => isError(value) && Array.isArray(value.details);
|
|
32
41
|
var formatValidationDetails = (err) => err.details.map((d) => d.message).join(" and ");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.mjs","names":[],"sources":["../../../../src/batteries/embeddings/transformers_js/validation.ts"],"sourcesContent":["/**\n * Runtime validation schema and wrapper for transformers.js Embeddings adapter options.\n *\n * @module @nhtio/adk/batteries/embeddings/transformers_js/validation\n *\n * @remarks\n * Validates `TransformersJsEmbeddingsAdapterOptions` at construction time. Throws\n * `E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS` on failure. `model` is required; unknown top-level\n * keys are rejected so typos fail loud.\n */\n\nimport { isError } from '@nhtio/adk/guards'\nimport { validator, ValidationError } from '@nhtio/validation'\nimport { E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS } from './exceptions'\nimport type { TransformersJsEmbeddingsAdapterOptions } from './types'\n\n/** Validator schema for `TransformersJsEmbeddingsAdapterOptions`. Rejects unknown top-level keys. */\nexport const transformersJsEmbeddingsOptionsSchema = validator\n .object<TransformersJsEmbeddingsAdapterOptions>({\n // Shared base\n model: validator.string().min(1).required(),\n queryPrefix: validator.string().optional(),\n documentPrefix: validator.string().optional(),\n dimensions: validator.number().integer().min(1).optional(),\n // Pipeline knobs (the transformers.js pipeline is a callable object — accept function or object)\n pipeline: validator\n .alternatives(validator.function(), validator.object().unknown(true))\n .optional(),\n createPipeline: validator.function().optional(),\n device: validator.string().optional(),\n dtype: validator.string().optional(),\n pooling: validator\n .string()\n .valid('none', 'mean', 'cls', 'first_token', 'eos', 'last_token')\n .default('mean'),\n normalize: validator.boolean().default(true),\n onInitProgress: validator.function().optional(),\n isAvailable: validator.function().optional(),\n })\n .unknown(false)\n\nconst isValidationError = (value: unknown): value is ValidationError =>\n isError(value) && Array.isArray((value as ValidationError).details)\n\nconst formatValidationDetails = (err: ValidationError): string =>\n err.details.map((d) => d.message).join(' and ')\n\n/**\n * Validates an arbitrary input against `transformersJsEmbeddingsOptionsSchema`.\n *\n * @param input - The raw options object to validate.\n * @returns The resolved options object with defaults filled in.\n * @throws {@link @nhtio/adk/batteries!E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS} when invalid.\n */\nexport const validateOptions = (input: unknown): TransformersJsEmbeddingsAdapterOptions => {\n const { value, error } = transformersJsEmbeddingsOptionsSchema.validate(input, {\n abortEarly: false,\n convert: false,\n })\n if (error && isValidationError(error)) {\n throw new E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS([formatValidationDetails(error)], {\n cause: error,\n })\n }\n return value as TransformersJsEmbeddingsAdapterOptions\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAiBA,IAAa,wCAAwC,UAClD,OAA+C;CAE9C,OAAO,UAAU,OAAO,EAAE,IAAI,CAAC,EAAE,SAAS;CAC1C,aAAa,UAAU,OAAO,EAAE,SAAS;CACzC,gBAAgB,UAAU,OAAO,EAAE,SAAS;CAC5C,YAAY,UAAU,OAAO,EAAE,QAAQ,EAAE,IAAI,CAAC,EAAE,SAAS;CAEzD,UAAU,UACP,aAAa,UAAU,SAAS,GAAG,UAAU,OAAO,EAAE,QAAQ,IAAI,CAAC,EACnE,SAAS;CACZ,gBAAgB,UAAU,SAAS,EAAE,SAAS;CAC9C,QAAQ,UAAU,OAAO,EAAE,SAAS;CACpC,OAAO,UAAU,OAAO,EAAE,SAAS;CACnC,SAAS,UACN,OAAO,EACP,MAAM,QAAQ,QAAQ,OAAO,eAAe,OAAO,YAAY,EAC/D,QAAQ,MAAM;CACjB,WAAW,UAAU,QAAQ,EAAE,QAAQ,IAAI;CAC3C,gBAAgB,UAAU,SAAS,EAAE,SAAS;CAC9C,aAAa,UAAU,SAAS,EAAE,SAAS;
|
|
1
|
+
{"version":3,"file":"validation.mjs","names":[],"sources":["../../../../src/batteries/embeddings/transformers_js/validation.ts"],"sourcesContent":["/**\n * Runtime validation schema and wrapper for transformers.js Embeddings adapter options.\n *\n * @module @nhtio/adk/batteries/embeddings/transformers_js/validation\n *\n * @remarks\n * Validates `TransformersJsEmbeddingsAdapterOptions` at construction time. Throws\n * `E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS` on failure. `model` is required; unknown top-level\n * keys are rejected so typos fail loud.\n */\n\nimport { isError } from '@nhtio/adk/guards'\nimport { validator, ValidationError } from '@nhtio/validation'\nimport { E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS } from './exceptions'\nimport type { TransformersJsEmbeddingsAdapterOptions } from './types'\n\n/** Validator schema for `TransformersJsEmbeddingsAdapterOptions`. Rejects unknown top-level keys. */\nexport const transformersJsEmbeddingsOptionsSchema = validator\n .object<TransformersJsEmbeddingsAdapterOptions>({\n // Shared base\n model: validator.string().min(1).required(),\n queryPrefix: validator.string().optional(),\n documentPrefix: validator.string().optional(),\n dimensions: validator.number().integer().min(1).optional(),\n // Pipeline knobs (the transformers.js pipeline is a callable object — accept function or object)\n pipeline: validator\n .alternatives(validator.function(), validator.object().unknown(true))\n .optional(),\n createPipeline: validator.function().optional(),\n device: validator.string().optional(),\n dtype: validator.string().optional(),\n pooling: validator\n .string()\n .valid('none', 'mean', 'cls', 'first_token', 'eos', 'last_token')\n .default('mean'),\n normalize: validator.boolean().default(true),\n poolingOwner: validator.string().valid('engine', 'battery').default('engine'),\n modelSource: validator.function().optional(),\n onInitProgress: validator.function().optional(),\n isAvailable: validator.function().optional(),\n // ── Lifecycle hooks (opt-in, normalized phase machine; additive over onInitProgress) ──\n onLifecycle: validator.function().optional(),\n onLoading: validator.function().optional(),\n onCompiling: validator.function().optional(),\n onReady: validator.function().optional(),\n onGenerating: validator.function().optional(),\n onComplete: validator.function().optional(),\n onError: validator.function().optional(),\n })\n .unknown(false)\n\nconst isValidationError = (value: unknown): value is ValidationError =>\n isError(value) && Array.isArray((value as ValidationError).details)\n\nconst formatValidationDetails = (err: ValidationError): string =>\n err.details.map((d) => d.message).join(' and ')\n\n/**\n * Validates an arbitrary input against `transformersJsEmbeddingsOptionsSchema`.\n *\n * @param input - The raw options object to validate.\n * @returns The resolved options object with defaults filled in.\n * @throws {@link @nhtio/adk/batteries!E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS} when invalid.\n */\nexport const validateOptions = (input: unknown): TransformersJsEmbeddingsAdapterOptions => {\n const { value, error } = transformersJsEmbeddingsOptionsSchema.validate(input, {\n abortEarly: false,\n convert: false,\n })\n if (error && isValidationError(error)) {\n throw new E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS([formatValidationDetails(error)], {\n cause: error,\n })\n }\n return value as TransformersJsEmbeddingsAdapterOptions\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAiBA,IAAa,wCAAwC,UAClD,OAA+C;CAE9C,OAAO,UAAU,OAAO,EAAE,IAAI,CAAC,EAAE,SAAS;CAC1C,aAAa,UAAU,OAAO,EAAE,SAAS;CACzC,gBAAgB,UAAU,OAAO,EAAE,SAAS;CAC5C,YAAY,UAAU,OAAO,EAAE,QAAQ,EAAE,IAAI,CAAC,EAAE,SAAS;CAEzD,UAAU,UACP,aAAa,UAAU,SAAS,GAAG,UAAU,OAAO,EAAE,QAAQ,IAAI,CAAC,EACnE,SAAS;CACZ,gBAAgB,UAAU,SAAS,EAAE,SAAS;CAC9C,QAAQ,UAAU,OAAO,EAAE,SAAS;CACpC,OAAO,UAAU,OAAO,EAAE,SAAS;CACnC,SAAS,UACN,OAAO,EACP,MAAM,QAAQ,QAAQ,OAAO,eAAe,OAAO,YAAY,EAC/D,QAAQ,MAAM;CACjB,WAAW,UAAU,QAAQ,EAAE,QAAQ,IAAI;CAC3C,cAAc,UAAU,OAAO,EAAE,MAAM,UAAU,SAAS,EAAE,QAAQ,QAAQ;CAC5E,aAAa,UAAU,SAAS,EAAE,SAAS;CAC3C,gBAAgB,UAAU,SAAS,EAAE,SAAS;CAC9C,aAAa,UAAU,SAAS,EAAE,SAAS;CAE3C,aAAa,UAAU,SAAS,EAAE,SAAS;CAC3C,WAAW,UAAU,SAAS,EAAE,SAAS;CACzC,aAAa,UAAU,SAAS,EAAE,SAAS;CAC3C,SAAS,UAAU,SAAS,EAAE,SAAS;CACvC,cAAc,UAAU,SAAS,EAAE,SAAS;CAC5C,YAAY,UAAU,SAAS,EAAE,SAAS;CAC1C,SAAS,UAAU,SAAS,EAAE,SAAS;AACzC,CAAC,EACA,QAAQ,KAAK;AAEhB,IAAM,qBAAqB,UACzB,QAAQ,KAAK,KAAK,MAAM,QAAS,MAA0B,OAAO;AAEpE,IAAM,2BAA2B,QAC/B,IAAI,QAAQ,KAAK,MAAM,EAAE,OAAO,EAAE,KAAK,OAAO;;;;;;;;AAShD,IAAa,mBAAmB,UAA2D;CACzF,MAAM,EAAE,OAAO,UAAU,sCAAsC,SAAS,OAAO;EAC7E,YAAY;EACZ,SAAS;CACX,CAAC;CACD,IAAI,SAAS,kBAAkB,KAAK,GAClC,MAAM,IAAI,6CAA6C,CAAC,wBAAwB,KAAK,CAAC,GAAG,EACvF,OAAO,MACT,CAAC;CAEH,OAAO;AACT"}
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
require("../../chunk-Cek0wNdY.js");
|
|
3
|
+
const require_batteries_embeddings_transformers_js_pooling = require("./transformers_js/pooling.cjs");
|
|
3
4
|
const require_batteries_embeddings_transformers_js_exceptions = require("./transformers_js/exceptions.cjs");
|
|
4
5
|
const require_batteries_embeddings_transformers_js_validation = require("./transformers_js/validation.cjs");
|
|
5
6
|
const require_batteries_embeddings_transformers_js_adapter = require("./transformers_js/adapter.cjs");
|
|
6
7
|
exports.E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS = require_batteries_embeddings_transformers_js_exceptions.E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS;
|
|
7
8
|
exports.E_TRANSFORMERS_JS_EMBEDDINGS_ENGINE_ERROR = require_batteries_embeddings_transformers_js_exceptions.E_TRANSFORMERS_JS_EMBEDDINGS_ENGINE_ERROR;
|
|
8
9
|
exports.TransformersJsEmbeddingsAdapter = require_batteries_embeddings_transformers_js_adapter.TransformersJsEmbeddingsAdapter;
|
|
10
|
+
exports.defaultPoolAndNormalize = require_batteries_embeddings_transformers_js_pooling.defaultPoolAndNormalize;
|
|
11
|
+
exports.l2Normalize = require_batteries_embeddings_transformers_js_pooling.l2Normalize;
|
|
12
|
+
exports.poolAndNormalize = require_batteries_embeddings_transformers_js_pooling.poolAndNormalize;
|
|
9
13
|
exports.transformersJsEmbeddingsOptionsSchema = require_batteries_embeddings_transformers_js_validation.transformersJsEmbeddingsOptionsSchema;
|
|
10
14
|
exports.validateOptions = require_batteries_embeddings_transformers_js_validation.validateOptions;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
+
import { defaultPoolAndNormalize, l2Normalize, poolAndNormalize } from "./transformers_js/pooling.mjs";
|
|
1
2
|
import { E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS, E_TRANSFORMERS_JS_EMBEDDINGS_ENGINE_ERROR } from "./transformers_js/exceptions.mjs";
|
|
2
3
|
import { transformersJsEmbeddingsOptionsSchema, validateOptions } from "./transformers_js/validation.mjs";
|
|
3
4
|
import { TransformersJsEmbeddingsAdapter } from "./transformers_js/adapter.mjs";
|
|
4
|
-
export { E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS, E_TRANSFORMERS_JS_EMBEDDINGS_ENGINE_ERROR, TransformersJsEmbeddingsAdapter, transformersJsEmbeddingsOptionsSchema, validateOptions };
|
|
5
|
+
export { E_INVALID_TRANSFORMERS_JS_EMBEDDINGS_OPTIONS, E_TRANSFORMERS_JS_EMBEDDINGS_ENGINE_ERROR, TransformersJsEmbeddingsAdapter, defaultPoolAndNormalize, l2Normalize, poolAndNormalize, transformersJsEmbeddingsOptionsSchema, validateOptions };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
require("../../../chunk-Cek0wNdY.js");
|
|
3
|
-
const require_tool_registry = require("../../../tool_registry-
|
|
3
|
+
const require_tool_registry = require("../../../tool_registry-DbS8Pem9.js");
|
|
4
4
|
require("../../../guards.cjs");
|
|
5
5
|
const require_batteries_embeddings_openai_helpers = require("../openai/helpers.cjs");
|
|
6
6
|
const require_batteries_embeddings_webllm_exceptions = require("./exceptions.cjs");
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { c as isError } from "../../../tool_registry-
|
|
1
|
+
import { c as isError } from "../../../tool_registry-Bj0JhXCv.mjs";
|
|
2
2
|
import "../../../guards.mjs";
|
|
3
3
|
import { applyEmbeddingPrefix } from "../openai/helpers.mjs";
|
|
4
4
|
import { E_INVALID_WEBLLM_EMBEDDINGS_OPTIONS, E_WEBLLM_EMBEDDINGS_ENGINE_ERROR } from "./exceptions.mjs";
|
package/batteries/encoding.cjs
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
require("../chunk-Cek0wNdY.js");
|
|
3
|
-
const require_tool_registry = require("../tool_registry-
|
|
4
|
-
const require_thought = require("../thought-
|
|
5
|
-
const require_tool = require("../tool-
|
|
6
|
-
const require_spooled_artifact = require("../spooled_artifact-
|
|
7
|
-
const require_common = require("../common-
|
|
8
|
-
const require_tool_call = require("../tool_call-
|
|
9
|
-
const require_spooled_markdown_artifact = require("../spooled_markdown_artifact-
|
|
3
|
+
const require_tool_registry = require("../tool_registry-DbS8Pem9.js");
|
|
4
|
+
const require_thought = require("../thought-CE88bddw.js");
|
|
5
|
+
const require_tool = require("../tool-BngpaOWb.js");
|
|
6
|
+
const require_spooled_artifact = require("../spooled_artifact-DrIR90nV.js");
|
|
7
|
+
const require_common = require("../common-ax3vKu6O.js");
|
|
8
|
+
const require_tool_call = require("../tool_call-CZVmISyc.js");
|
|
9
|
+
const require_spooled_markdown_artifact = require("../spooled_markdown_artifact-rg_WCNWq.js");
|
|
10
10
|
require("../index.cjs");
|
|
11
11
|
const require_batteries_storage_in_memory = require("./storage/in_memory.cjs");
|
|
12
12
|
let _nhtio_encoder = require("@nhtio/encoder");
|
package/batteries/encoding.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { i as Identity, n as Message, r as Memory, t as Thought } from "../thought-
|
|
3
|
-
import { t as Tool } from "../tool-
|
|
4
|
-
import { a as resolveMediaReader, c as ArtifactTool, i as registerSpoolReaderResolver, o as resolveSpoolReader, r as registerMediaReaderResolver, t as SpooledArtifact } from "../spooled_artifact-
|
|
5
|
-
import { a as inMemoryMediaReader, c as Retrievable, n as MEDIA_READER_TAG_IN_MEMORY, r as fromFetch, t as MEDIA_READER_TAG_FETCH } from "../common-
|
|
6
|
-
import { n as Media, o as decodeBase64, t as ToolCall } from "../tool_call-
|
|
7
|
-
import { n as SpooledJsonArtifact, t as SpooledMarkdownArtifact } from "../spooled_markdown_artifact-
|
|
1
|
+
import { i as Registry, r as Tokenizable, t as ToolRegistry } from "../tool_registry-Bj0JhXCv.mjs";
|
|
2
|
+
import { i as Identity, n as Message, r as Memory, t as Thought } from "../thought-DdiQCpgX.mjs";
|
|
3
|
+
import { t as Tool } from "../tool-asIfCjxb.mjs";
|
|
4
|
+
import { a as resolveMediaReader, c as ArtifactTool, i as registerSpoolReaderResolver, o as resolveSpoolReader, r as registerMediaReaderResolver, t as SpooledArtifact } from "../spooled_artifact-BnbqIZ3a.mjs";
|
|
5
|
+
import { a as inMemoryMediaReader, c as Retrievable, n as MEDIA_READER_TAG_IN_MEMORY, r as fromFetch, t as MEDIA_READER_TAG_FETCH } from "../common-DJH3UemH.mjs";
|
|
6
|
+
import { n as Media, o as decodeBase64, t as ToolCall } from "../tool_call-TtzdXxcb.mjs";
|
|
7
|
+
import { n as SpooledJsonArtifact, t as SpooledMarkdownArtifact } from "../spooled_markdown_artifact-BJH_JZFm.mjs";
|
|
8
8
|
import "../index.mjs";
|
|
9
9
|
import { InMemorySpoolReader, SPOOL_READER_TAG_IN_MEMORY } from "./storage/in_memory.mjs";
|
|
10
10
|
import { registerClass } from "@nhtio/encoder";
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The portable, battery-agnostic GENERATION contract shared by the on-device LLM batteries.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* INTERNAL to the bundled LLM batteries — intentionally NOT `@module`-tagged, so it stays private and
|
|
6
|
+
* is inlined into each consumer by the bundler (the same convention as `chat_common/helpers.ts`).
|
|
7
|
+
*
|
|
8
|
+
* **Why this exists.** transformers.js and LiteRT-LM express the same generation concepts with different
|
|
9
|
+
* option names and shapes — `maxNewTokens` vs `maxOutputTokens`, a flat `doSample`+`temperature`/`topK`/
|
|
10
|
+
* `topP` vs a nested `samplerParams:{type,k,p,…}`, one `multimodal:{image,audio}` object vs two
|
|
11
|
+
* `visionModalityEnabled`/`audioModalityEnabled` booleans. Swapping batteries therefore meant rewriting
|
|
12
|
+
* the config. This module defines ONE canonical surface ({@link ChatGenerationOptions}) that both
|
|
13
|
+
* batteries accept; each adapter maps it onto its native API via {@link resolveGenerationOptions}.
|
|
14
|
+
*
|
|
15
|
+
* **Contract.** The canonical fields are additive — every native field a battery already exposed
|
|
16
|
+
* remains a working escape hatch. When BOTH a canonical field and its native equivalent are set, the
|
|
17
|
+
* **canonical value wins** (it is the portable intent; the native field is the low-level override that
|
|
18
|
+
* the resolver only consults when the canonical one is absent). All values carry the same
|
|
19
|
+
* deterministic-friendly defaults across batteries (greedy, temp 0.7, top-k 40, top-p 0.95, max 1024).
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* The portable sampler strategy. Maps to each battery's native mechanism:
|
|
23
|
+
* - `'greedy'` — deterministic argmax. transformers.js `do_sample:false`; LiteRT `SamplerType.GREEDY`
|
|
24
|
+
* (which is top-1, so `k` is forced to 1).
|
|
25
|
+
* - `'top-k'` — sample from the top-`k` logits. transformers.js `do_sample:true`+`top_k`; LiteRT
|
|
26
|
+
* `SamplerType.TOP_K`+`k`.
|
|
27
|
+
* - `'top-p'` — nucleus sampling. transformers.js `do_sample:true`+`top_p`; LiteRT `SamplerType.TOP_P`+`p`.
|
|
28
|
+
*/
|
|
29
|
+
export type ChatSampler = 'greedy' | 'top-k' | 'top-p';
|
|
30
|
+
/** The canonical generation options both on-device batteries accept. Every field optional + defaulted. */
|
|
31
|
+
export interface ChatGenerationOptions {
|
|
32
|
+
/**
|
|
33
|
+
* Maximum tokens to GENERATE this turn. The portable spelling of transformers.js `maxNewTokens` /
|
|
34
|
+
* LiteRT `maxOutputTokens`. Default `1024`.
|
|
35
|
+
*/
|
|
36
|
+
maxTokens?: number;
|
|
37
|
+
/** Sampler strategy (default `'greedy'` — deterministic). See {@link ChatSampler}. */
|
|
38
|
+
sampler?: ChatSampler;
|
|
39
|
+
/** Sampling temperature, used when `sampler` is `'top-k'`/`'top-p'`. Always pinned. Default `0.7`. */
|
|
40
|
+
temperature?: number;
|
|
41
|
+
/** Top-K cutoff, used when `sampler` is `'top-k'`. Default `40`. */
|
|
42
|
+
topK?: number;
|
|
43
|
+
/** Top-P (nucleus) cutoff, used when `sampler` is `'top-p'`. Default `0.95`. */
|
|
44
|
+
topP?: number;
|
|
45
|
+
/** RNG seed for reproducible sampling (best-effort; only honoured by batteries that expose it). */
|
|
46
|
+
seed?: number;
|
|
47
|
+
/**
|
|
48
|
+
* Whether to enable the model's "thinking"/reasoning mode, passed EXPLICITLY to the chat template.
|
|
49
|
+
* Default `false` — many reasoning templates default thinking ON and burn the budget. Already shared
|
|
50
|
+
* by both batteries under this exact name; restated here so the whole generation contract is in one place.
|
|
51
|
+
*/
|
|
52
|
+
enableThinking?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Enable multimodal input by kind. The portable spelling of transformers.js `multimodal:{image,audio}`
|
|
55
|
+
* / LiteRT `visionModalityEnabled`+`audioModalityEnabled`.
|
|
56
|
+
*/
|
|
57
|
+
multimodal?: {
|
|
58
|
+
image?: boolean;
|
|
59
|
+
audio?: boolean;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/** The resolved, fully-defaulted generation config the adapters consume. */
|
|
63
|
+
export interface ResolvedGenerationOptions {
|
|
64
|
+
/** Max tokens to generate this turn. */
|
|
65
|
+
maxTokens: number;
|
|
66
|
+
/** The resolved sampler strategy. */
|
|
67
|
+
sampler: ChatSampler;
|
|
68
|
+
/** Sampling temperature (used by `'top-k'`/`'top-p'`). */
|
|
69
|
+
temperature: number;
|
|
70
|
+
/** Top-K cutoff (used by `'top-k'`). */
|
|
71
|
+
topK: number;
|
|
72
|
+
/** Top-P (nucleus) cutoff (used by `'top-p'`). */
|
|
73
|
+
topP: number;
|
|
74
|
+
/** RNG seed, when provided. */
|
|
75
|
+
seed?: number;
|
|
76
|
+
/** Whether thinking/reasoning mode is enabled. */
|
|
77
|
+
enableThinking: boolean;
|
|
78
|
+
/** Resolved multimodal-input flags by kind. */
|
|
79
|
+
multimodal: {
|
|
80
|
+
image: boolean;
|
|
81
|
+
audio: boolean;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/** Deterministic-friendly defaults, identical across batteries. */
|
|
85
|
+
export declare const GENERATION_DEFAULTS: ResolvedGenerationOptions;
|
|
86
|
+
/**
|
|
87
|
+
* Resolve the canonical {@link ChatGenerationOptions} (already merged across option layers) into a
|
|
88
|
+
* fully-defaulted {@link ResolvedGenerationOptions}. Native per-battery fallbacks are passed via
|
|
89
|
+
* `nativeFallbacks` and consulted ONLY when the canonical field is absent (canonical wins). The adapter
|
|
90
|
+
* then maps the resolved shape onto its runtime API.
|
|
91
|
+
*
|
|
92
|
+
* @param canonical - The canonical fields from the merged adapter options.
|
|
93
|
+
* @param nativeFallbacks - Battery-native equivalents to fall back to when a canonical field is unset
|
|
94
|
+
* (e.g. transformers.js `maxNewTokens`, LiteRT `maxOutputTokens`). Each is consulted second.
|
|
95
|
+
*/
|
|
96
|
+
export declare const resolveGenerationOptions: (canonical: ChatGenerationOptions, nativeFallbacks?: {
|
|
97
|
+
maxTokens?: number;
|
|
98
|
+
sampler?: ChatSampler;
|
|
99
|
+
temperature?: number;
|
|
100
|
+
topK?: number;
|
|
101
|
+
topP?: number;
|
|
102
|
+
seed?: number;
|
|
103
|
+
enableThinking?: boolean;
|
|
104
|
+
multimodal?: {
|
|
105
|
+
image?: boolean;
|
|
106
|
+
audio?: boolean;
|
|
107
|
+
};
|
|
108
|
+
}) => ResolvedGenerationOptions;
|
|
109
|
+
/** Default {@link resolveGenerationOptions}. */
|
|
110
|
+
export declare const defaultResolveGenerationOptions: (canonical: ChatGenerationOptions, nativeFallbacks?: {
|
|
111
|
+
maxTokens?: number;
|
|
112
|
+
sampler?: ChatSampler;
|
|
113
|
+
temperature?: number;
|
|
114
|
+
topK?: number;
|
|
115
|
+
topP?: number;
|
|
116
|
+
seed?: number;
|
|
117
|
+
enableThinking?: boolean;
|
|
118
|
+
multimodal?: {
|
|
119
|
+
image?: boolean;
|
|
120
|
+
audio?: boolean;
|
|
121
|
+
};
|
|
122
|
+
}) => ResolvedGenerationOptions;
|
|
@@ -16,6 +16,74 @@
|
|
|
16
16
|
import type { Tool, ArtifactTool, Tokenizable, Memory, Thought, Retrievable } from "../../../common";
|
|
17
17
|
import type { ChatCompletionsBucketOrder, ChatCompletionsTool, DescriptionLike, JsonSchema, MemoryAttrs, RetrievableAttrs, StandingInstructionAttrs, ThoughtAttrs, TrustedContentAttrs, UntrustedContentAttrs, ChatHelpersCommon } from "./types";
|
|
18
18
|
export declare const escapeXmlAttribute: (value: string) => string;
|
|
19
|
+
/**
|
|
20
|
+
* Neutralise the **no-nonce** `<system_instructions …>` / `</system_instructions>` developer-rules tier
|
|
21
|
+
* if it appears inside model- or user-supplied BODY content.
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* Every other trust tier (untrusted/trusted/peer/thought/memory/retrieved) carries an unguessable
|
|
25
|
+
* per-primitive nonce in its tag name, so a model that mirrors one cannot forge a sibling's closer. The
|
|
26
|
+
* standing-instructions tier is the lone exception — {@link renderStandingInstructions} emits it WITHOUT
|
|
27
|
+
* a nonce because it is the highest-authority block. That makes a model-mirrored copy textually
|
|
28
|
+
* identical to the real tier. The legitimate tier is ALWAYS harness-injected (built by
|
|
29
|
+
* `renderStandingInstructions` and concatenated into the system prompt) and never flows through a
|
|
30
|
+
* message/thought body — so escaping the leading `<` of the literal token wherever it occurs in body
|
|
31
|
+
* content is always safe and renders the copy inert (visible, but unmistakably not a structural tag).
|
|
32
|
+
* Mirrors how the nonce already neutralises the other tiers. See the envelope-mimicry threat model.
|
|
33
|
+
*/
|
|
34
|
+
export declare const neutraliseDeveloperRulesTag: (text: string) => string;
|
|
35
|
+
/**
|
|
36
|
+
* Strip the non-semantic envelope/turn-boundary special tokens (see {@link ENVELOPE_SPECIAL_TOKEN_RE})
|
|
37
|
+
* from decoded model text before it reaches the parser layer.
|
|
38
|
+
*
|
|
39
|
+
* @remarks
|
|
40
|
+
* **Why this exists.** The transformers.js streaming path decodes with `skip_special_tokens:false` (it
|
|
41
|
+
* must — the live prose-stop gate watches for tool/think markers, which ARE special tokens). That leaves
|
|
42
|
+
* envelope tokens like Llama's `<|python_tag|>{json}<|eom_id|>` or ChatML's trailing `<|im_end|>` in the
|
|
43
|
+
* accumulated text, so the JSON tool-call parsers see `<|python_tag|>{…}` and decline — even though the
|
|
44
|
+
* NON-streaming path (which decodes with `skip_special_tokens:true`) parses the identical call fine.
|
|
45
|
+
* Normalising here makes the stream and batch paths parse equivalent text. Surfaced by the deep model
|
|
46
|
+
* matrix (Llama-3.2-1B + Qwen2.5-Coder tool calls passed on batch, failed on stream).
|
|
47
|
+
*/
|
|
48
|
+
export declare const stripEnvelopeSpecialTokens: (text: string) => string;
|
|
49
|
+
/**
|
|
50
|
+
* Validate a media `mimeType` for safe interpolation into a `data:<mime>;base64,…` URI, a
|
|
51
|
+
* `Blob({type})`, or a synthetic-description line.
|
|
52
|
+
*
|
|
53
|
+
* @remarks
|
|
54
|
+
* A raw `mimeType` is attacker-influenced (it rides in on user uploads / tool output). The committee's
|
|
55
|
+
* sharpest finding was a `mimeType` like `image/png;base64,<payload>;x=` — interpolated into
|
|
56
|
+
* `data:${mime};base64,${b64}` it produces a DOUBLE `;base64,`, letting a permissive data-URI parser
|
|
57
|
+
* decode the attacker's prefix instead of the real payload (a content-type confusion / injection). A
|
|
58
|
+
* `\r\n` in the mime is an HTTP-header-injection vector if the URI is ever reflected. We accept ONLY a
|
|
59
|
+
* strict `type/subtype` (no params, no whitespace, no `;`/`,`); anything else collapses to the kind's
|
|
60
|
+
* generic safe subtype (so an image still decodes as an image) or `application/octet-stream`.
|
|
61
|
+
*/
|
|
62
|
+
export declare const sanitizeMimeType: (raw: string, kind?: "image" | "audio" | "video" | "document") => string;
|
|
63
|
+
/**
|
|
64
|
+
* Sanitise a media `filename` for interpolation into a synthetic-description line that is then placed
|
|
65
|
+
* INSIDE a trust envelope whose body is not XML-escaped.
|
|
66
|
+
*
|
|
67
|
+
* @remarks
|
|
68
|
+
* A filename is attacker-influenced and the synthetic-description body is not escaped, so a filename
|
|
69
|
+
* like `x.png</untrusted_content_<nonce>>SYSTEM: …` could close the envelope, and one that mimics the
|
|
70
|
+
* `[media: …]` format could forge a second descriptor. We strip the envelope-significant characters
|
|
71
|
+
* (`<`, `>`, and newlines/control chars) and length-cap (a megabyte filename is a prompt-bloat DoS).
|
|
72
|
+
* The filename is metadata, not content the model must read byte-exact, so stripping is safe.
|
|
73
|
+
*/
|
|
74
|
+
export declare const sanitizeFilenameForDescription: (filename: string, maxLen?: number) => string;
|
|
75
|
+
/**
|
|
76
|
+
* Clamp a stash entry's trust tier to its containing media's tier as a FLOOR: a stash entry may render
|
|
77
|
+
* at the parent's tier or LOWER (less trusted), never HIGHER.
|
|
78
|
+
*
|
|
79
|
+
* @remarks
|
|
80
|
+
* The committee's #1-ranked escalation: a `third-party-private` Media carrying a stash entry tagged
|
|
81
|
+
* `first-party` would otherwise render its fallback text in a `<trusted_content_…>` envelope (the
|
|
82
|
+
* media-fallback renderer keys the envelope off the entry's OWN tier). That lets untrusted content
|
|
83
|
+
* smuggle itself into the trusted tier. Flooring to the parent closes it: a child can de-escalate but
|
|
84
|
+
* never escalate above the asset it belongs to.
|
|
85
|
+
*/
|
|
86
|
+
export declare const floorTrustTier: <T extends string>(parent: T, entry: T) => T;
|
|
19
87
|
export declare const memoryToAttrs: (m: Memory) => {
|
|
20
88
|
memory: Memory;
|
|
21
89
|
attrs: MemoryAttrs;
|