Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: actually fix copilot stale requests (#2252)

Myles Scolnick75a4b182baaf
diff --git a/frontend/src/core/codemirror/copilot/extension.ts b/frontend/src/core/codemirror/copilot/extension.ts
index b59a889550..3ad3067220 100644
--- a/frontend/src/core/codemirror/copilot/extension.ts
+++ b/frontend/src/core/codemirror/copilot/extension.ts
@@ -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,
@@ -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.
diff --git a/frontend/src/core/codemirror/copilot/language-server.ts b/frontend/src/core/codemirror/copilot/language-server.ts
index a0611032b4..de23458d52 100644
--- a/frontend/src/core/codemirror/copilot/language-server.ts
+++ b/frontend/src/core/codemirror/copilot/language-server.ts
@@ -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 {
@@ -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(
@@ -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: [] };
}
diff --git a/frontend/src/core/codemirror/copilot/types.ts b/frontend/src/core/codemirror/copilot/types.ts
index 1ecb6303b1..8db6627bf0 100644
--- a/frontend/src/core/codemirror/copilot/types.ts
+++ b/frontend/src/core/codemirror/copilot/types.ts
@@ -58,6 +58,7 @@ export interface CopilotGetCompletionsParams {
export interface CopilotGetCompletionsResult {
completions: Array<{
text: string;
+ docVersion: number;
position: {
line: number;
character: number;