190proof 1.0.106 → 1.0.108

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
@@ -114,6 +114,19 @@ function anySignal(signals) {
114
114
  signals
115
115
  );
116
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
+ }
117
130
  async function withRetries(identifier, apiName, fn, options = {}) {
118
131
  var _a, _b, _c, _d, _e;
119
132
  const { retries = 5, baseDelayMs = 125, onError } = options;
@@ -754,28 +767,33 @@ async function callAnthropic(id, payload, config, requestTimeoutMs = 12e4, signa
754
767
  ...tools.slice(0, -1),
755
768
  { ...tools[tools.length - 1], cache_control: { type: "ephemeral" } }
756
769
  ] : tools;
757
- const response = await axios.post(
758
- "https://api.anthropic.com/v1/messages",
759
- {
760
- model: payload.model,
761
- messages: anthropicMessages,
762
- tools: cachedTools,
763
- // tool_choice requires tools in the request; drop it otherwise.
764
- tool_choice: (cachedTools == null ? void 0 : cachedTools.length) ? payload.tool_choice : void 0,
765
- temperature: payload.temperature,
766
- system: payload.system,
767
- max_tokens: 4096
768
- },
769
- {
770
- headers: {
771
- "content-type": "application/json",
772
- "x-api-key": process.env.ANTHROPIC_API_KEY,
773
- "anthropic-version": "2023-06-01",
774
- "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
775
785
  },
776
- timeout: requestTimeoutMs,
777
- signal
778
- }
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
+ )
779
797
  );
780
798
  data = response.data;
781
799
  }
@@ -992,17 +1010,22 @@ async function callGoogleAI(id, payload, requestTimeoutMs = 12e4, signal) {
992
1010
  }
993
1011
  let response;
994
1012
  try {
995
- const httpResponse = await axios.post(
996
- `https://generativelanguage.googleapis.com/v1beta/models/${payload.model}:generateContent`,
997
- requestBody,
998
- {
999
- headers: {
1000
- "content-type": "application/json",
1001
- "x-goog-api-key": process.env.GEMINI_API_KEY
1002
- },
1003
- timeout: requestTimeoutMs,
1004
- signal
1005
- }
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
+ )
1006
1029
  );
1007
1030
  response = httpResponse.data;
1008
1031
  } catch (err) {
@@ -1225,17 +1248,22 @@ function prepareGroqPayload(payload) {
1225
1248
  }
1226
1249
  async function callGroq(id, payload, requestTimeoutMs = 12e4, signal) {
1227
1250
  var _a, _b, _c, _d, _e, _f, _g;
1228
- const response = await axios.post(
1229
- "https://api.groq.com/openai/v1/chat/completions",
1230
- payload,
1231
- {
1232
- headers: {
1233
- "content-type": "application/json",
1234
- Authorization: `Bearer ${process.env.GROQ_API_KEY}`
1235
- },
1236
- timeout: requestTimeoutMs,
1237
- signal
1238
- }
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
+ )
1239
1267
  );
1240
1268
  if (response.data.error) {
1241
1269
  logger_default.error(id, "Groq error:", response.data.error);
@@ -1351,17 +1379,23 @@ function parseDsmlToolCalls(content) {
1351
1379
  }
1352
1380
  async function callOpenRouter(id, payload, requestTimeoutMs = 12e4, signal) {
1353
1381
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1354
- const response = await axios.post(
1355
- "https://openrouter.ai/api/v1/chat/completions",
1356
- payload,
1357
- {
1358
- headers: {
1359
- "content-type": "application/json",
1360
- Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`
1361
- },
1362
- timeout: requestTimeoutMs,
1363
- signal
1364
- }
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
+ )
1365
1399
  );
1366
1400
  if (response.data.error) {
1367
1401
  logger_default.error(id, "OpenRouter error:", response.data.error);