@@ -193,30 +193,35 @@ import {
193193} from "./lib/runtime/account-check-helpers.js" ;
194194import { runRuntimeAccountCheck } from "./lib/runtime/account-check.js" ;
195195import { createAccountCheckWorkingState } from "./lib/runtime/account-check-types.js" ;
196+ import {
197+ invalidateRuntimeAccountManagerCache ,
198+ reloadRuntimeAccountManager ,
199+ } from "./lib/runtime/account-manager-cache.js" ;
196200import {
197201 type TokenSuccessWithAccount as AccountPoolTokenSuccessWithAccount ,
198202 persistAccountPoolResults ,
199203} from "./lib/runtime/account-pool.js" ;
200- import { handleAccountSelectEvent } from "./lib/runtime/account-select-event.js" ;
201204import { resolveAccountSelection } from "./lib/runtime/account-selection.js" ;
202205import {
203206 formatRateLimitEntry ,
204207 getRateLimitResetTimeForFamily ,
205208 resolveActiveIndex ,
206209} from "./lib/runtime/account-status.js" ;
207- import {
208- invalidateRuntimeAccountManagerCache ,
209- reloadRuntimeAccountManager ,
210- } from "./lib/runtime/account-manager-cache.js" ;
211210import {
212211 createPersistAccounts ,
213212 runRuntimeOAuthFlow ,
214213} from "./lib/runtime/auth-facade.js" ;
214+ import { applyAccountStorageScopeEntry } from "./lib/runtime/account-storage-scope-entry.js" ;
215215import { runBrowserOAuthFlow } from "./lib/runtime/browser-oauth-flow.js" ;
216+ import { handleRuntimeEvent } from "./lib/runtime/event-handler.js" ;
216217import { hydrateRuntimeEmails } from "./lib/runtime/hydrate-emails.js" ;
217218import { buildLoginMenuAccounts } from "./lib/runtime/login-menu-accounts.js" ;
219+ import { ensureLiveAccountSyncEntry } from "./lib/runtime/live-sync-entry.js" ;
220+ import { applyLoaderRuntimeSetup } from "./lib/runtime/loader-setup.js" ;
218221import { buildManualOAuthFlow } from "./lib/runtime/manual-oauth-flow.js" ;
219- import { applyRuntimePreemptiveQuotaSettings } from "./lib/runtime/preemptive-quota.js" ;
222+ import {
223+ applyPreemptiveQuotaSettingsFromConfig ,
224+ } from "./lib/runtime/quota-settings.js" ;
220225import {
221226 ensureLiveAccountSyncState ,
222227 ensureRefreshGuardianState ,
@@ -418,16 +423,44 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
418423 authFallback,
419424 } ) ;
420425
426+ const reloadAccountManagerFromDisk = async (
427+ authFallback ?: OAuthAuthDetails ,
428+ ) : Promise < AccountManager > =>
429+ reloadRuntimeAccountManager < AccountManager > (
430+ makeReloadAccountManagerDeps ( authFallback ) ,
431+ ) ;
432+
433+ const applyAccountStorageScope = (
434+ pluginConfig : ReturnType < typeof loadPluginConfig > ,
435+ ) : void =>
436+ applyAccountStorageScopeEntry ( {
437+ pluginConfig,
438+ getPerProjectAccounts,
439+ getStorageBackupEnabled,
440+ setStorageBackupEnabled,
441+ isCodexCliSyncEnabled,
442+ getWarningShown : ( ) => perProjectStorageWarningShown ,
443+ setWarningShown : ( shown ) => {
444+ perProjectStorageWarningShown = shown ;
445+ } ,
446+ logWarn,
447+ pluginName : PLUGIN_NAME ,
448+ setStoragePath,
449+ cwd : ( ) => process . cwd ( ) ,
450+ applyAccountStorageScopeFromConfig,
451+ } ) ;
452+
421453 const ensureLiveAccountSync = async (
422454 pluginConfig : ReturnType < typeof loadPluginConfig > ,
423455 authFallback ?: OAuthAuthDetails ,
424456 ) : Promise < void > => {
425- const next = await ensureLiveAccountSyncState ( {
426- enabled : getLiveAccountSync ( pluginConfig ) ,
427- targetPath : getStoragePath ( ) ,
457+ const next = await ensureLiveAccountSyncEntry ( {
458+ pluginConfig,
459+ authFallback ,
428460 currentSync : liveAccountSync ,
429461 currentPath : liveAccountSyncPath ,
430- authFallback,
462+ getLiveAccountSync,
463+ getStoragePath,
431464 createSync : ( oauthFallback ) =>
432465 new LiveAccountSync (
433466 async ( ) => {
@@ -443,6 +476,7 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
443476 registerCleanup,
444477 logWarn,
445478 pluginName : PLUGIN_NAME ,
479+ ensureLiveAccountSyncState,
446480 } ) ;
447481 liveAccountSync = next . liveAccountSync ;
448482 liveAccountSyncPath = next . liveAccountSyncPath ;
@@ -471,38 +505,59 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
471505 next . refreshGuardianCleanupRegistered ;
472506 } ;
473507
508+ const ensureSessionAffinity = (
509+ pluginConfig : ReturnType < typeof loadPluginConfig > ,
510+ ) : void => {
511+ const next = ensureSessionAffinityState ( {
512+ enabled :
513+ getSessionAffinity ( pluginConfig ) ||
514+ getResponseContinuation ( pluginConfig ) ,
515+ ttlMs : getSessionAffinityTtlMs ( pluginConfig ) ,
516+ maxEntries : getSessionAffinityMaxEntries ( pluginConfig ) ,
517+ currentStore : sessionAffinityStore ,
518+ currentConfigKey : sessionAffinityConfigKey ,
519+ createStore : ( { ttlMs, maxEntries } ) =>
520+ new SessionAffinityStore ( { ttlMs, maxEntries } ) ,
521+ } ) ;
522+ sessionAffinityStore = next . sessionAffinityStore ;
523+ sessionAffinityConfigKey = next . sessionAffinityConfigKey ;
524+ } ;
525+
526+ const applyPreemptiveQuotaSettings = (
527+ pluginConfig : ReturnType < typeof loadPluginConfig > ,
528+ ) : void =>
529+ applyPreemptiveQuotaSettingsFromConfig ( pluginConfig , {
530+ configure : ( options ) => preemptiveQuotaScheduler . configure ( options ) ,
531+ getPreemptiveQuotaEnabled,
532+ getPreemptiveQuotaRemainingPercent5h,
533+ getPreemptiveQuotaRemainingPercent7d,
534+ getPreemptiveQuotaMaxDeferralMs,
535+ } ) ;
536+
474537 // Event handler for session recovery and account selection
475538 const eventHandler = async ( input : {
476539 event : { type : string ; properties ?: unknown } ;
477- } ) => {
478- try {
479- const handled = await handleAccountSelectEvent ( {
480- event : input . event ,
481- providerId : PROVIDER_ID ,
482- loadAccounts,
483- saveAccounts,
484- modelFamilies : MODEL_FAMILIES ,
485- getCachedAccountManager : ( ) => cachedAccountManager ,
486- reloadAccountManagerFromDisk : async ( ) => {
487- await reloadRuntimeAccountManager < AccountManager > (
488- makeReloadAccountManagerDeps ( ) ,
489- ) ;
490- } ,
491- setLastCodexCliActiveSyncIndex : ( index ) => {
492- lastCodexCliActiveSyncIndex = index ;
493- } ,
494- showToast : ( message , variant ) =>
495- showRuntimeToast ( client , message , variant ) ,
496- } ) ;
497- if ( handled ) {
498- return ;
499- }
500- } catch ( error ) {
501- logDebug (
502- `[${ PLUGIN_NAME } ] Event handler error: ${ error instanceof Error ? error . message : String ( error ) } ` ,
503- ) ;
504- }
505- } ;
540+ } ) =>
541+ handleRuntimeEvent ( {
542+ input,
543+ providerId : PROVIDER_ID ,
544+ modelFamilies : MODEL_FAMILIES ,
545+ loadAccounts,
546+ saveAccounts,
547+ hasCachedAccountManager : ( ) => ! ! cachedAccountManager ,
548+ syncCodexCliActiveSelectionForIndex : async ( index ) => {
549+ if ( ! cachedAccountManager ) return ;
550+ await cachedAccountManager . syncCodexCliActiveSelectionForIndex ( index ) ;
551+ } ,
552+ setLastCodexCliActiveSyncIndex : ( index ) => {
553+ lastCodexCliActiveSyncIndex = index ;
554+ } ,
555+ reloadAccountManagerFromDisk : ( ) => reloadAccountManagerFromDisk ( ) ,
556+ showToast : ( message , variant ) =>
557+ showRuntimeToast ( client , message , variant ) ,
558+ logDebug,
559+ pluginName : PLUGIN_NAME ,
560+ } ) ;
506561
507562 // Initialize runtime UI settings once on plugin load; auth/tools refresh this dynamically.
508563 applyUiRuntimeFromConfig ( loadPluginConfig ( ) , setUiRuntimeOptions ) ;
@@ -528,43 +583,14 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
528583 async loader ( getAuth : ( ) => Promise < Auth > , provider : unknown ) {
529584 const auth = await getAuth ( ) ;
530585 const pluginConfig = loadPluginConfig ( ) ;
531- applyUiRuntimeFromConfig ( pluginConfig , setUiRuntimeOptions ) ;
532- applyAccountStorageScopeFromConfig ( pluginConfig , {
533- getPerProjectAccounts,
534- getStorageBackupEnabled,
535- setStorageBackupEnabled,
536- isCodexCliSyncEnabled,
537- getWarningShown : ( ) => perProjectStorageWarningShown ,
538- setWarningShown : ( shown ) => {
539- perProjectStorageWarningShown = shown ;
540- } ,
541- logWarn,
542- pluginName : PLUGIN_NAME ,
543- setStoragePath,
544- cwd : ( ) => process . cwd ( ) ,
545- } ) ;
546- {
547- const next = ensureSessionAffinityState ( {
548- enabled :
549- getSessionAffinity ( pluginConfig ) ||
550- getResponseContinuation ( pluginConfig ) ,
551- ttlMs : getSessionAffinityTtlMs ( pluginConfig ) ,
552- maxEntries : getSessionAffinityMaxEntries ( pluginConfig ) ,
553- currentStore : sessionAffinityStore ,
554- currentConfigKey : sessionAffinityConfigKey ,
555- createStore : ( { ttlMs, maxEntries } ) =>
556- new SessionAffinityStore ( { ttlMs, maxEntries } ) ,
557- } ) ;
558- sessionAffinityStore = next . sessionAffinityStore ;
559- sessionAffinityConfigKey = next . sessionAffinityConfigKey ;
560- }
561- ensureRefreshGuardian ( pluginConfig ) ;
562- applyRuntimePreemptiveQuotaSettings ( pluginConfig , {
563- configure : ( options ) => preemptiveQuotaScheduler . configure ( options ) ,
564- getPreemptiveQuotaEnabled,
565- getPreemptiveQuotaRemainingPercent5h,
566- getPreemptiveQuotaRemainingPercent7d,
567- getPreemptiveQuotaMaxDeferralMs,
586+ applyLoaderRuntimeSetup ( {
587+ pluginConfig,
588+ applyUiRuntimeFromConfig : ( config ) =>
589+ applyUiRuntimeFromConfig ( config , setUiRuntimeOptions ) ,
590+ applyAccountStorageScope,
591+ ensureSessionAffinity,
592+ ensureRefreshGuardian,
593+ applyPreemptiveQuotaSettings,
568594 } ) ;
569595
570596 // Only handle OAuth auth type, skip API key auth
0 commit comments