Skip to content

Commit 32c2882

Browse files
committed
fix(amazonq): duplicate messages in flare chat
1 parent 76b61df commit 32c2882

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ import { AuthUtil } from 'aws-core-vscode/codewhisperer'
1515
import { updateConfigurationRequestType } from '@aws/language-server-runtimes/protocol'
1616

1717
export async function activate(languageClient: LanguageClient, encryptionKey: Buffer, mynahUIPath: string) {
18+
const disposables = globals.context.subscriptions
19+
1820
// Make sure we've sent an auth profile to the language server before even initializing the UI
1921
await updateProfile(languageClient)
2022

2123
const provider = new AmazonQChatViewProvider(mynahUIPath)
2224

23-
globals.context.subscriptions.push(
25+
disposables.push(
2426
window.registerWebviewViewProvider(AmazonQChatViewProvider.viewType, provider, {
2527
webviewOptions: {
2628
retainContextWhenHidden: true,
@@ -36,12 +38,18 @@ export async function activate(languageClient: LanguageClient, encryptionKey: Bu
3638
registerLanguageServerEventListener(languageClient, provider)
3739

3840
provider.onDidResolveWebview(() => {
39-
if (provider.webview) {
40-
DefaultAmazonQAppInitContext.instance.getAppsToWebViewMessageListener().onMessage((msg) => {
41-
provider.webview?.postMessage(msg).then(undefined, (e) => {
42-
getLogger().error('webView.postMessage failed: %s', (e as Error).message)
43-
})
41+
const disposable = DefaultAmazonQAppInitContext.instance.getAppsToWebViewMessageListener().onMessage((msg) => {
42+
provider.webview?.postMessage(msg).then(undefined, (e) => {
43+
getLogger().error('webView.postMessage failed: %s', (e as Error).message)
4444
})
45+
})
46+
47+
if (provider.webviewView) {
48+
disposables.push(
49+
provider.webviewView.onDidDispose(() => {
50+
disposable.dispose()
51+
})
52+
)
4553
}
4654

4755
registerMessageListeners(languageClient, provider, encryptionKey)
@@ -50,7 +58,7 @@ export async function activate(languageClient: LanguageClient, encryptionKey: Bu
5058
// register event listeners from the legacy agent flow
5159
await registerLegacyChatListeners(globals.context)
5260

53-
globals.context.subscriptions.push(
61+
disposables.push(
5462
AuthUtil.instance.regionProfileManager.onDidChangeRegionProfile(async () => {
5563
void updateProfile(languageClient)
5664
await provider.refreshWebview()

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
import {
77
EventEmitter,
88
CancellationToken,
9-
Webview,
109
WebviewView,
1110
WebviewViewProvider,
1211
WebviewViewResolveContext,
1312
Uri,
13+
Webview,
1414
} from 'vscode'
1515
import * as path from 'path'
1616
import {
@@ -28,7 +28,8 @@ export class AmazonQChatViewProvider implements WebviewViewProvider {
2828
private readonly onDidResolveWebviewEmitter = new EventEmitter<void>()
2929
public readonly onDidResolveWebview = this.onDidResolveWebviewEmitter.event
3030

31-
webview: Webview | undefined
31+
webviewView?: WebviewView
32+
webview?: Webview
3233

3334
connectorAdapterPath?: string
3435
uiPath?: string
@@ -40,8 +41,6 @@ export class AmazonQChatViewProvider implements WebviewViewProvider {
4041
context: WebviewViewResolveContext,
4142
_token: CancellationToken
4243
) {
43-
this.webview = webviewView.webview
44-
4544
const lspDir = Uri.parse(LanguageServerResolver.defaultDir())
4645
const dist = Uri.joinPath(globals.context.extensionUri, 'dist')
4746
webviewView.webview.options = {
@@ -63,7 +62,16 @@ export class AmazonQChatViewProvider implements WebviewViewProvider {
6362

6463
webviewView.webview.html = await this.getWebviewContent()
6564

65+
this.webviewView = webviewView
66+
this.webview = this.webviewView.webview
67+
6668
this.onDidResolveWebviewEmitter.fire()
69+
globals.context.subscriptions.push(
70+
this.webviewView.onDidDispose(() => {
71+
this.webviewView = undefined
72+
this.webview = undefined
73+
})
74+
)
6775
performance.mark(amazonqMark.open)
6876
}
6977

0 commit comments

Comments
 (0)