@blockrun/clawrouter 0.12.12 → 0.12.13

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
@@ -1665,7 +1665,7 @@ function createPayFetchWithPreAuth(baseFetch, client, ttlMs = DEFAULT_TTL_MS, op
1665
1665
  const responseText = await Promise.race([
1666
1666
  response.text(),
1667
1667
  new Promise(
1668
- (_, reject) => setTimeout(() => reject(new Error("Body read timeout")), 6e4)
1668
+ (_, reject) => setTimeout(() => reject(new Error("Body read timeout")), 3e4)
1669
1669
  )
1670
1670
  ]);
1671
1671
  if (responseText) body = JSON.parse(responseText);
@@ -5471,23 +5471,27 @@ var HEALTH_CHECK_TIMEOUT_MS = 2e3;
5471
5471
  var RATE_LIMIT_COOLDOWN_MS = 6e4;
5472
5472
  var PORT_RETRY_ATTEMPTS = 5;
5473
5473
  var PORT_RETRY_DELAY_MS = 1e3;
5474
- var BODY_READ_TIMEOUT_MS = 6e4;
5475
- async function readBodyWithTimeout(body, timeoutMs = BODY_READ_TIMEOUT_MS) {
5474
+ var MODEL_BODY_READ_TIMEOUT_MS = 3e5;
5475
+ var ERROR_BODY_READ_TIMEOUT_MS = 3e4;
5476
+ async function readBodyWithTimeout(body, timeoutMs = MODEL_BODY_READ_TIMEOUT_MS) {
5476
5477
  if (!body) return [];
5477
5478
  const reader = body.getReader();
5478
5479
  const chunks = [];
5480
+ let timer;
5479
5481
  try {
5480
5482
  while (true) {
5481
5483
  const result = await Promise.race([
5482
5484
  reader.read(),
5483
- new Promise(
5484
- (_, reject) => setTimeout(() => reject(new Error("Body read timeout")), timeoutMs)
5485
- )
5485
+ new Promise((_, reject) => {
5486
+ timer = setTimeout(() => reject(new Error("Body read timeout")), timeoutMs);
5487
+ })
5486
5488
  ]);
5489
+ clearTimeout(timer);
5487
5490
  if (result.done) break;
5488
5491
  chunks.push(result.value);
5489
5492
  }
5490
5493
  } finally {
5494
+ clearTimeout(timer);
5491
5495
  reader.releaseLock();
5492
5496
  }
5493
5497
  return chunks;
@@ -5592,6 +5596,8 @@ function canWrite(res) {
5592
5596
  }
5593
5597
  function safeWrite(res, data) {
5594
5598
  if (!canWrite(res)) {
5599
+ const bytes = typeof data === "string" ? Buffer.byteLength(data) : data.length;
5600
+ console.warn(`[ClawRouter] safeWrite: socket not writable, dropping ${bytes} bytes`);
5595
5601
  return false;
5596
5602
  }
5597
5603
  return res.write(data);
@@ -5991,7 +5997,7 @@ async function proxyPartnerRequest(req, res, apiBase, payFetch) {
5991
5997
  });
5992
5998
  res.writeHead(upstream.status, responseHeaders);
5993
5999
  if (upstream.body) {
5994
- const chunks = await readBodyWithTimeout(upstream.body);
6000
+ const chunks = await readBodyWithTimeout(upstream.body, ERROR_BODY_READ_TIMEOUT_MS);
5995
6001
  for (const chunk of chunks) {
5996
6002
  safeWrite(res, Buffer.from(chunk));
5997
6003
  }
@@ -6441,7 +6447,7 @@ async function tryModelRequest(upstreamUrl, method, headers, body, modelId, maxT
6441
6447
  signal
6442
6448
  });
6443
6449
  if (response.status !== 200) {
6444
- const errorBodyChunks = await readBodyWithTimeout(response.body);
6450
+ const errorBodyChunks = await readBodyWithTimeout(response.body, ERROR_BODY_READ_TIMEOUT_MS);
6445
6451
  const errorBody = Buffer.concat(errorBodyChunks).toString();
6446
6452
  const isProviderErr = isProviderError(response.status, errorBody);
6447
6453
  return {
@@ -6454,7 +6460,7 @@ async function tryModelRequest(upstreamUrl, method, headers, body, modelId, maxT
6454
6460
  const contentType = response.headers.get("content-type") || "";
6455
6461
  if (contentType.includes("json") || contentType.includes("text")) {
6456
6462
  try {
6457
- const clonedChunks = await readBodyWithTimeout(response.clone().body);
6463
+ const clonedChunks = await readBodyWithTimeout(response.clone().body, ERROR_BODY_READ_TIMEOUT_MS);
6458
6464
  const responseBody = Buffer.concat(clonedChunks).toString();
6459
6465
  const degradedReason = detectDegradedSuccessResponse(responseBody);
6460
6466
  if (degradedReason) {