@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,641 @@
1
+ const require_chunk = require("./chunk-KmRHZBOW.js");
2
+ const require_exceptions = require("./exceptions-D5YrO9Vm.js");
3
+ const require_runtime = require("./runtime-BJVkrGQe.js");
4
+ let js_tiktoken = require("js-tiktoken");
5
+ let _nhtio_validation = require("@nhtio/validation");
6
+ let llama_tokenizer_js = require("llama-tokenizer-js");
7
+ let _lenml_tokenizer_gemini = require("@lenml/tokenizer-gemini");
8
+ let dset = require("dset");
9
+ let klona = require("klona");
10
+ let dlv = require("dlv");
11
+ dlv = require_chunk.__toESM(dlv);
12
+ //#region src/lib/utils/guards.ts
13
+ /**
14
+ * Returns `true` if `value` is an instance of the class identified by `type` (and optionally `ctor`).
15
+ *
16
+ * @remarks
17
+ * Performs three checks in order: `instanceof ctor`, `Symbol.hasInstance`, then constructor-name
18
+ * comparison. The constructor-name fallback handles cross-realm cases where `instanceof` fails.
19
+ *
20
+ * @typeParam T - The expected instance type.
21
+ * @param value - The value to test.
22
+ * @param type - The constructor name to compare against when `instanceof` is unavailable.
23
+ * @param ctor - Optional constructor to use for `instanceof` and `Symbol.hasInstance` checks.
24
+ * @returns `true` when `value` is an instance of the class described by `type`/`ctor`.
25
+ */
26
+ var isInstanceOf = (value, type, ctor) => {
27
+ if ("undefined" !== typeof ctor && value instanceof ctor) return true;
28
+ /* istanbul ignore next 4 */
29
+ if ("undefined" !== typeof ctor && typeof ctor[Symbol.hasInstance] === "function" && ctor[Symbol.hasInstance](value))
30
+ /* istanbul ignore next */
31
+ return true;
32
+ if ("object" === typeof value && null !== value) return value.constructor?.name === type;
33
+ return false;
34
+ };
35
+ var errorSchema = _nhtio_validation.validator.any().custom((value, helpers) => {
36
+ if (isInstanceOf(value, "Error", Error)) return value;
37
+ return helpers.error("any.invalid");
38
+ }).required();
39
+ /**
40
+ * Returns `true` if `value` is an `Error` instance or satisfies the `Error` duck-type shape.
41
+ *
42
+ * @remarks
43
+ * Returns `false` for `undefined` and `null` — the `Error` contract requires an actual instance.
44
+ *
45
+ * @param value - The value to test.
46
+ * @returns `true` when `value` conforms to the `Error` shape.
47
+ */
48
+ var isError = (value) => {
49
+ return require_exceptions.passesSchema(errorSchema, value);
50
+ };
51
+ /**
52
+ * Type guard to check if a value is a plain object (not null, not array)
53
+ * @param value - The value to check
54
+ * @returns True if the value is a plain object, false otherwise
55
+ */
56
+ var isObject = (value) => {
57
+ return typeof value === "object" && value !== null && !Array.isArray(value);
58
+ };
59
+ //#endregion
60
+ //#region src/lib/classes/tokenizable.ts
61
+ /**
62
+ * The set of supported token encoding identifiers.
63
+ *
64
+ * @remarks
65
+ * Each value maps to a specific estimation backend:
66
+ * - `gpt2`, `r50k_base`, `p50k_base`, `p50k_edit`, `cl100k_base`, `o200k_base` — exact counts
67
+ * via `js-tiktoken` (OpenAI / tiktoken-compatible models).
68
+ * - `gemini` — exact counts via `@lenml/tokenizer-gemini`, which embeds Gemini's actual
69
+ * SentencePiece vocabulary locally with no API call required.
70
+ * - `llama2` — exact counts via `llama-tokenizer-js` (Llama 1 and 2). Llama 3+ uses a
71
+ * different vocabulary and should use the `llama3` identifier once a suitable sync backend
72
+ * is available.
73
+ * - `claude` — heuristic approximation using Anthropic's published ~3.5 chars/token ratio.
74
+ * No local tokenizer is available for Claude 3+ models; the Anthropic SDK's
75
+ * `messages.countTokens()` API is the only exact path but requires a network call.
76
+ *
77
+ * When adding a new encoding, add a case to {@link Tokenizable.estimateTokens}.
78
+ */
79
+ var TokenEncoding = [
80
+ "gpt2",
81
+ "r50k_base",
82
+ "p50k_base",
83
+ "p50k_edit",
84
+ "cl100k_base",
85
+ "o200k_base",
86
+ "gemini",
87
+ "llama2",
88
+ "claude"
89
+ ];
90
+ /**
91
+ * Backing schema for {@link Tokenizable.schema}.
92
+ *
93
+ * @remarks
94
+ * Accepts a plain `string` or an existing {@link Tokenizable} instance. Strings pass through
95
+ * unchanged; {@link Tokenizable} instances are accepted via {@link Tokenizable.isTokenizable}
96
+ * to remain cross-realm safe.
97
+ */
98
+ var stringOrTokenizableSchema = _nhtio_validation.validator.alternatives(_nhtio_validation.validator.string(), _nhtio_validation.validator.custom((value, helpers) => {
99
+ if (Tokenizable.isTokenizable(value)) return value;
100
+ return helpers.error("any.invalid");
101
+ }));
102
+ var geminiTokenizerInstance;
103
+ var llamaTokenizerInstance;
104
+ var getGeminiTokenizer = () => {
105
+ if (!geminiTokenizerInstance) geminiTokenizerInstance = (0, _lenml_tokenizer_gemini.fromPreTrained)();
106
+ return geminiTokenizerInstance;
107
+ };
108
+ var getLlamaTokenizer = () => {
109
+ if (!llamaTokenizerInstance) llamaTokenizerInstance = new llama_tokenizer_js.LlamaTokenizer();
110
+ return llamaTokenizerInstance;
111
+ };
112
+ /**
113
+ * A mutable string with a built-in token counter.
114
+ *
115
+ * @remarks
116
+ * The wrapped string can be read via the standard coercion protocol and updated at any time via
117
+ * {@link Tokenizable.set}. Token counts are computed lazily on first access per encoding and
118
+ * cached until the value changes, avoiding redundant encoder invocations when the same content
119
+ * is measured multiple times across a pipeline.
120
+ *
121
+ * Estimation is dispatched by encoding identifier — see {@link TokenEncoding} for the full list
122
+ * of supported backends and their accuracy characteristics. Unrecognised encodings fall back to
123
+ * a `ceil(length / 4)` character heuristic.
124
+ *
125
+ * The class implements the standard JS value-coercion protocol (`toString`, `valueOf`,
126
+ * `toJSON`, `toLocaleString`, `Symbol.for('nodejs.util.inspect.custom')`) so instances behave
127
+ * transparently as strings in most contexts.
128
+ */
129
+ var Tokenizable = class Tokenizable {
130
+ static TokenEncoding = TokenEncoding;
131
+ /**
132
+ * Validator schema that accepts a plain `string` or a {@link Tokenizable} instance.
133
+ *
134
+ * @remarks
135
+ * Reusable fragment for any schema that wants to accept either form — for example,
136
+ * `systemPrompt` and each item in `standingInstructions` in `turnContextSchema`.
137
+ */
138
+ static schema = stringOrTokenizableSchema;
139
+ #value;
140
+ #cache = /* @__PURE__ */ new Map();
141
+ /**
142
+ * @param value - The initial string value to wrap.
143
+ */
144
+ constructor(value) {
145
+ this.#value = value;
146
+ const estimateTokensWithTiktoken = (encoding) => {
147
+ try {
148
+ return (0, js_tiktoken.getEncoding)(encoding).encode(this.#value, []).length;
149
+ } catch {
150
+ return Number.POSITIVE_INFINITY;
151
+ }
152
+ };
153
+ const estimateTokensWithGemini = () => {
154
+ try {
155
+ return getGeminiTokenizer().encode(this.#value).length;
156
+ } catch {
157
+ return Number.POSITIVE_INFINITY;
158
+ }
159
+ };
160
+ const estimateTokensWithLlama2 = () => {
161
+ try {
162
+ return getLlamaTokenizer().encode(this.#value, false).length;
163
+ } catch {
164
+ return Number.POSITIVE_INFINITY;
165
+ }
166
+ };
167
+ const estimateTokensWithClaudeHeuristic = () => {
168
+ try {
169
+ return Math.ceil(this.#value.length / 3.5);
170
+ } catch {
171
+ return Number.POSITIVE_INFINITY;
172
+ }
173
+ };
174
+ Object.defineProperties(this, {
175
+ toJSON: {
176
+ value: () => this.#value,
177
+ enumerable: false,
178
+ configurable: false,
179
+ writable: false
180
+ },
181
+ toString: {
182
+ value: () => this.#value,
183
+ enumerable: false,
184
+ configurable: false,
185
+ writable: false
186
+ },
187
+ valueOf: {
188
+ value: () => this.#value,
189
+ enumerable: false,
190
+ configurable: false,
191
+ writable: false
192
+ },
193
+ toLocaleString: {
194
+ value: () => this.#value,
195
+ enumerable: false,
196
+ configurable: false,
197
+ writable: false
198
+ },
199
+ [Symbol.for("nodejs.util.inspect.custom")]: {
200
+ value: () => this.#value,
201
+ enumerable: false,
202
+ configurable: false,
203
+ writable: false
204
+ },
205
+ set: {
206
+ value: (next) => {
207
+ this.#value = next;
208
+ this.#cache.clear();
209
+ },
210
+ enumerable: false,
211
+ configurable: false,
212
+ writable: false
213
+ },
214
+ estimateTokens: {
215
+ value: (encoding) => {
216
+ if (this.#cache.has(encoding)) return this.#cache.get(encoding);
217
+ let estimate;
218
+ switch (encoding) {
219
+ case "gpt2":
220
+ case "r50k_base":
221
+ case "p50k_base":
222
+ case "p50k_edit":
223
+ case "cl100k_base":
224
+ case "o200k_base":
225
+ estimate = estimateTokensWithTiktoken(encoding);
226
+ break;
227
+ case "gemini":
228
+ estimate = estimateTokensWithGemini();
229
+ break;
230
+ case "llama2":
231
+ estimate = estimateTokensWithLlama2();
232
+ break;
233
+ case "claude":
234
+ estimate = estimateTokensWithClaudeHeuristic();
235
+ break;
236
+ }
237
+ if (estimate === Number.POSITIVE_INFINITY) estimate = Math.ceil(this.#value.length / 4);
238
+ else this.#cache.set(encoding, estimate);
239
+ return estimate;
240
+ },
241
+ enumerable: false,
242
+ configurable: false,
243
+ writable: false
244
+ }
245
+ });
246
+ }
247
+ /**
248
+ * Convenience overload for one-off token counting without managing a {@link Tokenizable} instance.
249
+ *
250
+ * @remarks
251
+ * Creates a temporary instance and immediately discards it — no caching benefit. Use the
252
+ * instance method when you need to count the same value under multiple encodings or when the
253
+ * value may change over time.
254
+ *
255
+ * @param value - The string to count tokens for.
256
+ * @param encoding - The encoding identifier to use for counting.
257
+ * @returns The estimated number of tokens.
258
+ */
259
+ static estimateTokens(value, encoding) {
260
+ return new Tokenizable(value).estimateTokens(encoding);
261
+ }
262
+ /**
263
+ * Returns `true` if `value` is a {@link Tokenizable} instance.
264
+ *
265
+ * @remarks
266
+ * Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety — `instanceof` would fail for instances
267
+ * created in a different module copy or VM context.
268
+ *
269
+ * @param value - The value to test.
270
+ * @returns `true` when `value` is a {@link Tokenizable} instance.
271
+ */
272
+ static isTokenizable(value) {
273
+ return isInstanceOf(value, "Tokenizable", Tokenizable);
274
+ }
275
+ };
276
+ //#endregion
277
+ //#region src/lib/classes/registry.ts
278
+ /**
279
+ * A controlled-mutation key-value store with dot-path access and deep-clone isolation.
280
+ *
281
+ * @remarks
282
+ * The registry enforces a safe read/write contract: callers never hold a live reference into
283
+ * the internal store. Every value that enters (`set`) or leaves (`get`, `all`) is deep-cloned
284
+ * via `klona`, so mutations to a retrieved value cannot affect stored state and vice versa.
285
+ *
286
+ * Keys are dot-delimited paths (e.g. `"user.profile.name"`), resolved via `dlv` for reads and
287
+ * `dset` for writes; intermediate objects are created automatically on write.
288
+ */
289
+ var Registry = class Registry {
290
+ #store;
291
+ /**
292
+ * @param initial - Optional plain object to seed the registry. Deep-cloned on construction.
293
+ * @throws {@link @nhtio/adk!E_INVALID_INITIAL_REGISTRY_VALUE} when `initial` is defined but not a plain object.
294
+ */
295
+ constructor(initial) {
296
+ if ("undefined" !== typeof initial && !isObject(initial)) throw new require_runtime.E_INVALID_INITIAL_REGISTRY_VALUE();
297
+ this.#store = initial ? (0, klona.klona)(initial) : {};
298
+ }
299
+ /**
300
+ * Returns `true` if `value` is a {@link Registry} instance.
301
+ *
302
+ * @remarks
303
+ * Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety.
304
+ *
305
+ * @param value - The value to test.
306
+ * @returns `true` when `value` is a {@link Registry} instance.
307
+ */
308
+ static isRegistry(value) {
309
+ return isInstanceOf(value, "Registry", Registry);
310
+ }
311
+ /**
312
+ * Retrieves the value at `key`, returning `defaultValue` if the path is absent.
313
+ *
314
+ * @remarks
315
+ * The returned value is a deep clone — mutating it will not affect the stored state.
316
+ *
317
+ * @typeParam T - Expected type of the value at `key`.
318
+ * @param key - Dot-delimited path into the store (e.g. `"user.name"`).
319
+ * @param defaultValue - Fallback returned when the path resolves to `undefined`.
320
+ * @returns A deep clone of the stored value cast to `T`, or `defaultValue` when the path is absent.
321
+ */
322
+ get(key, defaultValue) {
323
+ const value = (0, dlv.default)((0, klona.klona)(this.#store), key);
324
+ return "undefined" === typeof value ? defaultValue : value;
325
+ }
326
+ /**
327
+ * Sets the value at `key`, creating intermediate objects as needed.
328
+ *
329
+ * @remarks
330
+ * The stored value is isolated from the caller — mutating `value` after this call will not
331
+ * affect what is held in the registry.
332
+ *
333
+ * @param key - Dot-delimited path into the store (e.g. `"user.name"`).
334
+ * @param value - Value to store at the path.
335
+ */
336
+ set(key, value) {
337
+ (0, dset.dset)(this.#store, key, value);
338
+ }
339
+ /**
340
+ * Returns `true` if the registry has a value at `key`, `false` otherwise.
341
+ *
342
+ * @remarks
343
+ * A key resolving to `undefined` is treated as absent — same convention as {@link Registry.get}'s
344
+ * `defaultValue` fallback. No clone is performed; this is a pure existence check.
345
+ *
346
+ * @param key - Dot-delimited path into the store (e.g. `"user.name"`).
347
+ * @returns `true` when the path resolves to a value other than `undefined`.
348
+ */
349
+ has(key) {
350
+ return "undefined" !== typeof (0, dlv.default)(this.#store, key);
351
+ }
352
+ /**
353
+ * Returns all leaf dot-paths present in the registry.
354
+ *
355
+ * @remarks
356
+ * The store is deep-cloned before traversal. Plain objects are walked recursively with path
357
+ * segments joined by dots; arrays, primitives, `null`, and class instances are treated as leaves.
358
+ *
359
+ * @returns A string array of dot-delimited paths to leaf values in the store.
360
+ */
361
+ keys() {
362
+ const store = (0, klona.klona)(this.#store);
363
+ const keys = [];
364
+ const isPlainRecord = (value) => {
365
+ if (!isObject(value)) return false;
366
+ const prototype = Object.getPrototypeOf(value);
367
+ return prototype === Object.prototype || prototype === null;
368
+ };
369
+ const walk = (value, segments) => {
370
+ if (!isPlainRecord(value)) {
371
+ if (segments.length > 0) keys.push(segments.join("."));
372
+ return;
373
+ }
374
+ for (const [segment, child] of Object.entries(value)) walk(child, [...segments, segment]);
375
+ };
376
+ walk(store, []);
377
+ return keys;
378
+ }
379
+ /**
380
+ * Returns a deep clone of the entire store contents.
381
+ *
382
+ * @returns A plain object snapshot of all stored key-value pairs.
383
+ */
384
+ all() {
385
+ return (0, klona.klona)(this.#store);
386
+ }
387
+ };
388
+ //#endregion
389
+ //#region src/lib/utils/canonical_json.ts
390
+ /**
391
+ * Canonical `JSON.stringify` that sorts object keys recursively so that semantically-equal
392
+ * objects produce identical strings.
393
+ *
394
+ * @remarks
395
+ * Used wherever the ADK derives a stable identity from a structured value — for example,
396
+ * `Tool.executor` computing the `callId` for `ToolExecutionStart`/`End` events, and the
397
+ * `reportToolCall` executor helper computing the `checksum` field on `TurnToolCallContent`.
398
+ * Both code paths hash `canonicalStringify({ tool, args })` so that argument key order does not
399
+ * affect the resulting identifier.
400
+ *
401
+ * Arrays are serialised in their declared order (order is meaningful for an array). Object keys
402
+ * are sorted with `Array.prototype.sort()`'s default lexicographic comparator.
403
+ *
404
+ * @param value - The value to serialise.
405
+ * @returns A canonical JSON string representation of `value`.
406
+ */
407
+ function canonicalStringify(value) {
408
+ if (value === null || typeof value !== "object") return JSON.stringify(value);
409
+ if (Array.isArray(value)) return "[" + value.map((v) => canonicalStringify(v)).join(",") + "]";
410
+ const obj = value;
411
+ return "{" + Object.keys(obj).sort().map((k) => JSON.stringify(k) + ":" + canonicalStringify(obj[k])).join(",") + "}";
412
+ }
413
+ //#endregion
414
+ //#region src/lib/classes/tool_registry.ts
415
+ /**
416
+ * A mutable, turn-scoped collection of {@link @nhtio/adk!Tool} instances.
417
+ *
418
+ * @remarks
419
+ * Each `TurnRunner.run()` call constructs a fresh `ToolRegistry` from the runner's configured
420
+ * baseline tools, so middleware edits are isolated to the current turn and cannot bleed across
421
+ * concurrent or subsequent turns.
422
+ *
423
+ * `Tool` instances are immutable, so `all()` returns a fresh array without deep-cloning.
424
+ *
425
+ * `register()` throws {@link @nhtio/adk!E_TOOL_ALREADY_REGISTERED} if a tool with the same name is already
426
+ * present — pass `overwrite: true` to replace it explicitly.
427
+ */
428
+ var ToolRegistry = class ToolRegistry {
429
+ #tools;
430
+ /**
431
+ * Returns `true` if `value` is a {@link ToolRegistry} instance.
432
+ *
433
+ * @param value - The value to test.
434
+ * @returns `true` when `value` is a {@link ToolRegistry} instance.
435
+ */
436
+ static isToolRegistry(value) {
437
+ return isInstanceOf(value, "ToolRegistry", ToolRegistry);
438
+ }
439
+ /**
440
+ * @param tools - Optional initial tools. Insertion order is preserved. Duplicate names throw
441
+ * {@link @nhtio/adk!E_TOOL_ALREADY_REGISTERED} — ensure each tool has a unique name.
442
+ * @throws {@link @nhtio/adk!E_TOOL_ALREADY_REGISTERED} when two tools in `tools` share a name.
443
+ */
444
+ constructor(tools) {
445
+ this.#tools = /* @__PURE__ */ new Map();
446
+ for (const tool of tools ?? []) this.register(tool);
447
+ }
448
+ /**
449
+ * Adds a tool to the registry.
450
+ *
451
+ * @param tool - The tool to register.
452
+ * @param overwrite - When `true`, silently replaces an existing tool with the same name.
453
+ * Defaults to `false`.
454
+ * @throws {@link @nhtio/adk!E_TOOL_ALREADY_REGISTERED} when a tool with the same name is already registered
455
+ * and `overwrite` is not `true`.
456
+ */
457
+ register(tool, overwrite) {
458
+ if (this.#tools.has(tool.name) && !overwrite) throw new require_runtime.E_TOOL_ALREADY_REGISTERED();
459
+ this.#tools.set(tool.name, tool);
460
+ }
461
+ /**
462
+ * Removes the tool with the given name from the registry.
463
+ *
464
+ * @remarks
465
+ * No-ops if no tool with that name is registered.
466
+ *
467
+ * @param name - The name of the tool to remove.
468
+ */
469
+ unregister(name) {
470
+ this.#tools.delete(name);
471
+ }
472
+ /**
473
+ * Returns the tool registered under `name`, or `undefined` if not present.
474
+ *
475
+ * @param name - The tool name to look up.
476
+ */
477
+ get(name) {
478
+ return this.#tools.get(name);
479
+ }
480
+ /**
481
+ * Returns `true` if a tool with the given name is registered.
482
+ *
483
+ * @param name - The tool name to test.
484
+ */
485
+ has(name) {
486
+ return this.#tools.has(name);
487
+ }
488
+ /**
489
+ * Returns a fresh array of all registered tools in insertion order.
490
+ *
491
+ * @remarks
492
+ * Since {@link @nhtio/adk!Tool} instances are immutable, no deep-cloning is needed.
493
+ */
494
+ all() {
495
+ return Array.from(this.#tools.values());
496
+ }
497
+ /**
498
+ * Removes every tool whose {@link @nhtio/adk!Tool.ephemeral} flag is `true`.
499
+ *
500
+ * @remarks
501
+ * Synchronous and idempotent — calling it twice in a row is a no-op the second time. The
502
+ * canonical caller is {@link ToolRegistry.bindContext}, which schedules this method to run
503
+ * at {@link @nhtio/adk!DispatchContext.ack}. Non-ephemeral tools are left untouched.
504
+ */
505
+ pruneEphemeral() {
506
+ for (const [name, tool] of this.#tools) if (tool.ephemeral) this.#tools.delete(name);
507
+ }
508
+ /**
509
+ * Binds this registry to a {@link @nhtio/adk!DispatchContext} so that {@link pruneEphemeral} runs
510
+ * automatically when the context is acked.
511
+ *
512
+ * @remarks
513
+ * The handler does NOT fire on {@link @nhtio/adk!DispatchContext.nack} — failed executor runs leave
514
+ * any forged tools in place so the consumer can inspect what was registered when debugging the
515
+ * failure. Subscriptions are short-lived and die with the context regardless.
516
+ *
517
+ * Forgetting this call after merging in `Subclass.forgeTools(ctx)` output means ephemeral tools
518
+ * accumulate across executor invocations, and subsequent `forgeTools(ctx)` calls in later
519
+ * iterations will see a stale `callId` enum that excludes new tool calls. The plan-documented
520
+ * pattern is:
521
+ *
522
+ * ```ts
523
+ * const executor: DispatchExecutorFn = async (ctx) => {
524
+ * const forged = SpooledArtifact.forgeTools(ctx)
525
+ * const merged = ToolRegistry.merge([main, forged])
526
+ * main.bindContext(ctx)
527
+ * const result = await llm.invoke({ tools: merged.all(), ... })
528
+ * ctx.ack()
529
+ * }
530
+ * ```
531
+ *
532
+ * @param ctx - The execution context whose `ack` event should trigger pruning.
533
+ * @returns An unsubscribe function — calling it before `ctx.ack()` prevents pruning. Rarely
534
+ * useful outside of tests.
535
+ *
536
+ * @see {@link @nhtio/adk!SpooledArtifact.forgeTools}
537
+ * @see {@link @nhtio/adk!DispatchContext.onAck}
538
+ */
539
+ bindContext(ctx) {
540
+ return ctx.onAck(() => this.pruneEphemeral());
541
+ }
542
+ /**
543
+ * Combines multiple {@link ToolRegistry} instances into a fresh registry without mutating any
544
+ * input.
545
+ *
546
+ * @remarks
547
+ * Iteration is left-to-right across `registries` and then in each registry's insertion order.
548
+ * Collisions are resolved by consulting the **incoming** tool's {@link @nhtio/adk!Tool.onCollision} first:
549
+ *
550
+ * - `'replace'` (per-tool): the incoming tool wins, replacing the existing entry.
551
+ * - `'keep'` (per-tool): the existing entry wins; the incoming tool is dropped.
552
+ * - `'throw'` (per-tool, the default): fall back to the merge-level `options.onCollision`.
553
+ *
554
+ * The merge-level `options.onCollision` defaults to `'throw'`, which mirrors {@link register}.
555
+ *
556
+ * The result is a brand-new registry; no input is mutated and no event subscription is
557
+ * propagated. Each `Tool`'s `ephemeral` flag carries through unchanged — the flag lives on the
558
+ * tool, not the registry, so `bindContext(ctx)` on the merged registry will prune the forged
559
+ * tools as expected.
560
+ *
561
+ * @param registries - Registries to merge, in priority order (left-to-right insertion).
562
+ * @param options - Merge-level collision policy. Defaults to `{ onCollision: 'throw' }`.
563
+ * @returns A fresh {@link ToolRegistry} containing the resolved union of all inputs.
564
+ * @throws {@link @nhtio/adk!E_TOOL_ALREADY_REGISTERED} when the resolved collision policy is `'throw'`
565
+ * and a collision occurs.
566
+ */
567
+ static merge(registries, options) {
568
+ const policy = options?.onCollision ?? "throw";
569
+ const merged = new ToolRegistry();
570
+ for (const registry of registries) for (const tool of registry.all()) {
571
+ if (!merged.get(tool.name)) {
572
+ merged.register(tool);
573
+ continue;
574
+ }
575
+ const incomingPolicy = tool.onCollision;
576
+ if (incomingPolicy === "replace") {
577
+ merged.register(tool, true);
578
+ continue;
579
+ }
580
+ if (incomingPolicy === "keep") continue;
581
+ if (policy === "replace") {
582
+ merged.register(tool, true);
583
+ continue;
584
+ }
585
+ if (policy === "keep") continue;
586
+ throw new require_runtime.E_TOOL_ALREADY_REGISTERED();
587
+ }
588
+ return merged;
589
+ }
590
+ };
591
+ //#endregion
592
+ Object.defineProperty(exports, "Registry", {
593
+ enumerable: true,
594
+ get: function() {
595
+ return Registry;
596
+ }
597
+ });
598
+ Object.defineProperty(exports, "TokenEncoding", {
599
+ enumerable: true,
600
+ get: function() {
601
+ return TokenEncoding;
602
+ }
603
+ });
604
+ Object.defineProperty(exports, "Tokenizable", {
605
+ enumerable: true,
606
+ get: function() {
607
+ return Tokenizable;
608
+ }
609
+ });
610
+ Object.defineProperty(exports, "ToolRegistry", {
611
+ enumerable: true,
612
+ get: function() {
613
+ return ToolRegistry;
614
+ }
615
+ });
616
+ Object.defineProperty(exports, "canonicalStringify", {
617
+ enumerable: true,
618
+ get: function() {
619
+ return canonicalStringify;
620
+ }
621
+ });
622
+ Object.defineProperty(exports, "isError", {
623
+ enumerable: true,
624
+ get: function() {
625
+ return isError;
626
+ }
627
+ });
628
+ Object.defineProperty(exports, "isInstanceOf", {
629
+ enumerable: true,
630
+ get: function() {
631
+ return isInstanceOf;
632
+ }
633
+ });
634
+ Object.defineProperty(exports, "isObject", {
635
+ enumerable: true,
636
+ get: function() {
637
+ return isObject;
638
+ }
639
+ });
640
+
641
+ //# sourceMappingURL=tool_registry-Dkfprsck.js.map