From 32c2882208859010d7eee183add989b83c27b6be Mon Sep 17 00:00:00 2001 From: Josh Pinkney Date: Thu, 17 Apr 2025 18:33:55 -0400 Subject: [PATCH] fix(amazonq): duplicate messages in flare chat --- packages/amazonq/src/lsp/chat/activation.ts | 22 +++++++++++++------ .../amazonq/src/lsp/chat/webviewProvider.ts | 16 ++++++++++---- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/packages/amazonq/src/lsp/chat/activation.ts b/packages/amazonq/src/lsp/chat/activation.ts index c4091827ab7..a4b251ce834 100644 --- a/packages/amazonq/src/lsp/chat/activation.ts +++ b/packages/amazonq/src/lsp/chat/activation.ts @@ -15,12 +15,14 @@ import { AuthUtil } from 'aws-core-vscode/codewhisperer' import { updateConfigurationRequestType } from '@aws/language-server-runtimes/protocol' export async function activate(languageClient: LanguageClient, encryptionKey: Buffer, mynahUIPath: string) { + const disposables = globals.context.subscriptions + // Make sure we've sent an auth profile to the language server before even initializing the UI await updateProfile(languageClient) const provider = new AmazonQChatViewProvider(mynahUIPath) - globals.context.subscriptions.push( + disposables.push( window.registerWebviewViewProvider(AmazonQChatViewProvider.viewType, provider, { webviewOptions: { retainContextWhenHidden: true, @@ -36,12 +38,18 @@ export async function activate(languageClient: LanguageClient, encryptionKey: Bu registerLanguageServerEventListener(languageClient, provider) provider.onDidResolveWebview(() => { - if (provider.webview) { - DefaultAmazonQAppInitContext.instance.getAppsToWebViewMessageListener().onMessage((msg) => { - provider.webview?.postMessage(msg).then(undefined, (e) => { - getLogger().error('webView.postMessage failed: %s', (e as Error).message) - }) + const disposable = DefaultAmazonQAppInitContext.instance.getAppsToWebViewMessageListener().onMessage((msg) => { + provider.webview?.postMessage(msg).then(undefined, (e) => { + getLogger().error('webView.postMessage failed: %s', (e as Error).message) }) + }) + + if (provider.webviewView) { + disposables.push( + provider.webviewView.onDidDispose(() => { + disposable.dispose() + }) + ) } registerMessageListeners(languageClient, provider, encryptionKey) @@ -50,7 +58,7 @@ export async function activate(languageClient: LanguageClient, encryptionKey: Bu // register event listeners from the legacy agent flow await registerLegacyChatListeners(globals.context) - globals.context.subscriptions.push( + disposables.push( AuthUtil.instance.regionProfileManager.onDidChangeRegionProfile(async () => { void updateProfile(languageClient) await provider.refreshWebview() diff --git a/packages/amazonq/src/lsp/chat/webviewProvider.ts b/packages/amazonq/src/lsp/chat/webviewProvider.ts index 00ec096ea16..fd49308c5b2 100644 --- a/packages/amazonq/src/lsp/chat/webviewProvider.ts +++ b/packages/amazonq/src/lsp/chat/webviewProvider.ts @@ -6,11 +6,11 @@ import { EventEmitter, CancellationToken, - Webview, WebviewView, WebviewViewProvider, WebviewViewResolveContext, Uri, + Webview, } from 'vscode' import * as path from 'path' import { @@ -28,7 +28,8 @@ export class AmazonQChatViewProvider implements WebviewViewProvider { private readonly onDidResolveWebviewEmitter = new EventEmitter() public readonly onDidResolveWebview = this.onDidResolveWebviewEmitter.event - webview: Webview | undefined + webviewView?: WebviewView + webview?: Webview connectorAdapterPath?: string uiPath?: string @@ -40,8 +41,6 @@ export class AmazonQChatViewProvider implements WebviewViewProvider { context: WebviewViewResolveContext, _token: CancellationToken ) { - this.webview = webviewView.webview - const lspDir = Uri.parse(LanguageServerResolver.defaultDir()) const dist = Uri.joinPath(globals.context.extensionUri, 'dist') webviewView.webview.options = { @@ -63,7 +62,16 @@ export class AmazonQChatViewProvider implements WebviewViewProvider { webviewView.webview.html = await this.getWebviewContent() + this.webviewView = webviewView + this.webview = this.webviewView.webview + this.onDidResolveWebviewEmitter.fire() + globals.context.subscriptions.push( + this.webviewView.onDidDispose(() => { + this.webviewView = undefined + this.webview = undefined + }) + ) performance.mark(amazonqMark.open) }