llmz 0.0.6 → 0.0.7

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 (94) hide show
  1. package/README.md +0 -4
  2. package/package.json +5 -3
  3. package/dist/chat.d.ts +0 -17
  4. package/dist/chunk-4I7UPBLN.cjs +0 -104
  5. package/dist/chunk-4QTK5ALH.cjs +0 -211
  6. package/dist/chunk-5TRUJES5.js +0 -220
  7. package/dist/chunk-6WT5VZBG.cjs +0 -755
  8. package/dist/chunk-7WRN4E42.js +0 -3087
  9. package/dist/chunk-BEPRLBPK.cjs +0 -3123
  10. package/dist/chunk-CH5T6SPC.cjs +0 -694
  11. package/dist/chunk-D3ESDRLH.js +0 -3123
  12. package/dist/chunk-EJRCDWBA.cjs +0 -153
  13. package/dist/chunk-FIVFS4HG.cjs +0 -152
  14. package/dist/chunk-HP7RKM25.js +0 -153
  15. package/dist/chunk-HYVKY3W6.cjs +0 -283
  16. package/dist/chunk-IKSIOIIP.cjs +0 -28
  17. package/dist/chunk-JDABP4SD.cjs +0 -161
  18. package/dist/chunk-JKVVQN2P.js +0 -161
  19. package/dist/chunk-JQBT7UWN.js +0 -28
  20. package/dist/chunk-KMZDFWYZ.cjs +0 -33
  21. package/dist/chunk-ORQP26SZ.js +0 -33
  22. package/dist/chunk-P7J2WCBB.cjs +0 -220
  23. package/dist/chunk-QBXIE6EY.js +0 -211
  24. package/dist/chunk-QT4QF3YA.js +0 -152
  25. package/dist/chunk-S6FOL2HY.js +0 -104
  26. package/dist/chunk-UNMKB2DA.js +0 -283
  27. package/dist/chunk-UQOBUJIQ.cjs +0 -3087
  28. package/dist/chunk-WLUVY5QU.js +0 -755
  29. package/dist/chunk-XJNRORSK.js +0 -694
  30. package/dist/citations.d.ts +0 -63
  31. package/dist/compiler/compiler.d.ts +0 -28
  32. package/dist/compiler/index.d.ts +0 -2
  33. package/dist/compiler/plugins/async-iterator.d.ts +0 -4
  34. package/dist/compiler/plugins/braces-tsx.d.ts +0 -3
  35. package/dist/compiler/plugins/jsx-preserve-newlines.d.ts +0 -5
  36. package/dist/compiler/plugins/line-tracking.d.ts +0 -5
  37. package/dist/compiler/plugins/replace-comment.d.ts +0 -5
  38. package/dist/compiler/plugins/return-async.d.ts +0 -4
  39. package/dist/compiler/plugins/track-tool-calls.d.ts +0 -16
  40. package/dist/compiler/plugins/variable-extraction.d.ts +0 -5
  41. package/dist/component-R2Y74VUI.js +0 -16
  42. package/dist/component-TSNW3SC7.cjs +0 -16
  43. package/dist/component.d.ts +0 -60
  44. package/dist/component.default.d.ts +0 -316
  45. package/dist/context.d.ts +0 -177
  46. package/dist/dual-modes-UHNDHNIF.cjs +0 -12
  47. package/dist/dual-modes-ZUQKPJFH.js +0 -12
  48. package/dist/errors.d.ts +0 -64
  49. package/dist/exit-KJ4COC5N.cjs +0 -8
  50. package/dist/exit-OIYZLBVJ.js +0 -8
  51. package/dist/exit.d.ts +0 -26
  52. package/dist/formatting.d.ts +0 -5
  53. package/dist/getter.d.ts +0 -2
  54. package/dist/handlers.d.ts +0 -10
  55. package/dist/hoist.d.ts +0 -2
  56. package/dist/index.cjs +0 -919
  57. package/dist/index.d.ts +0 -22
  58. package/dist/index.js +0 -919
  59. package/dist/inspect.d.ts +0 -6
  60. package/dist/jsx-AEHVFB3L.js +0 -13
  61. package/dist/jsx-AJAXBWFE.cjs +0 -13
  62. package/dist/jsx.d.ts +0 -11
  63. package/dist/llmz-CPMVYIOJ.cjs +0 -581
  64. package/dist/llmz-LAN4WDFW.js +0 -581
  65. package/dist/llmz.d.ts +0 -40
  66. package/dist/objects.d.ts +0 -24
  67. package/dist/openai.d.ts +0 -8
  68. package/dist/prompts/chat-mode/system.md.d.ts +0 -2
  69. package/dist/prompts/chat-mode/user.md.d.ts +0 -2
  70. package/dist/prompts/common.d.ts +0 -6
  71. package/dist/prompts/dual-modes.d.ts +0 -2
  72. package/dist/prompts/prompt.d.ts +0 -50
  73. package/dist/prompts/worker-mode/system.md.d.ts +0 -2
  74. package/dist/prompts/worker-mode/user.md.d.ts +0 -2
  75. package/dist/result.d.ts +0 -33
  76. package/dist/snapshots.d.ts +0 -69
  77. package/dist/stack-traces.d.ts +0 -1
  78. package/dist/tool-LY77IWV2.js +0 -11
  79. package/dist/tool-SWJYOR2Z.cjs +0 -11
  80. package/dist/tool.d.ts +0 -55
  81. package/dist/transcript.d.ts +0 -9
  82. package/dist/truncator-NYN7BGKJ.cjs +0 -10
  83. package/dist/truncator-WAL2GCUY.js +0 -10
  84. package/dist/truncator.d.ts +0 -21
  85. package/dist/types.d.ts +0 -105
  86. package/dist/typings-AIV2OASX.js +0 -10
  87. package/dist/typings-XR6CYHW4.cjs +0 -10
  88. package/dist/typings.d.ts +0 -5
  89. package/dist/utils-L5GLCS3C.js +0 -37
  90. package/dist/utils-QC4I2L6R.cjs +0 -37
  91. package/dist/utils.d.ts +0 -21
  92. package/dist/vm-2DLG7V4G.cjs +0 -12
  93. package/dist/vm-FLBMZUA2.js +0 -12
  94. package/dist/vm.d.ts +0 -3
package/dist/inspect.d.ts DELETED
@@ -1,6 +0,0 @@
1
- type PreviewOptions = {
2
- tokens: number;
3
- };
4
- export declare function extractType(value: unknown, generic?: boolean): string;
5
- export declare const inspect: (value: unknown, name?: string, options?: PreviewOptions) => string;
6
- export {};
@@ -1,13 +0,0 @@
1
- import {
2
- Jsx,
3
- createJsxComponent,
4
- isAnyJsxComponent,
5
- isJsxComponent
6
- } from "./chunk-ORQP26SZ.js";
7
- import "./chunk-7WRN4E42.js";
8
- export {
9
- Jsx,
10
- createJsxComponent,
11
- isAnyJsxComponent,
12
- isJsxComponent
13
- };
@@ -1,13 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
-
3
-
4
-
5
-
6
- var _chunkKMZDFWYZcjs = require('./chunk-KMZDFWYZ.cjs');
7
- require('./chunk-UQOBUJIQ.cjs');
8
-
9
-
10
-
11
-
12
-
13
- exports.Jsx = _chunkKMZDFWYZcjs.Jsx; exports.createJsxComponent = _chunkKMZDFWYZcjs.createJsxComponent; exports.isAnyJsxComponent = _chunkKMZDFWYZcjs.isAnyJsxComponent; exports.isJsxComponent = _chunkKMZDFWYZcjs.isJsxComponent;
package/dist/jsx.d.ts DELETED
@@ -1,11 +0,0 @@
1
- import { z } from '@bpinternal/zui';
2
- export type JsxComponent<TType extends string = string, TProps extends {} = {}, TChildren = any> = {
3
- __jsx: true;
4
- type: TType;
5
- props: TProps;
6
- children: Array<TChildren>;
7
- };
8
- export declare function isAnyJsxComponent(component: any): component is JsxComponent;
9
- export declare function isJsxComponent<T extends JsxComponent>(type: T['type'], component: any): component is T;
10
- export declare const createJsxComponent: (props: Omit<JsxComponent, "__jsx">) => JsxComponent;
11
- export declare const Jsx: z.Schema<JsxComponent<string, {}, any>, import("@bpinternal/zui").ZodTypeDef, JsxComponent<string, {}, any>>;
@@ -1,581 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
2
-
3
- var _chunkBEPRLBPKcjs = require('./chunk-BEPRLBPK.cjs');
4
-
5
-
6
-
7
-
8
-
9
-
10
- var _chunkCH5T6SPCcjs = require('./chunk-CH5T6SPC.cjs');
11
- require('./chunk-6WT5VZBG.cjs');
12
- require('./chunk-4QTK5ALH.cjs');
13
- require('./chunk-HYVKY3W6.cjs');
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
- var _chunkJDABP4SDcjs = require('./chunk-JDABP4SD.cjs');
24
-
25
-
26
- var _chunkIKSIOIIPcjs = require('./chunk-IKSIOIIP.cjs');
27
- require('./chunk-4I7UPBLN.cjs');
28
- require('./chunk-EJRCDWBA.cjs');
29
- require('./chunk-KMZDFWYZ.cjs');
30
-
31
-
32
- var _chunkFIVFS4HGcjs = require('./chunk-FIVFS4HG.cjs');
33
-
34
-
35
-
36
- var _chunkP7J2WCBBcjs = require('./chunk-P7J2WCBB.cjs');
37
-
38
-
39
-
40
-
41
-
42
- var _chunkUQOBUJIQcjs = require('./chunk-UQOBUJIQ.cjs');
43
-
44
- // src/llmz.ts
45
- var _cognitive = require('@botpress/cognitive');
46
- var _zui = require('@bpinternal/zui');
47
- var _ms = require('ms'); var _ms2 = _interopRequireDefault(_ms);
48
- var _ulid = require('ulid');
49
- var getErrorMessage = (err) => err instanceof Error ? err.message : JSON.stringify(err);
50
- var SLOW_TOOL_WARNING = _ms2.default.call(void 0, "15s");
51
- var RESPONSE_LENGTH_BUFFER = {
52
- MIN_TOKENS: 1e3,
53
- MAX_TOKENS: 16e3,
54
- PERCENTAGE: 0.1
55
- };
56
- var getModelOutputLimit = (inputLength) => _chunkUQOBUJIQcjs.clamp_default.call(void 0,
57
- RESPONSE_LENGTH_BUFFER.PERCENTAGE * inputLength,
58
- RESPONSE_LENGTH_BUFFER.MIN_TOKENS,
59
- RESPONSE_LENGTH_BUFFER.MAX_TOKENS
60
- );
61
- var executeContext = async (props) => {
62
- var _a, _b;
63
- await _chunkP7J2WCBBcjs.init.call(void 0, );
64
- const result = await _executeContext(props);
65
- try {
66
- (_b = (_a = result.context.chat) == null ? void 0 : _a.onExecutionDone) == null ? void 0 : _b.call(_a, result);
67
- } catch (e) {
68
- }
69
- return result;
70
- };
71
- var _executeContext = async (props) => {
72
- var _a, _b, _c, _d;
73
- const { signal, onIterationEnd, onTrace, onExit, onBeforeExecution } = props;
74
- const cognitive = props.client instanceof _cognitive.Cognitive ? props.client : new (0, _cognitive.Cognitive)({ client: props.client });
75
- const cleanups = [];
76
- const ctx = new (0, _chunkCH5T6SPCcjs.Context)({
77
- chat: props.chat,
78
- instructions: props.instructions,
79
- objects: props.objects,
80
- tools: props.tools,
81
- loop: (_a = props.options) == null ? void 0 : _a.loop,
82
- temperature: (_b = props.options) == null ? void 0 : _b.temperature,
83
- model: (_c = props.options) == null ? void 0 : _c.model,
84
- timeout: (_d = props.options) == null ? void 0 : _d.timeout,
85
- exits: props.exits,
86
- snapshot: props.snapshot
87
- });
88
- try {
89
- while (true) {
90
- if (ctx.iterations.length >= ctx.loop) {
91
- return new (0, _chunkCH5T6SPCcjs.ErrorExecutionResult)(ctx, new (0, _chunkJDABP4SDcjs.LoopExceededError)());
92
- }
93
- const iteration = await ctx.nextIteration();
94
- if (signal == null ? void 0 : signal.aborted) {
95
- iteration.end({
96
- type: "aborted",
97
- aborted: {
98
- reason: _nullishCoalesce(signal.reason, () => ( "The operation was aborted"))
99
- }
100
- });
101
- return new (0, _chunkCH5T6SPCcjs.ErrorExecutionResult)(ctx, _nullishCoalesce(signal.reason, () => ( "The operation was aborted")));
102
- }
103
- cleanups.push(
104
- iteration.traces.onPush((traces) => {
105
- for (const trace of traces) {
106
- onTrace == null ? void 0 : onTrace({ trace, iteration: ctx.iterations.length });
107
- }
108
- })
109
- );
110
- try {
111
- await executeIteration({
112
- iteration,
113
- ctx,
114
- cognitive,
115
- abortSignal: signal,
116
- onExit,
117
- onBeforeExecution
118
- });
119
- } catch (err) {
120
- iteration.end({
121
- type: "execution_error",
122
- execution_error: {
123
- message: "An unexpected error occurred: " + getErrorMessage(err),
124
- stack: _chunkIKSIOIIPcjs.cleanStackTrace.call(void 0, _nullishCoalesce(err.stack, () => ( "No stack trace available")))
125
- }
126
- });
127
- }
128
- try {
129
- await (onIterationEnd == null ? void 0 : onIterationEnd(iteration));
130
- } catch (err) {
131
- console.error(err);
132
- }
133
- if (iteration.status.type === "exit_success") {
134
- const exitName = iteration.status.exit_success.exit_name;
135
- return new (0, _chunkCH5T6SPCcjs.SuccessExecutionResult)(ctx, {
136
- exit: iteration.exits.find((x) => x.name === exitName),
137
- result: iteration.status.exit_success.return_value
138
- });
139
- }
140
- if (iteration.status.type === "callback_requested") {
141
- return new (0, _chunkCH5T6SPCcjs.PartialExecutionResult)(
142
- ctx,
143
- iteration.status.callback_requested.signal,
144
- _chunkCH5T6SPCcjs.Snapshot.fromSignal(iteration.status.callback_requested.signal)
145
- );
146
- }
147
- if (iteration.status.type === "thinking_requested" || iteration.status.type === "exit_error" || iteration.status.type === "execution_error" || iteration.status.type === "invalid_code_error") {
148
- continue;
149
- }
150
- return new (0, _chunkCH5T6SPCcjs.ErrorExecutionResult)(ctx, _nullishCoalesce(iteration.error, () => ( `Unknown error. Status: ${iteration.status.type}`)));
151
- }
152
- } catch (error) {
153
- return new (0, _chunkCH5T6SPCcjs.ErrorExecutionResult)(ctx, _nullishCoalesce(error, () => ( "Unknown error")));
154
- } finally {
155
- for (const cleanup of cleanups) {
156
- try {
157
- cleanup();
158
- } catch (e2) {
159
- }
160
- }
161
- }
162
- };
163
- var executeIteration = async ({
164
- iteration,
165
- ctx,
166
- cognitive,
167
- abortSignal,
168
- onExit,
169
- onBeforeExecution
170
- }) => {
171
- var _a, _b, _c, _d, _e;
172
- let startedAt = Date.now();
173
- const traces = iteration.traces;
174
- const modelLimit = 128e3;
175
- const responseLengthBuffer = getModelOutputLimit(modelLimit);
176
- const messages = _chunkFIVFS4HGcjs.truncateWrappedContent.call(void 0, {
177
- messages: iteration.messages,
178
- tokenLimit: modelLimit - responseLengthBuffer,
179
- throwOnFailure: false
180
- }).filter(
181
- (x) => (
182
- // Filter out empty messages, as they are not valid inputs for the LLM
183
- // This can happen when a message is truncated and the content is empty
184
- x.content.trim().length > 0
185
- )
186
- );
187
- traces.push({
188
- type: "llm_call_started",
189
- started_at: startedAt,
190
- ended_at: startedAt,
191
- model: _nullishCoalesce(ctx.model, () => ( ""))
192
- });
193
- const output = await cognitive.generateContent({
194
- signal: abortSignal,
195
- systemPrompt: (_a = messages.find((x) => x.role === "system")) == null ? void 0 : _a.content,
196
- model: ctx.model,
197
- temperature: ctx.temperature,
198
- responseFormat: "text",
199
- messages: messages.filter((x) => x.role === "user" || x.role === "assistant").map(
200
- (x) => ({
201
- role: x.role === "user" ? "user" : "assistant",
202
- type: "text",
203
- content: x.content
204
- })
205
- ),
206
- stopSequences: ctx.version.getStopTokens()
207
- });
208
- const out = ((_c = (_b = output.output.choices) == null ? void 0 : _b[0]) == null ? void 0 : _c.type) === "text" && typeof ((_d = output.output.choices) == null ? void 0 : _d[0].content) === "string" ? output.output.choices[0].content : null;
209
- if (!out) {
210
- throw new Error("No output from LLM");
211
- }
212
- const assistantResponse = ctx.version.parseAssistantResponse(out);
213
- iteration.code = assistantResponse.code.trim();
214
- if (typeof onBeforeExecution === "function") {
215
- try {
216
- await onBeforeExecution(iteration);
217
- } catch (err) {
218
- if (err instanceof _chunkJDABP4SDcjs.ThinkSignal) {
219
- return iteration.end({
220
- type: "thinking_requested",
221
- thinking_requested: {
222
- variables: err.context,
223
- reason: err.reason
224
- }
225
- });
226
- }
227
- return iteration.end({
228
- type: "execution_error",
229
- execution_error: {
230
- message: `Error in onBeforeExecution hook: ${getErrorMessage(err)}`,
231
- stack: _chunkIKSIOIIPcjs.cleanStackTrace.call(void 0, _nullishCoalesce(err.stack, () => ( "No stack trace available")))
232
- }
233
- });
234
- }
235
- }
236
- iteration.llm = {
237
- cached: output.meta.cached || false,
238
- ended_at: Date.now(),
239
- started_at: startedAt,
240
- status: "success",
241
- tokens: output.meta.tokens.input + output.meta.tokens.output,
242
- spend: output.meta.cost.input + output.meta.cost.output,
243
- output: assistantResponse.raw,
244
- model: `${output.meta.model.integration}:${output.meta.model.model}`
245
- };
246
- traces.push({
247
- type: "llm_call_success",
248
- started_at: startedAt,
249
- ended_at: iteration.llm.ended_at,
250
- model: _nullishCoalesce(ctx.model, () => ( "")),
251
- code: iteration.code
252
- });
253
- const vmContext = { ..._chunkP7J2WCBBcjs.stripInvalidIdentifiers.call(void 0, iteration.variables) };
254
- for (const obj of iteration.objects) {
255
- const internalValues = {};
256
- const instance = {};
257
- for (const { name, value, writable, type } of _nullishCoalesce(obj.properties, () => ( []))) {
258
- internalValues[name] = value;
259
- const initialValue = value;
260
- const schema = _nullishCoalesce(type, () => ( _zui.z.any()));
261
- Object.defineProperty(instance, name, {
262
- enumerable: true,
263
- configurable: true,
264
- get() {
265
- return internalValues[name];
266
- },
267
- set(value2) {
268
- if (_chunkUQOBUJIQcjs.isEqual_default.call(void 0, value2, internalValues[name])) {
269
- return;
270
- }
271
- if (!writable) {
272
- throw new (0, _chunkJDABP4SDcjs.AssignmentError)(`Property ${obj.name}.${name} is read-only and cannot be modified`);
273
- }
274
- if (value2 === internalValues[name]) {
275
- return;
276
- }
277
- const parsed = schema.safeParse(value2);
278
- if (!parsed.success) {
279
- throw new (0, _chunkJDABP4SDcjs.AssignmentError)(
280
- `Invalid value for Object property ${obj.name}.${name}: ${getErrorMessage(parsed.error)}`
281
- );
282
- }
283
- internalValues[name] = parsed.data;
284
- traces.push({
285
- type: "property",
286
- started_at: Date.now(),
287
- object: obj.name,
288
- property: name,
289
- value: parsed.data
290
- });
291
- iteration.trackMutation({ object: obj.name, property: name, before: initialValue, after: parsed.data });
292
- }
293
- });
294
- }
295
- for (const tool of _nullishCoalesce(obj.tools, () => ( []))) {
296
- instance[tool.name] = wrapTool({ tool, traces, object: obj.name });
297
- }
298
- Object.preventExtensions(instance);
299
- Object.seal(instance);
300
- vmContext[obj.name] = instance;
301
- }
302
- for (const tool of iteration.tools) {
303
- const wrapped = wrapTool({ tool, traces });
304
- for (const key of [tool.name, ..._nullishCoalesce(tool.aliases, () => ( []))]) {
305
- vmContext[key] = wrapped;
306
- }
307
- }
308
- if (abortSignal == null ? void 0 : abortSignal.aborted) {
309
- traces.push({
310
- type: "abort_signal",
311
- started_at: Date.now(),
312
- reason: "The operation was aborted by user."
313
- });
314
- return iteration.end({
315
- type: "aborted",
316
- aborted: {
317
- reason: _nullishCoalesce((abortSignal == null ? void 0 : abortSignal.reason), () => ( "The operation was aborted"))
318
- }
319
- });
320
- }
321
- startedAt = Date.now();
322
- const result = await _chunkBEPRLBPKcjs.runAsyncFunction.call(void 0, vmContext, iteration.code, traces, abortSignal, ctx.timeout).catch(
323
- (err) => {
324
- return {
325
- success: false,
326
- error: err,
327
- lines_executed: [],
328
- traces: [],
329
- variables: {}
330
- };
331
- }
332
- );
333
- if (result.error && result.error instanceof _chunkJDABP4SDcjs.InvalidCodeError) {
334
- return iteration.end({
335
- type: "invalid_code_error",
336
- invalid_code_error: {
337
- message: result.error.message
338
- }
339
- });
340
- }
341
- traces.push({
342
- type: "code_execution",
343
- lines_executed: _nullishCoalesce(result.lines_executed, () => ( 0)),
344
- started_at: startedAt,
345
- ended_at: Date.now()
346
- });
347
- if (result.error && result.error instanceof _chunkJDABP4SDcjs.CodeExecutionError) {
348
- return iteration.end({
349
- type: "execution_error",
350
- execution_error: {
351
- message: result.error.message,
352
- stack: _chunkIKSIOIIPcjs.cleanStackTrace.call(void 0, _nullishCoalesce(_nullishCoalesce(result.error.stacktrace, () => ( result.error.stack)), () => ( "No stack trace available")))
353
- }
354
- });
355
- }
356
- if (abortSignal == null ? void 0 : abortSignal.aborted) {
357
- return iteration.end({
358
- type: "aborted",
359
- aborted: {
360
- reason: _nullishCoalesce((abortSignal == null ? void 0 : abortSignal.reason), () => ( "The operation was aborted"))
361
- }
362
- });
363
- }
364
- if (!result.success) {
365
- return iteration.end({
366
- type: "execution_error",
367
- execution_error: {
368
- message: _nullishCoalesce(((_e = result == null ? void 0 : result.error) == null ? void 0 : _e.message), () => ( "Unknown error occurred")),
369
- stack: _chunkIKSIOIIPcjs.cleanStackTrace.call(void 0, _nullishCoalesce(result.error.stack, () => ( "No stack trace available")))
370
- }
371
- });
372
- }
373
- if (result.signal instanceof _chunkJDABP4SDcjs.ThinkSignal) {
374
- return iteration.end({
375
- type: "thinking_requested",
376
- thinking_requested: {
377
- variables: result.signal.context,
378
- reason: result.signal.reason
379
- }
380
- });
381
- }
382
- if (result.signal instanceof _chunkJDABP4SDcjs.SnapshotSignal) {
383
- return iteration.end({
384
- type: "callback_requested",
385
- callback_requested: {
386
- signal: result.signal
387
- }
388
- });
389
- }
390
- const validActions = [...iteration.exits.map((x) => x.name.toLowerCase()), "think"];
391
- let returnValue = result.success && result.return_value ? result.return_value : null;
392
- const returnAction = returnValue == null ? void 0 : returnValue.action;
393
- const returnExit = _nullishCoalesce(iteration.exits.find((x) => x.name.toLowerCase() === (returnAction == null ? void 0 : returnAction.toLowerCase())), () => ( iteration.exits.find((x) => x.aliases.some((a) => a.toLowerCase() === (returnAction == null ? void 0 : returnAction.toLowerCase())))));
394
- if (returnAction === "think") {
395
- const variables = _chunkUQOBUJIQcjs.omit_default.call(void 0, _nullishCoalesce(returnValue, () => ( {})), "action");
396
- if (_chunkUQOBUJIQcjs.isPlainObject_default.call(void 0, variables) && Object.keys(variables).length > 0) {
397
- return iteration.end({
398
- type: "thinking_requested",
399
- thinking_requested: {
400
- variables,
401
- reason: "Thinking requested"
402
- }
403
- });
404
- }
405
- return iteration.end({
406
- type: "thinking_requested",
407
- thinking_requested: {
408
- reason: "Thinking requested",
409
- variables: iteration.variables
410
- }
411
- });
412
- }
413
- if (!returnAction) {
414
- return iteration.end({
415
- type: "exit_error",
416
- exit_error: {
417
- exit: "n/a",
418
- message: `Code did not return an action. Valid actions are: ${validActions.join(", ")}`,
419
- return_value: returnValue
420
- }
421
- });
422
- }
423
- if (!returnExit) {
424
- return iteration.end({
425
- type: "exit_error",
426
- exit_error: {
427
- exit: returnAction,
428
- message: `Exit "${returnAction}" not found. Valid actions are: ${validActions.join(", ")}`,
429
- return_value: returnValue
430
- }
431
- });
432
- }
433
- if (returnExit.zSchema) {
434
- const parsed = returnExit.zSchema.safeParse(returnValue == null ? void 0 : returnValue.value);
435
- if (!parsed.success) {
436
- return iteration.end({
437
- type: "exit_error",
438
- exit_error: {
439
- exit: returnExit.name,
440
- message: `Invalid return value for exit ${returnExit.name}: ${getErrorMessage(parsed.error)}`,
441
- return_value: returnValue
442
- }
443
- });
444
- }
445
- returnValue = { action: returnExit.name, value: parsed.data };
446
- }
447
- try {
448
- await (onExit == null ? void 0 : onExit({
449
- exit: returnExit,
450
- result: returnValue == null ? void 0 : returnValue.value
451
- }));
452
- } catch (err) {
453
- return iteration.end({
454
- type: "exit_error",
455
- exit_error: {
456
- exit: returnExit.name,
457
- message: `Error executing exit ${returnExit.name}: ${getErrorMessage(err)}`,
458
- return_value: returnValue
459
- }
460
- });
461
- }
462
- return iteration.end({
463
- type: "exit_success",
464
- exit_success: {
465
- exit_name: returnExit.name,
466
- return_value: returnValue == null ? void 0 : returnValue.value
467
- }
468
- });
469
- };
470
- function wrapTool({ tool, traces, object }) {
471
- const getToolInput = (input) => _nullishCoalesce(tool.zInput.safeParse(input).data, () => ( input));
472
- return function(input) {
473
- const toolCallId = `tcall_${_ulid.ulid.call(void 0, )}`;
474
- const alertSlowTool = setTimeout(
475
- () => traces.push({
476
- type: "tool_slow",
477
- tool_name: tool.name,
478
- tool_call_id: toolCallId,
479
- started_at: Date.now(),
480
- input: getToolInput(input),
481
- object,
482
- duration: SLOW_TOOL_WARNING
483
- }),
484
- SLOW_TOOL_WARNING
485
- );
486
- const cancelSlowTool = () => clearTimeout(alertSlowTool);
487
- const toolStart = Date.now();
488
- let output;
489
- let error;
490
- let success = true;
491
- const handleSignals = (error2) => {
492
- if (output === error2) {
493
- return true;
494
- }
495
- if (error2 instanceof _chunkJDABP4SDcjs.SnapshotSignal) {
496
- error2.toolCall = {
497
- name: tool.name,
498
- inputSchema: tool.input,
499
- outputSchema: tool.output,
500
- input
501
- };
502
- error2.message = _chunkJDABP4SDcjs.Signals.serializeError(error2);
503
- }
504
- if (error2 instanceof _chunkJDABP4SDcjs.ThinkSignal) {
505
- traces.push({
506
- type: "think_signal",
507
- started_at: Date.now(),
508
- line: 0,
509
- ended_at: Date.now()
510
- });
511
- success = true;
512
- output = error2;
513
- return true;
514
- }
515
- return false;
516
- };
517
- try {
518
- const result = tool.execute(input, {
519
- callId: toolCallId
520
- });
521
- if (result instanceof Promise || (result == null ? void 0 : result.then) && (result == null ? void 0 : result.catch)) {
522
- return result.then((res) => {
523
- output = res;
524
- success = true;
525
- return res;
526
- }).catch((err) => {
527
- if (!handleSignals(err)) {
528
- success = false;
529
- error = err;
530
- }
531
- throw err;
532
- }).finally(() => {
533
- cancelSlowTool();
534
- traces.push({
535
- type: "tool_call",
536
- tool_call_id: toolCallId,
537
- started_at: toolStart,
538
- ended_at: Date.now(),
539
- tool_name: tool.name,
540
- object,
541
- input: getToolInput(input),
542
- output,
543
- error,
544
- success
545
- });
546
- });
547
- }
548
- success = true;
549
- output = result;
550
- } catch (err) {
551
- if (!handleSignals(err)) {
552
- success = false;
553
- error = err;
554
- }
555
- }
556
- cancelSlowTool();
557
- traces.push({
558
- type: "tool_call",
559
- tool_call_id: toolCallId,
560
- started_at: toolStart,
561
- ended_at: Date.now(),
562
- tool_name: tool.name,
563
- object,
564
- input: getToolInput(input),
565
- output,
566
- error,
567
- success
568
- });
569
- if (!success) {
570
- throw error;
571
- }
572
- if (output instanceof _chunkJDABP4SDcjs.VMSignal) {
573
- throw output;
574
- }
575
- return output;
576
- };
577
- }
578
-
579
-
580
-
581
- exports._executeContext = _executeContext; exports.executeContext = executeContext;