llmz 0.9.0 → 1.0.2

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 (238) hide show
  1. package/DOCS.md +232 -1794
  2. package/ERRORS.md +101 -0
  3. package/README.md +96 -582
  4. package/dist/chat/chat.d.ts +63 -0
  5. package/dist/chat/component.d.ts +31 -0
  6. package/dist/chat/component.default.d.ts +58 -0
  7. package/dist/chat/response.d.ts +15 -0
  8. package/dist/compiler/compiler.d.ts +2 -7
  9. package/dist/compiler/plugins/dynamic-code.d.ts +7 -0
  10. package/dist/compiler/plugins/replace-comment.d.ts +2 -3
  11. package/dist/compiler/plugins/return-async.d.ts +5 -4
  12. package/dist/compiler/plugins/termination.d.ts +8 -0
  13. package/dist/compiler/plugins/tool-resolution.d.ts +4 -0
  14. package/dist/compiler/plugins/variable-extraction.d.ts +2 -8
  15. package/dist/component-CMFoCHyu.cjs +649 -0
  16. package/dist/component-DncqP4fB.js +614 -0
  17. package/dist/context.d.ts +71 -378
  18. package/dist/custom-client-C-WXvLD9.js +1655 -0
  19. package/dist/custom-client-Cgt9_0r1.cjs +1732 -0
  20. package/dist/custom-client.d.ts +12 -2
  21. package/dist/errors/catalog.d.ts +294 -0
  22. package/dist/errors/hooks.d.ts +2 -0
  23. package/dist/errors-C6ppQUZX.cjs +893 -0
  24. package/dist/errors-Crk5P8jo.js +636 -0
  25. package/dist/errors.d.ts +24 -36
  26. package/dist/execute-CiYvPs5Q.js +2061 -0
  27. package/dist/execute-DTTfacsi.cjs +2062 -0
  28. package/dist/{exit-DUM96DN1.cjs → exit-BOtKWPuT.cjs} +38 -28
  29. package/dist/{exit-CDdxDBRT.js → exit-DMPwCSba.js} +37 -27
  30. package/dist/exit.d.ts +19 -16
  31. package/dist/index.cjs +177 -1237
  32. package/dist/index.d.ts +25 -28
  33. package/dist/index.js +137 -1213
  34. package/dist/index.workerd.js +8974 -11145
  35. package/dist/inspect.d.ts +18 -4
  36. package/dist/inspection.d.ts +40 -0
  37. package/dist/native-0bq5ATZM.cjs +212 -0
  38. package/dist/native-n_718Z-T.js +177 -0
  39. package/dist/objects.d.ts +2 -57
  40. package/dist/prompts/native.d.ts +2 -0
  41. package/dist/prompts/prompt.d.ts +10 -101
  42. package/dist/quickjs-variant-21IO97PF.cjs +1080 -0
  43. package/dist/quickjs-variant-CtuGagsb.js +985 -0
  44. package/dist/result.d.ts +30 -861
  45. package/dist/runtime/execute.d.ts +3 -2
  46. package/dist/runtime/execution-activity.d.ts +11 -0
  47. package/dist/runtime/execution-diagnostics.d.ts +11 -0
  48. package/dist/runtime/execution-report.d.ts +51 -0
  49. package/dist/runtime/fixtures/chat.d.ts +8 -0
  50. package/dist/runtime/fixtures/long-search.d.ts +1 -1
  51. package/dist/runtime/fixtures/native-client.d.ts +33 -0
  52. package/dist/runtime/fixtures/protocol-languages.d.ts +1 -1
  53. package/dist/runtime/forced-inspection.d.ts +13 -0
  54. package/dist/runtime/forced-inspection.test.d.ts +1 -0
  55. package/dist/runtime/generate.d.ts +23 -20
  56. package/dist/runtime/generate.test.d.ts +1 -0
  57. package/dist/runtime/hook-provenance.test.d.ts +1 -0
  58. package/dist/runtime/inspection-hook.test.d.ts +1 -0
  59. package/dist/runtime/inspection-values.d.ts +14 -0
  60. package/dist/runtime/inspection-values.test.d.ts +1 -0
  61. package/dist/runtime/interruption-control-flow.test.d.ts +1 -0
  62. package/dist/runtime/interruption-settings.test.d.ts +1 -0
  63. package/dist/runtime/javascript-api.d.ts +62 -0
  64. package/dist/runtime/javascript-api.test.d.ts +1 -0
  65. package/dist/runtime/native-review.test.d.ts +1 -0
  66. package/dist/runtime/native-tools.d.ts +18 -0
  67. package/dist/runtime/native-tools.test.d.ts +1 -0
  68. package/dist/runtime/object-memory.test.d.ts +1 -0
  69. package/dist/runtime/single-tool.test.d.ts +1 -0
  70. package/dist/runtime/think-signal.test.d.ts +1 -0
  71. package/dist/runtime/token-budget.d.ts +22 -0
  72. package/dist/runtime/token-budget.test.d.ts +1 -0
  73. package/dist/runtime/tool-wrapper.d.ts +6 -5
  74. package/dist/runtime/traces.test.d.ts +1 -0
  75. package/dist/runtime/truncation.test.d.ts +1 -0
  76. package/dist/runtime/types.d.ts +47 -31
  77. package/dist/runtime/variable-recovery.test.d.ts +1 -0
  78. package/dist/runtime/vm-context.d.ts +6 -1
  79. package/dist/runtime/worker-completion.test.d.ts +1 -0
  80. package/dist/runtime-names.d.ts +4 -0
  81. package/dist/schema-CXlnbb3n.cjs +68 -0
  82. package/dist/schema-GE4ISPCR.js +45 -0
  83. package/dist/schema.d.ts +10 -0
  84. package/dist/schema.test.d.ts +1 -0
  85. package/dist/session/compactor.d.ts +51 -0
  86. package/dist/session/compactor.test.d.ts +1 -0
  87. package/dist/session/history.d.ts +36 -0
  88. package/dist/session/history.test.d.ts +1 -0
  89. package/dist/session/json.d.ts +3 -0
  90. package/dist/session/json.test.d.ts +1 -0
  91. package/dist/session/memory-codec.d.ts +9 -0
  92. package/dist/session/memory-codec.test.d.ts +1 -0
  93. package/dist/session/memory-render.d.ts +15 -0
  94. package/dist/session/memory-render.test.d.ts +1 -0
  95. package/dist/session/memory.d.ts +112 -0
  96. package/dist/session/memory.test.d.ts +1 -0
  97. package/dist/session/messages.d.ts +19 -0
  98. package/dist/session/messages.test.d.ts +1 -0
  99. package/dist/session/serialization.d.ts +36 -0
  100. package/dist/session/serialization.test.d.ts +1 -0
  101. package/dist/session/session-memory.test.d.ts +1 -0
  102. package/dist/session/session.d.ts +121 -0
  103. package/dist/session/session.test.d.ts +1 -0
  104. package/dist/session/speaker.test.d.ts +1 -0
  105. package/dist/{transcript.d.ts → session/transcript.d.ts} +4 -8
  106. package/dist/session/transcript.test.d.ts +1 -0
  107. package/dist/{tool-Bv8N4IPj.js → tool-DstssiAs.js} +144 -92
  108. package/dist/{tool-Dy6AJpJH.cjs → tool-_wK9hQOb.cjs} +146 -94
  109. package/dist/tool.d.ts +25 -17
  110. package/dist/truncate-BEKbdo3t.js +79 -0
  111. package/dist/truncate-uomAMGPs.cjs +102 -0
  112. package/dist/truncate.d.ts +24 -0
  113. package/dist/truncate.test.d.ts +1 -0
  114. package/dist/types.d.ts +33 -9
  115. package/dist/{typings-3lsqCbkn.cjs → typings-BA_XxAcY.cjs} +38 -193
  116. package/dist/{typings-CrBa7Tyy.js → typings-BEVDvtuk.js} +38 -175
  117. package/dist/typings-formatter.d.ts +1 -1
  118. package/dist/typings.d.ts +3 -0
  119. package/dist/{uniq-BYQ7GWwb.js → uniq-DfUeSNaX.js} +2 -2
  120. package/dist/{uniq-G872RWmE.cjs → uniq-DiXgysNV.cjs} +6 -18
  121. package/dist/{_baseIteratee-P_firB7b.js → utils-BOFzb8Ok.js} +1134 -16
  122. package/dist/{_baseIteratee-CF4qP2a8.cjs → utils-CTlsXvdj.cjs} +1281 -84
  123. package/dist/vm/drivers/node.d.ts +1 -1
  124. package/dist/vm/drivers/quickjs.d.ts +1 -1
  125. package/dist/vm/errors.d.ts +4 -4
  126. package/dist/vm/index.d.ts +3 -4
  127. package/dist/vm/instrument.d.ts +14 -6
  128. package/dist/vm/member-proxy.d.ts +4 -0
  129. package/dist/vm/member-proxy.test.d.ts +1 -0
  130. package/dist/vm/memory.test.d.ts +1 -0
  131. package/dist/vm/return-semantics.test.d.ts +1 -0
  132. package/dist/vm/types.d.ts +20 -3
  133. package/dist/{vm-BLsHw1da.js → vm-C7SfutHn.js} +1187 -180
  134. package/dist/{vm-D6s1zF6M.cjs → vm-gmekHc0i.cjs} +1221 -207
  135. package/package.json +13 -6
  136. package/dist/_baseForOwn-BfWDTL8L.cjs +0 -53
  137. package/dist/_baseForOwn-CFhWe_W6.js +0 -48
  138. package/dist/_getPrototype-CG7Dxrrz.js +0 -6
  139. package/dist/_getPrototype-DQDlNVVM.cjs +0 -10
  140. package/dist/_overArg-C5Hy7oCi.cjs +0 -196
  141. package/dist/_overArg-CRYbj1NH.js +0 -155
  142. package/dist/chat.d.ts +0 -401
  143. package/dist/common-BlRFtdf2.js +0 -807
  144. package/dist/common-BnvrDnIT.cjs +0 -860
  145. package/dist/compiler/plugins/track-tool-calls.d.ts +0 -33
  146. package/dist/compiler-C-OI9sim.cjs +0 -547
  147. package/dist/compiler-CW1VjQsn.js +0 -473
  148. package/dist/component-C6Agl2ns.cjs +0 -73
  149. package/dist/component-DGG7d_Mz.js +0 -44
  150. package/dist/component.d.ts +0 -88
  151. package/dist/component.default.d.ts +0 -278
  152. package/dist/custom-client-ChuZ63zp.js +0 -2658
  153. package/dist/custom-client-DAsGRnYc.cjs +0 -2729
  154. package/dist/dual-modes-DDkoCp3R.js +0 -3006
  155. package/dist/dual-modes-DPXiuvNe.cjs +0 -3090
  156. package/dist/errors-C962uJdS.cjs +0 -220
  157. package/dist/errors-Cg1G0Awu.js +0 -161
  158. package/dist/example-format.d.ts +0 -8
  159. package/dist/example.d.ts +0 -45
  160. package/dist/execute-BWcdv9so.js +0 -1059
  161. package/dist/execute-DM2-7y86.cjs +0 -1060
  162. package/dist/exit-parser.d.ts +0 -39
  163. package/dist/handlers.d.ts +0 -10
  164. package/dist/hoist.d.ts +0 -2
  165. package/dist/isEmpty-BhJRCqIF.js +0 -52
  166. package/dist/isEmpty-LY533Wmh.cjs +0 -57
  167. package/dist/isPlainObject-CSxXVKCk.cjs +0 -56
  168. package/dist/isPlainObject-D39AzPO2.js +0 -51
  169. package/dist/jsx-CIgdGGVs.js +0 -52
  170. package/dist/jsx-owqxrquU.cjs +0 -75
  171. package/dist/jsx.d.ts +0 -11
  172. package/dist/message-stream/index.d.ts +0 -9
  173. package/dist/message-stream/instructions.d.ts +0 -30
  174. package/dist/message-stream/normalizer.d.ts +0 -21
  175. package/dist/message-stream/parser.d.ts +0 -89
  176. package/dist/message-stream/registry.d.ts +0 -14
  177. package/dist/message-stream/replayable.d.ts +0 -27
  178. package/dist/message-stream/response-parser.d.ts +0 -24
  179. package/dist/message-stream/sanitize.d.ts +0 -10
  180. package/dist/message-stream/stream.d.ts +0 -54
  181. package/dist/message-stream/types.d.ts +0 -97
  182. package/dist/message-stream/validator.d.ts +0 -23
  183. package/dist/prompts/chat-mode/protocol.d.ts +0 -2
  184. package/dist/prompts/chat-mode/system.d.ts +0 -2
  185. package/dist/prompts/chat-mode/system.md.d.ts +0 -2
  186. package/dist/prompts/chat-mode/user.d.ts +0 -2
  187. package/dist/prompts/chat-mode/user.md.d.ts +0 -2
  188. package/dist/prompts/code-examples.d.ts +0 -2
  189. package/dist/prompts/common.d.ts +0 -7
  190. package/dist/prompts/dual-modes.d.ts +0 -2
  191. package/dist/prompts/execution-history.d.ts +0 -6
  192. package/dist/prompts/execution-state.d.ts +0 -3
  193. package/dist/prompts/protocol-basics.d.ts +0 -5
  194. package/dist/prompts/protocol.d.ts +0 -31
  195. package/dist/prompts/worker-mode/protocol.d.ts +0 -2
  196. package/dist/prompts/worker-mode/system.d.ts +0 -2
  197. package/dist/prompts/worker-mode/system.md.d.ts +0 -2
  198. package/dist/prompts/worker-mode/user.d.ts +0 -2
  199. package/dist/prompts/worker-mode/user.md.d.ts +0 -2
  200. package/dist/quickjs-variant-BYhMBpwU.js +0 -45
  201. package/dist/quickjs-variant-CsIwTVFC.cjs +0 -56
  202. package/dist/rolldown-runtime-EAsCxrDo.js +0 -14
  203. package/dist/runtime/fixtures/qwen-protocol-failures.d.ts +0 -29
  204. package/dist/runtime/interpret-result.d.ts +0 -22
  205. package/dist/snapshots.d.ts +0 -249
  206. package/dist/stack-traces-BIE_5Xwf.js +0 -21
  207. package/dist/stack-traces-ntw_qhoy.cjs +0 -26
  208. package/dist/truncator-CLkouTCO.cjs +0 -276
  209. package/dist/truncator-DZqEqnpD.js +0 -253
  210. package/dist/truncator.d.ts +0 -118
  211. package/dist/utils-3ESwP3yg.cjs +0 -1075
  212. package/dist/utils-UBeAFeEI.js +0 -950
  213. /package/dist/{citations.d.ts → chat/citations.d.ts} +0 -0
  214. /package/dist/{citations.test.d.ts → chat/citations.test.d.ts} +0 -0
  215. /package/dist/{component.default.test.d.ts → chat/component.default.test.d.ts} +0 -0
  216. /package/dist/{compiler/plugins/track-tool-calls.test.d.ts → chat/component.test.d.ts} +0 -0
  217. /package/dist/{example.test.d.ts → chat/response.test.d.ts} +0 -0
  218. /package/dist/{exit-parser.test.d.ts → compiler/plugins/dynamic-code.test.d.ts} +0 -0
  219. /package/dist/{handlers.test.d.ts → compiler/plugins/tool-resolution.test.d.ts} +0 -0
  220. /package/dist/{hoist.test.d.ts → errors/catalog.test.d.ts} +0 -0
  221. /package/dist/{message-stream/instructions.test.d.ts → exit.test.d.ts} +0 -0
  222. /package/dist/{message-stream/normalizer.test.d.ts → inspection.test.d.ts} +0 -0
  223. /package/dist/{message-stream/parser.test.d.ts → prompts/native.test.d.ts} +0 -0
  224. /package/dist/{message-stream/response-parser.test.d.ts → result.test.d.ts} +0 -0
  225. /package/dist/{message-stream/stream.test.d.ts → runtime/chat-completion.test.d.ts} +0 -0
  226. /package/dist/{message-stream/validator.test.d.ts → runtime/chat-delivery.test.d.ts} +0 -0
  227. /package/dist/{prompts/adherence.test.d.ts → runtime/chat-last-speaker.test.d.ts} +0 -0
  228. /package/dist/{prompts/catalogue-examples.test.d.ts → runtime/compatibility-regressions.test.d.ts} +0 -0
  229. /package/dist/{prompts/code-examples.test.d.ts → runtime/context-registration.test.d.ts} +0 -0
  230. /package/dist/{prompts/common.test.d.ts → runtime/empty-completion.test.d.ts} +0 -0
  231. /package/dist/{prompts/concrete-patterns.test.d.ts → runtime/error-recovery.test.d.ts} +0 -0
  232. /package/dist/{prompts/long-search.test.d.ts → runtime/errors.test.d.ts} +0 -0
  233. /package/dist/{prompts/protocol-simple.test.d.ts → runtime/execution-activity.test.d.ts} +0 -0
  234. /package/dist/{prompts/protocol.test.d.ts → runtime/execution-diagnostics.test.d.ts} +0 -0
  235. /package/dist/{prompts/sections.test.d.ts → runtime/execution-report.test.d.ts} +0 -0
  236. /package/dist/runtime/{interpret-result.test.d.ts → execution-safety.test.d.ts} +0 -0
  237. /package/dist/{transcript.test.d.ts → runtime/exit-control-flow.test.d.ts} +0 -0
  238. /package/dist/{truncator.test.d.ts → runtime/fixtures/long-search.test.d.ts} +0 -0
@@ -1,1059 +0,0 @@
1
- import { a as isEqual, l as toNumber } from "./dual-modes-DDkoCp3R.js";
2
- import { a as ResponseParser, i as toParsedAssistantResponse } from "./common-BlRFtdf2.js";
3
- import { d as Snapshot, f as parseExit, l as PartialExecutionResult, n as Context, s as ErrorExecutionResult, t as _CustomModelClient, u as SuccessExecutionResult } from "./custom-client-ChuZ63zp.js";
4
- import { d as stripInvalidIdentifiers, i as getErrorMessage, s as init } from "./utils-UBeAFeEI.js";
5
- import { t as createJsxComponent } from "./jsx-CIgdGGVs.js";
6
- import { t as cleanStackTrace } from "./stack-traces-BIE_5Xwf.js";
7
- import { a as InvalidCodeError, c as SnapshotSignal, i as CognitiveError, l as ThinkSignal, n as CodeExecutionError, o as LoopExceededError, s as Signals, t as AssignmentError } from "./errors-Cg1G0Awu.js";
8
- import { n as truncateWrappedContent } from "./truncator-DZqEqnpD.js";
9
- import { r as hasTopLevelReturn } from "./compiler-CW1VjQsn.js";
10
- import { runAsyncFunction, warmupVM } from "./vm-BLsHw1da.js";
11
- import { z } from "@bpinternal/zui";
12
- import { ulid } from "ulid";
13
- import { Client } from "@botpress/client";
14
- import { Cognitive } from "@botpress/cognitive";
15
- import ms from "ms";
16
- //#region ../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_baseClamp.js
17
- /**
18
- * The base implementation of `_.clamp` which doesn't coerce arguments.
19
- *
20
- * @private
21
- * @param {number} number The number to clamp.
22
- * @param {number} [lower] The lower bound.
23
- * @param {number} upper The upper bound.
24
- * @returns {number} Returns the clamped number.
25
- */
26
- function baseClamp(number, lower, upper) {
27
- if (number === number) {
28
- if (upper !== void 0) number = number <= upper ? number : upper;
29
- if (lower !== void 0) number = number >= lower ? number : lower;
30
- }
31
- return number;
32
- }
33
- //#endregion
34
- //#region ../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/clamp.js
35
- /**
36
- * Clamps `number` within the inclusive `lower` and `upper` bounds.
37
- *
38
- * @static
39
- * @memberOf _
40
- * @since 4.0.0
41
- * @category Number
42
- * @param {number} number The number to clamp.
43
- * @param {number} [lower] The lower bound.
44
- * @param {number} upper The upper bound.
45
- * @returns {number} Returns the clamped number.
46
- * @example
47
- *
48
- * _.clamp(-10, -5, 5);
49
- * // => -5
50
- *
51
- * _.clamp(10, -5, 5);
52
- * // => 5
53
- */
54
- function clamp(number, lower, upper) {
55
- if (upper === void 0) {
56
- upper = lower;
57
- lower = void 0;
58
- }
59
- if (upper !== void 0) {
60
- upper = toNumber(upper);
61
- upper = upper === upper ? upper : 0;
62
- }
63
- if (lower !== void 0) {
64
- lower = toNumber(lower);
65
- lower = lower === lower ? lower : 0;
66
- }
67
- return baseClamp(toNumber(number), lower, upper);
68
- }
69
- //#endregion
70
- //#region src/abort-signal.ts
71
- /**
72
- * Creates a new AbortController that aborts when any of the provided signals abort.
73
- * This is useful for combining multiple abort conditions (e.g., user cancellation, timeout, parent operation abort).
74
- *
75
- * @param signals - Array of AbortSignals to listen to. Undefined/null signals are ignored.
76
- * @returns A new AbortController that will abort if any input signal aborts
77
- *
78
- * @example
79
- * ```typescript
80
- * const userController = new AbortController()
81
- * const timeoutController = new AbortController()
82
- *
83
- * // Create a joined controller that aborts if either signal aborts
84
- * const joinedController = createJoinedAbortController([
85
- * userController.signal,
86
- * timeoutController.signal
87
- * ])
88
- *
89
- * // Use the joined signal in operations
90
- * await fetch('/api/data', { signal: joinedController.signal })
91
- * ```
92
- */
93
- function createJoinedAbortController(signals) {
94
- const controller = new AbortController();
95
- const validSignals = signals.filter((signal) => signal != null);
96
- if (validSignals.length === 0) return controller;
97
- for (const signal of validSignals) if (signal.aborted) {
98
- controller.abort(signal.reason);
99
- return controller;
100
- }
101
- const abortListeners = [];
102
- for (const signal of validSignals) {
103
- const listener = () => {
104
- controller.abort(signal.reason);
105
- cleanup();
106
- };
107
- signal.addEventListener("abort", listener);
108
- abortListeners.push(() => signal.removeEventListener("abort", listener));
109
- }
110
- const cleanup = () => {
111
- abortListeners.forEach((removeListener) => removeListener());
112
- };
113
- controller.signal.addEventListener("abort", cleanup, { once: true });
114
- return controller;
115
- }
116
- //#endregion
117
- //#region src/runtime/generate.ts
118
- const RESPONSE_LENGTH_BUFFER = {
119
- MIN_TOKENS: 1e3,
120
- MAX_TOKENS: 16e3,
121
- PERCENTAGE: .1
122
- };
123
- /** Maximum time to wait between two stream chunks before considering the stream stalled. */
124
- const STREAM_INACTIVITY_TIMEOUT = 18e4;
125
- /** A syntactically valid prefix must not execute when generation did not finish successfully. */
126
- const assertSuccessfulGeneration = (metadata) => {
127
- if (metadata.provider === "unknown") throw new CognitiveError("LLM generation failed: received error metadata with unknown provider");
128
- if (metadata.stopReason === "max_tokens" || metadata.stopReason === "content_filter") throw new CognitiveError(`LLM generation did not complete: stopReason=${metadata.stopReason}`);
129
- };
130
- const getModelOutputLimit = (inputLength) => clamp(RESPONSE_LENGTH_BUFFER.PERCENTAGE * inputLength, RESPONSE_LENGTH_BUFFER.MIN_TOKENS, RESPONSE_LENGTH_BUFFER.MAX_TOKENS);
131
- const generateCode = async ({ iteration, ctx, cognitive, controller, metadata, onSend, onSendDelta, onRunStart }) => {
132
- const startedAt = Date.now();
133
- const traces = iteration.traces;
134
- const modelRef = Array.isArray(iteration.model) ? iteration.model[0] : iteration.model;
135
- const model = await cognitive.getModelDetails(modelRef).catch((thrown) => {
136
- throw new CognitiveError(`Failed to fetch model details for model "${modelRef}": ${getErrorMessage(thrown)}`);
137
- });
138
- let modelLimit = Math.max(model.input.maxTokens, 8e3);
139
- if (ctx.maxTokens) modelLimit = Math.min(ctx.maxTokens, modelLimit);
140
- const responseLengthBuffer = getModelOutputLimit(modelLimit);
141
- if (iteration.tokens) iteration.tokens.limit = modelLimit;
142
- let messages;
143
- try {
144
- messages = truncateWrappedContent({
145
- messages: iteration.messages,
146
- tokenLimit: modelLimit - responseLengthBuffer,
147
- throwOnFailure: true
148
- }).filter((x) => typeof x.content !== "string" || x.content.trim().length > 0);
149
- } catch (thrown) {
150
- const cap = ctx.maxTokens ? ` (context window capped at ${modelLimit} tokens by options.maxTokens — consider raising or removing it)` : ` (model context window: ${modelLimit} tokens)`;
151
- throw new CognitiveError(`The prompt does not fit in the context window${cap}: ${getErrorMessage(thrown)}`);
152
- }
153
- iteration.messages = messages;
154
- traces.push({
155
- type: "llm_call_started",
156
- started_at: startedAt,
157
- ended_at: startedAt,
158
- model: model.id
159
- });
160
- const hasAudioParts = messages.some((message) => Array.isArray(message.content) && message.content.some((part) => part.type === "audio"));
161
- const input = {
162
- model: iteration.model,
163
- temperature: iteration.temperature,
164
- responseFormat: "text",
165
- reasoningEffort: iteration.reasoningEffort,
166
- messages,
167
- stopSequences: ctx.version.getStopTokens(),
168
- meta: metadata ? { metadata } : void 0,
169
- options: hasAudioParts ? { transcriptionModel: ctx.transcriptionModel ?? "fast" } : void 0
170
- };
171
- let responseMetadata;
172
- let raw;
173
- let assistantResponse;
174
- /** Milliseconds between the stream request and the first/last streamed tokens. */
175
- let timeToFirstToken;
176
- let timeToLastToken;
177
- const midStreamFallback = ctx.midStreamFallback === true;
178
- let attempt = 1;
179
- const messageMetadata = (itemId) => ({
180
- iterationId: iteration.id,
181
- id: midStreamFallback ? `${iteration.id}:${attempt}:${itemId}` : `${iteration.id}:${itemId}`
182
- });
183
- const preview = async (delta) => {
184
- try {
185
- await (onSendDelta === null || onSendDelta === void 0 ? void 0 : onSendDelta(delta));
186
- } catch (err) {
187
- if (delta.restart) throw new CognitiveError(`LLM stream restart handler failed: ${getErrorMessage(err)}`);
188
- }
189
- };
190
- const liveItems = /* @__PURE__ */ new Map();
191
- const liveContent = /* @__PURE__ */ new Map();
192
- let codeGenerationTraced = false;
193
- let runCompleted = false;
194
- let completions = [];
195
- const dispatchSends = async (events) => {
196
- for (const event of events) if (event.type === "item-start") {
197
- liveItems.set(event.item.id, event.item);
198
- if (event.item.kind === "run" && !codeGenerationTraced) {
199
- codeGenerationTraced = true;
200
- traces.push({
201
- type: "code_generation_started",
202
- started_at: Date.now()
203
- });
204
- onRunStart === null || onRunStart === void 0 || onRunStart();
205
- }
206
- } else if (event.type === "body-delta" && onSendDelta) {
207
- const item = liveItems.get(event.itemId);
208
- if ((item === null || item === void 0 ? void 0 : item.kind) !== "send" || runCompleted) continue;
209
- const content = (liveContent.get(item.id) ?? "") + event.delta;
210
- liveContent.set(item.id, content);
211
- const delta = {
212
- restart: false,
213
- ...messageMetadata(item.id),
214
- component: item.name,
215
- props: item.props,
216
- delta: event.delta,
217
- content
218
- };
219
- await preview(delta);
220
- } else if (event.type === "item-complete") {
221
- if (event.item.kind === "send" && onSend && !runCompleted) {
222
- const send = {
223
- name: event.item.name,
224
- props: event.item.props,
225
- body: event.item.body
226
- };
227
- const metadata = messageMetadata(event.item.id);
228
- completions.push(() => onSend(send, metadata));
229
- } else if (event.item.kind === "run" && event.item.status === "complete" && !runCompleted) runCompleted = true;
230
- }
231
- };
232
- if (typeof cognitive.generateTextStream === "function") {
233
- let parser = new ResponseParser();
234
- const streamController = createJoinedAbortController([controller.signal]);
235
- const requestedAt = Date.now();
236
- const stream = cognitive.generateTextStream({
237
- ...input,
238
- ...ctx.maxTimeToFirstToken || midStreamFallback ? { options: {
239
- ...input.options,
240
- ...ctx.maxTimeToFirstToken ? { maxTimeToFirstToken: ctx.maxTimeToFirstToken } : {},
241
- ...midStreamFallback ? { midStreamFallback: true } : {}
242
- } } : {}
243
- }, { signal: streamController.signal });
244
- const inactivityTimeout = Math.max(STREAM_INACTIVITY_TIMEOUT, ctx.maxTimeToFirstToken ?? 0);
245
- const nextChunk = async () => {
246
- let timer;
247
- const stalled = new Promise((_, reject) => {
248
- timer = setTimeout(() => {
249
- streamController.abort("LLM stream stalled");
250
- reject(/* @__PURE__ */ new Error(`LLM stream stalled: no data received for ${inactivityTimeout}ms`));
251
- }, inactivityTimeout);
252
- });
253
- try {
254
- return await Promise.race([stream.next(), stalled]);
255
- } finally {
256
- clearTimeout(timer);
257
- }
258
- };
259
- raw = "";
260
- let streamCompleted = false;
261
- let accepted = false;
262
- try {
263
- while (true) {
264
- var _chunk$value, _chunk$value2, _chunk$value3, _chunk$value4;
265
- let chunk;
266
- try {
267
- chunk = await nextChunk();
268
- } catch (thrown) {
269
- throw new CognitiveError(`LLM generation failed: ${getErrorMessage(thrown)}`);
270
- }
271
- if (chunk.done) {
272
- streamCompleted = true;
273
- break;
274
- }
275
- if (((_chunk$value = chunk.value) === null || _chunk$value === void 0 ? void 0 : _chunk$value.restart) && !midStreamFallback) {
276
- streamController.abort("Unexpected LLM stream restart");
277
- throw new CognitiveError("LLM stream restarted without options.midStreamFallback enabled");
278
- }
279
- if ((_chunk$value2 = chunk.value) === null || _chunk$value2 === void 0 ? void 0 : _chunk$value2.restart) {
280
- traces.push({
281
- type: "llm_call_restarted",
282
- started_at: Date.now(),
283
- ...chunk.value.restart
284
- });
285
- raw = "";
286
- completions = [];
287
- accepted = false;
288
- parser = new ResponseParser();
289
- liveItems.clear();
290
- liveContent.clear();
291
- codeGenerationTraced = false;
292
- runCompleted = false;
293
- responseMetadata = void 0;
294
- attempt = chunk.value.restart.attempt;
295
- await preview({
296
- ...chunk.value.restart,
297
- restart: true,
298
- iterationId: iteration.id
299
- });
300
- timeToFirstToken = void 0;
301
- timeToLastToken = void 0;
302
- continue;
303
- }
304
- if ((_chunk$value3 = chunk.value) === null || _chunk$value3 === void 0 ? void 0 : _chunk$value3.metadata) responseMetadata = chunk.value.metadata;
305
- const delta = (_chunk$value4 = chunk.value) === null || _chunk$value4 === void 0 ? void 0 : _chunk$value4.output;
306
- if (!delta) continue;
307
- timeToLastToken = Date.now() - requestedAt;
308
- timeToFirstToken ??= timeToLastToken;
309
- raw += delta;
310
- await dispatchSends(parser.push(delta));
311
- }
312
- if (!responseMetadata) throw new CognitiveError("LLM streaming completed without metadata");
313
- assertSuccessfulGeneration(responseMetadata);
314
- await dispatchSends(parser.finish(responseMetadata.stopReason));
315
- if (parser.valid) {
316
- for (const complete of completions) await complete();
317
- accepted = true;
318
- }
319
- assistantResponse = toParsedAssistantResponse(parser.items, raw, parser.diagnostics);
320
- } catch (error) {
321
- parser.finish();
322
- const usage = (responseMetadata === null || responseMetadata === void 0 ? void 0 : responseMetadata.usage) ?? {
323
- inputTokens: 0,
324
- outputTokens: 0,
325
- inputCost: 0,
326
- outputCost: 0
327
- };
328
- iteration.llm = {
329
- started_at: startedAt,
330
- ended_at: Date.now(),
331
- status: "error",
332
- cached: (responseMetadata === null || responseMetadata === void 0 ? void 0 : responseMetadata.cached) ?? false,
333
- tokens: usage.inputTokens + usage.outputTokens,
334
- spend: (responseMetadata === null || responseMetadata === void 0 ? void 0 : responseMetadata.cost) ?? usage.inputCost + usage.outputCost,
335
- output: raw,
336
- diagnostics: parser.diagnostics,
337
- model: (responseMetadata === null || responseMetadata === void 0 ? void 0 : responseMetadata.model) ?? model.id,
338
- time_to_first_token: timeToFirstToken,
339
- time_to_last_token: timeToLastToken,
340
- usage
341
- };
342
- throw error;
343
- } finally {
344
- streamController.abort("LLM stream closed");
345
- if (!streamCompleted) stream.return(void 0).catch((err) => {});
346
- if (!accepted && liveContent.size) await preview({
347
- restart: true,
348
- iterationId: iteration.id,
349
- attempt: attempt + 1,
350
- fromModel: model.id,
351
- toModel: model.id,
352
- reason: "invalid or incomplete response envelope"
353
- });
354
- }
355
- controller.signal.throwIfAborted();
356
- } else {
357
- const response = await cognitive.generateText(input, { signal: controller.signal }).catch((thrown) => {
358
- throw new CognitiveError(`LLM generation failed: ${getErrorMessage(thrown)}`);
359
- });
360
- if (response.error) throw new CognitiveError(`LLM generation failed: ${response.error}`);
361
- if (!response.output) throw new CognitiveError("LLM did not return any text output");
362
- responseMetadata = response.metadata;
363
- assertSuccessfulGeneration(responseMetadata);
364
- raw = response.output;
365
- assistantResponse = ctx.version.parseAssistantResponse(raw, responseMetadata.stopReason);
366
- for (const [index, send] of assistantResponse.sends.entries()) await (onSend === null || onSend === void 0 ? void 0 : onSend(send, messageMetadata(`send-${index}`)));
367
- }
368
- iteration.code = assistantResponse.code;
369
- iteration.sends = assistantResponse.sends;
370
- iteration.next = assistantResponse.next;
371
- const usage = responseMetadata.usage;
372
- iteration.llm = {
373
- cached: responseMetadata.cached || false,
374
- ended_at: Date.now(),
375
- started_at: startedAt,
376
- status: "success",
377
- tokens: usage.inputTokens + usage.outputTokens,
378
- spend: responseMetadata.cost ?? usage.inputCost + usage.outputCost,
379
- output: assistantResponse.raw,
380
- diagnostics: assistantResponse.diagnostics,
381
- model: `${responseMetadata.provider}:${responseMetadata.model}`,
382
- time_to_first_token: timeToFirstToken,
383
- time_to_last_token: timeToLastToken,
384
- usage: {
385
- inputTokens: usage.inputTokens,
386
- inputCost: usage.inputCost,
387
- outputTokens: usage.outputTokens,
388
- outputCost: usage.outputCost
389
- }
390
- };
391
- if (iteration.tokens) {
392
- iteration.tokens.input = usage.inputTokens;
393
- iteration.tokens.output = usage.outputTokens;
394
- iteration.tokens.total = usage.inputTokens + usage.outputTokens;
395
- }
396
- traces.push({
397
- type: "llm_call_success",
398
- started_at: startedAt,
399
- ended_at: iteration.llm.ended_at,
400
- model: iteration.llm.model,
401
- code: iteration.code ?? ""
402
- });
403
- };
404
- //#endregion
405
- //#region src/runtime/interpret-result.ts
406
- const interpretVMResult = async ({ iteration, result, controller, startedAt, onExit }) => {
407
- Object.assign(iteration.variables, Object.fromEntries(Object.entries(result.variables ?? {}).filter(([, value]) => value !== "[[non-primitive]]")));
408
- if (result.error && result.error instanceof InvalidCodeError) {
409
- iteration.end({
410
- type: "invalid_code_error",
411
- invalid_code_error: { message: result.error.message }
412
- });
413
- return;
414
- }
415
- iteration.traces.push({
416
- type: "code_execution",
417
- lines_executed: result.lines_executed ?? 0,
418
- started_at: startedAt,
419
- ended_at: Date.now()
420
- });
421
- if (controller.signal.aborted) {
422
- iteration.end({
423
- type: "aborted",
424
- aborted: { reason: controller.signal.reason ?? "The operation was aborted" }
425
- });
426
- return;
427
- }
428
- if (result.error && result.error instanceof CodeExecutionError) {
429
- iteration.end({
430
- type: "execution_error",
431
- execution_error: {
432
- message: result.error.message,
433
- stack: cleanStackTrace(result.error.stacktrace ?? result.error.stack ?? "No stack trace available")
434
- }
435
- });
436
- return;
437
- }
438
- if (!result.success) {
439
- var _result$error;
440
- iteration.end({
441
- type: "execution_error",
442
- execution_error: {
443
- message: (result === null || result === void 0 || (_result$error = result.error) === null || _result$error === void 0 ? void 0 : _result$error.message) ?? "Unknown error occurred",
444
- stack: cleanStackTrace(result.error.stack ?? "No stack trace available")
445
- }
446
- });
447
- return;
448
- }
449
- if (result.signal instanceof ThinkSignal) {
450
- iteration.end({
451
- type: "thinking_requested",
452
- thinking_requested: {
453
- variables: result.signal.context,
454
- reason: result.signal.reason,
455
- metadata: result.signal.metadata,
456
- interrupted: true
457
- }
458
- });
459
- return;
460
- }
461
- if (result.signal instanceof SnapshotSignal) {
462
- iteration.end({
463
- type: "callback_requested",
464
- callback_requested: { signal: result.signal }
465
- });
466
- return;
467
- }
468
- const returnValue = result.return_value;
469
- const codeRequestsInspection = hasTopLevelReturn(iteration.code ?? "");
470
- if (iteration.next && !codeRequestsInspection) {
471
- await applyNextExit({
472
- iteration,
473
- controller,
474
- onExit
475
- });
476
- return;
477
- }
478
- const localVariables = Object.fromEntries(Object.entries(result.variables ?? {}).filter(([, value]) => value !== "[[non-primitive]]"));
479
- iteration.end({
480
- type: "thinking_requested",
481
- thinking_requested: {
482
- reason: iteration.next ? `Code execution completed. The ■next=${iteration.next.name} exit was NOT applied because your code returns a value — inspect it below, then end your next response with ■next.` : "Code execution completed",
483
- variables: returnValue !== void 0 ? returnValue : Object.keys(localVariables).length ? localVariables : iteration.variables
484
- }
485
- });
486
- };
487
- /**
488
- * Applies the `■next=<exit>` block of the current iteration: validates the exit
489
- * name and props against the available exits, runs the onExit hook, and ends
490
- * the iteration with exit_success or exit_error.
491
- */
492
- const applyNextExit = async ({ iteration, controller, onExit }) => {
493
- const next = iteration.next;
494
- if (!next) throw new Error("applyNextExit called without a ■next block on the iteration");
495
- const attempt = (props) => parseExit(Object.keys(props).length ? {
496
- action: next.name,
497
- value: props
498
- } : { action: next.name }, iteration.exits);
499
- let parsedExit = attempt(next.props);
500
- const keys = Object.keys(next.props);
501
- if (!parsedExit.success && keys.length === 1 && [
502
- "props",
503
- "value",
504
- "data"
505
- ].includes(keys[0])) {
506
- const inner = next.props[keys[0]];
507
- if (inner && typeof inner === "object" && !Array.isArray(inner)) {
508
- const unwrapped = attempt(inner);
509
- if (unwrapped.success) parsedExit = unwrapped;
510
- }
511
- }
512
- const returnValue = {
513
- action: next.name,
514
- value: parsedExit.success ? parsedExit.value : next.props
515
- };
516
- if (!parsedExit.success) {
517
- var _parsedExit$validatio;
518
- const details = (_parsedExit$validatio = parsedExit.validationErrors) === null || _parsedExit$validatio === void 0 ? void 0 : _parsedExit$validatio.join("; ");
519
- const error = [parsedExit.error.split("\n")[0], details].filter(Boolean).join(". ");
520
- iteration.end({
521
- type: "exit_error",
522
- exit_error: {
523
- exit: next.name,
524
- message: `${error}. Received props: ${JSON.stringify(next.props)}. Choose an exit from <exits> and supply its required props as ONE JSON object on the SAME LINE as ■next=${next.name}. Do not use a JavaScript return statement or wrap the fields in "value" or "props".`,
525
- return_value: returnValue
526
- }
527
- });
528
- return;
529
- }
530
- const returnExit = parsedExit.exit;
531
- try {
532
- await (onExit === null || onExit === void 0 ? void 0 : onExit({
533
- exit: returnExit,
534
- result: parsedExit.value
535
- }, controller));
536
- } catch (err) {
537
- iteration.end({
538
- type: "exit_error",
539
- exit_error: {
540
- exit: returnExit.name,
541
- message: `Error executing exit ${returnExit.name}: ${getErrorMessage(err)}`,
542
- return_value: returnValue
543
- }
544
- });
545
- return;
546
- }
547
- iteration.end({
548
- type: "exit_success",
549
- exit_success: {
550
- exit_name: returnExit.name,
551
- return_value: parsedExit.value
552
- }
553
- });
554
- };
555
- //#endregion
556
- //#region src/runtime/utils.ts
557
- const finalizeIteration = async ({ iteration, status, controller, onIterationEnd }) => {
558
- if (status) iteration.end(status);
559
- if (iteration.status.type === "pending") return;
560
- try {
561
- await (onIterationEnd === null || onIterationEnd === void 0 ? void 0 : onIterationEnd(iteration, controller));
562
- } catch (err) {
563
- console.error(err);
564
- }
565
- };
566
- //#endregion
567
- //#region src/runtime/tool-wrapper.ts
568
- const SLOW_TOOL_WARNING = ms("15s");
569
- function wrapTool({ chat, tool, traces, object, iteration, beforeHook, afterHook, controller }) {
570
- const getToolInput = (input) => tool.zInput.safeParse(input).data ?? input;
571
- return async function(input) {
572
- const toolCallId = `tcall_${ulid()}`;
573
- const originalInput = input;
574
- let effectiveInput = input;
575
- const alertSlowTool = setTimeout(() => traces.push({
576
- type: "tool_slow",
577
- tool_name: tool.name,
578
- tool_call_id: toolCallId,
579
- started_at: Date.now(),
580
- input: getToolInput(originalInput),
581
- object,
582
- duration: SLOW_TOOL_WARNING
583
- }), SLOW_TOOL_WARNING);
584
- const toolStart = Date.now();
585
- let output;
586
- let error;
587
- let success = true;
588
- let signalToThrow;
589
- const pushToolCallTrace = () => {
590
- traces.push({
591
- type: "tool_call",
592
- tool_call_id: toolCallId,
593
- started_at: toolStart,
594
- ended_at: Date.now(),
595
- tool_name: tool.name,
596
- object,
597
- input: getToolInput(originalInput),
598
- output,
599
- error,
600
- success
601
- });
602
- };
603
- const handleSignals = async (err) => {
604
- if (output === err) return true;
605
- if (err instanceof SnapshotSignal) {
606
- err.toolCall = {
607
- name: tool.name,
608
- inputSchema: tool.input,
609
- outputSchema: tool.output,
610
- input: originalInput
611
- };
612
- err.message = Signals.serializeError(err);
613
- }
614
- if (err instanceof ThinkSignal) {
615
- signalToThrow = err;
616
- traces.push({
617
- type: "think_signal",
618
- started_at: Date.now(),
619
- line: 0,
620
- ended_at: Date.now()
621
- });
622
- success = true;
623
- output = err;
624
- const afterRes = await (afterHook === null || afterHook === void 0 ? void 0 : afterHook({
625
- iteration,
626
- tool,
627
- input: originalInput,
628
- output,
629
- controller,
630
- object,
631
- toolCallId
632
- }));
633
- if (typeof (afterRes === null || afterRes === void 0 ? void 0 : afterRes.output) !== "undefined") output = afterRes.output;
634
- return true;
635
- }
636
- return false;
637
- };
638
- try {
639
- const beforeRes = await (beforeHook === null || beforeHook === void 0 ? void 0 : beforeHook({
640
- iteration,
641
- tool,
642
- input: effectiveInput,
643
- controller,
644
- object,
645
- toolCallId
646
- }));
647
- if (typeof (beforeRes === null || beforeRes === void 0 ? void 0 : beforeRes.input) !== "undefined") effectiveInput = beforeRes.input;
648
- output = await tool.execute(effectiveInput, {
649
- callId: toolCallId,
650
- iterationId: iteration.id
651
- }, chat);
652
- const afterRes = await (afterHook === null || afterHook === void 0 ? void 0 : afterHook({
653
- iteration,
654
- tool,
655
- input: effectiveInput,
656
- output,
657
- controller,
658
- object,
659
- toolCallId
660
- }));
661
- if (typeof (afterRes === null || afterRes === void 0 ? void 0 : afterRes.output) !== "undefined") output = afterRes.output;
662
- } catch (err) {
663
- if (!await handleSignals(err)) {
664
- success = false;
665
- error = err;
666
- }
667
- } finally {
668
- clearTimeout(alertSlowTool);
669
- pushToolCallTrace();
670
- }
671
- if (!success) throw error;
672
- if (signalToThrow) throw signalToThrow;
673
- return output;
674
- };
675
- }
676
- //#endregion
677
- //#region src/runtime/vm-context.ts
678
- const buildVMContext = ({ ctx, iteration, controller, onBeforeTool, onAfterTool }) => {
679
- const traces = iteration.traces;
680
- const vmContext = { ...stripInvalidIdentifiers(iteration.variables) };
681
- for (const obj of iteration.objects) {
682
- const internalValues = {};
683
- const instance = {};
684
- for (const { name, value, writable, type } of obj.properties ?? []) {
685
- internalValues[name] = value;
686
- const initialValue = value;
687
- const schema = type ?? z.any();
688
- Object.defineProperty(instance, name, {
689
- enumerable: true,
690
- configurable: true,
691
- get() {
692
- return internalValues[name];
693
- },
694
- set(value) {
695
- if (isEqual(value, internalValues[name])) return;
696
- if (!writable) throw new AssignmentError(`Property ${obj.name}.${name} is read-only and cannot be modified`);
697
- const parsed = schema.safeParse(value);
698
- if (!parsed.success) throw new AssignmentError(`Invalid value for Object property ${obj.name}.${name}: ${getErrorMessage(parsed.error)}`);
699
- internalValues[name] = parsed.data;
700
- traces.push({
701
- type: "property",
702
- started_at: Date.now(),
703
- object: obj.name,
704
- property: name,
705
- value: parsed.data
706
- });
707
- iteration.trackMutation({
708
- object: obj.name,
709
- property: name,
710
- before: initialValue,
711
- after: parsed.data
712
- });
713
- }
714
- });
715
- }
716
- for (const tool of obj.tools ?? []) instance[tool.name] = wrapTool({
717
- chat: ctx.chat,
718
- tool,
719
- traces,
720
- object: obj.name,
721
- iteration,
722
- beforeHook: onBeforeTool,
723
- afterHook: onAfterTool,
724
- controller
725
- });
726
- Object.preventExtensions(instance);
727
- Object.seal(instance);
728
- vmContext[obj.name] = instance;
729
- }
730
- for (const tool of iteration.tools) {
731
- const wrapped = wrapTool({
732
- chat: ctx.chat,
733
- tool,
734
- traces,
735
- iteration,
736
- beforeHook: onBeforeTool,
737
- afterHook: onAfterTool,
738
- controller
739
- });
740
- for (const key of [tool.name, ...tool.aliases ?? []]) vmContext[key] = wrapped;
741
- }
742
- return vmContext;
743
- };
744
- //#endregion
745
- //#region src/runtime/execute.ts
746
- const executeContext = async (props) => {
747
- await init();
748
- const result = await executeContextInternal(props);
749
- try {
750
- var _result$context$chat, _result$context$chat$;
751
- (_result$context$chat = result.context.chat) === null || _result$context$chat === void 0 || (_result$context$chat$ = _result$context$chat.onExecutionDone) === null || _result$context$chat$ === void 0 || _result$context$chat$.call(_result$context$chat, result);
752
- } catch (err) {}
753
- return result;
754
- };
755
- const executeContextInternal = async (props) => {
756
- var _props$options, _props$options2, _props$options3, _props$options4, _props$options5, _props$options6;
757
- const controller = createJoinedAbortController([props.signal]);
758
- const { onIterationStart, onIterationEnd, onTrace, onExit, onBeforeExecution, onAfterTool, onBeforeTool } = props;
759
- const client = props.client ?? new Client();
760
- const cognitive = Cognitive.isCognitiveClient(client) || _CustomModelClient.isCustomClient(client) ? client : new Cognitive({ client });
761
- const ctx = new Context({
762
- chat: props.chat,
763
- instructions: props.instructions,
764
- examples: props.examples,
765
- objects: props.objects,
766
- tools: props.tools,
767
- loop: (_props$options = props.options) === null || _props$options === void 0 ? void 0 : _props$options.loop,
768
- timeout: (_props$options2 = props.options) === null || _props$options2 === void 0 ? void 0 : _props$options2.timeout,
769
- maxTokens: (_props$options3 = props.options) === null || _props$options3 === void 0 ? void 0 : _props$options3.maxTokens,
770
- maxTimeToFirstToken: (_props$options4 = props.options) === null || _props$options4 === void 0 ? void 0 : _props$options4.maxTimeToFirstToken,
771
- midStreamFallback: (_props$options5 = props.options) === null || _props$options5 === void 0 ? void 0 : _props$options5.midStreamFallback,
772
- transcriptionModel: (_props$options6 = props.options) === null || _props$options6 === void 0 ? void 0 : _props$options6.transcriptionModel,
773
- exits: props.exits,
774
- snapshot: props.snapshot,
775
- model: props.model,
776
- temperature: props.temperature,
777
- reasoningEffort: props.reasoningEffort
778
- });
779
- try {
780
- while (true) {
781
- if (ctx.iterations.length >= ctx.loop) return new ErrorExecutionResult(ctx, new LoopExceededError());
782
- const iteration = await ctx.nextIteration();
783
- const unsubscribeTrace = iteration.traces.onPush((traces) => {
784
- for (const trace of traces) onTrace === null || onTrace === void 0 || onTrace({
785
- trace,
786
- iteration: ctx.iterations.length,
787
- controller
788
- });
789
- });
790
- try {
791
- if ((await executeIterationStartHook({
792
- ctx,
793
- iteration,
794
- controller,
795
- onIterationStart,
796
- onIterationEnd
797
- })).type === "continue") continue;
798
- if (controller.signal.aborted) {
799
- await finalizeIteration({
800
- iteration,
801
- controller,
802
- onIterationEnd,
803
- status: {
804
- type: "aborted",
805
- aborted: { reason: controller.signal.reason ?? "The operation was aborted" }
806
- }
807
- });
808
- return new ErrorExecutionResult(ctx, controller.signal.reason ?? "The operation was aborted");
809
- }
810
- const iterationResult = await executeIterationWithErrorHandling({
811
- iteration,
812
- ctx,
813
- cognitive,
814
- controller,
815
- onExit,
816
- onBeforeExecution,
817
- onAfterTool,
818
- onBeforeTool,
819
- onIterationEnd,
820
- metadata: props.metadata
821
- });
822
- if (iterationResult.type === "return") return iterationResult.result;
823
- const selectedResult = transformIterationResult({
824
- ctx,
825
- iteration
826
- });
827
- if (selectedResult.type === "continue") continue;
828
- return selectedResult.result;
829
- } finally {
830
- try {
831
- unsubscribeTrace();
832
- } catch (err) {}
833
- }
834
- }
835
- } catch (error) {
836
- return new ErrorExecutionResult(ctx, error ?? "Unknown error");
837
- }
838
- };
839
- const executeIterationStartHook = async ({ ctx, iteration, controller, onIterationStart, onIterationEnd }) => {
840
- try {
841
- const hookRes = await (onIterationStart === null || onIterationStart === void 0 ? void 0 : onIterationStart(iteration, controller, ctx));
842
- if (hookRes) Object.assign(iteration, hookRes);
843
- return { type: "proceed" };
844
- } catch (err) {
845
- await finalizeIteration({
846
- iteration,
847
- controller,
848
- onIterationEnd,
849
- status: err instanceof ThinkSignal ? {
850
- type: "thinking_requested",
851
- thinking_requested: {
852
- variables: err.context,
853
- reason: err.reason
854
- }
855
- } : {
856
- type: "execution_error",
857
- execution_error: {
858
- message: `Error in onIterationStart hook: ${getErrorMessage(err)}`,
859
- stack: cleanStackTrace(err instanceof Error ? err.stack ?? "No stack trace available" : "No stack trace available")
860
- }
861
- }
862
- });
863
- return { type: "continue" };
864
- }
865
- };
866
- const executeIterationWithErrorHandling = async ({ iteration, ctx, cognitive, controller, onExit, onBeforeExecution, onAfterTool, onBeforeTool, onIterationEnd, metadata }) => {
867
- try {
868
- await executeIteration({
869
- iteration,
870
- ctx,
871
- cognitive,
872
- controller,
873
- onExit,
874
- onBeforeExecution,
875
- onAfterTool,
876
- onBeforeTool,
877
- metadata
878
- });
879
- await finalizeIteration({
880
- iteration,
881
- controller,
882
- onIterationEnd
883
- });
884
- return { type: "proceed" };
885
- } catch (err) {
886
- if (!(err instanceof CognitiveError)) {
887
- await finalizeIteration({
888
- iteration,
889
- controller,
890
- onIterationEnd,
891
- status: iteration.status.type === "pending" ? {
892
- type: "execution_error",
893
- execution_error: {
894
- message: "An unexpected error occurred: " + getErrorMessage(err),
895
- stack: cleanStackTrace(err instanceof Error ? err.stack ?? "No stack trace available" : "No stack trace available")
896
- }
897
- } : void 0
898
- });
899
- return { type: "proceed" };
900
- }
901
- if (!controller.signal.aborted) return {
902
- type: "return",
903
- result: new ErrorExecutionResult(ctx, err)
904
- };
905
- await finalizeIteration({
906
- iteration,
907
- controller,
908
- onIterationEnd,
909
- status: iteration.status.type === "pending" ? {
910
- type: "aborted",
911
- aborted: { reason: controller.signal.reason ?? "The operation was aborted" }
912
- } : void 0
913
- });
914
- return {
915
- type: "return",
916
- result: new ErrorExecutionResult(ctx, controller.signal.reason ?? "The operation was aborted")
917
- };
918
- }
919
- };
920
- const transformIterationResult = ({ ctx, iteration }) => {
921
- if (iteration.status.type === "exit_success") {
922
- const exitName = iteration.status.exit_success.exit_name;
923
- return {
924
- type: "return",
925
- result: new SuccessExecutionResult(ctx, {
926
- exit: iteration.exits.find((x) => x.name === exitName),
927
- result: iteration.status.exit_success.return_value
928
- })
929
- };
930
- }
931
- if (iteration.status.type === "callback_requested") return {
932
- type: "return",
933
- result: new PartialExecutionResult(ctx, iteration.status.callback_requested.signal, Snapshot.fromSignal(iteration.status.callback_requested.signal))
934
- };
935
- if (iteration.status.type === "aborted") return {
936
- type: "return",
937
- result: new ErrorExecutionResult(ctx, iteration.error ?? "The operation was aborted")
938
- };
939
- if (iteration.status.type === "thinking_requested" || iteration.status.type === "exit_error" || iteration.status.type === "execution_error" || iteration.status.type === "invalid_code_error") return { type: "continue" };
940
- return {
941
- type: "return",
942
- result: new ErrorExecutionResult(ctx, iteration.error ?? `Unknown error. Status: ${iteration.status.type}`)
943
- };
944
- };
945
- const executeIteration = async ({ iteration, ctx, cognitive, controller, onExit, onBeforeExecution, onBeforeTool, onAfterTool, metadata }) => {
946
- var _ctx$chat;
947
- const runCode = (code) => {
948
- const vmContext = buildVMContext({
949
- ctx,
950
- iteration,
951
- controller,
952
- onBeforeTool,
953
- onAfterTool
954
- });
955
- return runAsyncFunction(vmContext, code, iteration.traces, controller.signal, ctx.timeout).catch((err) => {
956
- return {
957
- success: false,
958
- error: err instanceof Error ? err : new Error(getErrorMessage(err)),
959
- lines_executed: [],
960
- traces: [],
961
- variables: {}
962
- };
963
- });
964
- };
965
- await generateCode({
966
- iteration,
967
- ctx,
968
- cognitive,
969
- controller,
970
- metadata,
971
- onRunStart: () => warmupVM(),
972
- onSend: async (send, messageMetadata) => {
973
- if (!ctx.chat) return;
974
- const sendStartedAt = Date.now();
975
- const registered = iteration.components.find((component) => component.definition.name.toLowerCase() === send.name);
976
- const children = send.body ? [send.body] : [];
977
- const component = registered ? registered.render(send.props, children) : createJsxComponent({
978
- type: send.name,
979
- props: send.props,
980
- children
981
- });
982
- try {
983
- await ctx.chat.handler(component, messageMetadata);
984
- } catch (err) {
985
- throw new Error(`Error while sending message (■send=${send.name}): ${getErrorMessage(err)}`);
986
- }
987
- iteration.traces.push({
988
- type: "yield",
989
- value: component,
990
- started_at: sendStartedAt,
991
- ended_at: Date.now()
992
- });
993
- },
994
- onSendDelta: ((_ctx$chat = ctx.chat) === null || _ctx$chat === void 0 ? void 0 : _ctx$chat.onMessageDelta) ? (delta) => ctx.chat.onMessageDelta(delta) : void 0
995
- });
996
- if (typeof onBeforeExecution === "function") try {
997
- const hookRes = await onBeforeExecution(iteration, controller);
998
- if (typeof (hookRes === null || hookRes === void 0 ? void 0 : hookRes.code) === "string" && hookRes.code.trim().length > 0) iteration.code = hookRes.code.trim();
999
- } catch (err) {
1000
- if (err instanceof ThinkSignal) {
1001
- iteration.end({
1002
- type: "thinking_requested",
1003
- thinking_requested: {
1004
- variables: err.context,
1005
- reason: err.reason
1006
- }
1007
- });
1008
- return;
1009
- }
1010
- iteration.end({
1011
- type: "execution_error",
1012
- execution_error: {
1013
- message: `Error in onBeforeExecution hook: ${getErrorMessage(err)}`,
1014
- stack: cleanStackTrace(err instanceof Error ? err.stack ?? "No stack trace available" : "No stack trace available")
1015
- }
1016
- });
1017
- return;
1018
- }
1019
- const traces = iteration.traces;
1020
- if (controller.signal.aborted) {
1021
- traces.push({
1022
- type: "abort_signal",
1023
- started_at: Date.now(),
1024
- reason: "The operation was aborted by user."
1025
- });
1026
- iteration.end({
1027
- type: "aborted",
1028
- aborted: { reason: controller.signal.reason ?? "The operation was aborted" }
1029
- });
1030
- return;
1031
- }
1032
- if (!iteration.code) {
1033
- var _iteration$llm;
1034
- if (iteration.next) {
1035
- await applyNextExit({
1036
- iteration,
1037
- controller,
1038
- onExit
1039
- });
1040
- return;
1041
- }
1042
- iteration.end({
1043
- type: "invalid_code_error",
1044
- invalid_code_error: { message: ((_iteration$llm = iteration.llm) === null || _iteration$llm === void 0 || (_iteration$llm = _iteration$llm.diagnostics) === null || _iteration$llm === void 0 || (_iteration$llm = _iteration$llm.find((diagnostic) => diagnostic.code === "invalid-envelope")) === null || _iteration$llm === void 0 ? void 0 : _iteration$llm.message) ?? "The response did not include a ■run block or a ■next exit. Reply with ■start, the required ■ blocks, and ■end." }
1045
- });
1046
- return;
1047
- }
1048
- const startedAt = Date.now();
1049
- const result = await runCode(iteration.code);
1050
- await interpretVMResult({
1051
- iteration,
1052
- result,
1053
- controller,
1054
- startedAt,
1055
- onExit
1056
- });
1057
- };
1058
- //#endregion
1059
- export { executeContext };