fix some tests
This commit is contained in:
parent
719f39831b
commit
d9d8985cc5
7 changed files with 333 additions and 31 deletions
311
core/autocomplete/CompletionProvider.ts
Normal file
311
core/autocomplete/CompletionProvider.ts
Normal file
|
|
@ -0,0 +1,311 @@
|
|||
import { ConfigHandler } from "../config/ConfigHandler.js";
|
||||
import { IDE, ILLM } from "../index.js";
|
||||
import { DEFAULT_AUTOCOMPLETE_OPTS } from "../util/parameters.js";
|
||||
import { AutocompleteLanguageInfo } from "./constants/AutocompleteLanguageInfo.js";
|
||||
import { constructAutocompletePrompt } from "./constructPrompt.js";
|
||||
import { AutocompleteSnippet } from "./context/ranking/index.js";
|
||||
import { postprocessCompletion } from "./postprocessing/index.js";
|
||||
// @prettier-ignore
|
||||
import { TRIAL_FIM_MODEL } from "../config/onboarding.js";
|
||||
import OpenAI from "../llm/llms/OpenAI.js";
|
||||
import { shouldCompleteMultiline } from "./classification/shouldCompleteMultiline.js";
|
||||
import { ContextRetrievalService } from "./context/ContextRetrievalService.js";
|
||||
import { BracketMatchingService } from "./filtering/BracketMatchingService.js";
|
||||
import { CompletionStreamer } from "./generation/CompletionStreamer.js";
|
||||
import { shouldPrefilter } from "./prefiltering/index.js";
|
||||
import { renderPrompt } from "./templating/index.js";
|
||||
import { AutocompleteDebouncer } from "./util/AutocompleteDebouncer.js";
|
||||
import { AutocompleteLoggingService } from "./util/AutocompleteLoggingService.js";
|
||||
import AutocompleteLruCache from "./util/AutocompleteLruCache.js";
|
||||
import { HelperVars } from "./util/HelperVars.js";
|
||||
import { AutocompleteInput, AutocompleteOutcome } from "./util/types.js";
|
||||
|
||||
const autocompleteCache = AutocompleteLruCache.get();
|
||||
|
||||
// Errors that can be expected on occasion even during normal functioning should not be shown.
|
||||
// Not worth disrupting the user to tell them that a single autocomplete request didn't go through
|
||||
const ERRORS_TO_IGNORE = [
|
||||
// From Ollama
|
||||
"unexpected server status",
|
||||
];
|
||||
|
||||
export type GetLspDefinitionsFunction = (
|
||||
filepath: string,
|
||||
contents: string,
|
||||
cursorIndex: number,
|
||||
ide: IDE,
|
||||
lang: AutocompleteLanguageInfo,
|
||||
) => Promise<AutocompleteSnippet[]>;
|
||||
|
||||
export class CompletionProvider {
|
||||
private autocompleteCache = AutocompleteLruCache.get();
|
||||
public errorsShown: Set<string> = new Set();
|
||||
private bracketMatchingService = new BracketMatchingService();
|
||||
private debouncer = new AutocompleteDebouncer();
|
||||
private completionStreamer: CompletionStreamer;
|
||||
private loggingService = new AutocompleteLoggingService();
|
||||
private contextRetrievalService: ContextRetrievalService;
|
||||
|
||||
constructor(
|
||||
private readonly configHandler: ConfigHandler,
|
||||
private readonly ide: IDE,
|
||||
private readonly _injectedGetLlm: () => Promise<ILLM | undefined>,
|
||||
private readonly _onError: (e: any) => void,
|
||||
private readonly getDefinitionsFromLsp: GetLspDefinitionsFunction,
|
||||
) {
|
||||
this.completionStreamer = new CompletionStreamer(this.onError.bind(this));
|
||||
this.contextRetrievalService = new ContextRetrievalService(this.ide);
|
||||
}
|
||||
|
||||
private async _prepareLlm(): Promise<ILLM | undefined> {
|
||||
const llm = await this._injectedGetLlm();
|
||||
|
||||
if (!llm) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Ignore empty API keys for Mistral since we currently write
|
||||
// a template provider without one during onboarding
|
||||
if (llm.providerName === "mistral" && llm.apiKey === "") {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Set temperature (but don't override)
|
||||
if (llm.completionOptions.temperature === undefined) {
|
||||
llm.completionOptions.temperature = 0.01;
|
||||
}
|
||||
|
||||
if (llm instanceof OpenAI) {
|
||||
llm.useLegacyCompletionsEndpoint = true;
|
||||
} else if (
|
||||
llm.providerName === "free-trial" &&
|
||||
llm.model !== TRIAL_FIM_MODEL
|
||||
) {
|
||||
llm.model = TRIAL_FIM_MODEL;
|
||||
}
|
||||
|
||||
return llm;
|
||||
}
|
||||
|
||||
private onError(e: any) {
|
||||
if (
|
||||
ERRORS_TO_IGNORE.some((err) =>
|
||||
typeof e === "string" ? e.includes(err) : e?.message?.includes(err),
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.warn("Error generating autocompletion: ", e);
|
||||
if (!this.errorsShown.has(e.message)) {
|
||||
this.errorsShown.add(e.message);
|
||||
this._onError(e);
|
||||
}
|
||||
}
|
||||
|
||||
public cancel() {
|
||||
this.loggingService.cancel();
|
||||
}
|
||||
|
||||
public accept(completionId: string) {
|
||||
const outcome = this.loggingService.accept(completionId);
|
||||
if (!outcome) {
|
||||
return;
|
||||
}
|
||||
this.bracketMatchingService.handleAcceptedCompletion(
|
||||
outcome.completion,
|
||||
outcome.filepath,
|
||||
);
|
||||
}
|
||||
|
||||
public markDisplayed(completionId: string, outcome: AutocompleteOutcome) {
|
||||
this.loggingService.markDisplayed(completionId, outcome);
|
||||
}
|
||||
|
||||
private async _getAutocompleteOptions() {
|
||||
const config = await this.configHandler.loadConfig();
|
||||
const options = {
|
||||
...DEFAULT_AUTOCOMPLETE_OPTS,
|
||||
...config.tabAutocompleteOptions,
|
||||
};
|
||||
return options;
|
||||
}
|
||||
|
||||
public async provideInlineCompletionItems(
|
||||
input: AutocompleteInput,
|
||||
token: AbortSignal | undefined,
|
||||
): Promise<AutocompleteOutcome | undefined> {
|
||||
try {
|
||||
const startTime = Date.now();
|
||||
const options = await this._getAutocompleteOptions();
|
||||
|
||||
// Debounce
|
||||
if (await this.debouncer.delayAndShouldDebounce(options.debounceDelay)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const llm = await this._prepareLlm();
|
||||
if (!llm) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const helper = await HelperVars.create(
|
||||
input,
|
||||
options,
|
||||
llm.model,
|
||||
this.ide,
|
||||
);
|
||||
|
||||
if (await shouldPrefilter(helper, this.ide)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Create abort signal if not given
|
||||
if (!token) {
|
||||
const controller = this.loggingService.createAbortController(
|
||||
input.completionId,
|
||||
);
|
||||
token = controller.signal;
|
||||
}
|
||||
|
||||
//////////
|
||||
|
||||
// Some IDEs might have special ways of finding snippets (e.g. JetBrains and VS Code have different "LSP-equivalent" systems,
|
||||
// or they might separately track recently edited ranges)
|
||||
const extraSnippets = await this._getExtraSnippets(helper);
|
||||
|
||||
let snippets = await constructAutocompletePrompt(
|
||||
helper,
|
||||
extraSnippets,
|
||||
this.contextRetrievalService,
|
||||
);
|
||||
|
||||
const { prompt, prefix, suffix, completionOptions } = renderPrompt(
|
||||
snippets,
|
||||
await this.ide.getWorkspaceDirs(),
|
||||
helper,
|
||||
);
|
||||
|
||||
// Completion
|
||||
let completion: string | undefined = "";
|
||||
|
||||
const cache = await autocompleteCache;
|
||||
const cachedCompletion = helper.options.useCache
|
||||
? await cache.get(helper.prunedPrefix)
|
||||
: undefined;
|
||||
let cacheHit = false;
|
||||
if (cachedCompletion) {
|
||||
// Cache
|
||||
cacheHit = true;
|
||||
completion = cachedCompletion;
|
||||
} else {
|
||||
const multiline =
|
||||
!helper.options.transform || shouldCompleteMultiline(helper);
|
||||
|
||||
const completionStream =
|
||||
this.completionStreamer.streamCompletionWithFilters(
|
||||
token,
|
||||
llm,
|
||||
prefix,
|
||||
suffix,
|
||||
prompt,
|
||||
multiline,
|
||||
completionOptions,
|
||||
helper,
|
||||
);
|
||||
|
||||
for await (const update of completionStream) {
|
||||
completion += update;
|
||||
}
|
||||
|
||||
// Don't postprocess if aborted
|
||||
if (token.aborted) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const processedCompletion = helper.options.transform
|
||||
? postprocessCompletion({
|
||||
completion,
|
||||
prefix: helper.prunedPrefix,
|
||||
suffix: helper.prunedSuffix,
|
||||
llm,
|
||||
})
|
||||
: completion;
|
||||
|
||||
completion = processedCompletion;
|
||||
}
|
||||
|
||||
if (!completion) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const outcome: AutocompleteOutcome = {
|
||||
time: Date.now() - startTime,
|
||||
completion,
|
||||
prefix,
|
||||
suffix,
|
||||
prompt,
|
||||
modelProvider: llm.providerName,
|
||||
modelName: llm.model,
|
||||
completionOptions,
|
||||
cacheHit,
|
||||
filepath: helper.filepath,
|
||||
completionId: helper.input.completionId,
|
||||
gitRepo: await this.ide.getRepoName(helper.filepath),
|
||||
uniqueId: await this.ide.getUniqueId(),
|
||||
timestamp: Date.now(),
|
||||
...helper.options,
|
||||
};
|
||||
|
||||
//////////
|
||||
|
||||
// Do some stuff later so as not to block return. Latency matters
|
||||
const completionToCache = outcome.completion;
|
||||
setTimeout(async () => {
|
||||
if (!outcome.cacheHit) {
|
||||
(await this.autocompleteCache).put(outcome.prefix, completionToCache);
|
||||
}
|
||||
}, 100);
|
||||
|
||||
// When using the JetBrains extension, Mark as displayed
|
||||
const ideType = (await this.ide.getIdeInfo()).ideType;
|
||||
if (ideType === "jetbrains") {
|
||||
this.markDisplayed(input.completionId, outcome);
|
||||
}
|
||||
|
||||
return outcome;
|
||||
} catch (e: any) {
|
||||
this.onError(e);
|
||||
} finally {
|
||||
this.loggingService.deleteAbortController(input.completionId);
|
||||
}
|
||||
}
|
||||
|
||||
private async _getExtraSnippets(
|
||||
helper: HelperVars,
|
||||
): Promise<AutocompleteSnippet[]> {
|
||||
let extraSnippets = helper.options.useOtherFiles
|
||||
? ((await Promise.race([
|
||||
this.getDefinitionsFromLsp(
|
||||
helper.input.filepath,
|
||||
helper.fullPrefix + helper.fullSuffix,
|
||||
helper.fullPrefix.length,
|
||||
this.ide,
|
||||
helper.lang,
|
||||
),
|
||||
new Promise((resolve) => {
|
||||
setTimeout(() => resolve([]), 100);
|
||||
}),
|
||||
])) as AutocompleteSnippet[])
|
||||
: [];
|
||||
|
||||
const workspaceDirs = await this.ide.getWorkspaceDirs();
|
||||
if (helper.options.onlyMyCode) {
|
||||
extraSnippets = extraSnippets.filter((snippet) => {
|
||||
return workspaceDirs.some((dir) => snippet.filepath.startsWith(dir));
|
||||
});
|
||||
}
|
||||
|
||||
return extraSnippets;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
TODO
|
||||
|
||||
- The generator reuse thing isn't working it seems
|
||||
|
||||
Tests for context:
|
||||
|
||||
- You need a static set of files to test. Ideally you don't have to rewrite the exact same files every time you want to make a new test.
|
||||
- So put some example files in a few folders. There should be a folder for each language I think.
|
||||
- You should definitely test each of the services separately
|
||||
|
|
@ -1,6 +1,4 @@
|
|||
import { SlashCommand } from "../..";
|
||||
import { getFullyQualifiedPath } from "../../../extensions/vscode/src/util/util";
|
||||
import { VsCodeIde } from "../../../extensions/vscode/src/VsCodeIde";
|
||||
import { stripImages } from "../../llm/images";
|
||||
|
||||
const MultiFileEditSlashCommand: SlashCommand = {
|
||||
|
|
@ -12,7 +10,10 @@ const MultiFileEditSlashCommand: SlashCommand = {
|
|||
selectedCode.map(async (item) => {
|
||||
// Split the range string from the filename, e.g. `filename.ts (1-2) -> [filename.ts, (1-2)]`
|
||||
const [filepath, rangeStr] = item.filepath.split(" ");
|
||||
const fullPath = getFullyQualifiedPath(ide as VsCodeIde, filepath);
|
||||
const { getFullyQualifiedPath } = await import(
|
||||
"../../../extensions/vscode/src/util/util"
|
||||
);
|
||||
const fullPath = getFullyQualifiedPath(ide as any, filepath); // any = VsCodeIde here, but importing it breaks tests
|
||||
|
||||
if (!fullPath) {
|
||||
return "";
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { defaultConfig } from "./default";
|
||||
import { getConfigTsPath } from "../util/paths";
|
||||
import { testConfigHandler } from "../test/util/fixtures";
|
||||
import { TEST_DIR } from "../test/util/testDir";
|
||||
import { getConfigTsPath } from "../util/paths";
|
||||
import { defaultConfig } from "./default";
|
||||
|
||||
describe.skip("Test the ConfigHandler and E2E config loading", () => {
|
||||
test("should show only local profile", () => {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
import ignore from "ignore";
|
||||
import path from "path";
|
||||
import os from "os";
|
||||
import fs from "fs";
|
||||
import ignore from "ignore";
|
||||
import { getGlobalContinueIgnorePath } from "../util/paths";
|
||||
|
||||
export const DEFAULT_IGNORE_FILETYPES = [
|
||||
"*.DS_Store",
|
||||
|
|
@ -118,17 +117,7 @@ export function gitIgArrayFromFile(file: string) {
|
|||
.filter((l) => !/^#|^$/.test(l)); // Remove empty lines
|
||||
}
|
||||
|
||||
export const GLOBAL_CONTINUEIGNORE_PATH = path.join(os.homedir(), ".continue", ".continueignore");
|
||||
|
||||
export const getGlobalContinueIgArray = () => {
|
||||
try {
|
||||
const contents = fs.readFileSync(GLOBAL_CONTINUEIGNORE_PATH, "utf8");
|
||||
return gitIgArrayFromFile(contents);
|
||||
} catch (error) {
|
||||
// If the file doesn't exist, that's okay
|
||||
if (error instanceof Error && ("code" in error) && error.code === "ENOENT") {
|
||||
return [];
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
const contents = fs.readFileSync(getGlobalContinueIgnorePath(), "utf8");
|
||||
return gitIgArrayFromFile(contents);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -28,6 +28,17 @@ export function getContinueUtilsPath(): string {
|
|||
return utilsPath;
|
||||
}
|
||||
|
||||
export function getGlobalContinueIgnorePath(): string {
|
||||
const continueIgnorePath = path.join(
|
||||
getContinueGlobalPath(),
|
||||
".continueignore",
|
||||
);
|
||||
if (!fs.existsSync(continueIgnorePath)) {
|
||||
fs.writeFileSync(continueIgnorePath, "");
|
||||
}
|
||||
return continueIgnorePath;
|
||||
}
|
||||
|
||||
export function getContinueGlobalPath(): string {
|
||||
// This is ~/.continue on mac/linux
|
||||
const continuePath = CONTINUE_GLOBAL_DIR;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
const os = require("node:os");
|
||||
import path from "node:path";
|
||||
import * as vscode from "vscode";
|
||||
import { VsCodeIde } from "../VsCodeIde";
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue