@@ -15,6 +15,8 @@ import {AgentMode} from "./AgentMode";
1515import path from "node:path" ;
1616import { logger } from "./Logger" ;
1717import type {
18+ AccountLoginCompletedNotification ,
19+ AccountUpdatedNotification ,
1820 GetAccountResponse ,
1921 ListMcpServerStatusParams ,
2022 ListMcpServerStatusResponse ,
@@ -70,23 +72,23 @@ export class CodexAcpClient {
7072 switch ( authRequest . methodId ) {
7173 case "api-key" : {
7274 if ( ! authRequest . _meta || ! authRequest . _meta [ "api-key" ] ) throw RequestError . invalidRequest ( ) ;
73- const loginCompletedVersion = this . codexClient . getAccountLoginCompletedVersion ( ) ;
75+ const loginCompletedPromise = this . awaitNextLoginCompleted ( ) ;
7476 await this . codexClient . accountLogin ( {
7577 type : "apiKey" ,
7678 apiKey : authRequest . _meta [ "api-key" ] . apiKey
7779 } ) ;
7880 this . gatewayConfig = null ;
79- const result = await this . codexClient . awaitLoginCompleted ( loginCompletedVersion ) ;
81+ const result = await loginCompletedPromise ;
8082 return result . success ;
8183 }
8284 case "chat-gpt" : {
83- const loginCompletedVersion = this . codexClient . getAccountLoginCompletedVersion ( ) ;
85+ const loginCompletedPromise = this . awaitNextLoginCompleted ( ) ;
8486 const loginResponse = await this . codexClient . accountLogin ( { type : "chatgpt" } ) ;
8587 if ( loginResponse . type == "chatgpt" ) {
8688 await open ( loginResponse . authUrl ) ;
8789 }
8890 this . gatewayConfig = null ;
89- const result = await this . codexClient . awaitLoginCompleted ( loginCompletedVersion ) ;
91+ const result = await loginCompletedPromise ;
9092 return result . success ;
9193 }
9294 case "gateway" :
@@ -159,9 +161,9 @@ export class CodexAcpClient {
159161 }
160162
161163 async logout ( ) : Promise < AuthenticationLogoutResponse > {
162- const accountUpdatedVersion = this . codexClient . getAccountUpdatedVersion ( ) ;
164+ const accountUpdatedPromise = this . awaitNextAccountUpdated ( ) ;
163165 await this . codexClient . accountLogout ( ) ;
164- await this . codexClient . awaitAccountUpdated ( accountUpdatedVersion ) ;
166+ await accountUpdatedPromise ;
165167 return { } ;
166168 }
167169
@@ -184,7 +186,6 @@ export class CodexAcpClient {
184186
185187 async resumeSession ( request : acp . ResumeSessionRequest ) : Promise < SessionMetadata > {
186188 await this . refreshSkills ( request . cwd , request . _meta ) ;
187- const mcpStartupVersion = this . codexClient . getMcpStartupCompleteVersion ( ) ;
188189
189190 const response = await this . codexClient . threadResume ( {
190191 approvalPolicy : null ,
@@ -207,12 +208,10 @@ export class CodexAcpClient {
207208 sessionId : request . sessionId ,
208209 currentModelId : currentModelId ,
209210 models : codexModels ,
210- mcpStartupVersion,
211211 }
212212 }
213213
214214 async loadSession ( request : acp . LoadSessionRequest ) : Promise < SessionMetadataWithThread > {
215- const mcpStartupVersion = this . codexClient . getMcpStartupCompleteVersion ( ) ;
216215 const response = await this . codexClient . threadResume ( {
217216 approvalPolicy : null ,
218217 sandbox : null ,
@@ -235,13 +234,11 @@ export class CodexAcpClient {
235234 currentModelId : currentModelId ,
236235 models : codexModels ,
237236 thread : response . thread ,
238- mcpStartupVersion,
239237 } ;
240238 }
241239
242240 async newSession ( request : acp . NewSessionRequest ) : Promise < SessionMetadata > {
243241 await this . refreshSkills ( request . cwd , request . _meta ) ;
244- const mcpStartupVersion = this . codexClient . getMcpStartupCompleteVersion ( ) ;
245242
246243 const response = await this . codexClient . threadStart ( {
247244 config : this . createSessionConfig ( request . cwd , request . mcpServers ) ,
@@ -267,7 +264,6 @@ export class CodexAcpClient {
267264 sessionId : response . thread . id ,
268265 currentModelId : currentModelId ,
269266 models : codexModels ,
270- mcpStartupVersion,
271267 } ;
272268 }
273269
@@ -276,6 +272,10 @@ export class CodexAcpClient {
276272 return startup . ready ;
277273 }
278274
275+ getMcpStartupCompleteVersion ( ) : number {
276+ return this . codexClient . getMcpStartupCompleteVersion ( ) ;
277+ }
278+
279279 private createSessionConfig ( projectPath : string , mcpServers : Array < McpServer > ) : JsonObject {
280280 const mergedConfig = {
281281 ...mergeGatewayConfig ( this . config , this . gatewayConfig ) ,
@@ -392,6 +392,22 @@ export class CodexAcpClient {
392392 return this . codexClient . listSkills ( params ?? { } ) ;
393393 }
394394
395+ private async awaitNextLoginCompleted ( ) : Promise < AccountLoginCompletedNotification > {
396+ return await new Promise ( ( resolve ) => {
397+ this . codexClient . connection . onNotification ( "account/login/completed" , ( event : AccountLoginCompletedNotification ) => {
398+ resolve ( event ) ;
399+ } ) ;
400+ } ) ;
401+ }
402+
403+ private async awaitNextAccountUpdated ( ) : Promise < AccountUpdatedNotification > {
404+ return await new Promise ( ( resolve ) => {
405+ this . codexClient . connection . onNotification ( "account/updated" , ( event : AccountUpdatedNotification ) => {
406+ resolve ( event ) ;
407+ } ) ;
408+ } ) ;
409+ }
410+
395411 async listMcpServers ( params : ListMcpServerStatusParams = { cursor : null , limit : null } ) : Promise < ListMcpServerStatusResponse > {
396412 return this . codexClient . listMcpServerStatus ( params ) ;
397413 }
@@ -535,7 +551,6 @@ export type SessionMetadata = {
535551 sessionId : string ,
536552 currentModelId : string ,
537553 models : Model [ ] ,
538- mcpStartupVersion : number ,
539554}
540555
541556export type SessionMetadataWithThread = SessionMetadata & {
0 commit comments