Skip to content

Commit

Permalink
fix: actually fix copilot stale requests (#2252)
Browse files Browse the repository at this point in the history
  • Loading branch information
mscolnick authored Sep 6, 2024
1 parent 61a2a49 commit 75a4b18
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
10 changes: 2 additions & 8 deletions frontend/src/core/codemirror/copilot/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function getCopilotRequest(
indentSize: 1,
insertSpaces: true,
path: COPILOT_FILENAME,
version: 0,
version: "replace_me" as unknown as number,
uri: `file://${COPILOT_FILENAME}`,
relativePath: COPILOT_FILENAME,
languageId: LANGUAGE_ID,
Expand All @@ -134,14 +134,8 @@ function getSuggestion(
const startOffset = completionPosition.character - userPosition.character;

// If startOffset is negative, we need to trim the beginning of displayText
const trimmedDisplayText =
startOffset < 0 ? displayText.slice(-startOffset) : displayText;

// If startOffset is positive, we need to prepend spaces
const resultText =
startOffset > 0
? " ".repeat(startOffset) + trimmedDisplayText
: trimmedDisplayText;
startOffset < 0 ? displayText.slice(-startOffset) : displayText;

// If the end of the suggestion already exists next in the document, we should trim it,
// for example closing quotes, brackets, etc.
Expand Down
36 changes: 19 additions & 17 deletions frontend/src/core/codemirror/copilot/language-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
} from "./state";
import { getCodes } from "./getCodes";
import { Logger } from "@/utils/Logger";
import { debounce } from "lodash-es";
import { throttle } from "lodash-es";

// A map of request methods and their parameters and return types
export interface LSPRequestMap {
Expand Down Expand Up @@ -133,27 +133,25 @@ export class CopilotLanguageServerClient extends LanguageServerClient {
return this._request("notifyRejected", params);
}

private async getCompletionInternal(
private getCompletionInternal = async (
params: CopilotGetCompletionsParams,
version: number,
): Promise<CopilotGetCompletionsResult> {
const response = await this._request("getCompletions", {
): Promise<CopilotGetCompletionsResult> => {
return await this._request("getCompletions", {
doc: {
...params.doc,
version: version,
},
});

return response;
}
};

// Even though the copilot extension has a debounce,
// there are multiple requests sent at the same time
// when multiple Codemirror instances are mounted at the same time.
// So we only need to debounce around 10ms.
private debouncedGetCompletionInternal = debounce(
this.getCompletionInternal.bind(this),
10,
// So we throttle it to ignore multiple requests at the same time.
private throttledGetCompletionInternal = throttle(
this.getCompletionInternal,
200,
);

async getCompletion(
Expand All @@ -163,21 +161,25 @@ export class CopilotLanguageServerClient extends LanguageServerClient {
return { completions: [] };
}

const version = this.documentVersion;
const requestVersion = this.documentVersion;
params.doc.version = requestVersion;

// If version is 0, it means the document hasn't been opened yet
if (version === 0) {
if (requestVersion === 0) {
return { completions: [] };
}

// Start a loading indicator
setGitHubCopilotLoadingVersion(version);
const response = await this.debouncedGetCompletionInternal(params, version);
setGitHubCopilotLoadingVersion(requestVersion);
const response = await this.throttledGetCompletionInternal(
params,
requestVersion,
);
// Stop the loading indicator (only if the version hasn't changed)
clearGitHubCopilotLoadingVersion(version);
clearGitHubCopilotLoadingVersion(requestVersion);

// If the document version has changed since the request was made, return an empty response
if (version !== this.documentVersion) {
if (requestVersion !== this.documentVersion) {
return { completions: [] };
}

Expand Down
1 change: 1 addition & 0 deletions frontend/src/core/codemirror/copilot/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface CopilotGetCompletionsParams {
export interface CopilotGetCompletionsResult {
completions: Array<{
text: string;
docVersion: number;
position: {
line: number;
character: number;
Expand Down

0 comments on commit 75a4b18

Please sign in to comment.