@cortexkit/anthropic-auth-core
Advanced tools
+47
-2
@@ -472,2 +472,31 @@ import { createHash, randomUUID } from 'node:crypto'; | ||
| } | ||
| function cachedQuotaWindowStillRelevant(window, now) { | ||
| if (!window) | ||
| return false; | ||
| if (!window.resetsAt) | ||
| return true; | ||
| const resetTime = Date.parse(window.resetsAt); | ||
| return !Number.isFinite(resetTime) || resetTime > now; | ||
| } | ||
| function cachedQuotaSnapshotStillRelevant(quota, now) { | ||
| return ['five_hour', 'seven_day'].every((key) => cachedQuotaWindowStillRelevant(quota?.[key], now)); | ||
| } | ||
| function isTransientQuotaError(error) { | ||
| const message = formatErrorMessage(error); | ||
| if (/Claude quota check failed: (429|5\d\d)\b/.test(message)) | ||
| return true; | ||
| if (!(error instanceof Error)) | ||
| return false; | ||
| const code = error.code; | ||
| return (message.includes('fetch failed') || | ||
| code === 'ECONNRESET' || | ||
| code === 'ECONNREFUSED' || | ||
| code === 'ETIMEDOUT' || | ||
| code === 'UND_ERR_CONNECT_TIMEOUT'); | ||
| } | ||
| function canUseCachedQuotaAfterRefreshError(account, storage, error, now) { | ||
| return (isTransientQuotaError(error) && | ||
| quotaSnapshotPassesPolicy(account.quota, storage) && | ||
| cachedQuotaSnapshotStillRelevant(account.quota, now)); | ||
| } | ||
| function clampPercent(value) { | ||
@@ -614,5 +643,13 @@ if (!Number.isFinite(value)) | ||
| } | ||
| catch { | ||
| if (!failClosedOnUnknownQuota(storage)) | ||
| catch (error) { | ||
| if (canUseCachedQuotaAfterRefreshError(account, storage, error, this.now())) { | ||
| log('[refresh] fallback quota using cached quota after refresh error', { | ||
| accountId: account.id, | ||
| error: formatErrorMessage(error), | ||
| }); | ||
| usable.push(account); | ||
| } | ||
| else if (!failClosedOnUnknownQuota(storage)) { | ||
| usable.push(account); | ||
| } | ||
| } | ||
@@ -731,2 +768,10 @@ } | ||
| } | ||
| if (!quotaIsStale(next, storage, this.now())) { | ||
| if (next.lastQuotaRefreshError) { | ||
| next.lastQuotaRefreshError = undefined; | ||
| updateStoredAccount(storage, next); | ||
| changed = true; | ||
| } | ||
| continue; | ||
| } | ||
| await this.refreshAccountQuota(next, storage); | ||
@@ -733,0 +778,0 @@ changed = true; |
+1
-1
| { | ||
| "name": "@cortexkit/anthropic-auth-core", | ||
| "version": "1.2.4", | ||
| "version": "1.2.5", | ||
| "type": "module", | ||
@@ -5,0 +5,0 @@ "repository": { |
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
158670
1.2%3908
1.16%