190proof 1.0.99 → 1.0.101
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/dist/index.js +68 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +68 -10
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -142,7 +142,7 @@ function isHeicImage(name, mime) {
|
|
|
142
142
|
var sharp = require("sharp");
|
|
143
143
|
var decode = require("heic-decode");
|
|
144
144
|
async function withRetries(identifier, apiName, fn, options = {}) {
|
|
145
|
-
var _a;
|
|
145
|
+
var _a, _b, _c, _d;
|
|
146
146
|
const { retries = 5, baseDelayMs = 125, onError } = options;
|
|
147
147
|
logger_default.log(identifier, `Calling ${apiName} API with retries`);
|
|
148
148
|
let lastError;
|
|
@@ -163,8 +163,9 @@ async function withRetries(identifier, apiName, fn, options = {}) {
|
|
|
163
163
|
await timeout(baseDelayMs * attempt);
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
|
+
const detail = ((_d = (_c = (_b = lastError == null ? void 0 : lastError.response) == null ? void 0 : _b.data) == null ? void 0 : _c.error) == null ? void 0 : _d.message) || (lastError == null ? void 0 : lastError.message) || String(lastError);
|
|
166
167
|
const error2 = new Error(
|
|
167
|
-
`Failed to call ${apiName} API after ${retries} attempts`
|
|
168
|
+
`Failed to call ${apiName} API after ${retries} attempts: ${detail}`
|
|
168
169
|
);
|
|
169
170
|
error2.cause = lastError;
|
|
170
171
|
throw error2;
|
|
@@ -1304,8 +1305,56 @@ function prepareOpenRouterPayload(payload) {
|
|
|
1304
1305
|
provider: payload.provider
|
|
1305
1306
|
};
|
|
1306
1307
|
}
|
|
1308
|
+
var DSML_ENVELOPE_RE = /<|+DSML|+tool_calls>/;
|
|
1309
|
+
var DSML_DELIMITER_RE = /<\/?|+DSML|+/;
|
|
1310
|
+
var DSML_INVOKE_RE = /<|+DSML|+invoke\s+name="([^"]+)"\s*>([\s\S]*?)<\/|+DSML|+invoke>/g;
|
|
1311
|
+
var DSML_PARAM_RE = /<|+DSML|+parameter\s+name="([^"]+)"(?:\s+string="(true|false)")?\s*>([\s\S]*?)<\/|+DSML|+parameter>/g;
|
|
1312
|
+
var DSML_ANY_TAG_RE = /<\/?|+DSML|+[^>]*>/g;
|
|
1313
|
+
function parseDsmlToolCalls(content) {
|
|
1314
|
+
const calls = [];
|
|
1315
|
+
DSML_INVOKE_RE.lastIndex = 0;
|
|
1316
|
+
let invokeMatch;
|
|
1317
|
+
while ((invokeMatch = DSML_INVOKE_RE.exec(content)) !== null) {
|
|
1318
|
+
const name = invokeMatch[1];
|
|
1319
|
+
const inner = invokeMatch[2];
|
|
1320
|
+
const args = {};
|
|
1321
|
+
let ok = true;
|
|
1322
|
+
DSML_PARAM_RE.lastIndex = 0;
|
|
1323
|
+
let paramMatch;
|
|
1324
|
+
while ((paramMatch = DSML_PARAM_RE.exec(inner)) !== null) {
|
|
1325
|
+
const [, paramName, stringAttr, rawValue] = paramMatch;
|
|
1326
|
+
if (stringAttr === "false") {
|
|
1327
|
+
try {
|
|
1328
|
+
args[paramName] = JSON.parse(rawValue);
|
|
1329
|
+
} catch (e) {
|
|
1330
|
+
ok = false;
|
|
1331
|
+
break;
|
|
1332
|
+
}
|
|
1333
|
+
} else {
|
|
1334
|
+
args[paramName] = rawValue;
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
if (ok && name) {
|
|
1338
|
+
calls.push({ id: `call_${calls.length}`, name, arguments: args });
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
if (!calls.length) {
|
|
1342
|
+
return { calls, remainingContent: content };
|
|
1343
|
+
}
|
|
1344
|
+
DSML_ANY_TAG_RE.lastIndex = 0;
|
|
1345
|
+
let first = -1;
|
|
1346
|
+
let last = -1;
|
|
1347
|
+
let tag;
|
|
1348
|
+
while ((tag = DSML_ANY_TAG_RE.exec(content)) !== null) {
|
|
1349
|
+
if (first === -1)
|
|
1350
|
+
first = tag.index;
|
|
1351
|
+
last = tag.index + tag[0].length;
|
|
1352
|
+
}
|
|
1353
|
+
const remaining = first === -1 ? content : (content.slice(0, first) + content.slice(last)).trim();
|
|
1354
|
+
return { calls, remainingContent: remaining.length ? remaining : null };
|
|
1355
|
+
}
|
|
1307
1356
|
async function callOpenRouter(id, payload, requestTimeoutMs = 12e4) {
|
|
1308
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1357
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
1309
1358
|
const response = await import_axios.default.post(
|
|
1310
1359
|
"https://openrouter.ai/api/v1/chat/completions",
|
|
1311
1360
|
payload,
|
|
@@ -1337,29 +1386,38 @@ async function callOpenRouter(id, payload, requestTimeoutMs = 12e4) {
|
|
|
1337
1386
|
});
|
|
1338
1387
|
}
|
|
1339
1388
|
}
|
|
1340
|
-
|
|
1389
|
+
let content = (_e = answer.content) != null ? _e : null;
|
|
1390
|
+
if (!functionCalls.length && content && DSML_ENVELOPE_RE.test(content)) {
|
|
1391
|
+
const { calls, remainingContent } = parseDsmlToolCalls(content);
|
|
1392
|
+
if (calls.length) {
|
|
1393
|
+
functionCalls.push(...calls);
|
|
1394
|
+
content = remainingContent;
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
const hasUnparsedDsml = !!content && DSML_DELIMITER_RE.test(content);
|
|
1398
|
+
if (!functionCalls.length && (!content || hasUnparsedDsml)) {
|
|
1341
1399
|
logger_default.error(
|
|
1342
1400
|
id,
|
|
1343
|
-
"OpenRouter:
|
|
1401
|
+
"OpenRouter: empty or unparseable completion:",
|
|
1344
1402
|
JSON.stringify(response.data)
|
|
1345
1403
|
);
|
|
1346
1404
|
throw new Error(
|
|
1347
|
-
"OpenRouter: received message without content or function_call"
|
|
1405
|
+
"OpenRouter: received message without usable content or function_call"
|
|
1348
1406
|
);
|
|
1349
1407
|
}
|
|
1350
1408
|
return {
|
|
1351
1409
|
role: "assistant",
|
|
1352
|
-
content:
|
|
1410
|
+
content: content || null,
|
|
1353
1411
|
function_call: functionCalls[0] || null,
|
|
1354
1412
|
function_calls: functionCalls,
|
|
1355
1413
|
files: [],
|
|
1356
|
-
reasoning: (
|
|
1357
|
-
reasoningDetails: (
|
|
1414
|
+
reasoning: (_f = answer.reasoning) != null ? _f : void 0,
|
|
1415
|
+
reasoningDetails: (_g = answer.reasoning_details) != null ? _g : void 0,
|
|
1358
1416
|
usage: response.data.usage ? {
|
|
1359
1417
|
prompt_tokens: response.data.usage.prompt_tokens,
|
|
1360
1418
|
completion_tokens: response.data.usage.completion_tokens,
|
|
1361
1419
|
total_tokens: response.data.usage.total_tokens,
|
|
1362
|
-
cached_tokens: (
|
|
1420
|
+
cached_tokens: (_i = (_h = response.data.usage.prompt_tokens_details) == null ? void 0 : _h.cached_tokens) != null ? _i : 0
|
|
1363
1421
|
} : null
|
|
1364
1422
|
};
|
|
1365
1423
|
}
|