@@ -80,12 +80,6 @@ export class AmazonQChatViewProvider implements WebviewViewProvider {
8080 this . webview = this . webviewView . webview
8181
8282 this . onDidResolveWebviewEmitter . fire ( )
83- globals . context . subscriptions . push (
84- this . webviewView . onDidDispose ( ( ) => {
85- this . webviewView = undefined
86- this . webview = undefined
87- } )
88- )
8983 performance . mark ( amazonqMark . open )
9084 }
9185
@@ -142,21 +136,42 @@ export class AmazonQChatViewProvider implements WebviewViewProvider {
142136 <script type="text/javascript" src="${ this . uiPath ?. toString ( ) } " defer onload="init()"></script>
143137 <script type="text/javascript" src="${ this . connectorAdapterPath ?. toString ( ) } "></script>
144138 <script type="text/javascript">
139+ let qChat = undefined
145140 const init = () => {
146141 const vscodeApi = acquireVsCodeApi()
147142 const hybridChatConnector = new HybridChatAdapter(${ ( await AuthUtil . instance . getChatAuthState ( ) ) . amazonQ === 'connected' } ,${ featureConfigData } ,${ welcomeCount } ,${ disclaimerAcknowledged } ,${ regionProfileString } ,${ disabledCommands } ,${ isSMUS } ,${ isSM } ,vscodeApi.postMessage)
148143 const commands = [hybridChatConnector.initialQuickActions[0]]
149- amazonQChat.createChat(vscodeApi, {disclaimerAcknowledged: ${ disclaimerAcknowledged } , pairProgrammingAcknowledged: ${ pairProgrammingAcknowledged } , quickActionCommands: commands}, hybridChatConnector, ${ JSON . stringify ( featureConfigData ) } );
144+ qChat = amazonQChat.createChat(vscodeApi, {disclaimerAcknowledged: ${ disclaimerAcknowledged } , pairProgrammingAcknowledged: ${ pairProgrammingAcknowledged } , quickActionCommands: commands}, hybridChatConnector, ${ JSON . stringify ( featureConfigData ) } );
150145 }
146+ window.addEventListener('message', (event) => {
147+ /**
148+ * special handler that "simulates" reloading the webview when a profile changes.
149+ * required because chat-client relies on initializedResult from the lsp that
150+ * are only sent once
151+ *
152+ * References:
153+ * closing tabs: https://github.com/aws/mynah-ui/blob/de736b52f369ba885cd19f33ac86c6f57b4a3134/docs/USAGE.md#removing-a-tab-programmatically-
154+ * opening tabs: https://github.com/aws/aws-toolkit-vscode/blob/c22efa03e73b241564c8051c35761eb8620edb83/packages/amazonq/test/e2e/amazonq/framework/framework.ts#L98
155+ */
156+ if (event.data.command === 'reload' && qChat) {
157+ // close all previous tabs
158+ Object.keys(qChat.getAllTabs()).forEach(tabId => qChat.removeTab(tabId, qChat.lastEventId));
159+
160+ // open a new "initial" tab
161+ ;(document.querySelectorAll('.mynah-nav-tabs-wrapper > button.mynah-button')[0]).click()
162+ }
163+ });
151164 </script>
152165 </body>
153166 </html>`
154167 }
155168
156169 async refreshWebview ( ) {
157170 if ( this . webview ) {
158- // refresh the webview when the profile changes
159- this . webview . html = await this . getWebviewContent ( )
171+ // post a message to the webview telling it to reload refresh
172+ void this . webview ?. postMessage ( {
173+ command : 'reload' ,
174+ } )
160175 }
161176 }
162177}
0 commit comments