continue-custom/core/nextEdit/NextEditProvider.ts
2025-07-17 21:29:02 -07:00

608 lines
20 KiB
TypeScript

import * as path from "path";
import { ConfigHandler } from "../config/ConfigHandler.js";
import {
BranchAndDir,
ChatMessage,
IDE,
ILLM,
Position,
RangeInFile,
} from "../index.js";
import OpenAI from "../llm/llms/OpenAI.js";
import { DEFAULT_AUTOCOMPLETE_OPTS } from "../util/parameters.js";
import { ContextRetrievalService } from "../autocomplete/context/ContextRetrievalService.js";
import { randomUUID } from "crypto";
import { BracketMatchingService } from "../autocomplete/filtering/BracketMatchingService.js";
import { CompletionStreamer } from "../autocomplete/generation/CompletionStreamer.js";
import { shouldPrefilter } from "../autocomplete/prefiltering/index.js";
import { getAllSnippetsWithoutRace } from "../autocomplete/snippets/index.js";
import { GetLspDefinitionsFunction } from "../autocomplete/types.js";
import { AutocompleteDebouncer } from "../autocomplete/util/AutocompleteDebouncer.js";
import AutocompleteLruCache from "../autocomplete/util/AutocompleteLruCache.js";
import { HelperVars } from "../autocomplete/util/HelperVars.js";
import { AutocompleteInput } from "../autocomplete/util/types.js";
import { myersDiff } from "../diff/myers.js";
import { replaceEscapedCharacters } from "../util/text.js";
import {
NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN,
NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN,
} from "./constants.js";
import {
calculateFinalCursorPosition,
getOffsetPositionAtLastNewLine,
} from "./diff/diff.js";
import { EditableRegionStrategy } from "./NextEditEditableRegionCarver.js";
import { NextEditLoggingService } from "./NextEditLoggingService.js";
import { NextEditPrefetchQueue } from "./NextEditPrefetchQueue.js";
import {
renderDefaultSystemPrompt,
renderDefaultUserPrompt,
renderPrompt,
} from "./templating/NextEditPromptEngine.js";
import { NextEditOutcome, Prompt, PromptMetadata } from "./types.js";
// import { renderPrompt } from "./templating/NextEditPromptEngine.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 NextEditProvider {
private static instance: NextEditProvider | null = null;
private autocompleteCache = AutocompleteLruCache.get();
public errorsShown: Set<string> = new Set();
private bracketMatchingService = new BracketMatchingService();
private debouncer = new AutocompleteDebouncer();
private completionStreamer: CompletionStreamer;
private loggingService: NextEditLoggingService;
private contextRetrievalService: ContextRetrievalService;
private endpointType: "default" | "fineTuned";
private diffContext: string = "";
private promptMetadata: PromptMetadata | null = null;
private prefetchQueue: NextEditPrefetchQueue;
// private refQueue: RangeInFile[] = [];
// TODO: This needs to be cleared globally on cancel.
public static currentEditChainId: string | null = null;
private 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,
endpointType: "default" | "fineTuned",
) {
this.completionStreamer = new CompletionStreamer(this.onError.bind(this));
this.contextRetrievalService = new ContextRetrievalService(this.ide);
this.endpointType = endpointType;
this.loggingService = NextEditLoggingService.getInstance();
const fetchFunction = async (
previousData: NextEditOutcome | undefined,
resource: RangeInFile | undefined,
other?: {
helper: HelperVars;
diffContext: string;
llm: ILLM;
token: AbortSignal;
startTime: number;
newCursorPos: Position;
editableRegionStrategy: EditableRegionStrategy;
},
) => {
if (!other) return undefined;
// console.log("previousData:", previousData?.completion);
// render prompt
// TODO: this has to set the editable region properly.
// console.log("fetchfunction - previousData:", previousData?.completion);
const promptMetadata = renderPrompt(
other.helper,
other.diffContext,
other.editableRegionStrategy,
previousData,
resource,
);
this.promptMetadata = promptMetadata;
// console.log("prompt:", promptMetadata.prompt);
// call llm.chat
const msg = await other.llm.chat([promptMetadata.prompt], other.token);
// built and return outcome
if (typeof msg.content === "string") {
if (msg.content === "") return undefined;
// NOTE: There are cases where msg.conetnt.split("<|start|>")[1] is undefined
const nextCompletion = replaceEscapedCharacters(
msg.content.split("<|editable_region_start|>\n")[1],
).replace(/\n$/, "");
const currCursorPos = other.helper.pos;
const editableRegionStartLine = Math.max(
currCursorPos.line - NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN,
0,
);
const editableRegionEndLine = Math.min(
currCursorPos.line + NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN,
other.helper.fileLines.length - 1,
);
const oldEditRangeSlice = other.helper.fileContents
.split("\n")
.slice(editableRegionStartLine, editableRegionEndLine + 1)
.join("\n");
// How far away is the current line from the start of the editable region?
const lineOffsetAtCursorPos =
currCursorPos.line - editableRegionStartLine;
// How long is the line at the current cursor position?
const newEditRangeSlice = nextCompletion;
const lineContentAtCursorPos =
newEditRangeSlice.split("\n")[lineOffsetAtCursorPos];
const diffLines = myersDiff(oldEditRangeSlice, newEditRangeSlice);
const offset = getOffsetPositionAtLastNewLine(
diffLines,
lineContentAtCursorPos,
lineOffsetAtCursorPos,
);
// Calculate the actual line number in the editor by adding the startPos offset
// to the line number from the diff calculation.
const finalCursorPos: Position = {
line: editableRegionStartLine + offset.line,
character: offset.character,
};
// TODO: if editable region strategy is naive, then we need to split the prediction up into regions. Also potentially filling up the prefetch queue.
const outcomeNext: NextEditOutcome = {
elapsed: Date.now() - other.startTime,
modelProvider: other.llm.underlyingProviderName,
modelName: other.llm.model + ":zetaDataset",
completionOptions: null,
// filepath: helper.filepath,
completionId: other.helper.input.completionId,
gitRepo: await this.ide.getRepoName(other.helper.filepath),
uniqueId: await this.ide.getUniqueId(),
timestamp: Date.now(),
fileUri: other.helper.filepath,
workspaceDirUri:
other.helper.workspaceUris[0] ??
path.dirname(other.helper.filepath),
prompt: this.promptMetadata!.prompt.content,
userEdits: this.promptMetadata!.userEdits,
userExcerpts: this.promptMetadata!.userExcerpts,
originalEditableRange: oldEditRangeSlice,
completion: nextCompletion,
cursorPosition: other.helper.pos,
finalCursorPosition: finalCursorPos,
editableRegionStartLine,
editableRegionEndLine,
...other.helper.options,
};
return outcomeNext;
} else {
return undefined;
}
};
this.prefetchQueue = new NextEditPrefetchQueue(
fetchFunction,
[],
undefined,
);
}
public static initialize(
configHandler: ConfigHandler,
ide: IDE,
injectedGetLlm: () => Promise<ILLM | undefined>,
onError: (e: any) => void,
getDefinitionsFromLsp: GetLspDefinitionsFunction,
endpointType: "default" | "fineTuned",
): NextEditProvider {
if (!NextEditProvider.instance) {
NextEditProvider.instance = new NextEditProvider(
configHandler,
ide,
injectedGetLlm,
onError,
getDefinitionsFromLsp,
endpointType,
);
}
return NextEditProvider.instance;
}
public static getInstance(): NextEditProvider {
if (!NextEditProvider.instance) {
throw new Error(
"NextEditProvider has not been initialized. Call initialize() first.",
);
}
return NextEditProvider.instance;
}
public addDiffToContext(diff: string): void {
this.diffContext = diff;
}
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;
}
// TODO: Resolve import error with TRIAL_FIM_MODEL
// 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;
// }
// }
// public reject(completionId: string) {
// const outcome = this.loggingService.reject(completionId);
// if (!outcome) {
// return;
// }
// }
public markDisplayed(completionId: string, outcome: NextEditOutcome) {
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<NextEditOutcome | 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 options = await this._getAutocompleteOptions();
// Debounce
if (await this.debouncer.delayAndShouldDebounce(options.debounceDelay)) {
return undefined;
}
const llm = await this._prepareLlm();
if (!llm) {
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(),
]);
// TODO: Toggle between the default endpoint and the finetuned endpoint.
const prompts: Prompt[] = [];
if (this.endpointType === "default") {
prompts.push(renderDefaultSystemPrompt());
prompts.push(renderDefaultUserPrompt(snippetPayload, helper));
} else {
const promptMetadata = renderPrompt(
helper,
this.diffContext,
EditableRegionStrategy.Naive,
);
this.promptMetadata = promptMetadata;
prompts.push(promptMetadata.prompt);
}
const editableRegionStartLine = Math.max(
helper.pos.line - NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN,
0,
);
const editableRegionEndLine = Math.min(
helper.pos.line + NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN,
helper.fileLines.length - 1,
);
const oldEditRangeSlice = helper.fileContents
.split("\n")
.slice(editableRegionStartLine, editableRegionEndLine + 1)
.join("\n");
const finalCursorPos = calculateFinalCursorPosition(
helper.pos,
editableRegionStartLine,
oldEditRangeSlice,
"",
);
if (this.endpointType === "default") {
const msg: ChatMessage = await llm.chat(prompts, token);
if (typeof msg.content === "string") {
const nextCompletion = JSON.parse(msg.content).newCode;
const outcomeNext: NextEditOutcome = {
elapsed: Date.now() - startTime,
modelProvider: llm.underlyingProviderName,
modelName: llm.model + ":zetaDataset",
completionOptions: null,
// filepath: helper.filepath,
completionId: helper.input.completionId,
gitRepo: await this.ide.getRepoName(helper.filepath),
uniqueId: await this.ide.getUniqueId(),
timestamp: Date.now(),
fileUri: helper.filepath,
workspaceDirUri:
helper.workspaceUris[0] ?? path.dirname(helper.filepath),
prompt: prompts.join("\n"),
userEdits: "",
userExcerpts: "",
originalEditableRange: "",
completion: nextCompletion,
cursorPosition: helper.pos,
finalCursorPosition: finalCursorPos,
editableRegionStartLine: 0,
editableRegionEndLine: 0,
...helper.options,
};
return outcomeNext;
} else {
return undefined;
}
} else {
const msg: ChatMessage = await llm.chat(prompts, token);
if (typeof msg.content === "string") {
// TODO: There are cases where msg.conetnt.split("<|start|>")[1] is undefined
const nextCompletion = replaceEscapedCharacters(
msg.content.split("<|editable_region_start|>\n")[1],
).replace(/\n$/, "");
const currCursorPos = helper.pos;
const editableRegionStartLine = Math.max(
currCursorPos.line - NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN,
0,
);
const editableRegionEndLine = Math.min(
currCursorPos.line + NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN,
helper.fileLines.length - 1,
);
const oldEditRangeSlice = helper.fileContents
.split("\n")
.slice(editableRegionStartLine, editableRegionEndLine + 1)
.join("\n");
const finalCursorPos = calculateFinalCursorPosition(
helper.pos,
editableRegionStartLine,
oldEditRangeSlice,
"",
);
const outcomeNext: NextEditOutcome = {
elapsed: Date.now() - startTime,
modelProvider: llm.underlyingProviderName,
modelName: llm.model + ":zetaDataset",
completionOptions: null,
// filepath: helper.filepath,
completionId: helper.input.completionId,
gitRepo: await this.ide.getRepoName(helper.filepath),
uniqueId: await this.ide.getUniqueId(),
timestamp: Date.now(),
fileUri: helper.filepath,
workspaceDirUri:
helper.workspaceUris[0] ?? path.dirname(helper.filepath),
prompt: this.promptMetadata!.prompt.content,
userEdits: this.promptMetadata!.userEdits,
userExcerpts: this.promptMetadata!.userExcerpts,
originalEditableRange: oldEditRangeSlice,
completion: nextCompletion,
cursorPosition: helper.pos,
finalCursorPosition: finalCursorPos,
editableRegionStartLine,
editableRegionEndLine,
...helper.options,
};
return outcomeNext;
} else {
return undefined;
}
}
} catch (e: any) {
this.onError(e);
} finally {
this.loggingService.deleteAbortController(input.completionId);
}
}
public async provideNextEditPrediction(
input: AutocompleteInput,
token: AbortSignal | undefined,
) {
try {
if (NextEditProvider.currentEditChainId) {
return await this.prefetchQueue.pop();
}
// Create abort signal if not given
if (!token) {
const controller = this.loggingService.createAbortController(
input.completionId,
);
token = controller.signal;
}
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;
}
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;
}
NextEditProvider.currentEditChainId = randomUUID();
this.prefetchQueue.loadOther({
helper,
diffContext: this.diffContext,
llm,
token,
startTime,
newCursorPos: helper.pos,
editableRegionStrategy: EditableRegionStrategy.Naive,
});
const [snippetPayload, workspaceDirs] = await Promise.all([
getAllSnippetsWithoutRace({
helper,
ide: this.ide,
getDefinitionsFromLsp: this.getDefinitionsFromLsp,
contextRetrievalService: this.contextRetrievalService,
}),
this.ide.getWorkspaceDirs(),
]);
// TODO: Get the references.
const refQueue = await this.ide.getReferences({
filepath: helper.filepath,
position: helper.pos,
});
const { config } = await this.configHandler.loadConfig();
const branches = await Promise.all(
workspaceDirs.map((dir) => this.ide.getBranch(dir)),
);
const tags: BranchAndDir[] = workspaceDirs.map((directory, i) => ({
directory,
branch: branches[i],
}));
// const chunks = await getTopRelevantCodeChunks(
// ` add(number) {
// this.result += number;
// return this;
// }`,
// {
// ide: this.ide,
// llm: llm,
// config: config!,
// tags: tags,
// // filterDirectory: getUriPathBasename(helper.workspaceUris[0]),
// },
// );
// console.log("chunks:", JSON.stringify(chunks, null, 2));
this.prefetchQueue.loadResource(refQueue);
await this.prefetchQueue.initialize();
return await this.prefetchQueue.pop();
} catch (e: any) {
this.onError(e);
} finally {
this.loggingService.deleteAbortController(input.completionId);
}
}
}