190proof 1.0.101 → 1.0.104
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/README.md +5 -0
- package/dist/index.d.mts +13 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +86 -41
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +86 -41
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -110,8 +110,13 @@ function isHeicImage(name, mime) {
|
|
|
110
110
|
// index.ts
|
|
111
111
|
var sharp = __require("sharp");
|
|
112
112
|
var decode = __require("heic-decode");
|
|
113
|
+
function anySignal(signals) {
|
|
114
|
+
return AbortSignal.any(
|
|
115
|
+
signals
|
|
116
|
+
);
|
|
117
|
+
}
|
|
113
118
|
async function withRetries(identifier, apiName, fn, options = {}) {
|
|
114
|
-
var _a, _b, _c, _d;
|
|
119
|
+
var _a, _b, _c, _d, _e;
|
|
115
120
|
const { retries = 5, baseDelayMs = 125, onError } = options;
|
|
116
121
|
logger_default.log(identifier, `Calling ${apiName} API with retries`);
|
|
117
122
|
let lastError;
|
|
@@ -120,19 +125,21 @@ async function withRetries(identifier, apiName, fn, options = {}) {
|
|
|
120
125
|
return await fn();
|
|
121
126
|
} catch (error3) {
|
|
122
127
|
lastError = error3;
|
|
128
|
+
if ((_a = options.signal) == null ? void 0 : _a.aborted)
|
|
129
|
+
throw error3;
|
|
123
130
|
if (onError) {
|
|
124
131
|
onError(error3, attempt);
|
|
125
132
|
} else {
|
|
126
133
|
logger_default.error(
|
|
127
134
|
identifier,
|
|
128
135
|
`Retry #${attempt} error: ${error3.message}`,
|
|
129
|
-
((
|
|
136
|
+
((_b = error3.response) == null ? void 0 : _b.data) || error3
|
|
130
137
|
);
|
|
131
138
|
}
|
|
132
139
|
await timeout(baseDelayMs * attempt);
|
|
133
140
|
}
|
|
134
141
|
}
|
|
135
|
-
const detail = ((
|
|
142
|
+
const detail = ((_e = (_d = (_c = lastError == null ? void 0 : lastError.response) == null ? void 0 : _c.data) == null ? void 0 : _d.error) == null ? void 0 : _e.message) || (lastError == null ? void 0 : lastError.message) || String(lastError);
|
|
136
143
|
const error2 = new Error(
|
|
137
144
|
`Failed to call ${apiName} API after ${retries} attempts: ${detail}`
|
|
138
145
|
);
|
|
@@ -282,6 +289,14 @@ function buildOpenAIRequestConfig(identifier, model, config) {
|
|
|
282
289
|
}
|
|
283
290
|
};
|
|
284
291
|
}
|
|
292
|
+
function filterOpenAICompatReasoningDetails(details) {
|
|
293
|
+
if (!Array.isArray(details))
|
|
294
|
+
return details || void 0;
|
|
295
|
+
const blocks = details.filter(
|
|
296
|
+
(block) => typeof (block == null ? void 0 : block.type) === "string" && block.type.startsWith("reasoning.")
|
|
297
|
+
);
|
|
298
|
+
return blocks.length ? blocks : void 0;
|
|
299
|
+
}
|
|
285
300
|
async function prepareOpenAIPayload(identifier, payload) {
|
|
286
301
|
var _a, _b;
|
|
287
302
|
const preparedPayload = {
|
|
@@ -349,13 +364,16 @@ async function prepareOpenAIPayload(identifier, payload) {
|
|
|
349
364
|
}
|
|
350
365
|
if (message.reasoning)
|
|
351
366
|
outMessage.reasoning = message.reasoning;
|
|
352
|
-
|
|
353
|
-
|
|
367
|
+
const reasoningDetails = filterOpenAICompatReasoningDetails(
|
|
368
|
+
message.reasoningDetails
|
|
369
|
+
);
|
|
370
|
+
if (reasoningDetails)
|
|
371
|
+
outMessage.reasoning_details = reasoningDetails;
|
|
354
372
|
preparedPayload.messages.push(outMessage);
|
|
355
373
|
}
|
|
356
374
|
return preparedPayload;
|
|
357
375
|
}
|
|
358
|
-
async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs, requestTimeoutMs = 12e4) {
|
|
376
|
+
async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs, requestTimeoutMs = 12e4, signal) {
|
|
359
377
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
360
378
|
const functionNames = openAiPayload.tools ? new Set(openAiPayload.tools.map((fn) => fn.function.name)) : null;
|
|
361
379
|
const { endpoint, headers } = buildOpenAIRequestConfig(
|
|
@@ -375,7 +393,10 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs,
|
|
|
375
393
|
method: "POST",
|
|
376
394
|
headers,
|
|
377
395
|
body: JSON.stringify({ ...openAiPayload, stream: true }),
|
|
378
|
-
|
|
396
|
+
// Merge (don't overwrite) the internal timeout controller with the caller's
|
|
397
|
+
// cancellation signal so both an internal timeout and an external abort stop
|
|
398
|
+
// the stream.
|
|
399
|
+
signal: signal ? anySignal([controller.signal, signal]) : controller.signal
|
|
379
400
|
});
|
|
380
401
|
if (!response.body) {
|
|
381
402
|
throw new Error("Stream error: no response body");
|
|
@@ -465,7 +486,7 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs,
|
|
|
465
486
|
}
|
|
466
487
|
}
|
|
467
488
|
}
|
|
468
|
-
async function callOpenAI(id, openAiPayload, openAiConfig, requestTimeoutMs = 12e4) {
|
|
489
|
+
async function callOpenAI(id, openAiPayload, openAiConfig, requestTimeoutMs = 12e4, signal) {
|
|
469
490
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
470
491
|
const { endpoint, headers } = buildOpenAIRequestConfig(
|
|
471
492
|
id,
|
|
@@ -480,7 +501,8 @@ async function callOpenAI(id, openAiPayload, openAiConfig, requestTimeoutMs = 12
|
|
|
480
501
|
method: "POST",
|
|
481
502
|
headers,
|
|
482
503
|
body: JSON.stringify({ ...openAiPayload, stream: false }),
|
|
483
|
-
|
|
504
|
+
// Merge the internal timeout controller with the caller's cancellation signal.
|
|
505
|
+
signal: signal ? anySignal([controller.signal, signal]) : controller.signal
|
|
484
506
|
});
|
|
485
507
|
if (!response.ok) {
|
|
486
508
|
const errorData = await response.json();
|
|
@@ -543,7 +565,7 @@ async function callOpenAI(id, openAiPayload, openAiConfig, requestTimeoutMs = 12
|
|
|
543
565
|
} : null
|
|
544
566
|
};
|
|
545
567
|
}
|
|
546
|
-
async function callOpenAiWithRetries(id, openAiPayload, openAiConfig, retries = 5, chunkTimeoutMs = 15e3, requestTimeoutMs = 12e4) {
|
|
568
|
+
async function callOpenAiWithRetries(id, openAiPayload, openAiConfig, retries = 5, chunkTimeoutMs = 15e3, requestTimeoutMs = 12e4, signal) {
|
|
547
569
|
logger_default.log(
|
|
548
570
|
id,
|
|
549
571
|
"Calling OpenAI API with retries:",
|
|
@@ -562,14 +584,16 @@ async function callOpenAiWithRetries(id, openAiPayload, openAiConfig, retries =
|
|
|
562
584
|
openAiPayload,
|
|
563
585
|
openAiConfig,
|
|
564
586
|
chunkTimeoutMs,
|
|
565
|
-
requestTimeoutMs
|
|
587
|
+
requestTimeoutMs,
|
|
588
|
+
signal
|
|
566
589
|
);
|
|
567
590
|
} else {
|
|
568
|
-
return callOpenAI(id, openAiPayload, openAiConfig, requestTimeoutMs);
|
|
591
|
+
return callOpenAI(id, openAiPayload, openAiConfig, requestTimeoutMs, signal);
|
|
569
592
|
}
|
|
570
593
|
},
|
|
571
594
|
{
|
|
572
595
|
retries,
|
|
596
|
+
signal,
|
|
573
597
|
baseDelayMs: 250,
|
|
574
598
|
onError: (error2, attempt) => {
|
|
575
599
|
var _a, _b;
|
|
@@ -684,7 +708,9 @@ async function prepareAnthropicPayload(_identifier, payload) {
|
|
|
684
708
|
});
|
|
685
709
|
}
|
|
686
710
|
}
|
|
687
|
-
const leadingBlocks = message.role === "assistant" && Array.isArray(message.reasoningDetails) ? message.reasoningDetails
|
|
711
|
+
const leadingBlocks = message.role === "assistant" && Array.isArray(message.reasoningDetails) ? message.reasoningDetails.filter(
|
|
712
|
+
(block) => (block == null ? void 0 : block.type) === "thinking" || (block == null ? void 0 : block.type) === "redacted_thinking"
|
|
713
|
+
) : [];
|
|
688
714
|
const toolUseBlocks = (message.functionCalls || []).map((fc, i) => {
|
|
689
715
|
var _a;
|
|
690
716
|
return {
|
|
@@ -701,7 +727,7 @@ async function prepareAnthropicPayload(_identifier, payload) {
|
|
|
701
727
|
}
|
|
702
728
|
return preparedPayload;
|
|
703
729
|
}
|
|
704
|
-
async function callAnthropic(id, payload, config, requestTimeoutMs = 12e4) {
|
|
730
|
+
async function callAnthropic(id, payload, config, requestTimeoutMs = 12e4, signal) {
|
|
705
731
|
var _a, _b, _c;
|
|
706
732
|
const anthropicMessages = jigAnthropicMessages(payload.messages);
|
|
707
733
|
const tools = (_a = payload.functions) == null ? void 0 : _a.map((f) => ({
|
|
@@ -725,7 +751,8 @@ async function callAnthropic(id, payload, config, requestTimeoutMs = 12e4) {
|
|
|
725
751
|
contentType: "application/json",
|
|
726
752
|
body: JSON.stringify(bedrockPayload),
|
|
727
753
|
modelId: MODEL_ID
|
|
728
|
-
})
|
|
754
|
+
}),
|
|
755
|
+
{ abortSignal: signal }
|
|
729
756
|
);
|
|
730
757
|
const decodedResponseBody = new TextDecoder().decode(response.body);
|
|
731
758
|
data = JSON.parse(decodedResponseBody);
|
|
@@ -751,7 +778,8 @@ async function callAnthropic(id, payload, config, requestTimeoutMs = 12e4) {
|
|
|
751
778
|
"anthropic-version": "2023-06-01",
|
|
752
779
|
"anthropic-beta": "tools-2024-04-04"
|
|
753
780
|
},
|
|
754
|
-
timeout: requestTimeoutMs
|
|
781
|
+
timeout: requestTimeoutMs,
|
|
782
|
+
signal
|
|
755
783
|
}
|
|
756
784
|
);
|
|
757
785
|
data = response.data;
|
|
@@ -821,13 +849,14 @@ ${text}` : text;
|
|
|
821
849
|
usage
|
|
822
850
|
};
|
|
823
851
|
}
|
|
824
|
-
async function callAnthropicWithRetries(id, payload, config, retries = 5, requestTimeoutMs = 12e4) {
|
|
852
|
+
async function callAnthropicWithRetries(id, payload, config, retries = 5, requestTimeoutMs = 12e4, signal) {
|
|
825
853
|
return withRetries(
|
|
826
854
|
id,
|
|
827
855
|
"Anthropic",
|
|
828
|
-
() => callAnthropic(id, payload, config, requestTimeoutMs),
|
|
856
|
+
() => callAnthropic(id, payload, config, requestTimeoutMs, signal),
|
|
829
857
|
{
|
|
830
|
-
retries
|
|
858
|
+
retries,
|
|
859
|
+
signal
|
|
831
860
|
}
|
|
832
861
|
);
|
|
833
862
|
}
|
|
@@ -945,7 +974,7 @@ async function prepareGoogleAIPayload(_identifier, payload) {
|
|
|
945
974
|
}
|
|
946
975
|
return preparedPayload;
|
|
947
976
|
}
|
|
948
|
-
async function callGoogleAI(id, payload, requestTimeoutMs = 12e4) {
|
|
977
|
+
async function callGoogleAI(id, payload, requestTimeoutMs = 12e4, signal) {
|
|
949
978
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
|
|
950
979
|
const contents = jigGoogleMessages(payload.messages);
|
|
951
980
|
const requestBody = {
|
|
@@ -969,7 +998,8 @@ async function callGoogleAI(id, payload, requestTimeoutMs = 12e4) {
|
|
|
969
998
|
"content-type": "application/json",
|
|
970
999
|
"x-goog-api-key": process.env.GEMINI_API_KEY
|
|
971
1000
|
},
|
|
972
|
-
timeout: requestTimeoutMs
|
|
1001
|
+
timeout: requestTimeoutMs,
|
|
1002
|
+
signal
|
|
973
1003
|
}
|
|
974
1004
|
);
|
|
975
1005
|
response = httpResponse.data;
|
|
@@ -1068,10 +1098,11 @@ function removeImagesFromGooglePayload(payload) {
|
|
|
1068
1098
|
}
|
|
1069
1099
|
return removedImages;
|
|
1070
1100
|
}
|
|
1071
|
-
async function callGoogleAIWithRetries(id, payload, retries = 5, requestTimeoutMs = 12e4) {
|
|
1101
|
+
async function callGoogleAIWithRetries(id, payload, retries = 5, requestTimeoutMs = 12e4, signal) {
|
|
1072
1102
|
let hasTriedWithoutImages = false;
|
|
1073
|
-
return withRetries(id, "Google AI", () => callGoogleAI(id, payload, requestTimeoutMs), {
|
|
1103
|
+
return withRetries(id, "Google AI", () => callGoogleAI(id, payload, requestTimeoutMs, signal), {
|
|
1074
1104
|
retries,
|
|
1105
|
+
signal,
|
|
1075
1106
|
onError: (error2, attempt) => {
|
|
1076
1107
|
var _a, _b;
|
|
1077
1108
|
const errorDetails = {
|
|
@@ -1178,8 +1209,11 @@ function prepareOpenAICompatMessages(messages) {
|
|
|
1178
1209
|
}
|
|
1179
1210
|
if (message.reasoning)
|
|
1180
1211
|
outMessage.reasoning = message.reasoning;
|
|
1181
|
-
|
|
1182
|
-
|
|
1212
|
+
const reasoningDetails = filterOpenAICompatReasoningDetails(
|
|
1213
|
+
message.reasoningDetails
|
|
1214
|
+
);
|
|
1215
|
+
if (reasoningDetails)
|
|
1216
|
+
outMessage.reasoning_details = reasoningDetails;
|
|
1183
1217
|
out.push(outMessage);
|
|
1184
1218
|
}
|
|
1185
1219
|
return out;
|
|
@@ -1197,7 +1231,7 @@ function prepareGroqPayload(payload) {
|
|
|
1197
1231
|
temperature: payload.temperature
|
|
1198
1232
|
};
|
|
1199
1233
|
}
|
|
1200
|
-
async function callGroq(id, payload, requestTimeoutMs = 12e4) {
|
|
1234
|
+
async function callGroq(id, payload, requestTimeoutMs = 12e4, signal) {
|
|
1201
1235
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
1202
1236
|
const response = await axios.post(
|
|
1203
1237
|
"https://api.groq.com/openai/v1/chat/completions",
|
|
@@ -1207,7 +1241,8 @@ async function callGroq(id, payload, requestTimeoutMs = 12e4) {
|
|
|
1207
1241
|
"content-type": "application/json",
|
|
1208
1242
|
Authorization: `Bearer ${process.env.GROQ_API_KEY}`
|
|
1209
1243
|
},
|
|
1210
|
-
timeout: requestTimeoutMs
|
|
1244
|
+
timeout: requestTimeoutMs,
|
|
1245
|
+
signal
|
|
1211
1246
|
}
|
|
1212
1247
|
);
|
|
1213
1248
|
if (response.data.error) {
|
|
@@ -1255,9 +1290,10 @@ async function callGroq(id, payload, requestTimeoutMs = 12e4) {
|
|
|
1255
1290
|
} : null
|
|
1256
1291
|
};
|
|
1257
1292
|
}
|
|
1258
|
-
async function callGroqWithRetries(id, payload, retries = 5, requestTimeoutMs = 12e4) {
|
|
1259
|
-
return withRetries(id, "Groq", () => callGroq(id, payload, requestTimeoutMs), {
|
|
1260
|
-
retries
|
|
1293
|
+
async function callGroqWithRetries(id, payload, retries = 5, requestTimeoutMs = 12e4, signal) {
|
|
1294
|
+
return withRetries(id, "Groq", () => callGroq(id, payload, requestTimeoutMs, signal), {
|
|
1295
|
+
retries,
|
|
1296
|
+
signal
|
|
1261
1297
|
});
|
|
1262
1298
|
}
|
|
1263
1299
|
function prepareOpenRouterPayload(payload) {
|
|
@@ -1322,7 +1358,7 @@ function parseDsmlToolCalls(content) {
|
|
|
1322
1358
|
const remaining = first === -1 ? content : (content.slice(0, first) + content.slice(last)).trim();
|
|
1323
1359
|
return { calls, remainingContent: remaining.length ? remaining : null };
|
|
1324
1360
|
}
|
|
1325
|
-
async function callOpenRouter(id, payload, requestTimeoutMs = 12e4) {
|
|
1361
|
+
async function callOpenRouter(id, payload, requestTimeoutMs = 12e4, signal) {
|
|
1326
1362
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
1327
1363
|
const response = await axios.post(
|
|
1328
1364
|
"https://openrouter.ai/api/v1/chat/completions",
|
|
@@ -1332,7 +1368,8 @@ async function callOpenRouter(id, payload, requestTimeoutMs = 12e4) {
|
|
|
1332
1368
|
"content-type": "application/json",
|
|
1333
1369
|
Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`
|
|
1334
1370
|
},
|
|
1335
|
-
timeout: requestTimeoutMs
|
|
1371
|
+
timeout: requestTimeoutMs,
|
|
1372
|
+
signal
|
|
1336
1373
|
}
|
|
1337
1374
|
);
|
|
1338
1375
|
if (response.data.error) {
|
|
@@ -1390,12 +1427,12 @@ async function callOpenRouter(id, payload, requestTimeoutMs = 12e4) {
|
|
|
1390
1427
|
} : null
|
|
1391
1428
|
};
|
|
1392
1429
|
}
|
|
1393
|
-
async function callOpenRouterWithRetries(id, payload, retries = 5, requestTimeoutMs = 12e4) {
|
|
1430
|
+
async function callOpenRouterWithRetries(id, payload, retries = 5, requestTimeoutMs = 12e4, signal) {
|
|
1394
1431
|
return withRetries(
|
|
1395
1432
|
id,
|
|
1396
1433
|
"OpenRouter",
|
|
1397
|
-
() => callOpenRouter(id, payload, requestTimeoutMs),
|
|
1398
|
-
{ retries }
|
|
1434
|
+
() => callOpenRouter(id, payload, requestTimeoutMs, signal),
|
|
1435
|
+
{ retries, signal }
|
|
1399
1436
|
);
|
|
1400
1437
|
}
|
|
1401
1438
|
var VALID_PROVIDERS = ["openai", "anthropic", "google", "groq", "openrouter"];
|
|
@@ -1436,11 +1473,12 @@ function parseModelString(model) {
|
|
|
1436
1473
|
);
|
|
1437
1474
|
}
|
|
1438
1475
|
async function callWithRetries(id, aiPayload, aiConfig, retries = 5, chunkTimeoutMs = 15e3) {
|
|
1439
|
-
var _a;
|
|
1476
|
+
var _a, _b;
|
|
1440
1477
|
try {
|
|
1441
1478
|
const { provider, modelId } = parseModelString(aiPayload.model);
|
|
1442
1479
|
const routingPayload = { ...aiPayload, model: modelId };
|
|
1443
1480
|
const requestTimeoutMs = (_a = aiPayload.requestTimeoutMs) != null ? _a : 12e4;
|
|
1481
|
+
const signal = aiPayload.signal;
|
|
1444
1482
|
switch (provider) {
|
|
1445
1483
|
case "anthropic":
|
|
1446
1484
|
return await callAnthropicWithRetries(
|
|
@@ -1448,7 +1486,8 @@ async function callWithRetries(id, aiPayload, aiConfig, retries = 5, chunkTimeou
|
|
|
1448
1486
|
await prepareAnthropicPayload(id, routingPayload),
|
|
1449
1487
|
aiConfig,
|
|
1450
1488
|
retries,
|
|
1451
|
-
requestTimeoutMs
|
|
1489
|
+
requestTimeoutMs,
|
|
1490
|
+
signal
|
|
1452
1491
|
);
|
|
1453
1492
|
case "openai":
|
|
1454
1493
|
return await callOpenAiWithRetries(
|
|
@@ -1457,31 +1496,37 @@ async function callWithRetries(id, aiPayload, aiConfig, retries = 5, chunkTimeou
|
|
|
1457
1496
|
aiConfig,
|
|
1458
1497
|
retries,
|
|
1459
1498
|
chunkTimeoutMs,
|
|
1460
|
-
requestTimeoutMs
|
|
1499
|
+
requestTimeoutMs,
|
|
1500
|
+
signal
|
|
1461
1501
|
);
|
|
1462
1502
|
case "groq":
|
|
1463
1503
|
return await callGroqWithRetries(
|
|
1464
1504
|
id,
|
|
1465
1505
|
prepareGroqPayload(routingPayload),
|
|
1466
1506
|
retries,
|
|
1467
|
-
requestTimeoutMs
|
|
1507
|
+
requestTimeoutMs,
|
|
1508
|
+
signal
|
|
1468
1509
|
);
|
|
1469
1510
|
case "google":
|
|
1470
1511
|
return await callGoogleAIWithRetries(
|
|
1471
1512
|
id,
|
|
1472
1513
|
await prepareGoogleAIPayload(id, routingPayload),
|
|
1473
1514
|
retries,
|
|
1474
|
-
requestTimeoutMs
|
|
1515
|
+
requestTimeoutMs,
|
|
1516
|
+
signal
|
|
1475
1517
|
);
|
|
1476
1518
|
case "openrouter":
|
|
1477
1519
|
return await callOpenRouterWithRetries(
|
|
1478
1520
|
id,
|
|
1479
1521
|
prepareOpenRouterPayload(routingPayload),
|
|
1480
1522
|
retries,
|
|
1481
|
-
requestTimeoutMs
|
|
1523
|
+
requestTimeoutMs,
|
|
1524
|
+
signal
|
|
1482
1525
|
);
|
|
1483
1526
|
}
|
|
1484
1527
|
} catch (error2) {
|
|
1528
|
+
if ((_b = aiPayload.signal) == null ? void 0 : _b.aborted)
|
|
1529
|
+
throw error2;
|
|
1485
1530
|
if (aiPayload.fallbackModel) {
|
|
1486
1531
|
logger_default.error(
|
|
1487
1532
|
id,
|