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.mjs CHANGED
@@ -111,7 +111,7 @@ function isHeicImage(name, mime) {
111
111
  var sharp = __require("sharp");
112
112
  var decode = __require("heic-decode");
113
113
  async function withRetries(identifier, apiName, fn, options = {}) {
114
- var _a;
114
+ var _a, _b, _c, _d;
115
115
  const { retries = 5, baseDelayMs = 125, onError } = options;
116
116
  logger_default.log(identifier, `Calling ${apiName} API with retries`);
117
117
  let lastError;
@@ -132,8 +132,9 @@ async function withRetries(identifier, apiName, fn, options = {}) {
132
132
  await timeout(baseDelayMs * attempt);
133
133
  }
134
134
  }
135
+ 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);
135
136
  const error2 = new Error(
136
- `Failed to call ${apiName} API after ${retries} attempts`
137
+ `Failed to call ${apiName} API after ${retries} attempts: ${detail}`
137
138
  );
138
139
  error2.cause = lastError;
139
140
  throw error2;
@@ -1273,8 +1274,56 @@ function prepareOpenRouterPayload(payload) {
1273
1274
  provider: payload.provider
1274
1275
  };
1275
1276
  }
1277
+ var DSML_ENVELOPE_RE = /<|+DSML|+tool_calls>/;
1278
+ var DSML_DELIMITER_RE = /<\/?|+DSML|+/;
1279
+ var DSML_INVOKE_RE = /<|+DSML|+invoke\s+name="([^"]+)"\s*>([\s\S]*?)<\/|+DSML|+invoke>/g;
1280
+ var DSML_PARAM_RE = /<|+DSML|+parameter\s+name="([^"]+)"(?:\s+string="(true|false)")?\s*>([\s\S]*?)<\/|+DSML|+parameter>/g;
1281
+ var DSML_ANY_TAG_RE = /<\/?|+DSML|+[^>]*>/g;
1282
+ function parseDsmlToolCalls(content) {
1283
+ const calls = [];
1284
+ DSML_INVOKE_RE.lastIndex = 0;
1285
+ let invokeMatch;
1286
+ while ((invokeMatch = DSML_INVOKE_RE.exec(content)) !== null) {
1287
+ const name = invokeMatch[1];
1288
+ const inner = invokeMatch[2];
1289
+ const args = {};
1290
+ let ok = true;
1291
+ DSML_PARAM_RE.lastIndex = 0;
1292
+ let paramMatch;
1293
+ while ((paramMatch = DSML_PARAM_RE.exec(inner)) !== null) {
1294
+ const [, paramName, stringAttr, rawValue] = paramMatch;
1295
+ if (stringAttr === "false") {
1296
+ try {
1297
+ args[paramName] = JSON.parse(rawValue);
1298
+ } catch (e) {
1299
+ ok = false;
1300
+ break;
1301
+ }
1302
+ } else {
1303
+ args[paramName] = rawValue;
1304
+ }
1305
+ }
1306
+ if (ok && name) {
1307
+ calls.push({ id: `call_${calls.length}`, name, arguments: args });
1308
+ }
1309
+ }
1310
+ if (!calls.length) {
1311
+ return { calls, remainingContent: content };
1312
+ }
1313
+ DSML_ANY_TAG_RE.lastIndex = 0;
1314
+ let first = -1;
1315
+ let last = -1;
1316
+ let tag;
1317
+ while ((tag = DSML_ANY_TAG_RE.exec(content)) !== null) {
1318
+ if (first === -1)
1319
+ first = tag.index;
1320
+ last = tag.index + tag[0].length;
1321
+ }
1322
+ const remaining = first === -1 ? content : (content.slice(0, first) + content.slice(last)).trim();
1323
+ return { calls, remainingContent: remaining.length ? remaining : null };
1324
+ }
1276
1325
  async function callOpenRouter(id, payload, requestTimeoutMs = 12e4) {
1277
- var _a, _b, _c, _d, _e, _f, _g, _h;
1326
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1278
1327
  const response = await axios.post(
1279
1328
  "https://openrouter.ai/api/v1/chat/completions",
1280
1329
  payload,
@@ -1306,29 +1355,38 @@ async function callOpenRouter(id, payload, requestTimeoutMs = 12e4) {
1306
1355
  });
1307
1356
  }
1308
1357
  }
1309
- if (!answer.content && !functionCalls.length) {
1358
+ let content = (_e = answer.content) != null ? _e : null;
1359
+ if (!functionCalls.length && content && DSML_ENVELOPE_RE.test(content)) {
1360
+ const { calls, remainingContent } = parseDsmlToolCalls(content);
1361
+ if (calls.length) {
1362
+ functionCalls.push(...calls);
1363
+ content = remainingContent;
1364
+ }
1365
+ }
1366
+ const hasUnparsedDsml = !!content && DSML_DELIMITER_RE.test(content);
1367
+ if (!functionCalls.length && (!content || hasUnparsedDsml)) {
1310
1368
  logger_default.error(
1311
1369
  id,
1312
- "OpenRouter: received message without content or function_call:",
1370
+ "OpenRouter: empty or unparseable completion:",
1313
1371
  JSON.stringify(response.data)
1314
1372
  );
1315
1373
  throw new Error(
1316
- "OpenRouter: received message without content or function_call"
1374
+ "OpenRouter: received message without usable content or function_call"
1317
1375
  );
1318
1376
  }
1319
1377
  return {
1320
1378
  role: "assistant",
1321
- content: answer.content || null,
1379
+ content: content || null,
1322
1380
  function_call: functionCalls[0] || null,
1323
1381
  function_calls: functionCalls,
1324
1382
  files: [],
1325
- reasoning: (_e = answer.reasoning) != null ? _e : void 0,
1326
- reasoningDetails: (_f = answer.reasoning_details) != null ? _f : void 0,
1383
+ reasoning: (_f = answer.reasoning) != null ? _f : void 0,
1384
+ reasoningDetails: (_g = answer.reasoning_details) != null ? _g : void 0,
1327
1385
  usage: response.data.usage ? {
1328
1386
  prompt_tokens: response.data.usage.prompt_tokens,
1329
1387
  completion_tokens: response.data.usage.completion_tokens,
1330
1388
  total_tokens: response.data.usage.total_tokens,
1331
- cached_tokens: (_h = (_g = response.data.usage.prompt_tokens_details) == null ? void 0 : _g.cached_tokens) != null ? _h : 0
1389
+ cached_tokens: (_i = (_h = response.data.usage.prompt_tokens_details) == null ? void 0 : _h.cached_tokens) != null ? _i : 0
1332
1390
  } : null
1333
1391
  };
1334
1392
  }