@ax-llm/ax 12.0.6 → 12.0.7

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
@@ -2623,48 +2623,59 @@ function createMessages2(req) {
2623
2623
  case "system":
2624
2624
  return { role: "system", content: msg.content };
2625
2625
  case "user":
2626
- if (Array.isArray(msg.content)) {
2626
+ const content = Array.isArray(msg.content) ? msg.content.map((c) => {
2627
+ switch (c.type) {
2628
+ case "text":
2629
+ return { type: "text", text: c.text };
2630
+ case "image": {
2631
+ const url = `data:${c.mimeType};base64,` + c.image;
2632
+ return {
2633
+ type: "image_url",
2634
+ image_url: { url, details: c.details ?? "auto" }
2635
+ };
2636
+ }
2637
+ case "audio": {
2638
+ const data = c.data;
2639
+ return {
2640
+ type: "input_audio",
2641
+ input_audio: { data, format: c.format ?? "wav" }
2642
+ };
2643
+ }
2644
+ default:
2645
+ throw new Error("Invalid content type");
2646
+ }
2647
+ }) : msg.content;
2648
+ return {
2649
+ role: "user",
2650
+ ...msg.name ? { name: msg.name } : {},
2651
+ content
2652
+ };
2653
+ case "assistant":
2654
+ const toolCalls = msg.functionCalls?.map((v) => ({
2655
+ id: v.id,
2656
+ type: "function",
2657
+ function: {
2658
+ name: v.function.name,
2659
+ arguments: typeof v.function.params === "object" ? JSON.stringify(v.function.params) : v.function.params
2660
+ }
2661
+ }));
2662
+ if (toolCalls && toolCalls.length > 0) {
2627
2663
  return {
2628
- role: "user",
2664
+ role: "assistant",
2665
+ ...msg.content ? { content: msg.content } : {},
2629
2666
  name: msg.name,
2630
- content: msg.content.map((c) => {
2631
- switch (c.type) {
2632
- case "text":
2633
- return { type: "text", text: c.text };
2634
- case "image": {
2635
- const url = `data:${c.mimeType};base64,` + c.image;
2636
- return {
2637
- type: "image_url",
2638
- image_url: { url, details: c.details ?? "auto" }
2639
- };
2640
- }
2641
- case "audio": {
2642
- const data = c.data;
2643
- return {
2644
- type: "input_audio",
2645
- input_audio: { data, format: c.format ?? "wav" }
2646
- };
2647
- }
2648
- default:
2649
- throw new Error("Invalid content type");
2650
- }
2651
- })
2667
+ tool_calls: toolCalls
2652
2668
  };
2653
2669
  }
2654
- return { role: "user", content: msg.content, name: msg.name };
2655
- case "assistant":
2670
+ if (!msg.content) {
2671
+ throw new Error(
2672
+ "Assistant content is required when no tool calls are provided"
2673
+ );
2674
+ }
2656
2675
  return {
2657
2676
  role: "assistant",
2658
2677
  content: msg.content,
2659
- name: msg.name,
2660
- tool_calls: msg.functionCalls?.map((v) => ({
2661
- id: v.id,
2662
- type: "function",
2663
- function: {
2664
- name: v.function.name,
2665
- arguments: typeof v.function.params === "object" ? JSON.stringify(v.function.params) : v.function.params
2666
- }
2667
- }))
2678
+ ...msg.name ? { name: msg.name } : {}
2668
2679
  };
2669
2680
  case "function":
2670
2681
  return {
@@ -8215,11 +8226,41 @@ var AxSignature = class _AxSignature {
8215
8226
  if (signature.validatedAtHash === this.sigHash) {
8216
8227
  this.validatedAtHash = this.sigHash;
8217
8228
  }
8229
+ } else if (typeof signature === "object" && signature !== null) {
8230
+ if (!("inputs" in signature) || !("outputs" in signature)) {
8231
+ throw new AxSignatureValidationError(
8232
+ "Invalid signature object: missing inputs or outputs",
8233
+ void 0,
8234
+ 'Signature object must have "inputs" and "outputs" arrays. Example: { inputs: [...], outputs: [...] }'
8235
+ );
8236
+ }
8237
+ if (!Array.isArray(signature.inputs) || !Array.isArray(signature.outputs)) {
8238
+ throw new AxSignatureValidationError(
8239
+ "Invalid signature object: inputs and outputs must be arrays",
8240
+ void 0,
8241
+ 'Both "inputs" and "outputs" must be arrays of AxField objects'
8242
+ );
8243
+ }
8244
+ try {
8245
+ this.description = signature.description;
8246
+ this.inputFields = signature.inputs.map((v) => this.parseField(v));
8247
+ this.outputFields = signature.outputs.map((v) => this.parseField(v));
8248
+ [this.sigHash, this.sigString] = this.updateHash();
8249
+ } catch (error) {
8250
+ if (error instanceof AxSignatureValidationError) {
8251
+ throw error;
8252
+ }
8253
+ throw new AxSignatureValidationError(
8254
+ `Failed to create signature from object: ${error instanceof Error ? error.message : "Unknown error"}`,
8255
+ void 0,
8256
+ "Check that all fields in inputs and outputs arrays are valid AxField objects"
8257
+ );
8258
+ }
8218
8259
  } else {
8219
8260
  throw new AxSignatureValidationError(
8220
8261
  "Invalid signature argument type",
8221
8262
  void 0,
8222
- "Signature must be a string or another AxSignature instance"
8263
+ "Signature must be a string, another AxSignature instance, or an object with inputs and outputs arrays"
8223
8264
  );
8224
8265
  }
8225
8266
  }
@@ -8262,7 +8303,7 @@ var AxSignature = class _AxSignature {
8262
8303
  }
8263
8304
  this.description = desc;
8264
8305
  this.invalidateValidationCache();
8265
- this.updateHash();
8306
+ this.updateHashLight();
8266
8307
  };
8267
8308
  addInputField = (field) => {
8268
8309
  try {
@@ -8938,11 +8979,9 @@ var AxGen = class extends AxProgramWithSignature {
8938
8979
  values = {};
8939
8980
  excludeContentFromTrace = false;
8940
8981
  thoughtFieldName;
8941
- logger;
8942
8982
  constructor(signature, options) {
8943
8983
  super(signature, { description: options?.description });
8944
8984
  this.options = options;
8945
- this.logger = options?.logger;
8946
8985
  this.thoughtFieldName = options?.thoughtFieldName ?? "thought";
8947
8986
  const promptTemplateOptions = {
8948
8987
  functions: options?.functions,
@@ -9032,6 +9071,7 @@ var AxGen = class extends AxProgramWithSignature {
9032
9071
  rateLimiter,
9033
9072
  stream,
9034
9073
  debug: false,
9074
+ // we do our own debug logging
9035
9075
  thinkingTokenBudget,
9036
9076
  showThoughts,
9037
9077
  traceContext,
@@ -9071,6 +9111,7 @@ var AxGen = class extends AxProgramWithSignature {
9071
9111
  fastFail,
9072
9112
  span
9073
9113
  });
9114
+ this.getLogger(ai, options)?.("", { tags: ["responseEnd"] });
9074
9115
  } else {
9075
9116
  yield await this.processResponse({
9076
9117
  ai,
@@ -9107,7 +9148,6 @@ var AxGen = class extends AxProgramWithSignature {
9107
9148
  mem.addResult(
9108
9149
  {
9109
9150
  content: "",
9110
- name: "initial",
9111
9151
  functionCalls: []
9112
9152
  },
9113
9153
  sessionId
@@ -9240,10 +9280,6 @@ Content: ${content}`
9240
9280
  xstate
9241
9281
  );
9242
9282
  }
9243
- if (ai.getOptions().debug) {
9244
- const logger = ai.getLogger();
9245
- logger("", { tags: ["responseEnd"] });
9246
- }
9247
9283
  }
9248
9284
  async processResponse({
9249
9285
  ai,
@@ -9315,9 +9351,11 @@ Content: ${result.content}`
9315
9351
  const stopFunction = (options?.stopFunction ?? this.options?.stopFunction)?.toLowerCase();
9316
9352
  const maxRetries = options.maxRetries ?? this.options?.maxRetries ?? 10;
9317
9353
  const maxSteps = options.maxSteps ?? this.options?.maxSteps ?? 10;
9318
- const debug = options.debug ?? ai.getOptions().debug;
9319
9354
  const debugHideSystemPrompt = options.debugHideSystemPrompt;
9320
- const memOptions = { debug, debugHideSystemPrompt };
9355
+ const memOptions = {
9356
+ debug: this.isDebug(ai, options),
9357
+ debugHideSystemPrompt
9358
+ };
9321
9359
  const mem = options.mem ?? this.options?.mem ?? new AxMemory(1e4, memOptions);
9322
9360
  let err;
9323
9361
  if (options?.functions && options.functions.length > 0) {
@@ -9371,10 +9409,7 @@ Content: ${result.content}`
9371
9409
  if (shouldContinue) {
9372
9410
  continue multiStepLoop;
9373
9411
  }
9374
- if (debug) {
9375
- const logger = options.logger ?? this.logger ?? ai.getLogger();
9376
- logger("", { tags: ["responseEnd"] });
9377
- }
9412
+ this.getLogger(ai, options)?.("", { tags: ["responseEnd"] });
9378
9413
  return;
9379
9414
  } catch (e) {
9380
9415
  let errorFields;
@@ -9516,6 +9551,12 @@ Content: ${result.content}`
9516
9551
  setExamples(examples, options) {
9517
9552
  super.setExamples(examples, options);
9518
9553
  }
9554
+ isDebug(ai, options) {
9555
+ return options?.debug ?? this.options?.debug ?? ai.getOptions().debug ?? false;
9556
+ }
9557
+ getLogger(ai, options) {
9558
+ return options?.logger ?? this.options?.logger ?? ai.getLogger();
9559
+ }
9519
9560
  };
9520
9561
  var AxGenerateError = class extends Error {
9521
9562
  details;