From 4e5fa149ea62d0bc445f2400ed2f313805d4e270 Mon Sep 17 00:00:00 2001 From: Zhiming Ma Date: Mon, 23 Dec 2024 23:57:17 +0800 Subject: [PATCH] fix: execute pending command with delay. --- clients/vscode/src/chat/WebviewHelper.ts | 26 +++++++++++-------- .../app/files/components/chat-side-bar.tsx | 5 +++- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/clients/vscode/src/chat/WebviewHelper.ts b/clients/vscode/src/chat/WebviewHelper.ts index c2416da26370..1a8e24a15d01 100644 --- a/clients/vscode/src/chat/WebviewHelper.ts +++ b/clients/vscode/src/chat/WebviewHelper.ts @@ -46,7 +46,7 @@ import { export class WebviewHelper { webview?: Webview; client?: ServerApi; - private pendingActions: (() => void)[] = []; + private pendingActions: (() => Promise)[] = []; private isChatPageDisplayed = false; constructor( @@ -284,10 +284,12 @@ export class WebviewHelper { public addRelevantContext(context: ClientFileContext) { if (this.client) { + this.logger.info(`Adding relevant context: ${context}`); this.client.addRelevantContext(context); } else { - this.pendingActions.push(() => { - this.client?.addRelevantContext(context); + this.pendingActions.push(async () => { + this.logger.info(`Adding pending relevant context: ${context}`); + await this.client?.addRelevantContext(context); }); } } @@ -297,9 +299,9 @@ export class WebviewHelper { this.logger.info(`Executing command: ${command}`); this.client.executeCommand(command); } else { - this.pendingActions.push(() => { + this.pendingActions.push(async () => { this.logger.info(`Executing pending command: ${command}`); - this.client?.executeCommand(command); + await this.client?.executeCommand(command); }); } } @@ -320,10 +322,12 @@ export class WebviewHelper { return; } - this.pendingActions.forEach((fn) => fn()); - this.pendingActions = []; + await this.syncActiveSelection(window.activeTextEditor); - this.syncActiveSelection(window.activeTextEditor); + this.pendingActions.forEach(async (fn) => { + await fn(); + }); + this.pendingActions = []; const agentConfig = this.lspClient.agentConfig.current; if (agentConfig?.server.token) { @@ -332,7 +336,7 @@ export class WebviewHelper { const isMac = isBrowser ? navigator.userAgent.toLowerCase().includes("mac") : process.platform.toLowerCase().includes("darwin"); - this.client?.init({ + await this.client?.init({ fetcherOptions: { authorization: agentConfig.server.token, }, @@ -343,12 +347,12 @@ export class WebviewHelper { public async syncActiveSelection(editor: TextEditor | undefined) { if (!editor || !this.isSupportedSchemeForActiveSelection(editor.document.uri.scheme)) { - this.syncActiveSelectionToChatPanel(null); + await this.syncActiveSelectionToChatPanel(null); return; } const fileContext = await getFileContextFromSelection(editor, this.gitProvider); - this.syncActiveSelectionToChatPanel(fileContext); + await this.syncActiveSelectionToChatPanel(fileContext); } public addAgentEventListeners() { diff --git a/ee/tabby-ui/app/files/components/chat-side-bar.tsx b/ee/tabby-ui/app/files/components/chat-side-bar.tsx index e96d1c23f3c5..5ca4f947fea6 100644 --- a/ee/tabby-ui/app/files/components/chat-side-bar.tsx +++ b/ee/tabby-ui/app/files/components/chat-side-bar.tsx @@ -119,7 +119,10 @@ export const ChatSideBar: React.FC = ({ gitUrl } }) - client.executeCommand(getCommand(pendingEvent)) + // FIXME: this delay is a workaround for waiting for the active selection to be updated + setTimeout(() => { + client.executeCommand(getCommand(pendingEvent)) + }, 500) } execute() }