@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,603 @@
1
+ import { a as validateOrThrow, i as passesSchema } from "./exceptions-NrzIHw_R.mjs";
2
+ import { c as isObject, o as isError, s as isInstanceOf, t as ToolRegistry } from "./tool_registry-DqLOyGyG.mjs";
3
+ import { O as E_TURN_GATE_ABORTED, T as E_PIPELINE_SHORT_CIRCUITED, _ as E_INVALID_TURN_GATE_RESOLUTION, f as E_INVALID_INITIAL_TURN_GATE_VALUE, k as E_TURN_GATE_TIMEOUT, n as E_INPUT_PIPELINE_ERROR, v as E_INVALID_TURN_RUNNER_CONFIG, w as E_OUTPUT_PIPELINE_ERROR } from "./runtime-CrEPIFgr.mjs";
4
+ import { t as Tool } from "./tool-D2WB1EA1.mjs";
5
+ import { r as TurnContext, t as DispatchRunner } from "./dispatch_runner-9j6bXHL3.mjs";
6
+ import { validator } from "@nhtio/validation";
7
+ import { DateTime } from "luxon";
8
+ import { Middleware } from "@nhtio/middleware";
9
+ import { TypedEventEmitter } from "@nhtio/tiny-typed-emitter";
10
+ //#region src/lib/classes/turn_gate.ts
11
+ /**
12
+ * Validator schema used to validate a {@link RawTurnGate} before constructing a {@link TurnGate}.
13
+ *
14
+ * @remarks
15
+ * - `schema` and `abortSignal` are validated as opaque passthrough values.
16
+ * - `timeout` must be a positive integer when provided.
17
+ */
18
+ var rawTurnGateSchema = validator.object({
19
+ id: validator.string().required(),
20
+ turnId: validator.string().required(),
21
+ reason: validator.string().required(),
22
+ payload: validator.any().required(),
23
+ schema: validator.any().custom((value, helpers) => {
24
+ if (value === void 0 || value === null) return void 0;
25
+ if (typeof value.validate === "function") return value;
26
+ return helpers.error("any.invalid");
27
+ }).optional(),
28
+ timeout: validator.number().integer().min(1).optional(),
29
+ abortSignal: validator.any().custom((value, helpers) => {
30
+ if (value === void 0 || value === null) return void 0;
31
+ if (typeof AbortSignal !== "undefined" && value instanceof AbortSignal) return value;
32
+ if (isObject(value) && typeof value.aborted === "boolean" && typeof value.addEventListener === "function") return value;
33
+ return helpers.error("any.invalid");
34
+ }).optional(),
35
+ createdAt: validator.datetime().required()
36
+ });
37
+ /**
38
+ * A cooperative suspension gate that blocks a turn's middleware pipeline until resolved, rejected,
39
+ * aborted, or timed out.
40
+ *
41
+ * @typeParam T - The expected type of the resolution value.
42
+ *
43
+ * @remarks
44
+ * Created exclusively via `ctx.waitFor()` — middleware never constructs a gate directly.
45
+ * The gate emits `turnGateOpen` on the runner's observability bus at creation time and
46
+ * `turnGateClosed` when it settles.
47
+ *
48
+ * Resolution is validated against an optional schema before the internal promise is settled.
49
+ * A validation failure throws {@link @nhtio/adk!E_INVALID_TURN_GATE_RESOLUTION} **synchronously in the
50
+ * caller's context** — the promise is NOT settled and the gate remains open.
51
+ */
52
+ var TurnGate = class TurnGate {
53
+ /**
54
+ * Validator schema that accepts a {@link RawTurnGate} object.
55
+ *
56
+ * @remarks
57
+ * Reusable fragment for any schema that needs to validate or nest a gate entry.
58
+ */
59
+ static schema = rawTurnGateSchema;
60
+ /**
61
+ * Returns `true` if `value` is a {@link TurnGate} instance.
62
+ *
63
+ * @remarks
64
+ * Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety.
65
+ *
66
+ * @param value - The value to test.
67
+ * @returns `true` when `value` is a {@link TurnGate} instance.
68
+ */
69
+ static isTurnGate(value) {
70
+ return isInstanceOf(value, "TurnGate", TurnGate);
71
+ }
72
+ #id;
73
+ #turnId;
74
+ #reason;
75
+ #payload;
76
+ #createdAt;
77
+ #settled;
78
+ #schema;
79
+ #controller;
80
+ #resolve;
81
+ #reject;
82
+ #promise;
83
+ /**
84
+ * @param raw - The raw gate input validated against `rawTurnGateSchema`.
85
+ * @throws {@link @nhtio/adk!E_INVALID_INITIAL_TURN_GATE_VALUE} when `raw` does not satisfy the schema.
86
+ */
87
+ constructor(raw) {
88
+ let resolved;
89
+ try {
90
+ resolved = validateOrThrow(rawTurnGateSchema, raw, true);
91
+ } catch (err) {
92
+ throw new E_INVALID_INITIAL_TURN_GATE_VALUE({ cause: isError(err) ? err : void 0 });
93
+ }
94
+ this.#id = resolved.id;
95
+ this.#turnId = resolved.turnId;
96
+ this.#reason = resolved.reason;
97
+ this.#payload = resolved.payload;
98
+ this.#createdAt = resolved.createdAt;
99
+ this.#settled = false;
100
+ this.#schema = resolved.schema;
101
+ this.#controller = new AbortController();
102
+ this.#promise = new Promise((resolve, reject) => {
103
+ this.#resolve = resolve;
104
+ this.#reject = reject;
105
+ });
106
+ const onAbort = () => {
107
+ if (!this.#settled) {
108
+ this.#settled = true;
109
+ this.#reject(new E_TURN_GATE_ABORTED());
110
+ }
111
+ };
112
+ this.#controller.signal.addEventListener("abort", onAbort, { once: true });
113
+ if (resolved.abortSignal) if (resolved.abortSignal.aborted) queueMicrotask(() => onAbort());
114
+ else {
115
+ resolved.abortSignal.addEventListener("abort", onAbort, { once: true });
116
+ this.#promise.then(() => resolved.abortSignal.removeEventListener("abort", onAbort), () => resolved.abortSignal.removeEventListener("abort", onAbort));
117
+ }
118
+ if (resolved.timeout !== void 0) {
119
+ const timer = setTimeout(() => {
120
+ if (!this.#settled) {
121
+ this.#settled = true;
122
+ this.#reject(new E_TURN_GATE_TIMEOUT());
123
+ }
124
+ }, resolved.timeout);
125
+ this.#promise.then(() => clearTimeout(timer), () => clearTimeout(timer));
126
+ }
127
+ Object.defineProperties(this, {
128
+ id: {
129
+ get: () => this.#id,
130
+ enumerable: true,
131
+ configurable: false
132
+ },
133
+ turnId: {
134
+ get: () => this.#turnId,
135
+ enumerable: true,
136
+ configurable: false
137
+ },
138
+ reason: {
139
+ get: () => this.#reason,
140
+ enumerable: true,
141
+ configurable: false
142
+ },
143
+ payload: {
144
+ get: () => this.#payload,
145
+ enumerable: true,
146
+ configurable: false
147
+ },
148
+ createdAt: {
149
+ get: () => this.#createdAt,
150
+ enumerable: true,
151
+ configurable: false
152
+ },
153
+ isSettled: {
154
+ get: () => this.#settled,
155
+ enumerable: true,
156
+ configurable: false
157
+ }
158
+ });
159
+ }
160
+ /**
161
+ * Resolves the gate with `value`, unblocking the awaiting middleware.
162
+ *
163
+ * @remarks
164
+ * If a schema was provided at construction, `value` is validated synchronously before the
165
+ * promise is settled. A validation failure throws {@link @nhtio/adk!E_INVALID_TURN_GATE_RESOLUTION}
166
+ * in the caller's context — the promise is NOT settled and the gate remains open.
167
+ *
168
+ * No-ops if the gate is already settled.
169
+ *
170
+ * @param value - The resolution value. Must satisfy the gate's schema when one was provided.
171
+ * @throws {@link @nhtio/adk!E_INVALID_TURN_GATE_RESOLUTION} when `value` fails schema validation.
172
+ */
173
+ resolve(value) {
174
+ if (this.#settled) return;
175
+ if (this.#schema !== void 0) try {
176
+ value = validateOrThrow(this.#schema, value, true);
177
+ } catch (err) {
178
+ throw new E_INVALID_TURN_GATE_RESOLUTION({ cause: isError(err) ? err : void 0 });
179
+ }
180
+ this.#settled = true;
181
+ this.#resolve(value);
182
+ }
183
+ /**
184
+ * Rejects the gate with `error`, unblocking the awaiting middleware with a rejection.
185
+ *
186
+ * @remarks
187
+ * No-ops if the gate is already settled.
188
+ *
189
+ * @param error - The rejection reason.
190
+ */
191
+ reject(error) {
192
+ if (this.#settled) return;
193
+ this.#settled = true;
194
+ this.#reject(error);
195
+ }
196
+ /**
197
+ * Aborts the gate by firing the internal `AbortController`, which rejects the promise with
198
+ * {@link @nhtio/adk!E_TURN_GATE_ABORTED}.
199
+ *
200
+ * @remarks
201
+ * No-ops if the gate is already settled. Distinct from the turn-level abort signal — this
202
+ * allows callers to cancel a specific gate without aborting the whole turn.
203
+ */
204
+ abort() {
205
+ if (this.#settled) return;
206
+ this.#controller.abort();
207
+ }
208
+ /**
209
+ * Returns the internal promise. Called by `ctx.waitFor()` to block the middleware pipeline.
210
+ *
211
+ * @internal
212
+ */
213
+ _promise() {
214
+ return this.#promise;
215
+ }
216
+ };
217
+ //#endregion
218
+ //#region src/lib/contracts/turn_runner_config.ts
219
+ /**
220
+ * Validator schema used to validate a {@link TurnRunnerConfig} at {@link @nhtio/adk!TurnRunner} construction time.
221
+ *
222
+ * @remarks
223
+ * Validates that all callbacks are functions of the correct arity, and that `tools` — when
224
+ * provided — is an array of valid {@link @nhtio/adk!Tool} instances. Defaults `tools` to `[]`.
225
+ *
226
+ * Throws {@link @nhtio/adk!E_INVALID_TURN_RUNNER_CONFIG} (via the {@link @nhtio/adk!TurnRunner} constructor) when
227
+ * validation fails.
228
+ */
229
+ var turnRunnerConfigSchema = validator.object({
230
+ executorCallback: validator.function().required(),
231
+ fetchMemoriesCallback: validator.function().arity(1).required(),
232
+ fetchMessagesCallback: validator.function().arity(1).required(),
233
+ fetchThoughtsCallback: validator.function().arity(1).required(),
234
+ fetchToolCallsCallback: validator.function().arity(1).required(),
235
+ fetchToolsCallback: validator.function().arity(1).required(),
236
+ refreshStandingInstructionsCallback: validator.function().arity(1).required(),
237
+ storeStandingInstructionCallback: validator.function().arity(2).required(),
238
+ mutateStandingInstructionCallback: validator.function().arity(2).required(),
239
+ deleteStandingInstructionCallback: validator.function().arity(2).required(),
240
+ storeMemoryCallback: validator.function().arity(2).required(),
241
+ mutateMemoryCallback: validator.function().arity(2).required(),
242
+ deleteMemoryCallback: validator.function().arity(2).required(),
243
+ fetchRetrievablesCallback: validator.function().arity(1).required(),
244
+ storeRetrievableCallback: validator.function().arity(2).required(),
245
+ mutateRetrievableCallback: validator.function().arity(2).required(),
246
+ deleteRetrievableCallback: validator.function().arity(2).required(),
247
+ storeMessageCallback: validator.function().arity(2).required(),
248
+ mutateMessageCallback: validator.function().arity(2).required(),
249
+ deleteMessageCallback: validator.function().arity(2).required(),
250
+ storeThoughtCallback: validator.function().arity(2).required(),
251
+ mutateThoughtCallback: validator.function().arity(2).required(),
252
+ deleteThoughtCallback: validator.function().arity(2).required(),
253
+ storeToolCallCallback: validator.function().arity(2).required(),
254
+ mutateToolCallCallback: validator.function().arity(2).required(),
255
+ deleteToolCallCallback: validator.function().arity(2).required(),
256
+ tools: validator.array().items(validator.any().custom((value, helpers) => {
257
+ if (Tool.isTool(value)) return value;
258
+ return helpers.error("any.invalid");
259
+ })).default([]),
260
+ turnInputPipeline: validator.array().items(validator.function()).default([]),
261
+ turnOutputPipeline: validator.array().items(validator.function()).default([]),
262
+ dispatchInputPipeline: validator.array().items(validator.function()).default([]),
263
+ dispatchOutputPipeline: validator.array().items(validator.function()).default([])
264
+ });
265
+ //#endregion
266
+ //#region src/lib/turn_runner.ts
267
+ /**
268
+ * Executes a single agent turn through paired input and output middleware pipelines.
269
+ *
270
+ * @remarks
271
+ * Construction validates `config` eagerly and throws {@link @nhtio/adk!E_INVALID_TURN_RUNNER_CONFIG} if it
272
+ * does not satisfy the schema — fail-fast so misconfiguration surfaces before any turn runs.
273
+ *
274
+ * Each call to {@link TurnRunner.run} threads a {@link @nhtio/adk!TurnContext} through the input pipeline,
275
+ * invokes the model, then threads the result through the output pipeline. Middleware on each side
276
+ * can read and mutate the context for pre- and post-processing (e.g. message normalisation, tool
277
+ * call dispatch, response filtering).
278
+ *
279
+ * **Two event buses:**
280
+ * - Functional bus (`on` / `off` / `once`): `message`, `thought`, `toolCall` — pipeline-affecting
281
+ * events that middleware raises throughout turn execution.
282
+ * - Observability bus (`observe` / `unobserve` / `observeOnce`): `turnStart`, `turnEnd`,
283
+ * `turnGateOpen`, `turnGateClosed`, `error` — instrumentation-only events that monitor execution
284
+ * without participating in it.
285
+ *
286
+ * Streaming content is surfaced via `message` and `thought` events; tool call lifecycle via
287
+ * `toolCall`; non-fatal pipeline errors via the observability `error` event; gate lifecycle via
288
+ * `turnGateOpen` and `turnGateClosed` — all throughout execution.
289
+ *
290
+ * @example
291
+ * ```ts
292
+ * const runner = new TurnRunner({
293
+ * fetchMemoriesCallback: async (ctx) => memoryStore.query(ctx),
294
+ * fetchMessagesCallback: async (ctx) => messageStore.history(ctx),
295
+ * fetchThoughtsCallback: async (ctx) => thoughtStore.history(ctx),
296
+ * fetchToolCallsCallback: async (ctx) => toolCallStore.history(ctx),
297
+ * })
298
+ * // Functional bus — pipeline events
299
+ * runner.on('message', (chunk) => process.stdout.write(chunk.aDelta))
300
+ * // Observability bus — instrumentation
301
+ * runner.observe('error', (err) => console.error(err.toString()))
302
+ * runner.observe('turnStart', ({ turnId }) => console.log('turn started', turnId))
303
+ * runner.observe('turnGateOpen', (gate) => {
304
+ * if (gate.reason === 'tool_approval') {
305
+ * gate.resolve(true) // approve immediately for this example
306
+ * }
307
+ * })
308
+ * await runner.run({
309
+ * turnAbortController: new AbortController(),
310
+ * systemPrompt: 'You are a helpful assistant.',
311
+ * standingInstructions: [],
312
+ * })
313
+ * ```
314
+ */
315
+ var TurnRunner = class TurnRunner {
316
+ /**
317
+ * Returns `true` if `value` is a {@link TurnRunner} instance.
318
+ *
319
+ * @remarks
320
+ * Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety.
321
+ *
322
+ * @param value - The value to test.
323
+ * @returns `true` when `value` is a {@link TurnRunner} instance.
324
+ */
325
+ static isTurnRunner(value) {
326
+ return isInstanceOf(value, "TurnRunner", TurnRunner);
327
+ }
328
+ #config;
329
+ #inputRunner;
330
+ #outputRunner;
331
+ #functionalEmitter;
332
+ #observabilityEmitter;
333
+ /**
334
+ * @param config - Construction-time configuration validated against {@link turnRunnerConfigSchema}.
335
+ * @throws {@link @nhtio/adk!E_INVALID_TURN_RUNNER_CONFIG} when `config` does not satisfy the schema.
336
+ */
337
+ constructor(config) {
338
+ if (!passesSchema(turnRunnerConfigSchema, config)) throw new E_INVALID_TURN_RUNNER_CONFIG();
339
+ this.#config = validateOrThrow(turnRunnerConfigSchema, config, true);
340
+ const turnInputPipeline = new Middleware();
341
+ const turnOutputPipeline = new Middleware();
342
+ const wrap = (fn) => (ctx, next) => {
343
+ if (ctx.aborted) return next();
344
+ return fn(ctx, next);
345
+ };
346
+ for (const fn of this.#config.turnInputPipeline) turnInputPipeline.add(wrap(fn));
347
+ for (const fn of this.#config.turnOutputPipeline) turnOutputPipeline.add(wrap(fn));
348
+ this.#inputRunner = turnInputPipeline.runner();
349
+ this.#outputRunner = turnOutputPipeline.runner();
350
+ this.#functionalEmitter = new TypedEventEmitter();
351
+ this.#observabilityEmitter = new TypedEventEmitter();
352
+ }
353
+ /**
354
+ * Removes a previously registered functional listener for `event`.
355
+ *
356
+ * @param event - The event to stop listening to.
357
+ * @param listener - The listener function to remove.
358
+ * @returns `this` for chaining.
359
+ */
360
+ off(event, listener) {
361
+ this.#functionalEmitter.off(event, listener);
362
+ return this;
363
+ }
364
+ /**
365
+ * Registers a persistent functional listener for `event`.
366
+ *
367
+ * @param event - The event to listen to.
368
+ * @param listener - The function to call on each emission.
369
+ * @returns `this` for chaining.
370
+ */
371
+ on(event, listener) {
372
+ this.#functionalEmitter.on(event, listener);
373
+ return this;
374
+ }
375
+ /**
376
+ * Registers a one-time functional listener for `event` that is automatically removed after the
377
+ * first emission.
378
+ *
379
+ * @param event - The event to listen to.
380
+ * @param listener - The function to call on the next emission.
381
+ * @returns `this` for chaining.
382
+ */
383
+ once(event, listener) {
384
+ this.#functionalEmitter.once(event, listener);
385
+ return this;
386
+ }
387
+ /**
388
+ * Removes a previously registered observability listener for `event`.
389
+ *
390
+ * @param event - The event to stop observing.
391
+ * @param listener - The listener function to remove.
392
+ * @returns `this` for chaining.
393
+ */
394
+ unobserve(event, listener) {
395
+ this.#observabilityEmitter.off(event, listener);
396
+ return this;
397
+ }
398
+ /**
399
+ * Registers a persistent observability listener for `event`.
400
+ *
401
+ * @remarks
402
+ * Use the observability bus (`observe` / `unobserve` / `observeOnce`) for instrumentation:
403
+ * turn lifecycle, gate lifecycle, and non-fatal errors. Use the functional bus (`on` / `off` /
404
+ * `once`) for pipeline-affecting events: `message`, `thought`, `toolCall`.
405
+ *
406
+ * @param event - The event to observe.
407
+ * @param listener - The function to call on each emission.
408
+ * @returns `this` for chaining.
409
+ */
410
+ observe(event, listener) {
411
+ this.#observabilityEmitter.on(event, listener);
412
+ return this;
413
+ }
414
+ /**
415
+ * Registers a one-time observability listener for `event` that is automatically removed after
416
+ * the first emission.
417
+ *
418
+ * @param event - The event to observe once.
419
+ * @param listener - The function to call on the next emission.
420
+ * @returns `this` for chaining.
421
+ */
422
+ observeOnce(event, listener) {
423
+ this.#observabilityEmitter.once(event, listener);
424
+ return this;
425
+ }
426
+ /**
427
+ * Executes a single agent turn against the provided raw context.
428
+ *
429
+ * @remarks
430
+ * Returns `Promise<void>` intentionally — all meaningful output surfaces via events, not return
431
+ * values. Register listeners before calling `run`: observability events (`turnStart`, `turnEnd`)
432
+ * bracket execution; functional events (`message`, `thought`, `toolCall`) fire throughout;
433
+ * observability `error` carries non-fatal pipeline failures; `turnGateOpen` and `turnGateClosed`
434
+ * fire when middleware suspends via `ctx.waitFor()`. Awaiting this method only tells you the
435
+ * pipeline has finished, not what it produced.
436
+ *
437
+ * Constructs a validated {@link @nhtio/adk!TurnContext} from `context` (throwing
438
+ * {@link @nhtio/adk!E_INVALID_TURN_CONTEXT} on failure), then runs the input middleware pipeline.
439
+ * Abort signals are silently swallowed.
440
+ *
441
+ * @param context - Raw input validated and wrapped into a {@link @nhtio/adk!TurnContext} before execution.
442
+ * @throws {@link @nhtio/adk!E_INVALID_TURN_CONTEXT} when `context` does not satisfy the schema.
443
+ */
444
+ async run(context) {
445
+ const abortController = context.turnAbortController ?? new AbortController();
446
+ let turnContextId;
447
+ const openGate = (raw) => {
448
+ const gate = new TurnGate({
449
+ ...raw,
450
+ turnId: turnContextId,
451
+ abortSignal: abortController.signal
452
+ });
453
+ this.#observabilityEmitter.emit("turnGateOpen", gate);
454
+ const promise = gate._promise();
455
+ promise.then(() => {
456
+ this.#observabilityEmitter.emit("turnGateClosed", {
457
+ gateId: gate.id,
458
+ turnId: gate.turnId,
459
+ result: "resolved",
460
+ settledAt: DateTime.now()
461
+ });
462
+ }, (err) => {
463
+ let result = "rejected";
464
+ if (isInstanceOf(err, "E_TURN_GATE_ABORTED")) result = "aborted";
465
+ else if (isInstanceOf(err, "E_TURN_GATE_TIMEOUT")) result = "timeout";
466
+ this.#observabilityEmitter.emit("turnGateClosed", {
467
+ gateId: gate.id,
468
+ turnId: gate.turnId,
469
+ result,
470
+ settledAt: DateTime.now()
471
+ });
472
+ });
473
+ return promise;
474
+ };
475
+ const tools = new ToolRegistry(this.#config.tools);
476
+ const turnContext = new TurnContext({
477
+ ...context,
478
+ turnAbortController: abortController
479
+ }, {
480
+ fetchMemories: this.#config.fetchMemoriesCallback,
481
+ fetchMessages: this.#config.fetchMessagesCallback,
482
+ fetchThoughts: this.#config.fetchThoughtsCallback,
483
+ fetchToolCalls: this.#config.fetchToolCallsCallback,
484
+ fetchTools: this.#config.fetchToolsCallback,
485
+ refreshStandingInstructions: this.#config.refreshStandingInstructionsCallback,
486
+ storeStandingInstruction: this.#config.storeStandingInstructionCallback,
487
+ mutateStandingInstruction: this.#config.mutateStandingInstructionCallback,
488
+ deleteStandingInstruction: this.#config.deleteStandingInstructionCallback,
489
+ storeMemory: this.#config.storeMemoryCallback,
490
+ mutateMemory: this.#config.mutateMemoryCallback,
491
+ deleteMemory: this.#config.deleteMemoryCallback,
492
+ fetchRetrievables: this.#config.fetchRetrievablesCallback,
493
+ storeRetrievable: this.#config.storeRetrievableCallback,
494
+ mutateRetrievable: this.#config.mutateRetrievableCallback,
495
+ deleteRetrievable: this.#config.deleteRetrievableCallback,
496
+ storeMessage: this.#config.storeMessageCallback,
497
+ mutateMessage: this.#config.mutateMessageCallback,
498
+ deleteMessage: this.#config.deleteMessageCallback,
499
+ storeThought: this.#config.storeThoughtCallback,
500
+ mutateThought: this.#config.mutateThoughtCallback,
501
+ deleteThought: this.#config.deleteThoughtCallback,
502
+ storeToolCall: this.#config.storeToolCallCallback,
503
+ mutateToolCall: this.#config.mutateToolCallCallback,
504
+ deleteToolCall: this.#config.deleteToolCallCallback,
505
+ emitMessage: (content) => this.#functionalEmitter.emit("message", content),
506
+ emitThought: (content) => this.#functionalEmitter.emit("thought", content),
507
+ emitToolCall: (content) => this.#functionalEmitter.emit("toolCall", content),
508
+ emitToolExecutionStart: (event) => this.#observabilityEmitter.emit("toolExecutionStart", event),
509
+ emitToolExecutionEnd: (event) => this.#observabilityEmitter.emit("toolExecutionEnd", event),
510
+ openGate,
511
+ tools
512
+ });
513
+ turnContextId = turnContext.id;
514
+ const startedAt = DateTime.now();
515
+ this.#observabilityEmitter.emit("turnStart", {
516
+ turnId: turnContext.id,
517
+ startedAt
518
+ });
519
+ const emitTurnEnd = () => {
520
+ const endedAt = DateTime.now();
521
+ this.#observabilityEmitter.emit("turnEnd", {
522
+ turnId: turnContext.id,
523
+ startedAt,
524
+ endedAt,
525
+ durationMs: endedAt.diff(startedAt).milliseconds
526
+ });
527
+ };
528
+ let inputFailed = false;
529
+ let inputReached = false;
530
+ await this.#inputRunner.errorHandler(async (error) => {
531
+ if (!isError(error) || !isInstanceOf(error, "AbortError")) {
532
+ inputFailed = true;
533
+ const err = new E_INPUT_PIPELINE_ERROR({ cause: isError(error) ? error : void 0 });
534
+ this.#observabilityEmitter.emit("error", err);
535
+ }
536
+ }).finalHandler(async () => {
537
+ inputReached = true;
538
+ }).run((fn, next) => Promise.resolve(fn(turnContext, next)));
539
+ if (!inputReached && !inputFailed && !turnContext.aborted) {
540
+ inputFailed = true;
541
+ const err = new E_PIPELINE_SHORT_CIRCUITED(["turn-input"]);
542
+ this.#observabilityEmitter.emit("error", err);
543
+ }
544
+ if (inputFailed || turnContext.aborted) {
545
+ emitTurnEnd();
546
+ return;
547
+ }
548
+ let dispatchFailed = false;
549
+ try {
550
+ await DispatchRunner.dispatch({
551
+ source: turnContext,
552
+ executor: this.#config.executorCallback,
553
+ turnInputPipeline: this.#config.dispatchInputPipeline,
554
+ turnOutputPipeline: this.#config.dispatchOutputPipeline,
555
+ observers: {
556
+ dispatchStart: [(e) => {
557
+ this.#observabilityEmitter.emit("dispatchStart", e);
558
+ }],
559
+ dispatchEnd: [(e) => {
560
+ this.#observabilityEmitter.emit("dispatchEnd", e);
561
+ }],
562
+ iterationStart: [(e) => {
563
+ this.#observabilityEmitter.emit("iterationStart", e);
564
+ }],
565
+ iterationEnd: [(e) => {
566
+ this.#observabilityEmitter.emit("iterationEnd", e);
567
+ }],
568
+ log: [(e) => {
569
+ this.#observabilityEmitter.emit("log", e);
570
+ }]
571
+ }
572
+ });
573
+ } catch (err) {
574
+ dispatchFailed = true;
575
+ const wrapped = isInstanceOf(err, "BaseException") ? err : err;
576
+ this.#observabilityEmitter.emit("error", wrapped);
577
+ }
578
+ if (dispatchFailed || turnContext.aborted) {
579
+ emitTurnEnd();
580
+ return;
581
+ }
582
+ let outputFailed = false;
583
+ let outputReached = false;
584
+ await this.#outputRunner.errorHandler(async (error) => {
585
+ if (!isError(error) || !isInstanceOf(error, "AbortError")) {
586
+ outputFailed = true;
587
+ const err = new E_OUTPUT_PIPELINE_ERROR({ cause: isError(error) ? error : void 0 });
588
+ this.#observabilityEmitter.emit("error", err);
589
+ }
590
+ }).finalHandler(async () => {
591
+ outputReached = true;
592
+ }).run((fn, next) => Promise.resolve(fn(turnContext, next)));
593
+ if (!outputReached && !outputFailed && !turnContext.aborted) {
594
+ const err = new E_PIPELINE_SHORT_CIRCUITED(["turn-output"]);
595
+ this.#observabilityEmitter.emit("error", err);
596
+ }
597
+ emitTurnEnd();
598
+ }
599
+ };
600
+ //#endregion
601
+ export { TurnGate as n, TurnRunner as t };
602
+
603
+ //# sourceMappingURL=turn_runner-y7eyEcJH.mjs.map