@@ -39,6 +39,8 @@ export type WebSocketHandlerOptions = {
3939 onPromptResponse : (
4040 action : Extract < ServerAction , { type : 'prompt-response' } > ,
4141 ) => Promise < void >
42+
43+ apiKey : string
4244}
4345
4446export class WebSocketHandler {
@@ -54,6 +56,7 @@ export class WebSocketHandler {
5456 private onResponseChunk : WebSocketHandlerOptions [ 'onResponseChunk' ]
5557 private onSubagentResponseChunk : WebSocketHandlerOptions [ 'onSubagentResponseChunk' ]
5658 private onPromptResponse : WebSocketHandlerOptions [ 'onPromptResponse' ]
59+ private apiKey : string
5760
5861 constructor ( {
5962 onWebsocketError = ( ) => { } ,
@@ -69,6 +72,8 @@ export class WebSocketHandler {
6972 onSubagentResponseChunk = async ( ) => { } ,
7073
7174 onPromptResponse = async ( ) => { } ,
75+
76+ apiKey,
7277 } : WebSocketHandlerOptions ) {
7378 this . cbWebSocket = new APIRealtimeClient (
7479 WEBSOCKET_URL ,
@@ -87,6 +92,8 @@ export class WebSocketHandler {
8792 this . onSubagentResponseChunk = onSubagentResponseChunk
8893
8994 this . onPromptResponse = onPromptResponse
95+
96+ this . apiKey = apiKey
9097 }
9198
9299 public async connect ( ) {
@@ -102,32 +109,6 @@ export class WebSocketHandler {
102109 this . cbWebSocket . close ( )
103110 }
104111
105- public async init ( {
106- authToken : apiKey ,
107- fileContext,
108- repoUrl,
109- } : Extract < ClientAction , { type : 'init' } > ) : Promise <
110- Extract < ServerAction , { type : 'init-response' } >
111- > {
112- let resolve ! : ( v : Extract < ServerAction , { type : 'init-response' } > ) => void
113- const promise = new Promise <
114- Extract < ServerAction , { type : 'init-response' } >
115- > ( ( res ) => {
116- resolve = res
117- } )
118- this . cbWebSocket . subscribe ( 'init-response' , resolve )
119-
120- this . cbWebSocket . sendAction ( {
121- type : 'init' ,
122- fingerprintId : 'codebuff-sdk' ,
123- authToken : apiKey ,
124- fileContext,
125- repoUrl,
126- } )
127-
128- return promise
129- }
130-
131112 private setupSubscriptions ( ) {
132113 this . cbWebSocket . subscribe ( 'action-error' , this . onResponseError )
133114
@@ -170,4 +151,34 @@ export class WebSocketHandler {
170151 // Handle full response from prompt
171152 this . cbWebSocket . subscribe ( 'prompt-response' , this . onPromptResponse )
172153 }
154+
155+ private getInputDefaultOptions ( ) {
156+ return {
157+ ...( {
158+ type : 'prompt' ,
159+ fingerprintId : 'codebuff-sdk' ,
160+ } as const ) ,
161+ authToken : this . apiKey ,
162+ }
163+ }
164+
165+ public sendInput (
166+ action : Omit <
167+ Extract < ClientAction , { type : 'prompt' } > ,
168+ keyof ReturnType < typeof this . getInputDefaultOptions >
169+ > ,
170+ ) {
171+ this . cbWebSocket . sendAction ( {
172+ ...action ,
173+ ...this . getInputDefaultOptions ( ) ,
174+ } )
175+ }
176+
177+ public cancelInput ( { promptId } : { promptId : string } ) {
178+ this . cbWebSocket . sendAction ( {
179+ type : 'cancel-user-input' ,
180+ authToken : this . apiKey ,
181+ promptId,
182+ } )
183+ }
173184}
0 commit comments