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.js CHANGED
@@ -146,6 +146,19 @@ function anySignal(signals) {
146
146
  signals
147
147
  );
148
148
  }
149
+ async function withRequestDeadline(apiName, requestTimeoutMs, signal, fn) {
150
+ const deadline = AbortSignal.timeout(requestTimeoutMs);
151
+ try {
152
+ return await fn(signal ? anySignal([signal, deadline]) : deadline);
153
+ } catch (error2) {
154
+ if (deadline.aborted && !(signal == null ? void 0 : signal.aborted)) {
155
+ throw new Error(
156
+ `${apiName} request exceeded hard deadline of ${requestTimeoutMs}ms`
157
+ );
158
+ }
159
+ throw error2;
160
+ }
161
+ }
149
162
  async function withRetries(identifier, apiName, fn, options = {}) {
150
163
  var _a, _b, _c, _d, _e;
151
164
  const { retries = 5, baseDelayMs = 125, onError } = options;
@@ -786,28 +799,33 @@ async function callAnthropic(id, payload, config, requestTimeoutMs = 12e4, signa
786
799
  ...tools.slice(0, -1),
787
800
  { ...tools[tools.length - 1], cache_control: { type: "ephemeral" } }
788
801
  ] : tools;
789
- const response = await import_axios.default.post(
790
- "https://api.anthropic.com/v1/messages",
791
- {
792
- model: payload.model,
793
- messages: anthropicMessages,
794
- tools: cachedTools,
795
- // tool_choice requires tools in the request; drop it otherwise.
796
- tool_choice: (cachedTools == null ? void 0 : cachedTools.length) ? payload.tool_choice : void 0,
797
- temperature: payload.temperature,
798
- system: payload.system,
799
- max_tokens: 4096
800
- },
801
- {
802
- headers: {
803
- "content-type": "application/json",
804
- "x-api-key": process.env.ANTHROPIC_API_KEY,
805
- "anthropic-version": "2023-06-01",
806
- "anthropic-beta": "tools-2024-04-04"
802
+ const response = await withRequestDeadline(
803
+ "Anthropic",
804
+ requestTimeoutMs,
805
+ signal,
806
+ (mergedSignal) => import_axios.default.post(
807
+ "https://api.anthropic.com/v1/messages",
808
+ {
809
+ model: payload.model,
810
+ messages: anthropicMessages,
811
+ tools: cachedTools,
812
+ // tool_choice requires tools in the request; drop it otherwise.
813
+ tool_choice: (cachedTools == null ? void 0 : cachedTools.length) ? payload.tool_choice : void 0,
814
+ temperature: payload.temperature,
815
+ system: payload.system,
816
+ max_tokens: 4096
807
817
  },
808
- timeout: requestTimeoutMs,
809
- signal
810
- }
818
+ {
819
+ headers: {
820
+ "content-type": "application/json",
821
+ "x-api-key": process.env.ANTHROPIC_API_KEY,
822
+ "anthropic-version": "2023-06-01",
823
+ "anthropic-beta": "tools-2024-04-04"
824
+ },
825
+ timeout: requestTimeoutMs,
826
+ signal: mergedSignal
827
+ }
828
+ )
811
829
  );
812
830
  data = response.data;
813
831
  }
@@ -1024,17 +1042,22 @@ async function callGoogleAI(id, payload, requestTimeoutMs = 12e4, signal) {
1024
1042
  }
1025
1043
  let response;
1026
1044
  try {
1027
- const httpResponse = await import_axios.default.post(
1028
- `https://generativelanguage.googleapis.com/v1beta/models/${payload.model}:generateContent`,
1029
- requestBody,
1030
- {
1031
- headers: {
1032
- "content-type": "application/json",
1033
- "x-goog-api-key": process.env.GEMINI_API_KEY
1034
- },
1035
- timeout: requestTimeoutMs,
1036
- signal
1037
- }
1045
+ const httpResponse = await withRequestDeadline(
1046
+ "Google AI",
1047
+ requestTimeoutMs,
1048
+ signal,
1049
+ (mergedSignal) => import_axios.default.post(
1050
+ `https://generativelanguage.googleapis.com/v1beta/models/${payload.model}:generateContent`,
1051
+ requestBody,
1052
+ {
1053
+ headers: {
1054
+ "content-type": "application/json",
1055
+ "x-goog-api-key": process.env.GEMINI_API_KEY
1056
+ },
1057
+ timeout: requestTimeoutMs,
1058
+ signal: mergedSignal
1059
+ }
1060
+ )
1038
1061
  );
1039
1062
  response = httpResponse.data;
1040
1063
  } catch (err) {
@@ -1257,17 +1280,22 @@ function prepareGroqPayload(payload) {
1257
1280
  }
1258
1281
  async function callGroq(id, payload, requestTimeoutMs = 12e4, signal) {
1259
1282
  var _a, _b, _c, _d, _e, _f, _g;
1260
- const response = await import_axios.default.post(
1261
- "https://api.groq.com/openai/v1/chat/completions",
1262
- payload,
1263
- {
1264
- headers: {
1265
- "content-type": "application/json",
1266
- Authorization: `Bearer ${process.env.GROQ_API_KEY}`
1267
- },
1268
- timeout: requestTimeoutMs,
1269
- signal
1270
- }
1283
+ const response = await withRequestDeadline(
1284
+ "Groq",
1285
+ requestTimeoutMs,
1286
+ signal,
1287
+ (mergedSignal) => import_axios.default.post(
1288
+ "https://api.groq.com/openai/v1/chat/completions",
1289
+ payload,
1290
+ {
1291
+ headers: {
1292
+ "content-type": "application/json",
1293
+ Authorization: `Bearer ${process.env.GROQ_API_KEY}`
1294
+ },
1295
+ timeout: requestTimeoutMs,
1296
+ signal: mergedSignal
1297
+ }
1298
+ )
1271
1299
  );
1272
1300
  if (response.data.error) {
1273
1301
  logger_default.error(id, "Groq error:", response.data.error);
@@ -1383,17 +1411,23 @@ function parseDsmlToolCalls(content) {
1383
1411
  }
1384
1412
  async function callOpenRouter(id, payload, requestTimeoutMs = 12e4, signal) {
1385
1413
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1386
- const response = await import_axios.default.post(
1387
- "https://openrouter.ai/api/v1/chat/completions",
1388
- payload,
1389
- {
1390
- headers: {
1391
- "content-type": "application/json",
1392
- Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`
1393
- },
1394
- timeout: requestTimeoutMs,
1395
- signal
1396
- }
1414
+ const response = await withRequestDeadline(
1415
+ "OpenRouter",
1416
+ requestTimeoutMs,
1417
+ signal,
1418
+ (mergedSignal) => import_axios.default.post(
1419
+ // Override point for tests (deadline behavior needs a local server).
1420
+ `${process.env.OPENROUTER_BASE_URL || "https://openrouter.ai"}/api/v1/chat/completions`,
1421
+ payload,
1422
+ {
1423
+ headers: {
1424
+ "content-type": "application/json",
1425
+ Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`
1426
+ },
1427
+ timeout: requestTimeoutMs,
1428
+ signal: mergedSignal
1429
+ }
1430
+ )
1397
1431
  );
1398
1432
  if (response.data.error) {
1399
1433
  logger_default.error(id, "OpenRouter error:", response.data.error);