@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.cjs
CHANGED
|
@@ -165,6 +165,7 @@ var import_api2 = require("@opentelemetry/api");
|
|
|
165
165
|
var axSpanAttributes = {
|
|
166
166
|
// LLM
|
|
167
167
|
LLM_SYSTEM: "gen_ai.system",
|
|
168
|
+
LLM_OPERATION_NAME: "gen_ai.operation.name",
|
|
168
169
|
LLM_REQUEST_MODEL: "gen_ai.request.model",
|
|
169
170
|
LLM_REQUEST_MAX_TOKENS: "gen_ai.request.max_tokens",
|
|
170
171
|
LLM_REQUEST_TEMPERATURE: "gen_ai.request.temperature",
|
|
@@ -174,8 +175,10 @@ var axSpanAttributes = {
|
|
|
174
175
|
LLM_REQUEST_STOP_SEQUENCES: "gen_ai.request.stop_sequences",
|
|
175
176
|
LLM_REQUEST_LLM_IS_STREAMING: "gen_ai.request.llm_is_streaming",
|
|
176
177
|
LLM_REQUEST_TOP_P: "gen_ai.request.top_p",
|
|
177
|
-
|
|
178
|
-
|
|
178
|
+
LLM_USAGE_INPUT_TOKENS: "gen_ai.usage.input_tokens",
|
|
179
|
+
LLM_USAGE_OUTPUT_TOKENS: "gen_ai.usage.output_tokens",
|
|
180
|
+
LLM_USAGE_TOTAL_TOKENS: "gen_ai.usage.total_tokens",
|
|
181
|
+
LLM_USAGE_THOUGHTS_TOKENS: "gen_ai.usage.thoughts_tokens",
|
|
179
182
|
// Vector DB
|
|
180
183
|
DB_SYSTEM: "db.system",
|
|
181
184
|
DB_TABLE: "db.table",
|
|
@@ -198,7 +201,13 @@ var axSpanAttributes = {
|
|
|
198
201
|
DB_QUERY_RESULT_DOCUMENT: "db.query.result.document"
|
|
199
202
|
};
|
|
200
203
|
var axSpanEvents = {
|
|
201
|
-
|
|
204
|
+
GEN_AI_USER_MESSAGE: "gen_ai.user.message",
|
|
205
|
+
GEN_AI_SYSTEM_MESSAGE: "gen_ai.system.message",
|
|
206
|
+
GEN_AI_ASSISTANT_MESSAGE: "gen_ai.assistant.message",
|
|
207
|
+
GEN_AI_TOOL_MESSAGE: "gen_ai.tool.message",
|
|
208
|
+
// For tool messages in request & response tool calls
|
|
209
|
+
GEN_AI_CHOICE: "gen_ai.choice",
|
|
210
|
+
GEN_AI_USAGE: "gen_ai.usage"
|
|
202
211
|
};
|
|
203
212
|
var AxLLMRequestTypeValues = /* @__PURE__ */ ((AxLLMRequestTypeValues2) => {
|
|
204
213
|
AxLLMRequestTypeValues2["COMPLETION"] = "completion";
|
|
@@ -636,9 +645,6 @@ var apiCall = async (api, json) => {
|
|
|
636
645
|
} finally {
|
|
637
646
|
clearTimeout(timeoutId);
|
|
638
647
|
reader.releaseLock();
|
|
639
|
-
if (api.span?.isRecording()) {
|
|
640
|
-
api.span.end();
|
|
641
|
-
}
|
|
642
648
|
}
|
|
643
649
|
}
|
|
644
650
|
read();
|
|
@@ -686,9 +692,6 @@ var apiCall = async (api, json) => {
|
|
|
686
692
|
if (timeoutId !== void 0) {
|
|
687
693
|
clearTimeout(timeoutId);
|
|
688
694
|
}
|
|
689
|
-
if (api.span?.isRecording()) {
|
|
690
|
-
api.span.end();
|
|
691
|
-
}
|
|
692
695
|
}
|
|
693
696
|
}
|
|
694
697
|
};
|
|
@@ -902,6 +905,7 @@ var AxBaseAI = class {
|
|
|
902
905
|
fetch;
|
|
903
906
|
tracer;
|
|
904
907
|
timeout;
|
|
908
|
+
excludeContentFromTrace;
|
|
905
909
|
models;
|
|
906
910
|
modelInfo;
|
|
907
911
|
modelUsage;
|
|
@@ -962,13 +966,16 @@ var AxBaseAI = class {
|
|
|
962
966
|
this.fetch = options.fetch;
|
|
963
967
|
this.timeout = options.timeout;
|
|
964
968
|
this.tracer = options.tracer;
|
|
969
|
+
this.excludeContentFromTrace = options.excludeContentFromTrace;
|
|
965
970
|
}
|
|
966
971
|
getOptions() {
|
|
967
972
|
return {
|
|
968
973
|
debug: this.debug,
|
|
969
974
|
rateLimiter: this.rt,
|
|
970
975
|
fetch: this.fetch,
|
|
971
|
-
tracer: this.tracer
|
|
976
|
+
tracer: this.tracer,
|
|
977
|
+
timeout: this.timeout,
|
|
978
|
+
excludeContentFromTrace: this.excludeContentFromTrace
|
|
972
979
|
};
|
|
973
980
|
}
|
|
974
981
|
getModelList() {
|
|
@@ -1066,13 +1073,18 @@ var AxBaseAI = class {
|
|
|
1066
1073
|
if (!canStream) {
|
|
1067
1074
|
modelConfig.stream = false;
|
|
1068
1075
|
}
|
|
1076
|
+
const canSetThinkingTokenBudget = this.getFeatures(model).thinkingTokenBudget;
|
|
1077
|
+
if (!canSetThinkingTokenBudget && options?.thinkingTokenBudget) {
|
|
1078
|
+
throw new Error("Thinking token budget is not supported for this model");
|
|
1079
|
+
}
|
|
1069
1080
|
if (this.tracer) {
|
|
1070
1081
|
return await this.tracer?.startActiveSpan(
|
|
1071
|
-
"Chat Request",
|
|
1082
|
+
"AI Chat Request",
|
|
1072
1083
|
{
|
|
1073
1084
|
kind: import_api2.SpanKind.SERVER,
|
|
1074
1085
|
attributes: {
|
|
1075
1086
|
[axSpanAttributes.LLM_SYSTEM]: this.name,
|
|
1087
|
+
[axSpanAttributes.LLM_OPERATION_NAME]: "chat",
|
|
1076
1088
|
[axSpanAttributes.LLM_REQUEST_MODEL]: model,
|
|
1077
1089
|
[axSpanAttributes.LLM_REQUEST_MAX_TOKENS]: modelConfig.maxTokens,
|
|
1078
1090
|
[axSpanAttributes.LLM_REQUEST_TEMPERATURE]: modelConfig.temperature,
|
|
@@ -1082,17 +1094,10 @@ var AxBaseAI = class {
|
|
|
1082
1094
|
[axSpanAttributes.LLM_REQUEST_PRESENCE_PENALTY]: modelConfig.presencePenalty,
|
|
1083
1095
|
[axSpanAttributes.LLM_REQUEST_STOP_SEQUENCES]: modelConfig.stopSequences?.join(", "),
|
|
1084
1096
|
[axSpanAttributes.LLM_REQUEST_LLM_IS_STREAMING]: modelConfig.stream
|
|
1085
|
-
// [AxSpanAttributes.LLM_PROMPTS]: _req.chatPrompt
|
|
1086
|
-
// ?.map((v) => v.content)
|
|
1087
|
-
// .join('\n')
|
|
1088
1097
|
}
|
|
1089
1098
|
},
|
|
1090
1099
|
async (span) => {
|
|
1091
|
-
|
|
1092
|
-
return await this._chat2(model, modelConfig, req, options, span);
|
|
1093
|
-
} finally {
|
|
1094
|
-
span.end();
|
|
1095
|
-
}
|
|
1100
|
+
return await this._chat2(model, modelConfig, req, options, span);
|
|
1096
1101
|
}
|
|
1097
1102
|
);
|
|
1098
1103
|
}
|
|
@@ -1138,6 +1143,9 @@ var AxBaseAI = class {
|
|
|
1138
1143
|
req,
|
|
1139
1144
|
options
|
|
1140
1145
|
);
|
|
1146
|
+
if (span?.isRecording()) {
|
|
1147
|
+
setChatRequestEvents(chatReq, span, this.excludeContentFromTrace);
|
|
1148
|
+
}
|
|
1141
1149
|
const res2 = await apiCall(
|
|
1142
1150
|
{
|
|
1143
1151
|
name: apiConfig.name,
|
|
@@ -1175,7 +1183,7 @@ var AxBaseAI = class {
|
|
|
1175
1183
|
}
|
|
1176
1184
|
this.modelUsage = res2.modelUsage;
|
|
1177
1185
|
if (span?.isRecording()) {
|
|
1178
|
-
|
|
1186
|
+
setChatResponseEvents(res2, span, this.excludeContentFromTrace);
|
|
1179
1187
|
}
|
|
1180
1188
|
if (debug) {
|
|
1181
1189
|
logResponse(res2);
|
|
@@ -1186,6 +1194,9 @@ var AxBaseAI = class {
|
|
|
1186
1194
|
if (debug) {
|
|
1187
1195
|
process.stdout.write("\n");
|
|
1188
1196
|
}
|
|
1197
|
+
if (span?.isRecording()) {
|
|
1198
|
+
span.end();
|
|
1199
|
+
}
|
|
1189
1200
|
};
|
|
1190
1201
|
const st = rv.pipeThrough(
|
|
1191
1202
|
new RespTransformStream(
|
|
@@ -1200,16 +1211,26 @@ var AxBaseAI = class {
|
|
|
1200
1211
|
}
|
|
1201
1212
|
const res = this.aiImpl.createChatResp(rv);
|
|
1202
1213
|
res.sessionId = options?.sessionId;
|
|
1214
|
+
if (!res.modelUsage) {
|
|
1215
|
+
const tokenUsage = this.aiImpl.getTokenUsage();
|
|
1216
|
+
if (tokenUsage) {
|
|
1217
|
+
res.modelUsage = {
|
|
1218
|
+
ai: this.name,
|
|
1219
|
+
model,
|
|
1220
|
+
tokens: tokenUsage
|
|
1221
|
+
};
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1203
1224
|
if (res.modelUsage) {
|
|
1204
1225
|
this.modelUsage = res.modelUsage;
|
|
1205
1226
|
}
|
|
1206
1227
|
if (span?.isRecording()) {
|
|
1207
|
-
|
|
1228
|
+
setChatResponseEvents(res, span, this.excludeContentFromTrace);
|
|
1229
|
+
span.end();
|
|
1208
1230
|
}
|
|
1209
1231
|
if (debug) {
|
|
1210
1232
|
logResponse(res);
|
|
1211
1233
|
}
|
|
1212
|
-
span?.end();
|
|
1213
1234
|
return res;
|
|
1214
1235
|
}
|
|
1215
1236
|
async embed(req, options) {
|
|
@@ -1233,11 +1254,12 @@ var AxBaseAI = class {
|
|
|
1233
1254
|
}
|
|
1234
1255
|
if (this.tracer) {
|
|
1235
1256
|
await this.tracer?.startActiveSpan(
|
|
1236
|
-
"Embed Request",
|
|
1257
|
+
"AI Embed Request",
|
|
1237
1258
|
{
|
|
1238
1259
|
kind: import_api2.SpanKind.SERVER,
|
|
1239
1260
|
attributes: {
|
|
1240
1261
|
[axSpanAttributes.LLM_SYSTEM]: this.name,
|
|
1262
|
+
[axSpanAttributes.LLM_OPERATION_NAME]: "embeddings",
|
|
1241
1263
|
[axSpanAttributes.LLM_REQUEST_MODEL]: embedModel
|
|
1242
1264
|
}
|
|
1243
1265
|
},
|
|
@@ -1292,8 +1314,12 @@ var AxBaseAI = class {
|
|
|
1292
1314
|
};
|
|
1293
1315
|
}
|
|
1294
1316
|
this.embedModelUsage = res.modelUsage;
|
|
1295
|
-
if (span?.isRecording()) {
|
|
1296
|
-
|
|
1317
|
+
if (span?.isRecording() && res.modelUsage?.tokens) {
|
|
1318
|
+
span.addEvent(axSpanEvents.GEN_AI_USAGE, {
|
|
1319
|
+
[axSpanAttributes.LLM_USAGE_INPUT_TOKENS]: res.modelUsage.tokens.promptTokens,
|
|
1320
|
+
[axSpanAttributes.LLM_USAGE_OUTPUT_TOKENS]: res.modelUsage.tokens.completionTokens ?? 0,
|
|
1321
|
+
[axSpanAttributes.LLM_USAGE_TOTAL_TOKENS]: res.modelUsage.tokens.totalTokens
|
|
1322
|
+
});
|
|
1297
1323
|
}
|
|
1298
1324
|
span?.end();
|
|
1299
1325
|
return res;
|
|
@@ -1317,11 +1343,113 @@ var AxBaseAI = class {
|
|
|
1317
1343
|
return item && "embedModel" in item ? item.embedModel : void 0;
|
|
1318
1344
|
}
|
|
1319
1345
|
};
|
|
1320
|
-
function
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1346
|
+
function setChatRequestEvents(req, span, excludeContentFromTrace) {
|
|
1347
|
+
const userMessages = [];
|
|
1348
|
+
if (req.chatPrompt && Array.isArray(req.chatPrompt) && req.chatPrompt.length > 0) {
|
|
1349
|
+
for (const prompt of req.chatPrompt) {
|
|
1350
|
+
switch (prompt.role) {
|
|
1351
|
+
case "system":
|
|
1352
|
+
if (prompt.content) {
|
|
1353
|
+
const eventData2 = {};
|
|
1354
|
+
if (!excludeContentFromTrace) {
|
|
1355
|
+
eventData2.content = prompt.content;
|
|
1356
|
+
}
|
|
1357
|
+
span.addEvent(axSpanEvents.GEN_AI_SYSTEM_MESSAGE, eventData2);
|
|
1358
|
+
}
|
|
1359
|
+
break;
|
|
1360
|
+
case "user":
|
|
1361
|
+
if (typeof prompt.content === "string") {
|
|
1362
|
+
userMessages.push(prompt.content);
|
|
1363
|
+
} else if (Array.isArray(prompt.content)) {
|
|
1364
|
+
for (const part of prompt.content) {
|
|
1365
|
+
if (part.type === "text") {
|
|
1366
|
+
userMessages.push(part.text);
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
break;
|
|
1371
|
+
case "assistant":
|
|
1372
|
+
const functionCalls = prompt.functionCalls?.map((call) => {
|
|
1373
|
+
return {
|
|
1374
|
+
id: call.id,
|
|
1375
|
+
type: call.type,
|
|
1376
|
+
function: call.function.name,
|
|
1377
|
+
arguments: call.function.params
|
|
1378
|
+
};
|
|
1379
|
+
});
|
|
1380
|
+
if (functionCalls && functionCalls.length > 0) {
|
|
1381
|
+
const eventData2 = {
|
|
1382
|
+
function_calls: JSON.stringify(functionCalls, null, 2)
|
|
1383
|
+
};
|
|
1384
|
+
if (!excludeContentFromTrace && prompt.content) {
|
|
1385
|
+
eventData2.content = prompt.content;
|
|
1386
|
+
}
|
|
1387
|
+
span.addEvent(axSpanEvents.GEN_AI_ASSISTANT_MESSAGE, eventData2);
|
|
1388
|
+
} else if (prompt.content) {
|
|
1389
|
+
const eventData2 = {};
|
|
1390
|
+
if (!excludeContentFromTrace) {
|
|
1391
|
+
eventData2.content = prompt.content;
|
|
1392
|
+
}
|
|
1393
|
+
span.addEvent(axSpanEvents.GEN_AI_ASSISTANT_MESSAGE, eventData2);
|
|
1394
|
+
}
|
|
1395
|
+
break;
|
|
1396
|
+
case "function":
|
|
1397
|
+
const eventData = {
|
|
1398
|
+
id: prompt.functionId
|
|
1399
|
+
};
|
|
1400
|
+
if (!excludeContentFromTrace) {
|
|
1401
|
+
eventData.content = prompt.result;
|
|
1402
|
+
}
|
|
1403
|
+
span.addEvent(axSpanEvents.GEN_AI_TOOL_MESSAGE, eventData);
|
|
1404
|
+
break;
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
const userEventData = {};
|
|
1409
|
+
if (!excludeContentFromTrace) {
|
|
1410
|
+
userEventData.content = userMessages.join("\n");
|
|
1411
|
+
}
|
|
1412
|
+
span.addEvent(axSpanEvents.GEN_AI_USER_MESSAGE, userEventData);
|
|
1413
|
+
}
|
|
1414
|
+
function setChatResponseEvents(res, span, excludeContentFromTrace) {
|
|
1415
|
+
if (res.modelUsage?.tokens) {
|
|
1416
|
+
const thoughTokens = res.modelUsage.tokens.thoughtsTokens ? {
|
|
1417
|
+
[axSpanAttributes.LLM_USAGE_THOUGHTS_TOKENS]: res.modelUsage.tokens.thoughtsTokens
|
|
1418
|
+
} : {};
|
|
1419
|
+
span.addEvent(axSpanEvents.GEN_AI_USAGE, {
|
|
1420
|
+
[axSpanAttributes.LLM_USAGE_INPUT_TOKENS]: res.modelUsage.tokens.promptTokens,
|
|
1421
|
+
[axSpanAttributes.LLM_USAGE_OUTPUT_TOKENS]: res.modelUsage.tokens.completionTokens ?? 0,
|
|
1422
|
+
[axSpanAttributes.LLM_USAGE_TOTAL_TOKENS]: res.modelUsage.tokens.totalTokens,
|
|
1423
|
+
...thoughTokens
|
|
1424
|
+
});
|
|
1425
|
+
}
|
|
1426
|
+
if (!res.results) {
|
|
1427
|
+
return;
|
|
1428
|
+
}
|
|
1429
|
+
for (const [index, result] of res.results.entries()) {
|
|
1430
|
+
const toolCalls = result.functionCalls?.map((call) => {
|
|
1431
|
+
return {
|
|
1432
|
+
id: call.id,
|
|
1433
|
+
type: call.type,
|
|
1434
|
+
function: call.function.name,
|
|
1435
|
+
arguments: call.function.params
|
|
1436
|
+
};
|
|
1437
|
+
});
|
|
1438
|
+
let message = {};
|
|
1439
|
+
if (toolCalls && toolCalls.length > 0) {
|
|
1440
|
+
if (!excludeContentFromTrace) {
|
|
1441
|
+
message.content = result.content;
|
|
1442
|
+
}
|
|
1443
|
+
message.tool_calls = toolCalls;
|
|
1444
|
+
} else {
|
|
1445
|
+
if (!excludeContentFromTrace) {
|
|
1446
|
+
message.content = result.content ?? "";
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
span.addEvent(axSpanEvents.GEN_AI_CHOICE, {
|
|
1450
|
+
finish_reason: result.finishReason,
|
|
1451
|
+
index,
|
|
1452
|
+
message: JSON.stringify(message, null, 2)
|
|
1325
1453
|
});
|
|
1326
1454
|
}
|
|
1327
1455
|
}
|
|
@@ -2714,7 +2842,7 @@ var AxAIDeepSeek = class extends AxAIOpenAIBase {
|
|
|
2714
2842
|
|
|
2715
2843
|
// ai/google-gemini/types.ts
|
|
2716
2844
|
var AxAIGoogleGeminiModel = /* @__PURE__ */ ((AxAIGoogleGeminiModel2) => {
|
|
2717
|
-
AxAIGoogleGeminiModel2["Gemini25Pro"] = "gemini-2.5-pro-preview-
|
|
2845
|
+
AxAIGoogleGeminiModel2["Gemini25Pro"] = "gemini-2.5-pro-preview-05-06";
|
|
2718
2846
|
AxAIGoogleGeminiModel2["Gemini25Flash"] = "gemini-2.5-flash-preview-04-17";
|
|
2719
2847
|
AxAIGoogleGeminiModel2["Gemini20Flash"] = "gemini-2.0-flash";
|
|
2720
2848
|
AxAIGoogleGeminiModel2["Gemini20FlashLite"] = "gemini-2.0-flash-lite-preview-02-05";
|
|
@@ -2763,7 +2891,7 @@ var AxAIGoogleGeminiEmbedTypes = /* @__PURE__ */ ((AxAIGoogleGeminiEmbedTypes2)
|
|
|
2763
2891
|
// ai/google-gemini/info.ts
|
|
2764
2892
|
var axModelInfoGoogleGemini = [
|
|
2765
2893
|
{
|
|
2766
|
-
name: "gemini-2.5-pro-preview-
|
|
2894
|
+
name: "gemini-2.5-pro-preview-05-06" /* Gemini25Pro */,
|
|
2767
2895
|
currency: "usd",
|
|
2768
2896
|
characterIsToken: false,
|
|
2769
2897
|
promptTokenCostPer1M: 2.5,
|
|
@@ -2847,7 +2975,7 @@ var safetySettings = [
|
|
|
2847
2975
|
}
|
|
2848
2976
|
];
|
|
2849
2977
|
var axAIGoogleGeminiDefaultConfig = () => structuredClone({
|
|
2850
|
-
model: "gemini-2.
|
|
2978
|
+
model: "gemini-2.5-flash-preview-04-17" /* Gemini25Flash */,
|
|
2851
2979
|
embedModel: "text-embedding-005" /* TextEmbedding005 */,
|
|
2852
2980
|
safetySettings,
|
|
2853
2981
|
...axBaseAIDefaultConfig()
|
|
@@ -2888,7 +3016,7 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2888
3016
|
n: config.n
|
|
2889
3017
|
};
|
|
2890
3018
|
}
|
|
2891
|
-
createChatReq = (req) => {
|
|
3019
|
+
createChatReq = (req, config) => {
|
|
2892
3020
|
const model = req.model;
|
|
2893
3021
|
const stream = req.modelConfig?.stream ?? this.config.stream;
|
|
2894
3022
|
if (!req.chatPrompt || req.chatPrompt.length === 0) {
|
|
@@ -2999,6 +3127,9 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2999
3127
|
}
|
|
3000
3128
|
});
|
|
3001
3129
|
}
|
|
3130
|
+
if (this.options?.urlContext) {
|
|
3131
|
+
tools.push({ url_context: {} });
|
|
3132
|
+
}
|
|
3002
3133
|
if (tools.length === 0) {
|
|
3003
3134
|
tools = void 0;
|
|
3004
3135
|
}
|
|
@@ -3024,6 +3155,16 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
3024
3155
|
} else if (tools && tools.length > 0) {
|
|
3025
3156
|
toolConfig = { function_calling_config: { mode: "AUTO" } };
|
|
3026
3157
|
}
|
|
3158
|
+
const thinkingConfig = {};
|
|
3159
|
+
if (this.config.thinking?.includeThoughts) {
|
|
3160
|
+
thinkingConfig.includeThoughts = true;
|
|
3161
|
+
}
|
|
3162
|
+
if (this.config.thinking?.thinkingTokenBudget) {
|
|
3163
|
+
thinkingConfig.thinkingBudget = this.config.thinking.thinkingTokenBudget;
|
|
3164
|
+
}
|
|
3165
|
+
if (config.thinkingTokenBudget) {
|
|
3166
|
+
thinkingConfig.thinkingBudget = config.thinkingTokenBudget;
|
|
3167
|
+
}
|
|
3027
3168
|
const generationConfig = {
|
|
3028
3169
|
maxOutputTokens: req.modelConfig?.maxTokens ?? this.config.maxTokens,
|
|
3029
3170
|
temperature: req.modelConfig?.temperature ?? this.config.temperature,
|
|
@@ -3033,9 +3174,7 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
3033
3174
|
candidateCount: 1,
|
|
3034
3175
|
stopSequences: req.modelConfig?.stopSequences ?? this.config.stopSequences,
|
|
3035
3176
|
responseMimeType: "text/plain",
|
|
3036
|
-
...
|
|
3037
|
-
thinkingConfig: this.config.thinkingConfig
|
|
3038
|
-
}
|
|
3177
|
+
...thinkingConfig ? { thinkingConfig } : {}
|
|
3039
3178
|
};
|
|
3040
3179
|
const safetySettings2 = this.config.safetySettings;
|
|
3041
3180
|
const reqValue = {
|
|
@@ -3116,7 +3255,11 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
3116
3255
|
}
|
|
3117
3256
|
for (const part of candidate.content.parts) {
|
|
3118
3257
|
if ("text" in part) {
|
|
3119
|
-
|
|
3258
|
+
if ("thought" in part && part.thought) {
|
|
3259
|
+
result.thought = part.text;
|
|
3260
|
+
} else {
|
|
3261
|
+
result.content = part.text;
|
|
3262
|
+
}
|
|
3120
3263
|
continue;
|
|
3121
3264
|
}
|
|
3122
3265
|
if ("functionCall" in part) {
|
|
@@ -3139,7 +3282,8 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
3139
3282
|
this.tokensUsed = {
|
|
3140
3283
|
totalTokens: resp.usageMetadata.totalTokenCount,
|
|
3141
3284
|
promptTokens: resp.usageMetadata.promptTokenCount,
|
|
3142
|
-
completionTokens: resp.usageMetadata.candidatesTokenCount
|
|
3285
|
+
completionTokens: resp.usageMetadata.candidatesTokenCount,
|
|
3286
|
+
thoughtsTokens: resp.usageMetadata.thoughtsTokenCount
|
|
3143
3287
|
};
|
|
3144
3288
|
}
|
|
3145
3289
|
return { results };
|
|
@@ -4730,6 +4874,14 @@ var AxSignature = class _AxSignature {
|
|
|
4730
4874
|
};
|
|
4731
4875
|
hash = () => this.sigHash;
|
|
4732
4876
|
toString = () => this.sigString;
|
|
4877
|
+
toJSON = () => {
|
|
4878
|
+
return {
|
|
4879
|
+
id: this.hash(),
|
|
4880
|
+
description: this.description,
|
|
4881
|
+
inputFields: this.inputFields,
|
|
4882
|
+
outputFields: this.outputFields
|
|
4883
|
+
};
|
|
4884
|
+
};
|
|
4733
4885
|
};
|
|
4734
4886
|
function renderField(field) {
|
|
4735
4887
|
let result = field.name;
|
|
@@ -6270,7 +6422,7 @@ var parseFunctions = (newFuncs, existingFuncs) => {
|
|
|
6270
6422
|
}
|
|
6271
6423
|
return [...existingFuncs ?? [], ...functions];
|
|
6272
6424
|
};
|
|
6273
|
-
var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, traceId) => {
|
|
6425
|
+
var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, traceId, span, excludeContentFromTelemetry) => {
|
|
6274
6426
|
const funcProc = new AxFunctionProcessor(functionList);
|
|
6275
6427
|
const functionsExecuted = /* @__PURE__ */ new Set();
|
|
6276
6428
|
const promises = functionCalls.map((func) => {
|
|
@@ -6279,6 +6431,16 @@ var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, t
|
|
|
6279
6431
|
}
|
|
6280
6432
|
const promise = funcProc.execute(func, { sessionId, traceId, ai }).then((functionResult) => {
|
|
6281
6433
|
functionsExecuted.add(func.name.toLowerCase());
|
|
6434
|
+
if (span) {
|
|
6435
|
+
const eventData = {
|
|
6436
|
+
name: func.name
|
|
6437
|
+
};
|
|
6438
|
+
if (!excludeContentFromTelemetry) {
|
|
6439
|
+
eventData.args = func.args;
|
|
6440
|
+
eventData.result = functionResult ?? "";
|
|
6441
|
+
}
|
|
6442
|
+
span.addEvent("function.call", eventData);
|
|
6443
|
+
}
|
|
6282
6444
|
return {
|
|
6283
6445
|
role: "function",
|
|
6284
6446
|
result: functionResult ?? "",
|
|
@@ -6287,6 +6449,17 @@ var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, t
|
|
|
6287
6449
|
}).catch((e) => {
|
|
6288
6450
|
if (e instanceof FunctionError) {
|
|
6289
6451
|
const result = e.getFixingInstructions();
|
|
6452
|
+
if (span) {
|
|
6453
|
+
const errorEventData = {
|
|
6454
|
+
name: func.name,
|
|
6455
|
+
message: e.toString()
|
|
6456
|
+
};
|
|
6457
|
+
if (!excludeContentFromTelemetry) {
|
|
6458
|
+
errorEventData.args = func.args;
|
|
6459
|
+
errorEventData.fixing_instructions = result;
|
|
6460
|
+
}
|
|
6461
|
+
span.addEvent("function.error", errorEventData);
|
|
6462
|
+
}
|
|
6290
6463
|
mem.add(
|
|
6291
6464
|
{
|
|
6292
6465
|
role: "function",
|
|
@@ -6344,6 +6517,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6344
6517
|
functionsExecuted = /* @__PURE__ */ new Set();
|
|
6345
6518
|
fieldProcessors = [];
|
|
6346
6519
|
streamingFieldProcessors = [];
|
|
6520
|
+
values = {};
|
|
6521
|
+
excludeContentFromTrace = false;
|
|
6347
6522
|
constructor(signature, options) {
|
|
6348
6523
|
super(signature, { description: options?.description });
|
|
6349
6524
|
this.options = options;
|
|
@@ -6353,6 +6528,7 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6353
6528
|
);
|
|
6354
6529
|
this.asserts = this.options?.asserts ?? [];
|
|
6355
6530
|
this.streamingAsserts = this.options?.streamingAsserts ?? [];
|
|
6531
|
+
this.excludeContentFromTrace = options?.excludeContentFromTrace ?? false;
|
|
6356
6532
|
this.usage = [];
|
|
6357
6533
|
if (options?.functions) {
|
|
6358
6534
|
this.functions = parseFunctions(options.functions);
|
|
@@ -6401,7 +6577,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6401
6577
|
rateLimiter,
|
|
6402
6578
|
stream,
|
|
6403
6579
|
functions: _functions,
|
|
6404
|
-
functionCall: _functionCall
|
|
6580
|
+
functionCall: _functionCall,
|
|
6581
|
+
thinkingTokenBudget
|
|
6405
6582
|
} = options ?? {};
|
|
6406
6583
|
const chatPrompt = mem?.history(sessionId) ?? [];
|
|
6407
6584
|
if (chatPrompt.length === 0) {
|
|
@@ -6422,7 +6599,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6422
6599
|
traceId,
|
|
6423
6600
|
rateLimiter,
|
|
6424
6601
|
stream,
|
|
6425
|
-
debug: false
|
|
6602
|
+
debug: false,
|
|
6603
|
+
thinkingTokenBudget
|
|
6426
6604
|
}
|
|
6427
6605
|
);
|
|
6428
6606
|
return res;
|
|
@@ -6430,7 +6608,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6430
6608
|
async *forwardCore({
|
|
6431
6609
|
ai,
|
|
6432
6610
|
mem,
|
|
6433
|
-
options
|
|
6611
|
+
options,
|
|
6612
|
+
span
|
|
6434
6613
|
}) {
|
|
6435
6614
|
const { sessionId, traceId, functions: _functions } = options ?? {};
|
|
6436
6615
|
const fastFail = options?.fastFail ?? this.options?.fastFail;
|
|
@@ -6450,7 +6629,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6450
6629
|
traceId,
|
|
6451
6630
|
sessionId,
|
|
6452
6631
|
functions,
|
|
6453
|
-
fastFail
|
|
6632
|
+
fastFail,
|
|
6633
|
+
span
|
|
6454
6634
|
});
|
|
6455
6635
|
} else {
|
|
6456
6636
|
yield await this.processResponse({
|
|
@@ -6460,7 +6640,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6460
6640
|
mem,
|
|
6461
6641
|
traceId,
|
|
6462
6642
|
sessionId,
|
|
6463
|
-
functions
|
|
6643
|
+
functions,
|
|
6644
|
+
span
|
|
6464
6645
|
});
|
|
6465
6646
|
}
|
|
6466
6647
|
}
|
|
@@ -6472,11 +6653,12 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6472
6653
|
sessionId,
|
|
6473
6654
|
traceId,
|
|
6474
6655
|
functions,
|
|
6475
|
-
fastFail
|
|
6656
|
+
fastFail,
|
|
6657
|
+
span
|
|
6476
6658
|
}) {
|
|
6477
6659
|
const streamingValidation = fastFail ?? ai.getFeatures(model).functionCot !== true;
|
|
6478
6660
|
const functionCalls = [];
|
|
6479
|
-
|
|
6661
|
+
this.values = {};
|
|
6480
6662
|
const xstate = {
|
|
6481
6663
|
extractedFields: [],
|
|
6482
6664
|
streamedIndex: {},
|
|
@@ -6503,6 +6685,9 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6503
6685
|
sessionId
|
|
6504
6686
|
);
|
|
6505
6687
|
} else if (result.content) {
|
|
6688
|
+
if (result.thought && result.thought.length > 0) {
|
|
6689
|
+
yield { thought: result.thought };
|
|
6690
|
+
}
|
|
6506
6691
|
content += result.content;
|
|
6507
6692
|
mem.updateResult(
|
|
6508
6693
|
{ name: result.name, content, delta: result.content },
|
|
@@ -6510,7 +6695,7 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6510
6695
|
);
|
|
6511
6696
|
const skip = streamingExtractValues(
|
|
6512
6697
|
this.signature,
|
|
6513
|
-
values,
|
|
6698
|
+
this.values,
|
|
6514
6699
|
xstate,
|
|
6515
6700
|
content,
|
|
6516
6701
|
streamingValidation
|
|
@@ -6531,12 +6716,20 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6531
6716
|
content,
|
|
6532
6717
|
xstate,
|
|
6533
6718
|
mem,
|
|
6534
|
-
values,
|
|
6719
|
+
this.values,
|
|
6535
6720
|
sessionId
|
|
6536
6721
|
);
|
|
6537
6722
|
}
|
|
6538
|
-
yield* streamValues(
|
|
6539
|
-
|
|
6723
|
+
yield* streamValues(
|
|
6724
|
+
this.signature,
|
|
6725
|
+
content,
|
|
6726
|
+
this.values,
|
|
6727
|
+
xstate
|
|
6728
|
+
);
|
|
6729
|
+
await assertAssertions(this.asserts, this.values);
|
|
6730
|
+
} else if (result.thought && result.thought.length > 0) {
|
|
6731
|
+
this.values.thought = this.values.thought ?? "" + result.thought;
|
|
6732
|
+
yield { thought: result.thought };
|
|
6540
6733
|
}
|
|
6541
6734
|
if (result.finishReason === "length") {
|
|
6542
6735
|
throw new Error(
|
|
@@ -6545,7 +6738,7 @@ Content: ${content}`
|
|
|
6545
6738
|
);
|
|
6546
6739
|
}
|
|
6547
6740
|
}
|
|
6548
|
-
const funcs = parseFunctionCalls(ai, functionCalls, values, model);
|
|
6741
|
+
const funcs = parseFunctionCalls(ai, functionCalls, this.values, model);
|
|
6549
6742
|
if (funcs) {
|
|
6550
6743
|
if (!functions) {
|
|
6551
6744
|
throw new Error("Functions are not defined");
|
|
@@ -6556,22 +6749,24 @@ Content: ${content}`
|
|
|
6556
6749
|
funcs,
|
|
6557
6750
|
mem,
|
|
6558
6751
|
sessionId,
|
|
6559
|
-
traceId
|
|
6752
|
+
traceId,
|
|
6753
|
+
span,
|
|
6754
|
+
this.excludeContentFromTrace
|
|
6560
6755
|
);
|
|
6561
6756
|
this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
|
|
6562
6757
|
} else {
|
|
6563
|
-
streamingExtractFinalValue(this.signature, values, xstate, content);
|
|
6758
|
+
streamingExtractFinalValue(this.signature, this.values, xstate, content);
|
|
6564
6759
|
await assertStreamingAssertions(
|
|
6565
6760
|
this.streamingAsserts,
|
|
6566
6761
|
xstate,
|
|
6567
6762
|
content,
|
|
6568
6763
|
true
|
|
6569
6764
|
);
|
|
6570
|
-
await assertAssertions(this.asserts, values);
|
|
6765
|
+
await assertAssertions(this.asserts, this.values);
|
|
6571
6766
|
if (this.fieldProcessors.length) {
|
|
6572
6767
|
await processFieldProcessors(
|
|
6573
6768
|
this.fieldProcessors,
|
|
6574
|
-
values,
|
|
6769
|
+
this.values,
|
|
6575
6770
|
mem,
|
|
6576
6771
|
sessionId
|
|
6577
6772
|
);
|
|
@@ -6582,12 +6777,17 @@ Content: ${content}`
|
|
|
6582
6777
|
content,
|
|
6583
6778
|
xstate,
|
|
6584
6779
|
mem,
|
|
6585
|
-
values,
|
|
6780
|
+
this.values,
|
|
6586
6781
|
sessionId,
|
|
6587
6782
|
true
|
|
6588
6783
|
);
|
|
6589
6784
|
}
|
|
6590
|
-
yield* streamValues(
|
|
6785
|
+
yield* streamValues(
|
|
6786
|
+
this.signature,
|
|
6787
|
+
content,
|
|
6788
|
+
this.values,
|
|
6789
|
+
xstate
|
|
6790
|
+
);
|
|
6591
6791
|
}
|
|
6592
6792
|
}
|
|
6593
6793
|
async processResponse({
|
|
@@ -6596,9 +6796,10 @@ Content: ${content}`
|
|
|
6596
6796
|
mem,
|
|
6597
6797
|
sessionId,
|
|
6598
6798
|
traceId,
|
|
6599
|
-
functions
|
|
6799
|
+
functions,
|
|
6800
|
+
span
|
|
6600
6801
|
}) {
|
|
6601
|
-
|
|
6802
|
+
this.values = {};
|
|
6602
6803
|
let results = res.results ?? [];
|
|
6603
6804
|
if (results.length > 1) {
|
|
6604
6805
|
results = results.filter((r) => r.functionCalls);
|
|
@@ -6609,7 +6810,7 @@ Content: ${content}`
|
|
|
6609
6810
|
}
|
|
6610
6811
|
mem.addResult(result, sessionId);
|
|
6611
6812
|
if (result.functionCalls?.length) {
|
|
6612
|
-
const funcs = parseFunctionCalls(ai, result.functionCalls, values);
|
|
6813
|
+
const funcs = parseFunctionCalls(ai, result.functionCalls, this.values);
|
|
6613
6814
|
if (funcs) {
|
|
6614
6815
|
if (!functions) {
|
|
6615
6816
|
throw new Error("Functions are not defined");
|
|
@@ -6620,17 +6821,22 @@ Content: ${content}`
|
|
|
6620
6821
|
funcs,
|
|
6621
6822
|
mem,
|
|
6622
6823
|
sessionId,
|
|
6623
|
-
traceId
|
|
6824
|
+
traceId,
|
|
6825
|
+
span,
|
|
6826
|
+
this.excludeContentFromTrace
|
|
6624
6827
|
);
|
|
6625
6828
|
this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
|
|
6626
6829
|
}
|
|
6627
6830
|
} else if (result.content) {
|
|
6628
|
-
|
|
6629
|
-
|
|
6831
|
+
if (result.thought && result.thought.length > 0) {
|
|
6832
|
+
this.values.thought = result.thought;
|
|
6833
|
+
}
|
|
6834
|
+
extractValues(this.signature, this.values, result.content);
|
|
6835
|
+
await assertAssertions(this.asserts, this.values);
|
|
6630
6836
|
if (this.fieldProcessors.length) {
|
|
6631
6837
|
await processFieldProcessors(
|
|
6632
6838
|
this.fieldProcessors,
|
|
6633
|
-
values,
|
|
6839
|
+
this.values,
|
|
6634
6840
|
mem,
|
|
6635
6841
|
sessionId
|
|
6636
6842
|
);
|
|
@@ -6643,13 +6849,12 @@ Content: ${result.content}`
|
|
|
6643
6849
|
);
|
|
6644
6850
|
}
|
|
6645
6851
|
}
|
|
6646
|
-
const publicValues = { ...values };
|
|
6647
6852
|
for (const field of this.signature.getOutputFields()) {
|
|
6648
6853
|
if (field.isInternal) {
|
|
6649
|
-
delete
|
|
6854
|
+
delete this.values[field.name];
|
|
6650
6855
|
}
|
|
6651
6856
|
}
|
|
6652
|
-
return { ...values };
|
|
6857
|
+
return { ...this.values };
|
|
6653
6858
|
}
|
|
6654
6859
|
async *_forward2(ai, values, options, span) {
|
|
6655
6860
|
const stopFunction = (options?.stopFunction ?? this.options?.stopFunction)?.toLowerCase();
|
|
@@ -6675,7 +6880,7 @@ Content: ${result.content}`
|
|
|
6675
6880
|
multiStepLoop: for (let n = 0; n < maxSteps; n++) {
|
|
6676
6881
|
for (let errCount = 0; errCount < maxRetries; errCount++) {
|
|
6677
6882
|
try {
|
|
6678
|
-
const generator = this.forwardCore({ options, ai, mem });
|
|
6883
|
+
const generator = this.forwardCore({ options, ai, mem, span });
|
|
6679
6884
|
for await (const delta of generator) {
|
|
6680
6885
|
if (delta !== void 0) {
|
|
6681
6886
|
yield { version: errCount, delta };
|
|
@@ -6699,10 +6904,22 @@ Content: ${result.content}`
|
|
|
6699
6904
|
if (e instanceof ValidationError) {
|
|
6700
6905
|
errorFields = e.getFixingInstructions();
|
|
6701
6906
|
err = e;
|
|
6907
|
+
if (span) {
|
|
6908
|
+
span.addEvent("validation.error", {
|
|
6909
|
+
message: e.toString(),
|
|
6910
|
+
fixing_instructions: errorFields?.map((f) => f.title).join(", ") ?? ""
|
|
6911
|
+
});
|
|
6912
|
+
}
|
|
6702
6913
|
} else if (e instanceof AxAssertionError) {
|
|
6703
6914
|
const e1 = e;
|
|
6704
6915
|
errorFields = e1.getFixingInstructions();
|
|
6705
6916
|
err = e;
|
|
6917
|
+
if (span) {
|
|
6918
|
+
span.addEvent("assertion.error", {
|
|
6919
|
+
message: e1.toString(),
|
|
6920
|
+
fixing_instructions: errorFields?.map((f) => f.title).join(", ") ?? ""
|
|
6921
|
+
});
|
|
6922
|
+
}
|
|
6706
6923
|
} else if (e instanceof AxAIServiceStreamTerminatedError) {
|
|
6707
6924
|
} else {
|
|
6708
6925
|
throw enhanceError(e, ai, this.signature);
|
|
@@ -6757,14 +6974,25 @@ Content: ${result.content}`
|
|
|
6757
6974
|
}
|
|
6758
6975
|
const funcNames = functions?.map((f) => f.name).join(",");
|
|
6759
6976
|
const attributes = {
|
|
6760
|
-
|
|
6761
|
-
|
|
6977
|
+
signature: JSON.stringify(this.signature.toJSON(), null, 2),
|
|
6978
|
+
...this.examples ? { examples: JSON.stringify(this.examples, null, 2) } : {},
|
|
6979
|
+
...funcNames ? { provided_functions: funcNames } : {},
|
|
6980
|
+
...options?.model ? { model: options.model } : {},
|
|
6981
|
+
...options?.thinkingTokenBudget ? { thinking_token_budget: options.thinkingTokenBudget } : {},
|
|
6982
|
+
...options?.maxSteps ? { max_steps: options.maxSteps } : {},
|
|
6983
|
+
...options?.maxRetries ? { max_retries: options.maxRetries } : {},
|
|
6984
|
+
...options?.fastFail ? { fast_fail: options.fastFail } : {}
|
|
6762
6985
|
};
|
|
6763
|
-
const
|
|
6986
|
+
const traceLabel = options.traceLabel ?? this.options?.traceLabel;
|
|
6987
|
+
const spanName = traceLabel ? `${traceLabel} (AxGen)` : "AxGen";
|
|
6988
|
+
const span = tracer.startSpan(spanName, {
|
|
6764
6989
|
kind: import_api21.SpanKind.SERVER,
|
|
6765
6990
|
attributes
|
|
6766
6991
|
});
|
|
6767
6992
|
try {
|
|
6993
|
+
if (!this.excludeContentFromTrace) {
|
|
6994
|
+
span.addEvent("input", { content: JSON.stringify(values, null, 2) });
|
|
6995
|
+
}
|
|
6768
6996
|
yield* this._forward2(
|
|
6769
6997
|
ai,
|
|
6770
6998
|
values,
|
|
@@ -6774,6 +7002,11 @@ Content: ${result.content}`
|
|
|
6774
7002
|
},
|
|
6775
7003
|
span
|
|
6776
7004
|
);
|
|
7005
|
+
if (!this.excludeContentFromTrace) {
|
|
7006
|
+
span.addEvent("output", {
|
|
7007
|
+
content: JSON.stringify(this.values, null, 2)
|
|
7008
|
+
});
|
|
7009
|
+
}
|
|
6777
7010
|
} finally {
|
|
6778
7011
|
span.end();
|
|
6779
7012
|
}
|