* feat: don't format when model is not mercury * chore: bump * fix: remove mercury-coder-nextedit * refactor: remove refs to mercury-coder-nextedit * feat: add util function for converting model name to enum * refactor: add new types * refactor: create an abstract base class for providers * refactor: add child provider for instinct * refactor: add provider for mercury coder * refactor: provider factory * refactor: use factory to create model providers based on helper.modelName * refactor: clean up redundant logic to be more provider-friendly * add buildPromptMetadata as an abstract method * refactor: use handlebars and provider-specific template renderers * refactor: use this.modelProvider instead of prompt engine * refactor: remove redundant helper * fix: update vitest after refactor * feat: add test for convertNextEditModelNameToEnum * fix: add isSecurityConcern imports * feat: add functions to return next edit menu items and update config * fix: get rid of callbacks * feat: render next edit menu items * fix: get rid of redundant completion request * feat: update next edit status on vscode workspace config update, refactor out common logic * feat: add continue.enableNextEdit * feat: add aborted field to check for false positives * fix: remove zetaDataset suffix * feat: make sure that we are tracking completions * feat: log aborts and move id generation to the top * feat: all requests are tracked. when request is aborted, log as aborted with zero-value data. else, graduate it from pending completion and set aborted to false * feat: add aborted to schema * feat: add a toggleNextEditEnabled command * feat: show (NE) in the status bar when next edit is enabled, add keymap * feat: add command definitions for toggleNextEditEnabled * refactor: handle validation in VsCodeExtension * feat: check if model does not support next edit * feat: use better menu item labels * fix: casing * chore: bump * feat: add a function to disable next edit * feat: disable next edit before e2e tests * chore: bump * fix: add a small delay to wait for UI loading * fix: NE is shown for all statuses * fix: wait for the NE to pop up * fix: wait longer for disableNextEdit, clear potential next edit notifications * fix: roll back clearAllNotifications * fix: try disabling next edit before each * fix: revert * feat: add sane defaults when no workspace settings are set * fix: fall back to false * fix: debugging * chore: change name * fix: try waiting for text * debug: more debug logs * fix: clear notifs * fix: suppress warning * fix: string matching * fix: jump label overlapping with the inline tip label (#7399) * feat: add methods to temporarily hide and show tooltip * feat: hide the inline tooltip when the jumpmanager's tooltip is shown * feat: focus on chat input after apply * revert * fix: HOTFIX local assistant loading (#7354) * fix: local assistant loading * fix: no need for excluding .continue/.continue * test: cases for config.yml and config.yaml names * feat: remove hide/show logic for creating a new svgbuilder instance * feat: use a new svgBuilder istance * chore: update package-lock.json --------- Co-authored-by: uinstinct <61635505+uinstinct@users.noreply.github.com> Co-authored-by: Dallin Romney <dallinromney@gmail.com> Co-authored-by: Ting-Wai <40179554+tingwai@users.noreply.github.com> --------- Co-authored-by: uinstinct <61635505+uinstinct@users.noreply.github.com> Co-authored-by: Dallin Romney <dallinromney@gmail.com> Co-authored-by: Ting-Wai <40179554+tingwai@users.noreply.github.com>
300 lines
9 KiB
TypeScript
300 lines
9 KiB
TypeScript
import { ConfigHandler } from "../config/ConfigHandler.js";
|
|
import { IDE, ILLM } from "../index.js";
|
|
import OpenAI from "../llm/llms/OpenAI.js";
|
|
import { DEFAULT_AUTOCOMPLETE_OPTS } from "../util/parameters.js";
|
|
|
|
import { shouldCompleteMultiline } from "./classification/shouldCompleteMultiline.js";
|
|
import { ContextRetrievalService } from "./context/ContextRetrievalService.js";
|
|
|
|
import { isSecurityConcern } from "../indexing/ignore.js";
|
|
import { BracketMatchingService } from "./filtering/BracketMatchingService.js";
|
|
import { CompletionStreamer } from "./generation/CompletionStreamer.js";
|
|
import { postprocessCompletion } from "./postprocessing/index.js";
|
|
import { shouldPrefilter } from "./prefiltering/index.js";
|
|
import { getAllSnippetsWithoutRace } from "./snippets/index.js";
|
|
import { renderPromptWithTokenLimit } from "./templating/index.js";
|
|
import { GetLspDefinitionsFunction } from "./types.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",
|
|
"operation was aborted",
|
|
];
|
|
|
|
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;
|
|
}
|
|
|
|
// Temporary fix for JetBrains autocomplete bug as described in https://github.com/continuedev/continue/pull/3022
|
|
if (llm.model === undefined && llm.completionOptions?.model !== undefined) {
|
|
llm.model = llm.completionOptions.model;
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
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(llm: ILLM) {
|
|
const { config } = await this.configHandler.loadConfig();
|
|
const options = {
|
|
...DEFAULT_AUTOCOMPLETE_OPTS,
|
|
...config?.tabAutocompleteOptions,
|
|
...llm.autocompleteOptions,
|
|
};
|
|
|
|
// Enable static contextualization if defined.
|
|
if (config?.experimental?.enableStaticContextualization) {
|
|
options.experimental_enableStaticContextualization = true;
|
|
}
|
|
|
|
return options;
|
|
}
|
|
|
|
public async provideInlineCompletionItems(
|
|
input: AutocompleteInput,
|
|
token: AbortSignal | undefined,
|
|
force?: boolean,
|
|
): Promise<AutocompleteOutcome | undefined> {
|
|
try {
|
|
// Create abort signal if not given
|
|
if (!token) {
|
|
const controller = this.loggingService.createAbortController(
|
|
input.completionId,
|
|
);
|
|
token = controller.signal;
|
|
}
|
|
const startTime = Date.now();
|
|
|
|
const llm = await this._prepareLlm();
|
|
if (!llm) {
|
|
return undefined;
|
|
}
|
|
|
|
if (isSecurityConcern(input.filepath)) {
|
|
return undefined;
|
|
}
|
|
|
|
const options = await this._getAutocompleteOptions(llm);
|
|
|
|
// Debounce
|
|
if (!force) {
|
|
if (
|
|
await this.debouncer.delayAndShouldDebounce(options.debounceDelay)
|
|
) {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
if (llm.promptTemplates?.autocomplete) {
|
|
options.template = llm.promptTemplates.autocomplete as string;
|
|
}
|
|
|
|
const helper = await HelperVars.create(
|
|
input,
|
|
options,
|
|
llm.model,
|
|
this.ide,
|
|
);
|
|
|
|
if (await shouldPrefilter(helper, this.ide)) {
|
|
return undefined;
|
|
}
|
|
|
|
const [snippetPayload, workspaceDirs] = await Promise.all([
|
|
getAllSnippetsWithoutRace({
|
|
helper,
|
|
ide: this.ide,
|
|
getDefinitionsFromLsp: this.getDefinitionsFromLsp,
|
|
contextRetrievalService: this.contextRetrievalService,
|
|
}),
|
|
this.ide.getWorkspaceDirs(),
|
|
]);
|
|
|
|
const { prompt, prefix, suffix, completionOptions } =
|
|
renderPromptWithTokenLimit({
|
|
snippetPayload,
|
|
workspaceDirs,
|
|
helper,
|
|
llm,
|
|
});
|
|
|
|
// 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.underlyingProviderName,
|
|
modelName: llm.model,
|
|
completionOptions,
|
|
cacheHit,
|
|
filepath: helper.filepath,
|
|
numLines: completion.split("\n").length,
|
|
completionId: helper.input.completionId,
|
|
gitRepo: await this.ide.getRepoName(helper.filepath),
|
|
uniqueId: await this.ide.getUniqueId(),
|
|
timestamp: new Date().toISOString(),
|
|
...helper.options,
|
|
};
|
|
|
|
if (options.experimental_enableStaticContextualization) {
|
|
outcome.enabledStaticContextualization = true;
|
|
}
|
|
|
|
//////////
|
|
|
|
// Save to cache
|
|
if (!outcome.cacheHit && helper.options.useCache) {
|
|
(await this.autocompleteCache)
|
|
.put(outcome.prefix, outcome.completion)
|
|
.catch((e) => console.warn(`Failed to save to cache: ${e.message}`));
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
}
|
|
}
|