@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.js CHANGED
@@ -735,7 +735,7 @@ var AxBaseAI = class {
735
735
  this.models = models;
736
736
  this.id = crypto.randomUUID();
737
737
  const model = this.getModel(defaults.model) ?? defaults.model;
738
- const embedModel = this.getEmbedModel(defaults.embedModel);
738
+ const embedModel = this.getEmbedModel(defaults.embedModel) ?? defaults.embedModel;
739
739
  this.defaults = { model, embedModel };
740
740
  if (!defaults.model || typeof defaults.model !== "string" || defaults.model === "") {
741
741
  throw new Error("No model defined");
@@ -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(
@@ -1122,13 +1137,7 @@ var AxBaseAI = class {
1122
1137
  }
1123
1138
  this.embedModelUsage = res.modelUsage;
1124
1139
  if (span?.isRecording()) {
1125
- if (res.modelUsage) {
1126
- this.embedModelUsage = res.modelUsage;
1127
- span.setAttributes({
1128
- [axSpanAttributes.LLM_USAGE_COMPLETION_TOKENS]: res.modelUsage.tokens?.completionTokens ?? 0,
1129
- [axSpanAttributes.LLM_USAGE_PROMPT_TOKENS]: res.modelUsage.tokens?.promptTokens
1130
- });
1131
- }
1140
+ setResponseAttr(res, span);
1132
1141
  }
1133
1142
  span?.end();
1134
1143
  return res;
@@ -3794,6 +3803,15 @@ var AxAI = class {
3794
3803
  getModelList() {
3795
3804
  return this.ai.getModelList();
3796
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
+ }
3797
3815
  getMetrics() {
3798
3816
  return this.ai.getMetrics();
3799
3817
  }
@@ -6365,7 +6383,10 @@ var AxGen = class extends AxProgramWithSignature {
6365
6383
  await assertAssertions(this.asserts, values);
6366
6384
  }
6367
6385
  if (result.finishReason === "length") {
6368
- throw new Error("Max tokens reached before completion");
6386
+ throw new Error(
6387
+ `Max tokens reached before completion
6388
+ Content: ${content}`
6389
+ );
6369
6390
  }
6370
6391
  }
6371
6392
  const funcs = parseFunctionCalls(ai, functionCalls, values, model);
@@ -6460,7 +6481,10 @@ var AxGen = class extends AxProgramWithSignature {
6460
6481
  }
6461
6482
  }
6462
6483
  if (result.finishReason === "length") {
6463
- throw new Error("Max tokens reached before completion");
6484
+ throw new Error(
6485
+ `Max tokens reached before completion
6486
+ Content: ${result.content}`
6487
+ );
6464
6488
  }
6465
6489
  }
6466
6490
  const publicValues = { ...values };
@@ -6525,7 +6549,7 @@ var AxGen = class extends AxProgramWithSignature {
6525
6549
  err = e;
6526
6550
  } else if (e instanceof AxAIServiceStreamTerminatedError) {
6527
6551
  } else {
6528
- throw e;
6552
+ throw enhanceError(e, ai);
6529
6553
  }
6530
6554
  if (errorFields) {
6531
6555
  handleValidationError(
@@ -6540,7 +6564,7 @@ var AxGen = class extends AxProgramWithSignature {
6540
6564
  }
6541
6565
  throw new Error(`Unable to fix validation error: ${err?.toString()}`);
6542
6566
  }
6543
- throw new Error(`Max steps reached: ${maxSteps}`);
6567
+ throw enhanceError(new Error(`Max steps reached: ${maxSteps}`), ai);
6544
6568
  }
6545
6569
  shouldContinueSteps(lastMemItem, stopFunction) {
6546
6570
  const stopFunctionExecuted = stopFunction && this.functionsExecuted.has(stopFunction);
@@ -6611,6 +6635,20 @@ var AxGen = class extends AxProgramWithSignature {
6611
6635
  });
6612
6636
  }
6613
6637
  };
6638
+ function enhanceError(e, ai) {
6639
+ const originalError = e instanceof Error ? e : new Error(String(e));
6640
+ const model = ai.getLastUsedChatModel();
6641
+ const modelConfig = ai.getLastUsedModelConfig();
6642
+ const details = [
6643
+ `model=${model}`,
6644
+ `maxTokens=${modelConfig?.maxTokens ?? "N/A"}`,
6645
+ `streaming=${modelConfig?.stream ?? false}`
6646
+ ].join(", ");
6647
+ const enhancedError = new Error();
6648
+ enhancedError.stack = `Generate Failed: ${details}
6649
+ ${originalError.stack}`;
6650
+ return enhancedError;
6651
+ }
6614
6652
 
6615
6653
  // prompts/agent.ts
6616
6654
  function processChildAgentFunction(childFunction, parentValues, parentInputKeys, modelList, options) {
@@ -6991,6 +7029,15 @@ var AxBalancer = class _AxBalancer {
6991
7029
  this.maxBackoffMs = options?.maxBackoffMs ?? 32e3;
6992
7030
  this.maxRetries = options?.maxRetries ?? 3;
6993
7031
  }
7032
+ getLastUsedChatModel() {
7033
+ return this.currentService.getLastUsedChatModel();
7034
+ }
7035
+ getLastUsedEmbedModel() {
7036
+ return this.currentService.getLastUsedEmbedModel();
7037
+ }
7038
+ getLastUsedModelConfig() {
7039
+ return this.currentService.getLastUsedModelConfig();
7040
+ }
6994
7041
  /**
6995
7042
  * Service comparator that respects the input order of services.
6996
7043
  */
@@ -9155,6 +9202,15 @@ var AxMockAIService = class {
9155
9202
  embed: { count: 0, rate: 0, total: 0 }
9156
9203
  }
9157
9204
  };
9205
+ getLastUsedChatModel() {
9206
+ throw new Error("Method not implemented.");
9207
+ }
9208
+ getLastUsedEmbedModel() {
9209
+ throw new Error("Method not implemented.");
9210
+ }
9211
+ getLastUsedModelConfig() {
9212
+ throw new Error("Method not implemented.");
9213
+ }
9158
9214
  getName() {
9159
9215
  return this.config.name ?? "mock-ai-service";
9160
9216
  }
@@ -11064,6 +11120,7 @@ var AxMCPStdioTransport = class {
11064
11120
  // ai/multiservice.ts
11065
11121
  var AxMultiServiceRouter = class {
11066
11122
  options;
11123
+ lastUsedService;
11067
11124
  services = /* @__PURE__ */ new Map();
11068
11125
  /**
11069
11126
  * Constructs a new multi-service router.
@@ -11122,6 +11179,15 @@ var AxMultiServiceRouter = class {
11122
11179
  }
11123
11180
  }
11124
11181
  }
11182
+ getLastUsedChatModel() {
11183
+ return this.lastUsedService?.getLastUsedChatModel();
11184
+ }
11185
+ getLastUsedEmbedModel() {
11186
+ return this.lastUsedService?.getLastUsedEmbedModel();
11187
+ }
11188
+ getLastUsedModelConfig() {
11189
+ return this.lastUsedService?.getLastUsedModelConfig();
11190
+ }
11125
11191
  /**
11126
11192
  * Delegates the chat call to the service matching the provided model key.
11127
11193
  */
@@ -11134,6 +11200,7 @@ var AxMultiServiceRouter = class {
11134
11200
  if (!item) {
11135
11201
  throw new Error(`No service found for model key: ${modelKey}`);
11136
11202
  }
11203
+ this.lastUsedService = item.service;
11137
11204
  if (!item.model) {
11138
11205
  const { model, ...reqWithoutModel } = req;
11139
11206
  return await item.service.chat(reqWithoutModel, options);
@@ -11152,6 +11219,7 @@ var AxMultiServiceRouter = class {
11152
11219
  if (!item) {
11153
11220
  throw new Error(`No service found for embed model key: ${embedModelKey}`);
11154
11221
  }
11222
+ this.lastUsedService = item.service;
11155
11223
  if (!item.model) {
11156
11224
  const { embedModel, ...reqWithoutEmbedModel } = req;
11157
11225
  return await item.service.embed(reqWithoutEmbedModel, options);
@@ -11206,11 +11274,17 @@ var AxMultiServiceRouter = class {
11206
11274
  * or falls back to the first service if none has been used.
11207
11275
  */
11208
11276
  getMetrics() {
11209
- const service = this.services.values().next().value;
11210
- if (!service) {
11277
+ let serviceInstance = this.lastUsedService;
11278
+ if (!serviceInstance) {
11279
+ const firstServiceEntry = this.services.values().next().value;
11280
+ if (firstServiceEntry) {
11281
+ serviceInstance = "service" in firstServiceEntry ? firstServiceEntry.service : firstServiceEntry;
11282
+ }
11283
+ }
11284
+ if (!serviceInstance) {
11211
11285
  throw new Error("No service available to get metrics.");
11212
11286
  }
11213
- return service.service.getMetrics();
11287
+ return serviceInstance.getMetrics();
11214
11288
  }
11215
11289
  /**
11216
11290
  * Sets options on all underlying services.