@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,233 @@
1
+ import type { HookHandler } from '@nhtio/hooks';
2
+ import type { Memory } from "../classes/memory";
3
+ import type { Message } from "../classes/message";
4
+ import type { Thought } from "../classes/thought";
5
+ import type { ToolCall } from "../classes/tool_call";
6
+ import type { Retrievable } from "../classes/retrievable";
7
+ import type { Tokenizable } from "../classes/tokenizable";
8
+ import type { TurnStreamableContent, TurnToolCallContent, ToolExecutionStartEvent, ToolExecutionEndEvent } from "./turn_runner";
9
+ /**
10
+ * Hook event map for {@link @nhtio/adk!DispatchContext}.
11
+ *
12
+ * @remarks
13
+ * Each key maps to a tuple of `[HookArgs, CleanupArgs]`. These events mirror the functional and
14
+ * observability events on {@link @nhtio/adk!TurnRunner} but are surfaced through `@nhtio/hooks` rather than
15
+ * `TypedEventEmitter`, which allows the entire execution context to be garbage-collected once
16
+ * execution completes — no lingering listener references.
17
+ *
18
+ * Mutation hooks (`stored*` / `mutated*` / `deleted*`) fire on every mutation in both standalone
19
+ * and derived paths. The `DispatchRunner` subscribes to these hooks to queue deltas for
20
+ * later bubbling to a parent `TurnContext`; users may subscribe for observability or testing.
21
+ */
22
+ export type DispatchContextHooks = {
23
+ /** Fired when a streaming message chunk is emitted during execution. */
24
+ message: [
25
+ [
26
+ TurnStreamableContent
27
+ ],
28
+ [
29
+ ]
30
+ ];
31
+ /** Fired when a reasoning trace chunk is emitted during execution. */
32
+ thought: [
33
+ [
34
+ TurnStreamableContent
35
+ ],
36
+ [
37
+ ]
38
+ ];
39
+ /** Fired when a tool call is emitted (on request and again on settlement). */
40
+ toolCall: [
41
+ [
42
+ TurnToolCallContent
43
+ ],
44
+ [
45
+ ]
46
+ ];
47
+ /** Fired immediately before a tool handler is called (after arg validation). */
48
+ toolExecutionStart: [
49
+ [
50
+ ToolExecutionStartEvent
51
+ ],
52
+ [
53
+ ]
54
+ ];
55
+ /** Fired after a tool handler returns or throws. */
56
+ toolExecutionEnd: [
57
+ [
58
+ ToolExecutionEndEvent
59
+ ],
60
+ [
61
+ ]
62
+ ];
63
+ /** Fired after a standing instruction is stored locally and persisted. */
64
+ storedStandingInstruction: [
65
+ [
66
+ Tokenizable
67
+ ],
68
+ [
69
+ ]
70
+ ];
71
+ /** Fired after a standing instruction is mutated locally and persisted. */
72
+ mutatedStandingInstruction: [
73
+ [
74
+ Tokenizable
75
+ ],
76
+ [
77
+ ]
78
+ ];
79
+ /** Fired after a standing instruction is removed locally and from persistence. */
80
+ deletedStandingInstruction: [
81
+ [
82
+ Tokenizable
83
+ ],
84
+ [
85
+ ]
86
+ ];
87
+ /** Fired after a memory is stored locally and persisted. */
88
+ storedMemory: [
89
+ [
90
+ Memory
91
+ ],
92
+ [
93
+ ]
94
+ ];
95
+ /** Fired after a memory is mutated locally and persisted. */
96
+ mutatedMemory: [
97
+ [
98
+ Memory
99
+ ],
100
+ [
101
+ ]
102
+ ];
103
+ /** Fired after a memory is removed locally and from persistence. Payload is the deleted id. */
104
+ deletedMemory: [
105
+ [
106
+ string
107
+ ],
108
+ [
109
+ ]
110
+ ];
111
+ /** Fired after a retrievable record is stored locally and persisted. */
112
+ storedRetrievable: [
113
+ [
114
+ Retrievable
115
+ ],
116
+ [
117
+ ]
118
+ ];
119
+ /** Fired after a retrievable record is mutated locally and persisted. */
120
+ mutatedRetrievable: [
121
+ [
122
+ Retrievable
123
+ ],
124
+ [
125
+ ]
126
+ ];
127
+ /** Fired after a retrievable record is removed locally and from persistence. Payload is the deleted id. */
128
+ deletedRetrievable: [
129
+ [
130
+ string
131
+ ],
132
+ [
133
+ ]
134
+ ];
135
+ /** Fired after a message is stored locally and persisted. */
136
+ storedMessage: [
137
+ [
138
+ Message
139
+ ],
140
+ [
141
+ ]
142
+ ];
143
+ /** Fired after a message is mutated locally and persisted. */
144
+ mutatedMessage: [
145
+ [
146
+ Message
147
+ ],
148
+ [
149
+ ]
150
+ ];
151
+ /** Fired after a message is removed locally and from persistence. Payload is the deleted id. */
152
+ deletedMessage: [
153
+ [
154
+ string
155
+ ],
156
+ [
157
+ ]
158
+ ];
159
+ /** Fired after a thought is stored locally and persisted. */
160
+ storedThought: [
161
+ [
162
+ Thought
163
+ ],
164
+ [
165
+ ]
166
+ ];
167
+ /** Fired after a thought is mutated locally and persisted. */
168
+ mutatedThought: [
169
+ [
170
+ Thought
171
+ ],
172
+ [
173
+ ]
174
+ ];
175
+ /** Fired after a thought is removed locally and from persistence. Payload is the deleted id. */
176
+ deletedThought: [
177
+ [
178
+ string
179
+ ],
180
+ [
181
+ ]
182
+ ];
183
+ /**
184
+ * Fired after a tool call is stored locally and persisted.
185
+ *
186
+ * @remarks
187
+ * `ToolCall`s with `fromArtifactTool === true` originated from an
188
+ * {@link @nhtio/adk!ArtifactTool} invocation and carry a
189
+ * {@link @nhtio/adk!Tokenizable} in `results` (NOT a
190
+ * {@link @nhtio/adk!SpooledArtifact}). Subsequent
191
+ * `SpooledArtifact.forgeTools(ctx)` calls filter these out of the `callId` enum to prevent
192
+ * the artifact-grep-on-an-artifact-grep recursion.
193
+ */
194
+ storedToolCall: [
195
+ [
196
+ ToolCall
197
+ ],
198
+ [
199
+ ]
200
+ ];
201
+ /**
202
+ * Fired after a tool call is mutated locally and persisted.
203
+ *
204
+ * @remarks
205
+ * See {@link DispatchContextHooks.storedToolCall} for the `fromArtifactTool` /
206
+ * `Tokenizable`-shaped `results` invariant.
207
+ */
208
+ mutatedToolCall: [
209
+ [
210
+ ToolCall
211
+ ],
212
+ [
213
+ ]
214
+ ];
215
+ /** Fired after a tool call is removed locally and from persistence. Payload is the deleted id. */
216
+ deletedToolCall: [
217
+ [
218
+ string
219
+ ],
220
+ [
221
+ ]
222
+ ];
223
+ };
224
+ /**
225
+ * Optional hook registrations supplied to {@link @nhtio/adk!DispatchContext} at construction time.
226
+ *
227
+ * @remarks
228
+ * Each key may be a single handler or an array of handlers. Handlers are registered in order and
229
+ * fired synchronously (fire-and-forget) on each corresponding emit call.
230
+ */
231
+ export type DispatchContextHookRegistrations = {
232
+ [E in keyof DispatchContextHooks]?: HookHandler<DispatchContextHooks[E][0], DispatchContextHooks[E][1]> | HookHandler<DispatchContextHooks[E][0], DispatchContextHooks[E][1]>[];
233
+ };
@@ -0,0 +1,387 @@
1
+ import type { DateTime } from 'luxon';
2
+ import type { NextFn } from '@nhtio/middleware';
3
+ import type { HookHandler } from '@nhtio/hooks';
4
+ import type { BaseException } from "../classes/base_exception";
5
+ import type { DispatchContext } from "../contracts/dispatch_context";
6
+ import type { TurnStreamableContent, TurnToolCallContent, ToolExecutionStartEvent, ToolExecutionEndEvent } from "./turn_runner";
7
+ /**
8
+ * Middleware function signature for the input and output pipelines in {@link @nhtio/adk!DispatchRunner}.
9
+ *
10
+ * @remarks
11
+ * Receives the active {@link @nhtio/adk!DispatchContext} and a `next` callback to advance the pipeline.
12
+ * Middleware can inspect `ctx.isSignalled` to bail early when an earlier middleware or the
13
+ * executor has called `ctx.ack()` / `ctx.nack()`, and `ctx.aborted` for external cancellation.
14
+ * Use `ctx.iteration` and `ctx.toolCallCount(checksum)` to implement iteration bounds and
15
+ * checksum-repeat bounds in your own middleware — the runner itself does not impose either.
16
+ */
17
+ export type DispatchPipelineMiddlewareFn = (ctx: DispatchContext, next: NextFn) => void | Promise<void>;
18
+ /**
19
+ * Per-dispatch helpers passed to {@link DispatchExecutorFn} alongside the active
20
+ * {@link @nhtio/adk!DispatchContext}.
21
+ *
22
+ * @remarks
23
+ * `DispatchRunner` constructs a fresh helpers instance for every dispatch and threads it
24
+ * through every iteration of that dispatch. Per-id stream state is scoped to the dispatch —
25
+ * helpers are garbage-collected with the runner, so cross-dispatch state cannot leak.
26
+ *
27
+ * Helpers are **emit-only**: they call `ctx.emitMessage` / `ctx.emitThought` /
28
+ * `ctx.emitToolCall` to surface streaming content. They do not persist `Message` / `Thought` /
29
+ * `ToolCall` records, because building those records requires implementation-specific fields
30
+ * (`role`, `identity`, `SpooledArtifact` for results) that the wire payload doesn't carry. The
31
+ * executor calls `ctx.storeMessage(...)` / `ctx.storeThought(...)` / `ctx.storeToolCall(...)`
32
+ * itself when it has the full record assembled.
33
+ *
34
+ * The value the helpers add is per-id accumulation state. Without them, every executor
35
+ * reimplements a per-id `Map<id, { full, createdAt, ... }>` to track streaming chunks across
36
+ * SDK callbacks.
37
+ */
38
+ export interface DispatchExecutorHelpers {
39
+ /**
40
+ * Append a delta to the message stream for `id` and emit a {@link @nhtio/adk!TurnStreamableContent}.
41
+ *
42
+ * @remarks
43
+ * On the first call for `id`, creates the stream with `createdAt` / `updatedAt` set to now
44
+ * and `full` equal to `deltaText`. On subsequent calls, appends `deltaText` to the
45
+ * accumulated `full` and updates `updatedAt`. When `opts.isComplete` is true, sets
46
+ * `completedAt` and seals the stream — subsequent calls for the same `id` will throw.
47
+ *
48
+ * @param id - Stable identifier for this message stream.
49
+ * @param deltaText - The new chunk to append.
50
+ * @param opts.isComplete - When true, this is the final chunk for `id`.
51
+ */
52
+ reportMessage(id: string, deltaText: string, opts?: {
53
+ isComplete?: boolean;
54
+ }): void;
55
+ /**
56
+ * Append a delta to the thought stream for `id` and emit a {@link @nhtio/adk!TurnStreamableContent}.
57
+ *
58
+ * @remarks
59
+ * Same accumulation semantics as {@link DispatchExecutorHelpers.reportMessage} but emits via
60
+ * `ctx.emitThought`. Used for reasoning trace chunks that the executor wants to surface but
61
+ * not show to the end user.
62
+ *
63
+ * @param id - Stable identifier for this thought stream.
64
+ * @param deltaText - The new chunk to append.
65
+ * @param opts.isComplete - When true, this is the final chunk for `id`.
66
+ */
67
+ reportThought(id: string, deltaText: string, opts?: {
68
+ isComplete?: boolean;
69
+ }): void;
70
+ /**
71
+ * Update tool call state for `id` and emit a {@link @nhtio/adk!TurnToolCallContent}.
72
+ *
73
+ * @remarks
74
+ * Accepts a partial of the tool call fields; the helper merges into the per-id state and
75
+ * emits the merged view. Typical usage is two calls: one with `{ tool, args }` to announce
76
+ * the requested call (helper auto-computes `checksum` via SHA-256 of
77
+ * `JSON.stringify({tool, args})`), and one with `{ results, isComplete: true, isError? }`
78
+ * after the tool runs.
79
+ *
80
+ * Calls after `isComplete: true` will throw — the per-id state is sealed.
81
+ *
82
+ * @param id - Stable identifier for this tool call (correlates request with result).
83
+ * @param partial - Fields to merge into the tool call state.
84
+ */
85
+ reportToolCall(id: string, partial: {
86
+ tool?: string;
87
+ args?: unknown;
88
+ /**
89
+ * Shape depends on the tool kind backing this call.
90
+ *
91
+ * @remarks
92
+ * For a normal {@link @nhtio/adk!Tool} call, this is typically a
93
+ * {@link @nhtio/adk!SpooledArtifact} (single or array — a
94
+ * tool may legitimately spool multiple bounded artifacts in a single call) wrapping
95
+ * the bytes returned by the handler, or one or more {@link @nhtio/adk!Media}
96
+ * instances when the handler chose the explicit-modality return path. For an
97
+ * {@link @nhtio/adk!ArtifactTool} call, this should be the raw
98
+ * string the handler emitted (`Tokenizable.toString()`-equivalent — already the
99
+ * model-visible answer). Type stays `unknown` to keep the wire-side payload narrow.
100
+ */
101
+ results?: unknown;
102
+ isError?: boolean;
103
+ isComplete?: boolean;
104
+ }): void;
105
+ /**
106
+ * Emit a structured log event for the current dispatch.
107
+ *
108
+ * @remarks
109
+ * `trace` / `debug` / `info` / `warn` / `error` mirror the standard syslog severity levels.
110
+ * Each call routes through the runner's observability bus as a `log` event so middleware,
111
+ * tests, and consumer observability stacks can subscribe without monkey-patching the
112
+ * executor. The runner enriches every emission with the active `dispatchId` and 0-based
113
+ * `iteration` index — call sites only need to supply a `kind` discriminator, a human-readable
114
+ * message, and an optional structured `payload`.
115
+ *
116
+ * The `log` channel is the canonical egress for executor-side diagnostics — retry decisions,
117
+ * idle / request-timeout fires, HTTP error bodies, SSE chunk anomalies, provider-quirk
118
+ * warnings, context-window perBucket breakdowns. Use it instead of `console.*`.
119
+ */
120
+ log: DispatchExecutorLogChannel;
121
+ }
122
+ /**
123
+ * Severity of a structured log event emitted by an executor via {@link DispatchExecutorHelpers.log}.
124
+ *
125
+ * @remarks
126
+ * Mirrors the lowercase syslog-style level vocabulary every JS logger converges on
127
+ * (`pino`, `winston`, `bunyan`, `loglevel`). Consumers filter on the `level` field of the
128
+ * delivered event — there is no per-level configuration on the channel itself.
129
+ */
130
+ export type DispatchExecutorLogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error';
131
+ /**
132
+ * Per-call structured payload for {@link DispatchExecutorLogChannel}.
133
+ *
134
+ * @remarks
135
+ * `kind` is a short, stable discriminator the executor authored — observability middleware
136
+ * matches on it to filter / group / aggregate events without parsing the human-readable
137
+ * `message`. `payload` is the structured detail block; it is delivered to subscribers verbatim
138
+ * (the runner never inspects it).
139
+ */
140
+ export interface DispatchExecutorLogEntry {
141
+ /** Stable discriminator authored by the executor (e.g. `'retry-attempt'`, `'http-error'`). */
142
+ kind: string;
143
+ /** Human-readable message — safe to surface in logs or test failure output. */
144
+ message: string;
145
+ /** Optional structured detail block. Delivered to subscribers verbatim. */
146
+ payload?: Record<string, unknown>;
147
+ }
148
+ /**
149
+ * Five-level log channel exposed on {@link DispatchExecutorHelpers}.
150
+ *
151
+ * @remarks
152
+ * Each method emits a `log` observability event with the corresponding `level`. Implementations
153
+ * are non-blocking and side-effect-only — they never throw, never modify the
154
+ * {@link @nhtio/adk!DispatchContext}, and never participate in ack / nack flow.
155
+ */
156
+ export interface DispatchExecutorLogChannel {
157
+ /** Emit a `trace`-level structured log event. */
158
+ trace(entry: DispatchExecutorLogEntry): void;
159
+ /** Emit a `debug`-level structured log event. */
160
+ debug(entry: DispatchExecutorLogEntry): void;
161
+ /** Emit an `info`-level structured log event. */
162
+ info(entry: DispatchExecutorLogEntry): void;
163
+ /** Emit a `warn`-level structured log event. */
164
+ warn(entry: DispatchExecutorLogEntry): void;
165
+ /** Emit an `error`-level structured log event. */
166
+ error(entry: DispatchExecutorLogEntry): void;
167
+ }
168
+ /**
169
+ * Payload fired on the observability `log` hook for every structured event emitted via
170
+ * {@link DispatchExecutorHelpers.log}.
171
+ *
172
+ * @remarks
173
+ * Enriched by the runner with the active `dispatchId` and 0-based `iteration` index so
174
+ * subscribers can correlate events across multiple in-flight dispatches without threading
175
+ * extra context themselves.
176
+ */
177
+ export interface LogEvent {
178
+ /** Stable identifier for the dispatch that produced the event. */
179
+ dispatchId: string;
180
+ /** 0-based iteration index within the dispatch. */
181
+ iteration: number;
182
+ /** When the event was emitted. */
183
+ emittedAt: DateTime;
184
+ /** Severity level the executor selected. */
185
+ level: DispatchExecutorLogLevel;
186
+ /** Stable discriminator authored by the executor. */
187
+ kind: string;
188
+ /** Human-readable message. */
189
+ message: string;
190
+ /** Optional structured detail block. */
191
+ payload?: Record<string, unknown>;
192
+ }
193
+ /**
194
+ * The user-supplied callback that performs the actual LLM API call within a dispatch.
195
+ *
196
+ * @remarks
197
+ * Invoked between the input and output middleware pipelines on every iteration. Receives the
198
+ * active {@link @nhtio/adk!DispatchContext} and an {@link DispatchExecutorHelpers} object that manages
199
+ * per-id streaming state for the dispatch. The executor's responsibilities:
200
+ *
201
+ * 1. Make the actual LLM API / SDK call (the ADK has no opinion on which provider).
202
+ * 2. Normalise streaming responses into `TurnStreamableContent` / `TurnToolCallContent` shapes
203
+ * and report them via the helpers.
204
+ * 3. Persist the resulting `Message` / `Thought` / `ToolCall` records via `ctx.storeMessage` /
205
+ * `ctx.storeThought` / `ctx.storeToolCall` once the implementation-specific fields are
206
+ * known.
207
+ * 4. Decide when the loop is done — typically `ctx.ack()` after a response with no further
208
+ * tool calls, or `ctx.nack(err)` on failure. The runner will loop again if neither signal
209
+ * nor abort fires.
210
+ *
211
+ * Wired into a `TurnRunner` via `TurnRunnerConfig.executorCallback`. Invoked once per
212
+ * iteration inside `DispatchRunner.dispatch()`, between the input and output middleware
213
+ * pipelines.
214
+ */
215
+ export type DispatchExecutorFn = (ctx: DispatchContext, helpers: DispatchExecutorHelpers) => void | Promise<void>;
216
+ /**
217
+ * Payload fired when a dispatch begins.
218
+ */
219
+ export interface DispatchStartEvent {
220
+ /** Stable identifier for this dispatch (UUIDv6). */
221
+ dispatchId: string;
222
+ /** When the dispatch began. */
223
+ startedAt: DateTime;
224
+ }
225
+ /**
226
+ * Payload fired when a dispatch ends — successfully, by error, or by abort.
227
+ */
228
+ export interface DispatchEndEvent {
229
+ /** Stable identifier for this dispatch. */
230
+ dispatchId: string;
231
+ /** How the dispatch settled. */
232
+ status: 'ack' | 'nack' | 'aborted';
233
+ /** The error stored by `ctx.nack(error)`, or `undefined` for `ack` / `aborted`. */
234
+ error?: Error;
235
+ /** Total iterations that ran during this dispatch. */
236
+ iterations: number;
237
+ /** When the dispatch began. */
238
+ startedAt: DateTime;
239
+ /** When the dispatch ended. */
240
+ endedAt: DateTime;
241
+ /** Duration in milliseconds. */
242
+ durationMs: number;
243
+ }
244
+ /**
245
+ * Payload fired at the start of each iteration within a dispatch.
246
+ */
247
+ export interface IterationStartEvent {
248
+ /** Stable identifier for the parent dispatch. */
249
+ dispatchId: string;
250
+ /** 0-based iteration index within the dispatch. */
251
+ iteration: number;
252
+ /** When this iteration began. */
253
+ startedAt: DateTime;
254
+ }
255
+ /**
256
+ * Payload fired at the end of each iteration within a dispatch.
257
+ */
258
+ export interface IterationEndEvent {
259
+ /** Stable identifier for the parent dispatch. */
260
+ dispatchId: string;
261
+ /** 0-based iteration index within the dispatch. */
262
+ iteration: number;
263
+ /** When this iteration began. */
264
+ startedAt: DateTime;
265
+ /** When this iteration ended. */
266
+ endedAt: DateTime;
267
+ /** Duration in milliseconds. */
268
+ durationMs: number;
269
+ }
270
+ /**
271
+ * Functional hook events on {@link @nhtio/adk!DispatchRunner}.
272
+ *
273
+ * @remarks
274
+ * Pipeline-affecting events forwarded from the {@link @nhtio/adk!DispatchContext}. Register handlers
275
+ * via the `hooks` field of the dispatch input.
276
+ */
277
+ export type DispatchRunnerFunctionalHooks = {
278
+ /** Fired for every streaming message chunk. Forwarded from the context. */
279
+ message: [
280
+ [
281
+ TurnStreamableContent
282
+ ],
283
+ [
284
+ ]
285
+ ];
286
+ /** Fired for every reasoning trace chunk. Forwarded from the context. */
287
+ thought: [
288
+ [
289
+ TurnStreamableContent
290
+ ],
291
+ [
292
+ ]
293
+ ];
294
+ /** Fired for every tool call (on request and on settlement). Forwarded from the context. */
295
+ toolCall: [
296
+ [
297
+ TurnToolCallContent
298
+ ],
299
+ [
300
+ ]
301
+ ];
302
+ };
303
+ /**
304
+ * Observability hook events on {@link @nhtio/adk!DispatchRunner}.
305
+ *
306
+ * @remarks
307
+ * Instrumentation-only events. Register handlers via the `observers` field of the dispatch
308
+ * input. Removing an observer does not affect dispatch correctness.
309
+ */
310
+ export type DispatchRunnerObservabilityHooks = {
311
+ /** Forwarded from the context immediately before a tool handler is called. */
312
+ toolExecutionStart: [
313
+ [
314
+ ToolExecutionStartEvent
315
+ ],
316
+ [
317
+ ]
318
+ ];
319
+ /** Forwarded from the context after a tool handler returns or throws. */
320
+ toolExecutionEnd: [
321
+ [
322
+ ToolExecutionEndEvent
323
+ ],
324
+ [
325
+ ]
326
+ ];
327
+ /** Fired at the start of each iteration. */
328
+ iterationStart: [
329
+ [
330
+ IterationStartEvent
331
+ ],
332
+ [
333
+ ]
334
+ ];
335
+ /** Fired at the end of each iteration. */
336
+ iterationEnd: [
337
+ [
338
+ IterationEndEvent
339
+ ],
340
+ [
341
+ ]
342
+ ];
343
+ /** Fired once when the dispatch begins. */
344
+ dispatchStart: [
345
+ [
346
+ DispatchStartEvent
347
+ ],
348
+ [
349
+ ]
350
+ ];
351
+ /** Fired once when the dispatch ends (ack / nack / aborted). */
352
+ dispatchEnd: [
353
+ [
354
+ DispatchEndEvent
355
+ ],
356
+ [
357
+ ]
358
+ ];
359
+ /** Fired when a non-fatal pipeline or executor error occurs. The exception is also re-thrown. */
360
+ error: [
361
+ [
362
+ BaseException
363
+ ],
364
+ [
365
+ ]
366
+ ];
367
+ /** Fired for every structured log event emitted by the executor via {@link DispatchExecutorHelpers.log}. */
368
+ log: [
369
+ [
370
+ LogEvent
371
+ ],
372
+ [
373
+ ]
374
+ ];
375
+ };
376
+ /**
377
+ * Optional functional hook registrations supplied to {@link @nhtio/adk!DispatchRunner.dispatch}.
378
+ */
379
+ export type DispatchRunnerFunctionalHookRegistrations = {
380
+ [E in keyof DispatchRunnerFunctionalHooks]?: HookHandler<DispatchRunnerFunctionalHooks[E][0], DispatchRunnerFunctionalHooks[E][1]> | HookHandler<DispatchRunnerFunctionalHooks[E][0], DispatchRunnerFunctionalHooks[E][1]>[];
381
+ };
382
+ /**
383
+ * Optional observability hook registrations supplied to {@link @nhtio/adk!DispatchRunner.dispatch}.
384
+ */
385
+ export type DispatchRunnerObservabilityHookRegistrations = {
386
+ [E in keyof DispatchRunnerObservabilityHooks]?: HookHandler<DispatchRunnerObservabilityHooks[E][0], DispatchRunnerObservabilityHooks[E][1]> | HookHandler<DispatchRunnerObservabilityHooks[E][0], DispatchRunnerObservabilityHooks[E][1]>[];
387
+ };