@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,267 @@
1
+ import { Registry } from "../classes/registry";
2
+ import { Tokenizable } from "../classes/tokenizable";
3
+ import type { Tool } from "../classes/tool";
4
+ import type { Memory } from "../classes/memory";
5
+ import type { Message } from "../classes/message";
6
+ import type { Thought } from "../classes/thought";
7
+ import type { ToolCall } from "../classes/tool_call";
8
+ import type { Retrievable } from "../classes/retrievable";
9
+ import type { ToolRegistry } from "../classes/tool_registry";
10
+ import type { EmitMessageFn, EmitThoughtFn, EmitToolCallFn, EmitToolExecutionEndFn, EmitToolExecutionStartFn, OpenGateFn } from "../types/turn_runner";
11
+ /**
12
+ * Plain input object supplied to {@link TurnContext} at construction time.
13
+ *
14
+ * @remarks
15
+ * Validated against `turnContextSchema` before the `TurnContext` instance is created.
16
+ * Fields will grow as the turn execution model takes shape (e.g. input message, tool
17
+ * definitions, model client config).
18
+ */
19
+ export interface RawTurnContext {
20
+ /** `AbortController` whose signal can be used to cancel the turn mid-flight. */
21
+ turnAbortController: AbortController;
22
+ /** A registry for arbitrary additional data to be added to the context as needed; initially empty. */
23
+ stash?: Record<string, unknown>;
24
+ /** The system prompt guiding the agent's behavior for this turn. */
25
+ systemPrompt: string | Tokenizable;
26
+ /** Standing instructions for the agent, applied to every turn. */
27
+ standingInstructions: (string | Tokenizable)[];
28
+ }
29
+ /**
30
+ * A fully-resolved {@link RawTurnContext} where all optional fields have been filled in by the
31
+ * schema (e.g. `stash` defaulted to `{}`).
32
+ *
33
+ * @remarks
34
+ * This is the shape returned by `turnContextSchema` after validation — use it wherever a
35
+ * guaranteed-present context is needed rather than the raw caller-supplied input.
36
+ */
37
+ export type ResolvedTurnContext = Required<RawTurnContext>;
38
+ /**
39
+ * Validator schema used to validate a {@link RawTurnContext} before constructing a {@link TurnContext}.
40
+ *
41
+ * @remarks
42
+ * Validates all four fields of {@link RawTurnContext}:
43
+ * - `turnAbortController` — required `AbortController` instance.
44
+ * - `stash` — optional string-keyed object; defaults to `{}`.
45
+ * - `systemPrompt` — required string or {@link @nhtio/adk!Tokenizable}, via {@link @nhtio/adk!Tokenizable.schema}.
46
+ * - `standingInstructions` — optional array of strings or {@link @nhtio/adk!Tokenizable} instances, each
47
+ * validated via {@link @nhtio/adk!Tokenizable.schema}; defaults to `[]`.
48
+ *
49
+ * Throws a `ValidationException` (via {@link validateOrThrow}) when validation fails.
50
+ */
51
+ export declare const turnContextSchema: import("@nhtio/validation").ObjectSchema<RawTurnContext>;
52
+ /**
53
+ * A function that retrieves the memories relevant to the current turn.
54
+ *
55
+ * @remarks
56
+ * Receives the active {@link TurnContext} so implementations can filter or rank memories
57
+ * based on turn-specific state (e.g. the system prompt, standing instructions, or stash).
58
+ * May be synchronous or asynchronous.
59
+ */
60
+ export type MemoryRetrievalFn = (ctx: TurnContext) => Memory[] | Promise<Memory[]>;
61
+ /**
62
+ * A function that retrieves the retrievable (RAG) records relevant to the current turn.
63
+ *
64
+ * @remarks
65
+ * Receives the active {@link TurnContext} so implementations can rank, filter, or compose
66
+ * retrieval results against the turn-specific state (system prompt, standing instructions, etc.).
67
+ * The retrieval middleware that produces these records is responsible for assigning each one's
68
+ * `trustTier` — batteries MUST NOT auto-classify retrieved content.
69
+ * May be synchronous or asynchronous.
70
+ */
71
+ export type RetrievableRetrievalFn = (ctx: TurnContext) => Retrievable[] | Promise<Retrievable[]>;
72
+ /**
73
+ * A function that retrieves the conversation messages relevant to the current turn.
74
+ *
75
+ * @remarks
76
+ * Receives the active {@link TurnContext} so implementations can apply turn-aware filtering or
77
+ * windowing. Returns only `user` and `assistant` {@link @nhtio/adk!Message} entries — system instructions
78
+ * and tool results are not part of the persisted message history.
79
+ * May be synchronous or asynchronous.
80
+ */
81
+ export type MessageRetrievalFn = (ctx: TurnContext) => Message[] | Promise<Message[]>;
82
+ /**
83
+ * A function that retrieves the thought traces relevant to the current turn.
84
+ *
85
+ * @remarks
86
+ * Receives the active {@link TurnContext} so implementations can apply turn-aware filtering or
87
+ * attribution (e.g. filtering to a specific agent's identity in multi-agent conversations).
88
+ * May be synchronous or asynchronous.
89
+ */
90
+ export type ThoughtRetrievalFn = (ctx: TurnContext) => Thought[] | Promise<Thought[]>;
91
+ /**
92
+ * A function that retrieves the tool call records relevant to the current turn.
93
+ *
94
+ * @remarks
95
+ * Receives the active {@link TurnContext} so implementations can filter by completion state,
96
+ * agent identity, or any other turn-specific criteria.
97
+ * May be synchronous or asynchronous.
98
+ */
99
+ export type ToolCallRetrievalFn = (ctx: TurnContext) => ToolCall[] | Promise<ToolCall[]>;
100
+ /**
101
+ * A function that retrieves the tools available for the current turn.
102
+ *
103
+ * @remarks
104
+ * Receives the active {@link TurnContext} so implementations can apply turn-aware filtering
105
+ * (e.g. RBAC scopes, feature flags). May be synchronous or asynchronous.
106
+ */
107
+ export type ToolsRetrievalFn = (ctx: TurnContext) => Tool[] | Promise<Tool[]>;
108
+ /**
109
+ * A function that refreshes and returns the standing instructions for the current turn.
110
+ *
111
+ * @remarks
112
+ * Called to re-derive standing instructions mid-turn when they may have changed.
113
+ * May be synchronous or asynchronous.
114
+ */
115
+ export type StandingInstructionsRefreshFn = (ctx: TurnContext) => (string | Tokenizable)[] | Promise<(string | Tokenizable)[]>;
116
+ /** Stores a new standing instruction in the persistence layer. */
117
+ export type StandingInstructionStoreFn = (ctx: TurnContext, v: string | Tokenizable) => void | Promise<void>;
118
+ /** Updates an existing standing instruction in the persistence layer. */
119
+ export type StandingInstructionMutateFn = (ctx: TurnContext, v: string | Tokenizable) => void | Promise<void>;
120
+ /** Removes a standing instruction from the persistence layer. */
121
+ export type StandingInstructionDeleteFn = (ctx: TurnContext, v: string | Tokenizable) => void | Promise<void>;
122
+ /** Stores a new memory in the persistence layer. */
123
+ export type MemoryStoreFn = (ctx: TurnContext, v: Memory) => void | Promise<void>;
124
+ /** Updates an existing memory in the persistence layer. */
125
+ export type MemoryMutateFn = (ctx: TurnContext, v: Memory) => void | Promise<void>;
126
+ /** Removes a memory from the persistence layer by ID. */
127
+ export type MemoryDeleteFn = (ctx: TurnContext, id: string) => void | Promise<void>;
128
+ /** Stores a new retrievable record in the persistence layer. */
129
+ export type RetrievableStoreFn = (ctx: TurnContext, v: Retrievable) => void | Promise<void>;
130
+ /** Updates an existing retrievable record in the persistence layer. */
131
+ export type RetrievableMutateFn = (ctx: TurnContext, v: Retrievable) => void | Promise<void>;
132
+ /** Removes a retrievable record from the persistence layer by ID. */
133
+ export type RetrievableDeleteFn = (ctx: TurnContext, id: string) => void | Promise<void>;
134
+ /** Stores a new message in the persistence layer. */
135
+ export type MessageStoreFn = (ctx: TurnContext, v: Message) => void | Promise<void>;
136
+ /** Updates an existing message in the persistence layer. */
137
+ export type MessageMutateFn = (ctx: TurnContext, v: Message) => void | Promise<void>;
138
+ /** Removes a message from the persistence layer by ID. */
139
+ export type MessageDeleteFn = (ctx: TurnContext, id: string) => void | Promise<void>;
140
+ /** Stores a new thought in the persistence layer. */
141
+ export type ThoughtStoreFn = (ctx: TurnContext, v: Thought) => void | Promise<void>;
142
+ /** Updates an existing thought in the persistence layer. */
143
+ export type ThoughtMutateFn = (ctx: TurnContext, v: Thought) => void | Promise<void>;
144
+ /** Removes a thought from the persistence layer by ID. */
145
+ export type ThoughtDeleteFn = (ctx: TurnContext, id: string) => void | Promise<void>;
146
+ /** Stores a new tool call in the persistence layer. */
147
+ export type ToolCallStoreFn = (ctx: TurnContext, v: ToolCall) => void | Promise<void>;
148
+ /** Updates an existing tool call in the persistence layer. */
149
+ export type ToolCallMutateFn = (ctx: TurnContext, v: ToolCall) => void | Promise<void>;
150
+ /** Removes a tool call from the persistence layer by ID. */
151
+ export type ToolCallDeleteFn = (ctx: TurnContext, id: string) => void | Promise<void>;
152
+ /**
153
+ * The validated, strongly-typed context object threaded through every middleware step in a
154
+ * single agent turn.
155
+ *
156
+ * @remarks
157
+ * Constructed from a {@link RawTurnContext} by {@link @nhtio/adk!TurnRunner.run}. Middleware functions
158
+ * receive this object and use it to read and share state across pipeline steps.
159
+ */
160
+ export declare class TurnContext {
161
+ #private;
162
+ /**
163
+ * Returns `true` if `value` is a {@link TurnContext} instance.
164
+ *
165
+ * @remarks
166
+ * Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety. The ADK does not export the
167
+ * `TurnContext` class itself as a constructable value — use this guard plus the
168
+ * {@link TurnContext} type for runtime detection and TypeScript narrowing.
169
+ *
170
+ * @param value - The value to test.
171
+ * @returns `true` when `value` is a {@link TurnContext} instance.
172
+ */
173
+ static isTurnContext(value: unknown): value is TurnContext;
174
+ /** Unique identifier for this turn, generated as a UUIDv6 at construction time. */
175
+ readonly id: string;
176
+ /** `true` when the turn's `AbortController` signal has fired. */
177
+ readonly aborted: boolean;
178
+ /** The `AbortSignal` from the turn's `AbortController`. */
179
+ readonly abortSignal: AbortSignal;
180
+ /**
181
+ * Aborts the turn's `AbortController` with the supplied reason. Middleware should call this
182
+ * when refusing the turn — the runner reads `aborted` between every stage and short-circuits
183
+ * cleanly: `turnEnd` still fires, no `error` event is emitted, and during dispatch
184
+ * `dispatchEnd.status === 'aborted'` carries the operational signal.
185
+ */
186
+ readonly abort: (reason?: unknown) => void;
187
+ /** Arbitrary key-value store that middleware can read and write across pipeline steps. */
188
+ readonly stash: Registry;
189
+ /** The system prompt guiding the agent's behaviour for this turn. */
190
+ readonly systemPrompt: Tokenizable;
191
+ /** Standing instructions applied to every turn, in insertion order. */
192
+ readonly standingInstructions: Set<Tokenizable>;
193
+ /** Memories loaded for this turn; populated by middleware after calling `fetchMemories()`. */
194
+ readonly turnMemories: Set<Memory>;
195
+ /** Retrievable records loaded for this turn; populated by middleware after calling `fetchRetrievables()`. */
196
+ readonly turnRetrievables: Set<Retrievable>;
197
+ /** Conversation messages loaded for this turn; populated by middleware after calling `fetchMessages()`. */
198
+ readonly turnMessages: Set<Message>;
199
+ /** Thought traces loaded for this turn; populated by middleware after calling `fetchThoughts()`. */
200
+ readonly turnThoughts: Set<Thought>;
201
+ /** Tool call records loaded for this turn; populated by middleware after calling `fetchToolCalls()`. */
202
+ readonly turnToolCalls: Set<ToolCall>;
203
+ /** Fetches memories relevant to this turn; delegates to the callback supplied at construction. */
204
+ readonly fetchMemories: () => Memory[] | Promise<Memory[]>;
205
+ /** Fetches conversation messages relevant to this turn; delegates to the callback supplied at construction. */
206
+ readonly fetchMessages: () => Message[] | Promise<Message[]>;
207
+ /** Fetches thought traces relevant to this turn; delegates to the callback supplied at construction. */
208
+ readonly fetchThoughts: () => Thought[] | Promise<Thought[]>;
209
+ /** Fetches tool call records relevant to this turn; delegates to the callback supplied at construction. */
210
+ readonly fetchToolCalls: () => ToolCall[] | Promise<ToolCall[]>;
211
+ /** Fetches tools available for this turn; delegates to the callback supplied at construction. */
212
+ readonly fetchTools: () => Tool[] | Promise<Tool[]>;
213
+ /** Refreshes and returns the standing instructions; delegates to the callback supplied at construction. */
214
+ readonly refreshStandingInstructions: () => (string | Tokenizable)[] | Promise<(string | Tokenizable)[]>;
215
+ /** Stores a new standing instruction in the persistence layer. */
216
+ readonly storeStandingInstruction: (v: string | Tokenizable) => void | Promise<void>;
217
+ /** Updates an existing standing instruction in the persistence layer. */
218
+ readonly mutateStandingInstruction: (v: string | Tokenizable) => void | Promise<void>;
219
+ /** Removes a standing instruction from the persistence layer. */
220
+ readonly deleteStandingInstruction: (v: string | Tokenizable) => void | Promise<void>;
221
+ /** Stores a new memory in the persistence layer. */
222
+ readonly storeMemory: (v: Memory) => void | Promise<void>;
223
+ /** Updates an existing memory in the persistence layer. */
224
+ readonly mutateMemory: (v: Memory) => void | Promise<void>;
225
+ /** Removes a memory from the persistence layer by ID. */
226
+ readonly deleteMemory: (id: string) => void | Promise<void>;
227
+ /** Fetches retrievable records relevant to this turn; delegates to the callback supplied at construction. */
228
+ readonly fetchRetrievables: () => Retrievable[] | Promise<Retrievable[]>;
229
+ /** Stores a new retrievable record in the persistence layer. */
230
+ readonly storeRetrievable: (v: Retrievable) => void | Promise<void>;
231
+ /** Updates an existing retrievable record in the persistence layer. */
232
+ readonly mutateRetrievable: (v: Retrievable) => void | Promise<void>;
233
+ /** Removes a retrievable record from the persistence layer by ID. */
234
+ readonly deleteRetrievable: (id: string) => void | Promise<void>;
235
+ /** Stores a new message in the persistence layer. */
236
+ readonly storeMessage: (v: Message) => void | Promise<void>;
237
+ /** Updates an existing message in the persistence layer. */
238
+ readonly mutateMessage: (v: Message) => void | Promise<void>;
239
+ /** Removes a message from the persistence layer by ID. */
240
+ readonly deleteMessage: (id: string) => void | Promise<void>;
241
+ /** Stores a new thought in the persistence layer. */
242
+ readonly storeThought: (v: Thought) => void | Promise<void>;
243
+ /** Updates an existing thought in the persistence layer. */
244
+ readonly mutateThought: (v: Thought) => void | Promise<void>;
245
+ /** Removes a thought from the persistence layer by ID. */
246
+ readonly deleteThought: (id: string) => void | Promise<void>;
247
+ /** Stores a new tool call in the persistence layer. */
248
+ readonly storeToolCall: (v: ToolCall) => void | Promise<void>;
249
+ /** Updates an existing tool call in the persistence layer. */
250
+ readonly mutateToolCall: (v: ToolCall) => void | Promise<void>;
251
+ /** Removes a tool call from the persistence layer by ID. */
252
+ readonly deleteToolCall: (id: string) => void | Promise<void>;
253
+ /** Emits a `message` event on the runner; may be called at any point during the turn. */
254
+ readonly emitMessage: EmitMessageFn;
255
+ /** Emits a `thought` event on the runner; may be called at any point during the turn. */
256
+ readonly emitThought: EmitThoughtFn;
257
+ /** Emits a `toolCall` event on the runner; may be called at any point during the turn. */
258
+ readonly emitToolCall: EmitToolCallFn;
259
+ /** Emits a `toolExecutionStart` event on the observability bus; forwarded from `DispatchContext` by `DispatchRunner` when a tool is invoked inside a dispatch. */
260
+ readonly emitToolExecutionStart: EmitToolExecutionStartFn;
261
+ /** Emits a `toolExecutionEnd` event on the observability bus; forwarded from `DispatchContext` by `DispatchRunner` when a tool finishes executing inside a dispatch. */
262
+ readonly emitToolExecutionEnd: EmitToolExecutionEndFn;
263
+ /** Opens a turn gate and suspends until it resolves, rejects, times out, or is aborted. */
264
+ readonly waitFor: OpenGateFn;
265
+ /** Turn-scoped tool registry constructed from the runner's baseline tools; middleware may trim or extend it. */
266
+ readonly tools: ToolRegistry;
267
+ }
@@ -0,0 +1,98 @@
1
+ import { TurnContext } from "./contracts/turn_runner_context";
2
+ import type { RawDispatchContext } from "./contracts/dispatch_context";
3
+ import type { DispatchPipelineMiddlewareFn, DispatchExecutorFn, DispatchRunnerFunctionalHookRegistrations, DispatchRunnerObservabilityHookRegistrations } from "./types/dispatch_runner";
4
+ /**
5
+ * Plain input object supplied to {@link DispatchRunner.dispatch}.
6
+ *
7
+ * @remarks
8
+ * Exactly one of `source` or `raw` is required:
9
+ *
10
+ * - `source` — switches to the **derived path**. The runner snapshots primitives from the
11
+ * provided {@link @nhtio/adk!TurnContext}, wires all fetch/refresh/mutation callbacks to delegate to it,
12
+ * forwards emits back to its buses, and bubbles mutations to its Sets at the end of every
13
+ * iteration.
14
+ *
15
+ * - `raw` — switches to the **standalone path**. The runner constructs an {@link @nhtio/adk!DispatchContext}
16
+ * directly from the provided raw input. No parent relationship exists; mutations are not bubbled.
17
+ *
18
+ * The `executor` is the user-provided callback that performs the actual LLM API call between the
19
+ * input and output middleware pipelines on every iteration.
20
+ */
21
+ export interface RawDispatchRunnerInput {
22
+ /** Source {@link @nhtio/adk!TurnContext} to derive the execution context from. Mutually exclusive with `raw`. */
23
+ source?: TurnContext;
24
+ /** Raw input for a standalone {@link @nhtio/adk!DispatchContext}. Mutually exclusive with `source`. */
25
+ raw?: Omit<RawDispatchContext, 'hooks'>;
26
+ /** User-provided callback that makes the LLM API call. Invoked between input and output pipelines on every iteration. */
27
+ executor: DispatchExecutorFn;
28
+ /** Input middleware functions, executed in order before the executor on every iteration. */
29
+ turnInputPipeline?: DispatchPipelineMiddlewareFn[];
30
+ /** Output middleware functions, executed in order after the executor on every iteration. */
31
+ turnOutputPipeline?: DispatchPipelineMiddlewareFn[];
32
+ /** Optional functional hook registrations: message, thought, toolCall. */
33
+ hooks?: DispatchRunnerFunctionalHookRegistrations;
34
+ /** Optional observability hook registrations: lifecycle events + tool execution + error. */
35
+ observers?: DispatchRunnerObservabilityHookRegistrations;
36
+ }
37
+ /**
38
+ * Orchestrates a single LLM execution dispatch — input pipeline → executor → output pipeline —
39
+ * looped until middleware/executor signals completion via {@link @nhtio/adk!DispatchContext.ack} /
40
+ * {@link @nhtio/adk!DispatchContext.nack} or the abort signal fires.
41
+ *
42
+ * @remarks
43
+ * `DispatchRunner` has a private constructor and is invoked via the static `dispatch()`
44
+ * method. Each dispatch creates a fresh single-use runner that is garbage-collected after the
45
+ * call completes — matching the `@nhtio/hooks` GC rationale already baked into the context.
46
+ *
47
+ * The runner owns the relationship between an {@link @nhtio/adk!DispatchContext} and its parent
48
+ * {@link @nhtio/adk!TurnContext} (when given a source). It subscribes to the context's mutation hooks,
49
+ * queues `ContextDelta` entries, and flushes them to the parent's Sets at the end of every
50
+ * iteration. Emits propagate from the context's hooks → the runner's hooks → (optionally) the
51
+ * parent `TurnContext`'s emit methods → the `TurnRunner`'s buses.
52
+ *
53
+ * Two hook buses, mirroring `TurnRunner`'s pattern:
54
+ *
55
+ * - **Functional** (`hooks`): `message`, `thought`, `toolCall` — pipeline-affecting events
56
+ * - **Observability** (`observers`): `iterationStart`, `iterationEnd`, `dispatchStart`,
57
+ * `dispatchEnd`, `error`, `toolExecutionStart`, `toolExecutionEnd` — instrumentation only
58
+ *
59
+ * The runner has no `maxIterations`, no `maxToolCallChecksumRepeats`. Implementers use the
60
+ * primitives — `ctx.iteration`, `ctx.toolCallCount(checksum)`, `ctx.ack()`, `ctx.nack()`,
61
+ * `ctx.abortSignal` — to build any termination bounds they need in their own middleware.
62
+ */
63
+ /**
64
+ * Module-private token gating direct construction of {@link DispatchRunner}. Callers must use
65
+ * {@link DispatchRunner.dispatch}; the symbol is not exported, so external code cannot satisfy
66
+ * the guard at runtime.
67
+ */
68
+ declare const CONSTRUCT_TOKEN: unique symbol;
69
+ export declare class DispatchRunner {
70
+ #private;
71
+ constructor(token: typeof CONSTRUCT_TOKEN, sourceCtx: TurnContext | undefined, turnInputPipeline: DispatchPipelineMiddlewareFn[], turnOutputPipeline: DispatchPipelineMiddlewareFn[], hooks: DispatchRunnerFunctionalHookRegistrations | undefined, observers: DispatchRunnerObservabilityHookRegistrations | undefined);
72
+ /**
73
+ * Returns `true` if `value` is a {@link DispatchRunner} instance.
74
+ *
75
+ * @remarks
76
+ * Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety.
77
+ *
78
+ * @param value - The value to test.
79
+ * @returns `true` when `value` is a {@link DispatchRunner} instance.
80
+ */
81
+ static isDispatchRunner(value: unknown): value is DispatchRunner;
82
+ /**
83
+ * Dispatches a single LLM execution.
84
+ *
85
+ * @remarks
86
+ * Constructs an {@link @nhtio/adk!DispatchContext} (derived from `source` or from `raw`), runs the
87
+ * iteration loop, and resolves when middleware/executor signals completion via `ctx.ack()` or
88
+ * the abort signal fires. Rejects with the nack error when middleware/executor calls
89
+ * `ctx.nack(err)`. Pipeline and executor errors are wrapped, surfaced on the observability
90
+ * `error` hook, and re-thrown.
91
+ *
92
+ * @param input - The dispatch input. Provide either `source` (derived path) or `raw` (standalone).
93
+ * @throws {@link @nhtio/adk!E_INVALID_LLM_DISPATCH_INPUT} when the input does not satisfy validation, or
94
+ * when neither `source` nor `raw` is provided, or when both are provided.
95
+ */
96
+ static dispatch(input: RawDispatchRunnerInput): Promise<void>;
97
+ }
98
+ export {};