190proof 1.0.109 → 1.0.111
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 +3 -0
- package/dist/index.d.mts +20 -1
- package/dist/index.d.ts +20 -1
- package/dist/index.js +212 -111
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +210 -109
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
2
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
3
|
}) : x)(function(x) {
|
|
4
|
-
if (typeof require !== "undefined")
|
|
4
|
+
if (typeof require !== "undefined")
|
|
5
|
+
return require.apply(this, arguments);
|
|
5
6
|
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
7
|
});
|
|
7
8
|
|
|
@@ -137,7 +138,8 @@ async function withRetries(identifier, apiName, fn, options = {}) {
|
|
|
137
138
|
return await fn();
|
|
138
139
|
} catch (error3) {
|
|
139
140
|
lastError = error3;
|
|
140
|
-
if ((_a = options.signal) == null ? void 0 : _a.aborted)
|
|
141
|
+
if ((_a = options.signal) == null ? void 0 : _a.aborted)
|
|
142
|
+
throw error3;
|
|
141
143
|
if (onError) {
|
|
142
144
|
onError(error3, attempt);
|
|
143
145
|
} else {
|
|
@@ -161,7 +163,8 @@ function parseStreamedResponse(identifier, paragraph, toolCallAccumulators, allo
|
|
|
161
163
|
const functionCalls = [];
|
|
162
164
|
for (let i = 0; i < toolCallAccumulators.length; i++) {
|
|
163
165
|
const acc = toolCallAccumulators[i];
|
|
164
|
-
if (!acc.name || !acc.arguments)
|
|
166
|
+
if (!acc.name || !acc.arguments)
|
|
167
|
+
continue;
|
|
165
168
|
if (allowedFunctionNames && !allowedFunctionNames.has(acc.name)) {
|
|
166
169
|
throw new Error(
|
|
167
170
|
`Stream error: received function call with unknown name: ${acc.name}`
|
|
@@ -300,7 +303,8 @@ function buildOpenAIRequestConfig(identifier, model, config) {
|
|
|
300
303
|
};
|
|
301
304
|
}
|
|
302
305
|
function filterOpenAICompatReasoningDetails(details) {
|
|
303
|
-
if (!Array.isArray(details))
|
|
306
|
+
if (!Array.isArray(details))
|
|
307
|
+
return details || void 0;
|
|
304
308
|
const blocks = details.filter(
|
|
305
309
|
(block) => typeof (block == null ? void 0 : block.type) === "string" && block.type.startsWith("reasoning.")
|
|
306
310
|
);
|
|
@@ -371,11 +375,13 @@ async function prepareOpenAIPayload(identifier, payload) {
|
|
|
371
375
|
};
|
|
372
376
|
});
|
|
373
377
|
}
|
|
374
|
-
if (message.reasoning)
|
|
378
|
+
if (message.reasoning)
|
|
379
|
+
outMessage.reasoning = message.reasoning;
|
|
375
380
|
const reasoningDetails = filterOpenAICompatReasoningDetails(
|
|
376
381
|
message.reasoningDetails
|
|
377
382
|
);
|
|
378
|
-
if (reasoningDetails)
|
|
383
|
+
if (reasoningDetails)
|
|
384
|
+
outMessage.reasoning_details = reasoningDetails;
|
|
379
385
|
preparedPayload.messages.push(outMessage);
|
|
380
386
|
}
|
|
381
387
|
return preparedPayload;
|
|
@@ -437,7 +443,8 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs,
|
|
|
437
443
|
}
|
|
438
444
|
const jsonStrings = chunk.split(/^data: /gm);
|
|
439
445
|
for (const jsonString of jsonStrings) {
|
|
440
|
-
if (!jsonString)
|
|
446
|
+
if (!jsonString)
|
|
447
|
+
continue;
|
|
441
448
|
if (jsonString.includes("[DONE]")) {
|
|
442
449
|
clearTimeout(overallTimeout);
|
|
443
450
|
return parseStreamedResponse(
|
|
@@ -475,7 +482,8 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs,
|
|
|
475
482
|
while (toolCallAccumulators.length <= idx) {
|
|
476
483
|
toolCallAccumulators.push({ name: "", arguments: "" });
|
|
477
484
|
}
|
|
478
|
-
if (toolCall.id)
|
|
485
|
+
if (toolCall.id)
|
|
486
|
+
toolCallAccumulators[idx].id = toolCall.id;
|
|
479
487
|
if ((_e = toolCall.function) == null ? void 0 : _e.name)
|
|
480
488
|
toolCallAccumulators[idx].name += toolCall.function.name;
|
|
481
489
|
if ((_f = toolCall.function) == null ? void 0 : _f.arguments)
|
|
@@ -483,9 +491,11 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs,
|
|
|
483
491
|
}
|
|
484
492
|
}
|
|
485
493
|
const text = (_h = (_g = json.choices[0]) == null ? void 0 : _g.delta) == null ? void 0 : _h.content;
|
|
486
|
-
if (text)
|
|
494
|
+
if (text)
|
|
495
|
+
paragraph += text;
|
|
487
496
|
const reasoningDelta = (_j = (_i = json.choices[0]) == null ? void 0 : _i.delta) == null ? void 0 : _j.reasoning;
|
|
488
|
-
if (reasoningDelta)
|
|
497
|
+
if (reasoningDelta)
|
|
498
|
+
reasoning += reasoningDelta;
|
|
489
499
|
}
|
|
490
500
|
}
|
|
491
501
|
}
|
|
@@ -630,7 +640,8 @@ function jigAnthropicMessages(messages) {
|
|
|
630
640
|
];
|
|
631
641
|
}
|
|
632
642
|
jiggedMessages = jiggedMessages.reduce((acc, message) => {
|
|
633
|
-
if (acc.length === 0)
|
|
643
|
+
if (acc.length === 0)
|
|
644
|
+
return [message];
|
|
634
645
|
const lastMessage = acc[acc.length - 1];
|
|
635
646
|
if (lastMessage.role === message.role) {
|
|
636
647
|
const lastContent = Array.isArray(lastMessage.content) ? lastMessage.content : [{ type: "text", text: lastMessage.content }];
|
|
@@ -884,7 +895,8 @@ function jigGoogleMessages(messages) {
|
|
|
884
895
|
];
|
|
885
896
|
}
|
|
886
897
|
jiggedMessages = jiggedMessages.reduce((acc, message) => {
|
|
887
|
-
if (acc.length === 0)
|
|
898
|
+
if (acc.length === 0)
|
|
899
|
+
return [message];
|
|
888
900
|
const lastMessage = acc[acc.length - 1];
|
|
889
901
|
if (lastMessage.role === message.role) {
|
|
890
902
|
lastMessage.parts = [...lastMessage.parts, ...message.parts];
|
|
@@ -922,7 +934,8 @@ async function prepareGoogleAIPayload(_identifier, payload) {
|
|
|
922
934
|
const toolNameById = /* @__PURE__ */ new Map();
|
|
923
935
|
for (const m of payload.messages) {
|
|
924
936
|
for (const fc of m.functionCalls || []) {
|
|
925
|
-
if (fc.id)
|
|
937
|
+
if (fc.id)
|
|
938
|
+
toolNameById.set(fc.id, fc.name);
|
|
926
939
|
}
|
|
927
940
|
}
|
|
928
941
|
for (const message of payload.messages) {
|
|
@@ -999,7 +1012,8 @@ async function callGoogleAI(id, payload, requestTimeoutMs = 12e4, signal) {
|
|
|
999
1012
|
contents,
|
|
1000
1013
|
generationConfig: { responseModalities: ["TEXT"] }
|
|
1001
1014
|
};
|
|
1002
|
-
if (payload.tools)
|
|
1015
|
+
if (payload.tools)
|
|
1016
|
+
requestBody.tools = [payload.tools];
|
|
1003
1017
|
if (payload.tools && payload.toolConfig) {
|
|
1004
1018
|
requestBody.toolConfig = payload.toolConfig;
|
|
1005
1019
|
}
|
|
@@ -1057,7 +1071,8 @@ async function callGoogleAI(id, payload, requestTimeoutMs = 12e4, signal) {
|
|
|
1057
1071
|
});
|
|
1058
1072
|
continue;
|
|
1059
1073
|
}
|
|
1060
|
-
if (part.text)
|
|
1074
|
+
if (part.text)
|
|
1075
|
+
text += part.text;
|
|
1061
1076
|
if ((_m = part.inlineData) == null ? void 0 : _m.data) {
|
|
1062
1077
|
files.push({ mimeType: "image/png", data: part.inlineData.data });
|
|
1063
1078
|
}
|
|
@@ -1134,15 +1149,21 @@ async function callGoogleAIWithRetries(id, payload, retries = 5, requestTimeoutM
|
|
|
1134
1149
|
finishReason: error2.finishReason,
|
|
1135
1150
|
modelVersion: error2.modelVersion
|
|
1136
1151
|
};
|
|
1137
|
-
if (error2.safetyRatings)
|
|
1138
|
-
|
|
1152
|
+
if (error2.safetyRatings)
|
|
1153
|
+
errorDetails.safetyRatings = error2.safetyRatings;
|
|
1154
|
+
if (error2.usageMetadata)
|
|
1155
|
+
errorDetails.usageMetadata = error2.usageMetadata;
|
|
1139
1156
|
if (error2.promptFeedback)
|
|
1140
1157
|
errorDetails.promptFeedback = error2.promptFeedback;
|
|
1141
|
-
if (error2.status)
|
|
1142
|
-
|
|
1143
|
-
if (error2.
|
|
1158
|
+
if (error2.status)
|
|
1159
|
+
errorDetails.httpStatus = error2.status;
|
|
1160
|
+
if (error2.code)
|
|
1161
|
+
errorDetails.errorCode = error2.code;
|
|
1162
|
+
if (error2.details)
|
|
1163
|
+
errorDetails.errorDetails = error2.details;
|
|
1144
1164
|
const fileUris = payload.messages.flatMap((m) => m.parts).filter((p) => "fileData" in p).map((p) => p.fileData.fileUri);
|
|
1145
|
-
if (fileUris.length)
|
|
1165
|
+
if (fileUris.length)
|
|
1166
|
+
errorDetails.fileUris = fileUris;
|
|
1146
1167
|
logger_default.error(
|
|
1147
1168
|
id,
|
|
1148
1169
|
`Retry #${attempt} error: ${error2.message}`,
|
|
@@ -1222,13 +1243,16 @@ function prepareOpenAICompatMessages(messages) {
|
|
|
1222
1243
|
}
|
|
1223
1244
|
};
|
|
1224
1245
|
});
|
|
1225
|
-
if (!message.content)
|
|
1246
|
+
if (!message.content)
|
|
1247
|
+
outMessage.content = null;
|
|
1226
1248
|
}
|
|
1227
|
-
if (message.reasoning)
|
|
1249
|
+
if (message.reasoning)
|
|
1250
|
+
outMessage.reasoning = message.reasoning;
|
|
1228
1251
|
const reasoningDetails = filterOpenAICompatReasoningDetails(
|
|
1229
1252
|
message.reasoningDetails
|
|
1230
1253
|
);
|
|
1231
|
-
if (reasoningDetails)
|
|
1254
|
+
if (reasoningDetails)
|
|
1255
|
+
outMessage.reasoning_details = reasoningDetails;
|
|
1232
1256
|
out.push(outMessage);
|
|
1233
1257
|
}
|
|
1234
1258
|
return out;
|
|
@@ -1371,7 +1395,8 @@ function parseDsmlToolCalls(content) {
|
|
|
1371
1395
|
let last = -1;
|
|
1372
1396
|
let tag;
|
|
1373
1397
|
while ((tag = DSML_ANY_TAG_RE.exec(content)) !== null) {
|
|
1374
|
-
if (first === -1)
|
|
1398
|
+
if (first === -1)
|
|
1399
|
+
first = tag.index;
|
|
1375
1400
|
last = tag.index + tag[0].length;
|
|
1376
1401
|
}
|
|
1377
1402
|
const remaining = first === -1 ? content : (content.slice(0, first) + content.slice(last)).trim();
|
|
@@ -1387,7 +1412,8 @@ function finalizeOpenRouterMessage(id, raw) {
|
|
|
1387
1412
|
const functionCalls = [];
|
|
1388
1413
|
for (let i = 0; i < raw.toolCalls.length; i++) {
|
|
1389
1414
|
const tc = raw.toolCalls[i];
|
|
1390
|
-
if (!tc.name)
|
|
1415
|
+
if (!tc.name)
|
|
1416
|
+
continue;
|
|
1391
1417
|
functionCalls.push({
|
|
1392
1418
|
id: (_a = tc.id) != null ? _a : `call_${i}`,
|
|
1393
1419
|
name: tc.name,
|
|
@@ -1438,7 +1464,8 @@ async function callOpenRouterStream(id, payload, streamTimeoutMs = OPENROUTER_ST
|
|
|
1438
1464
|
controller.abort();
|
|
1439
1465
|
};
|
|
1440
1466
|
const unref = (t) => {
|
|
1441
|
-
if (typeof t === "object" && "unref" in t)
|
|
1467
|
+
if (typeof t === "object" && "unref" in t)
|
|
1468
|
+
t.unref();
|
|
1442
1469
|
return t;
|
|
1443
1470
|
};
|
|
1444
1471
|
const totalTimer = unref(
|
|
@@ -1504,88 +1531,98 @@ async function callOpenRouterStream(id, payload, streamTimeoutMs = OPENROUTER_ST
|
|
|
1504
1531
|
const reader = response.body.getReader();
|
|
1505
1532
|
const decoder = new TextDecoder();
|
|
1506
1533
|
let lineBuffer = "";
|
|
1507
|
-
outer:
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
let
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
id,
|
|
1530
|
-
"OpenRouter stream: unparseable data line:",
|
|
1531
|
-
dataStr.slice(0, 200)
|
|
1532
|
-
);
|
|
1533
|
-
continue;
|
|
1534
|
-
}
|
|
1535
|
-
dataChunks++;
|
|
1536
|
-
if (json.error) {
|
|
1537
|
-
logger_default.error(id, "OpenRouter stream error event:", json.error);
|
|
1538
|
-
const error2 = new Error(
|
|
1539
|
-
`OpenRouter error: ${json.error.message}`
|
|
1540
|
-
);
|
|
1541
|
-
error2.data = json.error;
|
|
1542
|
-
throw error2;
|
|
1543
|
-
}
|
|
1544
|
-
if (json.provider) provider = json.provider;
|
|
1545
|
-
let useful = false;
|
|
1546
|
-
if (json.usage) {
|
|
1547
|
-
usage = json.usage;
|
|
1548
|
-
useful = true;
|
|
1549
|
-
}
|
|
1550
|
-
const choice = (_c = json.choices) == null ? void 0 : _c[0];
|
|
1551
|
-
if (choice) {
|
|
1552
|
-
const delta = (_d = choice.delta) != null ? _d : {};
|
|
1553
|
-
if (delta.content) {
|
|
1554
|
-
paragraph += delta.content;
|
|
1555
|
-
useful = true;
|
|
1534
|
+
outer:
|
|
1535
|
+
while (true) {
|
|
1536
|
+
const { done, value } = await reader.read();
|
|
1537
|
+
if (done)
|
|
1538
|
+
break;
|
|
1539
|
+
lineBuffer += decoder.decode(value, { stream: true });
|
|
1540
|
+
let newlineIdx;
|
|
1541
|
+
while ((newlineIdx = lineBuffer.indexOf("\n")) !== -1) {
|
|
1542
|
+
let line = lineBuffer.slice(0, newlineIdx);
|
|
1543
|
+
lineBuffer = lineBuffer.slice(newlineIdx + 1);
|
|
1544
|
+
if (line.endsWith("\r"))
|
|
1545
|
+
line = line.slice(0, -1);
|
|
1546
|
+
if (!line)
|
|
1547
|
+
continue;
|
|
1548
|
+
if (line.startsWith(":"))
|
|
1549
|
+
continue;
|
|
1550
|
+
if (!line.startsWith("data:"))
|
|
1551
|
+
continue;
|
|
1552
|
+
const dataStr = line.slice(5).trimStart();
|
|
1553
|
+
if (dataStr === "[DONE]") {
|
|
1554
|
+
sawDone = true;
|
|
1555
|
+
break outer;
|
|
1556
1556
|
}
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1557
|
+
let json;
|
|
1558
|
+
try {
|
|
1559
|
+
json = JSON.parse(dataStr);
|
|
1560
|
+
} catch (e) {
|
|
1561
|
+
logger_default.error(
|
|
1562
|
+
id,
|
|
1563
|
+
"OpenRouter stream: unparseable data line:",
|
|
1564
|
+
dataStr.slice(0, 200)
|
|
1565
|
+
);
|
|
1566
|
+
continue;
|
|
1567
|
+
}
|
|
1568
|
+
dataChunks++;
|
|
1569
|
+
if (json.error) {
|
|
1570
|
+
logger_default.error(id, "OpenRouter stream error event:", json.error);
|
|
1571
|
+
const error2 = new Error(
|
|
1572
|
+
`OpenRouter error: ${json.error.message}`
|
|
1573
|
+
);
|
|
1574
|
+
error2.data = json.error;
|
|
1575
|
+
throw error2;
|
|
1560
1576
|
}
|
|
1561
|
-
if (
|
|
1562
|
-
|
|
1577
|
+
if (json.provider)
|
|
1578
|
+
provider = json.provider;
|
|
1579
|
+
let useful = false;
|
|
1580
|
+
if (json.usage) {
|
|
1581
|
+
usage = json.usage;
|
|
1563
1582
|
useful = true;
|
|
1564
1583
|
}
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1584
|
+
const choice = (_c = json.choices) == null ? void 0 : _c[0];
|
|
1585
|
+
if (choice) {
|
|
1586
|
+
const delta = (_d = choice.delta) != null ? _d : {};
|
|
1587
|
+
if (delta.content) {
|
|
1588
|
+
paragraph += delta.content;
|
|
1589
|
+
useful = true;
|
|
1590
|
+
}
|
|
1591
|
+
if (delta.reasoning) {
|
|
1592
|
+
reasoning += delta.reasoning;
|
|
1593
|
+
useful = true;
|
|
1594
|
+
}
|
|
1595
|
+
if (Array.isArray(delta.reasoning_details) && delta.reasoning_details.length) {
|
|
1596
|
+
reasoningDetails.push(...delta.reasoning_details);
|
|
1597
|
+
useful = true;
|
|
1598
|
+
}
|
|
1599
|
+
if (Array.isArray(delta.tool_calls)) {
|
|
1600
|
+
for (const toolCall of delta.tool_calls) {
|
|
1601
|
+
const idx = (_e = toolCall.index) != null ? _e : 0;
|
|
1602
|
+
while (toolCalls.length <= idx) {
|
|
1603
|
+
toolCalls.push({ name: "", argumentsJson: "" });
|
|
1604
|
+
}
|
|
1605
|
+
if (toolCall.id)
|
|
1606
|
+
toolCalls[idx].id = toolCall.id;
|
|
1607
|
+
if ((_f = toolCall.function) == null ? void 0 : _f.name)
|
|
1608
|
+
toolCalls[idx].name += toolCall.function.name;
|
|
1609
|
+
if ((_g = toolCall.function) == null ? void 0 : _g.arguments)
|
|
1610
|
+
toolCalls[idx].argumentsJson += toolCall.function.arguments;
|
|
1611
|
+
useful = true;
|
|
1570
1612
|
}
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
if ((_g = toolCall.function) == null ? void 0 : _g.arguments)
|
|
1575
|
-
toolCalls[idx].argumentsJson += toolCall.function.arguments;
|
|
1613
|
+
}
|
|
1614
|
+
if (choice.finish_reason) {
|
|
1615
|
+
finishReason = choice.finish_reason;
|
|
1576
1616
|
useful = true;
|
|
1577
1617
|
}
|
|
1578
1618
|
}
|
|
1579
|
-
if (
|
|
1580
|
-
|
|
1581
|
-
useful = true;
|
|
1582
|
-
}
|
|
1619
|
+
if (useful)
|
|
1620
|
+
armStallTimer();
|
|
1583
1621
|
}
|
|
1584
|
-
if (useful) armStallTimer();
|
|
1585
1622
|
}
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1623
|
+
if (sawDone)
|
|
1624
|
+
reader.cancel().catch(() => {
|
|
1625
|
+
});
|
|
1589
1626
|
if (!sawDone && !finishReason) {
|
|
1590
1627
|
logger_default.error(
|
|
1591
1628
|
id,
|
|
@@ -1624,7 +1661,9 @@ function parseOpenRouterBody(id, data) {
|
|
|
1624
1661
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1625
1662
|
if (data.error) {
|
|
1626
1663
|
logger_default.error(id, "OpenRouter error:", data.error);
|
|
1627
|
-
|
|
1664
|
+
const error2 = new Error(`OpenRouter error: ${data.error.message}`);
|
|
1665
|
+
error2.data = data.error;
|
|
1666
|
+
throw error2;
|
|
1628
1667
|
}
|
|
1629
1668
|
const answer = (_b = (_a = data.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.message;
|
|
1630
1669
|
if (!answer) {
|
|
@@ -1661,22 +1700,81 @@ async function callOpenRouterNonStreaming(id, payload, requestTimeoutMs = OPENRO
|
|
|
1661
1700
|
);
|
|
1662
1701
|
return parseOpenRouterBody(id, response.data);
|
|
1663
1702
|
}
|
|
1703
|
+
var normalizeProviderKey = (name) => name.split("/")[0].toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
1704
|
+
var MODERATION_RE = /moderat|inappropriate content|content polic|content management|flagged/i;
|
|
1705
|
+
function moderationEvictionSlug(error2, payload) {
|
|
1706
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
1707
|
+
const body = (_c = error2 == null ? void 0 : error2.data) != null ? _c : (_b = (_a = error2 == null ? void 0 : error2.response) == null ? void 0 : _a.data) == null ? void 0 : _b.error;
|
|
1708
|
+
if (!body)
|
|
1709
|
+
return null;
|
|
1710
|
+
const message = String((_d = body.message) != null ? _d : "");
|
|
1711
|
+
const isModeration = body.code === 403 || Array.isArray((_e = body.metadata) == null ? void 0 : _e.reasons) || MODERATION_RE.test(message);
|
|
1712
|
+
if (!isModeration)
|
|
1713
|
+
return null;
|
|
1714
|
+
const display = (_h = (_f = body.metadata) == null ? void 0 : _f.provider_name) != null ? _h : (_g = /Upstream error from ([^:]+):/.exec(message)) == null ? void 0 : _g[1];
|
|
1715
|
+
if (!display)
|
|
1716
|
+
return null;
|
|
1717
|
+
const key = normalizeProviderKey(display);
|
|
1718
|
+
const fromOrder = (_j = (_i = payload.provider) == null ? void 0 : _i.order) == null ? void 0 : _j.find(
|
|
1719
|
+
(entry) => normalizeProviderKey(entry) === key
|
|
1720
|
+
);
|
|
1721
|
+
return fromOrder ? fromOrder.split("/")[0] : display.toLowerCase();
|
|
1722
|
+
}
|
|
1723
|
+
var MIN_STREAM_ATTEMPT_MS = 1e4;
|
|
1724
|
+
function streamAttemptBudgetMs(options) {
|
|
1725
|
+
if (options.streamDeadlineAt === void 0)
|
|
1726
|
+
return options.streamTimeoutMs;
|
|
1727
|
+
const remaining = options.streamDeadlineAt - Date.now();
|
|
1728
|
+
if (remaining < MIN_STREAM_ATTEMPT_MS)
|
|
1729
|
+
return null;
|
|
1730
|
+
return Math.min(options.streamTimeoutMs, remaining);
|
|
1731
|
+
}
|
|
1664
1732
|
async function callOpenRouterWithRetries(id, payload, retries = 5, options, signal) {
|
|
1733
|
+
const evicted = [];
|
|
1665
1734
|
return withRetries(
|
|
1666
1735
|
id,
|
|
1667
1736
|
"OpenRouter",
|
|
1668
|
-
() => options.streaming ?
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1737
|
+
() => (options.streaming ? (() => {
|
|
1738
|
+
const budget = streamAttemptBudgetMs(options);
|
|
1739
|
+
if (budget === null) {
|
|
1740
|
+
const error2 = new Error(
|
|
1741
|
+
"OpenRouter stream skipped: caller deadline leaves too little time for another attempt"
|
|
1742
|
+
);
|
|
1743
|
+
error2.deadlineExceeded = true;
|
|
1744
|
+
logger_default.error(id, error2.message);
|
|
1745
|
+
throw error2;
|
|
1746
|
+
}
|
|
1747
|
+
return callOpenRouterStream(
|
|
1748
|
+
id,
|
|
1749
|
+
payload,
|
|
1750
|
+
budget,
|
|
1751
|
+
options.chunkTimeoutMs,
|
|
1752
|
+
signal
|
|
1753
|
+
);
|
|
1754
|
+
})() : callOpenRouterNonStreaming(
|
|
1675
1755
|
id,
|
|
1676
1756
|
payload,
|
|
1677
1757
|
options.requestTimeoutMs,
|
|
1678
1758
|
signal
|
|
1679
|
-
)
|
|
1759
|
+
)).catch((error2) => {
|
|
1760
|
+
var _a, _b, _c, _d;
|
|
1761
|
+
const slug = moderationEvictionSlug(error2, payload);
|
|
1762
|
+
if (slug && !evicted.includes(slug)) {
|
|
1763
|
+
evicted.push(slug);
|
|
1764
|
+
payload.provider = {
|
|
1765
|
+
...payload.provider,
|
|
1766
|
+
ignore: [...(_b = (_a = payload.provider) == null ? void 0 : _a.ignore) != null ? _b : [], slug],
|
|
1767
|
+
order: (_d = (_c = payload.provider) == null ? void 0 : _c.order) == null ? void 0 : _d.filter(
|
|
1768
|
+
(entry) => normalizeProviderKey(entry) !== normalizeProviderKey(slug)
|
|
1769
|
+
)
|
|
1770
|
+
};
|
|
1771
|
+
logger_default.log(
|
|
1772
|
+
id,
|
|
1773
|
+
`OpenRouter moderation eviction: ignoring provider "${slug}" for remaining attempts`
|
|
1774
|
+
);
|
|
1775
|
+
}
|
|
1776
|
+
throw error2;
|
|
1777
|
+
}),
|
|
1680
1778
|
{ retries, signal }
|
|
1681
1779
|
);
|
|
1682
1780
|
}
|
|
@@ -1773,6 +1871,7 @@ async function callWithRetries(id, aiPayload, aiConfig, retries = 5, chunkTimeou
|
|
|
1773
1871
|
{
|
|
1774
1872
|
streaming: (_b = aiPayload.streaming) != null ? _b : true,
|
|
1775
1873
|
streamTimeoutMs: (_c = aiPayload.streamTimeoutMs) != null ? _c : OPENROUTER_STREAM_TIMEOUT_MS,
|
|
1874
|
+
streamDeadlineAt: aiPayload.streamDeadlineAt,
|
|
1776
1875
|
requestTimeoutMs: (_d = aiPayload.requestTimeoutMs) != null ? _d : OPENROUTER_NONSTREAM_TIMEOUT_MS,
|
|
1777
1876
|
chunkTimeoutMs
|
|
1778
1877
|
},
|
|
@@ -1783,7 +1882,8 @@ async function callWithRetries(id, aiPayload, aiConfig, retries = 5, chunkTimeou
|
|
|
1783
1882
|
(_e = result.provider) != null ? _e : result.provider = provider;
|
|
1784
1883
|
return result;
|
|
1785
1884
|
} catch (error2) {
|
|
1786
|
-
if ((_f = aiPayload.signal) == null ? void 0 : _f.aborted)
|
|
1885
|
+
if ((_f = aiPayload.signal) == null ? void 0 : _f.aborted)
|
|
1886
|
+
throw error2;
|
|
1787
1887
|
if (aiPayload.fallbackModel) {
|
|
1788
1888
|
logger_default.error(
|
|
1789
1889
|
id,
|
|
@@ -1813,6 +1913,7 @@ export {
|
|
|
1813
1913
|
GPTModel,
|
|
1814
1914
|
GeminiModel,
|
|
1815
1915
|
GroqModel,
|
|
1916
|
+
MIN_STREAM_ATTEMPT_MS,
|
|
1816
1917
|
OPENROUTER_NONSTREAM_TIMEOUT_MS,
|
|
1817
1918
|
OPENROUTER_STREAM_TIMEOUT_MS,
|
|
1818
1919
|
OpenRouterModel,
|