@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,656 @@
1
+ import { a as Tokenizable, c as isObject, n as canonicalStringify, o as isError, s as isInstanceOf, t as ToolRegistry } from "../../../tool_registry-DqLOyGyG.mjs";
2
+ import { n as Message, t as Thought } from "../../../thought-CDb457b4.mjs";
3
+ import "../../../common-DeZaonK1.mjs";
4
+ import { n as Media, t as ToolCall } from "../../../tool_call-BKyyxGaZ.mjs";
5
+ import { i as ArtifactTool, t as SpooledArtifact } from "../../../spooled_artifact-C5ZtGxuJ.mjs";
6
+ import "../../../guards.mjs";
7
+ import { InMemorySpoolStore } from "../../storage/in_memory.mjs";
8
+ import { defaultBuildChatCompletionsHistory, defaultCreateChatCompletionsToolCallDeltaAccumulator, defaultDescriptionToChatCompletionsJsonSchema, defaultFilterThoughts, defaultRenderChatCompletionsSystemPrompt, defaultRenderChatCompletionsToolCallResult, defaultRenderFirstPartyRetrievables, defaultRenderMemories, defaultRenderRetrievableSafetyDirective, defaultRenderRetrievables, defaultRenderStandingInstructions, defaultRenderThirdPartyPrivateRetrievables, defaultRenderThirdPartyPublicRetrievables, defaultRenderThought, defaultRenderTimelineMessage, defaultRenderTrustedContent, defaultRenderUntrustedContent, defaultToolsToChatCompletionsTools } from "../openai_chat_completions/helpers.mjs";
9
+ import { E_INVALID_WEBLLM_CHAT_COMPLETIONS_OPTIONS, E_WEBLLM_CHAT_COMPLETIONS_CONTEXT_OVERFLOW, E_WEBLLM_CHAT_COMPLETIONS_INVALID_TOOL_CALL_ARGS, E_WEBLLM_CHAT_COMPLETIONS_STREAM_ERROR } from "./exceptions.mjs";
10
+ import { validateOptions } from "./validation.mjs";
11
+ import "./helpers.mjs";
12
+ import { v6 } from "uuid";
13
+ import { DateTime } from "luxon";
14
+ import { sha256 } from "js-sha256";
15
+ //#region src/batteries/llm/webllm_chat_completions/adapter.ts
16
+ /**
17
+ * Cross-environment executor adapter for WebLLM Chat Completions compatible endpoints.
18
+ *
19
+ * @module @nhtio/adk/batteries/llm/webllm_chat_completions/adapter
20
+ *
21
+ * @remarks
22
+ * Cross-environment LLM adapter for the WebLLM Chat Completions wire shape. Chat Completions was
23
+ * chosen as the ADK's reference adapter because it is the de-facto interchange format for the
24
+ * majority of OpenAI-compatible gateways (vLLM, Together, Groq, Fireworks, Ollama, Azure OpenAI,
25
+ * OpenRouter, DeepSeek, Mistral La Plateforme, and most self-hosted deployments). Its tool-call
26
+ * synthetic-history shape (`role: 'assistant', tool_calls: [...]` followed by `role: 'tool'` with
27
+ * `tool_call_id`) is the lowest-common-denominator that every conformant gateway accepts.
28
+ *
29
+ * The adapter is built around three pluggable layers:
30
+ *
31
+ * 1. **Translation helpers** — the thirteen swappable functions exported from `./helpers` turn
32
+ * ADK primitives ({@link @nhtio/adk!Tokenizable}, {@link @nhtio/adk!Memory}, {@link @nhtio/adk!Message}, {@link @nhtio/adk!Thought},
33
+ * {@link @nhtio/adk!ToolCall}, {@link @nhtio/adk!Tool}, {@link @nhtio/adk!ArtifactTool}, {@link @nhtio/adk!SpooledArtifact}) into Chat
34
+ * Completions wire shapes. Consumers override individual helpers via `options.helpers.*` to
35
+ * customise envelope formats, bucket ordering, thought surfacing, or JSON Schema generation
36
+ * without forking the adapter.
37
+ * 2. **Three-layer options merging** — constructor baseline, per-`executor()` overrides, and
38
+ * per-iteration `ctx.stash.webLLMChatCompletions` overrides combine with key-by-key
39
+ * precedence for `helpers` and wholesale replacement for everything else.
40
+ * The merged shape is re-validated on every iteration so a malformed stash override
41
+ * fails loud, not silently.
42
+ * 3. **WebLLM engine invocation** — accepts a preloaded `engine` or lazy `createEngine` factory.
43
+ * The resolved request body is passed directly to WebLLM's OpenAI-compatible chat API.
44
+ *
45
+ * Per-iteration flow (steps 1–9 of the plan):
46
+ * 1. Merge constructor / executor / stash options and re-validate.
47
+ * 2. Resolve helpers, falling back to bundled `default*` for each unset field.
48
+ * 3. Forge artifact-query tools by walking `ctx.turnToolCalls`, collecting unique
49
+ * `SpooledArtifact` constructors, calling `<Ctor>.forgeTools(ctx)` on each, and merging the
50
+ * results with `ctx.tools`.
51
+ * 4. Pre-render every persisted tool-call result into the prompt-ready string the timeline will
52
+ * use, cached by `tc.id`.
53
+ * 5. When `tokenEncoding !== null`, sum the token weight of every persisted bucket and throw
54
+ * {@link @nhtio/adk/batteries!E_WEBLLM_CHAT_COMPLETIONS_CONTEXT_OVERFLOW} when the total exceeds `contextWindow`.
55
+ * 6. Build the request body via `buildChatCompletionsHistory`; carry vendor-opaque reasoning
56
+ * blocks through the `_adk_reasoning_payloads` side-channel.
57
+ * 7. Resolve or lazily create a WebLLM engine and call `engine.chat.completions.create(body)`.
58
+ * 8. Streaming path: consume WebLLM's async chunk iterable; surface deltas through
59
+ * `helpers.reportMessage` / `reportThought` / `reportToolCall`; assemble tool-call deltas via
60
+ * the accumulator; persist `Message` / `Thought` / `ToolCall` records on stream end.
61
+ * 9. Non-streaming path: consume the returned Chat Completion object; same persistence +
62
+ * tool-execution loop.
63
+ */
64
+ var ADK_CONTROL_KEYS = new Set([
65
+ "engine",
66
+ "createEngine",
67
+ "engineConfig",
68
+ "chatOptions",
69
+ "stream",
70
+ "bucketOrder",
71
+ "contextWindow",
72
+ "selfIdentity",
73
+ "thoughtSurfacing",
74
+ "tokenEncoding",
75
+ "replayCompatibility",
76
+ "helpers",
77
+ "strictToolChoice",
78
+ "unsupportedMediaPolicy",
79
+ "onInitProgress",
80
+ "isWebGPUAvailable",
81
+ "autoAck"
82
+ ]);
83
+ var mergeHelpers = (layers) => {
84
+ let merged;
85
+ for (const layer of layers) {
86
+ if (!layer) continue;
87
+ merged = {
88
+ ...merged ?? {},
89
+ ...layer
90
+ };
91
+ }
92
+ return merged;
93
+ };
94
+ var mergeOptions = (baseline, exec, stash) => {
95
+ const layers = [
96
+ baseline,
97
+ exec ?? {},
98
+ stash ?? {}
99
+ ];
100
+ const out = {};
101
+ for (const layer of layers) for (const [k, v] of Object.entries(layer)) {
102
+ if (v === void 0) continue;
103
+ if (k === "helpers") continue;
104
+ out[k] = v;
105
+ }
106
+ const helpers = mergeHelpers(layers.map((l) => l.helpers));
107
+ if (helpers !== void 0) out.helpers = helpers;
108
+ return out;
109
+ };
110
+ var resolveHelpers = (overrides) => {
111
+ const src = overrides ?? {};
112
+ return {
113
+ descriptionToChatCompletionsJsonSchema: src.descriptionToChatCompletionsJsonSchema ?? defaultDescriptionToChatCompletionsJsonSchema,
114
+ renderUntrustedContent: src.renderUntrustedContent ?? defaultRenderUntrustedContent,
115
+ renderTrustedContent: src.renderTrustedContent ?? defaultRenderTrustedContent,
116
+ renderStandingInstructions: src.renderStandingInstructions ?? defaultRenderStandingInstructions,
117
+ renderMemories: src.renderMemories ?? defaultRenderMemories,
118
+ renderRetrievables: src.renderRetrievables ?? defaultRenderRetrievables,
119
+ renderRetrievableSafetyDirective: src.renderRetrievableSafetyDirective ?? defaultRenderRetrievableSafetyDirective,
120
+ renderFirstPartyRetrievables: src.renderFirstPartyRetrievables ?? defaultRenderFirstPartyRetrievables,
121
+ renderThirdPartyPublicRetrievables: src.renderThirdPartyPublicRetrievables ?? defaultRenderThirdPartyPublicRetrievables,
122
+ renderThirdPartyPrivateRetrievables: src.renderThirdPartyPrivateRetrievables ?? defaultRenderThirdPartyPrivateRetrievables,
123
+ renderTimelineMessage: src.renderTimelineMessage ?? defaultRenderTimelineMessage,
124
+ renderThought: src.renderThought ?? defaultRenderThought,
125
+ filterThoughts: src.filterThoughts ?? defaultFilterThoughts,
126
+ toolsToChatCompletionsTools: src.toolsToChatCompletionsTools ?? defaultToolsToChatCompletionsTools,
127
+ renderChatCompletionsSystemPrompt: src.renderChatCompletionsSystemPrompt ?? defaultRenderChatCompletionsSystemPrompt,
128
+ renderChatCompletionsToolCallResult: src.renderChatCompletionsToolCallResult ?? defaultRenderChatCompletionsToolCallResult,
129
+ buildChatCompletionsHistory: src.buildChatCompletionsHistory ?? defaultBuildChatCompletionsHistory,
130
+ createChatCompletionsToolCallDeltaAccumulator: src.createChatCompletionsToolCallDeltaAccumulator ?? defaultCreateChatCompletionsToolCallDeltaAccumulator
131
+ };
132
+ };
133
+ var computeChecksum = (tool, args) => sha256(canonicalStringify({
134
+ tool,
135
+ args
136
+ }));
137
+ var nowIso = () => DateTime.now().toISO() ?? (/* @__PURE__ */ new Date()).toISOString();
138
+ var estimateTokensOf = async (value, encoding) => {
139
+ return Promise.resolve(value.estimateTokens(encoding));
140
+ };
141
+ /**
142
+ * Opinionated cross-environment LLM adapter for the WebLLM Chat Completions wire shape.
143
+ *
144
+ * @remarks
145
+ * Construction validates options eagerly via {@link @nhtio/adk/batteries!validateOptions} and throws
146
+ * {@link @nhtio/adk/batteries!E_INVALID_WEBLLM_CHAT_COMPLETIONS_OPTIONS} on failure — config bugs fail loud, not at
147
+ * dispatch time. The returned instance is reusable: call {@link WebLLMChatCompletionsAdapter.executor}
148
+ * once per `DispatchRunner` configuration to obtain an {@link @nhtio/adk!DispatchExecutorFn} bound to the
149
+ * baseline plus optional executor-scope overrides.
150
+ *
151
+ * Per-iteration overrides live on the active {@link @nhtio/adk!DispatchContext}'s
152
+ * `stash.webLLMChatCompletions` slot and take highest precedence — they merge into the
153
+ * executor-scope shape on every iteration. `helpers` merge key-by-key across all three layers;
154
+ * every other field is replaced wholesale at the highest layer that
155
+ * sets it.
156
+ */
157
+ var WebLLMChatCompletionsAdapter = class WebLLMChatCompletionsAdapter {
158
+ /**
159
+ * Customary key for per-iteration overrides on `ctx.stash`. The adapter reads
160
+ * `ctx.stash.get(WebLLMChatCompletionsAdapter.STASH_KEY, {})` at the start of every
161
+ * iteration and merges the value into the resolved options shape.
162
+ */
163
+ static STASH_KEY = "webLLMChatCompletions";
164
+ #baseline;
165
+ #engine;
166
+ #enginePromise;
167
+ static isAvailable() {
168
+ return typeof globalThis.navigator !== "undefined" && "gpu" in globalThis.navigator && typeof globalThis.navigator.gpu !== "undefined";
169
+ }
170
+ /**
171
+ * @param options - Constructor-baseline options. Re-validated on every iteration after
172
+ * per-dispatch and per-iteration overrides are layered in.
173
+ * @throws {@link @nhtio/adk/batteries!E_INVALID_WEBLLM_CHAT_COMPLETIONS_OPTIONS} when `options` does not satisfy
174
+ * {@link @nhtio/adk/batteries!webLLMChatCompletionsOptionsSchema}.
175
+ */
176
+ constructor(options) {
177
+ this.#baseline = validateOptions(options);
178
+ this.#engine = this.#baseline.engine;
179
+ }
180
+ async preload(overrides) {
181
+ const merged = validateOptions(mergeOptions(this.#baseline, overrides, void 0));
182
+ return this.#resolveEngine(merged);
183
+ }
184
+ reset() {
185
+ this.#engine = void 0;
186
+ this.#enginePromise = void 0;
187
+ }
188
+ isAvailable() {
189
+ return (this.#baseline.isWebGPUAvailable ?? WebLLMChatCompletionsAdapter.isAvailable)();
190
+ }
191
+ async #resolveEngine(merged) {
192
+ if (merged.engine) {
193
+ this.#engine = merged.engine;
194
+ return merged.engine;
195
+ }
196
+ if (this.#engine) return this.#engine;
197
+ if (!(merged.isWebGPUAvailable ?? WebLLMChatCompletionsAdapter.isAvailable)()) throw new E_INVALID_WEBLLM_CHAT_COMPLETIONS_OPTIONS(["WebLLM requires a browser/runtime with WebGPU support"]);
198
+ this.#enginePromise ??= (async () => {
199
+ const engine = await (merged.createEngine ?? (async ({ model, engineConfig, chatOptions, onInitProgress }) => {
200
+ const { CreateMLCEngine } = await import("@mlc-ai/web-llm");
201
+ return await CreateMLCEngine(model, {
202
+ ...engineConfig ?? {},
203
+ initProgressCallback: onInitProgress
204
+ }, chatOptions);
205
+ }))({
206
+ model: merged.model,
207
+ engineConfig: merged.engineConfig,
208
+ chatOptions: merged.chatOptions,
209
+ onInitProgress: merged.onInitProgress
210
+ });
211
+ this.#engine = engine;
212
+ return engine;
213
+ })();
214
+ return this.#enginePromise;
215
+ }
216
+ /**
217
+ * Returns an {@link @nhtio/adk!DispatchExecutorFn} bound to this adapter's baseline plus optional
218
+ * executor-scope overrides. The returned function is reusable across iterations — every
219
+ * iteration re-merges with `ctx.stash[STASH_KEY]` and re-validates the result.
220
+ *
221
+ * @param overrides - Optional executor-scope overrides. Higher precedence than the baseline,
222
+ * lower precedence than `ctx.stash[STASH_KEY]`.
223
+ * @returns An {@link @nhtio/adk!DispatchExecutorFn} suitable for `DispatchRunner`.
224
+ */
225
+ executor(overrides) {
226
+ const baseline = this.#baseline;
227
+ const adapterClass = WebLLMChatCompletionsAdapter;
228
+ return async (ctx, helpers) => {
229
+ const localWarn = (msg) => {
230
+ helpers.log.warn({
231
+ kind: "helper-warning",
232
+ message: msg
233
+ });
234
+ };
235
+ const stashRaw = ctx.stash.get(adapterClass.STASH_KEY, {});
236
+ const merged = validateOptions(mergeOptions(baseline, overrides, stashRaw && typeof stashRaw === "object" ? stashRaw : {}));
237
+ if (merged.tokenEncoding !== null && merged.contextWindow === void 0) throw new E_INVALID_WEBLLM_CHAT_COMPLETIONS_OPTIONS(["tokenEncoding is non-null but contextWindow is undefined"]);
238
+ const resolvedHelpers = resolveHelpers(merged.helpers);
239
+ const uniqueCtors = /* @__PURE__ */ new Set();
240
+ for (const tc of ctx.turnToolCalls) {
241
+ const ctor = tc.results?.constructor;
242
+ if (ctor && SpooledArtifact.isSpooledArtifactConstructor(ctor)) uniqueCtors.add(ctor);
243
+ }
244
+ const forgedRegistries = [];
245
+ for (const ctor of uniqueCtors) {
246
+ const forgeFn = ctor.forgeTools;
247
+ if (typeof forgeFn === "function") forgedRegistries.push(forgeFn.call(ctor, ctx));
248
+ }
249
+ const mergedRegistry = ToolRegistry.merge([ctx.tools, ...forgedRegistries], { onCollision: "replace" });
250
+ mergedRegistry.bindContext(ctx);
251
+ const renderedToolCallResults = /* @__PURE__ */ new Map();
252
+ for (const tc of ctx.turnToolCalls) {
253
+ const rendered = await resolvedHelpers.renderChatCompletionsToolCallResult({
254
+ toolCall: tc,
255
+ results: tc.results,
256
+ tool: mergedRegistry.get(tc.tool),
257
+ renderUntrustedContent: resolvedHelpers.renderUntrustedContent,
258
+ renderTrustedContent: resolvedHelpers.renderTrustedContent,
259
+ unsupportedMediaPolicy: merged.unsupportedMediaPolicy ?? "throw",
260
+ warn: localWarn
261
+ });
262
+ renderedToolCallResults.set(tc.id, rendered);
263
+ }
264
+ if (merged.tokenEncoding !== null && merged.contextWindow !== void 0) {
265
+ const encoding = merged.tokenEncoding;
266
+ let spTokens = await estimateTokensOf(ctx.systemPrompt, encoding);
267
+ let siTokens = 0;
268
+ for (const si of ctx.standingInstructions) siTokens += await estimateTokensOf(si, encoding);
269
+ let memTokens = 0;
270
+ for (const mem of ctx.turnMemories) memTokens += await estimateTokensOf(mem.content, encoding);
271
+ let retTokens = 0;
272
+ for (const r of ctx.turnRetrievables) retTokens += await estimateTokensOf(r.content, encoding);
273
+ let tlTokens = 0;
274
+ for (const msg of ctx.turnMessages) if (msg.content !== void 0) tlTokens += await estimateTokensOf(msg.content, encoding);
275
+ for (const th of ctx.turnThoughts) tlTokens += await estimateTokensOf(th.content, encoding);
276
+ for (const rendered of renderedToolCallResults.values()) {
277
+ const tk = new Tokenizable(typeof rendered === "string" ? rendered : rendered.filter((b) => b.type === "text").map((b) => b.text).join("\n"));
278
+ tlTokens += await estimateTokensOf(tk, encoding);
279
+ }
280
+ const total = spTokens + siTokens + memTokens + retTokens + tlTokens;
281
+ const perBucketObj = {
282
+ systemPrompt: spTokens,
283
+ standingInstructions: siTokens,
284
+ memories: memTokens,
285
+ retrievables: retTokens,
286
+ timeline: tlTokens
287
+ };
288
+ helpers.log.debug({
289
+ kind: "context-window-usage",
290
+ message: `Context window usage: ${total}/${merged.contextWindow} tokens`,
291
+ payload: {
292
+ total,
293
+ limit: merged.contextWindow,
294
+ encoding,
295
+ perBucket: perBucketObj
296
+ }
297
+ });
298
+ if (total > merged.contextWindow) {
299
+ const perBucket = JSON.stringify(perBucketObj);
300
+ throw new E_WEBLLM_CHAT_COMPLETIONS_CONTEXT_OVERFLOW([
301
+ total,
302
+ merged.contextWindow,
303
+ encoding,
304
+ perBucket
305
+ ]);
306
+ }
307
+ }
308
+ const forcedToolNames = [];
309
+ const toolChoice = merged.tool_choice;
310
+ let toolChoiceVariant = "function";
311
+ if (toolChoice && typeof toolChoice === "object") {
312
+ if ("function" in toolChoice && toolChoice.type === "function") forcedToolNames.push(toolChoice.function.name);
313
+ else if ("custom" in toolChoice && toolChoice.type === "custom") forcedToolNames.push(toolChoice.custom.name);
314
+ else if (toolChoice.type === "allowed_tools") {
315
+ toolChoiceVariant = "allowed_tools";
316
+ for (const entry of toolChoice.allowed_tools.tools) if ("function" in entry) forcedToolNames.push(entry.function.name);
317
+ else if ("custom" in entry) forcedToolNames.push(entry.custom.name);
318
+ }
319
+ }
320
+ const forcedForgedHits = [];
321
+ for (const name of forcedToolNames) if (mergedRegistry.get(name)?.ephemeral === true) forcedForgedHits.push({ toolName: name });
322
+ if (forcedForgedHits.length > 0) {
323
+ if (merged.strictToolChoice === true) throw new E_INVALID_WEBLLM_CHAT_COMPLETIONS_OPTIONS([`tool_choice forces forged ephemeral artifact-query tool(s): ${forcedForgedHits.map((h) => h.toolName).join(", ")} — these may not exist on the next iteration. Remove the override or unset strictToolChoice.`]);
324
+ helpers.log.warn({
325
+ kind: "tool-choice-forged-artifact",
326
+ message: `tool_choice forces ${forcedForgedHits.length} forged ephemeral artifact-query tool(s); this is almost always a misconfiguration`,
327
+ payload: {
328
+ toolNames: forcedForgedHits.map((h) => h.toolName),
329
+ variant: toolChoiceVariant
330
+ }
331
+ });
332
+ }
333
+ const { messages: wireMessages, reasoningPayloads } = await resolvedHelpers.buildChatCompletionsHistory({
334
+ systemPrompt: ctx.systemPrompt,
335
+ standingInstructions: ctx.standingInstructions,
336
+ memories: ctx.turnMemories,
337
+ retrievables: ctx.turnRetrievables,
338
+ messages: ctx.turnMessages,
339
+ thoughts: ctx.turnThoughts,
340
+ toolCalls: ctx.turnToolCalls,
341
+ tools: mergedRegistry,
342
+ renderedToolCallResults,
343
+ bucketOrder: merged.bucketOrder ?? [
344
+ "standingInstructions",
345
+ "memories",
346
+ "retrievables",
347
+ "timeline"
348
+ ],
349
+ selfIdentity: merged.selfIdentity ?? "assistant",
350
+ thoughtSurfacing: merged.thoughtSurfacing ?? "all-self",
351
+ replayCompatibility: merged.replayCompatibility ?? [],
352
+ renderChatCompletionsToolCallResult: resolvedHelpers.renderChatCompletionsToolCallResult,
353
+ renderChatCompletionsSystemPrompt: resolvedHelpers.renderChatCompletionsSystemPrompt,
354
+ renderStandingInstructions: resolvedHelpers.renderStandingInstructions,
355
+ renderMemories: resolvedHelpers.renderMemories,
356
+ renderRetrievables: resolvedHelpers.renderRetrievables,
357
+ renderRetrievableSafetyDirective: resolvedHelpers.renderRetrievableSafetyDirective,
358
+ renderFirstPartyRetrievables: resolvedHelpers.renderFirstPartyRetrievables,
359
+ renderThirdPartyPublicRetrievables: resolvedHelpers.renderThirdPartyPublicRetrievables,
360
+ renderThirdPartyPrivateRetrievables: resolvedHelpers.renderThirdPartyPrivateRetrievables,
361
+ renderTimelineMessage: resolvedHelpers.renderTimelineMessage,
362
+ renderThought: resolvedHelpers.renderThought,
363
+ filterThoughts: resolvedHelpers.filterThoughts,
364
+ renderUntrustedContent: resolvedHelpers.renderUntrustedContent,
365
+ renderTrustedContent: resolvedHelpers.renderTrustedContent,
366
+ unsupportedMediaPolicy: merged.unsupportedMediaPolicy ?? "throw",
367
+ warn: localWarn
368
+ });
369
+ const stream = merged.stream ?? true;
370
+ const body = {
371
+ model: merged.model,
372
+ messages: wireMessages,
373
+ stream
374
+ };
375
+ for (const [k, v] of Object.entries(merged)) {
376
+ if (ADK_CONTROL_KEYS.has(k)) continue;
377
+ if (k === "model" || k === "messages" || k === "stream") continue;
378
+ if (v === void 0) continue;
379
+ body[k] = v;
380
+ }
381
+ const toolsArr = mergedRegistry.all();
382
+ if (toolsArr.length > 0) body.tools = resolvedHelpers.toolsToChatCompletionsTools(toolsArr, { descriptionToChatCompletionsJsonSchema: resolvedHelpers.descriptionToChatCompletionsJsonSchema });
383
+ if (reasoningPayloads.length > 0) body._adk_reasoning_payloads = reasoningPayloads;
384
+ let completion;
385
+ try {
386
+ completion = await (await this.#resolveEngine(merged)).chat.completions.create(body);
387
+ } catch (err) {
388
+ helpers.log.error({
389
+ kind: "webllm-engine-error",
390
+ message: `WebLLM engine failure: ${isError(err) ? err.message : String(err)}`,
391
+ payload: { detail: isError(err) ? err.message : String(err) }
392
+ });
393
+ ctx.nack(new E_WEBLLM_CHAT_COMPLETIONS_STREAM_ERROR([isError(err) ? err.message : String(err)]));
394
+ return;
395
+ }
396
+ const spoolStore = new InMemorySpoolStore();
397
+ const executeAndPersistToolCall = async (call) => {
398
+ const tool = mergedRegistry.get(call.name);
399
+ let args = {};
400
+ let parseError;
401
+ if (call.args && call.args.length > 0) try {
402
+ const parsed = JSON.parse(call.args);
403
+ if (isObject(parsed)) args = parsed;
404
+ else parseError = new E_WEBLLM_CHAT_COMPLETIONS_INVALID_TOOL_CALL_ARGS([`must be a JSON object; received ${Array.isArray(parsed) ? "array" : parsed === null ? "null" : typeof parsed}`, call.args]);
405
+ } catch {
406
+ parseError = new E_WEBLLM_CHAT_COMPLETIONS_INVALID_TOOL_CALL_ARGS(["are not valid JSON", call.args]);
407
+ }
408
+ const completedAt = nowIso();
409
+ if (parseError !== void 0) {
410
+ const results = new Tokenizable(parseError.message);
411
+ helpers.reportToolCall(call.id, {
412
+ tool: call.name,
413
+ args
414
+ });
415
+ helpers.reportToolCall(call.id, {
416
+ results,
417
+ isError: true,
418
+ isComplete: true
419
+ });
420
+ const checksum = computeChecksum(call.name, args);
421
+ await ctx.storeToolCall(new ToolCall({
422
+ id: call.id,
423
+ tool: call.name,
424
+ args,
425
+ checksum,
426
+ isComplete: true,
427
+ isError: true,
428
+ results,
429
+ createdAt: completedAt,
430
+ updatedAt: completedAt,
431
+ completedAt
432
+ }));
433
+ return;
434
+ }
435
+ if (!tool) {
436
+ const results = new Tokenizable(`Tool not found: ${call.name}`);
437
+ helpers.reportToolCall(call.id, {
438
+ tool: call.name,
439
+ args
440
+ });
441
+ helpers.reportToolCall(call.id, {
442
+ results,
443
+ isError: true,
444
+ isComplete: true
445
+ });
446
+ const checksum = computeChecksum(call.name, args);
447
+ await ctx.storeToolCall(new ToolCall({
448
+ id: call.id,
449
+ tool: call.name,
450
+ args,
451
+ checksum,
452
+ isComplete: true,
453
+ isError: true,
454
+ results,
455
+ createdAt: completedAt,
456
+ updatedAt: completedAt,
457
+ completedAt
458
+ }));
459
+ return;
460
+ }
461
+ helpers.reportToolCall(call.id, {
462
+ tool: tool.name,
463
+ args
464
+ });
465
+ const isArtifactTool = ArtifactTool.isArtifactTool(tool);
466
+ let results = new Tokenizable("");
467
+ let toolHadError = false;
468
+ try {
469
+ const raw = await tool.executor(ctx)(args);
470
+ if (isArtifactTool) if (Tokenizable.isTokenizable(raw)) results = raw;
471
+ else if (typeof raw === "string") results = new Tokenizable(raw);
472
+ else throw new Error(`ArtifactTool "${tool.name}" returned a non-string/non-Tokenizable value`);
473
+ else if (Media.isMedia(raw)) results = raw;
474
+ else if (Array.isArray(raw) && raw.length > 0 && raw.every((m) => Media.isMedia(m))) results = raw;
475
+ else if (typeof raw === "string" || isInstanceOf(raw, "Uint8Array", Uint8Array)) {
476
+ const reader = spoolStore.write(call.id, raw);
477
+ results = new ((tool.artifactConstructor?.()) ?? SpooledArtifact)(reader);
478
+ } else {
479
+ const reader = spoolStore.write(call.id, String(raw));
480
+ results = new ((tool.artifactConstructor?.()) ?? SpooledArtifact)(reader);
481
+ }
482
+ } catch (err) {
483
+ toolHadError = true;
484
+ let detailMsg = isError(err) ? err.message : String(err);
485
+ if (isError(err) && isError(err.cause) && err.cause.message && err.cause.message !== err.message) detailMsg = `${detailMsg} ${err.cause.message}`;
486
+ results = new Tokenizable(detailMsg);
487
+ }
488
+ helpers.reportToolCall(call.id, {
489
+ results,
490
+ isError: toolHadError,
491
+ isComplete: true
492
+ });
493
+ const checksum = computeChecksum(tool.name, args);
494
+ const completedAt2 = nowIso();
495
+ await ctx.storeToolCall(new ToolCall({
496
+ id: call.id,
497
+ tool: tool.name,
498
+ args,
499
+ checksum,
500
+ isComplete: true,
501
+ isError: toolHadError,
502
+ results,
503
+ fromArtifactTool: isArtifactTool,
504
+ createdAt: completedAt2,
505
+ updatedAt: completedAt2,
506
+ completedAt: completedAt2
507
+ }));
508
+ };
509
+ const selfIdentity = merged.selfIdentity ?? "assistant";
510
+ if (stream) {
511
+ const chunks = completion;
512
+ if (!chunks || typeof chunks[Symbol.asyncIterator] !== "function") {
513
+ ctx.nack(new E_WEBLLM_CHAT_COMPLETIONS_STREAM_ERROR(["engine did not return a stream"]));
514
+ return;
515
+ }
516
+ const accumulator = resolvedHelpers.createChatCompletionsToolCallDeltaAccumulator();
517
+ const streamId = v6();
518
+ let partialMessageContent = "";
519
+ let partialThoughtContent = "";
520
+ let sawMessageDelta = false;
521
+ let sawThoughtDelta = false;
522
+ const drainAndPersist = async () => {
523
+ if (sawMessageDelta) {
524
+ helpers.reportMessage(streamId, "", { isComplete: true });
525
+ await ctx.storeMessage(new Message({
526
+ id: streamId,
527
+ role: "assistant",
528
+ content: partialMessageContent,
529
+ identity: selfIdentity,
530
+ createdAt: nowIso(),
531
+ updatedAt: nowIso()
532
+ }));
533
+ }
534
+ if (sawThoughtDelta) {
535
+ helpers.reportThought(streamId, "", { isComplete: true });
536
+ await ctx.storeThought(new Thought({
537
+ id: streamId,
538
+ content: partialThoughtContent,
539
+ identity: selfIdentity,
540
+ createdAt: nowIso(),
541
+ updatedAt: nowIso()
542
+ }));
543
+ }
544
+ const calls = accumulator.drain();
545
+ helpers.log.debug({
546
+ kind: "accumulator-finalised",
547
+ message: `Stream finalised: ${calls.length} tool call(s), message=${sawMessageDelta}, thought=${sawThoughtDelta}`,
548
+ payload: {
549
+ toolCallCount: calls.length,
550
+ sawMessageDelta,
551
+ sawThoughtDelta
552
+ }
553
+ });
554
+ if (calls.length === 0) {
555
+ if (merged.autoAck) ctx.ack();
556
+ return;
557
+ }
558
+ for (const call of calls) {
559
+ if (ctx.abortSignal.aborted) return;
560
+ await executeAndPersistToolCall(call);
561
+ }
562
+ };
563
+ try {
564
+ for await (const chunk of chunks) {
565
+ if (ctx.abortSignal.aborted) return;
566
+ const delta = chunk.choices?.[0]?.delta;
567
+ if (!delta) continue;
568
+ if (typeof delta.content === "string" && delta.content.length > 0) {
569
+ sawMessageDelta = true;
570
+ partialMessageContent += delta.content;
571
+ helpers.reportMessage(streamId, delta.content);
572
+ }
573
+ const reasoning = delta.reasoning_content;
574
+ if (typeof reasoning === "string" && reasoning.length > 0) {
575
+ sawThoughtDelta = true;
576
+ partialThoughtContent += reasoning;
577
+ helpers.reportThought(streamId, reasoning);
578
+ }
579
+ if (Array.isArray(delta.tool_calls)) for (const d of delta.tool_calls) accumulator.feed(d);
580
+ }
581
+ await drainAndPersist();
582
+ } catch (err) {
583
+ if (ctx.abortSignal.aborted) return;
584
+ helpers.log.error({
585
+ kind: "stream-error",
586
+ message: `WebLLM stream failed: ${isError(err) ? err.message : String(err)}`,
587
+ payload: { detail: isError(err) ? err.message : String(err) }
588
+ });
589
+ ctx.nack(new E_WEBLLM_CHAT_COMPLETIONS_STREAM_ERROR([isError(err) ? err.message : String(err)]));
590
+ return;
591
+ }
592
+ return;
593
+ }
594
+ const parsed = completion;
595
+ const choice = parsed.choices?.[0];
596
+ if (!choice) {
597
+ if (merged.autoAck) ctx.ack();
598
+ return;
599
+ }
600
+ const msg = choice.message;
601
+ const responseId = parsed.id ?? v6();
602
+ if (msg && typeof msg.content === "string" && msg.content.length > 0) {
603
+ const messageId = `${responseId}:message`;
604
+ helpers.reportMessage(messageId, msg.content, { isComplete: true });
605
+ await ctx.storeMessage(new Message({
606
+ id: messageId,
607
+ role: "assistant",
608
+ content: msg.content,
609
+ identity: selfIdentity,
610
+ createdAt: nowIso(),
611
+ updatedAt: nowIso()
612
+ }));
613
+ }
614
+ const reasoning = msg?.reasoning_content;
615
+ if (typeof reasoning === "string" && reasoning.length > 0) {
616
+ const thoughtId = `${responseId}:thought`;
617
+ helpers.reportThought(thoughtId, reasoning, { isComplete: true });
618
+ await ctx.storeThought(new Thought({
619
+ id: thoughtId,
620
+ content: reasoning,
621
+ identity: selfIdentity,
622
+ createdAt: nowIso(),
623
+ updatedAt: nowIso()
624
+ }));
625
+ }
626
+ const rawCalls = msg?.tool_calls ?? [];
627
+ if (rawCalls.length === 0) {
628
+ if (merged.autoAck) ctx.ack();
629
+ return;
630
+ }
631
+ const calls = rawCalls.map((tc) => ({
632
+ id: tc.id,
633
+ type: tc.type ?? "function",
634
+ name: tc.function?.name ?? "",
635
+ args: tc.function?.arguments ?? ""
636
+ }));
637
+ for (const call of calls) {
638
+ if (ctx.abortSignal.aborted) return;
639
+ await executeAndPersistToolCall(call);
640
+ }
641
+ };
642
+ }
643
+ /**
644
+ * Returns `true` when `value` is an {@link WebLLMChatCompletionsAdapter} instance.
645
+ *
646
+ * @param value - The value to test.
647
+ * @returns `true` when `value` is an `WebLLMChatCompletionsAdapter` instance.
648
+ */
649
+ static isWebLLMChatCompletionsAdapter(value) {
650
+ return isInstanceOf(value, "WebLLMChatCompletionsAdapter", WebLLMChatCompletionsAdapter);
651
+ }
652
+ };
653
+ //#endregion
654
+ export { WebLLMChatCompletionsAdapter };
655
+
656
+ //# sourceMappingURL=adapter.mjs.map