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