@ax-llm/ax 10.0.47 → 10.0.49

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
@@ -84,6 +84,7 @@ __export(index_exports, {
84
84
  AxDefaultResultReranker: () => AxDefaultResultReranker,
85
85
  AxDockerSession: () => AxDockerSession,
86
86
  AxEmbeddingAdapter: () => AxEmbeddingAdapter,
87
+ AxFunctionError: () => AxFunctionError,
87
88
  AxFunctionProcessor: () => AxFunctionProcessor,
88
89
  AxGen: () => AxGen,
89
90
  AxHFDataLoader: () => AxHFDataLoader,
@@ -319,7 +320,7 @@ var AxAIServiceError = class extends Error {
319
320
  super(message);
320
321
  this.url = url;
321
322
  this.requestBody = requestBody;
322
- this.name = "AxAIServiceError";
323
+ this.name = this.constructor.name;
323
324
  this.timestamp = (/* @__PURE__ */ new Date()).toISOString();
324
325
  this.errorId = crypto.randomUUID();
325
326
  this.context = context;
@@ -356,7 +357,7 @@ var AxAIServiceStatusError = class extends AxAIServiceError {
356
357
  });
357
358
  this.status = status;
358
359
  this.statusText = statusText;
359
- this.name = "AxAIServiceStatusError";
360
+ this.name = this.constructor.name;
360
361
  }
361
362
  };
362
363
  var AxAIServiceNetworkError = class extends AxAIServiceError {
@@ -367,14 +368,14 @@ var AxAIServiceNetworkError = class extends AxAIServiceError {
367
368
  ...context
368
369
  });
369
370
  this.originalError = originalError;
370
- this.name = "AxAIServiceNetworkError";
371
+ this.name = this.constructor.name;
371
372
  this.stack = originalError.stack;
372
373
  }
373
374
  };
374
375
  var AxAIServiceResponseError = class extends AxAIServiceError {
375
376
  constructor(message, url, requestBody, context) {
376
377
  super(message, url, requestBody, context);
377
- this.name = "AxAIServiceResponseError";
378
+ this.name = this.constructor.name;
378
379
  }
379
380
  };
380
381
  var AxAIServiceStreamTerminatedError = class extends AxAIServiceError {
@@ -384,7 +385,7 @@ var AxAIServiceStreamTerminatedError = class extends AxAIServiceError {
384
385
  ...context
385
386
  });
386
387
  this.lastChunk = lastChunk;
387
- this.name = "AxAIServiceStreamTerminatedError";
388
+ this.name = this.constructor.name;
388
389
  }
389
390
  };
390
391
  var AxAIServiceTimeoutError = class extends AxAIServiceError {
@@ -393,13 +394,13 @@ var AxAIServiceTimeoutError = class extends AxAIServiceError {
393
394
  timeoutMs,
394
395
  ...context
395
396
  });
396
- this.name = "AxAIServiceTimeoutError";
397
+ this.name = this.constructor.name;
397
398
  }
398
399
  };
399
400
  var AxAIServiceAuthenticationError = class extends AxAIServiceError {
400
401
  constructor(url, requestBody, context) {
401
402
  super("Authentication failed", url, requestBody, context);
402
- this.name = "AxAIServiceAuthenticationError";
403
+ this.name = this.constructor.name;
403
404
  }
404
405
  };
405
406
  function calculateRetryDelay(attempt, config) {
@@ -1586,16 +1587,18 @@ function createMessages(chatPrompt) {
1586
1587
  const items = chatPrompt.map((msg) => {
1587
1588
  switch (msg.role) {
1588
1589
  case "function":
1590
+ const content = [
1591
+ {
1592
+ type: "tool_result",
1593
+ content: msg.result,
1594
+ tool_use_id: msg.functionId,
1595
+ ...msg.isError ? { is_error: true } : {},
1596
+ ...msg.cache ? { cache: { type: "ephemeral" } } : {}
1597
+ }
1598
+ ];
1589
1599
  return {
1590
1600
  role: "user",
1591
- content: [
1592
- {
1593
- type: "tool_result",
1594
- content: msg.result,
1595
- tool_use_id: msg.functionId,
1596
- ...msg.cache ? { cache: { type: "ephemeral" } } : {}
1597
- }
1598
- ]
1601
+ content
1599
1602
  };
1600
1603
  case "user": {
1601
1604
  if (typeof msg.content === "string") {
@@ -1604,7 +1607,7 @@ function createMessages(chatPrompt) {
1604
1607
  content: msg.content
1605
1608
  };
1606
1609
  }
1607
- const content = msg.content.map((v) => {
1610
+ const content2 = msg.content.map((v) => {
1608
1611
  switch (v.type) {
1609
1612
  case "text":
1610
1613
  return {
@@ -1628,16 +1631,16 @@ function createMessages(chatPrompt) {
1628
1631
  });
1629
1632
  return {
1630
1633
  role: "user",
1631
- content
1634
+ content: content2
1632
1635
  };
1633
1636
  }
1634
1637
  case "assistant": {
1635
- let content = "";
1638
+ let content2 = "";
1636
1639
  if (typeof msg.content === "string") {
1637
- content = msg.content;
1640
+ content2 = msg.content;
1638
1641
  }
1639
1642
  if (typeof msg.functionCalls !== "undefined") {
1640
- content = msg.functionCalls.map((v) => {
1643
+ content2 = msg.functionCalls.map((v) => {
1641
1644
  let input;
1642
1645
  if (typeof v.function.params === "string") {
1643
1646
  input = JSON.parse(v.function.params);
@@ -1655,7 +1658,7 @@ function createMessages(chatPrompt) {
1655
1658
  }
1656
1659
  return {
1657
1660
  role: "assistant",
1658
- content
1661
+ content: content2
1659
1662
  };
1660
1663
  }
1661
1664
  default:
@@ -3817,21 +3820,12 @@ var AxMemory = class {
3817
3820
 
3818
3821
  // dsp/asserts.ts
3819
3822
  var AxAssertionError = class extends Error {
3820
- values;
3821
- optional;
3822
3823
  constructor({
3823
- message,
3824
- values,
3825
- optional
3824
+ message
3826
3825
  }) {
3827
3826
  super(message);
3828
- this.values = values;
3829
- this.optional = optional;
3830
3827
  this.name = this.constructor.name;
3831
- this.stack = new Error().stack;
3832
3828
  }
3833
- getValue = () => this.values;
3834
- getOptional = () => this.optional;
3835
3829
  getFixingInstructions = () => {
3836
3830
  const extraFields = [];
3837
3831
  extraFields.push({
@@ -3844,13 +3838,16 @@ var AxAssertionError = class extends Error {
3844
3838
  };
3845
3839
  var assertAssertions = (asserts, values) => {
3846
3840
  for (const assert of asserts) {
3847
- const { fn, message, optional } = assert;
3841
+ const { fn, message } = assert;
3848
3842
  const res = fn(values);
3849
3843
  if (res === void 0) {
3850
3844
  continue;
3851
3845
  }
3852
- if (!res && message) {
3853
- throw new AxAssertionError({ message, values, optional });
3846
+ if (!res) {
3847
+ if (!message) {
3848
+ throw new Error(`Assertion Failed: No message provided for assertion`);
3849
+ }
3850
+ throw new AxAssertionError({ message });
3854
3851
  }
3855
3852
  }
3856
3853
  };
@@ -3866,13 +3863,13 @@ var assertStreamingAssertions = (asserts, values, xstate, content, final) => {
3866
3863
  }
3867
3864
  const currValue = content.substring(xstate.s);
3868
3865
  for (const assert of fieldAsserts) {
3869
- const { message, optional, fn } = assert;
3866
+ const { message, fn } = assert;
3870
3867
  const res = fn(currValue, final);
3871
3868
  if (res === void 0) {
3872
3869
  continue;
3873
3870
  }
3874
3871
  if (!res && message) {
3875
- throw new AxAssertionError({ message, values, optional });
3872
+ throw new AxAssertionError({ message });
3876
3873
  }
3877
3874
  }
3878
3875
  };
@@ -4743,9 +4740,9 @@ var functionCallInstructions = `
4743
4740
  - Use the function results to generate the output fields.`;
4744
4741
  var formattingRules = `
4745
4742
  ## Output Formatting Rules
4743
+ - Output must strictly follow the defined plaintext \`key: value\` field format.
4746
4744
  - Each output key, value must strictly adhere to the specified output field formatting rules.
4747
4745
  - No preamble, postscript, or supplementary information.
4748
- - Output must be in plain text, with each \`key: value\` pair on a new line.
4749
4746
  - Do not repeat output fields.`;
4750
4747
  var AxPromptTemplate = class {
4751
4748
  sig;
@@ -5126,13 +5123,11 @@ var ValidationError = class extends Error {
5126
5123
  super(message);
5127
5124
  this.fields = fields;
5128
5125
  this.name = this.constructor.name;
5129
- Error.captureStackTrace(this, this.constructor);
5130
5126
  }
5131
- getFields = () => this.fields;
5132
5127
  getFixingInstructions = () => {
5133
5128
  return this.fields.map((field) => ({
5134
5129
  name: "outputError",
5135
- title: "Error In Output",
5130
+ title: "Errors In Output Fields",
5136
5131
  description: `Please fix and return the field \`${field.title}\` of type \`${toFieldType(field.type)}\`, ${this.message}.`
5137
5132
  }));
5138
5133
  };
@@ -5149,7 +5144,7 @@ function handleValidationError(mem, errorFields, ai, promptTemplate, sessionId)
5149
5144
  if (ai.getOptions().debug) {
5150
5145
  const errors = errorFields.map((field) => `- ${field.title}: ${field.description}`).join("\n");
5151
5146
  process.stdout.write(colorLog4.red(`
5152
- Error Correction:
5147
+ \u274C Error Correction:
5153
5148
  ${errors}
5154
5149
  `));
5155
5150
  }
@@ -5490,6 +5485,43 @@ var validateJSONSchema = (schema) => {
5490
5485
  };
5491
5486
 
5492
5487
  // dsp/functions.ts
5488
+ var colorLog5 = new ColorLog();
5489
+ var AxFunctionError = class extends Error {
5490
+ constructor(fields) {
5491
+ super();
5492
+ this.fields = fields;
5493
+ this.name = this.constructor.name;
5494
+ }
5495
+ getFields = () => this.fields;
5496
+ };
5497
+ var FunctionError = class extends Error {
5498
+ constructor(fields, func, funcId) {
5499
+ super();
5500
+ this.fields = fields;
5501
+ this.func = func;
5502
+ this.funcId = funcId;
5503
+ }
5504
+ getFunctionId = () => this.funcId;
5505
+ getFieldDescription(fieldName) {
5506
+ if (!this.func.parameters?.properties?.[fieldName]) {
5507
+ return "";
5508
+ }
5509
+ const fieldSchema = this.func.parameters.properties[fieldName];
5510
+ let description = fieldSchema.description;
5511
+ if (fieldSchema.enum?.length) {
5512
+ description += ` Allowed values are: ${fieldSchema.enum.join(", ")}`;
5513
+ }
5514
+ return description;
5515
+ }
5516
+ getFixingInstructions = () => {
5517
+ const bulletPoints = this.fields.map((fieldError) => {
5518
+ const schemaDescription = this.getFieldDescription(fieldError.field) || "";
5519
+ return `- \`${fieldError.field}\` - ${fieldError.message} (${schemaDescription}).`;
5520
+ });
5521
+ return `Errors In Function Arguments: Fix the following invalid arguments to '${this.func.name}'
5522
+ ${bulletPoints.join("\n")}`;
5523
+ };
5524
+ };
5493
5525
  var AxFunctionProcessor = class {
5494
5526
  funcList = [];
5495
5527
  constructor(funcList) {
@@ -5509,16 +5541,10 @@ var AxFunctionProcessor = class {
5509
5541
  } : void 0;
5510
5542
  if (!fnSpec.parameters) {
5511
5543
  const res2 = fnSpec.func.length === 1 ? await fnSpec.func(opt) : await fnSpec.func();
5512
- return {
5513
- id: func.id,
5514
- result: JSON.stringify(res2, null, 2)
5515
- };
5544
+ return typeof res2 === "string" ? res2 : JSON.stringify(res2, null, 2);
5516
5545
  }
5517
5546
  const res = fnSpec.func.length === 2 ? await fnSpec.func(args, opt) : await fnSpec.func(args);
5518
- return {
5519
- id: func.id,
5520
- result: JSON.stringify(res, null, 2)
5521
- };
5547
+ return typeof res === "string" ? res : JSON.stringify(res, null, 2);
5522
5548
  };
5523
5549
  execute = async (func, options) => {
5524
5550
  const fnSpec = this.funcList.find(
@@ -5530,7 +5556,14 @@ var AxFunctionProcessor = class {
5530
5556
  if (!fnSpec.func) {
5531
5557
  throw new Error("No handler for function: " + func.name);
5532
5558
  }
5533
- return await this.executeFunction(fnSpec, func, options);
5559
+ try {
5560
+ return await this.executeFunction(fnSpec, func, options);
5561
+ } catch (e) {
5562
+ if (e instanceof AxFunctionError) {
5563
+ throw new FunctionError(e.getFields(), fnSpec, func.id);
5564
+ }
5565
+ throw e;
5566
+ }
5534
5567
  };
5535
5568
  };
5536
5569
  var parseFunctions = (newFuncs, existingFuncs) => {
@@ -5551,19 +5584,44 @@ var parseFunctions = (newFuncs, existingFuncs) => {
5551
5584
  var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, traceId) => {
5552
5585
  const funcProc = new AxFunctionProcessor(functionList);
5553
5586
  const functionsExecuted = /* @__PURE__ */ new Set();
5554
- const promises = functionCalls.map(
5555
- (func) => funcProc?.execute(func, { sessionId, traceId, ai }).then((fres) => {
5587
+ const promises = functionCalls.map((func) => {
5588
+ if (!func.id) {
5589
+ throw new Error(`Function ${func.name} did not return an ID`);
5590
+ }
5591
+ const promise = funcProc.execute(func, { sessionId, traceId, ai }).then((functionResult) => {
5556
5592
  functionsExecuted.add(func.name.toLowerCase());
5557
- if (fres?.id) {
5558
- return {
5559
- role: "function",
5560
- result: fres.result ?? "",
5561
- functionId: fres.id
5562
- };
5593
+ return {
5594
+ role: "function",
5595
+ result: functionResult ?? "",
5596
+ functionId: func.id
5597
+ };
5598
+ }).catch((e) => {
5599
+ if (e instanceof FunctionError) {
5600
+ const result = e.getFixingInstructions();
5601
+ mem.add(
5602
+ {
5603
+ role: "function",
5604
+ functionId: func.id,
5605
+ isError: true,
5606
+ result
5607
+ },
5608
+ sessionId
5609
+ );
5610
+ mem.addTag("error");
5611
+ if (ai.getOptions().debug) {
5612
+ process.stdout.write(
5613
+ colorLog5.red(`
5614
+ \u274C Function Error Correction:
5615
+ ${result}
5616
+ `)
5617
+ );
5618
+ }
5619
+ } else {
5620
+ throw e;
5563
5621
  }
5564
- return null;
5565
- })
5566
- );
5622
+ });
5623
+ return promise;
5624
+ });
5567
5625
  const results = await Promise.all(promises);
5568
5626
  results.forEach((result) => {
5569
5627
  if (result) {
@@ -5609,11 +5667,11 @@ var AxGen = class extends AxProgramWithSignature {
5609
5667
  this.functions = parseFunctions(options.functions);
5610
5668
  }
5611
5669
  }
5612
- addAssert = (fn, message, optional) => {
5613
- this.asserts.push({ fn, message, optional });
5670
+ addAssert = (fn, message) => {
5671
+ this.asserts.push({ fn, message });
5614
5672
  };
5615
- addStreamingAssert = (fieldName, fn, message, optional) => {
5616
- this.streamingAsserts.push({ fieldName, fn, message, optional });
5673
+ addStreamingAssert = (fieldName, fn, message) => {
5674
+ this.streamingAsserts.push({ fieldName, fn, message });
5617
5675
  };
5618
5676
  async forwardSendRequest({
5619
5677
  ai,
@@ -5878,9 +5936,6 @@ var AxGen = class extends AxProgramWithSignature {
5878
5936
  }
5879
5937
  }
5880
5938
  }
5881
- if (err instanceof AxAssertionError && err.getOptional()) {
5882
- return err.getValue();
5883
- }
5884
5939
  throw new Error(`Unable to fix validation error: ${err?.message}`);
5885
5940
  }
5886
5941
  throw new Error(`Max steps reached: ${maxSteps}`);
@@ -7467,7 +7522,7 @@ var AxJSInterpreter = class {
7467
7522
  };
7468
7523
 
7469
7524
  // dsp/router.ts
7470
- var colorLog5 = new ColorLog();
7525
+ var colorLog6 = new ColorLog();
7471
7526
  var AxRoute = class {
7472
7527
  name;
7473
7528
  context;
@@ -7519,7 +7574,7 @@ var AxRouter = class {
7519
7574
  }
7520
7575
  if (this.debug) {
7521
7576
  console.log(
7522
- colorLog5.whiteBright(`query: ${text}`) + "\n" + colorLog5.greenBright(
7577
+ colorLog6.whiteBright(`query: ${text}`) + "\n" + colorLog6.greenBright(
7523
7578
  JSON.stringify(m.map((m2) => `${m2.id}, ${m2.score}`))
7524
7579
  )
7525
7580
  );
@@ -7940,6 +7995,7 @@ var AxRAG = class extends AxChainOfThought {
7940
7995
  AxDefaultResultReranker,
7941
7996
  AxDockerSession,
7942
7997
  AxEmbeddingAdapter,
7998
+ AxFunctionError,
7943
7999
  AxFunctionProcessor,
7944
8000
  AxGen,
7945
8001
  AxHFDataLoader,