llmz 0.8.0 → 1.0.1

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