@nhtio/adk 0.1.0-master-e5947c71 → 0.1.0-master-74266e68
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 +124 -1
- package/artifact_methods-BvoDGBnH.cjs +1 -0
- package/artifact_methods-q52ETLwI.mjs +1 -0
- package/batteries/artifacts/ecmascript.cjs +1 -1
- package/batteries/artifacts/ecmascript.mjs +1 -1
- package/batteries/artifacts/toon.cjs +1 -1
- package/batteries/artifacts/toon.mjs +1 -1
- package/batteries/artifacts/xml.cjs +1 -1
- package/batteries/artifacts/xml.mjs +1 -1
- package/batteries/artifacts/yaml.cjs +1 -1
- package/batteries/artifacts/yaml.mjs +1 -1
- package/batteries/artifacts.cjs +4 -4
- package/batteries/artifacts.mjs +4 -4
- package/batteries/dev-tools/forge.cjs +3 -3
- package/batteries/dev-tools/forge.mjs +3 -3
- package/batteries/dev-tools.cjs +2 -2
- package/batteries/dev-tools.mjs +2 -2
- package/batteries/encoding.cjs +7 -7
- package/batteries/encoding.mjs +7 -7
- package/batteries/generation/gemini/adapter.cjs +1 -1
- package/batteries/generation/gemini/adapter.mjs +1 -1
- package/batteries/generation/local_diffusion/adapter.cjs +1 -1
- package/batteries/generation/local_diffusion/adapter.mjs +1 -1
- package/batteries/generation/openai/adapter.cjs +1 -1
- package/batteries/generation/openai/adapter.mjs +1 -1
- package/batteries/llm/anthropic_messages/adapter.cjs +6 -6
- package/batteries/llm/anthropic_messages/adapter.mjs +6 -6
- package/batteries/llm/anthropic_messages/count_tokens.cjs +2 -2
- package/batteries/llm/anthropic_messages/count_tokens.mjs +1 -1
- package/batteries/llm/anthropic_messages/helpers.cjs +3 -3
- package/batteries/llm/anthropic_messages/helpers.mjs +3 -3
- package/batteries/llm/anthropic_messages/validation.cjs +2 -2
- package/batteries/llm/anthropic_messages/validation.mjs +2 -2
- package/batteries/llm/anthropic_messages.cjs +2 -2
- package/batteries/llm/anthropic_messages.mjs +2 -2
- package/batteries/llm/bedrock_converse/adapter.cjs +6 -6
- package/batteries/llm/bedrock_converse/adapter.mjs +6 -6
- package/batteries/llm/bedrock_converse/helpers.cjs +1 -1
- package/batteries/llm/bedrock_converse/helpers.mjs +1 -1
- package/batteries/llm/bedrock_converse/validation.cjs +2 -2
- package/batteries/llm/bedrock_converse/validation.mjs +2 -2
- package/batteries/llm/bedrock_converse.cjs +2 -2
- package/batteries/llm/bedrock_converse.mjs +2 -2
- package/batteries/llm/claude_code_cli/adapter.cjs +7 -7
- package/batteries/llm/claude_code_cli/adapter.mjs +7 -7
- package/batteries/llm/claude_code_cli/helpers.cjs +3 -3
- package/batteries/llm/claude_code_cli/helpers.mjs +3 -3
- package/batteries/llm/claude_code_cli/validation.cjs +2 -2
- package/batteries/llm/claude_code_cli/validation.mjs +2 -2
- package/batteries/llm/claude_code_cli.cjs +2 -2
- package/batteries/llm/claude_code_cli.mjs +2 -2
- package/batteries/llm/gemini_generate_content/adapter.cjs +7 -7
- package/batteries/llm/gemini_generate_content/adapter.mjs +7 -7
- package/batteries/llm/gemini_generate_content/helpers.cjs +1 -1
- package/batteries/llm/gemini_generate_content/helpers.mjs +1 -1
- package/batteries/llm/gemini_generate_content/validation.cjs +2 -2
- package/batteries/llm/gemini_generate_content/validation.mjs +2 -2
- package/batteries/llm/gemini_generate_content.cjs +2 -2
- package/batteries/llm/gemini_generate_content.mjs +2 -2
- package/batteries/llm/litert_lm/adapter.cjs +6 -6
- package/batteries/llm/litert_lm/adapter.mjs +6 -6
- package/batteries/llm/litert_lm/helpers.cjs +3 -3
- package/batteries/llm/litert_lm/helpers.mjs +3 -3
- package/batteries/llm/litert_lm/validation.cjs +2 -2
- package/batteries/llm/litert_lm/validation.mjs +2 -2
- package/batteries/llm/litert_lm.cjs +1 -1
- package/batteries/llm/litert_lm.mjs +1 -1
- package/batteries/llm/ollama/adapter.cjs +6 -6
- package/batteries/llm/ollama/adapter.mjs +6 -6
- package/batteries/llm/ollama/helpers.cjs +4 -4
- package/batteries/llm/ollama/helpers.mjs +4 -4
- package/batteries/llm/ollama/validation.cjs +2 -2
- package/batteries/llm/ollama/validation.mjs +2 -2
- package/batteries/llm/ollama.cjs +2 -2
- package/batteries/llm/ollama.mjs +2 -2
- 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 +4 -4
- package/batteries/llm/openai_chat_completions/helpers.mjs +4 -4
- package/batteries/llm/openai_chat_completions/validation.cjs +2 -2
- package/batteries/llm/openai_chat_completions/validation.mjs +2 -2
- package/batteries/llm/openai_chat_completions.cjs +2 -2
- package/batteries/llm/openai_chat_completions.mjs +2 -2
- package/batteries/llm/openai_responses/adapter.cjs +6 -6
- package/batteries/llm/openai_responses/adapter.mjs +6 -6
- package/batteries/llm/openai_responses/helpers.cjs +3 -3
- package/batteries/llm/openai_responses/helpers.mjs +3 -3
- package/batteries/llm/openai_responses/validation.cjs +2 -2
- package/batteries/llm/openai_responses/validation.mjs +2 -2
- package/batteries/llm/openai_responses.cjs +2 -2
- package/batteries/llm/openai_responses.mjs +2 -2
- package/batteries/llm/transformers_js/adapter.cjs +6 -6
- package/batteries/llm/transformers_js/adapter.mjs +6 -6
- package/batteries/llm/transformers_js/helpers.cjs +3 -3
- package/batteries/llm/transformers_js/helpers.mjs +3 -3
- package/batteries/llm/transformers_js/validation.cjs +2 -2
- package/batteries/llm/transformers_js/validation.mjs +2 -2
- package/batteries/llm/transformers_js.cjs +1 -1
- package/batteries/llm/transformers_js.mjs +1 -1
- package/batteries/llm/webllm_chat_completions/adapter.cjs +6 -6
- package/batteries/llm/webllm_chat_completions/adapter.mjs +6 -6
- 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/validation.cjs +2 -2
- package/batteries/llm/webllm_chat_completions/validation.mjs +2 -2
- package/batteries/llm/webllm_chat_completions.cjs +2 -2
- package/batteries/llm/webllm_chat_completions.mjs +2 -2
- package/batteries/llm.cjs +2 -2
- package/batteries/llm.mjs +2 -2
- package/batteries/media/contracts.cjs.map +1 -1
- package/batteries/media/contracts.d.ts +93 -2
- package/batteries/media/contracts.mjs.map +1 -1
- package/batteries/media/engines/execa_executor.cjs +285 -13
- package/batteries/media/engines/execa_executor.cjs.map +1 -1
- package/batteries/media/engines/execa_executor.d.ts +113 -8
- package/batteries/media/engines/execa_executor.mjs +285 -13
- package/batteries/media/engines/execa_executor.mjs.map +1 -1
- package/batteries/media/forge.cjs +4 -4
- package/batteries/media/forge.mjs +4 -4
- package/batteries/media/lint.cjs +1 -1
- package/batteries/media/lint.mjs +1 -1
- package/batteries/media.cjs +1 -1
- package/batteries/orchestration/dispatch_reasoner.cjs +4 -4
- package/batteries/orchestration/dispatch_reasoner.mjs +4 -4
- package/batteries/orchestration/executor.cjs +2 -2
- package/batteries/orchestration/executor.mjs +2 -2
- package/batteries/orchestration/forge.cjs +3 -3
- package/batteries/orchestration/forge.mjs +3 -3
- package/batteries/orchestration/validation.cjs +2 -2
- package/batteries/orchestration/validation.mjs +2 -2
- package/batteries/orchestration.cjs +3 -3
- package/batteries/orchestration.mjs +3 -3
- package/batteries/sandbox/contracts/policy_enforcer.d.ts +12 -6
- package/batteries/sandbox/executor.d.ts +28 -10
- package/batteries/sandbox/js.cjs +1 -1
- package/batteries/sandbox/js.mjs +1 -1
- package/batteries/sandbox/node/index.d.ts +1 -1
- package/batteries/sandbox/node/srt_enforcer.d.ts +74 -0
- package/batteries/sandbox/node.cjs +98 -5
- package/batteries/sandbox/node.cjs.map +1 -1
- package/batteries/sandbox/node.mjs +98 -6
- package/batteries/sandbox/node.mjs.map +1 -1
- package/batteries/sandbox/tools.cjs +1 -1
- package/batteries/sandbox/tools.mjs +1 -1
- package/batteries/sandbox.cjs +13 -13
- package/batteries/sandbox.cjs.map +1 -1
- package/batteries/sandbox.mjs +13 -13
- package/batteries/sandbox.mjs.map +1 -1
- package/batteries/skills/in_memory.cjs +1 -1
- package/batteries/skills.cjs +10 -10
- package/batteries/skills.mjs +9 -9
- package/batteries/tools/_shared.cjs +1 -1
- package/batteries/tools/_shared.mjs +1 -1
- package/batteries/tools/color.cjs +2 -2
- package/batteries/tools/color.mjs +2 -2
- package/batteries/tools/comparison.cjs +2 -2
- package/batteries/tools/comparison.mjs +2 -2
- package/batteries/tools/data_structure.cjs +2 -2
- package/batteries/tools/data_structure.mjs +2 -2
- 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 +2 -2
- package/batteries/tools/encoding.mjs +2 -2
- package/batteries/tools/formatting.cjs +2 -2
- package/batteries/tools/formatting.mjs +2 -2
- package/batteries/tools/geo_basics.cjs +2 -2
- package/batteries/tools/geo_basics.mjs +2 -2
- package/batteries/tools/math.cjs +2 -2
- package/batteries/tools/math.mjs +2 -2
- package/batteries/tools/memory.cjs +5 -5
- package/batteries/tools/memory.mjs +5 -5
- package/batteries/tools/parsing.cjs +3 -3
- package/batteries/tools/parsing.mjs +3 -3
- 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 +3 -3
- package/batteries/tools/standing_instructions.mjs +3 -3
- package/batteries/tools/statistics.cjs +3 -3
- package/batteries/tools/statistics.mjs +3 -3
- package/batteries/tools/string_processing.cjs +2 -2
- package/batteries/tools/string_processing.mjs +2 -2
- package/batteries/tools/structured_data.cjs +2 -2
- package/batteries/tools/structured_data.mjs +2 -2
- 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.cjs +2 -2
- package/batteries/tools.mjs +2 -2
- package/batteries/validation.cjs +4 -4
- package/batteries/validation.mjs +4 -4
- package/batteries/vector/chroma.cjs +1 -1
- package/batteries/vector/chroma.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/hnswlib.cjs +1 -1
- package/batteries/vector/hnswlib.mjs +1 -1
- package/batteries/vector/in_memory.cjs +1 -1
- package/batteries/vector/in_memory.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/milvus.cjs +1 -1
- package/batteries/vector/milvus.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/orama.cjs +1 -1
- package/batteries/vector/orama.mjs +1 -1
- package/batteries/vector/pgvector.cjs +1 -1
- package/batteries/vector/pgvector.mjs +1 -1
- package/batteries/vector/pinecone.cjs +1 -1
- package/batteries/vector/pinecone.mjs +1 -1
- package/batteries/vector/qdrant.cjs +1 -1
- package/batteries/vector/qdrant.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/sqlite_vec.cjs +1 -1
- package/batteries/vector/sqlite_vec.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/vector.cjs +2 -2
- package/batteries/vector.mjs +2 -2
- package/batteries.cjs +9 -9
- package/batteries.mjs +9 -9
- package/{chat_common-DKw8FDol.mjs → chat_common-CeoHwSsz.mjs} +2 -2
- package/{chat_common-DKw8FDol.mjs.map → chat_common-CeoHwSsz.mjs.map} +1 -1
- package/{chat_common-vSxDBGgr.cjs → chat_common-DlIK1IAn.cjs} +2 -2
- package/{chat_common-vSxDBGgr.cjs.map → chat_common-DlIK1IAn.cjs.map} +1 -1
- package/{common-oI1niK9Z.mjs → common-B5maOoF_.mjs} +7 -7
- package/{common-oI1niK9Z.mjs.map → common-B5maOoF_.mjs.map} +1 -1
- package/{common-BcB05Isq.cjs → common-joo6vLu-.cjs} +7 -7
- package/{common-BcB05Isq.cjs.map → common-joo6vLu-.cjs.map} +1 -1
- package/common.cjs +7 -7
- package/common.mjs +7 -7
- package/{dispatch_runner-CTAI0RSs.cjs → dispatch_runner-BTaUtdtO.cjs} +5 -5
- package/{dispatch_runner-CTAI0RSs.cjs.map → dispatch_runner-BTaUtdtO.cjs.map} +1 -1
- package/{dispatch_runner-BZDXUe0G.mjs → dispatch_runner-DO7jo_gl.mjs} +5 -5
- package/{dispatch_runner-BZDXUe0G.mjs.map → dispatch_runner-DO7jo_gl.mjs.map} +1 -1
- package/dispatch_runner.cjs +1 -1
- package/dispatch_runner.mjs +1 -1
- package/{ecmascript-DOv8gVQ5.cjs → ecmascript-ChvxTCXz.cjs} +3 -3
- package/{ecmascript-DOv8gVQ5.cjs.map → ecmascript-ChvxTCXz.cjs.map} +1 -1
- package/{ecmascript-B9Z2C24U.mjs → ecmascript-bgwVyZG1.mjs} +3 -3
- package/{ecmascript-B9Z2C24U.mjs.map → ecmascript-bgwVyZG1.mjs.map} +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/exceptions.cjs +1 -1
- package/exceptions.mjs +1 -1
- package/forge.cjs +2 -2
- package/forge.mjs +2 -2
- package/guards.cjs +7 -7
- package/guards.mjs +7 -7
- package/{helpers-mbn_EsDV.mjs → helpers-C1T0DV8Q.mjs} +3 -3
- package/{helpers-mbn_EsDV.mjs.map → helpers-C1T0DV8Q.mjs.map} +1 -1
- package/{helpers-CiZnHmxC.cjs → helpers-D6ebO8Vc.cjs} +3 -3
- package/{helpers-CiZnHmxC.cjs.map → helpers-D6ebO8Vc.cjs.map} +1 -1
- package/index.cjs +10 -10
- package/index.mjs +10 -10
- package/{manifest-BPcrFRN4.cjs → manifest-DPYWN15l.cjs} +2 -2
- package/{manifest-BPcrFRN4.cjs.map → manifest-DPYWN15l.cjs.map} +1 -1
- package/mcp/adk-docs-corpus.json +2 -2
- package/package.json +407 -407
- package/{retrievable-CXTAUuuv.mjs → retrievable-CrGKx4sc.mjs} +3 -3
- package/{retrievable-CXTAUuuv.mjs.map → retrievable-CrGKx4sc.mjs.map} +1 -1
- package/{retrievable-BOuQSDHF.cjs → retrievable-DVkAglIv.cjs} +3 -3
- package/{retrievable-BOuQSDHF.cjs.map → retrievable-DVkAglIv.cjs.map} +1 -1
- package/{scrapper-lXnFGDi0.cjs → scrapper-Boc6kq2v.cjs} +4 -4
- package/{scrapper-lXnFGDi0.cjs.map → scrapper-Boc6kq2v.cjs.map} +1 -1
- package/{scrapper-BCdUm8c8.mjs → scrapper-D2Gy9Jhv.mjs} +4 -4
- package/{scrapper-BCdUm8c8.mjs.map → scrapper-D2Gy9Jhv.mjs.map} +1 -1
- package/{searxng-N7qu6dA-.cjs → searxng-DOy3eBDH.cjs} +4 -4
- package/{searxng-N7qu6dA-.cjs.map → searxng-DOy3eBDH.cjs.map} +1 -1
- package/{searxng-B-LdaVUP.mjs → searxng-cCb0lAcN.mjs} +4 -4
- package/{searxng-B-LdaVUP.mjs.map → searxng-cCb0lAcN.mjs.map} +1 -1
- package/server.json +2 -2
- package/skills/adk-assembly/SKILL.md +2 -2
- package/{spooled_artifact-Bw_7swIp.mjs → spooled_artifact-BkOe4-j-.mjs} +731 -731
- package/spooled_artifact-BkOe4-j-.mjs.map +1 -0
- package/{spooled_artifact-DZXsP4s1.cjs → spooled_artifact-BoYsldvO.cjs} +730 -730
- package/spooled_artifact-BoYsldvO.cjs.map +1 -0
- package/spooled_artifact.cjs +2 -2
- package/spooled_artifact.mjs +2 -2
- package/{spooled_markdown_artifact-vd6eNtIP.mjs → spooled_markdown_artifact-Dnl-lCGv.mjs} +3 -3
- package/{spooled_markdown_artifact-vd6eNtIP.mjs.map → spooled_markdown_artifact-Dnl-lCGv.mjs.map} +1 -1
- package/{spooled_markdown_artifact-Bo0u8cRS.cjs → spooled_markdown_artifact-DzbydBel.cjs} +3 -3
- package/{spooled_markdown_artifact-Bo0u8cRS.cjs.map → spooled_markdown_artifact-DzbydBel.cjs.map} +1 -1
- package/{thought-CxSaF6Xf.mjs → thought-BrQt6HAb.mjs} +3 -3
- package/{thought-CxSaF6Xf.mjs.map → thought-BrQt6HAb.mjs.map} +1 -1
- package/{thought-C12mqaKQ.cjs → thought-CXsVh5hS.cjs} +3 -3
- package/{thought-C12mqaKQ.cjs.map → thought-CXsVh5hS.cjs.map} +1 -1
- package/{tokenizable-BcodRtoW.cjs → tokenizable-DDIdXbJN.cjs} +29 -29
- package/{tokenizable-BcodRtoW.cjs.map → tokenizable-DDIdXbJN.cjs.map} +1 -1
- package/{tokenizable-VB4Av8GF.mjs → tokenizable-xPXj1AQX.mjs} +30 -30
- package/{tokenizable-VB4Av8GF.mjs.map → tokenizable-xPXj1AQX.mjs.map} +1 -1
- package/{tool_call-Cfaj-6zK.mjs → tool_call-8crmZsO8.mjs} +4 -4
- package/{tool_call-Cfaj-6zK.mjs.map → tool_call-8crmZsO8.mjs.map} +1 -1
- package/{tool_call-CsBOpKsg.cjs → tool_call-NvQcPf5d.cjs} +4 -4
- package/{tool_call-CsBOpKsg.cjs.map → tool_call-NvQcPf5d.cjs.map} +1 -1
- package/{tools-BjHXlDlV.cjs → tools-BHc8qB5I.cjs} +6 -6
- package/{tools-BjHXlDlV.cjs.map → tools-BHc8qB5I.cjs.map} +1 -1
- package/{tools-Vkfht7mU.mjs → tools-geIlta3C.mjs} +6 -6
- package/{tools-Vkfht7mU.mjs.map → tools-geIlta3C.mjs.map} +1 -1
- package/{toon-BSTQnHed.mjs → toon-BA9Q7NHG.mjs} +5 -5
- package/{toon-BSTQnHed.mjs.map → toon-BA9Q7NHG.mjs.map} +1 -1
- package/{toon-5c9ceKiU.cjs → toon-C8IFssRi.cjs} +5 -5
- package/{toon-5c9ceKiU.cjs.map → toon-C8IFssRi.cjs.map} +1 -1
- package/{turn_runner--tsMLLx4.cjs → turn_runner-A-vpLUvj.cjs} +5 -5
- package/{turn_runner--tsMLLx4.cjs.map → turn_runner-A-vpLUvj.cjs.map} +1 -1
- package/{turn_runner-BHHD6h77.mjs → turn_runner-BmVXcBZ2.mjs} +5 -5
- package/{turn_runner-BHHD6h77.mjs.map → turn_runner-BmVXcBZ2.mjs.map} +1 -1
- package/turn_runner.cjs +1 -1
- package/turn_runner.mjs +1 -1
- package/{xml-CYseDztH.mjs → xml-B1JIGLpU.mjs} +5 -5
- package/{xml-CYseDztH.mjs.map → xml-B1JIGLpU.mjs.map} +1 -1
- package/{xml-DTOnPN52.cjs → xml-Cu7ZvgBQ.cjs} +5 -5
- package/{xml-DTOnPN52.cjs.map → xml-Cu7ZvgBQ.cjs.map} +1 -1
- package/{yaml-BPBoiOjx.mjs → yaml-1mP4uxdf.mjs} +5 -5
- package/{yaml-BPBoiOjx.mjs.map → yaml-1mP4uxdf.mjs.map} +1 -1
- package/{yaml-CMrbNWLc.cjs → yaml-BlB29N68.cjs} +5 -5
- package/{yaml-CMrbNWLc.cjs.map → yaml-BlB29N68.cjs.map} +1 -1
- package/artifact_methods-Cg0go7Q4.mjs +0 -1
- package/artifact_methods-Dtf4-3C-.cjs +0 -1
- package/spooled_artifact-Bw_7swIp.mjs.map +0 -1
- package/spooled_artifact-DZXsP4s1.cjs.map +0 -1
|
@@ -1,530 +1,536 @@
|
|
|
1
1
|
import { a as passesSchema, i as asyncValidateOrThrow, o as validateOrThrow, r as ValidationException } from "./exceptions-DRYLe-J4.mjs";
|
|
2
2
|
import { n as isInstanceOf, r as isObject, t as isError } from "./guards-BEpY7lBI.mjs";
|
|
3
|
-
import {
|
|
3
|
+
import { C as E_INVALID_TOOL_ARGS, F as E_READER_NOT_DESCRIBABLE, L as E_TOOL_ALREADY_REGISTERED, M as E_NO_READER_RESOLVER, R as E_TOOL_DOWNSTREAM_ERROR, c as E_ARTIFACT_ID_COLLISION, h as E_INVALID_INITIAL_REGISTRY_VALUE, j as E_NOT_A_SPOOL_READER, n as TokenEncoding, o as DECODE_METHOD, r as Tokenizable, s as ENCODE_METHOD, y as E_INVALID_INITIAL_TOOL_VALUE } from "./tokenizable-xPXj1AQX.mjs";
|
|
4
|
+
import { DateTime } from "luxon";
|
|
5
|
+
import { sha256 } from "js-sha256";
|
|
4
6
|
import { decode, encode, validator } from "@nhtio/validation";
|
|
5
7
|
import { dset } from "dset";
|
|
6
8
|
import { klona } from "klona";
|
|
7
9
|
import delve from "dlv";
|
|
8
|
-
|
|
9
|
-
import { sha256 } from "js-sha256";
|
|
10
|
-
//#region src/lib/classes/registry.ts
|
|
10
|
+
//#region src/lib/classes/tool_registry.ts
|
|
11
11
|
/**
|
|
12
|
-
* A
|
|
12
|
+
* A mutable, turn-scoped collection of {@link @nhtio/adk!Tool} instances.
|
|
13
13
|
*
|
|
14
14
|
* @remarks
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
15
|
+
* Each `TurnRunner.run()` call constructs a fresh `ToolRegistry` from the runner's configured
|
|
16
|
+
* baseline tools, so middleware edits are isolated to the current turn and cannot bleed across
|
|
17
|
+
* concurrent or subsequent turns.
|
|
18
18
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
19
|
+
* `Tool` instances are immutable, so `all()` returns a fresh array without deep-cloning.
|
|
20
|
+
*
|
|
21
|
+
* `register()` throws {@link @nhtio/adk!E_TOOL_ALREADY_REGISTERED} if a tool with the same name is already
|
|
22
|
+
* present — pass `overwrite: true` to replace it explicitly.
|
|
23
|
+
*
|
|
24
|
+
* Tools can be **hidden** — registered and callable, but excluded from the default tool list
|
|
25
|
+
* rendered to the model. This is useful for discovery patterns where an agent has a tool that
|
|
26
|
+
* enumerates available tools, and the model picks one to call by name in a subsequent iteration.
|
|
27
|
+
* Hidden state is a property of the registry, not the tool: the same tool can be visible in one
|
|
28
|
+
* registry and hidden in another. See {@link hide}, {@link visible}, {@link hidden}.
|
|
21
29
|
*/
|
|
22
|
-
var
|
|
23
|
-
#
|
|
30
|
+
var ToolRegistry = class ToolRegistry {
|
|
31
|
+
#tools;
|
|
32
|
+
#hidden;
|
|
24
33
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
34
|
+
* Returns `true` if `value` is a {@link ToolRegistry} instance.
|
|
35
|
+
*
|
|
36
|
+
* @param value - The value to test.
|
|
37
|
+
* @returns `true` when `value` is a {@link ToolRegistry} instance.
|
|
27
38
|
*/
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
this.#store = initial ? klona(initial) : {};
|
|
39
|
+
static isToolRegistry(value) {
|
|
40
|
+
return isInstanceOf(value, "ToolRegistry", ToolRegistry);
|
|
31
41
|
}
|
|
32
42
|
/**
|
|
33
|
-
*
|
|
43
|
+
* @param tools - Optional initial tools. Insertion order is preserved. Duplicate names throw
|
|
44
|
+
* {@link @nhtio/adk!E_TOOL_ALREADY_REGISTERED} — ensure each tool has a unique name.
|
|
45
|
+
* @throws {@link @nhtio/adk!E_TOOL_ALREADY_REGISTERED} when two tools in `tools` share a name.
|
|
46
|
+
*/
|
|
47
|
+
constructor(tools) {
|
|
48
|
+
this.#tools = /* @__PURE__ */ new Map();
|
|
49
|
+
this.#hidden = /* @__PURE__ */ new Set();
|
|
50
|
+
for (const tool of tools ?? []) this.register(tool);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Adds a tool to the registry.
|
|
54
|
+
*
|
|
55
|
+
* @param tool - The tool to register.
|
|
56
|
+
* @param overwrite - When `true`, silently replaces an existing tool with the same name.
|
|
57
|
+
* Defaults to `false`.
|
|
58
|
+
* @throws {@link @nhtio/adk!E_TOOL_ALREADY_REGISTERED} when a tool with the same name is already registered
|
|
59
|
+
* and `overwrite` is not `true`.
|
|
60
|
+
*/
|
|
61
|
+
register(tool, overwrite) {
|
|
62
|
+
if (this.#tools.has(tool.name) && !overwrite) throw new E_TOOL_ALREADY_REGISTERED();
|
|
63
|
+
this.#tools.set(tool.name, tool);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Removes the tool with the given name from the registry.
|
|
34
67
|
*
|
|
35
68
|
* @remarks
|
|
36
|
-
*
|
|
69
|
+
* Also removes the name from the hidden set if present. No-ops if no tool with that name is
|
|
70
|
+
* registered.
|
|
37
71
|
*
|
|
38
|
-
* @param
|
|
39
|
-
* @returns `true` when `value` is a {@link Registry} instance.
|
|
72
|
+
* @param name - The name of the tool to remove.
|
|
40
73
|
*/
|
|
41
|
-
|
|
42
|
-
|
|
74
|
+
unregister(name) {
|
|
75
|
+
this.#tools.delete(name);
|
|
76
|
+
this.#hidden.delete(name);
|
|
43
77
|
}
|
|
44
78
|
/**
|
|
45
|
-
*
|
|
79
|
+
* Returns the tool registered under `name`, or `undefined` if not present.
|
|
46
80
|
*
|
|
47
|
-
* @
|
|
48
|
-
|
|
81
|
+
* @param name - The tool name to look up.
|
|
82
|
+
*/
|
|
83
|
+
get(name) {
|
|
84
|
+
return this.#tools.get(name);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Returns `true` if a tool with the given name is registered.
|
|
49
88
|
*
|
|
50
|
-
* @
|
|
51
|
-
* @param key - Dot-delimited path into the store (e.g. `"user.name"`).
|
|
52
|
-
* @param defaultValue - Fallback returned when the path resolves to `undefined`.
|
|
53
|
-
* @returns A deep clone of the stored value cast to `T`, or `defaultValue` when the path is absent.
|
|
89
|
+
* @param name - The tool name to test.
|
|
54
90
|
*/
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return "undefined" === typeof value ? defaultValue : value;
|
|
91
|
+
has(name) {
|
|
92
|
+
return this.#tools.has(name);
|
|
58
93
|
}
|
|
59
94
|
/**
|
|
60
|
-
*
|
|
95
|
+
* Returns a fresh array of all registered tools in insertion order.
|
|
61
96
|
*
|
|
62
97
|
* @remarks
|
|
63
|
-
*
|
|
64
|
-
*
|
|
98
|
+
* Includes both visible and hidden tools. Use {@link visible} to get only non-hidden tools, or
|
|
99
|
+
* {@link hidden} to get only hidden tools.
|
|
65
100
|
*
|
|
66
|
-
* @
|
|
67
|
-
* @param value - Value to store at the path.
|
|
101
|
+
* Since {@link @nhtio/adk!Tool} instances are immutable, no deep-cloning is needed.
|
|
68
102
|
*/
|
|
69
|
-
|
|
70
|
-
|
|
103
|
+
all() {
|
|
104
|
+
return Array.from(this.#tools.values());
|
|
71
105
|
}
|
|
72
106
|
/**
|
|
73
|
-
* Returns
|
|
107
|
+
* Returns a fresh array of registered tools that are **not** hidden, in insertion order.
|
|
74
108
|
*
|
|
75
109
|
* @remarks
|
|
76
|
-
*
|
|
77
|
-
*
|
|
110
|
+
* This is the accessor LLM batteries should use when building the tool list for the model.
|
|
111
|
+
* Hidden tools are still callable (they resolve via {@link get}) but are excluded from the
|
|
112
|
+
* rendered tool definitions.
|
|
113
|
+
*/
|
|
114
|
+
visible() {
|
|
115
|
+
return this.all().filter((t) => !this.#hidden.has(t.name));
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Returns a fresh array of registered tools that **are** hidden, in insertion order.
|
|
78
119
|
*
|
|
79
|
-
* @
|
|
80
|
-
*
|
|
120
|
+
* @remarks
|
|
121
|
+
* The converse of {@link visible}. Useful for discovery tools that enumerate all available
|
|
122
|
+
* tools, and for propagating hidden state across {@link merge}.
|
|
81
123
|
*/
|
|
82
|
-
|
|
83
|
-
return
|
|
124
|
+
hidden() {
|
|
125
|
+
return this.all().filter((t) => this.#hidden.has(t.name));
|
|
84
126
|
}
|
|
85
127
|
/**
|
|
86
|
-
*
|
|
128
|
+
* Marks one or more registered tools as hidden.
|
|
87
129
|
*
|
|
88
130
|
* @remarks
|
|
89
|
-
*
|
|
90
|
-
*
|
|
131
|
+
* Hidden tools remain registered and callable via {@link get}, but are excluded from
|
|
132
|
+
* {@link visible} (and therefore from the LLM tool list). No-ops for any name that is not
|
|
133
|
+
* currently registered — the end result (the tool is not visible) matches the intent.
|
|
91
134
|
*
|
|
92
|
-
* @
|
|
135
|
+
* @param names - One or more tool names to hide.
|
|
93
136
|
*/
|
|
94
|
-
|
|
95
|
-
const
|
|
96
|
-
const keys = [];
|
|
97
|
-
const isPlainRecord = (value) => {
|
|
98
|
-
if (!isObject(value)) return false;
|
|
99
|
-
const prototype = Object.getPrototypeOf(value);
|
|
100
|
-
return prototype === Object.prototype || prototype === null;
|
|
101
|
-
};
|
|
102
|
-
const walk = (value, segments) => {
|
|
103
|
-
if (!isPlainRecord(value)) {
|
|
104
|
-
if (segments.length > 0) keys.push(segments.join("."));
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
for (const [segment, child] of Object.entries(value)) walk(child, [...segments, segment]);
|
|
108
|
-
};
|
|
109
|
-
walk(store, []);
|
|
110
|
-
return keys;
|
|
137
|
+
hide(...names) {
|
|
138
|
+
for (const name of names) this.#hidden.add(name);
|
|
111
139
|
}
|
|
112
140
|
/**
|
|
113
|
-
*
|
|
141
|
+
* Unmarks one or more tools as hidden, making them visible again.
|
|
114
142
|
*
|
|
115
|
-
* @
|
|
143
|
+
* @remarks
|
|
144
|
+
* No-ops for any name that is not currently hidden.
|
|
145
|
+
*
|
|
146
|
+
* @param names - One or more tool names to unhide.
|
|
116
147
|
*/
|
|
117
|
-
|
|
118
|
-
|
|
148
|
+
unhide(...names) {
|
|
149
|
+
for (const name of names) this.#hidden.delete(name);
|
|
119
150
|
}
|
|
120
151
|
/**
|
|
121
|
-
*
|
|
152
|
+
* Replaces the entire hidden set with the given tool names.
|
|
122
153
|
*
|
|
123
154
|
* @remarks
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
155
|
+
* Any previously hidden tool not in `names` becomes visible. Names that are not registered are
|
|
156
|
+
* silently ignored — they are added to the set but have no effect until a tool with that name
|
|
157
|
+
* is registered.
|
|
127
158
|
*
|
|
128
|
-
* @
|
|
159
|
+
* @param names - The complete set of tool names to hide.
|
|
129
160
|
*/
|
|
130
|
-
|
|
131
|
-
|
|
161
|
+
setHidden(...names) {
|
|
162
|
+
this.#hidden = new Set(names);
|
|
132
163
|
}
|
|
133
164
|
/**
|
|
134
|
-
*
|
|
165
|
+
* Unhides every tool in the registry.
|
|
166
|
+
*/
|
|
167
|
+
clearHidden() {
|
|
168
|
+
this.#hidden.clear();
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Removes every tool whose {@link @nhtio/adk!Tool.ephemeral} flag is `true`.
|
|
135
172
|
*
|
|
136
|
-
* @
|
|
137
|
-
*
|
|
173
|
+
* @remarks
|
|
174
|
+
* Also removes pruned tool names from the hidden set. Synchronous and idempotent — calling it
|
|
175
|
+
* twice in a row is a no-op the second time. The canonical caller is
|
|
176
|
+
* {@link ToolRegistry.bindContext}, which schedules this method to run at
|
|
177
|
+
* {@link @nhtio/adk!DispatchContext.ack}. Non-ephemeral tools are left untouched.
|
|
138
178
|
*/
|
|
139
|
-
|
|
140
|
-
|
|
179
|
+
pruneEphemeral() {
|
|
180
|
+
for (const [name, tool] of this.#tools) if (tool.ephemeral) {
|
|
181
|
+
this.#tools.delete(name);
|
|
182
|
+
this.#hidden.delete(name);
|
|
183
|
+
}
|
|
141
184
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
* @
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
185
|
+
/**
|
|
186
|
+
* Binds this registry to a {@link @nhtio/adk!DispatchContext} so that {@link pruneEphemeral} runs
|
|
187
|
+
* automatically when the context is acked.
|
|
188
|
+
*
|
|
189
|
+
* @remarks
|
|
190
|
+
* The handler does NOT fire on {@link @nhtio/adk!DispatchContext.nack} — failed executor runs leave
|
|
191
|
+
* any forged tools in place so the consumer can inspect what was registered when debugging the
|
|
192
|
+
* failure. Subscriptions are short-lived and die with the context regardless.
|
|
193
|
+
*
|
|
194
|
+
* ARTIFACT READERS ARE FORGED BY THE CORE. As of the core-forge change, the `DispatchRunner` forges
|
|
195
|
+
* artifact-reader tools from prior-turn `SpooledArtifact` results into `ctx.tools` (and calls
|
|
196
|
+
* `ctx.tools.bindContext(ctx)`) once per iteration, BEFORE the input pipeline — so a battery executor no
|
|
197
|
+
* longer forges or binds; it reads the already-forged `ctx.tools` for both representation (rendering the
|
|
198
|
+
* tool declarations) and resolution (looking up an incoming call by name). The lifecycle is unchanged:
|
|
199
|
+
* ephemeral readers are still pruned on `ack`, and the core re-forges each iteration (prune-then-forge) so
|
|
200
|
+
* the `callId` enum never goes stale.
|
|
201
|
+
*
|
|
202
|
+
* `bindContext` remains the public seam for a CUSTOM consumer that forges its own ephemeral tools outside
|
|
203
|
+
* the core path. If you forge into a long-lived registry yourself, bind it (or prune manually) or the
|
|
204
|
+
* ephemeral tools accumulate and later `forgeTools(ctx)` calls see a stale `callId` enum. The pattern for a
|
|
205
|
+
* hand-rolled forge is:
|
|
206
|
+
*
|
|
207
|
+
* ```ts
|
|
208
|
+
* // Custom forge (the core already does this for artifact readers on ctx.tools):
|
|
209
|
+
* const forged = SpooledArtifact.forgeTools(ctx)
|
|
210
|
+
* for (const tool of forged.all()) ctx.tools.register(tool, true)
|
|
211
|
+
* ctx.tools.bindContext(ctx) // prune the ephemeral readers when the dispatch acks
|
|
212
|
+
* ```
|
|
213
|
+
*
|
|
214
|
+
* @param ctx - The execution context whose `ack` event should trigger pruning.
|
|
215
|
+
* @returns An unsubscribe function — calling it before `ctx.ack()` prevents pruning. Rarely
|
|
216
|
+
* useful outside of tests.
|
|
217
|
+
*
|
|
218
|
+
* @see {@link @nhtio/adk!SpooledArtifact.forgeTools}
|
|
219
|
+
* @see {@link @nhtio/adk!DispatchContext.onAck}
|
|
220
|
+
*/
|
|
221
|
+
bindContext(ctx) {
|
|
222
|
+
return ctx.onAck(() => this.pruneEphemeral());
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Combines multiple {@link ToolRegistry} instances into a fresh registry without mutating any
|
|
226
|
+
* input.
|
|
227
|
+
*
|
|
228
|
+
* @remarks
|
|
229
|
+
* Iteration is left-to-right across `registries` and then in each registry's insertion order.
|
|
230
|
+
* Collisions are resolved by consulting the **incoming** tool's {@link @nhtio/adk!Tool.onCollision} first:
|
|
231
|
+
*
|
|
232
|
+
* - `'replace'` (per-tool): the incoming tool wins, replacing the existing entry.
|
|
233
|
+
* - `'keep'` (per-tool): the existing entry wins; the incoming tool is dropped.
|
|
234
|
+
* - `'throw'` (per-tool, the default): fall back to the merge-level `options.onCollision`.
|
|
235
|
+
*
|
|
236
|
+
* The merge-level `options.onCollision` defaults to `'throw'`, which mirrors {@link register}.
|
|
237
|
+
*
|
|
238
|
+
* The result is a brand-new registry; no input is mutated and no event subscription is
|
|
239
|
+
* propagated. Each `Tool`'s `ephemeral` flag carries through unchanged — the flag lives on the
|
|
240
|
+
* tool, not the registry, so `bindContext(ctx)` on the merged registry will prune the forged
|
|
241
|
+
* tools as expected.
|
|
242
|
+
*
|
|
243
|
+
* Hidden state is also propagated: a tool that is hidden in any source registry remains hidden
|
|
244
|
+
* in the merged result, provided it survives collision resolution.
|
|
245
|
+
*
|
|
246
|
+
* @param registries - Registries to merge, in priority order (left-to-right insertion).
|
|
247
|
+
* @param options - Merge-level collision policy. Defaults to `{ onCollision: 'throw' }`.
|
|
248
|
+
* @returns A fresh {@link ToolRegistry} containing the resolved union of all inputs.
|
|
249
|
+
* @throws {@link @nhtio/adk!E_TOOL_ALREADY_REGISTERED} when the resolved collision policy is `'throw'`
|
|
250
|
+
* and a collision occurs.
|
|
251
|
+
*/
|
|
252
|
+
static merge(registries, options) {
|
|
253
|
+
const policy = options?.onCollision ?? "throw";
|
|
254
|
+
const merged = new ToolRegistry();
|
|
255
|
+
for (const registry of registries) {
|
|
256
|
+
for (const tool of registry.all()) {
|
|
257
|
+
if (!merged.get(tool.name)) {
|
|
258
|
+
merged.register(tool);
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
const incomingPolicy = tool.onCollision;
|
|
262
|
+
if (incomingPolicy === "replace") {
|
|
263
|
+
merged.register(tool, true);
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
if (incomingPolicy === "keep") continue;
|
|
267
|
+
if (policy === "replace") {
|
|
268
|
+
merged.register(tool, true);
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
if (policy === "keep") continue;
|
|
272
|
+
throw new E_TOOL_ALREADY_REGISTERED();
|
|
273
|
+
}
|
|
274
|
+
for (const tool of registry.hidden()) if (merged.has(tool.name)) merged.hide(tool.name);
|
|
275
|
+
}
|
|
276
|
+
return merged;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Serialise this ToolRegistry into an `@nhtio/encoder` snapshot.
|
|
280
|
+
*
|
|
281
|
+
* @remarks
|
|
282
|
+
* Emits the live {@link @nhtio/adk!Tool} instances (the encoder recurses into each — so every tool's
|
|
283
|
+
* handler-closure caveat from {@link Tool.[ENCODE_METHOD]} applies) plus the hidden-set names. Both
|
|
284
|
+
* {@link @nhtio/adk!Tool} and {@link @nhtio/adk!ArtifactTool} entries round-trip to their correct
|
|
285
|
+
* subtype. Round-trips via {@link ToolRegistry.[DECODE_METHOD]}.
|
|
286
|
+
*
|
|
287
|
+
* @returns A `{ tools, hidden }` snapshot.
|
|
288
|
+
*/
|
|
289
|
+
[ENCODE_METHOD]() {
|
|
290
|
+
return {
|
|
291
|
+
tools: this.all(),
|
|
292
|
+
hidden: this.hidden().map((tool) => tool.name)
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Reconstruct a {@link ToolRegistry} from a {@link ToolRegistry.[ENCODE_METHOD]} snapshot.
|
|
297
|
+
*
|
|
298
|
+
* @param data - The snapshot produced by {@link ToolRegistry.[ENCODE_METHOD]}.
|
|
299
|
+
* @returns A fresh {@link ToolRegistry} with the same tools and hidden set.
|
|
300
|
+
*/
|
|
301
|
+
static [DECODE_METHOD](data) {
|
|
302
|
+
const snapshot = data;
|
|
303
|
+
const registry = new ToolRegistry(snapshot.tools);
|
|
304
|
+
registry.setHidden(...snapshot.hidden);
|
|
305
|
+
return registry;
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
//#endregion
|
|
309
|
+
//#region src/lib/utils/canonical_json.ts
|
|
197
310
|
/**
|
|
198
|
-
*
|
|
199
|
-
*
|
|
311
|
+
* Canonical `JSON.stringify` that sorts object keys recursively so that semantically-equal
|
|
312
|
+
* objects produce identical strings.
|
|
200
313
|
*
|
|
201
314
|
* @remarks
|
|
202
|
-
*
|
|
203
|
-
*
|
|
204
|
-
*
|
|
315
|
+
* Used wherever the ADK derives a stable identity from a structured value — for example,
|
|
316
|
+
* `Tool.executor` computing the `callId` for `ToolExecutionStart`/`End` events, and the
|
|
317
|
+
* `reportToolCall` executor helper computing the `checksum` field on `TurnToolCallContent`.
|
|
318
|
+
* Both code paths hash `canonicalStringify({ tool, args })` so that argument key order does not
|
|
319
|
+
* affect the resulting identifier.
|
|
205
320
|
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
*/
|
|
209
|
-
var implementsSpooledArtifactConstructor = (value) => {
|
|
210
|
-
return passesSchema(spooledArtifactConstructorSchema, value);
|
|
211
|
-
};
|
|
212
|
-
/**
|
|
213
|
-
* Creates the validator fragment for a resolver returning a spooled-artifact constructor.
|
|
321
|
+
* Arrays are serialised in their declared order (order is meaningful for an array). Object keys
|
|
322
|
+
* are sorted with `Array.prototype.sort()`'s default lexicographic comparator.
|
|
214
323
|
*
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
* share exactly the same validation behaviour.
|
|
324
|
+
* @param value - The value to serialise.
|
|
325
|
+
* @returns A canonical JSON string representation of `value`.
|
|
218
326
|
*/
|
|
219
|
-
|
|
220
|
-
if (typeof value !== "
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
return helpers.error("any.invalid");
|
|
226
|
-
}
|
|
227
|
-
return implementsSpooledArtifactConstructor(resolved) ? value : helpers.error("any.invalid");
|
|
228
|
-
});
|
|
327
|
+
function canonicalStringify(value) {
|
|
328
|
+
if (value === null || typeof value !== "object") return JSON.stringify(value);
|
|
329
|
+
if (Array.isArray(value)) return "[" + value.map((v) => canonicalStringify(v)).join(",") + "]";
|
|
330
|
+
const obj = value;
|
|
331
|
+
return "{" + Object.keys(obj).sort().map((k) => JSON.stringify(k) + ":" + canonicalStringify(obj[k])).join(",") + "}";
|
|
332
|
+
}
|
|
229
333
|
//#endregion
|
|
230
|
-
//#region src/lib/classes/
|
|
231
|
-
/**
|
|
232
|
-
* Validator schema for a {@link RawTool}.
|
|
233
|
-
*/
|
|
234
|
-
var rawToolSchema = validator.object({
|
|
235
|
-
name: validator.string().required(),
|
|
236
|
-
description: validator.string().required(),
|
|
237
|
-
inputSchema: validator.any().custom((value, helpers) => {
|
|
238
|
-
if (validator.isSchema(value) && value.type === "object") return value;
|
|
239
|
-
return helpers.error("any.invalid");
|
|
240
|
-
}).required(),
|
|
241
|
-
handler: validator.function().required(),
|
|
242
|
-
artifactConstructor: artifactConstructorResolverSchema().optional(),
|
|
243
|
-
meta: validator.object().pattern(validator.string(), validator.any()).default({}),
|
|
244
|
-
ephemeral: validator.boolean().default(false),
|
|
245
|
-
trusted: validator.boolean().default(false),
|
|
246
|
-
onCollision: validator.string().valid("throw", "replace", "keep").default("throw")
|
|
247
|
-
});
|
|
334
|
+
//#region src/lib/classes/registry.ts
|
|
248
335
|
/**
|
|
249
|
-
* A
|
|
250
|
-
* description, input schema, execution handler, and the {@link @nhtio/adk!SpooledArtifact} subclass that
|
|
251
|
-
* wraps its serialised output.
|
|
252
|
-
*
|
|
253
|
-
* @typeParam A - The {@link @nhtio/adk!SpooledArtifact} subtype this tool's results should be wrapped in.
|
|
254
|
-
* Defaults to {@link @nhtio/adk!SpooledArtifact}.
|
|
336
|
+
* A controlled-mutation key-value store with dot-path access and deep-clone isolation.
|
|
255
337
|
*
|
|
256
338
|
* @remarks
|
|
257
|
-
* The
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
* `.description()`, `.note()`, `.example()` etc. once, and that information is available in both
|
|
261
|
-
* contexts without duplication.
|
|
262
|
-
*
|
|
263
|
-
* The handler is private — invoke it only through `executor(ctx)` which validates args, fires
|
|
264
|
-
* observability events (with a stable `callId` derived from the tool name and arguments), and
|
|
265
|
-
* wraps handler errors in {@link @nhtio/adk!E_TOOL_DOWNSTREAM_ERROR}. The handler returns serialised bytes
|
|
266
|
-
* (`string | Uint8Array`); persistence is the consumer's responsibility.
|
|
339
|
+
* The registry enforces a safe read/write contract: callers never hold a live reference into
|
|
340
|
+
* the internal store. Every value that enters (`set`) or leaves (`get`, `all`) is deep-cloned
|
|
341
|
+
* via `klona`, so mutations to a retrieved value cannot affect stored state and vice versa.
|
|
267
342
|
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
* on the tool instance; the consumer reads it when assembling persisted records.
|
|
343
|
+
* Keys are dot-delimited paths (e.g. `"user.profile.name"`), resolved via `dlv` for reads and
|
|
344
|
+
* `dset` for writes; intermediate objects are created automatically on write.
|
|
271
345
|
*/
|
|
272
|
-
var
|
|
346
|
+
var Registry = class Registry {
|
|
347
|
+
#store;
|
|
273
348
|
/**
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
* @remarks
|
|
277
|
-
* Reusable fragment for any schema that needs to validate or nest a tool entry
|
|
278
|
-
* (e.g. `TurnRunnerConfig.tools`).
|
|
349
|
+
* @param initial - Optional plain object to seed the registry. Deep-cloned on construction.
|
|
350
|
+
* @throws {@link @nhtio/adk!E_INVALID_INITIAL_REGISTRY_VALUE} when `initial` is defined but not a plain object.
|
|
279
351
|
*/
|
|
280
|
-
|
|
352
|
+
constructor(initial) {
|
|
353
|
+
if ("undefined" !== typeof initial && !isObject(initial)) throw new E_INVALID_INITIAL_REGISTRY_VALUE();
|
|
354
|
+
this.#store = initial ? klona(initial) : {};
|
|
355
|
+
}
|
|
281
356
|
/**
|
|
282
|
-
* Returns `true` if `value` is a {@link
|
|
357
|
+
* Returns `true` if `value` is a {@link Registry} instance.
|
|
358
|
+
*
|
|
359
|
+
* @remarks
|
|
360
|
+
* Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety.
|
|
283
361
|
*
|
|
284
362
|
* @param value - The value to test.
|
|
285
|
-
* @returns `true` when `value` is a {@link
|
|
363
|
+
* @returns `true` when `value` is a {@link Registry} instance.
|
|
286
364
|
*/
|
|
287
|
-
static
|
|
288
|
-
return isInstanceOf(value, "
|
|
365
|
+
static isRegistry(value) {
|
|
366
|
+
return isInstanceOf(value, "Registry", Registry);
|
|
289
367
|
}
|
|
290
|
-
#name;
|
|
291
|
-
#description;
|
|
292
|
-
#inputSchema;
|
|
293
|
-
#handler;
|
|
294
|
-
#artifactConstructor;
|
|
295
|
-
#meta;
|
|
296
|
-
#ephemeral;
|
|
297
|
-
#trusted;
|
|
298
|
-
#onCollision;
|
|
299
368
|
/**
|
|
300
|
-
*
|
|
301
|
-
*
|
|
369
|
+
* Retrieves the value at `key`, returning `defaultValue` if the path is absent.
|
|
370
|
+
*
|
|
371
|
+
* @remarks
|
|
372
|
+
* The returned value is a deep clone — mutating it will not affect the stored state.
|
|
373
|
+
*
|
|
374
|
+
* @typeParam T - Expected type of the value at `key`.
|
|
375
|
+
* @param key - Dot-delimited path into the store (e.g. `"user.name"`).
|
|
376
|
+
* @param defaultValue - Fallback returned when the path resolves to `undefined`.
|
|
377
|
+
* @returns A deep clone of the stored value cast to `T`, or `defaultValue` when the path is absent.
|
|
302
378
|
*/
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
resolved = validateOrThrow(rawToolSchema, raw, true);
|
|
307
|
-
} catch (err) {
|
|
308
|
-
throw new E_INVALID_INITIAL_TOOL_VALUE({ cause: isError(err) ? err : void 0 });
|
|
309
|
-
}
|
|
310
|
-
this.#name = resolved.name;
|
|
311
|
-
this.#description = resolved.description;
|
|
312
|
-
this.#inputSchema = resolved.inputSchema;
|
|
313
|
-
this.#handler = resolved.handler;
|
|
314
|
-
this.#artifactConstructor = resolved.artifactConstructor;
|
|
315
|
-
this.#meta = new Registry(resolved.meta);
|
|
316
|
-
this.#ephemeral = resolved.ephemeral;
|
|
317
|
-
this.#trusted = resolved.trusted;
|
|
318
|
-
this.#onCollision = resolved.onCollision;
|
|
319
|
-
Object.defineProperties(this, {
|
|
320
|
-
name: {
|
|
321
|
-
get: () => this.#name,
|
|
322
|
-
enumerable: true,
|
|
323
|
-
configurable: false
|
|
324
|
-
},
|
|
325
|
-
description: {
|
|
326
|
-
get: () => this.#description,
|
|
327
|
-
enumerable: true,
|
|
328
|
-
configurable: false
|
|
329
|
-
},
|
|
330
|
-
inputSchema: {
|
|
331
|
-
get: () => this.#inputSchema,
|
|
332
|
-
enumerable: true,
|
|
333
|
-
configurable: false
|
|
334
|
-
},
|
|
335
|
-
artifactConstructor: {
|
|
336
|
-
get: () => this.#artifactConstructor,
|
|
337
|
-
enumerable: true,
|
|
338
|
-
configurable: false
|
|
339
|
-
},
|
|
340
|
-
meta: {
|
|
341
|
-
get: () => this.#meta,
|
|
342
|
-
enumerable: true,
|
|
343
|
-
configurable: false
|
|
344
|
-
},
|
|
345
|
-
ephemeral: {
|
|
346
|
-
get: () => this.#ephemeral,
|
|
347
|
-
enumerable: true,
|
|
348
|
-
configurable: false
|
|
349
|
-
},
|
|
350
|
-
trusted: {
|
|
351
|
-
get: () => this.#trusted,
|
|
352
|
-
enumerable: true,
|
|
353
|
-
configurable: false
|
|
354
|
-
},
|
|
355
|
-
onCollision: {
|
|
356
|
-
get: () => this.#onCollision,
|
|
357
|
-
enumerable: true,
|
|
358
|
-
configurable: false
|
|
359
|
-
}
|
|
360
|
-
});
|
|
379
|
+
get(key, defaultValue) {
|
|
380
|
+
const value = delve(klona(this.#store), key);
|
|
381
|
+
return "undefined" === typeof value ? defaultValue : value;
|
|
361
382
|
}
|
|
362
383
|
/**
|
|
363
|
-
*
|
|
384
|
+
* Sets the value at `key`, creating intermediate objects as needed.
|
|
364
385
|
*
|
|
365
386
|
* @remarks
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
* in the tool call loop, not a downstream failure.
|
|
387
|
+
* The stored value is isolated from the caller — mutating `value` after this call will not
|
|
388
|
+
* affect what is held in the registry.
|
|
369
389
|
*
|
|
370
|
-
* @param
|
|
371
|
-
* @
|
|
372
|
-
* @throws {@link @nhtio/adk!E_INVALID_TOOL_ARGS} when `args` does not satisfy the input schema.
|
|
390
|
+
* @param key - Dot-delimited path into the store (e.g. `"user.name"`).
|
|
391
|
+
* @param value - Value to store at the path.
|
|
373
392
|
*/
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
return await asyncValidateOrThrow(this.#inputSchema, args);
|
|
377
|
-
} catch (err) {
|
|
378
|
-
if (isInstanceOf(err, "ValidationException", ValidationException)) throw new E_INVALID_TOOL_ARGS({ cause: err });
|
|
379
|
-
throw err;
|
|
380
|
-
}
|
|
393
|
+
set(key, value) {
|
|
394
|
+
dset(this.#store, key, value);
|
|
381
395
|
}
|
|
382
396
|
/**
|
|
383
|
-
* Returns
|
|
397
|
+
* Returns `true` if the registry has a value at `key`, `false` otherwise.
|
|
384
398
|
*
|
|
385
399
|
* @remarks
|
|
386
|
-
*
|
|
387
|
-
*
|
|
388
|
-
* (3) emits `toolExecutionStart` on the context (with the computed `callId`), (4) calls the
|
|
389
|
-
* handler, (5) emits `toolExecutionEnd` (with the same `callId`), (6) wraps any handler error
|
|
390
|
-
* in {@link @nhtio/adk!E_TOOL_DOWNSTREAM_ERROR} before re-throwing.
|
|
400
|
+
* A key resolving to `undefined` is treated as absent — same convention as {@link Registry.get}'s
|
|
401
|
+
* `defaultValue` fallback. No clone is performed; this is a pure existence check.
|
|
391
402
|
*
|
|
392
|
-
*
|
|
393
|
-
*
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
403
|
+
* @param key - Dot-delimited path into the store (e.g. `"user.name"`).
|
|
404
|
+
* @returns `true` when the path resolves to a value other than `undefined`.
|
|
405
|
+
*/
|
|
406
|
+
has(key) {
|
|
407
|
+
return "undefined" !== typeof delve(this.#store, key);
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Returns all leaf dot-paths present in the registry.
|
|
398
411
|
*
|
|
399
|
-
*
|
|
412
|
+
* @remarks
|
|
413
|
+
* The store is deep-cloned before traversal. Plain objects are walked recursively with path
|
|
414
|
+
* segments joined by dots; arrays, primitives, `null`, and class instances are treated as leaves.
|
|
400
415
|
*
|
|
401
|
-
* @
|
|
402
|
-
* @returns An async function `(args) => Promise<string | Uint8Array | SpooledArtifact | Media | Media[]>`.
|
|
403
|
-
* A `SpooledArtifact` is a result the HANDLER built (streamed to storage and wrapped itself); a
|
|
404
|
-
* wrap-site passes it through unwrapped rather than re-spooling it. `string`/`Uint8Array` are the
|
|
405
|
-
* opposite case — bytes the consumer must still store and wrap.
|
|
416
|
+
* @returns A string array of dot-delimited paths to leaf values in the store.
|
|
406
417
|
*/
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
args: validatedArgs,
|
|
420
|
-
startedAt
|
|
421
|
-
});
|
|
422
|
-
try {
|
|
423
|
-
const result = await this.#handler(validatedArgs, ctx, this.#meta);
|
|
424
|
-
const endedAt = DateTime.now();
|
|
425
|
-
ctx.emitToolExecutionEnd({
|
|
426
|
-
toolName: this.#name,
|
|
427
|
-
turnId: ctx.id,
|
|
428
|
-
callId,
|
|
429
|
-
startedAt,
|
|
430
|
-
endedAt,
|
|
431
|
-
durationMs: endedAt.diff(startedAt).milliseconds,
|
|
432
|
-
isError: false
|
|
433
|
-
});
|
|
434
|
-
return result;
|
|
435
|
-
} catch (err) {
|
|
436
|
-
const endedAt = DateTime.now();
|
|
437
|
-
ctx.emitToolExecutionEnd({
|
|
438
|
-
toolName: this.#name,
|
|
439
|
-
turnId: ctx.id,
|
|
440
|
-
callId,
|
|
441
|
-
startedAt,
|
|
442
|
-
endedAt,
|
|
443
|
-
durationMs: endedAt.diff(startedAt).milliseconds,
|
|
444
|
-
isError: true
|
|
445
|
-
});
|
|
446
|
-
throw new E_TOOL_DOWNSTREAM_ERROR({ cause: isError(err) ? err : void 0 });
|
|
418
|
+
keys() {
|
|
419
|
+
const store = klona(this.#store);
|
|
420
|
+
const keys = [];
|
|
421
|
+
const isPlainRecord = (value) => {
|
|
422
|
+
if (!isObject(value)) return false;
|
|
423
|
+
const prototype = Object.getPrototypeOf(value);
|
|
424
|
+
return prototype === Object.prototype || prototype === null;
|
|
425
|
+
};
|
|
426
|
+
const walk = (value, segments) => {
|
|
427
|
+
if (!isPlainRecord(value)) {
|
|
428
|
+
if (segments.length > 0) keys.push(segments.join("."));
|
|
429
|
+
return;
|
|
447
430
|
}
|
|
431
|
+
for (const [segment, child] of Object.entries(value)) walk(child, [...segments, segment]);
|
|
448
432
|
};
|
|
433
|
+
walk(store, []);
|
|
434
|
+
return keys;
|
|
449
435
|
}
|
|
450
436
|
/**
|
|
451
|
-
* Returns a
|
|
452
|
-
*
|
|
453
|
-
* @remarks
|
|
454
|
-
* The `inputSchema` property is the result of calling `.describe()` on the raw schema — a plain
|
|
455
|
-
* object carrying all the annotation metadata (descriptions, notes, examples, types) without any
|
|
456
|
-
* validator functions. Use this to build provider-specific LLM tool definitions.
|
|
437
|
+
* Returns a deep clone of the entire store contents.
|
|
457
438
|
*
|
|
458
|
-
* @returns
|
|
439
|
+
* @returns A plain object snapshot of all stored key-value pairs.
|
|
459
440
|
*/
|
|
460
|
-
|
|
461
|
-
return
|
|
462
|
-
name: this.#name,
|
|
463
|
-
description: this.#description,
|
|
464
|
-
inputSchema: this.#inputSchema.describe()
|
|
465
|
-
};
|
|
441
|
+
all() {
|
|
442
|
+
return klona(this.#store);
|
|
466
443
|
}
|
|
467
444
|
/**
|
|
468
|
-
* Serialise this
|
|
445
|
+
* Serialise this Registry into an `@nhtio/encoder` snapshot.
|
|
469
446
|
*
|
|
470
447
|
* @remarks
|
|
471
|
-
*
|
|
472
|
-
*
|
|
473
|
-
*
|
|
474
|
-
* - **`inputSchema`** — delegated to `@nhtio/validation`'s `encode()`, which captures the full schema
|
|
475
|
-
* description plus its library version into a compact string; decode rebuilds the live `Schema`.
|
|
476
|
-
* - **`handler` / `artifactConstructor`** — emitted as **functions**. The encoder's `FunctionSerializer`
|
|
477
|
-
* serialises them by **source text** (`fn.toString()`). This is the sharp edge: a handler that
|
|
478
|
-
* closes over `ctx`, service clients, or config loses those bindings on decode — only the source
|
|
479
|
-
* survives, and the captured variables read back as `undefined`. A `.bind()`-ed or native handler
|
|
480
|
-
* cannot be serialised at all and the encoder throws. Tools are rarely serialised in practice; when
|
|
481
|
-
* they are, write handlers that reconstruct their dependencies inside the body rather than closing
|
|
482
|
-
* over them.
|
|
448
|
+
* The snapshot is a deep clone of the store ({@link Registry.all}). Leaf values that are themselves
|
|
449
|
+
* registered encodable instances round-trip; anything the encoder cannot serialise throws at encode
|
|
450
|
+
* time (standard encoder behaviour). Round-trips via {@link Registry.[DECODE_METHOD]}.
|
|
483
451
|
*
|
|
484
|
-
* @returns A
|
|
452
|
+
* @returns A deep-cloned plain-object snapshot of the store.
|
|
485
453
|
*/
|
|
486
454
|
[ENCODE_METHOD]() {
|
|
487
|
-
return
|
|
488
|
-
name: this.#name,
|
|
489
|
-
description: this.#description,
|
|
490
|
-
inputSchema: encode(this.#inputSchema),
|
|
491
|
-
handler: this.#handler,
|
|
492
|
-
artifactConstructor: this.#artifactConstructor,
|
|
493
|
-
meta: this.#meta.all(),
|
|
494
|
-
ephemeral: this.#ephemeral,
|
|
495
|
-
trusted: this.#trusted,
|
|
496
|
-
onCollision: this.#onCollision
|
|
497
|
-
};
|
|
455
|
+
return this.all();
|
|
498
456
|
}
|
|
499
457
|
/**
|
|
500
|
-
* Reconstruct a {@link
|
|
501
|
-
*
|
|
502
|
-
* @remarks
|
|
503
|
-
* Rebuilds the live `Schema` via `@nhtio/validation`'s `decode()`, then re-validates through the
|
|
504
|
-
* constructor. The rehydrated `handler` carries only its source text — see
|
|
505
|
-
* {@link Tool.[ENCODE_METHOD]} for the closure caveat.
|
|
458
|
+
* Reconstruct a {@link Registry} from a {@link Registry.[ENCODE_METHOD]} snapshot.
|
|
506
459
|
*
|
|
507
|
-
* @param data - The snapshot produced by {@link
|
|
508
|
-
* @returns A
|
|
460
|
+
* @param data - The store snapshot produced by {@link Registry.[ENCODE_METHOD]}.
|
|
461
|
+
* @returns A fresh {@link Registry} seeded with the snapshot.
|
|
509
462
|
*/
|
|
510
463
|
static [DECODE_METHOD](data) {
|
|
511
|
-
|
|
512
|
-
return new Tool({
|
|
513
|
-
...snapshot,
|
|
514
|
-
inputSchema: decode(snapshot.inputSchema)
|
|
515
|
-
});
|
|
464
|
+
return new Registry(data);
|
|
516
465
|
}
|
|
517
466
|
};
|
|
518
467
|
//#endregion
|
|
519
|
-
//#region src/lib/
|
|
468
|
+
//#region src/lib/contracts/spooled_artifact_constructor.ts
|
|
469
|
+
var ARTIFACT_METHODS = [
|
|
470
|
+
"head",
|
|
471
|
+
"tail",
|
|
472
|
+
"grep",
|
|
473
|
+
"cat",
|
|
474
|
+
"byteLength",
|
|
475
|
+
"lineCount",
|
|
476
|
+
"estimateTokens"
|
|
477
|
+
];
|
|
520
478
|
/**
|
|
521
|
-
* Validator schema
|
|
479
|
+
* Validator schema used to validate a {@link SpooledArtifactConstructorLike} value.
|
|
522
480
|
*
|
|
523
481
|
* @remarks
|
|
524
|
-
*
|
|
525
|
-
*
|
|
482
|
+
* Because the validator is invoked at validate-time (not at module-load), it is safe to inspect
|
|
483
|
+
* the constructor's prototype here. The check is duck-typed: the value must be a function whose
|
|
484
|
+
* `prototype` carries every canonical artifact instance method (`head`, `tail`, `grep`, `cat`,
|
|
485
|
+
* `byteLength`, `lineCount`, `estimateTokens`). This mirrors {@link spoolReaderSchema}'s
|
|
486
|
+
* cross-realm-safe duck-type pattern — `instanceof SpooledArtifact` would be tighter but would
|
|
487
|
+
* force a value-import of the class and reopen the module cycle.
|
|
526
488
|
*/
|
|
527
|
-
var
|
|
489
|
+
var spooledArtifactConstructorSchema = validator.any().required().custom((value, helpers) => {
|
|
490
|
+
if (typeof value !== "function") return helpers.error("any.invalid");
|
|
491
|
+
const proto = value.prototype;
|
|
492
|
+
if (proto === void 0 || proto === null) return helpers.error("any.invalid");
|
|
493
|
+
if (ARTIFACT_METHODS.every((m) => typeof proto[m] === "function")) return value;
|
|
494
|
+
return helpers.error("any.invalid");
|
|
495
|
+
});
|
|
496
|
+
/**
|
|
497
|
+
* Returns `true` if `value` is a constructor whose prototype carries every canonical
|
|
498
|
+
* {@link @nhtio/adk!SpooledArtifact} instance method.
|
|
499
|
+
*
|
|
500
|
+
* @remarks
|
|
501
|
+
* Duck-typed; does not use `instanceof SpooledArtifact`. Used by the `Tool.artifactConstructor`
|
|
502
|
+
* resolver validator and any other site that needs to recognise a `SpooledArtifact`-like
|
|
503
|
+
* constructor without pulling the class into its module-load graph.
|
|
504
|
+
*
|
|
505
|
+
* @param value - The value to test.
|
|
506
|
+
* @returns `true` when `value` is a `SpooledArtifact`-shaped constructor.
|
|
507
|
+
*/
|
|
508
|
+
var implementsSpooledArtifactConstructor = (value) => {
|
|
509
|
+
return passesSchema(spooledArtifactConstructorSchema, value);
|
|
510
|
+
};
|
|
511
|
+
/**
|
|
512
|
+
* Creates the validator fragment for a resolver returning a spooled-artifact constructor.
|
|
513
|
+
*
|
|
514
|
+
* The resolver is invoked during validation, then its result is checked with the canonical
|
|
515
|
+
* cross-realm-safe constructor guard. Keeping this fragment here makes `Tool` and `Retrievable`
|
|
516
|
+
* share exactly the same validation behaviour.
|
|
517
|
+
*/
|
|
518
|
+
var artifactConstructorResolverSchema = () => validator.any().custom((value, helpers) => {
|
|
519
|
+
if (typeof value !== "function") return helpers.error("any.invalid");
|
|
520
|
+
let resolved;
|
|
521
|
+
try {
|
|
522
|
+
resolved = value();
|
|
523
|
+
} catch {
|
|
524
|
+
return helpers.error("any.invalid");
|
|
525
|
+
}
|
|
526
|
+
return implementsSpooledArtifactConstructor(resolved) ? value : helpers.error("any.invalid");
|
|
527
|
+
});
|
|
528
|
+
//#endregion
|
|
529
|
+
//#region src/lib/classes/tool.ts
|
|
530
|
+
/**
|
|
531
|
+
* Validator schema for a {@link RawTool}.
|
|
532
|
+
*/
|
|
533
|
+
var rawToolSchema = validator.object({
|
|
528
534
|
name: validator.string().required(),
|
|
529
535
|
description: validator.string().required(),
|
|
530
536
|
inputSchema: validator.any().custom((value, helpers) => {
|
|
@@ -532,404 +538,398 @@ var rawArtifactToolSchema = validator.object({
|
|
|
532
538
|
return helpers.error("any.invalid");
|
|
533
539
|
}).required(),
|
|
534
540
|
handler: validator.function().required(),
|
|
541
|
+
artifactConstructor: artifactConstructorResolverSchema().optional(),
|
|
535
542
|
meta: validator.object().pattern(validator.string(), validator.any()).default({}),
|
|
536
543
|
ephemeral: validator.boolean().default(false),
|
|
537
544
|
trusted: validator.boolean().default(false),
|
|
538
|
-
onCollision: validator.string().valid("throw", "replace", "keep").default("throw")
|
|
539
|
-
artifactConstructor: validator.any().forbidden()
|
|
545
|
+
onCollision: validator.string().valid("throw", "replace", "keep").default("throw")
|
|
540
546
|
});
|
|
541
547
|
/**
|
|
542
|
-
* A
|
|
543
|
-
*
|
|
544
|
-
*
|
|
545
|
-
*
|
|
546
|
-
* @remarks
|
|
547
|
-
* `ArtifactTool` is the canonical producer for **forged artifact-query tools** — the tools
|
|
548
|
-
* `SpooledArtifact.forgeTools(ctx)` emits so the model can `head`, `tail`, `grep`, `json_get`,
|
|
549
|
-
* `md_headings` (etc.) an artifact that is already in `ctx.turnToolCalls`.
|
|
550
|
-
*
|
|
551
|
-
* The difference from {@link @nhtio/adk!Tool} is structural, not stylistic:
|
|
552
|
-
*
|
|
553
|
-
* - A normal `Tool`'s handler returns bytes the ADK wraps in a fresh `SpooledArtifact`.
|
|
554
|
-
* The artifact lands in `ToolCall.results`, joins `ctx.turnToolCalls`, and is itself a
|
|
555
|
-
* first-class queryable artifact in the turn.
|
|
556
|
-
* - An `ArtifactTool`'s handler returns a string that is **already the model-visible answer**
|
|
557
|
-
* to a query against an existing artifact. The ADK wraps it in a `Tokenizable` rather
|
|
558
|
-
* than a `SpooledArtifact`; nothing new is queryable on its own. Subsequent
|
|
559
|
-
* `forgeTools(ctx)` calls exclude `ToolCall`s produced by an `ArtifactTool` from the
|
|
560
|
-
* `callId` enum (via the `ToolCall.fromArtifactTool` marker) — this is the structural fix
|
|
561
|
-
* that breaks the otherwise-recursive grep-on-the-grep-result loop.
|
|
548
|
+
* A tool definition that serves as the single source of truth for a callable tool: its name,
|
|
549
|
+
* description, input schema, execution handler, and the {@link @nhtio/adk!SpooledArtifact} subclass that
|
|
550
|
+
* wraps its serialised output.
|
|
562
551
|
*
|
|
563
|
-
*
|
|
564
|
-
*
|
|
565
|
-
*/
|
|
566
|
-
var ArtifactTool = class ArtifactTool extends Tool {
|
|
567
|
-
/**
|
|
568
|
-
* Validator schema that accepts a {@link RawArtifactTool} object.
|
|
569
|
-
*
|
|
570
|
-
* @remarks
|
|
571
|
-
* Differs from {@link @nhtio/adk!Tool.schema} by forbidding `artifactConstructor` — wrapping is
|
|
572
|
-
* exactly the thing this class opts out of. Typed identically to {@link @nhtio/adk!Tool.schema} so the
|
|
573
|
-
* subclass relationship `class ArtifactTool extends Tool` remains structurally sound; the
|
|
574
|
-
* runtime validation rules still differ as declared by `rawArtifactToolSchema`.
|
|
575
|
-
*/
|
|
576
|
-
static schema = rawArtifactToolSchema;
|
|
577
|
-
/**
|
|
578
|
-
* Returns `true` if `value` is an {@link ArtifactTool} instance.
|
|
579
|
-
*
|
|
580
|
-
* @remarks
|
|
581
|
-
* Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety — `instanceof` would fail for instances
|
|
582
|
-
* created in a different module copy or VM context.
|
|
583
|
-
*
|
|
584
|
-
* @param value - The value to test.
|
|
585
|
-
* @returns `true` when `value` is an {@link ArtifactTool} instance.
|
|
586
|
-
*/
|
|
587
|
-
static isArtifactTool(value) {
|
|
588
|
-
return isInstanceOf(value, "ArtifactTool", ArtifactTool);
|
|
589
|
-
}
|
|
590
|
-
/**
|
|
591
|
-
* @param raw - Raw tool input validated against {@link ArtifactTool.schema}.
|
|
592
|
-
*
|
|
593
|
-
* @throws {@link @nhtio/adk!E_INVALID_INITIAL_TOOL_VALUE} when `raw` does not satisfy
|
|
594
|
-
* {@link ArtifactTool.schema} (most commonly, when `artifactConstructor` is supplied — it is
|
|
595
|
-
* explicitly forbidden on this class) or when the base {@link @nhtio/adk!Tool} constructor rejects the
|
|
596
|
-
* input for any reason.
|
|
597
|
-
*/
|
|
598
|
-
constructor(raw) {
|
|
599
|
-
try {
|
|
600
|
-
validateOrThrow(rawArtifactToolSchema, raw, true);
|
|
601
|
-
} catch (err) {
|
|
602
|
-
if (isInstanceOf(err, "ValidationException", ValidationException)) throw new E_INVALID_INITIAL_TOOL_VALUE({ cause: err });
|
|
603
|
-
throw err;
|
|
604
|
-
}
|
|
605
|
-
super(raw);
|
|
606
|
-
}
|
|
607
|
-
/**
|
|
608
|
-
* Serialise this ArtifactTool into an `@nhtio/encoder` snapshot.
|
|
609
|
-
*
|
|
610
|
-
* @remarks
|
|
611
|
-
* Reuses the base {@link Tool.[ENCODE_METHOD]} (function-serialised `handler`, validation-encoded
|
|
612
|
-
* `inputSchema`, plain config) and strips `artifactConstructor` — which {@link ArtifactTool.schema}
|
|
613
|
-
* forbids. The handler's closure caveat from {@link Tool.[ENCODE_METHOD]} applies here too.
|
|
614
|
-
*
|
|
615
|
-
* @returns A {@link RawArtifactTool}-shaped snapshot (with `inputSchema` as an encoded string).
|
|
616
|
-
*/
|
|
617
|
-
[ENCODE_METHOD]() {
|
|
618
|
-
const base = super[ENCODE_METHOD]();
|
|
619
|
-
delete base.artifactConstructor;
|
|
620
|
-
return base;
|
|
621
|
-
}
|
|
622
|
-
/**
|
|
623
|
-
* Reconstruct an {@link ArtifactTool} from an {@link ArtifactTool.[ENCODE_METHOD]} snapshot.
|
|
624
|
-
*
|
|
625
|
-
* @param data - The snapshot produced by {@link ArtifactTool.[ENCODE_METHOD]}.
|
|
626
|
-
* @returns A fully-validated {@link ArtifactTool}.
|
|
627
|
-
*/
|
|
628
|
-
static [DECODE_METHOD](data) {
|
|
629
|
-
const snapshot = data;
|
|
630
|
-
return new ArtifactTool({
|
|
631
|
-
...snapshot,
|
|
632
|
-
inputSchema: decode(snapshot.inputSchema)
|
|
633
|
-
});
|
|
634
|
-
}
|
|
635
|
-
};
|
|
636
|
-
//#endregion
|
|
637
|
-
//#region src/lib/classes/tool_registry.ts
|
|
638
|
-
/**
|
|
639
|
-
* A mutable, turn-scoped collection of {@link @nhtio/adk!Tool} instances.
|
|
552
|
+
* @typeParam A - The {@link @nhtio/adk!SpooledArtifact} subtype this tool's results should be wrapped in.
|
|
553
|
+
* Defaults to {@link @nhtio/adk!SpooledArtifact}.
|
|
640
554
|
*
|
|
641
555
|
* @remarks
|
|
642
|
-
*
|
|
643
|
-
*
|
|
644
|
-
*
|
|
645
|
-
*
|
|
646
|
-
*
|
|
556
|
+
* The `inputSchema` is a `@nhtio/validation` schema. It is used at runtime to validate incoming
|
|
557
|
+
* arguments before the handler is called, and its `.describe()` output provides all the metadata
|
|
558
|
+
* needed to build a provider-specific LLM tool definition — annotate the schema with
|
|
559
|
+
* `.description()`, `.note()`, `.example()` etc. once, and that information is available in both
|
|
560
|
+
* contexts without duplication.
|
|
647
561
|
*
|
|
648
|
-
*
|
|
649
|
-
*
|
|
562
|
+
* The handler is private — invoke it only through `executor(ctx)` which validates args, fires
|
|
563
|
+
* observability events (with a stable `callId` derived from the tool name and arguments), and
|
|
564
|
+
* wraps handler errors in {@link @nhtio/adk!E_TOOL_DOWNSTREAM_ERROR}. The handler returns serialised bytes
|
|
565
|
+
* (`string | Uint8Array`); persistence is the consumer's responsibility.
|
|
650
566
|
*
|
|
651
|
-
*
|
|
652
|
-
*
|
|
653
|
-
*
|
|
654
|
-
* Hidden state is a property of the registry, not the tool: the same tool can be visible in one
|
|
655
|
-
* registry and hidden in another. See {@link hide}, {@link visible}, {@link hidden}.
|
|
567
|
+
* `artifactConstructor` is the {@link @nhtio/adk!SpooledArtifact} subclass the consumer should use when
|
|
568
|
+
* wrapping the handler's output into a `ToolCall.results` field. The author declares it once
|
|
569
|
+
* on the tool instance; the consumer reads it when assembling persisted records.
|
|
656
570
|
*/
|
|
657
|
-
var
|
|
658
|
-
#tools;
|
|
659
|
-
#hidden;
|
|
660
|
-
/**
|
|
661
|
-
* Returns `true` if `value` is a {@link ToolRegistry} instance.
|
|
662
|
-
*
|
|
663
|
-
* @param value - The value to test.
|
|
664
|
-
* @returns `true` when `value` is a {@link ToolRegistry} instance.
|
|
665
|
-
*/
|
|
666
|
-
static isToolRegistry(value) {
|
|
667
|
-
return isInstanceOf(value, "ToolRegistry", ToolRegistry);
|
|
668
|
-
}
|
|
669
|
-
/**
|
|
670
|
-
* @param tools - Optional initial tools. Insertion order is preserved. Duplicate names throw
|
|
671
|
-
* {@link @nhtio/adk!E_TOOL_ALREADY_REGISTERED} — ensure each tool has a unique name.
|
|
672
|
-
* @throws {@link @nhtio/adk!E_TOOL_ALREADY_REGISTERED} when two tools in `tools` share a name.
|
|
673
|
-
*/
|
|
674
|
-
constructor(tools) {
|
|
675
|
-
this.#tools = /* @__PURE__ */ new Map();
|
|
676
|
-
this.#hidden = /* @__PURE__ */ new Set();
|
|
677
|
-
for (const tool of tools ?? []) this.register(tool);
|
|
678
|
-
}
|
|
679
|
-
/**
|
|
680
|
-
* Adds a tool to the registry.
|
|
681
|
-
*
|
|
682
|
-
* @param tool - The tool to register.
|
|
683
|
-
* @param overwrite - When `true`, silently replaces an existing tool with the same name.
|
|
684
|
-
* Defaults to `false`.
|
|
685
|
-
* @throws {@link @nhtio/adk!E_TOOL_ALREADY_REGISTERED} when a tool with the same name is already registered
|
|
686
|
-
* and `overwrite` is not `true`.
|
|
687
|
-
*/
|
|
688
|
-
register(tool, overwrite) {
|
|
689
|
-
if (this.#tools.has(tool.name) && !overwrite) throw new E_TOOL_ALREADY_REGISTERED();
|
|
690
|
-
this.#tools.set(tool.name, tool);
|
|
691
|
-
}
|
|
571
|
+
var Tool = class Tool {
|
|
692
572
|
/**
|
|
693
|
-
*
|
|
573
|
+
* Validator schema that accepts a {@link RawTool} object.
|
|
694
574
|
*
|
|
695
575
|
* @remarks
|
|
696
|
-
*
|
|
697
|
-
*
|
|
698
|
-
*
|
|
699
|
-
* @param name - The name of the tool to remove.
|
|
576
|
+
* Reusable fragment for any schema that needs to validate or nest a tool entry
|
|
577
|
+
* (e.g. `TurnRunnerConfig.tools`).
|
|
700
578
|
*/
|
|
701
|
-
|
|
702
|
-
this.#tools.delete(name);
|
|
703
|
-
this.#hidden.delete(name);
|
|
704
|
-
}
|
|
579
|
+
static schema = rawToolSchema;
|
|
705
580
|
/**
|
|
706
|
-
* Returns
|
|
581
|
+
* Returns `true` if `value` is a {@link Tool} instance.
|
|
707
582
|
*
|
|
708
|
-
* @param
|
|
583
|
+
* @param value - The value to test.
|
|
584
|
+
* @returns `true` when `value` is a {@link Tool} instance.
|
|
709
585
|
*/
|
|
710
|
-
|
|
711
|
-
return
|
|
586
|
+
static isTool(value) {
|
|
587
|
+
return isInstanceOf(value, "Tool", Tool);
|
|
712
588
|
}
|
|
589
|
+
#name;
|
|
590
|
+
#description;
|
|
591
|
+
#inputSchema;
|
|
592
|
+
#handler;
|
|
593
|
+
#artifactConstructor;
|
|
594
|
+
#meta;
|
|
595
|
+
#ephemeral;
|
|
596
|
+
#trusted;
|
|
597
|
+
#onCollision;
|
|
713
598
|
/**
|
|
714
|
-
*
|
|
715
|
-
*
|
|
716
|
-
* @param name - The tool name to test.
|
|
599
|
+
* @param raw - The raw tool input validated against `rawToolSchema`.
|
|
600
|
+
* @throws {@link @nhtio/adk!E_INVALID_INITIAL_TOOL_VALUE} when `raw` does not satisfy the schema.
|
|
717
601
|
*/
|
|
718
|
-
|
|
719
|
-
|
|
602
|
+
constructor(raw) {
|
|
603
|
+
let resolved;
|
|
604
|
+
try {
|
|
605
|
+
resolved = validateOrThrow(rawToolSchema, raw, true);
|
|
606
|
+
} catch (err) {
|
|
607
|
+
throw new E_INVALID_INITIAL_TOOL_VALUE({ cause: isError(err) ? err : void 0 });
|
|
608
|
+
}
|
|
609
|
+
this.#name = resolved.name;
|
|
610
|
+
this.#description = resolved.description;
|
|
611
|
+
this.#inputSchema = resolved.inputSchema;
|
|
612
|
+
this.#handler = resolved.handler;
|
|
613
|
+
this.#artifactConstructor = resolved.artifactConstructor;
|
|
614
|
+
this.#meta = new Registry(resolved.meta);
|
|
615
|
+
this.#ephemeral = resolved.ephemeral;
|
|
616
|
+
this.#trusted = resolved.trusted;
|
|
617
|
+
this.#onCollision = resolved.onCollision;
|
|
618
|
+
Object.defineProperties(this, {
|
|
619
|
+
name: {
|
|
620
|
+
get: () => this.#name,
|
|
621
|
+
enumerable: true,
|
|
622
|
+
configurable: false
|
|
623
|
+
},
|
|
624
|
+
description: {
|
|
625
|
+
get: () => this.#description,
|
|
626
|
+
enumerable: true,
|
|
627
|
+
configurable: false
|
|
628
|
+
},
|
|
629
|
+
inputSchema: {
|
|
630
|
+
get: () => this.#inputSchema,
|
|
631
|
+
enumerable: true,
|
|
632
|
+
configurable: false
|
|
633
|
+
},
|
|
634
|
+
artifactConstructor: {
|
|
635
|
+
get: () => this.#artifactConstructor,
|
|
636
|
+
enumerable: true,
|
|
637
|
+
configurable: false
|
|
638
|
+
},
|
|
639
|
+
meta: {
|
|
640
|
+
get: () => this.#meta,
|
|
641
|
+
enumerable: true,
|
|
642
|
+
configurable: false
|
|
643
|
+
},
|
|
644
|
+
ephemeral: {
|
|
645
|
+
get: () => this.#ephemeral,
|
|
646
|
+
enumerable: true,
|
|
647
|
+
configurable: false
|
|
648
|
+
},
|
|
649
|
+
trusted: {
|
|
650
|
+
get: () => this.#trusted,
|
|
651
|
+
enumerable: true,
|
|
652
|
+
configurable: false
|
|
653
|
+
},
|
|
654
|
+
onCollision: {
|
|
655
|
+
get: () => this.#onCollision,
|
|
656
|
+
enumerable: true,
|
|
657
|
+
configurable: false
|
|
658
|
+
}
|
|
659
|
+
});
|
|
720
660
|
}
|
|
721
661
|
/**
|
|
722
|
-
*
|
|
662
|
+
* Validates `args` against the tool's input schema asynchronously.
|
|
723
663
|
*
|
|
724
664
|
* @remarks
|
|
725
|
-
*
|
|
726
|
-
* {@link
|
|
665
|
+
* Async to support schemas with external validators (e.g. database lookups, API calls).
|
|
666
|
+
* A validation failure throws {@link @nhtio/adk!E_INVALID_TOOL_ARGS} — this indicates a programming error
|
|
667
|
+
* in the tool call loop, not a downstream failure.
|
|
727
668
|
*
|
|
728
|
-
*
|
|
669
|
+
* @param args - The arguments to validate.
|
|
670
|
+
* @returns The validated (and coerced) arguments.
|
|
671
|
+
* @throws {@link @nhtio/adk!E_INVALID_TOOL_ARGS} when `args` does not satisfy the input schema.
|
|
729
672
|
*/
|
|
730
|
-
|
|
731
|
-
|
|
673
|
+
async validate(args) {
|
|
674
|
+
try {
|
|
675
|
+
return await asyncValidateOrThrow(this.#inputSchema, args);
|
|
676
|
+
} catch (err) {
|
|
677
|
+
if (isInstanceOf(err, "ValidationException", ValidationException)) throw new E_INVALID_TOOL_ARGS({ cause: err });
|
|
678
|
+
throw err;
|
|
679
|
+
}
|
|
732
680
|
}
|
|
733
681
|
/**
|
|
734
|
-
* Returns a
|
|
682
|
+
* Returns a bound executor function for this tool against the given turn context.
|
|
735
683
|
*
|
|
736
684
|
* @remarks
|
|
737
|
-
*
|
|
738
|
-
*
|
|
739
|
-
*
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
return this.all().filter((t) => !this.#hidden.has(t.name));
|
|
743
|
-
}
|
|
744
|
-
/**
|
|
745
|
-
* Returns a fresh array of registered tools that **are** hidden, in insertion order.
|
|
685
|
+
* The executor: (1) computes a stable `callId` as `sha256(canonicalStringify({tool, args}))`
|
|
686
|
+
* over the **raw, pre-validation args**, (2) validates `args` via {@link Tool.validate},
|
|
687
|
+
* (3) emits `toolExecutionStart` on the context (with the computed `callId`), (4) calls the
|
|
688
|
+
* handler, (5) emits `toolExecutionEnd` (with the same `callId`), (6) wraps any handler error
|
|
689
|
+
* in {@link @nhtio/adk!E_TOOL_DOWNSTREAM_ERROR} before re-throwing.
|
|
746
690
|
*
|
|
747
|
-
*
|
|
748
|
-
*
|
|
749
|
-
*
|
|
691
|
+
* The handler usually returns serialised bytes (`string | Uint8Array`) — persistence is then the
|
|
692
|
+
* consumer's concern, and {@link Tool.artifactConstructor} is the class a wrap-site uses when
|
|
693
|
+
* wrapping those bytes into a `ToolCall.results` field. **A handler may instead return a
|
|
694
|
+
* {@link @nhtio/adk!SpooledArtifact} it built itself** (streaming into storage and wrapping the
|
|
695
|
+
* reader), in which case wrap-sites pass it through untouched and `artifactConstructor` does not
|
|
696
|
+
* apply — the returned instance's own class is what artifact-tool forging reads.
|
|
697
|
+
*
|
|
698
|
+
* Pattern mirrors `Middleware.runner()` — call once per turn, reuse the returned function.
|
|
699
|
+
*
|
|
700
|
+
* @param ctx - The active turn context. Provides emit functions and turn ID.
|
|
701
|
+
* @returns An async function `(args) => Promise<string | Uint8Array | SpooledArtifact | Media | Media[]>`.
|
|
702
|
+
* A `SpooledArtifact` is a result the HANDLER built (streamed to storage and wrapped itself); a
|
|
703
|
+
* wrap-site passes it through unwrapped rather than re-spooling it. `string`/`Uint8Array` are the
|
|
704
|
+
* opposite case — bytes the consumer must still store and wrap.
|
|
750
705
|
*/
|
|
751
|
-
|
|
752
|
-
return
|
|
706
|
+
executor(ctx) {
|
|
707
|
+
return async (args) => {
|
|
708
|
+
const callId = sha256(canonicalStringify({
|
|
709
|
+
tool: this.#name,
|
|
710
|
+
args
|
|
711
|
+
}));
|
|
712
|
+
const validatedArgs = await this.validate(args);
|
|
713
|
+
const startedAt = DateTime.now();
|
|
714
|
+
ctx.emitToolExecutionStart({
|
|
715
|
+
toolName: this.#name,
|
|
716
|
+
turnId: ctx.id,
|
|
717
|
+
callId,
|
|
718
|
+
args: validatedArgs,
|
|
719
|
+
startedAt
|
|
720
|
+
});
|
|
721
|
+
try {
|
|
722
|
+
const result = await this.#handler(validatedArgs, ctx, this.#meta);
|
|
723
|
+
const endedAt = DateTime.now();
|
|
724
|
+
ctx.emitToolExecutionEnd({
|
|
725
|
+
toolName: this.#name,
|
|
726
|
+
turnId: ctx.id,
|
|
727
|
+
callId,
|
|
728
|
+
startedAt,
|
|
729
|
+
endedAt,
|
|
730
|
+
durationMs: endedAt.diff(startedAt).milliseconds,
|
|
731
|
+
isError: false
|
|
732
|
+
});
|
|
733
|
+
return result;
|
|
734
|
+
} catch (err) {
|
|
735
|
+
const endedAt = DateTime.now();
|
|
736
|
+
ctx.emitToolExecutionEnd({
|
|
737
|
+
toolName: this.#name,
|
|
738
|
+
turnId: ctx.id,
|
|
739
|
+
callId,
|
|
740
|
+
startedAt,
|
|
741
|
+
endedAt,
|
|
742
|
+
durationMs: endedAt.diff(startedAt).milliseconds,
|
|
743
|
+
isError: true
|
|
744
|
+
});
|
|
745
|
+
throw new E_TOOL_DOWNSTREAM_ERROR({ cause: isError(err) ? err : void 0 });
|
|
746
|
+
}
|
|
747
|
+
};
|
|
753
748
|
}
|
|
754
749
|
/**
|
|
755
|
-
*
|
|
750
|
+
* Returns a fully serialisable snapshot of this tool's definition.
|
|
756
751
|
*
|
|
757
752
|
* @remarks
|
|
758
|
-
*
|
|
759
|
-
*
|
|
760
|
-
*
|
|
753
|
+
* The `inputSchema` property is the result of calling `.describe()` on the raw schema — a plain
|
|
754
|
+
* object carrying all the annotation metadata (descriptions, notes, examples, types) without any
|
|
755
|
+
* validator functions. Use this to build provider-specific LLM tool definitions.
|
|
761
756
|
*
|
|
762
|
-
* @
|
|
757
|
+
* @returns `{ name, description, inputSchema }` where `inputSchema` is the schema description.
|
|
763
758
|
*/
|
|
764
|
-
|
|
765
|
-
|
|
759
|
+
describe() {
|
|
760
|
+
return {
|
|
761
|
+
name: this.#name,
|
|
762
|
+
description: this.#description,
|
|
763
|
+
inputSchema: this.#inputSchema.describe()
|
|
764
|
+
};
|
|
766
765
|
}
|
|
767
766
|
/**
|
|
768
|
-
*
|
|
767
|
+
* Serialise this Tool into an `@nhtio/encoder` snapshot.
|
|
769
768
|
*
|
|
770
769
|
* @remarks
|
|
771
|
-
*
|
|
770
|
+
* Three halves with different serialisation strategies:
|
|
772
771
|
*
|
|
773
|
-
*
|
|
772
|
+
* - **Plain config** (`name`, `description`, `meta`, flags) — emitted verbatim.
|
|
773
|
+
* - **`inputSchema`** — delegated to `@nhtio/validation`'s `encode()`, which captures the full schema
|
|
774
|
+
* description plus its library version into a compact string; decode rebuilds the live `Schema`.
|
|
775
|
+
* - **`handler` / `artifactConstructor`** — emitted as **functions**. The encoder's `FunctionSerializer`
|
|
776
|
+
* serialises them by **source text** (`fn.toString()`). This is the sharp edge: a handler that
|
|
777
|
+
* closes over `ctx`, service clients, or config loses those bindings on decode — only the source
|
|
778
|
+
* survives, and the captured variables read back as `undefined`. A `.bind()`-ed or native handler
|
|
779
|
+
* cannot be serialised at all and the encoder throws. Tools are rarely serialised in practice; when
|
|
780
|
+
* they are, write handlers that reconstruct their dependencies inside the body rather than closing
|
|
781
|
+
* over them.
|
|
782
|
+
*
|
|
783
|
+
* @returns A {@link RawTool}-shaped snapshot (with `inputSchema` as an encoded string).
|
|
774
784
|
*/
|
|
775
|
-
|
|
776
|
-
|
|
785
|
+
[ENCODE_METHOD]() {
|
|
786
|
+
return {
|
|
787
|
+
name: this.#name,
|
|
788
|
+
description: this.#description,
|
|
789
|
+
inputSchema: encode(this.#inputSchema),
|
|
790
|
+
handler: this.#handler,
|
|
791
|
+
artifactConstructor: this.#artifactConstructor,
|
|
792
|
+
meta: this.#meta.all(),
|
|
793
|
+
ephemeral: this.#ephemeral,
|
|
794
|
+
trusted: this.#trusted,
|
|
795
|
+
onCollision: this.#onCollision
|
|
796
|
+
};
|
|
777
797
|
}
|
|
778
798
|
/**
|
|
779
|
-
*
|
|
799
|
+
* Reconstruct a {@link Tool} from a {@link Tool.[ENCODE_METHOD]} snapshot.
|
|
780
800
|
*
|
|
781
801
|
* @remarks
|
|
782
|
-
*
|
|
783
|
-
*
|
|
784
|
-
*
|
|
802
|
+
* Rebuilds the live `Schema` via `@nhtio/validation`'s `decode()`, then re-validates through the
|
|
803
|
+
* constructor. The rehydrated `handler` carries only its source text — see
|
|
804
|
+
* {@link Tool.[ENCODE_METHOD]} for the closure caveat.
|
|
785
805
|
*
|
|
786
|
-
* @param
|
|
787
|
-
|
|
788
|
-
setHidden(...names) {
|
|
789
|
-
this.#hidden = new Set(names);
|
|
790
|
-
}
|
|
791
|
-
/**
|
|
792
|
-
* Unhides every tool in the registry.
|
|
806
|
+
* @param data - The snapshot produced by {@link Tool.[ENCODE_METHOD]}.
|
|
807
|
+
* @returns A fully-validated {@link Tool}.
|
|
793
808
|
*/
|
|
794
|
-
|
|
795
|
-
|
|
809
|
+
static [DECODE_METHOD](data) {
|
|
810
|
+
const snapshot = data;
|
|
811
|
+
return new Tool({
|
|
812
|
+
...snapshot,
|
|
813
|
+
inputSchema: decode(snapshot.inputSchema)
|
|
814
|
+
});
|
|
796
815
|
}
|
|
816
|
+
};
|
|
817
|
+
//#endregion
|
|
818
|
+
//#region src/lib/classes/artifact_tool.ts
|
|
819
|
+
/**
|
|
820
|
+
* Validator schema for a {@link RawArtifactTool}.
|
|
821
|
+
*
|
|
822
|
+
* @remarks
|
|
823
|
+
* Mirrors the base tool schema but explicitly forbids `artifactConstructor` — the entire
|
|
824
|
+
* point of `ArtifactTool` is to opt out of `SpooledArtifact` wrapping.
|
|
825
|
+
*/
|
|
826
|
+
var rawArtifactToolSchema = validator.object({
|
|
827
|
+
name: validator.string().required(),
|
|
828
|
+
description: validator.string().required(),
|
|
829
|
+
inputSchema: validator.any().custom((value, helpers) => {
|
|
830
|
+
if (validator.isSchema(value) && value.type === "object") return value;
|
|
831
|
+
return helpers.error("any.invalid");
|
|
832
|
+
}).required(),
|
|
833
|
+
handler: validator.function().required(),
|
|
834
|
+
meta: validator.object().pattern(validator.string(), validator.any()).default({}),
|
|
835
|
+
ephemeral: validator.boolean().default(false),
|
|
836
|
+
trusted: validator.boolean().default(false),
|
|
837
|
+
onCollision: validator.string().valid("throw", "replace", "keep").default("throw"),
|
|
838
|
+
artifactConstructor: validator.any().forbidden()
|
|
839
|
+
});
|
|
840
|
+
/**
|
|
841
|
+
* A {@link @nhtio/adk!Tool} subclass whose handler return value is wrapped directly in a
|
|
842
|
+
* {@link @nhtio/adk!Tokenizable} (not a {@link @nhtio/adk!SpooledArtifact}) when it
|
|
843
|
+
* lands on `ToolCall.results`.
|
|
844
|
+
*
|
|
845
|
+
* @remarks
|
|
846
|
+
* `ArtifactTool` is the canonical producer for **forged artifact-query tools** — the tools
|
|
847
|
+
* `SpooledArtifact.forgeTools(ctx)` emits so the model can `head`, `tail`, `grep`, `json_get`,
|
|
848
|
+
* `md_headings` (etc.) an artifact that is already in `ctx.turnToolCalls`.
|
|
849
|
+
*
|
|
850
|
+
* The difference from {@link @nhtio/adk!Tool} is structural, not stylistic:
|
|
851
|
+
*
|
|
852
|
+
* - A normal `Tool`'s handler returns bytes the ADK wraps in a fresh `SpooledArtifact`.
|
|
853
|
+
* The artifact lands in `ToolCall.results`, joins `ctx.turnToolCalls`, and is itself a
|
|
854
|
+
* first-class queryable artifact in the turn.
|
|
855
|
+
* - An `ArtifactTool`'s handler returns a string that is **already the model-visible answer**
|
|
856
|
+
* to a query against an existing artifact. The ADK wraps it in a `Tokenizable` rather
|
|
857
|
+
* than a `SpooledArtifact`; nothing new is queryable on its own. Subsequent
|
|
858
|
+
* `forgeTools(ctx)` calls exclude `ToolCall`s produced by an `ArtifactTool` from the
|
|
859
|
+
* `callId` enum (via the `ToolCall.fromArtifactTool` marker) — this is the structural fix
|
|
860
|
+
* that breaks the otherwise-recursive grep-on-the-grep-result loop.
|
|
861
|
+
*
|
|
862
|
+
* Consumers who want to build their own artifact-query tools (e.g. for a domain-specific
|
|
863
|
+
* spooled subclass not shipped by the ADK) should extend or instantiate this class.
|
|
864
|
+
*/
|
|
865
|
+
var ArtifactTool = class ArtifactTool extends Tool {
|
|
797
866
|
/**
|
|
798
|
-
*
|
|
867
|
+
* Validator schema that accepts a {@link RawArtifactTool} object.
|
|
799
868
|
*
|
|
800
869
|
* @remarks
|
|
801
|
-
*
|
|
802
|
-
*
|
|
803
|
-
*
|
|
804
|
-
*
|
|
870
|
+
* Differs from {@link @nhtio/adk!Tool.schema} by forbidding `artifactConstructor` — wrapping is
|
|
871
|
+
* exactly the thing this class opts out of. Typed identically to {@link @nhtio/adk!Tool.schema} so the
|
|
872
|
+
* subclass relationship `class ArtifactTool extends Tool` remains structurally sound; the
|
|
873
|
+
* runtime validation rules still differ as declared by `rawArtifactToolSchema`.
|
|
805
874
|
*/
|
|
806
|
-
|
|
807
|
-
for (const [name, tool] of this.#tools) if (tool.ephemeral) {
|
|
808
|
-
this.#tools.delete(name);
|
|
809
|
-
this.#hidden.delete(name);
|
|
810
|
-
}
|
|
811
|
-
}
|
|
875
|
+
static schema = rawArtifactToolSchema;
|
|
812
876
|
/**
|
|
813
|
-
*
|
|
814
|
-
* automatically when the context is acked.
|
|
877
|
+
* Returns `true` if `value` is an {@link ArtifactTool} instance.
|
|
815
878
|
*
|
|
816
879
|
* @remarks
|
|
817
|
-
*
|
|
818
|
-
*
|
|
819
|
-
* failure. Subscriptions are short-lived and die with the context regardless.
|
|
820
|
-
*
|
|
821
|
-
* ARTIFACT READERS ARE FORGED BY THE CORE. As of the core-forge change, the `DispatchRunner` forges
|
|
822
|
-
* artifact-reader tools from prior-turn `SpooledArtifact` results into `ctx.tools` (and calls
|
|
823
|
-
* `ctx.tools.bindContext(ctx)`) once per iteration, BEFORE the input pipeline — so a battery executor no
|
|
824
|
-
* longer forges or binds; it reads the already-forged `ctx.tools` for both representation (rendering the
|
|
825
|
-
* tool declarations) and resolution (looking up an incoming call by name). The lifecycle is unchanged:
|
|
826
|
-
* ephemeral readers are still pruned on `ack`, and the core re-forges each iteration (prune-then-forge) so
|
|
827
|
-
* the `callId` enum never goes stale.
|
|
828
|
-
*
|
|
829
|
-
* `bindContext` remains the public seam for a CUSTOM consumer that forges its own ephemeral tools outside
|
|
830
|
-
* the core path. If you forge into a long-lived registry yourself, bind it (or prune manually) or the
|
|
831
|
-
* ephemeral tools accumulate and later `forgeTools(ctx)` calls see a stale `callId` enum. The pattern for a
|
|
832
|
-
* hand-rolled forge is:
|
|
833
|
-
*
|
|
834
|
-
* ```ts
|
|
835
|
-
* // Custom forge (the core already does this for artifact readers on ctx.tools):
|
|
836
|
-
* const forged = SpooledArtifact.forgeTools(ctx)
|
|
837
|
-
* for (const tool of forged.all()) ctx.tools.register(tool, true)
|
|
838
|
-
* ctx.tools.bindContext(ctx) // prune the ephemeral readers when the dispatch acks
|
|
839
|
-
* ```
|
|
840
|
-
*
|
|
841
|
-
* @param ctx - The execution context whose `ack` event should trigger pruning.
|
|
842
|
-
* @returns An unsubscribe function — calling it before `ctx.ack()` prevents pruning. Rarely
|
|
843
|
-
* useful outside of tests.
|
|
880
|
+
* Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety — `instanceof` would fail for instances
|
|
881
|
+
* created in a different module copy or VM context.
|
|
844
882
|
*
|
|
845
|
-
* @
|
|
846
|
-
* @
|
|
883
|
+
* @param value - The value to test.
|
|
884
|
+
* @returns `true` when `value` is an {@link ArtifactTool} instance.
|
|
847
885
|
*/
|
|
848
|
-
|
|
849
|
-
return
|
|
886
|
+
static isArtifactTool(value) {
|
|
887
|
+
return isInstanceOf(value, "ArtifactTool", ArtifactTool);
|
|
850
888
|
}
|
|
851
889
|
/**
|
|
852
|
-
*
|
|
853
|
-
* input.
|
|
854
|
-
*
|
|
855
|
-
* @remarks
|
|
856
|
-
* Iteration is left-to-right across `registries` and then in each registry's insertion order.
|
|
857
|
-
* Collisions are resolved by consulting the **incoming** tool's {@link @nhtio/adk!Tool.onCollision} first:
|
|
858
|
-
*
|
|
859
|
-
* - `'replace'` (per-tool): the incoming tool wins, replacing the existing entry.
|
|
860
|
-
* - `'keep'` (per-tool): the existing entry wins; the incoming tool is dropped.
|
|
861
|
-
* - `'throw'` (per-tool, the default): fall back to the merge-level `options.onCollision`.
|
|
862
|
-
*
|
|
863
|
-
* The merge-level `options.onCollision` defaults to `'throw'`, which mirrors {@link register}.
|
|
864
|
-
*
|
|
865
|
-
* The result is a brand-new registry; no input is mutated and no event subscription is
|
|
866
|
-
* propagated. Each `Tool`'s `ephemeral` flag carries through unchanged — the flag lives on the
|
|
867
|
-
* tool, not the registry, so `bindContext(ctx)` on the merged registry will prune the forged
|
|
868
|
-
* tools as expected.
|
|
869
|
-
*
|
|
870
|
-
* Hidden state is also propagated: a tool that is hidden in any source registry remains hidden
|
|
871
|
-
* in the merged result, provided it survives collision resolution.
|
|
890
|
+
* @param raw - Raw tool input validated against {@link ArtifactTool.schema}.
|
|
872
891
|
*
|
|
873
|
-
* @
|
|
874
|
-
*
|
|
875
|
-
*
|
|
876
|
-
*
|
|
877
|
-
* and a collision occurs.
|
|
892
|
+
* @throws {@link @nhtio/adk!E_INVALID_INITIAL_TOOL_VALUE} when `raw` does not satisfy
|
|
893
|
+
* {@link ArtifactTool.schema} (most commonly, when `artifactConstructor` is supplied — it is
|
|
894
|
+
* explicitly forbidden on this class) or when the base {@link @nhtio/adk!Tool} constructor rejects the
|
|
895
|
+
* input for any reason.
|
|
878
896
|
*/
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
merged.register(tool);
|
|
886
|
-
continue;
|
|
887
|
-
}
|
|
888
|
-
const incomingPolicy = tool.onCollision;
|
|
889
|
-
if (incomingPolicy === "replace") {
|
|
890
|
-
merged.register(tool, true);
|
|
891
|
-
continue;
|
|
892
|
-
}
|
|
893
|
-
if (incomingPolicy === "keep") continue;
|
|
894
|
-
if (policy === "replace") {
|
|
895
|
-
merged.register(tool, true);
|
|
896
|
-
continue;
|
|
897
|
-
}
|
|
898
|
-
if (policy === "keep") continue;
|
|
899
|
-
throw new E_TOOL_ALREADY_REGISTERED();
|
|
900
|
-
}
|
|
901
|
-
for (const tool of registry.hidden()) if (merged.has(tool.name)) merged.hide(tool.name);
|
|
897
|
+
constructor(raw) {
|
|
898
|
+
try {
|
|
899
|
+
validateOrThrow(rawArtifactToolSchema, raw, true);
|
|
900
|
+
} catch (err) {
|
|
901
|
+
if (isInstanceOf(err, "ValidationException", ValidationException)) throw new E_INVALID_INITIAL_TOOL_VALUE({ cause: err });
|
|
902
|
+
throw err;
|
|
902
903
|
}
|
|
903
|
-
|
|
904
|
+
super(raw);
|
|
904
905
|
}
|
|
905
906
|
/**
|
|
906
|
-
* Serialise this
|
|
907
|
+
* Serialise this ArtifactTool into an `@nhtio/encoder` snapshot.
|
|
907
908
|
*
|
|
908
909
|
* @remarks
|
|
909
|
-
*
|
|
910
|
-
*
|
|
911
|
-
*
|
|
912
|
-
* subtype. Round-trips via {@link ToolRegistry.[DECODE_METHOD]}.
|
|
910
|
+
* Reuses the base {@link Tool.[ENCODE_METHOD]} (function-serialised `handler`, validation-encoded
|
|
911
|
+
* `inputSchema`, plain config) and strips `artifactConstructor` — which {@link ArtifactTool.schema}
|
|
912
|
+
* forbids. The handler's closure caveat from {@link Tool.[ENCODE_METHOD]} applies here too.
|
|
913
913
|
*
|
|
914
|
-
* @returns A
|
|
914
|
+
* @returns A {@link RawArtifactTool}-shaped snapshot (with `inputSchema` as an encoded string).
|
|
915
915
|
*/
|
|
916
916
|
[ENCODE_METHOD]() {
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
};
|
|
917
|
+
const base = super[ENCODE_METHOD]();
|
|
918
|
+
delete base.artifactConstructor;
|
|
919
|
+
return base;
|
|
921
920
|
}
|
|
922
921
|
/**
|
|
923
|
-
* Reconstruct
|
|
922
|
+
* Reconstruct an {@link ArtifactTool} from an {@link ArtifactTool.[ENCODE_METHOD]} snapshot.
|
|
924
923
|
*
|
|
925
|
-
* @param data - The snapshot produced by {@link
|
|
926
|
-
* @returns A
|
|
924
|
+
* @param data - The snapshot produced by {@link ArtifactTool.[ENCODE_METHOD]}.
|
|
925
|
+
* @returns A fully-validated {@link ArtifactTool}.
|
|
927
926
|
*/
|
|
928
927
|
static [DECODE_METHOD](data) {
|
|
929
928
|
const snapshot = data;
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
929
|
+
return new ArtifactTool({
|
|
930
|
+
...snapshot,
|
|
931
|
+
inputSchema: decode(snapshot.inputSchema)
|
|
932
|
+
});
|
|
933
933
|
}
|
|
934
934
|
};
|
|
935
935
|
//#endregion
|
|
@@ -1628,6 +1628,6 @@ var SpooledArtifact = class SpooledArtifact {
|
|
|
1628
1628
|
}
|
|
1629
1629
|
};
|
|
1630
1630
|
//#endregion
|
|
1631
|
-
export { resolveArtifactById as a, resolveMediaReader as c,
|
|
1631
|
+
export { resolveArtifactById as a, resolveMediaReader as c, ArtifactTool as d, Tool as f, ToolRegistry as g, canonicalStringify as h, effectiveToolMethods as i, resolveSpoolReader as l, Registry as m, collectArtifactCompatibleIds as n, registerMediaReaderResolver as o, artifactConstructorResolverSchema as p, defaultSerialise as r, registerSpoolReaderResolver as s, SpooledArtifact as t, implementsSpoolReader as u };
|
|
1632
1632
|
|
|
1633
|
-
//# sourceMappingURL=spooled_artifact-
|
|
1633
|
+
//# sourceMappingURL=spooled_artifact-BkOe4-j-.mjs.map
|