Merge branch 'main' of https://github.com/continuedev/continue into dallin/docs-ui
This commit is contained in:
commit
32c2845f57
4 changed files with 72 additions and 60 deletions
|
|
@ -6,7 +6,7 @@ import { DEFAULT_AUTOCOMPLETE_OPTS } from "../util/parameters.js";
|
|||
|
||||
import { shouldCompleteMultiline } from "./classification/shouldCompleteMultiline.js";
|
||||
import { AutocompleteLanguageInfo } from "./constants/AutocompleteLanguageInfo.js";
|
||||
import { constructAutocompletePrompt } from "./constructPrompt.js";
|
||||
import { aggregateSnippets } from "./aggregateSnippets";
|
||||
// @prettier-ignore
|
||||
|
||||
import { ContextRetrievalService } from "./context/ContextRetrievalService.js";
|
||||
|
|
@ -176,7 +176,7 @@ export class CompletionProvider {
|
|||
// or they might separately track recently edited ranges)
|
||||
const extraSnippets = await this._getExtraSnippets(helper);
|
||||
|
||||
let snippets = await constructAutocompletePrompt(
|
||||
let snippets = await aggregateSnippets(
|
||||
helper,
|
||||
extraSnippets,
|
||||
this.contextRetrievalService,
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ function getRangeOfPrefixAndSuffixWithBuffer(
|
|||
return prefixSuffixRangeWithBuffer;
|
||||
}
|
||||
|
||||
export async function constructAutocompletePrompt(
|
||||
export async function aggregateSnippets(
|
||||
helper: HelperVars,
|
||||
extraSnippets: AutocompleteSnippet[],
|
||||
contextRetrievalService: ContextRetrievalService,
|
||||
|
|
@ -28,42 +28,30 @@ export function formatExternalSnippet(
|
|||
];
|
||||
return lines.join("\n");
|
||||
}
|
||||
function renderStringTemplate(
|
||||
template: string,
|
||||
prefix: string,
|
||||
suffix: string,
|
||||
|
||||
function getContextComments(
|
||||
snippets: AutocompleteSnippet[],
|
||||
lang: AutocompleteLanguageInfo,
|
||||
filepath: string,
|
||||
reponame: string,
|
||||
prefix: string,
|
||||
suffix: string,
|
||||
) {
|
||||
const filename = getBasename(filepath);
|
||||
const compiledTemplate = Handlebars.compile(template);
|
||||
|
||||
// Format snippets as comments and prepend to prefix
|
||||
const formattedSnippets = snippets
|
||||
.map((snippet) =>
|
||||
formatExternalSnippet(snippet.filepath, snippet.contents, lang),
|
||||
)
|
||||
.join("\n");
|
||||
if (formattedSnippets.length > 0) {
|
||||
prefix = `${formattedSnippets}\n\n${prefix}`;
|
||||
} else if (prefix.trim().length === 0 && suffix.trim().length === 0) {
|
||||
// If it's an empty file, include the file name as a comment
|
||||
prefix = `${lang.singleLineComment} ${getLastNPathParts(
|
||||
filepath,
|
||||
2,
|
||||
)}\n${prefix}`;
|
||||
return `${formattedSnippets}\n\n`;
|
||||
}
|
||||
|
||||
const prompt = compiledTemplate({
|
||||
prefix,
|
||||
suffix,
|
||||
filename,
|
||||
reponame,
|
||||
language: lang.name,
|
||||
});
|
||||
return prompt;
|
||||
const isEmptyFile = prefix.trim().length === 0 && suffix.trim().length === 0;
|
||||
if (isEmptyFile) {
|
||||
// Include the file name as a comment
|
||||
prefix = `${lang.singleLineComment} ${getLastNPathParts(filepath, 2)}\n`;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
function getTemplate(helper: HelperVars): AutocompleteTemplate {
|
||||
|
|
@ -77,6 +65,26 @@ function getTemplate(helper: HelperVars): AutocompleteTemplate {
|
|||
return getTemplateForModel(helper.modelName);
|
||||
}
|
||||
|
||||
function renderStringTemplate(
|
||||
template: string,
|
||||
prefix: string,
|
||||
suffix: string,
|
||||
lang: AutocompleteLanguageInfo,
|
||||
filepath: string,
|
||||
reponame: string,
|
||||
) {
|
||||
const filename = getBasename(filepath);
|
||||
const compiledTemplate = Handlebars.compile(template);
|
||||
|
||||
return compiledTemplate({
|
||||
prefix,
|
||||
suffix,
|
||||
filename,
|
||||
reponame,
|
||||
language: lang.name,
|
||||
});
|
||||
}
|
||||
|
||||
export function renderPrompt(
|
||||
snippets: AutocompleteSnippet[],
|
||||
workspaceDirs: string[],
|
||||
|
|
@ -88,18 +96,12 @@ export function renderPrompt(
|
|||
completionOptions: Partial<CompletionOptions> | undefined;
|
||||
} {
|
||||
// If prefix is manually passed
|
||||
let prefix = helper.prunedPrefix;
|
||||
let suffix = helper.prunedSuffix;
|
||||
let prefix = helper.input.manuallyPassPrefix || helper.prunedPrefix;
|
||||
let suffix = helper.input.manuallyPassPrefix ? "" : helper.prunedSuffix;
|
||||
|
||||
if (helper.input.manuallyPassPrefix) {
|
||||
prefix = helper.input.manuallyPassPrefix;
|
||||
suffix = "";
|
||||
}
|
||||
|
||||
let prompt: string;
|
||||
const reponame = getBasename(workspaceDirs[0] ?? "myproject");
|
||||
|
||||
let { template, compilePrefixSuffix, completionOptions } =
|
||||
const { template, compilePrefixSuffix, completionOptions } =
|
||||
getTemplate(helper);
|
||||
|
||||
// Some models have prompts that need two passes. This lets us pass the compiled prefix/suffix
|
||||
|
|
@ -112,40 +114,50 @@ export function renderPrompt(
|
|||
reponame,
|
||||
snippets,
|
||||
);
|
||||
}
|
||||
|
||||
// Templates can be passed as a Handlebars template string or a function
|
||||
if (typeof template === "string") {
|
||||
prompt = renderStringTemplate(
|
||||
template,
|
||||
prefix,
|
||||
suffix,
|
||||
} else {
|
||||
const contextComments = getContextComments(
|
||||
snippets,
|
||||
helper.lang,
|
||||
helper.filepath,
|
||||
reponame,
|
||||
);
|
||||
} else {
|
||||
prompt = template(
|
||||
prefix,
|
||||
suffix,
|
||||
helper.filepath,
|
||||
reponame,
|
||||
helper.lang.name,
|
||||
snippets,
|
||||
);
|
||||
prefix = `${contextComments}${prefix}`;
|
||||
}
|
||||
|
||||
const prompt =
|
||||
// Templates can be passed as a Handlebars template string or a function
|
||||
typeof template === "string"
|
||||
? renderStringTemplate(
|
||||
template,
|
||||
prefix,
|
||||
suffix,
|
||||
helper.lang,
|
||||
helper.filepath,
|
||||
reponame,
|
||||
)
|
||||
: template(
|
||||
prefix,
|
||||
suffix,
|
||||
helper.filepath,
|
||||
reponame,
|
||||
helper.lang.name,
|
||||
snippets,
|
||||
);
|
||||
|
||||
const stopTokens = getStopTokens(
|
||||
completionOptions,
|
||||
helper.lang,
|
||||
helper.modelName,
|
||||
);
|
||||
|
||||
completionOptions = {
|
||||
...completionOptions,
|
||||
stop: stopTokens,
|
||||
return {
|
||||
prompt,
|
||||
prefix,
|
||||
suffix,
|
||||
completionOptions: {
|
||||
...completionOptions,
|
||||
stop: stopTokens,
|
||||
},
|
||||
};
|
||||
|
||||
return { prompt, prefix, suffix, completionOptions };
|
||||
}
|
||||
|
|
|
|||
6
core/package-lock.json
generated
6
core/package-lock.json
generated
|
|
@ -6827,9 +6827,9 @@
|
|||
}
|
||||
},
|
||||
"node_modules/cross-spawn": {
|
||||
"version": "7.0.3",
|
||||
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
|
||||
"integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==",
|
||||
"version": "7.0.6",
|
||||
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
|
||||
"integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==",
|
||||
"dependencies": {
|
||||
"path-key": "^3.1.0",
|
||||
"shebang-command": "^2.0.0",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue