@ax-llm/ax 10.0.48 → 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
@@ -1587,16 +1587,18 @@ function createMessages(chatPrompt) {
1587
1587
  const items = chatPrompt.map((msg) => {
1588
1588
  switch (msg.role) {
1589
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
+ ];
1590
1599
  return {
1591
1600
  role: "user",
1592
- content: [
1593
- {
1594
- type: "tool_result",
1595
- content: msg.result,
1596
- tool_use_id: msg.functionId,
1597
- ...msg.cache ? { cache: { type: "ephemeral" } } : {}
1598
- }
1599
- ]
1601
+ content
1600
1602
  };
1601
1603
  case "user": {
1602
1604
  if (typeof msg.content === "string") {
@@ -1605,7 +1607,7 @@ function createMessages(chatPrompt) {
1605
1607
  content: msg.content
1606
1608
  };
1607
1609
  }
1608
- const content = msg.content.map((v) => {
1610
+ const content2 = msg.content.map((v) => {
1609
1611
  switch (v.type) {
1610
1612
  case "text":
1611
1613
  return {
@@ -1629,16 +1631,16 @@ function createMessages(chatPrompt) {
1629
1631
  });
1630
1632
  return {
1631
1633
  role: "user",
1632
- content
1634
+ content: content2
1633
1635
  };
1634
1636
  }
1635
1637
  case "assistant": {
1636
- let content = "";
1638
+ let content2 = "";
1637
1639
  if (typeof msg.content === "string") {
1638
- content = msg.content;
1640
+ content2 = msg.content;
1639
1641
  }
1640
1642
  if (typeof msg.functionCalls !== "undefined") {
1641
- content = msg.functionCalls.map((v) => {
1643
+ content2 = msg.functionCalls.map((v) => {
1642
1644
  let input;
1643
1645
  if (typeof v.function.params === "string") {
1644
1646
  input = JSON.parse(v.function.params);
@@ -1656,7 +1658,7 @@ function createMessages(chatPrompt) {
1656
1658
  }
1657
1659
  return {
1658
1660
  role: "assistant",
1659
- content
1661
+ content: content2
1660
1662
  };
1661
1663
  }
1662
1664
  default:
@@ -3823,7 +3825,6 @@ var AxAssertionError = class extends Error {
3823
3825
  }) {
3824
3826
  super(message);
3825
3827
  this.name = this.constructor.name;
3826
- this.stack = new Error().stack;
3827
3828
  }
3828
3829
  getFixingInstructions = () => {
3829
3830
  const extraFields = [];
@@ -4739,9 +4740,9 @@ var functionCallInstructions = `
4739
4740
  - Use the function results to generate the output fields.`;
4740
4741
  var formattingRules = `
4741
4742
  ## Output Formatting Rules
4743
+ - Output must strictly follow the defined plaintext \`key: value\` field format.
4742
4744
  - Each output key, value must strictly adhere to the specified output field formatting rules.
4743
4745
  - No preamble, postscript, or supplementary information.
4744
- - Output must be in plain text, with each \`key: value\` pair on a new line.
4745
4746
  - Do not repeat output fields.`;
4746
4747
  var AxPromptTemplate = class {
4747
4748
  sig;
@@ -5143,7 +5144,7 @@ function handleValidationError(mem, errorFields, ai, promptTemplate, sessionId)
5143
5144
  if (ai.getOptions().debug) {
5144
5145
  const errors = errorFields.map((field) => `- ${field.title}: ${field.description}`).join("\n");
5145
5146
  process.stdout.write(colorLog4.red(`
5146
- Error Correction:
5147
+ \u274C Error Correction:
5147
5148
  ${errors}
5148
5149
  `));
5149
5150
  }
@@ -5484,6 +5485,7 @@ var validateJSONSchema = (schema) => {
5484
5485
  };
5485
5486
 
5486
5487
  // dsp/functions.ts
5488
+ var colorLog5 = new ColorLog();
5487
5489
  var AxFunctionError = class extends Error {
5488
5490
  constructor(fields) {
5489
5491
  super();
@@ -5493,17 +5495,13 @@ var AxFunctionError = class extends Error {
5493
5495
  getFields = () => this.fields;
5494
5496
  };
5495
5497
  var FunctionError = class extends Error {
5496
- fields;
5497
- func;
5498
- constructor({
5499
- fields,
5500
- func
5501
- }) {
5498
+ constructor(fields, func, funcId) {
5502
5499
  super();
5503
5500
  this.fields = fields;
5504
5501
  this.func = func;
5505
- this.name = this.constructor.name;
5502
+ this.funcId = funcId;
5506
5503
  }
5504
+ getFunctionId = () => this.funcId;
5507
5505
  getFieldDescription(fieldName) {
5508
5506
  if (!this.func.parameters?.properties?.[fieldName]) {
5509
5507
  return "";
@@ -5516,15 +5514,12 @@ var FunctionError = class extends Error {
5516
5514
  return description;
5517
5515
  }
5518
5516
  getFixingInstructions = () => {
5519
- return this.fields.map((fieldError) => {
5520
- const schemaDescription = this.getFieldDescription(fieldError.field);
5521
- const fullDescription = schemaDescription ? `${fieldError.message}. ${schemaDescription}` : fieldError.message;
5522
- return {
5523
- name: "functionArgumentError",
5524
- title: `Errors in Function '${this.func.name}' Arguments`,
5525
- description: `Please fix the argument '${fieldError.field}' in function '${this.func.name}': ${fullDescription}`
5526
- };
5517
+ const bulletPoints = this.fields.map((fieldError) => {
5518
+ const schemaDescription = this.getFieldDescription(fieldError.field) || "";
5519
+ return `- \`${fieldError.field}\` - ${fieldError.message} (${schemaDescription}).`;
5527
5520
  });
5521
+ return `Errors In Function Arguments: Fix the following invalid arguments to '${this.func.name}'
5522
+ ${bulletPoints.join("\n")}`;
5528
5523
  };
5529
5524
  };
5530
5525
  var AxFunctionProcessor = class {
@@ -5546,16 +5541,10 @@ var AxFunctionProcessor = class {
5546
5541
  } : void 0;
5547
5542
  if (!fnSpec.parameters) {
5548
5543
  const res2 = fnSpec.func.length === 1 ? await fnSpec.func(opt) : await fnSpec.func();
5549
- return {
5550
- id: func.id,
5551
- result: JSON.stringify(res2, null, 2)
5552
- };
5544
+ return typeof res2 === "string" ? res2 : JSON.stringify(res2, null, 2);
5553
5545
  }
5554
5546
  const res = fnSpec.func.length === 2 ? await fnSpec.func(args, opt) : await fnSpec.func(args);
5555
- return {
5556
- id: func.id,
5557
- result: JSON.stringify(res, null, 2)
5558
- };
5547
+ return typeof res === "string" ? res : JSON.stringify(res, null, 2);
5559
5548
  };
5560
5549
  execute = async (func, options) => {
5561
5550
  const fnSpec = this.funcList.find(
@@ -5571,10 +5560,7 @@ var AxFunctionProcessor = class {
5571
5560
  return await this.executeFunction(fnSpec, func, options);
5572
5561
  } catch (e) {
5573
5562
  if (e instanceof AxFunctionError) {
5574
- throw new FunctionError({
5575
- fields: e.getFields(),
5576
- func: fnSpec
5577
- });
5563
+ throw new FunctionError(e.getFields(), fnSpec, func.id);
5578
5564
  }
5579
5565
  throw e;
5580
5566
  }
@@ -5598,19 +5584,44 @@ var parseFunctions = (newFuncs, existingFuncs) => {
5598
5584
  var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, traceId) => {
5599
5585
  const funcProc = new AxFunctionProcessor(functionList);
5600
5586
  const functionsExecuted = /* @__PURE__ */ new Set();
5601
- const promises = functionCalls.map(
5602
- (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) => {
5603
5592
  functionsExecuted.add(func.name.toLowerCase());
5604
- if (fres?.id) {
5605
- return {
5606
- role: "function",
5607
- result: fres.result ?? "",
5608
- functionId: fres.id
5609
- };
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;
5610
5621
  }
5611
- return null;
5612
- })
5613
- );
5622
+ });
5623
+ return promise;
5624
+ });
5614
5625
  const results = await Promise.all(promises);
5615
5626
  results.forEach((result) => {
5616
5627
  if (result) {
@@ -5910,9 +5921,6 @@ var AxGen = class extends AxProgramWithSignature {
5910
5921
  const e1 = e;
5911
5922
  errorFields = e1.getFixingInstructions();
5912
5923
  err = e;
5913
- } else if (e instanceof FunctionError) {
5914
- errorFields = e.getFixingInstructions();
5915
- err = e;
5916
5924
  } else if (e instanceof AxAIServiceStreamTerminatedError) {
5917
5925
  } else {
5918
5926
  throw e;
@@ -7514,7 +7522,7 @@ var AxJSInterpreter = class {
7514
7522
  };
7515
7523
 
7516
7524
  // dsp/router.ts
7517
- var colorLog5 = new ColorLog();
7525
+ var colorLog6 = new ColorLog();
7518
7526
  var AxRoute = class {
7519
7527
  name;
7520
7528
  context;
@@ -7566,7 +7574,7 @@ var AxRouter = class {
7566
7574
  }
7567
7575
  if (this.debug) {
7568
7576
  console.log(
7569
- colorLog5.whiteBright(`query: ${text}`) + "\n" + colorLog5.greenBright(
7577
+ colorLog6.whiteBright(`query: ${text}`) + "\n" + colorLog6.greenBright(
7570
7578
  JSON.stringify(m.map((m2) => `${m2.id}, ${m2.score}`))
7571
7579
  )
7572
7580
  );