55 parseRefreshParts ,
66 formatRefreshParts ,
77} from "./auth" ;
8- import { saveAccounts , type AccountStorage , type RateLimitState , type ModelFamily } from "./storage" ;
8+ import { saveAccounts , type AccountStorage , type RateLimitState , type ModelFamily , type AccountTier } from "./storage" ;
99
10- export type { ModelFamily } from "./storage" ;
10+ export type { ModelFamily , AccountTier } from "./storage" ;
1111
1212export interface ManagedAccount {
1313 index : number ;
@@ -17,6 +17,7 @@ export interface ManagedAccount {
1717 rateLimitResetTimes : RateLimitState ;
1818 lastUsed : number ;
1919 email ?: string ;
20+ tier ?: AccountTier ;
2021 lastSwitchReason ?: "rate-limit" | "initial" | "rotation" ;
2122}
2223
@@ -30,8 +31,11 @@ function clearExpiredRateLimits(account: ManagedAccount): void {
3031 if ( account . rateLimitResetTimes . claude !== undefined && now >= account . rateLimitResetTimes . claude ) {
3132 delete account . rateLimitResetTimes . claude ;
3233 }
33- if ( account . rateLimitResetTimes . gemini !== undefined && now >= account . rateLimitResetTimes . gemini ) {
34- delete account . rateLimitResetTimes . gemini ;
34+ if ( account . rateLimitResetTimes [ "gemini-flash" ] !== undefined && now >= account . rateLimitResetTimes [ "gemini-flash" ] ) {
35+ delete account . rateLimitResetTimes [ "gemini-flash" ] ;
36+ }
37+ if ( account . rateLimitResetTimes [ "gemini-pro" ] !== undefined && now >= account . rateLimitResetTimes [ "gemini-pro" ] ) {
38+ delete account . rateLimitResetTimes [ "gemini-pro" ] ;
3539 }
3640}
3741
@@ -67,6 +71,7 @@ export class AccountManager {
6771 rateLimitResetTimes : acc . rateLimitResetTimes ?? { } ,
6872 lastUsed : acc . lastUsed ,
6973 email : acc . email ,
74+ tier : acc . tier ,
7075 lastSwitchReason : acc . lastSwitchReason ,
7176 } ) ) ;
7277 } else {
@@ -99,9 +104,10 @@ export class AccountManager {
99104
100105 async save ( ) : Promise < void > {
101106 const storage : AccountStorage = {
102- version : 2 ,
107+ version : 3 ,
103108 accounts : this . accounts . map ( ( acc ) => ( {
104109 email : acc . email ,
110+ tier : acc . tier ,
105111 refreshToken : acc . parts . refreshToken ,
106112 projectId : acc . parts . projectId ,
107113 managedProjectId : acc . parts . managedProjectId ,
@@ -133,12 +139,19 @@ export class AccountManager {
133139 }
134140
135141 getCurrentOrNextForFamily ( family : ModelFamily ) : ManagedAccount | null {
142+ this . accounts . forEach ( clearExpiredRateLimits ) ;
143+
136144 const current = this . getCurrentAccount ( ) ;
137145 if ( current ) {
138- clearExpiredRateLimits ( current ) ;
139146 if ( ! isRateLimitedForFamily ( current , family ) ) {
140- current . lastUsed = Date . now ( ) ;
141- return current ;
147+ const betterTierAvailable =
148+ current . tier !== "paid" &&
149+ this . accounts . some ( ( a ) => a . tier === "paid" && ! isRateLimitedForFamily ( a , family ) ) ;
150+
151+ if ( ! betterTierAvailable ) {
152+ current . lastUsed = Date . now ( ) ;
153+ return current ;
154+ }
142155 }
143156 }
144157
@@ -150,16 +163,17 @@ export class AccountManager {
150163 }
151164
152165 getNextForFamily ( family : ModelFamily ) : ManagedAccount | null {
153- const available = this . accounts . filter ( ( a ) => {
154- clearExpiredRateLimits ( a ) ;
155- return ! isRateLimitedForFamily ( a , family ) ;
156- } ) ;
166+ const available = this . accounts . filter ( ( a ) => ! isRateLimitedForFamily ( a , family ) ) ;
157167
158168 if ( available . length === 0 ) {
159169 return null ;
160170 }
161171
162- const account = available [ this . currentIndex % available . length ] ;
172+ // Prioritize paid accounts
173+ const paidAvailable = available . filter ( ( a ) => a . tier === "paid" ) ;
174+ const pool = paidAvailable . length > 0 ? paidAvailable : available ;
175+
176+ const account = pool [ this . currentIndex % pool . length ] ;
163177 if ( ! account ) {
164178 return null ;
165179 }
@@ -195,7 +209,7 @@ export class AccountManager {
195209 } ;
196210 }
197211
198- addAccount ( parts : RefreshParts , access ?: string , expires ?: number , email ?: string ) : void {
212+ addAccount ( parts : RefreshParts , access ?: string , expires ?: number , email ?: string , tier ?: AccountTier ) : void {
199213 this . accounts . push ( {
200214 index : this . accounts . length ,
201215 parts,
@@ -204,6 +218,7 @@ export class AccountManager {
204218 rateLimitResetTimes : { } ,
205219 lastUsed : 0 ,
206220 email,
221+ tier,
207222 } ) ;
208223 }
209224
0 commit comments