improv: fix failing tests, add 'transform' autocomplete option

This commit is contained in:
tomasz-io 2024-10-25 10:57:55 +07:00
parent 4cb84c11b0
commit d12395838b
12 changed files with 138 additions and 96 deletions

View file

@ -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", "<file_sep>"];
@ -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;

View file

@ -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", () => {});
});
});

View file

@ -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: `
<script>
let count = 0;
@ -1972,14 +1972,14 @@ export default {
Clicked {count} times
</button>
`,
llmOutput: `doubledCount = count * 2`,
expectedCompletion: `doubledCount = count * 2`,
},
llmOutput: `doubledCount = count * 2`,
expectedCompletion: `doubledCount = count * 2`,
},
{
description: "Should autocomplete Svelte component inside HTML",
filename: "NestedComponent.svelte",
input: `
{
description: "Should autocomplete Svelte component inside HTML",
filename: "NestedComponent.svelte",
input: `
<script>
import ChildComponent from './ChildComponent.svelte';
</script>
@ -1989,14 +1989,14 @@ export default {
<ChildComponent <|fim|> />
</main>
`,
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: `
<script>
let items = ["Apple", "Banana", "Cherry"];
</script>
@ -2007,8 +2007,7 @@ export default {
{/each<|fim|>
</ul>
`,
llmOutput: `}`,
expectedCompletion: `}`,
},
llmOutput: `}`,
expectedCompletion: `}`,
},
];

View file

@ -17,6 +17,9 @@ export interface AutocompleteFileringTestInput {
input: string;
llmOutput: string;
expectedCompletion: string | null;
options?: {
only?: boolean;
};
}
export async function testAutocompleteFiltering(

View file

@ -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;

2
core/index.d.ts vendored
View file

@ -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;

View file

@ -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;

View file

@ -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."

View file

@ -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."

View file

@ -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."

View file

@ -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."

View file

@ -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(),