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