@blockrun/clawrouter 0.12.12 → 0.12.16

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.ts CHANGED
@@ -504,6 +504,7 @@ declare class SolanaBalanceMonitor {
504
504
  formatUSDC(amountMicros: bigint): string;
505
505
  getWalletAddress(): string;
506
506
  private fetchBalance;
507
+ private fetchBalanceOnce;
507
508
  private buildInfo;
508
509
  }
509
510
 
package/dist/index.js CHANGED
@@ -83,6 +83,14 @@ var init_solana_balance = __esm({
83
83
  async fetchBalance() {
84
84
  const owner = solAddress(this.walletAddress);
85
85
  const mint = solAddress(SOLANA_USDC_MINT);
86
+ for (let attempt = 0; attempt < 2; attempt++) {
87
+ const result = await this.fetchBalanceOnce(owner, mint);
88
+ if (result > 0n || attempt === 1) return result;
89
+ await new Promise((r) => setTimeout(r, 1e3));
90
+ }
91
+ return 0n;
92
+ }
93
+ async fetchBalanceOnce(owner, mint) {
86
94
  const controller = new AbortController();
87
95
  const timer = setTimeout(() => controller.abort(), BALANCE_TIMEOUT_MS);
88
96
  try {
@@ -1021,6 +1029,13 @@ function resolveModelAlias(model) {
1021
1029
  if (resolvedWithoutPrefix) return resolvedWithoutPrefix;
1022
1030
  return withoutPrefix;
1023
1031
  }
1032
+ if (normalized.startsWith("openai/")) {
1033
+ const withoutPrefix = normalized.slice("openai/".length);
1034
+ const resolvedWithoutPrefix = MODEL_ALIASES[withoutPrefix];
1035
+ if (resolvedWithoutPrefix) return resolvedWithoutPrefix;
1036
+ const isVirtualProfile = BLOCKRUN_MODELS.some((m) => m.id === withoutPrefix);
1037
+ if (isVirtualProfile) return withoutPrefix;
1038
+ }
1024
1039
  return model;
1025
1040
  }
1026
1041
  var BLOCKRUN_MODELS = [
@@ -1665,7 +1680,7 @@ function createPayFetchWithPreAuth(baseFetch, client, ttlMs = DEFAULT_TTL_MS, op
1665
1680
  const responseText = await Promise.race([
1666
1681
  response.text(),
1667
1682
  new Promise(
1668
- (_, reject) => setTimeout(() => reject(new Error("Body read timeout")), 6e4)
1683
+ (_, reject) => setTimeout(() => reject(new Error("Body read timeout")), 3e4)
1669
1684
  )
1670
1685
  ]);
1671
1686
  if (responseText) body = JSON.parse(responseText);
@@ -5471,23 +5486,27 @@ var HEALTH_CHECK_TIMEOUT_MS = 2e3;
5471
5486
  var RATE_LIMIT_COOLDOWN_MS = 6e4;
5472
5487
  var PORT_RETRY_ATTEMPTS = 5;
5473
5488
  var PORT_RETRY_DELAY_MS = 1e3;
5474
- var BODY_READ_TIMEOUT_MS = 6e4;
5475
- async function readBodyWithTimeout(body, timeoutMs = BODY_READ_TIMEOUT_MS) {
5489
+ var MODEL_BODY_READ_TIMEOUT_MS = 3e5;
5490
+ var ERROR_BODY_READ_TIMEOUT_MS = 3e4;
5491
+ async function readBodyWithTimeout(body, timeoutMs = MODEL_BODY_READ_TIMEOUT_MS) {
5476
5492
  if (!body) return [];
5477
5493
  const reader = body.getReader();
5478
5494
  const chunks = [];
5495
+ let timer;
5479
5496
  try {
5480
5497
  while (true) {
5481
5498
  const result = await Promise.race([
5482
5499
  reader.read(),
5483
- new Promise(
5484
- (_, reject) => setTimeout(() => reject(new Error("Body read timeout")), timeoutMs)
5485
- )
5500
+ new Promise((_, reject) => {
5501
+ timer = setTimeout(() => reject(new Error("Body read timeout")), timeoutMs);
5502
+ })
5486
5503
  ]);
5504
+ clearTimeout(timer);
5487
5505
  if (result.done) break;
5488
5506
  chunks.push(result.value);
5489
5507
  }
5490
5508
  } finally {
5509
+ clearTimeout(timer);
5491
5510
  reader.releaseLock();
5492
5511
  }
5493
5512
  return chunks;
@@ -5592,6 +5611,8 @@ function canWrite(res) {
5592
5611
  }
5593
5612
  function safeWrite(res, data) {
5594
5613
  if (!canWrite(res)) {
5614
+ const bytes = typeof data === "string" ? Buffer.byteLength(data) : data.length;
5615
+ console.warn(`[ClawRouter] safeWrite: socket not writable, dropping ${bytes} bytes`);
5595
5616
  return false;
5596
5617
  }
5597
5618
  return res.write(data);
@@ -5991,7 +6012,7 @@ async function proxyPartnerRequest(req, res, apiBase, payFetch) {
5991
6012
  });
5992
6013
  res.writeHead(upstream.status, responseHeaders);
5993
6014
  if (upstream.body) {
5994
- const chunks = await readBodyWithTimeout(upstream.body);
6015
+ const chunks = await readBodyWithTimeout(upstream.body, ERROR_BODY_READ_TIMEOUT_MS);
5995
6016
  for (const chunk of chunks) {
5996
6017
  safeWrite(res, Buffer.from(chunk));
5997
6018
  }
@@ -6441,7 +6462,7 @@ async function tryModelRequest(upstreamUrl, method, headers, body, modelId, maxT
6441
6462
  signal
6442
6463
  });
6443
6464
  if (response.status !== 200) {
6444
- const errorBodyChunks = await readBodyWithTimeout(response.body);
6465
+ const errorBodyChunks = await readBodyWithTimeout(response.body, ERROR_BODY_READ_TIMEOUT_MS);
6445
6466
  const errorBody = Buffer.concat(errorBodyChunks).toString();
6446
6467
  const isProviderErr = isProviderError(response.status, errorBody);
6447
6468
  return {
@@ -6454,7 +6475,7 @@ async function tryModelRequest(upstreamUrl, method, headers, body, modelId, maxT
6454
6475
  const contentType = response.headers.get("content-type") || "";
6455
6476
  if (contentType.includes("json") || contentType.includes("text")) {
6456
6477
  try {
6457
- const clonedChunks = await readBodyWithTimeout(response.clone().body);
6478
+ const clonedChunks = await readBodyWithTimeout(response.clone().body, ERROR_BODY_READ_TIMEOUT_MS);
6458
6479
  const responseBody = Buffer.concat(clonedChunks).toString();
6459
6480
  const degradedReason = detectDegradedSuccessResponse(responseBody);
6460
6481
  if (degradedReason) {
@@ -6839,9 +6860,9 @@ async function proxyRequest(req, res, apiBase, payFetch, options, routerOpts, de
6839
6860
  const normalizedModel = typeof parsed.model === "string" ? parsed.model.trim().toLowerCase() : "";
6840
6861
  const resolvedModel = resolveModelAlias(normalizedModel);
6841
6862
  const wasAlias = resolvedModel !== normalizedModel;
6842
- const isRoutingProfile = ROUTING_PROFILES.has(normalizedModel);
6863
+ const isRoutingProfile = ROUTING_PROFILES.has(normalizedModel) || ROUTING_PROFILES.has(resolvedModel);
6843
6864
  if (isRoutingProfile) {
6844
- const profileName = normalizedModel.replace("blockrun/", "");
6865
+ const profileName = resolvedModel.replace("blockrun/", "");
6845
6866
  routingProfile = profileName;
6846
6867
  }
6847
6868
  console.log(