Skip to content

Commit

Permalink
feat: add global mutexAbortController for chat features (#3372)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sma1lboy authored Nov 5, 2024
1 parent 32373eb commit a3bdfda
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
14 changes: 14 additions & 0 deletions clients/tabby-agent/src/chat/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export let mutexAbortController: AbortController | undefined = undefined;

// reset global mutexAbortController to undefined
export const resetMutexAbortController = () => {
mutexAbortController = undefined;
};

// initialize global mutexAbortController
export const initMutexAbortController = () => {
if (!mutexAbortController) {
mutexAbortController = new AbortController();
}
return mutexAbortController;
};
22 changes: 11 additions & 11 deletions clients/tabby-agent/src/chat/inlineEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import {
ChatEditRequest,
ChatEditParams,
ChatEditResolveRequest,
ChatEditResolveParams,
ChatEditCommandRequest,
ChatEditCommandParams,
ChatEditCommand,
ChatFeatureNotAvailableError,
ChatEditDocumentTooLongError,
ChatEditMutexError,
ServerCapabilities,
ChatEditResolveParams,
} from "../protocol";
import cryptoRandomString from "crypto-random-string";
import { isEmptyRange } from "../utils/range";
import { applyWorkspaceEdit, readResponseStream, Edit } from "./utils";
import { readResponseStream, Edit, applyWorkspaceEdit } from "./utils";
import { initMutexAbortController, mutexAbortController, resetMutexAbortController } from "./global";

export class ChatEditProvider implements Feature {
private lspConnection: Connection | undefined = undefined;
private currentEdit: Edit | undefined = undefined;
private mutexAbortController: AbortController | undefined = undefined;

constructor(
private readonly configurations: Configurations,
Expand Down Expand Up @@ -127,15 +127,15 @@ export class ChatEditProvider implements Feature {
throw { name: "ChatEditDocumentTooLongError", message: "Document too long" } as ChatEditDocumentTooLongError;
}

if (this.mutexAbortController && !this.mutexAbortController.signal.aborted) {
if (mutexAbortController && !mutexAbortController.signal.aborted) {
throw {
name: "ChatEditMutexError",
message: "Another smart edit is already in progress",
} as ChatEditMutexError;
}

this.mutexAbortController = new AbortController();
token.onCancellationRequested(() => this.mutexAbortController?.abort());
initMutexAbortController();
token.onCancellationRequested(() => mutexAbortController?.abort());

let insertMode: boolean = isEmptyRange(params.location.range);
const presetCommand = /^\/\w+\b/g.exec(params.command)?.[0];
Expand Down Expand Up @@ -202,7 +202,7 @@ export class ChatEditProvider implements Feature {
model: "",
stream: true,
},
this.mutexAbortController.signal,
mutexAbortController?.signal,
);

const editId = "tabby-" + cryptoRandomString({ length: 6, type: "alphanumeric" });
Expand All @@ -226,10 +226,10 @@ export class ChatEditProvider implements Feature {
readableStream,
this.lspConnection,
this.currentEdit,
this.mutexAbortController,
mutexAbortController,
() => {
this.currentEdit = undefined;
this.mutexAbortController = undefined;
resetMutexAbortController();
},
config.chat.edit.responseDocumentTag,
config.chat.edit.responseCommentTag,
Expand All @@ -239,13 +239,13 @@ export class ChatEditProvider implements Feature {

async stopEdit(id: ChatEditToken): Promise<void> {
if (this.isCurrentEdit(id)) {
this.mutexAbortController?.abort();
mutexAbortController?.abort();
}
}

async resolveEdit(params: ChatEditResolveParams): Promise<boolean> {
if (params.action === "cancel") {
this.mutexAbortController?.abort();
mutexAbortController?.abort();
return false;
}

Expand Down
18 changes: 9 additions & 9 deletions clients/tabby-agent/src/chat/smartApply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import cryptoRandomString from "crypto-random-string";
import { getLogger } from "../logger";
import { readResponseStream, showDocument, Edit } from "./utils";
import { getSmartApplyRange } from "./smartRange";
import { initMutexAbortController, mutexAbortController, resetMutexAbortController } from "./global";

const logger = getLogger("SmartApplyFeature");

export class SmartApplyFeature implements Feature {
private lspConnection: Connection | undefined = undefined;
private mutexAbortController: AbortController | undefined = undefined;
constructor(
private readonly configurations: Configurations,
private readonly tabbyApiClient: TabbyApiClient,
Expand Down Expand Up @@ -61,16 +61,16 @@ export class SmartApplyFeature implements Feature {
return false;
}

if (this.mutexAbortController && !this.mutexAbortController.signal.aborted) {
if (mutexAbortController && !mutexAbortController.signal.aborted) {
logger.warn("Another smart edit is already in progress");
throw {
name: "ChatEditMutexError",
message: "Another smart edit is already in progress",
} as ChatEditMutexError;
}
this.mutexAbortController = new AbortController();
logger.debug("mutex abort status: " + (this.mutexAbortController === undefined));
token.onCancellationRequested(() => this.mutexAbortController?.abort());
initMutexAbortController();
logger.debug("mutex abort status: " + (mutexAbortController === undefined));
token.onCancellationRequested(() => mutexAbortController?.abort());

let applyRange = getSmartApplyRange(document, params.text);
//if cannot find range, lets use backend LLMs
Expand Down Expand Up @@ -115,9 +115,9 @@ export class SmartApplyFeature implements Feature {
this.lspConnection,
this.tabbyApiClient,
this.configurations,
this.mutexAbortController,
mutexAbortController,
() => {
this.mutexAbortController = undefined;
resetMutexAbortController();
},
);
return true;
Expand All @@ -126,7 +126,7 @@ export class SmartApplyFeature implements Feature {
return false;
} finally {
logger.debug("Resetting mutex abort controller");
this.mutexAbortController = undefined;
resetMutexAbortController();
}
}
}
Expand Down Expand Up @@ -222,7 +222,7 @@ async function provideSmartApplyEditLLM(
lspConnection: Connection,
tabbyApiClient: TabbyApiClient,
configurations: Configurations,
mutexAbortController: AbortController,
mutexAbortController: AbortController | undefined,
onResetMutex: () => void,
): Promise<boolean> {
if (!document) {
Expand Down
2 changes: 1 addition & 1 deletion clients/vscode/src/chat/WebviewHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ export class WebviewHelper {
this.logger.info("Smart apply in editor started.");
this.logger.trace("Smart apply in editor with content:", { content });

await window.withProgress(
window.withProgress(
{
location: ProgressLocation.Notification,
title: "Smart Apply in Progress",
Expand Down

0 comments on commit a3bdfda

Please sign in to comment.