@ax-llm/ax 11.0.41 → 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 +310 -73
- package/index.cjs.map +1 -1
- package/index.d.cts +49 -7
- package/index.d.ts +49 -7
- package/index.js +310 -73
- 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
|
};
|
|
@@ -901,6 +904,8 @@ var AxBaseAI = class {
|
|
|
901
904
|
rt;
|
|
902
905
|
fetch;
|
|
903
906
|
tracer;
|
|
907
|
+
timeout;
|
|
908
|
+
excludeContentFromTrace;
|
|
904
909
|
models;
|
|
905
910
|
modelInfo;
|
|
906
911
|
modelUsage;
|
|
@@ -959,14 +964,18 @@ var AxBaseAI = class {
|
|
|
959
964
|
this.debug = options.debug ?? false;
|
|
960
965
|
this.rt = options.rateLimiter;
|
|
961
966
|
this.fetch = options.fetch;
|
|
967
|
+
this.timeout = options.timeout;
|
|
962
968
|
this.tracer = options.tracer;
|
|
969
|
+
this.excludeContentFromTrace = options.excludeContentFromTrace;
|
|
963
970
|
}
|
|
964
971
|
getOptions() {
|
|
965
972
|
return {
|
|
966
973
|
debug: this.debug,
|
|
967
974
|
rateLimiter: this.rt,
|
|
968
975
|
fetch: this.fetch,
|
|
969
|
-
tracer: this.tracer
|
|
976
|
+
tracer: this.tracer,
|
|
977
|
+
timeout: this.timeout,
|
|
978
|
+
excludeContentFromTrace: this.excludeContentFromTrace
|
|
970
979
|
};
|
|
971
980
|
}
|
|
972
981
|
getModelList() {
|
|
@@ -1064,13 +1073,18 @@ var AxBaseAI = class {
|
|
|
1064
1073
|
if (!canStream) {
|
|
1065
1074
|
modelConfig.stream = false;
|
|
1066
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
|
+
}
|
|
1067
1080
|
if (this.tracer) {
|
|
1068
1081
|
return await this.tracer?.startActiveSpan(
|
|
1069
|
-
"Chat Request",
|
|
1082
|
+
"AI Chat Request",
|
|
1070
1083
|
{
|
|
1071
1084
|
kind: import_api2.SpanKind.SERVER,
|
|
1072
1085
|
attributes: {
|
|
1073
1086
|
[axSpanAttributes.LLM_SYSTEM]: this.name,
|
|
1087
|
+
[axSpanAttributes.LLM_OPERATION_NAME]: "chat",
|
|
1074
1088
|
[axSpanAttributes.LLM_REQUEST_MODEL]: model,
|
|
1075
1089
|
[axSpanAttributes.LLM_REQUEST_MAX_TOKENS]: modelConfig.maxTokens,
|
|
1076
1090
|
[axSpanAttributes.LLM_REQUEST_TEMPERATURE]: modelConfig.temperature,
|
|
@@ -1080,17 +1094,10 @@ var AxBaseAI = class {
|
|
|
1080
1094
|
[axSpanAttributes.LLM_REQUEST_PRESENCE_PENALTY]: modelConfig.presencePenalty,
|
|
1081
1095
|
[axSpanAttributes.LLM_REQUEST_STOP_SEQUENCES]: modelConfig.stopSequences?.join(", "),
|
|
1082
1096
|
[axSpanAttributes.LLM_REQUEST_LLM_IS_STREAMING]: modelConfig.stream
|
|
1083
|
-
// [AxSpanAttributes.LLM_PROMPTS]: _req.chatPrompt
|
|
1084
|
-
// ?.map((v) => v.content)
|
|
1085
|
-
// .join('\n')
|
|
1086
1097
|
}
|
|
1087
1098
|
},
|
|
1088
1099
|
async (span) => {
|
|
1089
|
-
|
|
1090
|
-
return await this._chat2(model, modelConfig, req, options, span);
|
|
1091
|
-
} finally {
|
|
1092
|
-
span.end();
|
|
1093
|
-
}
|
|
1100
|
+
return await this._chat2(model, modelConfig, req, options, span);
|
|
1094
1101
|
}
|
|
1095
1102
|
);
|
|
1096
1103
|
}
|
|
@@ -1136,12 +1143,16 @@ var AxBaseAI = class {
|
|
|
1136
1143
|
req,
|
|
1137
1144
|
options
|
|
1138
1145
|
);
|
|
1146
|
+
if (span?.isRecording()) {
|
|
1147
|
+
setChatRequestEvents(chatReq, span, this.excludeContentFromTrace);
|
|
1148
|
+
}
|
|
1139
1149
|
const res2 = await apiCall(
|
|
1140
1150
|
{
|
|
1141
1151
|
name: apiConfig.name,
|
|
1142
1152
|
url: this.apiURL,
|
|
1143
1153
|
headers: await this.buildHeaders(apiConfig.headers),
|
|
1144
1154
|
stream: modelConfig.stream,
|
|
1155
|
+
timeout: this.timeout,
|
|
1145
1156
|
debug,
|
|
1146
1157
|
fetch: this.fetch,
|
|
1147
1158
|
span
|
|
@@ -1172,7 +1183,7 @@ var AxBaseAI = class {
|
|
|
1172
1183
|
}
|
|
1173
1184
|
this.modelUsage = res2.modelUsage;
|
|
1174
1185
|
if (span?.isRecording()) {
|
|
1175
|
-
|
|
1186
|
+
setChatResponseEvents(res2, span, this.excludeContentFromTrace);
|
|
1176
1187
|
}
|
|
1177
1188
|
if (debug) {
|
|
1178
1189
|
logResponse(res2);
|
|
@@ -1183,6 +1194,9 @@ var AxBaseAI = class {
|
|
|
1183
1194
|
if (debug) {
|
|
1184
1195
|
process.stdout.write("\n");
|
|
1185
1196
|
}
|
|
1197
|
+
if (span?.isRecording()) {
|
|
1198
|
+
span.end();
|
|
1199
|
+
}
|
|
1186
1200
|
};
|
|
1187
1201
|
const st = rv.pipeThrough(
|
|
1188
1202
|
new RespTransformStream(
|
|
@@ -1197,16 +1211,26 @@ var AxBaseAI = class {
|
|
|
1197
1211
|
}
|
|
1198
1212
|
const res = this.aiImpl.createChatResp(rv);
|
|
1199
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
|
+
}
|
|
1200
1224
|
if (res.modelUsage) {
|
|
1201
1225
|
this.modelUsage = res.modelUsage;
|
|
1202
1226
|
}
|
|
1203
1227
|
if (span?.isRecording()) {
|
|
1204
|
-
|
|
1228
|
+
setChatResponseEvents(res, span, this.excludeContentFromTrace);
|
|
1229
|
+
span.end();
|
|
1205
1230
|
}
|
|
1206
1231
|
if (debug) {
|
|
1207
1232
|
logResponse(res);
|
|
1208
1233
|
}
|
|
1209
|
-
span?.end();
|
|
1210
1234
|
return res;
|
|
1211
1235
|
}
|
|
1212
1236
|
async embed(req, options) {
|
|
@@ -1230,11 +1254,12 @@ var AxBaseAI = class {
|
|
|
1230
1254
|
}
|
|
1231
1255
|
if (this.tracer) {
|
|
1232
1256
|
await this.tracer?.startActiveSpan(
|
|
1233
|
-
"Embed Request",
|
|
1257
|
+
"AI Embed Request",
|
|
1234
1258
|
{
|
|
1235
1259
|
kind: import_api2.SpanKind.SERVER,
|
|
1236
1260
|
attributes: {
|
|
1237
1261
|
[axSpanAttributes.LLM_SYSTEM]: this.name,
|
|
1262
|
+
[axSpanAttributes.LLM_OPERATION_NAME]: "embeddings",
|
|
1238
1263
|
[axSpanAttributes.LLM_REQUEST_MODEL]: embedModel
|
|
1239
1264
|
}
|
|
1240
1265
|
},
|
|
@@ -1271,6 +1296,7 @@ var AxBaseAI = class {
|
|
|
1271
1296
|
headers: await this.buildHeaders(apiConfig.headers),
|
|
1272
1297
|
debug,
|
|
1273
1298
|
fetch: this.fetch,
|
|
1299
|
+
timeout: this.timeout,
|
|
1274
1300
|
span
|
|
1275
1301
|
},
|
|
1276
1302
|
reqValue
|
|
@@ -1288,8 +1314,12 @@ var AxBaseAI = class {
|
|
|
1288
1314
|
};
|
|
1289
1315
|
}
|
|
1290
1316
|
this.embedModelUsage = res.modelUsage;
|
|
1291
|
-
if (span?.isRecording()) {
|
|
1292
|
-
|
|
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
|
+
});
|
|
1293
1323
|
}
|
|
1294
1324
|
span?.end();
|
|
1295
1325
|
return res;
|
|
@@ -1313,11 +1343,113 @@ var AxBaseAI = class {
|
|
|
1313
1343
|
return item && "embedModel" in item ? item.embedModel : void 0;
|
|
1314
1344
|
}
|
|
1315
1345
|
};
|
|
1316
|
-
function
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
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)
|
|
1321
1453
|
});
|
|
1322
1454
|
}
|
|
1323
1455
|
}
|
|
@@ -2710,7 +2842,7 @@ var AxAIDeepSeek = class extends AxAIOpenAIBase {
|
|
|
2710
2842
|
|
|
2711
2843
|
// ai/google-gemini/types.ts
|
|
2712
2844
|
var AxAIGoogleGeminiModel = /* @__PURE__ */ ((AxAIGoogleGeminiModel2) => {
|
|
2713
|
-
AxAIGoogleGeminiModel2["Gemini25Pro"] = "gemini-2.5-pro-preview-
|
|
2845
|
+
AxAIGoogleGeminiModel2["Gemini25Pro"] = "gemini-2.5-pro-preview-05-06";
|
|
2714
2846
|
AxAIGoogleGeminiModel2["Gemini25Flash"] = "gemini-2.5-flash-preview-04-17";
|
|
2715
2847
|
AxAIGoogleGeminiModel2["Gemini20Flash"] = "gemini-2.0-flash";
|
|
2716
2848
|
AxAIGoogleGeminiModel2["Gemini20FlashLite"] = "gemini-2.0-flash-lite-preview-02-05";
|
|
@@ -2759,7 +2891,7 @@ var AxAIGoogleGeminiEmbedTypes = /* @__PURE__ */ ((AxAIGoogleGeminiEmbedTypes2)
|
|
|
2759
2891
|
// ai/google-gemini/info.ts
|
|
2760
2892
|
var axModelInfoGoogleGemini = [
|
|
2761
2893
|
{
|
|
2762
|
-
name: "gemini-2.5-pro-preview-
|
|
2894
|
+
name: "gemini-2.5-pro-preview-05-06" /* Gemini25Pro */,
|
|
2763
2895
|
currency: "usd",
|
|
2764
2896
|
characterIsToken: false,
|
|
2765
2897
|
promptTokenCostPer1M: 2.5,
|
|
@@ -2843,7 +2975,7 @@ var safetySettings = [
|
|
|
2843
2975
|
}
|
|
2844
2976
|
];
|
|
2845
2977
|
var axAIGoogleGeminiDefaultConfig = () => structuredClone({
|
|
2846
|
-
model: "gemini-2.
|
|
2978
|
+
model: "gemini-2.5-flash-preview-04-17" /* Gemini25Flash */,
|
|
2847
2979
|
embedModel: "text-embedding-005" /* TextEmbedding005 */,
|
|
2848
2980
|
safetySettings,
|
|
2849
2981
|
...axBaseAIDefaultConfig()
|
|
@@ -2884,7 +3016,7 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2884
3016
|
n: config.n
|
|
2885
3017
|
};
|
|
2886
3018
|
}
|
|
2887
|
-
createChatReq = (req) => {
|
|
3019
|
+
createChatReq = (req, config) => {
|
|
2888
3020
|
const model = req.model;
|
|
2889
3021
|
const stream = req.modelConfig?.stream ?? this.config.stream;
|
|
2890
3022
|
if (!req.chatPrompt || req.chatPrompt.length === 0) {
|
|
@@ -2995,6 +3127,9 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2995
3127
|
}
|
|
2996
3128
|
});
|
|
2997
3129
|
}
|
|
3130
|
+
if (this.options?.urlContext) {
|
|
3131
|
+
tools.push({ url_context: {} });
|
|
3132
|
+
}
|
|
2998
3133
|
if (tools.length === 0) {
|
|
2999
3134
|
tools = void 0;
|
|
3000
3135
|
}
|
|
@@ -3020,6 +3155,16 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
3020
3155
|
} else if (tools && tools.length > 0) {
|
|
3021
3156
|
toolConfig = { function_calling_config: { mode: "AUTO" } };
|
|
3022
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
|
+
}
|
|
3023
3168
|
const generationConfig = {
|
|
3024
3169
|
maxOutputTokens: req.modelConfig?.maxTokens ?? this.config.maxTokens,
|
|
3025
3170
|
temperature: req.modelConfig?.temperature ?? this.config.temperature,
|
|
@@ -3029,9 +3174,7 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
3029
3174
|
candidateCount: 1,
|
|
3030
3175
|
stopSequences: req.modelConfig?.stopSequences ?? this.config.stopSequences,
|
|
3031
3176
|
responseMimeType: "text/plain",
|
|
3032
|
-
...
|
|
3033
|
-
thinkingConfig: this.config.thinkingConfig
|
|
3034
|
-
}
|
|
3177
|
+
...thinkingConfig ? { thinkingConfig } : {}
|
|
3035
3178
|
};
|
|
3036
3179
|
const safetySettings2 = this.config.safetySettings;
|
|
3037
3180
|
const reqValue = {
|
|
@@ -3112,7 +3255,11 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
3112
3255
|
}
|
|
3113
3256
|
for (const part of candidate.content.parts) {
|
|
3114
3257
|
if ("text" in part) {
|
|
3115
|
-
|
|
3258
|
+
if ("thought" in part && part.thought) {
|
|
3259
|
+
result.thought = part.text;
|
|
3260
|
+
} else {
|
|
3261
|
+
result.content = part.text;
|
|
3262
|
+
}
|
|
3116
3263
|
continue;
|
|
3117
3264
|
}
|
|
3118
3265
|
if ("functionCall" in part) {
|
|
@@ -3135,7 +3282,8 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
3135
3282
|
this.tokensUsed = {
|
|
3136
3283
|
totalTokens: resp.usageMetadata.totalTokenCount,
|
|
3137
3284
|
promptTokens: resp.usageMetadata.promptTokenCount,
|
|
3138
|
-
completionTokens: resp.usageMetadata.candidatesTokenCount
|
|
3285
|
+
completionTokens: resp.usageMetadata.candidatesTokenCount,
|
|
3286
|
+
thoughtsTokens: resp.usageMetadata.thoughtsTokenCount
|
|
3139
3287
|
};
|
|
3140
3288
|
}
|
|
3141
3289
|
return { results };
|
|
@@ -4726,6 +4874,14 @@ var AxSignature = class _AxSignature {
|
|
|
4726
4874
|
};
|
|
4727
4875
|
hash = () => this.sigHash;
|
|
4728
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
|
+
};
|
|
4729
4885
|
};
|
|
4730
4886
|
function renderField(field) {
|
|
4731
4887
|
let result = field.name;
|
|
@@ -5949,7 +6105,7 @@ function* streamValues(sig, content, values, xstate) {
|
|
|
5949
6105
|
const v = value.slice(s);
|
|
5950
6106
|
if (v && v.length > 0) {
|
|
5951
6107
|
yield { [key]: v };
|
|
5952
|
-
xstate.streamedIndex[key] = s +
|
|
6108
|
+
xstate.streamedIndex[key] = s + v.length;
|
|
5953
6109
|
}
|
|
5954
6110
|
continue;
|
|
5955
6111
|
}
|
|
@@ -6266,7 +6422,7 @@ var parseFunctions = (newFuncs, existingFuncs) => {
|
|
|
6266
6422
|
}
|
|
6267
6423
|
return [...existingFuncs ?? [], ...functions];
|
|
6268
6424
|
};
|
|
6269
|
-
var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, traceId) => {
|
|
6425
|
+
var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, traceId, span, excludeContentFromTelemetry) => {
|
|
6270
6426
|
const funcProc = new AxFunctionProcessor(functionList);
|
|
6271
6427
|
const functionsExecuted = /* @__PURE__ */ new Set();
|
|
6272
6428
|
const promises = functionCalls.map((func) => {
|
|
@@ -6275,6 +6431,16 @@ var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, t
|
|
|
6275
6431
|
}
|
|
6276
6432
|
const promise = funcProc.execute(func, { sessionId, traceId, ai }).then((functionResult) => {
|
|
6277
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
|
+
}
|
|
6278
6444
|
return {
|
|
6279
6445
|
role: "function",
|
|
6280
6446
|
result: functionResult ?? "",
|
|
@@ -6283,6 +6449,17 @@ var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, t
|
|
|
6283
6449
|
}).catch((e) => {
|
|
6284
6450
|
if (e instanceof FunctionError) {
|
|
6285
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
|
+
}
|
|
6286
6463
|
mem.add(
|
|
6287
6464
|
{
|
|
6288
6465
|
role: "function",
|
|
@@ -6340,6 +6517,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6340
6517
|
functionsExecuted = /* @__PURE__ */ new Set();
|
|
6341
6518
|
fieldProcessors = [];
|
|
6342
6519
|
streamingFieldProcessors = [];
|
|
6520
|
+
values = {};
|
|
6521
|
+
excludeContentFromTrace = false;
|
|
6343
6522
|
constructor(signature, options) {
|
|
6344
6523
|
super(signature, { description: options?.description });
|
|
6345
6524
|
this.options = options;
|
|
@@ -6349,6 +6528,7 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6349
6528
|
);
|
|
6350
6529
|
this.asserts = this.options?.asserts ?? [];
|
|
6351
6530
|
this.streamingAsserts = this.options?.streamingAsserts ?? [];
|
|
6531
|
+
this.excludeContentFromTrace = options?.excludeContentFromTrace ?? false;
|
|
6352
6532
|
this.usage = [];
|
|
6353
6533
|
if (options?.functions) {
|
|
6354
6534
|
this.functions = parseFunctions(options.functions);
|
|
@@ -6397,7 +6577,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6397
6577
|
rateLimiter,
|
|
6398
6578
|
stream,
|
|
6399
6579
|
functions: _functions,
|
|
6400
|
-
functionCall: _functionCall
|
|
6580
|
+
functionCall: _functionCall,
|
|
6581
|
+
thinkingTokenBudget
|
|
6401
6582
|
} = options ?? {};
|
|
6402
6583
|
const chatPrompt = mem?.history(sessionId) ?? [];
|
|
6403
6584
|
if (chatPrompt.length === 0) {
|
|
@@ -6418,7 +6599,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6418
6599
|
traceId,
|
|
6419
6600
|
rateLimiter,
|
|
6420
6601
|
stream,
|
|
6421
|
-
debug: false
|
|
6602
|
+
debug: false,
|
|
6603
|
+
thinkingTokenBudget
|
|
6422
6604
|
}
|
|
6423
6605
|
);
|
|
6424
6606
|
return res;
|
|
@@ -6426,7 +6608,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6426
6608
|
async *forwardCore({
|
|
6427
6609
|
ai,
|
|
6428
6610
|
mem,
|
|
6429
|
-
options
|
|
6611
|
+
options,
|
|
6612
|
+
span
|
|
6430
6613
|
}) {
|
|
6431
6614
|
const { sessionId, traceId, functions: _functions } = options ?? {};
|
|
6432
6615
|
const fastFail = options?.fastFail ?? this.options?.fastFail;
|
|
@@ -6446,7 +6629,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6446
6629
|
traceId,
|
|
6447
6630
|
sessionId,
|
|
6448
6631
|
functions,
|
|
6449
|
-
fastFail
|
|
6632
|
+
fastFail,
|
|
6633
|
+
span
|
|
6450
6634
|
});
|
|
6451
6635
|
} else {
|
|
6452
6636
|
yield await this.processResponse({
|
|
@@ -6456,7 +6640,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6456
6640
|
mem,
|
|
6457
6641
|
traceId,
|
|
6458
6642
|
sessionId,
|
|
6459
|
-
functions
|
|
6643
|
+
functions,
|
|
6644
|
+
span
|
|
6460
6645
|
});
|
|
6461
6646
|
}
|
|
6462
6647
|
}
|
|
@@ -6468,11 +6653,12 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6468
6653
|
sessionId,
|
|
6469
6654
|
traceId,
|
|
6470
6655
|
functions,
|
|
6471
|
-
fastFail
|
|
6656
|
+
fastFail,
|
|
6657
|
+
span
|
|
6472
6658
|
}) {
|
|
6473
6659
|
const streamingValidation = fastFail ?? ai.getFeatures(model).functionCot !== true;
|
|
6474
6660
|
const functionCalls = [];
|
|
6475
|
-
|
|
6661
|
+
this.values = {};
|
|
6476
6662
|
const xstate = {
|
|
6477
6663
|
extractedFields: [],
|
|
6478
6664
|
streamedIndex: {},
|
|
@@ -6499,6 +6685,9 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6499
6685
|
sessionId
|
|
6500
6686
|
);
|
|
6501
6687
|
} else if (result.content) {
|
|
6688
|
+
if (result.thought && result.thought.length > 0) {
|
|
6689
|
+
yield { thought: result.thought };
|
|
6690
|
+
}
|
|
6502
6691
|
content += result.content;
|
|
6503
6692
|
mem.updateResult(
|
|
6504
6693
|
{ name: result.name, content, delta: result.content },
|
|
@@ -6506,7 +6695,7 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6506
6695
|
);
|
|
6507
6696
|
const skip = streamingExtractValues(
|
|
6508
6697
|
this.signature,
|
|
6509
|
-
values,
|
|
6698
|
+
this.values,
|
|
6510
6699
|
xstate,
|
|
6511
6700
|
content,
|
|
6512
6701
|
streamingValidation
|
|
@@ -6527,12 +6716,20 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6527
6716
|
content,
|
|
6528
6717
|
xstate,
|
|
6529
6718
|
mem,
|
|
6530
|
-
values,
|
|
6719
|
+
this.values,
|
|
6531
6720
|
sessionId
|
|
6532
6721
|
);
|
|
6533
6722
|
}
|
|
6534
|
-
yield* streamValues(
|
|
6535
|
-
|
|
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 };
|
|
6536
6733
|
}
|
|
6537
6734
|
if (result.finishReason === "length") {
|
|
6538
6735
|
throw new Error(
|
|
@@ -6541,7 +6738,7 @@ Content: ${content}`
|
|
|
6541
6738
|
);
|
|
6542
6739
|
}
|
|
6543
6740
|
}
|
|
6544
|
-
const funcs = parseFunctionCalls(ai, functionCalls, values, model);
|
|
6741
|
+
const funcs = parseFunctionCalls(ai, functionCalls, this.values, model);
|
|
6545
6742
|
if (funcs) {
|
|
6546
6743
|
if (!functions) {
|
|
6547
6744
|
throw new Error("Functions are not defined");
|
|
@@ -6552,22 +6749,24 @@ Content: ${content}`
|
|
|
6552
6749
|
funcs,
|
|
6553
6750
|
mem,
|
|
6554
6751
|
sessionId,
|
|
6555
|
-
traceId
|
|
6752
|
+
traceId,
|
|
6753
|
+
span,
|
|
6754
|
+
this.excludeContentFromTrace
|
|
6556
6755
|
);
|
|
6557
6756
|
this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
|
|
6558
6757
|
} else {
|
|
6559
|
-
streamingExtractFinalValue(this.signature, values, xstate, content);
|
|
6758
|
+
streamingExtractFinalValue(this.signature, this.values, xstate, content);
|
|
6560
6759
|
await assertStreamingAssertions(
|
|
6561
6760
|
this.streamingAsserts,
|
|
6562
6761
|
xstate,
|
|
6563
6762
|
content,
|
|
6564
6763
|
true
|
|
6565
6764
|
);
|
|
6566
|
-
await assertAssertions(this.asserts, values);
|
|
6765
|
+
await assertAssertions(this.asserts, this.values);
|
|
6567
6766
|
if (this.fieldProcessors.length) {
|
|
6568
6767
|
await processFieldProcessors(
|
|
6569
6768
|
this.fieldProcessors,
|
|
6570
|
-
values,
|
|
6769
|
+
this.values,
|
|
6571
6770
|
mem,
|
|
6572
6771
|
sessionId
|
|
6573
6772
|
);
|
|
@@ -6578,12 +6777,17 @@ Content: ${content}`
|
|
|
6578
6777
|
content,
|
|
6579
6778
|
xstate,
|
|
6580
6779
|
mem,
|
|
6581
|
-
values,
|
|
6780
|
+
this.values,
|
|
6582
6781
|
sessionId,
|
|
6583
6782
|
true
|
|
6584
6783
|
);
|
|
6585
6784
|
}
|
|
6586
|
-
yield* streamValues(
|
|
6785
|
+
yield* streamValues(
|
|
6786
|
+
this.signature,
|
|
6787
|
+
content,
|
|
6788
|
+
this.values,
|
|
6789
|
+
xstate
|
|
6790
|
+
);
|
|
6587
6791
|
}
|
|
6588
6792
|
}
|
|
6589
6793
|
async processResponse({
|
|
@@ -6592,9 +6796,10 @@ Content: ${content}`
|
|
|
6592
6796
|
mem,
|
|
6593
6797
|
sessionId,
|
|
6594
6798
|
traceId,
|
|
6595
|
-
functions
|
|
6799
|
+
functions,
|
|
6800
|
+
span
|
|
6596
6801
|
}) {
|
|
6597
|
-
|
|
6802
|
+
this.values = {};
|
|
6598
6803
|
let results = res.results ?? [];
|
|
6599
6804
|
if (results.length > 1) {
|
|
6600
6805
|
results = results.filter((r) => r.functionCalls);
|
|
@@ -6605,7 +6810,7 @@ Content: ${content}`
|
|
|
6605
6810
|
}
|
|
6606
6811
|
mem.addResult(result, sessionId);
|
|
6607
6812
|
if (result.functionCalls?.length) {
|
|
6608
|
-
const funcs = parseFunctionCalls(ai, result.functionCalls, values);
|
|
6813
|
+
const funcs = parseFunctionCalls(ai, result.functionCalls, this.values);
|
|
6609
6814
|
if (funcs) {
|
|
6610
6815
|
if (!functions) {
|
|
6611
6816
|
throw new Error("Functions are not defined");
|
|
@@ -6616,17 +6821,22 @@ Content: ${content}`
|
|
|
6616
6821
|
funcs,
|
|
6617
6822
|
mem,
|
|
6618
6823
|
sessionId,
|
|
6619
|
-
traceId
|
|
6824
|
+
traceId,
|
|
6825
|
+
span,
|
|
6826
|
+
this.excludeContentFromTrace
|
|
6620
6827
|
);
|
|
6621
6828
|
this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
|
|
6622
6829
|
}
|
|
6623
6830
|
} else if (result.content) {
|
|
6624
|
-
|
|
6625
|
-
|
|
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);
|
|
6626
6836
|
if (this.fieldProcessors.length) {
|
|
6627
6837
|
await processFieldProcessors(
|
|
6628
6838
|
this.fieldProcessors,
|
|
6629
|
-
values,
|
|
6839
|
+
this.values,
|
|
6630
6840
|
mem,
|
|
6631
6841
|
sessionId
|
|
6632
6842
|
);
|
|
@@ -6639,13 +6849,12 @@ Content: ${result.content}`
|
|
|
6639
6849
|
);
|
|
6640
6850
|
}
|
|
6641
6851
|
}
|
|
6642
|
-
const publicValues = { ...values };
|
|
6643
6852
|
for (const field of this.signature.getOutputFields()) {
|
|
6644
6853
|
if (field.isInternal) {
|
|
6645
|
-
delete
|
|
6854
|
+
delete this.values[field.name];
|
|
6646
6855
|
}
|
|
6647
6856
|
}
|
|
6648
|
-
return { ...values };
|
|
6857
|
+
return { ...this.values };
|
|
6649
6858
|
}
|
|
6650
6859
|
async *_forward2(ai, values, options, span) {
|
|
6651
6860
|
const stopFunction = (options?.stopFunction ?? this.options?.stopFunction)?.toLowerCase();
|
|
@@ -6671,7 +6880,7 @@ Content: ${result.content}`
|
|
|
6671
6880
|
multiStepLoop: for (let n = 0; n < maxSteps; n++) {
|
|
6672
6881
|
for (let errCount = 0; errCount < maxRetries; errCount++) {
|
|
6673
6882
|
try {
|
|
6674
|
-
const generator = this.forwardCore({ options, ai, mem });
|
|
6883
|
+
const generator = this.forwardCore({ options, ai, mem, span });
|
|
6675
6884
|
for await (const delta of generator) {
|
|
6676
6885
|
if (delta !== void 0) {
|
|
6677
6886
|
yield { version: errCount, delta };
|
|
@@ -6695,10 +6904,22 @@ Content: ${result.content}`
|
|
|
6695
6904
|
if (e instanceof ValidationError) {
|
|
6696
6905
|
errorFields = e.getFixingInstructions();
|
|
6697
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
|
+
}
|
|
6698
6913
|
} else if (e instanceof AxAssertionError) {
|
|
6699
6914
|
const e1 = e;
|
|
6700
6915
|
errorFields = e1.getFixingInstructions();
|
|
6701
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
|
+
}
|
|
6702
6923
|
} else if (e instanceof AxAIServiceStreamTerminatedError) {
|
|
6703
6924
|
} else {
|
|
6704
6925
|
throw enhanceError(e, ai, this.signature);
|
|
@@ -6753,14 +6974,25 @@ Content: ${result.content}`
|
|
|
6753
6974
|
}
|
|
6754
6975
|
const funcNames = functions?.map((f) => f.name).join(",");
|
|
6755
6976
|
const attributes = {
|
|
6756
|
-
|
|
6757
|
-
|
|
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 } : {}
|
|
6758
6985
|
};
|
|
6759
|
-
const
|
|
6986
|
+
const traceLabel = options.traceLabel ?? this.options?.traceLabel;
|
|
6987
|
+
const spanName = traceLabel ? `${traceLabel} (AxGen)` : "AxGen";
|
|
6988
|
+
const span = tracer.startSpan(spanName, {
|
|
6760
6989
|
kind: import_api21.SpanKind.SERVER,
|
|
6761
6990
|
attributes
|
|
6762
6991
|
});
|
|
6763
6992
|
try {
|
|
6993
|
+
if (!this.excludeContentFromTrace) {
|
|
6994
|
+
span.addEvent("input", { content: JSON.stringify(values, null, 2) });
|
|
6995
|
+
}
|
|
6764
6996
|
yield* this._forward2(
|
|
6765
6997
|
ai,
|
|
6766
6998
|
values,
|
|
@@ -6770,6 +7002,11 @@ Content: ${result.content}`
|
|
|
6770
7002
|
},
|
|
6771
7003
|
span
|
|
6772
7004
|
);
|
|
7005
|
+
if (!this.excludeContentFromTrace) {
|
|
7006
|
+
span.addEvent("output", {
|
|
7007
|
+
content: JSON.stringify(this.values, null, 2)
|
|
7008
|
+
});
|
|
7009
|
+
}
|
|
6773
7010
|
} finally {
|
|
6774
7011
|
span.end();
|
|
6775
7012
|
}
|