@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,578 @@
1
+ import { a as validateOrThrow, i as passesSchema } from "./exceptions-NrzIHw_R.mjs";
2
+ import { a as Tokenizable, c as isObject, o as isError, r as Registry, s as isInstanceOf } from "./tool_registry-DqLOyGyG.mjs";
3
+ import { S as E_NOT_A_MEDIA_READER, i as E_INVALID_INITIAL_MEDIA_VALUE, u as E_INVALID_INITIAL_TOOL_CALL_VALUE } from "./runtime-CrEPIFgr.mjs";
4
+ import { t as SpooledArtifact } from "./spooled_artifact-C5ZtGxuJ.mjs";
5
+ import { validator } from "@nhtio/validation";
6
+ import { v6 } from "uuid";
7
+ //#region src/lib/contracts/media_reader.ts
8
+ /**
9
+ * Validator schema used to validate a MediaReader value.
10
+ *
11
+ * @remarks
12
+ * Because MediaReader is a structural interface with no associated constructor, validation is
13
+ * duck-typed: the value must be an object, class instance, or function with `stream` and
14
+ * `byteLength` present as callable properties. Arity is not enforced.
15
+ */
16
+ var mediaReaderSchema = validator.any().required().custom((value, helpers) => {
17
+ if (value !== null && value !== void 0 && typeof value.stream === "function" && typeof value.byteLength === "function") return value;
18
+ return helpers.error("any.invalid");
19
+ });
20
+ /**
21
+ * Returns `true` if `value` implements the MediaReader interface.
22
+ *
23
+ * @remarks
24
+ * Duck-typed: checks that `value` is non-null with `stream` and `byteLength` as callable
25
+ * functions. Does not use `instanceof` — there is no MediaReader constructor.
26
+ *
27
+ * @param value - The value to test.
28
+ * @returns `true` when `value` conforms to the MediaReader interface.
29
+ */
30
+ var implementsMediaReader = (value) => {
31
+ return passesSchema(mediaReaderSchema, value);
32
+ };
33
+ //#endregion
34
+ //#region src/lib/classes/media.ts
35
+ /**
36
+ * The set of supported media kinds.
37
+ *
38
+ * @remarks
39
+ * Modality coverage is asymmetric across providers. The framework defines no
40
+ * `supportedModalities` field — how a battery handles a modality it cannot natively render is
41
+ * the battery author's call (see `unsupportedMediaPolicy` on the OpenAI Chat Completions
42
+ * battery).
43
+ */
44
+ var MediaKind = [
45
+ "image",
46
+ "audio",
47
+ "video",
48
+ "document"
49
+ ];
50
+ /**
51
+ * Provenance axis. *Who is the framework willing to vouch for as the source of these bytes?*
52
+ *
53
+ * @remarks
54
+ * Mirrors `RetrievableTrustTier` deliberately — same vocabulary, same question:
55
+ * *did this content come from a place the agent should treat as authoritative?*
56
+ *
57
+ * - `'first-party'` — deployer-vetted bytes (tool output the operator authored, signed
58
+ * internal assets).
59
+ * - `'third-party-public'` — open-web fetches, public APIs, public corpora.
60
+ * - `'third-party-private'` — user uploads, partner APIs, private corpora.
61
+ */
62
+ var MediaTrustTier = [
63
+ "first-party",
64
+ "third-party-public",
65
+ "third-party-private"
66
+ ];
67
+ /**
68
+ * Modality-hazard axis. *How dangerous is it to let the model decode these bytes?*
69
+ *
70
+ * @remarks
71
+ * Orthogonal to provenance — a first-party trusted PDF can still carry hidden text layers; a
72
+ * third-party-public raw image can still be encoded as opaque pixels with adversarial
73
+ * perturbations.
74
+ *
75
+ * - `'inert'` — bytes the model never decodes as instructions (e.g. a handle that is never
76
+ * inlined into the prompt).
77
+ * - `'extractable-instructions'` — text-bearing media: PDFs, screenshots with UI text, documents.
78
+ * Hazard is OCR / embedded-text-layer reads.
79
+ * - `'opaque-perceptual'` — raw vision/audio/video the model encodes directly. Hazard is
80
+ * steganographic LSB prompts, adversarial perturbations, ultrasonic audio — invisible to any
81
+ * pre-screen.
82
+ *
83
+ * See `/the-loop/trust-tiers/media` and its research sub-page `/the-loop/trust-tiers/media/research`.
84
+ */
85
+ var MediaModalityHazard = [
86
+ "inert",
87
+ "extractable-instructions",
88
+ "opaque-perceptual"
89
+ ];
90
+ var stashEntrySchema = validator.object({
91
+ value: validator.any().required(),
92
+ trustTier: validator.string().valid(...MediaTrustTier).required(),
93
+ derivedFromMedia: validator.string().optional()
94
+ }).unknown(false);
95
+ /**
96
+ * Validator schema used to validate a {@link RawMedia} before constructing a {@link Media}.
97
+ */
98
+ var rawMediaSchema = validator.object({
99
+ id: validator.string().optional(),
100
+ kind: validator.string().valid(...MediaKind).required(),
101
+ mimeType: validator.string().required(),
102
+ filename: validator.string().required(),
103
+ reader: mediaReaderSchema.required(),
104
+ trustTier: validator.string().valid(...MediaTrustTier).required(),
105
+ modalityHazard: validator.string().valid(...MediaModalityHazard).required(),
106
+ source: validator.string().optional(),
107
+ stash: validator.object().pattern(validator.string(), stashEntrySchema).optional()
108
+ });
109
+ var conservativeHazardForKind = (kind) => {
110
+ return kind === "document" ? "extractable-instructions" : "opaque-perceptual";
111
+ };
112
+ /**
113
+ * Cross-environment base64 encoder for a `Uint8Array`.
114
+ *
115
+ * @remarks
116
+ * Prefers Node's `Buffer.from(buf).toString('base64')` when `globalThis.Buffer` exists; otherwise
117
+ * chunk-encodes through `btoa` with a 0x8000-byte window to avoid `Maximum call stack size
118
+ * exceeded` on large buffers.
119
+ */
120
+ var encodeBase64 = (bytes) => {
121
+ const maybeBuffer = globalThis.Buffer;
122
+ if (maybeBuffer && typeof maybeBuffer.from === "function") return maybeBuffer.from(bytes).toString("base64");
123
+ const chunkSize = 32768;
124
+ let binary = "";
125
+ for (let i = 0; i < bytes.length; i += chunkSize) {
126
+ const chunk = bytes.subarray(i, i + chunkSize);
127
+ binary += String.fromCharCode.apply(null, Array.from(chunk));
128
+ }
129
+ return btoa(binary);
130
+ };
131
+ /**
132
+ * Lazy, re-openable view over a binary asset (image, audio, video, document).
133
+ *
134
+ * @remarks
135
+ * Dual-peer to {@link @nhtio/adk!Tokenizable} (silo) and {@link @nhtio/adk!SpooledArtifact}
136
+ * (handle). Wraps a {@link @nhtio/adk!MediaReader} contract — the framework owns the contract, the
137
+ * implementor owns the storage backend. Bytes are reached only through the reader; the primitive
138
+ * itself never inlines bytes.
139
+ *
140
+ * Construction requires `trustTier` and `modalityHazard` — the framework refuses to guess
141
+ * provenance or decoding hazard. Ergonomic factories ({@link Media.userAttachment},
142
+ * {@link Media.toolGenerated}, {@link Media.retrievedPublic}, {@link Media.retrievedPrivate})
143
+ * force the labelling decision at the call site without becoming defaults on the bare
144
+ * constructor.
145
+ */
146
+ var Media = class Media {
147
+ /**
148
+ * Validator schema that accepts a {@link RawMedia} object.
149
+ */
150
+ static schema = rawMediaSchema;
151
+ /**
152
+ * The set of recognised media kinds. Exposed for downstream schemas that need to discriminate
153
+ * on `kind`.
154
+ */
155
+ static MediaKind = MediaKind;
156
+ /**
157
+ * The set of recognised trust tiers.
158
+ */
159
+ static MediaTrustTier = MediaTrustTier;
160
+ /**
161
+ * The set of recognised modality hazards.
162
+ */
163
+ static MediaModalityHazard = MediaModalityHazard;
164
+ /**
165
+ * Returns `true` if `value` is a {@link Media} instance.
166
+ *
167
+ * @remarks
168
+ * Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety.
169
+ *
170
+ * @param value - The value to test.
171
+ * @returns `true` when `value` is a {@link Media} instance.
172
+ */
173
+ static isMedia(value) {
174
+ return isInstanceOf(value, "Media", Media);
175
+ }
176
+ #id;
177
+ #kind;
178
+ #mimeType;
179
+ #filename;
180
+ #source;
181
+ #trustTier;
182
+ #modalityHazard;
183
+ #reader;
184
+ #stash;
185
+ /**
186
+ * @param raw - The raw media input validated against `rawMediaSchema`.
187
+ * @throws {@link @nhtio/adk/exceptions!E_INVALID_INITIAL_MEDIA_VALUE} when `raw` does not satisfy the schema.
188
+ * @throws {@link @nhtio/adk/exceptions!E_NOT_A_MEDIA_READER} when `raw.reader` does not implement {@link @nhtio/adk!MediaReader}.
189
+ */
190
+ constructor(raw) {
191
+ let resolved;
192
+ try {
193
+ resolved = validateOrThrow(rawMediaSchema, raw, true);
194
+ } catch (err) {
195
+ throw new E_INVALID_INITIAL_MEDIA_VALUE({ cause: isError(err) ? err : void 0 });
196
+ }
197
+ if (!implementsMediaReader(resolved.reader)) throw new E_NOT_A_MEDIA_READER();
198
+ this.#id = resolved.id ?? v6();
199
+ this.#kind = resolved.kind;
200
+ this.#mimeType = resolved.mimeType;
201
+ this.#filename = resolved.filename;
202
+ this.#source = resolved.source;
203
+ this.#trustTier = resolved.trustTier;
204
+ this.#modalityHazard = resolved.modalityHazard;
205
+ this.#reader = resolved.reader;
206
+ this.#stash = new Registry(resolved.stash);
207
+ Object.defineProperties(this, {
208
+ id: {
209
+ get: () => this.#id,
210
+ enumerable: true,
211
+ configurable: false
212
+ },
213
+ kind: {
214
+ get: () => this.#kind,
215
+ enumerable: true,
216
+ configurable: false
217
+ },
218
+ mimeType: {
219
+ get: () => this.#mimeType,
220
+ enumerable: true,
221
+ configurable: false
222
+ },
223
+ filename: {
224
+ get: () => this.#filename,
225
+ enumerable: true,
226
+ configurable: false
227
+ },
228
+ source: {
229
+ get: () => this.#source,
230
+ enumerable: true,
231
+ configurable: false
232
+ },
233
+ trustTier: {
234
+ get: () => this.#trustTier,
235
+ enumerable: true,
236
+ configurable: false
237
+ },
238
+ modalityHazard: {
239
+ get: () => this.#modalityHazard,
240
+ enumerable: true,
241
+ configurable: false
242
+ },
243
+ stash: {
244
+ get: () => this.#stash,
245
+ enumerable: true,
246
+ configurable: false
247
+ }
248
+ });
249
+ }
250
+ /**
251
+ * Re-opens the underlying byte source and returns a fresh ReadableStream.
252
+ *
253
+ * @returns A drainable `ReadableStream` over the underlying bytes.
254
+ */
255
+ async stream() {
256
+ return this.#reader.stream();
257
+ }
258
+ /**
259
+ * Returns the total number of bytes in the underlying data, or `undefined` if unknown.
260
+ *
261
+ * @returns The byte length, or `undefined` when the underlying source cannot report it.
262
+ */
263
+ async byteLength() {
264
+ return this.#reader.byteLength();
265
+ }
266
+ /**
267
+ * Drains the reader's stream and returns the underlying bytes as a single `Uint8Array`.
268
+ *
269
+ * @remarks
270
+ * Convenience for callers that need the full buffer (e.g. inline base64 encoding). Forces
271
+ * full materialisation — large assets should be piped through {@link Media.stream} instead.
272
+ */
273
+ async asBytes() {
274
+ const reader = (await this.stream()).getReader();
275
+ const chunks = [];
276
+ let total = 0;
277
+ while (true) {
278
+ const { value, done } = await reader.read();
279
+ if (done) break;
280
+ if (value) {
281
+ chunks.push(value);
282
+ total += value.byteLength;
283
+ }
284
+ }
285
+ const out = new Uint8Array(total);
286
+ let offset = 0;
287
+ for (const chunk of chunks) {
288
+ out.set(chunk, offset);
289
+ offset += chunk.byteLength;
290
+ }
291
+ return out;
292
+ }
293
+ /**
294
+ * Drains the reader's stream and returns the underlying bytes as a base64 string.
295
+ *
296
+ * @remarks
297
+ * Cross-environment: prefers Node's `Buffer.from(buf).toString('base64')` when available;
298
+ * otherwise chunk-encodes through `btoa` with a 0x8000-byte window to avoid stack overflow
299
+ * on large buffers.
300
+ */
301
+ async asBase64() {
302
+ return encodeBase64(await this.asBytes());
303
+ }
304
+ /**
305
+ * Returns the metadata-only serialisation of this Media. Bytes and the reader are stripped
306
+ * so naive event/log serialisation never materialises bytes.
307
+ *
308
+ * @remarks
309
+ * Implementations that have cheap, already-cached `byteLength` may opt to include it; this
310
+ * default implementation omits it to preserve the "lazy by default" invariant. Consumers that
311
+ * need byteLength on the serialised payload should call `await media.byteLength()` and merge
312
+ * the result.
313
+ */
314
+ toJSON() {
315
+ return {
316
+ id: this.#id,
317
+ kind: this.#kind,
318
+ mimeType: this.#mimeType,
319
+ filename: this.#filename,
320
+ source: this.#source,
321
+ trustTier: this.#trustTier,
322
+ modalityHazard: this.#modalityHazard,
323
+ stash: this.#stash.all()
324
+ };
325
+ }
326
+ /**
327
+ * Factory: constructs a {@link Media} representing a user-supplied attachment.
328
+ *
329
+ * @remarks
330
+ * Pre-fills `trustTier: 'third-party-private'` and derives `modalityHazard` from `kind`
331
+ * (`document` → `'extractable-instructions'`; everything else → `'opaque-perceptual'`).
332
+ * Use the bare constructor when the conservative kind→hazard mapping is wrong for your case.
333
+ */
334
+ static userAttachment(args) {
335
+ return new Media({
336
+ ...args,
337
+ trustTier: "third-party-private",
338
+ modalityHazard: conservativeHazardForKind(args.kind)
339
+ });
340
+ }
341
+ /**
342
+ * Factory: constructs a {@link Media} produced by a first-party tool.
343
+ *
344
+ * @remarks
345
+ * Pre-fills `trustTier: 'first-party'` and derives `modalityHazard` from `kind`.
346
+ */
347
+ static toolGenerated(args) {
348
+ return new Media({
349
+ ...args,
350
+ trustTier: "first-party",
351
+ modalityHazard: conservativeHazardForKind(args.kind)
352
+ });
353
+ }
354
+ /**
355
+ * Factory: constructs a {@link Media} retrieved from a public third-party source.
356
+ *
357
+ * @remarks
358
+ * Pre-fills `trustTier: 'third-party-public'` and derives `modalityHazard` from `kind`.
359
+ */
360
+ static retrievedPublic(args) {
361
+ return new Media({
362
+ ...args,
363
+ trustTier: "third-party-public",
364
+ modalityHazard: conservativeHazardForKind(args.kind)
365
+ });
366
+ }
367
+ /**
368
+ * Factory: constructs a {@link Media} retrieved from a private third-party source.
369
+ *
370
+ * @remarks
371
+ * Pre-fills `trustTier: 'third-party-private'` and derives `modalityHazard` from `kind`.
372
+ */
373
+ static retrievedPrivate(args) {
374
+ return new Media({
375
+ ...args,
376
+ trustTier: "third-party-private",
377
+ modalityHazard: conservativeHazardForKind(args.kind)
378
+ });
379
+ }
380
+ };
381
+ /**
382
+ * Returns `true` if `value` is a {@link Media} instance.
383
+ *
384
+ * @remarks
385
+ * Module-level convenience alias for {@link Media.isMedia}. Uses {@link @nhtio/adk!isInstanceOf} for
386
+ * cross-realm safety.
387
+ */
388
+ var isMedia = (value) => {
389
+ return isInstanceOf(value, "Media", Media);
390
+ };
391
+ //#endregion
392
+ //#region src/lib/classes/tool_call.ts
393
+ var isToolCallResults = (value) => {
394
+ if (Tokenizable.isTokenizable(value)) return true;
395
+ if (SpooledArtifact.isSpooledArtifact(value)) return true;
396
+ if (Media.isMedia(value)) return true;
397
+ if (Array.isArray(value) && value.length > 0) {
398
+ if (value.every((entry) => Media.isMedia(entry))) return true;
399
+ if (value.every((entry) => SpooledArtifact.isSpooledArtifact(entry))) return true;
400
+ }
401
+ return false;
402
+ };
403
+ /**
404
+ * Validator schema used to validate a {@link RawToolCall} before constructing a {@link ToolCall}.
405
+ *
406
+ * @remarks
407
+ * Validates all fields of {@link RawToolCall}:
408
+ * - `id` — required non-empty string.
409
+ * - `tool` — required non-empty string.
410
+ * - `args` — required; either a plain object or a JSON string that deserialises to an object.
411
+ * Strings are parsed and the resulting object is stored.
412
+ * - `checksum` — required string.
413
+ * - `isComplete` — required boolean.
414
+ * - `isError` — required boolean.
415
+ * - `results` — required; one of {@link @nhtio/adk!Tokenizable}, {@link @nhtio/adk!SpooledArtifact}, a non-empty
416
+ * `SpooledArtifact[]`, {@link @nhtio/adk!Media}, or a non-empty `Media[]`. Arrays must be homogeneous.
417
+ * - `createdAt` / `updatedAt` / `completedAt` — required datetime-parseable values, normalised to `DateTime`.
418
+ *
419
+ * Throws {@link @nhtio/adk!E_INVALID_INITIAL_TOOL_CALL_VALUE} (via the {@link ToolCall} constructor) when
420
+ * validation fails.
421
+ */
422
+ var rawToolCallSchema = validator.object({
423
+ id: validator.string().required(),
424
+ tool: validator.string().required(),
425
+ args: validator.alternatives(validator.object().unknown(true), validator.string().custom((value, helpers) => {
426
+ try {
427
+ const parsed = JSON.parse(value);
428
+ if (!isObject(parsed)) return helpers.error("any.invalid");
429
+ return parsed;
430
+ } catch {
431
+ return helpers.error("any.invalid");
432
+ }
433
+ })).required(),
434
+ checksum: validator.string().required(),
435
+ isComplete: validator.boolean().required(),
436
+ isError: validator.boolean().required(),
437
+ results: validator.any().custom((value, helpers) => {
438
+ if (isToolCallResults(value)) return value;
439
+ return helpers.error("any.invalid");
440
+ }).required(),
441
+ fromArtifactTool: validator.boolean().default(false),
442
+ inline: validator.boolean().default(true),
443
+ createdAt: validator.datetime().required(),
444
+ updatedAt: validator.datetime().required(),
445
+ completedAt: validator.datetime().required()
446
+ });
447
+ /**
448
+ * An immutable, validated tool call record associated with a turn.
449
+ *
450
+ * @remarks
451
+ * Represents a completed tool invocation from the conversation history — `results`,
452
+ * `completedAt`, `isComplete`, and `isError` are all present and required.
453
+ * Temporal fields are normalised to Luxon `DateTime` instances at construction time.
454
+ */
455
+ var ToolCall = class ToolCall {
456
+ /**
457
+ * Validator schema that accepts a {@link RawToolCall} object.
458
+ *
459
+ * @remarks
460
+ * Reusable fragment for any schema that needs to validate or nest a tool call entry.
461
+ */
462
+ static schema = rawToolCallSchema;
463
+ /**
464
+ * Returns `true` if `value` is a {@link ToolCall} instance.
465
+ *
466
+ * @remarks
467
+ * Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety — `instanceof` would fail for instances
468
+ * created in a different module copy or VM context.
469
+ *
470
+ * @param value - The value to test.
471
+ * @returns `true` when `value` is a {@link ToolCall} instance.
472
+ */
473
+ static isToolCall(value) {
474
+ return isInstanceOf(value, "ToolCall", ToolCall);
475
+ }
476
+ #id;
477
+ #tool;
478
+ #args;
479
+ #checksum;
480
+ #isComplete;
481
+ #isError;
482
+ #results;
483
+ #fromArtifactTool;
484
+ #inline;
485
+ #createdAt;
486
+ #updatedAt;
487
+ #completedAt;
488
+ /**
489
+ * @param raw - The raw tool call input validated against `rawToolCallSchema`.
490
+ * @throws {@link @nhtio/adk!E_INVALID_INITIAL_TOOL_CALL_VALUE} when `raw` does not satisfy the schema.
491
+ */
492
+ constructor(raw) {
493
+ let resolved;
494
+ try {
495
+ resolved = validateOrThrow(rawToolCallSchema, raw, true);
496
+ } catch (err) {
497
+ throw new E_INVALID_INITIAL_TOOL_CALL_VALUE({ cause: isError(err) ? err : void 0 });
498
+ }
499
+ this.#id = resolved.id;
500
+ this.#tool = resolved.tool;
501
+ this.#args = resolved.args;
502
+ this.#checksum = resolved.checksum;
503
+ this.#isComplete = resolved.isComplete;
504
+ this.#isError = resolved.isError;
505
+ this.#results = resolved.results;
506
+ this.#fromArtifactTool = resolved.fromArtifactTool;
507
+ this.#inline = resolved.inline;
508
+ this.#createdAt = resolved.createdAt;
509
+ this.#updatedAt = resolved.updatedAt;
510
+ this.#completedAt = resolved.completedAt;
511
+ Object.defineProperties(this, {
512
+ id: {
513
+ get: () => this.#id,
514
+ enumerable: true,
515
+ configurable: false
516
+ },
517
+ tool: {
518
+ get: () => this.#tool,
519
+ enumerable: true,
520
+ configurable: false
521
+ },
522
+ args: {
523
+ get: () => this.#args,
524
+ enumerable: true,
525
+ configurable: false
526
+ },
527
+ checksum: {
528
+ get: () => this.#checksum,
529
+ enumerable: true,
530
+ configurable: false
531
+ },
532
+ isComplete: {
533
+ get: () => this.#isComplete,
534
+ enumerable: true,
535
+ configurable: false
536
+ },
537
+ isError: {
538
+ get: () => this.#isError,
539
+ enumerable: true,
540
+ configurable: false
541
+ },
542
+ results: {
543
+ get: () => this.#results,
544
+ enumerable: true,
545
+ configurable: false
546
+ },
547
+ fromArtifactTool: {
548
+ get: () => this.#fromArtifactTool,
549
+ enumerable: true,
550
+ configurable: false
551
+ },
552
+ inline: {
553
+ get: () => this.#inline,
554
+ enumerable: true,
555
+ configurable: false
556
+ },
557
+ createdAt: {
558
+ get: () => this.#createdAt,
559
+ enumerable: true,
560
+ configurable: false
561
+ },
562
+ updatedAt: {
563
+ get: () => this.#updatedAt,
564
+ enumerable: true,
565
+ configurable: false
566
+ },
567
+ completedAt: {
568
+ get: () => this.#completedAt,
569
+ enumerable: true,
570
+ configurable: false
571
+ }
572
+ });
573
+ }
574
+ };
575
+ //#endregion
576
+ export { mediaReaderSchema as a, implementsMediaReader as i, Media as n, isMedia as r, ToolCall as t };
577
+
578
+ //# sourceMappingURL=tool_call-BKyyxGaZ.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool_call-BKyyxGaZ.mjs","names":["#id","#kind","#mimeType","#filename","#source","#trustTier","#modalityHazard","#reader","#stash","#id","#tool","#args","#checksum","#isComplete","#isError","#results","#fromArtifactTool","#inline","#createdAt","#updatedAt","#completedAt"],"sources":["../src/lib/contracts/media_reader.ts","../src/lib/classes/media.ts","../src/lib/classes/tool_call.ts"],"sourcesContent":["import { validator } from '@nhtio/validation'\nimport { passesSchema } from '../utils/validation'\n\n/**\n * Re-openable byte source contract for a Media instance.\n *\n * @remarks\n * Peer to {@link @nhtio/adk!SpoolReader} but tuned for binary streaming rather than line-indexed text.\n * Each `stream()` call must return a fresh, drainable `ReadableStream` over the same underlying\n * bytes — implementations model replay: in-memory readers reconstitute the stream from the\n * buffer, file-backed readers reopen the file handle, HTTP-backed readers re-issue the fetch,\n * cloud blob readers re-issue the GET. The implementor owns the storage and the cost of keeping\n * the underlying source addressable. Implementors whose underlying source is genuinely\n * non-replayable (a raw HTTP body they were handed once) are responsible for caching locally\n * before constructing the Media.\n *\n * Both methods may be synchronous or asynchronous to accommodate both in-memory and I/O-backed\n * implementations without forcing unnecessary promise overhead on simple cases.\n */\nexport interface MediaReader {\n /**\n * Re-opens the underlying byte source and returns a fresh ReadableStream.\n *\n * @remarks\n * Each call yields a new, drainable stream over the same bytes. Render code that needs the\n * full buffer (e.g. base64-encoding an inline image_url) drains the stream; render code that\n * can forward the stream (e.g. multipart upload) passes the stream through without buffering.\n *\n * @returns A drainable ReadableStream of Uint8Array chunks over the underlying bytes.\n */\n stream(): ReadableStream<Uint8Array> | Promise<ReadableStream<Uint8Array>>\n\n /**\n * Returns the total number of bytes in the underlying data, or `undefined` if unknown.\n *\n * @remarks\n * Used for telemetry, budget checks, and pre-flight provider size validation without forcing\n * a stream drain. Sources of unknown length may return `undefined` — absence is treated as\n * \"unknown\", not \"zero\".\n *\n * @returns The byte length of the underlying data, or `undefined` when unknown.\n */\n byteLength(): number | undefined | Promise<number | undefined>\n}\n\n/**\n * Validator schema used to validate a MediaReader value.\n *\n * @remarks\n * Because MediaReader is a structural interface with no associated constructor, validation is\n * duck-typed: the value must be an object, class instance, or function with `stream` and\n * `byteLength` present as callable properties. Arity is not enforced.\n */\nexport const mediaReaderSchema = validator\n .any()\n .required()\n .custom((value, helpers) => {\n if (\n value !== null &&\n value !== undefined &&\n typeof (value as any).stream === 'function' &&\n typeof (value as any).byteLength === 'function'\n ) {\n return value as MediaReader\n }\n return helpers.error('any.invalid')\n })\n\n/**\n * Returns `true` if `value` implements the MediaReader interface.\n *\n * @remarks\n * Duck-typed: checks that `value` is non-null with `stream` and `byteLength` as callable\n * functions. Does not use `instanceof` — there is no MediaReader constructor.\n *\n * @param value - The value to test.\n * @returns `true` when `value` conforms to the MediaReader interface.\n */\nexport const implementsMediaReader = (value: unknown): value is MediaReader => {\n return passesSchema(mediaReaderSchema, value)\n}\n","import { v6 as uuidv6 } from 'uuid'\nimport { Registry } from './registry'\nimport { isError } from '../utils/guards'\nimport { validator } from '@nhtio/validation'\nimport { isInstanceOf } from '../utils/guards'\nimport { validateOrThrow } from '../utils/validation'\nimport { implementsMediaReader, mediaReaderSchema } from '../contracts/media_reader'\nimport { E_INVALID_INITIAL_MEDIA_VALUE, E_NOT_A_MEDIA_READER } from '../exceptions/runtime'\nimport type { MediaReader } from '../contracts/media_reader'\n\n/**\n * The set of supported media kinds.\n *\n * @remarks\n * Modality coverage is asymmetric across providers. The framework defines no\n * `supportedModalities` field — how a battery handles a modality it cannot natively render is\n * the battery author's call (see `unsupportedMediaPolicy` on the OpenAI Chat Completions\n * battery).\n */\nexport const MediaKind = ['image', 'audio', 'video', 'document'] as const\n\n/**\n * Union of all recognised media kind identifier strings.\n */\nexport type MediaKind = (typeof MediaKind)[number]\n\n/**\n * Provenance axis. *Who is the framework willing to vouch for as the source of these bytes?*\n *\n * @remarks\n * Mirrors `RetrievableTrustTier` deliberately — same vocabulary, same question:\n * *did this content come from a place the agent should treat as authoritative?*\n *\n * - `'first-party'` — deployer-vetted bytes (tool output the operator authored, signed\n * internal assets).\n * - `'third-party-public'` — open-web fetches, public APIs, public corpora.\n * - `'third-party-private'` — user uploads, partner APIs, private corpora.\n */\nexport const MediaTrustTier = ['first-party', 'third-party-public', 'third-party-private'] as const\nexport type MediaTrustTier = (typeof MediaTrustTier)[number]\n\n/**\n * Modality-hazard axis. *How dangerous is it to let the model decode these bytes?*\n *\n * @remarks\n * Orthogonal to provenance — a first-party trusted PDF can still carry hidden text layers; a\n * third-party-public raw image can still be encoded as opaque pixels with adversarial\n * perturbations.\n *\n * - `'inert'` — bytes the model never decodes as instructions (e.g. a handle that is never\n * inlined into the prompt).\n * - `'extractable-instructions'` — text-bearing media: PDFs, screenshots with UI text, documents.\n * Hazard is OCR / embedded-text-layer reads.\n * - `'opaque-perceptual'` — raw vision/audio/video the model encodes directly. Hazard is\n * steganographic LSB prompts, adversarial perturbations, ultrasonic audio — invisible to any\n * pre-screen.\n *\n * See `/the-loop/trust-tiers/media` and its research sub-page `/the-loop/trust-tiers/media/research`.\n */\nexport const MediaModalityHazard = [\n 'inert',\n 'extractable-instructions',\n 'opaque-perceptual',\n] as const\nexport type MediaModalityHazard = (typeof MediaModalityHazard)[number]\n\n/**\n * Per-entry shape stored in a {@link Media}'s `stash` register.\n *\n * @remarks\n * Each entry carries its own trust tier so render code can route derived text (OCR, captions,\n * transcripts) through its own envelope independent of the parent media. How a battery or\n * middleware assigns those entry-level tiers is the implementor's call — the primitive contract\n * does not enforce a \"downgrade derived interpretation from possibly-adversarial bytes\" policy.\n */\nexport interface MediaStashEntry {\n /** The value of the entry — any serialisable shape the consumer wants to store. */\n value: unknown\n /** Trust tier for this specific entry; routed independently of the parent media. */\n trustTier: MediaTrustTier\n /** Optional pointer to the parent Media id this entry was derived from. */\n derivedFromMedia?: string\n}\n\n/**\n * Plain input object supplied to {@link Media} at construction time.\n *\n * @remarks\n * Validated against `rawMediaSchema` before the `Media` instance is created.\n */\nexport interface RawMedia {\n /**\n * Stable unique identifier for this media instance. Required for strict symmetry with\n * `Message.id` and `ToolCall.id`. When omitted, a fresh UUIDv6 is assigned at construction\n * time.\n */\n id?: string\n /** The media kind. See {@link MediaKind}. */\n kind: MediaKind\n /** The MIME type of the underlying bytes. */\n mimeType: string\n /** Filename used by providers that key on it (e.g. OpenAI `file.filename`). */\n filename: string\n /** Re-openable byte source. See {@link @nhtio/adk!MediaReader}. */\n reader: MediaReader\n /**\n * Trust tier declared at construction time. Required — there is NO default.\n * See {@link MediaTrustTier}.\n */\n trustTier: MediaTrustTier\n /**\n * Modality hazard declared at construction time. Required — there is NO default.\n * See {@link MediaModalityHazard}.\n */\n modalityHazard: MediaModalityHazard\n /** Optional provenance pointer (URL, tool name, etc.) for audit / events. */\n source?: string\n /**\n * Free-form per-instance metadata register. Middleware pipelines append to this — typically\n * with a text description, transcript, caption, or alt-text — so downstream code that cannot\n * consume the media natively has a model-readable fallback. No keys are reserved by the\n * framework. Defaults to `{}`.\n */\n stash?: Record<string, MediaStashEntry>\n}\n\nconst stashEntrySchema = validator\n .object<MediaStashEntry>({\n value: validator.any().required(),\n trustTier: validator\n .string()\n .valid(...MediaTrustTier)\n .required(),\n derivedFromMedia: validator.string().optional(),\n })\n .unknown(false)\n\n/**\n * Validator schema used to validate a {@link RawMedia} before constructing a {@link Media}.\n */\nconst rawMediaSchema = validator.object<RawMedia>({\n id: validator.string().optional(),\n kind: validator\n .string()\n .valid(...MediaKind)\n .required(),\n mimeType: validator.string().required(),\n filename: validator.string().required(),\n reader: mediaReaderSchema.required(),\n trustTier: validator\n .string()\n .valid(...MediaTrustTier)\n .required(),\n modalityHazard: validator\n .string()\n .valid(...MediaModalityHazard)\n .required(),\n source: validator.string().optional(),\n stash: validator.object().pattern(validator.string(), stashEntrySchema).optional(),\n})\n\ninterface ResolvedMedia {\n id?: string\n kind: MediaKind\n mimeType: string\n filename: string\n reader: MediaReader\n trustTier: MediaTrustTier\n modalityHazard: MediaModalityHazard\n source?: string\n stash?: Record<string, MediaStashEntry>\n}\n\nconst conservativeHazardForKind = (kind: MediaKind): MediaModalityHazard => {\n return kind === 'document' ? 'extractable-instructions' : 'opaque-perceptual'\n}\n\n/**\n * Shape returned by {@link Media.toJSON}. Metadata-only — bytes and the reader are stripped so\n * naive event/log serialisation never materialises bytes.\n */\nexport interface SerializedMedia {\n id: string\n kind: MediaKind\n mimeType: string\n filename: string\n source?: string\n trustTier: MediaTrustTier\n modalityHazard: MediaModalityHazard\n stash: Record<string, MediaStashEntry>\n byteLength?: number\n}\n\n/**\n * Cross-environment base64 encoder for a `Uint8Array`.\n *\n * @remarks\n * Prefers Node's `Buffer.from(buf).toString('base64')` when `globalThis.Buffer` exists; otherwise\n * chunk-encodes through `btoa` with a 0x8000-byte window to avoid `Maximum call stack size\n * exceeded` on large buffers.\n */\nconst encodeBase64 = (bytes: Uint8Array): string => {\n const maybeBuffer = (\n globalThis as { Buffer?: { from(b: Uint8Array): { toString(enc: string): string } } }\n ).Buffer\n if (maybeBuffer && typeof maybeBuffer.from === 'function') {\n return maybeBuffer.from(bytes).toString('base64')\n }\n const chunkSize = 0x8000\n let binary = ''\n for (let i = 0; i < bytes.length; i += chunkSize) {\n const chunk = bytes.subarray(i, i + chunkSize)\n binary += String.fromCharCode.apply(null, Array.from(chunk) as number[])\n }\n return btoa(binary)\n}\n\n/**\n * Lazy, re-openable view over a binary asset (image, audio, video, document).\n *\n * @remarks\n * Dual-peer to {@link @nhtio/adk!Tokenizable} (silo) and {@link @nhtio/adk!SpooledArtifact}\n * (handle). Wraps a {@link @nhtio/adk!MediaReader} contract — the framework owns the contract, the\n * implementor owns the storage backend. Bytes are reached only through the reader; the primitive\n * itself never inlines bytes.\n *\n * Construction requires `trustTier` and `modalityHazard` — the framework refuses to guess\n * provenance or decoding hazard. Ergonomic factories ({@link Media.userAttachment},\n * {@link Media.toolGenerated}, {@link Media.retrievedPublic}, {@link Media.retrievedPrivate})\n * force the labelling decision at the call site without becoming defaults on the bare\n * constructor.\n */\nexport class Media {\n /**\n * Validator schema that accepts a {@link RawMedia} object.\n */\n public static schema = rawMediaSchema\n\n /**\n * The set of recognised media kinds. Exposed for downstream schemas that need to discriminate\n * on `kind`.\n */\n public static MediaKind = MediaKind\n\n /**\n * The set of recognised trust tiers.\n */\n public static MediaTrustTier = MediaTrustTier\n\n /**\n * The set of recognised modality hazards.\n */\n public static MediaModalityHazard = MediaModalityHazard\n\n /**\n * Returns `true` if `value` is a {@link Media} instance.\n *\n * @remarks\n * Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety.\n *\n * @param value - The value to test.\n * @returns `true` when `value` is a {@link Media} instance.\n */\n public static isMedia(value: unknown): value is Media {\n return isInstanceOf(value, 'Media', Media)\n }\n\n /** Stable unique identifier. */\n declare readonly id: string\n /** Media kind. */\n declare readonly kind: MediaKind\n /** MIME type of the underlying bytes. */\n declare readonly mimeType: string\n /** Filename surfaced to providers that key on it. */\n declare readonly filename: string\n /** Optional provenance pointer. */\n declare readonly source: string | undefined\n /** Trust tier declared at construction time. */\n declare readonly trustTier: MediaTrustTier\n /** Modality hazard declared at construction time. */\n declare readonly modalityHazard: MediaModalityHazard\n /** Mutable per-instance metadata register; middleware pipelines append to this. */\n declare readonly stash: Registry\n\n #id: string\n #kind: MediaKind\n #mimeType: string\n #filename: string\n #source?: string\n #trustTier: MediaTrustTier\n #modalityHazard: MediaModalityHazard\n #reader: MediaReader\n #stash: Registry\n\n /**\n * @param raw - The raw media input validated against `rawMediaSchema`.\n * @throws {@link @nhtio/adk/exceptions!E_INVALID_INITIAL_MEDIA_VALUE} when `raw` does not satisfy the schema.\n * @throws {@link @nhtio/adk/exceptions!E_NOT_A_MEDIA_READER} when `raw.reader` does not implement {@link @nhtio/adk!MediaReader}.\n */\n constructor(raw: RawMedia) {\n let resolved: ResolvedMedia\n try {\n resolved = validateOrThrow<ResolvedMedia>(rawMediaSchema, raw, true)\n } catch (err) {\n throw new E_INVALID_INITIAL_MEDIA_VALUE({ cause: isError(err) ? err : undefined })\n }\n if (!implementsMediaReader(resolved.reader)) {\n throw new E_NOT_A_MEDIA_READER()\n }\n this.#id = resolved.id ?? uuidv6()\n this.#kind = resolved.kind\n this.#mimeType = resolved.mimeType\n this.#filename = resolved.filename\n this.#source = resolved.source\n this.#trustTier = resolved.trustTier\n this.#modalityHazard = resolved.modalityHazard\n this.#reader = resolved.reader\n this.#stash = new Registry(resolved.stash as Record<string, unknown> | undefined)\n\n Object.defineProperties(this, {\n id: {\n get: () => this.#id,\n enumerable: true,\n configurable: false,\n },\n kind: {\n get: () => this.#kind,\n enumerable: true,\n configurable: false,\n },\n mimeType: {\n get: () => this.#mimeType,\n enumerable: true,\n configurable: false,\n },\n filename: {\n get: () => this.#filename,\n enumerable: true,\n configurable: false,\n },\n source: {\n get: () => this.#source,\n enumerable: true,\n configurable: false,\n },\n trustTier: {\n get: () => this.#trustTier,\n enumerable: true,\n configurable: false,\n },\n modalityHazard: {\n get: () => this.#modalityHazard,\n enumerable: true,\n configurable: false,\n },\n stash: {\n get: () => this.#stash,\n enumerable: true,\n configurable: false,\n },\n })\n }\n\n /**\n * Re-opens the underlying byte source and returns a fresh ReadableStream.\n *\n * @returns A drainable `ReadableStream` over the underlying bytes.\n */\n async stream(): Promise<ReadableStream<Uint8Array>> {\n return this.#reader.stream()\n }\n\n /**\n * Returns the total number of bytes in the underlying data, or `undefined` if unknown.\n *\n * @returns The byte length, or `undefined` when the underlying source cannot report it.\n */\n async byteLength(): Promise<number | undefined> {\n return this.#reader.byteLength()\n }\n\n /**\n * Drains the reader's stream and returns the underlying bytes as a single `Uint8Array`.\n *\n * @remarks\n * Convenience for callers that need the full buffer (e.g. inline base64 encoding). Forces\n * full materialisation — large assets should be piped through {@link Media.stream} instead.\n */\n async asBytes(): Promise<Uint8Array> {\n const stream = await this.stream()\n const reader = stream.getReader()\n const chunks: Uint8Array[] = []\n let total = 0\n while (true) {\n const { value, done } = await reader.read()\n if (done) break\n if (value) {\n chunks.push(value)\n total += value.byteLength\n }\n }\n const out = new Uint8Array(total)\n let offset = 0\n for (const chunk of chunks) {\n out.set(chunk, offset)\n offset += chunk.byteLength\n }\n return out\n }\n\n /**\n * Drains the reader's stream and returns the underlying bytes as a base64 string.\n *\n * @remarks\n * Cross-environment: prefers Node's `Buffer.from(buf).toString('base64')` when available;\n * otherwise chunk-encodes through `btoa` with a 0x8000-byte window to avoid stack overflow\n * on large buffers.\n */\n async asBase64(): Promise<string> {\n const bytes = await this.asBytes()\n return encodeBase64(bytes)\n }\n\n /**\n * Returns the metadata-only serialisation of this Media. Bytes and the reader are stripped\n * so naive event/log serialisation never materialises bytes.\n *\n * @remarks\n * Implementations that have cheap, already-cached `byteLength` may opt to include it; this\n * default implementation omits it to preserve the \"lazy by default\" invariant. Consumers that\n * need byteLength on the serialised payload should call `await media.byteLength()` and merge\n * the result.\n */\n toJSON(): SerializedMedia {\n return {\n id: this.#id,\n kind: this.#kind,\n mimeType: this.#mimeType,\n filename: this.#filename,\n source: this.#source,\n trustTier: this.#trustTier,\n modalityHazard: this.#modalityHazard,\n stash: this.#stash.all() as Record<string, MediaStashEntry>,\n }\n }\n\n /**\n * Factory: constructs a {@link Media} representing a user-supplied attachment.\n *\n * @remarks\n * Pre-fills `trustTier: 'third-party-private'` and derives `modalityHazard` from `kind`\n * (`document` → `'extractable-instructions'`; everything else → `'opaque-perceptual'`).\n * Use the bare constructor when the conservative kind→hazard mapping is wrong for your case.\n */\n public static userAttachment(args: {\n id?: string\n kind: MediaKind\n mimeType: string\n filename: string\n reader: MediaReader\n source?: string\n stash?: Record<string, MediaStashEntry>\n }): Media {\n return new Media({\n ...args,\n trustTier: 'third-party-private',\n modalityHazard: conservativeHazardForKind(args.kind),\n })\n }\n\n /**\n * Factory: constructs a {@link Media} produced by a first-party tool.\n *\n * @remarks\n * Pre-fills `trustTier: 'first-party'` and derives `modalityHazard` from `kind`.\n */\n public static toolGenerated(args: {\n id?: string\n kind: MediaKind\n mimeType: string\n filename: string\n reader: MediaReader\n source?: string\n stash?: Record<string, MediaStashEntry>\n }): Media {\n return new Media({\n ...args,\n trustTier: 'first-party',\n modalityHazard: conservativeHazardForKind(args.kind),\n })\n }\n\n /**\n * Factory: constructs a {@link Media} retrieved from a public third-party source.\n *\n * @remarks\n * Pre-fills `trustTier: 'third-party-public'` and derives `modalityHazard` from `kind`.\n */\n public static retrievedPublic(args: {\n id?: string\n kind: MediaKind\n mimeType: string\n filename: string\n reader: MediaReader\n source?: string\n stash?: Record<string, MediaStashEntry>\n }): Media {\n return new Media({\n ...args,\n trustTier: 'third-party-public',\n modalityHazard: conservativeHazardForKind(args.kind),\n })\n }\n\n /**\n * Factory: constructs a {@link Media} retrieved from a private third-party source.\n *\n * @remarks\n * Pre-fills `trustTier: 'third-party-private'` and derives `modalityHazard` from `kind`.\n */\n public static retrievedPrivate(args: {\n id?: string\n kind: MediaKind\n mimeType: string\n filename: string\n reader: MediaReader\n source?: string\n stash?: Record<string, MediaStashEntry>\n }): Media {\n return new Media({\n ...args,\n trustTier: 'third-party-private',\n modalityHazard: conservativeHazardForKind(args.kind),\n })\n }\n}\n\n/**\n * Returns `true` if `value` is a {@link Media} instance.\n *\n * @remarks\n * Module-level convenience alias for {@link Media.isMedia}. Uses {@link @nhtio/adk!isInstanceOf} for\n * cross-realm safety.\n */\nexport const isMedia = (value: unknown): value is Media => {\n return isInstanceOf(value, 'Media', Media)\n}\n","import { Media } from './media'\nimport { Tokenizable } from './tokenizable'\nimport { validator } from '@nhtio/validation'\nimport { SpooledArtifact } from './spooled_artifact'\nimport { validateOrThrow } from '../utils/validation'\nimport { isObject, isInstanceOf, isError } from '../utils/guards'\nimport { E_INVALID_INITIAL_TOOL_CALL_VALUE } from '../exceptions/runtime'\nimport type { DateTime } from 'luxon'\n\n/**\n * Union of every shape a {@link ToolCall.results} field may carry.\n *\n * @remarks\n * Three silos with distinct render-time semantics:\n *\n * - {@link @nhtio/adk!Tokenizable} — always singular. The {@link @nhtio/adk!ArtifactTool}\n * carve-out: a model-visible text answer that explicitly opts out of artifact wrapping to\n * break the recursive grep-on-the-grep-result loop.\n * - {@link @nhtio/adk!SpooledArtifact} or `SpooledArtifact[]` — bounded text output spooled to durable\n * storage. A single tool call may legitimately produce multiple artifacts (e.g. one tool\n * that returns N PR bodies). LLM adapters render either inline (full body in trust envelope)\n * or as a handle reference (forged artifact-query tools).\n * - {@link @nhtio/adk!Media} or `Media[]` — binary modality output (image, audio, video, document).\n * Adapters render as provider-specific content blocks (`image_url`, `input_audio`, `file`,\n * etc.). Bytes are lazy — reached only through {@link @nhtio/adk!Media.stream}.\n */\nexport type ToolCallResults = Tokenizable | SpooledArtifact | SpooledArtifact[] | Media | Media[]\n\nconst isToolCallResults = (value: unknown): value is ToolCallResults => {\n if (Tokenizable.isTokenizable(value)) return true\n if (SpooledArtifact.isSpooledArtifact(value)) return true\n if (Media.isMedia(value)) return true\n if (Array.isArray(value) && value.length > 0) {\n const allMedia = value.every((entry) => Media.isMedia(entry))\n if (allMedia) return true\n const allSpooled = value.every((entry) => SpooledArtifact.isSpooledArtifact(entry))\n if (allSpooled) return true\n }\n return false\n}\n\n/**\n * Plain input object supplied to {@link ToolCall} at construction time.\n *\n * @remarks\n * Validated against `rawToolCallSchema` before the `ToolCall` instance is created.\n * Temporal fields accept any value that Luxon can parse — ISO strings, Unix timestamps,\n * `Date` objects, or existing `DateTime` instances.\n */\nexport interface RawToolCall {\n /** Stable unique identifier for this tool call; correlates the request with its result. */\n id: string\n /** Name of the tool the model has requested. */\n tool: string\n /**\n * Arguments the model supplied for this tool call.\n *\n * @remarks\n * Accepts either a plain object or a JSON-encoded string that deserialises to an object.\n * Always exposed as a plain object on the constructed {@link ToolCall} instance.\n */\n args: string | Record<string, unknown>\n /** Integrity checksum over `tool` and `args`; can be used to detect tampering before execution. */\n checksum: string\n /** `true` once the tool call has finished (successfully or not). */\n isComplete: boolean\n /** `true` when the tool execution produced an error; inspect `results` for detail. */\n isError: boolean\n /**\n * Result returned by the tool, or error detail when `isError` is `true`.\n *\n * @remarks\n * Three silos with distinct render-time semantics — see {@link ToolCallResults}:\n *\n * - For a normal {@link @nhtio/adk!Tool} call whose handler returned `string` or\n * `Uint8Array`, this is a {@link @nhtio/adk!SpooledArtifact} (or one of its subclasses) wrapping the\n * spooled bytes. Tools that legitimately produce multiple bounded artifacts may return\n * a `SpooledArtifact[]`.\n * - For a `Tool` call whose handler returned a {@link @nhtio/adk!Media} or `Media[]`, this is the same\n * media handle(s) — the explicit-modality silo bypasses `SpooledArtifact` wrapping because\n * the bytes are binary and rendered as provider-specific content blocks, not text.\n * - For an {@link @nhtio/adk!ArtifactTool} call (a forged artifact-query tool),\n * this is a {@link @nhtio/adk!Tokenizable} holding the raw model-visible answer — `ArtifactTool`\n * explicitly opts out of wrapping to break the recursive grep-on-the-grep-result loop.\n *\n * The ADK sets {@link RawToolCall.fromArtifactTool} on calls produced by an\n * `ArtifactTool` so subsequent `forgeTools(ctx)` invocations can filter them out of the\n * `callId` enum.\n */\n results: ToolCallResults\n /**\n * `true` when this tool call originated from an {@link @nhtio/adk!ArtifactTool}\n * invocation. Defaults to `false`.\n *\n * @remarks\n * Set by the ADK's result-wrapping touch sites when `ArtifactTool.isArtifactTool(tool)`\n * holds. Read by `SpooledArtifact.forgeTools(ctx)` when building each descriptor's `callId`\n * enum — calls with this flag set are excluded so the model can't `artifact_grep` on a\n * previous `artifact_grep` result. Optional in the raw shape (defaults to `false`); always\n * defined on the constructed {@link ToolCall}.\n *\n * @defaultValue `false`\n */\n fromArtifactTool?: boolean\n /**\n * When `true` (default), LLM adapters render this tool call's result inline — the full\n * stringified body is wrapped in the adapter's trust envelope and sent to the model as the\n * `tool` role content. When `false`, the adapter surfaces the result as a \"handle\" — a\n * directions-bearing envelope that tells the model which forged artifact-query tools to call\n * against this `tc.id` to read the content incrementally.\n *\n * @remarks\n * Policy is the producer's or middleware's call:\n * - A normal tool returns its result with `inline: true` by default so its output is shown\n * verbatim.\n * - Middleware that wants to keep large results out of the model's prompt sets `inline: false`\n * (typically via `ctx.mutateToolCall(tc.id, { inline: false })`) before yielding.\n * - LLM adapters do not override the flag, do not size-check the result, and do not silently\n * switch to the handle pattern.\n *\n * For {@link @nhtio/adk!Tokenizable} results, the flag is effectively informational — handles only make\n * sense for {@link @nhtio/adk!SpooledArtifact} (which is the only result kind the forged artifact-query\n * tools can read). When `inline: false` is set on a call whose `results` is a `Tokenizable`,\n * the adapter renders inline anyway and may log a warning.\n *\n * @defaultValue `true`\n */\n inline?: boolean\n /** When this tool call was first created. */\n createdAt: string | number | Date | DateTime\n /** When this tool call was last modified. */\n updatedAt: string | number | Date | DateTime\n /** When the tool call completed. */\n completedAt: string | number | Date | DateTime\n}\n\n/**\n * A fully-resolved {@link RawToolCall} where temporal fields have been normalised to Luxon\n * `DateTime` instances.\n *\n * @remarks\n * Used internally by the {@link ToolCall} constructor to assign private fields with\n * guaranteed types.\n */\ninterface ResolvedToolCall {\n id: string\n tool: string\n args: Record<string, unknown>\n checksum: string\n isComplete: boolean\n isError: boolean\n results: ToolCallResults\n fromArtifactTool: boolean\n inline: boolean\n createdAt: DateTime\n updatedAt: DateTime\n completedAt: DateTime\n}\n\n/**\n * Validator schema used to validate a {@link RawToolCall} before constructing a {@link ToolCall}.\n *\n * @remarks\n * Validates all fields of {@link RawToolCall}:\n * - `id` — required non-empty string.\n * - `tool` — required non-empty string.\n * - `args` — required; either a plain object or a JSON string that deserialises to an object.\n * Strings are parsed and the resulting object is stored.\n * - `checksum` — required string.\n * - `isComplete` — required boolean.\n * - `isError` — required boolean.\n * - `results` — required; one of {@link @nhtio/adk!Tokenizable}, {@link @nhtio/adk!SpooledArtifact}, a non-empty\n * `SpooledArtifact[]`, {@link @nhtio/adk!Media}, or a non-empty `Media[]`. Arrays must be homogeneous.\n * - `createdAt` / `updatedAt` / `completedAt` — required datetime-parseable values, normalised to `DateTime`.\n *\n * Throws {@link @nhtio/adk!E_INVALID_INITIAL_TOOL_CALL_VALUE} (via the {@link ToolCall} constructor) when\n * validation fails.\n */\nconst rawToolCallSchema = validator.object<RawToolCall>({\n id: validator.string().required(),\n tool: validator.string().required(),\n args: validator\n .alternatives(\n validator.object().unknown(true),\n validator.string().custom((value, helpers) => {\n try {\n const parsed = JSON.parse(value)\n if (!isObject(parsed)) {\n return helpers.error('any.invalid')\n }\n return parsed\n } catch {\n return helpers.error('any.invalid')\n }\n })\n )\n .required(),\n checksum: validator.string().required(),\n isComplete: validator.boolean().required(),\n isError: validator.boolean().required(),\n results: validator\n .any()\n .custom((value, helpers) => {\n if (isToolCallResults(value)) {\n return value\n }\n return helpers.error('any.invalid')\n })\n .required(),\n fromArtifactTool: validator.boolean().default(false),\n inline: validator.boolean().default(true),\n createdAt: validator.datetime().required(),\n updatedAt: validator.datetime().required(),\n completedAt: validator.datetime().required(),\n})\n\n/**\n * An immutable, validated tool call record associated with a turn.\n *\n * @remarks\n * Represents a completed tool invocation from the conversation history — `results`,\n * `completedAt`, `isComplete`, and `isError` are all present and required.\n * Temporal fields are normalised to Luxon `DateTime` instances at construction time.\n */\nexport class ToolCall {\n /**\n * Validator schema that accepts a {@link RawToolCall} object.\n *\n * @remarks\n * Reusable fragment for any schema that needs to validate or nest a tool call entry.\n */\n public static schema = rawToolCallSchema\n\n /**\n * Returns `true` if `value` is a {@link ToolCall} instance.\n *\n * @remarks\n * Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety — `instanceof` would fail for instances\n * created in a different module copy or VM context.\n *\n * @param value - The value to test.\n * @returns `true` when `value` is a {@link ToolCall} instance.\n */\n public static isToolCall(value: unknown): value is ToolCall {\n return isInstanceOf(value, 'ToolCall', ToolCall)\n }\n\n /** Stable unique identifier for this tool call; correlates the request with its result. */\n declare readonly id: string\n /** Name of the tool the model has requested. */\n declare readonly tool: string\n /** Arguments the model supplied for this tool call, always as a plain object. */\n declare readonly args: Record<string, unknown>\n /** Integrity checksum over `tool` and `args`. */\n declare readonly checksum: string\n /** `true` once the tool call has finished (successfully or not). */\n declare readonly isComplete: boolean\n /** `true` when the tool execution produced an error; inspect `results` for detail. */\n declare readonly isError: boolean\n /**\n * Result returned by the tool, or error detail when `isError` is `true`.\n *\n * @remarks\n * One of three silos — see {@link ToolCallResults}. {@link @nhtio/adk!SpooledArtifact} or\n * `SpooledArtifact[]` for normal text-output {@link @nhtio/adk!Tool} calls;\n * {@link @nhtio/adk!Media} or `Media[]` for tool calls whose handler returned binary modality output;\n * {@link @nhtio/adk!Tokenizable} for {@link @nhtio/adk!ArtifactTool} calls\n * (see {@link ToolCall.fromArtifactTool}).\n */\n declare readonly results: ToolCallResults\n /**\n * `true` when this tool call originated from an {@link @nhtio/adk!ArtifactTool}\n * invocation. Used by `SpooledArtifact.forgeTools(ctx)` to filter out forged-tool results from\n * the `callId` enum it builds.\n */\n declare readonly fromArtifactTool: boolean\n /**\n * `true` (default) renders this tool call's result inline in the prompt; `false` instructs LLM\n * adapters to surface the result as a handle reference. See {@link RawToolCall.inline}.\n */\n declare readonly inline: boolean\n /** When this tool call was first created. */\n declare readonly createdAt: DateTime\n /** When this tool call was last modified. */\n declare readonly updatedAt: DateTime\n /** When the tool call completed. */\n declare readonly completedAt: DateTime\n\n #id: string\n #tool: string\n #args: Record<string, unknown>\n #checksum: string\n #isComplete: boolean\n #isError: boolean\n #results: ToolCallResults\n #fromArtifactTool: boolean\n #inline: boolean\n #createdAt: DateTime\n #updatedAt: DateTime\n #completedAt: DateTime\n\n /**\n * @param raw - The raw tool call input validated against `rawToolCallSchema`.\n * @throws {@link @nhtio/adk!E_INVALID_INITIAL_TOOL_CALL_VALUE} when `raw` does not satisfy the schema.\n */\n constructor(raw: RawToolCall) {\n let resolved: ResolvedToolCall\n try {\n resolved = validateOrThrow<ResolvedToolCall>(rawToolCallSchema, raw, true)\n } catch (err) {\n throw new E_INVALID_INITIAL_TOOL_CALL_VALUE({ cause: isError(err) ? err : undefined })\n }\n this.#id = resolved.id\n this.#tool = resolved.tool\n this.#args = resolved.args\n this.#checksum = resolved.checksum\n this.#isComplete = resolved.isComplete\n this.#isError = resolved.isError\n this.#results = resolved.results\n this.#fromArtifactTool = resolved.fromArtifactTool\n this.#inline = resolved.inline\n this.#createdAt = resolved.createdAt\n this.#updatedAt = resolved.updatedAt\n this.#completedAt = resolved.completedAt\n\n Object.defineProperties(this, {\n id: {\n get: () => this.#id,\n enumerable: true,\n configurable: false,\n },\n tool: {\n get: () => this.#tool,\n enumerable: true,\n configurable: false,\n },\n args: {\n get: () => this.#args,\n enumerable: true,\n configurable: false,\n },\n checksum: {\n get: () => this.#checksum,\n enumerable: true,\n configurable: false,\n },\n isComplete: {\n get: () => this.#isComplete,\n enumerable: true,\n configurable: false,\n },\n isError: {\n get: () => this.#isError,\n enumerable: true,\n configurable: false,\n },\n results: {\n get: () => this.#results,\n enumerable: true,\n configurable: false,\n },\n fromArtifactTool: {\n get: () => this.#fromArtifactTool,\n enumerable: true,\n configurable: false,\n },\n inline: {\n get: () => this.#inline,\n enumerable: true,\n configurable: false,\n },\n createdAt: {\n get: () => this.#createdAt,\n enumerable: true,\n configurable: false,\n },\n updatedAt: {\n get: () => this.#updatedAt,\n enumerable: true,\n configurable: false,\n },\n completedAt: {\n get: () => this.#completedAt,\n enumerable: true,\n configurable: false,\n },\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AAqDA,IAAa,oBAAoB,UAC9B,IAAI,EACJ,SAAS,EACT,QAAQ,OAAO,YAAY;CAC1B,IACE,UAAU,QACV,UAAU,KAAA,KACV,OAAQ,MAAc,WAAW,cACjC,OAAQ,MAAc,eAAe,YAErC,OAAO;CAET,OAAO,QAAQ,MAAM,aAAa;AACpC,CAAC;;;;;;;;;;;AAYH,IAAa,yBAAyB,UAAyC;CAC7E,OAAO,aAAa,mBAAmB,KAAK;AAC9C;;;;;;;;;;;;AC7DA,IAAa,YAAY;CAAC;CAAS;CAAS;CAAS;AAAU;;;;;;;;;;;;;AAmB/D,IAAa,iBAAiB;CAAC;CAAe;CAAsB;AAAqB;;;;;;;;;;;;;;;;;;;AAqBzF,IAAa,sBAAsB;CACjC;CACA;CACA;AACF;AA+DA,IAAM,mBAAmB,UACtB,OAAwB;CACvB,OAAO,UAAU,IAAI,EAAE,SAAS;CAChC,WAAW,UACR,OAAO,EACP,MAAM,GAAG,cAAc,EACvB,SAAS;CACZ,kBAAkB,UAAU,OAAO,EAAE,SAAS;AAChD,CAAC,EACA,QAAQ,KAAK;;;;AAKhB,IAAM,iBAAiB,UAAU,OAAiB;CAChD,IAAI,UAAU,OAAO,EAAE,SAAS;CAChC,MAAM,UACH,OAAO,EACP,MAAM,GAAG,SAAS,EAClB,SAAS;CACZ,UAAU,UAAU,OAAO,EAAE,SAAS;CACtC,UAAU,UAAU,OAAO,EAAE,SAAS;CACtC,QAAQ,k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