190proof 1.0.98 → 1.0.100

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.d.mts CHANGED
@@ -259,9 +259,10 @@ interface GenericPayload {
259
259
  provider?: OpenRouterProviderPreferences;
260
260
  /**
261
261
  * Per-request HTTP timeout in ms for the underlying provider call (applied
262
- * per attempt, not across retries). Currently honored by the Google adapter;
263
- * defaults to 60s when omitted. Raise it for slow, large generations (e.g.
264
- * single-file app codegen) so a long-but-valid response isn't cut short.
262
+ * per attempt, not across retries). Honored by all adapters (Anthropic,
263
+ * Google, OpenAI, OpenRouter, Groq); defaults to 120s when omitted. Raise it
264
+ * for slow, large generations (e.g. single-file app codegen) so a long-but-
265
+ * valid response isn't cut short.
265
266
  */
266
267
  requestTimeoutMs?: number;
267
268
  }
package/dist/index.d.ts CHANGED
@@ -259,9 +259,10 @@ interface GenericPayload {
259
259
  provider?: OpenRouterProviderPreferences;
260
260
  /**
261
261
  * Per-request HTTP timeout in ms for the underlying provider call (applied
262
- * per attempt, not across retries). Currently honored by the Google adapter;
263
- * defaults to 60s when omitted. Raise it for slow, large generations (e.g.
264
- * single-file app codegen) so a long-but-valid response isn't cut short.
262
+ * per attempt, not across retries). Honored by all adapters (Anthropic,
263
+ * Google, OpenAI, OpenRouter, Groq); defaults to 120s when omitted. Raise it
264
+ * for slow, large generations (e.g. single-file app codegen) so a long-but-
265
+ * valid response isn't cut short.
265
266
  */
266
267
  requestTimeoutMs?: number;
267
268
  }
package/dist/index.js CHANGED
@@ -142,7 +142,7 @@ function isHeicImage(name, mime) {
142
142
  var sharp = require("sharp");
143
143
  var decode = require("heic-decode");
144
144
  async function withRetries(identifier, apiName, fn, options = {}) {
145
- var _a;
145
+ var _a, _b, _c, _d;
146
146
  const { retries = 5, baseDelayMs = 125, onError } = options;
147
147
  logger_default.log(identifier, `Calling ${apiName} API with retries`);
148
148
  let lastError;
@@ -163,8 +163,9 @@ async function withRetries(identifier, apiName, fn, options = {}) {
163
163
  await timeout(baseDelayMs * attempt);
164
164
  }
165
165
  }
166
+ const detail = ((_d = (_c = (_b = lastError == null ? void 0 : lastError.response) == null ? void 0 : _b.data) == null ? void 0 : _c.error) == null ? void 0 : _d.message) || (lastError == null ? void 0 : lastError.message) || String(lastError);
166
167
  const error2 = new Error(
167
- `Failed to call ${apiName} API after ${retries} attempts`
168
+ `Failed to call ${apiName} API after ${retries} attempts: ${detail}`
168
169
  );
169
170
  error2.cause = lastError;
170
171
  throw error2;
@@ -385,7 +386,7 @@ async function prepareOpenAIPayload(identifier, payload) {
385
386
  }
386
387
  return preparedPayload;
387
388
  }
388
- async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs) {
389
+ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs, requestTimeoutMs = 12e4) {
389
390
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
390
391
  const functionNames = openAiPayload.tools ? new Set(openAiPayload.tools.map((fn) => fn.function.name)) : null;
391
392
  const { endpoint, headers } = buildOpenAIRequestConfig(
@@ -394,6 +395,13 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs)
394
395
  openAiConfig
395
396
  );
396
397
  const controller = new AbortController();
398
+ const overallTimeout = setTimeout(() => {
399
+ logger_default.error(id, `Request timeout after ${requestTimeoutMs}ms`);
400
+ controller.abort();
401
+ }, requestTimeoutMs);
402
+ if (typeof overallTimeout === "object" && "unref" in overallTimeout) {
403
+ overallTimeout.unref();
404
+ }
397
405
  const response = await fetch(endpoint, {
398
406
  method: "POST",
399
407
  headers,
@@ -435,6 +443,7 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs)
435
443
  if (!jsonString)
436
444
  continue;
437
445
  if (jsonString.includes("[DONE]")) {
446
+ clearTimeout(overallTimeout);
438
447
  return parseStreamedResponse(
439
448
  id,
440
449
  paragraph,
@@ -487,24 +496,32 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs)
487
496
  }
488
497
  }
489
498
  }
490
- async function callOpenAI(id, openAiPayload, openAiConfig) {
499
+ async function callOpenAI(id, openAiPayload, openAiConfig, requestTimeoutMs = 12e4) {
491
500
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
492
501
  const { endpoint, headers } = buildOpenAIRequestConfig(
493
502
  id,
494
503
  openAiPayload.model,
495
504
  openAiConfig
496
505
  );
497
- const response = await fetch(endpoint, {
498
- method: "POST",
499
- headers,
500
- body: JSON.stringify({ ...openAiPayload, stream: false })
501
- });
502
- if (!response.ok) {
503
- const errorData = await response.json();
504
- logger_default.error(id, "OpenAI API error:", errorData);
505
- throw new Error(`OpenAI API Error: ${errorData.error.message}`);
506
+ const controller = new AbortController();
507
+ const timer = setTimeout(() => controller.abort(), requestTimeoutMs);
508
+ let data;
509
+ try {
510
+ const response = await fetch(endpoint, {
511
+ method: "POST",
512
+ headers,
513
+ body: JSON.stringify({ ...openAiPayload, stream: false }),
514
+ signal: controller.signal
515
+ });
516
+ if (!response.ok) {
517
+ const errorData = await response.json();
518
+ logger_default.error(id, "OpenAI API error:", errorData);
519
+ throw new Error(`OpenAI API Error: ${errorData.error.message}`);
520
+ }
521
+ data = await response.json();
522
+ } finally {
523
+ clearTimeout(timer);
506
524
  }
507
- const data = await response.json();
508
525
  if (!((_a = data.choices) == null ? void 0 : _a.length)) {
509
526
  if (data.error) {
510
527
  logger_default.error(id, "OpenAI error:", data.error);
@@ -557,7 +574,7 @@ async function callOpenAI(id, openAiPayload, openAiConfig) {
557
574
  } : null
558
575
  };
559
576
  }
560
- async function callOpenAiWithRetries(id, openAiPayload, openAiConfig, retries = 5, chunkTimeoutMs = 15e3) {
577
+ async function callOpenAiWithRetries(id, openAiPayload, openAiConfig, retries = 5, chunkTimeoutMs = 15e3, requestTimeoutMs = 12e4) {
561
578
  logger_default.log(
562
579
  id,
563
580
  "Calling OpenAI API with retries:",
@@ -575,10 +592,11 @@ async function callOpenAiWithRetries(id, openAiPayload, openAiConfig, retries =
575
592
  id,
576
593
  openAiPayload,
577
594
  openAiConfig,
578
- chunkTimeoutMs
595
+ chunkTimeoutMs,
596
+ requestTimeoutMs
579
597
  );
580
598
  } else {
581
- return callOpenAI(id, openAiPayload, openAiConfig);
599
+ return callOpenAI(id, openAiPayload, openAiConfig, requestTimeoutMs);
582
600
  }
583
601
  },
584
602
  {
@@ -714,7 +732,7 @@ async function prepareAnthropicPayload(_identifier, payload) {
714
732
  }
715
733
  return preparedPayload;
716
734
  }
717
- async function callAnthropic(id, payload, config) {
735
+ async function callAnthropic(id, payload, config, requestTimeoutMs = 12e4) {
718
736
  var _a, _b, _c;
719
737
  const anthropicMessages = jigAnthropicMessages(payload.messages);
720
738
  const tools = (_a = payload.functions) == null ? void 0 : _a.map((f) => ({
@@ -764,7 +782,7 @@ async function callAnthropic(id, payload, config) {
764
782
  "anthropic-version": "2023-06-01",
765
783
  "anthropic-beta": "tools-2024-04-04"
766
784
  },
767
- timeout: 6e4
785
+ timeout: requestTimeoutMs
768
786
  }
769
787
  );
770
788
  data = response.data;
@@ -834,11 +852,11 @@ ${text}` : text;
834
852
  usage
835
853
  };
836
854
  }
837
- async function callAnthropicWithRetries(id, payload, config, retries = 5) {
855
+ async function callAnthropicWithRetries(id, payload, config, retries = 5, requestTimeoutMs = 12e4) {
838
856
  return withRetries(
839
857
  id,
840
858
  "Anthropic",
841
- () => callAnthropic(id, payload, config),
859
+ () => callAnthropic(id, payload, config, requestTimeoutMs),
842
860
  {
843
861
  retries
844
862
  }
@@ -874,7 +892,6 @@ async function prepareGoogleAIPayload(_identifier, payload) {
874
892
  const preparedPayload = {
875
893
  model: payload.model,
876
894
  messages: [],
877
- requestTimeoutMs: payload.requestTimeoutMs,
878
895
  tools: payload.functions ? {
879
896
  functionDeclarations: payload.functions.map((fn) => ({
880
897
  name: fn.name,
@@ -959,8 +976,8 @@ async function prepareGoogleAIPayload(_identifier, payload) {
959
976
  }
960
977
  return preparedPayload;
961
978
  }
962
- async function callGoogleAI(id, payload) {
963
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s;
979
+ async function callGoogleAI(id, payload, requestTimeoutMs = 12e4) {
980
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
964
981
  const contents = jigGoogleMessages(payload.messages);
965
982
  const requestBody = {
966
983
  contents,
@@ -983,50 +1000,47 @@ async function callGoogleAI(id, payload) {
983
1000
  "content-type": "application/json",
984
1001
  "x-goog-api-key": process.env.GEMINI_API_KEY
985
1002
  },
986
- // Per-attempt timeout. Defaults to 60s; callers pass a larger value via
987
- // payload.requestTimeoutMs for slow, large generations (e.g. single-file
988
- // app codegen) so a long-but-valid response isn't cut short.
989
- timeout: (_a = payload.requestTimeoutMs) != null ? _a : 6e4
1003
+ timeout: requestTimeoutMs
990
1004
  }
991
1005
  );
992
1006
  response = httpResponse.data;
993
1007
  } catch (err) {
994
- const apiError = (_c = (_b = err == null ? void 0 : err.response) == null ? void 0 : _b.data) == null ? void 0 : _c.error;
1008
+ const apiError = (_b = (_a = err == null ? void 0 : err.response) == null ? void 0 : _a.data) == null ? void 0 : _b.error;
995
1009
  const wrapped = new Error(
996
1010
  (apiError == null ? void 0 : apiError.message) || (err == null ? void 0 : err.message) || "Google AI API request failed"
997
1011
  );
998
- wrapped.status = (_e = apiError == null ? void 0 : apiError.status) != null ? _e : (_d = err == null ? void 0 : err.response) == null ? void 0 : _d.status;
1012
+ wrapped.status = (_d = apiError == null ? void 0 : apiError.status) != null ? _d : (_c = err == null ? void 0 : err.response) == null ? void 0 : _c.status;
999
1013
  wrapped.code = apiError == null ? void 0 : apiError.code;
1000
1014
  wrapped.details = apiError == null ? void 0 : apiError.details;
1001
- wrapped.promptFeedback = (_g = (_f = err == null ? void 0 : err.response) == null ? void 0 : _f.data) == null ? void 0 : _g.promptFeedback;
1015
+ wrapped.promptFeedback = (_f = (_e = err == null ? void 0 : err.response) == null ? void 0 : _e.data) == null ? void 0 : _f.promptFeedback;
1002
1016
  throw wrapped;
1003
1017
  }
1004
1018
  let text = "";
1005
1019
  const files = [];
1006
1020
  const reasoningParts = [];
1007
1021
  const functionCalls = [];
1008
- for (const part of ((_j = (_i = (_h = response.candidates) == null ? void 0 : _h[0]) == null ? void 0 : _i.content) == null ? void 0 : _j.parts) || []) {
1022
+ for (const part of ((_i = (_h = (_g = response.candidates) == null ? void 0 : _g[0]) == null ? void 0 : _h.content) == null ? void 0 : _i.parts) || []) {
1009
1023
  if (part.thought) {
1010
1024
  reasoningParts.push(part);
1011
1025
  continue;
1012
1026
  }
1013
1027
  if (part.functionCall) {
1014
1028
  functionCalls.push({
1015
- id: (_k = part.functionCall.id) != null ? _k : `call_${functionCalls.length}`,
1016
- name: (_l = part.functionCall.name) != null ? _l : "",
1017
- arguments: (_m = part.functionCall.args) != null ? _m : {},
1029
+ id: (_j = part.functionCall.id) != null ? _j : `call_${functionCalls.length}`,
1030
+ name: (_k = part.functionCall.name) != null ? _k : "",
1031
+ arguments: (_l = part.functionCall.args) != null ? _l : {},
1018
1032
  thoughtSignature: part.thoughtSignature
1019
1033
  });
1020
1034
  continue;
1021
1035
  }
1022
1036
  if (part.text)
1023
1037
  text += part.text;
1024
- if ((_n = part.inlineData) == null ? void 0 : _n.data) {
1038
+ if ((_m = part.inlineData) == null ? void 0 : _m.data) {
1025
1039
  files.push({ mimeType: "image/png", data: part.inlineData.data });
1026
1040
  }
1027
1041
  }
1028
1042
  if (!text && !functionCalls.length && !files.length) {
1029
- const candidate = (_o = response.candidates) == null ? void 0 : _o[0];
1043
+ const candidate = (_n = response.candidates) == null ? void 0 : _n[0];
1030
1044
  const finishReason = candidate == null ? void 0 : candidate.finishReason;
1031
1045
  logger_default.error(id, "Missing text & functions in Google AI API response:", {
1032
1046
  finishReason,
@@ -1061,10 +1075,10 @@ async function callGoogleAI(id, payload) {
1061
1075
  function_calls: functionCalls,
1062
1076
  reasoningDetails: reasoningParts.length ? reasoningParts : void 0,
1063
1077
  usage: response.usageMetadata ? {
1064
- prompt_tokens: (_p = response.usageMetadata.promptTokenCount) != null ? _p : 0,
1065
- completion_tokens: (_q = response.usageMetadata.candidatesTokenCount) != null ? _q : 0,
1066
- total_tokens: (_r = response.usageMetadata.totalTokenCount) != null ? _r : 0,
1067
- cached_tokens: (_s = response.usageMetadata.cachedContentTokenCount) != null ? _s : 0
1078
+ prompt_tokens: (_o = response.usageMetadata.promptTokenCount) != null ? _o : 0,
1079
+ completion_tokens: (_p = response.usageMetadata.candidatesTokenCount) != null ? _p : 0,
1080
+ total_tokens: (_q = response.usageMetadata.totalTokenCount) != null ? _q : 0,
1081
+ cached_tokens: (_r = response.usageMetadata.cachedContentTokenCount) != null ? _r : 0
1068
1082
  } : null
1069
1083
  };
1070
1084
  }
@@ -1085,9 +1099,9 @@ function removeImagesFromGooglePayload(payload) {
1085
1099
  }
1086
1100
  return removedImages;
1087
1101
  }
1088
- async function callGoogleAIWithRetries(id, payload, retries = 5) {
1102
+ async function callGoogleAIWithRetries(id, payload, retries = 5, requestTimeoutMs = 12e4) {
1089
1103
  let hasTriedWithoutImages = false;
1090
- return withRetries(id, "Google AI", () => callGoogleAI(id, payload), {
1104
+ return withRetries(id, "Google AI", () => callGoogleAI(id, payload, requestTimeoutMs), {
1091
1105
  retries,
1092
1106
  onError: (error2, attempt) => {
1093
1107
  var _a, _b;
@@ -1214,7 +1228,7 @@ function prepareGroqPayload(payload) {
1214
1228
  temperature: payload.temperature
1215
1229
  };
1216
1230
  }
1217
- async function callGroq(id, payload) {
1231
+ async function callGroq(id, payload, requestTimeoutMs = 12e4) {
1218
1232
  var _a, _b, _c, _d, _e, _f, _g;
1219
1233
  const response = await import_axios.default.post(
1220
1234
  "https://api.groq.com/openai/v1/chat/completions",
@@ -1223,7 +1237,8 @@ async function callGroq(id, payload) {
1223
1237
  headers: {
1224
1238
  "content-type": "application/json",
1225
1239
  Authorization: `Bearer ${process.env.GROQ_API_KEY}`
1226
- }
1240
+ },
1241
+ timeout: requestTimeoutMs
1227
1242
  }
1228
1243
  );
1229
1244
  if (response.data.error) {
@@ -1271,8 +1286,10 @@ async function callGroq(id, payload) {
1271
1286
  } : null
1272
1287
  };
1273
1288
  }
1274
- async function callGroqWithRetries(id, payload, retries = 5) {
1275
- return withRetries(id, "Groq", () => callGroq(id, payload), { retries });
1289
+ async function callGroqWithRetries(id, payload, retries = 5, requestTimeoutMs = 12e4) {
1290
+ return withRetries(id, "Groq", () => callGroq(id, payload, requestTimeoutMs), {
1291
+ retries
1292
+ });
1276
1293
  }
1277
1294
  function prepareOpenRouterPayload(payload) {
1278
1295
  var _a;
@@ -1288,7 +1305,7 @@ function prepareOpenRouterPayload(payload) {
1288
1305
  provider: payload.provider
1289
1306
  };
1290
1307
  }
1291
- async function callOpenRouter(id, payload) {
1308
+ async function callOpenRouter(id, payload, requestTimeoutMs = 12e4) {
1292
1309
  var _a, _b, _c, _d, _e, _f, _g, _h;
1293
1310
  const response = await import_axios.default.post(
1294
1311
  "https://openrouter.ai/api/v1/chat/completions",
@@ -1297,7 +1314,8 @@ async function callOpenRouter(id, payload) {
1297
1314
  headers: {
1298
1315
  "content-type": "application/json",
1299
1316
  Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`
1300
- }
1317
+ },
1318
+ timeout: requestTimeoutMs
1301
1319
  }
1302
1320
  );
1303
1321
  if (response.data.error) {
@@ -1346,8 +1364,13 @@ async function callOpenRouter(id, payload) {
1346
1364
  } : null
1347
1365
  };
1348
1366
  }
1349
- async function callOpenRouterWithRetries(id, payload, retries = 5) {
1350
- return withRetries(id, "OpenRouter", () => callOpenRouter(id, payload), { retries });
1367
+ async function callOpenRouterWithRetries(id, payload, retries = 5, requestTimeoutMs = 12e4) {
1368
+ return withRetries(
1369
+ id,
1370
+ "OpenRouter",
1371
+ () => callOpenRouter(id, payload, requestTimeoutMs),
1372
+ { retries }
1373
+ );
1351
1374
  }
1352
1375
  var VALID_PROVIDERS = ["openai", "anthropic", "google", "groq", "openrouter"];
1353
1376
  var ENUM_PROVIDER_MAP = [
@@ -1387,16 +1410,19 @@ function parseModelString(model) {
1387
1410
  );
1388
1411
  }
1389
1412
  async function callWithRetries(id, aiPayload, aiConfig, retries = 5, chunkTimeoutMs = 15e3) {
1413
+ var _a;
1390
1414
  try {
1391
1415
  const { provider, modelId } = parseModelString(aiPayload.model);
1392
1416
  const routingPayload = { ...aiPayload, model: modelId };
1417
+ const requestTimeoutMs = (_a = aiPayload.requestTimeoutMs) != null ? _a : 12e4;
1393
1418
  switch (provider) {
1394
1419
  case "anthropic":
1395
1420
  return await callAnthropicWithRetries(
1396
1421
  id,
1397
1422
  await prepareAnthropicPayload(id, routingPayload),
1398
1423
  aiConfig,
1399
- retries
1424
+ retries,
1425
+ requestTimeoutMs
1400
1426
  );
1401
1427
  case "openai":
1402
1428
  return await callOpenAiWithRetries(
@@ -1404,25 +1430,29 @@ async function callWithRetries(id, aiPayload, aiConfig, retries = 5, chunkTimeou
1404
1430
  await prepareOpenAIPayload(id, routingPayload),
1405
1431
  aiConfig,
1406
1432
  retries,
1407
- chunkTimeoutMs
1433
+ chunkTimeoutMs,
1434
+ requestTimeoutMs
1408
1435
  );
1409
1436
  case "groq":
1410
1437
  return await callGroqWithRetries(
1411
1438
  id,
1412
1439
  prepareGroqPayload(routingPayload),
1413
- retries
1440
+ retries,
1441
+ requestTimeoutMs
1414
1442
  );
1415
1443
  case "google":
1416
1444
  return await callGoogleAIWithRetries(
1417
1445
  id,
1418
1446
  await prepareGoogleAIPayload(id, routingPayload),
1419
- retries
1447
+ retries,
1448
+ requestTimeoutMs
1420
1449
  );
1421
1450
  case "openrouter":
1422
1451
  return await callOpenRouterWithRetries(
1423
1452
  id,
1424
1453
  prepareOpenRouterPayload(routingPayload),
1425
- retries
1454
+ retries,
1455
+ requestTimeoutMs
1426
1456
  );
1427
1457
  }
1428
1458
  } catch (error2) {