@@ -11,9 +11,11 @@ import { getSSLErrorHint } from '@ant/model-provider';
1111import { sendNotification } from '../services/notifier.js' ;
1212import {
1313 completeChatGPTDeviceLogin ,
14+ removeChatGPTAuth ,
1415 requestChatGPTDeviceCode ,
1516 type ChatGPTDeviceCode ,
1617} from '../services/api/openai/chatgptAuth.js' ;
18+ import { clearOpenAIClientCache } from '../services/api/openai/client.js' ;
1719import { OAuthService } from '../services/oauth/index.js' ;
1820import { getOauthAccountInfo , validateForceLoginOrg } from '../utils/auth.js' ;
1921import { openBrowser } from '../utils/browser.js' ;
@@ -909,6 +911,11 @@ function OAuthStatusMessage({
909911 process . env [ k ] = v ;
910912 }
911913 }
914+ // Drop any cached OpenAI client so the next request rebuilds it
915+ // with the new env vars. Also clear ChatGPT auth file so a prior
916+ // ChatGPT Subscription login can't leak into the OpenAI Compatible path.
917+ clearOpenAIClientCache ( ) ;
918+ void removeChatGPTAuth ( ) . catch ( ( ) => { } ) ;
912919 setOAuthStatus ( { state : 'success' } ) ;
913920 void onDone ( ) ;
914921 }
@@ -1043,6 +1050,11 @@ function OAuthStatusMessage({
10431050 throw new Error ( 'Failed to save settings. Please try again.' ) ;
10441051 }
10451052 for ( const [ k , v ] of Object . entries ( env ) ) process . env [ k ] = v ;
1053+ // Drop any cached OpenAI client built from prior OpenAI Compatible
1054+ // env vars; the ChatGPT Subscription path bypasses the SDK client
1055+ // entirely (uses createChatGPTResponsesStream) but a stale cached
1056+ // client would still be picked up by sideQuery.
1057+ clearOpenAIClientCache ( ) ;
10461058 setOAuthStatus ( { state : 'success' } ) ;
10471059 void onDone ( ) ;
10481060 } catch ( err ) {
@@ -1468,6 +1480,10 @@ function OAuthStatusMessage({
14681480 process . env [ k ] = v ;
14691481 }
14701482 }
1483+ // Drop any cached OpenAI client and ChatGPT auth so the new
1484+ // provider/credentials take effect on the next request.
1485+ clearOpenAIClientCache ( ) ;
1486+ void removeChatGPTAuth ( ) . catch ( ( ) => { } ) ;
14711487 logEvent ( 'tengu_china_login_success' , { } ) ;
14721488 setOAuthStatus ( { state : 'success' } ) ;
14731489 void onDone ( ) ;
0 commit comments