@ax-llm/ax 11.0.37 → 11.0.38

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
@@ -905,6 +905,9 @@ var AxBaseAI = class {
905
905
  modelUsage;
906
906
  embedModelUsage;
907
907
  defaults;
908
+ lastUsedModelConfig;
909
+ lastUsedChatModel;
910
+ lastUsedEmbedModel;
908
911
  apiURL;
909
912
  name;
910
913
  id;
@@ -994,6 +997,15 @@ var AxBaseAI = class {
994
997
  getFeatures(model) {
995
998
  return typeof this.supportFor === "function" ? this.supportFor(model ?? this.defaults.model) : this.supportFor;
996
999
  }
1000
+ getLastUsedChatModel() {
1001
+ return this.lastUsedChatModel;
1002
+ }
1003
+ getLastUsedEmbedModel() {
1004
+ return this.lastUsedEmbedModel;
1005
+ }
1006
+ getLastUsedModelConfig() {
1007
+ return this.lastUsedModelConfig;
1008
+ }
997
1009
  // Method to calculate percentiles
998
1010
  calculatePercentile(samples, percentile) {
999
1011
  if (samples.length === 0) return 0;
@@ -1116,6 +1128,8 @@ var AxBaseAI = class {
1116
1128
  functions,
1117
1129
  modelConfig
1118
1130
  };
1131
+ this.lastUsedChatModel = model;
1132
+ this.lastUsedModelConfig = modelConfig;
1119
1133
  const fn = async () => {
1120
1134
  const [apiConfig, reqValue] = this.aiImpl.createChatReq(
1121
1135
  req,
@@ -1246,6 +1260,7 @@ var AxBaseAI = class {
1246
1260
  ...embedReq,
1247
1261
  embedModel
1248
1262
  };
1263
+ this.lastUsedEmbedModel = embedModel;
1249
1264
  const fn = async () => {
1250
1265
  const [apiConfig, reqValue] = this.aiImpl.createEmbedReq(req);
1251
1266
  const res2 = await apiCall(
@@ -3939,6 +3954,15 @@ var AxAI = class {
3939
3954
  getModelList() {
3940
3955
  return this.ai.getModelList();
3941
3956
  }
3957
+ getLastUsedChatModel() {
3958
+ return this.ai.getLastUsedChatModel();
3959
+ }
3960
+ getLastUsedEmbedModel() {
3961
+ return this.ai.getLastUsedEmbedModel();
3962
+ }
3963
+ getLastUsedModelConfig() {
3964
+ return this.ai.getLastUsedModelConfig();
3965
+ }
3942
3966
  getMetrics() {
3943
3967
  return this.ai.getMetrics();
3944
3968
  }
@@ -6510,7 +6534,10 @@ var AxGen = class extends AxProgramWithSignature {
6510
6534
  await assertAssertions(this.asserts, values);
6511
6535
  }
6512
6536
  if (result.finishReason === "length") {
6513
- throw new Error("Max tokens reached before completion");
6537
+ throw new Error(
6538
+ `Max tokens reached before completion
6539
+ Content: ${content}`
6540
+ );
6514
6541
  }
6515
6542
  }
6516
6543
  const funcs = parseFunctionCalls(ai, functionCalls, values, model);
@@ -6605,7 +6632,10 @@ var AxGen = class extends AxProgramWithSignature {
6605
6632
  }
6606
6633
  }
6607
6634
  if (result.finishReason === "length") {
6608
- throw new Error("Max tokens reached before completion");
6635
+ throw new Error(
6636
+ `Max tokens reached before completion
6637
+ Content: ${result.content}`
6638
+ );
6609
6639
  }
6610
6640
  }
6611
6641
  const publicValues = { ...values };
@@ -6670,7 +6700,7 @@ var AxGen = class extends AxProgramWithSignature {
6670
6700
  err = e;
6671
6701
  } else if (e instanceof AxAIServiceStreamTerminatedError) {
6672
6702
  } else {
6673
- throw e;
6703
+ throw enhanceError(e, ai);
6674
6704
  }
6675
6705
  if (errorFields) {
6676
6706
  handleValidationError(
@@ -6685,7 +6715,7 @@ var AxGen = class extends AxProgramWithSignature {
6685
6715
  }
6686
6716
  throw new Error(`Unable to fix validation error: ${err?.toString()}`);
6687
6717
  }
6688
- throw new Error(`Max steps reached: ${maxSteps}`);
6718
+ throw enhanceError(new Error(`Max steps reached: ${maxSteps}`), ai);
6689
6719
  }
6690
6720
  shouldContinueSteps(lastMemItem, stopFunction) {
6691
6721
  const stopFunctionExecuted = stopFunction && this.functionsExecuted.has(stopFunction);
@@ -6756,6 +6786,20 @@ var AxGen = class extends AxProgramWithSignature {
6756
6786
  });
6757
6787
  }
6758
6788
  };
6789
+ function enhanceError(e, ai) {
6790
+ const originalError = e instanceof Error ? e : new Error(String(e));
6791
+ const model = ai.getLastUsedChatModel();
6792
+ const modelConfig = ai.getLastUsedModelConfig();
6793
+ const details = [
6794
+ `model=${model}`,
6795
+ `maxTokens=${modelConfig?.maxTokens ?? "N/A"}`,
6796
+ `streaming=${modelConfig?.stream ?? false}`
6797
+ ].join(", ");
6798
+ const enhancedError = new Error();
6799
+ enhancedError.stack = `Generate Failed: ${details}
6800
+ ${originalError.stack}`;
6801
+ return enhancedError;
6802
+ }
6759
6803
 
6760
6804
  // prompts/agent.ts
6761
6805
  function processChildAgentFunction(childFunction, parentValues, parentInputKeys, modelList, options) {
@@ -7136,6 +7180,15 @@ var AxBalancer = class _AxBalancer {
7136
7180
  this.maxBackoffMs = options?.maxBackoffMs ?? 32e3;
7137
7181
  this.maxRetries = options?.maxRetries ?? 3;
7138
7182
  }
7183
+ getLastUsedChatModel() {
7184
+ return this.currentService.getLastUsedChatModel();
7185
+ }
7186
+ getLastUsedEmbedModel() {
7187
+ return this.currentService.getLastUsedEmbedModel();
7188
+ }
7189
+ getLastUsedModelConfig() {
7190
+ return this.currentService.getLastUsedModelConfig();
7191
+ }
7139
7192
  /**
7140
7193
  * Service comparator that respects the input order of services.
7141
7194
  */
@@ -9300,6 +9353,15 @@ var AxMockAIService = class {
9300
9353
  embed: { count: 0, rate: 0, total: 0 }
9301
9354
  }
9302
9355
  };
9356
+ getLastUsedChatModel() {
9357
+ throw new Error("Method not implemented.");
9358
+ }
9359
+ getLastUsedEmbedModel() {
9360
+ throw new Error("Method not implemented.");
9361
+ }
9362
+ getLastUsedModelConfig() {
9363
+ throw new Error("Method not implemented.");
9364
+ }
9303
9365
  getName() {
9304
9366
  return this.config.name ?? "mock-ai-service";
9305
9367
  }
@@ -11209,6 +11271,7 @@ var AxMCPStdioTransport = class {
11209
11271
  // ai/multiservice.ts
11210
11272
  var AxMultiServiceRouter = class {
11211
11273
  options;
11274
+ lastUsedService;
11212
11275
  services = /* @__PURE__ */ new Map();
11213
11276
  /**
11214
11277
  * Constructs a new multi-service router.
@@ -11267,6 +11330,15 @@ var AxMultiServiceRouter = class {
11267
11330
  }
11268
11331
  }
11269
11332
  }
11333
+ getLastUsedChatModel() {
11334
+ return this.lastUsedService?.getLastUsedChatModel();
11335
+ }
11336
+ getLastUsedEmbedModel() {
11337
+ return this.lastUsedService?.getLastUsedEmbedModel();
11338
+ }
11339
+ getLastUsedModelConfig() {
11340
+ return this.lastUsedService?.getLastUsedModelConfig();
11341
+ }
11270
11342
  /**
11271
11343
  * Delegates the chat call to the service matching the provided model key.
11272
11344
  */
@@ -11279,6 +11351,7 @@ var AxMultiServiceRouter = class {
11279
11351
  if (!item) {
11280
11352
  throw new Error(`No service found for model key: ${modelKey}`);
11281
11353
  }
11354
+ this.lastUsedService = item.service;
11282
11355
  if (!item.model) {
11283
11356
  const { model, ...reqWithoutModel } = req;
11284
11357
  return await item.service.chat(reqWithoutModel, options);
@@ -11297,6 +11370,7 @@ var AxMultiServiceRouter = class {
11297
11370
  if (!item) {
11298
11371
  throw new Error(`No service found for embed model key: ${embedModelKey}`);
11299
11372
  }
11373
+ this.lastUsedService = item.service;
11300
11374
  if (!item.model) {
11301
11375
  const { embedModel, ...reqWithoutEmbedModel } = req;
11302
11376
  return await item.service.embed(reqWithoutEmbedModel, options);
@@ -11351,11 +11425,17 @@ var AxMultiServiceRouter = class {
11351
11425
  * or falls back to the first service if none has been used.
11352
11426
  */
11353
11427
  getMetrics() {
11354
- const service = this.services.values().next().value;
11355
- if (!service) {
11428
+ let serviceInstance = this.lastUsedService;
11429
+ if (!serviceInstance) {
11430
+ const firstServiceEntry = this.services.values().next().value;
11431
+ if (firstServiceEntry) {
11432
+ serviceInstance = "service" in firstServiceEntry ? firstServiceEntry.service : firstServiceEntry;
11433
+ }
11434
+ }
11435
+ if (!serviceInstance) {
11356
11436
  throw new Error("No service available to get metrics.");
11357
11437
  }
11358
- return service.service.getMetrics();
11438
+ return serviceInstance.getMetrics();
11359
11439
  }
11360
11440
  /**
11361
11441
  * Sets options on all underlying services.