190proof 1.0.115 → 1.0.117
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 +11 -1
- package/dist/index.d.mts +19 -6
- package/dist/index.d.ts +19 -6
- package/dist/index.js +178 -161
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +175 -160
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
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")
|
|
5
|
-
return require.apply(this, arguments);
|
|
4
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
6
5
|
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
7
6
|
});
|
|
8
7
|
|
|
@@ -138,8 +137,7 @@ async function withRetries(identifier, apiName, fn, options = {}) {
|
|
|
138
137
|
return await fn();
|
|
139
138
|
} catch (error3) {
|
|
140
139
|
lastError = error3;
|
|
141
|
-
if ((_a = options.signal) == null ? void 0 : _a.aborted)
|
|
142
|
-
throw error3;
|
|
140
|
+
if ((_a = options.signal) == null ? void 0 : _a.aborted) throw error3;
|
|
143
141
|
if (onError) {
|
|
144
142
|
onError(error3, attempt);
|
|
145
143
|
} else {
|
|
@@ -163,8 +161,7 @@ function parseStreamedResponse(identifier, paragraph, toolCallAccumulators, allo
|
|
|
163
161
|
const functionCalls = [];
|
|
164
162
|
for (let i = 0; i < toolCallAccumulators.length; i++) {
|
|
165
163
|
const acc = toolCallAccumulators[i];
|
|
166
|
-
if (!acc.name || !acc.arguments)
|
|
167
|
-
continue;
|
|
164
|
+
if (!acc.name || !acc.arguments) continue;
|
|
168
165
|
if (allowedFunctionNames && !allowedFunctionNames.has(acc.name)) {
|
|
169
166
|
throw new Error(
|
|
170
167
|
`Stream error: received function call with unknown name: ${acc.name}`
|
|
@@ -303,8 +300,7 @@ function buildOpenAIRequestConfig(identifier, model, config) {
|
|
|
303
300
|
};
|
|
304
301
|
}
|
|
305
302
|
function filterOpenAICompatReasoningDetails(details) {
|
|
306
|
-
if (!Array.isArray(details))
|
|
307
|
-
return details || void 0;
|
|
303
|
+
if (!Array.isArray(details)) return details || void 0;
|
|
308
304
|
const blocks = details.filter(
|
|
309
305
|
(block) => typeof (block == null ? void 0 : block.type) === "string" && block.type.startsWith("reasoning.")
|
|
310
306
|
);
|
|
@@ -376,19 +372,17 @@ async function prepareOpenAIPayload(identifier, payload) {
|
|
|
376
372
|
};
|
|
377
373
|
});
|
|
378
374
|
}
|
|
379
|
-
if (message.reasoning)
|
|
380
|
-
outMessage.reasoning = message.reasoning;
|
|
375
|
+
if (message.reasoning) outMessage.reasoning = message.reasoning;
|
|
381
376
|
const reasoningDetails = filterOpenAICompatReasoningDetails(
|
|
382
377
|
message.reasoningDetails
|
|
383
378
|
);
|
|
384
|
-
if (reasoningDetails)
|
|
385
|
-
outMessage.reasoning_details = reasoningDetails;
|
|
379
|
+
if (reasoningDetails) outMessage.reasoning_details = reasoningDetails;
|
|
386
380
|
preparedPayload.messages.push(outMessage);
|
|
387
381
|
}
|
|
388
382
|
return preparedPayload;
|
|
389
383
|
}
|
|
390
384
|
async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs, requestTimeoutMs = 12e4, signal) {
|
|
391
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
385
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
392
386
|
const functionNames = openAiPayload.tools ? new Set(openAiPayload.tools.map((fn) => fn.function.name)) : null;
|
|
393
387
|
const { endpoint, headers } = buildOpenAIRequestConfig(
|
|
394
388
|
id,
|
|
@@ -406,7 +400,15 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs,
|
|
|
406
400
|
const response = await fetch(endpoint, {
|
|
407
401
|
method: "POST",
|
|
408
402
|
headers,
|
|
409
|
-
|
|
403
|
+
// include_usage: OpenAI only reports token usage on streams when asked,
|
|
404
|
+
// via a final choices-less chunk — without it every streamed call is
|
|
405
|
+
// invisible to usage accounting. Skipped for Azure, where older
|
|
406
|
+
// api-versions reject stream_options.
|
|
407
|
+
body: JSON.stringify({
|
|
408
|
+
...openAiPayload,
|
|
409
|
+
stream: true,
|
|
410
|
+
...(openAiConfig == null ? void 0 : openAiConfig.service) !== "azure" ? { stream_options: { include_usage: true } } : {}
|
|
411
|
+
}),
|
|
410
412
|
// Merge (don't overwrite) the internal timeout controller with the caller's
|
|
411
413
|
// cancellation signal so both an internal timeout and an external abort stop
|
|
412
414
|
// the stream.
|
|
@@ -417,6 +419,7 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs,
|
|
|
417
419
|
}
|
|
418
420
|
let paragraph = "";
|
|
419
421
|
let reasoning = "";
|
|
422
|
+
let streamUsage = null;
|
|
420
423
|
const toolCallAccumulators = [];
|
|
421
424
|
const reader = response.body.getReader();
|
|
422
425
|
let partialChunk = "";
|
|
@@ -444,17 +447,18 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs,
|
|
|
444
447
|
}
|
|
445
448
|
const jsonStrings = chunk.split(/^data: /gm);
|
|
446
449
|
for (const jsonString of jsonStrings) {
|
|
447
|
-
if (!jsonString)
|
|
448
|
-
continue;
|
|
450
|
+
if (!jsonString) continue;
|
|
449
451
|
if (jsonString.includes("[DONE]")) {
|
|
450
452
|
clearTimeout(overallTimeout);
|
|
451
|
-
|
|
453
|
+
const parsed = parseStreamedResponse(
|
|
452
454
|
id,
|
|
453
455
|
paragraph,
|
|
454
456
|
toolCallAccumulators,
|
|
455
457
|
functionNames,
|
|
456
458
|
reasoning
|
|
457
459
|
);
|
|
460
|
+
parsed.usage = streamUsage;
|
|
461
|
+
return parsed;
|
|
458
462
|
}
|
|
459
463
|
let json;
|
|
460
464
|
try {
|
|
@@ -464,6 +468,15 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs,
|
|
|
464
468
|
continue;
|
|
465
469
|
}
|
|
466
470
|
if (!((_a = json.choices) == null ? void 0 : _a.length)) {
|
|
471
|
+
if (json.usage) {
|
|
472
|
+
streamUsage = {
|
|
473
|
+
prompt_tokens: json.usage.prompt_tokens,
|
|
474
|
+
completion_tokens: json.usage.completion_tokens,
|
|
475
|
+
total_tokens: json.usage.total_tokens,
|
|
476
|
+
cached_tokens: (_c = (_b = json.usage.prompt_tokens_details) == null ? void 0 : _b.cached_tokens) != null ? _c : 0
|
|
477
|
+
};
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
467
480
|
if (json.error) {
|
|
468
481
|
logger_default.error(id, "Stream error from OpenAI:", json.error);
|
|
469
482
|
const error2 = new Error("Stream error: OpenAI error");
|
|
@@ -476,27 +489,24 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs,
|
|
|
476
489
|
}
|
|
477
490
|
continue;
|
|
478
491
|
}
|
|
479
|
-
const toolCalls = (
|
|
492
|
+
const toolCalls = (_e = (_d = json.choices[0]) == null ? void 0 : _d.delta) == null ? void 0 : _e.tool_calls;
|
|
480
493
|
if (toolCalls) {
|
|
481
494
|
for (const toolCall of toolCalls) {
|
|
482
|
-
const idx = (
|
|
495
|
+
const idx = (_f = toolCall.index) != null ? _f : 0;
|
|
483
496
|
while (toolCallAccumulators.length <= idx) {
|
|
484
497
|
toolCallAccumulators.push({ name: "", arguments: "" });
|
|
485
498
|
}
|
|
486
|
-
if (toolCall.id)
|
|
487
|
-
|
|
488
|
-
if ((_e = toolCall.function) == null ? void 0 : _e.name)
|
|
499
|
+
if (toolCall.id) toolCallAccumulators[idx].id = toolCall.id;
|
|
500
|
+
if ((_g = toolCall.function) == null ? void 0 : _g.name)
|
|
489
501
|
toolCallAccumulators[idx].name += toolCall.function.name;
|
|
490
|
-
if ((
|
|
502
|
+
if ((_h = toolCall.function) == null ? void 0 : _h.arguments)
|
|
491
503
|
toolCallAccumulators[idx].arguments += toolCall.function.arguments;
|
|
492
504
|
}
|
|
493
505
|
}
|
|
494
|
-
const text = (
|
|
495
|
-
if (text)
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
if (reasoningDelta)
|
|
499
|
-
reasoning += reasoningDelta;
|
|
506
|
+
const text = (_j = (_i = json.choices[0]) == null ? void 0 : _i.delta) == null ? void 0 : _j.content;
|
|
507
|
+
if (text) paragraph += text;
|
|
508
|
+
const reasoningDelta = (_l = (_k = json.choices[0]) == null ? void 0 : _k.delta) == null ? void 0 : _l.reasoning;
|
|
509
|
+
if (reasoningDelta) reasoning += reasoningDelta;
|
|
500
510
|
}
|
|
501
511
|
}
|
|
502
512
|
}
|
|
@@ -641,8 +651,7 @@ function jigAnthropicMessages(messages) {
|
|
|
641
651
|
];
|
|
642
652
|
}
|
|
643
653
|
jiggedMessages = jiggedMessages.reduce((acc, message) => {
|
|
644
|
-
if (acc.length === 0)
|
|
645
|
-
return [message];
|
|
654
|
+
if (acc.length === 0) return [message];
|
|
646
655
|
const lastMessage = acc[acc.length - 1];
|
|
647
656
|
if (lastMessage.role === message.role) {
|
|
648
657
|
const lastContent = Array.isArray(lastMessage.content) ? lastMessage.content : [{ type: "text", text: lastMessage.content }];
|
|
@@ -896,8 +905,7 @@ function jigGoogleMessages(messages) {
|
|
|
896
905
|
];
|
|
897
906
|
}
|
|
898
907
|
jiggedMessages = jiggedMessages.reduce((acc, message) => {
|
|
899
|
-
if (acc.length === 0)
|
|
900
|
-
return [message];
|
|
908
|
+
if (acc.length === 0) return [message];
|
|
901
909
|
const lastMessage = acc[acc.length - 1];
|
|
902
910
|
if (lastMessage.role === message.role) {
|
|
903
911
|
lastMessage.parts = [...lastMessage.parts, ...message.parts];
|
|
@@ -936,8 +944,7 @@ async function prepareGoogleAIPayload(_identifier, payload) {
|
|
|
936
944
|
const toolNameById = /* @__PURE__ */ new Map();
|
|
937
945
|
for (const m of payload.messages) {
|
|
938
946
|
for (const fc of m.functionCalls || []) {
|
|
939
|
-
if (fc.id)
|
|
940
|
-
toolNameById.set(fc.id, fc.name);
|
|
947
|
+
if (fc.id) toolNameById.set(fc.id, fc.name);
|
|
941
948
|
}
|
|
942
949
|
}
|
|
943
950
|
for (const message of payload.messages) {
|
|
@@ -1017,8 +1024,7 @@ async function callGoogleAI(id, payload, requestTimeoutMs = 12e4, signal) {
|
|
|
1017
1024
|
...payload.thinkingConfig ? { thinkingConfig: payload.thinkingConfig } : {}
|
|
1018
1025
|
}
|
|
1019
1026
|
};
|
|
1020
|
-
if (payload.tools)
|
|
1021
|
-
requestBody.tools = [payload.tools];
|
|
1027
|
+
if (payload.tools) requestBody.tools = [payload.tools];
|
|
1022
1028
|
if (payload.tools && payload.toolConfig) {
|
|
1023
1029
|
requestBody.toolConfig = payload.toolConfig;
|
|
1024
1030
|
}
|
|
@@ -1076,8 +1082,7 @@ async function callGoogleAI(id, payload, requestTimeoutMs = 12e4, signal) {
|
|
|
1076
1082
|
});
|
|
1077
1083
|
continue;
|
|
1078
1084
|
}
|
|
1079
|
-
if (part.text)
|
|
1080
|
-
text += part.text;
|
|
1085
|
+
if (part.text) text += part.text;
|
|
1081
1086
|
if ((_m = part.inlineData) == null ? void 0 : _m.data) {
|
|
1082
1087
|
files.push({ mimeType: "image/png", data: part.inlineData.data });
|
|
1083
1088
|
}
|
|
@@ -1155,21 +1160,15 @@ async function callGoogleAIWithRetries(id, payload, retries = 5, requestTimeoutM
|
|
|
1155
1160
|
finishReason: error2.finishReason,
|
|
1156
1161
|
modelVersion: error2.modelVersion
|
|
1157
1162
|
};
|
|
1158
|
-
if (error2.safetyRatings)
|
|
1159
|
-
|
|
1160
|
-
if (error2.usageMetadata)
|
|
1161
|
-
errorDetails.usageMetadata = error2.usageMetadata;
|
|
1163
|
+
if (error2.safetyRatings) errorDetails.safetyRatings = error2.safetyRatings;
|
|
1164
|
+
if (error2.usageMetadata) errorDetails.usageMetadata = error2.usageMetadata;
|
|
1162
1165
|
if (error2.promptFeedback)
|
|
1163
1166
|
errorDetails.promptFeedback = error2.promptFeedback;
|
|
1164
|
-
if (error2.status)
|
|
1165
|
-
|
|
1166
|
-
if (error2.
|
|
1167
|
-
errorDetails.errorCode = error2.code;
|
|
1168
|
-
if (error2.details)
|
|
1169
|
-
errorDetails.errorDetails = error2.details;
|
|
1167
|
+
if (error2.status) errorDetails.httpStatus = error2.status;
|
|
1168
|
+
if (error2.code) errorDetails.errorCode = error2.code;
|
|
1169
|
+
if (error2.details) errorDetails.errorDetails = error2.details;
|
|
1170
1170
|
const fileUris = payload.messages.flatMap((m) => m.parts).filter((p) => "fileData" in p).map((p) => p.fileData.fileUri);
|
|
1171
|
-
if (fileUris.length)
|
|
1172
|
-
errorDetails.fileUris = fileUris;
|
|
1171
|
+
if (fileUris.length) errorDetails.fileUris = fileUris;
|
|
1173
1172
|
logger_default.error(
|
|
1174
1173
|
id,
|
|
1175
1174
|
`Retry #${attempt} error: ${error2.message}`,
|
|
@@ -1219,7 +1218,7 @@ async function callGoogleAIWithRetries(id, payload, retries = 5, requestTimeoutM
|
|
|
1219
1218
|
function normalizeMessageContent(content) {
|
|
1220
1219
|
return Array.isArray(content) ? content.map((c) => c.type === "text" ? c.text : `[${c.type}]`).join("\n") : content;
|
|
1221
1220
|
}
|
|
1222
|
-
function prepareOpenAICompatMessages(messages) {
|
|
1221
|
+
function prepareOpenAICompatMessages(messages, opts = {}) {
|
|
1223
1222
|
var _a;
|
|
1224
1223
|
const out = [];
|
|
1225
1224
|
for (const message of messages) {
|
|
@@ -1241,6 +1240,22 @@ function prepareOpenAICompatMessages(messages) {
|
|
|
1241
1240
|
role: message.role,
|
|
1242
1241
|
content
|
|
1243
1242
|
};
|
|
1243
|
+
if (opts.imageParts) {
|
|
1244
|
+
const imageBlocks = (message.files || []).filter(
|
|
1245
|
+
(file) => ALLOWED_IMAGE_MIME_TYPES.includes(file.mimeType) && (file.url || file.data)
|
|
1246
|
+
).map((file) => ({
|
|
1247
|
+
type: "image_url",
|
|
1248
|
+
image_url: {
|
|
1249
|
+
url: file.url || `data:${file.mimeType};base64,${file.data}`
|
|
1250
|
+
}
|
|
1251
|
+
}));
|
|
1252
|
+
if (imageBlocks.length) {
|
|
1253
|
+
outMessage.content = [
|
|
1254
|
+
...content ? [{ type: "text", text: content }] : [],
|
|
1255
|
+
...imageBlocks
|
|
1256
|
+
];
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1244
1259
|
if ((_a = message.functionCalls) == null ? void 0 : _a.length) {
|
|
1245
1260
|
outMessage.tool_calls = message.functionCalls.map((fc, i) => {
|
|
1246
1261
|
var _a2;
|
|
@@ -1253,16 +1268,14 @@ function prepareOpenAICompatMessages(messages) {
|
|
|
1253
1268
|
}
|
|
1254
1269
|
};
|
|
1255
1270
|
});
|
|
1256
|
-
if (!content)
|
|
1271
|
+
if (!content && !Array.isArray(outMessage.content))
|
|
1257
1272
|
outMessage.content = null;
|
|
1258
1273
|
}
|
|
1259
|
-
if (message.reasoning)
|
|
1260
|
-
outMessage.reasoning = message.reasoning;
|
|
1274
|
+
if (message.reasoning) outMessage.reasoning = message.reasoning;
|
|
1261
1275
|
const reasoningDetails = filterOpenAICompatReasoningDetails(
|
|
1262
1276
|
message.reasoningDetails
|
|
1263
1277
|
);
|
|
1264
|
-
if (reasoningDetails)
|
|
1265
|
-
outMessage.reasoning_details = reasoningDetails;
|
|
1278
|
+
if (reasoningDetails) outMessage.reasoning_details = reasoningDetails;
|
|
1266
1279
|
out.push(outMessage);
|
|
1267
1280
|
}
|
|
1268
1281
|
return out;
|
|
@@ -1350,18 +1363,22 @@ async function callGroqWithRetries(id, payload, retries = 5, requestTimeoutMs =
|
|
|
1350
1363
|
signal
|
|
1351
1364
|
});
|
|
1352
1365
|
}
|
|
1366
|
+
var openRouterImageRejectedModels = /* @__PURE__ */ new Set();
|
|
1353
1367
|
function prepareOpenRouterPayload(payload) {
|
|
1354
1368
|
var _a;
|
|
1355
1369
|
return {
|
|
1356
1370
|
model: payload.model,
|
|
1357
|
-
messages: prepareOpenAICompatMessages(payload.messages
|
|
1371
|
+
messages: prepareOpenAICompatMessages(payload.messages, {
|
|
1372
|
+
imageParts: !openRouterImageRejectedModels.has(String(payload.model))
|
|
1373
|
+
}),
|
|
1358
1374
|
tools: (_a = payload.functions) == null ? void 0 : _a.map((fn) => ({
|
|
1359
1375
|
type: "function",
|
|
1360
1376
|
function: fn
|
|
1361
1377
|
})),
|
|
1362
1378
|
tool_choice: payload.function_call ? typeof payload.function_call === "string" ? payload.function_call : { type: "function", function: payload.function_call } : void 0,
|
|
1363
1379
|
temperature: payload.temperature,
|
|
1364
|
-
provider: payload.provider
|
|
1380
|
+
provider: payload.provider,
|
|
1381
|
+
reasoning_effort: payload.reasoningEffort
|
|
1365
1382
|
};
|
|
1366
1383
|
}
|
|
1367
1384
|
var DSML_ENVELOPE_RE = /<|+DSML|+tool_calls>/;
|
|
@@ -1405,8 +1422,7 @@ function parseDsmlToolCalls(content) {
|
|
|
1405
1422
|
let last = -1;
|
|
1406
1423
|
let tag;
|
|
1407
1424
|
while ((tag = DSML_ANY_TAG_RE.exec(content)) !== null) {
|
|
1408
|
-
if (first === -1)
|
|
1409
|
-
first = tag.index;
|
|
1425
|
+
if (first === -1) first = tag.index;
|
|
1410
1426
|
last = tag.index + tag[0].length;
|
|
1411
1427
|
}
|
|
1412
1428
|
const remaining = first === -1 ? content : (content.slice(0, first) + content.slice(last)).trim();
|
|
@@ -1422,8 +1438,7 @@ function finalizeOpenRouterMessage(id, raw) {
|
|
|
1422
1438
|
const functionCalls = [];
|
|
1423
1439
|
for (let i = 0; i < raw.toolCalls.length; i++) {
|
|
1424
1440
|
const tc = raw.toolCalls[i];
|
|
1425
|
-
if (!tc.name)
|
|
1426
|
-
continue;
|
|
1441
|
+
if (!tc.name) continue;
|
|
1427
1442
|
functionCalls.push({
|
|
1428
1443
|
id: (_a = tc.id) != null ? _a : `call_${i}`,
|
|
1429
1444
|
name: tc.name,
|
|
@@ -1476,8 +1491,7 @@ async function callOpenRouterStream(id, payload, streamTimeoutMs = OPENROUTER_ST
|
|
|
1476
1491
|
controller.abort();
|
|
1477
1492
|
};
|
|
1478
1493
|
const unref = (t) => {
|
|
1479
|
-
if (typeof t === "object" && "unref" in t)
|
|
1480
|
-
t.unref();
|
|
1494
|
+
if (typeof t === "object" && "unref" in t) t.unref();
|
|
1481
1495
|
return t;
|
|
1482
1496
|
};
|
|
1483
1497
|
const totalTimer = unref(
|
|
@@ -1545,98 +1559,88 @@ async function callOpenRouterStream(id, payload, streamTimeoutMs = OPENROUTER_ST
|
|
|
1545
1559
|
const reader = response.body.getReader();
|
|
1546
1560
|
const decoder = new TextDecoder();
|
|
1547
1561
|
let lineBuffer = "";
|
|
1548
|
-
outer:
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
let newlineIdx;
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1562
|
+
outer: while (true) {
|
|
1563
|
+
const { done, value } = await reader.read();
|
|
1564
|
+
if (done) break;
|
|
1565
|
+
lineBuffer += decoder.decode(value, { stream: true });
|
|
1566
|
+
let newlineIdx;
|
|
1567
|
+
while ((newlineIdx = lineBuffer.indexOf("\n")) !== -1) {
|
|
1568
|
+
let line = lineBuffer.slice(0, newlineIdx);
|
|
1569
|
+
lineBuffer = lineBuffer.slice(newlineIdx + 1);
|
|
1570
|
+
if (line.endsWith("\r")) line = line.slice(0, -1);
|
|
1571
|
+
if (!line) continue;
|
|
1572
|
+
if (line.startsWith(":")) continue;
|
|
1573
|
+
if (!line.startsWith("data:")) continue;
|
|
1574
|
+
const dataStr = line.slice(5).trimStart();
|
|
1575
|
+
if (dataStr === "[DONE]") {
|
|
1576
|
+
sawDone = true;
|
|
1577
|
+
break outer;
|
|
1578
|
+
}
|
|
1579
|
+
let json;
|
|
1580
|
+
try {
|
|
1581
|
+
json = JSON.parse(dataStr);
|
|
1582
|
+
} catch (e) {
|
|
1583
|
+
logger_default.error(
|
|
1584
|
+
id,
|
|
1585
|
+
"OpenRouter stream: unparseable data line:",
|
|
1586
|
+
dataStr.slice(0, 200)
|
|
1587
|
+
);
|
|
1588
|
+
continue;
|
|
1589
|
+
}
|
|
1590
|
+
dataChunks++;
|
|
1591
|
+
if (json.error) {
|
|
1592
|
+
logger_default.error(id, "OpenRouter stream error event:", json.error);
|
|
1593
|
+
const error2 = new Error(
|
|
1594
|
+
`OpenRouter error: ${json.error.message}`
|
|
1595
|
+
);
|
|
1596
|
+
error2.data = json.error;
|
|
1597
|
+
throw error2;
|
|
1598
|
+
}
|
|
1599
|
+
if (json.provider) provider = json.provider;
|
|
1600
|
+
let useful = false;
|
|
1601
|
+
if (json.usage) {
|
|
1602
|
+
usage = json.usage;
|
|
1603
|
+
useful = true;
|
|
1604
|
+
}
|
|
1605
|
+
const choice = (_c = json.choices) == null ? void 0 : _c[0];
|
|
1606
|
+
if (choice) {
|
|
1607
|
+
const delta = (_d = choice.delta) != null ? _d : {};
|
|
1608
|
+
if (delta.content) {
|
|
1609
|
+
paragraph += delta.content;
|
|
1610
|
+
useful = true;
|
|
1581
1611
|
}
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
const error2 = new Error(
|
|
1586
|
-
`OpenRouter error: ${json.error.message}`
|
|
1587
|
-
);
|
|
1588
|
-
error2.data = json.error;
|
|
1589
|
-
throw error2;
|
|
1612
|
+
if (delta.reasoning) {
|
|
1613
|
+
reasoning += delta.reasoning;
|
|
1614
|
+
useful = true;
|
|
1590
1615
|
}
|
|
1591
|
-
if (
|
|
1592
|
-
|
|
1593
|
-
let useful = false;
|
|
1594
|
-
if (json.usage) {
|
|
1595
|
-
usage = json.usage;
|
|
1616
|
+
if (Array.isArray(delta.reasoning_details) && delta.reasoning_details.length) {
|
|
1617
|
+
reasoningDetails.push(...delta.reasoning_details);
|
|
1596
1618
|
useful = true;
|
|
1597
1619
|
}
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
useful = true;
|
|
1604
|
-
}
|
|
1605
|
-
if (delta.reasoning) {
|
|
1606
|
-
reasoning += delta.reasoning;
|
|
1607
|
-
useful = true;
|
|
1608
|
-
}
|
|
1609
|
-
if (Array.isArray(delta.reasoning_details) && delta.reasoning_details.length) {
|
|
1610
|
-
reasoningDetails.push(...delta.reasoning_details);
|
|
1611
|
-
useful = true;
|
|
1612
|
-
}
|
|
1613
|
-
if (Array.isArray(delta.tool_calls)) {
|
|
1614
|
-
for (const toolCall of delta.tool_calls) {
|
|
1615
|
-
const idx = (_e = toolCall.index) != null ? _e : 0;
|
|
1616
|
-
while (toolCalls.length <= idx) {
|
|
1617
|
-
toolCalls.push({ name: "", argumentsJson: "" });
|
|
1618
|
-
}
|
|
1619
|
-
if (toolCall.id)
|
|
1620
|
-
toolCalls[idx].id = toolCall.id;
|
|
1621
|
-
if ((_f = toolCall.function) == null ? void 0 : _f.name)
|
|
1622
|
-
toolCalls[idx].name += toolCall.function.name;
|
|
1623
|
-
if ((_g = toolCall.function) == null ? void 0 : _g.arguments)
|
|
1624
|
-
toolCalls[idx].argumentsJson += toolCall.function.arguments;
|
|
1625
|
-
useful = true;
|
|
1620
|
+
if (Array.isArray(delta.tool_calls)) {
|
|
1621
|
+
for (const toolCall of delta.tool_calls) {
|
|
1622
|
+
const idx = (_e = toolCall.index) != null ? _e : 0;
|
|
1623
|
+
while (toolCalls.length <= idx) {
|
|
1624
|
+
toolCalls.push({ name: "", argumentsJson: "" });
|
|
1626
1625
|
}
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1626
|
+
if (toolCall.id) toolCalls[idx].id = toolCall.id;
|
|
1627
|
+
if ((_f = toolCall.function) == null ? void 0 : _f.name)
|
|
1628
|
+
toolCalls[idx].name += toolCall.function.name;
|
|
1629
|
+
if ((_g = toolCall.function) == null ? void 0 : _g.arguments)
|
|
1630
|
+
toolCalls[idx].argumentsJson += toolCall.function.arguments;
|
|
1630
1631
|
useful = true;
|
|
1631
1632
|
}
|
|
1632
1633
|
}
|
|
1633
|
-
if (
|
|
1634
|
-
|
|
1634
|
+
if (choice.finish_reason) {
|
|
1635
|
+
finishReason = choice.finish_reason;
|
|
1636
|
+
useful = true;
|
|
1637
|
+
}
|
|
1635
1638
|
}
|
|
1639
|
+
if (useful) armStallTimer();
|
|
1636
1640
|
}
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1641
|
+
}
|
|
1642
|
+
if (sawDone) reader.cancel().catch(() => {
|
|
1643
|
+
});
|
|
1640
1644
|
if (!sawDone && !finishReason) {
|
|
1641
1645
|
logger_default.error(
|
|
1642
1646
|
id,
|
|
@@ -1743,15 +1747,12 @@ var MODERATION_RE = /moderat|inappropriate content|content polic|content managem
|
|
|
1743
1747
|
function moderationEvictionSlug(error2, payload) {
|
|
1744
1748
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
1745
1749
|
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;
|
|
1746
|
-
if (!body)
|
|
1747
|
-
return null;
|
|
1750
|
+
if (!body) return null;
|
|
1748
1751
|
const message = String((_d = body.message) != null ? _d : "");
|
|
1749
1752
|
const isModeration = body.code === 403 || Array.isArray((_e = body.metadata) == null ? void 0 : _e.reasons) || MODERATION_RE.test(message);
|
|
1750
|
-
if (!isModeration)
|
|
1751
|
-
return null;
|
|
1753
|
+
if (!isModeration) return null;
|
|
1752
1754
|
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];
|
|
1753
|
-
if (!display)
|
|
1754
|
-
return null;
|
|
1755
|
+
if (!display) return null;
|
|
1755
1756
|
const key = normalizeProviderKey(display);
|
|
1756
1757
|
const fromOrder = (_j = (_i = payload.provider) == null ? void 0 : _i.order) == null ? void 0 : _j.find(
|
|
1757
1758
|
(entry) => normalizeProviderKey(entry) === key
|
|
@@ -1760,12 +1761,15 @@ function moderationEvictionSlug(error2, payload) {
|
|
|
1760
1761
|
}
|
|
1761
1762
|
var MIN_STREAM_ATTEMPT_MS = 1e4;
|
|
1762
1763
|
var estimateTokens = (text) => Math.round(text.length / 4);
|
|
1764
|
+
function isImageInputRejection(error2) {
|
|
1765
|
+
var _a, _b, _c, _d;
|
|
1766
|
+
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;
|
|
1767
|
+
return /no endpoints found.*image input/i.test(String((_d = body == null ? void 0 : body.message) != null ? _d : ""));
|
|
1768
|
+
}
|
|
1763
1769
|
function streamAttemptBudgetMs(options) {
|
|
1764
|
-
if (options.streamDeadlineAt === void 0)
|
|
1765
|
-
return options.streamTimeoutMs;
|
|
1770
|
+
if (options.streamDeadlineAt === void 0) return options.streamTimeoutMs;
|
|
1766
1771
|
const remaining = options.streamDeadlineAt - Date.now();
|
|
1767
|
-
if (remaining < MIN_STREAM_ATTEMPT_MS)
|
|
1768
|
-
return null;
|
|
1772
|
+
if (remaining < MIN_STREAM_ATTEMPT_MS) return null;
|
|
1769
1773
|
return Math.min(options.streamTimeoutMs, remaining);
|
|
1770
1774
|
}
|
|
1771
1775
|
async function callOpenRouterWithRetries(id, payload, retries = 5, options, signal) {
|
|
@@ -1812,6 +1816,16 @@ async function callOpenRouterWithRetries(id, payload, retries = 5, options, sign
|
|
|
1812
1816
|
`OpenRouter moderation eviction: ignoring provider "${slug}" for remaining attempts`
|
|
1813
1817
|
);
|
|
1814
1818
|
}
|
|
1819
|
+
if (isImageInputRejection(error2) && options.genericMessages) {
|
|
1820
|
+
openRouterImageRejectedModels.add(String(payload.model));
|
|
1821
|
+
payload.messages = prepareOpenAICompatMessages(
|
|
1822
|
+
options.genericMessages
|
|
1823
|
+
);
|
|
1824
|
+
logger_default.log(
|
|
1825
|
+
id,
|
|
1826
|
+
`OpenRouter: ${payload.model} has no image-capable endpoints; retrying with images as text refs`
|
|
1827
|
+
);
|
|
1828
|
+
}
|
|
1815
1829
|
throw error2;
|
|
1816
1830
|
}),
|
|
1817
1831
|
{ retries, signal }
|
|
@@ -1912,7 +1926,8 @@ async function callWithRetries(id, aiPayload, aiConfig, retries = 5, chunkTimeou
|
|
|
1912
1926
|
streamTimeoutMs: (_c = aiPayload.streamTimeoutMs) != null ? _c : OPENROUTER_STREAM_TIMEOUT_MS,
|
|
1913
1927
|
streamDeadlineAt: aiPayload.streamDeadlineAt,
|
|
1914
1928
|
requestTimeoutMs: (_d = aiPayload.requestTimeoutMs) != null ? _d : OPENROUTER_NONSTREAM_TIMEOUT_MS,
|
|
1915
|
-
chunkTimeoutMs
|
|
1929
|
+
chunkTimeoutMs,
|
|
1930
|
+
genericMessages: routingPayload.messages
|
|
1916
1931
|
},
|
|
1917
1932
|
signal
|
|
1918
1933
|
);
|
|
@@ -1921,8 +1936,7 @@ async function callWithRetries(id, aiPayload, aiConfig, retries = 5, chunkTimeou
|
|
|
1921
1936
|
(_e = result.provider) != null ? _e : result.provider = provider;
|
|
1922
1937
|
return result;
|
|
1923
1938
|
} catch (error2) {
|
|
1924
|
-
if ((_f = aiPayload.signal) == null ? void 0 : _f.aborted)
|
|
1925
|
-
throw error2;
|
|
1939
|
+
if ((_f = aiPayload.signal) == null ? void 0 : _f.aborted) throw error2;
|
|
1926
1940
|
if (aiPayload.fallbackModel) {
|
|
1927
1941
|
logger_default.error(
|
|
1928
1942
|
id,
|
|
@@ -1957,6 +1971,7 @@ export {
|
|
|
1957
1971
|
OPENROUTER_STREAM_TIMEOUT_MS,
|
|
1958
1972
|
OpenRouterModel,
|
|
1959
1973
|
callWithRetries,
|
|
1974
|
+
openRouterImageRejectedModels,
|
|
1960
1975
|
parseModelString
|
|
1961
1976
|
};
|
|
1962
1977
|
//# sourceMappingURL=index.mjs.map
|