llmz 0.0.82 → 0.0.83

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 (68) hide show
  1. package/dist/{chunk-BJMZIH3M.js → chunk-34GJUS6U.js} +1047 -1047
  2. package/dist/{chunk-EQ4JDBE5.js → chunk-3T5IU7OU.js} +1 -1
  3. package/dist/{chunk-H5LCBNN6.cjs → chunk-52QTFQOB.cjs} +18 -18
  4. package/dist/{chunk-3NVOHQVY.js → chunk-5MAFEI2A.js} +4 -2
  5. package/dist/{chunk-HBKQKO3R.js → chunk-5Z2SJOGS.js} +3 -3
  6. package/dist/{chunk-OX3Y2Z5K.cjs → chunk-5ZF6NTYI.cjs} +3 -3
  7. package/dist/{chunk-TB6TCWEB.cjs → chunk-6KR6VRA6.cjs} +2 -2
  8. package/dist/{chunk-BXPPMP5X.cjs → chunk-73XYX76S.cjs} +14 -14
  9. package/dist/{chunk-VXQ2CZ7B.cjs → chunk-CQ3UJW76.cjs} +4 -4
  10. package/dist/{chunk-22DGAZXC.js → chunk-EFEF2AL7.js} +2 -2
  11. package/dist/{chunk-7XVBJQ53.cjs → chunk-HC2QZTVT.cjs} +1047 -1047
  12. package/dist/{chunk-HP3T6PF4.js → chunk-I2CZYEMH.js} +4 -4
  13. package/dist/{chunk-GFZCNSYW.cjs → chunk-JXD2KJVB.cjs} +23 -23
  14. package/dist/{chunk-YFG53MRF.js → chunk-MM2FLD3J.js} +1 -1
  15. package/dist/{chunk-DO7DN4FZ.js → chunk-NPVV3PHY.js} +1 -1
  16. package/dist/{chunk-5X5TDCMX.cjs → chunk-NQBIU5VM.cjs} +51 -51
  17. package/dist/{chunk-FQ4QCUTI.cjs → chunk-NWTG73QR.cjs} +10 -8
  18. package/dist/{chunk-DLV5FJ42.cjs → chunk-RLUTWFAA.cjs} +9 -9
  19. package/dist/{chunk-2UM7PFM3.js → chunk-VDTORFBP.js} +6 -6
  20. package/dist/{chunk-E74GBCTT.js → chunk-VLJA22PB.js} +2 -2
  21. package/dist/{chunk-E6UFMAW7.js → chunk-XTEHT2TK.js} +1 -1
  22. package/dist/{chunk-X24YFCFY.cjs → chunk-YCLKXXSF.cjs} +28 -28
  23. package/dist/{component-GVCCL5XU.js → component-2Z3KLWLX.js} +3 -3
  24. package/dist/component-NNHBHD72.cjs +18 -0
  25. package/dist/dual-modes-E5HGE35E.cjs +12 -0
  26. package/dist/dual-modes-VDFZH4YM.js +12 -0
  27. package/dist/{llmz-JTZUIYRP.js → execute-CJVHUFYI.js} +536 -420
  28. package/dist/{llmz-2HD6WKIP.cjs → execute-R6R3A42S.cjs} +545 -429
  29. package/dist/exit-GKF4EEMZ.js +8 -0
  30. package/dist/exit-VZASQELZ.cjs +8 -0
  31. package/dist/index.cjs +49 -49
  32. package/dist/index.d.ts +1 -1
  33. package/dist/index.js +22 -22
  34. package/dist/{jsx-RIPUQSAU.js → jsx-DDZ63UUF.js} +2 -2
  35. package/dist/jsx-ULPWX5TW.cjs +13 -0
  36. package/dist/runtime/execute.d.ts +3 -0
  37. package/dist/runtime/generate.d.ts +10 -0
  38. package/dist/runtime/interpret-result.d.ts +12 -0
  39. package/dist/runtime/interpret-result.test.d.ts +1 -0
  40. package/dist/runtime/tool-wrapper.d.ts +17 -0
  41. package/dist/runtime/tool-wrapper.test.d.ts +1 -0
  42. package/dist/{llmz.d.ts → runtime/types.d.ts} +12 -13
  43. package/dist/runtime/utils.d.ts +10 -0
  44. package/dist/runtime/vm-context.d.ts +12 -0
  45. package/dist/tool-4VK7TZV2.cjs +11 -0
  46. package/dist/tool-JZP3UN2F.js +11 -0
  47. package/dist/{truncator-HCWH5MJ3.js → truncator-IIRX3MB2.js} +3 -3
  48. package/dist/truncator-NWENJXMR.cjs +10 -0
  49. package/dist/typings-SHVPZ5AR.cjs +10 -0
  50. package/dist/{typings-FPFJ5D5R.js → typings-SKZSSL2M.js} +3 -3
  51. package/dist/utils-6MFSS4AT.cjs +41 -0
  52. package/dist/{utils-M5KFPSNZ.js → utils-SYJGSVFW.js} +4 -2
  53. package/dist/utils.d.ts +1 -0
  54. package/dist/{vm-KBBQ7U5L.js → vm-PYCKS77H.js} +3 -3
  55. package/dist/vm-VHB67FVH.cjs +10 -0
  56. package/package.json +1 -1
  57. package/dist/component-YMPMWSTU.cjs +0 -18
  58. package/dist/dual-modes-KOU5YJ3Q.cjs +0 -12
  59. package/dist/dual-modes-X7YHJBFJ.js +0 -12
  60. package/dist/exit-A34GODDB.cjs +0 -8
  61. package/dist/exit-J55SREDL.js +0 -8
  62. package/dist/jsx-XEUSAKAK.cjs +0 -13
  63. package/dist/tool-B3DJ3D2F.js +0 -11
  64. package/dist/tool-IWPZD5OY.cjs +0 -11
  65. package/dist/truncator-SOO6NHLD.cjs +0 -10
  66. package/dist/typings-O2FLZGQN.cjs +0 -10
  67. package/dist/utils-PWX3JPCF.cjs +0 -39
  68. package/dist/vm-R2XBIWE4.cjs +0 -10
@@ -1,6 +1,6 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
2
2
 
3
- var _chunkGFZCNSYWcjs = require('./chunk-GFZCNSYW.cjs');
3
+ var _chunkJXD2KJVBcjs = require('./chunk-JXD2KJVB.cjs');
4
4
 
5
5
 
6
6
 
@@ -8,11 +8,10 @@ var _chunkGFZCNSYWcjs = require('./chunk-GFZCNSYW.cjs');
8
8
 
9
9
 
10
10
 
11
- var _chunkX24YFCFYcjs = require('./chunk-X24YFCFY.cjs');
12
- require('./chunk-5X5TDCMX.cjs');
13
- require('./chunk-H5LCBNN6.cjs');
14
- require('./chunk-BXPPMP5X.cjs');
15
-
11
+ var _chunkYCLKXXSFcjs = require('./chunk-YCLKXXSF.cjs');
12
+ require('./chunk-NQBIU5VM.cjs');
13
+ require('./chunk-52QTFQOB.cjs');
14
+ require('./chunk-73XYX76S.cjs');
16
15
 
17
16
 
18
17
 
@@ -26,33 +25,27 @@ var _chunkPK72FAKDcjs = require('./chunk-PK72FAKD.cjs');
26
25
 
27
26
 
28
27
  var _chunkKIN7Y247cjs = require('./chunk-KIN7Y247.cjs');
29
- require('./chunk-DLV5FJ42.cjs');
30
- require('./chunk-VXQ2CZ7B.cjs');
31
- require('./chunk-OX3Y2Z5K.cjs');
32
-
33
-
34
- var _chunkTB6TCWEBcjs = require('./chunk-TB6TCWEB.cjs');
35
-
28
+ require('./chunk-RLUTWFAA.cjs');
29
+ require('./chunk-CQ3UJW76.cjs');
30
+ require('./chunk-5ZF6NTYI.cjs');
36
31
 
37
32
 
38
- var _chunkFQ4QCUTIcjs = require('./chunk-FQ4QCUTI.cjs');
33
+ var _chunk6KR6VRA6cjs = require('./chunk-6KR6VRA6.cjs');
39
34
 
40
35
 
41
36
 
42
37
 
38
+ var _chunkNWTG73QRcjs = require('./chunk-NWTG73QR.cjs');
43
39
 
44
- var _chunk7XVBJQ53cjs = require('./chunk-7XVBJQ53.cjs');
45
40
 
46
- // src/llmz.ts
47
- var _client = require('@botpress/client');
48
41
 
49
42
 
50
43
 
44
+ var _chunkHC2QZTVTcjs = require('./chunk-HC2QZTVT.cjs');
51
45
 
46
+ // src/runtime/execute.ts
47
+ var _client = require('@botpress/client');
52
48
  var _cognitive = require('@botpress/cognitive');
53
- var _zui = require('@bpinternal/zui');
54
- var _ms = require('ms'); var _ms2 = _interopRequireDefault(_ms);
55
- var _ulid = require('ulid');
56
49
 
57
50
  // src/abort-signal.ts
58
51
  function createJoinedAbortController(signals) {
@@ -83,172 +76,32 @@ function createJoinedAbortController(signals) {
83
76
  return controller;
84
77
  }
85
78
 
86
- // src/llmz.ts
87
- var getErrorMessage = (err) => err instanceof Error ? err.message : JSON.stringify(err);
88
- var SLOW_TOOL_WARNING = _ms2.default.call(void 0, "15s");
79
+ // src/runtime/generate.ts
89
80
  var RESPONSE_LENGTH_BUFFER = {
90
81
  MIN_TOKENS: 1e3,
91
82
  MAX_TOKENS: 16e3,
92
83
  PERCENTAGE: 0.1
93
84
  };
94
- var getModelOutputLimit = (inputLength) => _chunk7XVBJQ53cjs.clamp_default.call(void 0,
85
+ var getModelOutputLimit = (inputLength) => _chunkHC2QZTVTcjs.clamp_default.call(void 0,
95
86
  RESPONSE_LENGTH_BUFFER.PERCENTAGE * inputLength,
96
87
  RESPONSE_LENGTH_BUFFER.MIN_TOKENS,
97
88
  RESPONSE_LENGTH_BUFFER.MAX_TOKENS
98
89
  );
99
- var executeContext = async (props) => {
100
- var _a, _b;
101
- await _chunkFQ4QCUTIcjs.init.call(void 0, );
102
- const result = await _executeContext(props);
103
- try {
104
- (_b = (_a = result.context.chat) == null ? void 0 : _a.onExecutionDone) == null ? void 0 : _b.call(_a, result);
105
- } catch (e) {
106
- }
107
- return result;
108
- };
109
- var _executeContext = async (props) => {
110
- var _a, _b;
111
- const controller = createJoinedAbortController([props.signal]);
112
- const { onIterationStart, onIterationEnd, onTrace, onExit, onBeforeExecution, onAfterTool, onBeforeTool } = props;
113
- const client = _nullishCoalesce(props.client, () => ( new (0, _client.Client)()));
114
- const cognitive = _cognitive.Cognitive.isCognitiveClient(client) ? client : _cognitive.CognitiveBeta.isBetaClient(client) ? _cognitive.cognitiveFromBeta.call(void 0, client) : new (0, _cognitive.Cognitive)({ client, __experimental_beta: true });
115
- const cleanups = [];
116
- const ctx = new (0, _chunkX24YFCFYcjs.Context)({
117
- chat: props.chat,
118
- instructions: props.instructions,
119
- objects: props.objects,
120
- tools: props.tools,
121
- loop: (_a = props.options) == null ? void 0 : _a.loop,
122
- timeout: (_b = props.options) == null ? void 0 : _b.timeout,
123
- exits: props.exits,
124
- snapshot: props.snapshot,
125
- model: props.model,
126
- temperature: props.temperature,
127
- reasoningEffort: props.reasoningEffort
128
- });
129
- try {
130
- while (true) {
131
- if (ctx.iterations.length >= ctx.loop) {
132
- return new (0, _chunkX24YFCFYcjs.ErrorExecutionResult)(ctx, new (0, _chunkPK72FAKDcjs.LoopExceededError)());
133
- }
134
- const iteration = await ctx.nextIteration();
135
- try {
136
- await executeOnIterationStartHook({
137
- iteration,
138
- ctx,
139
- onIterationStart,
140
- controller,
141
- onIterationEnd
142
- });
143
- } catch (err) {
144
- if (err instanceof _chunkPK72FAKDcjs.ThinkSignal) {
145
- continue;
146
- }
147
- }
148
- if (controller.signal.aborted) {
149
- iteration.end({
150
- type: "aborted",
151
- aborted: {
152
- reason: _nullishCoalesce(controller.signal.reason, () => ( "The operation was aborted"))
153
- }
154
- });
155
- return new (0, _chunkX24YFCFYcjs.ErrorExecutionResult)(ctx, _nullishCoalesce(controller.signal.reason, () => ( "The operation was aborted")));
156
- }
157
- cleanups.push(
158
- iteration.traces.onPush((traces) => {
159
- for (const trace of traces) {
160
- onTrace == null ? void 0 : onTrace({ trace, iteration: ctx.iterations.length });
161
- }
162
- })
163
- );
164
- try {
165
- await executeIteration({
166
- iteration,
167
- ctx,
168
- cognitive,
169
- controller,
170
- onExit,
171
- onBeforeExecution,
172
- onAfterTool,
173
- onBeforeTool
174
- });
175
- } catch (err) {
176
- if (err instanceof _chunkPK72FAKDcjs.CognitiveError) {
177
- return new (0, _chunkX24YFCFYcjs.ErrorExecutionResult)(ctx, err);
178
- }
179
- iteration.end({
180
- type: "execution_error",
181
- execution_error: {
182
- message: "An unexpected error occurred: " + getErrorMessage(err),
183
- stack: _chunkKIN7Y247cjs.cleanStackTrace.call(void 0, _nullishCoalesce(err.stack, () => ( "No stack trace available")))
184
- }
185
- });
186
- }
187
- try {
188
- await (onIterationEnd == null ? void 0 : onIterationEnd(iteration, controller));
189
- } catch (err) {
190
- console.error(err);
191
- }
192
- if (iteration.status.type === "exit_success") {
193
- const exitName = iteration.status.exit_success.exit_name;
194
- return new (0, _chunkX24YFCFYcjs.SuccessExecutionResult)(ctx, {
195
- exit: iteration.exits.find((x) => x.name === exitName),
196
- result: iteration.status.exit_success.return_value
197
- });
198
- }
199
- if (iteration.status.type === "callback_requested") {
200
- return new (0, _chunkX24YFCFYcjs.PartialExecutionResult)(
201
- ctx,
202
- iteration.status.callback_requested.signal,
203
- _chunkX24YFCFYcjs.Snapshot.fromSignal(iteration.status.callback_requested.signal)
204
- );
205
- }
206
- if (iteration.status.type === "thinking_requested" || iteration.status.type === "exit_error" || iteration.status.type === "execution_error" || iteration.status.type === "invalid_code_error") {
207
- continue;
208
- }
209
- return new (0, _chunkX24YFCFYcjs.ErrorExecutionResult)(ctx, _nullishCoalesce(iteration.error, () => ( `Unknown error. Status: ${iteration.status.type}`)));
210
- }
211
- } catch (error) {
212
- return new (0, _chunkX24YFCFYcjs.ErrorExecutionResult)(ctx, _nullishCoalesce(error, () => ( "Unknown error")));
213
- } finally {
214
- for (const cleanup of cleanups) {
215
- try {
216
- cleanup();
217
- } catch (e2) {
218
- }
219
- }
220
- }
221
- };
222
- var executeIteration = async ({
223
- iteration,
224
- ctx,
225
- cognitive,
226
- controller,
227
- onExit,
228
- onBeforeExecution,
229
- onBeforeTool,
230
- onAfterTool
231
- }) => {
232
- var _a, _b, _c, _d;
233
- let startedAt = Date.now();
90
+ var generateCode = async ({ iteration, ctx, cognitive, controller }) => {
91
+ var _a, _b, _c;
92
+ const startedAt = Date.now();
234
93
  const traces = iteration.traces;
235
94
  const modelRef = Array.isArray(iteration.model) ? iteration.model[0] : iteration.model;
236
95
  const model = await cognitive.getModelDetails(modelRef).catch((thrown) => {
237
- throw new (0, _chunkPK72FAKDcjs.CognitiveError)(`Failed to fetch model details for model "${modelRef}": ${getErrorMessage(thrown)}`);
96
+ throw new (0, _chunkPK72FAKDcjs.CognitiveError)(`Failed to fetch model details for model "${modelRef}": ${_chunkNWTG73QRcjs.getErrorMessage.call(void 0, thrown)}`);
238
97
  });
239
98
  const modelLimit = Math.max(model.input.maxTokens, 8e3);
240
99
  const responseLengthBuffer = getModelOutputLimit(modelLimit);
241
- const messages = _chunkTB6TCWEBcjs.truncateWrappedContent.call(void 0, {
100
+ const messages = _chunk6KR6VRA6cjs.truncateWrappedContent.call(void 0, {
242
101
  messages: iteration.messages,
243
102
  tokenLimit: modelLimit - responseLengthBuffer,
244
103
  throwOnFailure: true
245
- }).filter(
246
- (x) => (
247
- // Filter out empty messages, as they are not valid inputs for the LLM
248
- // This can happen when a message is truncated and the content is empty
249
- typeof x.content !== "string" || x.content.trim().length > 0
250
- )
251
- );
104
+ }).filter((x) => typeof x.content !== "string" || x.content.trim().length > 0);
252
105
  iteration.messages = messages;
253
106
  traces.push({
254
107
  type: "llm_call_started",
@@ -259,8 +112,6 @@ var executeIteration = async ({
259
112
  const output = await cognitive.generateContent({
260
113
  signal: controller.signal,
261
114
  systemPrompt: (_a = messages.find((x) => x.role === "system")) == null ? void 0 : _a.content,
262
- // Validated upstream in Context (see context.ts isValidModel). Cast narrows
263
- // Models' `({} & string)` escape hatch down to cognitive's stricter InputModel.
264
115
  model: iteration.model,
265
116
  temperature: iteration.temperature,
266
117
  responseFormat: "text",
@@ -268,7 +119,7 @@ var executeIteration = async ({
268
119
  messages: messages.filter((x) => x.role !== "system"),
269
120
  stopSequences: ctx.version.getStopTokens()
270
121
  }).catch((thrown) => {
271
- throw new (0, _chunkPK72FAKDcjs.CognitiveError)(`LLM generation failed: ${getErrorMessage(thrown)}`);
122
+ throw new (0, _chunkPK72FAKDcjs.CognitiveError)(`LLM generation failed: ${_chunkNWTG73QRcjs.getErrorMessage.call(void 0, thrown)}`);
272
123
  });
273
124
  const out = typeof ((_c = (_b = output.output.choices) == null ? void 0 : _b[0]) == null ? void 0 : _c.content) === "string" ? output.output.choices[0].content : null;
274
125
  if (!out) {
@@ -276,31 +127,6 @@ var executeIteration = async ({
276
127
  }
277
128
  const assistantResponse = ctx.version.parseAssistantResponse(out);
278
129
  iteration.code = assistantResponse.code.trim();
279
- if (typeof onBeforeExecution === "function") {
280
- try {
281
- const hookRes = await onBeforeExecution(iteration, controller);
282
- if (typeof (hookRes == null ? void 0 : hookRes.code) === "string" && hookRes.code.trim().length > 0) {
283
- iteration.code = hookRes.code.trim();
284
- }
285
- } catch (err) {
286
- if (err instanceof _chunkPK72FAKDcjs.ThinkSignal) {
287
- return iteration.end({
288
- type: "thinking_requested",
289
- thinking_requested: {
290
- variables: err.context,
291
- reason: err.reason
292
- }
293
- });
294
- }
295
- return iteration.end({
296
- type: "execution_error",
297
- execution_error: {
298
- message: `Error in onBeforeExecution hook: ${getErrorMessage(err)}`,
299
- stack: _chunkKIN7Y247cjs.cleanStackTrace.call(void 0, _nullishCoalesce(err.stack, () => ( "No stack trace available")))
300
- }
301
- });
302
- }
303
- }
304
130
  iteration.llm = {
305
131
  cached: output.meta.cached || false,
306
132
  ended_at: Date.now(),
@@ -319,149 +145,63 @@ var executeIteration = async ({
319
145
  model: model.ref,
320
146
  code: iteration.code
321
147
  });
322
- const vmContext = { ..._chunkFQ4QCUTIcjs.stripInvalidIdentifiers.call(void 0, iteration.variables) };
323
- for (const obj of iteration.objects) {
324
- const internalValues = {};
325
- const instance = {};
326
- for (const { name, value, writable, type } of _nullishCoalesce(obj.properties, () => ( []))) {
327
- internalValues[name] = value;
328
- const initialValue = value;
329
- const schema = _nullishCoalesce(type, () => ( _zui.z.any()));
330
- Object.defineProperty(instance, name, {
331
- enumerable: true,
332
- configurable: true,
333
- get() {
334
- return internalValues[name];
335
- },
336
- set(value2) {
337
- if (_chunk7XVBJQ53cjs.isEqual_default.call(void 0, value2, internalValues[name])) {
338
- return;
339
- }
340
- if (!writable) {
341
- throw new (0, _chunkPK72FAKDcjs.AssignmentError)(`Property ${obj.name}.${name} is read-only and cannot be modified`);
342
- }
343
- if (value2 === internalValues[name]) {
344
- return;
345
- }
346
- const parsed = schema.safeParse(value2);
347
- if (!parsed.success) {
348
- throw new (0, _chunkPK72FAKDcjs.AssignmentError)(
349
- `Invalid value for Object property ${obj.name}.${name}: ${getErrorMessage(parsed.error)}`
350
- );
351
- }
352
- internalValues[name] = parsed.data;
353
- traces.push({
354
- type: "property",
355
- started_at: Date.now(),
356
- object: obj.name,
357
- property: name,
358
- value: parsed.data
359
- });
360
- iteration.trackMutation({ object: obj.name, property: name, before: initialValue, after: parsed.data });
361
- }
362
- });
363
- }
364
- for (const tool of _nullishCoalesce(obj.tools, () => ( []))) {
365
- instance[tool.name] = wrapTool({
366
- chat: ctx.chat,
367
- tool,
368
- traces,
369
- object: obj.name,
370
- iteration,
371
- beforeHook: onBeforeTool,
372
- afterHook: onAfterTool,
373
- controller
374
- });
375
- }
376
- Object.preventExtensions(instance);
377
- Object.seal(instance);
378
- vmContext[obj.name] = instance;
379
- }
380
- for (const tool of iteration.tools) {
381
- const wrapped = wrapTool({
382
- chat: ctx.chat,
383
- tool,
384
- traces,
385
- iteration,
386
- beforeHook: onBeforeTool,
387
- afterHook: onAfterTool,
388
- controller
389
- });
390
- for (const key of [tool.name, ..._nullishCoalesce(tool.aliases, () => ( []))]) {
391
- vmContext[key] = wrapped;
392
- }
393
- }
394
- if (controller.signal.aborted) {
395
- traces.push({
396
- type: "abort_signal",
397
- started_at: Date.now(),
398
- reason: "The operation was aborted by user."
399
- });
400
- return iteration.end({
401
- type: "aborted",
402
- aborted: {
403
- reason: _nullishCoalesce(controller.signal.reason, () => ( "The operation was aborted"))
404
- }
405
- });
406
- }
407
- startedAt = Date.now();
408
- const result = await _chunkGFZCNSYWcjs.runAsyncFunction.call(void 0,
409
- vmContext,
410
- iteration.code,
411
- traces,
412
- controller.signal,
413
- ctx.timeout
414
- ).catch((err) => {
415
- return {
416
- success: false,
417
- error: err,
418
- lines_executed: [],
419
- traces: [],
420
- variables: {}
421
- };
422
- });
148
+ };
149
+
150
+ // src/runtime/interpret-result.ts
151
+ var interpretVMResult = async ({
152
+ iteration,
153
+ result,
154
+ controller,
155
+ startedAt,
156
+ onExit
157
+ }) => {
158
+ var _a;
423
159
  if (result.error && result.error instanceof _chunkPK72FAKDcjs.InvalidCodeError) {
424
- return iteration.end({
160
+ iteration.end({
425
161
  type: "invalid_code_error",
426
162
  invalid_code_error: {
427
163
  message: result.error.message
428
164
  }
429
165
  });
166
+ return;
430
167
  }
431
- traces.push({
168
+ iteration.traces.push({
432
169
  type: "code_execution",
433
170
  lines_executed: _nullishCoalesce(result.lines_executed, () => ( 0)),
434
171
  started_at: startedAt,
435
172
  ended_at: Date.now()
436
173
  });
437
174
  if (controller.signal.aborted) {
438
- return iteration.end({
175
+ iteration.end({
439
176
  type: "aborted",
440
177
  aborted: {
441
178
  reason: _nullishCoalesce(controller.signal.reason, () => ( "The operation was aborted"))
442
179
  }
443
180
  });
181
+ return;
444
182
  }
445
183
  if (result.error && result.error instanceof _chunkPK72FAKDcjs.CodeExecutionError) {
446
- return iteration.end({
184
+ iteration.end({
447
185
  type: "execution_error",
448
186
  execution_error: {
449
187
  message: result.error.message,
450
188
  stack: _chunkKIN7Y247cjs.cleanStackTrace.call(void 0, _nullishCoalesce(_nullishCoalesce(result.error.stacktrace, () => ( result.error.stack)), () => ( "No stack trace available")))
451
189
  }
452
190
  });
191
+ return;
453
192
  }
454
193
  if (!result.success) {
455
- return iteration.end({
194
+ iteration.end({
456
195
  type: "execution_error",
457
196
  execution_error: {
458
- message: _nullishCoalesce(((_d = result == null ? void 0 : result.error) == null ? void 0 : _d.message), () => ( "Unknown error occurred")),
197
+ message: _nullishCoalesce(((_a = result == null ? void 0 : result.error) == null ? void 0 : _a.message), () => ( "Unknown error occurred")),
459
198
  stack: _chunkKIN7Y247cjs.cleanStackTrace.call(void 0, _nullishCoalesce(result.error.stack, () => ( "No stack trace available")))
460
199
  }
461
200
  });
201
+ return;
462
202
  }
463
203
  if (result.signal instanceof _chunkPK72FAKDcjs.ThinkSignal) {
464
- return iteration.end({
204
+ iteration.end({
465
205
  type: "thinking_requested",
466
206
  thinking_requested: {
467
207
  variables: result.signal.context,
@@ -469,39 +209,43 @@ var executeIteration = async ({
469
209
  metadata: result.signal.metadata
470
210
  }
471
211
  });
212
+ return;
472
213
  }
473
214
  if (result.signal instanceof _chunkPK72FAKDcjs.SnapshotSignal) {
474
- return iteration.end({
215
+ iteration.end({
475
216
  type: "callback_requested",
476
217
  callback_requested: {
477
218
  signal: result.signal
478
219
  }
479
220
  });
221
+ return;
480
222
  }
481
223
  let returnValue = result.success && result.return_value ? result.return_value : null;
482
224
  const returnAction = returnValue == null ? void 0 : returnValue.action;
483
225
  if (returnAction === "think") {
484
- const variables = _chunk7XVBJQ53cjs.omit_default.call(void 0, _nullishCoalesce(returnValue, () => ( {})), "action");
485
- if (_chunk7XVBJQ53cjs.isPlainObject_default.call(void 0, variables) && Object.keys(variables).length > 0) {
486
- return iteration.end({
226
+ const variables = _chunkHC2QZTVTcjs.omit_default.call(void 0, _nullishCoalesce(returnValue, () => ( {})), "action");
227
+ if (_chunkHC2QZTVTcjs.isPlainObject_default.call(void 0, variables) && Object.keys(variables).length > 0) {
228
+ iteration.end({
487
229
  type: "thinking_requested",
488
230
  thinking_requested: {
489
231
  variables,
490
232
  reason: "Thinking requested"
491
233
  }
492
234
  });
235
+ return;
493
236
  }
494
- return iteration.end({
237
+ iteration.end({
495
238
  type: "thinking_requested",
496
239
  thinking_requested: {
497
240
  reason: "Thinking requested",
498
241
  variables: iteration.variables
499
242
  }
500
243
  });
244
+ return;
501
245
  }
502
- const parsedExit = _chunkX24YFCFYcjs.parseExit.call(void 0, returnValue, iteration.exits);
246
+ const parsedExit = _chunkYCLKXXSFcjs.parseExit.call(void 0, returnValue, iteration.exits);
503
247
  if (!parsedExit.success) {
504
- return iteration.end({
248
+ iteration.end({
505
249
  type: "exit_error",
506
250
  exit_error: {
507
251
  exit: _nullishCoalesce((returnValue == null ? void 0 : returnValue.action), () => ( "n/a")),
@@ -509,6 +253,7 @@ var executeIteration = async ({
509
253
  return_value: returnValue
510
254
  }
511
255
  });
256
+ return;
512
257
  }
513
258
  const returnExit = parsedExit.exit;
514
259
  returnValue = { action: returnExit.name, value: parsedExit.value };
@@ -518,58 +263,109 @@ var executeIteration = async ({
518
263
  result: returnValue == null ? void 0 : returnValue.value
519
264
  }));
520
265
  } catch (err) {
521
- return iteration.end({
266
+ iteration.end({
522
267
  type: "exit_error",
523
268
  exit_error: {
524
269
  exit: returnExit.name,
525
- message: `Error executing exit ${returnExit.name}: ${getErrorMessage(err)}`,
270
+ message: `Error executing exit ${returnExit.name}: ${_chunkNWTG73QRcjs.getErrorMessage.call(void 0, err)}`,
526
271
  return_value: returnValue
527
272
  }
528
273
  });
274
+ return;
529
275
  }
530
- return iteration.end({
276
+ iteration.end({
531
277
  type: "exit_success",
532
278
  exit_success: {
533
279
  exit_name: returnExit.name,
534
280
  return_value: returnValue == null ? void 0 : returnValue.value
535
281
  }
536
282
  });
283
+ return;
284
+ };
285
+
286
+ // src/runtime/utils.ts
287
+ var finalizeIteration = async ({ iteration, status, controller, onIterationEnd }) => {
288
+ if (status) {
289
+ iteration.end(status);
290
+ }
291
+ if (iteration.status.type === "pending") {
292
+ return;
293
+ }
294
+ try {
295
+ await (onIterationEnd == null ? void 0 : onIterationEnd(iteration, controller));
296
+ } catch (err) {
297
+ console.error(err);
298
+ }
537
299
  };
538
- function wrapTool({ chat, tool, traces, object, iteration, beforeHook, afterHook, controller }) {
300
+
301
+ // src/runtime/vm-context.ts
302
+ var _zui = require('@bpinternal/zui');
303
+
304
+ // src/runtime/tool-wrapper.ts
305
+ var _ms = require('ms'); var _ms2 = _interopRequireDefault(_ms);
306
+ var _ulid = require('ulid');
307
+ var SLOW_TOOL_WARNING = _ms2.default.call(void 0, "15s");
308
+ function wrapTool({
309
+ chat,
310
+ tool,
311
+ traces,
312
+ object,
313
+ iteration,
314
+ beforeHook,
315
+ afterHook,
316
+ controller
317
+ }) {
539
318
  const getToolInput = (input) => _nullishCoalesce(tool.zInput.safeParse(input).data, () => ( input));
540
- return function(input) {
319
+ return async function(input) {
541
320
  const toolCallId = `tcall_${_ulid.ulid.call(void 0, )}`;
321
+ const originalInput = input;
322
+ let effectiveInput = input;
542
323
  const alertSlowTool = setTimeout(
543
324
  () => traces.push({
544
325
  type: "tool_slow",
545
326
  tool_name: tool.name,
546
327
  tool_call_id: toolCallId,
547
328
  started_at: Date.now(),
548
- input: getToolInput(input),
329
+ input: getToolInput(originalInput),
549
330
  object,
550
331
  duration: SLOW_TOOL_WARNING
551
332
  }),
552
333
  SLOW_TOOL_WARNING
553
334
  );
554
- const cancelSlowTool = () => clearTimeout(alertSlowTool);
555
335
  const toolStart = Date.now();
556
336
  let output;
557
337
  let error;
558
338
  let success = true;
559
- const handleSignals = (error2) => {
560
- if (output === error2) {
339
+ let signalToThrow;
340
+ const pushToolCallTrace = () => {
341
+ traces.push({
342
+ type: "tool_call",
343
+ tool_call_id: toolCallId,
344
+ started_at: toolStart,
345
+ ended_at: Date.now(),
346
+ tool_name: tool.name,
347
+ object,
348
+ input: getToolInput(originalInput),
349
+ output,
350
+ error,
351
+ success
352
+ });
353
+ };
354
+ const handleSignals = async (err) => {
355
+ if (output === err) {
561
356
  return true;
562
357
  }
563
- if (error2 instanceof _chunkPK72FAKDcjs.SnapshotSignal) {
564
- error2.toolCall = {
358
+ if (err instanceof _chunkPK72FAKDcjs.SnapshotSignal) {
359
+ err.toolCall = {
565
360
  name: tool.name,
566
361
  inputSchema: tool.input,
567
362
  outputSchema: tool.output,
568
- input
363
+ input: originalInput
569
364
  };
570
- error2.message = _chunkPK72FAKDcjs.Signals.serializeError(error2);
365
+ err.message = _chunkPK72FAKDcjs.Signals.serializeError(err);
571
366
  }
572
- if (error2 instanceof _chunkPK72FAKDcjs.ThinkSignal) {
367
+ if (err instanceof _chunkPK72FAKDcjs.ThinkSignal) {
368
+ signalToThrow = err;
573
369
  traces.push({
574
370
  type: "think_signal",
575
371
  started_at: Date.now(),
@@ -577,150 +373,470 @@ function wrapTool({ chat, tool, traces, object, iteration, beforeHook, afterHook
577
373
  ended_at: Date.now()
578
374
  });
579
375
  success = true;
580
- output = error2;
581
- return true;
582
- }
583
- return false;
584
- };
585
- try {
586
- const withHooks = async (input2) => {
587
- const beforeRes = await (beforeHook == null ? void 0 : beforeHook({
588
- iteration,
589
- tool,
590
- input: input2,
591
- controller,
592
- object,
593
- toolCallId
594
- }));
595
- if (typeof (beforeRes == null ? void 0 : beforeRes.input) !== "undefined") {
596
- input2 = beforeRes.input;
597
- }
598
- let output2 = await tool.execute(
599
- input2,
600
- {
601
- callId: toolCallId
602
- },
603
- chat
604
- );
376
+ output = err;
605
377
  const afterRes = await (afterHook == null ? void 0 : afterHook({
606
378
  iteration,
607
379
  tool,
608
- input: input2,
609
- output: output2,
380
+ input: originalInput,
381
+ output,
610
382
  controller,
611
383
  object,
612
384
  toolCallId
613
385
  }));
614
386
  if (typeof (afterRes == null ? void 0 : afterRes.output) !== "undefined") {
615
- output2 = afterRes.output;
387
+ output = afterRes.output;
616
388
  }
617
- return output2;
618
- };
619
- const result = withHooks(input);
620
- if (result instanceof Promise || (result == null ? void 0 : result.then) && (result == null ? void 0 : result.catch)) {
621
- return result.then((res) => {
622
- output = res;
623
- success = true;
624
- return res;
625
- }).catch(async (err) => {
626
- if (!handleSignals(err)) {
627
- success = false;
628
- error = err;
629
- } else {
630
- const afterRes = await (afterHook == null ? void 0 : afterHook({
631
- iteration,
632
- tool,
633
- input,
634
- output,
635
- controller,
636
- object,
637
- toolCallId
638
- }));
639
- if (typeof (afterRes == null ? void 0 : afterRes.output) !== "undefined") {
640
- output = afterRes.output;
641
- }
642
- }
643
- throw err;
644
- }).finally(() => {
645
- cancelSlowTool();
646
- traces.push({
647
- type: "tool_call",
648
- tool_call_id: toolCallId,
649
- started_at: toolStart,
650
- ended_at: Date.now(),
651
- tool_name: tool.name,
652
- object,
653
- input: getToolInput(input),
654
- output,
655
- error,
656
- success
657
- });
658
- });
389
+ return true;
659
390
  }
660
- success = true;
661
- output = result;
662
- } catch (err) {
663
- if (!handleSignals(err)) {
664
- success = false;
391
+ return false;
392
+ };
393
+ try {
394
+ const beforeRes = await (beforeHook == null ? void 0 : beforeHook({
395
+ iteration,
396
+ tool,
397
+ input: effectiveInput,
398
+ controller,
399
+ object,
400
+ toolCallId
401
+ }));
402
+ if (typeof (beforeRes == null ? void 0 : beforeRes.input) !== "undefined") {
403
+ effectiveInput = beforeRes.input;
404
+ }
405
+ output = await tool.execute(
406
+ effectiveInput,
407
+ {
408
+ callId: toolCallId
409
+ },
410
+ chat
411
+ );
412
+ const afterRes = await (afterHook == null ? void 0 : afterHook({
413
+ iteration,
414
+ tool,
415
+ input: effectiveInput,
416
+ output,
417
+ controller,
418
+ object,
419
+ toolCallId
420
+ }));
421
+ if (typeof (afterRes == null ? void 0 : afterRes.output) !== "undefined") {
422
+ output = afterRes.output;
423
+ }
424
+ } catch (err) {
425
+ if (!await handleSignals(err)) {
426
+ success = false;
665
427
  error = err;
666
428
  }
429
+ } finally {
430
+ clearTimeout(alertSlowTool);
431
+ pushToolCallTrace();
667
432
  }
668
- cancelSlowTool();
669
- traces.push({
670
- type: "tool_call",
671
- tool_call_id: toolCallId,
672
- started_at: toolStart,
673
- ended_at: Date.now(),
674
- tool_name: tool.name,
675
- object,
676
- input: getToolInput(input),
677
- output,
678
- error,
679
- success
680
- });
681
433
  if (!success) {
682
434
  throw error;
683
435
  }
684
- if (output instanceof _chunkPK72FAKDcjs.VMSignal) {
685
- throw output;
436
+ if (signalToThrow) {
437
+ throw signalToThrow;
686
438
  }
687
439
  return output;
688
440
  };
689
441
  }
690
- var executeOnIterationStartHook = async (props) => {
691
- const { iteration, ctx, onIterationStart, controller, onIterationEnd } = props;
442
+
443
+ // src/runtime/vm-context.ts
444
+ var buildVMContext = ({
445
+ ctx,
446
+ iteration,
447
+ controller,
448
+ onBeforeTool,
449
+ onAfterTool
450
+ }) => {
451
+ const traces = iteration.traces;
452
+ const vmContext = { ..._chunkNWTG73QRcjs.stripInvalidIdentifiers.call(void 0, iteration.variables) };
453
+ for (const obj of iteration.objects) {
454
+ const internalValues = {};
455
+ const instance = {};
456
+ for (const { name, value, writable, type } of _nullishCoalesce(obj.properties, () => ( []))) {
457
+ internalValues[name] = value;
458
+ const initialValue = value;
459
+ const schema = _nullishCoalesce(type, () => ( _zui.z.any()));
460
+ Object.defineProperty(instance, name, {
461
+ enumerable: true,
462
+ configurable: true,
463
+ get() {
464
+ return internalValues[name];
465
+ },
466
+ set(value2) {
467
+ if (_chunkHC2QZTVTcjs.isEqual_default.call(void 0, value2, internalValues[name])) {
468
+ return;
469
+ }
470
+ if (!writable) {
471
+ throw new (0, _chunkPK72FAKDcjs.AssignmentError)(`Property ${obj.name}.${name} is read-only and cannot be modified`);
472
+ }
473
+ const parsed = schema.safeParse(value2);
474
+ if (!parsed.success) {
475
+ throw new (0, _chunkPK72FAKDcjs.AssignmentError)(
476
+ `Invalid value for Object property ${obj.name}.${name}: ${_chunkNWTG73QRcjs.getErrorMessage.call(void 0, parsed.error)}`
477
+ );
478
+ }
479
+ internalValues[name] = parsed.data;
480
+ traces.push({
481
+ type: "property",
482
+ started_at: Date.now(),
483
+ object: obj.name,
484
+ property: name,
485
+ value: parsed.data
486
+ });
487
+ iteration.trackMutation({ object: obj.name, property: name, before: initialValue, after: parsed.data });
488
+ }
489
+ });
490
+ }
491
+ for (const tool of _nullishCoalesce(obj.tools, () => ( []))) {
492
+ instance[tool.name] = wrapTool({
493
+ chat: ctx.chat,
494
+ tool,
495
+ traces,
496
+ object: obj.name,
497
+ iteration,
498
+ beforeHook: onBeforeTool,
499
+ afterHook: onAfterTool,
500
+ controller
501
+ });
502
+ }
503
+ Object.preventExtensions(instance);
504
+ Object.seal(instance);
505
+ vmContext[obj.name] = instance;
506
+ }
507
+ for (const tool of iteration.tools) {
508
+ const wrapped = wrapTool({
509
+ chat: ctx.chat,
510
+ tool,
511
+ traces,
512
+ iteration,
513
+ beforeHook: onBeforeTool,
514
+ afterHook: onAfterTool,
515
+ controller
516
+ });
517
+ for (const key of [tool.name, ..._nullishCoalesce(tool.aliases, () => ( []))]) {
518
+ vmContext[key] = wrapped;
519
+ }
520
+ }
521
+ return vmContext;
522
+ };
523
+
524
+ // src/runtime/execute.ts
525
+ var executeContext = async (props) => {
526
+ var _a, _b;
527
+ await _chunkNWTG73QRcjs.init.call(void 0, );
528
+ const result = await executeContextInternal(props);
529
+ try {
530
+ (_b = (_a = result.context.chat) == null ? void 0 : _a.onExecutionDone) == null ? void 0 : _b.call(_a, result);
531
+ } catch (err) {
532
+ }
533
+ return result;
534
+ };
535
+ var executeContextInternal = async (props) => {
536
+ var _a, _b;
537
+ const controller = createJoinedAbortController([props.signal]);
538
+ const { onIterationStart, onIterationEnd, onTrace, onExit, onBeforeExecution, onAfterTool, onBeforeTool } = props;
539
+ const client = _nullishCoalesce(props.client, () => ( new (0, _client.Client)()));
540
+ const cognitive = _cognitive.Cognitive.isCognitiveClient(client) ? client : _cognitive.CognitiveBeta.isBetaClient(client) ? _cognitive.cognitiveFromBeta.call(void 0, client) : new (0, _cognitive.Cognitive)({ client, __experimental_beta: true });
541
+ const ctx = new (0, _chunkYCLKXXSFcjs.Context)({
542
+ chat: props.chat,
543
+ instructions: props.instructions,
544
+ objects: props.objects,
545
+ tools: props.tools,
546
+ loop: (_a = props.options) == null ? void 0 : _a.loop,
547
+ timeout: (_b = props.options) == null ? void 0 : _b.timeout,
548
+ exits: props.exits,
549
+ snapshot: props.snapshot,
550
+ model: props.model,
551
+ temperature: props.temperature,
552
+ reasoningEffort: props.reasoningEffort
553
+ });
554
+ try {
555
+ while (true) {
556
+ if (ctx.iterations.length >= ctx.loop) {
557
+ return new (0, _chunkYCLKXXSFcjs.ErrorExecutionResult)(ctx, new (0, _chunkPK72FAKDcjs.LoopExceededError)());
558
+ }
559
+ const iteration = await ctx.nextIteration();
560
+ const unsubscribeTrace = iteration.traces.onPush((traces) => {
561
+ for (const trace of traces) {
562
+ onTrace == null ? void 0 : onTrace({ trace, iteration: ctx.iterations.length });
563
+ }
564
+ });
565
+ try {
566
+ const startHookResult = await executeIterationStartHook({
567
+ ctx,
568
+ iteration,
569
+ controller,
570
+ onIterationStart,
571
+ onIterationEnd
572
+ });
573
+ if (startHookResult.type === "continue") {
574
+ continue;
575
+ }
576
+ if (controller.signal.aborted) {
577
+ await finalizeIteration({
578
+ iteration,
579
+ controller,
580
+ onIterationEnd,
581
+ status: {
582
+ type: "aborted",
583
+ aborted: {
584
+ reason: _nullishCoalesce(controller.signal.reason, () => ( "The operation was aborted"))
585
+ }
586
+ }
587
+ });
588
+ return new (0, _chunkYCLKXXSFcjs.ErrorExecutionResult)(ctx, _nullishCoalesce(controller.signal.reason, () => ( "The operation was aborted")));
589
+ }
590
+ const iterationResult = await executeIterationWithErrorHandling({
591
+ iteration,
592
+ ctx,
593
+ cognitive,
594
+ controller,
595
+ onExit,
596
+ onBeforeExecution,
597
+ onAfterTool,
598
+ onBeforeTool,
599
+ onIterationEnd
600
+ });
601
+ if (iterationResult.type === "return") {
602
+ return iterationResult.result;
603
+ }
604
+ const selectedResult = transformIterationResult({ ctx, iteration });
605
+ if (selectedResult.type === "continue") {
606
+ continue;
607
+ }
608
+ return selectedResult.result;
609
+ } finally {
610
+ try {
611
+ unsubscribeTrace();
612
+ } catch (err) {
613
+ }
614
+ }
615
+ }
616
+ } catch (error) {
617
+ return new (0, _chunkYCLKXXSFcjs.ErrorExecutionResult)(ctx, _nullishCoalesce(error, () => ( "Unknown error")));
618
+ }
619
+ };
620
+ var executeIterationStartHook = async ({
621
+ ctx,
622
+ iteration,
623
+ controller,
624
+ onIterationStart,
625
+ onIterationEnd
626
+ }) => {
692
627
  try {
693
628
  const hookRes = await (onIterationStart == null ? void 0 : onIterationStart(iteration, controller, ctx));
694
629
  if (hookRes) {
695
630
  Object.assign(iteration, hookRes);
696
631
  }
632
+ return { type: "proceed" };
697
633
  } catch (err) {
698
- if (err instanceof _chunkPK72FAKDcjs.ThinkSignal) {
699
- iteration.end({
634
+ await finalizeIteration({
635
+ iteration,
636
+ controller,
637
+ onIterationEnd,
638
+ status: err instanceof _chunkPK72FAKDcjs.ThinkSignal ? {
700
639
  type: "thinking_requested",
701
640
  thinking_requested: {
702
641
  variables: err.context,
703
642
  reason: err.reason
704
643
  }
644
+ } : {
645
+ type: "execution_error",
646
+ execution_error: {
647
+ message: `Error in onIterationStart hook: ${_chunkNWTG73QRcjs.getErrorMessage.call(void 0, err)}`,
648
+ stack: _chunkKIN7Y247cjs.cleanStackTrace.call(void 0,
649
+ err instanceof Error ? _nullishCoalesce(err.stack, () => ( "No stack trace available")) : "No stack trace available"
650
+ )
651
+ }
652
+ }
653
+ });
654
+ return { type: "continue" };
655
+ }
656
+ };
657
+ var executeIterationWithErrorHandling = async ({
658
+ iteration,
659
+ ctx,
660
+ cognitive,
661
+ controller,
662
+ onExit,
663
+ onBeforeExecution,
664
+ onAfterTool,
665
+ onBeforeTool,
666
+ onIterationEnd
667
+ }) => {
668
+ try {
669
+ await executeIteration({
670
+ iteration,
671
+ ctx,
672
+ cognitive,
673
+ controller,
674
+ onExit,
675
+ onBeforeExecution,
676
+ onAfterTool,
677
+ onBeforeTool
678
+ });
679
+ await finalizeIteration({ iteration, controller, onIterationEnd });
680
+ return { type: "proceed" };
681
+ } catch (err) {
682
+ if (!(err instanceof _chunkPK72FAKDcjs.CognitiveError)) {
683
+ await finalizeIteration({
684
+ iteration,
685
+ controller,
686
+ onIterationEnd,
687
+ status: iteration.status.type === "pending" ? {
688
+ type: "execution_error",
689
+ execution_error: {
690
+ message: "An unexpected error occurred: " + _chunkNWTG73QRcjs.getErrorMessage.call(void 0, err),
691
+ stack: _chunkKIN7Y247cjs.cleanStackTrace.call(void 0,
692
+ err instanceof Error ? _nullishCoalesce(err.stack, () => ( "No stack trace available")) : "No stack trace available"
693
+ )
694
+ }
695
+ } : void 0
705
696
  });
706
- try {
707
- await (onIterationEnd == null ? void 0 : onIterationEnd(iteration, controller));
708
- } catch (err2) {
709
- console.error(err2);
697
+ return { type: "proceed" };
698
+ }
699
+ if (!controller.signal.aborted) {
700
+ return {
701
+ type: "return",
702
+ result: new (0, _chunkYCLKXXSFcjs.ErrorExecutionResult)(ctx, err)
703
+ };
704
+ }
705
+ await finalizeIteration({
706
+ iteration,
707
+ controller,
708
+ onIterationEnd,
709
+ status: iteration.status.type === "pending" ? {
710
+ type: "aborted",
711
+ aborted: {
712
+ reason: _nullishCoalesce(controller.signal.reason, () => ( "The operation was aborted"))
713
+ }
714
+ } : void 0
715
+ });
716
+ return {
717
+ type: "return",
718
+ result: new (0, _chunkYCLKXXSFcjs.ErrorExecutionResult)(ctx, _nullishCoalesce(controller.signal.reason, () => ( "The operation was aborted")))
719
+ };
720
+ }
721
+ };
722
+ var transformIterationResult = ({
723
+ ctx,
724
+ iteration
725
+ }) => {
726
+ if (iteration.status.type === "exit_success") {
727
+ const exitName = iteration.status.exit_success.exit_name;
728
+ return {
729
+ type: "return",
730
+ result: new (0, _chunkYCLKXXSFcjs.SuccessExecutionResult)(ctx, {
731
+ exit: iteration.exits.find((x) => x.name === exitName),
732
+ result: iteration.status.exit_success.return_value
733
+ })
734
+ };
735
+ }
736
+ if (iteration.status.type === "callback_requested") {
737
+ return {
738
+ type: "return",
739
+ result: new (0, _chunkYCLKXXSFcjs.PartialExecutionResult)(
740
+ ctx,
741
+ iteration.status.callback_requested.signal,
742
+ _chunkYCLKXXSFcjs.Snapshot.fromSignal(iteration.status.callback_requested.signal)
743
+ )
744
+ };
745
+ }
746
+ if (iteration.status.type === "aborted") {
747
+ return {
748
+ type: "return",
749
+ result: new (0, _chunkYCLKXXSFcjs.ErrorExecutionResult)(ctx, _nullishCoalesce(iteration.error, () => ( "The operation was aborted")))
750
+ };
751
+ }
752
+ if (iteration.status.type === "thinking_requested" || iteration.status.type === "exit_error" || iteration.status.type === "execution_error" || iteration.status.type === "invalid_code_error") {
753
+ return { type: "continue" };
754
+ }
755
+ return {
756
+ type: "return",
757
+ result: new (0, _chunkYCLKXXSFcjs.ErrorExecutionResult)(ctx, _nullishCoalesce(iteration.error, () => ( `Unknown error. Status: ${iteration.status.type}`)))
758
+ };
759
+ };
760
+ var executeIteration = async ({
761
+ iteration,
762
+ ctx,
763
+ cognitive,
764
+ controller,
765
+ onExit,
766
+ onBeforeExecution,
767
+ onBeforeTool,
768
+ onAfterTool
769
+ }) => {
770
+ await generateCode({ iteration, ctx, cognitive, controller });
771
+ if (typeof onBeforeExecution === "function") {
772
+ try {
773
+ const hookRes = await onBeforeExecution(iteration, controller);
774
+ if (typeof (hookRes == null ? void 0 : hookRes.code) === "string" && hookRes.code.trim().length > 0) {
775
+ iteration.code = hookRes.code.trim();
776
+ }
777
+ } catch (err) {
778
+ if (err instanceof _chunkPK72FAKDcjs.ThinkSignal) {
779
+ iteration.end({
780
+ type: "thinking_requested",
781
+ thinking_requested: {
782
+ variables: err.context,
783
+ reason: err.reason
784
+ }
785
+ });
786
+ return;
710
787
  }
711
- } else {
712
788
  iteration.end({
713
789
  type: "execution_error",
714
790
  execution_error: {
715
- message: `Error in onIterationStart hook: ${getErrorMessage(err)}`,
716
- stack: _chunkKIN7Y247cjs.cleanStackTrace.call(void 0, _nullishCoalesce(err.stack, () => ( "No stack trace available")))
791
+ message: `Error in onBeforeExecution hook: ${_chunkNWTG73QRcjs.getErrorMessage.call(void 0, err)}`,
792
+ stack: _chunkKIN7Y247cjs.cleanStackTrace.call(void 0,
793
+ err instanceof Error ? _nullishCoalesce(err.stack, () => ( "No stack trace available")) : "No stack trace available"
794
+ )
717
795
  }
718
796
  });
797
+ return;
719
798
  }
720
- throw err;
721
799
  }
800
+ const traces = iteration.traces;
801
+ const vmContext = buildVMContext({ ctx, iteration, controller, onBeforeTool, onAfterTool });
802
+ if (controller.signal.aborted) {
803
+ traces.push({
804
+ type: "abort_signal",
805
+ started_at: Date.now(),
806
+ reason: "The operation was aborted by user."
807
+ });
808
+ iteration.end({
809
+ type: "aborted",
810
+ aborted: {
811
+ reason: _nullishCoalesce(controller.signal.reason, () => ( "The operation was aborted"))
812
+ }
813
+ });
814
+ return;
815
+ }
816
+ const startedAt = Date.now();
817
+ const result = await _chunkJXD2KJVBcjs.runAsyncFunction.call(void 0,
818
+ vmContext,
819
+ _nullishCoalesce(iteration.code, () => ( "")),
820
+ traces,
821
+ controller.signal,
822
+ ctx.timeout
823
+ ).catch((err) => {
824
+ return {
825
+ success: false,
826
+ error: err instanceof Error ? err : new Error(_chunkNWTG73QRcjs.getErrorMessage.call(void 0, err)),
827
+ lines_executed: [],
828
+ traces: [],
829
+ variables: {}
830
+ };
831
+ });
832
+ await interpretVMResult({
833
+ iteration,
834
+ result,
835
+ controller,
836
+ startedAt,
837
+ onExit
838
+ });
722
839
  };
723
840
 
724
841
 
725
-
726
- exports._executeContext = _executeContext; exports.executeContext = executeContext;
842
+ exports.executeContext = executeContext;