refactor context retrieval service
This commit is contained in:
parent
bf13f20042
commit
ef4b32975d
2 changed files with 90 additions and 62 deletions
|
|
@ -88,7 +88,7 @@ export async function constructAutocompletePrompt(
|
|||
// Prune prefix/suffix based on token budgets
|
||||
const { prunedPrefix, prunedSuffix } = prunePrefixSuffix(helper);
|
||||
|
||||
let snippets = await contextRetrievalService.retrieve(
|
||||
let snippets = await contextRetrievalService.retrieveCandidateSnippets(
|
||||
prunedPrefix,
|
||||
helper,
|
||||
extraSnippets,
|
||||
|
|
|
|||
|
|
@ -5,17 +5,6 @@ import { ImportDefinitionsService } from "./ImportDefinitionsService";
|
|||
import { AutocompleteSnippet, getSymbolsForSnippet } from "./ranking";
|
||||
import { RootPathContextService } from "./RootPathContextService";
|
||||
|
||||
export function findMatchingRange(
|
||||
recentlyEditedRanges: RecentlyEditedRange[],
|
||||
linePrefix: string,
|
||||
): RecentlyEditedRange | undefined {
|
||||
return recentlyEditedRanges.find((recentlyEditedRange) => {
|
||||
return recentlyEditedRange.lines.some((line) =>
|
||||
line.startsWith(linePrefix),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export class ContextRetrievalService {
|
||||
private importDefinitionsService: ImportDefinitionsService;
|
||||
private rootPathContextService: RootPathContextService;
|
||||
|
|
@ -28,69 +17,108 @@ export class ContextRetrievalService {
|
|||
);
|
||||
}
|
||||
|
||||
public async retrieve(
|
||||
private getSnippetsFromRecentlyEditedRanges(
|
||||
prunedPrefix: string,
|
||||
helper: HelperVars,
|
||||
): AutocompleteSnippet[] {
|
||||
if (helper.options.useRecentlyEdited === false) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const currentLinePrefix = prunedPrefix.trim().split("\n").slice(-1)[0];
|
||||
if (
|
||||
currentLinePrefix?.length > helper.options.recentLinePrefixMatchMinLength
|
||||
) {
|
||||
const matchingRange = this.findMatchingRange(
|
||||
helper.input.recentlyEditedRanges,
|
||||
currentLinePrefix,
|
||||
);
|
||||
|
||||
if (matchingRange) {
|
||||
return [
|
||||
{
|
||||
...matchingRange,
|
||||
contents: matchingRange.lines.join("\n"),
|
||||
score: 0.8,
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
private async getSnippetsFromImportDefinitions(
|
||||
helper: HelperVars,
|
||||
): Promise<AutocompleteSnippet[]> {
|
||||
if (helper.options.useImports === false) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const importSnippets = [];
|
||||
const fileInfo = this.importDefinitionsService.get(helper.filepath);
|
||||
if (fileInfo) {
|
||||
const { imports } = fileInfo;
|
||||
// Look for imports of any symbols around the current range
|
||||
const textAroundCursor =
|
||||
helper.fullPrefix.split("\n").slice(-5).join("\n") +
|
||||
helper.fullSuffix.split("\n").slice(0, 3).join("\n");
|
||||
const symbols = Array.from(getSymbolsForSnippet(textAroundCursor)).filter(
|
||||
(symbol) => !helper.lang.topLevelKeywords.includes(symbol),
|
||||
);
|
||||
for (const symbol of symbols) {
|
||||
const rifs = imports[symbol];
|
||||
if (Array.isArray(rifs)) {
|
||||
importSnippets.push(...rifs);
|
||||
}
|
||||
}
|
||||
}
|
||||
return importSnippets;
|
||||
}
|
||||
|
||||
public async retrieveCandidateSnippets(
|
||||
prunedPrefix: string,
|
||||
helper: HelperVars,
|
||||
extraSnippets: AutocompleteSnippet[],
|
||||
) {
|
||||
// Find external snippets
|
||||
if (helper.options.useOtherFiles === false) {
|
||||
return [];
|
||||
}
|
||||
|
||||
let snippets: AutocompleteSnippet[] = [];
|
||||
|
||||
if (helper.options.useOtherFiles) {
|
||||
snippets.push(...extraSnippets);
|
||||
// Snippets injected by the IDE for IDE-specific reasons
|
||||
snippets.push(...extraSnippets);
|
||||
|
||||
if (helper.options.useRecentlyEdited) {
|
||||
const currentLinePrefix = prunedPrefix.trim().split("\n").slice(-1)[0];
|
||||
if (
|
||||
currentLinePrefix?.length >
|
||||
helper.options.recentLinePrefixMatchMinLength
|
||||
) {
|
||||
const matchingRange = findMatchingRange(
|
||||
helper.input.recentlyEditedRanges,
|
||||
currentLinePrefix,
|
||||
);
|
||||
if (matchingRange) {
|
||||
snippets.push({
|
||||
...matchingRange,
|
||||
contents: matchingRange.lines.join("\n"),
|
||||
score: 0.8,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
// If a recently edited range has a line that is a perfect match with the start of the current line
|
||||
snippets.push(
|
||||
...this.getSnippetsFromRecentlyEditedRanges(prunedPrefix, helper),
|
||||
);
|
||||
|
||||
// Use imports
|
||||
if (helper.options.useImports) {
|
||||
const importSnippets = [];
|
||||
const fileInfo = this.importDefinitionsService.get(helper.filepath);
|
||||
if (fileInfo) {
|
||||
const { imports } = fileInfo;
|
||||
// Look for imports of any symbols around the current range
|
||||
const textAroundCursor =
|
||||
helper.fullPrefix.split("\n").slice(-5).join("\n") +
|
||||
helper.fullSuffix.split("\n").slice(0, 3).join("\n");
|
||||
const symbols = Array.from(
|
||||
getSymbolsForSnippet(textAroundCursor),
|
||||
).filter((symbol) => !helper.lang.topLevelKeywords.includes(symbol));
|
||||
for (const symbol of symbols) {
|
||||
const rifs = imports[symbol];
|
||||
if (Array.isArray(rifs)) {
|
||||
importSnippets.push(...rifs);
|
||||
}
|
||||
}
|
||||
}
|
||||
snippets.push(...importSnippets);
|
||||
}
|
||||
// Import definitions of any symbols in near range of the caret
|
||||
snippets.push(...(await this.getSnippetsFromImportDefinitions(helper)));
|
||||
|
||||
if (helper.options.useRootPathContext && helper.treePath) {
|
||||
const ctx = await this.rootPathContextService.getContextForPath(
|
||||
// Root path context https://blog.continue.dev/root-path-context-the-secret-ingredient-in-continues-autocomplete-prompt/
|
||||
if (helper.options.useRootPathContext && helper.treePath) {
|
||||
snippets.push(
|
||||
...(await this.rootPathContextService.getContextForPath(
|
||||
helper.filepath,
|
||||
helper.treePath,
|
||||
);
|
||||
snippets.push(...ctx);
|
||||
}
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
return snippets;
|
||||
}
|
||||
|
||||
findMatchingRange(
|
||||
recentlyEditedRanges: RecentlyEditedRange[],
|
||||
linePrefix: string,
|
||||
): RecentlyEditedRange | undefined {
|
||||
return recentlyEditedRanges.find((recentlyEditedRange) => {
|
||||
return recentlyEditedRange.lines.some((line) =>
|
||||
line.startsWith(linePrefix),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue