(apiKey, modelKey = null)
| 900 | * back to a fresh cascade on a different account instead of queuing. |
| 901 | */ |
| 902 | export function acquireAccountByKey(apiKey, modelKey = null) { |
| 903 | const now = Date.now(); |
| 904 | const a = accounts.find(x => x.apiKey === apiKey); |
| 905 | if (!a) return null; |
| 906 | if (a.status !== 'active') return null; |
| 907 | if (isAccountInMaintenance(a)) return null; |
| 908 | if (isRateLimitedForModel(a, modelKey, now)) return null; |
| 909 | const limit = rpmLimitFor(a); |
| 910 | if (limit <= 0) return null; |
| 911 | const used = pruneRpmHistory(a, now); |
| 912 | if (used >= limit) return null; |
| 913 | if (modelKey && !isModelAllowedForAccount(a, modelKey)) return null; |
| 914 | const reservationTimestamp = nextReservationToken(now); |
| 915 | a._rpmHistory.push(reservationTimestamp); |
| 916 | a.lastUsed = now; |
| 917 | a._inflight = (a._inflight || 0) + 1; |
| 918 | a._inflightAt = now; |
| 919 | return { |
| 920 | id: a.id, email: a.email, apiKey: a.apiKey, |
| 921 | apiServerUrl: a.apiServerUrl || '', |
| 922 | proxy: getEffectiveProxy(a.id) || null, |
| 923 | reservationTimestamp, |
| 924 | }; |
| 925 | } |
| 926 | |
| 927 | /** |
| 928 | * Explain why a pinned account cannot be used right now. Used by strict |
no test coverage detected