190proof 1.0.97 → 1.0.99

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
@@ -354,7 +354,7 @@ async function prepareOpenAIPayload(identifier, payload) {
354
354
  }
355
355
  return preparedPayload;
356
356
  }
357
- async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs) {
357
+ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs, requestTimeoutMs = 12e4) {
358
358
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
359
359
  const functionNames = openAiPayload.tools ? new Set(openAiPayload.tools.map((fn) => fn.function.name)) : null;
360
360
  const { endpoint, headers } = buildOpenAIRequestConfig(
@@ -363,6 +363,13 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs)
363
363
  openAiConfig
364
364
  );
365
365
  const controller = new AbortController();
366
+ const overallTimeout = setTimeout(() => {
367
+ logger_default.error(id, `Request timeout after ${requestTimeoutMs}ms`);
368
+ controller.abort();
369
+ }, requestTimeoutMs);
370
+ if (typeof overallTimeout === "object" && "unref" in overallTimeout) {
371
+ overallTimeout.unref();
372
+ }
366
373
  const response = await fetch(endpoint, {
367
374
  method: "POST",
368
375
  headers,
@@ -404,6 +411,7 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs)
404
411
  if (!jsonString)
405
412
  continue;
406
413
  if (jsonString.includes("[DONE]")) {
414
+ clearTimeout(overallTimeout);
407
415
  return parseStreamedResponse(
408
416
  id,
409
417
  paragraph,
@@ -456,24 +464,32 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs)
456
464
  }
457
465
  }
458
466
  }
459
- async function callOpenAI(id, openAiPayload, openAiConfig) {
467
+ async function callOpenAI(id, openAiPayload, openAiConfig, requestTimeoutMs = 12e4) {
460
468
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
461
469
  const { endpoint, headers } = buildOpenAIRequestConfig(
462
470
  id,
463
471
  openAiPayload.model,
464
472
  openAiConfig
465
473
  );
466
- const response = await fetch(endpoint, {
467
- method: "POST",
468
- headers,
469
- body: JSON.stringify({ ...openAiPayload, stream: false })
470
- });
471
- if (!response.ok) {
472
- const errorData = await response.json();
473
- logger_default.error(id, "OpenAI API error:", errorData);
474
- throw new Error(`OpenAI API Error: ${errorData.error.message}`);
474
+ const controller = new AbortController();
475
+ const timer = setTimeout(() => controller.abort(), requestTimeoutMs);
476
+ let data;
477
+ try {
478
+ const response = await fetch(endpoint, {
479
+ method: "POST",
480
+ headers,
481
+ body: JSON.stringify({ ...openAiPayload, stream: false }),
482
+ signal: controller.signal
483
+ });
484
+ if (!response.ok) {
485
+ const errorData = await response.json();
486
+ logger_default.error(id, "OpenAI API error:", errorData);
487
+ throw new Error(`OpenAI API Error: ${errorData.error.message}`);
488
+ }
489
+ data = await response.json();
490
+ } finally {
491
+ clearTimeout(timer);
475
492
  }
476
- const data = await response.json();
477
493
  if (!((_a = data.choices) == null ? void 0 : _a.length)) {
478
494
  if (data.error) {
479
495
  logger_default.error(id, "OpenAI error:", data.error);
@@ -526,7 +542,7 @@ async function callOpenAI(id, openAiPayload, openAiConfig) {
526
542
  } : null
527
543
  };
528
544
  }
529
- async function callOpenAiWithRetries(id, openAiPayload, openAiConfig, retries = 5, chunkTimeoutMs = 15e3) {
545
+ async function callOpenAiWithRetries(id, openAiPayload, openAiConfig, retries = 5, chunkTimeoutMs = 15e3, requestTimeoutMs = 12e4) {
530
546
  logger_default.log(
531
547
  id,
532
548
  "Calling OpenAI API with retries:",
@@ -544,10 +560,11 @@ async function callOpenAiWithRetries(id, openAiPayload, openAiConfig, retries =
544
560
  id,
545
561
  openAiPayload,
546
562
  openAiConfig,
547
- chunkTimeoutMs
563
+ chunkTimeoutMs,
564
+ requestTimeoutMs
548
565
  );
549
566
  } else {
550
- return callOpenAI(id, openAiPayload, openAiConfig);
567
+ return callOpenAI(id, openAiPayload, openAiConfig, requestTimeoutMs);
551
568
  }
552
569
  },
553
570
  {
@@ -683,7 +700,7 @@ async function prepareAnthropicPayload(_identifier, payload) {
683
700
  }
684
701
  return preparedPayload;
685
702
  }
686
- async function callAnthropic(id, payload, config) {
703
+ async function callAnthropic(id, payload, config, requestTimeoutMs = 12e4) {
687
704
  var _a, _b, _c;
688
705
  const anthropicMessages = jigAnthropicMessages(payload.messages);
689
706
  const tools = (_a = payload.functions) == null ? void 0 : _a.map((f) => ({
@@ -733,7 +750,7 @@ async function callAnthropic(id, payload, config) {
733
750
  "anthropic-version": "2023-06-01",
734
751
  "anthropic-beta": "tools-2024-04-04"
735
752
  },
736
- timeout: 6e4
753
+ timeout: requestTimeoutMs
737
754
  }
738
755
  );
739
756
  data = response.data;
@@ -803,11 +820,11 @@ ${text}` : text;
803
820
  usage
804
821
  };
805
822
  }
806
- async function callAnthropicWithRetries(id, payload, config, retries = 5) {
823
+ async function callAnthropicWithRetries(id, payload, config, retries = 5, requestTimeoutMs = 12e4) {
807
824
  return withRetries(
808
825
  id,
809
826
  "Anthropic",
810
- () => callAnthropic(id, payload, config),
827
+ () => callAnthropic(id, payload, config, requestTimeoutMs),
811
828
  {
812
829
  retries
813
830
  }
@@ -927,7 +944,7 @@ async function prepareGoogleAIPayload(_identifier, payload) {
927
944
  }
928
945
  return preparedPayload;
929
946
  }
930
- async function callGoogleAI(id, payload) {
947
+ async function callGoogleAI(id, payload, requestTimeoutMs = 12e4) {
931
948
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
932
949
  const contents = jigGoogleMessages(payload.messages);
933
950
  const requestBody = {
@@ -951,7 +968,7 @@ async function callGoogleAI(id, payload) {
951
968
  "content-type": "application/json",
952
969
  "x-goog-api-key": process.env.GEMINI_API_KEY
953
970
  },
954
- timeout: 6e4
971
+ timeout: requestTimeoutMs
955
972
  }
956
973
  );
957
974
  response = httpResponse.data;
@@ -1050,9 +1067,9 @@ function removeImagesFromGooglePayload(payload) {
1050
1067
  }
1051
1068
  return removedImages;
1052
1069
  }
1053
- async function callGoogleAIWithRetries(id, payload, retries = 5) {
1070
+ async function callGoogleAIWithRetries(id, payload, retries = 5, requestTimeoutMs = 12e4) {
1054
1071
  let hasTriedWithoutImages = false;
1055
- return withRetries(id, "Google AI", () => callGoogleAI(id, payload), {
1072
+ return withRetries(id, "Google AI", () => callGoogleAI(id, payload, requestTimeoutMs), {
1056
1073
  retries,
1057
1074
  onError: (error2, attempt) => {
1058
1075
  var _a, _b;
@@ -1179,7 +1196,7 @@ function prepareGroqPayload(payload) {
1179
1196
  temperature: payload.temperature
1180
1197
  };
1181
1198
  }
1182
- async function callGroq(id, payload) {
1199
+ async function callGroq(id, payload, requestTimeoutMs = 12e4) {
1183
1200
  var _a, _b, _c, _d, _e, _f, _g;
1184
1201
  const response = await axios.post(
1185
1202
  "https://api.groq.com/openai/v1/chat/completions",
@@ -1188,7 +1205,8 @@ async function callGroq(id, payload) {
1188
1205
  headers: {
1189
1206
  "content-type": "application/json",
1190
1207
  Authorization: `Bearer ${process.env.GROQ_API_KEY}`
1191
- }
1208
+ },
1209
+ timeout: requestTimeoutMs
1192
1210
  }
1193
1211
  );
1194
1212
  if (response.data.error) {
@@ -1236,8 +1254,10 @@ async function callGroq(id, payload) {
1236
1254
  } : null
1237
1255
  };
1238
1256
  }
1239
- async function callGroqWithRetries(id, payload, retries = 5) {
1240
- return withRetries(id, "Groq", () => callGroq(id, payload), { retries });
1257
+ async function callGroqWithRetries(id, payload, retries = 5, requestTimeoutMs = 12e4) {
1258
+ return withRetries(id, "Groq", () => callGroq(id, payload, requestTimeoutMs), {
1259
+ retries
1260
+ });
1241
1261
  }
1242
1262
  function prepareOpenRouterPayload(payload) {
1243
1263
  var _a;
@@ -1253,7 +1273,7 @@ function prepareOpenRouterPayload(payload) {
1253
1273
  provider: payload.provider
1254
1274
  };
1255
1275
  }
1256
- async function callOpenRouter(id, payload) {
1276
+ async function callOpenRouter(id, payload, requestTimeoutMs = 12e4) {
1257
1277
  var _a, _b, _c, _d, _e, _f, _g, _h;
1258
1278
  const response = await axios.post(
1259
1279
  "https://openrouter.ai/api/v1/chat/completions",
@@ -1262,7 +1282,8 @@ async function callOpenRouter(id, payload) {
1262
1282
  headers: {
1263
1283
  "content-type": "application/json",
1264
1284
  Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`
1265
- }
1285
+ },
1286
+ timeout: requestTimeoutMs
1266
1287
  }
1267
1288
  );
1268
1289
  if (response.data.error) {
@@ -1311,8 +1332,13 @@ async function callOpenRouter(id, payload) {
1311
1332
  } : null
1312
1333
  };
1313
1334
  }
1314
- async function callOpenRouterWithRetries(id, payload, retries = 5) {
1315
- return withRetries(id, "OpenRouter", () => callOpenRouter(id, payload), { retries });
1335
+ async function callOpenRouterWithRetries(id, payload, retries = 5, requestTimeoutMs = 12e4) {
1336
+ return withRetries(
1337
+ id,
1338
+ "OpenRouter",
1339
+ () => callOpenRouter(id, payload, requestTimeoutMs),
1340
+ { retries }
1341
+ );
1316
1342
  }
1317
1343
  var VALID_PROVIDERS = ["openai", "anthropic", "google", "groq", "openrouter"];
1318
1344
  var ENUM_PROVIDER_MAP = [
@@ -1352,16 +1378,19 @@ function parseModelString(model) {
1352
1378
  );
1353
1379
  }
1354
1380
  async function callWithRetries(id, aiPayload, aiConfig, retries = 5, chunkTimeoutMs = 15e3) {
1381
+ var _a;
1355
1382
  try {
1356
1383
  const { provider, modelId } = parseModelString(aiPayload.model);
1357
1384
  const routingPayload = { ...aiPayload, model: modelId };
1385
+ const requestTimeoutMs = (_a = aiPayload.requestTimeoutMs) != null ? _a : 12e4;
1358
1386
  switch (provider) {
1359
1387
  case "anthropic":
1360
1388
  return await callAnthropicWithRetries(
1361
1389
  id,
1362
1390
  await prepareAnthropicPayload(id, routingPayload),
1363
1391
  aiConfig,
1364
- retries
1392
+ retries,
1393
+ requestTimeoutMs
1365
1394
  );
1366
1395
  case "openai":
1367
1396
  return await callOpenAiWithRetries(
@@ -1369,25 +1398,29 @@ async function callWithRetries(id, aiPayload, aiConfig, retries = 5, chunkTimeou
1369
1398
  await prepareOpenAIPayload(id, routingPayload),
1370
1399
  aiConfig,
1371
1400
  retries,
1372
- chunkTimeoutMs
1401
+ chunkTimeoutMs,
1402
+ requestTimeoutMs
1373
1403
  );
1374
1404
  case "groq":
1375
1405
  return await callGroqWithRetries(
1376
1406
  id,
1377
1407
  prepareGroqPayload(routingPayload),
1378
- retries
1408
+ retries,
1409
+ requestTimeoutMs
1379
1410
  );
1380
1411
  case "google":
1381
1412
  return await callGoogleAIWithRetries(
1382
1413
  id,
1383
1414
  await prepareGoogleAIPayload(id, routingPayload),
1384
- retries
1415
+ retries,
1416
+ requestTimeoutMs
1385
1417
  );
1386
1418
  case "openrouter":
1387
1419
  return await callOpenRouterWithRetries(
1388
1420
  id,
1389
1421
  prepareOpenRouterPayload(routingPayload),
1390
- retries
1422
+ retries,
1423
+ requestTimeoutMs
1391
1424
  );
1392
1425
  }
1393
1426
  } catch (error2) {