@ax-llm/ax 10.0.49 → 11.0.0

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
@@ -5,9 +5,9 @@ import { SpanKind } from "@opentelemetry/api";
5
5
  function getModelInfo({
6
6
  model,
7
7
  modelInfo,
8
- modelMap = {}
8
+ models
9
9
  }) {
10
- const mappedModel = modelMap?.[model] ?? model;
10
+ const mappedModel = models?.find((v) => v.key === model)?.model ?? model;
11
11
  const exactMatch = modelInfo.find((v) => v.name === model);
12
12
  if (exactMatch) return exactMatch;
13
13
  const normalizedName = mappedModel.replace(/^(anthropic\.|openai\.)/, "").replace(/-latest$/, "").replace(/-\d{8}$/, "").replace(/-v\d+:\d+$/, "").replace(/@\d{8}$/, "").replace(/-\d{2,}(-[a-zA-Z0-9-]+)?$/, "").replace(/-v\d+@\d{8}$/, "").replace(/-v\d+$/, "");
@@ -636,10 +636,10 @@ var AxBaseAI = class {
636
636
  apiURL,
637
637
  headers,
638
638
  modelInfo,
639
- models,
639
+ defaults,
640
640
  options = {},
641
641
  supportFor,
642
- modelMap
642
+ models
643
643
  }) {
644
644
  this.aiImpl = aiImpl;
645
645
  this.name = name;
@@ -648,12 +648,11 @@ var AxBaseAI = class {
648
648
  this.supportFor = supportFor;
649
649
  this.tracer = options.tracer;
650
650
  this.modelInfo = modelInfo;
651
- this.modelMap = modelMap;
652
- this.models = {
653
- model: modelMap?.[models.model] ?? models.model,
654
- embedModel: modelMap?.[models.embedModel ?? ""] ?? models.embedModel
655
- };
656
- if (!models.model || typeof models.model !== "string" || models.model === "") {
651
+ this.models = models;
652
+ const model = this.models?.find((v) => v.key === defaults.model)?.model ?? defaults.model;
653
+ const embedModel = this.models?.find((v) => v.key === defaults.embedModel)?.model ?? defaults.embedModel;
654
+ this.defaults = { model, embedModel };
655
+ if (!defaults.model || typeof defaults.model !== "string" || defaults.model === "") {
657
656
  throw new Error("No model defined");
658
657
  }
659
658
  this.setOptions(options);
@@ -662,11 +661,11 @@ var AxBaseAI = class {
662
661
  rt;
663
662
  fetch;
664
663
  tracer;
665
- modelMap;
664
+ models;
666
665
  modelInfo;
667
666
  modelUsage;
668
667
  embedModelUsage;
669
- models;
668
+ defaults;
670
669
  apiURL;
671
670
  name;
672
671
  headers;
@@ -733,9 +732,9 @@ var AxBaseAI = class {
733
732
  }
734
733
  getModelInfo() {
735
734
  const mi = getModelInfo({
736
- model: this.models.model,
735
+ model: this.defaults.model,
737
736
  modelInfo: this.modelInfo,
738
- modelMap: this.modelMap
737
+ models: this.models
739
738
  });
740
739
  return {
741
740
  ...mi,
@@ -743,27 +742,27 @@ var AxBaseAI = class {
743
742
  };
744
743
  }
745
744
  getEmbedModelInfo() {
746
- if (!this.models.embedModel) {
745
+ if (!this.defaults.embedModel) {
747
746
  return;
748
747
  }
749
748
  const mi = getModelInfo({
750
- model: this.models.embedModel,
749
+ model: this.defaults.embedModel,
751
750
  modelInfo: this.modelInfo,
752
- modelMap: this.modelMap
751
+ models: this.models
753
752
  });
754
753
  return {
755
754
  ...mi,
756
755
  provider: this.name
757
756
  };
758
757
  }
759
- getModelMap() {
760
- return this.modelMap;
758
+ getModelList() {
759
+ return this.models;
761
760
  }
762
761
  getName() {
763
762
  return this.name;
764
763
  }
765
764
  getFeatures(model) {
766
- return typeof this.supportFor === "function" ? this.supportFor(model ?? this.models.model) : this.supportFor;
765
+ return typeof this.supportFor === "function" ? this.supportFor(model ?? this.defaults.model) : this.supportFor;
767
766
  }
768
767
  // Method to calculate percentiles
769
768
  calculatePercentile(samples, percentile) {
@@ -811,7 +810,7 @@ var AxBaseAI = class {
811
810
  }
812
811
  }
813
812
  async _chat1(req, options) {
814
- const model = req.model ? this.modelMap?.[req.model] ?? req.model : this.modelMap?.[this.models.model] ?? this.models.model;
813
+ const model = req.model ? this.models?.find((v) => v.key === req.model)?.model ?? req.model : this.defaults.model;
815
814
  const modelConfig = {
816
815
  ...this.aiImpl.getModelConfig(),
817
816
  ...req.modelConfig
@@ -955,7 +954,7 @@ var AxBaseAI = class {
955
954
  }
956
955
  }
957
956
  async _embed1(req, options) {
958
- const embedModel = req.embedModel ? this.modelMap?.[req.embedModel] ?? req.embedModel : this.modelMap?.[this.models.embedModel ?? ""] ?? this.models.embedModel;
957
+ const embedModel = req.embedModel ? this.models?.find((v) => v.key === req.embedModel)?.model ?? req.embedModel : this.defaults.embedModel;
959
958
  if (!embedModel) {
960
959
  throw new Error("No embed model defined");
961
960
  }
@@ -966,7 +965,7 @@ var AxBaseAI = class {
966
965
  kind: SpanKind.SERVER,
967
966
  attributes: {
968
967
  [axSpanAttributes.LLM_SYSTEM]: this.name,
969
- [axSpanAttributes.LLM_REQUEST_MODEL]: req.embedModel ?? this.models.embedModel
968
+ [axSpanAttributes.LLM_REQUEST_MODEL]: req.embedModel ?? this.defaults.embedModel
970
969
  }
971
970
  },
972
971
  async (span) => {
@@ -1439,7 +1438,7 @@ var AxAIAnthropic = class extends AxBaseAI {
1439
1438
  region,
1440
1439
  config,
1441
1440
  options,
1442
- modelMap
1441
+ models
1443
1442
  }) {
1444
1443
  const isVertex = projectId !== void 0 && region !== void 0;
1445
1444
  let apiURL;
@@ -1475,10 +1474,10 @@ var AxAIAnthropic = class extends AxBaseAI {
1475
1474
  apiURL,
1476
1475
  headers,
1477
1476
  modelInfo: axModelInfoAnthropic,
1478
- models: { model: _config.model },
1477
+ defaults: { model: _config.model },
1479
1478
  options,
1480
- supportFor: { functions: true, streaming: true },
1481
- modelMap
1479
+ supportFor: { functions: true, streaming: true, functionCot: true },
1480
+ models
1482
1481
  });
1483
1482
  }
1484
1483
  };
@@ -1962,7 +1961,7 @@ var AxAIOpenAI = class extends AxBaseAI {
1962
1961
  options,
1963
1962
  apiURL,
1964
1963
  modelInfo = axModelInfoOpenAI,
1965
- modelMap
1964
+ models
1966
1965
  }) {
1967
1966
  if (!apiKey || apiKey === "") {
1968
1967
  throw new Error("OpenAI API key not set");
@@ -1981,7 +1980,7 @@ var AxAIOpenAI = class extends AxBaseAI {
1981
1980
  apiURL: apiURL ? apiURL : "https://api.openai.com/v1",
1982
1981
  headers: async () => ({ Authorization: `Bearer ${apiKey}` }),
1983
1982
  modelInfo,
1984
- models: {
1983
+ defaults: {
1985
1984
  model: _config.model,
1986
1985
  embedModel: _config.embedModel
1987
1986
  },
@@ -1989,7 +1988,7 @@ var AxAIOpenAI = class extends AxBaseAI {
1989
1988
  supportFor: () => {
1990
1989
  return { functions: true, streaming: true };
1991
1990
  },
1992
- modelMap
1991
+ models
1993
1992
  });
1994
1993
  }
1995
1994
  };
@@ -2007,7 +2006,7 @@ var AxAIAzureOpenAI = class extends AxAIOpenAI {
2007
2006
  version = "api-version=2024-02-15-preview",
2008
2007
  config,
2009
2008
  options,
2010
- modelMap
2009
+ models
2011
2010
  }) {
2012
2011
  if (!apiKey || apiKey === "") {
2013
2012
  throw new Error("Azure OpenAPI API key not set");
@@ -2022,7 +2021,7 @@ var AxAIAzureOpenAI = class extends AxAIOpenAI {
2022
2021
  ...axAIAzureOpenAIDefaultConfig(),
2023
2022
  ...config
2024
2023
  };
2025
- super({ apiKey, config: _config, options, modelMap });
2024
+ super({ apiKey, config: _config, options, models });
2026
2025
  const host = resourceName.includes("://") ? resourceName : `https://${resourceName}.openai.azure.com/`;
2027
2026
  super.setName("Azure OpenAI");
2028
2027
  super.setAPIURL(
@@ -2278,7 +2277,7 @@ var AxAICohere = class extends AxBaseAI {
2278
2277
  apiKey,
2279
2278
  config,
2280
2279
  options,
2281
- modelMap
2280
+ models
2282
2281
  }) {
2283
2282
  if (!apiKey || apiKey === "") {
2284
2283
  throw new Error("Cohere API key not set");
@@ -2293,10 +2292,10 @@ var AxAICohere = class extends AxBaseAI {
2293
2292
  apiURL: "https://api.cohere.ai/v1",
2294
2293
  headers: async () => ({ Authorization: `Bearer ${apiKey}` }),
2295
2294
  modelInfo: axModelInfoCohere,
2296
- models: { model: _config.model },
2295
+ defaults: { model: _config.model },
2297
2296
  supportFor: { functions: true, streaming: true },
2298
2297
  options,
2299
- modelMap
2298
+ models
2300
2299
  });
2301
2300
  }
2302
2301
  };
@@ -2390,7 +2389,7 @@ var AxAIDeepSeek = class extends AxAIOpenAI {
2390
2389
  apiKey,
2391
2390
  config,
2392
2391
  options,
2393
- modelMap
2392
+ models
2394
2393
  }) {
2395
2394
  if (!apiKey || apiKey === "") {
2396
2395
  throw new Error("DeepSeek API key not set");
@@ -2405,7 +2404,7 @@ var AxAIDeepSeek = class extends AxAIOpenAI {
2405
2404
  options,
2406
2405
  apiURL: "https://api.deepseek.com",
2407
2406
  modelInfo: axModelInfoDeepSeek,
2408
- modelMap
2407
+ models
2409
2408
  });
2410
2409
  super.setName("DeepSeek");
2411
2410
  }
@@ -2413,6 +2412,10 @@ var AxAIDeepSeek = class extends AxAIOpenAI {
2413
2412
 
2414
2413
  // ai/google-gemini/types.ts
2415
2414
  var AxAIGoogleGeminiModel = /* @__PURE__ */ ((AxAIGoogleGeminiModel2) => {
2415
+ AxAIGoogleGeminiModel2["Gemini20Pro"] = "gemini-2.0-pro-exp-02-05";
2416
+ AxAIGoogleGeminiModel2["Gemini20Flash"] = "gemini-2.0-flash";
2417
+ AxAIGoogleGeminiModel2["Gemini20FlashLite"] = "gemini-2.0-flash-lite-preview-02-05";
2418
+ AxAIGoogleGeminiModel2["Gemini20FlashThinking"] = "gemini-2.0-flash-thinking-exp-01-21";
2416
2419
  AxAIGoogleGeminiModel2["Gemini1Pro"] = "gemini-1.0-pro";
2417
2420
  AxAIGoogleGeminiModel2["Gemini15Flash"] = "gemini-1.5-flash";
2418
2421
  AxAIGoogleGeminiModel2["Gemini15Flash8B"] = "gemini-1.5-flash-8b";
@@ -2444,31 +2447,59 @@ var AxAIGoogleGeminiSafetyThreshold = /* @__PURE__ */ ((AxAIGoogleGeminiSafetyTh
2444
2447
 
2445
2448
  // ai/google-gemini/info.ts
2446
2449
  var axModelInfoGoogleGemini = [
2450
+ {
2451
+ name: "gemini-2.0-pro-exp-02-05" /* Gemini20Pro */,
2452
+ currency: "usd",
2453
+ characterIsToken: false,
2454
+ promptTokenCostPer1M: 0,
2455
+ completionTokenCostPer1M: 0
2456
+ },
2457
+ {
2458
+ name: "gemini-2.0-flash" /* Gemini20Flash */,
2459
+ currency: "usd",
2460
+ characterIsToken: false,
2461
+ promptTokenCostPer1M: 0.01,
2462
+ completionTokenCostPer1M: 0.4
2463
+ },
2464
+ {
2465
+ name: "gemini-2.0-flash-lite-preview-02-05" /* Gemini20FlashLite */,
2466
+ currency: "usd",
2467
+ characterIsToken: false,
2468
+ promptTokenCostPer1M: 0,
2469
+ completionTokenCostPer1M: 0
2470
+ },
2471
+ {
2472
+ name: "gemini-2.0-flash-thinking-exp-01-21" /* Gemini20FlashThinking */,
2473
+ currency: "usd",
2474
+ characterIsToken: false,
2475
+ promptTokenCostPer1M: 0,
2476
+ completionTokenCostPer1M: 0
2477
+ },
2447
2478
  {
2448
2479
  name: "gemini-1.5-flash" /* Gemini15Flash */,
2449
2480
  currency: "usd",
2450
- characterIsToken: true,
2481
+ characterIsToken: false,
2451
2482
  promptTokenCostPer1M: 0.075,
2452
2483
  completionTokenCostPer1M: 0.3
2453
2484
  },
2454
2485
  {
2455
2486
  name: "gemini-1.5-flash-8b" /* Gemini15Flash8B */,
2456
2487
  currency: "usd",
2457
- characterIsToken: true,
2488
+ characterIsToken: false,
2458
2489
  promptTokenCostPer1M: 0.0375,
2459
2490
  completionTokenCostPer1M: 0.15
2460
2491
  },
2461
2492
  {
2462
2493
  name: "gemini-1.5-pro" /* Gemini15Pro */,
2463
2494
  currency: "usd",
2464
- characterIsToken: true,
2495
+ characterIsToken: false,
2465
2496
  promptTokenCostPer1M: 1.25,
2466
2497
  completionTokenCostPer1M: 5
2467
2498
  },
2468
2499
  {
2469
2500
  name: "gemini-1.0-pro" /* Gemini1Pro */,
2470
2501
  currency: "usd",
2471
- characterIsToken: true,
2502
+ characterIsToken: false,
2472
2503
  promptTokenCostPer1M: 0.5,
2473
2504
  completionTokenCostPer1M: 1.5
2474
2505
  }
@@ -2782,7 +2813,7 @@ var AxAIGoogleGemini = class extends AxBaseAI {
2782
2813
  region,
2783
2814
  config,
2784
2815
  options,
2785
- modelMap
2816
+ models
2786
2817
  }) {
2787
2818
  const isVertex = projectId !== void 0 && region !== void 0;
2788
2819
  let apiURL;
@@ -2814,13 +2845,13 @@ var AxAIGoogleGemini = class extends AxBaseAI {
2814
2845
  apiURL,
2815
2846
  headers,
2816
2847
  modelInfo: axModelInfoGoogleGemini,
2817
- models: {
2848
+ defaults: {
2818
2849
  model: _config.model,
2819
2850
  embedModel: _config.embedModel
2820
2851
  },
2821
2852
  options,
2822
2853
  supportFor: { functions: true, streaming: true },
2823
- modelMap
2854
+ models
2824
2855
  });
2825
2856
  }
2826
2857
  };
@@ -2922,7 +2953,7 @@ var AxAIGroq = class extends AxAIOpenAI {
2922
2953
  apiKey,
2923
2954
  config,
2924
2955
  options,
2925
- modelMap
2956
+ models
2926
2957
  }) {
2927
2958
  if (!apiKey || apiKey === "") {
2928
2959
  throw new Error("Groq API key not set");
@@ -2941,7 +2972,7 @@ var AxAIGroq = class extends AxAIOpenAI {
2941
2972
  options: _options,
2942
2973
  modelInfo: axModelInfoGroq,
2943
2974
  apiURL: "https://api.groq.com/openai/v1",
2944
- modelMap
2975
+ models
2945
2976
  });
2946
2977
  super.setName("Groq");
2947
2978
  this.setOptions(_options);
@@ -3065,7 +3096,7 @@ var AxAIHuggingFace = class extends AxBaseAI {
3065
3096
  apiKey,
3066
3097
  config,
3067
3098
  options,
3068
- modelMap
3099
+ models
3069
3100
  }) {
3070
3101
  if (!apiKey || apiKey === "") {
3071
3102
  throw new Error("HuggingFace API key not set");
@@ -3080,10 +3111,10 @@ var AxAIHuggingFace = class extends AxBaseAI {
3080
3111
  apiURL: "https://api-inference.huggingface.co",
3081
3112
  headers: async () => ({ Authorization: `Bearer ${apiKey}` }),
3082
3113
  modelInfo: axModelInfoHuggingFace,
3083
- models: { model: _config.model },
3114
+ defaults: { model: _config.model },
3084
3115
  options,
3085
3116
  supportFor: { functions: false, streaming: false },
3086
- modelMap
3117
+ models
3087
3118
  });
3088
3119
  }
3089
3120
  };
@@ -3167,7 +3198,7 @@ var AxAIMistral = class extends AxAIOpenAI {
3167
3198
  apiKey,
3168
3199
  config,
3169
3200
  options,
3170
- modelMap
3201
+ models
3171
3202
  }) {
3172
3203
  if (!apiKey || apiKey === "") {
3173
3204
  throw new Error("Mistral API key not set");
@@ -3182,7 +3213,7 @@ var AxAIMistral = class extends AxAIOpenAI {
3182
3213
  options,
3183
3214
  apiURL: "https://api.mistral.ai/v1",
3184
3215
  modelInfo: axModelInfoMistral,
3185
- modelMap
3216
+ models
3186
3217
  });
3187
3218
  super.setName("Mistral");
3188
3219
  }
@@ -3200,7 +3231,7 @@ var AxAIOllama = class extends AxAIOpenAI {
3200
3231
  url = "http://localhost:11434/v1",
3201
3232
  config,
3202
3233
  options,
3203
- modelMap
3234
+ models
3204
3235
  }) {
3205
3236
  const _config = {
3206
3237
  ...axAIOllamaDefaultConfig(),
@@ -3211,7 +3242,7 @@ var AxAIOllama = class extends AxAIOpenAI {
3211
3242
  options,
3212
3243
  config: _config,
3213
3244
  apiURL: url,
3214
- modelMap
3245
+ models
3215
3246
  });
3216
3247
  super.setName("Ollama");
3217
3248
  }
@@ -3414,7 +3445,7 @@ var AxAIReka = class extends AxBaseAI {
3414
3445
  options,
3415
3446
  apiURL,
3416
3447
  modelInfo = axModelInfoReka,
3417
- modelMap
3448
+ models
3418
3449
  }) {
3419
3450
  if (!apiKey || apiKey === "") {
3420
3451
  throw new Error("Reka API key not set");
@@ -3429,12 +3460,12 @@ var AxAIReka = class extends AxBaseAI {
3429
3460
  apiURL: apiURL ? apiURL : "https://api.reka.ai/v1/chat",
3430
3461
  headers: async () => ({ "X-Api-Key": apiKey }),
3431
3462
  modelInfo,
3432
- models: {
3463
+ defaults: {
3433
3464
  model: _config.model
3434
3465
  },
3435
3466
  options,
3436
3467
  supportFor: { functions: true, streaming: true },
3437
- modelMap
3468
+ models
3438
3469
  });
3439
3470
  }
3440
3471
  };
@@ -3453,7 +3484,7 @@ var AxAITogether = class extends AxAIOpenAI {
3453
3484
  apiKey,
3454
3485
  config,
3455
3486
  options,
3456
- modelMap
3487
+ models
3457
3488
  }) {
3458
3489
  if (!apiKey || apiKey === "") {
3459
3490
  throw new Error("Together API key not set");
@@ -3468,7 +3499,7 @@ var AxAITogether = class extends AxAIOpenAI {
3468
3499
  options,
3469
3500
  apiURL: "https://api.together.xyz/v1",
3470
3501
  modelInfo: axModelInfoTogether,
3471
- modelMap
3502
+ models
3472
3503
  });
3473
3504
  super.setName("Together");
3474
3505
  }
@@ -3531,8 +3562,8 @@ var AxAI = class {
3531
3562
  getFeatures(model) {
3532
3563
  return this.ai.getFeatures(model);
3533
3564
  }
3534
- getModelMap() {
3535
- return this.ai.getModelMap();
3565
+ getModelList() {
3566
+ return this.ai.getModelList();
3536
3567
  }
3537
3568
  getMetrics() {
3538
3569
  return this.ai.getMetrics();
@@ -5133,7 +5164,7 @@ var checkMissingRequiredFields = (xstate, values, currentIndex) => {
5133
5164
  });
5134
5165
  }
5135
5166
  };
5136
- var streamingExtractValues = (sig, values, xstate, content) => {
5167
+ var streamingExtractValues = (sig, values, xstate, content, streamingValidation = false) => {
5137
5168
  const fields = sig.getOutputFields();
5138
5169
  for (const [index, field] of fields.entries()) {
5139
5170
  if (field.name in values) {
@@ -5143,6 +5174,12 @@ var streamingExtractValues = (sig, values, xstate, content) => {
5143
5174
  let e = matchesContent(content, prefix, xstate.s + 1);
5144
5175
  switch (e) {
5145
5176
  case -1:
5177
+ if (streamingValidation && xstate.s == -1 && !field.isOptional) {
5178
+ throw new ValidationError({
5179
+ message: "Required field not found",
5180
+ fields: [field]
5181
+ });
5182
+ }
5146
5183
  continue;
5147
5184
  // Field is not found, continue to the next field
5148
5185
  case -2:
@@ -5159,6 +5196,7 @@ var streamingExtractValues = (sig, values, xstate, content) => {
5159
5196
  checkMissingRequiredFields(xstate, values, index);
5160
5197
  xstate.s = e + prefixLen;
5161
5198
  xstate.currField = field;
5199
+ xstate.currFieldIndex = index;
5162
5200
  if (!xstate.extractedFields.includes(field)) {
5163
5201
  xstate.extractedFields.push(field);
5164
5202
  }
@@ -5172,8 +5210,8 @@ var streamingExtractFinalValue = (sig, values, xstate, content) => {
5172
5210
  values[xstate.currField.name] = parsedValue;
5173
5211
  }
5174
5212
  }
5175
- const fields = sig.getOutputFields();
5176
- checkMissingRequiredFields(xstate, values, fields.length - 1);
5213
+ const sigFields = sig.getOutputFields();
5214
+ checkMissingRequiredFields(xstate, values, sigFields.length);
5177
5215
  };
5178
5216
  var convertValueToType = (field, val) => {
5179
5217
  switch (field.type?.name) {
@@ -5658,6 +5696,7 @@ var AxGen = class extends AxProgramWithSignature {
5658
5696
  traceId,
5659
5697
  functions
5660
5698
  }) {
5699
+ const streamingValidation = ai.getFeatures().functionCot !== true;
5661
5700
  const functionCalls = [];
5662
5701
  const values = {};
5663
5702
  const xstate = {
@@ -5673,14 +5712,21 @@ var AxGen = class extends AxProgramWithSignature {
5673
5712
  if (v.modelUsage) {
5674
5713
  this.usage.push({ ...usageInfo, ...v.modelUsage });
5675
5714
  }
5676
- if (result.content) {
5715
+ if (result.functionCalls) {
5716
+ mergeFunctionCalls(functionCalls, result.functionCalls);
5717
+ mem.updateResult(
5718
+ { name: result.name, content, functionCalls },
5719
+ sessionId
5720
+ );
5721
+ } else if (result.content) {
5677
5722
  content += result.content;
5678
5723
  mem.updateResult({ name: result.name, content }, sessionId);
5679
5724
  const skip = streamingExtractValues(
5680
5725
  this.signature,
5681
5726
  values,
5682
5727
  xstate,
5683
- content
5728
+ content,
5729
+ streamingValidation
5684
5730
  );
5685
5731
  if (skip) {
5686
5732
  continue;
@@ -5695,13 +5741,6 @@ var AxGen = class extends AxProgramWithSignature {
5695
5741
  assertAssertions(this.asserts, values);
5696
5742
  yield* streamValues(this.signature, values, xstate, content);
5697
5743
  }
5698
- if (result.functionCalls) {
5699
- mergeFunctionCalls(functionCalls, result.functionCalls);
5700
- mem.updateResult(
5701
- { name: result.name, content, functionCalls },
5702
- sessionId
5703
- );
5704
- }
5705
5744
  if (result.finishReason === "length") {
5706
5745
  throw new Error("Max tokens reached before completion");
5707
5746
  }
@@ -5720,17 +5759,18 @@ var AxGen = class extends AxProgramWithSignature {
5720
5759
  traceId
5721
5760
  );
5722
5761
  this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
5762
+ } else {
5763
+ streamingExtractFinalValue(this.signature, values, xstate, content);
5764
+ assertStreamingAssertions(
5765
+ this.streamingAsserts,
5766
+ values,
5767
+ xstate,
5768
+ content,
5769
+ true
5770
+ );
5771
+ assertAssertions(this.asserts, values);
5772
+ yield* streamValues(this.signature, values, xstate, content, true);
5723
5773
  }
5724
- streamingExtractFinalValue(this.signature, values, xstate, content);
5725
- assertStreamingAssertions(
5726
- this.streamingAsserts,
5727
- values,
5728
- xstate,
5729
- content,
5730
- true
5731
- );
5732
- assertAssertions(this.asserts, values);
5733
- yield* streamValues(this.signature, values, xstate, content, true);
5734
5774
  }
5735
5775
  async processResponse({
5736
5776
  ai,
@@ -5742,15 +5782,15 @@ var AxGen = class extends AxProgramWithSignature {
5742
5782
  functions
5743
5783
  }) {
5744
5784
  const values = {};
5745
- for (const result of res.results ?? []) {
5785
+ let results = res.results ?? [];
5786
+ if (res.results.length > 1) {
5787
+ results = res.results.filter((r) => r.functionCalls);
5788
+ }
5789
+ for (const result of results) {
5746
5790
  if (res.modelUsage) {
5747
5791
  this.usage.push({ ...usageInfo, ...res.modelUsage });
5748
5792
  }
5749
5793
  mem.addResult(result, sessionId);
5750
- if (result.content) {
5751
- extractValues(this.signature, values, result.content);
5752
- assertAssertions(this.asserts, values);
5753
- }
5754
5794
  if (result.functionCalls) {
5755
5795
  const funcs = parseFunctionCalls(ai, result.functionCalls, values);
5756
5796
  if (funcs) {
@@ -5767,6 +5807,9 @@ var AxGen = class extends AxProgramWithSignature {
5767
5807
  );
5768
5808
  this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
5769
5809
  }
5810
+ } else if (result.content) {
5811
+ extractValues(this.signature, values, result.content);
5812
+ assertAssertions(this.asserts, values);
5770
5813
  }
5771
5814
  if (result.finishReason === "length") {
5772
5815
  throw new Error("Max tokens reached before completion");
@@ -5914,6 +5957,7 @@ var AxAgent = class {
5914
5957
  ai;
5915
5958
  signature;
5916
5959
  program;
5960
+ functions;
5917
5961
  agents;
5918
5962
  name;
5919
5963
  description;
@@ -5929,14 +5973,9 @@ var AxAgent = class {
5929
5973
  }, options) {
5930
5974
  this.ai = ai;
5931
5975
  this.agents = agents;
5976
+ this.functions = functions;
5932
5977
  this.signature = new AxSignature(signature);
5933
5978
  this.signature.setDescription(description);
5934
- const funcs = [
5935
- ...functions ?? [],
5936
- ...agents?.map((a) => a.getFunction()) ?? []
5937
- ];
5938
- const opt = { ...options, functions: funcs };
5939
- this.program = new AxGen(this.signature, opt);
5940
5979
  if (!name || name.length < 5) {
5941
5980
  throw new Error(
5942
5981
  `Agent name must be at least 10 characters (more descriptive): ${name}`
@@ -5947,6 +5986,10 @@ var AxAgent = class {
5947
5986
  `Agent description must be at least 20 characters (explain in detail what the agent does): ${description}`
5948
5987
  );
5949
5988
  }
5989
+ this.program = new AxGen(this.signature, options);
5990
+ for (const agent of agents ?? []) {
5991
+ this.program.register(agent);
5992
+ }
5950
5993
  this.name = name;
5951
5994
  this.description = description;
5952
5995
  this.subAgentList = agents?.map((a) => a.getFunction().name).join(", ");
@@ -5956,8 +5999,9 @@ var AxAgent = class {
5956
5999
  parameters: this.signature.toJSONSchema(),
5957
6000
  func: () => this.forward
5958
6001
  };
5959
- for (const agent of agents ?? []) {
5960
- this.program.register(agent);
6002
+ const mm = ai?.getModelList();
6003
+ if (mm) {
6004
+ this.func.parameters = addModelParameter(this.func.parameters, mm);
5961
6005
  }
5962
6006
  }
5963
6007
  setExamples(examples) {
@@ -5983,38 +6027,41 @@ var AxAgent = class {
5983
6027
  }
5984
6028
  getFunction() {
5985
6029
  const boundFunc = this.forward.bind(this);
5986
- const wrappedFunc = (values, options) => {
6030
+ const wrappedFunc = (valuesAndModel, options) => {
6031
+ const { model, ...values } = valuesAndModel;
5987
6032
  const ai = this.ai ?? options?.ai;
5988
6033
  if (!ai) {
5989
6034
  throw new Error("AI service is required to run the agent");
5990
6035
  }
5991
- return boundFunc(ai, values, options);
6036
+ return boundFunc(ai, values, { ...options, model });
5992
6037
  };
5993
6038
  return {
5994
6039
  ...this.func,
5995
6040
  func: wrappedFunc
5996
6041
  };
5997
6042
  }
5998
- init(ai, options) {
5999
- const _ai = this.ai ?? ai;
6000
- const funcs = [
6001
- ...options?.functions ?? [],
6002
- ...this.agents?.map((a) => a.getFunction()) ?? []
6043
+ init(parentAi, options) {
6044
+ const ai = this.ai ?? parentAi;
6045
+ const mm = ai?.getModelList();
6046
+ const agentFuncs = this.agents?.map((a) => a.getFunction())?.map(
6047
+ (f) => mm ? { ...f, parameters: addModelParameter(f.parameters, mm) } : f
6048
+ );
6049
+ const functions = [
6050
+ ...options?.functions ?? this.functions ?? [],
6051
+ ...agentFuncs ?? []
6003
6052
  ];
6004
- const opt = options;
6005
- if (funcs.length > 0) {
6006
- const opt2 = { ...options, functions: funcs };
6007
- this.program = new AxGen(this.signature, opt2);
6008
- }
6009
- return { _ai, opt };
6053
+ return { ai, functions };
6010
6054
  }
6011
- async forward(ai, values, options) {
6012
- const { _ai, opt } = this.init(ai, options);
6013
- return await this.program.forward(_ai, values, opt);
6055
+ async forward(parentAi, values, options) {
6056
+ const { ai, functions } = this.init(parentAi, options);
6057
+ return await this.program.forward(ai, values, { ...options, functions });
6014
6058
  }
6015
- async *streamingForward(ai, values, options) {
6016
- const { _ai, opt } = this.init(ai, options);
6017
- return yield* this.program.streamingForward(_ai, values, opt);
6059
+ async *streamingForward(parentAi, values, options) {
6060
+ const { ai, functions } = this.init(parentAi, options);
6061
+ return yield* this.program.streamingForward(ai, values, {
6062
+ ...options,
6063
+ functions
6064
+ });
6018
6065
  }
6019
6066
  };
6020
6067
  function toCamelCase(inputString) {
@@ -6028,6 +6075,31 @@ function toCamelCase(inputString) {
6028
6075
  }).join("");
6029
6076
  return camelCaseString;
6030
6077
  }
6078
+ function addModelParameter(parameters, models) {
6079
+ const baseSchema = parameters ? structuredClone(parameters) : {
6080
+ type: "object",
6081
+ properties: {},
6082
+ required: []
6083
+ };
6084
+ if (baseSchema.properties?.model) {
6085
+ return baseSchema;
6086
+ }
6087
+ const modelProperty = {
6088
+ type: "string",
6089
+ enum: models.map((m) => m.key),
6090
+ description: `The AI model to use for this function call. Available options: ${models.map((m) => `${m.key}: ${m.description}`).join(" | ")}`
6091
+ };
6092
+ const newProperties = {
6093
+ ...baseSchema.properties ?? {},
6094
+ model: modelProperty
6095
+ };
6096
+ const newRequired = [...baseSchema.required ?? [], "model"];
6097
+ return {
6098
+ ...baseSchema,
6099
+ properties: newProperties,
6100
+ required: newRequired
6101
+ };
6102
+ }
6031
6103
 
6032
6104
  // docs/tika.ts
6033
6105
  import { createReadStream } from "node:fs";
@@ -6110,7 +6182,7 @@ var AxBalancer = class _AxBalancer {
6110
6182
  const bTotalCost = (bInfo.promptTokenCostPer1M || Infinity) + (bInfo.completionTokenCostPer1M || Infinity);
6111
6183
  return aTotalCost - bTotalCost;
6112
6184
  };
6113
- getModelMap() {
6185
+ getModelList() {
6114
6186
  throw new Error("Method not implemented.");
6115
6187
  }
6116
6188
  getNextService() {
@@ -7727,8 +7799,8 @@ var AxMockAIService = class {
7727
7799
  streaming: this.config.features?.streaming ?? false
7728
7800
  };
7729
7801
  }
7730
- getModelMap() {
7731
- return this.config.modelMap;
7802
+ getModelList() {
7803
+ return this.config.models;
7732
7804
  }
7733
7805
  getMetrics() {
7734
7806
  return this.metrics;