@@ -243,8 +243,6 @@ export const createAntigravityPlugin = (providerId: string) => async (
243243 auth : {
244244 provider : providerId ,
245245 loader : async ( getAuth : GetAuth , provider : Provider ) : Promise < LoaderResult | Record < string , unknown > > => {
246- // Track which account was used in the previous request for detecting switches
247- let previousAccountIndex : number | null = null ;
248246 const auth = await getAuth ( ) ;
249247
250248 // If OpenCode has no valid OAuth auth, clear any stale account storage
@@ -334,10 +332,36 @@ export const createAntigravityPlugin = (providerId: string) => async (
334332 let lastFailure : FailureContext | null = null ;
335333 let lastError : Error | null = null ;
336334 const abortSignal = init ?. signal ?? undefined ;
335+
336+ // Track which account was used in this request for detecting switches
337+ // This is scoped to the fetch call so it resets per-request
338+ let previousAccountIndex : number | null = null ;
339+
340+ // Helper to check if request was aborted
341+ const checkAborted = ( ) => {
342+ if ( abortSignal ?. aborted ) {
343+ throw abortSignal . reason instanceof Error ? abortSignal . reason : new Error ( "Aborted" ) ;
344+ }
345+ } ;
346+
347+ // Helper to show toast without blocking on abort
348+ const showToast = async ( message : string , variant : "info" | "warning" | "success" | "error" ) => {
349+ if ( abortSignal ?. aborted ) return ;
350+ try {
351+ await client . tui . showToast ( {
352+ body : { message, variant } ,
353+ } ) ;
354+ } catch {
355+ // TUI may not be available
356+ }
357+ } ;
337358
338359 // Use while(true) loop to handle rate limits with backoff
339360 // This ensures we wait and retry when all accounts are rate-limited
340361 while ( true ) {
362+ // Check for abort at the start of each iteration
363+ checkAborted ( ) ;
364+
341365 const accountCount = accountManager . getAccountCount ( ) ;
342366
343367 if ( accountCount === 0 ) {
@@ -351,16 +375,7 @@ export const createAntigravityPlugin = (providerId: string) => async (
351375 const waitMs = accountManager . getMinWaitTimeMs ( ) || 60_000 ;
352376 const waitSec = Math . max ( 1 , Math . ceil ( waitMs / 1000 ) ) ;
353377
354- try {
355- await client . tui . showToast ( {
356- body : {
357- message : `All ${ accountCount } account(s) rate-limited. Waiting ${ waitSec } s...` ,
358- variant : "warning" ,
359- } ,
360- } ) ;
361- } catch {
362- // TUI may not be available
363- }
378+ await showToast ( `All ${ accountCount } account(s) rate-limited. Waiting ${ waitSec } s...` , "warning" ) ;
364379
365380 // Wait for the cooldown to expire
366381 await sleep ( waitMs , abortSignal ) ;
@@ -371,16 +386,10 @@ export const createAntigravityPlugin = (providerId: string) => async (
371386 const isAccountSwitch = previousAccountIndex !== null && previousAccountIndex !== account . index ;
372387 if ( isAccountSwitch || previousAccountIndex === null ) {
373388 const accountLabel = account . email || `Account ${ account . index + 1 } ` ;
374- try {
375- await client . tui . showToast ( {
376- body : {
377- message : `Using ${ accountLabel } ${ accountCount > 1 ? ` (${ account . index + 1 } /${ accountCount } )` : "" } ` ,
378- variant : "info" ,
379- } ,
380- } ) ;
381- } catch {
382- // TUI may not be available
383- }
389+ await showToast (
390+ `Using ${ accountLabel } ${ accountCount > 1 ? ` (${ account . index + 1 } /${ accountCount } )` : "" } ` ,
391+ "info"
392+ ) ;
384393 }
385394 previousAccountIndex = account . index ;
386395
@@ -515,16 +524,7 @@ export const createAntigravityPlugin = (providerId: string) => async (
515524
516525 if ( accountManager . getAccountCount ( ) > 1 ) {
517526 // Multiple accounts - switch to next
518- try {
519- await client . tui . showToast ( {
520- body : {
521- message : `Rate limited on ${ accountLabel } . Switching...` ,
522- variant : "warning" ,
523- } ,
524- } ) ;
525- } catch {
526- // TUI may not be available
527- }
527+ await showToast ( `Rate limited on ${ accountLabel } . Switching...` , "warning" ) ;
528528
529529 lastFailure = {
530530 response,
@@ -543,16 +543,7 @@ export const createAntigravityPlugin = (providerId: string) => async (
543543 } else {
544544 // Single account - wait and retry
545545 const waitSec = Math . max ( 1 , Math . ceil ( retryAfterMs / 1000 ) ) ;
546- try {
547- await client . tui . showToast ( {
548- body : {
549- message : `Rate limited. Waiting ${ waitSec } s...` ,
550- variant : "warning" ,
551- } ,
552- } ) ;
553- } catch {
554- // TUI may not be available
555- }
546+ await showToast ( `Rate limited. Waiting ${ waitSec } s...` , "warning" ) ;
556547
557548 lastFailure = {
558549 response,
0 commit comments