@cortexkit/anthropic-auth-core 1.2.4 → 1.2.5

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.
Files changed (2) hide show
  1. package/dist/accounts.js +47 -2
  2. package/package.json +1 -1
package/dist/accounts.js CHANGED
@@ -469,6 +469,35 @@ function quotaIsStale(account, storage, now) {
469
469
  return !window || now - window.checkedAt >= maxAge;
470
470
  });
471
471
  }
472
+ function cachedQuotaWindowStillRelevant(window, now) {
473
+ if (!window)
474
+ return false;
475
+ if (!window.resetsAt)
476
+ return true;
477
+ const resetTime = Date.parse(window.resetsAt);
478
+ return !Number.isFinite(resetTime) || resetTime > now;
479
+ }
480
+ function cachedQuotaSnapshotStillRelevant(quota, now) {
481
+ return ['five_hour', 'seven_day'].every((key) => cachedQuotaWindowStillRelevant(quota?.[key], now));
482
+ }
483
+ function isTransientQuotaError(error) {
484
+ const message = formatErrorMessage(error);
485
+ if (/Claude quota check failed: (429|5\d\d)\b/.test(message))
486
+ return true;
487
+ if (!(error instanceof Error))
488
+ return false;
489
+ const code = error.code;
490
+ return (message.includes('fetch failed') ||
491
+ code === 'ECONNRESET' ||
492
+ code === 'ECONNREFUSED' ||
493
+ code === 'ETIMEDOUT' ||
494
+ code === 'UND_ERR_CONNECT_TIMEOUT');
495
+ }
496
+ function canUseCachedQuotaAfterRefreshError(account, storage, error, now) {
497
+ return (isTransientQuotaError(error) &&
498
+ quotaSnapshotPassesPolicy(account.quota, storage) &&
499
+ cachedQuotaSnapshotStillRelevant(account.quota, now));
500
+ }
472
501
  function clampPercent(value) {
473
502
  if (!Number.isFinite(value))
474
503
  return 0;
@@ -610,9 +639,17 @@ export class FallbackAccountManager {
610
639
  if (this.accountPassesQuotaPolicy(next, storage))
611
640
  usable.push(next);
612
641
  }
613
- catch {
614
- if (!failClosedOnUnknownQuota(storage))
642
+ catch (error) {
643
+ if (canUseCachedQuotaAfterRefreshError(account, storage, error, this.now())) {
644
+ log('[refresh] fallback quota using cached quota after refresh error', {
645
+ accountId: account.id,
646
+ error: formatErrorMessage(error),
647
+ });
648
+ usable.push(account);
649
+ }
650
+ else if (!failClosedOnUnknownQuota(storage)) {
615
651
  usable.push(account);
652
+ }
616
653
  }
617
654
  }
618
655
  if (changed)
@@ -726,6 +763,14 @@ export class FallbackAccountManager {
726
763
  next = await this.refreshAccount(next, storage);
727
764
  changed = true;
728
765
  }
766
+ if (!quotaIsStale(next, storage, this.now())) {
767
+ if (next.lastQuotaRefreshError) {
768
+ next.lastQuotaRefreshError = undefined;
769
+ updateStoredAccount(storage, next);
770
+ changed = true;
771
+ }
772
+ continue;
773
+ }
729
774
  await this.refreshAccountQuota(next, storage);
730
775
  changed = true;
731
776
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/anthropic-auth-core",
3
- "version": "1.2.4",
3
+ "version": "1.2.5",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",