@ax-llm/ax 11.0.33 → 11.0.35

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
@@ -734,8 +734,8 @@ var AxBaseAI = class {
734
734
  this.modelInfo = modelInfo;
735
735
  this.models = models;
736
736
  this.id = crypto.randomUUID();
737
- const model = this.models?.find((v) => v.key === defaults.model)?.model ?? defaults.model;
738
- const embedModel = defaults.embedModel;
737
+ const model = this.getModel(defaults.model) ?? defaults.model;
738
+ const embedModel = this.getEmbedModel(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");
@@ -801,18 +801,10 @@ var AxBaseAI = class {
801
801
  this.headers = headers;
802
802
  }
803
803
  setOptions(options) {
804
- if (options.debug) {
805
- this.debug = options.debug;
806
- }
807
- if (options.rateLimiter) {
808
- this.rt = options.rateLimiter;
809
- }
810
- if (options.fetch) {
811
- this.fetch = options.fetch;
812
- }
813
- if (options.tracer) {
814
- this.tracer = options.tracer;
815
- }
804
+ this.debug = options.debug ?? false;
805
+ this.rt = options.rateLimiter;
806
+ this.fetch = options.fetch;
807
+ this.tracer = options.tracer;
816
808
  }
817
809
  getOptions() {
818
810
  return {
@@ -823,11 +815,27 @@ var AxBaseAI = class {
823
815
  };
824
816
  }
825
817
  getModelList() {
826
- return this.models?.filter((model) => !model.isInternal)?.map((model) => ({
827
- key: model.key,
828
- description: model.description,
829
- model: model.model
830
- }));
818
+ const models = [];
819
+ for (const model of this.models ?? []) {
820
+ if (model.isInternal) {
821
+ continue;
822
+ }
823
+ if ("model" in model && model.model) {
824
+ models.push({
825
+ key: model.key,
826
+ description: model.description,
827
+ model: model.model
828
+ });
829
+ }
830
+ if ("embedModel" in model && model.embedModel) {
831
+ models.push({
832
+ key: model.key,
833
+ description: model.description,
834
+ embedModel: model.embedModel
835
+ });
836
+ }
837
+ }
838
+ return models;
831
839
  }
832
840
  getDefaultModels() {
833
841
  return {
@@ -888,7 +896,7 @@ var AxBaseAI = class {
888
896
  }
889
897
  }
890
898
  async _chat1(req, options) {
891
- const model = req.model ? this.models?.find((v) => v.key === req.model)?.model ?? req.model : this.defaults.model;
899
+ const model = this.getModel(req.model) ?? req.model ?? this.defaults.model;
892
900
  const modelConfig = {
893
901
  ...this.aiImpl.getModelConfig(),
894
902
  ...req.modelConfig
@@ -952,6 +960,7 @@ var AxBaseAI = class {
952
960
  if (!this.aiImpl.createChatReq) {
953
961
  throw new Error("generateChatReq not implemented");
954
962
  }
963
+ const debug = options?.debug ?? this.debug;
955
964
  let functions;
956
965
  if (chatReq.functions && chatReq.functions.length > 0) {
957
966
  functions = chatReq.functions.map((fn2) => this.cleanupFunctionSchema(fn2));
@@ -973,7 +982,7 @@ var AxBaseAI = class {
973
982
  url: this.apiURL,
974
983
  headers: await this.buildHeaders(apiConfig.headers),
975
984
  stream: modelConfig.stream,
976
- debug: this.debug,
985
+ debug,
977
986
  fetch: this.fetch,
978
987
  span
979
988
  },
@@ -981,7 +990,7 @@ var AxBaseAI = class {
981
990
  );
982
991
  return res2;
983
992
  };
984
- if (options?.debug ?? this.debug) {
993
+ if (debug) {
985
994
  logChatRequest(req.chatPrompt, options?.debugHideSystemPrompt);
986
995
  }
987
996
  const rt = options?.rateLimiter ?? this.rt;
@@ -1000,13 +1009,13 @@ var AxBaseAI = class {
1000
1009
  if (span?.isRecording()) {
1001
1010
  setResponseAttr(res2, span);
1002
1011
  }
1003
- if (options?.debug ?? this.debug) {
1012
+ if (debug) {
1004
1013
  logResponse(res2);
1005
1014
  }
1006
1015
  return res2;
1007
1016
  };
1008
1017
  const doneCb = async (_values) => {
1009
- if (options?.debug ?? this.debug) {
1018
+ if (debug) {
1010
1019
  process.stdout.write("\n");
1011
1020
  }
1012
1021
  };
@@ -1029,7 +1038,7 @@ var AxBaseAI = class {
1029
1038
  if (span?.isRecording()) {
1030
1039
  setResponseAttr(res, span);
1031
1040
  }
1032
- if (options?.debug ?? this.debug) {
1041
+ if (debug) {
1033
1042
  logResponse(res);
1034
1043
  }
1035
1044
  span?.end();
@@ -1050,7 +1059,7 @@ var AxBaseAI = class {
1050
1059
  }
1051
1060
  }
1052
1061
  async _embed1(req, options) {
1053
- const embedModel = req.embedModel ?? this.defaults.embedModel;
1062
+ const embedModel = this.getEmbedModel(req.embedModel) ?? req.embedModel ?? this.defaults.embedModel;
1054
1063
  if (!embedModel) {
1055
1064
  throw new Error("No embed model defined");
1056
1065
  }
@@ -1082,6 +1091,7 @@ var AxBaseAI = class {
1082
1091
  if (!this.aiImpl.createEmbedResp) {
1083
1092
  throw new Error("generateEmbedResp not implemented");
1084
1093
  }
1094
+ const debug = options?.debug ?? this.debug;
1085
1095
  const req = {
1086
1096
  ...embedReq,
1087
1097
  embedModel
@@ -1093,7 +1103,7 @@ var AxBaseAI = class {
1093
1103
  name: apiConfig.name,
1094
1104
  url: this.apiURL,
1095
1105
  headers: await this.buildHeaders(apiConfig.headers),
1096
- debug: this.debug,
1106
+ debug,
1097
1107
  fetch: this.fetch,
1098
1108
  span
1099
1109
  },
@@ -1119,6 +1129,21 @@ var AxBaseAI = class {
1119
1129
  async buildHeaders(headers = {}) {
1120
1130
  return { ...headers, ...await this.headers() };
1121
1131
  }
1132
+ getModelByKey(modelName) {
1133
+ if (!modelName) {
1134
+ return void 0;
1135
+ }
1136
+ const item = this.models?.find((v) => v.key === modelName);
1137
+ return item;
1138
+ }
1139
+ getModel(modelName) {
1140
+ const item = this.getModelByKey(modelName);
1141
+ return item && "model" in item ? item.model : void 0;
1142
+ }
1143
+ getEmbedModel(modelName) {
1144
+ const item = this.getModelByKey(modelName);
1145
+ return item && "embedModel" in item ? item.embedModel : void 0;
1146
+ }
1122
1147
  };
1123
1148
  function setResponseAttr(res, span) {
1124
1149
  if (res.modelUsage) {
@@ -1645,10 +1670,12 @@ function mapFinishReason(stopReason) {
1645
1670
  // ai/openai/types.ts
1646
1671
  var AxAIOpenAIModel = /* @__PURE__ */ ((AxAIOpenAIModel2) => {
1647
1672
  AxAIOpenAIModel2["O1"] = "o1";
1673
+ AxAIOpenAIModel2["O3"] = "o3";
1648
1674
  AxAIOpenAIModel2["O1Mini"] = "o1-mini";
1649
1675
  AxAIOpenAIModel2["O3Mini"] = "o3-mini";
1676
+ AxAIOpenAIModel2["O4Mini"] = "o4-mini";
1650
1677
  AxAIOpenAIModel2["GPT4"] = "gpt-4";
1651
- AxAIOpenAIModel2["GPT45"] = "gpt-4.5-preview";
1678
+ AxAIOpenAIModel2["GPT41"] = "gpt-4.1";
1652
1679
  AxAIOpenAIModel2["GPT4O"] = "gpt-4o";
1653
1680
  AxAIOpenAIModel2["GPT4OMini"] = "gpt-4o-mini";
1654
1681
  AxAIOpenAIModel2["GPT4ChatGPT4O"] = "chatgpt-4o-latest";
@@ -1669,12 +1696,6 @@ var AxAIOpenAIEmbedModel = /* @__PURE__ */ ((AxAIOpenAIEmbedModel2) => {
1669
1696
 
1670
1697
  // ai/openai/info.ts
1671
1698
  var axModelInfoOpenAI = [
1672
- {
1673
- name: "gpt-4.5-preview" /* GPT45 */,
1674
- currency: "usd",
1675
- promptTokenCostPer1M: 75,
1676
- completionTokenCostPer1M: 150
1677
- },
1678
1699
  {
1679
1700
  name: "o1" /* O1 */,
1680
1701
  currency: "usd",
@@ -1693,12 +1714,24 @@ var axModelInfoOpenAI = [
1693
1714
  promptTokenCostPer1M: 1.1,
1694
1715
  completionTokenCostPer1M: 4.4
1695
1716
  },
1717
+ {
1718
+ name: "o4-mini" /* O4Mini */,
1719
+ currency: "usd",
1720
+ promptTokenCostPer1M: 1.1,
1721
+ completionTokenCostPer1M: 4.4
1722
+ },
1696
1723
  {
1697
1724
  name: "gpt-4" /* GPT4 */,
1698
1725
  currency: "usd",
1699
1726
  promptTokenCostPer1M: 30,
1700
1727
  completionTokenCostPer1M: 60
1701
1728
  },
1729
+ {
1730
+ name: "gpt-4.1" /* GPT41 */,
1731
+ currency: "usd",
1732
+ promptTokenCostPer1M: 2,
1733
+ completionTokenCostPer1M: 8
1734
+ },
1702
1735
  {
1703
1736
  name: "gpt-4o" /* GPT4O */,
1704
1737
  currency: "usd",
@@ -2503,8 +2536,8 @@ var AxAIDeepSeek = class extends AxAIOpenAIBase {
2503
2536
 
2504
2537
  // ai/google-gemini/types.ts
2505
2538
  var AxAIGoogleGeminiModel = /* @__PURE__ */ ((AxAIGoogleGeminiModel2) => {
2506
- AxAIGoogleGeminiModel2["Gemini25Pro"] = "gemini-2.5-pro-exp-03-25";
2507
- AxAIGoogleGeminiModel2["Gemini20Pro"] = "gemini-2.0-pro-exp-02-05";
2539
+ AxAIGoogleGeminiModel2["Gemini25Pro"] = "gemini-2.5-pro-preview-03-25";
2540
+ AxAIGoogleGeminiModel2["Gemini25Flash"] = "gemini-2.5-flash-preview-04-17";
2508
2541
  AxAIGoogleGeminiModel2["Gemini20Flash"] = "gemini-2.0-flash";
2509
2542
  AxAIGoogleGeminiModel2["Gemini20FlashLite"] = "gemini-2.0-flash-lite-preview-02-05";
2510
2543
  AxAIGoogleGeminiModel2["Gemini20FlashThinking"] = "gemini-2.0-flash-thinking-exp-01-21";
@@ -2552,11 +2585,18 @@ var AxAIGoogleGeminiEmbedTypes = /* @__PURE__ */ ((AxAIGoogleGeminiEmbedTypes2)
2552
2585
  // ai/google-gemini/info.ts
2553
2586
  var axModelInfoGoogleGemini = [
2554
2587
  {
2555
- name: "gemini-2.0-pro-exp-02-05" /* Gemini20Pro */,
2588
+ name: "gemini-2.5-pro-preview-03-25" /* Gemini25Pro */,
2556
2589
  currency: "usd",
2557
2590
  characterIsToken: false,
2558
- promptTokenCostPer1M: 0,
2559
- completionTokenCostPer1M: 0
2591
+ promptTokenCostPer1M: 2.5,
2592
+ completionTokenCostPer1M: 15
2593
+ },
2594
+ {
2595
+ name: "gemini-2.5-flash-preview-04-17" /* Gemini25Flash */,
2596
+ currency: "usd",
2597
+ characterIsToken: false,
2598
+ promptTokenCostPer1M: 15,
2599
+ completionTokenCostPer1M: 3.5
2560
2600
  },
2561
2601
  {
2562
2602
  name: "gemini-2.0-flash" /* Gemini20Flash */,
@@ -2810,7 +2850,10 @@ var AxAIGoogleGeminiImpl = class {
2810
2850
  frequencyPenalty: req.modelConfig?.frequencyPenalty ?? this.config.frequencyPenalty,
2811
2851
  candidateCount: 1,
2812
2852
  stopSequences: req.modelConfig?.stopSequences ?? this.config.stopSequences,
2813
- responseMimeType: "text/plain"
2853
+ responseMimeType: "text/plain",
2854
+ ...this.config.thinkingConfig && {
2855
+ thinkingConfig: this.config.thinkingConfig
2856
+ }
2814
2857
  };
2815
2858
  const safetySettings2 = this.config.safetySettings;
2816
2859
  const reqValue = {
@@ -4773,6 +4816,9 @@ function matchesContent(content, prefix, startIndex = 0, prefixCache = globalPre
4773
4816
  );
4774
4817
  for (let i = 0; i < prefixes.length - 1; i++) {
4775
4818
  const partialPrefix = prefixes[i];
4819
+ if (partialPrefix === "\n" || partialPrefix === ":") {
4820
+ continue;
4821
+ }
4776
4822
  if (partialPrefix && contentEnd.endsWith(partialPrefix)) {
4777
4823
  return -2;
4778
4824
  }
@@ -5555,7 +5601,7 @@ var streamingExtractValues = (sig, values, xstate, content, streamingValidation
5555
5601
  }
5556
5602
  const isFirst = xstate.extractedFields.length === 0;
5557
5603
  const prefix = (isFirst ? "" : "\n") + field.title + ":";
5558
- let e = matchesContent(content, prefix, xstate.s === 0 ? 0 : xstate.s + 1);
5604
+ let e = matchesContent(content, prefix, xstate.s);
5559
5605
  switch (e) {
5560
5606
  case -1:
5561
5607
  if (streamingValidation && values.length == 0 && !field.isOptional) {
@@ -6421,10 +6467,8 @@ var AxGen = class extends AxProgramWithSignature {
6421
6467
  const maxRetries = options.maxRetries ?? this.options?.maxRetries ?? 10;
6422
6468
  const maxSteps = options.maxSteps ?? this.options?.maxSteps ?? 10;
6423
6469
  const debug = options.debug ?? ai.getOptions().debug;
6424
- const memOptions = {
6425
- debug: options.debug,
6426
- debugHideSystemPrompt: options.debugHideSystemPrompt
6427
- };
6470
+ const debugHideSystemPrompt = options.debugHideSystemPrompt;
6471
+ const memOptions = { debug, debugHideSystemPrompt };
6428
6472
  const mem = options.mem ?? this.options?.mem ?? new AxMemory(1e4, memOptions);
6429
6473
  let err;
6430
6474
  if (options?.functions && options.functions.length > 0) {
@@ -6485,7 +6529,7 @@ var AxGen = class extends AxProgramWithSignature {
6485
6529
  }
6486
6530
  }
6487
6531
  }
6488
- throw new Error(`Unable to fix validation error: ${err}`);
6532
+ throw new Error(`Unable to fix validation error: ${err?.toString()}`);
6489
6533
  }
6490
6534
  throw new Error(`Max steps reached: ${maxSteps}`);
6491
6535
  }
@@ -6502,7 +6546,7 @@ var AxGen = class extends AxProgramWithSignature {
6502
6546
  return false;
6503
6547
  }
6504
6548
  async *_forward1(ai, values, options) {
6505
- const tracer = this.options?.tracer ?? options?.tracer;
6549
+ const tracer = options?.tracer ?? this.options?.tracer;
6506
6550
  let functions = this.functions;
6507
6551
  if (options?.functions) {
6508
6552
  functions = parseFunctions(options.functions, this.functions);
@@ -6538,9 +6582,7 @@ var AxGen = class extends AxProgramWithSignature {
6538
6582
  }
6539
6583
  }
6540
6584
  async forward(ai, values, options) {
6541
- const generator = this._forward1(ai, values, {
6542
- ...options
6543
- });
6585
+ const generator = this._forward1(ai, values, options ?? {});
6544
6586
  let buffer = {};
6545
6587
  let currentVersion = 0;
6546
6588
  for await (const item of generator) {
@@ -9204,7 +9246,7 @@ var AxMockAIService = class {
9204
9246
  }
9205
9247
  };
9206
9248
 
9207
- // dsp/router.ts
9249
+ // dsp/classifier.ts
9208
9250
  var colorLog6 = new ColorLog();
9209
9251
  var AxSimpleClassifierClass = class {
9210
9252
  name;
@@ -11013,6 +11055,7 @@ var AxMCPStdioTransport = class {
11013
11055
 
11014
11056
  // ai/multiservice.ts
11015
11057
  var AxMultiServiceRouter = class {
11058
+ options;
11016
11059
  services = /* @__PURE__ */ new Map();
11017
11060
  /**
11018
11061
  * Constructs a new multi-service router.
@@ -11033,9 +11076,7 @@ var AxMultiServiceRouter = class {
11033
11076
  this.services.set(item.key, {
11034
11077
  service,
11035
11078
  description,
11036
- isInternal,
11037
- model: item.service.getDefaultModels().model,
11038
- useDefaultModel: true
11079
+ isInternal
11039
11080
  });
11040
11081
  } else {
11041
11082
  const modelList = item.getModelList();
@@ -11044,18 +11085,31 @@ var AxMultiServiceRouter = class {
11044
11085
  `Service ${index} \`${item.getName()}\` has no model list.`
11045
11086
  );
11046
11087
  }
11047
- for (const { key, description, model } of modelList ?? []) {
11048
- if (this.services.has(key)) {
11049
- const otherService = this.services.get(key)?.service;
11088
+ for (const v of modelList) {
11089
+ if (this.services.has(v.key)) {
11090
+ const otherService = this.services.get(v.key)?.service;
11050
11091
  throw new Error(
11051
- `Service ${index} \`${item.getName()}\` has duplicate model key: ${key} as service ${otherService?.getName()}`
11092
+ `Service ${index} \`${item.getName()}\` has duplicate model key: ${v.key} as service ${otherService?.getName()}`
11052
11093
  );
11094
+ } else {
11095
+ if ("model" in v && typeof v.model) {
11096
+ this.services.set(v.key, {
11097
+ description: v.description,
11098
+ service: item,
11099
+ model: v.model
11100
+ });
11101
+ } else if ("embedModel" in v && v.embedModel) {
11102
+ this.services.set(v.key, {
11103
+ description: v.description,
11104
+ service: item,
11105
+ embedModel: v.embedModel
11106
+ });
11107
+ } else {
11108
+ throw new Error(
11109
+ `Key ${v.key} in model list for service ${index} \`${item.getName()}\` is missing a model or embedModel property.`
11110
+ );
11111
+ }
11053
11112
  }
11054
- this.services.set(key, {
11055
- description,
11056
- service: item,
11057
- model
11058
- });
11059
11113
  }
11060
11114
  }
11061
11115
  }
@@ -11072,25 +11126,32 @@ var AxMultiServiceRouter = class {
11072
11126
  if (!item) {
11073
11127
  throw new Error(`No service found for model key: ${modelKey}`);
11074
11128
  }
11075
- const service = item.service;
11076
- const model = item.useDefaultModel ? req.model : modelKey;
11077
- return await service.chat({ model, ...req }, options);
11129
+ if (!item.model) {
11130
+ const { model, ...reqWithoutModel } = req;
11131
+ return await item.service.chat(reqWithoutModel, options);
11132
+ }
11133
+ return await item.service.chat({ model: modelKey, ...req }, options);
11078
11134
  }
11079
11135
  /**
11080
11136
  * Delegates the embed call to the service matching the provided embed model key.
11081
11137
  */
11082
11138
  async embed(req, options) {
11083
- const modelKey = req.embedModel;
11084
- if (!modelKey) {
11139
+ const embedModelKey = req.embedModel;
11140
+ if (!embedModelKey) {
11085
11141
  throw new Error("Embed model key must be specified for multi-service");
11086
11142
  }
11087
- const item = this.services.get(modelKey);
11143
+ const item = this.services.get(embedModelKey);
11088
11144
  if (!item) {
11089
- throw new Error(`No service found for embed model key: ${modelKey}`);
11145
+ throw new Error(`No service found for embed model key: ${embedModelKey}`);
11146
+ }
11147
+ if (!item.model) {
11148
+ const { embedModel, ...reqWithoutEmbedModel } = req;
11149
+ return await item.service.embed(reqWithoutEmbedModel, options);
11090
11150
  }
11091
- const service = item.service;
11092
- const embedModel = item.useDefaultModel ? req.embedModel : modelKey;
11093
- return await service.embed({ embedModel, ...req }, options);
11151
+ return await item.service.embed(
11152
+ { embedModel: embedModelKey, ...req },
11153
+ options
11154
+ );
11094
11155
  }
11095
11156
  /**
11096
11157
  * Returns a composite ID built from the IDs of the underlying services.
@@ -11108,11 +11169,15 @@ var AxMultiServiceRouter = class {
11108
11169
  * Aggregates all available models across the underlying services.
11109
11170
  */
11110
11171
  getModelList() {
11111
- return Array.from(this.services).filter(([, value]) => !value.isInternal).map(([key, { description, model }]) => ({
11112
- key,
11113
- description,
11114
- model
11115
- }));
11172
+ return Array.from(this.services).filter(([, value]) => !value.isInternal).map(([key, v]) => {
11173
+ if (v.model) {
11174
+ return { key, description: v.description, model: v.model };
11175
+ } else if (v.embedModel) {
11176
+ return { key, description: v.description, embedModel: v.embedModel };
11177
+ } else {
11178
+ throw new Error(`Service ${key} has no model or embedModel`);
11179
+ }
11180
+ });
11116
11181
  }
11117
11182
  getDefaultModels() {
11118
11183
  throw new Error(
@@ -11151,17 +11216,14 @@ var AxMultiServiceRouter = class {
11151
11216
  for (const service of this.services.values()) {
11152
11217
  service.service.setOptions(options);
11153
11218
  }
11219
+ this.options = options;
11154
11220
  }
11155
11221
  /**
11156
11222
  * Returns the options from the last used service,
11157
11223
  * or falls back to the first service if none has been used.
11158
11224
  */
11159
11225
  getOptions() {
11160
- const service = this.services.values().next().value;
11161
- if (!service) {
11162
- throw new Error("No service available to get options.");
11163
- }
11164
- return service.service.getOptions();
11226
+ return this.options ?? {};
11165
11227
  }
11166
11228
  };
11167
11229