@@ -7,9 +7,9 @@ import type {
77 ServerNotification
88} from "./app-server" ;
99import type {
10- AccountLoginCompletedNotification ,
10+ AccountLoginCompletedNotification , AccountUpdatedNotification ,
1111 GetAccountParams ,
12- GetAccountResponse , LoginAccountParams , LoginAccountResponse ,
12+ GetAccountResponse , LoginAccountParams , LoginAccountResponse , LogoutAccountResponse ,
1313 ThreadStartParams ,
1414 ThreadStartResponse ,
1515 TurnCompletedNotification ,
@@ -26,6 +26,11 @@ export class CodexAppServerClient {
2626
2727 constructor ( connection : MessageConnection ) {
2828 this . connection = connection ;
29+ this . onServerNotification ( ( notification ) => {
30+ for ( const callback of this . transportEventHandlers ) {
31+ callback ( { eventType : "notification" , ...notification } ) ;
32+ }
33+ } ) ;
2934 }
3035
3136 async initialize ( params : InitializeParams ) : Promise < InitializeResponse > {
@@ -44,6 +49,10 @@ export class CodexAppServerClient {
4449 return await this . sendRequest ( { method : "account/login/start" , params : params } ) ;
4550 }
4651
52+ async accountLogout ( ) : Promise < LogoutAccountResponse > {
53+ return await this . sendRequest ( { method : "account/logout" , params : undefined } ) ;
54+ }
55+
4756 async awaitLoginCompleted ( ) : Promise < AccountLoginCompletedNotification > {
4857 return await new Promise ( ( resolve ) => {
4958 this . connection . onNotification ( "account/login/completed" , ( event : AccountLoginCompletedNotification ) => {
@@ -52,6 +61,14 @@ export class CodexAppServerClient {
5261 } ) ;
5362 }
5463
64+ async awaitAccountUpdated ( ) : Promise < AccountUpdatedNotification > {
65+ return await new Promise ( ( resolve ) => {
66+ this . connection . onNotification ( "account/updated" , ( event : AccountUpdatedNotification ) => {
67+ resolve ( event ) ;
68+ } ) ;
69+ } ) ;
70+ }
71+
5572 async accountRead ( params : GetAccountParams ) : Promise < GetAccountResponse > {
5673 return await this . sendRequest ( { method : "account/read" , params : params } ) ;
5774 }
@@ -87,11 +104,31 @@ export class CodexAppServerClient {
87104 return ( params as { msg ?: EventMsg } ) ?. msg ?? null ;
88105 }
89106
107+ private transportEventHandlers : Array < ( event : ClientTransportEvent ) => void > = [ ] ;
108+ onClientTransportEvent ( callback : ( event : ClientTransportEvent ) => void ) {
109+ this . transportEventHandlers . push ( callback ) ;
110+ }
111+
90112 private async sendRequest < R > ( request : CodexRequest ) : Promise < R > {
91- return await this . connection . sendRequest ( request . method , request . params ) ;
113+ for ( const callback of this . transportEventHandlers ) {
114+ callback ( { eventType : "request" , ...request } ) ;
115+ }
116+ let result : any ;
117+ if ( request . params ) {
118+ result = await this . connection . sendRequest < R > ( request . method , request . params )
119+ }
120+ else {
121+ await this . connection . sendRequest < R > ( request . method ) ;
122+ }
123+ for ( const callback of this . transportEventHandlers ) {
124+ callback ( { eventType : "response" , ...result } ) ;
125+ }
126+ return result ;
92127 }
93128}
94129
130+ export type ClientTransportEvent = { eventType : "request" } & CodexRequest | { eventType : "response" } & unknown | { eventType : "notification" } & ServerNotification ;
131+
95132type CodexRequest = DistributiveOmit < ClientRequest , "id" >
96133
97134type DistributiveOmit < T , K extends keyof any > = T extends any
0 commit comments