@ax-llm/ax 11.0.54 → 11.0.56

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.
package/index.cjs CHANGED
@@ -5979,15 +5979,11 @@ var AxPromptTemplate = class {
5979
5979
  task;
5980
5980
  thoughtFieldName;
5981
5981
  functions;
5982
- strictExamples;
5983
- optionalOutputFields;
5984
5982
  constructor(sig, options, fieldTemplates) {
5985
5983
  this.sig = sig;
5986
5984
  this.fieldTemplates = fieldTemplates;
5987
5985
  this.thoughtFieldName = options?.thoughtFieldName ?? "thought";
5988
5986
  this.functions = options?.functions;
5989
- this.strictExamples = options?.strictExamples ?? false;
5990
- this.optionalOutputFields = options?.optionalOutputFields ?? [];
5991
5987
  const task = [];
5992
5988
  const inArgs = renderDescFields(this.sig.getInputFields());
5993
5989
  const outArgs = renderDescFields(this.sig.getOutputFields());
@@ -6157,33 +6153,22 @@ ${outputFields}`);
6157
6153
  };
6158
6154
  renderExamples = (data) => {
6159
6155
  const list = [];
6160
- const inputExampleContext = {
6161
- isExample: true,
6162
- strictExamples: this.strictExamples,
6163
- optionalOutputFields: this.optionalOutputFields,
6164
- isInputField: true
6165
- };
6166
- const outputExampleContext = {
6167
- isExample: true,
6168
- strictExamples: this.strictExamples,
6169
- optionalOutputFields: this.optionalOutputFields,
6170
- isInputField: false
6156
+ const exampleContext = {
6157
+ isExample: true
6171
6158
  };
6172
6159
  for (const [index, item] of data.entries()) {
6173
- const renderedInputItem = this.sig.getInputFields().map((field) => this.renderInField(field, item, inputExampleContext)).filter((v) => v !== void 0).flat();
6174
- const outputFields = this.sig.getOutputFields();
6175
- const renderedOutputItem = outputFields.map((field) => this.renderInField(field, item, outputExampleContext)).filter((v) => v !== void 0).flat();
6176
- if (renderedOutputItem.length === 0) {
6177
- const missingFields = outputFields.filter((field) => !item[field.name]);
6178
- const allMissingFieldsAreOptional = missingFields.every(
6179
- (field) => this.optionalOutputFields.includes(field.name)
6180
- );
6181
- if (!allMissingFieldsAreOptional) {
6182
- throw new Error(
6183
- `Output fields are required in examples: index: ${index}, data: ${JSON.stringify(item)}`
6184
- );
6185
- }
6186
- }
6160
+ const renderedInputItem = this.sig.getInputFields().map(
6161
+ (field) => this.renderInField(field, item, {
6162
+ ...exampleContext,
6163
+ isInputField: true
6164
+ })
6165
+ ).filter((v) => v !== void 0).flat();
6166
+ const renderedOutputItem = this.sig.getOutputFields().map(
6167
+ (field) => this.renderInField(field, item, {
6168
+ ...exampleContext,
6169
+ isInputField: false
6170
+ })
6171
+ ).filter((v) => v !== void 0).flat();
6187
6172
  const renderedItem = [...renderedInputItem, ...renderedOutputItem];
6188
6173
  if (index > 0 && renderedItem.length > 0 && renderedItem[0]?.type === "text") {
6189
6174
  list.push({ type: "text", text: "---\n\n" });
@@ -6204,20 +6189,19 @@ ${outputFields}`);
6204
6189
  const list = [];
6205
6190
  const inputFields = this.sig.getInputFields();
6206
6191
  const outputFields = this.sig.getOutputFields();
6192
+ const demoContext = {
6193
+ isExample: true
6194
+ };
6207
6195
  for (const item of data) {
6208
6196
  const inputRenderedItems = inputFields.map(
6209
6197
  (field) => this.renderInField(field, item, {
6210
- isExample: true,
6211
- strictExamples: this.strictExamples,
6212
- optionalOutputFields: this.optionalOutputFields,
6198
+ ...demoContext,
6213
6199
  isInputField: true
6214
6200
  })
6215
6201
  ).filter((v) => v !== void 0).flat();
6216
6202
  const outputRenderedItems = outputFields.map(
6217
6203
  (field) => this.renderInField(field, item, {
6218
- isExample: true,
6219
- strictExamples: this.strictExamples,
6220
- optionalOutputFields: this.optionalOutputFields,
6204
+ ...demoContext,
6221
6205
  isInputField: false
6222
6206
  })
6223
6207
  ).filter((v) => v !== void 0).flat();
@@ -6433,24 +6417,9 @@ var isEmptyValue = (field, value, context3) => {
6433
6417
  }
6434
6418
  if (!value || (Array.isArray(value) || typeof value === "string") && value.length === 0) {
6435
6419
  if (context3?.isExample) {
6436
- const isInputField = context3?.isInputField ?? true;
6437
- if (isInputField) {
6438
- if (!context3?.strictExamples) {
6439
- return true;
6440
- } else {
6441
- if (field.isOptional || field.isInternal) {
6442
- return true;
6443
- }
6444
- throw new Error(`Value for input field '${field.name}' is required.`);
6445
- }
6446
- } else {
6447
- if (field.isOptional || field.isInternal || context3?.optionalOutputFields?.includes(field.name)) {
6448
- return true;
6449
- }
6450
- throw new Error(`Value for output field '${field.name}' is required.`);
6451
- }
6420
+ return true;
6452
6421
  }
6453
- if (field.isOptional || field.isInternal || context3?.optionalOutputFields?.includes(field.name)) {
6422
+ if (field.isOptional || field.isInternal) {
6454
6423
  return true;
6455
6424
  }
6456
6425
  const fieldType = context3?.isInputField !== false ? "input" : "output";
@@ -6923,6 +6892,9 @@ function getFieldProcessingMessage(field, resultText) {
6923
6892
  var validateJSONSchema = (schema) => {
6924
6893
  const errors = [];
6925
6894
  const validateSchemaObject = (schema2, path = "") => {
6895
+ if (!schema2 || typeof schema2 !== "object") {
6896
+ return;
6897
+ }
6926
6898
  const validTypes = [
6927
6899
  "array",
6928
6900
  "integer",
@@ -6932,38 +6904,155 @@ var validateJSONSchema = (schema) => {
6932
6904
  "null",
6933
6905
  "object"
6934
6906
  ];
6907
+ if (schema2.anyOf && Array.isArray(schema2.anyOf)) {
6908
+ if (schema2.anyOf.length === 0) {
6909
+ errors.push({
6910
+ path: path || "root",
6911
+ issue: "anyOf array is empty",
6912
+ fix: "Add at least one schema to the anyOf array",
6913
+ example: 'anyOf: [{ type: "string" }, { type: "null" }]'
6914
+ });
6915
+ }
6916
+ schema2.anyOf.forEach((subSchema, index) => {
6917
+ validateSchemaObject(subSchema, `${path}anyOf[${index}].`);
6918
+ });
6919
+ return;
6920
+ }
6921
+ if (schema2.oneOf && Array.isArray(schema2.oneOf)) {
6922
+ if (schema2.oneOf.length === 0) {
6923
+ errors.push({
6924
+ path: path || "root",
6925
+ issue: "oneOf array is empty",
6926
+ fix: "Add at least one schema to the oneOf array",
6927
+ example: 'oneOf: [{ type: "string" }, { type: "number" }]'
6928
+ });
6929
+ }
6930
+ schema2.oneOf.forEach((subSchema, index) => {
6931
+ validateSchemaObject(subSchema, `${path}oneOf[${index}].`);
6932
+ });
6933
+ return;
6934
+ }
6935
+ if (schema2.allOf && Array.isArray(schema2.allOf)) {
6936
+ if (schema2.allOf.length === 0) {
6937
+ errors.push({
6938
+ path: path || "root",
6939
+ issue: "allOf array is empty",
6940
+ fix: "Add at least one schema to the allOf array",
6941
+ example: 'allOf: [{ type: "object" }, { properties: { name: { type: "string" } } }]'
6942
+ });
6943
+ }
6944
+ schema2.allOf.forEach((subSchema, index) => {
6945
+ validateSchemaObject(subSchema, `${path}allOf[${index}].`);
6946
+ });
6947
+ return;
6948
+ }
6949
+ if (!schema2.type) {
6950
+ return;
6951
+ }
6935
6952
  if (!validTypes.includes(schema2.type)) {
6936
- errors.push(`Invalid type '${schema2.type}' at ${path || "root"}`);
6953
+ errors.push({
6954
+ path: path || "root",
6955
+ issue: `Invalid type '${schema2.type}'`,
6956
+ fix: `Change type to one of: ${validTypes.join(", ")}`,
6957
+ example: `{ type: "string" } or { type: "object" }`
6958
+ });
6937
6959
  return;
6938
6960
  }
6939
- if (schema2.type === "object" && schema2.properties) {
6940
- if (typeof schema2.properties !== "object" || Array.isArray(schema2.properties)) {
6941
- errors.push(`Invalid properties object at ${path || "root"}`);
6942
- } else {
6943
- for (const key in schema2.properties) {
6944
- const value = schema2.properties[key];
6945
- if (typeof value !== "object") {
6946
- errors.push(`Invalid schema object at ${path}${key}`);
6947
- continue;
6961
+ if (schema2.type === "object") {
6962
+ if (schema2.properties) {
6963
+ if (typeof schema2.properties !== "object" || Array.isArray(schema2.properties)) {
6964
+ errors.push({
6965
+ path: path || "root",
6966
+ issue: "properties must be an object, not an array or primitive",
6967
+ fix: "Change properties to be an object with property names as keys",
6968
+ example: 'properties: { name: { type: "string" }, age: { type: "number" } }'
6969
+ });
6970
+ } else {
6971
+ for (const key in schema2.properties) {
6972
+ const value = schema2.properties[key];
6973
+ if (value === void 0 || value === null) {
6974
+ continue;
6975
+ }
6976
+ if (typeof value !== "object") {
6977
+ errors.push({
6978
+ path: `${path}${key}`,
6979
+ issue: `Property schema must be an object, got ${typeof value}`,
6980
+ fix: "Define the property as a proper schema object",
6981
+ example: `${key}: { type: "string", description: "..." }`
6982
+ });
6983
+ continue;
6984
+ }
6985
+ validateSchemaObject(value, `${path}${key}.`);
6948
6986
  }
6949
- validateSchemaObject(value, `${path}${key}.`);
6950
6987
  }
6951
6988
  }
6952
- if (schema2.required && !Array.isArray(schema2.required)) {
6953
- errors.push(`'required' should be an array at ${path || "root"}`);
6989
+ if (schema2.required) {
6990
+ if (!Array.isArray(schema2.required)) {
6991
+ errors.push({
6992
+ path: path || "root",
6993
+ issue: `'required' must be an array, got ${typeof schema2.required}`,
6994
+ fix: "Change required to be an array of property names",
6995
+ example: 'required: ["name", "email"] instead of required: "name,email"'
6996
+ });
6997
+ } else if (schema2.required.length === 0) {
6998
+ } else {
6999
+ if (schema2.properties) {
7000
+ for (const requiredProp of schema2.required) {
7001
+ if (typeof requiredProp !== "string") {
7002
+ errors.push({
7003
+ path: `${path}required`,
7004
+ issue: `Required property names must be strings, got ${typeof requiredProp}`,
7005
+ fix: "Ensure all items in required array are strings",
7006
+ example: 'required: ["name", "email"] not required: [123, "email"]'
7007
+ });
7008
+ } else if (!(requiredProp in schema2.properties)) {
7009
+ errors.push({
7010
+ path: `${path}required`,
7011
+ issue: `Required property '${requiredProp}' is not defined in properties`,
7012
+ fix: `Either add '${requiredProp}' to properties or remove it from required`,
7013
+ example: `properties: { ${requiredProp}: { type: "string" } }`
7014
+ });
7015
+ }
7016
+ }
7017
+ }
7018
+ }
6954
7019
  }
6955
7020
  }
6956
- if (schema2.type === "array" && schema2.items) {
6957
- if (typeof schema2.items !== "object") {
6958
- errors.push(`Invalid items schema at ${path || "root"}`);
6959
- } else {
6960
- validateSchemaObject(schema2.items, `${path}items.`);
7021
+ if (schema2.type === "array") {
7022
+ if (schema2.items) {
7023
+ if (typeof schema2.items !== "object") {
7024
+ errors.push({
7025
+ path: `${path}items`,
7026
+ issue: `Array items schema must be an object, got ${typeof schema2.items}`,
7027
+ fix: "Define items as a proper schema object",
7028
+ example: 'items: { type: "string" } or items: { type: "object", properties: {...} }'
7029
+ });
7030
+ } else {
7031
+ validateSchemaObject(schema2.items, `${path}items.`);
7032
+ }
6961
7033
  }
6962
7034
  }
6963
7035
  };
6964
7036
  validateSchemaObject(schema);
6965
7037
  if (errors.length > 0) {
6966
- throw new Error(errors.join("; "));
7038
+ const errorMessage = [
7039
+ "JSON Schema validation failed:",
7040
+ "",
7041
+ ...errors.map((error, index) => {
7042
+ const parts = [
7043
+ `${index + 1}. Path: ${error.path}`,
7044
+ ` Issue: ${error.issue}`,
7045
+ ` Fix: ${error.fix}`
7046
+ ];
7047
+ if (error.example) {
7048
+ parts.push(` Example: ${error.example}`);
7049
+ }
7050
+ return parts.join("\n");
7051
+ }),
7052
+ "",
7053
+ "Please fix these issues and try again."
7054
+ ].join("\n");
7055
+ throw new Error(errorMessage);
6967
7056
  }
6968
7057
  };
6969
7058
 
@@ -7781,7 +7870,7 @@ var AxProgramWithSignature = class {
7781
7870
  const res = {};
7782
7871
  for (const f of fields) {
7783
7872
  const value = e[f.name];
7784
- if (value) {
7873
+ if (value !== void 0) {
7785
7874
  validateValue(f, value);
7786
7875
  res[f.name] = value;
7787
7876
  }
@@ -7914,9 +8003,7 @@ var AxGen = class extends AxProgramWithSignature {
7914
8003
  this.thoughtFieldName = options?.thoughtFieldName ?? "thought";
7915
8004
  const promptTemplateOptions = {
7916
8005
  functions: options?.functions,
7917
- thoughtFieldName: this.thoughtFieldName,
7918
- strictExamples: options?.strictExamples,
7919
- optionalOutputFields: options?.optionalOutputFields
8006
+ thoughtFieldName: this.thoughtFieldName
7920
8007
  };
7921
8008
  this.promptTemplate = new (options?.promptTemplate ?? AxPromptTemplate)(
7922
8009
  this.signature,
@@ -8280,9 +8367,7 @@ Content: ${result.content}`
8280
8367
  const promptTemplateClass = this.options?.promptTemplate ?? AxPromptTemplate;
8281
8368
  const currentPromptTemplateOptions = {
8282
8369
  functions: options.functions,
8283
- thoughtFieldName: this.thoughtFieldName,
8284
- strictExamples: this.options?.strictExamples,
8285
- optionalOutputFields: this.options?.optionalOutputFields
8370
+ thoughtFieldName: this.thoughtFieldName
8286
8371
  };
8287
8372
  this.promptTemplate = new promptTemplateClass(
8288
8373
  this.signature,
@@ -8470,19 +8555,6 @@ Content: ${result.content}`
8470
8555
  }
8471
8556
  setExamples(examples, options) {
8472
8557
  super.setExamples(examples, options);
8473
- if (options?.optionalOutputFields) {
8474
- const promptTemplateClass = this.options?.promptTemplate ?? AxPromptTemplate;
8475
- const currentPromptTemplateOptions = {
8476
- functions: this.functions,
8477
- thoughtFieldName: this.thoughtFieldName,
8478
- strictExamples: this.options?.strictExamples,
8479
- optionalOutputFields: options.optionalOutputFields
8480
- };
8481
- this.promptTemplate = new promptTemplateClass(
8482
- this.signature,
8483
- currentPromptTemplateOptions
8484
- );
8485
- }
8486
8558
  }
8487
8559
  };
8488
8560
  var AxGenerateError = class extends Error {