From d12395838bd5e529015dd45484ff5384e99cde5e Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Fri, 25 Oct 2024 10:57:55 +0700 Subject: [PATCH] improv: fix failing tests, add 'transform' autocomplete option --- core/autocomplete/completionProvider.ts | 113 ++++++++++-------- core/autocomplete/test/filter.test.ts | 24 +++- core/autocomplete/test/testCases.ts | 51 ++++---- core/autocomplete/test/util.ts | 3 + core/diff/util.ts | 5 + core/index.d.ts | 2 +- core/util/parameters.ts | 1 + docs/static/schemas/config.json | 10 +- .../src/main/resources/config_schema.json | 10 +- extensions/vscode/config_schema.json | 10 +- extensions/vscode/continue_rc_schema.json | 4 + packages/config-types/src/index.ts | 1 + 12 files changed, 138 insertions(+), 96 deletions(-) diff --git a/core/autocomplete/completionProvider.ts b/core/autocomplete/completionProvider.ts index 893f9bc36..30d706c56 100644 --- a/core/autocomplete/completionProvider.ts +++ b/core/autocomplete/completionProvider.ts @@ -95,6 +95,7 @@ const autocompleteCache = AutocompleteLruCache.get(); const DOUBLE_NEWLINE = "\n\n"; const WINDOWS_DOUBLE_NEWLINE = "\r\n\r\n"; +// TODO: Do we want to stop completions when reaching a `/src/` string? const SRC_DIRECTORY = "/src/"; // Starcoder2 tends to output artifacts starting with the letter "t" const STARCODER2_T_ARTIFACTS = ["t.", "\nt", ""]; @@ -360,7 +361,7 @@ export class CompletionProvider { * elsewhere in the code. That said, I'm not yet confident enough to * remove this. */ - if (isOnlyWhitespace(outcome.completion)) { + if (options.transform && isOnlyWhitespace(outcome.completion)) { return undefined; } @@ -480,10 +481,13 @@ export class CompletionProvider { // Filter const lang = languageForFilepath(filepath); - const line = fileLines[pos.line] ?? ""; - for (const endOfLine of lang.endOfLine) { - if (line.endsWith(endOfLine) && pos.character >= line.length) { - return undefined; + + if (options.transform) { + const line = fileLines[pos.line] ?? ""; + for (const endOfLine of lang.endOfLine) { + if (line.endsWith(endOfLine) && pos.character >= line.length) { + return undefined; + } } } @@ -667,23 +671,25 @@ export class CompletionProvider { } else { const stop = [ ...(completionOptions?.stop || []), - ...multilineStops, + // ...multilineStops, ...commonStops, ...(llm.model.toLowerCase().includes("starcoder2") ? STARCODER2_T_ARTIFACTS : []), ...(lang.stopWords ?? []), - ...lang.topLevelKeywords.map((word) => `\n${word}`), + // ...lang.topLevelKeywords.map((word) => `\n${word}`), ]; - const multiline = this.isMultiline({ - multilineCompletions: options.multilineCompletions, - language: lang, - selectedCompletionInfo: input.selectedCompletionInfo, - prefix, - suffix, - completeMultiline, - }); + const multiline = + !options.transform || + this.isMultiline({ + multilineCompletions: options.multilineCompletions, + language: lang, + selectedCompletionInfo: input.selectedCompletionInfo, + prefix, + suffix, + completeMultiline, + }); // Try to reuse pending requests if what the user typed matches start of completion const generator = this.generatorReuseManager.getGenerator( @@ -718,39 +724,44 @@ export class CompletionProvider { } }; let charGenerator = generatorWithCancellation(); - charGenerator = noFirstCharNewline(charGenerator); - charGenerator = onlyWhitespaceAfterEndOfLine( - charGenerator, - lang.endOfLine, - fullStop, - ); - charGenerator = stopAtStopTokens(charGenerator, stop); - let lineGenerator = streamLines(charGenerator); - lineGenerator = stopAtLines(lineGenerator, fullStop); - lineGenerator = stopAtRepeatingLines(lineGenerator, fullStop); - lineGenerator = avoidPathLineAndEmptyComments( - lineGenerator, - lang.singleLineComment, - ); - lineGenerator = skipPrefixes(lineGenerator); - lineGenerator = noTopLevelKeywordsMidline( - lineGenerator, - lang.topLevelKeywords, - fullStop, - ); - - for (const lineFilter of lang.lineFilters ?? []) { - lineGenerator = lineFilter({ lines: lineGenerator, fullStop }); + if (options.transform) { + // charGenerator = noFirstCharNewline(charGenerator); + // charGenerator = onlyWhitespaceAfterEndOfLine( + // charGenerator, + // lang.endOfLine, + // fullStop, + // ); + charGenerator = stopAtStopTokens(charGenerator, stop); } - lineGenerator = streamWithNewLines(lineGenerator); + let lineGenerator = streamLines(charGenerator); + if (options.transform) { + lineGenerator = stopAtLines(lineGenerator, fullStop); + lineGenerator = stopAtRepeatingLines(lineGenerator, fullStop); + lineGenerator = avoidPathLineAndEmptyComments( + lineGenerator, + lang.singleLineComment, + ); + lineGenerator = skipPrefixes(lineGenerator); - const finalGenerator = stopAtSimilarLine( - lineGenerator, - lineBelowCursor, - fullStop, - ); + // lineGenerator = noTopLevelKeywordsMidline( + // lineGenerator, + // lang.topLevelKeywords, + // fullStop, + // ); + for (const lineFilter of lang.lineFilters ?? []) { + lineGenerator = lineFilter({ lines: lineGenerator, fullStop }); + } + + lineGenerator = stopAtSimilarLine( + lineGenerator, + lineBelowCursor, + fullStop, + ); + } + + const finalGenerator = streamWithNewLines(lineGenerator); try { for await (const update of finalGenerator) { @@ -767,12 +778,14 @@ export class CompletionProvider { return undefined; } - const processedCompletion = postprocessCompletion({ - completion, - prefix, - suffix, - llm, - }); + const processedCompletion = options.transform + ? postprocessCompletion({ + completion, + prefix, + suffix, + llm, + }) + : completion; if (!processedCompletion) { return undefined; diff --git a/core/autocomplete/test/filter.test.ts b/core/autocomplete/test/filter.test.ts index 502c0aaa3..171b1cb44 100644 --- a/core/autocomplete/test/filter.test.ts +++ b/core/autocomplete/test/filter.test.ts @@ -1,5 +1,16 @@ -import { testAutocompleteFiltering } from "./util"; -import { testCases } from "./testCases"; +import { + AutocompleteFileringTestInput, + testAutocompleteFiltering, +} from "./util"; +import { TEST_CASES } from "./testCases"; + +const filterTestCases = (tests: AutocompleteFileringTestInput[]) => { + if (tests.some((test) => test.options?.only)) { + return tests.filter((test) => test.options?.only); + } + + return tests; +}; describe("llms/Mock", () => { describe("Autocomplete Filtering Tests", () => { @@ -7,8 +18,13 @@ describe("llms/Mock", () => { await new Promise((resolve) => setTimeout(resolve, 50)); }); - it.each(testCases)("$description", async (testCase) => { - await testAutocompleteFiltering(testCase); + describe("Should return unmodified LLM output", () => { + it.each(filterTestCases(TEST_CASES))("$description", async (testCase) => { + await testAutocompleteFiltering(testCase); + }); }); + + // TODO: Add test cases where modifying LLM output is desirable + describe.skip("Should return modified LLM output", () => {}); }); }); diff --git a/core/autocomplete/test/testCases.ts b/core/autocomplete/test/testCases.ts index d171c9f5d..2deeec9de 100644 --- a/core/autocomplete/test/testCases.ts +++ b/core/autocomplete/test/testCases.ts @@ -1,7 +1,7 @@ import { dedent } from "../../util"; import { AutocompleteFileringTestInput } from "./util"; -export const testCases: AutocompleteFileringTestInput[] = [ +export const TEST_CASES: AutocompleteFileringTestInput[] = [ { description: "should pass", filename: "test.js", @@ -188,7 +188,6 @@ input:focus { llmOutput: `--primary-color`, expectedCompletion: `--primary-color`, }, - { description: "Should complete CSS grid template columns", filename: "grid.css", @@ -199,8 +198,8 @@ input:focus { grid-gap: 10px; } `, - llmOutput: `3, 1fr);`, - expectedCompletion: `3, 1fr);`, + llmOutput: `3, 1fr`, + expectedCompletion: `3, 1fr`, }, { @@ -1007,7 +1006,8 @@ fn get_status_code_description(code: u16) -> &'static str { `, expectedCompletion: `403 => "Forbidden", 401 => "Unauthorized", - _ => "Unknown Status",`, + _ => "Unknown Status", +`, }, { description: "Should autocomplete Rust struct definition", @@ -1955,9 +1955,9 @@ export default { expectedCompletion: `this.completed`, }, { - description: "Should autocomplete Svelte reactive statement", - filename: "Counter.svelte", - input: ` + description: "Should autocomplete Svelte reactive statement", + filename: "Counter.svelte", + input: ` @@ -1989,14 +1989,14 @@ export default { /> `, - llmOutput: `name="World"`, - expectedCompletion: `name="World"`, -}, + llmOutput: `name="World"`, + expectedCompletion: `name="World"`, + }, -{ - description: "Should handle autocomplete in Svelte each block", - filename: "List.svelte", - input: ` + { + description: "Should handle autocomplete in Svelte each block", + filename: "List.svelte", + input: ` @@ -2007,8 +2007,7 @@ export default { {/each<|fim|> `, - llmOutput: `}`, - expectedCompletion: `}`, -}, - + llmOutput: `}`, + expectedCompletion: `}`, + }, ]; diff --git a/core/autocomplete/test/util.ts b/core/autocomplete/test/util.ts index 8a65031a1..60e7b3f93 100644 --- a/core/autocomplete/test/util.ts +++ b/core/autocomplete/test/util.ts @@ -17,6 +17,9 @@ export interface AutocompleteFileringTestInput { input: string; llmOutput: string; expectedCompletion: string | null; + options?: { + only?: boolean; + }; } export async function testAutocompleteFiltering( diff --git a/core/diff/util.ts b/core/diff/util.ts index 6d218bcbf..1758bb704 100644 --- a/core/diff/util.ts +++ b/core/diff/util.ts @@ -110,6 +110,11 @@ export async function* streamLines( yield line; allLines.push(line); } + + if (buffer === "" && chunk.endsWith("\n")) { + yield ""; + allLines.push(""); + } } if (buffer.length > 0) { yield buffer; diff --git a/core/index.d.ts b/core/index.d.ts index e7c22db2f..7bbbaf8e0 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -650,7 +650,6 @@ type ModelProvider = | "mock" | "cerebras"; - export type ModelName = | "AUTODETECT" // OpenAI @@ -878,6 +877,7 @@ export interface TabAutocompleteOptions { debounceDelay: number; maxSuffixPercentage: number; prefixPercentage: number; + transform?: boolean; template?: string; multilineCompletions: "always" | "never" | "auto"; slidingWindowPrefixPercentage: number; diff --git a/core/util/parameters.ts b/core/util/parameters.ts index 88c7d38a6..4a99dec8c 100644 --- a/core/util/parameters.ts +++ b/core/util/parameters.ts @@ -21,6 +21,7 @@ export const DEFAULT_AUTOCOMPLETE_OPTS: TabAutocompleteOptions = { disableInFiles: undefined, useImports: true, useRootPathContext: true, + transform: true, }; export const COUNT_COMPLETION_REJECTED_AFTER = 10_000; diff --git a/docs/static/schemas/config.json b/docs/static/schemas/config.json index ce4e8e1a4..077bdcf93 100644 --- a/docs/static/schemas/config.json +++ b/docs/static/schemas/config.json @@ -1162,11 +1162,7 @@ "model": { "anyOf": [ { - "enum": [ - "codestral", - "mistral-nemo", - "mistral-large" - ] + "enum": ["codestral", "mistral-nemo", "mistral-large"] }, { "type": "string" } ] @@ -2712,6 +2708,10 @@ "type": "number", "description": "The percentage of the input that should be dedicated to the prefix." }, + "transform": { + "type": "boolean", + "description": "Whether LLM output should be transformed to correct common model pitfalls." + }, "template": { "type": "string", "description": "An optional template string to be used for autocomplete. It will be rendered with the Mustache templating language, and is passed the 'prefix' and 'suffix' variables." diff --git a/extensions/intellij/src/main/resources/config_schema.json b/extensions/intellij/src/main/resources/config_schema.json index ce4e8e1a4..077bdcf93 100644 --- a/extensions/intellij/src/main/resources/config_schema.json +++ b/extensions/intellij/src/main/resources/config_schema.json @@ -1162,11 +1162,7 @@ "model": { "anyOf": [ { - "enum": [ - "codestral", - "mistral-nemo", - "mistral-large" - ] + "enum": ["codestral", "mistral-nemo", "mistral-large"] }, { "type": "string" } ] @@ -2712,6 +2708,10 @@ "type": "number", "description": "The percentage of the input that should be dedicated to the prefix." }, + "transform": { + "type": "boolean", + "description": "Whether LLM output should be transformed to correct common model pitfalls." + }, "template": { "type": "string", "description": "An optional template string to be used for autocomplete. It will be rendered with the Mustache templating language, and is passed the 'prefix' and 'suffix' variables." diff --git a/extensions/vscode/config_schema.json b/extensions/vscode/config_schema.json index ce4e8e1a4..077bdcf93 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.json @@ -1162,11 +1162,7 @@ "model": { "anyOf": [ { - "enum": [ - "codestral", - "mistral-nemo", - "mistral-large" - ] + "enum": ["codestral", "mistral-nemo", "mistral-large"] }, { "type": "string" } ] @@ -2712,6 +2708,10 @@ "type": "number", "description": "The percentage of the input that should be dedicated to the prefix." }, + "transform": { + "type": "boolean", + "description": "Whether LLM output should be transformed to correct common model pitfalls." + }, "template": { "type": "string", "description": "An optional template string to be used for autocomplete. It will be rendered with the Mustache templating language, and is passed the 'prefix' and 'suffix' variables." diff --git a/extensions/vscode/continue_rc_schema.json b/extensions/vscode/continue_rc_schema.json index b2b7dbb78..4ee643f5c 100644 --- a/extensions/vscode/continue_rc_schema.json +++ b/extensions/vscode/continue_rc_schema.json @@ -3057,6 +3057,10 @@ "type": "number", "description": "The percentage of the input that should be dedicated to the prefix." }, + "transform": { + "type": "boolean", + "description": "Whether LLM output should be transformed to correct common model pitfalls." + }, "template": { "type": "string", "description": "An optional template string to be used for autocomplete. It will be rendered with the Mustache templating language, and is passed the 'prefix' and 'suffix' variables." diff --git a/packages/config-types/src/index.ts b/packages/config-types/src/index.ts index 26130c4ba..ee5699996 100644 --- a/packages/config-types/src/index.ts +++ b/packages/config-types/src/index.ts @@ -133,6 +133,7 @@ export const tabAutocompleteOptionsSchema = z.object({ debounceDelay: z.number(), maxSuffixPercentage: z.number(), prefixPercentage: z.number(), + transform: z.boolean().optional(), template: z.string().optional(), multilineCompletions: z.enum(["always", "never", "auto"]), slidingWindowPrefixPercentage: z.number(),