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