@@ -11,6 +11,7 @@ import {
1111 COPY_TO_CLIPBOARD ,
1212 AuthFollowUpType ,
1313 DISCLAIMER_ACKNOWLEDGED ,
14+ UiMessageResultParams ,
1415} from '@aws/chat-client-ui-types'
1516import {
1617 ChatResult ,
@@ -21,6 +22,10 @@ import {
2122 QuickActionResult ,
2223 QuickActionParams ,
2324 insertToCursorPositionNotificationType ,
25+ ErrorCodes ,
26+ ResponseError ,
27+ openTabRequestType ,
28+ getSerializedChatRequestType ,
2429} from '@aws/language-server-runtimes/protocol'
2530import { v4 as uuidv4 } from 'uuid'
2631import * as vscode from 'vscode'
@@ -184,6 +189,59 @@ export function registerMessageListeners(
184189 break
185190 }
186191 } , undefined )
192+
193+ const registerHandlerWithResponseRouter = ( command : string ) => {
194+ const handler = async ( params : any , _ : any ) => {
195+ const mapErrorType = ( type : string | undefined ) : number => {
196+ switch ( type ) {
197+ case 'InvalidRequest' :
198+ return ErrorCodes . InvalidRequest
199+ case 'InternalError' :
200+ return ErrorCodes . InternalError
201+ case 'UnknownError' :
202+ default :
203+ return ErrorCodes . UnknownErrorCode
204+ }
205+ }
206+ const requestId = uuidv4 ( )
207+
208+ void provider . webview ?. postMessage ( {
209+ requestId : requestId ,
210+ command : command ,
211+ params : params ,
212+ } )
213+ const responsePromise = new Promise < UiMessageResultParams | undefined > ( ( resolve , reject ) => {
214+ const timeout = setTimeout ( ( ) => {
215+ disposable ?. dispose ( )
216+ reject ( new Error ( 'Request timed out' ) )
217+ } , 30000 )
218+
219+ const disposable = provider . webview ?. onDidReceiveMessage ( ( message : any ) => {
220+ if ( message . requestId === requestId ) {
221+ clearTimeout ( timeout )
222+ disposable ?. dispose ( )
223+ resolve ( message . params )
224+ }
225+ } )
226+ } )
227+
228+ const result = await responsePromise
229+
230+ if ( result ?. success ) {
231+ return result . result
232+ } else {
233+ return new ResponseError (
234+ mapErrorType ( result ?. error . type ) ,
235+ result ?. error . message ?? 'No response from client'
236+ )
237+ }
238+ }
239+
240+ languageClient . onRequest ( command , handler )
241+ }
242+
243+ registerHandlerWithResponseRouter ( openTabRequestType . method )
244+ registerHandlerWithResponseRouter ( getSerializedChatRequestType . method )
187245}
188246
189247function isServerEvent ( command : string ) {
0 commit comments