HelperVars
This commit is contained in:
parent
9a92ab4933
commit
70eeddcd17
6 changed files with 248 additions and 176 deletions
|
|
@ -1,18 +1,14 @@
|
|||
import { ConfigHandler } from "../config/ConfigHandler.js";
|
||||
import { IDE, ILLM, TabAutocompleteOptions } from "../index.js";
|
||||
import { IDE, ILLM } from "../index.js";
|
||||
import { logDevData } from "../util/devdata.js";
|
||||
import {
|
||||
COUNT_COMPLETION_REJECTED_AFTER,
|
||||
DEFAULT_AUTOCOMPLETE_OPTS,
|
||||
} from "../util/parameters.js";
|
||||
import { Telemetry } from "../util/posthog.js";
|
||||
import { getRangeInString } from "../util/ranges.js";
|
||||
|
||||
import { AutocompleteLanguageInfo } from "./constants/AutocompleteLanguageInfo.js";
|
||||
import {
|
||||
constructAutocompletePrompt,
|
||||
languageForFilepath,
|
||||
} from "./constructPrompt.js";
|
||||
import { constructAutocompletePrompt } from "./constructPrompt.js";
|
||||
import { AutocompleteSnippet } from "./context/ranking/index.js";
|
||||
import { RootPathContextService } from "./context/RootPathContextService.js";
|
||||
import { GeneratorReuseManager } from "./generation/GeneratorReuseManager.js";
|
||||
|
|
@ -25,7 +21,9 @@ import AutocompleteLruCache from "./AutocompleteLruCache.js";
|
|||
import { ImportDefinitionsService } from "./context/ImportDefinitionsService.js";
|
||||
import { BracketMatchingService } from "./filtering/BracketMatchingService.js";
|
||||
import { StreamTransformPipeline } from "./filtering/streamTransforms/StreamTransformPipeline.js";
|
||||
import { HelperVars } from "./HelperVars.js";
|
||||
import { shouldPrefilter } from "./prefiltering/index.js";
|
||||
import { constructInitialPrefixSuffix } from "./templating/constructPrefixSuffix.js";
|
||||
import { renderPrompt } from "./templating/index.js";
|
||||
import { AutocompleteInput, AutocompleteOutcome } from "./types.js";
|
||||
|
||||
|
|
@ -199,9 +197,15 @@ export class CompletionProvider {
|
|||
token: AbortSignal | undefined,
|
||||
): Promise<AutocompleteOutcome | undefined> {
|
||||
try {
|
||||
const options = await this._getAutocompleteOptions();
|
||||
const llm = await this._prepareLlm();
|
||||
if (!llm) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (await shouldPrefilter(input, options, this.ide)) {
|
||||
const options = await this._getAutocompleteOptions();
|
||||
const helper = new HelperVars(input, options, llm.model, this.ide);
|
||||
|
||||
if (await shouldPrefilter(helper, this.ide)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
|
|
@ -211,10 +215,6 @@ export class CompletionProvider {
|
|||
}
|
||||
|
||||
// Get completion
|
||||
const llm = await this._prepareLlm();
|
||||
if (!llm) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Create abort signal if not given
|
||||
if (!token) {
|
||||
|
|
@ -223,7 +223,7 @@ export class CompletionProvider {
|
|||
this._abortControllers.set(input.completionId, controller);
|
||||
}
|
||||
|
||||
const outcome = await this.getTabCompletion(token, options, llm, input);
|
||||
const outcome = await this.getTabCompletion(token, llm, helper);
|
||||
|
||||
if (!outcome?.completion) {
|
||||
return undefined;
|
||||
|
|
@ -287,59 +287,19 @@ export class CompletionProvider {
|
|||
};
|
||||
}
|
||||
|
||||
async getTabCompletion(
|
||||
token: AbortSignal,
|
||||
options: TabAutocompleteOptions,
|
||||
llm: ILLM,
|
||||
input: AutocompleteInput,
|
||||
): Promise<AutocompleteOutcome | undefined> {
|
||||
const startTime = Date.now();
|
||||
|
||||
const {
|
||||
filepath,
|
||||
pos,
|
||||
recentlyEditedFiles,
|
||||
recentlyEditedRanges,
|
||||
clipboardText,
|
||||
manuallyPassFileContents,
|
||||
manuallyPassPrefix,
|
||||
} = input;
|
||||
const fileContents =
|
||||
manuallyPassFileContents ?? (await this.ide.readFile(filepath));
|
||||
const fileLines = fileContents.split("\n");
|
||||
|
||||
// Filter
|
||||
const lang = languageForFilepath(filepath);
|
||||
|
||||
// Prompt
|
||||
let fullPrefix =
|
||||
getRangeInString(fileContents, {
|
||||
start: { line: 0, character: 0 },
|
||||
end: input.selectedCompletionInfo?.range.start ?? pos,
|
||||
}) + (input.selectedCompletionInfo?.text ?? "");
|
||||
|
||||
if (input.injectDetails) {
|
||||
const lines = fullPrefix.split("\n");
|
||||
fullPrefix = `${lines.slice(0, -1).join("\n")}\n${
|
||||
lang.singleLineComment
|
||||
} ${input.injectDetails
|
||||
.split("\n")
|
||||
.join(`\n${lang.singleLineComment} `)}\n${lines[lines.length - 1]}`;
|
||||
}
|
||||
|
||||
const fullSuffix = getRangeInString(fileContents, {
|
||||
start: pos,
|
||||
end: { line: fileLines.length - 1, character: Number.MAX_SAFE_INTEGER },
|
||||
});
|
||||
|
||||
let extrasSnippets = options.useOtherFiles
|
||||
private async _getExtraSnippets(
|
||||
helper: HelperVars,
|
||||
fullPrefix: string,
|
||||
fullSuffix: string,
|
||||
): Promise<AutocompleteSnippet[]> {
|
||||
let extraSnippets = helper.options.useOtherFiles
|
||||
? ((await Promise.race([
|
||||
this.getDefinitionsFromLsp(
|
||||
filepath,
|
||||
helper.input.filepath,
|
||||
fullPrefix + fullSuffix,
|
||||
fullPrefix.length,
|
||||
this.ide,
|
||||
lang,
|
||||
helper.lang,
|
||||
),
|
||||
new Promise((resolve) => {
|
||||
setTimeout(() => resolve([]), 100);
|
||||
|
|
@ -348,54 +308,64 @@ export class CompletionProvider {
|
|||
: [];
|
||||
|
||||
const workspaceDirs = await this.ide.getWorkspaceDirs();
|
||||
if (options.onlyMyCode) {
|
||||
extrasSnippets = extrasSnippets.filter((snippet) => {
|
||||
if (helper.options.onlyMyCode) {
|
||||
extraSnippets = extraSnippets.filter((snippet) => {
|
||||
return workspaceDirs.some((dir) => snippet.filepath.startsWith(dir));
|
||||
});
|
||||
}
|
||||
|
||||
return extraSnippets;
|
||||
}
|
||||
|
||||
async getTabCompletion(
|
||||
token: AbortSignal,
|
||||
llm: ILLM,
|
||||
helper: HelperVars,
|
||||
): Promise<AutocompleteOutcome | undefined> {
|
||||
const startTime = Date.now();
|
||||
|
||||
// Construct full prefix/suffix (a few edge cases handled in here)
|
||||
const { prefix: fullPrefix, suffix: fullSuffix } =
|
||||
await constructInitialPrefixSuffix(helper.input, this.ide);
|
||||
|
||||
// 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,
|
||||
fullPrefix,
|
||||
fullSuffix,
|
||||
);
|
||||
|
||||
let { prefix, suffix, completeMultiline, snippets } =
|
||||
await constructAutocompletePrompt(
|
||||
filepath,
|
||||
pos.line,
|
||||
fullPrefix,
|
||||
fullSuffix,
|
||||
clipboardText,
|
||||
lang,
|
||||
options,
|
||||
recentlyEditedRanges,
|
||||
recentlyEditedFiles,
|
||||
llm.model,
|
||||
extrasSnippets,
|
||||
helper,
|
||||
extraSnippets,
|
||||
this.importDefinitionsService,
|
||||
this.rootPathContextService,
|
||||
);
|
||||
|
||||
// If prefix is manually passed
|
||||
if (manuallyPassPrefix) {
|
||||
prefix = manuallyPassPrefix;
|
||||
if (helper.input.manuallyPassPrefix) {
|
||||
prefix = helper.input.manuallyPassPrefix;
|
||||
suffix = "";
|
||||
}
|
||||
|
||||
const [prompt, completionOptions, multiline] = renderPrompt(
|
||||
options,
|
||||
prefix,
|
||||
suffix,
|
||||
filepath,
|
||||
lang,
|
||||
snippets,
|
||||
llm.model,
|
||||
workspaceDirs,
|
||||
options.template,
|
||||
input.selectedCompletionInfo,
|
||||
await this.ide.getWorkspaceDirs(),
|
||||
completeMultiline,
|
||||
helper,
|
||||
);
|
||||
|
||||
// Completion
|
||||
let completion: string | undefined = "";
|
||||
|
||||
const cache = await autocompleteCache;
|
||||
const cachedCompletion = options.useCache
|
||||
const cachedCompletion = helper.options.useCache
|
||||
? await cache.get(prefix)
|
||||
: undefined;
|
||||
let cacheHit = false;
|
||||
|
|
@ -434,17 +404,17 @@ export class CompletionProvider {
|
|||
};
|
||||
|
||||
const initialGenerator = generatorWithCancellation();
|
||||
const finalGenerator = options.transform
|
||||
const finalGenerator = helper.options.transform
|
||||
? this.streamTransformPipeline.transform(
|
||||
initialGenerator,
|
||||
prefix,
|
||||
suffix,
|
||||
filepath,
|
||||
helper.filepath,
|
||||
multiline,
|
||||
pos,
|
||||
fileLines,
|
||||
helper.pos,
|
||||
helper.fileLines,
|
||||
completionOptions?.stop || [],
|
||||
lang,
|
||||
helper.lang,
|
||||
fullStop,
|
||||
)
|
||||
: initialGenerator;
|
||||
|
|
@ -464,7 +434,7 @@ export class CompletionProvider {
|
|||
return undefined;
|
||||
}
|
||||
|
||||
const processedCompletion = options.transform
|
||||
const processedCompletion = helper.options.transform
|
||||
? postprocessCompletion({
|
||||
completion,
|
||||
prefix,
|
||||
|
|
@ -492,11 +462,11 @@ export class CompletionProvider {
|
|||
modelName: llm.model,
|
||||
completionOptions,
|
||||
cacheHit,
|
||||
filepath: input.filepath,
|
||||
completionId: input.completionId,
|
||||
gitRepo: await this.ide.getRepoName(input.filepath),
|
||||
filepath: helper.filepath,
|
||||
completionId: helper.input.completionId,
|
||||
gitRepo: await this.ide.getRepoName(helper.filepath),
|
||||
uniqueId: await this.ide.getUniqueId(),
|
||||
...options,
|
||||
...helper.options,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
61
core/autocomplete/HelperVars.ts
Normal file
61
core/autocomplete/HelperVars.ts
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import { IDE, TabAutocompleteOptions } from "..";
|
||||
import { AutocompleteLanguageInfo } from "./constants/AutocompleteLanguageInfo";
|
||||
import { languageForFilepath } from "./constructPrompt";
|
||||
import { AutocompleteInput } from "./types";
|
||||
|
||||
/**
|
||||
* A collection of variables that are often accessed throughout the autocomplete pipeline
|
||||
* It's noisy to re-calculate all the time or inject them into each function
|
||||
*/
|
||||
export class HelperVars {
|
||||
lang: AutocompleteLanguageInfo;
|
||||
private _fileContents: string | undefined;
|
||||
private _fileLines: string[] | undefined;
|
||||
|
||||
constructor(
|
||||
public readonly input: AutocompleteInput,
|
||||
public readonly options: TabAutocompleteOptions,
|
||||
public readonly modelName: string,
|
||||
private readonly ide: IDE,
|
||||
) {
|
||||
this.lang = languageForFilepath(input.filepath);
|
||||
}
|
||||
|
||||
async init() {
|
||||
// Don't do anything if already initialized
|
||||
if (this._fileContents !== undefined) return;
|
||||
|
||||
this._fileContents =
|
||||
this.input.manuallyPassFileContents ??
|
||||
(await this.ide.readFile(this.filepath));
|
||||
|
||||
this._fileLines = this._fileContents.split("\n");
|
||||
}
|
||||
|
||||
// Fast access
|
||||
get filepath() {
|
||||
return this.input.filepath;
|
||||
}
|
||||
get pos() {
|
||||
return this.input.pos;
|
||||
}
|
||||
|
||||
// Getters for lazy access
|
||||
get fileContents(): string {
|
||||
if (this._fileContents === undefined) {
|
||||
throw new Error(
|
||||
"HelperVars must be initialized before accessing fileContents",
|
||||
);
|
||||
}
|
||||
return this._fileContents;
|
||||
}
|
||||
|
||||
get fileLines(): string[] {
|
||||
if (this._fileLines === undefined) {
|
||||
throw new Error(
|
||||
"HelperVars must be initialized before accessing fileLines",
|
||||
);
|
||||
}
|
||||
return this._fileLines;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,3 @@
|
|||
import { RangeInFileWithContents } from "../commands/util.js";
|
||||
import { TabAutocompleteOptions } from "../index.js";
|
||||
|
||||
import {
|
||||
countTokens,
|
||||
pruneLinesFromBottom,
|
||||
|
|
@ -21,7 +18,8 @@ import {
|
|||
type AutocompleteSnippet,
|
||||
} from "./context/ranking/index.js";
|
||||
import { RootPathContextService } from "./context/RootPathContextService.js";
|
||||
import { RecentlyEditedRange, findMatchingRange } from "./recentlyEdited.js";
|
||||
import { HelperVars } from "./HelperVars.js";
|
||||
import { findMatchingRange } from "./recentlyEdited.js";
|
||||
import { AstPath, getAst, getTreePathAtCursor } from "./util/ast.js";
|
||||
|
||||
export function languageForFilepath(
|
||||
|
|
@ -31,16 +29,9 @@ export function languageForFilepath(
|
|||
}
|
||||
|
||||
export async function constructAutocompletePrompt(
|
||||
filepath: string,
|
||||
cursorLine: number,
|
||||
fullPrefix: string,
|
||||
fullSuffix: string,
|
||||
clipboardText: string,
|
||||
language: AutocompleteLanguageInfo,
|
||||
options: TabAutocompleteOptions,
|
||||
recentlyEditedRanges: RecentlyEditedRange[],
|
||||
recentlyEditedFiles: RangeInFileWithContents[],
|
||||
modelName: string,
|
||||
helper: HelperVars,
|
||||
extraSnippets: AutocompleteSnippet[],
|
||||
importDefinitionsService: ImportDefinitionsService,
|
||||
rootPathContextService: RootPathContextService,
|
||||
|
|
@ -52,20 +43,29 @@ export async function constructAutocompletePrompt(
|
|||
snippets: AutocompleteSnippet[];
|
||||
}> {
|
||||
// Construct basic prefix
|
||||
const maxPrefixTokens = options.maxPromptTokens * options.prefixPercentage;
|
||||
const prefix = pruneLinesFromTop(fullPrefix, maxPrefixTokens, modelName);
|
||||
const maxPrefixTokens =
|
||||
helper.options.maxPromptTokens * helper.options.prefixPercentage;
|
||||
const prefix = pruneLinesFromTop(
|
||||
fullPrefix,
|
||||
maxPrefixTokens,
|
||||
helper.modelName,
|
||||
);
|
||||
|
||||
// Construct suffix
|
||||
const maxSuffixTokens = Math.min(
|
||||
options.maxPromptTokens - countTokens(prefix, modelName),
|
||||
options.maxSuffixPercentage * options.maxPromptTokens,
|
||||
helper.options.maxPromptTokens - countTokens(prefix, helper.modelName),
|
||||
helper.options.maxSuffixPercentage * helper.options.maxPromptTokens,
|
||||
);
|
||||
const suffix = pruneLinesFromBottom(
|
||||
fullSuffix,
|
||||
maxSuffixTokens,
|
||||
helper.modelName,
|
||||
);
|
||||
const suffix = pruneLinesFromBottom(fullSuffix, maxSuffixTokens, modelName);
|
||||
|
||||
// Calculate AST Path
|
||||
let treePath: AstPath | undefined;
|
||||
try {
|
||||
const ast = await getAst(filepath, fullPrefix + fullSuffix);
|
||||
const ast = await getAst(helper.filepath, fullPrefix + fullSuffix);
|
||||
if (ast) {
|
||||
treePath = await getTreePathAtCursor(ast, fullPrefix.length);
|
||||
}
|
||||
|
|
@ -76,15 +76,17 @@ export async function constructAutocompletePrompt(
|
|||
// Find external snippets
|
||||
let snippets: AutocompleteSnippet[] = [];
|
||||
|
||||
if (options.useOtherFiles) {
|
||||
if (helper.options.useOtherFiles) {
|
||||
snippets.push(...extraSnippets);
|
||||
|
||||
const windowAroundCursor =
|
||||
fullPrefix.slice(
|
||||
-options.slidingWindowSize * options.slidingWindowPrefixPercentage,
|
||||
-helper.options.slidingWindowSize *
|
||||
helper.options.slidingWindowPrefixPercentage,
|
||||
) +
|
||||
fullSuffix.slice(
|
||||
options.slidingWindowSize * (1 - options.slidingWindowPrefixPercentage),
|
||||
helper.options.slidingWindowSize *
|
||||
(1 - helper.options.slidingWindowPrefixPercentage),
|
||||
);
|
||||
|
||||
// This was much too slow, and not super useful
|
||||
|
|
@ -103,11 +105,14 @@ export async function constructAutocompletePrompt(
|
|||
// })),
|
||||
// );
|
||||
|
||||
if (options.useRecentlyEdited) {
|
||||
if (helper.options.useRecentlyEdited) {
|
||||
const currentLinePrefix = prefix.trim().split("\n").slice(-1)[0];
|
||||
if (currentLinePrefix?.length > options.recentLinePrefixMatchMinLength) {
|
||||
if (
|
||||
currentLinePrefix?.length >
|
||||
helper.options.recentLinePrefixMatchMinLength
|
||||
) {
|
||||
const matchingRange = findMatchingRange(
|
||||
recentlyEditedRanges,
|
||||
helper.input.recentlyEditedRanges,
|
||||
currentLinePrefix,
|
||||
);
|
||||
if (matchingRange) {
|
||||
|
|
@ -121,9 +126,9 @@ export async function constructAutocompletePrompt(
|
|||
}
|
||||
|
||||
// Use imports
|
||||
if (options.useImports) {
|
||||
if (helper.options.useImports) {
|
||||
const importSnippets = [];
|
||||
const fileInfo = importDefinitionsService.get(filepath);
|
||||
const fileInfo = importDefinitionsService.get(helper.filepath);
|
||||
if (fileInfo) {
|
||||
const { imports } = fileInfo;
|
||||
// Look for imports of any symbols around the current range
|
||||
|
|
@ -132,7 +137,7 @@ export async function constructAutocompletePrompt(
|
|||
fullSuffix.split("\n").slice(0, 3).join("\n");
|
||||
const symbols = Array.from(
|
||||
getSymbolsForSnippet(textAroundCursor),
|
||||
).filter((symbol) => !language.topLevelKeywords.includes(symbol));
|
||||
).filter((symbol) => !helper.lang.topLevelKeywords.includes(symbol));
|
||||
for (const symbol of symbols) {
|
||||
const rifs = imports[symbol];
|
||||
if (Array.isArray(rifs)) {
|
||||
|
|
@ -143,9 +148,9 @@ export async function constructAutocompletePrompt(
|
|||
snippets.push(...importSnippets);
|
||||
}
|
||||
|
||||
if (options.useRootPathContext && treePath) {
|
||||
if (helper.options.useRootPathContext && treePath) {
|
||||
const ctx = await rootPathContextService.getContextForPath(
|
||||
filepath,
|
||||
helper.filepath,
|
||||
treePath,
|
||||
);
|
||||
snippets.push(...ctx);
|
||||
|
|
@ -165,7 +170,7 @@ export async function constructAutocompletePrompt(
|
|||
|
||||
// Fill maxSnippetTokens with snippets
|
||||
const maxSnippetTokens =
|
||||
options.maxPromptTokens * options.maxSnippetPercentage;
|
||||
helper.options.maxPromptTokens * helper.options.maxSnippetPercentage;
|
||||
|
||||
// Remove prefix range from snippets
|
||||
const prefixLines = prefix.split("\n").length;
|
||||
|
|
@ -173,28 +178,29 @@ export async function constructAutocompletePrompt(
|
|||
const buffer = 8;
|
||||
const prefixSuffixRangeWithBuffer = {
|
||||
start: {
|
||||
line: cursorLine - prefixLines - buffer,
|
||||
line: helper.pos.line - prefixLines - buffer,
|
||||
character: 0,
|
||||
},
|
||||
end: {
|
||||
line: cursorLine + suffixLines + buffer,
|
||||
line: helper.pos.line + suffixLines + buffer,
|
||||
character: 0,
|
||||
},
|
||||
};
|
||||
let finalSnippets = removeRangeFromSnippets(
|
||||
scoredSnippets,
|
||||
filepath.split("://").slice(-1)[0],
|
||||
helper.filepath.split("://").slice(-1)[0],
|
||||
prefixSuffixRangeWithBuffer,
|
||||
);
|
||||
|
||||
// Filter snippets for those with best scores (must be above threshold)
|
||||
finalSnippets = finalSnippets.filter(
|
||||
(snippet) => snippet.score >= options.recentlyEditedSimilarityThreshold,
|
||||
(snippet) =>
|
||||
snippet.score >= helper.options.recentlyEditedSimilarityThreshold,
|
||||
);
|
||||
finalSnippets = fillPromptWithSnippets(
|
||||
scoredSnippets,
|
||||
maxSnippetTokens,
|
||||
modelName,
|
||||
helper.modelName,
|
||||
);
|
||||
|
||||
snippets = finalSnippets;
|
||||
|
|
@ -208,7 +214,7 @@ export async function constructAutocompletePrompt(
|
|||
treePath,
|
||||
fullPrefix,
|
||||
fullSuffix,
|
||||
language,
|
||||
helper.lang,
|
||||
),
|
||||
snippets,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
import path from "node:path";
|
||||
import { IDE, Position, TabAutocompleteOptions } from "../..";
|
||||
import { IDE } from "../..";
|
||||
import { getBasename } from "../../util";
|
||||
import { getConfigJsonPath } from "../../util/paths";
|
||||
import { languageForFilepath } from "../constructPrompt";
|
||||
import { AutocompleteInput } from "../types";
|
||||
import { HelperVars } from "../HelperVars";
|
||||
|
||||
async function isDisabledForFile(
|
||||
currentFilepath: string,
|
||||
|
|
@ -38,52 +37,39 @@ async function isDisabledForFile(
|
|||
}
|
||||
}
|
||||
|
||||
async function shouldLanguageSpecificPrefilter(
|
||||
manuallyPassFileContents: string | undefined,
|
||||
filepath: string,
|
||||
pos: Position,
|
||||
ide: IDE,
|
||||
) {
|
||||
const lang = languageForFilepath(filepath);
|
||||
const fileContents =
|
||||
manuallyPassFileContents ?? (await ide.readFile(filepath));
|
||||
const fileLines = fileContents.split("\n");
|
||||
const line = fileLines[pos.line] ?? "";
|
||||
for (const endOfLine of lang.endOfLine) {
|
||||
if (line.endsWith(endOfLine) && pos.character >= line.length) {
|
||||
async function shouldLanguageSpecificPrefilter(helper: HelperVars) {
|
||||
const line = helper.fileLines[helper.pos.line] ?? "";
|
||||
for (const endOfLine of helper.lang.endOfLine) {
|
||||
if (line.endsWith(endOfLine) && helper.pos.character >= line.length) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function shouldPrefilter(
|
||||
input: AutocompleteInput,
|
||||
options: TabAutocompleteOptions,
|
||||
helper: HelperVars,
|
||||
ide: IDE,
|
||||
): Promise<boolean> {
|
||||
// Allow disabling autocomplete from config.json
|
||||
if (options.disable) {
|
||||
if (helper.options.disable) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check whether we're in the continue config.json file
|
||||
if (input.filepath === getConfigJsonPath()) {
|
||||
if (helper.filepath === getConfigJsonPath()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check whether autocomplete is disabled for this file
|
||||
if (await isDisabledForFile(input.filepath, options.disableInFiles, ide)) {
|
||||
if (
|
||||
await isDisabledForFile(helper.filepath, helper.options.disableInFiles, ide)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (
|
||||
options.transform &&
|
||||
(await shouldLanguageSpecificPrefilter(
|
||||
input.manuallyPassFileContents,
|
||||
input.filepath,
|
||||
input.pos,
|
||||
ide,
|
||||
))
|
||||
helper.options.transform &&
|
||||
(await shouldLanguageSpecificPrefilter(helper))
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
43
core/autocomplete/templating/constructPrefixSuffix.ts
Normal file
43
core/autocomplete/templating/constructPrefixSuffix.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import { IDE } from "../..";
|
||||
import { getRangeInString } from "../../util/ranges";
|
||||
import { languageForFilepath } from "../constructPrompt";
|
||||
import { AutocompleteInput } from "../types";
|
||||
|
||||
/**
|
||||
* We have to handle a few edge cases in getting the entire prefix/suffix for the current file.
|
||||
* This is entirely prior to finding snippets from other files
|
||||
*/
|
||||
export async function constructInitialPrefixSuffix(
|
||||
input: AutocompleteInput,
|
||||
ide: IDE,
|
||||
): Promise<{
|
||||
prefix: string;
|
||||
suffix: string;
|
||||
}> {
|
||||
const lang = languageForFilepath(input.filepath);
|
||||
|
||||
const fileContents =
|
||||
input.manuallyPassFileContents ?? (await ide.readFile(input.filepath));
|
||||
const fileLines = fileContents.split("\n");
|
||||
let prefix =
|
||||
getRangeInString(fileContents, {
|
||||
start: { line: 0, character: 0 },
|
||||
end: input.selectedCompletionInfo?.range.start ?? input.pos,
|
||||
}) + (input.selectedCompletionInfo?.text ?? "");
|
||||
|
||||
if (input.injectDetails) {
|
||||
const lines = prefix.split("\n");
|
||||
prefix = `${lines.slice(0, -1).join("\n")}\n${
|
||||
lang.singleLineComment
|
||||
} ${input.injectDetails
|
||||
.split("\n")
|
||||
.join(`\n${lang.singleLineComment} `)}\n${lines[lines.length - 1]}`;
|
||||
}
|
||||
|
||||
const suffix = getRangeInString(fileContents, {
|
||||
start: input.pos,
|
||||
end: { line: fileLines.length - 1, character: Number.MAX_SAFE_INTEGER },
|
||||
});
|
||||
|
||||
return { prefix, suffix };
|
||||
}
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
import { CompletionOptions, TabAutocompleteOptions } from "../..";
|
||||
import { CompletionOptions } from "../..";
|
||||
import { getBasename, getLastNPathParts } from "../../util";
|
||||
import { decideMultilineEarly } from "../classification/shouldCompleteMultiline";
|
||||
import { AutocompleteLanguageInfo } from "../constants/AutocompleteLanguageInfo";
|
||||
import { AutocompleteSnippet } from "../context/ranking";
|
||||
import { AutocompleteInput } from "../types";
|
||||
import { HelperVars } from "../HelperVars";
|
||||
import { getTemplateForModel } from "./AutocompleteTemplate";
|
||||
import { getStopTokens } from "./getStopTokens";
|
||||
|
||||
|
|
@ -61,25 +61,20 @@ function renderStringTemplate(
|
|||
return prompt;
|
||||
}
|
||||
export function renderPrompt(
|
||||
options: TabAutocompleteOptions,
|
||||
prefix: string,
|
||||
suffix: string,
|
||||
filepath: string,
|
||||
lang: AutocompleteLanguageInfo,
|
||||
snippets: AutocompleteSnippet[],
|
||||
model: string,
|
||||
workspaceDirs: string[],
|
||||
userDefinedTemplate: string | undefined,
|
||||
selectedCompletionInfo: AutocompleteInput["selectedCompletionInfo"],
|
||||
completeMultiline: boolean,
|
||||
helper: HelperVars,
|
||||
): [string, Partial<CompletionOptions> | undefined, boolean] {
|
||||
let {
|
||||
template,
|
||||
completionOptions,
|
||||
compilePrefixSuffix = undefined,
|
||||
} = userDefinedTemplate
|
||||
? { template: userDefinedTemplate, completionOptions: {} }
|
||||
: getTemplateForModel(model);
|
||||
} = helper.options.template
|
||||
? { template: helper.options.template, completionOptions: {} }
|
||||
: getTemplateForModel(helper.modelName);
|
||||
|
||||
let prompt: string;
|
||||
const reponame = getBasename(workspaceDirs[0] ?? "myproject");
|
||||
|
|
@ -90,7 +85,7 @@ export function renderPrompt(
|
|||
[prefix, suffix] = compilePrefixSuffix(
|
||||
prefix,
|
||||
suffix,
|
||||
filepath,
|
||||
helper.filepath,
|
||||
reponame,
|
||||
snippets,
|
||||
);
|
||||
|
|
@ -103,26 +98,37 @@ export function renderPrompt(
|
|||
prefix,
|
||||
suffix,
|
||||
snippets,
|
||||
lang,
|
||||
filepath,
|
||||
helper.lang,
|
||||
helper.filepath,
|
||||
reponame,
|
||||
);
|
||||
} else {
|
||||
prompt = template(prefix, suffix, filepath, reponame, lang.name, snippets);
|
||||
prompt = template(
|
||||
prefix,
|
||||
suffix,
|
||||
helper.filepath,
|
||||
reponame,
|
||||
helper.lang.name,
|
||||
snippets,
|
||||
);
|
||||
}
|
||||
|
||||
const multiline =
|
||||
!options.transform ||
|
||||
!helper.options.transform ||
|
||||
decideMultilineEarly({
|
||||
multilineCompletions: options.multilineCompletions,
|
||||
language: lang,
|
||||
selectedCompletionInfo: selectedCompletionInfo,
|
||||
multilineCompletions: helper.options.multilineCompletions,
|
||||
language: helper.lang,
|
||||
selectedCompletionInfo: helper.input.selectedCompletionInfo,
|
||||
prefix,
|
||||
suffix,
|
||||
completeMultiline,
|
||||
});
|
||||
|
||||
const stopTokens = getStopTokens(completionOptions, lang, model);
|
||||
const stopTokens = getStopTokens(
|
||||
completionOptions,
|
||||
helper.lang,
|
||||
helper.modelName,
|
||||
);
|
||||
|
||||
completionOptions = {
|
||||
...completionOptions,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue