Skip to content

Commit

Permalink
fix: execute pending command with delay.
Browse files Browse the repository at this point in the history
  • Loading branch information
icycodes committed Dec 23, 2024
1 parent 7da4e5c commit 4e5fa14
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
26 changes: 15 additions & 11 deletions clients/vscode/src/chat/WebviewHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import {
export class WebviewHelper {
webview?: Webview;
client?: ServerApi;
private pendingActions: (() => void)[] = [];
private pendingActions: (() => Promise<void>)[] = [];
private isChatPageDisplayed = false;

constructor(
Expand Down Expand Up @@ -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);
});
}
}
Expand All @@ -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);
});
}
}
Expand All @@ -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) {
Expand All @@ -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,
},
Expand All @@ -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() {
Expand Down
5 changes: 4 additions & 1 deletion ee/tabby-ui/app/files/components/chat-side-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ export const ChatSideBar: React.FC<ChatSideBarProps> = ({
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()
}
Expand Down

0 comments on commit 4e5fa14

Please sign in to comment.