llmz 0.0.2 → 0.0.4

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 (81) hide show
  1. package/dist/{chunk-RB2L33ZR.js → chunk-4EWY34YA.js} +24 -24
  2. package/dist/{chunk-UG2GCPHF.js → chunk-4KB5WXHR.js} +3 -2
  3. package/dist/{chunk-YPOOKE3A.cjs → chunk-4QTK5ALH.cjs} +59 -28
  4. package/dist/{chunk-BKGPO722.cjs → chunk-5NVNEEYW.cjs} +55 -55
  5. package/dist/{chunk-DFZ6GX5C.js → chunk-5TRUJES5.js} +1 -1
  6. package/dist/{chunk-C2TSWGYC.cjs → chunk-6YWYCVAB.cjs} +5 -4
  7. package/dist/{chunk-7S3UY52U.js → chunk-7WRN4E42.js} +36 -8
  8. package/dist/{chunk-Q4DEJGXD.js → chunk-EFGXTO64.js} +2 -2
  9. package/dist/{chunk-VP6WWAE2.cjs → chunk-FIVFS4HG.cjs} +2 -2
  10. package/dist/{chunk-YUDUY7XW.cjs → chunk-HYVKY3W6.cjs} +23 -24
  11. package/dist/{chunk-PQN6HW7A.cjs → chunk-IKSIOIIP.cjs} +3 -3
  12. package/dist/{chunk-ZUAFUOUR.cjs → chunk-JDABP4SD.cjs} +3 -3
  13. package/dist/{chunk-E5W3P7EO.cjs → chunk-JK2LZW2G.cjs} +20 -2
  14. package/dist/{chunk-RJHDQL4C.js → chunk-JKVVQN2P.js} +1 -1
  15. package/dist/{chunk-MSTBEKOQ.js → chunk-JQBT7UWN.js} +3 -3
  16. package/dist/{chunk-BOLDQGES.cjs → chunk-KMZDFWYZ.cjs} +3 -3
  17. package/dist/{chunk-R2N2UUKC.cjs → chunk-LVKZYKTP.cjs} +14 -8
  18. package/dist/{chunk-7FIPXMZR.js → chunk-ORQP26SZ.js} +1 -1
  19. package/dist/{chunk-ENFB45AI.cjs → chunk-P7J2WCBB.cjs} +7 -7
  20. package/dist/{chunk-DILHJIMP.js → chunk-QBXIE6EY.js} +49 -18
  21. package/dist/{chunk-UVSOAH2H.js → chunk-QT4QF3YA.js} +1 -1
  22. package/dist/{chunk-6QDFMQHA.js → chunk-S6WICIDW.js} +8 -2
  23. package/dist/{chunk-QPSUMPLP.js → chunk-TJQVC4CE.js} +6 -6
  24. package/dist/{chunk-ZSRC3TN4.js → chunk-UNMKB2DA.js} +9 -10
  25. package/dist/{chunk-3CGLDS5T.cjs → chunk-UQOBUJIQ.cjs} +38 -10
  26. package/dist/{chunk-QZGFCB4T.cjs → chunk-VAF2H6UD.cjs} +22 -22
  27. package/dist/{chunk-E2B5DRIC.cjs → chunk-W6U2VXSF.cjs} +25 -25
  28. package/dist/{chunk-3T465BEW.js → chunk-ZCPQ3QOW.js} +19 -1
  29. package/dist/citations.d.ts +63 -0
  30. package/dist/component-HQ5YQNRX.cjs +14 -0
  31. package/dist/component-LQDU72LX.js +14 -0
  32. package/dist/component.d.ts +32 -10
  33. package/dist/component.default.d.ts +334 -8
  34. package/dist/context.d.ts +3 -1
  35. package/dist/dual-modes-QHBOFWHM.js +13 -0
  36. package/dist/dual-modes-YE4S2AIL.cjs +13 -0
  37. package/dist/exit-F6ZUL2NV.js +8 -0
  38. package/dist/exit-IDKGZD7M.cjs +8 -0
  39. package/dist/exit.d.ts +2 -0
  40. package/dist/index.cjs +209 -58
  41. package/dist/index.d.ts +3 -2
  42. package/dist/index.js +193 -42
  43. package/dist/{jsx-CJ2RBODH.js → jsx-AEHVFB3L.js} +2 -3
  44. package/dist/jsx-AJAXBWFE.cjs +13 -0
  45. package/dist/{llmz-WY74UVE3.js → llmz-AS5TGCQS.js} +98 -39
  46. package/dist/{llmz-EGTGPSTX.cjs → llmz-R6XZG3JQ.cjs} +129 -70
  47. package/dist/llmz.d.ts +3 -2
  48. package/dist/snapshots.d.ts +9 -1
  49. package/dist/tool-LY77IWV2.js +11 -0
  50. package/dist/tool-SWJYOR2Z.cjs +11 -0
  51. package/dist/tool.d.ts +34 -9
  52. package/dist/truncator-NYN7BGKJ.cjs +10 -0
  53. package/dist/truncator-WAL2GCUY.js +10 -0
  54. package/dist/types.d.ts +7 -1
  55. package/dist/typings-AIV2OASX.js +10 -0
  56. package/dist/typings-XR6CYHW4.cjs +10 -0
  57. package/dist/{utils-TS23YJPE.js → utils-L5GLCS3C.js} +2 -3
  58. package/dist/utils-QC4I2L6R.cjs +37 -0
  59. package/dist/vm-GJ5R72AP.cjs +12 -0
  60. package/dist/vm-ZUQOSRRM.js +12 -0
  61. package/dist/vm.d.ts +1 -1
  62. package/package.json +8 -6
  63. package/dist/chunk-A7BDFLAE.cjs +0 -30
  64. package/dist/chunk-YSQDPG26.js +0 -30
  65. package/dist/component-DWBRHLEN.cjs +0 -9
  66. package/dist/component-R3ZAVXUC.js +0 -9
  67. package/dist/dual-modes-2VDEOJ5D.js +0 -13
  68. package/dist/dual-modes-3MH5N6RY.cjs +0 -13
  69. package/dist/exit-GYMXZZ4I.js +0 -9
  70. package/dist/exit-PQHMJEOG.cjs +0 -9
  71. package/dist/jsx-PPLE5YTB.cjs +0 -14
  72. package/dist/tool-AY47ML65.cjs +0 -12
  73. package/dist/tool-WVIPESKN.js +0 -12
  74. package/dist/truncator-MTONQESU.js +0 -11
  75. package/dist/truncator-ZKPOW2I3.cjs +0 -11
  76. package/dist/typings-HTYLRNIC.js +0 -11
  77. package/dist/typings-QRJGGNUM.cjs +0 -11
  78. package/dist/utils-UR4IMUHC.cjs +0 -38
  79. package/dist/vm-4JFSZAMR.cjs +0 -13
  80. package/dist/vm-SMMA664M.js +0 -13
  81. package/patches/source-map-js@1.2.1.patch +0 -16
@@ -1,16 +1,17 @@
1
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
2
 
3
- var _chunkBKGPO722cjs = require('./chunk-BKGPO722.cjs');
4
- require('./chunk-BOLDQGES.cjs');
3
+ var _chunkW6U2VXSFcjs = require('./chunk-W6U2VXSF.cjs');
5
4
 
6
5
 
7
- var _chunkC2TSWGYCcjs = require('./chunk-C2TSWGYC.cjs');
6
+ var _chunk5NVNEEYWcjs = require('./chunk-5NVNEEYW.cjs');
8
7
 
9
8
 
10
- var _chunkYPOOKE3Acjs = require('./chunk-YPOOKE3A.cjs');
11
- require('./chunk-YUDUY7XW.cjs');
9
+ var _chunk6YWYCVABcjs = require('./chunk-6YWYCVAB.cjs');
10
+ require('./chunk-VAF2H6UD.cjs');
12
11
 
13
12
 
13
+ var _chunk4QTK5ALHcjs = require('./chunk-4QTK5ALH.cjs');
14
+ require('./chunk-HYVKY3W6.cjs');
14
15
 
15
16
 
16
17
 
@@ -18,44 +19,43 @@ require('./chunk-YUDUY7XW.cjs');
18
19
 
19
20
 
20
21
 
21
- var _chunkZUAFUOURcjs = require('./chunk-ZUAFUOUR.cjs');
22
22
 
23
23
 
24
- var _chunkR2N2UUKCcjs = require('./chunk-R2N2UUKC.cjs');
24
+ var _chunkJDABP4SDcjs = require('./chunk-JDABP4SD.cjs');
25
25
 
26
26
 
27
- var _chunkE2B5DRICcjs = require('./chunk-E2B5DRIC.cjs');
28
- require('./chunk-QZGFCB4T.cjs');
27
+ var _chunkIKSIOIIPcjs = require('./chunk-IKSIOIIP.cjs');
29
28
 
30
29
 
31
- var _chunkPQN6HW7Acjs = require('./chunk-PQN6HW7A.cjs');
30
+ var _chunkLVKZYKTPcjs = require('./chunk-LVKZYKTP.cjs');
32
31
 
33
32
 
34
- var _chunkE5W3P7EOcjs = require('./chunk-E5W3P7EO.cjs');
33
+ var _chunkJK2LZW2Gcjs = require('./chunk-JK2LZW2G.cjs');
34
+ require('./chunk-KMZDFWYZ.cjs');
35
35
 
36
36
 
37
37
 
38
- var _chunkVP6WWAE2cjs = require('./chunk-VP6WWAE2.cjs');
38
+ var _chunkFIVFS4HGcjs = require('./chunk-FIVFS4HG.cjs');
39
39
 
40
40
 
41
41
 
42
- var _chunkENFB45AIcjs = require('./chunk-ENFB45AI.cjs');
42
+ var _chunkP7J2WCBBcjs = require('./chunk-P7J2WCBB.cjs');
43
43
 
44
44
 
45
45
 
46
46
 
47
47
 
48
48
 
49
- var _chunk3CGLDS5Tcjs = require('./chunk-3CGLDS5T.cjs');
50
- require('./chunk-A7BDFLAE.cjs');
49
+ var _chunkUQOBUJIQcjs = require('./chunk-UQOBUJIQ.cjs');
51
50
 
52
51
  // src/llmz.ts
53
52
  var _cognitive = require('@botpress/cognitive');
54
53
  var _zui = require('@bpinternal/zui');
55
54
  var _ms = require('ms'); var _ms2 = _interopRequireDefault(_ms);
55
+ var _ulid = require('ulid');
56
56
 
57
57
  // src/context.ts
58
- var _ulid = require('ulid');
58
+
59
59
 
60
60
  // src/getter.ts
61
61
  var getValue = async (valueOrGetter, ctx) => {
@@ -72,15 +72,15 @@ var getValue = async (valueOrGetter, ctx) => {
72
72
 
73
73
  // src/handlers.ts
74
74
  var Emitter = (_class = class {constructor() { _class.prototype.__init.call(this);_class.prototype.__init2.call(this); }
75
- __init() {this.handlers = []}
75
+ __init() {this._handlers = []}
76
76
  __init2() {this.subscribe = (fn) => {
77
- this.handlers.push(fn);
77
+ this._handlers.push(fn);
78
78
  return () => {
79
- this.handlers = this.handlers.filter((handler) => handler !== fn);
79
+ this._handlers = this._handlers.filter((handler) => handler !== fn);
80
80
  };
81
81
  }}
82
82
  emit(event) {
83
- this.handlers.forEach((handler) => handler(event));
83
+ this._handlers.forEach((handler) => handler(event));
84
84
  }
85
85
  }, _class);
86
86
  var HookedArray = class extends Array {
@@ -145,11 +145,11 @@ ${preview.trim()}
145
145
  };
146
146
 
147
147
  // src/context.ts
148
- var ThinkExit = new (0, _chunkR2N2UUKCcjs.Exit)({
148
+ var ThinkExit = new (0, _chunkLVKZYKTPcjs.Exit)({
149
149
  name: "think",
150
150
  description: "Think about the current situation and provide a response"
151
151
  });
152
- var ListenExit = new (0, _chunkR2N2UUKCcjs.Exit)({
152
+ var ListenExit = new (0, _chunkLVKZYKTPcjs.Exit)({
153
153
  name: "listen",
154
154
  description: "Listen to the user and provide a response"
155
155
  });
@@ -262,17 +262,19 @@ var Context = (_class2 = class {
262
262
 
263
263
 
264
264
 
265
- __init3() {this.version = _chunkE2B5DRICcjs.DualModePrompt}
265
+ __init3() {this.version = _chunkW6U2VXSFcjs.DualModePrompt}
266
+ __init4() {this.timeout = 6e4}
267
+ // Default timeout of 60 seconds
266
268
 
267
269
 
268
270
 
269
271
 
270
272
 
271
- __init4() {this.iteration = 0}
273
+ __init5() {this.iteration = 0}
272
274
 
273
275
  async nextIteration() {
274
276
  if (this.iterations.length >= this.loop) {
275
- throw new (0, _chunkZUAFUOURcjs.LoopExceededError)();
277
+ throw new (0, _chunkJDABP4SDcjs.LoopExceededError)();
276
278
  }
277
279
  if (this.snapshot && this.snapshot.status.type === "pending") {
278
280
  throw new Error(
@@ -297,12 +299,12 @@ var Context = (_class2 = class {
297
299
  const variables = {};
298
300
  if ((lastIteration == null ? void 0 : lastIteration.status.type) === "thinking_requested") {
299
301
  const lastThinkingVariables = lastIteration.status.thinking_requested.variables;
300
- if (_chunk3CGLDS5Tcjs.isPlainObject_default.call(void 0, lastThinkingVariables)) {
301
- Object.assign(variables, _chunk3CGLDS5Tcjs.cloneDeep_default.call(void 0, lastThinkingVariables));
302
+ if (_chunkUQOBUJIQcjs.isPlainObject_default.call(void 0, lastThinkingVariables)) {
303
+ Object.assign(variables, _chunkUQOBUJIQcjs.cloneDeep_default.call(void 0, lastThinkingVariables));
302
304
  }
303
305
  }
304
- if (_chunk3CGLDS5Tcjs.isPlainObject_default.call(void 0, lastIteration == null ? void 0 : lastIteration.variables)) {
305
- Object.assign(variables, _chunk3CGLDS5Tcjs.cloneDeep_default.call(void 0, _nullishCoalesce((lastIteration == null ? void 0 : lastIteration.variables), () => ( {}))));
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), () => ( {}))));
306
308
  }
307
309
  return variables;
308
310
  }
@@ -375,7 +377,7 @@ var Context = (_class2 = class {
375
377
  ...lastIterationMessages,
376
378
  {
377
379
  role: "assistant",
378
- content: _chunkVP6WWAE2cjs.wrapContent.call(void 0, _nullishCoalesce(((_c = lastIteration.llm) == null ? void 0 : _c.output), () => ( "")), { preserve: "top", flex: 4, minTokens: 25 })
380
+ content: _chunkFIVFS4HGcjs.wrapContent.call(void 0, _nullishCoalesce(((_c = lastIteration.llm) == null ? void 0 : _c.output), () => ( "")), { preserve: "top", flex: 4, minTokens: 25 })
379
381
  },
380
382
  await this.version.getThinkingMessage({
381
383
  reason: lastIteration.status.thinking_requested.reason,
@@ -388,7 +390,7 @@ var Context = (_class2 = class {
388
390
  ...lastIterationMessages,
389
391
  {
390
392
  role: "assistant",
391
- content: _chunkVP6WWAE2cjs.wrapContent.call(void 0, _nullishCoalesce(((_d = lastIteration.llm) == null ? void 0 : _d.output), () => ( "")), { preserve: "top", flex: 4, minTokens: 25 })
393
+ content: _chunkFIVFS4HGcjs.wrapContent.call(void 0, _nullishCoalesce(((_d = lastIteration.llm) == null ? void 0 : _d.output), () => ( "")), { preserve: "top", flex: 4, minTokens: 25 })
392
394
  },
393
395
  await this.version.getInvalidCodeMessage({
394
396
  code: _nullishCoalesce(lastIteration.code, () => ( "// No code generated")),
@@ -401,7 +403,7 @@ var Context = (_class2 = class {
401
403
  ...lastIterationMessages,
402
404
  {
403
405
  role: "assistant",
404
- content: _chunkVP6WWAE2cjs.wrapContent.call(void 0, _nullishCoalesce(((_e = lastIteration.llm) == null ? void 0 : _e.output), () => ( "")), { preserve: "top", flex: 4, minTokens: 25 })
406
+ content: _chunkFIVFS4HGcjs.wrapContent.call(void 0, _nullishCoalesce(((_e = lastIteration.llm) == null ? void 0 : _e.output), () => ( "")), { preserve: "top", flex: 4, minTokens: 25 })
405
407
  },
406
408
  await this.version.getInvalidCodeMessage({
407
409
  code: _nullishCoalesce(lastIteration.code, () => ( "// No code generated")),
@@ -414,7 +416,7 @@ var Context = (_class2 = class {
414
416
  ...lastIterationMessages,
415
417
  {
416
418
  role: "assistant",
417
- content: _chunkVP6WWAE2cjs.wrapContent.call(void 0, _nullishCoalesce(((_f = lastIteration.llm) == null ? void 0 : _f.output), () => ( "")), { preserve: "top", flex: 4, minTokens: 25 })
419
+ content: _chunkFIVFS4HGcjs.wrapContent.call(void 0, _nullishCoalesce(((_f = lastIteration.llm) == null ? void 0 : _f.output), () => ( "")), { preserve: "top", flex: 4, minTokens: 25 })
418
420
  },
419
421
  await this.version.getCodeExecutionErrorMessage({
420
422
  message: lastIteration.status.execution_error.message,
@@ -430,7 +432,7 @@ var Context = (_class2 = class {
430
432
  var _a;
431
433
  const instructions = await getValue(this.instructions, this);
432
434
  const transcript = new TranscriptArray(await _asyncNullishCoalesce(await getValue(this.transcript, this), async () => ( [])));
433
- const tools = _chunkYPOOKE3Acjs.Tool.withUniqueNames(await _asyncNullishCoalesce(await getValue(this.tools, this), async () => ( [])));
435
+ const tools = _chunk4QTK5ALHcjs.Tool.withUniqueNames(await _asyncNullishCoalesce(await getValue(this.tools, this), async () => ( [])));
434
436
  const objects = await _asyncNullishCoalesce(await getValue(this.objects, this), async () => ( []));
435
437
  const exits = await _asyncNullishCoalesce(await getValue(this.exits, this), async () => ( []));
436
438
  const components = await _asyncNullishCoalesce(await getValue(this.components, this), async () => ( []));
@@ -441,7 +443,7 @@ var Context = (_class2 = class {
441
443
  throw new Error("Too many tools. Expected at most 100 tools.");
442
444
  }
443
445
  for (const component of components) {
444
- _chunkE5W3P7EOcjs.assertValidComponent.call(void 0, component);
446
+ _chunkJK2LZW2Gcjs.assertValidComponent.call(void 0, component.definition);
445
447
  }
446
448
  const ReservedToolNames = [
447
449
  "think",
@@ -472,7 +474,10 @@ var Context = (_class2 = class {
472
474
  }
473
475
  if (MessageTool && components.length) {
474
476
  MessageTool.aliases = Array.from(
475
- /* @__PURE__ */ new Set([...MessageTool.aliases, ...components.flatMap((x) => [x.name, ..._nullishCoalesce(x.aliases, () => ( []))])])
477
+ /* @__PURE__ */ new Set([
478
+ ...MessageTool.aliases,
479
+ ...components.flatMap((x) => [x.definition.name, ..._nullishCoalesce(x.definition.aliases, () => ( []))])
480
+ ])
476
481
  );
477
482
  }
478
483
  const nonMessageTools = tools.filter((x) => {
@@ -486,10 +491,12 @@ var Context = (_class2 = class {
486
491
  if (ReservedToolNames.includes(name)) {
487
492
  throw new Error(`Tool name "${name}" (${tool.name}) is reserved. Please choose a different name.`);
488
493
  }
489
- if (components.find((x) => {
490
- var _a2;
491
- return x.name.toLowerCase() === name || ((_a2 = x.aliases) == null ? void 0 : _a2.map((x2) => x2.toLowerCase()).includes(name));
492
- })) {
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
+ )) {
493
500
  throw new Error(
494
501
  `Tool name "${name}" (${tool.name}) is already used by a component. Please choose a different name.`
495
502
  );
@@ -528,7 +535,7 @@ var Context = (_class2 = class {
528
535
  components
529
536
  };
530
537
  }
531
- constructor(props) {;_class2.prototype.__init3.call(this);_class2.prototype.__init4.call(this);
538
+ constructor(props) {;_class2.prototype.__init3.call(this);_class2.prototype.__init4.call(this);_class2.prototype.__init5.call(this);
532
539
  this.id = `llmz_${_ulid.ulid.call(void 0, )}`;
533
540
  this.transcript = props.transcript;
534
541
  this.instructions = props.instructions;
@@ -536,6 +543,7 @@ var Context = (_class2 = class {
536
543
  this.tools = props.tools;
537
544
  this.exits = props.exits;
538
545
  this.components = props.components;
546
+ this.timeout = Math.min(999999999, Math.max(0, _nullishCoalesce(props.timeout, () => ( 6e4))));
539
547
  this.loop = _nullishCoalesce(props.loop, () => ( 3));
540
548
  this.temperature = _nullishCoalesce(props.temperature, () => ( 0.7));
541
549
  this.model = props.model;
@@ -559,15 +567,15 @@ var RESPONSE_LENGTH_BUFFER = {
559
567
  MAX_TOKENS: 16e3,
560
568
  PERCENTAGE: 0.1
561
569
  };
562
- var getModelOutputLimit = (inputLength) => _chunk3CGLDS5Tcjs.clamp_default.call(void 0,
570
+ var getModelOutputLimit = (inputLength) => _chunkUQOBUJIQcjs.clamp_default.call(void 0,
563
571
  RESPONSE_LENGTH_BUFFER.PERCENTAGE * inputLength,
564
572
  RESPONSE_LENGTH_BUFFER.MIN_TOKENS,
565
573
  RESPONSE_LENGTH_BUFFER.MAX_TOKENS
566
574
  );
567
575
  var executeContext = async (props) => {
568
- var _a, _b, _c;
569
- await _chunkENFB45AIcjs.init.call(void 0, );
570
- const { signal, onIterationEnd, onTrace, onExit } = props;
576
+ var _a, _b, _c, _d;
577
+ await _chunkP7J2WCBBcjs.init.call(void 0, );
578
+ const { signal, onIterationEnd, onTrace, onExit, onBeforeExecution } = props;
571
579
  const cognitive = props.client instanceof _cognitive.Cognitive ? props.client : new (0, _cognitive.Cognitive)({ client: props.client });
572
580
  const cleanups = [];
573
581
  const ctx = new Context({
@@ -577,6 +585,7 @@ var executeContext = async (props) => {
577
585
  loop: (_a = props.options) == null ? void 0 : _a.loop,
578
586
  temperature: (_b = props.options) == null ? void 0 : _b.temperature,
579
587
  model: (_c = props.options) == null ? void 0 : _c.model,
588
+ timeout: (_d = props.options) == null ? void 0 : _d.timeout,
580
589
  transcript: props.transcript,
581
590
  exits: props.exits,
582
591
  components: props.components,
@@ -593,6 +602,20 @@ var executeContext = async (props) => {
593
602
  };
594
603
  }
595
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
+ }
596
619
  cleanups.push(
597
620
  iteration.traces.onPush((traces) => {
598
621
  for (const trace of traces) {
@@ -606,14 +629,15 @@ var executeContext = async (props) => {
606
629
  ctx,
607
630
  cognitive,
608
631
  abortSignal: signal,
609
- onExit
632
+ onExit,
633
+ onBeforeExecution
610
634
  });
611
635
  } catch (err) {
612
636
  iteration.end({
613
637
  type: "execution_error",
614
638
  execution_error: {
615
- stack: _chunkPQN6HW7Acjs.cleanStackTrace.call(void 0, _nullishCoalesce(err.stack, () => ( "No stack trace available"))),
616
- message: "An unexpected error occurred: " + getErrorMessage(err)
639
+ message: "An unexpected error occurred: " + getErrorMessage(err),
640
+ stack: _chunkIKSIOIIPcjs.cleanStackTrace.call(void 0, _nullishCoalesce(err.stack, () => ( "No stack trace available")))
617
641
  }
618
642
  });
619
643
  }
@@ -635,7 +659,7 @@ var executeContext = async (props) => {
635
659
  context: ctx,
636
660
  iterations: ctx.iterations,
637
661
  signal: iteration.status.callback_requested.signal,
638
- snapshot: _chunkC2TSWGYCcjs.Snapshot.fromSignal(iteration.status.callback_requested.signal)
662
+ snapshot: _chunk6YWYCVABcjs.Snapshot.fromSignal(iteration.status.callback_requested.signal)
639
663
  };
640
664
  }
641
665
  if (iteration.status.type === "thinking_requested" || iteration.status.type === "exit_error" || iteration.status.type === "execution_error" || iteration.status.type === "invalid_code_error") {
@@ -669,14 +693,15 @@ var executeIteration = async ({
669
693
  ctx,
670
694
  cognitive,
671
695
  abortSignal,
672
- onExit
696
+ onExit,
697
+ onBeforeExecution
673
698
  }) => {
674
699
  var _a, _b, _c, _d, _e;
675
700
  let startedAt = Date.now();
676
701
  const traces = iteration.traces;
677
702
  const modelLimit = 128e3;
678
703
  const responseLengthBuffer = getModelOutputLimit(modelLimit);
679
- const messages = _chunkVP6WWAE2cjs.truncateWrappedContent.call(void 0, {
704
+ const messages = _chunkFIVFS4HGcjs.truncateWrappedContent.call(void 0, {
680
705
  messages: iteration.messages,
681
706
  tokenLimit: modelLimit - responseLengthBuffer,
682
707
  throwOnFailure: false
@@ -687,6 +712,12 @@ var executeIteration = async ({
687
712
  x.content.trim().length > 0
688
713
  )
689
714
  );
715
+ traces.push({
716
+ type: "llm_call_started",
717
+ started_at: startedAt,
718
+ ended_at: startedAt,
719
+ model: _nullishCoalesce(ctx.model, () => ( ""))
720
+ });
690
721
  const output = await cognitive.generateContent({
691
722
  signal: abortSignal,
692
723
  systemPrompt: (_a = messages.find((x) => x.role === "system")) == null ? void 0 : _a.content,
@@ -708,6 +739,28 @@ var executeIteration = async ({
708
739
  }
709
740
  const assistantResponse = ctx.version.parseAssistantResponse(out);
710
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
+ }
711
764
  iteration.llm = {
712
765
  cached: output.meta.cached || false,
713
766
  ended_at: Date.now(),
@@ -725,7 +778,7 @@ var executeIteration = async ({
725
778
  model: _nullishCoalesce(ctx.model, () => ( "")),
726
779
  code: iteration.code
727
780
  });
728
- const vmContext = { ..._chunkENFB45AIcjs.stripInvalidIdentifiers.call(void 0, iteration.variables) };
781
+ const vmContext = { ..._chunkP7J2WCBBcjs.stripInvalidIdentifiers.call(void 0, iteration.variables) };
729
782
  for (const obj of iteration.objects) {
730
783
  const internalValues = {};
731
784
  const instance = {};
@@ -740,18 +793,18 @@ var executeIteration = async ({
740
793
  return internalValues[name];
741
794
  },
742
795
  set(value2) {
743
- if (_chunk3CGLDS5Tcjs.isEqual_default.call(void 0, value2, internalValues[name])) {
796
+ if (_chunkUQOBUJIQcjs.isEqual_default.call(void 0, value2, internalValues[name])) {
744
797
  return;
745
798
  }
746
799
  if (!writable) {
747
- throw new (0, _chunkZUAFUOURcjs.AssignmentError)(`Property ${obj.name}.${name} is read-only and cannot be modified`);
800
+ throw new (0, _chunkJDABP4SDcjs.AssignmentError)(`Property ${obj.name}.${name} is read-only and cannot be modified`);
748
801
  }
749
802
  if (value2 === internalValues[name]) {
750
803
  return;
751
804
  }
752
805
  const parsed = schema.safeParse(value2);
753
806
  if (!parsed.success) {
754
- throw new (0, _chunkZUAFUOURcjs.AssignmentError)(
807
+ throw new (0, _chunkJDABP4SDcjs.AssignmentError)(
755
808
  `Invalid value for Object property ${obj.name}.${name}: ${getErrorMessage(parsed.error)}`
756
809
  );
757
810
  }
@@ -794,7 +847,7 @@ var executeIteration = async ({
794
847
  });
795
848
  }
796
849
  startedAt = Date.now();
797
- const result = await _chunkBKGPO722cjs.runAsyncFunction.call(void 0, vmContext, assistantResponse.code.trim(), traces, abortSignal).catch(
850
+ const result = await _chunk5NVNEEYWcjs.runAsyncFunction.call(void 0, vmContext, iteration.code, traces, abortSignal, ctx.timeout).catch(
798
851
  (err) => {
799
852
  return {
800
853
  success: false,
@@ -805,7 +858,7 @@ var executeIteration = async ({
805
858
  };
806
859
  }
807
860
  );
808
- if (result.error && result.error instanceof _chunkZUAFUOURcjs.InvalidCodeError) {
861
+ if (result.error && result.error instanceof _chunkJDABP4SDcjs.InvalidCodeError) {
809
862
  return iteration.end({
810
863
  type: "invalid_code_error",
811
864
  invalid_code_error: {
@@ -819,12 +872,12 @@ var executeIteration = async ({
819
872
  started_at: startedAt,
820
873
  ended_at: Date.now()
821
874
  });
822
- if (result.error && result.error instanceof _chunkZUAFUOURcjs.CodeExecutionError) {
875
+ if (result.error && result.error instanceof _chunkJDABP4SDcjs.CodeExecutionError) {
823
876
  return iteration.end({
824
877
  type: "execution_error",
825
878
  execution_error: {
826
879
  message: result.error.message,
827
- stack: _chunkPQN6HW7Acjs.cleanStackTrace.call(void 0, _nullishCoalesce(result.error.stack, () => ( "No stack trace available")))
880
+ stack: _chunkIKSIOIIPcjs.cleanStackTrace.call(void 0, _nullishCoalesce(_nullishCoalesce(result.error.stacktrace, () => ( result.error.stack)), () => ( "No stack trace available")))
828
881
  }
829
882
  });
830
883
  }
@@ -841,20 +894,20 @@ var executeIteration = async ({
841
894
  type: "execution_error",
842
895
  execution_error: {
843
896
  message: _nullishCoalesce(((_e = result == null ? void 0 : result.error) == null ? void 0 : _e.message), () => ( "Unknown error occurred")),
844
- stack: _chunkPQN6HW7Acjs.cleanStackTrace.call(void 0, _nullishCoalesce(result.error.stack, () => ( "No stack trace available")))
897
+ stack: _chunkIKSIOIIPcjs.cleanStackTrace.call(void 0, _nullishCoalesce(result.error.stack, () => ( "No stack trace available")))
845
898
  }
846
899
  });
847
900
  }
848
- if (result.signal instanceof _chunkZUAFUOURcjs.ThinkSignal) {
901
+ if (result.signal instanceof _chunkJDABP4SDcjs.ThinkSignal) {
849
902
  return iteration.end({
850
903
  type: "thinking_requested",
851
904
  thinking_requested: {
852
- variables: result.signal.variables,
905
+ variables: result.signal.context,
853
906
  reason: result.signal.reason
854
907
  }
855
908
  });
856
909
  }
857
- if (result.signal instanceof _chunkZUAFUOURcjs.SnapshotSignal) {
910
+ if (result.signal instanceof _chunkJDABP4SDcjs.SnapshotSignal) {
858
911
  return iteration.end({
859
912
  type: "callback_requested",
860
913
  callback_requested: {
@@ -867,8 +920,8 @@ var executeIteration = async ({
867
920
  const returnAction = returnValue == null ? void 0 : returnValue.action;
868
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())))));
869
922
  if (returnAction === "think") {
870
- const variables = _chunk3CGLDS5Tcjs.omit_default.call(void 0, _nullishCoalesce(returnValue, () => ( {})), "action");
871
- if (_chunk3CGLDS5Tcjs.isPlainObject_default.call(void 0, variables) && Object.keys(variables).length > 0) {
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) {
872
925
  return iteration.end({
873
926
  type: "thinking_requested",
874
927
  thinking_requested: {
@@ -942,10 +995,12 @@ var executeIteration = async ({
942
995
  function wrapTool({ tool, traces, object }) {
943
996
  const getToolInput = (input) => _nullishCoalesce(tool.zInput.safeParse(input).data, () => ( input));
944
997
  return function(input) {
998
+ const toolCallId = `tcall_${_ulid.ulid.call(void 0, )}`;
945
999
  const alertSlowTool = setTimeout(
946
1000
  () => traces.push({
947
1001
  type: "tool_slow",
948
1002
  tool_name: tool.name,
1003
+ tool_call_id: toolCallId,
949
1004
  started_at: Date.now(),
950
1005
  input: getToolInput(input),
951
1006
  object,
@@ -962,16 +1017,16 @@ function wrapTool({ tool, traces, object }) {
962
1017
  if (output === error2) {
963
1018
  return true;
964
1019
  }
965
- if (error2 instanceof _chunkZUAFUOURcjs.SnapshotSignal) {
1020
+ if (error2 instanceof _chunkJDABP4SDcjs.SnapshotSignal) {
966
1021
  error2.toolCall = {
967
1022
  name: tool.name,
968
1023
  inputSchema: tool.input,
969
1024
  outputSchema: tool.output,
970
1025
  input
971
1026
  };
972
- error2.message = _chunkZUAFUOURcjs.Signals.serializeError(error2);
1027
+ error2.message = _chunkJDABP4SDcjs.Signals.serializeError(error2);
973
1028
  }
974
- if (error2 instanceof _chunkZUAFUOURcjs.ThinkSignal) {
1029
+ if (error2 instanceof _chunkJDABP4SDcjs.ThinkSignal) {
975
1030
  traces.push({
976
1031
  type: "think_signal",
977
1032
  started_at: Date.now(),
@@ -985,7 +1040,9 @@ function wrapTool({ tool, traces, object }) {
985
1040
  return false;
986
1041
  };
987
1042
  try {
988
- const result = tool.execute(input);
1043
+ const result = tool.execute(input, {
1044
+ callId: toolCallId
1045
+ });
989
1046
  if (result instanceof Promise || (result == null ? void 0 : result.then) && (result == null ? void 0 : result.catch)) {
990
1047
  return result.then((res) => {
991
1048
  output = res;
@@ -1001,6 +1058,7 @@ function wrapTool({ tool, traces, object }) {
1001
1058
  cancelSlowTool();
1002
1059
  traces.push({
1003
1060
  type: "tool_call",
1061
+ tool_call_id: toolCallId,
1004
1062
  started_at: toolStart,
1005
1063
  ended_at: Date.now(),
1006
1064
  tool_name: tool.name,
@@ -1023,6 +1081,7 @@ function wrapTool({ tool, traces, object }) {
1023
1081
  cancelSlowTool();
1024
1082
  traces.push({
1025
1083
  type: "tool_call",
1084
+ tool_call_id: toolCallId,
1026
1085
  started_at: toolStart,
1027
1086
  ended_at: Date.now(),
1028
1087
  tool_name: tool.name,
@@ -1035,7 +1094,7 @@ function wrapTool({ tool, traces, object }) {
1035
1094
  if (!success) {
1036
1095
  throw error;
1037
1096
  }
1038
- if (output instanceof _chunkZUAFUOURcjs.VMSignal) {
1097
+ if (output instanceof _chunkJDABP4SDcjs.VMSignal) {
1039
1098
  throw output;
1040
1099
  }
1041
1100
  return output;
package/dist/llmz.d.ts CHANGED
@@ -19,9 +19,10 @@ export type ExecutionHooks = {
19
19
  trace: Trace;
20
20
  iteration: number;
21
21
  }) => void;
22
- onExit?: (exit: Exit, value: unknown) => Promise<void> | void;
22
+ onExit?: <T = unknown>(exit: Exit<T>, value: T) => Promise<void> | void;
23
+ onBeforeExecution?: (iteration: Iteration) => Promise<void> | void;
23
24
  };
24
- type Options = Partial<Pick<Context, 'loop' | 'temperature' | 'model'>>;
25
+ type Options = Partial<Pick<Context, 'loop' | 'temperature' | 'model' | 'timeout'>>;
25
26
  export type ExecutionProps = {
26
27
  instructions?: ValueOrGetter<string, Context>;
27
28
  components?: ValueOrGetter<Component[], Context>;
@@ -51,8 +51,16 @@ export declare class Snapshot {
51
51
  stack: string;
52
52
  variables: Variable[];
53
53
  toolCall: ToolCall | undefined;
54
+ status: SnapshotStatus;
54
55
  };
55
- static fromJSON(json: Snapshot): Snapshot;
56
+ static fromJSON(json: {
57
+ id: string;
58
+ reason?: string;
59
+ stack: string;
60
+ variables: Variable[];
61
+ toolCall?: ToolCall;
62
+ status: SnapshotStatus;
63
+ }): Snapshot;
56
64
  clone(): Snapshot;
57
65
  reset(): void;
58
66
  resolve(value: any): void;
@@ -0,0 +1,11 @@
1
+ import {
2
+ Tool
3
+ } from "./chunk-QBXIE6EY.js";
4
+ import "./chunk-UNMKB2DA.js";
5
+ import "./chunk-JKVVQN2P.js";
6
+ import "./chunk-JQBT7UWN.js";
7
+ import "./chunk-5TRUJES5.js";
8
+ import "./chunk-7WRN4E42.js";
9
+ export {
10
+ Tool
11
+ };
@@ -0,0 +1,11 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+ var _chunk4QTK5ALHcjs = require('./chunk-4QTK5ALH.cjs');
4
+ require('./chunk-HYVKY3W6.cjs');
5
+ require('./chunk-JDABP4SD.cjs');
6
+ require('./chunk-IKSIOIIP.cjs');
7
+ require('./chunk-P7J2WCBB.cjs');
8
+ require('./chunk-UQOBUJIQ.cjs');
9
+
10
+
11
+ exports.Tool = _chunk4QTK5ALHcjs.Tool;
package/dist/tool.d.ts CHANGED
@@ -1,6 +1,17 @@
1
- import { type JSONSchema } from '@bpinternal/zui';
1
+ import { type JSONSchema, TypeOf } from '@bpinternal/zui';
2
2
  import { ZuiType } from './types.js';
3
- export declare class Tool<I = unknown, O = unknown> {
3
+ type ToolRetryInput<I> = {
4
+ input: I;
5
+ attempt: number;
6
+ error?: unknown;
7
+ };
8
+ export type ToolRetryFn<I> = (args: ToolRetryInput<I>) => boolean | Promise<boolean>;
9
+ type IsObject<T> = T extends object ? (T extends Function ? false : true) : false;
10
+ type SmartPartial<T> = IsObject<T> extends true ? Partial<T> : T;
11
+ type ToolCallContext = {
12
+ callId: string;
13
+ };
14
+ export declare class Tool<I extends ZuiType = ZuiType, O extends ZuiType = ZuiType> {
4
15
  private _staticInputValues?;
5
16
  name: string;
6
17
  aliases: string[];
@@ -8,23 +19,37 @@ export declare class Tool<I = unknown, O = unknown> {
8
19
  metadata: Record<string, unknown>;
9
20
  input?: JSONSchema;
10
21
  output?: JSONSchema;
11
- setStaticInputValues(values: Partial<I>): this;
22
+ retry?: ToolRetryFn<TypeOf<I>>;
23
+ MAX_RETRIES: number;
24
+ setStaticInputValues(values: SmartPartial<TypeOf<I>>): this;
12
25
  get zInput(): import("@bpinternal/zui").ZodTypeAny;
13
26
  get zOutput(): import("@bpinternal/zui").ZodTypeAny | import("@bpinternal/zui").ZodVoid;
14
27
  rename(name: string): this;
15
- clone(): Tool<I, O>;
28
+ clone<IX extends ZuiType = I, OX extends ZuiType = O>(props?: Partial<{
29
+ name: string;
30
+ aliases?: string[];
31
+ description?: string;
32
+ metadata?: Record<string, unknown>;
33
+ input: IX | ((original: I | undefined) => IX);
34
+ output: OX | ((original: O | undefined) => OX);
35
+ staticInputValues?: SmartPartial<TypeOf<IX>>;
36
+ handler: (args: TypeOf<IX>, ctx: ToolCallContext) => Promise<TypeOf<OX>>;
37
+ retry: ToolRetryFn<TypeOf<IX>>;
38
+ }>): Tool<IX, OX>;
16
39
  private _handler;
17
40
  constructor(props: {
18
41
  name: string;
19
42
  aliases?: string[];
20
43
  description?: string;
21
44
  metadata?: Record<string, unknown>;
22
- input?: ZuiType<I>;
23
- output?: ZuiType<O>;
24
- staticInputValues?: Partial<I>;
25
- handler: (args: I) => Promise<O>;
45
+ input?: I;
46
+ output?: O;
47
+ staticInputValues?: Partial<TypeOf<I>>;
48
+ handler: (args: TypeOf<I>, ctx: ToolCallContext) => Promise<TypeOf<O>>;
49
+ retry?: ToolRetryFn<TypeOf<I>>;
26
50
  });
27
- execute(input: I): Promise<O>;
51
+ execute(input: TypeOf<I>, ctx: ToolCallContext): Promise<TypeOf<O>>;
28
52
  getTypings(): Promise<string>;
29
53
  static withUniqueNames: (tools: Tool<any, any>[]) => Tool<any, any>[];
30
54
  }
55
+ export {};