@threaded/ai 1.0.25 → 1.0.26
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/.claude/settings.local.json +15 -0
- package/dist/index.cjs +84 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.js +83 -14
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"permissions": {
|
|
3
|
+
"allow": [
|
|
4
|
+
"WebSearch",
|
|
5
|
+
"WebFetch(domain:platform.openai.com)",
|
|
6
|
+
"WebFetch(domain:docs.anthropic.com)",
|
|
7
|
+
"WebFetch(domain:ai.google.dev)",
|
|
8
|
+
"WebFetch(domain:docs.x.ai)",
|
|
9
|
+
"WebFetch(domain:docs.cloud.google.com)",
|
|
10
|
+
"WebFetch(domain:latenode.com)",
|
|
11
|
+
"WebFetch(domain:github.com)",
|
|
12
|
+
"WebFetch(domain:docs.aimlapi.com)"
|
|
13
|
+
]
|
|
14
|
+
}
|
|
15
|
+
}
|
package/dist/index.cjs
CHANGED
|
@@ -33,6 +33,7 @@ __export(index_exports, {
|
|
|
33
33
|
IMAGE_EDIT_MODEL_SCHEMA: () => IMAGE_EDIT_MODEL_SCHEMA,
|
|
34
34
|
IMAGE_MODEL_SCHEMA: () => IMAGE_MODEL_SCHEMA,
|
|
35
35
|
Inherit: () => Inherit,
|
|
36
|
+
addUsage: () => addUsage,
|
|
36
37
|
appendToLastRequest: () => appendToLastRequest,
|
|
37
38
|
compose: () => compose,
|
|
38
39
|
convertMCPSchemaToToolSchema: () => convertMCPSchemaToToolSchema,
|
|
@@ -222,6 +223,11 @@ var maxCalls = (toolConfig, maxCalls2) => ({
|
|
|
222
223
|
...toolConfig,
|
|
223
224
|
_maxCalls: maxCalls2
|
|
224
225
|
});
|
|
226
|
+
var addUsage = (existing, promptTokens, completionTokens, totalTokens) => ({
|
|
227
|
+
promptTokens: (existing?.promptTokens || 0) + promptTokens,
|
|
228
|
+
completionTokens: (existing?.completionTokens || 0) + completionTokens,
|
|
229
|
+
totalTokens: (existing?.totalTokens || 0) + totalTokens
|
|
230
|
+
});
|
|
225
231
|
|
|
226
232
|
// src/embed.ts
|
|
227
233
|
var modelCache = /* @__PURE__ */ new Map();
|
|
@@ -533,7 +539,8 @@ var callOpenAI = async (config, ctx) => {
|
|
|
533
539
|
const body = {
|
|
534
540
|
model: model2,
|
|
535
541
|
messages,
|
|
536
|
-
stream: !!ctx.stream
|
|
542
|
+
stream: !!ctx.stream,
|
|
543
|
+
...ctx.stream && { stream_options: { include_usage: true } }
|
|
537
544
|
};
|
|
538
545
|
if (schema) {
|
|
539
546
|
body.response_format = {
|
|
@@ -578,7 +585,8 @@ var callOpenAI = async (config, ctx) => {
|
|
|
578
585
|
return {
|
|
579
586
|
...ctx,
|
|
580
587
|
lastResponse: msg,
|
|
581
|
-
history: [...ctx.history, msg]
|
|
588
|
+
history: [...ctx.history, msg],
|
|
589
|
+
usage: addUsage(ctx.usage, data.usage?.prompt_tokens || 0, data.usage?.completion_tokens || 0, data.usage?.total_tokens || 0)
|
|
582
590
|
};
|
|
583
591
|
};
|
|
584
592
|
var handleOpenAIStream = async (response, ctx) => {
|
|
@@ -587,6 +595,7 @@ var handleOpenAIStream = async (response, ctx) => {
|
|
|
587
595
|
let fullContent = "";
|
|
588
596
|
let toolCalls = [];
|
|
589
597
|
let buffer = "";
|
|
598
|
+
let streamUsage = null;
|
|
590
599
|
try {
|
|
591
600
|
while (true) {
|
|
592
601
|
if (ctx.abortSignal?.aborted) {
|
|
@@ -604,6 +613,9 @@ var handleOpenAIStream = async (response, ctx) => {
|
|
|
604
613
|
if (!data) continue;
|
|
605
614
|
try {
|
|
606
615
|
const parsed = JSON.parse(data);
|
|
616
|
+
if (parsed.usage) {
|
|
617
|
+
streamUsage = parsed.usage;
|
|
618
|
+
}
|
|
607
619
|
const delta = parsed.choices?.[0]?.delta;
|
|
608
620
|
if (delta?.content) {
|
|
609
621
|
fullContent += delta.content;
|
|
@@ -629,10 +641,15 @@ var handleOpenAIStream = async (response, ctx) => {
|
|
|
629
641
|
if (toolCalls.length > 0) {
|
|
630
642
|
msg.tool_calls = toolCalls;
|
|
631
643
|
}
|
|
644
|
+
const usage = addUsage(ctx.usage, streamUsage?.prompt_tokens || 0, streamUsage?.completion_tokens || 0, streamUsage?.total_tokens || 0);
|
|
645
|
+
if (ctx.stream && streamUsage) {
|
|
646
|
+
ctx.stream({ type: "usage", usage });
|
|
647
|
+
}
|
|
632
648
|
return {
|
|
633
649
|
...ctx,
|
|
634
650
|
lastResponse: msg,
|
|
635
|
-
history: [...ctx.history, msg]
|
|
651
|
+
history: [...ctx.history, msg],
|
|
652
|
+
usage
|
|
636
653
|
};
|
|
637
654
|
};
|
|
638
655
|
|
|
@@ -768,10 +785,13 @@ Return only the JSON object, no other text or formatting.`;
|
|
|
768
785
|
}
|
|
769
786
|
];
|
|
770
787
|
}
|
|
788
|
+
const inputTokens = data.usage?.input_tokens || 0;
|
|
789
|
+
const outputTokens = data.usage?.output_tokens || 0;
|
|
771
790
|
return {
|
|
772
791
|
...ctx,
|
|
773
792
|
lastResponse: msg,
|
|
774
|
-
history: [...ctx.history, msg]
|
|
793
|
+
history: [...ctx.history, msg],
|
|
794
|
+
usage: addUsage(ctx.usage, inputTokens, outputTokens, inputTokens + outputTokens)
|
|
775
795
|
};
|
|
776
796
|
};
|
|
777
797
|
var handleAnthropicStream = async (response, ctx) => {
|
|
@@ -780,6 +800,8 @@ var handleAnthropicStream = async (response, ctx) => {
|
|
|
780
800
|
let fullContent = "";
|
|
781
801
|
const toolCalls = [];
|
|
782
802
|
let buffer = "";
|
|
803
|
+
let inputTokens = 0;
|
|
804
|
+
let outputTokens = 0;
|
|
783
805
|
try {
|
|
784
806
|
while (true) {
|
|
785
807
|
if (ctx.abortSignal?.aborted) {
|
|
@@ -796,6 +818,12 @@ var handleAnthropicStream = async (response, ctx) => {
|
|
|
796
818
|
if (!data) continue;
|
|
797
819
|
try {
|
|
798
820
|
const parsed = JSON.parse(data);
|
|
821
|
+
if (parsed.type === "message_start" && parsed.message?.usage) {
|
|
822
|
+
inputTokens = parsed.message.usage.input_tokens || 0;
|
|
823
|
+
}
|
|
824
|
+
if (parsed.type === "message_delta" && parsed.usage) {
|
|
825
|
+
outputTokens = parsed.usage.output_tokens || 0;
|
|
826
|
+
}
|
|
799
827
|
if (parsed.type === "content_block_delta" && parsed.delta?.text) {
|
|
800
828
|
fullContent += parsed.delta.text;
|
|
801
829
|
if (ctx.stream) {
|
|
@@ -835,10 +863,15 @@ var handleAnthropicStream = async (response, ctx) => {
|
|
|
835
863
|
if (toolCalls.length > 0) {
|
|
836
864
|
msg.tool_calls = toolCalls.map(({ index, ...tc }) => tc);
|
|
837
865
|
}
|
|
866
|
+
const usage = addUsage(ctx.usage, inputTokens, outputTokens, inputTokens + outputTokens);
|
|
867
|
+
if (ctx.stream && (inputTokens || outputTokens)) {
|
|
868
|
+
ctx.stream({ type: "usage", usage });
|
|
869
|
+
}
|
|
838
870
|
return {
|
|
839
871
|
...ctx,
|
|
840
872
|
lastResponse: msg,
|
|
841
|
-
history: [...ctx.history, msg]
|
|
873
|
+
history: [...ctx.history, msg],
|
|
874
|
+
usage
|
|
842
875
|
};
|
|
843
876
|
};
|
|
844
877
|
|
|
@@ -991,10 +1024,12 @@ var callGoogle = async (config, ctx) => {
|
|
|
991
1024
|
if (toolCalls.length > 0) {
|
|
992
1025
|
msg.tool_calls = toolCalls;
|
|
993
1026
|
}
|
|
1027
|
+
const um = data.usageMetadata;
|
|
994
1028
|
return {
|
|
995
1029
|
...ctx,
|
|
996
1030
|
lastResponse: msg,
|
|
997
|
-
history: [...ctx.history, msg]
|
|
1031
|
+
history: [...ctx.history, msg],
|
|
1032
|
+
usage: addUsage(ctx.usage, um?.promptTokenCount || 0, um?.candidatesTokenCount || 0, um?.totalTokenCount || 0)
|
|
998
1033
|
};
|
|
999
1034
|
};
|
|
1000
1035
|
var handleGoogleStream = async (response, ctx) => {
|
|
@@ -1003,6 +1038,7 @@ var handleGoogleStream = async (response, ctx) => {
|
|
|
1003
1038
|
let fullContent = "";
|
|
1004
1039
|
const toolCalls = [];
|
|
1005
1040
|
let buffer = "";
|
|
1041
|
+
let usageMetadata = null;
|
|
1006
1042
|
try {
|
|
1007
1043
|
while (true) {
|
|
1008
1044
|
if (ctx.abortSignal?.aborted) {
|
|
@@ -1019,6 +1055,9 @@ var handleGoogleStream = async (response, ctx) => {
|
|
|
1019
1055
|
if (!data) continue;
|
|
1020
1056
|
try {
|
|
1021
1057
|
const parsed = JSON.parse(data);
|
|
1058
|
+
if (parsed.usageMetadata) {
|
|
1059
|
+
usageMetadata = parsed.usageMetadata;
|
|
1060
|
+
}
|
|
1022
1061
|
const candidate = parsed.candidates?.[0];
|
|
1023
1062
|
const parts = candidate?.content?.parts || [];
|
|
1024
1063
|
for (const part of parts) {
|
|
@@ -1058,10 +1097,16 @@ var handleGoogleStream = async (response, ctx) => {
|
|
|
1058
1097
|
if (toolCalls.length > 0) {
|
|
1059
1098
|
msg.tool_calls = toolCalls;
|
|
1060
1099
|
}
|
|
1100
|
+
const um = usageMetadata;
|
|
1101
|
+
const usage = addUsage(ctx.usage, um?.promptTokenCount || 0, um?.candidatesTokenCount || 0, um?.totalTokenCount || 0);
|
|
1102
|
+
if (ctx.stream && um) {
|
|
1103
|
+
ctx.stream({ type: "usage", usage });
|
|
1104
|
+
}
|
|
1061
1105
|
return {
|
|
1062
1106
|
...ctx,
|
|
1063
1107
|
lastResponse: msg,
|
|
1064
|
-
history: [...ctx.history, msg]
|
|
1108
|
+
history: [...ctx.history, msg],
|
|
1109
|
+
usage
|
|
1065
1110
|
};
|
|
1066
1111
|
};
|
|
1067
1112
|
|
|
@@ -1110,7 +1155,8 @@ var callXAI = async (config, ctx) => {
|
|
|
1110
1155
|
const body = {
|
|
1111
1156
|
model: model2,
|
|
1112
1157
|
messages,
|
|
1113
|
-
stream: !!ctx.stream
|
|
1158
|
+
stream: !!ctx.stream,
|
|
1159
|
+
...ctx.stream && { stream_options: { include_usage: true } }
|
|
1114
1160
|
};
|
|
1115
1161
|
if (schema) {
|
|
1116
1162
|
body.response_format = {
|
|
@@ -1155,7 +1201,8 @@ var callXAI = async (config, ctx) => {
|
|
|
1155
1201
|
return {
|
|
1156
1202
|
...ctx,
|
|
1157
1203
|
lastResponse: msg,
|
|
1158
|
-
history: [...ctx.history, msg]
|
|
1204
|
+
history: [...ctx.history, msg],
|
|
1205
|
+
usage: addUsage(ctx.usage, data.usage?.prompt_tokens || 0, data.usage?.completion_tokens || 0, data.usage?.total_tokens || 0)
|
|
1159
1206
|
};
|
|
1160
1207
|
};
|
|
1161
1208
|
var handleXAIStream = async (response, ctx) => {
|
|
@@ -1164,6 +1211,7 @@ var handleXAIStream = async (response, ctx) => {
|
|
|
1164
1211
|
let fullContent = "";
|
|
1165
1212
|
let toolCalls = [];
|
|
1166
1213
|
let buffer = "";
|
|
1214
|
+
let streamUsage = null;
|
|
1167
1215
|
try {
|
|
1168
1216
|
while (true) {
|
|
1169
1217
|
if (ctx.abortSignal?.aborted) {
|
|
@@ -1181,6 +1229,9 @@ var handleXAIStream = async (response, ctx) => {
|
|
|
1181
1229
|
if (!data) continue;
|
|
1182
1230
|
try {
|
|
1183
1231
|
const parsed = JSON.parse(data);
|
|
1232
|
+
if (parsed.usage) {
|
|
1233
|
+
streamUsage = parsed.usage;
|
|
1234
|
+
}
|
|
1184
1235
|
const delta = parsed.choices?.[0]?.delta;
|
|
1185
1236
|
if (delta?.content) {
|
|
1186
1237
|
fullContent += delta.content;
|
|
@@ -1206,10 +1257,15 @@ var handleXAIStream = async (response, ctx) => {
|
|
|
1206
1257
|
if (toolCalls.length > 0) {
|
|
1207
1258
|
msg.tool_calls = toolCalls;
|
|
1208
1259
|
}
|
|
1260
|
+
const usage = addUsage(ctx.usage, streamUsage?.prompt_tokens || 0, streamUsage?.completion_tokens || 0, streamUsage?.total_tokens || 0);
|
|
1261
|
+
if (ctx.stream && streamUsage) {
|
|
1262
|
+
ctx.stream({ type: "usage", usage });
|
|
1263
|
+
}
|
|
1209
1264
|
return {
|
|
1210
1265
|
...ctx,
|
|
1211
1266
|
lastResponse: msg,
|
|
1212
|
-
history: [...ctx.history, msg]
|
|
1267
|
+
history: [...ctx.history, msg],
|
|
1268
|
+
usage
|
|
1213
1269
|
};
|
|
1214
1270
|
};
|
|
1215
1271
|
|
|
@@ -1242,7 +1298,8 @@ var callLocal = async (config, ctx) => {
|
|
|
1242
1298
|
const body = {
|
|
1243
1299
|
model: model2,
|
|
1244
1300
|
messages,
|
|
1245
|
-
stream: !!ctx.stream
|
|
1301
|
+
stream: !!ctx.stream,
|
|
1302
|
+
...ctx.stream && { stream_options: { include_usage: true } }
|
|
1246
1303
|
};
|
|
1247
1304
|
if (schema) {
|
|
1248
1305
|
body.response_format = {
|
|
@@ -1290,7 +1347,8 @@ var callLocal = async (config, ctx) => {
|
|
|
1290
1347
|
return {
|
|
1291
1348
|
...ctx,
|
|
1292
1349
|
lastResponse: msg,
|
|
1293
|
-
history: [...ctx.history, msg]
|
|
1350
|
+
history: [...ctx.history, msg],
|
|
1351
|
+
usage: addUsage(ctx.usage, data.usage?.prompt_tokens || 0, data.usage?.completion_tokens || 0, data.usage?.total_tokens || 0)
|
|
1294
1352
|
};
|
|
1295
1353
|
};
|
|
1296
1354
|
var handleLocalStream = async (response, ctx) => {
|
|
@@ -1299,6 +1357,7 @@ var handleLocalStream = async (response, ctx) => {
|
|
|
1299
1357
|
let fullContent = "";
|
|
1300
1358
|
let toolCalls = [];
|
|
1301
1359
|
let buffer = "";
|
|
1360
|
+
let streamUsage = null;
|
|
1302
1361
|
try {
|
|
1303
1362
|
while (true) {
|
|
1304
1363
|
if (ctx.abortSignal?.aborted) {
|
|
@@ -1316,6 +1375,9 @@ var handleLocalStream = async (response, ctx) => {
|
|
|
1316
1375
|
if (!data) continue;
|
|
1317
1376
|
try {
|
|
1318
1377
|
const parsed = JSON.parse(data);
|
|
1378
|
+
if (parsed.usage) {
|
|
1379
|
+
streamUsage = parsed.usage;
|
|
1380
|
+
}
|
|
1319
1381
|
const delta = parsed.choices?.[0]?.delta;
|
|
1320
1382
|
if (delta?.content) {
|
|
1321
1383
|
fullContent += delta.content;
|
|
@@ -1341,10 +1403,15 @@ var handleLocalStream = async (response, ctx) => {
|
|
|
1341
1403
|
if (toolCalls.length > 0) {
|
|
1342
1404
|
msg.tool_calls = toolCalls;
|
|
1343
1405
|
}
|
|
1406
|
+
const usage = addUsage(ctx.usage, streamUsage?.prompt_tokens || 0, streamUsage?.completion_tokens || 0, streamUsage?.total_tokens || 0);
|
|
1407
|
+
if (ctx.stream && streamUsage) {
|
|
1408
|
+
ctx.stream({ type: "usage", usage });
|
|
1409
|
+
}
|
|
1344
1410
|
return {
|
|
1345
1411
|
...ctx,
|
|
1346
1412
|
lastResponse: msg,
|
|
1347
|
-
history: [...ctx.history, msg]
|
|
1413
|
+
history: [...ctx.history, msg],
|
|
1414
|
+
usage
|
|
1348
1415
|
};
|
|
1349
1416
|
};
|
|
1350
1417
|
|
|
@@ -1861,6 +1928,7 @@ var scopeContext = (config, ctx) => {
|
|
|
1861
1928
|
}
|
|
1862
1929
|
scopedCtx.stream = ctx.stream;
|
|
1863
1930
|
scopedCtx.abortSignal = ctx.abortSignal;
|
|
1931
|
+
scopedCtx.usage = ctx.usage;
|
|
1864
1932
|
if (config.tools) {
|
|
1865
1933
|
const toolDefinitions = config.tools.map(toolConfigToToolDefinition);
|
|
1866
1934
|
const toolExecutors = config.tools.reduce(
|
|
@@ -1914,7 +1982,8 @@ var scope = (config, ...steps) => {
|
|
|
1914
1982
|
history: config.silent ? ctx.history : scopedCtx.history,
|
|
1915
1983
|
lastResponse: config.silent ? ctx.lastResponse : scopedCtx.lastResponse,
|
|
1916
1984
|
lastRequest: config.silent ? ctx.lastRequest : scopedCtx.lastRequest,
|
|
1917
|
-
stopReason: config.silent ? ctx.stopReason : scopedCtx.stopReason
|
|
1985
|
+
stopReason: config.silent ? ctx.stopReason : scopedCtx.stopReason,
|
|
1986
|
+
usage: scopedCtx.usage
|
|
1918
1987
|
};
|
|
1919
1988
|
};
|
|
1920
1989
|
};
|
|
@@ -1976,6 +2045,7 @@ var rateLimited = (config) => (fn) => {
|
|
|
1976
2045
|
IMAGE_EDIT_MODEL_SCHEMA,
|
|
1977
2046
|
IMAGE_MODEL_SCHEMA,
|
|
1978
2047
|
Inherit,
|
|
2048
|
+
addUsage,
|
|
1979
2049
|
appendToLastRequest,
|
|
1980
2050
|
compose,
|
|
1981
2051
|
convertMCPSchemaToToolSchema,
|