@@ -26,30 +26,37 @@ import {
2626 ResponseError ,
2727 openTabRequestType ,
2828 getSerializedChatRequestType ,
29+ listConversationsRequestType ,
30+ conversationClickRequestType ,
31+ ShowSaveFileDialogRequestType ,
32+ ShowSaveFileDialogParams ,
33+ LSPErrorCodes ,
34+ tabBarActionRequestType ,
2935} from '@aws/language-server-runtimes/protocol'
3036import { v4 as uuidv4 } from 'uuid'
3137import * as vscode from 'vscode'
32- import { Disposable , LanguageClient , Position , State , TextDocumentIdentifier } from 'vscode-languageclient'
38+ import { Disposable , LanguageClient , Position , TextDocumentIdentifier } from 'vscode-languageclient'
3339import * as jose from 'jose'
3440import { AmazonQChatViewProvider } from './webviewProvider'
3541import { AuthUtil } from 'aws-core-vscode/codewhisperer'
3642import { AmazonQPromptSettings , messages } from 'aws-core-vscode/shared'
3743
3844export function registerLanguageServerEventListener ( languageClient : LanguageClient , provider : AmazonQChatViewProvider ) {
39- languageClient . onDidChangeState ( ( { oldState, newState } ) => {
40- if ( oldState === State . Starting && newState === State . Running ) {
41- languageClient . info (
42- 'Language client received initializeResult from server:' ,
43- JSON . stringify ( languageClient . initializeResult )
44- )
45+ languageClient . info (
46+ 'Language client received initializeResult from server:' ,
47+ JSON . stringify ( languageClient . initializeResult )
48+ )
4549
46- const chatOptions = languageClient . initializeResult ?. awsServerCapabilities ?. chatOptions
50+ const chatOptions = languageClient . initializeResult ?. awsServerCapabilities ?. chatOptions
4751
48- void provider . webview ?. postMessage ( {
49- command : CHAT_OPTIONS ,
50- params : chatOptions ,
51- } )
52- }
52+ // Enable the history feature flag
53+ chatOptions . history = true
54+
55+ provider . onDidResolveWebview ( ( ) => {
56+ void provider . webview ?. postMessage ( {
57+ command : CHAT_OPTIONS ,
58+ params : chatOptions ,
59+ } )
5360 } )
5461
5562 languageClient . onTelemetry ( ( e ) => {
@@ -65,6 +72,7 @@ export function registerMessageListeners(
6572 provider . webview ?. onDidReceiveMessage ( async ( message ) => {
6673 languageClient . info ( `[VSCode Client] Received ${ JSON . stringify ( message ) } from chat` )
6774
75+ const webview = provider . webview
6876 switch ( message . command ) {
6977 case COPY_TO_CLIPBOARD :
7078 languageClient . info ( '[VSCode Client] Copy to clipboard event received' )
@@ -177,6 +185,15 @@ export function registerMessageListeners(
177185 )
178186 break
179187 }
188+ case listConversationsRequestType . method :
189+ await handleRequest ( languageClient , message . params , webview , listConversationsRequestType . method )
190+ break
191+ case conversationClickRequestType . method :
192+ await handleRequest ( languageClient , message . params , webview , conversationClickRequestType . method )
193+ break
194+ case tabBarActionRequestType . method :
195+ await handleRequest ( languageClient , message . params , webview , tabBarActionRequestType . method )
196+ break
180197 case followUpClickNotificationType . method :
181198 if ( ! isValidAuthFollowUpType ( message . params . followUp . type ) ) {
182199 languageClient . sendNotification ( followUpClickNotificationType . method , message . params )
@@ -242,6 +259,36 @@ export function registerMessageListeners(
242259
243260 registerHandlerWithResponseRouter ( openTabRequestType . method )
244261 registerHandlerWithResponseRouter ( getSerializedChatRequestType . method )
262+
263+ languageClient . onRequest ( ShowSaveFileDialogRequestType . method , async ( params : ShowSaveFileDialogParams ) => {
264+ const filters : Record < string , string [ ] > = { }
265+ const formatMappings = [
266+ { format : 'markdown' , key : 'Markdown' , extensions : [ 'md' ] } ,
267+ { format : 'html' , key : 'HTML' , extensions : [ 'html' ] } ,
268+ ]
269+
270+ for ( const format of params . supportedFormats ?? [ ] ) {
271+ const mapping = formatMappings . find ( ( m ) => m . format === format )
272+ if ( mapping ) {
273+ filters [ mapping . key ] = mapping . extensions
274+ }
275+ }
276+
277+ const saveAtUri = params . defaultUri ? vscode . Uri . parse ( params . defaultUri ) : vscode . Uri . file ( 'export-chat.md' )
278+ const targetUri = await vscode . window . showSaveDialog ( {
279+ filters,
280+ defaultUri : saveAtUri ,
281+ title : 'Export' ,
282+ } )
283+
284+ if ( ! targetUri ) {
285+ return new ResponseError ( LSPErrorCodes . RequestFailed , 'Export failed' )
286+ }
287+
288+ return {
289+ targetUri : targetUri . toString ( ) ,
290+ }
291+ } )
245292}
246293
247294function isServerEvent ( command : string ) {
@@ -316,3 +363,16 @@ async function handleCompleteResult<T>(
316363 } )
317364 disposable . dispose ( )
318365}
366+
367+ async function handleRequest (
368+ languageClient : LanguageClient ,
369+ params : any ,
370+ webview : vscode . Webview | undefined ,
371+ requestMethod : string
372+ ) {
373+ const result = await languageClient . sendRequest ( requestMethod , params )
374+ void webview ?. postMessage ( {
375+ command : requestMethod ,
376+ params : result ,
377+ } )
378+ }
0 commit comments