diff --git a/core/autocomplete/constructPrompt.ts b/core/autocomplete/constructPrompt.ts index 8c608ba71..1b5dab648 100644 --- a/core/autocomplete/constructPrompt.ts +++ b/core/autocomplete/constructPrompt.ts @@ -10,7 +10,7 @@ import { getBasename } from "../util"; import { getAst, getScopeAroundRange, getTreePathAtCursor } from "./ast"; import { AutocompleteLanguageInfo, LANGUAGES, Typescript } from "./languages"; -import { rankSnippets } from "./ranking"; +import { AutocompleteSnippet, fillPromptWithSnippets, rankSnippets } from "./ranking"; import { slidingWindowMatcher } from "./slidingWindow"; export function languageForFilepath( @@ -94,14 +94,11 @@ export async function constructAutocompletePrompt( fullSuffix: string, clipboardText: string, language: AutocompleteLanguageInfo, - getDefinitions: ( - document: RangeInFileWithContents, - cursorIndex: number - ) => Promise, options: TabAutocompleteOptions, recentlyEditedRanges: RangeInFileWithContents[], recentlyEditedDocuments: RangeInFileWithContents[], - modelName: string + modelName: string, + extraSnippets: AutocompleteSnippet[] ): Promise<{ prefix: string; suffix: string; @@ -109,7 +106,7 @@ export async function constructAutocompletePrompt( completeMultiline: boolean; }> { // Find external snippets - let snippets: RangeInFileWithContents[] = []; + let snippets: RangeInFileWithContents[] = extraSnippets; const windowAroundCursor = fullPrefix.slice( @@ -140,15 +137,17 @@ export async function constructAutocompletePrompt( snippets.push(...(recentlyEdited as any)); // Rank / order the snippets - snippets = rankSnippets(snippets, windowAroundCursor).filter( + const scoredSnippets = rankSnippets(snippets, windowAroundCursor).filter( // Filter out snippets that are already in prefix (snippet) => snippet.score < 0.98 ); - // How to add snippets to the prefix? Count separately? Always keep some of the prefix?? + // Fill maxSnippetTokens with snippets + const maxSnippetTokens = options.maxPromptTokens * options.maxSnippetPercentage; + const finalSnippets = fillPromptWithSnippets(scoredSnippets, maxSnippetTokens, modelName); // Construct basic prefix / suffix - const formattedSnippets = snippets + const formattedSnippets = finalSnippets .map((snippet) => formatExternalSnippet(snippet.filepath, snippet.contents, language) ) diff --git a/core/autocomplete/index.ts b/core/autocomplete/index.ts deleted file mode 100644 index e69de29bb..000000000 diff --git a/core/autocomplete/parameters.ts b/core/autocomplete/parameters.ts index f41184088..18dc7f244 100644 --- a/core/autocomplete/parameters.ts +++ b/core/autocomplete/parameters.ts @@ -10,4 +10,5 @@ export const DEFAULT_AUTOCOMPLETE_OPTS: TabAutocompleteOptions = { multilineCompletions: "auto", slidingWindowPrefixPercentage: 0.75, slidingWindowSize: 500, + maxSnippetPercentage: 0.6 }; diff --git a/core/autocomplete/ranking.ts b/core/autocomplete/ranking.ts index 81da512eb..cb87f2e34 100644 --- a/core/autocomplete/ranking.ts +++ b/core/autocomplete/ranking.ts @@ -1,4 +1,5 @@ import { RangeInFileWithContents } from "../commands/util"; +import { countTokens } from "../llm/countTokens"; export type AutocompleteSnippet = RangeInFileWithContents & { score: number; @@ -110,3 +111,24 @@ function mergeOverlappingRangeContents( const numOverlapping = first.range.end.line - second.range.start.line; return firstLines.slice(-numOverlapping).join("\n") + "\n" + second.contents; } + + +/** + * Fill the allowed space with snippets + */ +export function fillPromptWithSnippets(snippets: AutocompleteSnippet[], maxSnippetTokens: number, modelName: string): AutocompleteSnippet[] { + let tokensRemaining = maxSnippetTokens; + const keptSnippets: AutocompleteSnippet[] = [] + for (let i = 0; i < snippets.length; i++) { + const snippet = snippets[i]; + const tokenCount = countTokens(snippet.contents, modelName) + if (tokensRemaining - tokenCount >= 0) { + tokensRemaining -= tokenCount; + keptSnippets.push(snippet); + } else { + continue; + } + } + + return keptSnippets; +}; \ No newline at end of file diff --git a/core/config/default.ts b/core/config/default.ts index d80e0ef7e..3cf7d4390 100644 --- a/core/config/default.ts +++ b/core/config/default.ts @@ -59,6 +59,11 @@ export const defaultConfig: SerializedContinueConfig = { { name: "problems", params: {} }, { name: "codebase", params: {} }, ], + tabAutocompleteModel: { + title: "Starcoder 3b", + provider: "ollama", + model: "starcoder-3b", + } }; export const defaultConfigJetBrains: SerializedContinueConfig = { diff --git a/core/index.d.ts b/core/index.d.ts index 65e56bfd5..3eb295151 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -588,6 +588,7 @@ export interface TabAutocompleteOptions { multilineCompletions: "always" | "never" | "auto"; slidingWindowPrefixPercentage: number; slidingWindowSize: number; + maxSnippetPercentage: number; } export interface SerializedContinueConfig { diff --git a/extensions/vscode/src/autocomplete/getTabCompletion.ts b/extensions/vscode/src/autocomplete/getTabCompletion.ts index dda474b44..a7ec82a0e 100644 --- a/extensions/vscode/src/autocomplete/getTabCompletion.ts +++ b/extensions/vscode/src/autocomplete/getTabCompletion.ts @@ -9,6 +9,7 @@ import { stopAtSimilarLine, streamWithNewLines, } from "core/autocomplete/lineStream"; +import { AutocompleteSnippet } from "core/autocomplete/ranking"; import { getTemplateForModel } from "core/autocomplete/templates"; import { GeneratorReuseManager } from "core/autocomplete/util"; import { streamLines } from "core/diff/util"; @@ -76,6 +77,14 @@ export async function getTabCompletion( Math.min(pos.line + 1, document.lineCount - 1) ).text; const clipboardText = await vscode.env.clipboard.readText(); + + let extrasSnippets = (await Promise.race([ + getDefinitionsFromLsp(document.uri.fsPath, fullPrefix + fullSuffix, fullPrefix.length, ide), + new Promise((resolve) => { + setTimeout(() => resolve([]), 100); + }) + ])) as AutocompleteSnippet[] + const { prefix, suffix, completeMultiline } = await constructAutocompletePrompt( document.uri.toString(), @@ -83,11 +92,11 @@ export async function getTabCompletion( fullSuffix, clipboardText, lang, - (document, cursorIndex) => getDefinitionsFromLsp(document, cursorIndex, ide), options, await recentlyEditedTracker.getRecentlyEditedRanges(), await recentlyEditedTracker.getRecentlyEditedDocuments(), - llm.model + llm.model, + extrasSnippets ); const { template, completionOptions } = options.template diff --git a/extensions/vscode/src/autocomplete/lsp.ts b/extensions/vscode/src/autocomplete/lsp.ts index d21763424..a949d06ab 100644 --- a/extensions/vscode/src/autocomplete/lsp.ts +++ b/extensions/vscode/src/autocomplete/lsp.ts @@ -1,5 +1,6 @@ import { IDE, RangeInFile } from "core"; import { getAst, getTreePathAtCursor } from "core/autocomplete/ast"; +import { AutocompleteSnippet } from "core/autocomplete/ranking"; import { RangeInFileWithContents } from "core/commands/util"; import * as vscode from "vscode"; import Parser from "web-tree-sitter"; @@ -65,11 +66,12 @@ async function getDefinitionsForNode( */ export async function getDefinitionsFromLsp( - document: RangeInFileWithContents, + filepath: string, + contents: string, cursorIndex: number, ide: IDE -): Promise { - const ast = await getAst(document.filepath, document.contents); +): Promise { + const ast = await getAst(filepath, contents); if (!ast) return []; const treePath = await getTreePathAtCursor(ast, cursorIndex); @@ -77,7 +79,7 @@ export async function getDefinitionsFromLsp( const results: RangeInFileWithContents[] = []; for (const node of treePath.reverse()) { - const definitions = await getDefinitionsForNode(document.filepath, node); + const definitions = await getDefinitionsForNode(filepath, node); results.push( ...(await Promise.all( definitions.map(async (def) => ({ @@ -97,5 +99,8 @@ export async function getDefinitionsFromLsp( ); } - return results; + return results.map((result) => ({ + ...result, + score: 0.8 + })); } diff --git a/extensions/vscode/src/lang-server/autocompletePromptBuilder.ts b/extensions/vscode/src/lang-server/autocompletePromptBuilder.ts deleted file mode 100644 index 8a8895f55..000000000 --- a/extensions/vscode/src/lang-server/autocompletePromptBuilder.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { IDE } from "core"; -import { AutocompleteSnippet } from "core/autocomplete/ranking"; -import * as vscode from "vscode"; - -export class AutocompletePromptBuilder { - constructor(private readonly ide: IDE) {} - - async getDefinition( - uri: string, - line: number, - character: number - ): Promise { - const definitions = (await vscode.commands.executeCommand( - "vscode.executeDefinitionProvider", - vscode.Uri.parse(uri), - new vscode.Position(line, character) - )) as any; - - if (definitions[0]?.targetRange) { - return { - filepath: uri, - contents: await this.ide.readRangeInFile( - definitions[0].targetUri.fsPath, - definitions[0].targetRange - ), - range: definitions[0].targetRange, - score: 0.8 - }; - } - - return undefined; - } -}