llmz 0.0.81 → 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-BSI7Y6YH.js → execute-CJVHUFYI.js} +536 -416
- package/dist/{llmz-7TDUOEQE.cjs → execute-R6R3A42S.cjs} +543 -423
- 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} +13 -14
- 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 +2 -2
- 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,35 +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 { Cognitive } from "@botpress/cognitive";
|
|
49
|
-
import { z } from "@bpinternal/zui";
|
|
50
|
-
import ms from "ms";
|
|
51
|
-
import { ulid } from "ulid";
|
|
48
|
+
import { Cognitive, CognitiveBeta, cognitiveFromBeta } from "@botpress/cognitive";
|
|
52
49
|
|
|
53
50
|
// src/abort-signal.ts
|
|
54
51
|
function createJoinedAbortController(signals) {
|
|
@@ -79,9 +76,7 @@ function createJoinedAbortController(signals) {
|
|
|
79
76
|
return controller;
|
|
80
77
|
}
|
|
81
78
|
|
|
82
|
-
// src/
|
|
83
|
-
var getErrorMessage = (err) => err instanceof Error ? err.message : JSON.stringify(err);
|
|
84
|
-
var SLOW_TOOL_WARNING = ms("15s");
|
|
79
|
+
// src/runtime/generate.ts
|
|
85
80
|
var RESPONSE_LENGTH_BUFFER = {
|
|
86
81
|
MIN_TOKENS: 1e3,
|
|
87
82
|
MAX_TOKENS: 16e3,
|
|
@@ -92,141 +87,9 @@ var getModelOutputLimit = (inputLength) => clamp_default(
|
|
|
92
87
|
RESPONSE_LENGTH_BUFFER.MIN_TOKENS,
|
|
93
88
|
RESPONSE_LENGTH_BUFFER.MAX_TOKENS
|
|
94
89
|
);
|
|
95
|
-
var
|
|
96
|
-
var _a, _b;
|
|
97
|
-
|
|
98
|
-
const result = await _executeContext(props);
|
|
99
|
-
try {
|
|
100
|
-
(_b = (_a = result.context.chat) == null ? void 0 : _a.onExecutionDone) == null ? void 0 : _b.call(_a, result);
|
|
101
|
-
} catch {
|
|
102
|
-
}
|
|
103
|
-
return result;
|
|
104
|
-
};
|
|
105
|
-
var _executeContext = async (props) => {
|
|
106
|
-
var _a, _b;
|
|
107
|
-
const controller = createJoinedAbortController([props.signal]);
|
|
108
|
-
const { onIterationStart, onIterationEnd, onTrace, onExit, onBeforeExecution, onAfterTool, onBeforeTool } = props;
|
|
109
|
-
const client = props.client ?? new Client();
|
|
110
|
-
const cognitive = Cognitive.isCognitiveClient(client) ? client : new Cognitive({ client, __experimental_beta: true });
|
|
111
|
-
const cleanups = [];
|
|
112
|
-
const ctx = new Context({
|
|
113
|
-
chat: props.chat,
|
|
114
|
-
instructions: props.instructions,
|
|
115
|
-
objects: props.objects,
|
|
116
|
-
tools: props.tools,
|
|
117
|
-
loop: (_a = props.options) == null ? void 0 : _a.loop,
|
|
118
|
-
timeout: (_b = props.options) == null ? void 0 : _b.timeout,
|
|
119
|
-
exits: props.exits,
|
|
120
|
-
snapshot: props.snapshot,
|
|
121
|
-
model: props.model,
|
|
122
|
-
temperature: props.temperature,
|
|
123
|
-
reasoningEffort: props.reasoningEffort
|
|
124
|
-
});
|
|
125
|
-
try {
|
|
126
|
-
while (true) {
|
|
127
|
-
if (ctx.iterations.length >= ctx.loop) {
|
|
128
|
-
return new ErrorExecutionResult(ctx, new LoopExceededError());
|
|
129
|
-
}
|
|
130
|
-
const iteration = await ctx.nextIteration();
|
|
131
|
-
try {
|
|
132
|
-
await executeOnIterationStartHook({
|
|
133
|
-
iteration,
|
|
134
|
-
ctx,
|
|
135
|
-
onIterationStart,
|
|
136
|
-
controller,
|
|
137
|
-
onIterationEnd
|
|
138
|
-
});
|
|
139
|
-
} catch (err) {
|
|
140
|
-
if (err instanceof ThinkSignal) {
|
|
141
|
-
continue;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
if (controller.signal.aborted) {
|
|
145
|
-
iteration.end({
|
|
146
|
-
type: "aborted",
|
|
147
|
-
aborted: {
|
|
148
|
-
reason: controller.signal.reason ?? "The operation was aborted"
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
return new ErrorExecutionResult(ctx, controller.signal.reason ?? "The operation was aborted");
|
|
152
|
-
}
|
|
153
|
-
cleanups.push(
|
|
154
|
-
iteration.traces.onPush((traces) => {
|
|
155
|
-
for (const trace of traces) {
|
|
156
|
-
onTrace == null ? void 0 : onTrace({ trace, iteration: ctx.iterations.length });
|
|
157
|
-
}
|
|
158
|
-
})
|
|
159
|
-
);
|
|
160
|
-
try {
|
|
161
|
-
await executeIteration({
|
|
162
|
-
iteration,
|
|
163
|
-
ctx,
|
|
164
|
-
cognitive,
|
|
165
|
-
controller,
|
|
166
|
-
onExit,
|
|
167
|
-
onBeforeExecution,
|
|
168
|
-
onAfterTool,
|
|
169
|
-
onBeforeTool
|
|
170
|
-
});
|
|
171
|
-
} catch (err) {
|
|
172
|
-
if (err instanceof CognitiveError) {
|
|
173
|
-
return new ErrorExecutionResult(ctx, err);
|
|
174
|
-
}
|
|
175
|
-
iteration.end({
|
|
176
|
-
type: "execution_error",
|
|
177
|
-
execution_error: {
|
|
178
|
-
message: "An unexpected error occurred: " + getErrorMessage(err),
|
|
179
|
-
stack: cleanStackTrace(err.stack ?? "No stack trace available")
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
try {
|
|
184
|
-
await (onIterationEnd == null ? void 0 : onIterationEnd(iteration, controller));
|
|
185
|
-
} catch (err) {
|
|
186
|
-
console.error(err);
|
|
187
|
-
}
|
|
188
|
-
if (iteration.status.type === "exit_success") {
|
|
189
|
-
const exitName = iteration.status.exit_success.exit_name;
|
|
190
|
-
return new SuccessExecutionResult(ctx, {
|
|
191
|
-
exit: iteration.exits.find((x) => x.name === exitName),
|
|
192
|
-
result: iteration.status.exit_success.return_value
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
if (iteration.status.type === "callback_requested") {
|
|
196
|
-
return new PartialExecutionResult(
|
|
197
|
-
ctx,
|
|
198
|
-
iteration.status.callback_requested.signal,
|
|
199
|
-
Snapshot.fromSignal(iteration.status.callback_requested.signal)
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
if (iteration.status.type === "thinking_requested" || iteration.status.type === "exit_error" || iteration.status.type === "execution_error" || iteration.status.type === "invalid_code_error") {
|
|
203
|
-
continue;
|
|
204
|
-
}
|
|
205
|
-
return new ErrorExecutionResult(ctx, iteration.error ?? `Unknown error. Status: ${iteration.status.type}`);
|
|
206
|
-
}
|
|
207
|
-
} catch (error) {
|
|
208
|
-
return new ErrorExecutionResult(ctx, error ?? "Unknown error");
|
|
209
|
-
} finally {
|
|
210
|
-
for (const cleanup of cleanups) {
|
|
211
|
-
try {
|
|
212
|
-
cleanup();
|
|
213
|
-
} catch {
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
};
|
|
218
|
-
var executeIteration = async ({
|
|
219
|
-
iteration,
|
|
220
|
-
ctx,
|
|
221
|
-
cognitive,
|
|
222
|
-
controller,
|
|
223
|
-
onExit,
|
|
224
|
-
onBeforeExecution,
|
|
225
|
-
onBeforeTool,
|
|
226
|
-
onAfterTool
|
|
227
|
-
}) => {
|
|
228
|
-
var _a, _b, _c, _d;
|
|
229
|
-
let startedAt = Date.now();
|
|
90
|
+
var generateCode = async ({ iteration, ctx, cognitive, controller }) => {
|
|
91
|
+
var _a, _b, _c;
|
|
92
|
+
const startedAt = Date.now();
|
|
230
93
|
const traces = iteration.traces;
|
|
231
94
|
const modelRef = Array.isArray(iteration.model) ? iteration.model[0] : iteration.model;
|
|
232
95
|
const model = await cognitive.getModelDetails(modelRef).catch((thrown) => {
|
|
@@ -238,13 +101,7 @@ var executeIteration = async ({
|
|
|
238
101
|
messages: iteration.messages,
|
|
239
102
|
tokenLimit: modelLimit - responseLengthBuffer,
|
|
240
103
|
throwOnFailure: true
|
|
241
|
-
}).filter(
|
|
242
|
-
(x) => (
|
|
243
|
-
// Filter out empty messages, as they are not valid inputs for the LLM
|
|
244
|
-
// This can happen when a message is truncated and the content is empty
|
|
245
|
-
typeof x.content !== "string" || x.content.trim().length > 0
|
|
246
|
-
)
|
|
247
|
-
);
|
|
104
|
+
}).filter((x) => typeof x.content !== "string" || x.content.trim().length > 0);
|
|
248
105
|
iteration.messages = messages;
|
|
249
106
|
traces.push({
|
|
250
107
|
type: "llm_call_started",
|
|
@@ -255,8 +112,6 @@ var executeIteration = async ({
|
|
|
255
112
|
const output = await cognitive.generateContent({
|
|
256
113
|
signal: controller.signal,
|
|
257
114
|
systemPrompt: (_a = messages.find((x) => x.role === "system")) == null ? void 0 : _a.content,
|
|
258
|
-
// Validated upstream in Context (see context.ts isValidModel). Cast narrows
|
|
259
|
-
// Models' `({} & string)` escape hatch down to cognitive's stricter InputModel.
|
|
260
115
|
model: iteration.model,
|
|
261
116
|
temperature: iteration.temperature,
|
|
262
117
|
responseFormat: "text",
|
|
@@ -272,31 +127,6 @@ var executeIteration = async ({
|
|
|
272
127
|
}
|
|
273
128
|
const assistantResponse = ctx.version.parseAssistantResponse(out);
|
|
274
129
|
iteration.code = assistantResponse.code.trim();
|
|
275
|
-
if (typeof onBeforeExecution === "function") {
|
|
276
|
-
try {
|
|
277
|
-
const hookRes = await onBeforeExecution(iteration, controller);
|
|
278
|
-
if (typeof (hookRes == null ? void 0 : hookRes.code) === "string" && hookRes.code.trim().length > 0) {
|
|
279
|
-
iteration.code = hookRes.code.trim();
|
|
280
|
-
}
|
|
281
|
-
} catch (err) {
|
|
282
|
-
if (err instanceof ThinkSignal) {
|
|
283
|
-
return iteration.end({
|
|
284
|
-
type: "thinking_requested",
|
|
285
|
-
thinking_requested: {
|
|
286
|
-
variables: err.context,
|
|
287
|
-
reason: err.reason
|
|
288
|
-
}
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
return iteration.end({
|
|
292
|
-
type: "execution_error",
|
|
293
|
-
execution_error: {
|
|
294
|
-
message: `Error in onBeforeExecution hook: ${getErrorMessage(err)}`,
|
|
295
|
-
stack: cleanStackTrace(err.stack ?? "No stack trace available")
|
|
296
|
-
}
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
130
|
iteration.llm = {
|
|
301
131
|
cached: output.meta.cached || false,
|
|
302
132
|
ended_at: Date.now(),
|
|
@@ -315,149 +145,63 @@ var executeIteration = async ({
|
|
|
315
145
|
model: model.ref,
|
|
316
146
|
code: iteration.code
|
|
317
147
|
});
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
get() {
|
|
330
|
-
return internalValues[name];
|
|
331
|
-
},
|
|
332
|
-
set(value2) {
|
|
333
|
-
if (isEqual_default(value2, internalValues[name])) {
|
|
334
|
-
return;
|
|
335
|
-
}
|
|
336
|
-
if (!writable) {
|
|
337
|
-
throw new AssignmentError(`Property ${obj.name}.${name} is read-only and cannot be modified`);
|
|
338
|
-
}
|
|
339
|
-
if (value2 === internalValues[name]) {
|
|
340
|
-
return;
|
|
341
|
-
}
|
|
342
|
-
const parsed = schema.safeParse(value2);
|
|
343
|
-
if (!parsed.success) {
|
|
344
|
-
throw new AssignmentError(
|
|
345
|
-
`Invalid value for Object property ${obj.name}.${name}: ${getErrorMessage(parsed.error)}`
|
|
346
|
-
);
|
|
347
|
-
}
|
|
348
|
-
internalValues[name] = parsed.data;
|
|
349
|
-
traces.push({
|
|
350
|
-
type: "property",
|
|
351
|
-
started_at: Date.now(),
|
|
352
|
-
object: obj.name,
|
|
353
|
-
property: name,
|
|
354
|
-
value: parsed.data
|
|
355
|
-
});
|
|
356
|
-
iteration.trackMutation({ object: obj.name, property: name, before: initialValue, after: parsed.data });
|
|
357
|
-
}
|
|
358
|
-
});
|
|
359
|
-
}
|
|
360
|
-
for (const tool of obj.tools ?? []) {
|
|
361
|
-
instance[tool.name] = wrapTool({
|
|
362
|
-
chat: ctx.chat,
|
|
363
|
-
tool,
|
|
364
|
-
traces,
|
|
365
|
-
object: obj.name,
|
|
366
|
-
iteration,
|
|
367
|
-
beforeHook: onBeforeTool,
|
|
368
|
-
afterHook: onAfterTool,
|
|
369
|
-
controller
|
|
370
|
-
});
|
|
371
|
-
}
|
|
372
|
-
Object.preventExtensions(instance);
|
|
373
|
-
Object.seal(instance);
|
|
374
|
-
vmContext[obj.name] = instance;
|
|
375
|
-
}
|
|
376
|
-
for (const tool of iteration.tools) {
|
|
377
|
-
const wrapped = wrapTool({
|
|
378
|
-
chat: ctx.chat,
|
|
379
|
-
tool,
|
|
380
|
-
traces,
|
|
381
|
-
iteration,
|
|
382
|
-
beforeHook: onBeforeTool,
|
|
383
|
-
afterHook: onAfterTool,
|
|
384
|
-
controller
|
|
385
|
-
});
|
|
386
|
-
for (const key of [tool.name, ...tool.aliases ?? []]) {
|
|
387
|
-
vmContext[key] = wrapped;
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
if (controller.signal.aborted) {
|
|
391
|
-
traces.push({
|
|
392
|
-
type: "abort_signal",
|
|
393
|
-
started_at: Date.now(),
|
|
394
|
-
reason: "The operation was aborted by user."
|
|
395
|
-
});
|
|
396
|
-
return iteration.end({
|
|
397
|
-
type: "aborted",
|
|
398
|
-
aborted: {
|
|
399
|
-
reason: controller.signal.reason ?? "The operation was aborted"
|
|
400
|
-
}
|
|
401
|
-
});
|
|
402
|
-
}
|
|
403
|
-
startedAt = Date.now();
|
|
404
|
-
const result = await runAsyncFunction(
|
|
405
|
-
vmContext,
|
|
406
|
-
iteration.code,
|
|
407
|
-
traces,
|
|
408
|
-
controller.signal,
|
|
409
|
-
ctx.timeout
|
|
410
|
-
).catch((err) => {
|
|
411
|
-
return {
|
|
412
|
-
success: false,
|
|
413
|
-
error: err,
|
|
414
|
-
lines_executed: [],
|
|
415
|
-
traces: [],
|
|
416
|
-
variables: {}
|
|
417
|
-
};
|
|
418
|
-
});
|
|
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;
|
|
419
159
|
if (result.error && result.error instanceof InvalidCodeError) {
|
|
420
|
-
|
|
160
|
+
iteration.end({
|
|
421
161
|
type: "invalid_code_error",
|
|
422
162
|
invalid_code_error: {
|
|
423
163
|
message: result.error.message
|
|
424
164
|
}
|
|
425
165
|
});
|
|
166
|
+
return;
|
|
426
167
|
}
|
|
427
|
-
traces.push({
|
|
168
|
+
iteration.traces.push({
|
|
428
169
|
type: "code_execution",
|
|
429
170
|
lines_executed: result.lines_executed ?? 0,
|
|
430
171
|
started_at: startedAt,
|
|
431
172
|
ended_at: Date.now()
|
|
432
173
|
});
|
|
433
174
|
if (controller.signal.aborted) {
|
|
434
|
-
|
|
175
|
+
iteration.end({
|
|
435
176
|
type: "aborted",
|
|
436
177
|
aborted: {
|
|
437
178
|
reason: controller.signal.reason ?? "The operation was aborted"
|
|
438
179
|
}
|
|
439
180
|
});
|
|
181
|
+
return;
|
|
440
182
|
}
|
|
441
183
|
if (result.error && result.error instanceof CodeExecutionError) {
|
|
442
|
-
|
|
184
|
+
iteration.end({
|
|
443
185
|
type: "execution_error",
|
|
444
186
|
execution_error: {
|
|
445
187
|
message: result.error.message,
|
|
446
188
|
stack: cleanStackTrace(result.error.stacktrace ?? result.error.stack ?? "No stack trace available")
|
|
447
189
|
}
|
|
448
190
|
});
|
|
191
|
+
return;
|
|
449
192
|
}
|
|
450
193
|
if (!result.success) {
|
|
451
|
-
|
|
194
|
+
iteration.end({
|
|
452
195
|
type: "execution_error",
|
|
453
196
|
execution_error: {
|
|
454
|
-
message: ((
|
|
197
|
+
message: ((_a = result == null ? void 0 : result.error) == null ? void 0 : _a.message) ?? "Unknown error occurred",
|
|
455
198
|
stack: cleanStackTrace(result.error.stack ?? "No stack trace available")
|
|
456
199
|
}
|
|
457
200
|
});
|
|
201
|
+
return;
|
|
458
202
|
}
|
|
459
203
|
if (result.signal instanceof ThinkSignal) {
|
|
460
|
-
|
|
204
|
+
iteration.end({
|
|
461
205
|
type: "thinking_requested",
|
|
462
206
|
thinking_requested: {
|
|
463
207
|
variables: result.signal.context,
|
|
@@ -465,39 +209,43 @@ var executeIteration = async ({
|
|
|
465
209
|
metadata: result.signal.metadata
|
|
466
210
|
}
|
|
467
211
|
});
|
|
212
|
+
return;
|
|
468
213
|
}
|
|
469
214
|
if (result.signal instanceof SnapshotSignal) {
|
|
470
|
-
|
|
215
|
+
iteration.end({
|
|
471
216
|
type: "callback_requested",
|
|
472
217
|
callback_requested: {
|
|
473
218
|
signal: result.signal
|
|
474
219
|
}
|
|
475
220
|
});
|
|
221
|
+
return;
|
|
476
222
|
}
|
|
477
223
|
let returnValue = result.success && result.return_value ? result.return_value : null;
|
|
478
224
|
const returnAction = returnValue == null ? void 0 : returnValue.action;
|
|
479
225
|
if (returnAction === "think") {
|
|
480
226
|
const variables = omit_default(returnValue ?? {}, "action");
|
|
481
227
|
if (isPlainObject_default(variables) && Object.keys(variables).length > 0) {
|
|
482
|
-
|
|
228
|
+
iteration.end({
|
|
483
229
|
type: "thinking_requested",
|
|
484
230
|
thinking_requested: {
|
|
485
231
|
variables,
|
|
486
232
|
reason: "Thinking requested"
|
|
487
233
|
}
|
|
488
234
|
});
|
|
235
|
+
return;
|
|
489
236
|
}
|
|
490
|
-
|
|
237
|
+
iteration.end({
|
|
491
238
|
type: "thinking_requested",
|
|
492
239
|
thinking_requested: {
|
|
493
240
|
reason: "Thinking requested",
|
|
494
241
|
variables: iteration.variables
|
|
495
242
|
}
|
|
496
243
|
});
|
|
244
|
+
return;
|
|
497
245
|
}
|
|
498
246
|
const parsedExit = parseExit(returnValue, iteration.exits);
|
|
499
247
|
if (!parsedExit.success) {
|
|
500
|
-
|
|
248
|
+
iteration.end({
|
|
501
249
|
type: "exit_error",
|
|
502
250
|
exit_error: {
|
|
503
251
|
exit: (returnValue == null ? void 0 : returnValue.action) ?? "n/a",
|
|
@@ -505,6 +253,7 @@ var executeIteration = async ({
|
|
|
505
253
|
return_value: returnValue
|
|
506
254
|
}
|
|
507
255
|
});
|
|
256
|
+
return;
|
|
508
257
|
}
|
|
509
258
|
const returnExit = parsedExit.exit;
|
|
510
259
|
returnValue = { action: returnExit.name, value: parsedExit.value };
|
|
@@ -514,7 +263,7 @@ var executeIteration = async ({
|
|
|
514
263
|
result: returnValue == null ? void 0 : returnValue.value
|
|
515
264
|
}));
|
|
516
265
|
} catch (err) {
|
|
517
|
-
|
|
266
|
+
iteration.end({
|
|
518
267
|
type: "exit_error",
|
|
519
268
|
exit_error: {
|
|
520
269
|
exit: returnExit.name,
|
|
@@ -522,50 +271,101 @@ var executeIteration = async ({
|
|
|
522
271
|
return_value: returnValue
|
|
523
272
|
}
|
|
524
273
|
});
|
|
274
|
+
return;
|
|
525
275
|
}
|
|
526
|
-
|
|
276
|
+
iteration.end({
|
|
527
277
|
type: "exit_success",
|
|
528
278
|
exit_success: {
|
|
529
279
|
exit_name: returnExit.name,
|
|
530
280
|
return_value: returnValue == null ? void 0 : returnValue.value
|
|
531
281
|
}
|
|
532
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
|
+
}
|
|
533
299
|
};
|
|
534
|
-
|
|
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
|
+
}) {
|
|
535
318
|
const getToolInput = (input) => tool.zInput.safeParse(input).data ?? input;
|
|
536
|
-
return function(input) {
|
|
319
|
+
return async function(input) {
|
|
537
320
|
const toolCallId = `tcall_${ulid()}`;
|
|
321
|
+
const originalInput = input;
|
|
322
|
+
let effectiveInput = input;
|
|
538
323
|
const alertSlowTool = setTimeout(
|
|
539
324
|
() => traces.push({
|
|
540
325
|
type: "tool_slow",
|
|
541
326
|
tool_name: tool.name,
|
|
542
327
|
tool_call_id: toolCallId,
|
|
543
328
|
started_at: Date.now(),
|
|
544
|
-
input: getToolInput(
|
|
329
|
+
input: getToolInput(originalInput),
|
|
545
330
|
object,
|
|
546
331
|
duration: SLOW_TOOL_WARNING
|
|
547
332
|
}),
|
|
548
333
|
SLOW_TOOL_WARNING
|
|
549
334
|
);
|
|
550
|
-
const cancelSlowTool = () => clearTimeout(alertSlowTool);
|
|
551
335
|
const toolStart = Date.now();
|
|
552
336
|
let output;
|
|
553
337
|
let error;
|
|
554
338
|
let success = true;
|
|
555
|
-
|
|
556
|
-
|
|
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) {
|
|
557
356
|
return true;
|
|
558
357
|
}
|
|
559
|
-
if (
|
|
560
|
-
|
|
358
|
+
if (err instanceof SnapshotSignal) {
|
|
359
|
+
err.toolCall = {
|
|
561
360
|
name: tool.name,
|
|
562
361
|
inputSchema: tool.input,
|
|
563
362
|
outputSchema: tool.output,
|
|
564
|
-
input
|
|
363
|
+
input: originalInput
|
|
565
364
|
};
|
|
566
|
-
|
|
365
|
+
err.message = Signals.serializeError(err);
|
|
567
366
|
}
|
|
568
|
-
if (
|
|
367
|
+
if (err instanceof ThinkSignal) {
|
|
368
|
+
signalToThrow = err;
|
|
569
369
|
traces.push({
|
|
570
370
|
type: "think_signal",
|
|
571
371
|
started_at: Date.now(),
|
|
@@ -573,150 +373,470 @@ function wrapTool({ chat, tool, traces, object, iteration, beforeHook, afterHook
|
|
|
573
373
|
ended_at: Date.now()
|
|
574
374
|
});
|
|
575
375
|
success = true;
|
|
576
|
-
output =
|
|
577
|
-
return true;
|
|
578
|
-
}
|
|
579
|
-
return false;
|
|
580
|
-
};
|
|
581
|
-
try {
|
|
582
|
-
const withHooks = async (input2) => {
|
|
583
|
-
const beforeRes = await (beforeHook == null ? void 0 : beforeHook({
|
|
584
|
-
iteration,
|
|
585
|
-
tool,
|
|
586
|
-
input: input2,
|
|
587
|
-
controller,
|
|
588
|
-
object,
|
|
589
|
-
toolCallId
|
|
590
|
-
}));
|
|
591
|
-
if (typeof (beforeRes == null ? void 0 : beforeRes.input) !== "undefined") {
|
|
592
|
-
input2 = beforeRes.input;
|
|
593
|
-
}
|
|
594
|
-
let output2 = await tool.execute(
|
|
595
|
-
input2,
|
|
596
|
-
{
|
|
597
|
-
callId: toolCallId
|
|
598
|
-
},
|
|
599
|
-
chat
|
|
600
|
-
);
|
|
376
|
+
output = err;
|
|
601
377
|
const afterRes = await (afterHook == null ? void 0 : afterHook({
|
|
602
378
|
iteration,
|
|
603
379
|
tool,
|
|
604
|
-
input:
|
|
605
|
-
output
|
|
380
|
+
input: originalInput,
|
|
381
|
+
output,
|
|
606
382
|
controller,
|
|
607
383
|
object,
|
|
608
384
|
toolCallId
|
|
609
385
|
}));
|
|
610
386
|
if (typeof (afterRes == null ? void 0 : afterRes.output) !== "undefined") {
|
|
611
|
-
|
|
387
|
+
output = afterRes.output;
|
|
612
388
|
}
|
|
613
|
-
return
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
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
|
-
tool_name: tool.name,
|
|
648
|
-
object,
|
|
649
|
-
input: getToolInput(input),
|
|
650
|
-
output,
|
|
651
|
-
error,
|
|
652
|
-
success
|
|
653
|
-
});
|
|
654
|
-
});
|
|
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;
|
|
655
423
|
}
|
|
656
|
-
success = true;
|
|
657
|
-
output = result;
|
|
658
424
|
} catch (err) {
|
|
659
|
-
if (!handleSignals(err)) {
|
|
425
|
+
if (!await handleSignals(err)) {
|
|
660
426
|
success = false;
|
|
661
427
|
error = err;
|
|
662
428
|
}
|
|
429
|
+
} finally {
|
|
430
|
+
clearTimeout(alertSlowTool);
|
|
431
|
+
pushToolCallTrace();
|
|
663
432
|
}
|
|
664
|
-
cancelSlowTool();
|
|
665
|
-
traces.push({
|
|
666
|
-
type: "tool_call",
|
|
667
|
-
tool_call_id: toolCallId,
|
|
668
|
-
started_at: toolStart,
|
|
669
|
-
ended_at: Date.now(),
|
|
670
|
-
tool_name: tool.name,
|
|
671
|
-
object,
|
|
672
|
-
input: getToolInput(input),
|
|
673
|
-
output,
|
|
674
|
-
error,
|
|
675
|
-
success
|
|
676
|
-
});
|
|
677
433
|
if (!success) {
|
|
678
434
|
throw error;
|
|
679
435
|
}
|
|
680
|
-
if (
|
|
681
|
-
throw
|
|
436
|
+
if (signalToThrow) {
|
|
437
|
+
throw signalToThrow;
|
|
682
438
|
}
|
|
683
439
|
return output;
|
|
684
440
|
};
|
|
685
441
|
}
|
|
686
|
-
|
|
687
|
-
|
|
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
|
+
}) => {
|
|
688
627
|
try {
|
|
689
628
|
const hookRes = await (onIterationStart == null ? void 0 : onIterationStart(iteration, controller, ctx));
|
|
690
629
|
if (hookRes) {
|
|
691
630
|
Object.assign(iteration, hookRes);
|
|
692
631
|
}
|
|
632
|
+
return { type: "proceed" };
|
|
693
633
|
} catch (err) {
|
|
694
|
-
|
|
695
|
-
iteration
|
|
634
|
+
await finalizeIteration({
|
|
635
|
+
iteration,
|
|
636
|
+
controller,
|
|
637
|
+
onIterationEnd,
|
|
638
|
+
status: err instanceof ThinkSignal ? {
|
|
696
639
|
type: "thinking_requested",
|
|
697
640
|
thinking_requested: {
|
|
698
641
|
variables: err.context,
|
|
699
642
|
reason: err.reason
|
|
700
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
|
|
701
696
|
});
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
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;
|
|
706
787
|
}
|
|
707
|
-
} else {
|
|
708
788
|
iteration.end({
|
|
709
789
|
type: "execution_error",
|
|
710
790
|
execution_error: {
|
|
711
|
-
message: `Error in
|
|
712
|
-
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
|
+
)
|
|
713
795
|
}
|
|
714
796
|
});
|
|
797
|
+
return;
|
|
715
798
|
}
|
|
716
|
-
throw err;
|
|
717
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
|
+
});
|
|
718
839
|
};
|
|
719
840
|
export {
|
|
720
|
-
_executeContext,
|
|
721
841
|
executeContext
|
|
722
842
|
};
|