Skip to content

Commit cda61ec

Browse files
authored
fix(amazonq): simulate refresh of chat (aws#7142)
## Problem Reloading the webview directly causes export/history to be missing ## Solution Add a specialized handler that simulates reloading the webview when a profile changes, rather than actually refresh everything. This is required because the chat-client relies on initializedResult values from the language server that are only sent once --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 57940ed commit cda61ec

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

packages/amazonq/src/lsp/chat/webviewProvider.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
172+
void this.webview?.postMessage({
173+
command: 'reload',
174+
})
160175
}
161176
}
162177
}

0 commit comments

Comments
 (0)