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.
- package/dist/{chunk-BJMZIH3M.js → chunk-34GJUS6U.js} +1047 -1047
- package/dist/{chunk-EQ4JDBE5.js → chunk-3T5IU7OU.js} +1 -1
- package/dist/{chunk-H5LCBNN6.cjs → chunk-52QTFQOB.cjs} +18 -18
- package/dist/{chunk-3NVOHQVY.js → chunk-5MAFEI2A.js} +4 -2
- package/dist/{chunk-HBKQKO3R.js → chunk-5Z2SJOGS.js} +3 -3
- package/dist/{chunk-OX3Y2Z5K.cjs → chunk-5ZF6NTYI.cjs} +3 -3
- package/dist/{chunk-TB6TCWEB.cjs → chunk-6KR6VRA6.cjs} +2 -2
- package/dist/{chunk-BXPPMP5X.cjs → chunk-73XYX76S.cjs} +14 -14
- package/dist/{chunk-VXQ2CZ7B.cjs → chunk-CQ3UJW76.cjs} +4 -4
- package/dist/{chunk-22DGAZXC.js → chunk-EFEF2AL7.js} +2 -2
- package/dist/{chunk-7XVBJQ53.cjs → chunk-HC2QZTVT.cjs} +1047 -1047
- package/dist/{chunk-HP3T6PF4.js → chunk-I2CZYEMH.js} +4 -4
- package/dist/{chunk-GFZCNSYW.cjs → chunk-JXD2KJVB.cjs} +23 -23
- package/dist/{chunk-YFG53MRF.js → chunk-MM2FLD3J.js} +1 -1
- package/dist/{chunk-DO7DN4FZ.js → chunk-NPVV3PHY.js} +1 -1
- package/dist/{chunk-5X5TDCMX.cjs → chunk-NQBIU5VM.cjs} +51 -51
- package/dist/{chunk-FQ4QCUTI.cjs → chunk-NWTG73QR.cjs} +10 -8
- package/dist/{chunk-DLV5FJ42.cjs → chunk-RLUTWFAA.cjs} +9 -9
- package/dist/{chunk-2UM7PFM3.js → chunk-VDTORFBP.js} +6 -6
- package/dist/{chunk-E74GBCTT.js → chunk-VLJA22PB.js} +2 -2
- package/dist/{chunk-E6UFMAW7.js → chunk-XTEHT2TK.js} +1 -1
- package/dist/{chunk-X24YFCFY.cjs → chunk-YCLKXXSF.cjs} +28 -28
- package/dist/{component-GVCCL5XU.js → component-2Z3KLWLX.js} +3 -3
- package/dist/component-NNHBHD72.cjs +18 -0
- package/dist/dual-modes-E5HGE35E.cjs +12 -0
- package/dist/dual-modes-VDFZH4YM.js +12 -0
- package/dist/{llmz-JTZUIYRP.js → execute-CJVHUFYI.js} +536 -420
- package/dist/{llmz-2HD6WKIP.cjs → execute-R6R3A42S.cjs} +545 -429
- package/dist/exit-GKF4EEMZ.js +8 -0
- package/dist/exit-VZASQELZ.cjs +8 -0
- package/dist/index.cjs +49 -49
- package/dist/index.d.ts +1 -1
- package/dist/index.js +22 -22
- package/dist/{jsx-RIPUQSAU.js → jsx-DDZ63UUF.js} +2 -2
- package/dist/jsx-ULPWX5TW.cjs +13 -0
- package/dist/runtime/execute.d.ts +3 -0
- package/dist/runtime/generate.d.ts +10 -0
- package/dist/runtime/interpret-result.d.ts +12 -0
- package/dist/runtime/interpret-result.test.d.ts +1 -0
- package/dist/runtime/tool-wrapper.d.ts +17 -0
- package/dist/runtime/tool-wrapper.test.d.ts +1 -0
- package/dist/{llmz.d.ts → runtime/types.d.ts} +12 -13
- package/dist/runtime/utils.d.ts +10 -0
- package/dist/runtime/vm-context.d.ts +12 -0
- package/dist/tool-4VK7TZV2.cjs +11 -0
- package/dist/tool-JZP3UN2F.js +11 -0
- package/dist/{truncator-HCWH5MJ3.js → truncator-IIRX3MB2.js} +3 -3
- package/dist/truncator-NWENJXMR.cjs +10 -0
- package/dist/typings-SHVPZ5AR.cjs +10 -0
- package/dist/{typings-FPFJ5D5R.js → typings-SKZSSL2M.js} +3 -3
- package/dist/utils-6MFSS4AT.cjs +41 -0
- package/dist/{utils-M5KFPSNZ.js → utils-SYJGSVFW.js} +4 -2
- package/dist/utils.d.ts +1 -0
- package/dist/{vm-KBBQ7U5L.js → vm-PYCKS77H.js} +3 -3
- package/dist/vm-VHB67FVH.cjs +10 -0
- package/package.json +1 -1
- package/dist/component-YMPMWSTU.cjs +0 -18
- package/dist/dual-modes-KOU5YJ3Q.cjs +0 -12
- package/dist/dual-modes-X7YHJBFJ.js +0 -12
- package/dist/exit-A34GODDB.cjs +0 -8
- package/dist/exit-J55SREDL.js +0 -8
- package/dist/jsx-XEUSAKAK.cjs +0 -13
- package/dist/tool-B3DJ3D2F.js +0 -11
- package/dist/tool-IWPZD5OY.cjs +0 -11
- package/dist/truncator-SOO6NHLD.cjs +0 -10
- package/dist/typings-O2FLZGQN.cjs +0 -10
- package/dist/utils-PWX3JPCF.cjs +0 -39
- package/dist/vm-R2XBIWE4.cjs +0 -10
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
runAsyncFunction
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-EFEF2AL7.js";
|
|
4
4
|
import {
|
|
5
5
|
Context,
|
|
6
6
|
ErrorExecutionResult,
|
|
@@ -8,10 +8,10 @@ import {
|
|
|
8
8
|
Snapshot,
|
|
9
9
|
SuccessExecutionResult,
|
|
10
10
|
parseExit
|
|
11
|
-
} from "./chunk-
|
|
12
|
-
import "./chunk-
|
|
13
|
-
import "./chunk-
|
|
14
|
-
import "./chunk-
|
|
11
|
+
} from "./chunk-VDTORFBP.js";
|
|
12
|
+
import "./chunk-I2CZYEMH.js";
|
|
13
|
+
import "./chunk-5Z2SJOGS.js";
|
|
14
|
+
import "./chunk-NPVV3PHY.js";
|
|
15
15
|
import {
|
|
16
16
|
AssignmentError,
|
|
17
17
|
CodeExecutionError,
|
|
@@ -20,39 +20,32 @@ import {
|
|
|
20
20
|
LoopExceededError,
|
|
21
21
|
Signals,
|
|
22
22
|
SnapshotSignal,
|
|
23
|
-
ThinkSignal
|
|
24
|
-
VMSignal
|
|
23
|
+
ThinkSignal
|
|
25
24
|
} from "./chunk-WSVDMGMR.js";
|
|
26
25
|
import {
|
|
27
26
|
cleanStackTrace
|
|
28
27
|
} from "./chunk-YEAWWJSJ.js";
|
|
29
|
-
import "./chunk-
|
|
30
|
-
import "./chunk-
|
|
31
|
-
import "./chunk-
|
|
28
|
+
import "./chunk-VLJA22PB.js";
|
|
29
|
+
import "./chunk-3T5IU7OU.js";
|
|
30
|
+
import "./chunk-XTEHT2TK.js";
|
|
32
31
|
import {
|
|
33
32
|
truncateWrappedContent
|
|
34
|
-
} from "./chunk-
|
|
33
|
+
} from "./chunk-MM2FLD3J.js";
|
|
35
34
|
import {
|
|
35
|
+
getErrorMessage,
|
|
36
36
|
init,
|
|
37
37
|
stripInvalidIdentifiers
|
|
38
|
-
} from "./chunk-
|
|
38
|
+
} from "./chunk-5MAFEI2A.js";
|
|
39
39
|
import {
|
|
40
40
|
clamp_default,
|
|
41
41
|
isEqual_default,
|
|
42
42
|
isPlainObject_default,
|
|
43
43
|
omit_default
|
|
44
|
-
} from "./chunk-
|
|
44
|
+
} from "./chunk-34GJUS6U.js";
|
|
45
45
|
|
|
46
|
-
// src/
|
|
46
|
+
// src/runtime/execute.ts
|
|
47
47
|
import { Client } from "@botpress/client";
|
|
48
|
-
import {
|
|
49
|
-
Cognitive,
|
|
50
|
-
CognitiveBeta,
|
|
51
|
-
cognitiveFromBeta
|
|
52
|
-
} from "@botpress/cognitive";
|
|
53
|
-
import { z } from "@bpinternal/zui";
|
|
54
|
-
import ms from "ms";
|
|
55
|
-
import { ulid } from "ulid";
|
|
48
|
+
import { Cognitive, CognitiveBeta, cognitiveFromBeta } from "@botpress/cognitive";
|
|
56
49
|
|
|
57
50
|
// src/abort-signal.ts
|
|
58
51
|
function createJoinedAbortController(signals) {
|
|
@@ -83,9 +76,7 @@ function createJoinedAbortController(signals) {
|
|
|
83
76
|
return controller;
|
|
84
77
|
}
|
|
85
78
|
|
|
86
|
-
// src/
|
|
87
|
-
var getErrorMessage = (err) => err instanceof Error ? err.message : JSON.stringify(err);
|
|
88
|
-
var SLOW_TOOL_WARNING = ms("15s");
|
|
79
|
+
// src/runtime/generate.ts
|
|
89
80
|
var RESPONSE_LENGTH_BUFFER = {
|
|
90
81
|
MIN_TOKENS: 1e3,
|
|
91
82
|
MAX_TOKENS: 16e3,
|
|
@@ -96,141 +87,9 @@ var getModelOutputLimit = (inputLength) => clamp_default(
|
|
|
96
87
|
RESPONSE_LENGTH_BUFFER.MIN_TOKENS,
|
|
97
88
|
RESPONSE_LENGTH_BUFFER.MAX_TOKENS
|
|
98
89
|
);
|
|
99
|
-
var
|
|
100
|
-
var _a, _b;
|
|
101
|
-
|
|
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 {
|
|
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 = props.client ?? new Client();
|
|
114
|
-
const cognitive = Cognitive.isCognitiveClient(client) ? client : CognitiveBeta.isBetaClient(client) ? cognitiveFromBeta(client) : new Cognitive({ client, __experimental_beta: true });
|
|
115
|
-
const cleanups = [];
|
|
116
|
-
const ctx = new 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 ErrorExecutionResult(ctx, new 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 ThinkSignal) {
|
|
145
|
-
continue;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
if (controller.signal.aborted) {
|
|
149
|
-
iteration.end({
|
|
150
|
-
type: "aborted",
|
|
151
|
-
aborted: {
|
|
152
|
-
reason: controller.signal.reason ?? "The operation was aborted"
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
return new ErrorExecutionResult(ctx, 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 CognitiveError) {
|
|
177
|
-
return new ErrorExecutionResult(ctx, err);
|
|
178
|
-
}
|
|
179
|
-
iteration.end({
|
|
180
|
-
type: "execution_error",
|
|
181
|
-
execution_error: {
|
|
182
|
-
message: "An unexpected error occurred: " + getErrorMessage(err),
|
|
183
|
-
stack: cleanStackTrace(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 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 PartialExecutionResult(
|
|
201
|
-
ctx,
|
|
202
|
-
iteration.status.callback_requested.signal,
|
|
203
|
-
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 ErrorExecutionResult(ctx, iteration.error ?? `Unknown error. Status: ${iteration.status.type}`);
|
|
210
|
-
}
|
|
211
|
-
} catch (error) {
|
|
212
|
-
return new ErrorExecutionResult(ctx, error ?? "Unknown error");
|
|
213
|
-
} finally {
|
|
214
|
-
for (const cleanup of cleanups) {
|
|
215
|
-
try {
|
|
216
|
-
cleanup();
|
|
217
|
-
} catch {
|
|
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) => {
|
|
@@ -242,13 +101,7 @@ var executeIteration = async ({
|
|
|
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",
|
|
@@ -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 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: cleanStackTrace(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
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
get() {
|
|
334
|
-
return internalValues[name];
|
|
335
|
-
},
|
|
336
|
-
set(value2) {
|
|
337
|
-
if (isEqual_default(value2, internalValues[name])) {
|
|
338
|
-
return;
|
|
339
|
-
}
|
|
340
|
-
if (!writable) {
|
|
341
|
-
throw new 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 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 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, ...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: controller.signal.reason ?? "The operation was aborted"
|
|
404
|
-
}
|
|
405
|
-
});
|
|
406
|
-
}
|
|
407
|
-
startedAt = Date.now();
|
|
408
|
-
const result = await runAsyncFunction(
|
|
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 InvalidCodeError) {
|
|
424
|
-
|
|
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: result.lines_executed ?? 0,
|
|
434
171
|
started_at: startedAt,
|
|
435
172
|
ended_at: Date.now()
|
|
436
173
|
});
|
|
437
174
|
if (controller.signal.aborted) {
|
|
438
|
-
|
|
175
|
+
iteration.end({
|
|
439
176
|
type: "aborted",
|
|
440
177
|
aborted: {
|
|
441
178
|
reason: controller.signal.reason ?? "The operation was aborted"
|
|
442
179
|
}
|
|
443
180
|
});
|
|
181
|
+
return;
|
|
444
182
|
}
|
|
445
183
|
if (result.error && result.error instanceof CodeExecutionError) {
|
|
446
|
-
|
|
184
|
+
iteration.end({
|
|
447
185
|
type: "execution_error",
|
|
448
186
|
execution_error: {
|
|
449
187
|
message: result.error.message,
|
|
450
188
|
stack: cleanStackTrace(result.error.stacktrace ?? result.error.stack ?? "No stack trace available")
|
|
451
189
|
}
|
|
452
190
|
});
|
|
191
|
+
return;
|
|
453
192
|
}
|
|
454
193
|
if (!result.success) {
|
|
455
|
-
|
|
194
|
+
iteration.end({
|
|
456
195
|
type: "execution_error",
|
|
457
196
|
execution_error: {
|
|
458
|
-
message: ((
|
|
197
|
+
message: ((_a = result == null ? void 0 : result.error) == null ? void 0 : _a.message) ?? "Unknown error occurred",
|
|
459
198
|
stack: cleanStackTrace(result.error.stack ?? "No stack trace available")
|
|
460
199
|
}
|
|
461
200
|
});
|
|
201
|
+
return;
|
|
462
202
|
}
|
|
463
203
|
if (result.signal instanceof ThinkSignal) {
|
|
464
|
-
|
|
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 SnapshotSignal) {
|
|
474
|
-
|
|
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
226
|
const variables = omit_default(returnValue ?? {}, "action");
|
|
485
227
|
if (isPlainObject_default(variables) && Object.keys(variables).length > 0) {
|
|
486
|
-
|
|
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
|
-
|
|
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
246
|
const parsedExit = parseExit(returnValue, iteration.exits);
|
|
503
247
|
if (!parsedExit.success) {
|
|
504
|
-
|
|
248
|
+
iteration.end({
|
|
505
249
|
type: "exit_error",
|
|
506
250
|
exit_error: {
|
|
507
251
|
exit: (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,7 +263,7 @@ var executeIteration = async ({
|
|
|
518
263
|
result: returnValue == null ? void 0 : returnValue.value
|
|
519
264
|
}));
|
|
520
265
|
} catch (err) {
|
|
521
|
-
|
|
266
|
+
iteration.end({
|
|
522
267
|
type: "exit_error",
|
|
523
268
|
exit_error: {
|
|
524
269
|
exit: returnExit.name,
|
|
@@ -526,50 +271,101 @@ var executeIteration = async ({
|
|
|
526
271
|
return_value: returnValue
|
|
527
272
|
}
|
|
528
273
|
});
|
|
274
|
+
return;
|
|
529
275
|
}
|
|
530
|
-
|
|
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
|
-
|
|
300
|
+
|
|
301
|
+
// src/runtime/vm-context.ts
|
|
302
|
+
import { z } from "@bpinternal/zui";
|
|
303
|
+
|
|
304
|
+
// src/runtime/tool-wrapper.ts
|
|
305
|
+
import ms from "ms";
|
|
306
|
+
import { ulid } from "ulid";
|
|
307
|
+
var SLOW_TOOL_WARNING = ms("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) => tool.zInput.safeParse(input).data ?? input;
|
|
540
|
-
return function(input) {
|
|
319
|
+
return async function(input) {
|
|
541
320
|
const toolCallId = `tcall_${ulid()}`;
|
|
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(
|
|
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
|
-
|
|
560
|
-
|
|
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 (
|
|
564
|
-
|
|
358
|
+
if (err instanceof 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
|
-
|
|
365
|
+
err.message = Signals.serializeError(err);
|
|
571
366
|
}
|
|
572
|
-
if (
|
|
367
|
+
if (err instanceof 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 =
|
|
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:
|
|
609
|
-
output
|
|
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
|
-
|
|
387
|
+
output = afterRes.output;
|
|
616
388
|
}
|
|
617
|
-
return
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
tool_name: tool.name,
|
|
652
|
-
object,
|
|
653
|
-
input: getToolInput(input),
|
|
654
|
-
output,
|
|
655
|
-
error,
|
|
656
|
-
success
|
|
657
|
-
});
|
|
658
|
-
});
|
|
389
|
+
return true;
|
|
390
|
+
}
|
|
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;
|
|
659
423
|
}
|
|
660
|
-
success = true;
|
|
661
|
-
output = result;
|
|
662
424
|
} catch (err) {
|
|
663
|
-
if (!handleSignals(err)) {
|
|
425
|
+
if (!await handleSignals(err)) {
|
|
664
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 (
|
|
685
|
-
throw
|
|
436
|
+
if (signalToThrow) {
|
|
437
|
+
throw signalToThrow;
|
|
686
438
|
}
|
|
687
439
|
return output;
|
|
688
440
|
};
|
|
689
441
|
}
|
|
690
|
-
|
|
691
|
-
|
|
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 = { ...stripInvalidIdentifiers(iteration.variables) };
|
|
453
|
+
for (const obj of iteration.objects) {
|
|
454
|
+
const internalValues = {};
|
|
455
|
+
const instance = {};
|
|
456
|
+
for (const { name, value, writable, type } of obj.properties ?? []) {
|
|
457
|
+
internalValues[name] = value;
|
|
458
|
+
const initialValue = value;
|
|
459
|
+
const schema = type ?? 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 (isEqual_default(value2, internalValues[name])) {
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
if (!writable) {
|
|
471
|
+
throw new 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 AssignmentError(
|
|
476
|
+
`Invalid value for Object property ${obj.name}.${name}: ${getErrorMessage(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 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, ...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 init();
|
|
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 = props.client ?? new Client();
|
|
540
|
+
const cognitive = Cognitive.isCognitiveClient(client) ? client : CognitiveBeta.isBetaClient(client) ? cognitiveFromBeta(client) : new Cognitive({ client, __experimental_beta: true });
|
|
541
|
+
const ctx = new 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 ErrorExecutionResult(ctx, new 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: controller.signal.reason ?? "The operation was aborted"
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
});
|
|
588
|
+
return new ErrorExecutionResult(ctx, 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 ErrorExecutionResult(ctx, 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
|
-
|
|
699
|
-
iteration
|
|
634
|
+
await finalizeIteration({
|
|
635
|
+
iteration,
|
|
636
|
+
controller,
|
|
637
|
+
onIterationEnd,
|
|
638
|
+
status: err instanceof 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: ${getErrorMessage(err)}`,
|
|
648
|
+
stack: cleanStackTrace(
|
|
649
|
+
err instanceof Error ? 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 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: " + getErrorMessage(err),
|
|
691
|
+
stack: cleanStackTrace(
|
|
692
|
+
err instanceof Error ? err.stack ?? "No stack trace available" : "No stack trace available"
|
|
693
|
+
)
|
|
694
|
+
}
|
|
695
|
+
} : void 0
|
|
705
696
|
});
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
697
|
+
return { type: "proceed" };
|
|
698
|
+
}
|
|
699
|
+
if (!controller.signal.aborted) {
|
|
700
|
+
return {
|
|
701
|
+
type: "return",
|
|
702
|
+
result: new 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: controller.signal.reason ?? "The operation was aborted"
|
|
713
|
+
}
|
|
714
|
+
} : void 0
|
|
715
|
+
});
|
|
716
|
+
return {
|
|
717
|
+
type: "return",
|
|
718
|
+
result: new ErrorExecutionResult(ctx, 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 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 PartialExecutionResult(
|
|
740
|
+
ctx,
|
|
741
|
+
iteration.status.callback_requested.signal,
|
|
742
|
+
Snapshot.fromSignal(iteration.status.callback_requested.signal)
|
|
743
|
+
)
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
if (iteration.status.type === "aborted") {
|
|
747
|
+
return {
|
|
748
|
+
type: "return",
|
|
749
|
+
result: new ErrorExecutionResult(ctx, 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 ErrorExecutionResult(ctx, 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 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
|
|
716
|
-
stack: cleanStackTrace(
|
|
791
|
+
message: `Error in onBeforeExecution hook: ${getErrorMessage(err)}`,
|
|
792
|
+
stack: cleanStackTrace(
|
|
793
|
+
err instanceof Error ? 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: controller.signal.reason ?? "The operation was aborted"
|
|
812
|
+
}
|
|
813
|
+
});
|
|
814
|
+
return;
|
|
815
|
+
}
|
|
816
|
+
const startedAt = Date.now();
|
|
817
|
+
const result = await runAsyncFunction(
|
|
818
|
+
vmContext,
|
|
819
|
+
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(getErrorMessage(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
|
export {
|
|
724
|
-
_executeContext,
|
|
725
841
|
executeContext
|
|
726
842
|
};
|