190proof 1.0.98 → 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
  }
@@ -843,7 +860,6 @@ async function prepareGoogleAIPayload(_identifier, payload) {
843
860
  const preparedPayload = {
844
861
  model: payload.model,
845
862
  messages: [],
846
- requestTimeoutMs: payload.requestTimeoutMs,
847
863
  tools: payload.functions ? {
848
864
  functionDeclarations: payload.functions.map((fn) => ({
849
865
  name: fn.name,
@@ -928,8 +944,8 @@ async function prepareGoogleAIPayload(_identifier, payload) {
928
944
  }
929
945
  return preparedPayload;
930
946
  }
931
- async function callGoogleAI(id, payload) {
932
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s;
947
+ async function callGoogleAI(id, payload, requestTimeoutMs = 12e4) {
948
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
933
949
  const contents = jigGoogleMessages(payload.messages);
934
950
  const requestBody = {
935
951
  contents,
@@ -952,50 +968,47 @@ async function callGoogleAI(id, payload) {
952
968
  "content-type": "application/json",
953
969
  "x-goog-api-key": process.env.GEMINI_API_KEY
954
970
  },
955
- // Per-attempt timeout. Defaults to 60s; callers pass a larger value via
956
- // payload.requestTimeoutMs for slow, large generations (e.g. single-file
957
- // app codegen) so a long-but-valid response isn't cut short.
958
- timeout: (_a = payload.requestTimeoutMs) != null ? _a : 6e4
971
+ timeout: requestTimeoutMs
959
972
  }
960
973
  );
961
974
  response = httpResponse.data;
962
975
  } catch (err) {
963
- const apiError = (_c = (_b = err == null ? void 0 : err.response) == null ? void 0 : _b.data) == null ? void 0 : _c.error;
976
+ const apiError = (_b = (_a = err == null ? void 0 : err.response) == null ? void 0 : _a.data) == null ? void 0 : _b.error;
964
977
  const wrapped = new Error(
965
978
  (apiError == null ? void 0 : apiError.message) || (err == null ? void 0 : err.message) || "Google AI API request failed"
966
979
  );
967
- wrapped.status = (_e = apiError == null ? void 0 : apiError.status) != null ? _e : (_d = err == null ? void 0 : err.response) == null ? void 0 : _d.status;
980
+ wrapped.status = (_d = apiError == null ? void 0 : apiError.status) != null ? _d : (_c = err == null ? void 0 : err.response) == null ? void 0 : _c.status;
968
981
  wrapped.code = apiError == null ? void 0 : apiError.code;
969
982
  wrapped.details = apiError == null ? void 0 : apiError.details;
970
- wrapped.promptFeedback = (_g = (_f = err == null ? void 0 : err.response) == null ? void 0 : _f.data) == null ? void 0 : _g.promptFeedback;
983
+ wrapped.promptFeedback = (_f = (_e = err == null ? void 0 : err.response) == null ? void 0 : _e.data) == null ? void 0 : _f.promptFeedback;
971
984
  throw wrapped;
972
985
  }
973
986
  let text = "";
974
987
  const files = [];
975
988
  const reasoningParts = [];
976
989
  const functionCalls = [];
977
- for (const part of ((_j = (_i = (_h = response.candidates) == null ? void 0 : _h[0]) == null ? void 0 : _i.content) == null ? void 0 : _j.parts) || []) {
990
+ for (const part of ((_i = (_h = (_g = response.candidates) == null ? void 0 : _g[0]) == null ? void 0 : _h.content) == null ? void 0 : _i.parts) || []) {
978
991
  if (part.thought) {
979
992
  reasoningParts.push(part);
980
993
  continue;
981
994
  }
982
995
  if (part.functionCall) {
983
996
  functionCalls.push({
984
- id: (_k = part.functionCall.id) != null ? _k : `call_${functionCalls.length}`,
985
- name: (_l = part.functionCall.name) != null ? _l : "",
986
- arguments: (_m = part.functionCall.args) != null ? _m : {},
997
+ id: (_j = part.functionCall.id) != null ? _j : `call_${functionCalls.length}`,
998
+ name: (_k = part.functionCall.name) != null ? _k : "",
999
+ arguments: (_l = part.functionCall.args) != null ? _l : {},
987
1000
  thoughtSignature: part.thoughtSignature
988
1001
  });
989
1002
  continue;
990
1003
  }
991
1004
  if (part.text)
992
1005
  text += part.text;
993
- if ((_n = part.inlineData) == null ? void 0 : _n.data) {
1006
+ if ((_m = part.inlineData) == null ? void 0 : _m.data) {
994
1007
  files.push({ mimeType: "image/png", data: part.inlineData.data });
995
1008
  }
996
1009
  }
997
1010
  if (!text && !functionCalls.length && !files.length) {
998
- const candidate = (_o = response.candidates) == null ? void 0 : _o[0];
1011
+ const candidate = (_n = response.candidates) == null ? void 0 : _n[0];
999
1012
  const finishReason = candidate == null ? void 0 : candidate.finishReason;
1000
1013
  logger_default.error(id, "Missing text & functions in Google AI API response:", {
1001
1014
  finishReason,
@@ -1030,10 +1043,10 @@ async function callGoogleAI(id, payload) {
1030
1043
  function_calls: functionCalls,
1031
1044
  reasoningDetails: reasoningParts.length ? reasoningParts : void 0,
1032
1045
  usage: response.usageMetadata ? {
1033
- prompt_tokens: (_p = response.usageMetadata.promptTokenCount) != null ? _p : 0,
1034
- completion_tokens: (_q = response.usageMetadata.candidatesTokenCount) != null ? _q : 0,
1035
- total_tokens: (_r = response.usageMetadata.totalTokenCount) != null ? _r : 0,
1036
- cached_tokens: (_s = response.usageMetadata.cachedContentTokenCount) != null ? _s : 0
1046
+ prompt_tokens: (_o = response.usageMetadata.promptTokenCount) != null ? _o : 0,
1047
+ completion_tokens: (_p = response.usageMetadata.candidatesTokenCount) != null ? _p : 0,
1048
+ total_tokens: (_q = response.usageMetadata.totalTokenCount) != null ? _q : 0,
1049
+ cached_tokens: (_r = response.usageMetadata.cachedContentTokenCount) != null ? _r : 0
1037
1050
  } : null
1038
1051
  };
1039
1052
  }
@@ -1054,9 +1067,9 @@ function removeImagesFromGooglePayload(payload) {
1054
1067
  }
1055
1068
  return removedImages;
1056
1069
  }
1057
- async function callGoogleAIWithRetries(id, payload, retries = 5) {
1070
+ async function callGoogleAIWithRetries(id, payload, retries = 5, requestTimeoutMs = 12e4) {
1058
1071
  let hasTriedWithoutImages = false;
1059
- return withRetries(id, "Google AI", () => callGoogleAI(id, payload), {
1072
+ return withRetries(id, "Google AI", () => callGoogleAI(id, payload, requestTimeoutMs), {
1060
1073
  retries,
1061
1074
  onError: (error2, attempt) => {
1062
1075
  var _a, _b;
@@ -1183,7 +1196,7 @@ function prepareGroqPayload(payload) {
1183
1196
  temperature: payload.temperature
1184
1197
  };
1185
1198
  }
1186
- async function callGroq(id, payload) {
1199
+ async function callGroq(id, payload, requestTimeoutMs = 12e4) {
1187
1200
  var _a, _b, _c, _d, _e, _f, _g;
1188
1201
  const response = await axios.post(
1189
1202
  "https://api.groq.com/openai/v1/chat/completions",
@@ -1192,7 +1205,8 @@ async function callGroq(id, payload) {
1192
1205
  headers: {
1193
1206
  "content-type": "application/json",
1194
1207
  Authorization: `Bearer ${process.env.GROQ_API_KEY}`
1195
- }
1208
+ },
1209
+ timeout: requestTimeoutMs
1196
1210
  }
1197
1211
  );
1198
1212
  if (response.data.error) {
@@ -1240,8 +1254,10 @@ async function callGroq(id, payload) {
1240
1254
  } : null
1241
1255
  };
1242
1256
  }
1243
- async function callGroqWithRetries(id, payload, retries = 5) {
1244
- 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
+ });
1245
1261
  }
1246
1262
  function prepareOpenRouterPayload(payload) {
1247
1263
  var _a;
@@ -1257,7 +1273,7 @@ function prepareOpenRouterPayload(payload) {
1257
1273
  provider: payload.provider
1258
1274
  };
1259
1275
  }
1260
- async function callOpenRouter(id, payload) {
1276
+ async function callOpenRouter(id, payload, requestTimeoutMs = 12e4) {
1261
1277
  var _a, _b, _c, _d, _e, _f, _g, _h;
1262
1278
  const response = await axios.post(
1263
1279
  "https://openrouter.ai/api/v1/chat/completions",
@@ -1266,7 +1282,8 @@ async function callOpenRouter(id, payload) {
1266
1282
  headers: {
1267
1283
  "content-type": "application/json",
1268
1284
  Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`
1269
- }
1285
+ },
1286
+ timeout: requestTimeoutMs
1270
1287
  }
1271
1288
  );
1272
1289
  if (response.data.error) {
@@ -1315,8 +1332,13 @@ async function callOpenRouter(id, payload) {
1315
1332
  } : null
1316
1333
  };
1317
1334
  }
1318
- async function callOpenRouterWithRetries(id, payload, retries = 5) {
1319
- 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
+ );
1320
1342
  }
1321
1343
  var VALID_PROVIDERS = ["openai", "anthropic", "google", "groq", "openrouter"];
1322
1344
  var ENUM_PROVIDER_MAP = [
@@ -1356,16 +1378,19 @@ function parseModelString(model) {
1356
1378
  );
1357
1379
  }
1358
1380
  async function callWithRetries(id, aiPayload, aiConfig, retries = 5, chunkTimeoutMs = 15e3) {
1381
+ var _a;
1359
1382
  try {
1360
1383
  const { provider, modelId } = parseModelString(aiPayload.model);
1361
1384
  const routingPayload = { ...aiPayload, model: modelId };
1385
+ const requestTimeoutMs = (_a = aiPayload.requestTimeoutMs) != null ? _a : 12e4;
1362
1386
  switch (provider) {
1363
1387
  case "anthropic":
1364
1388
  return await callAnthropicWithRetries(
1365
1389
  id,
1366
1390
  await prepareAnthropicPayload(id, routingPayload),
1367
1391
  aiConfig,
1368
- retries
1392
+ retries,
1393
+ requestTimeoutMs
1369
1394
  );
1370
1395
  case "openai":
1371
1396
  return await callOpenAiWithRetries(
@@ -1373,25 +1398,29 @@ async function callWithRetries(id, aiPayload, aiConfig, retries = 5, chunkTimeou
1373
1398
  await prepareOpenAIPayload(id, routingPayload),
1374
1399
  aiConfig,
1375
1400
  retries,
1376
- chunkTimeoutMs
1401
+ chunkTimeoutMs,
1402
+ requestTimeoutMs
1377
1403
  );
1378
1404
  case "groq":
1379
1405
  return await callGroqWithRetries(
1380
1406
  id,
1381
1407
  prepareGroqPayload(routingPayload),
1382
- retries
1408
+ retries,
1409
+ requestTimeoutMs
1383
1410
  );
1384
1411
  case "google":
1385
1412
  return await callGoogleAIWithRetries(
1386
1413
  id,
1387
1414
  await prepareGoogleAIPayload(id, routingPayload),
1388
- retries
1415
+ retries,
1416
+ requestTimeoutMs
1389
1417
  );
1390
1418
  case "openrouter":
1391
1419
  return await callOpenRouterWithRetries(
1392
1420
  id,
1393
1421
  prepareOpenRouterPayload(routingPayload),
1394
- retries
1422
+ retries,
1423
+ requestTimeoutMs
1395
1424
  );
1396
1425
  }
1397
1426
  } catch (error2) {