190proof 1.0.114 → 1.0.116

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
@@ -311,6 +311,7 @@ async function prepareOpenAIPayload(identifier, payload) {
311
311
  const preparedPayload = {
312
312
  model: payload.model,
313
313
  messages: [],
314
+ reasoning_effort: payload.reasoningEffort,
314
315
  tools: (_a = payload.functions) == null ? void 0 : _a.map((fn) => ({
315
316
  type: "function",
316
317
  function: fn
@@ -381,7 +382,7 @@ async function prepareOpenAIPayload(identifier, payload) {
381
382
  return preparedPayload;
382
383
  }
383
384
  async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs, requestTimeoutMs = 12e4, signal) {
384
- 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;
385
386
  const functionNames = openAiPayload.tools ? new Set(openAiPayload.tools.map((fn) => fn.function.name)) : null;
386
387
  const { endpoint, headers } = buildOpenAIRequestConfig(
387
388
  id,
@@ -399,7 +400,15 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs,
399
400
  const response = await fetch(endpoint, {
400
401
  method: "POST",
401
402
  headers,
402
- body: JSON.stringify({ ...openAiPayload, stream: true }),
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
+ }),
403
412
  // Merge (don't overwrite) the internal timeout controller with the caller's
404
413
  // cancellation signal so both an internal timeout and an external abort stop
405
414
  // the stream.
@@ -410,6 +419,7 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs,
410
419
  }
411
420
  let paragraph = "";
412
421
  let reasoning = "";
422
+ let streamUsage = null;
413
423
  const toolCallAccumulators = [];
414
424
  const reader = response.body.getReader();
415
425
  let partialChunk = "";
@@ -440,13 +450,15 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs,
440
450
  if (!jsonString) continue;
441
451
  if (jsonString.includes("[DONE]")) {
442
452
  clearTimeout(overallTimeout);
443
- return parseStreamedResponse(
453
+ const parsed = parseStreamedResponse(
444
454
  id,
445
455
  paragraph,
446
456
  toolCallAccumulators,
447
457
  functionNames,
448
458
  reasoning
449
459
  );
460
+ parsed.usage = streamUsage;
461
+ return parsed;
450
462
  }
451
463
  let json;
452
464
  try {
@@ -456,6 +468,15 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs,
456
468
  continue;
457
469
  }
458
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
+ }
459
480
  if (json.error) {
460
481
  logger_default.error(id, "Stream error from OpenAI:", json.error);
461
482
  const error2 = new Error("Stream error: OpenAI error");
@@ -468,23 +489,23 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs,
468
489
  }
469
490
  continue;
470
491
  }
471
- const toolCalls = (_c = (_b = json.choices[0]) == null ? void 0 : _b.delta) == null ? void 0 : _c.tool_calls;
492
+ const toolCalls = (_e = (_d = json.choices[0]) == null ? void 0 : _d.delta) == null ? void 0 : _e.tool_calls;
472
493
  if (toolCalls) {
473
494
  for (const toolCall of toolCalls) {
474
- const idx = (_d = toolCall.index) != null ? _d : 0;
495
+ const idx = (_f = toolCall.index) != null ? _f : 0;
475
496
  while (toolCallAccumulators.length <= idx) {
476
497
  toolCallAccumulators.push({ name: "", arguments: "" });
477
498
  }
478
499
  if (toolCall.id) toolCallAccumulators[idx].id = toolCall.id;
479
- if ((_e = toolCall.function) == null ? void 0 : _e.name)
500
+ if ((_g = toolCall.function) == null ? void 0 : _g.name)
480
501
  toolCallAccumulators[idx].name += toolCall.function.name;
481
- if ((_f = toolCall.function) == null ? void 0 : _f.arguments)
502
+ if ((_h = toolCall.function) == null ? void 0 : _h.arguments)
482
503
  toolCallAccumulators[idx].arguments += toolCall.function.arguments;
483
504
  }
484
505
  }
485
- const text = (_h = (_g = json.choices[0]) == null ? void 0 : _g.delta) == null ? void 0 : _h.content;
506
+ const text = (_j = (_i = json.choices[0]) == null ? void 0 : _i.delta) == null ? void 0 : _j.content;
486
507
  if (text) paragraph += text;
487
- const reasoningDelta = (_j = (_i = json.choices[0]) == null ? void 0 : _i.delta) == null ? void 0 : _j.reasoning;
508
+ const reasoningDelta = (_l = (_k = json.choices[0]) == null ? void 0 : _k.delta) == null ? void 0 : _l.reasoning;
488
509
  if (reasoningDelta) reasoning += reasoningDelta;
489
510
  }
490
511
  }
@@ -1197,7 +1218,7 @@ async function callGoogleAIWithRetries(id, payload, retries = 5, requestTimeoutM
1197
1218
  function normalizeMessageContent(content) {
1198
1219
  return Array.isArray(content) ? content.map((c) => c.type === "text" ? c.text : `[${c.type}]`).join("\n") : content;
1199
1220
  }
1200
- function prepareOpenAICompatMessages(messages) {
1221
+ function prepareOpenAICompatMessages(messages, opts = {}) {
1201
1222
  var _a;
1202
1223
  const out = [];
1203
1224
  for (const message of messages) {
@@ -1219,6 +1240,22 @@ function prepareOpenAICompatMessages(messages) {
1219
1240
  role: message.role,
1220
1241
  content
1221
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
+ }
1222
1259
  if ((_a = message.functionCalls) == null ? void 0 : _a.length) {
1223
1260
  outMessage.tool_calls = message.functionCalls.map((fc, i) => {
1224
1261
  var _a2;
@@ -1231,7 +1268,8 @@ function prepareOpenAICompatMessages(messages) {
1231
1268
  }
1232
1269
  };
1233
1270
  });
1234
- if (!content) outMessage.content = null;
1271
+ if (!content && !Array.isArray(outMessage.content))
1272
+ outMessage.content = null;
1235
1273
  }
1236
1274
  if (message.reasoning) outMessage.reasoning = message.reasoning;
1237
1275
  const reasoningDetails = filterOpenAICompatReasoningDetails(
@@ -1325,11 +1363,14 @@ async function callGroqWithRetries(id, payload, retries = 5, requestTimeoutMs =
1325
1363
  signal
1326
1364
  });
1327
1365
  }
1366
+ var openRouterImageRejectedModels = /* @__PURE__ */ new Set();
1328
1367
  function prepareOpenRouterPayload(payload) {
1329
1368
  var _a;
1330
1369
  return {
1331
1370
  model: payload.model,
1332
- messages: prepareOpenAICompatMessages(payload.messages),
1371
+ messages: prepareOpenAICompatMessages(payload.messages, {
1372
+ imageParts: !openRouterImageRejectedModels.has(String(payload.model))
1373
+ }),
1333
1374
  tools: (_a = payload.functions) == null ? void 0 : _a.map((fn) => ({
1334
1375
  type: "function",
1335
1376
  function: fn
@@ -1719,6 +1760,11 @@ function moderationEvictionSlug(error2, payload) {
1719
1760
  }
1720
1761
  var MIN_STREAM_ATTEMPT_MS = 1e4;
1721
1762
  var estimateTokens = (text) => Math.round(text.length / 4);
1763
+ function isImageInputRejection(error2) {
1764
+ var _a, _b, _c, _d;
1765
+ 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;
1766
+ return /no endpoints found.*image input/i.test(String((_d = body == null ? void 0 : body.message) != null ? _d : ""));
1767
+ }
1722
1768
  function streamAttemptBudgetMs(options) {
1723
1769
  if (options.streamDeadlineAt === void 0) return options.streamTimeoutMs;
1724
1770
  const remaining = options.streamDeadlineAt - Date.now();
@@ -1769,6 +1815,16 @@ async function callOpenRouterWithRetries(id, payload, retries = 5, options, sign
1769
1815
  `OpenRouter moderation eviction: ignoring provider "${slug}" for remaining attempts`
1770
1816
  );
1771
1817
  }
1818
+ if (isImageInputRejection(error2) && options.genericMessages) {
1819
+ openRouterImageRejectedModels.add(String(payload.model));
1820
+ payload.messages = prepareOpenAICompatMessages(
1821
+ options.genericMessages
1822
+ );
1823
+ logger_default.log(
1824
+ id,
1825
+ `OpenRouter: ${payload.model} has no image-capable endpoints; retrying with images as text refs`
1826
+ );
1827
+ }
1772
1828
  throw error2;
1773
1829
  }),
1774
1830
  { retries, signal }
@@ -1869,7 +1925,8 @@ async function callWithRetries(id, aiPayload, aiConfig, retries = 5, chunkTimeou
1869
1925
  streamTimeoutMs: (_c = aiPayload.streamTimeoutMs) != null ? _c : OPENROUTER_STREAM_TIMEOUT_MS,
1870
1926
  streamDeadlineAt: aiPayload.streamDeadlineAt,
1871
1927
  requestTimeoutMs: (_d = aiPayload.requestTimeoutMs) != null ? _d : OPENROUTER_NONSTREAM_TIMEOUT_MS,
1872
- chunkTimeoutMs
1928
+ chunkTimeoutMs,
1929
+ genericMessages: routingPayload.messages
1873
1930
  },
1874
1931
  signal
1875
1932
  );
@@ -1913,6 +1970,7 @@ export {
1913
1970
  OPENROUTER_STREAM_TIMEOUT_MS,
1914
1971
  OpenRouterModel,
1915
1972
  callWithRetries,
1973
+ openRouterImageRejectedModels,
1916
1974
  parseModelString
1917
1975
  };
1918
1976
  //# sourceMappingURL=index.mjs.map