@ax-llm/ax 11.0.36 → 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
@@ -886,7 +886,7 @@ var AxBaseAI = class {
886
886
  this.models = models;
887
887
  this.id = crypto.randomUUID();
888
888
  const model = this.getModel(defaults.model) ?? defaults.model;
889
- const embedModel = this.getEmbedModel(defaults.embedModel);
889
+ const embedModel = this.getEmbedModel(defaults.embedModel) ?? defaults.embedModel;
890
890
  this.defaults = { model, embedModel };
891
891
  if (!defaults.model || typeof defaults.model !== "string" || defaults.model === "") {
892
892
  throw new Error("No model defined");
@@ -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(
@@ -1273,13 +1288,7 @@ var AxBaseAI = class {
1273
1288
  }
1274
1289
  this.embedModelUsage = res.modelUsage;
1275
1290
  if (span?.isRecording()) {
1276
- if (res.modelUsage) {
1277
- this.embedModelUsage = res.modelUsage;
1278
- span.setAttributes({
1279
- [axSpanAttributes.LLM_USAGE_COMPLETION_TOKENS]: res.modelUsage.tokens?.completionTokens ?? 0,
1280
- [axSpanAttributes.LLM_USAGE_PROMPT_TOKENS]: res.modelUsage.tokens?.promptTokens
1281
- });
1282
- }
1291
+ setResponseAttr(res, span);
1283
1292
  }
1284
1293
  span?.end();
1285
1294
  return res;
@@ -3945,6 +3954,15 @@ var AxAI = class {
3945
3954
  getModelList() {
3946
3955
  return this.ai.getModelList();
3947
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
+ }
3948
3966
  getMetrics() {
3949
3967
  return this.ai.getMetrics();
3950
3968
  }
@@ -6516,7 +6534,10 @@ var AxGen = class extends AxProgramWithSignature {
6516
6534
  await assertAssertions(this.asserts, values);
6517
6535
  }
6518
6536
  if (result.finishReason === "length") {
6519
- throw new Error("Max tokens reached before completion");
6537
+ throw new Error(
6538
+ `Max tokens reached before completion
6539
+ Content: ${content}`
6540
+ );
6520
6541
  }
6521
6542
  }
6522
6543
  const funcs = parseFunctionCalls(ai, functionCalls, values, model);
@@ -6611,7 +6632,10 @@ var AxGen = class extends AxProgramWithSignature {
6611
6632
  }
6612
6633
  }
6613
6634
  if (result.finishReason === "length") {
6614
- throw new Error("Max tokens reached before completion");
6635
+ throw new Error(
6636
+ `Max tokens reached before completion
6637
+ Content: ${result.content}`
6638
+ );
6615
6639
  }
6616
6640
  }
6617
6641
  const publicValues = { ...values };
@@ -6676,7 +6700,7 @@ var AxGen = class extends AxProgramWithSignature {
6676
6700
  err = e;
6677
6701
  } else if (e instanceof AxAIServiceStreamTerminatedError) {
6678
6702
  } else {
6679
- throw e;
6703
+ throw enhanceError(e, ai);
6680
6704
  }
6681
6705
  if (errorFields) {
6682
6706
  handleValidationError(
@@ -6691,7 +6715,7 @@ var AxGen = class extends AxProgramWithSignature {
6691
6715
  }
6692
6716
  throw new Error(`Unable to fix validation error: ${err?.toString()}`);
6693
6717
  }
6694
- throw new Error(`Max steps reached: ${maxSteps}`);
6718
+ throw enhanceError(new Error(`Max steps reached: ${maxSteps}`), ai);
6695
6719
  }
6696
6720
  shouldContinueSteps(lastMemItem, stopFunction) {
6697
6721
  const stopFunctionExecuted = stopFunction && this.functionsExecuted.has(stopFunction);
@@ -6762,6 +6786,20 @@ var AxGen = class extends AxProgramWithSignature {
6762
6786
  });
6763
6787
  }
6764
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
+ }
6765
6803
 
6766
6804
  // prompts/agent.ts
6767
6805
  function processChildAgentFunction(childFunction, parentValues, parentInputKeys, modelList, options) {
@@ -7142,6 +7180,15 @@ var AxBalancer = class _AxBalancer {
7142
7180
  this.maxBackoffMs = options?.maxBackoffMs ?? 32e3;
7143
7181
  this.maxRetries = options?.maxRetries ?? 3;
7144
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
+ }
7145
7192
  /**
7146
7193
  * Service comparator that respects the input order of services.
7147
7194
  */
@@ -9306,6 +9353,15 @@ var AxMockAIService = class {
9306
9353
  embed: { count: 0, rate: 0, total: 0 }
9307
9354
  }
9308
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
+ }
9309
9365
  getName() {
9310
9366
  return this.config.name ?? "mock-ai-service";
9311
9367
  }
@@ -11215,6 +11271,7 @@ var AxMCPStdioTransport = class {
11215
11271
  // ai/multiservice.ts
11216
11272
  var AxMultiServiceRouter = class {
11217
11273
  options;
11274
+ lastUsedService;
11218
11275
  services = /* @__PURE__ */ new Map();
11219
11276
  /**
11220
11277
  * Constructs a new multi-service router.
@@ -11273,6 +11330,15 @@ var AxMultiServiceRouter = class {
11273
11330
  }
11274
11331
  }
11275
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
+ }
11276
11342
  /**
11277
11343
  * Delegates the chat call to the service matching the provided model key.
11278
11344
  */
@@ -11285,6 +11351,7 @@ var AxMultiServiceRouter = class {
11285
11351
  if (!item) {
11286
11352
  throw new Error(`No service found for model key: ${modelKey}`);
11287
11353
  }
11354
+ this.lastUsedService = item.service;
11288
11355
  if (!item.model) {
11289
11356
  const { model, ...reqWithoutModel } = req;
11290
11357
  return await item.service.chat(reqWithoutModel, options);
@@ -11303,6 +11370,7 @@ var AxMultiServiceRouter = class {
11303
11370
  if (!item) {
11304
11371
  throw new Error(`No service found for embed model key: ${embedModelKey}`);
11305
11372
  }
11373
+ this.lastUsedService = item.service;
11306
11374
  if (!item.model) {
11307
11375
  const { embedModel, ...reqWithoutEmbedModel } = req;
11308
11376
  return await item.service.embed(reqWithoutEmbedModel, options);
@@ -11357,11 +11425,17 @@ var AxMultiServiceRouter = class {
11357
11425
  * or falls back to the first service if none has been used.
11358
11426
  */
11359
11427
  getMetrics() {
11360
- const service = this.services.values().next().value;
11361
- 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) {
11362
11436
  throw new Error("No service available to get metrics.");
11363
11437
  }
11364
- return service.service.getMetrics();
11438
+ return serviceInstance.getMetrics();
11365
11439
  }
11366
11440
  /**
11367
11441
  * Sets options on all underlying services.