llmz 0.9.0 → 0.9.2

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 (28) hide show
  1. package/dist/{common-BnvrDnIT.cjs → common-DJkSpeqw.cjs} +1 -1
  2. package/dist/{common-BlRFtdf2.js → common-DuEhn6u3.js} +1 -1
  3. package/dist/compiler/plugins/variable-extraction.d.ts +1 -7
  4. package/dist/{compiler-C-OI9sim.cjs → compiler-BJJUmNS9.cjs} +34 -68
  5. package/dist/{compiler-CW1VjQsn.js → compiler-BPl-5CXh.js} +34 -68
  6. package/dist/{component-C6Agl2ns.cjs → component-CPKXe6Ca.cjs} +2 -2
  7. package/dist/{custom-client-DAsGRnYc.cjs → custom-client-B_o56CB4.cjs} +6 -6
  8. package/dist/{custom-client-ChuZ63zp.js → custom-client-C-nDKG37.js} +3 -3
  9. package/dist/{dual-modes-DPXiuvNe.cjs → dual-modes-BNHlj2qM.cjs} +15 -14
  10. package/dist/{dual-modes-DDkoCp3R.js → dual-modes-BNIiNLrR.js} +13 -12
  11. package/dist/{execute-DM2-7y86.cjs → execute-0bvqakr7.cjs} +31 -18
  12. package/dist/{execute-BWcdv9so.js → execute-CWFIL5ny.js} +30 -17
  13. package/dist/{exit-DUM96DN1.cjs → exit-CGLXI0BH.cjs} +2 -2
  14. package/dist/index.cjs +21 -21
  15. package/dist/index.js +12 -12
  16. package/dist/index.workerd.js +217 -195
  17. package/dist/{jsx-owqxrquU.cjs → jsx-BJ6GWXI2.cjs} +1 -1
  18. package/dist/runtime/generate.test.d.ts +1 -0
  19. package/dist/{tool-Bv8N4IPj.js → tool-B6Pmuk6K.js} +35 -49
  20. package/dist/{tool-Dy6AJpJH.cjs → tool-BLTrA-sL.cjs} +38 -52
  21. package/dist/tool.d.ts +5 -3
  22. package/dist/{truncator-DZqEqnpD.js → truncator-CD7QtE84.js} +11 -6
  23. package/dist/{truncator-CLkouTCO.cjs → truncator-DmCu9M4m.cjs} +13 -8
  24. package/dist/{typings-3lsqCbkn.cjs → typings-DXJMu59O.cjs} +2 -2
  25. package/dist/{utils-3ESwP3yg.cjs → utils-DGQ4sRke.cjs} +1 -1
  26. package/dist/{vm-BLsHw1da.js → vm-Cn3jvZ00.js} +109 -53
  27. package/dist/{vm-D6s1zF6M.cjs → vm-X0uTj5Aa.cjs} +109 -53
  28. package/package.json +2 -2
@@ -52,7 +52,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
52
52
  }) : target, mod));
53
53
  //#endregion
54
54
  //#region package.json
55
- var version = "0.9.0", quoteExample, quotePartialExample, exampleBoundaryInstructions, quoteResponseExample;
55
+ var version = "0.9.2", quoteExample, quotePartialExample, exampleBoundaryInstructions, quoteResponseExample;
56
56
  var init_example_format = __esmMin((() => {
57
57
  quoteExample = (output) => `"""\n${output}\n"""`;
58
58
  quotePartialExample = (output) => quoteExample(`(...)\n${output}\n(...)`);
@@ -1047,82 +1047,44 @@ var init_track_tool_calls = __esmMin((() => {
1047
1047
  }));
1048
1048
  //#endregion
1049
1049
  //#region src/compiler/plugins/variable-extraction.ts
1050
- /**
1051
- * Tracks user variables so the VM can read their values between iterations:
1052
- * - after each variable declaration: `__var__("a", () => eval("a"));`
1053
- * - at the top of function bodies, for their parameters
1054
- *
1055
- * The `eval` indirection captures the variable lazily in its own scope.
1056
- */
1050
+ function declaredNames(id) {
1051
+ if (!id) return [];
1052
+ if (id.type === "Identifier") return [id.name];
1053
+ if (id.type === "RestElement") return declaredNames(id.argument);
1054
+ if (id.type === "AssignmentPattern") return declaredNames(id.left);
1055
+ if (id.type === "ArrayPattern") return id.elements.flatMap(declaredNames);
1056
+ if (id.type === "ObjectPattern") return id.properties.flatMap((prop) => declaredNames(prop.type === "RestElement" ? prop.argument : prop.value));
1057
+ return [];
1058
+ }
1059
+ /** Retain getters only for bindings visible between iterations, never local shadows. */
1057
1060
  function applyVariableTracking(ctx, variables) {
1058
- const trackerFor = (name) => {
1059
- if (name.startsWith("__")) return null;
1060
- variables.add(name);
1061
- return `${VariableTrackingFnIdentifier}(${JSON.stringify(name)}, () => eval(${JSON.stringify(name)}));`;
1062
- };
1063
- const track = (names) => names.map(trackerFor).filter((t) => !!t).join("");
1064
- const declaredNames = (id) => {
1065
- if (id.type === "Identifier") return [id.name];
1066
- if (id.type === "ObjectPattern") {
1067
- const names = [];
1068
- for (const prop of id.properties) {
1069
- if (prop.type !== "Property") continue;
1070
- if (prop.value.type === "Identifier") names.push(prop.value.name);
1071
- else if (prop.value.type === "ObjectPattern") names.push(...declaredNames(prop.value));
1072
- else if (prop.value.type === "AssignmentPattern" && prop.value.left.type === "Identifier") names.push(prop.value.left.name);
1073
- }
1074
- return names;
1075
- }
1076
- if (id.type === "ArrayPattern") {
1077
- const names = [];
1078
- for (const element of id.elements) {
1079
- if (!element) continue;
1080
- if (element.type === "Identifier") names.push(element.name);
1081
- else if (element.type === "RestElement" && element.argument.type === "Identifier") names.push(element.argument.name);
1082
- else if (element.type === "AssignmentPattern" && element.left.type === "Identifier") names.push(element.left.name);
1083
- }
1084
- return names;
1085
- }
1086
- return [];
1087
- };
1088
- const paramNames = (params) => {
1089
- const names = [];
1090
- for (const param of params) if (param.type === "Identifier") names.push(param.name);
1091
- else if (param.type === "AssignmentPattern" && param.left.type === "Identifier") names.push(param.left.name);
1092
- else if (param.type === "ObjectPattern") {
1093
- for (const prop of param.properties) if (prop.type === "Property" && prop.value.type === "Identifier") names.push(prop.value.name);
1094
- } else if (param.type === "ArrayPattern") {
1095
- for (const element of param.elements) if (element?.type === "Identifier") names.push(element.name);
1096
- }
1097
- return names;
1098
- };
1099
- walk$1(ctx.ast, (node, parent) => {
1100
- if (node.type === "VariableDeclaration") {
1101
- if (parent && (parent.type === "ForStatement" || parent.type === "ForInStatement" || parent.type === "ForOfStatement")) return;
1102
- const trackers = track(node.declarations.flatMap((d) => declaredNames(d.id)));
1103
- if (trackers) {
1104
- const semi = ctx.code[node.end - 1] === ";" ? "" : ";";
1105
- ctx.ms.appendRight(node.end, `${semi}${trackers}`);
1106
- }
1107
- return;
1108
- }
1109
- if (node.type === "FunctionDeclaration" || node.type === "ArrowFunctionExpression") {
1110
- const trackers = track(paramNames(node.params));
1111
- if (!trackers) return;
1112
- const body = node.body;
1113
- if (body.type === "BlockStatement") ctx.ms.appendRight(body.start + 1, trackers);
1114
- else {
1115
- const expressions = trackers.slice(0, -1).replaceAll(";", ",");
1116
- ctx.ms.prependLeft(body.start, `(${expressions}, (`);
1117
- ctx.ms.appendRight(body.end, "))");
1118
- }
1061
+ const root = ctx.ast.body.find((node) => node.type === "FunctionDeclaration" && node.id?.name === "__fn__")?.body ?? ctx.ast;
1062
+ const varBindings = /* @__PURE__ */ new Set();
1063
+ const track = (names) => names.map((name) => `${VariableTrackingFnIdentifier}(${JSON.stringify(name)}, () => eval(${JSON.stringify(name)}));`).join("");
1064
+ walk$1(root, (node, parent, ancestors) => {
1065
+ if (node.type !== "VariableDeclaration" || ancestors.some((ancestor) => FUNCTION_TYPES$1.has(ancestor.type) || ancestor.type === "StaticBlock")) return;
1066
+ if (node.kind !== "var" && parent !== root) return;
1067
+ const names = node.declarations.flatMap((declaration) => declaredNames(declaration.id)).filter((name) => !name.startsWith("__"));
1068
+ for (const name of names) variables.add(name);
1069
+ if (node.kind === "var" && parent !== root) for (const name of names) varBindings.add(name);
1070
+ else if (node.declarations.length > 1) ctx.ms.appendLeft(node.start, track(names));
1071
+ else {
1072
+ const semi = ctx.code[node.end - 1] === ";" ? "" : ";";
1073
+ ctx.ms.appendRight(node.end, semi + track(names));
1119
1074
  }
1120
1075
  });
1076
+ const entry = root.body.find((node) => !node.directive);
1077
+ if (entry && varBindings.size) ctx.ms.appendLeft(entry.start, track([...varBindings]));
1121
1078
  }
1122
- var VariableTrackingFnIdentifier;
1079
+ var VariableTrackingFnIdentifier, FUNCTION_TYPES$1;
1123
1080
  var init_variable_extraction = __esmMin((() => {
1124
1081
  init_ast();
1125
1082
  VariableTrackingFnIdentifier = "__var__";
1083
+ FUNCTION_TYPES$1 = /* @__PURE__ */ new Set([
1084
+ "FunctionDeclaration",
1085
+ "FunctionExpression",
1086
+ "ArrowFunctionExpression"
1087
+ ]);
1126
1088
  }));
1127
1089
  //#endregion
1128
1090
  //#region src/compiler/compiler.ts
@@ -9402,6 +9364,8 @@ var init_tool = __esmMin((() => {
9402
9364
  init_utils$1();
9403
9365
  Tool = class Tool {
9404
9366
  _staticInputValues;
9367
+ _inputSchema;
9368
+ _outputSchema;
9405
9369
  name;
9406
9370
  aliases = [];
9407
9371
  description;
@@ -9472,23 +9436,14 @@ var init_tool = __esmMin((() => {
9472
9436
  * @internal
9473
9437
  */
9474
9438
  get zInput() {
9475
- let input;
9476
- if (this.input) try {
9477
- input = transforms.fromJSONSchema(this.input);
9478
- } catch {
9479
- input = transforms.fromJSONSchemaLegacy(this.input);
9480
- }
9481
- else input = z.any();
9482
- if (!isEmpty(this._staticInputValues)) {
9483
- if (z.is.zuiObject(input)) {
9484
- const inputExtensions = convertObjectToZuiLiterals(this._staticInputValues);
9485
- input = input.extend(inputExtensions);
9486
- } else if (z.is.zuiArray(input)) {
9487
- const inputExtensions = convertObjectToZuiLiterals(this._staticInputValues);
9488
- input = z.array(input.element.extend(inputExtensions));
9489
- } else input = convertObjectToZuiLiterals(this._staticInputValues);
9490
- }
9491
- return input;
9439
+ const input = this._inputSchema ?? (this.input ? fromJSONSchemaCompat(this.input) : z.any());
9440
+ if (isEmpty(this._staticInputValues)) return input;
9441
+ const shape = this.input ? fromJSONSchemaCompat(this.input) : z.any();
9442
+ let overrides;
9443
+ if (z.is.zuiObject(shape)) overrides = z.object(convertObjectToZuiLiterals(this._staticInputValues)).passthrough();
9444
+ else if (z.is.zuiArray(shape)) overrides = z.array(z.object(convertObjectToZuiLiterals(this._staticInputValues)).passthrough());
9445
+ else overrides = convertObjectToZuiLiterals(this._staticInputValues);
9446
+ return overrides.pipe(input);
9492
9447
  }
9493
9448
  /**
9494
9449
  * Gets the output schema for validation.
@@ -9584,18 +9539,8 @@ var init_tool = __esmMin((() => {
9584
9539
  */
9585
9540
  clone(props = {}) {
9586
9541
  try {
9587
- let zInput;
9588
- if (this.input) try {
9589
- zInput = transforms.fromJSONSchema(this.input);
9590
- } catch {
9591
- zInput = transforms.fromJSONSchemaLegacy(this.input);
9592
- }
9593
- let zOutput;
9594
- if (this.output) try {
9595
- zOutput = transforms.fromJSONSchema(this.output);
9596
- } catch {
9597
- zOutput = transforms.fromJSONSchemaLegacy(this.output);
9598
- }
9542
+ const zInput = this._inputSchema ?? (this.input ? fromJSONSchemaCompat(this.input) : void 0);
9543
+ const zOutput = this._outputSchema ?? (this.output ? fromJSONSchemaCompat(this.output) : void 0);
9599
9544
  return new Tool({
9600
9545
  name: props.name ?? this.name,
9601
9546
  aliases: props.aliases ?? [...this.aliases],
@@ -9618,7 +9563,7 @@ var init_tool = __esmMin((() => {
9618
9563
  * @param props.name - Unique tool name (must be valid TypeScript identifier)
9619
9564
  * @param props.description - Human-readable description for the LLM
9620
9565
  * @param props.input - Zui/Zod schema for input validation (optional)
9621
- * @param props.output - Zui/Zod schema for output validation (optional)
9566
+ * @param props.output - Zui/Zod schema describing output (optional)
9622
9567
  * @param props.handler - Async function that implements the tool logic
9623
9568
  * @param props.aliases - Alternative names for the tool (optional)
9624
9569
  * @param props.metadata - Additional metadata for the tool (optional)
@@ -9695,21 +9640,25 @@ var init_tool = __esmMin((() => {
9695
9640
  if (props.aliases !== void 0 && !Array.isArray(props.aliases)) throw new Error(`Invalid aliases for tool ${props.name}. Expected an array, but got type "${typeof props.aliases}"`);
9696
9641
  if (props.aliases && props.aliases.some((alias) => !isValidIdentifier(alias))) throw new Error(`Invalid aliases for tool ${props.name}. Expected an array of valid identifiers.`);
9697
9642
  if (typeof props.input !== "undefined") {
9698
- if (isZuiSchema(props.input)) try {
9699
- this.input = transforms.toJSONSchema(props.input);
9700
- } catch {
9701
- this.input = transforms.toJSONSchemaLegacy(props.input);
9702
- }
9703
- else if (isJsonSchema(props.input)) this.input = props.input;
9643
+ if (isZuiSchema(props.input)) {
9644
+ this._inputSchema = props.input;
9645
+ try {
9646
+ this.input = transforms.toJSONSchema(props.input);
9647
+ } catch {
9648
+ this.input = transforms.toJSONSchemaLegacy(props.input);
9649
+ }
9650
+ } else if (isJsonSchema(props.input)) this.input = props.input;
9704
9651
  else throw new Error(`Invalid input schema for tool ${props.name}. Expected a ZodType or JSONSchema, but got type "${typeof props.input}"`);
9705
9652
  }
9706
9653
  if (typeof props.output !== "undefined") {
9707
- if (isZuiSchema(props.output)) try {
9708
- this.output = transforms.toJSONSchema(props.output);
9709
- } catch {
9710
- this.output = transforms.toJSONSchemaLegacy(props.output);
9711
- }
9712
- else if (isJsonSchema(props.output)) this.output = props.output;
9654
+ if (isZuiSchema(props.output)) {
9655
+ this._outputSchema = props.output;
9656
+ try {
9657
+ this.output = transforms.toJSONSchema(props.output);
9658
+ } catch {
9659
+ this.output = transforms.toJSONSchemaLegacy(props.output);
9660
+ }
9661
+ } else if (isJsonSchema(props.output)) this.output = props.output;
9713
9662
  else throw new Error(`Invalid output schema for tool ${props.name}. Expected a ZodType or JSONSchema, but got type "${typeof props.output}"`);
9714
9663
  }
9715
9664
  this.name = props.name;
@@ -9740,7 +9689,7 @@ var init_tool = __esmMin((() => {
9740
9689
  /**
9741
9690
  * Executes the tool with the given input and context.
9742
9691
  *
9743
- * This method handles input validation, retry logic, output validation, and error handling.
9692
+ * This method handles input validation, retry logic, and error handling.
9744
9693
  * It's called internally by the LLMz execution engine when generated code calls the tool.
9745
9694
  *
9746
9695
  * @param input - Input data to pass to the tool handler
@@ -9762,17 +9711,16 @@ var init_tool = __esmMin((() => {
9762
9711
  * @internal This method is primarily used internally by the LLMz execution engine
9763
9712
  */
9764
9713
  async execute(rawInput, ctx, chat) {
9765
- const input = this.zInput._def.typeName === "ZodObject" ? rawInput ?? {} : rawInput;
9766
- const pInput = this.zInput.safeParse(input);
9714
+ const shape = this.input ? fromJSONSchemaCompat(this.input) : z.any();
9715
+ const input = z.is.zuiObject(shape) ? rawInput ?? {} : rawInput;
9716
+ const pInput = await this.zInput.safeParseAsync(input);
9767
9717
  if (!pInput.success) throw new Error(`Tool "${this.name}" received invalid input: ${pInput.error.message}`);
9768
9718
  let attempt = 0;
9769
9719
  let yieldedCount = 0;
9770
9720
  while (attempt < this.MAX_RETRIES) try {
9771
- const result = await this._executeHandler(pInput.data, ctx, chat, yieldedCount, (n) => {
9721
+ return await this._executeHandler(pInput.data, ctx, chat, yieldedCount, (n) => {
9772
9722
  yieldedCount = n;
9773
9723
  });
9774
- const pOutput = this.zOutput.safeParse(result);
9775
- return pOutput.success ? pOutput.data : result;
9776
9724
  } catch (err) {
9777
9725
  if (!await this.retry?.({
9778
9726
  input: pInput.data,
@@ -11528,7 +11476,7 @@ function printLimitedJson(obj, maxDepth, maxLength, maxKeys) {
11528
11476
  }
11529
11477
  }
11530
11478
  if (typeof currentObj !== "object" || currentObj === null) {
11531
- const value = JSON.stringify(currentObj);
11479
+ const value = stringify(currentObj);
11532
11480
  currentLength += getTokenizer().count(value);
11533
11481
  return value;
11534
11482
  }
@@ -11600,10 +11548,10 @@ function previewValue(value, length = LONG_TEXT_LENGTH) {
11600
11548
  };
11601
11549
  const previewObj = (obj) => {
11602
11550
  const mapped = mapKeys(obj, (_value, key) => previewStr(key));
11603
- return JSON.stringify(mapped);
11551
+ return stringify(mapped);
11604
11552
  };
11605
11553
  if (typeof value === "string") return "<string> " + previewStr(value);
11606
- if (typeof value === "object" && Array.isArray(value)) return "<array> " + previewStr(JSON.stringify(value));
11554
+ if (typeof value === "object" && Array.isArray(value)) return "<array> " + previewStr(stringify(value));
11607
11555
  if (typeof value === "object" && value instanceof Date) return "<date> " + value.toISOString();
11608
11556
  if (typeof value === "object" && value instanceof RegExp) return "<regexp> " + value.toString();
11609
11557
  if (typeof value === "object") return "<object> " + previewObj(value);
@@ -11643,15 +11591,15 @@ function previewArray(arr, maxStringLength = LONG_TEXT_LENGTH) {
11643
11591
  lines.push("// Analysis Summary");
11644
11592
  lines.push(SUBTITLE_LN);
11645
11593
  const typesCount = countBy(arr, (item) => extractType(item, false));
11646
- const ordered = orderBy(arr.filter((item) => !isNil(item) && JSON.stringify(item).length < 100), (item) => JSON.stringify(item), "asc");
11594
+ const ordered = orderBy(arr.filter((item) => !isNil(item) && stringify(item).length < 100), (item) => stringify(item), "asc");
11647
11595
  const minValues = ordered.slice(0, 3).map((item) => previewValue(item, 10));
11648
11596
  const maxValues = ordered.slice(-3).map((item) => previewValue(item, 10));
11649
11597
  const uniqueItems = uniqWith(arr, isEqual);
11650
11598
  const nullValues = filter(arr, isNil).length;
11651
- const memoryUsage = bytes(JSON.stringify(arr).length);
11599
+ const memoryUsage = bytes(stringify(arr).length);
11652
11600
  lines.push(`Total Items: ${arr.length}`);
11653
11601
  lines.push(`Unique Items: ${uniqueItems.length}`);
11654
- lines.push(`Types: ${JSON.stringify(typesCount)}`);
11602
+ lines.push(`Types: ${stringify(typesCount)}`);
11655
11603
  lines.push(`Minimum Values: [${minValues.join(", ")}]`);
11656
11604
  lines.push(`Maximum Values: [${maxValues.join(", ")}]`);
11657
11605
  lines.push(`Memory Usage: ${memoryUsage}`);
@@ -11674,7 +11622,7 @@ function previewObject(obj, options) {
11674
11622
  const keys = Object.keys(obj);
11675
11623
  const uniqueEntries = uniq(Object.values(obj));
11676
11624
  const nilValues = filter(entries, ([, value]) => isNil(value)).length;
11677
- const memoryUsage = bytes(JSON.stringify(obj).length);
11625
+ const memoryUsage = bytes(stringify(obj).length);
11678
11626
  lines.push(`Total Entries: ${NUMBER_LOCALE.format(entries.length)}`);
11679
11627
  lines.push(`Keys: ${previewValue(keys)}`);
11680
11628
  lines.push(`Popular Types: ${previewValue(typesCount)}`);
@@ -11724,7 +11672,7 @@ function previewLongText(text, length = LONG_TEXT_LENGTH) {
11724
11672
  lines.push(`Most Common Words: ${mostCommonWords}`);
11725
11673
  return lines.join("\n").replace(/\n{2,}/g, SECTION_SEP);
11726
11674
  }
11727
- var SUBTITLE_LN, SECTION_SEP, NUMBER_LOCALE, LONG_TEXT_LENGTH, DEFAULT_OPTIONS, inspect;
11675
+ var SUBTITLE_LN, SECTION_SEP, NUMBER_LOCALE, LONG_TEXT_LENGTH, DEFAULT_OPTIONS, stringify, inspect;
11728
11676
  var init_inspect = __esmMin((() => {
11729
11677
  init_lodash();
11730
11678
  init_utils$1();
@@ -11733,6 +11681,7 @@ var init_inspect = __esmMin((() => {
11733
11681
  NUMBER_LOCALE = new Intl.NumberFormat("en-US");
11734
11682
  LONG_TEXT_LENGTH = 4096;
11735
11683
  DEFAULT_OPTIONS = { tokens: 1e5 };
11684
+ stringify = (value) => JSON.stringify(value, (_key, item) => typeof item === "bigint" ? item.toString() + "n" : item);
11736
11685
  inspect = (value, name, options = DEFAULT_OPTIONS) => {
11737
11686
  options ??= DEFAULT_OPTIONS;
11738
11687
  options.tokens ??= DEFAULT_OPTIONS.tokens;
@@ -11741,7 +11690,7 @@ var init_inspect = __esmMin((() => {
11741
11690
  let header = "";
11742
11691
  if (name) header = `// const ${name}: ${genericType}\n`;
11743
11692
  if (genericType === "Array") return header + previewArray(value, options.maxStringLength);
11744
- else if (genericType === "error") return header + previewError(value);
11693
+ else if (genericType === "error") return getTokenizer().truncate(header + previewError(value), options.tokens);
11745
11694
  else if (genericType === "object") return header + previewObject(value, options);
11746
11695
  else if (genericType === "boolean") return header + previewValue(value);
11747
11696
  else if (typeof value === "string") {
@@ -11750,7 +11699,7 @@ var init_inspect = __esmMin((() => {
11750
11699
  }
11751
11700
  return header + previewValue(value);
11752
11701
  } catch (err) {
11753
- return `Error: ${err?.message ?? "Unknown Error"}`;
11702
+ return getTokenizer().truncate(`Error: ${err?.message ?? "Unknown Error"}`, options.tokens);
11754
11703
  }
11755
11704
  };
11756
11705
  }));
@@ -12394,9 +12343,9 @@ function truncateWrappedContent({ messages, tokenLimit, throwOnFailure = true })
12394
12343
  * After { content: 'content', tokens: 10, truncatable: false }
12395
12344
  */
12396
12345
  const parts = [];
12397
- for (const msg of messages) {
12346
+ const contents = messages.flatMap((message) => typeof message.content === "string" ? [message.content] : (message.content ?? []).flatMap((part) => part.type === "text" ? [part.text ?? ""] : []));
12347
+ for (const content of contents) {
12398
12348
  const current = [];
12399
- const content = typeof msg.content === "string" ? msg.content : "";
12400
12349
  let match;
12401
12350
  const parser = new _MessageContentParser();
12402
12351
  while ((match = parser.parse(content)) !== null) {
@@ -12454,13 +12403,15 @@ function truncateWrappedContent({ messages, tokenLimit, throwOnFailure = true })
12454
12403
  biggest.tokens -= toRemove;
12455
12404
  currentCount -= toRemove;
12456
12405
  }
12457
- return messages.map((msg, i) => {
12458
- const p = parts[i];
12459
- return {
12460
- ...msg,
12461
- content: typeof msg.content === "string" ? _renderRemainingWrappers(p.map((part) => part.content).join("")) : msg.content
12462
- };
12463
- });
12406
+ let textIndex = 0;
12407
+ const renderText = () => _renderRemainingWrappers(parts[textIndex++].map((part) => part.content).join(""));
12408
+ return messages.map((message) => ({
12409
+ ...message,
12410
+ content: typeof message.content === "string" ? renderText() : message.content?.map((part) => part.type === "text" ? {
12411
+ ...part,
12412
+ text: renderText()
12413
+ } : part) ?? message.content
12414
+ }));
12464
12415
  }
12465
12416
  var DEFAULT_REMOVE_CHUNK, WRAP_OPEN_TAG_1, WRAP_OPEN_TAG_2, WRAP_CLOSE_TAG, REGEXP, DEFAULT_TRUNCATE_OPTIONS, _MessageContentParser, _createRegex, _renderRemainingWrappers, stripTruncationTags, _countTotalTokens, _getTwoBiggestTruncables;
12466
12417
  var init_truncator = __esmMin((() => {
@@ -17691,7 +17642,7 @@ function bridgeGetterSetter(vm, key, prop, descriptor, context, toVmValue) {
17691
17642
  if (descriptor.set) {
17692
17643
  const setterBridge = vm.newFunction(`set_${propName}`, (valueHandle) => {
17693
17644
  try {
17694
- const jsValue = vm.dump(valueHandle);
17645
+ const jsValue = readVmValue(vm, valueHandle, true);
17695
17646
  if (prop) context[key][prop] = jsValue;
17696
17647
  else context[key] = jsValue;
17697
17648
  return vm.undefined;
@@ -17716,27 +17667,84 @@ function bridgeGetterSetter(vm, key, prop, descriptor, context, toVmValue) {
17716
17667
  if ("error" in result) result.error?.dispose();
17717
17668
  else result.value.dispose();
17718
17669
  }
17719
- function setupVariableTrackingBridge(vm, variables) {
17720
- const varTrackFnHandle = vm.newFunction(Identifiers.VariableTrackingFnIdentifier, (nameHandle, getterHandle) => {
17670
+ function installValueReader(vm) {
17671
+ vm.evalCode(`
17672
+ globalThis.__llmz_read_value = function encode(value, strict, seen = new Set()) {
17673
+ if (value === undefined) return ['undefined'];
17674
+ if (typeof value === 'number') {
17675
+ if (strict && !Number.isFinite(value)) throw new Error('Non-finite numbers cannot be assigned to host properties');
17676
+ return ['number', Object.is(value, -0) ? '-0' : String(value)];
17677
+ }
17678
+ if (value === null || typeof value === 'string' || typeof value === 'boolean') return ['value', value];
17679
+ if (typeof value !== 'object') throw new Error('Unsupported VM value: ' + typeof value);
17680
+ if (seen.has(value)) throw new Error('Cyclic VM values cannot be transferred');
17681
+ if (value instanceof Date) return ['value', value.toJSON()];
17682
+ seen.add(value);
17683
+ try {
17684
+ if (Array.isArray(value)) return ['array', Array.from(value, item => encode(item, strict, seen))];
17685
+ return ['object', Object.entries(value).map(([key, item]) => [key, encode(item, strict, seen)])];
17686
+ } finally {
17687
+ seen.delete(value);
17688
+ }
17689
+ };
17690
+ `).unwrap().dispose();
17691
+ }
17692
+ function readVmValue(vm, handle, strict = false) {
17693
+ const decode = ([type, value]) => {
17694
+ if (type === "undefined") return void 0;
17695
+ if (type === "number") return Number(value);
17696
+ if (type === "array") return value.map(decode);
17697
+ if (type === "object") return Object.fromEntries(value.map(([key, item]) => [key, decode(item)]));
17698
+ return value;
17699
+ };
17700
+ const encoder = vm.getProp(vm.global, "__llmz_read_value");
17701
+ try {
17702
+ const result = vm.callFunction(encoder, vm.undefined, handle, strict ? vm.true : vm.false);
17703
+ if (result.error) {
17704
+ const error = vm.dump(result.error);
17705
+ result.error.dispose();
17706
+ if (strict) throw new Error(error.message);
17707
+ const copy = handle.dup();
17708
+ try {
17709
+ return vm.dump(copy);
17710
+ } finally {
17711
+ if (copy.alive) copy.dispose();
17712
+ }
17713
+ }
17714
+ try {
17715
+ return decode(vm.dump(result.value));
17716
+ } finally {
17717
+ result.value.dispose();
17718
+ }
17719
+ } finally {
17720
+ encoder.dispose();
17721
+ }
17722
+ }
17723
+ function setupVariableTrackingBridge(vm, variables, getters) {
17724
+ const tracker = vm.newFunction(Identifiers.VariableTrackingFnIdentifier, (nameHandle, getterHandle) => {
17721
17725
  const name = vm.getString(nameHandle);
17722
17726
  if (NO_TRACKING.includes(name)) return;
17723
- try {
17724
- const valueResult = vm.callFunction(getterHandle, vm.undefined);
17725
- if ("error" in valueResult) {
17726
- variables[name] = "[[non-primitive]]";
17727
- valueResult.error?.dispose();
17728
- return;
17727
+ getters.get(name)?.dispose();
17728
+ const getter = getterHandle.dup();
17729
+ getters.set(name, getter);
17730
+ variables[name] = () => {
17731
+ const result = vm.callFunction(getter, vm.undefined);
17732
+ if (result.error) {
17733
+ result.error.dispose();
17734
+ return "[[non-primitive]]";
17729
17735
  }
17730
- const value = vm.dump(valueResult.value);
17731
- valueResult.value.dispose();
17732
- if (typeof value === "function" || typeof value === "string" && value.includes("=>")) variables[name] = "[[non-primitive]]";
17733
- else variables[name] = value;
17734
- } catch {
17735
- variables[name] = "[[non-primitive]]";
17736
- }
17736
+ try {
17737
+ if (vm.typeof(result.value) === "function") return "[[non-primitive]]";
17738
+ return readVmValue(vm, result.value);
17739
+ } catch {
17740
+ return "[[non-primitive]]";
17741
+ } finally {
17742
+ result.value.dispose();
17743
+ }
17744
+ };
17737
17745
  });
17738
- vm.setProp(vm.global, Identifiers.VariableTrackingFnIdentifier, varTrackFnHandle);
17739
- varTrackFnHandle.dispose();
17746
+ vm.setProp(vm.global, Identifiers.VariableTrackingFnIdentifier, tracker);
17747
+ tracker.dispose();
17740
17748
  }
17741
17749
  function buildScriptCode(transformedCode) {
17742
17750
  return `
@@ -17793,6 +17801,22 @@ var init_quickjs = __esmMin((() => {
17793
17801
  const trackedProperties = /* @__PURE__ */ new Set();
17794
17802
  const referenceProperties = /* @__PURE__ */ new Set();
17795
17803
  const pendingPromises = [];
17804
+ const variableGetters = /* @__PURE__ */ new Map();
17805
+ const objectConstructor = vm.getProp(vm.global, "Object");
17806
+ const freezeFunction = vm.getProp(objectConstructor, "freeze");
17807
+ objectConstructor.dispose();
17808
+ const preserveFreeze = (value, handle) => {
17809
+ if (Object.isFrozen(value)) {
17810
+ const frozen = vm.callFunction(freezeFunction, vm.undefined, handle);
17811
+ try {
17812
+ frozen.unwrap().dispose();
17813
+ } catch (error) {
17814
+ handle.dispose();
17815
+ throw error;
17816
+ }
17817
+ }
17818
+ return handle;
17819
+ };
17796
17820
  const toVmValue = (value) => {
17797
17821
  if (typeof value === "string") return vm.newString(value);
17798
17822
  else if (typeof value === "number") return vm.newNumber(value);
@@ -17800,20 +17824,13 @@ var init_quickjs = __esmMin((() => {
17800
17824
  else if (value === null) return vm.null;
17801
17825
  else if (value === void 0) return vm.undefined;
17802
17826
  else if (Array.isArray(value)) {
17803
- const items = value.map((item) => {
17804
- if (typeof item === "string") return JSON.stringify(item);
17805
- else if (typeof item === "number" || typeof item === "boolean") return String(item);
17806
- else if (item === null) return "null";
17807
- else if (item === void 0) return "undefined";
17808
- else if (typeof item === "object") return JSON.stringify(item);
17809
- return "undefined";
17827
+ const array = vm.newArray();
17828
+ value.forEach((item, index) => {
17829
+ const handle = toVmValue(item);
17830
+ vm.setProp(array, index, handle);
17831
+ disposeIfNeeded(handle);
17810
17832
  });
17811
- const result = vm.evalCode(`[${items.join(",")}]`);
17812
- if ("error" in result) {
17813
- result.error?.dispose();
17814
- return vm.undefined;
17815
- }
17816
- return result.value;
17833
+ return preserveFreeze(value, array);
17817
17834
  } else if (typeof value === "object") {
17818
17835
  const obj = vm.newObject();
17819
17836
  for (const [k, v] of Object.entries(value)) if (typeof v !== "function") {
@@ -17821,7 +17838,7 @@ var init_quickjs = __esmMin((() => {
17821
17838
  vm.setProp(obj, k, propHandle);
17822
17839
  disposeIfNeeded(propHandle);
17823
17840
  }
17824
- return obj;
17841
+ return preserveFreeze(value, obj);
17825
17842
  }
17826
17843
  return vm.undefined;
17827
17844
  };
@@ -17852,8 +17869,9 @@ var init_quickjs = __esmMin((() => {
17852
17869
  };
17853
17870
  };
17854
17871
  try {
17872
+ installValueReader(vm);
17855
17873
  bridgeContextToVM(context, vm, trackedProperties, referenceProperties, toVmValue, disposeIfNeeded, bridgeFunction);
17856
- setupVariableTrackingBridge(vm, variables);
17874
+ setupVariableTrackingBridge(vm, variables, variableGetters);
17857
17875
  const scriptCode = buildScriptCode(transformed.code);
17858
17876
  const copyBackContextFromVM = () => {
17859
17877
  for (const key of trackedProperties) {
@@ -17910,7 +17928,7 @@ var init_quickjs = __esmMin((() => {
17910
17928
  let returnValue = void 0;
17911
17929
  if (resultSet) {
17912
17930
  const resultResult = vm.evalCode("globalThis.__llmz_result");
17913
- returnValue = vm.dump(resultResult.unwrap());
17931
+ returnValue = readVmValue(vm, resultResult.unwrap());
17914
17932
  resultResult.unwrap().dispose();
17915
17933
  }
17916
17934
  returnValue = Signals.maybeDeserializeError(returnValue);
@@ -17942,6 +17960,8 @@ var init_quickjs = __esmMin((() => {
17942
17960
  })).catch(() => {});
17943
17961
  return handleErrorQuickJS(err, code, consumer, traces, variables, lines_executed, userCodeStartLine, ctx.currentToolCall ?? state.currentToolCall);
17944
17962
  } finally {
17963
+ for (const getter of variableGetters.values()) getter.dispose();
17964
+ freezeFunction.dispose();
17945
17965
  try {
17946
17966
  vm.dispose();
17947
17967
  } catch {}
@@ -18058,21 +18078,10 @@ async function runAsyncFunction(context, code, traces = [], signal = null, timeo
18058
18078
  });
18059
18079
  context ??= {};
18060
18080
  for (const name of Array.from(transformed.variables)) delete context[name];
18061
- let driver;
18081
+ let driver = new NodeDriver();
18062
18082
  if (useQuickJS()) try {
18083
+ await loadQuickJSModule();
18063
18084
  driver = new QuickJSDriver();
18064
- return await driver.execute({
18065
- transformed,
18066
- consumer,
18067
- context,
18068
- traces,
18069
- signal,
18070
- timeout,
18071
- code,
18072
- lines_executed,
18073
- variables,
18074
- currentToolCall: void 0
18075
- });
18076
18085
  } catch (quickjsError) {
18077
18086
  const variant = getQuickJSVariant();
18078
18087
  const debugInfo = {
@@ -18098,7 +18107,6 @@ async function runAsyncFunction(context, code, traces = [], signal = null, timeo
18098
18107
  console.warn("Error:", quickjsError?.message || quickjsError);
18099
18108
  console.warn("Debug info:", JSON.stringify(debugInfo, null, 2));
18100
18109
  }
18101
- driver = new NodeDriver();
18102
18110
  return await driver.execute({
18103
18111
  transformed,
18104
18112
  consumer,
@@ -18146,19 +18154,21 @@ var init_generate = __esmMin((() => {
18146
18154
  STREAM_INACTIVITY_TIMEOUT = 18e4;
18147
18155
  assertSuccessfulGeneration = (metadata) => {
18148
18156
  if (metadata.provider === "unknown") throw new CognitiveError("LLM generation failed: received error metadata with unknown provider");
18149
- if (metadata.stopReason === "max_tokens" || metadata.stopReason === "content_filter") throw new CognitiveError(`LLM generation did not complete: stopReason=${metadata.stopReason}`);
18157
+ if (metadata.stopReason === "max_tokens" || metadata.stopReason === "content_filter" || metadata.stopReason === "other") throw new CognitiveError(`LLM generation did not complete: stopReason=${metadata.stopReason}`);
18150
18158
  };
18151
18159
  getModelOutputLimit = (inputLength) => clamp(RESPONSE_LENGTH_BUFFER.PERCENTAGE * inputLength, RESPONSE_LENGTH_BUFFER.MIN_TOKENS, RESPONSE_LENGTH_BUFFER.MAX_TOKENS);
18152
18160
  generateCode = async ({ iteration, ctx, cognitive, controller, metadata, onSend, onSendDelta, onRunStart }) => {
18153
18161
  const startedAt = Date.now();
18154
18162
  const traces = iteration.traces;
18155
- const modelRef = Array.isArray(iteration.model) ? iteration.model[0] : iteration.model;
18156
- const model = await cognitive.getModelDetails(modelRef).catch((thrown) => {
18163
+ controller.signal.throwIfAborted();
18164
+ const modelRefs = Array.isArray(iteration.model) ? iteration.model : [iteration.model];
18165
+ const models = await Promise.all(modelRefs.map((modelRef) => cognitive.getModelDetails(modelRef).catch((thrown) => {
18157
18166
  throw new CognitiveError(`Failed to fetch model details for model "${modelRef}": ${getErrorMessage(thrown)}`);
18158
- });
18159
- let modelLimit = Math.max(model.input.maxTokens, 8e3);
18160
- if (ctx.maxTokens) modelLimit = Math.min(ctx.maxTokens, modelLimit);
18161
- const responseLengthBuffer = getModelOutputLimit(modelLimit);
18167
+ })));
18168
+ controller.signal.throwIfAborted();
18169
+ const model = models[0];
18170
+ const modelLimit = Math.min(...models.map((model) => model.input.maxTokens), ctx.maxTokens ?? Infinity);
18171
+ const responseLengthBuffer = Math.min(getModelOutputLimit(modelLimit), Math.floor(modelLimit / 2));
18162
18172
  if (iteration.tokens) iteration.tokens.limit = modelLimit;
18163
18173
  let messages;
18164
18174
  try {
@@ -18377,6 +18387,7 @@ var init_generate = __esmMin((() => {
18377
18387
  const response = await cognitive.generateText(input, { signal: controller.signal }).catch((thrown) => {
18378
18388
  throw new CognitiveError(`LLM generation failed: ${getErrorMessage(thrown)}`);
18379
18389
  });
18390
+ controller.signal.throwIfAborted();
18380
18391
  if (response.error) throw new CognitiveError(`LLM generation failed: ${response.error}`);
18381
18392
  if (!response.output) throw new CognitiveError("LLM did not return any text output");
18382
18393
  responseMetadata = response.metadata;
@@ -18590,8 +18601,10 @@ var init_utils = __esmMin((() => {
18590
18601
  //#endregion
18591
18602
  //#region src/runtime/tool-wrapper.ts
18592
18603
  function wrapTool({ chat, tool, traces, object, iteration, beforeHook, afterHook, controller }) {
18593
- const getToolInput = (input) => tool.zInput.safeParse(input).data ?? input;
18604
+ const traceSchema = tool.input ? fromJSONSchemaCompat(tool.input) : void 0;
18605
+ const getToolInput = (input) => traceSchema?.safeParse(input).data ?? input;
18594
18606
  return async function(input) {
18607
+ controller.signal.throwIfAborted();
18595
18608
  const toolCallId = `tcall_${ulid()}`;
18596
18609
  const originalInput = input;
18597
18610
  let effectiveInput = input;
@@ -18667,6 +18680,7 @@ function wrapTool({ chat, tool, traces, object, iteration, beforeHook, afterHook
18667
18680
  object,
18668
18681
  toolCallId
18669
18682
  });
18683
+ controller.signal.throwIfAborted();
18670
18684
  if (typeof beforeRes?.input !== "undefined") effectiveInput = beforeRes.input;
18671
18685
  output = await tool.execute(effectiveInput, {
18672
18686
  callId: toolCallId,
@@ -18683,9 +18697,16 @@ function wrapTool({ chat, tool, traces, object, iteration, beforeHook, afterHook
18683
18697
  });
18684
18698
  if (typeof afterRes?.output !== "undefined") output = afterRes.output;
18685
18699
  } catch (err) {
18686
- if (!await handleSignals(err)) {
18700
+ try {
18701
+ if (!await handleSignals(err)) {
18702
+ success = false;
18703
+ error = err;
18704
+ output = void 0;
18705
+ }
18706
+ } catch (hookError) {
18687
18707
  success = false;
18688
- error = err;
18708
+ error = hookError;
18709
+ output = void 0;
18689
18710
  }
18690
18711
  } finally {
18691
18712
  clearTimeout(alertSlowTool);
@@ -18699,6 +18720,7 @@ function wrapTool({ chat, tool, traces, object, iteration, beforeHook, afterHook
18699
18720
  var SLOW_TOOL_WARNING;
18700
18721
  var init_tool_wrapper = __esmMin((() => {
18701
18722
  init_errors$1();
18723
+ init_utils$1();
18702
18724
  SLOW_TOOL_WARNING = ms("15s");
18703
18725
  }));
18704
18726
  //#endregion