llmz 0.0.4 → 0.0.6

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 (41) hide show
  1. package/README.md +4 -0
  2. package/dist/chat.d.ts +17 -0
  3. package/dist/{chunk-LVKZYKTP.cjs → chunk-4I7UPBLN.cjs} +2 -2
  4. package/dist/{chunk-W6U2VXSF.cjs → chunk-6WT5VZBG.cjs} +348 -12
  5. package/dist/{chunk-5NVNEEYW.cjs → chunk-BEPRLBPK.cjs} +60 -16
  6. package/dist/chunk-CH5T6SPC.cjs +694 -0
  7. package/dist/{chunk-4EWY34YA.js → chunk-D3ESDRLH.js} +60 -16
  8. package/dist/{chunk-JK2LZW2G.cjs → chunk-EJRCDWBA.cjs} +45 -6
  9. package/dist/{chunk-ZCPQ3QOW.js → chunk-HP7RKM25.js} +45 -6
  10. package/dist/{chunk-S6WICIDW.js → chunk-S6FOL2HY.js} +2 -2
  11. package/dist/{chunk-TJQVC4CE.js → chunk-WLUVY5QU.js} +341 -5
  12. package/dist/chunk-XJNRORSK.js +694 -0
  13. package/dist/{component-LQDU72LX.js → component-R2Y74VUI.js} +3 -1
  14. package/dist/component-TSNW3SC7.cjs +16 -0
  15. package/dist/component.d.ts +6 -19
  16. package/dist/component.default.d.ts +131 -151
  17. package/dist/context.d.ts +13 -5
  18. package/dist/{dual-modes-YE4S2AIL.cjs → dual-modes-UHNDHNIF.cjs} +3 -4
  19. package/dist/{dual-modes-QHBOFWHM.js → dual-modes-ZUQKPJFH.js} +2 -3
  20. package/dist/{exit-IDKGZD7M.cjs → exit-KJ4COC5N.cjs} +2 -2
  21. package/dist/{exit-F6ZUL2NV.js → exit-OIYZLBVJ.js} +1 -1
  22. package/dist/exit.d.ts +5 -1
  23. package/dist/getter.d.ts +1 -1
  24. package/dist/index.cjs +85 -128
  25. package/dist/index.d.ts +11 -8
  26. package/dist/index.js +80 -123
  27. package/dist/llmz-CPMVYIOJ.cjs +581 -0
  28. package/dist/llmz-LAN4WDFW.js +581 -0
  29. package/dist/llmz.d.ts +8 -10
  30. package/dist/result.d.ts +33 -0
  31. package/dist/types.d.ts +1 -33
  32. package/dist/{vm-GJ5R72AP.cjs → vm-2DLG7V4G.cjs} +2 -2
  33. package/dist/{vm-ZUQOSRRM.js → vm-FLBMZUA2.js} +1 -1
  34. package/package.json +1 -1
  35. package/dist/chunk-4KB5WXHR.js +0 -92
  36. package/dist/chunk-6YWYCVAB.cjs +0 -92
  37. package/dist/chunk-EFGXTO64.js +0 -344
  38. package/dist/chunk-VAF2H6UD.cjs +0 -344
  39. package/dist/component-HQ5YQNRX.cjs +0 -14
  40. package/dist/llmz-AS5TGCQS.js +0 -1108
  41. package/dist/llmz-R6XZG3JQ.cjs +0 -1108
@@ -1,1108 +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(); } } async function _asyncNullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return await rhsFn(); } } var _class; var _class2;
2
-
3
- var _chunkW6U2VXSFcjs = require('./chunk-W6U2VXSF.cjs');
4
-
5
-
6
- var _chunk5NVNEEYWcjs = require('./chunk-5NVNEEYW.cjs');
7
-
8
-
9
- var _chunk6YWYCVABcjs = require('./chunk-6YWYCVAB.cjs');
10
- require('./chunk-VAF2H6UD.cjs');
11
-
12
-
13
- var _chunk4QTK5ALHcjs = require('./chunk-4QTK5ALH.cjs');
14
- require('./chunk-HYVKY3W6.cjs');
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
- var _chunkJDABP4SDcjs = require('./chunk-JDABP4SD.cjs');
25
-
26
-
27
- var _chunkIKSIOIIPcjs = require('./chunk-IKSIOIIP.cjs');
28
-
29
-
30
- var _chunkLVKZYKTPcjs = require('./chunk-LVKZYKTP.cjs');
31
-
32
-
33
- var _chunkJK2LZW2Gcjs = require('./chunk-JK2LZW2G.cjs');
34
- require('./chunk-KMZDFWYZ.cjs');
35
-
36
-
37
-
38
- var _chunkFIVFS4HGcjs = require('./chunk-FIVFS4HG.cjs');
39
-
40
-
41
-
42
- var _chunkP7J2WCBBcjs = require('./chunk-P7J2WCBB.cjs');
43
-
44
-
45
-
46
-
47
-
48
-
49
- var _chunkUQOBUJIQcjs = require('./chunk-UQOBUJIQ.cjs');
50
-
51
- // src/llmz.ts
52
- var _cognitive = require('@botpress/cognitive');
53
- var _zui = require('@bpinternal/zui');
54
- var _ms = require('ms'); var _ms2 = _interopRequireDefault(_ms);
55
- var _ulid = require('ulid');
56
-
57
- // src/context.ts
58
-
59
-
60
- // src/getter.ts
61
- var getValue = async (valueOrGetter, ctx) => {
62
- if (typeof valueOrGetter === "function") {
63
- try {
64
- return await valueOrGetter(ctx);
65
- } catch (e) {
66
- throw new Error(`Error while getting value for ${valueOrGetter}: ${e}`);
67
- }
68
- } else {
69
- return valueOrGetter;
70
- }
71
- };
72
-
73
- // src/handlers.ts
74
- var Emitter = (_class = class {constructor() { _class.prototype.__init.call(this);_class.prototype.__init2.call(this); }
75
- __init() {this._handlers = []}
76
- __init2() {this.subscribe = (fn) => {
77
- this._handlers.push(fn);
78
- return () => {
79
- this._handlers = this._handlers.filter((handler) => handler !== fn);
80
- };
81
- }}
82
- emit(event) {
83
- this._handlers.forEach((handler) => handler(event));
84
- }
85
- }, _class);
86
- var HookedArray = class extends Array {
87
- #listeners = new Emitter();
88
- constructor(...items) {
89
- super(...items);
90
- Object.setPrototypeOf(this, new.target.prototype);
91
- }
92
- push(...items) {
93
- try {
94
- this.#listeners.emit(items);
95
- } finally {
96
- return super.push(...items);
97
- }
98
- }
99
- onPush(fn) {
100
- return this.#listeners.subscribe(fn);
101
- }
102
- };
103
-
104
- // src/transcript.ts
105
- var MAX_MESSAGE_LENGTH = 5e3;
106
- var TranscriptArray = class extends Array {
107
- constructor(items = []) {
108
- items = Array.isArray(items) ? items : [];
109
- super(...items);
110
- items.forEach((item) => {
111
- if (!["user", "assistant"].includes(item.role)) {
112
- throw new Error(`Invalid role "${item.role}" in transcript message`);
113
- }
114
- if (item.name && typeof item.name !== "string") {
115
- throw new Error(`Invalid name for transcript message. Expected a string, but got type "${typeof item.name}"`);
116
- }
117
- if (typeof item.content !== "string") {
118
- throw new Error(
119
- `Invalid content for transcript message. Expected a string, but got type "${typeof item.content}"`
120
- );
121
- }
122
- });
123
- Object.setPrototypeOf(this, new.target.prototype);
124
- }
125
- toString() {
126
- if (!this.length) {
127
- return "";
128
- }
129
- return this.map((item, idx) => {
130
- var _a;
131
- const msgIdx = "message-" + String(idx + 1).padStart(3, "0");
132
- const shouldTruncate = item.content.length > MAX_MESSAGE_LENGTH;
133
- const preview = shouldTruncate ? item.content.slice(0, MAX_MESSAGE_LENGTH) + "\n... (truncated)" : item.content;
134
- const tags = [];
135
- tags.push({ key: "role", value: item.role });
136
- if ((_a = item.name) == null ? void 0 : _a.length) {
137
- tags.push({ key: "name", value: item.name });
138
- }
139
- const tagsString = tags.map(({ key, value }) => `${key}="${value}"`).join(" ");
140
- return `<${msgIdx} ${tagsString}>
141
- ${preview.trim()}
142
- </${msgIdx}>`;
143
- }).join("\n");
144
- }
145
- };
146
-
147
- // src/context.ts
148
- var ThinkExit = new (0, _chunkLVKZYKTPcjs.Exit)({
149
- name: "think",
150
- description: "Think about the current situation and provide a response"
151
- });
152
- var ListenExit = new (0, _chunkLVKZYKTPcjs.Exit)({
153
- name: "listen",
154
- description: "Listen to the user and provide a response"
155
- });
156
- var Iteration = class {
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
- get mutations() {
167
- return [...this._mutations.values()];
168
- }
169
- trackMutation(mutation) {
170
- this._mutations.set(`${_nullishCoalesce(mutation.object, () => ( "global"))}:${mutation.property}`, mutation);
171
- }
172
-
173
- get transcript() {
174
- return this._parameters.transcript;
175
- }
176
- get tools() {
177
- return this._parameters.tools;
178
- }
179
- get objects() {
180
- return this._parameters.objects;
181
- }
182
- get exits() {
183
- const exits = [...this._parameters.exits, ThinkExit];
184
- if (this.isChatEnabled) {
185
- exits.push(ListenExit);
186
- }
187
- return exits;
188
- }
189
- get instructions() {
190
- return this._parameters.instructions;
191
- }
192
-
193
- hasExited() {
194
- return ["exit_success"].includes(this.status.type);
195
- }
196
- hasExitedWith(exit) {
197
- return this.status.type === "exit_success" && this.status.exit_success.exit_name === exit.name;
198
- }
199
- isSuccessful() {
200
- return ["callback_requested", "exit_success", "thinking_requested"].includes(
201
- this.status.type
202
- );
203
- }
204
- isFailed() {
205
- return [
206
- "generation_error",
207
- "invalid_code_error",
208
- "execution_error",
209
- "exit_error",
210
- "aborted"
211
- ].includes(this.status.type);
212
- }
213
- get duration() {
214
- const ms2 = (_nullishCoalesce(this.ended_ts, () => ( Date.now()))) - this.started_ts;
215
- const trailing = this.ended_ts ? "" : " (still running)";
216
- return ms2.toLocaleString("en-US", { style: "unit", unit: "millisecond" }) + trailing;
217
- }
218
- get error() {
219
- if (this.status.type === "generation_error") {
220
- return `CodeGenerationError: ${this.status.generation_error.message}`;
221
- }
222
- if (this.status.type === "invalid_code_error") {
223
- return `InvalidCodeError: ${this.status.invalid_code_error.message}`;
224
- }
225
- if (this.status.type === "execution_error") {
226
- return `CodeExecutionError: ${this.status.execution_error.message}`;
227
- }
228
- if (this.status.type === "exit_error") {
229
- return `ExitError: ${this.status.exit_error.message}`;
230
- }
231
- if (this.status.type === "aborted") {
232
- return `Aborted: ${this.status.aborted.reason}`;
233
- }
234
- return null;
235
- }
236
- get isChatEnabled() {
237
- return this._parameters.tools.find((x) => x.name.toLowerCase() === "message") !== void 0;
238
- }
239
- constructor(props) {
240
- this.id = props.id;
241
- this.status = { type: "pending" };
242
- this.traces = new HookedArray();
243
- this._mutations = /* @__PURE__ */ new Map();
244
- this.messages = props.messages;
245
- this.variables = props.variables;
246
- this._parameters = props.parameters;
247
- this.started_ts = Date.now();
248
- }
249
- end(status) {
250
- if (this.status.type !== "pending") {
251
- throw new Error(`Iteration ${this.id} has already ended with status ${this.status.type}`);
252
- }
253
- this.ended_ts = Date.now();
254
- this.status = status;
255
- }
256
- };
257
- var Context = (_class2 = class {
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
- __init3() {this.version = _chunkW6U2VXSFcjs.DualModePrompt}
266
- __init4() {this.timeout = 6e4}
267
- // Default timeout of 60 seconds
268
-
269
-
270
-
271
-
272
-
273
- __init5() {this.iteration = 0}
274
-
275
- async nextIteration() {
276
- if (this.iterations.length >= this.loop) {
277
- throw new (0, _chunkJDABP4SDcjs.LoopExceededError)();
278
- }
279
- if (this.snapshot && this.snapshot.status.type === "pending") {
280
- throw new Error(
281
- `Cannot resume execution from a snapshot that is still pending: ${this.snapshot.id}. Please resolve() or reject() it first.`
282
- );
283
- }
284
- const parameters = await this._refreshIterationParameters();
285
- const messages = await this._getIterationMessages(parameters);
286
- const iteration = new Iteration({
287
- id: `${this.id}_${this.iterations.length + 1}`,
288
- variables: this._getIterationVariables(),
289
- parameters,
290
- messages
291
- });
292
- this.iterations.push(iteration);
293
- this.iteration = this.iterations.length;
294
- this.snapshot = void 0;
295
- return iteration;
296
- }
297
- _getIterationVariables() {
298
- const lastIteration = this.iterations.at(-1);
299
- const variables = {};
300
- if ((lastIteration == null ? void 0 : lastIteration.status.type) === "thinking_requested") {
301
- const lastThinkingVariables = lastIteration.status.thinking_requested.variables;
302
- if (_chunkUQOBUJIQcjs.isPlainObject_default.call(void 0, lastThinkingVariables)) {
303
- Object.assign(variables, _chunkUQOBUJIQcjs.cloneDeep_default.call(void 0, lastThinkingVariables));
304
- }
305
- }
306
- if (_chunkUQOBUJIQcjs.isPlainObject_default.call(void 0, lastIteration == null ? void 0 : lastIteration.variables)) {
307
- Object.assign(variables, _chunkUQOBUJIQcjs.cloneDeep_default.call(void 0, _nullishCoalesce((lastIteration == null ? void 0 : lastIteration.variables), () => ( {}))));
308
- }
309
- return variables;
310
- }
311
- async _getIterationMessages(parameters) {
312
- var _a, _b, _c, _d, _e, _f;
313
- const lastIteration = this.iterations.at(-1);
314
- if (((_a = this.snapshot) == null ? void 0 : _a.status.type) === "resolved") {
315
- return [
316
- await this.version.getSystemMessage({
317
- globalTools: parameters.tools,
318
- objects: parameters.objects,
319
- instructions: parameters.instructions,
320
- transcript: parameters.transcript,
321
- exits: parameters.exits,
322
- components: parameters.components
323
- }),
324
- await this.version.getSnapshotResolvedMessage({
325
- snapshot: this.snapshot
326
- })
327
- ];
328
- }
329
- if (((_b = this.snapshot) == null ? void 0 : _b.status.type) === "rejected") {
330
- return [
331
- await this.version.getSystemMessage({
332
- globalTools: parameters.tools,
333
- objects: parameters.objects,
334
- instructions: parameters.instructions,
335
- transcript: parameters.transcript,
336
- exits: parameters.exits,
337
- components: parameters.components
338
- }),
339
- await this.version.getSnapshotRejectedMessage({
340
- snapshot: this.snapshot
341
- })
342
- ];
343
- }
344
- if (!lastIteration) {
345
- return [
346
- await this.version.getSystemMessage({
347
- globalTools: parameters.tools,
348
- objects: parameters.objects,
349
- instructions: parameters.instructions,
350
- transcript: parameters.transcript,
351
- exits: parameters.exits,
352
- components: parameters.components
353
- }),
354
- await this.version.getInitialUserMessage({
355
- globalTools: parameters.tools,
356
- objects: parameters.objects,
357
- instructions: parameters.instructions,
358
- transcript: parameters.transcript,
359
- exits: parameters.exits,
360
- components: parameters.components
361
- })
362
- ];
363
- }
364
- const lastIterationMessages = [
365
- await this.version.getSystemMessage({
366
- globalTools: parameters.tools,
367
- objects: parameters.objects,
368
- instructions: parameters.instructions,
369
- transcript: parameters.transcript,
370
- exits: parameters.exits,
371
- components: parameters.components
372
- }),
373
- ...lastIteration.messages.filter((x) => x.role !== "system")
374
- ];
375
- if ((lastIteration == null ? void 0 : lastIteration.status.type) === "thinking_requested") {
376
- return [
377
- ...lastIterationMessages,
378
- {
379
- role: "assistant",
380
- content: _chunkFIVFS4HGcjs.wrapContent.call(void 0, _nullishCoalesce(((_c = lastIteration.llm) == null ? void 0 : _c.output), () => ( "")), { preserve: "top", flex: 4, minTokens: 25 })
381
- },
382
- await this.version.getThinkingMessage({
383
- reason: lastIteration.status.thinking_requested.reason,
384
- variables: lastIteration.status.thinking_requested.variables
385
- })
386
- ];
387
- }
388
- if ((lastIteration == null ? void 0 : lastIteration.status.type) === "exit_error") {
389
- return [
390
- ...lastIterationMessages,
391
- {
392
- role: "assistant",
393
- content: _chunkFIVFS4HGcjs.wrapContent.call(void 0, _nullishCoalesce(((_d = lastIteration.llm) == null ? void 0 : _d.output), () => ( "")), { preserve: "top", flex: 4, minTokens: 25 })
394
- },
395
- await this.version.getInvalidCodeMessage({
396
- code: _nullishCoalesce(lastIteration.code, () => ( "// No code generated")),
397
- message: `Invalid return statement (action: ${lastIteration.status.exit_error.exit}): ${lastIteration.status.exit_error.message}`
398
- })
399
- ];
400
- }
401
- if ((lastIteration == null ? void 0 : lastIteration.status.type) === "invalid_code_error") {
402
- return [
403
- ...lastIterationMessages,
404
- {
405
- role: "assistant",
406
- content: _chunkFIVFS4HGcjs.wrapContent.call(void 0, _nullishCoalesce(((_e = lastIteration.llm) == null ? void 0 : _e.output), () => ( "")), { preserve: "top", flex: 4, minTokens: 25 })
407
- },
408
- await this.version.getInvalidCodeMessage({
409
- code: _nullishCoalesce(lastIteration.code, () => ( "// No code generated")),
410
- message: lastIteration.status.invalid_code_error.message
411
- })
412
- ];
413
- }
414
- if ((lastIteration == null ? void 0 : lastIteration.status.type) === "execution_error") {
415
- return [
416
- ...lastIterationMessages,
417
- {
418
- role: "assistant",
419
- content: _chunkFIVFS4HGcjs.wrapContent.call(void 0, _nullishCoalesce(((_f = lastIteration.llm) == null ? void 0 : _f.output), () => ( "")), { preserve: "top", flex: 4, minTokens: 25 })
420
- },
421
- await this.version.getCodeExecutionErrorMessage({
422
- message: lastIteration.status.execution_error.message,
423
- stacktrace: lastIteration.status.execution_error.stack
424
- })
425
- ];
426
- }
427
- throw new Error(
428
- `Unexpected iteration status: ${lastIteration == null ? void 0 : lastIteration.status.type}. This is likely a bug, please report it.`
429
- );
430
- }
431
- async _refreshIterationParameters() {
432
- var _a;
433
- const instructions = await getValue(this.instructions, this);
434
- const transcript = new TranscriptArray(await _asyncNullishCoalesce(await getValue(this.transcript, this), async () => ( [])));
435
- const tools = _chunk4QTK5ALHcjs.Tool.withUniqueNames(await _asyncNullishCoalesce(await getValue(this.tools, this), async () => ( [])));
436
- const objects = await _asyncNullishCoalesce(await getValue(this.objects, this), async () => ( []));
437
- const exits = await _asyncNullishCoalesce(await getValue(this.exits, this), async () => ( []));
438
- const components = await _asyncNullishCoalesce(await getValue(this.components, this), async () => ( []));
439
- if (objects && objects.length > 100) {
440
- throw new Error("Too many objects. Expected at most 100 objects.");
441
- }
442
- if (tools && tools.length > 100) {
443
- throw new Error("Too many tools. Expected at most 100 tools.");
444
- }
445
- for (const component of components) {
446
- _chunkJK2LZW2Gcjs.assertValidComponent.call(void 0, component.definition);
447
- }
448
- const ReservedToolNames = [
449
- "think",
450
- "listen",
451
- "return",
452
- "exit",
453
- "action",
454
- "function",
455
- "callback",
456
- "code",
457
- "execute",
458
- "jsx",
459
- "object",
460
- "string",
461
- "number",
462
- "boolean",
463
- "array"
464
- ];
465
- const MessageTool = (_a = tools.find((x) => {
466
- var _a2;
467
- return x.name.toLowerCase() === "message" || ((_a2 = x.aliases) == null ? void 0 : _a2.includes("message"));
468
- })) == null ? void 0 : _a.clone();
469
- if (MessageTool && !components.length) {
470
- throw new Error("The Message tool is only available in chat mode. Please provide at least one component.");
471
- }
472
- if (!MessageTool && components.length) {
473
- throw new Error('When using components, you need to provide a tool called "Message"');
474
- }
475
- if (MessageTool && components.length) {
476
- MessageTool.aliases = Array.from(
477
- /* @__PURE__ */ new Set([
478
- ...MessageTool.aliases,
479
- ...components.flatMap((x) => [x.definition.name, ..._nullishCoalesce(x.definition.aliases, () => ( []))])
480
- ])
481
- );
482
- }
483
- const nonMessageTools = tools.filter((x) => {
484
- var _a2;
485
- return x.name.toLowerCase() !== "message" && !((_a2 = x.aliases) == null ? void 0 : _a2.includes("message"));
486
- });
487
- const allTools = MessageTool ? [MessageTool, ...nonMessageTools] : nonMessageTools;
488
- for (const tool of nonMessageTools) {
489
- for (let name of [...tool.aliases, tool.name]) {
490
- name = name.toLowerCase();
491
- if (ReservedToolNames.includes(name)) {
492
- throw new Error(`Tool name "${name}" (${tool.name}) is reserved. Please choose a different name.`);
493
- }
494
- if (components.find(
495
- (x) => {
496
- var _a2;
497
- return x.definition.name.toLowerCase() === name || ((_a2 = x.definition.aliases) == null ? void 0 : _a2.map((x2) => x2.toLowerCase()).includes(name));
498
- }
499
- )) {
500
- throw new Error(
501
- `Tool name "${name}" (${tool.name}) is already used by a component. Please choose a different name.`
502
- );
503
- }
504
- if (exits.find((x) => x.name.toLowerCase() === name) || exits.find((x) => {
505
- var _a2;
506
- return (_a2 = x.aliases) == null ? void 0 : _a2.map((x2) => x2.toLowerCase()).includes(name);
507
- })) {
508
- throw new Error(
509
- `Tool name "${name}" (${tool.name}) is already used by an exit. Please choose a different name.`
510
- );
511
- }
512
- }
513
- }
514
- if (exits && exits.length > 100) {
515
- throw new Error("Too many exits. Expected at most 100 exits.");
516
- }
517
- if (components && components.length > 100) {
518
- throw new Error("Too many components. Expected at most 100 components.");
519
- }
520
- if (instructions && instructions.length > 1e6) {
521
- throw new Error("Instructions are too long. Expected at most 1,000,000 characters.");
522
- }
523
- if (transcript && transcript.length > 250) {
524
- throw new Error("Too many transcript messages. Expected at most 250 messages.");
525
- }
526
- if (!components.length && !exits.length) {
527
- throw new Error("When no components are provided, at least one exit is required.");
528
- }
529
- return {
530
- transcript,
531
- tools: allTools,
532
- objects,
533
- exits,
534
- instructions,
535
- components
536
- };
537
- }
538
- constructor(props) {;_class2.prototype.__init3.call(this);_class2.prototype.__init4.call(this);_class2.prototype.__init5.call(this);
539
- this.id = `llmz_${_ulid.ulid.call(void 0, )}`;
540
- this.transcript = props.transcript;
541
- this.instructions = props.instructions;
542
- this.objects = props.objects;
543
- this.tools = props.tools;
544
- this.exits = props.exits;
545
- this.components = props.components;
546
- this.timeout = Math.min(999999999, Math.max(0, _nullishCoalesce(props.timeout, () => ( 6e4))));
547
- this.loop = _nullishCoalesce(props.loop, () => ( 3));
548
- this.temperature = _nullishCoalesce(props.temperature, () => ( 0.7));
549
- this.model = props.model;
550
- this.iterations = [];
551
- this.metadata = _nullishCoalesce(props.metadata, () => ( {}));
552
- this.snapshot = props.snapshot;
553
- if (this.loop < 1 || this.loop > 100) {
554
- throw new Error("Invalid loop. Expected a number between 1 and 100.");
555
- }
556
- if (this.temperature < 0 || this.temperature > 2) {
557
- throw new Error("Invalid temperature. Expected a number between 0 and 2.");
558
- }
559
- }
560
- }, _class2);
561
-
562
- // src/llmz.ts
563
- var getErrorMessage = (err) => err instanceof Error ? err.message : JSON.stringify(err);
564
- var SLOW_TOOL_WARNING = _ms2.default.call(void 0, "15s");
565
- var RESPONSE_LENGTH_BUFFER = {
566
- MIN_TOKENS: 1e3,
567
- MAX_TOKENS: 16e3,
568
- PERCENTAGE: 0.1
569
- };
570
- var getModelOutputLimit = (inputLength) => _chunkUQOBUJIQcjs.clamp_default.call(void 0,
571
- RESPONSE_LENGTH_BUFFER.PERCENTAGE * inputLength,
572
- RESPONSE_LENGTH_BUFFER.MIN_TOKENS,
573
- RESPONSE_LENGTH_BUFFER.MAX_TOKENS
574
- );
575
- var executeContext = async (props) => {
576
- var _a, _b, _c, _d;
577
- await _chunkP7J2WCBBcjs.init.call(void 0, );
578
- const { signal, onIterationEnd, onTrace, onExit, onBeforeExecution } = props;
579
- const cognitive = props.client instanceof _cognitive.Cognitive ? props.client : new (0, _cognitive.Cognitive)({ client: props.client });
580
- const cleanups = [];
581
- const ctx = new Context({
582
- instructions: props.instructions,
583
- objects: props.objects,
584
- tools: props.tools,
585
- loop: (_a = props.options) == null ? void 0 : _a.loop,
586
- temperature: (_b = props.options) == null ? void 0 : _b.temperature,
587
- model: (_c = props.options) == null ? void 0 : _c.model,
588
- timeout: (_d = props.options) == null ? void 0 : _d.timeout,
589
- transcript: props.transcript,
590
- exits: props.exits,
591
- components: props.components,
592
- snapshot: props.snapshot
593
- });
594
- try {
595
- while (true) {
596
- if (ctx.iterations.length >= ctx.loop) {
597
- return {
598
- status: "error",
599
- context: ctx,
600
- error: `Loop limit exceeded. Maximum allowed loops: ${ctx.loop}`,
601
- iterations: ctx.iterations
602
- };
603
- }
604
- const iteration = await ctx.nextIteration();
605
- if (signal == null ? void 0 : signal.aborted) {
606
- iteration.end({
607
- type: "aborted",
608
- aborted: {
609
- reason: _nullishCoalesce(signal.reason, () => ( "The operation was aborted"))
610
- }
611
- });
612
- return {
613
- status: "error",
614
- error: _nullishCoalesce(signal.reason, () => ( "The operation was aborted")),
615
- context: ctx,
616
- iterations: ctx.iterations
617
- };
618
- }
619
- cleanups.push(
620
- iteration.traces.onPush((traces) => {
621
- for (const trace of traces) {
622
- onTrace == null ? void 0 : onTrace({ trace, iteration: ctx.iterations.length });
623
- }
624
- })
625
- );
626
- try {
627
- await executeIteration({
628
- iteration,
629
- ctx,
630
- cognitive,
631
- abortSignal: signal,
632
- onExit,
633
- onBeforeExecution
634
- });
635
- } catch (err) {
636
- iteration.end({
637
- type: "execution_error",
638
- execution_error: {
639
- message: "An unexpected error occurred: " + getErrorMessage(err),
640
- stack: _chunkIKSIOIIPcjs.cleanStackTrace.call(void 0, _nullishCoalesce(err.stack, () => ( "No stack trace available")))
641
- }
642
- });
643
- }
644
- try {
645
- await (onIterationEnd == null ? void 0 : onIterationEnd(iteration));
646
- } catch (err) {
647
- console.error(err);
648
- }
649
- if (iteration.status.type === "exit_success") {
650
- return {
651
- status: "success",
652
- context: ctx,
653
- iterations: ctx.iterations
654
- };
655
- }
656
- if (iteration.status.type === "callback_requested") {
657
- return {
658
- status: "interrupted",
659
- context: ctx,
660
- iterations: ctx.iterations,
661
- signal: iteration.status.callback_requested.signal,
662
- snapshot: _chunk6YWYCVABcjs.Snapshot.fromSignal(iteration.status.callback_requested.signal)
663
- };
664
- }
665
- if (iteration.status.type === "thinking_requested" || iteration.status.type === "exit_error" || iteration.status.type === "execution_error" || iteration.status.type === "invalid_code_error") {
666
- continue;
667
- }
668
- return {
669
- context: ctx,
670
- error: _nullishCoalesce(iteration.error, () => ( `Unknown error. Status: ${iteration.status.type}`)),
671
- status: "error",
672
- iterations: ctx.iterations
673
- };
674
- }
675
- } catch (error) {
676
- return {
677
- status: "error",
678
- iterations: ctx.iterations,
679
- context: ctx,
680
- error: error instanceof Error ? error.message : _nullishCoalesce((error == null ? void 0 : error.toString()), () => ( "Unknown error"))
681
- };
682
- } finally {
683
- for (const cleanup of cleanups) {
684
- try {
685
- cleanup();
686
- } catch (e2) {
687
- }
688
- }
689
- }
690
- };
691
- var executeIteration = async ({
692
- iteration,
693
- ctx,
694
- cognitive,
695
- abortSignal,
696
- onExit,
697
- onBeforeExecution
698
- }) => {
699
- var _a, _b, _c, _d, _e;
700
- let startedAt = Date.now();
701
- const traces = iteration.traces;
702
- const modelLimit = 128e3;
703
- const responseLengthBuffer = getModelOutputLimit(modelLimit);
704
- const messages = _chunkFIVFS4HGcjs.truncateWrappedContent.call(void 0, {
705
- messages: iteration.messages,
706
- tokenLimit: modelLimit - responseLengthBuffer,
707
- throwOnFailure: false
708
- }).filter(
709
- (x) => (
710
- // Filter out empty messages, as they are not valid inputs for the LLM
711
- // This can happen when a message is truncated and the content is empty
712
- x.content.trim().length > 0
713
- )
714
- );
715
- traces.push({
716
- type: "llm_call_started",
717
- started_at: startedAt,
718
- ended_at: startedAt,
719
- model: _nullishCoalesce(ctx.model, () => ( ""))
720
- });
721
- const output = await cognitive.generateContent({
722
- signal: abortSignal,
723
- systemPrompt: (_a = messages.find((x) => x.role === "system")) == null ? void 0 : _a.content,
724
- model: ctx.model,
725
- temperature: ctx.temperature,
726
- responseFormat: "text",
727
- messages: messages.filter((x) => x.role === "user" || x.role === "assistant").map(
728
- (x) => ({
729
- role: x.role === "user" ? "user" : "assistant",
730
- type: "text",
731
- content: x.content
732
- })
733
- ),
734
- stopSequences: ctx.version.getStopTokens()
735
- });
736
- 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;
737
- if (!out) {
738
- throw new Error("No output from LLM");
739
- }
740
- const assistantResponse = ctx.version.parseAssistantResponse(out);
741
- iteration.code = assistantResponse.code.trim();
742
- if (typeof onBeforeExecution === "function") {
743
- try {
744
- await onBeforeExecution(iteration);
745
- } catch (err) {
746
- if (err instanceof _chunkJDABP4SDcjs.ThinkSignal) {
747
- return iteration.end({
748
- type: "thinking_requested",
749
- thinking_requested: {
750
- variables: err.context,
751
- reason: err.reason
752
- }
753
- });
754
- }
755
- return iteration.end({
756
- type: "execution_error",
757
- execution_error: {
758
- message: `Error in onBeforeExecution hook: ${getErrorMessage(err)}`,
759
- stack: _chunkIKSIOIIPcjs.cleanStackTrace.call(void 0, _nullishCoalesce(err.stack, () => ( "No stack trace available")))
760
- }
761
- });
762
- }
763
- }
764
- iteration.llm = {
765
- cached: output.meta.cached || false,
766
- ended_at: Date.now(),
767
- started_at: startedAt,
768
- status: "success",
769
- tokens: output.meta.tokens.input + output.meta.tokens.output,
770
- spend: output.meta.cost.input + output.meta.cost.output,
771
- output: assistantResponse.raw,
772
- model: `${output.meta.model.integration}:${output.meta.model.model}`
773
- };
774
- traces.push({
775
- type: "llm_call_success",
776
- started_at: startedAt,
777
- ended_at: iteration.llm.ended_at,
778
- model: _nullishCoalesce(ctx.model, () => ( "")),
779
- code: iteration.code
780
- });
781
- const vmContext = { ..._chunkP7J2WCBBcjs.stripInvalidIdentifiers.call(void 0, iteration.variables) };
782
- for (const obj of iteration.objects) {
783
- const internalValues = {};
784
- const instance = {};
785
- for (const { name, value, writable, type } of _nullishCoalesce(obj.properties, () => ( []))) {
786
- internalValues[name] = value;
787
- const initialValue = value;
788
- const schema = _nullishCoalesce(type, () => ( _zui.z.any()));
789
- Object.defineProperty(instance, name, {
790
- enumerable: true,
791
- configurable: true,
792
- get() {
793
- return internalValues[name];
794
- },
795
- set(value2) {
796
- if (_chunkUQOBUJIQcjs.isEqual_default.call(void 0, value2, internalValues[name])) {
797
- return;
798
- }
799
- if (!writable) {
800
- throw new (0, _chunkJDABP4SDcjs.AssignmentError)(`Property ${obj.name}.${name} is read-only and cannot be modified`);
801
- }
802
- if (value2 === internalValues[name]) {
803
- return;
804
- }
805
- const parsed = schema.safeParse(value2);
806
- if (!parsed.success) {
807
- throw new (0, _chunkJDABP4SDcjs.AssignmentError)(
808
- `Invalid value for Object property ${obj.name}.${name}: ${getErrorMessage(parsed.error)}`
809
- );
810
- }
811
- internalValues[name] = parsed.data;
812
- traces.push({
813
- type: "property",
814
- started_at: Date.now(),
815
- object: obj.name,
816
- property: name,
817
- value: parsed.data
818
- });
819
- iteration.trackMutation({ object: obj.name, property: name, before: initialValue, after: parsed.data });
820
- }
821
- });
822
- }
823
- for (const tool of _nullishCoalesce(obj.tools, () => ( []))) {
824
- instance[tool.name] = wrapTool({ tool, traces, object: obj.name });
825
- }
826
- Object.preventExtensions(instance);
827
- Object.seal(instance);
828
- vmContext[obj.name] = instance;
829
- }
830
- for (const tool of iteration.tools) {
831
- const wrapped = wrapTool({ tool, traces });
832
- for (const key of [tool.name, ..._nullishCoalesce(tool.aliases, () => ( []))]) {
833
- vmContext[key] = wrapped;
834
- }
835
- }
836
- if (abortSignal == null ? void 0 : abortSignal.aborted) {
837
- traces.push({
838
- type: "abort_signal",
839
- started_at: Date.now(),
840
- reason: "The operation was aborted by user."
841
- });
842
- return iteration.end({
843
- type: "aborted",
844
- aborted: {
845
- reason: _nullishCoalesce((abortSignal == null ? void 0 : abortSignal.reason), () => ( "The operation was aborted"))
846
- }
847
- });
848
- }
849
- startedAt = Date.now();
850
- const result = await _chunk5NVNEEYWcjs.runAsyncFunction.call(void 0, vmContext, iteration.code, traces, abortSignal, ctx.timeout).catch(
851
- (err) => {
852
- return {
853
- success: false,
854
- error: err,
855
- lines_executed: [],
856
- traces: [],
857
- variables: {}
858
- };
859
- }
860
- );
861
- if (result.error && result.error instanceof _chunkJDABP4SDcjs.InvalidCodeError) {
862
- return iteration.end({
863
- type: "invalid_code_error",
864
- invalid_code_error: {
865
- message: result.error.message
866
- }
867
- });
868
- }
869
- traces.push({
870
- type: "code_execution",
871
- lines_executed: _nullishCoalesce(result.lines_executed, () => ( 0)),
872
- started_at: startedAt,
873
- ended_at: Date.now()
874
- });
875
- if (result.error && result.error instanceof _chunkJDABP4SDcjs.CodeExecutionError) {
876
- return iteration.end({
877
- type: "execution_error",
878
- execution_error: {
879
- message: result.error.message,
880
- stack: _chunkIKSIOIIPcjs.cleanStackTrace.call(void 0, _nullishCoalesce(_nullishCoalesce(result.error.stacktrace, () => ( result.error.stack)), () => ( "No stack trace available")))
881
- }
882
- });
883
- }
884
- if (abortSignal == null ? void 0 : abortSignal.aborted) {
885
- return iteration.end({
886
- type: "aborted",
887
- aborted: {
888
- reason: _nullishCoalesce((abortSignal == null ? void 0 : abortSignal.reason), () => ( "The operation was aborted"))
889
- }
890
- });
891
- }
892
- if (!result.success) {
893
- return iteration.end({
894
- type: "execution_error",
895
- execution_error: {
896
- message: _nullishCoalesce(((_e = result == null ? void 0 : result.error) == null ? void 0 : _e.message), () => ( "Unknown error occurred")),
897
- stack: _chunkIKSIOIIPcjs.cleanStackTrace.call(void 0, _nullishCoalesce(result.error.stack, () => ( "No stack trace available")))
898
- }
899
- });
900
- }
901
- if (result.signal instanceof _chunkJDABP4SDcjs.ThinkSignal) {
902
- return iteration.end({
903
- type: "thinking_requested",
904
- thinking_requested: {
905
- variables: result.signal.context,
906
- reason: result.signal.reason
907
- }
908
- });
909
- }
910
- if (result.signal instanceof _chunkJDABP4SDcjs.SnapshotSignal) {
911
- return iteration.end({
912
- type: "callback_requested",
913
- callback_requested: {
914
- signal: result.signal
915
- }
916
- });
917
- }
918
- const validActions = [...iteration.exits.map((x) => x.name.toLowerCase()), "think"];
919
- let returnValue = result.success && result.return_value ? result.return_value : null;
920
- const returnAction = returnValue == null ? void 0 : returnValue.action;
921
- 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())))));
922
- if (returnAction === "think") {
923
- const variables = _chunkUQOBUJIQcjs.omit_default.call(void 0, _nullishCoalesce(returnValue, () => ( {})), "action");
924
- if (_chunkUQOBUJIQcjs.isPlainObject_default.call(void 0, variables) && Object.keys(variables).length > 0) {
925
- return iteration.end({
926
- type: "thinking_requested",
927
- thinking_requested: {
928
- variables,
929
- reason: "Thinking requested"
930
- }
931
- });
932
- }
933
- return iteration.end({
934
- type: "thinking_requested",
935
- thinking_requested: {
936
- reason: "Thinking requested",
937
- variables: iteration.variables
938
- }
939
- });
940
- }
941
- if (!returnAction) {
942
- return iteration.end({
943
- type: "exit_error",
944
- exit_error: {
945
- exit: "n/a",
946
- message: `Code did not return an action. Valid actions are: ${validActions.join(", ")}`,
947
- return_value: returnValue
948
- }
949
- });
950
- }
951
- if (!returnExit) {
952
- return iteration.end({
953
- type: "exit_error",
954
- exit_error: {
955
- exit: returnAction,
956
- message: `Exit "${returnAction}" not found. Valid actions are: ${validActions.join(", ")}`,
957
- return_value: returnValue
958
- }
959
- });
960
- }
961
- if (returnExit.zSchema) {
962
- const parsed = returnExit.zSchema.safeParse(returnValue == null ? void 0 : returnValue.value);
963
- if (!parsed.success) {
964
- return iteration.end({
965
- type: "exit_error",
966
- exit_error: {
967
- exit: returnExit.name,
968
- message: `Invalid return value for exit ${returnExit.name}: ${getErrorMessage(parsed.error)}`,
969
- return_value: returnValue
970
- }
971
- });
972
- }
973
- returnValue = { action: returnExit.name, value: parsed.data };
974
- }
975
- try {
976
- await (onExit == null ? void 0 : onExit(returnExit, returnValue == null ? void 0 : returnValue.value));
977
- } catch (err) {
978
- return iteration.end({
979
- type: "exit_error",
980
- exit_error: {
981
- exit: returnExit.name,
982
- message: `Error executing exit ${returnExit.name}: ${getErrorMessage(err)}`,
983
- return_value: returnValue
984
- }
985
- });
986
- }
987
- return iteration.end({
988
- type: "exit_success",
989
- exit_success: {
990
- exit_name: returnExit.name,
991
- return_value: returnValue == null ? void 0 : returnValue.value
992
- }
993
- });
994
- };
995
- function wrapTool({ tool, traces, object }) {
996
- const getToolInput = (input) => _nullishCoalesce(tool.zInput.safeParse(input).data, () => ( input));
997
- return function(input) {
998
- const toolCallId = `tcall_${_ulid.ulid.call(void 0, )}`;
999
- const alertSlowTool = setTimeout(
1000
- () => traces.push({
1001
- type: "tool_slow",
1002
- tool_name: tool.name,
1003
- tool_call_id: toolCallId,
1004
- started_at: Date.now(),
1005
- input: getToolInput(input),
1006
- object,
1007
- duration: SLOW_TOOL_WARNING
1008
- }),
1009
- SLOW_TOOL_WARNING
1010
- );
1011
- const cancelSlowTool = () => clearTimeout(alertSlowTool);
1012
- const toolStart = Date.now();
1013
- let output;
1014
- let error;
1015
- let success = true;
1016
- const handleSignals = (error2) => {
1017
- if (output === error2) {
1018
- return true;
1019
- }
1020
- if (error2 instanceof _chunkJDABP4SDcjs.SnapshotSignal) {
1021
- error2.toolCall = {
1022
- name: tool.name,
1023
- inputSchema: tool.input,
1024
- outputSchema: tool.output,
1025
- input
1026
- };
1027
- error2.message = _chunkJDABP4SDcjs.Signals.serializeError(error2);
1028
- }
1029
- if (error2 instanceof _chunkJDABP4SDcjs.ThinkSignal) {
1030
- traces.push({
1031
- type: "think_signal",
1032
- started_at: Date.now(),
1033
- line: 0,
1034
- ended_at: Date.now()
1035
- });
1036
- success = true;
1037
- output = error2;
1038
- return true;
1039
- }
1040
- return false;
1041
- };
1042
- try {
1043
- const result = tool.execute(input, {
1044
- callId: toolCallId
1045
- });
1046
- if (result instanceof Promise || (result == null ? void 0 : result.then) && (result == null ? void 0 : result.catch)) {
1047
- return result.then((res) => {
1048
- output = res;
1049
- success = true;
1050
- return res;
1051
- }).catch((err) => {
1052
- if (!handleSignals(err)) {
1053
- success = false;
1054
- error = err;
1055
- }
1056
- throw err;
1057
- }).finally(() => {
1058
- cancelSlowTool();
1059
- traces.push({
1060
- type: "tool_call",
1061
- tool_call_id: toolCallId,
1062
- started_at: toolStart,
1063
- ended_at: Date.now(),
1064
- tool_name: tool.name,
1065
- object,
1066
- input: getToolInput(input),
1067
- output,
1068
- error,
1069
- success
1070
- });
1071
- });
1072
- }
1073
- success = true;
1074
- output = result;
1075
- } catch (err) {
1076
- if (!handleSignals(err)) {
1077
- success = false;
1078
- error = err;
1079
- }
1080
- }
1081
- cancelSlowTool();
1082
- traces.push({
1083
- type: "tool_call",
1084
- tool_call_id: toolCallId,
1085
- started_at: toolStart,
1086
- ended_at: Date.now(),
1087
- tool_name: tool.name,
1088
- object,
1089
- input: getToolInput(input),
1090
- output,
1091
- error,
1092
- success
1093
- });
1094
- if (!success) {
1095
- throw error;
1096
- }
1097
- if (output instanceof _chunkJDABP4SDcjs.VMSignal) {
1098
- throw output;
1099
- }
1100
- return output;
1101
- };
1102
- }
1103
- var llmz = {
1104
- executeContext
1105
- };
1106
-
1107
-
1108
- exports.llmz = llmz;