@@ -5,8 +5,15 @@ import type {
55 InitializeParams ,
66 InitializeResponse ,
77 NewConversationParams ,
8- NewConversationResponse , SendUserMessageParams , SendUserMessageResponse , TaskCompleteEvent
8+ NewConversationResponse , SendUserMessageParams , SendUserMessageResponse , ServerNotification , TaskCompleteEvent
99} from "./app-server" ;
10+ import type {
11+ ThreadStartParams ,
12+ ThreadStartResponse ,
13+ TurnCompletedNotification ,
14+ TurnStartParams ,
15+ TurnStartResponse
16+ } from "./app-server/v2" ;
1017
1118export class CodexClient {
1219 readonly connection : MessageConnection ;
@@ -15,39 +22,31 @@ export class CodexClient {
1522 this . connection = connection ;
1623 }
1724
18- async initialize ( params : InitializeParams ) {
19- const response : InitializeResponse = await this . connection . sendRequest ( "initialize" , params )
20- return response ;
21- }
22-
23- async newConversation ( params : NewConversationParams ) {
24- const response : NewConversationResponse = await this . connection . sendRequest ( "newConversation" , params )
25- return response ;
25+ async initialize ( params : InitializeParams ) : Promise < InitializeResponse > {
26+ return await this . sendRequest ( { method : "initialize" , params : params } ) ;
2627 }
2728
28- async sendUserMessage ( params : SendUserMessageParams ) {
29- const response : SendUserMessageResponse = await this . connection . sendRequest ( "sendUserMessage" , params )
30- return response ;
29+ async turnStart ( params : TurnStartParams ) : Promise < TurnStartResponse > {
30+ return await this . sendRequest ( { method : "turn/start" , params : params } ) ;
3131 }
3232
33- async addConversationListener ( params : AddConversationListenerParams ) {
34- const response : AddConversationSubscriptionResponse = await this . connection . sendRequest ( "addConversationListener" , params )
35- return response ;
33+ async threadStart ( params : ThreadStartParams ) : Promise < ThreadStartResponse > {
34+ return await this . sendRequest ( { method : "thread/start" , params : params } ) ;
3635 }
3736
38- async waitForCompletion ( ) {
39- await new Promise ( ( resolve ) => {
40- this . connection . onNotification ( "codex/event/task_complete " , ( event : TaskCompleteEvent ) => {
37+ async waitForCompletion ( ) : Promise < TurnCompletedNotification > {
38+ return await new Promise ( ( resolve ) => {
39+ this . connection . onNotification ( "turn/completed " , ( event : TurnCompletedNotification ) => {
4140 resolve ( event ) ;
4241 } ) ;
4342 } ) ;
4443 }
4544
46- onMessageEvent ( callback : ( event : EventMsg ) => void ) {
45+ onServerNotification ( callback : ( event : ServerNotification ) => void ) {
4746 this . connection . onUnhandledNotification ( ( data ) => {
48- const event = this . getEventMessage ( data ) ;
49- if ( event ) {
50- callback ( event )
47+ const serverNotification = data as ServerNotification ?? null ;
48+ if ( serverNotification ) {
49+ callback ( serverNotification )
5150 }
5251 } ) ;
5352 }
@@ -70,4 +69,9 @@ export class CodexClient {
7069 this . connection . end ( ) ;
7170 }
7271
73- }
72+ private async sendRequest < R > ( request : CodexRequest ) : Promise < R > {
73+ return await this . connection . sendRequest ( request . method , request . params ) ;
74+ }
75+ }
76+
77+ type CodexRequest = Omit < ClientRequest , "id" > ;
0 commit comments