@ax-llm/ax 11.0.37 → 11.0.39

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.js CHANGED
@@ -754,6 +754,9 @@ var AxBaseAI = class {
754
754
  modelUsage;
755
755
  embedModelUsage;
756
756
  defaults;
757
+ lastUsedModelConfig;
758
+ lastUsedChatModel;
759
+ lastUsedEmbedModel;
757
760
  apiURL;
758
761
  name;
759
762
  id;
@@ -843,6 +846,15 @@ var AxBaseAI = class {
843
846
  getFeatures(model) {
844
847
  return typeof this.supportFor === "function" ? this.supportFor(model ?? this.defaults.model) : this.supportFor;
845
848
  }
849
+ getLastUsedChatModel() {
850
+ return this.lastUsedChatModel;
851
+ }
852
+ getLastUsedEmbedModel() {
853
+ return this.lastUsedEmbedModel;
854
+ }
855
+ getLastUsedModelConfig() {
856
+ return this.lastUsedModelConfig;
857
+ }
846
858
  // Method to calculate percentiles
847
859
  calculatePercentile(samples, percentile) {
848
860
  if (samples.length === 0) return 0;
@@ -965,6 +977,8 @@ var AxBaseAI = class {
965
977
  functions,
966
978
  modelConfig
967
979
  };
980
+ this.lastUsedChatModel = model;
981
+ this.lastUsedModelConfig = modelConfig;
968
982
  const fn = async () => {
969
983
  const [apiConfig, reqValue] = this.aiImpl.createChatReq(
970
984
  req,
@@ -1095,6 +1109,7 @@ var AxBaseAI = class {
1095
1109
  ...embedReq,
1096
1110
  embedModel
1097
1111
  };
1112
+ this.lastUsedEmbedModel = embedModel;
1098
1113
  const fn = async () => {
1099
1114
  const [apiConfig, reqValue] = this.aiImpl.createEmbedReq(req);
1100
1115
  const res2 = await apiCall(
@@ -3788,6 +3803,15 @@ var AxAI = class {
3788
3803
  getModelList() {
3789
3804
  return this.ai.getModelList();
3790
3805
  }
3806
+ getLastUsedChatModel() {
3807
+ return this.ai.getLastUsedChatModel();
3808
+ }
3809
+ getLastUsedEmbedModel() {
3810
+ return this.ai.getLastUsedEmbedModel();
3811
+ }
3812
+ getLastUsedModelConfig() {
3813
+ return this.ai.getLastUsedModelConfig();
3814
+ }
3791
3815
  getMetrics() {
3792
3816
  return this.ai.getMetrics();
3793
3817
  }
@@ -6359,7 +6383,10 @@ var AxGen = class extends AxProgramWithSignature {
6359
6383
  await assertAssertions(this.asserts, values);
6360
6384
  }
6361
6385
  if (result.finishReason === "length") {
6362
- throw new Error("Max tokens reached before completion");
6386
+ throw new Error(
6387
+ `Max tokens reached before completion
6388
+ Content: ${content}`
6389
+ );
6363
6390
  }
6364
6391
  }
6365
6392
  const funcs = parseFunctionCalls(ai, functionCalls, values, model);
@@ -6454,7 +6481,10 @@ var AxGen = class extends AxProgramWithSignature {
6454
6481
  }
6455
6482
  }
6456
6483
  if (result.finishReason === "length") {
6457
- throw new Error("Max tokens reached before completion");
6484
+ throw new Error(
6485
+ `Max tokens reached before completion
6486
+ Content: ${result.content}`
6487
+ );
6458
6488
  }
6459
6489
  }
6460
6490
  const publicValues = { ...values };
@@ -6519,7 +6549,7 @@ var AxGen = class extends AxProgramWithSignature {
6519
6549
  err = e;
6520
6550
  } else if (e instanceof AxAIServiceStreamTerminatedError) {
6521
6551
  } else {
6522
- throw e;
6552
+ throw enhanceError(e, ai, this.signature);
6523
6553
  }
6524
6554
  if (errorFields) {
6525
6555
  handleValidationError(
@@ -6532,9 +6562,17 @@ var AxGen = class extends AxProgramWithSignature {
6532
6562
  }
6533
6563
  }
6534
6564
  }
6535
- throw new Error(`Unable to fix validation error: ${err?.toString()}`);
6565
+ throw enhanceError(
6566
+ new Error(`Unable to fix validation error: ${err?.toString()}`),
6567
+ ai,
6568
+ this.signature
6569
+ );
6536
6570
  }
6537
- throw new Error(`Max steps reached: ${maxSteps}`);
6571
+ throw enhanceError(
6572
+ new Error(`Max steps reached: ${maxSteps}`),
6573
+ ai,
6574
+ this.signature
6575
+ );
6538
6576
  }
6539
6577
  shouldContinueSteps(lastMemItem, stopFunction) {
6540
6578
  const stopFunctionExecuted = stopFunction && this.functionsExecuted.has(stopFunction);
@@ -6605,6 +6643,22 @@ var AxGen = class extends AxProgramWithSignature {
6605
6643
  });
6606
6644
  }
6607
6645
  };
6646
+ function enhanceError(e, ai, signature) {
6647
+ const originalError = e instanceof Error ? e : new Error(String(e));
6648
+ const model = ai.getLastUsedChatModel();
6649
+ const modelConfig = ai.getLastUsedModelConfig();
6650
+ const details = [
6651
+ `name=${ai.getName()}`,
6652
+ `model=${model}`,
6653
+ `maxTokens=${modelConfig?.maxTokens ?? "N/A"}`,
6654
+ `streaming=${modelConfig?.stream ?? false}`
6655
+ ].join(", ");
6656
+ return new Error(
6657
+ `Generate Failed:${signature.toString()}
6658
+ Details: ${details}`,
6659
+ { cause: originalError }
6660
+ );
6661
+ }
6608
6662
 
6609
6663
  // prompts/agent.ts
6610
6664
  function processChildAgentFunction(childFunction, parentValues, parentInputKeys, modelList, options) {
@@ -6985,6 +7039,15 @@ var AxBalancer = class _AxBalancer {
6985
7039
  this.maxBackoffMs = options?.maxBackoffMs ?? 32e3;
6986
7040
  this.maxRetries = options?.maxRetries ?? 3;
6987
7041
  }
7042
+ getLastUsedChatModel() {
7043
+ return this.currentService.getLastUsedChatModel();
7044
+ }
7045
+ getLastUsedEmbedModel() {
7046
+ return this.currentService.getLastUsedEmbedModel();
7047
+ }
7048
+ getLastUsedModelConfig() {
7049
+ return this.currentService.getLastUsedModelConfig();
7050
+ }
6988
7051
  /**
6989
7052
  * Service comparator that respects the input order of services.
6990
7053
  */
@@ -9149,6 +9212,15 @@ var AxMockAIService = class {
9149
9212
  embed: { count: 0, rate: 0, total: 0 }
9150
9213
  }
9151
9214
  };
9215
+ getLastUsedChatModel() {
9216
+ throw new Error("Method not implemented.");
9217
+ }
9218
+ getLastUsedEmbedModel() {
9219
+ throw new Error("Method not implemented.");
9220
+ }
9221
+ getLastUsedModelConfig() {
9222
+ throw new Error("Method not implemented.");
9223
+ }
9152
9224
  getName() {
9153
9225
  return this.config.name ?? "mock-ai-service";
9154
9226
  }
@@ -11058,6 +11130,7 @@ var AxMCPStdioTransport = class {
11058
11130
  // ai/multiservice.ts
11059
11131
  var AxMultiServiceRouter = class {
11060
11132
  options;
11133
+ lastUsedService;
11061
11134
  services = /* @__PURE__ */ new Map();
11062
11135
  /**
11063
11136
  * Constructs a new multi-service router.
@@ -11116,6 +11189,15 @@ var AxMultiServiceRouter = class {
11116
11189
  }
11117
11190
  }
11118
11191
  }
11192
+ getLastUsedChatModel() {
11193
+ return this.lastUsedService?.getLastUsedChatModel();
11194
+ }
11195
+ getLastUsedEmbedModel() {
11196
+ return this.lastUsedService?.getLastUsedEmbedModel();
11197
+ }
11198
+ getLastUsedModelConfig() {
11199
+ return this.lastUsedService?.getLastUsedModelConfig();
11200
+ }
11119
11201
  /**
11120
11202
  * Delegates the chat call to the service matching the provided model key.
11121
11203
  */
@@ -11128,6 +11210,7 @@ var AxMultiServiceRouter = class {
11128
11210
  if (!item) {
11129
11211
  throw new Error(`No service found for model key: ${modelKey}`);
11130
11212
  }
11213
+ this.lastUsedService = item.service;
11131
11214
  if (!item.model) {
11132
11215
  const { model, ...reqWithoutModel } = req;
11133
11216
  return await item.service.chat(reqWithoutModel, options);
@@ -11146,6 +11229,7 @@ var AxMultiServiceRouter = class {
11146
11229
  if (!item) {
11147
11230
  throw new Error(`No service found for embed model key: ${embedModelKey}`);
11148
11231
  }
11232
+ this.lastUsedService = item.service;
11149
11233
  if (!item.model) {
11150
11234
  const { embedModel, ...reqWithoutEmbedModel } = req;
11151
11235
  return await item.service.embed(reqWithoutEmbedModel, options);
@@ -11200,11 +11284,17 @@ var AxMultiServiceRouter = class {
11200
11284
  * or falls back to the first service if none has been used.
11201
11285
  */
11202
11286
  getMetrics() {
11203
- const service = this.services.values().next().value;
11204
- if (!service) {
11287
+ let serviceInstance = this.lastUsedService;
11288
+ if (!serviceInstance) {
11289
+ const firstServiceEntry = this.services.values().next().value;
11290
+ if (firstServiceEntry) {
11291
+ serviceInstance = "service" in firstServiceEntry ? firstServiceEntry.service : firstServiceEntry;
11292
+ }
11293
+ }
11294
+ if (!serviceInstance) {
11205
11295
  throw new Error("No service available to get metrics.");
11206
11296
  }
11207
- return service.service.getMetrics();
11297
+ return serviceInstance.getMetrics();
11208
11298
  }
11209
11299
  /**
11210
11300
  * Sets options on all underlying services.