@ax-llm/ax 11.0.42 → 11.0.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs +305 -72
- package/index.cjs.map +1 -1
- package/index.d.cts +46 -7
- package/index.d.ts +46 -7
- package/index.js +305 -72
- package/index.js.map +1 -1
- package/package.json +1 -1
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
|
-
|
|
18
|
-
|
|
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
|
-
|
|
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() {
|
|
@@ -914,13 +921,18 @@ var AxBaseAI = class {
|
|
|
914
921
|
if (!canStream) {
|
|
915
922
|
modelConfig.stream = false;
|
|
916
923
|
}
|
|
924
|
+
const canSetThinkingTokenBudget = this.getFeatures(model).thinkingTokenBudget;
|
|
925
|
+
if (!canSetThinkingTokenBudget && options?.thinkingTokenBudget) {
|
|
926
|
+
throw new Error("Thinking token budget is not supported for this model");
|
|
927
|
+
}
|
|
917
928
|
if (this.tracer) {
|
|
918
929
|
return await this.tracer?.startActiveSpan(
|
|
919
|
-
"Chat Request",
|
|
930
|
+
"AI Chat Request",
|
|
920
931
|
{
|
|
921
932
|
kind: SpanKind.SERVER,
|
|
922
933
|
attributes: {
|
|
923
934
|
[axSpanAttributes.LLM_SYSTEM]: this.name,
|
|
935
|
+
[axSpanAttributes.LLM_OPERATION_NAME]: "chat",
|
|
924
936
|
[axSpanAttributes.LLM_REQUEST_MODEL]: model,
|
|
925
937
|
[axSpanAttributes.LLM_REQUEST_MAX_TOKENS]: modelConfig.maxTokens,
|
|
926
938
|
[axSpanAttributes.LLM_REQUEST_TEMPERATURE]: modelConfig.temperature,
|
|
@@ -930,17 +942,10 @@ var AxBaseAI = class {
|
|
|
930
942
|
[axSpanAttributes.LLM_REQUEST_PRESENCE_PENALTY]: modelConfig.presencePenalty,
|
|
931
943
|
[axSpanAttributes.LLM_REQUEST_STOP_SEQUENCES]: modelConfig.stopSequences?.join(", "),
|
|
932
944
|
[axSpanAttributes.LLM_REQUEST_LLM_IS_STREAMING]: modelConfig.stream
|
|
933
|
-
// [AxSpanAttributes.LLM_PROMPTS]: _req.chatPrompt
|
|
934
|
-
// ?.map((v) => v.content)
|
|
935
|
-
// .join('\n')
|
|
936
945
|
}
|
|
937
946
|
},
|
|
938
947
|
async (span) => {
|
|
939
|
-
|
|
940
|
-
return await this._chat2(model, modelConfig, req, options, span);
|
|
941
|
-
} finally {
|
|
942
|
-
span.end();
|
|
943
|
-
}
|
|
948
|
+
return await this._chat2(model, modelConfig, req, options, span);
|
|
944
949
|
}
|
|
945
950
|
);
|
|
946
951
|
}
|
|
@@ -986,6 +991,9 @@ var AxBaseAI = class {
|
|
|
986
991
|
req,
|
|
987
992
|
options
|
|
988
993
|
);
|
|
994
|
+
if (span?.isRecording()) {
|
|
995
|
+
setChatRequestEvents(chatReq, span, this.excludeContentFromTrace);
|
|
996
|
+
}
|
|
989
997
|
const res2 = await apiCall(
|
|
990
998
|
{
|
|
991
999
|
name: apiConfig.name,
|
|
@@ -1023,7 +1031,7 @@ var AxBaseAI = class {
|
|
|
1023
1031
|
}
|
|
1024
1032
|
this.modelUsage = res2.modelUsage;
|
|
1025
1033
|
if (span?.isRecording()) {
|
|
1026
|
-
|
|
1034
|
+
setChatResponseEvents(res2, span, this.excludeContentFromTrace);
|
|
1027
1035
|
}
|
|
1028
1036
|
if (debug) {
|
|
1029
1037
|
logResponse(res2);
|
|
@@ -1034,6 +1042,9 @@ var AxBaseAI = class {
|
|
|
1034
1042
|
if (debug) {
|
|
1035
1043
|
process.stdout.write("\n");
|
|
1036
1044
|
}
|
|
1045
|
+
if (span?.isRecording()) {
|
|
1046
|
+
span.end();
|
|
1047
|
+
}
|
|
1037
1048
|
};
|
|
1038
1049
|
const st = rv.pipeThrough(
|
|
1039
1050
|
new RespTransformStream(
|
|
@@ -1048,16 +1059,26 @@ var AxBaseAI = class {
|
|
|
1048
1059
|
}
|
|
1049
1060
|
const res = this.aiImpl.createChatResp(rv);
|
|
1050
1061
|
res.sessionId = options?.sessionId;
|
|
1062
|
+
if (!res.modelUsage) {
|
|
1063
|
+
const tokenUsage = this.aiImpl.getTokenUsage();
|
|
1064
|
+
if (tokenUsage) {
|
|
1065
|
+
res.modelUsage = {
|
|
1066
|
+
ai: this.name,
|
|
1067
|
+
model,
|
|
1068
|
+
tokens: tokenUsage
|
|
1069
|
+
};
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1051
1072
|
if (res.modelUsage) {
|
|
1052
1073
|
this.modelUsage = res.modelUsage;
|
|
1053
1074
|
}
|
|
1054
1075
|
if (span?.isRecording()) {
|
|
1055
|
-
|
|
1076
|
+
setChatResponseEvents(res, span, this.excludeContentFromTrace);
|
|
1077
|
+
span.end();
|
|
1056
1078
|
}
|
|
1057
1079
|
if (debug) {
|
|
1058
1080
|
logResponse(res);
|
|
1059
1081
|
}
|
|
1060
|
-
span?.end();
|
|
1061
1082
|
return res;
|
|
1062
1083
|
}
|
|
1063
1084
|
async embed(req, options) {
|
|
@@ -1081,11 +1102,12 @@ var AxBaseAI = class {
|
|
|
1081
1102
|
}
|
|
1082
1103
|
if (this.tracer) {
|
|
1083
1104
|
await this.tracer?.startActiveSpan(
|
|
1084
|
-
"Embed Request",
|
|
1105
|
+
"AI Embed Request",
|
|
1085
1106
|
{
|
|
1086
1107
|
kind: SpanKind.SERVER,
|
|
1087
1108
|
attributes: {
|
|
1088
1109
|
[axSpanAttributes.LLM_SYSTEM]: this.name,
|
|
1110
|
+
[axSpanAttributes.LLM_OPERATION_NAME]: "embeddings",
|
|
1089
1111
|
[axSpanAttributes.LLM_REQUEST_MODEL]: embedModel
|
|
1090
1112
|
}
|
|
1091
1113
|
},
|
|
@@ -1140,8 +1162,12 @@ var AxBaseAI = class {
|
|
|
1140
1162
|
};
|
|
1141
1163
|
}
|
|
1142
1164
|
this.embedModelUsage = res.modelUsage;
|
|
1143
|
-
if (span?.isRecording()) {
|
|
1144
|
-
|
|
1165
|
+
if (span?.isRecording() && res.modelUsage?.tokens) {
|
|
1166
|
+
span.addEvent(axSpanEvents.GEN_AI_USAGE, {
|
|
1167
|
+
[axSpanAttributes.LLM_USAGE_INPUT_TOKENS]: res.modelUsage.tokens.promptTokens,
|
|
1168
|
+
[axSpanAttributes.LLM_USAGE_OUTPUT_TOKENS]: res.modelUsage.tokens.completionTokens ?? 0,
|
|
1169
|
+
[axSpanAttributes.LLM_USAGE_TOTAL_TOKENS]: res.modelUsage.tokens.totalTokens
|
|
1170
|
+
});
|
|
1145
1171
|
}
|
|
1146
1172
|
span?.end();
|
|
1147
1173
|
return res;
|
|
@@ -1165,11 +1191,113 @@ var AxBaseAI = class {
|
|
|
1165
1191
|
return item && "embedModel" in item ? item.embedModel : void 0;
|
|
1166
1192
|
}
|
|
1167
1193
|
};
|
|
1168
|
-
function
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1194
|
+
function setChatRequestEvents(req, span, excludeContentFromTrace) {
|
|
1195
|
+
const userMessages = [];
|
|
1196
|
+
if (req.chatPrompt && Array.isArray(req.chatPrompt) && req.chatPrompt.length > 0) {
|
|
1197
|
+
for (const prompt of req.chatPrompt) {
|
|
1198
|
+
switch (prompt.role) {
|
|
1199
|
+
case "system":
|
|
1200
|
+
if (prompt.content) {
|
|
1201
|
+
const eventData2 = {};
|
|
1202
|
+
if (!excludeContentFromTrace) {
|
|
1203
|
+
eventData2.content = prompt.content;
|
|
1204
|
+
}
|
|
1205
|
+
span.addEvent(axSpanEvents.GEN_AI_SYSTEM_MESSAGE, eventData2);
|
|
1206
|
+
}
|
|
1207
|
+
break;
|
|
1208
|
+
case "user":
|
|
1209
|
+
if (typeof prompt.content === "string") {
|
|
1210
|
+
userMessages.push(prompt.content);
|
|
1211
|
+
} else if (Array.isArray(prompt.content)) {
|
|
1212
|
+
for (const part of prompt.content) {
|
|
1213
|
+
if (part.type === "text") {
|
|
1214
|
+
userMessages.push(part.text);
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
break;
|
|
1219
|
+
case "assistant":
|
|
1220
|
+
const functionCalls = prompt.functionCalls?.map((call) => {
|
|
1221
|
+
return {
|
|
1222
|
+
id: call.id,
|
|
1223
|
+
type: call.type,
|
|
1224
|
+
function: call.function.name,
|
|
1225
|
+
arguments: call.function.params
|
|
1226
|
+
};
|
|
1227
|
+
});
|
|
1228
|
+
if (functionCalls && functionCalls.length > 0) {
|
|
1229
|
+
const eventData2 = {
|
|
1230
|
+
function_calls: JSON.stringify(functionCalls, null, 2)
|
|
1231
|
+
};
|
|
1232
|
+
if (!excludeContentFromTrace && prompt.content) {
|
|
1233
|
+
eventData2.content = prompt.content;
|
|
1234
|
+
}
|
|
1235
|
+
span.addEvent(axSpanEvents.GEN_AI_ASSISTANT_MESSAGE, eventData2);
|
|
1236
|
+
} else if (prompt.content) {
|
|
1237
|
+
const eventData2 = {};
|
|
1238
|
+
if (!excludeContentFromTrace) {
|
|
1239
|
+
eventData2.content = prompt.content;
|
|
1240
|
+
}
|
|
1241
|
+
span.addEvent(axSpanEvents.GEN_AI_ASSISTANT_MESSAGE, eventData2);
|
|
1242
|
+
}
|
|
1243
|
+
break;
|
|
1244
|
+
case "function":
|
|
1245
|
+
const eventData = {
|
|
1246
|
+
id: prompt.functionId
|
|
1247
|
+
};
|
|
1248
|
+
if (!excludeContentFromTrace) {
|
|
1249
|
+
eventData.content = prompt.result;
|
|
1250
|
+
}
|
|
1251
|
+
span.addEvent(axSpanEvents.GEN_AI_TOOL_MESSAGE, eventData);
|
|
1252
|
+
break;
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
const userEventData = {};
|
|
1257
|
+
if (!excludeContentFromTrace) {
|
|
1258
|
+
userEventData.content = userMessages.join("\n");
|
|
1259
|
+
}
|
|
1260
|
+
span.addEvent(axSpanEvents.GEN_AI_USER_MESSAGE, userEventData);
|
|
1261
|
+
}
|
|
1262
|
+
function setChatResponseEvents(res, span, excludeContentFromTrace) {
|
|
1263
|
+
if (res.modelUsage?.tokens) {
|
|
1264
|
+
const thoughTokens = res.modelUsage.tokens.thoughtsTokens ? {
|
|
1265
|
+
[axSpanAttributes.LLM_USAGE_THOUGHTS_TOKENS]: res.modelUsage.tokens.thoughtsTokens
|
|
1266
|
+
} : {};
|
|
1267
|
+
span.addEvent(axSpanEvents.GEN_AI_USAGE, {
|
|
1268
|
+
[axSpanAttributes.LLM_USAGE_INPUT_TOKENS]: res.modelUsage.tokens.promptTokens,
|
|
1269
|
+
[axSpanAttributes.LLM_USAGE_OUTPUT_TOKENS]: res.modelUsage.tokens.completionTokens ?? 0,
|
|
1270
|
+
[axSpanAttributes.LLM_USAGE_TOTAL_TOKENS]: res.modelUsage.tokens.totalTokens,
|
|
1271
|
+
...thoughTokens
|
|
1272
|
+
});
|
|
1273
|
+
}
|
|
1274
|
+
if (!res.results) {
|
|
1275
|
+
return;
|
|
1276
|
+
}
|
|
1277
|
+
for (const [index, result] of res.results.entries()) {
|
|
1278
|
+
const toolCalls = result.functionCalls?.map((call) => {
|
|
1279
|
+
return {
|
|
1280
|
+
id: call.id,
|
|
1281
|
+
type: call.type,
|
|
1282
|
+
function: call.function.name,
|
|
1283
|
+
arguments: call.function.params
|
|
1284
|
+
};
|
|
1285
|
+
});
|
|
1286
|
+
let message = {};
|
|
1287
|
+
if (toolCalls && toolCalls.length > 0) {
|
|
1288
|
+
if (!excludeContentFromTrace) {
|
|
1289
|
+
message.content = result.content;
|
|
1290
|
+
}
|
|
1291
|
+
message.tool_calls = toolCalls;
|
|
1292
|
+
} else {
|
|
1293
|
+
if (!excludeContentFromTrace) {
|
|
1294
|
+
message.content = result.content ?? "";
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
span.addEvent(axSpanEvents.GEN_AI_CHOICE, {
|
|
1298
|
+
finish_reason: result.finishReason,
|
|
1299
|
+
index,
|
|
1300
|
+
message: JSON.stringify(message, null, 2)
|
|
1173
1301
|
});
|
|
1174
1302
|
}
|
|
1175
1303
|
}
|
|
@@ -2562,7 +2690,7 @@ var AxAIDeepSeek = class extends AxAIOpenAIBase {
|
|
|
2562
2690
|
|
|
2563
2691
|
// ai/google-gemini/types.ts
|
|
2564
2692
|
var AxAIGoogleGeminiModel = /* @__PURE__ */ ((AxAIGoogleGeminiModel2) => {
|
|
2565
|
-
AxAIGoogleGeminiModel2["Gemini25Pro"] = "gemini-2.5-pro-preview-
|
|
2693
|
+
AxAIGoogleGeminiModel2["Gemini25Pro"] = "gemini-2.5-pro-preview-05-06";
|
|
2566
2694
|
AxAIGoogleGeminiModel2["Gemini25Flash"] = "gemini-2.5-flash-preview-04-17";
|
|
2567
2695
|
AxAIGoogleGeminiModel2["Gemini20Flash"] = "gemini-2.0-flash";
|
|
2568
2696
|
AxAIGoogleGeminiModel2["Gemini20FlashLite"] = "gemini-2.0-flash-lite-preview-02-05";
|
|
@@ -2611,7 +2739,7 @@ var AxAIGoogleGeminiEmbedTypes = /* @__PURE__ */ ((AxAIGoogleGeminiEmbedTypes2)
|
|
|
2611
2739
|
// ai/google-gemini/info.ts
|
|
2612
2740
|
var axModelInfoGoogleGemini = [
|
|
2613
2741
|
{
|
|
2614
|
-
name: "gemini-2.5-pro-preview-
|
|
2742
|
+
name: "gemini-2.5-pro-preview-05-06" /* Gemini25Pro */,
|
|
2615
2743
|
currency: "usd",
|
|
2616
2744
|
characterIsToken: false,
|
|
2617
2745
|
promptTokenCostPer1M: 2.5,
|
|
@@ -2695,7 +2823,7 @@ var safetySettings = [
|
|
|
2695
2823
|
}
|
|
2696
2824
|
];
|
|
2697
2825
|
var axAIGoogleGeminiDefaultConfig = () => structuredClone({
|
|
2698
|
-
model: "gemini-2.
|
|
2826
|
+
model: "gemini-2.5-flash-preview-04-17" /* Gemini25Flash */,
|
|
2699
2827
|
embedModel: "text-embedding-005" /* TextEmbedding005 */,
|
|
2700
2828
|
safetySettings,
|
|
2701
2829
|
...axBaseAIDefaultConfig()
|
|
@@ -2736,7 +2864,7 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2736
2864
|
n: config.n
|
|
2737
2865
|
};
|
|
2738
2866
|
}
|
|
2739
|
-
createChatReq = (req) => {
|
|
2867
|
+
createChatReq = (req, config) => {
|
|
2740
2868
|
const model = req.model;
|
|
2741
2869
|
const stream = req.modelConfig?.stream ?? this.config.stream;
|
|
2742
2870
|
if (!req.chatPrompt || req.chatPrompt.length === 0) {
|
|
@@ -2847,6 +2975,9 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2847
2975
|
}
|
|
2848
2976
|
});
|
|
2849
2977
|
}
|
|
2978
|
+
if (this.options?.urlContext) {
|
|
2979
|
+
tools.push({ url_context: {} });
|
|
2980
|
+
}
|
|
2850
2981
|
if (tools.length === 0) {
|
|
2851
2982
|
tools = void 0;
|
|
2852
2983
|
}
|
|
@@ -2872,6 +3003,16 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2872
3003
|
} else if (tools && tools.length > 0) {
|
|
2873
3004
|
toolConfig = { function_calling_config: { mode: "AUTO" } };
|
|
2874
3005
|
}
|
|
3006
|
+
const thinkingConfig = {};
|
|
3007
|
+
if (this.config.thinking?.includeThoughts) {
|
|
3008
|
+
thinkingConfig.includeThoughts = true;
|
|
3009
|
+
}
|
|
3010
|
+
if (this.config.thinking?.thinkingTokenBudget) {
|
|
3011
|
+
thinkingConfig.thinkingBudget = this.config.thinking.thinkingTokenBudget;
|
|
3012
|
+
}
|
|
3013
|
+
if (config.thinkingTokenBudget) {
|
|
3014
|
+
thinkingConfig.thinkingBudget = config.thinkingTokenBudget;
|
|
3015
|
+
}
|
|
2875
3016
|
const generationConfig = {
|
|
2876
3017
|
maxOutputTokens: req.modelConfig?.maxTokens ?? this.config.maxTokens,
|
|
2877
3018
|
temperature: req.modelConfig?.temperature ?? this.config.temperature,
|
|
@@ -2881,9 +3022,7 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2881
3022
|
candidateCount: 1,
|
|
2882
3023
|
stopSequences: req.modelConfig?.stopSequences ?? this.config.stopSequences,
|
|
2883
3024
|
responseMimeType: "text/plain",
|
|
2884
|
-
...
|
|
2885
|
-
thinkingConfig: this.config.thinkingConfig
|
|
2886
|
-
}
|
|
3025
|
+
...thinkingConfig ? { thinkingConfig } : {}
|
|
2887
3026
|
};
|
|
2888
3027
|
const safetySettings2 = this.config.safetySettings;
|
|
2889
3028
|
const reqValue = {
|
|
@@ -2964,7 +3103,11 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2964
3103
|
}
|
|
2965
3104
|
for (const part of candidate.content.parts) {
|
|
2966
3105
|
if ("text" in part) {
|
|
2967
|
-
|
|
3106
|
+
if ("thought" in part && part.thought) {
|
|
3107
|
+
result.thought = part.text;
|
|
3108
|
+
} else {
|
|
3109
|
+
result.content = part.text;
|
|
3110
|
+
}
|
|
2968
3111
|
continue;
|
|
2969
3112
|
}
|
|
2970
3113
|
if ("functionCall" in part) {
|
|
@@ -2987,7 +3130,8 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2987
3130
|
this.tokensUsed = {
|
|
2988
3131
|
totalTokens: resp.usageMetadata.totalTokenCount,
|
|
2989
3132
|
promptTokens: resp.usageMetadata.promptTokenCount,
|
|
2990
|
-
completionTokens: resp.usageMetadata.candidatesTokenCount
|
|
3133
|
+
completionTokens: resp.usageMetadata.candidatesTokenCount,
|
|
3134
|
+
thoughtsTokens: resp.usageMetadata.thoughtsTokenCount
|
|
2991
3135
|
};
|
|
2992
3136
|
}
|
|
2993
3137
|
return { results };
|
|
@@ -4578,6 +4722,14 @@ var AxSignature = class _AxSignature {
|
|
|
4578
4722
|
};
|
|
4579
4723
|
hash = () => this.sigHash;
|
|
4580
4724
|
toString = () => this.sigString;
|
|
4725
|
+
toJSON = () => {
|
|
4726
|
+
return {
|
|
4727
|
+
id: this.hash(),
|
|
4728
|
+
description: this.description,
|
|
4729
|
+
inputFields: this.inputFields,
|
|
4730
|
+
outputFields: this.outputFields
|
|
4731
|
+
};
|
|
4732
|
+
};
|
|
4581
4733
|
};
|
|
4582
4734
|
function renderField(field) {
|
|
4583
4735
|
let result = field.name;
|
|
@@ -6118,7 +6270,7 @@ var parseFunctions = (newFuncs, existingFuncs) => {
|
|
|
6118
6270
|
}
|
|
6119
6271
|
return [...existingFuncs ?? [], ...functions];
|
|
6120
6272
|
};
|
|
6121
|
-
var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, traceId) => {
|
|
6273
|
+
var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, traceId, span, excludeContentFromTelemetry) => {
|
|
6122
6274
|
const funcProc = new AxFunctionProcessor(functionList);
|
|
6123
6275
|
const functionsExecuted = /* @__PURE__ */ new Set();
|
|
6124
6276
|
const promises = functionCalls.map((func) => {
|
|
@@ -6127,6 +6279,16 @@ var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, t
|
|
|
6127
6279
|
}
|
|
6128
6280
|
const promise = funcProc.execute(func, { sessionId, traceId, ai }).then((functionResult) => {
|
|
6129
6281
|
functionsExecuted.add(func.name.toLowerCase());
|
|
6282
|
+
if (span) {
|
|
6283
|
+
const eventData = {
|
|
6284
|
+
name: func.name
|
|
6285
|
+
};
|
|
6286
|
+
if (!excludeContentFromTelemetry) {
|
|
6287
|
+
eventData.args = func.args;
|
|
6288
|
+
eventData.result = functionResult ?? "";
|
|
6289
|
+
}
|
|
6290
|
+
span.addEvent("function.call", eventData);
|
|
6291
|
+
}
|
|
6130
6292
|
return {
|
|
6131
6293
|
role: "function",
|
|
6132
6294
|
result: functionResult ?? "",
|
|
@@ -6135,6 +6297,17 @@ var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, t
|
|
|
6135
6297
|
}).catch((e) => {
|
|
6136
6298
|
if (e instanceof FunctionError) {
|
|
6137
6299
|
const result = e.getFixingInstructions();
|
|
6300
|
+
if (span) {
|
|
6301
|
+
const errorEventData = {
|
|
6302
|
+
name: func.name,
|
|
6303
|
+
message: e.toString()
|
|
6304
|
+
};
|
|
6305
|
+
if (!excludeContentFromTelemetry) {
|
|
6306
|
+
errorEventData.args = func.args;
|
|
6307
|
+
errorEventData.fixing_instructions = result;
|
|
6308
|
+
}
|
|
6309
|
+
span.addEvent("function.error", errorEventData);
|
|
6310
|
+
}
|
|
6138
6311
|
mem.add(
|
|
6139
6312
|
{
|
|
6140
6313
|
role: "function",
|
|
@@ -6192,6 +6365,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6192
6365
|
functionsExecuted = /* @__PURE__ */ new Set();
|
|
6193
6366
|
fieldProcessors = [];
|
|
6194
6367
|
streamingFieldProcessors = [];
|
|
6368
|
+
values = {};
|
|
6369
|
+
excludeContentFromTrace = false;
|
|
6195
6370
|
constructor(signature, options) {
|
|
6196
6371
|
super(signature, { description: options?.description });
|
|
6197
6372
|
this.options = options;
|
|
@@ -6201,6 +6376,7 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6201
6376
|
);
|
|
6202
6377
|
this.asserts = this.options?.asserts ?? [];
|
|
6203
6378
|
this.streamingAsserts = this.options?.streamingAsserts ?? [];
|
|
6379
|
+
this.excludeContentFromTrace = options?.excludeContentFromTrace ?? false;
|
|
6204
6380
|
this.usage = [];
|
|
6205
6381
|
if (options?.functions) {
|
|
6206
6382
|
this.functions = parseFunctions(options.functions);
|
|
@@ -6249,7 +6425,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6249
6425
|
rateLimiter,
|
|
6250
6426
|
stream,
|
|
6251
6427
|
functions: _functions,
|
|
6252
|
-
functionCall: _functionCall
|
|
6428
|
+
functionCall: _functionCall,
|
|
6429
|
+
thinkingTokenBudget
|
|
6253
6430
|
} = options ?? {};
|
|
6254
6431
|
const chatPrompt = mem?.history(sessionId) ?? [];
|
|
6255
6432
|
if (chatPrompt.length === 0) {
|
|
@@ -6270,7 +6447,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6270
6447
|
traceId,
|
|
6271
6448
|
rateLimiter,
|
|
6272
6449
|
stream,
|
|
6273
|
-
debug: false
|
|
6450
|
+
debug: false,
|
|
6451
|
+
thinkingTokenBudget
|
|
6274
6452
|
}
|
|
6275
6453
|
);
|
|
6276
6454
|
return res;
|
|
@@ -6278,7 +6456,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6278
6456
|
async *forwardCore({
|
|
6279
6457
|
ai,
|
|
6280
6458
|
mem,
|
|
6281
|
-
options
|
|
6459
|
+
options,
|
|
6460
|
+
span
|
|
6282
6461
|
}) {
|
|
6283
6462
|
const { sessionId, traceId, functions: _functions } = options ?? {};
|
|
6284
6463
|
const fastFail = options?.fastFail ?? this.options?.fastFail;
|
|
@@ -6298,7 +6477,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6298
6477
|
traceId,
|
|
6299
6478
|
sessionId,
|
|
6300
6479
|
functions,
|
|
6301
|
-
fastFail
|
|
6480
|
+
fastFail,
|
|
6481
|
+
span
|
|
6302
6482
|
});
|
|
6303
6483
|
} else {
|
|
6304
6484
|
yield await this.processResponse({
|
|
@@ -6308,7 +6488,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6308
6488
|
mem,
|
|
6309
6489
|
traceId,
|
|
6310
6490
|
sessionId,
|
|
6311
|
-
functions
|
|
6491
|
+
functions,
|
|
6492
|
+
span
|
|
6312
6493
|
});
|
|
6313
6494
|
}
|
|
6314
6495
|
}
|
|
@@ -6320,11 +6501,12 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6320
6501
|
sessionId,
|
|
6321
6502
|
traceId,
|
|
6322
6503
|
functions,
|
|
6323
|
-
fastFail
|
|
6504
|
+
fastFail,
|
|
6505
|
+
span
|
|
6324
6506
|
}) {
|
|
6325
6507
|
const streamingValidation = fastFail ?? ai.getFeatures(model).functionCot !== true;
|
|
6326
6508
|
const functionCalls = [];
|
|
6327
|
-
|
|
6509
|
+
this.values = {};
|
|
6328
6510
|
const xstate = {
|
|
6329
6511
|
extractedFields: [],
|
|
6330
6512
|
streamedIndex: {},
|
|
@@ -6351,6 +6533,9 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6351
6533
|
sessionId
|
|
6352
6534
|
);
|
|
6353
6535
|
} else if (result.content) {
|
|
6536
|
+
if (result.thought && result.thought.length > 0) {
|
|
6537
|
+
yield { thought: result.thought };
|
|
6538
|
+
}
|
|
6354
6539
|
content += result.content;
|
|
6355
6540
|
mem.updateResult(
|
|
6356
6541
|
{ name: result.name, content, delta: result.content },
|
|
@@ -6358,7 +6543,7 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6358
6543
|
);
|
|
6359
6544
|
const skip = streamingExtractValues(
|
|
6360
6545
|
this.signature,
|
|
6361
|
-
values,
|
|
6546
|
+
this.values,
|
|
6362
6547
|
xstate,
|
|
6363
6548
|
content,
|
|
6364
6549
|
streamingValidation
|
|
@@ -6379,12 +6564,20 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6379
6564
|
content,
|
|
6380
6565
|
xstate,
|
|
6381
6566
|
mem,
|
|
6382
|
-
values,
|
|
6567
|
+
this.values,
|
|
6383
6568
|
sessionId
|
|
6384
6569
|
);
|
|
6385
6570
|
}
|
|
6386
|
-
yield* streamValues(
|
|
6387
|
-
|
|
6571
|
+
yield* streamValues(
|
|
6572
|
+
this.signature,
|
|
6573
|
+
content,
|
|
6574
|
+
this.values,
|
|
6575
|
+
xstate
|
|
6576
|
+
);
|
|
6577
|
+
await assertAssertions(this.asserts, this.values);
|
|
6578
|
+
} else if (result.thought && result.thought.length > 0) {
|
|
6579
|
+
this.values.thought = this.values.thought ?? "" + result.thought;
|
|
6580
|
+
yield { thought: result.thought };
|
|
6388
6581
|
}
|
|
6389
6582
|
if (result.finishReason === "length") {
|
|
6390
6583
|
throw new Error(
|
|
@@ -6393,7 +6586,7 @@ Content: ${content}`
|
|
|
6393
6586
|
);
|
|
6394
6587
|
}
|
|
6395
6588
|
}
|
|
6396
|
-
const funcs = parseFunctionCalls(ai, functionCalls, values, model);
|
|
6589
|
+
const funcs = parseFunctionCalls(ai, functionCalls, this.values, model);
|
|
6397
6590
|
if (funcs) {
|
|
6398
6591
|
if (!functions) {
|
|
6399
6592
|
throw new Error("Functions are not defined");
|
|
@@ -6404,22 +6597,24 @@ Content: ${content}`
|
|
|
6404
6597
|
funcs,
|
|
6405
6598
|
mem,
|
|
6406
6599
|
sessionId,
|
|
6407
|
-
traceId
|
|
6600
|
+
traceId,
|
|
6601
|
+
span,
|
|
6602
|
+
this.excludeContentFromTrace
|
|
6408
6603
|
);
|
|
6409
6604
|
this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
|
|
6410
6605
|
} else {
|
|
6411
|
-
streamingExtractFinalValue(this.signature, values, xstate, content);
|
|
6606
|
+
streamingExtractFinalValue(this.signature, this.values, xstate, content);
|
|
6412
6607
|
await assertStreamingAssertions(
|
|
6413
6608
|
this.streamingAsserts,
|
|
6414
6609
|
xstate,
|
|
6415
6610
|
content,
|
|
6416
6611
|
true
|
|
6417
6612
|
);
|
|
6418
|
-
await assertAssertions(this.asserts, values);
|
|
6613
|
+
await assertAssertions(this.asserts, this.values);
|
|
6419
6614
|
if (this.fieldProcessors.length) {
|
|
6420
6615
|
await processFieldProcessors(
|
|
6421
6616
|
this.fieldProcessors,
|
|
6422
|
-
values,
|
|
6617
|
+
this.values,
|
|
6423
6618
|
mem,
|
|
6424
6619
|
sessionId
|
|
6425
6620
|
);
|
|
@@ -6430,12 +6625,17 @@ Content: ${content}`
|
|
|
6430
6625
|
content,
|
|
6431
6626
|
xstate,
|
|
6432
6627
|
mem,
|
|
6433
|
-
values,
|
|
6628
|
+
this.values,
|
|
6434
6629
|
sessionId,
|
|
6435
6630
|
true
|
|
6436
6631
|
);
|
|
6437
6632
|
}
|
|
6438
|
-
yield* streamValues(
|
|
6633
|
+
yield* streamValues(
|
|
6634
|
+
this.signature,
|
|
6635
|
+
content,
|
|
6636
|
+
this.values,
|
|
6637
|
+
xstate
|
|
6638
|
+
);
|
|
6439
6639
|
}
|
|
6440
6640
|
}
|
|
6441
6641
|
async processResponse({
|
|
@@ -6444,9 +6644,10 @@ Content: ${content}`
|
|
|
6444
6644
|
mem,
|
|
6445
6645
|
sessionId,
|
|
6446
6646
|
traceId,
|
|
6447
|
-
functions
|
|
6647
|
+
functions,
|
|
6648
|
+
span
|
|
6448
6649
|
}) {
|
|
6449
|
-
|
|
6650
|
+
this.values = {};
|
|
6450
6651
|
let results = res.results ?? [];
|
|
6451
6652
|
if (results.length > 1) {
|
|
6452
6653
|
results = results.filter((r) => r.functionCalls);
|
|
@@ -6457,7 +6658,7 @@ Content: ${content}`
|
|
|
6457
6658
|
}
|
|
6458
6659
|
mem.addResult(result, sessionId);
|
|
6459
6660
|
if (result.functionCalls?.length) {
|
|
6460
|
-
const funcs = parseFunctionCalls(ai, result.functionCalls, values);
|
|
6661
|
+
const funcs = parseFunctionCalls(ai, result.functionCalls, this.values);
|
|
6461
6662
|
if (funcs) {
|
|
6462
6663
|
if (!functions) {
|
|
6463
6664
|
throw new Error("Functions are not defined");
|
|
@@ -6468,17 +6669,22 @@ Content: ${content}`
|
|
|
6468
6669
|
funcs,
|
|
6469
6670
|
mem,
|
|
6470
6671
|
sessionId,
|
|
6471
|
-
traceId
|
|
6672
|
+
traceId,
|
|
6673
|
+
span,
|
|
6674
|
+
this.excludeContentFromTrace
|
|
6472
6675
|
);
|
|
6473
6676
|
this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
|
|
6474
6677
|
}
|
|
6475
6678
|
} else if (result.content) {
|
|
6476
|
-
|
|
6477
|
-
|
|
6679
|
+
if (result.thought && result.thought.length > 0) {
|
|
6680
|
+
this.values.thought = result.thought;
|
|
6681
|
+
}
|
|
6682
|
+
extractValues(this.signature, this.values, result.content);
|
|
6683
|
+
await assertAssertions(this.asserts, this.values);
|
|
6478
6684
|
if (this.fieldProcessors.length) {
|
|
6479
6685
|
await processFieldProcessors(
|
|
6480
6686
|
this.fieldProcessors,
|
|
6481
|
-
values,
|
|
6687
|
+
this.values,
|
|
6482
6688
|
mem,
|
|
6483
6689
|
sessionId
|
|
6484
6690
|
);
|
|
@@ -6491,13 +6697,12 @@ Content: ${result.content}`
|
|
|
6491
6697
|
);
|
|
6492
6698
|
}
|
|
6493
6699
|
}
|
|
6494
|
-
const publicValues = { ...values };
|
|
6495
6700
|
for (const field of this.signature.getOutputFields()) {
|
|
6496
6701
|
if (field.isInternal) {
|
|
6497
|
-
delete
|
|
6702
|
+
delete this.values[field.name];
|
|
6498
6703
|
}
|
|
6499
6704
|
}
|
|
6500
|
-
return { ...values };
|
|
6705
|
+
return { ...this.values };
|
|
6501
6706
|
}
|
|
6502
6707
|
async *_forward2(ai, values, options, span) {
|
|
6503
6708
|
const stopFunction = (options?.stopFunction ?? this.options?.stopFunction)?.toLowerCase();
|
|
@@ -6523,7 +6728,7 @@ Content: ${result.content}`
|
|
|
6523
6728
|
multiStepLoop: for (let n = 0; n < maxSteps; n++) {
|
|
6524
6729
|
for (let errCount = 0; errCount < maxRetries; errCount++) {
|
|
6525
6730
|
try {
|
|
6526
|
-
const generator = this.forwardCore({ options, ai, mem });
|
|
6731
|
+
const generator = this.forwardCore({ options, ai, mem, span });
|
|
6527
6732
|
for await (const delta of generator) {
|
|
6528
6733
|
if (delta !== void 0) {
|
|
6529
6734
|
yield { version: errCount, delta };
|
|
@@ -6547,10 +6752,22 @@ Content: ${result.content}`
|
|
|
6547
6752
|
if (e instanceof ValidationError) {
|
|
6548
6753
|
errorFields = e.getFixingInstructions();
|
|
6549
6754
|
err = e;
|
|
6755
|
+
if (span) {
|
|
6756
|
+
span.addEvent("validation.error", {
|
|
6757
|
+
message: e.toString(),
|
|
6758
|
+
fixing_instructions: errorFields?.map((f) => f.title).join(", ") ?? ""
|
|
6759
|
+
});
|
|
6760
|
+
}
|
|
6550
6761
|
} else if (e instanceof AxAssertionError) {
|
|
6551
6762
|
const e1 = e;
|
|
6552
6763
|
errorFields = e1.getFixingInstructions();
|
|
6553
6764
|
err = e;
|
|
6765
|
+
if (span) {
|
|
6766
|
+
span.addEvent("assertion.error", {
|
|
6767
|
+
message: e1.toString(),
|
|
6768
|
+
fixing_instructions: errorFields?.map((f) => f.title).join(", ") ?? ""
|
|
6769
|
+
});
|
|
6770
|
+
}
|
|
6554
6771
|
} else if (e instanceof AxAIServiceStreamTerminatedError) {
|
|
6555
6772
|
} else {
|
|
6556
6773
|
throw enhanceError(e, ai, this.signature);
|
|
@@ -6605,14 +6822,25 @@ Content: ${result.content}`
|
|
|
6605
6822
|
}
|
|
6606
6823
|
const funcNames = functions?.map((f) => f.name).join(",");
|
|
6607
6824
|
const attributes = {
|
|
6608
|
-
|
|
6609
|
-
|
|
6825
|
+
signature: JSON.stringify(this.signature.toJSON(), null, 2),
|
|
6826
|
+
...this.examples ? { examples: JSON.stringify(this.examples, null, 2) } : {},
|
|
6827
|
+
...funcNames ? { provided_functions: funcNames } : {},
|
|
6828
|
+
...options?.model ? { model: options.model } : {},
|
|
6829
|
+
...options?.thinkingTokenBudget ? { thinking_token_budget: options.thinkingTokenBudget } : {},
|
|
6830
|
+
...options?.maxSteps ? { max_steps: options.maxSteps } : {},
|
|
6831
|
+
...options?.maxRetries ? { max_retries: options.maxRetries } : {},
|
|
6832
|
+
...options?.fastFail ? { fast_fail: options.fastFail } : {}
|
|
6610
6833
|
};
|
|
6611
|
-
const
|
|
6834
|
+
const traceLabel = options.traceLabel ?? this.options?.traceLabel;
|
|
6835
|
+
const spanName = traceLabel ? `${traceLabel} (AxGen)` : "AxGen";
|
|
6836
|
+
const span = tracer.startSpan(spanName, {
|
|
6612
6837
|
kind: SpanKind2.SERVER,
|
|
6613
6838
|
attributes
|
|
6614
6839
|
});
|
|
6615
6840
|
try {
|
|
6841
|
+
if (!this.excludeContentFromTrace) {
|
|
6842
|
+
span.addEvent("input", { content: JSON.stringify(values, null, 2) });
|
|
6843
|
+
}
|
|
6616
6844
|
yield* this._forward2(
|
|
6617
6845
|
ai,
|
|
6618
6846
|
values,
|
|
@@ -6622,6 +6850,11 @@ Content: ${result.content}`
|
|
|
6622
6850
|
},
|
|
6623
6851
|
span
|
|
6624
6852
|
);
|
|
6853
|
+
if (!this.excludeContentFromTrace) {
|
|
6854
|
+
span.addEvent("output", {
|
|
6855
|
+
content: JSON.stringify(this.values, null, 2)
|
|
6856
|
+
});
|
|
6857
|
+
}
|
|
6625
6858
|
} finally {
|
|
6626
6859
|
span.end();
|
|
6627
6860
|
}
|