190proof 1.0.105 → 1.0.107

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
@@ -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
 
@@ -115,6 +114,19 @@ function anySignal(signals) {
115
114
  signals
116
115
  );
117
116
  }
117
+ async function withRequestDeadline(apiName, requestTimeoutMs, signal, fn) {
118
+ const deadline = AbortSignal.timeout(requestTimeoutMs);
119
+ try {
120
+ return await fn(signal ? anySignal([signal, deadline]) : deadline);
121
+ } catch (error2) {
122
+ if (deadline.aborted && !(signal == null ? void 0 : signal.aborted)) {
123
+ throw new Error(
124
+ `${apiName} request exceeded hard deadline of ${requestTimeoutMs}ms`
125
+ );
126
+ }
127
+ throw error2;
128
+ }
129
+ }
118
130
  async function withRetries(identifier, apiName, fn, options = {}) {
119
131
  var _a, _b, _c, _d, _e;
120
132
  const { retries = 5, baseDelayMs = 125, onError } = options;
@@ -125,8 +137,7 @@ async function withRetries(identifier, apiName, fn, options = {}) {
125
137
  return await fn();
126
138
  } catch (error3) {
127
139
  lastError = error3;
128
- if ((_a = options.signal) == null ? void 0 : _a.aborted)
129
- throw error3;
140
+ if ((_a = options.signal) == null ? void 0 : _a.aborted) throw error3;
130
141
  if (onError) {
131
142
  onError(error3, attempt);
132
143
  } else {
@@ -150,8 +161,7 @@ function parseStreamedResponse(identifier, paragraph, toolCallAccumulators, allo
150
161
  const functionCalls = [];
151
162
  for (let i = 0; i < toolCallAccumulators.length; i++) {
152
163
  const acc = toolCallAccumulators[i];
153
- if (!acc.name || !acc.arguments)
154
- continue;
164
+ if (!acc.name || !acc.arguments) continue;
155
165
  if (allowedFunctionNames && !allowedFunctionNames.has(acc.name)) {
156
166
  throw new Error(
157
167
  `Stream error: received function call with unknown name: ${acc.name}`
@@ -290,8 +300,7 @@ function buildOpenAIRequestConfig(identifier, model, config) {
290
300
  };
291
301
  }
292
302
  function filterOpenAICompatReasoningDetails(details) {
293
- if (!Array.isArray(details))
294
- return details || void 0;
303
+ if (!Array.isArray(details)) return details || void 0;
295
304
  const blocks = details.filter(
296
305
  (block) => typeof (block == null ? void 0 : block.type) === "string" && block.type.startsWith("reasoning.")
297
306
  );
@@ -362,13 +371,11 @@ async function prepareOpenAIPayload(identifier, payload) {
362
371
  };
363
372
  });
364
373
  }
365
- if (message.reasoning)
366
- outMessage.reasoning = message.reasoning;
374
+ if (message.reasoning) outMessage.reasoning = message.reasoning;
367
375
  const reasoningDetails = filterOpenAICompatReasoningDetails(
368
376
  message.reasoningDetails
369
377
  );
370
- if (reasoningDetails)
371
- outMessage.reasoning_details = reasoningDetails;
378
+ if (reasoningDetails) outMessage.reasoning_details = reasoningDetails;
372
379
  preparedPayload.messages.push(outMessage);
373
380
  }
374
381
  return preparedPayload;
@@ -430,8 +437,7 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs,
430
437
  }
431
438
  const jsonStrings = chunk.split(/^data: /gm);
432
439
  for (const jsonString of jsonStrings) {
433
- if (!jsonString)
434
- continue;
440
+ if (!jsonString) continue;
435
441
  if (jsonString.includes("[DONE]")) {
436
442
  clearTimeout(overallTimeout);
437
443
  return parseStreamedResponse(
@@ -469,8 +475,7 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs,
469
475
  while (toolCallAccumulators.length <= idx) {
470
476
  toolCallAccumulators.push({ name: "", arguments: "" });
471
477
  }
472
- if (toolCall.id)
473
- toolCallAccumulators[idx].id = toolCall.id;
478
+ if (toolCall.id) toolCallAccumulators[idx].id = toolCall.id;
474
479
  if ((_e = toolCall.function) == null ? void 0 : _e.name)
475
480
  toolCallAccumulators[idx].name += toolCall.function.name;
476
481
  if ((_f = toolCall.function) == null ? void 0 : _f.arguments)
@@ -478,11 +483,9 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs,
478
483
  }
479
484
  }
480
485
  const text = (_h = (_g = json.choices[0]) == null ? void 0 : _g.delta) == null ? void 0 : _h.content;
481
- if (text)
482
- paragraph += text;
486
+ if (text) paragraph += text;
483
487
  const reasoningDelta = (_j = (_i = json.choices[0]) == null ? void 0 : _i.delta) == null ? void 0 : _j.reasoning;
484
- if (reasoningDelta)
485
- reasoning += reasoningDelta;
488
+ if (reasoningDelta) reasoning += reasoningDelta;
486
489
  }
487
490
  }
488
491
  }
@@ -627,8 +630,7 @@ function jigAnthropicMessages(messages) {
627
630
  ];
628
631
  }
629
632
  jiggedMessages = jiggedMessages.reduce((acc, message) => {
630
- if (acc.length === 0)
631
- return [message];
633
+ if (acc.length === 0) return [message];
632
634
  const lastMessage = acc[acc.length - 1];
633
635
  if (lastMessage.role === message.role) {
634
636
  const lastContent = Array.isArray(lastMessage.content) ? lastMessage.content : [{ type: "text", text: lastMessage.content }];
@@ -765,28 +767,33 @@ async function callAnthropic(id, payload, config, requestTimeoutMs = 12e4, signa
765
767
  ...tools.slice(0, -1),
766
768
  { ...tools[tools.length - 1], cache_control: { type: "ephemeral" } }
767
769
  ] : tools;
768
- const response = await axios.post(
769
- "https://api.anthropic.com/v1/messages",
770
- {
771
- model: payload.model,
772
- messages: anthropicMessages,
773
- tools: cachedTools,
774
- // tool_choice requires tools in the request; drop it otherwise.
775
- tool_choice: (cachedTools == null ? void 0 : cachedTools.length) ? payload.tool_choice : void 0,
776
- temperature: payload.temperature,
777
- system: payload.system,
778
- max_tokens: 4096
779
- },
780
- {
781
- headers: {
782
- "content-type": "application/json",
783
- "x-api-key": process.env.ANTHROPIC_API_KEY,
784
- "anthropic-version": "2023-06-01",
785
- "anthropic-beta": "tools-2024-04-04"
770
+ const response = await withRequestDeadline(
771
+ "Anthropic",
772
+ requestTimeoutMs,
773
+ signal,
774
+ (mergedSignal) => axios.post(
775
+ "https://api.anthropic.com/v1/messages",
776
+ {
777
+ model: payload.model,
778
+ messages: anthropicMessages,
779
+ tools: cachedTools,
780
+ // tool_choice requires tools in the request; drop it otherwise.
781
+ tool_choice: (cachedTools == null ? void 0 : cachedTools.length) ? payload.tool_choice : void 0,
782
+ temperature: payload.temperature,
783
+ system: payload.system,
784
+ max_tokens: 4096
786
785
  },
787
- timeout: requestTimeoutMs,
788
- signal
789
- }
786
+ {
787
+ headers: {
788
+ "content-type": "application/json",
789
+ "x-api-key": process.env.ANTHROPIC_API_KEY,
790
+ "anthropic-version": "2023-06-01",
791
+ "anthropic-beta": "tools-2024-04-04"
792
+ },
793
+ timeout: requestTimeoutMs,
794
+ signal: mergedSignal
795
+ }
796
+ )
790
797
  );
791
798
  data = response.data;
792
799
  }
@@ -877,8 +884,7 @@ function jigGoogleMessages(messages) {
877
884
  ];
878
885
  }
879
886
  jiggedMessages = jiggedMessages.reduce((acc, message) => {
880
- if (acc.length === 0)
881
- return [message];
887
+ if (acc.length === 0) return [message];
882
888
  const lastMessage = acc[acc.length - 1];
883
889
  if (lastMessage.role === message.role) {
884
890
  lastMessage.parts = [...lastMessage.parts, ...message.parts];
@@ -916,8 +922,7 @@ async function prepareGoogleAIPayload(_identifier, payload) {
916
922
  const toolNameById = /* @__PURE__ */ new Map();
917
923
  for (const m of payload.messages) {
918
924
  for (const fc of m.functionCalls || []) {
919
- if (fc.id)
920
- toolNameById.set(fc.id, fc.name);
925
+ if (fc.id) toolNameById.set(fc.id, fc.name);
921
926
  }
922
927
  }
923
928
  for (const message of payload.messages) {
@@ -994,8 +999,7 @@ async function callGoogleAI(id, payload, requestTimeoutMs = 12e4, signal) {
994
999
  contents,
995
1000
  generationConfig: { responseModalities: ["TEXT"] }
996
1001
  };
997
- if (payload.tools)
998
- requestBody.tools = [payload.tools];
1002
+ if (payload.tools) requestBody.tools = [payload.tools];
999
1003
  if (payload.tools && payload.toolConfig) {
1000
1004
  requestBody.toolConfig = payload.toolConfig;
1001
1005
  }
@@ -1006,17 +1010,22 @@ async function callGoogleAI(id, payload, requestTimeoutMs = 12e4, signal) {
1006
1010
  }
1007
1011
  let response;
1008
1012
  try {
1009
- const httpResponse = await axios.post(
1010
- `https://generativelanguage.googleapis.com/v1beta/models/${payload.model}:generateContent`,
1011
- requestBody,
1012
- {
1013
- headers: {
1014
- "content-type": "application/json",
1015
- "x-goog-api-key": process.env.GEMINI_API_KEY
1016
- },
1017
- timeout: requestTimeoutMs,
1018
- signal
1019
- }
1013
+ const httpResponse = await withRequestDeadline(
1014
+ "Google AI",
1015
+ requestTimeoutMs,
1016
+ signal,
1017
+ (mergedSignal) => axios.post(
1018
+ `https://generativelanguage.googleapis.com/v1beta/models/${payload.model}:generateContent`,
1019
+ requestBody,
1020
+ {
1021
+ headers: {
1022
+ "content-type": "application/json",
1023
+ "x-goog-api-key": process.env.GEMINI_API_KEY
1024
+ },
1025
+ timeout: requestTimeoutMs,
1026
+ signal: mergedSignal
1027
+ }
1028
+ )
1020
1029
  );
1021
1030
  response = httpResponse.data;
1022
1031
  } catch (err) {
@@ -1048,8 +1057,7 @@ async function callGoogleAI(id, payload, requestTimeoutMs = 12e4, signal) {
1048
1057
  });
1049
1058
  continue;
1050
1059
  }
1051
- if (part.text)
1052
- text += part.text;
1060
+ if (part.text) text += part.text;
1053
1061
  if ((_m = part.inlineData) == null ? void 0 : _m.data) {
1054
1062
  files.push({ mimeType: "image/png", data: part.inlineData.data });
1055
1063
  }
@@ -1126,21 +1134,15 @@ async function callGoogleAIWithRetries(id, payload, retries = 5, requestTimeoutM
1126
1134
  finishReason: error2.finishReason,
1127
1135
  modelVersion: error2.modelVersion
1128
1136
  };
1129
- if (error2.safetyRatings)
1130
- errorDetails.safetyRatings = error2.safetyRatings;
1131
- if (error2.usageMetadata)
1132
- errorDetails.usageMetadata = error2.usageMetadata;
1137
+ if (error2.safetyRatings) errorDetails.safetyRatings = error2.safetyRatings;
1138
+ if (error2.usageMetadata) errorDetails.usageMetadata = error2.usageMetadata;
1133
1139
  if (error2.promptFeedback)
1134
1140
  errorDetails.promptFeedback = error2.promptFeedback;
1135
- if (error2.status)
1136
- errorDetails.httpStatus = error2.status;
1137
- if (error2.code)
1138
- errorDetails.errorCode = error2.code;
1139
- if (error2.details)
1140
- errorDetails.errorDetails = error2.details;
1141
+ if (error2.status) errorDetails.httpStatus = error2.status;
1142
+ if (error2.code) errorDetails.errorCode = error2.code;
1143
+ if (error2.details) errorDetails.errorDetails = error2.details;
1141
1144
  const fileUris = payload.messages.flatMap((m) => m.parts).filter((p) => "fileData" in p).map((p) => p.fileData.fileUri);
1142
- if (fileUris.length)
1143
- errorDetails.fileUris = fileUris;
1145
+ if (fileUris.length) errorDetails.fileUris = fileUris;
1144
1146
  logger_default.error(
1145
1147
  id,
1146
1148
  `Retry #${attempt} error: ${error2.message}`,
@@ -1220,16 +1222,13 @@ function prepareOpenAICompatMessages(messages) {
1220
1222
  }
1221
1223
  };
1222
1224
  });
1223
- if (!message.content)
1224
- outMessage.content = null;
1225
+ if (!message.content) outMessage.content = null;
1225
1226
  }
1226
- if (message.reasoning)
1227
- outMessage.reasoning = message.reasoning;
1227
+ if (message.reasoning) outMessage.reasoning = message.reasoning;
1228
1228
  const reasoningDetails = filterOpenAICompatReasoningDetails(
1229
1229
  message.reasoningDetails
1230
1230
  );
1231
- if (reasoningDetails)
1232
- outMessage.reasoning_details = reasoningDetails;
1231
+ if (reasoningDetails) outMessage.reasoning_details = reasoningDetails;
1233
1232
  out.push(outMessage);
1234
1233
  }
1235
1234
  return out;
@@ -1249,17 +1248,22 @@ function prepareGroqPayload(payload) {
1249
1248
  }
1250
1249
  async function callGroq(id, payload, requestTimeoutMs = 12e4, signal) {
1251
1250
  var _a, _b, _c, _d, _e, _f, _g;
1252
- const response = await axios.post(
1253
- "https://api.groq.com/openai/v1/chat/completions",
1254
- payload,
1255
- {
1256
- headers: {
1257
- "content-type": "application/json",
1258
- Authorization: `Bearer ${process.env.GROQ_API_KEY}`
1259
- },
1260
- timeout: requestTimeoutMs,
1261
- signal
1262
- }
1251
+ const response = await withRequestDeadline(
1252
+ "Groq",
1253
+ requestTimeoutMs,
1254
+ signal,
1255
+ (mergedSignal) => axios.post(
1256
+ "https://api.groq.com/openai/v1/chat/completions",
1257
+ payload,
1258
+ {
1259
+ headers: {
1260
+ "content-type": "application/json",
1261
+ Authorization: `Bearer ${process.env.GROQ_API_KEY}`
1262
+ },
1263
+ timeout: requestTimeoutMs,
1264
+ signal: mergedSignal
1265
+ }
1266
+ )
1263
1267
  );
1264
1268
  if (response.data.error) {
1265
1269
  logger_default.error(id, "Groq error:", response.data.error);
@@ -1367,8 +1371,7 @@ function parseDsmlToolCalls(content) {
1367
1371
  let last = -1;
1368
1372
  let tag;
1369
1373
  while ((tag = DSML_ANY_TAG_RE.exec(content)) !== null) {
1370
- if (first === -1)
1371
- first = tag.index;
1374
+ if (first === -1) first = tag.index;
1372
1375
  last = tag.index + tag[0].length;
1373
1376
  }
1374
1377
  const remaining = first === -1 ? content : (content.slice(0, first) + content.slice(last)).trim();
@@ -1376,17 +1379,23 @@ function parseDsmlToolCalls(content) {
1376
1379
  }
1377
1380
  async function callOpenRouter(id, payload, requestTimeoutMs = 12e4, signal) {
1378
1381
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1379
- const response = await axios.post(
1380
- "https://openrouter.ai/api/v1/chat/completions",
1381
- payload,
1382
- {
1383
- headers: {
1384
- "content-type": "application/json",
1385
- Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`
1386
- },
1387
- timeout: requestTimeoutMs,
1388
- signal
1389
- }
1382
+ const response = await withRequestDeadline(
1383
+ "OpenRouter",
1384
+ requestTimeoutMs,
1385
+ signal,
1386
+ (mergedSignal) => axios.post(
1387
+ // Override point for tests (deadline behavior needs a local server).
1388
+ `${process.env.OPENROUTER_BASE_URL || "https://openrouter.ai"}/api/v1/chat/completions`,
1389
+ payload,
1390
+ {
1391
+ headers: {
1392
+ "content-type": "application/json",
1393
+ Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`
1394
+ },
1395
+ timeout: requestTimeoutMs,
1396
+ signal: mergedSignal
1397
+ }
1398
+ )
1390
1399
  );
1391
1400
  if (response.data.error) {
1392
1401
  logger_default.error(id, "OpenRouter error:", response.data.error);
@@ -1552,8 +1561,7 @@ async function callWithRetries(id, aiPayload, aiConfig, retries = 5, chunkTimeou
1552
1561
  (_b = result.provider) != null ? _b : result.provider = provider;
1553
1562
  return result;
1554
1563
  } catch (error2) {
1555
- if ((_c = aiPayload.signal) == null ? void 0 : _c.aborted)
1556
- throw error2;
1564
+ if ((_c = aiPayload.signal) == null ? void 0 : _c.aborted) throw error2;
1557
1565
  if (aiPayload.fallbackModel) {
1558
1566
  logger_default.error(
1559
1567
  id,