@ax-llm/ax 11.0.42 → 11.0.45

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,6 +5,7 @@ import { SpanKind } from "@opentelemetry/api";
5
5
  var axSpanAttributes = {
6
6
  // LLM
7
7
  LLM_SYSTEM: "gen_ai.system",
8
+ LLM_OPERATION_NAME: "gen_ai.operation.name",
8
9
  LLM_REQUEST_MODEL: "gen_ai.request.model",
9
10
  LLM_REQUEST_MAX_TOKENS: "gen_ai.request.max_tokens",
10
11
  LLM_REQUEST_TEMPERATURE: "gen_ai.request.temperature",
@@ -14,8 +15,10 @@ var axSpanAttributes = {
14
15
  LLM_REQUEST_STOP_SEQUENCES: "gen_ai.request.stop_sequences",
15
16
  LLM_REQUEST_LLM_IS_STREAMING: "gen_ai.request.llm_is_streaming",
16
17
  LLM_REQUEST_TOP_P: "gen_ai.request.top_p",
17
- LLM_USAGE_PROMPT_TOKENS: "gen_ai.usage.prompt_tokens",
18
- LLM_USAGE_COMPLETION_TOKENS: "gen_ai.usage.completion_tokens",
18
+ LLM_USAGE_INPUT_TOKENS: "gen_ai.usage.input_tokens",
19
+ LLM_USAGE_OUTPUT_TOKENS: "gen_ai.usage.output_tokens",
20
+ LLM_USAGE_TOTAL_TOKENS: "gen_ai.usage.total_tokens",
21
+ LLM_USAGE_THOUGHTS_TOKENS: "gen_ai.usage.thoughts_tokens",
19
22
  // Vector DB
20
23
  DB_SYSTEM: "db.system",
21
24
  DB_TABLE: "db.table",
@@ -38,7 +41,13 @@ var axSpanAttributes = {
38
41
  DB_QUERY_RESULT_DOCUMENT: "db.query.result.document"
39
42
  };
40
43
  var axSpanEvents = {
41
- LLM_PROMPT: "gen_ai.prompt"
44
+ GEN_AI_USER_MESSAGE: "gen_ai.user.message",
45
+ GEN_AI_SYSTEM_MESSAGE: "gen_ai.system.message",
46
+ GEN_AI_ASSISTANT_MESSAGE: "gen_ai.assistant.message",
47
+ GEN_AI_TOOL_MESSAGE: "gen_ai.tool.message",
48
+ // For tool messages in request & response tool calls
49
+ GEN_AI_CHOICE: "gen_ai.choice",
50
+ GEN_AI_USAGE: "gen_ai.usage"
42
51
  };
43
52
  var AxLLMRequestTypeValues = /* @__PURE__ */ ((AxLLMRequestTypeValues2) => {
44
53
  AxLLMRequestTypeValues2["COMPLETION"] = "completion";
@@ -482,9 +491,6 @@ var apiCall = async (api, json) => {
482
491
  } finally {
483
492
  clearTimeout(timeoutId);
484
493
  reader.releaseLock();
485
- if (api.span?.isRecording()) {
486
- api.span.end();
487
- }
488
494
  }
489
495
  }
490
496
  read();
@@ -532,9 +538,6 @@ var apiCall = async (api, json) => {
532
538
  if (timeoutId !== void 0) {
533
539
  clearTimeout(timeoutId);
534
540
  }
535
- if (api.span?.isRecording()) {
536
- api.span.end();
537
- }
538
541
  }
539
542
  }
540
543
  };
@@ -750,6 +753,7 @@ var AxBaseAI = class {
750
753
  fetch;
751
754
  tracer;
752
755
  timeout;
756
+ excludeContentFromTrace;
753
757
  models;
754
758
  modelInfo;
755
759
  modelUsage;
@@ -810,13 +814,16 @@ var AxBaseAI = class {
810
814
  this.fetch = options.fetch;
811
815
  this.timeout = options.timeout;
812
816
  this.tracer = options.tracer;
817
+ this.excludeContentFromTrace = options.excludeContentFromTrace;
813
818
  }
814
819
  getOptions() {
815
820
  return {
816
821
  debug: this.debug,
817
822
  rateLimiter: this.rt,
818
823
  fetch: this.fetch,
819
- tracer: this.tracer
824
+ tracer: this.tracer,
825
+ timeout: this.timeout,
826
+ excludeContentFromTrace: this.excludeContentFromTrace
820
827
  };
821
828
  }
822
829
  getModelList() {
@@ -916,11 +923,12 @@ var AxBaseAI = class {
916
923
  }
917
924
  if (this.tracer) {
918
925
  return await this.tracer?.startActiveSpan(
919
- "Chat Request",
926
+ "AI Chat Request",
920
927
  {
921
928
  kind: SpanKind.SERVER,
922
929
  attributes: {
923
930
  [axSpanAttributes.LLM_SYSTEM]: this.name,
931
+ [axSpanAttributes.LLM_OPERATION_NAME]: "chat",
924
932
  [axSpanAttributes.LLM_REQUEST_MODEL]: model,
925
933
  [axSpanAttributes.LLM_REQUEST_MAX_TOKENS]: modelConfig.maxTokens,
926
934
  [axSpanAttributes.LLM_REQUEST_TEMPERATURE]: modelConfig.temperature,
@@ -930,17 +938,10 @@ var AxBaseAI = class {
930
938
  [axSpanAttributes.LLM_REQUEST_PRESENCE_PENALTY]: modelConfig.presencePenalty,
931
939
  [axSpanAttributes.LLM_REQUEST_STOP_SEQUENCES]: modelConfig.stopSequences?.join(", "),
932
940
  [axSpanAttributes.LLM_REQUEST_LLM_IS_STREAMING]: modelConfig.stream
933
- // [AxSpanAttributes.LLM_PROMPTS]: _req.chatPrompt
934
- // ?.map((v) => v.content)
935
- // .join('\n')
936
941
  }
937
942
  },
938
943
  async (span) => {
939
- try {
940
- return await this._chat2(model, modelConfig, req, options, span);
941
- } finally {
942
- span.end();
943
- }
944
+ return await this._chat2(model, modelConfig, req, options, span);
944
945
  }
945
946
  );
946
947
  }
@@ -986,6 +987,9 @@ var AxBaseAI = class {
986
987
  req,
987
988
  options
988
989
  );
990
+ if (span?.isRecording()) {
991
+ setChatRequestEvents(chatReq, span, this.excludeContentFromTrace);
992
+ }
989
993
  const res2 = await apiCall(
990
994
  {
991
995
  name: apiConfig.name,
@@ -1023,7 +1027,7 @@ var AxBaseAI = class {
1023
1027
  }
1024
1028
  this.modelUsage = res2.modelUsage;
1025
1029
  if (span?.isRecording()) {
1026
- setResponseAttr(res2, span);
1030
+ setChatResponseEvents(res2, span, this.excludeContentFromTrace);
1027
1031
  }
1028
1032
  if (debug) {
1029
1033
  logResponse(res2);
@@ -1034,6 +1038,9 @@ var AxBaseAI = class {
1034
1038
  if (debug) {
1035
1039
  process.stdout.write("\n");
1036
1040
  }
1041
+ if (span?.isRecording()) {
1042
+ span.end();
1043
+ }
1037
1044
  };
1038
1045
  const st = rv.pipeThrough(
1039
1046
  new RespTransformStream(
@@ -1048,16 +1055,26 @@ var AxBaseAI = class {
1048
1055
  }
1049
1056
  const res = this.aiImpl.createChatResp(rv);
1050
1057
  res.sessionId = options?.sessionId;
1058
+ if (!res.modelUsage) {
1059
+ const tokenUsage = this.aiImpl.getTokenUsage();
1060
+ if (tokenUsage) {
1061
+ res.modelUsage = {
1062
+ ai: this.name,
1063
+ model,
1064
+ tokens: tokenUsage
1065
+ };
1066
+ }
1067
+ }
1051
1068
  if (res.modelUsage) {
1052
1069
  this.modelUsage = res.modelUsage;
1053
1070
  }
1054
1071
  if (span?.isRecording()) {
1055
- setResponseAttr(res, span);
1072
+ setChatResponseEvents(res, span, this.excludeContentFromTrace);
1073
+ span.end();
1056
1074
  }
1057
1075
  if (debug) {
1058
1076
  logResponse(res);
1059
1077
  }
1060
- span?.end();
1061
1078
  return res;
1062
1079
  }
1063
1080
  async embed(req, options) {
@@ -1081,11 +1098,12 @@ var AxBaseAI = class {
1081
1098
  }
1082
1099
  if (this.tracer) {
1083
1100
  await this.tracer?.startActiveSpan(
1084
- "Embed Request",
1101
+ "AI Embed Request",
1085
1102
  {
1086
1103
  kind: SpanKind.SERVER,
1087
1104
  attributes: {
1088
1105
  [axSpanAttributes.LLM_SYSTEM]: this.name,
1106
+ [axSpanAttributes.LLM_OPERATION_NAME]: "embeddings",
1089
1107
  [axSpanAttributes.LLM_REQUEST_MODEL]: embedModel
1090
1108
  }
1091
1109
  },
@@ -1140,8 +1158,12 @@ var AxBaseAI = class {
1140
1158
  };
1141
1159
  }
1142
1160
  this.embedModelUsage = res.modelUsage;
1143
- if (span?.isRecording()) {
1144
- setResponseAttr(res, span);
1161
+ if (span?.isRecording() && res.modelUsage?.tokens) {
1162
+ span.addEvent(axSpanEvents.GEN_AI_USAGE, {
1163
+ [axSpanAttributes.LLM_USAGE_INPUT_TOKENS]: res.modelUsage.tokens.promptTokens,
1164
+ [axSpanAttributes.LLM_USAGE_OUTPUT_TOKENS]: res.modelUsage.tokens.completionTokens ?? 0,
1165
+ [axSpanAttributes.LLM_USAGE_TOTAL_TOKENS]: res.modelUsage.tokens.totalTokens
1166
+ });
1145
1167
  }
1146
1168
  span?.end();
1147
1169
  return res;
@@ -1165,11 +1187,113 @@ var AxBaseAI = class {
1165
1187
  return item && "embedModel" in item ? item.embedModel : void 0;
1166
1188
  }
1167
1189
  };
1168
- function setResponseAttr(res, span) {
1169
- if (res.modelUsage) {
1170
- span.setAttributes({
1171
- [axSpanAttributes.LLM_USAGE_COMPLETION_TOKENS]: res.modelUsage.tokens?.completionTokens ?? 0,
1172
- [axSpanAttributes.LLM_USAGE_PROMPT_TOKENS]: res.modelUsage.tokens?.promptTokens
1190
+ function setChatRequestEvents(req, span, excludeContentFromTrace) {
1191
+ const userMessages = [];
1192
+ if (req.chatPrompt && Array.isArray(req.chatPrompt) && req.chatPrompt.length > 0) {
1193
+ for (const prompt of req.chatPrompt) {
1194
+ switch (prompt.role) {
1195
+ case "system":
1196
+ if (prompt.content) {
1197
+ const eventData2 = {};
1198
+ if (!excludeContentFromTrace) {
1199
+ eventData2.content = prompt.content;
1200
+ }
1201
+ span.addEvent(axSpanEvents.GEN_AI_SYSTEM_MESSAGE, eventData2);
1202
+ }
1203
+ break;
1204
+ case "user":
1205
+ if (typeof prompt.content === "string") {
1206
+ userMessages.push(prompt.content);
1207
+ } else if (Array.isArray(prompt.content)) {
1208
+ for (const part of prompt.content) {
1209
+ if (part.type === "text") {
1210
+ userMessages.push(part.text);
1211
+ }
1212
+ }
1213
+ }
1214
+ break;
1215
+ case "assistant":
1216
+ const functionCalls = prompt.functionCalls?.map((call) => {
1217
+ return {
1218
+ id: call.id,
1219
+ type: call.type,
1220
+ function: call.function.name,
1221
+ arguments: call.function.params
1222
+ };
1223
+ });
1224
+ if (functionCalls && functionCalls.length > 0) {
1225
+ const eventData2 = {
1226
+ function_calls: JSON.stringify(functionCalls, null, 2)
1227
+ };
1228
+ if (!excludeContentFromTrace && prompt.content) {
1229
+ eventData2.content = prompt.content;
1230
+ }
1231
+ span.addEvent(axSpanEvents.GEN_AI_ASSISTANT_MESSAGE, eventData2);
1232
+ } else if (prompt.content) {
1233
+ const eventData2 = {};
1234
+ if (!excludeContentFromTrace) {
1235
+ eventData2.content = prompt.content;
1236
+ }
1237
+ span.addEvent(axSpanEvents.GEN_AI_ASSISTANT_MESSAGE, eventData2);
1238
+ }
1239
+ break;
1240
+ case "function":
1241
+ const eventData = {
1242
+ id: prompt.functionId
1243
+ };
1244
+ if (!excludeContentFromTrace) {
1245
+ eventData.content = prompt.result;
1246
+ }
1247
+ span.addEvent(axSpanEvents.GEN_AI_TOOL_MESSAGE, eventData);
1248
+ break;
1249
+ }
1250
+ }
1251
+ }
1252
+ const userEventData = {};
1253
+ if (!excludeContentFromTrace) {
1254
+ userEventData.content = userMessages.join("\n");
1255
+ }
1256
+ span.addEvent(axSpanEvents.GEN_AI_USER_MESSAGE, userEventData);
1257
+ }
1258
+ function setChatResponseEvents(res, span, excludeContentFromTrace) {
1259
+ if (res.modelUsage?.tokens) {
1260
+ const thoughTokens = res.modelUsage.tokens.thoughtsTokens ? {
1261
+ [axSpanAttributes.LLM_USAGE_THOUGHTS_TOKENS]: res.modelUsage.tokens.thoughtsTokens
1262
+ } : {};
1263
+ span.addEvent(axSpanEvents.GEN_AI_USAGE, {
1264
+ [axSpanAttributes.LLM_USAGE_INPUT_TOKENS]: res.modelUsage.tokens.promptTokens,
1265
+ [axSpanAttributes.LLM_USAGE_OUTPUT_TOKENS]: res.modelUsage.tokens.completionTokens ?? 0,
1266
+ [axSpanAttributes.LLM_USAGE_TOTAL_TOKENS]: res.modelUsage.tokens.totalTokens,
1267
+ ...thoughTokens
1268
+ });
1269
+ }
1270
+ if (!res.results) {
1271
+ return;
1272
+ }
1273
+ for (const [index, result] of res.results.entries()) {
1274
+ const toolCalls = result.functionCalls?.map((call) => {
1275
+ return {
1276
+ id: call.id,
1277
+ type: call.type,
1278
+ function: call.function.name,
1279
+ arguments: call.function.params
1280
+ };
1281
+ });
1282
+ let message = {};
1283
+ if (toolCalls && toolCalls.length > 0) {
1284
+ if (!excludeContentFromTrace) {
1285
+ message.content = result.content;
1286
+ }
1287
+ message.tool_calls = toolCalls;
1288
+ } else {
1289
+ if (!excludeContentFromTrace) {
1290
+ message.content = result.content ?? "";
1291
+ }
1292
+ }
1293
+ span.addEvent(axSpanEvents.GEN_AI_CHOICE, {
1294
+ finish_reason: result.finishReason,
1295
+ index,
1296
+ message: JSON.stringify(message, null, 2)
1173
1297
  });
1174
1298
  }
1175
1299
  }
@@ -1826,9 +1950,10 @@ var axAIOpenAIFastConfig = () => ({
1826
1950
  model: "gpt-4.1-mini" /* GPT41Mini */
1827
1951
  });
1828
1952
  var AxAIOpenAIImpl = class {
1829
- constructor(config, streamingUsage) {
1953
+ constructor(config, streamingUsage, chatReqUpdater) {
1830
1954
  this.config = config;
1831
1955
  this.streamingUsage = streamingUsage;
1956
+ this.chatReqUpdater = chatReqUpdater;
1832
1957
  }
1833
1958
  tokensUsed;
1834
1959
  getTokenUsage() {
@@ -1848,7 +1973,7 @@ var AxAIOpenAIImpl = class {
1848
1973
  stream: config.stream
1849
1974
  };
1850
1975
  }
1851
- createChatReq(req, _config) {
1976
+ createChatReq(req, config) {
1852
1977
  const model = req.model;
1853
1978
  if (!req.chatPrompt || req.chatPrompt.length === 0) {
1854
1979
  throw new Error("Chat prompt is empty");
@@ -1868,12 +1993,11 @@ var AxAIOpenAIImpl = class {
1868
1993
  const messages = createMessages2(req);
1869
1994
  const frequencyPenalty = req.modelConfig?.frequencyPenalty ?? this.config.frequencyPenalty;
1870
1995
  const stream = req.modelConfig?.stream ?? this.config.stream;
1871
- const reasoningEffort = isReasoningModel(model) ? this.config.reasoningEffort : void 0;
1872
1996
  const store = this.config.store;
1873
- const reqValue = {
1997
+ let reqValue = {
1874
1998
  model,
1875
1999
  messages,
1876
- response_format: this.config?.responseFormat ? { type: this.config?.responseFormat } : void 0,
2000
+ response_format: this.config?.responseFormat ? { type: this.config.responseFormat } : void 0,
1877
2001
  tools,
1878
2002
  tool_choice: toolsChoice,
1879
2003
  max_completion_tokens: req.modelConfig?.maxTokens ?? this.config.maxTokens ?? 500,
@@ -1885,9 +2009,29 @@ var AxAIOpenAIImpl = class {
1885
2009
  logit_bias: this.config.logitBias,
1886
2010
  ...frequencyPenalty ? { frequency_penalty: frequencyPenalty } : {},
1887
2011
  ...stream && this.streamingUsage ? { stream: true, stream_options: { include_usage: true } } : {},
1888
- ...reasoningEffort ? { reasoning_effort: reasoningEffort } : {},
1889
2012
  ...store ? { store } : {}
1890
2013
  };
2014
+ if (this.config.reasoningEffort) {
2015
+ reqValue.reasoning_effort = this.config.reasoningEffort;
2016
+ }
2017
+ if (config.thinkingTokenBudget) {
2018
+ switch (config.thinkingTokenBudget) {
2019
+ case "minimal":
2020
+ reqValue.reasoning_effort = "low";
2021
+ break;
2022
+ case "low":
2023
+ reqValue.reasoning_effort = "medium";
2024
+ break;
2025
+ case "medium":
2026
+ reqValue.reasoning_effort = "high";
2027
+ break;
2028
+ case "high":
2029
+ reqValue.reasoning_effort = "high";
2030
+ }
2031
+ }
2032
+ if (this.chatReqUpdater) {
2033
+ reqValue = this.chatReqUpdater(reqValue);
2034
+ }
1891
2035
  return [apiConfig, reqValue];
1892
2036
  }
1893
2037
  createEmbedReq(req) {
@@ -2070,14 +2214,16 @@ var AxAIOpenAIBase = class extends AxBaseAI {
2070
2214
  options,
2071
2215
  apiURL,
2072
2216
  modelInfo,
2073
- models
2217
+ models,
2218
+ chatReqUpdater
2074
2219
  }) {
2075
2220
  if (!apiKey || apiKey === "") {
2076
2221
  throw new Error("OpenAI API key not set");
2077
2222
  }
2078
2223
  const aiImpl = new AxAIOpenAIImpl(
2079
2224
  config,
2080
- options?.streamingUsage ?? true
2225
+ options?.streamingUsage ?? true,
2226
+ chatReqUpdater
2081
2227
  );
2082
2228
  super(aiImpl, {
2083
2229
  name: "OpenAI",
@@ -2096,9 +2242,6 @@ var AxAIOpenAIBase = class extends AxBaseAI {
2096
2242
  });
2097
2243
  }
2098
2244
  };
2099
- var isReasoningModel = (model) => ["o1-mini" /* O1Mini */, "o1" /* O1 */, "o3-mini" /* O3Mini */].includes(
2100
- model
2101
- );
2102
2245
  var AxAIOpenAI = class extends AxAIOpenAIBase {
2103
2246
  constructor({
2104
2247
  apiKey,
@@ -2562,7 +2705,7 @@ var AxAIDeepSeek = class extends AxAIOpenAIBase {
2562
2705
 
2563
2706
  // ai/google-gemini/types.ts
2564
2707
  var AxAIGoogleGeminiModel = /* @__PURE__ */ ((AxAIGoogleGeminiModel2) => {
2565
- AxAIGoogleGeminiModel2["Gemini25Pro"] = "gemini-2.5-pro-preview-03-25";
2708
+ AxAIGoogleGeminiModel2["Gemini25Pro"] = "gemini-2.5-pro-preview-05-06";
2566
2709
  AxAIGoogleGeminiModel2["Gemini25Flash"] = "gemini-2.5-flash-preview-04-17";
2567
2710
  AxAIGoogleGeminiModel2["Gemini20Flash"] = "gemini-2.0-flash";
2568
2711
  AxAIGoogleGeminiModel2["Gemini20FlashLite"] = "gemini-2.0-flash-lite-preview-02-05";
@@ -2611,7 +2754,7 @@ var AxAIGoogleGeminiEmbedTypes = /* @__PURE__ */ ((AxAIGoogleGeminiEmbedTypes2)
2611
2754
  // ai/google-gemini/info.ts
2612
2755
  var axModelInfoGoogleGemini = [
2613
2756
  {
2614
- name: "gemini-2.5-pro-preview-03-25" /* Gemini25Pro */,
2757
+ name: "gemini-2.5-pro-preview-05-06" /* Gemini25Pro */,
2615
2758
  currency: "usd",
2616
2759
  characterIsToken: false,
2617
2760
  promptTokenCostPer1M: 2.5,
@@ -2695,7 +2838,7 @@ var safetySettings = [
2695
2838
  }
2696
2839
  ];
2697
2840
  var axAIGoogleGeminiDefaultConfig = () => structuredClone({
2698
- model: "gemini-2.0-flash" /* Gemini20Flash */,
2841
+ model: "gemini-2.5-flash-preview-04-17" /* Gemini25Flash */,
2699
2842
  embedModel: "text-embedding-005" /* TextEmbedding005 */,
2700
2843
  safetySettings,
2701
2844
  ...axBaseAIDefaultConfig()
@@ -2736,7 +2879,7 @@ var AxAIGoogleGeminiImpl = class {
2736
2879
  n: config.n
2737
2880
  };
2738
2881
  }
2739
- createChatReq = (req) => {
2882
+ createChatReq = (req, config) => {
2740
2883
  const model = req.model;
2741
2884
  const stream = req.modelConfig?.stream ?? this.config.stream;
2742
2885
  if (!req.chatPrompt || req.chatPrompt.length === 0) {
@@ -2847,6 +2990,9 @@ var AxAIGoogleGeminiImpl = class {
2847
2990
  }
2848
2991
  });
2849
2992
  }
2993
+ if (this.options?.urlContext) {
2994
+ tools.push({ url_context: {} });
2995
+ }
2850
2996
  if (tools.length === 0) {
2851
2997
  tools = void 0;
2852
2998
  }
@@ -2872,6 +3018,29 @@ var AxAIGoogleGeminiImpl = class {
2872
3018
  } else if (tools && tools.length > 0) {
2873
3019
  toolConfig = { function_calling_config: { mode: "AUTO" } };
2874
3020
  }
3021
+ const thinkingConfig = {};
3022
+ if (this.config.thinking?.includeThoughts) {
3023
+ thinkingConfig.includeThoughts = true;
3024
+ }
3025
+ if (this.config.thinking?.thinkingTokenBudget) {
3026
+ thinkingConfig.thinkingBudget = this.config.thinking.thinkingTokenBudget;
3027
+ }
3028
+ if (config.thinkingTokenBudget) {
3029
+ switch (config.thinkingTokenBudget) {
3030
+ case "minimal":
3031
+ thinkingConfig.thinkingBudget = 0;
3032
+ break;
3033
+ case "low":
3034
+ thinkingConfig.thinkingBudget = 1024;
3035
+ break;
3036
+ case "medium":
3037
+ thinkingConfig.thinkingBudget = 4096;
3038
+ break;
3039
+ case "high":
3040
+ thinkingConfig.thinkingBudget = 8192;
3041
+ break;
3042
+ }
3043
+ }
2875
3044
  const generationConfig = {
2876
3045
  maxOutputTokens: req.modelConfig?.maxTokens ?? this.config.maxTokens,
2877
3046
  temperature: req.modelConfig?.temperature ?? this.config.temperature,
@@ -2881,9 +3050,7 @@ var AxAIGoogleGeminiImpl = class {
2881
3050
  candidateCount: 1,
2882
3051
  stopSequences: req.modelConfig?.stopSequences ?? this.config.stopSequences,
2883
3052
  responseMimeType: "text/plain",
2884
- ...this.config.thinkingConfig && {
2885
- thinkingConfig: this.config.thinkingConfig
2886
- }
3053
+ ...thinkingConfig ? { thinkingConfig } : {}
2887
3054
  };
2888
3055
  const safetySettings2 = this.config.safetySettings;
2889
3056
  const reqValue = {
@@ -2964,7 +3131,11 @@ var AxAIGoogleGeminiImpl = class {
2964
3131
  }
2965
3132
  for (const part of candidate.content.parts) {
2966
3133
  if ("text" in part) {
2967
- result.content = part.text;
3134
+ if ("thought" in part && part.thought) {
3135
+ result.thought = part.text;
3136
+ } else {
3137
+ result.content = part.text;
3138
+ }
2968
3139
  continue;
2969
3140
  }
2970
3141
  if ("functionCall" in part) {
@@ -2987,7 +3158,8 @@ var AxAIGoogleGeminiImpl = class {
2987
3158
  this.tokensUsed = {
2988
3159
  totalTokens: resp.usageMetadata.totalTokenCount,
2989
3160
  promptTokens: resp.usageMetadata.promptTokenCount,
2990
- completionTokens: resp.usageMetadata.candidatesTokenCount
3161
+ completionTokens: resp.usageMetadata.candidatesTokenCount,
3162
+ thoughtsTokens: resp.usageMetadata.thoughtsTokenCount
2991
3163
  };
2992
3164
  }
2993
3165
  return { results };
@@ -3833,6 +4005,82 @@ var AxAI = class {
3833
4005
  }
3834
4006
  };
3835
4007
 
4008
+ // ai/x-grok/types.ts
4009
+ var AxAIGrokModel = /* @__PURE__ */ ((AxAIGrokModel2) => {
4010
+ AxAIGrokModel2["Grok3"] = "grok-3";
4011
+ AxAIGrokModel2["Grok3Mini"] = "grok-3-mini";
4012
+ AxAIGrokModel2["Grok3Fast"] = "grok-3-fast";
4013
+ AxAIGrokModel2["Grok3MiniFast"] = "grok-3-mini-fast";
4014
+ return AxAIGrokModel2;
4015
+ })(AxAIGrokModel || {});
4016
+ var AxAIGrokEmbedModels = /* @__PURE__ */ ((AxAIGrokEmbedModels3) => {
4017
+ AxAIGrokEmbedModels3["GrokEmbedSmall"] = "grok-embed-small";
4018
+ return AxAIGrokEmbedModels3;
4019
+ })(AxAIGrokEmbedModels || {});
4020
+
4021
+ // ai/x-grok/info.ts
4022
+ var axModelInfoGrok = [
4023
+ {
4024
+ name: "grok-3" /* Grok3 */,
4025
+ currency: "USD",
4026
+ promptTokenCostPer1M: 3,
4027
+ completionTokenCostPer1M: 15
4028
+ },
4029
+ {
4030
+ name: "grok-3-mini" /* Grok3Mini */,
4031
+ currency: "USD",
4032
+ promptTokenCostPer1M: 0.3,
4033
+ completionTokenCostPer1M: 0.5
4034
+ },
4035
+ {
4036
+ name: "grok-3-fast" /* Grok3Fast */,
4037
+ currency: "USD",
4038
+ promptTokenCostPer1M: 5,
4039
+ completionTokenCostPer1M: 25
4040
+ },
4041
+ {
4042
+ name: "grok-3-mini-fast" /* Grok3MiniFast */,
4043
+ currency: "USD",
4044
+ promptTokenCostPer1M: 0.6,
4045
+ completionTokenCostPer1M: 4
4046
+ }
4047
+ ];
4048
+
4049
+ // ai/x-grok/api.ts
4050
+ var axAIGrokDefaultConfig = () => structuredClone({
4051
+ model: "grok-3-mini" /* Grok3Mini */,
4052
+ ...axBaseAIDefaultConfig()
4053
+ });
4054
+ var axAIGrokBestConfig = () => structuredClone({
4055
+ ...axAIGrokDefaultConfig(),
4056
+ model: "grok-3" /* Grok3 */
4057
+ });
4058
+ var AxAIGrok = class extends AxAIOpenAIBase {
4059
+ constructor({
4060
+ apiKey,
4061
+ config,
4062
+ options,
4063
+ models
4064
+ }) {
4065
+ if (!apiKey || apiKey === "") {
4066
+ throw new Error("Grok API key not set");
4067
+ }
4068
+ const _config = {
4069
+ ...axAIGrokDefaultConfig(),
4070
+ ...config
4071
+ };
4072
+ super({
4073
+ apiKey,
4074
+ config: _config,
4075
+ options,
4076
+ apiURL: "https://api.x.ai/v1",
4077
+ modelInfo: axModelInfoGrok,
4078
+ models
4079
+ });
4080
+ super.setName("Grok");
4081
+ }
4082
+ };
4083
+
3836
4084
  // dsp/generate.ts
3837
4085
  import { ReadableStream as ReadableStream3 } from "node:stream/web";
3838
4086
  import { SpanKind as SpanKind2 } from "@opentelemetry/api";
@@ -4578,6 +4826,14 @@ var AxSignature = class _AxSignature {
4578
4826
  };
4579
4827
  hash = () => this.sigHash;
4580
4828
  toString = () => this.sigString;
4829
+ toJSON = () => {
4830
+ return {
4831
+ id: this.hash(),
4832
+ description: this.description,
4833
+ inputFields: this.inputFields,
4834
+ outputFields: this.outputFields
4835
+ };
4836
+ };
4581
4837
  };
4582
4838
  function renderField(field) {
4583
4839
  let result = field.name;
@@ -6118,7 +6374,7 @@ var parseFunctions = (newFuncs, existingFuncs) => {
6118
6374
  }
6119
6375
  return [...existingFuncs ?? [], ...functions];
6120
6376
  };
6121
- var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, traceId) => {
6377
+ var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, traceId, span, excludeContentFromTelemetry) => {
6122
6378
  const funcProc = new AxFunctionProcessor(functionList);
6123
6379
  const functionsExecuted = /* @__PURE__ */ new Set();
6124
6380
  const promises = functionCalls.map((func) => {
@@ -6127,6 +6383,16 @@ var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, t
6127
6383
  }
6128
6384
  const promise = funcProc.execute(func, { sessionId, traceId, ai }).then((functionResult) => {
6129
6385
  functionsExecuted.add(func.name.toLowerCase());
6386
+ if (span) {
6387
+ const eventData = {
6388
+ name: func.name
6389
+ };
6390
+ if (!excludeContentFromTelemetry) {
6391
+ eventData.args = func.args;
6392
+ eventData.result = functionResult ?? "";
6393
+ }
6394
+ span.addEvent("function.call", eventData);
6395
+ }
6130
6396
  return {
6131
6397
  role: "function",
6132
6398
  result: functionResult ?? "",
@@ -6135,6 +6401,17 @@ var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, t
6135
6401
  }).catch((e) => {
6136
6402
  if (e instanceof FunctionError) {
6137
6403
  const result = e.getFixingInstructions();
6404
+ if (span) {
6405
+ const errorEventData = {
6406
+ name: func.name,
6407
+ message: e.toString()
6408
+ };
6409
+ if (!excludeContentFromTelemetry) {
6410
+ errorEventData.args = func.args;
6411
+ errorEventData.fixing_instructions = result;
6412
+ }
6413
+ span.addEvent("function.error", errorEventData);
6414
+ }
6138
6415
  mem.add(
6139
6416
  {
6140
6417
  role: "function",
@@ -6192,6 +6469,8 @@ var AxGen = class extends AxProgramWithSignature {
6192
6469
  functionsExecuted = /* @__PURE__ */ new Set();
6193
6470
  fieldProcessors = [];
6194
6471
  streamingFieldProcessors = [];
6472
+ values = {};
6473
+ excludeContentFromTrace = false;
6195
6474
  constructor(signature, options) {
6196
6475
  super(signature, { description: options?.description });
6197
6476
  this.options = options;
@@ -6201,6 +6480,7 @@ var AxGen = class extends AxProgramWithSignature {
6201
6480
  );
6202
6481
  this.asserts = this.options?.asserts ?? [];
6203
6482
  this.streamingAsserts = this.options?.streamingAsserts ?? [];
6483
+ this.excludeContentFromTrace = options?.excludeContentFromTrace ?? false;
6204
6484
  this.usage = [];
6205
6485
  if (options?.functions) {
6206
6486
  this.functions = parseFunctions(options.functions);
@@ -6249,7 +6529,8 @@ var AxGen = class extends AxProgramWithSignature {
6249
6529
  rateLimiter,
6250
6530
  stream,
6251
6531
  functions: _functions,
6252
- functionCall: _functionCall
6532
+ functionCall: _functionCall,
6533
+ thinkingTokenBudget
6253
6534
  } = options ?? {};
6254
6535
  const chatPrompt = mem?.history(sessionId) ?? [];
6255
6536
  if (chatPrompt.length === 0) {
@@ -6270,7 +6551,8 @@ var AxGen = class extends AxProgramWithSignature {
6270
6551
  traceId,
6271
6552
  rateLimiter,
6272
6553
  stream,
6273
- debug: false
6554
+ debug: false,
6555
+ thinkingTokenBudget
6274
6556
  }
6275
6557
  );
6276
6558
  return res;
@@ -6278,7 +6560,8 @@ var AxGen = class extends AxProgramWithSignature {
6278
6560
  async *forwardCore({
6279
6561
  ai,
6280
6562
  mem,
6281
- options
6563
+ options,
6564
+ span
6282
6565
  }) {
6283
6566
  const { sessionId, traceId, functions: _functions } = options ?? {};
6284
6567
  const fastFail = options?.fastFail ?? this.options?.fastFail;
@@ -6298,7 +6581,8 @@ var AxGen = class extends AxProgramWithSignature {
6298
6581
  traceId,
6299
6582
  sessionId,
6300
6583
  functions,
6301
- fastFail
6584
+ fastFail,
6585
+ span
6302
6586
  });
6303
6587
  } else {
6304
6588
  yield await this.processResponse({
@@ -6308,7 +6592,8 @@ var AxGen = class extends AxProgramWithSignature {
6308
6592
  mem,
6309
6593
  traceId,
6310
6594
  sessionId,
6311
- functions
6595
+ functions,
6596
+ span
6312
6597
  });
6313
6598
  }
6314
6599
  }
@@ -6320,11 +6605,12 @@ var AxGen = class extends AxProgramWithSignature {
6320
6605
  sessionId,
6321
6606
  traceId,
6322
6607
  functions,
6323
- fastFail
6608
+ fastFail,
6609
+ span
6324
6610
  }) {
6325
6611
  const streamingValidation = fastFail ?? ai.getFeatures(model).functionCot !== true;
6326
6612
  const functionCalls = [];
6327
- const values = {};
6613
+ this.values = {};
6328
6614
  const xstate = {
6329
6615
  extractedFields: [],
6330
6616
  streamedIndex: {},
@@ -6351,6 +6637,9 @@ var AxGen = class extends AxProgramWithSignature {
6351
6637
  sessionId
6352
6638
  );
6353
6639
  } else if (result.content) {
6640
+ if (result.thought && result.thought.length > 0) {
6641
+ yield { thought: result.thought };
6642
+ }
6354
6643
  content += result.content;
6355
6644
  mem.updateResult(
6356
6645
  { name: result.name, content, delta: result.content },
@@ -6358,7 +6647,7 @@ var AxGen = class extends AxProgramWithSignature {
6358
6647
  );
6359
6648
  const skip = streamingExtractValues(
6360
6649
  this.signature,
6361
- values,
6650
+ this.values,
6362
6651
  xstate,
6363
6652
  content,
6364
6653
  streamingValidation
@@ -6379,12 +6668,20 @@ var AxGen = class extends AxProgramWithSignature {
6379
6668
  content,
6380
6669
  xstate,
6381
6670
  mem,
6382
- values,
6671
+ this.values,
6383
6672
  sessionId
6384
6673
  );
6385
6674
  }
6386
- yield* streamValues(this.signature, content, values, xstate);
6387
- await assertAssertions(this.asserts, values);
6675
+ yield* streamValues(
6676
+ this.signature,
6677
+ content,
6678
+ this.values,
6679
+ xstate
6680
+ );
6681
+ await assertAssertions(this.asserts, this.values);
6682
+ } else if (result.thought && result.thought.length > 0) {
6683
+ this.values.thought = this.values.thought ?? "" + result.thought;
6684
+ yield { thought: result.thought };
6388
6685
  }
6389
6686
  if (result.finishReason === "length") {
6390
6687
  throw new Error(
@@ -6393,7 +6690,7 @@ Content: ${content}`
6393
6690
  );
6394
6691
  }
6395
6692
  }
6396
- const funcs = parseFunctionCalls(ai, functionCalls, values, model);
6693
+ const funcs = parseFunctionCalls(ai, functionCalls, this.values, model);
6397
6694
  if (funcs) {
6398
6695
  if (!functions) {
6399
6696
  throw new Error("Functions are not defined");
@@ -6404,22 +6701,24 @@ Content: ${content}`
6404
6701
  funcs,
6405
6702
  mem,
6406
6703
  sessionId,
6407
- traceId
6704
+ traceId,
6705
+ span,
6706
+ this.excludeContentFromTrace
6408
6707
  );
6409
6708
  this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
6410
6709
  } else {
6411
- streamingExtractFinalValue(this.signature, values, xstate, content);
6710
+ streamingExtractFinalValue(this.signature, this.values, xstate, content);
6412
6711
  await assertStreamingAssertions(
6413
6712
  this.streamingAsserts,
6414
6713
  xstate,
6415
6714
  content,
6416
6715
  true
6417
6716
  );
6418
- await assertAssertions(this.asserts, values);
6717
+ await assertAssertions(this.asserts, this.values);
6419
6718
  if (this.fieldProcessors.length) {
6420
6719
  await processFieldProcessors(
6421
6720
  this.fieldProcessors,
6422
- values,
6721
+ this.values,
6423
6722
  mem,
6424
6723
  sessionId
6425
6724
  );
@@ -6430,12 +6729,17 @@ Content: ${content}`
6430
6729
  content,
6431
6730
  xstate,
6432
6731
  mem,
6433
- values,
6732
+ this.values,
6434
6733
  sessionId,
6435
6734
  true
6436
6735
  );
6437
6736
  }
6438
- yield* streamValues(this.signature, content, values, xstate);
6737
+ yield* streamValues(
6738
+ this.signature,
6739
+ content,
6740
+ this.values,
6741
+ xstate
6742
+ );
6439
6743
  }
6440
6744
  }
6441
6745
  async processResponse({
@@ -6444,9 +6748,10 @@ Content: ${content}`
6444
6748
  mem,
6445
6749
  sessionId,
6446
6750
  traceId,
6447
- functions
6751
+ functions,
6752
+ span
6448
6753
  }) {
6449
- const values = {};
6754
+ this.values = {};
6450
6755
  let results = res.results ?? [];
6451
6756
  if (results.length > 1) {
6452
6757
  results = results.filter((r) => r.functionCalls);
@@ -6457,7 +6762,7 @@ Content: ${content}`
6457
6762
  }
6458
6763
  mem.addResult(result, sessionId);
6459
6764
  if (result.functionCalls?.length) {
6460
- const funcs = parseFunctionCalls(ai, result.functionCalls, values);
6765
+ const funcs = parseFunctionCalls(ai, result.functionCalls, this.values);
6461
6766
  if (funcs) {
6462
6767
  if (!functions) {
6463
6768
  throw new Error("Functions are not defined");
@@ -6468,17 +6773,22 @@ Content: ${content}`
6468
6773
  funcs,
6469
6774
  mem,
6470
6775
  sessionId,
6471
- traceId
6776
+ traceId,
6777
+ span,
6778
+ this.excludeContentFromTrace
6472
6779
  );
6473
6780
  this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
6474
6781
  }
6475
6782
  } else if (result.content) {
6476
- extractValues(this.signature, values, result.content);
6477
- await assertAssertions(this.asserts, values);
6783
+ if (result.thought && result.thought.length > 0) {
6784
+ this.values.thought = result.thought;
6785
+ }
6786
+ extractValues(this.signature, this.values, result.content);
6787
+ await assertAssertions(this.asserts, this.values);
6478
6788
  if (this.fieldProcessors.length) {
6479
6789
  await processFieldProcessors(
6480
6790
  this.fieldProcessors,
6481
- values,
6791
+ this.values,
6482
6792
  mem,
6483
6793
  sessionId
6484
6794
  );
@@ -6491,13 +6801,12 @@ Content: ${result.content}`
6491
6801
  );
6492
6802
  }
6493
6803
  }
6494
- const publicValues = { ...values };
6495
6804
  for (const field of this.signature.getOutputFields()) {
6496
6805
  if (field.isInternal) {
6497
- delete publicValues[field.name];
6806
+ delete this.values[field.name];
6498
6807
  }
6499
6808
  }
6500
- return { ...values };
6809
+ return { ...this.values };
6501
6810
  }
6502
6811
  async *_forward2(ai, values, options, span) {
6503
6812
  const stopFunction = (options?.stopFunction ?? this.options?.stopFunction)?.toLowerCase();
@@ -6523,7 +6832,7 @@ Content: ${result.content}`
6523
6832
  multiStepLoop: for (let n = 0; n < maxSteps; n++) {
6524
6833
  for (let errCount = 0; errCount < maxRetries; errCount++) {
6525
6834
  try {
6526
- const generator = this.forwardCore({ options, ai, mem });
6835
+ const generator = this.forwardCore({ options, ai, mem, span });
6527
6836
  for await (const delta of generator) {
6528
6837
  if (delta !== void 0) {
6529
6838
  yield { version: errCount, delta };
@@ -6547,10 +6856,22 @@ Content: ${result.content}`
6547
6856
  if (e instanceof ValidationError) {
6548
6857
  errorFields = e.getFixingInstructions();
6549
6858
  err = e;
6859
+ if (span) {
6860
+ span.addEvent("validation.error", {
6861
+ message: e.toString(),
6862
+ fixing_instructions: errorFields?.map((f) => f.title).join(", ") ?? ""
6863
+ });
6864
+ }
6550
6865
  } else if (e instanceof AxAssertionError) {
6551
6866
  const e1 = e;
6552
6867
  errorFields = e1.getFixingInstructions();
6553
6868
  err = e;
6869
+ if (span) {
6870
+ span.addEvent("assertion.error", {
6871
+ message: e1.toString(),
6872
+ fixing_instructions: errorFields?.map((f) => f.title).join(", ") ?? ""
6873
+ });
6874
+ }
6554
6875
  } else if (e instanceof AxAIServiceStreamTerminatedError) {
6555
6876
  } else {
6556
6877
  throw enhanceError(e, ai, this.signature);
@@ -6605,14 +6926,25 @@ Content: ${result.content}`
6605
6926
  }
6606
6927
  const funcNames = functions?.map((f) => f.name).join(",");
6607
6928
  const attributes = {
6608
- "generate.signature": this.signature.toString(),
6609
- "generate.functions": funcNames ?? ""
6929
+ signature: JSON.stringify(this.signature.toJSON(), null, 2),
6930
+ ...this.examples ? { examples: JSON.stringify(this.examples, null, 2) } : {},
6931
+ ...funcNames ? { provided_functions: funcNames } : {},
6932
+ ...options?.model ? { model: options.model } : {},
6933
+ ...options?.thinkingTokenBudget ? { thinking_token_budget: options.thinkingTokenBudget } : {},
6934
+ ...options?.maxSteps ? { max_steps: options.maxSteps } : {},
6935
+ ...options?.maxRetries ? { max_retries: options.maxRetries } : {},
6936
+ ...options?.fastFail ? { fast_fail: options.fastFail } : {}
6610
6937
  };
6611
- const span = tracer.startSpan("Generate", {
6938
+ const traceLabel = options.traceLabel ?? this.options?.traceLabel;
6939
+ const spanName = traceLabel ? `${traceLabel} (AxGen)` : "AxGen";
6940
+ const span = tracer.startSpan(spanName, {
6612
6941
  kind: SpanKind2.SERVER,
6613
6942
  attributes
6614
6943
  });
6615
6944
  try {
6945
+ if (!this.excludeContentFromTrace) {
6946
+ span.addEvent("input", { content: JSON.stringify(values, null, 2) });
6947
+ }
6616
6948
  yield* this._forward2(
6617
6949
  ai,
6618
6950
  values,
@@ -6622,6 +6954,11 @@ Content: ${result.content}`
6622
6954
  },
6623
6955
  span
6624
6956
  );
6957
+ if (!this.excludeContentFromTrace) {
6958
+ span.addEvent("output", {
6959
+ content: JSON.stringify(this.values, null, 2)
6960
+ });
6961
+ }
6625
6962
  } finally {
6626
6963
  span.end();
6627
6964
  }
@@ -11378,6 +11715,9 @@ export {
11378
11715
  AxAIGoogleGeminiModel,
11379
11716
  AxAIGoogleGeminiSafetyCategory,
11380
11717
  AxAIGoogleGeminiSafetyThreshold,
11718
+ AxAIGrok,
11719
+ AxAIGrokEmbedModels,
11720
+ AxAIGrokModel,
11381
11721
  AxAIGroq,
11382
11722
  AxAIGroqModel,
11383
11723
  AxAIHuggingFace,
@@ -11458,6 +11798,8 @@ export {
11458
11798
  axAIDeepSeekDefaultConfig,
11459
11799
  axAIGoogleGeminiDefaultConfig,
11460
11800
  axAIGoogleGeminiDefaultCreativeConfig,
11801
+ axAIGrokBestConfig,
11802
+ axAIGrokDefaultConfig,
11461
11803
  axAIHuggingFaceCreativeConfig,
11462
11804
  axAIHuggingFaceDefaultConfig,
11463
11805
  axAIMistralBestConfig,
@@ -11479,6 +11821,7 @@ export {
11479
11821
  axModelInfoCohere,
11480
11822
  axModelInfoDeepSeek,
11481
11823
  axModelInfoGoogleGemini,
11824
+ axModelInfoGrok,
11482
11825
  axModelInfoGroq,
11483
11826
  axModelInfoHuggingFace,
11484
11827
  axModelInfoMistral,