@@ -165,6 +165,11 @@ import {
165165 resolveAccountSelection ,
166166 type TokenSuccessWithAccount ,
167167} from "./lib/runtime/account-selection.js" ;
168+ import {
169+ formatRateLimitEntry ,
170+ getRateLimitResetTimeForFamily ,
171+ resolveActiveIndex ,
172+ } from "./lib/runtime/account-state.js" ;
168173import { buildManualOAuthFlow } from "./lib/runtime/manual-oauth-flow.js" ;
169174import {
170175 createRuntimeMetrics ,
@@ -483,22 +488,6 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
483488 }
484489 } ;
485490
486- const resolveActiveIndex = (
487- storage : {
488- activeIndex : number ;
489- activeIndexByFamily ?: Partial < Record < ModelFamily , number > > ;
490- accounts : unknown [ ] ;
491- } ,
492- family : ModelFamily = "codex" ,
493- ) : number => {
494- const total = storage . accounts . length ;
495- if ( total === 0 ) return 0 ;
496- const rawCandidate =
497- storage . activeIndexByFamily ?. [ family ] ?? storage . activeIndex ;
498- const raw = Number . isFinite ( rawCandidate ) ? rawCandidate : 0 ;
499- return Math . max ( 0 , Math . min ( raw , total - 1 ) ) ;
500- } ;
501-
502491 const hydrateEmails = async (
503492 storage : AccountStorageV3 | null ,
504493 ) : Promise < AccountStorageV3 | null > => {
@@ -571,40 +560,6 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
571560 return storage ;
572561 } ;
573562
574- const getRateLimitResetTimeForFamily = (
575- account : { rateLimitResetTimes ?: Record < string , number | undefined > } ,
576- now : number ,
577- family : ModelFamily ,
578- ) : number | null => {
579- const times = account . rateLimitResetTimes ;
580- if ( ! times ) return null ;
581-
582- let minReset : number | null = null ;
583- const prefix = `${ family } :` ;
584- for ( const [ key , value ] of Object . entries ( times ) ) {
585- if ( typeof value !== "number" ) continue ;
586- if ( value <= now ) continue ;
587- if ( key !== family && ! key . startsWith ( prefix ) ) continue ;
588- if ( minReset === null || value < minReset ) {
589- minReset = value ;
590- }
591- }
592-
593- return minReset ;
594- } ;
595-
596- const formatRateLimitEntry = (
597- account : { rateLimitResetTimes ?: Record < string , number | undefined > } ,
598- now : number ,
599- family : ModelFamily = "codex" ,
600- ) : string | null => {
601- const resetAt = getRateLimitResetTimeForFamily ( account , now , family ) ;
602- if ( typeof resetAt !== "number" ) return null ;
603- const remaining = resetAt - now ;
604- if ( remaining <= 0 ) return null ;
605- return `resets in ${ formatWaitTime ( remaining ) } ` ;
606- } ;
607-
608563 const applyUiRuntimeFromConfig = (
609564 pluginConfig : ReturnType < typeof loadPluginConfig > ,
610565 ) : UiRuntimeOptions => {
0 commit comments