@nhtio/adk 0.1.0-master-f0aa531d

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (297) hide show
  1. package/LICENSE.md +9 -0
  2. package/README.md +3 -0
  3. package/batteries/index.d.ts +28 -0
  4. package/batteries/llm/index.d.ts +11 -0
  5. package/batteries/llm/openai_chat_completions/adapter.cjs +916 -0
  6. package/batteries/llm/openai_chat_completions/adapter.cjs.map +1 -0
  7. package/batteries/llm/openai_chat_completions/adapter.d.ts +101 -0
  8. package/batteries/llm/openai_chat_completions/adapter.mjs +914 -0
  9. package/batteries/llm/openai_chat_completions/adapter.mjs.map +1 -0
  10. package/batteries/llm/openai_chat_completions/exceptions.cjs +89 -0
  11. package/batteries/llm/openai_chat_completions/exceptions.cjs.map +1 -0
  12. package/batteries/llm/openai_chat_completions/exceptions.d.ts +97 -0
  13. package/batteries/llm/openai_chat_completions/exceptions.mjs +81 -0
  14. package/batteries/llm/openai_chat_completions/exceptions.mjs.map +1 -0
  15. package/batteries/llm/openai_chat_completions/helpers.cjs +819 -0
  16. package/batteries/llm/openai_chat_completions/helpers.cjs.map +1 -0
  17. package/batteries/llm/openai_chat_completions/helpers.d.ts +233 -0
  18. package/batteries/llm/openai_chat_completions/helpers.mjs +783 -0
  19. package/batteries/llm/openai_chat_completions/helpers.mjs.map +1 -0
  20. package/batteries/llm/openai_chat_completions/index.d.ts +27 -0
  21. package/batteries/llm/openai_chat_completions/types.cjs +1 -0
  22. package/batteries/llm/openai_chat_completions/types.d.ts +524 -0
  23. package/batteries/llm/openai_chat_completions/types.mjs +0 -0
  24. package/batteries/llm/openai_chat_completions/validation.cjs +190 -0
  25. package/batteries/llm/openai_chat_completions/validation.cjs.map +1 -0
  26. package/batteries/llm/openai_chat_completions/validation.d.ts +31 -0
  27. package/batteries/llm/openai_chat_completions/validation.mjs +187 -0
  28. package/batteries/llm/openai_chat_completions/validation.mjs.map +1 -0
  29. package/batteries/llm/openai_chat_completions.cjs +51 -0
  30. package/batteries/llm/openai_chat_completions.mjs +5 -0
  31. package/batteries/llm/webllm_chat_completions/adapter.cjs +658 -0
  32. package/batteries/llm/webllm_chat_completions/adapter.cjs.map +1 -0
  33. package/batteries/llm/webllm_chat_completions/adapter.d.ts +103 -0
  34. package/batteries/llm/webllm_chat_completions/adapter.mjs +656 -0
  35. package/batteries/llm/webllm_chat_completions/adapter.mjs.map +1 -0
  36. package/batteries/llm/webllm_chat_completions/exceptions.cjs +70 -0
  37. package/batteries/llm/webllm_chat_completions/exceptions.cjs.map +1 -0
  38. package/batteries/llm/webllm_chat_completions/exceptions.d.ts +74 -0
  39. package/batteries/llm/webllm_chat_completions/exceptions.mjs +65 -0
  40. package/batteries/llm/webllm_chat_completions/exceptions.mjs.map +1 -0
  41. package/batteries/llm/webllm_chat_completions/helpers.cjs +38 -0
  42. package/batteries/llm/webllm_chat_completions/helpers.d.ts +6 -0
  43. package/batteries/llm/webllm_chat_completions/helpers.mjs +2 -0
  44. package/batteries/llm/webllm_chat_completions/index.d.ts +25 -0
  45. package/batteries/llm/webllm_chat_completions/types.d.ts +31 -0
  46. package/batteries/llm/webllm_chat_completions/validation.cjs +115 -0
  47. package/batteries/llm/webllm_chat_completions/validation.cjs.map +1 -0
  48. package/batteries/llm/webllm_chat_completions/validation.d.ts +8 -0
  49. package/batteries/llm/webllm_chat_completions/validation.mjs +112 -0
  50. package/batteries/llm/webllm_chat_completions/validation.mjs.map +1 -0
  51. package/batteries/llm/webllm_chat_completions.cjs +50 -0
  52. package/batteries/llm/webllm_chat_completions.mjs +6 -0
  53. package/batteries/llm.cjs +63 -0
  54. package/batteries/llm.mjs +10 -0
  55. package/batteries/storage/flydrive/index.d.ts +167 -0
  56. package/batteries/storage/flydrive.cjs +249 -0
  57. package/batteries/storage/flydrive.cjs.map +1 -0
  58. package/batteries/storage/flydrive.mjs +249 -0
  59. package/batteries/storage/flydrive.mjs.map +1 -0
  60. package/batteries/storage/in_memory/index.d.ts +106 -0
  61. package/batteries/storage/in_memory.cjs +121 -0
  62. package/batteries/storage/in_memory.cjs.map +1 -0
  63. package/batteries/storage/in_memory.mjs +119 -0
  64. package/batteries/storage/in_memory.mjs.map +1 -0
  65. package/batteries/storage/index.d.ts +18 -0
  66. package/batteries/storage/opfs/index.d.ts +299 -0
  67. package/batteries/storage/opfs.cjs +368 -0
  68. package/batteries/storage/opfs.cjs.map +1 -0
  69. package/batteries/storage/opfs.mjs +366 -0
  70. package/batteries/storage/opfs.mjs.map +1 -0
  71. package/batteries/storage.cjs +4 -0
  72. package/batteries/storage.mjs +2 -0
  73. package/batteries/tools/color/index.d.ts +37 -0
  74. package/batteries/tools/color.cjs +659 -0
  75. package/batteries/tools/color.cjs.map +1 -0
  76. package/batteries/tools/color.mjs +655 -0
  77. package/batteries/tools/color.mjs.map +1 -0
  78. package/batteries/tools/comparison/index.d.ts +29 -0
  79. package/batteries/tools/comparison.cjs +171 -0
  80. package/batteries/tools/comparison.cjs.map +1 -0
  81. package/batteries/tools/comparison.mjs +168 -0
  82. package/batteries/tools/comparison.mjs.map +1 -0
  83. package/batteries/tools/data_structure/index.d.ts +30 -0
  84. package/batteries/tools/data_structure.cjs +270 -0
  85. package/batteries/tools/data_structure.cjs.map +1 -0
  86. package/batteries/tools/data_structure.mjs +267 -0
  87. package/batteries/tools/data_structure.mjs.map +1 -0
  88. package/batteries/tools/datetime_extended/index.d.ts +51 -0
  89. package/batteries/tools/datetime_extended.cjs +309 -0
  90. package/batteries/tools/datetime_extended.cjs.map +1 -0
  91. package/batteries/tools/datetime_extended.mjs +302 -0
  92. package/batteries/tools/datetime_extended.mjs.map +1 -0
  93. package/batteries/tools/datetime_math/index.d.ts +36 -0
  94. package/batteries/tools/datetime_math.cjs +175 -0
  95. package/batteries/tools/datetime_math.cjs.map +1 -0
  96. package/batteries/tools/datetime_math.mjs +171 -0
  97. package/batteries/tools/datetime_math.mjs.map +1 -0
  98. package/batteries/tools/encoding/index.d.ts +36 -0
  99. package/batteries/tools/encoding.cjs +156 -0
  100. package/batteries/tools/encoding.cjs.map +1 -0
  101. package/batteries/tools/encoding.mjs +152 -0
  102. package/batteries/tools/encoding.mjs.map +1 -0
  103. package/batteries/tools/formatting/index.d.ts +28 -0
  104. package/batteries/tools/formatting.cjs +120 -0
  105. package/batteries/tools/formatting.cjs.map +1 -0
  106. package/batteries/tools/formatting.mjs +117 -0
  107. package/batteries/tools/formatting.mjs.map +1 -0
  108. package/batteries/tools/geo_basics/index.d.ts +33 -0
  109. package/batteries/tools/geo_basics.cjs +136 -0
  110. package/batteries/tools/geo_basics.cjs.map +1 -0
  111. package/batteries/tools/geo_basics.mjs +132 -0
  112. package/batteries/tools/geo_basics.mjs.map +1 -0
  113. package/batteries/tools/index.d.ts +32 -0
  114. package/batteries/tools/math/index.d.ts +37 -0
  115. package/batteries/tools/math.cjs +136 -0
  116. package/batteries/tools/math.cjs.map +1 -0
  117. package/batteries/tools/math.mjs +133 -0
  118. package/batteries/tools/math.mjs.map +1 -0
  119. package/batteries/tools/memory/index.d.ts +73 -0
  120. package/batteries/tools/memory.cjs +193 -0
  121. package/batteries/tools/memory.cjs.map +1 -0
  122. package/batteries/tools/memory.mjs +187 -0
  123. package/batteries/tools/memory.mjs.map +1 -0
  124. package/batteries/tools/parsing/index.d.ts +47 -0
  125. package/batteries/tools/parsing.cjs +191 -0
  126. package/batteries/tools/parsing.cjs.map +1 -0
  127. package/batteries/tools/parsing.mjs +185 -0
  128. package/batteries/tools/parsing.mjs.map +1 -0
  129. package/batteries/tools/retrievables/index.d.ts +81 -0
  130. package/batteries/tools/retrievables.cjs +215 -0
  131. package/batteries/tools/retrievables.cjs.map +1 -0
  132. package/batteries/tools/retrievables.mjs +209 -0
  133. package/batteries/tools/retrievables.mjs.map +1 -0
  134. package/batteries/tools/standing_instructions/index.d.ts +64 -0
  135. package/batteries/tools/standing_instructions.cjs +126 -0
  136. package/batteries/tools/standing_instructions.cjs.map +1 -0
  137. package/batteries/tools/standing_instructions.mjs +121 -0
  138. package/batteries/tools/standing_instructions.mjs.map +1 -0
  139. package/batteries/tools/statistics/index.d.ts +46 -0
  140. package/batteries/tools/statistics.cjs +253 -0
  141. package/batteries/tools/statistics.cjs.map +1 -0
  142. package/batteries/tools/statistics.mjs +248 -0
  143. package/batteries/tools/statistics.mjs.map +1 -0
  144. package/batteries/tools/string_processing/index.d.ts +29 -0
  145. package/batteries/tools/string_processing.cjs +154 -0
  146. package/batteries/tools/string_processing.cjs.map +1 -0
  147. package/batteries/tools/string_processing.mjs +151 -0
  148. package/batteries/tools/string_processing.mjs.map +1 -0
  149. package/batteries/tools/structured_data/index.d.ts +34 -0
  150. package/batteries/tools/structured_data.cjs +189 -0
  151. package/batteries/tools/structured_data.cjs.map +1 -0
  152. package/batteries/tools/structured_data.mjs +185 -0
  153. package/batteries/tools/structured_data.mjs.map +1 -0
  154. package/batteries/tools/text_analysis/index.d.ts +31 -0
  155. package/batteries/tools/text_analysis.cjs +120 -0
  156. package/batteries/tools/text_analysis.cjs.map +1 -0
  157. package/batteries/tools/text_analysis.mjs +117 -0
  158. package/batteries/tools/text_analysis.mjs.map +1 -0
  159. package/batteries/tools/text_comparison/index.d.ts +28 -0
  160. package/batteries/tools/text_comparison.cjs +96 -0
  161. package/batteries/tools/text_comparison.cjs.map +1 -0
  162. package/batteries/tools/text_comparison.mjs +93 -0
  163. package/batteries/tools/text_comparison.mjs.map +1 -0
  164. package/batteries/tools/time/index.d.ts +27 -0
  165. package/batteries/tools/time.cjs +63 -0
  166. package/batteries/tools/time.cjs.map +1 -0
  167. package/batteries/tools/time.mjs +60 -0
  168. package/batteries/tools/time.mjs.map +1 -0
  169. package/batteries/tools/unit_conversion/index.d.ts +19 -0
  170. package/batteries/tools/unit_conversion.cjs +452 -0
  171. package/batteries/tools/unit_conversion.cjs.map +1 -0
  172. package/batteries/tools/unit_conversion.mjs +450 -0
  173. package/batteries/tools/unit_conversion.mjs.map +1 -0
  174. package/batteries/tools.cjs +80 -0
  175. package/batteries/tools.mjs +21 -0
  176. package/batteries.cjs +142 -0
  177. package/batteries.mjs +30 -0
  178. package/chunk-KmRHZBOW.js +35 -0
  179. package/common-DeZaonK1.mjs +208 -0
  180. package/common-DeZaonK1.mjs.map +1 -0
  181. package/common-Od8edUXU.js +232 -0
  182. package/common-Od8edUXU.js.map +1 -0
  183. package/common.cjs +31 -0
  184. package/common.d.ts +108 -0
  185. package/common.mjs +8 -0
  186. package/dispatch_runner-9j6bXHL3.mjs +1609 -0
  187. package/dispatch_runner-9j6bXHL3.mjs.map +1 -0
  188. package/dispatch_runner-CsoH0nld.js +1627 -0
  189. package/dispatch_runner-CsoH0nld.js.map +1 -0
  190. package/dispatch_runner.cjs +3 -0
  191. package/dispatch_runner.d.ts +17 -0
  192. package/dispatch_runner.mjs +2 -0
  193. package/exceptions-D5YrO9Vm.js +280 -0
  194. package/exceptions-D5YrO9Vm.js.map +1 -0
  195. package/exceptions-NrzIHw_R.mjs +244 -0
  196. package/exceptions-NrzIHw_R.mjs.map +1 -0
  197. package/exceptions.cjs +33 -0
  198. package/exceptions.d.ts +52 -0
  199. package/exceptions.mjs +3 -0
  200. package/factories.cjs +4 -0
  201. package/factories.d.ts +39 -0
  202. package/factories.mjs +2 -0
  203. package/forge.cjs +9 -0
  204. package/forge.d.ts +49 -0
  205. package/forge.mjs +5 -0
  206. package/guards.cjs +96 -0
  207. package/guards.cjs.map +1 -0
  208. package/guards.d.ts +83 -0
  209. package/guards.mjs +72 -0
  210. package/guards.mjs.map +1 -0
  211. package/index.cjs +107 -0
  212. package/index.cjs.map +1 -0
  213. package/index.d.ts +18 -0
  214. package/index.mjs +31 -0
  215. package/index.mjs.map +1 -0
  216. package/lib/classes/artifact_tool.d.ts +129 -0
  217. package/lib/classes/base_exception.d.ts +83 -0
  218. package/lib/classes/identity.d.ts +71 -0
  219. package/lib/classes/media.d.ts +326 -0
  220. package/lib/classes/memory.d.ts +72 -0
  221. package/lib/classes/message.d.ts +137 -0
  222. package/lib/classes/registry.d.ts +79 -0
  223. package/lib/classes/retrievable.d.ts +100 -0
  224. package/lib/classes/spooled_artifact.d.ts +296 -0
  225. package/lib/classes/spooled_json_artifact.d.ts +158 -0
  226. package/lib/classes/spooled_markdown_artifact.d.ts +202 -0
  227. package/lib/classes/thought.d.ts +142 -0
  228. package/lib/classes/tokenizable.d.ts +124 -0
  229. package/lib/classes/tool.d.ts +228 -0
  230. package/lib/classes/tool_call.d.ts +190 -0
  231. package/lib/classes/tool_registry.d.ts +159 -0
  232. package/lib/classes/turn_gate.d.ts +109 -0
  233. package/lib/contracts/dispatch_context.d.ts +345 -0
  234. package/lib/contracts/media_reader.d.ts +60 -0
  235. package/lib/contracts/spool_reader.d.ts +80 -0
  236. package/lib/contracts/spooled_artifact_constructor.d.ts +38 -0
  237. package/lib/contracts/turn_runner_config.d.ts +101 -0
  238. package/lib/contracts/turn_runner_context.d.ts +267 -0
  239. package/lib/dispatch_runner.d.ts +98 -0
  240. package/lib/exceptions/runtime.d.ts +370 -0
  241. package/lib/helpers/media_readers.d.ts +39 -0
  242. package/lib/turn_runner.d.ts +144 -0
  243. package/lib/types/dispatch_context.d.ts +233 -0
  244. package/lib/types/dispatch_runner.d.ts +387 -0
  245. package/lib/types/turn_runner.d.ts +322 -0
  246. package/lib/utils/canonical_json.d.ts +18 -0
  247. package/lib/utils/exceptions.d.ts +78 -0
  248. package/lib/utils/guards.d.ts +32 -0
  249. package/lib/utils/validation.d.ts +77 -0
  250. package/package.json +334 -0
  251. package/runtime-BJVkrGQe.js +519 -0
  252. package/runtime-BJVkrGQe.js.map +1 -0
  253. package/runtime-CrEPIFgr.mjs +346 -0
  254. package/runtime-CrEPIFgr.mjs.map +1 -0
  255. package/skills/adk-assembly/SKILL.md +109 -0
  256. package/skills/adk-assembly/references/assembly-contract.md +66 -0
  257. package/skills/adk-assembly/references/executors-tools-pipelines-events.md +113 -0
  258. package/skills/adk-assembly/references/first-integration.md +93 -0
  259. package/skills/adk-assembly/references/storage-and-context.md +102 -0
  260. package/spooled_artifact-C5ZtGxuJ.mjs +544 -0
  261. package/spooled_artifact-C5ZtGxuJ.mjs.map +1 -0
  262. package/spooled_artifact-Cm9Te22K.js +568 -0
  263. package/spooled_artifact-Cm9Te22K.js.map +1 -0
  264. package/spooled_artifact.cjs +7 -0
  265. package/spooled_artifact.d.ts +40 -0
  266. package/spooled_artifact.mjs +3 -0
  267. package/spooled_markdown_artifact-BpUJol0W.mjs +771 -0
  268. package/spooled_markdown_artifact-BpUJol0W.mjs.map +1 -0
  269. package/spooled_markdown_artifact-RRB113sy.js +786 -0
  270. package/spooled_markdown_artifact-RRB113sy.js.map +1 -0
  271. package/thought-CDb457b4.mjs +470 -0
  272. package/thought-CDb457b4.mjs.map +1 -0
  273. package/thought-DuN2PgdO.js +494 -0
  274. package/thought-DuN2PgdO.js.map +1 -0
  275. package/tool-COSeH8I6.js +302 -0
  276. package/tool-COSeH8I6.js.map +1 -0
  277. package/tool-D2WB1EA1.mjs +296 -0
  278. package/tool-D2WB1EA1.mjs.map +1 -0
  279. package/tool_call-BKyyxGaZ.mjs +578 -0
  280. package/tool_call-BKyyxGaZ.mjs.map +1 -0
  281. package/tool_call-DFgzcVcU.js +608 -0
  282. package/tool_call-DFgzcVcU.js.map +1 -0
  283. package/tool_registry-Dkfprsck.js +641 -0
  284. package/tool_registry-Dkfprsck.js.map +1 -0
  285. package/tool_registry-DqLOyGyG.mjs +592 -0
  286. package/tool_registry-DqLOyGyG.mjs.map +1 -0
  287. package/turn_runner-CMm2BHdX.js +615 -0
  288. package/turn_runner-CMm2BHdX.js.map +1 -0
  289. package/turn_runner-y7eyEcJH.mjs +603 -0
  290. package/turn_runner-y7eyEcJH.mjs.map +1 -0
  291. package/turn_runner.cjs +3 -0
  292. package/turn_runner.d.ts +21 -0
  293. package/turn_runner.mjs +2 -0
  294. package/types.cjs +1 -0
  295. package/types.d.ts +56 -0
  296. package/types.mjs +0 -0
  297. package/vite-env.d.ts +23 -0
@@ -0,0 +1,296 @@
1
+ import { a as validateOrThrow, i as passesSchema, n as ValidationException, r as asyncValidateOrThrow } from "./exceptions-NrzIHw_R.mjs";
2
+ import { n as canonicalStringify, o as isError, r as Registry, s as isInstanceOf } from "./tool_registry-DqLOyGyG.mjs";
3
+ import { D as E_TOOL_DOWNSTREAM_ERROR, d as E_INVALID_INITIAL_TOOL_VALUE, h as E_INVALID_TOOL_ARGS } from "./runtime-CrEPIFgr.mjs";
4
+ import { validator } from "@nhtio/validation";
5
+ import { DateTime } from "luxon";
6
+ import { sha256 } from "js-sha256";
7
+ //#region src/lib/contracts/spooled_artifact_constructor.ts
8
+ var ARTIFACT_METHODS = [
9
+ "head",
10
+ "tail",
11
+ "grep",
12
+ "cat",
13
+ "byteLength",
14
+ "lineCount",
15
+ "estimateTokens"
16
+ ];
17
+ /**
18
+ * Validator schema used to validate a {@link SpooledArtifactConstructorLike} value.
19
+ *
20
+ * @remarks
21
+ * Because the validator is invoked at validate-time (not at module-load), it is safe to inspect
22
+ * the constructor's prototype here. The check is duck-typed: the value must be a function whose
23
+ * `prototype` carries every canonical artifact instance method (`head`, `tail`, `grep`, `cat`,
24
+ * `byteLength`, `lineCount`, `estimateTokens`). This mirrors {@link spoolReaderSchema}'s
25
+ * cross-realm-safe duck-type pattern — `instanceof SpooledArtifact` would be tighter but would
26
+ * force a value-import of the class and reopen the module cycle.
27
+ */
28
+ var spooledArtifactConstructorSchema = validator.any().custom((value, helpers) => {
29
+ if (typeof value !== "function") return helpers.error("any.invalid");
30
+ const proto = value.prototype;
31
+ if (proto === void 0 || proto === null) return helpers.error("any.invalid");
32
+ if (ARTIFACT_METHODS.every((m) => typeof proto[m] === "function")) return value;
33
+ return helpers.error("any.invalid");
34
+ });
35
+ /**
36
+ * Returns `true` if `value` is a constructor whose prototype carries every canonical
37
+ * {@link @nhtio/adk!SpooledArtifact} instance method.
38
+ *
39
+ * @remarks
40
+ * Duck-typed; does not use `instanceof SpooledArtifact`. Used by the `Tool.artifactConstructor`
41
+ * resolver validator and any other site that needs to recognise a `SpooledArtifact`-like
42
+ * constructor without pulling the class into its module-load graph.
43
+ *
44
+ * @param value - The value to test.
45
+ * @returns `true` when `value` is a `SpooledArtifact`-shaped constructor.
46
+ */
47
+ var implementsSpooledArtifactConstructor = (value) => {
48
+ return passesSchema(spooledArtifactConstructorSchema, value);
49
+ };
50
+ //#endregion
51
+ //#region src/lib/classes/tool.ts
52
+ /**
53
+ * Validator schema for a {@link RawTool}.
54
+ */
55
+ var rawToolSchema = validator.object({
56
+ name: validator.string().required(),
57
+ description: validator.string().required(),
58
+ inputSchema: validator.any().custom((value, helpers) => {
59
+ if (validator.isSchema(value) && value.type === "object") return value;
60
+ return helpers.error("any.invalid");
61
+ }).required(),
62
+ handler: validator.function().required(),
63
+ artifactConstructor: validator.any().custom((value, helpers) => {
64
+ if (typeof value !== "function") return helpers.error("any.invalid");
65
+ let resolved;
66
+ try {
67
+ resolved = value();
68
+ } catch {
69
+ return helpers.error("any.invalid");
70
+ }
71
+ if (implementsSpooledArtifactConstructor(resolved)) return value;
72
+ return helpers.error("any.invalid");
73
+ }).optional(),
74
+ meta: validator.object().pattern(validator.string(), validator.any()).default({}),
75
+ ephemeral: validator.boolean().default(false),
76
+ trusted: validator.boolean().default(false),
77
+ onCollision: validator.string().valid("throw", "replace", "keep").default("throw")
78
+ });
79
+ /**
80
+ * A tool definition that serves as the single source of truth for a callable tool: its name,
81
+ * description, input schema, execution handler, and the {@link @nhtio/adk!SpooledArtifact} subclass that
82
+ * wraps its serialised output.
83
+ *
84
+ * @typeParam A - The {@link @nhtio/adk!SpooledArtifact} subtype this tool's results should be wrapped in.
85
+ * Defaults to {@link @nhtio/adk!SpooledArtifact}.
86
+ *
87
+ * @remarks
88
+ * The `inputSchema` is a `@nhtio/validation` schema. It is used at runtime to validate incoming
89
+ * arguments before the handler is called, and its `.describe()` output provides all the metadata
90
+ * needed to build a provider-specific LLM tool definition — annotate the schema with
91
+ * `.description()`, `.note()`, `.example()` etc. once, and that information is available in both
92
+ * contexts without duplication.
93
+ *
94
+ * The handler is private — invoke it only through `executor(ctx)` which validates args, fires
95
+ * observability events (with a stable `callId` derived from the tool name and arguments), and
96
+ * wraps handler errors in {@link @nhtio/adk!E_TOOL_DOWNSTREAM_ERROR}. The handler returns serialised bytes
97
+ * (`string | Uint8Array`); persistence is the consumer's responsibility.
98
+ *
99
+ * `artifactConstructor` is the {@link @nhtio/adk!SpooledArtifact} subclass the consumer should use when
100
+ * wrapping the handler's output into a `ToolCall.results` field. The author declares it once
101
+ * on the tool instance; the consumer reads it when assembling persisted records.
102
+ */
103
+ var Tool = class Tool {
104
+ /**
105
+ * Validator schema that accepts a {@link RawTool} object.
106
+ *
107
+ * @remarks
108
+ * Reusable fragment for any schema that needs to validate or nest a tool entry
109
+ * (e.g. `TurnRunnerConfig.tools`).
110
+ */
111
+ static schema = rawToolSchema;
112
+ /**
113
+ * Returns `true` if `value` is a {@link Tool} instance.
114
+ *
115
+ * @param value - The value to test.
116
+ * @returns `true` when `value` is a {@link Tool} instance.
117
+ */
118
+ static isTool(value) {
119
+ return isInstanceOf(value, "Tool", Tool);
120
+ }
121
+ #name;
122
+ #description;
123
+ #inputSchema;
124
+ #handler;
125
+ #artifactConstructor;
126
+ #meta;
127
+ #ephemeral;
128
+ #trusted;
129
+ #onCollision;
130
+ /**
131
+ * @param raw - The raw tool input validated against `rawToolSchema`.
132
+ * @throws {@link @nhtio/adk!E_INVALID_INITIAL_TOOL_VALUE} when `raw` does not satisfy the schema.
133
+ */
134
+ constructor(raw) {
135
+ let resolved;
136
+ try {
137
+ resolved = validateOrThrow(rawToolSchema, raw, true);
138
+ } catch (err) {
139
+ throw new E_INVALID_INITIAL_TOOL_VALUE({ cause: isError(err) ? err : void 0 });
140
+ }
141
+ this.#name = resolved.name;
142
+ this.#description = resolved.description;
143
+ this.#inputSchema = resolved.inputSchema;
144
+ this.#handler = resolved.handler;
145
+ this.#artifactConstructor = resolved.artifactConstructor;
146
+ this.#meta = new Registry(resolved.meta);
147
+ this.#ephemeral = resolved.ephemeral;
148
+ this.#trusted = resolved.trusted;
149
+ this.#onCollision = resolved.onCollision;
150
+ Object.defineProperties(this, {
151
+ name: {
152
+ get: () => this.#name,
153
+ enumerable: true,
154
+ configurable: false
155
+ },
156
+ description: {
157
+ get: () => this.#description,
158
+ enumerable: true,
159
+ configurable: false
160
+ },
161
+ inputSchema: {
162
+ get: () => this.#inputSchema,
163
+ enumerable: true,
164
+ configurable: false
165
+ },
166
+ artifactConstructor: {
167
+ get: () => this.#artifactConstructor,
168
+ enumerable: true,
169
+ configurable: false
170
+ },
171
+ meta: {
172
+ get: () => this.#meta,
173
+ enumerable: true,
174
+ configurable: false
175
+ },
176
+ ephemeral: {
177
+ get: () => this.#ephemeral,
178
+ enumerable: true,
179
+ configurable: false
180
+ },
181
+ trusted: {
182
+ get: () => this.#trusted,
183
+ enumerable: true,
184
+ configurable: false
185
+ },
186
+ onCollision: {
187
+ get: () => this.#onCollision,
188
+ enumerable: true,
189
+ configurable: false
190
+ }
191
+ });
192
+ }
193
+ /**
194
+ * Validates `args` against the tool's input schema asynchronously.
195
+ *
196
+ * @remarks
197
+ * Async to support schemas with external validators (e.g. database lookups, API calls).
198
+ * A validation failure throws {@link @nhtio/adk!E_INVALID_TOOL_ARGS} — this indicates a programming error
199
+ * in the tool call loop, not a downstream failure.
200
+ *
201
+ * @param args - The arguments to validate.
202
+ * @returns The validated (and coerced) arguments.
203
+ * @throws {@link @nhtio/adk!E_INVALID_TOOL_ARGS} when `args` does not satisfy the input schema.
204
+ */
205
+ async validate(args) {
206
+ try {
207
+ return await asyncValidateOrThrow(this.#inputSchema, args);
208
+ } catch (err) {
209
+ if (isInstanceOf(err, "ValidationException", ValidationException)) throw new E_INVALID_TOOL_ARGS({ cause: err });
210
+ throw err;
211
+ }
212
+ }
213
+ /**
214
+ * Returns a bound executor function for this tool against the given turn context.
215
+ *
216
+ * @remarks
217
+ * The executor: (1) computes a stable `callId` as `sha256(canonicalStringify({tool, args}))`
218
+ * over the **raw, pre-validation args**, (2) validates `args` via {@link Tool.validate},
219
+ * (3) emits `toolExecutionStart` on the context (with the computed `callId`), (4) calls the
220
+ * handler, (5) emits `toolExecutionEnd` (with the same `callId`), (6) wraps any handler error
221
+ * in {@link @nhtio/adk!E_TOOL_DOWNSTREAM_ERROR} before re-throwing.
222
+ *
223
+ * The handler returns serialised bytes (`string | Uint8Array`) — persistence is the consumer's
224
+ * concern. Use {@link Tool.artifactConstructor} when wrapping the bytes into a
225
+ * `ToolCall.results` field.
226
+ *
227
+ * Pattern mirrors `Middleware.runner()` — call once per turn, reuse the returned function.
228
+ *
229
+ * @param ctx - The active turn context. Provides emit functions and turn ID.
230
+ * @returns An async function `(args) => Promise<string | Uint8Array>`.
231
+ */
232
+ executor(ctx) {
233
+ return async (args) => {
234
+ const callId = sha256(canonicalStringify({
235
+ tool: this.#name,
236
+ args
237
+ }));
238
+ const validatedArgs = await this.validate(args);
239
+ const startedAt = DateTime.now();
240
+ ctx.emitToolExecutionStart({
241
+ toolName: this.#name,
242
+ turnId: ctx.id,
243
+ callId,
244
+ args: validatedArgs,
245
+ startedAt
246
+ });
247
+ try {
248
+ const result = await this.#handler(validatedArgs, ctx, this.#meta);
249
+ const endedAt = DateTime.now();
250
+ ctx.emitToolExecutionEnd({
251
+ toolName: this.#name,
252
+ turnId: ctx.id,
253
+ callId,
254
+ startedAt,
255
+ endedAt,
256
+ durationMs: endedAt.diff(startedAt).milliseconds,
257
+ isError: false
258
+ });
259
+ return result;
260
+ } catch (err) {
261
+ const endedAt = DateTime.now();
262
+ ctx.emitToolExecutionEnd({
263
+ toolName: this.#name,
264
+ turnId: ctx.id,
265
+ callId,
266
+ startedAt,
267
+ endedAt,
268
+ durationMs: endedAt.diff(startedAt).milliseconds,
269
+ isError: true
270
+ });
271
+ throw new E_TOOL_DOWNSTREAM_ERROR({ cause: isError(err) ? err : void 0 });
272
+ }
273
+ };
274
+ }
275
+ /**
276
+ * Returns a fully serialisable snapshot of this tool's definition.
277
+ *
278
+ * @remarks
279
+ * The `inputSchema` property is the result of calling `.describe()` on the raw schema — a plain
280
+ * object carrying all the annotation metadata (descriptions, notes, examples, types) without any
281
+ * validator functions. Use this to build provider-specific LLM tool definitions.
282
+ *
283
+ * @returns `{ name, description, inputSchema }` where `inputSchema` is the schema description.
284
+ */
285
+ describe() {
286
+ return {
287
+ name: this.#name,
288
+ description: this.#description,
289
+ inputSchema: this.#inputSchema.describe()
290
+ };
291
+ }
292
+ };
293
+ //#endregion
294
+ export { Tool as t };
295
+
296
+ //# sourceMappingURL=tool-D2WB1EA1.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-D2WB1EA1.mjs","names":["#name","#description","#inputSchema","#handler","#artifactConstructor","#meta","#ephemeral","#trusted","#onCollision"],"sources":["../src/lib/contracts/spooled_artifact_constructor.ts","../src/lib/classes/tool.ts"],"sourcesContent":["import { validator } from '@nhtio/validation'\nimport { passesSchema } from '../utils/validation'\nimport type { SpooledArtifact } from '../classes/spooled_artifact'\n\n/**\n * Constructor signature for any {@link @nhtio/adk!SpooledArtifact} (the class itself or a subclass).\n *\n * @remarks\n * Re-declared here at the contract level so consumers — and the `Tool.artifactConstructor`\n * resolver validator in particular — can talk about the constructor shape without value-importing\n * the {@link @nhtio/adk!SpooledArtifact} class (which would close the `tool.ts` ↔ `spooled_artifact.ts` ↔\n * `artifact_tool.ts` module cycle at load time and TDZ-crash `ArtifactTool extends Tool`).\n *\n * @typeParam A - The {@link @nhtio/adk!SpooledArtifact} subtype the constructor produces.\n */\nexport type SpooledArtifactConstructorLike<A extends SpooledArtifact = SpooledArtifact> = new (\n ...args: any[]\n) => A\n\nconst ARTIFACT_METHODS = [\n 'head',\n 'tail',\n 'grep',\n 'cat',\n 'byteLength',\n 'lineCount',\n 'estimateTokens',\n] as const\n\n/**\n * Validator schema used to validate a {@link SpooledArtifactConstructorLike} value.\n *\n * @remarks\n * Because the validator is invoked at validate-time (not at module-load), it is safe to inspect\n * the constructor's prototype here. The check is duck-typed: the value must be a function whose\n * `prototype` carries every canonical artifact instance method (`head`, `tail`, `grep`, `cat`,\n * `byteLength`, `lineCount`, `estimateTokens`). This mirrors {@link spoolReaderSchema}'s\n * cross-realm-safe duck-type pattern — `instanceof SpooledArtifact` would be tighter but would\n * force a value-import of the class and reopen the module cycle.\n */\nexport const spooledArtifactConstructorSchema = validator.any().custom((value, helpers) => {\n if (typeof value !== 'function') return helpers.error('any.invalid')\n const proto = (value as { prototype?: unknown }).prototype\n if (proto === undefined || proto === null) return helpers.error('any.invalid')\n if (ARTIFACT_METHODS.every((m) => typeof (proto as Record<string, unknown>)[m] === 'function')) {\n return value\n }\n return helpers.error('any.invalid')\n})\n\n/**\n * Returns `true` if `value` is a constructor whose prototype carries every canonical\n * {@link @nhtio/adk!SpooledArtifact} instance method.\n *\n * @remarks\n * Duck-typed; does not use `instanceof SpooledArtifact`. Used by the `Tool.artifactConstructor`\n * resolver validator and any other site that needs to recognise a `SpooledArtifact`-like\n * constructor without pulling the class into its module-load graph.\n *\n * @param value - The value to test.\n * @returns `true` when `value` is a `SpooledArtifact`-shaped constructor.\n */\nexport const implementsSpooledArtifactConstructor = (\n value: unknown\n): value is SpooledArtifactConstructorLike => {\n return passesSchema(spooledArtifactConstructorSchema, value)\n}\n","import { DateTime } from 'luxon'\nimport { sha256 } from 'js-sha256'\nimport { Registry } from './registry'\nimport { validator } from '@nhtio/validation'\nimport { isInstanceOf, isError } from '../utils/guards'\nimport { canonicalStringify } from '../utils/canonical_json'\nimport { validateOrThrow, asyncValidateOrThrow, ValidationException } from '../utils/validation'\nimport { implementsSpooledArtifactConstructor } from '../contracts/spooled_artifact_constructor'\nimport {\n E_INVALID_INITIAL_TOOL_VALUE,\n E_INVALID_TOOL_ARGS,\n E_TOOL_DOWNSTREAM_ERROR,\n} from '../exceptions/runtime'\nimport type { Media } from './media'\nimport type { Schema, Description } from '@nhtio/validation'\nimport type { DispatchContext } from '../contracts/dispatch_context'\nimport type { SpooledArtifact, SpooledArtifactConstructor } from './spooled_artifact'\n\n/**\n * A zero-arg function that returns the {@link @nhtio/adk!SpooledArtifactConstructor} the consumer should\n * use when wrapping this tool's serialised output into a `ToolCall.results` field.\n *\n * @remarks\n * Why a resolver (and not the constructor itself)? `tool.ts` participates in a module-load\n * cycle with `spooled_artifact.ts` and `artifact_tool.ts` (`ArtifactTool extends Tool` closes\n * the loop). Any eager value-level reference to `SpooledArtifact` in `tool.ts` would crash the\n * cycle with a TDZ error. A resolver lets `tool.ts` validate \"is a function\" at module-load\n * time and defer the actual constructor check to validate-time (which always runs after every\n * module body has executed). Wrap-sites invoke `tool.artifactConstructor?.() ?? SpooledArtifact`\n * to obtain the final constructor.\n */\nexport type ArtifactConstructorResolver<A extends SpooledArtifact = SpooledArtifact> =\n () => SpooledArtifactConstructor<A>\n\n/**\n * The execution function for a {@link Tool}.\n *\n * @remarks\n * Receives the raw arguments passed to the executor, the active {@link @nhtio/adk!DispatchContext}, and the\n * tool's metadata registry.\n *\n * Return shapes:\n * - `string` / `Uint8Array` — opaque serialised output. The ADK does not persist the bytes\n * itself; the consumer's executor middleware is responsible for storing them and wrapping\n * them via `tool.artifactConstructor?.() ?? SpooledArtifact` when assembling the `ToolCall`\n * record.\n * - {@link @nhtio/adk!Media} / `Media[]` — explicit-modality silo. Bypasses\n * {@link Tool.artifactConstructor} — the handler returns the final result shape directly.\n * The LLM battery renders each `Media` as a provider-specific content block.\n */\nexport type ToolHandler = (\n args: unknown,\n ctx: DispatchContext,\n meta: Registry\n) => string | Uint8Array | Media | Media[] | Promise<string | Uint8Array | Media | Media[]>\n\n/**\n * Plain input object supplied to {@link Tool} at construction time.\n *\n * @typeParam A - The {@link @nhtio/adk!SpooledArtifact} subtype used to wrap this tool's results when\n * the consumer assembles a `ToolCall.results` field. Defaults to {@link @nhtio/adk!SpooledArtifact}\n * (plain text). Tools producing JSON output should set this to `SpooledJsonArtifact`; tools\n * producing markdown should set it to `SpooledMarkdownArtifact`; consumers can also pass a\n * custom subclass.\n */\nexport interface RawTool<A extends SpooledArtifact = SpooledArtifact> {\n /** Unique identifier used in LLM tool definitions. Recommend lowercase snake_case. */\n name: string\n /** Human-readable description passed to the model to explain what the tool does. */\n description: string\n /** @nhtio/validation schema for the tool's input arguments. Annotate with `.description()`, `.note()`, `.example()` etc. to produce rich LLM tool definitions via `.describe()`. */\n inputSchema: Schema\n /** Execution function. Not exposed as a public property — invoke via `executor()`. */\n handler: ToolHandler\n /**\n * Zero-arg resolver returning the {@link @nhtio/adk!SpooledArtifactConstructor} the consumer should use\n * when wrapping this tool's serialised output into a `ToolCall.results` field. Optional —\n * when omitted, wrap-sites fall back to {@link @nhtio/adk!SpooledArtifact} (plain text).\n *\n * @remarks\n * Recommended call shape: `artifactConstructor: () => SpooledJsonArtifact`. The closure is\n * the indirection that lets `tool.ts` validate this field without eagerly importing\n * `SpooledArtifact` (which would crash the `tool.ts ↔ spooled_artifact.ts ↔ artifact_tool.ts`\n * module-load cycle). At validate time the schema invokes the resolver and verifies its\n * return value is a `SpooledArtifact`-derived constructor — wrong-shape resolvers throw\n * {@link @nhtio/adk!E_INVALID_INITIAL_TOOL_VALUE}.\n *\n * Wrap-sites (storage batteries, scripted executors) read the constructor via\n * `tool.artifactConstructor?.() ?? SpooledArtifact`.\n */\n artifactConstructor?: ArtifactConstructorResolver<A>\n /** Optional arbitrary metadata for this tool (e.g. RBAC scopes, feature flags). Defaults to `{}`. Stored in a {@link @nhtio/adk!Registry} for dot-path access. */\n meta?: Record<string, unknown>\n /**\n * When `true`, marks this tool as owned by a specific {@link @nhtio/adk!DispatchContext} so that\n * `ToolRegistry.pruneEphemeral()` will drop it at ctx-completion.\n *\n * @remarks\n * The flag is advisory at the `Tool` level — registries decide what to do with it. The canonical\n * producer of ephemeral tools is `SpooledArtifact.forgeTools(ctx)`, which sets this to `true`\n * on every artifact-query tool it emits.\n *\n * @defaultValue `false`\n */\n ephemeral?: boolean\n /**\n * When `true`, declares that this tool's output should be treated as **trusted developer/user\n * intent** rather than as untrusted third-party text when surfaced to the model.\n *\n * @remarks\n * LLM batteries read this flag per call when rendering tool-call results. The default\n * untrusted envelope (e.g. `<untrusted_content>` in the OpenAI Chat Completions battery) is the\n * secure-by-default treatment for arbitrary tool output. A tool whose output is authored by the\n * user or operator (Q&A tools surfacing user-authored answers, human-in-the-loop approval\n * gates, feedback-collection tools, configuration tools returning developer-authored\n * constants) sets this to `true` so the LLM battery routes the result through its trusted\n * envelope (`<trusted_content>` in the OpenAI Chat Completions battery).\n *\n * Trust is a property of the tool's output, not a property of how a particular battery is\n * wired — putting the flag here means the trust signal travels with the tool wherever it is\n * registered, no per-battery string lists, no name-matching to fail-open on typos.\n *\n * @defaultValue `false`\n */\n trusted?: boolean\n /**\n * Self-declared merge collision policy. Honoured by `ToolRegistry.merge` (NOT by\n * `ToolRegistry.register`) when this tool collides with another of the same name.\n *\n * @remarks\n * - `'throw'` (default): defer to the merge-level `options.onCollision`. If that is also\n * `'throw'`, the merge raises `E_TOOL_ALREADY_REGISTERED`. This matches the default behaviour\n * of `ToolRegistry.register`.\n * - `'replace'`: this tool always wins the collision, regardless of the merge-level option.\n * - `'keep'`: this tool always loses to any previously-registered tool of the same name.\n *\n * Forged artifact-query tools set this to `'replace'` so that merging multiple\n * `Subclass.forgeTools(ctx)` outputs (whose base-method tools overlap by name) resolves\n * silently — the descriptors, snapshot, and handler behaviour are interchangeable across\n * subclasses, so replacement is a behavioural no-op.\n *\n * @defaultValue `'throw'`\n */\n onCollision?: 'throw' | 'replace' | 'keep'\n}\n\n/**\n * Validator schema for a {@link RawTool}.\n */\nconst rawToolSchema = validator.object<RawTool>({\n name: validator.string().required(),\n description: validator.string().required(),\n inputSchema: validator\n .any()\n .custom((value, helpers) => {\n if (validator.isSchema(value) && (value as any).type === 'object') return value\n return helpers.error('any.invalid')\n })\n .required(),\n handler: validator.function().required(),\n artifactConstructor: validator\n .any()\n .custom((value, helpers) => {\n if (typeof value !== 'function') return helpers.error('any.invalid')\n // The resolver runs at validate time — well after the tool.ts ↔ spooled_artifact.ts ↔\n // artifact_tool.ts module cycle has fully unwound — so invoking it is safe. Delegate the\n // \"is this a SpooledArtifact-shaped constructor?\" check to the contract-level guard so\n // there's one canonical duck-type test (mirrors `implementsSpoolReader`'s pattern).\n let resolved: unknown\n try {\n resolved = (value as () => unknown)()\n } catch {\n return helpers.error('any.invalid')\n }\n if (implementsSpooledArtifactConstructor(resolved)) return value\n return helpers.error('any.invalid')\n })\n .optional(),\n meta: validator.object().pattern(validator.string(), validator.any()).default({}),\n ephemeral: validator.boolean().default(false),\n trusted: validator.boolean().default(false),\n onCollision: validator.string().valid('throw', 'replace', 'keep').default('throw'),\n})\n\n/**\n * A tool definition that serves as the single source of truth for a callable tool: its name,\n * description, input schema, execution handler, and the {@link @nhtio/adk!SpooledArtifact} subclass that\n * wraps its serialised output.\n *\n * @typeParam A - The {@link @nhtio/adk!SpooledArtifact} subtype this tool's results should be wrapped in.\n * Defaults to {@link @nhtio/adk!SpooledArtifact}.\n *\n * @remarks\n * The `inputSchema` is a `@nhtio/validation` schema. It is used at runtime to validate incoming\n * arguments before the handler is called, and its `.describe()` output provides all the metadata\n * needed to build a provider-specific LLM tool definition — annotate the schema with\n * `.description()`, `.note()`, `.example()` etc. once, and that information is available in both\n * contexts without duplication.\n *\n * The handler is private — invoke it only through `executor(ctx)` which validates args, fires\n * observability events (with a stable `callId` derived from the tool name and arguments), and\n * wraps handler errors in {@link @nhtio/adk!E_TOOL_DOWNSTREAM_ERROR}. The handler returns serialised bytes\n * (`string | Uint8Array`); persistence is the consumer's responsibility.\n *\n * `artifactConstructor` is the {@link @nhtio/adk!SpooledArtifact} subclass the consumer should use when\n * wrapping the handler's output into a `ToolCall.results` field. The author declares it once\n * on the tool instance; the consumer reads it when assembling persisted records.\n */\nexport class Tool<A extends SpooledArtifact = SpooledArtifact> {\n /**\n * Validator schema that accepts a {@link RawTool} object.\n *\n * @remarks\n * Reusable fragment for any schema that needs to validate or nest a tool entry\n * (e.g. `TurnRunnerConfig.tools`).\n */\n public static schema = rawToolSchema\n\n /**\n * Returns `true` if `value` is a {@link Tool} instance.\n *\n * @param value - The value to test.\n * @returns `true` when `value` is a {@link Tool} instance.\n */\n public static isTool(value: unknown): value is Tool {\n return isInstanceOf(value, 'Tool', Tool)\n }\n\n declare readonly name: string\n declare readonly description: string\n declare readonly inputSchema: Schema\n declare readonly artifactConstructor: ArtifactConstructorResolver<A> | undefined\n declare readonly meta: Registry\n declare readonly ephemeral: boolean\n declare readonly trusted: boolean\n declare readonly onCollision: 'throw' | 'replace' | 'keep'\n\n #name: string\n #description: string\n #inputSchema: Schema\n #handler: ToolHandler\n #artifactConstructor: ArtifactConstructorResolver<A> | undefined\n #meta: Registry\n #ephemeral: boolean\n #trusted: boolean\n #onCollision: 'throw' | 'replace' | 'keep'\n\n /**\n * @param raw - The raw tool input validated against `rawToolSchema`.\n * @throws {@link @nhtio/adk!E_INVALID_INITIAL_TOOL_VALUE} when `raw` does not satisfy the schema.\n */\n constructor(raw: RawTool<A>) {\n let resolved: RawTool<A> & {\n meta: Record<string, unknown>\n ephemeral: boolean\n trusted: boolean\n onCollision: 'throw' | 'replace' | 'keep'\n }\n try {\n resolved = validateOrThrow<typeof resolved>(\n rawToolSchema,\n raw as RawTool,\n true\n ) as typeof resolved\n } catch (err) {\n throw new E_INVALID_INITIAL_TOOL_VALUE({ cause: isError(err) ? err : undefined })\n }\n\n this.#name = resolved.name\n this.#description = resolved.description\n this.#inputSchema = resolved.inputSchema\n this.#handler = resolved.handler\n this.#artifactConstructor = resolved.artifactConstructor as\n | ArtifactConstructorResolver<A>\n | undefined\n this.#meta = new Registry(resolved.meta)\n this.#ephemeral = resolved.ephemeral\n this.#trusted = resolved.trusted\n this.#onCollision = resolved.onCollision\n\n Object.defineProperties(this, {\n name: {\n get: () => this.#name,\n enumerable: true,\n configurable: false,\n },\n description: {\n get: () => this.#description,\n enumerable: true,\n configurable: false,\n },\n inputSchema: {\n get: () => this.#inputSchema,\n enumerable: true,\n configurable: false,\n },\n artifactConstructor: {\n get: () => this.#artifactConstructor,\n enumerable: true,\n configurable: false,\n },\n meta: {\n get: () => this.#meta,\n enumerable: true,\n configurable: false,\n },\n ephemeral: {\n get: () => this.#ephemeral,\n enumerable: true,\n configurable: false,\n },\n trusted: {\n get: () => this.#trusted,\n enumerable: true,\n configurable: false,\n },\n onCollision: {\n get: () => this.#onCollision,\n enumerable: true,\n configurable: false,\n },\n })\n }\n\n /**\n * Validates `args` against the tool's input schema asynchronously.\n *\n * @remarks\n * Async to support schemas with external validators (e.g. database lookups, API calls).\n * A validation failure throws {@link @nhtio/adk!E_INVALID_TOOL_ARGS} — this indicates a programming error\n * in the tool call loop, not a downstream failure.\n *\n * @param args - The arguments to validate.\n * @returns The validated (and coerced) arguments.\n * @throws {@link @nhtio/adk!E_INVALID_TOOL_ARGS} when `args` does not satisfy the input schema.\n */\n async validate(args: unknown): Promise<unknown> {\n try {\n return await asyncValidateOrThrow(this.#inputSchema, args)\n } catch (err) {\n if (isInstanceOf(err, 'ValidationException', ValidationException)) {\n throw new E_INVALID_TOOL_ARGS({ cause: err })\n }\n throw err\n }\n }\n\n /**\n * Returns a bound executor function for this tool against the given turn context.\n *\n * @remarks\n * The executor: (1) computes a stable `callId` as `sha256(canonicalStringify({tool, args}))`\n * over the **raw, pre-validation args**, (2) validates `args` via {@link Tool.validate},\n * (3) emits `toolExecutionStart` on the context (with the computed `callId`), (4) calls the\n * handler, (5) emits `toolExecutionEnd` (with the same `callId`), (6) wraps any handler error\n * in {@link @nhtio/adk!E_TOOL_DOWNSTREAM_ERROR} before re-throwing.\n *\n * The handler returns serialised bytes (`string | Uint8Array`) — persistence is the consumer's\n * concern. Use {@link Tool.artifactConstructor} when wrapping the bytes into a\n * `ToolCall.results` field.\n *\n * Pattern mirrors `Middleware.runner()` — call once per turn, reuse the returned function.\n *\n * @param ctx - The active turn context. Provides emit functions and turn ID.\n * @returns An async function `(args) => Promise<string | Uint8Array>`.\n */\n executor(\n ctx: DispatchContext\n ): (args: unknown) => Promise<string | Uint8Array | Media | Media[]> {\n return async (args: unknown): Promise<string | Uint8Array | Media | Media[]> => {\n // Compute callId over raw args (pre-validation) so two invocations with the same\n // (tool, raw args) produce the same identifier even if validation coerces values.\n const callId = sha256(canonicalStringify({ tool: this.#name, args }))\n const validatedArgs = await this.validate(args)\n const startedAt = DateTime.now()\n ctx.emitToolExecutionStart({\n toolName: this.#name,\n turnId: ctx.id,\n callId,\n args: validatedArgs,\n startedAt,\n })\n try {\n const result = await this.#handler(validatedArgs, ctx, this.#meta)\n const endedAt = DateTime.now()\n ctx.emitToolExecutionEnd({\n toolName: this.#name,\n turnId: ctx.id,\n callId,\n startedAt,\n endedAt,\n durationMs: endedAt.diff(startedAt).milliseconds,\n isError: false,\n })\n return result\n } catch (err) {\n const endedAt = DateTime.now()\n ctx.emitToolExecutionEnd({\n toolName: this.#name,\n turnId: ctx.id,\n callId,\n startedAt,\n endedAt,\n durationMs: endedAt.diff(startedAt).milliseconds,\n isError: true,\n })\n throw new E_TOOL_DOWNSTREAM_ERROR({ cause: isError(err) ? err : undefined })\n }\n }\n }\n\n /**\n * Returns a fully serialisable snapshot of this tool's definition.\n *\n * @remarks\n * The `inputSchema` property is the result of calling `.describe()` on the raw schema — a plain\n * object carrying all the annotation metadata (descriptions, notes, examples, types) without any\n * validator functions. Use this to build provider-specific LLM tool definitions.\n *\n * @returns `{ name, description, inputSchema }` where `inputSchema` is the schema description.\n */\n describe(): { name: string; description: string; inputSchema: Description } {\n return {\n name: this.#name,\n description: this.#description,\n inputSchema: this.#inputSchema.describe(),\n }\n }\n}\n"],"mappings":";;;;;;;AAmBA,IAAM,mBAAmB;CACvB;CACA;CACA;CACA;CACA;CACA;CACA;AACF;;;;;;;;;;;;AAaA,IAAa,mCAAmC,UAAU,IAAI,EAAE,QAAQ,OAAO,YAAY;CACzF,IAAI,OAAO,UAAU,YAAY,OAAO,QAAQ,MAAM,aAAa;CACnE,MAAM,Q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