chore(agent): require min length for code shown to complete (#3073)

* Require min length for code shown to complete

* [autofix.ci] apply automated fixes

* more review fixes

* Remove dropBlank module + more fixes

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Shankar 2024-09-06 14:26:39 -07:00 committed by GitHub
parent d5bde52de4
commit 7227d0b171
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 38 additions and 29 deletions

View File

@ -71,6 +71,7 @@ export type AgentConfig = {
postprocess: {
limitScope: any;
calculateReplaceRange: any;
minCompletionChars: number;
};
chat: {
edit: {
@ -172,6 +173,7 @@ export const defaultAgentConfig: AgentConfig = {
postprocess: {
limitScope: {},
calculateReplaceRange: {},
minCompletionChars: 4,
},
chat: {
edit: {

View File

@ -1,6 +1,9 @@
import { CompletionItem } from "../CompletionSolution";
import { getLogger } from "../logger";
import { AgentConfig } from "../AgentConfig";
export type PostprocessFilterFactory = (config: unknown) => PostprocessFilter;
export type PostprocessFilterFactory =
| (() => PostprocessFilter)
| ((config: AgentConfig["postprocess"]) => PostprocessFilter);
export type PostprocessFilter = (item: CompletionItem) => CompletionItem | Promise<CompletionItem>;
export const logger = getLogger("Postprocess");

View File

@ -1,12 +0,0 @@
import { PostprocessFilter } from "./base";
import { CompletionItem } from "../CompletionSolution";
import { isBlank } from "../utils";
export function dropBlank(): PostprocessFilter {
return (item: CompletionItem): CompletionItem => {
if (isBlank(item.fullText) || isBlank(item.text)) {
return CompletionItem.createBlankItem(item.context);
}
return item;
};
}

View File

@ -1,22 +1,23 @@
import { dropBlank } from "./dropBlank";
import { dropMinimum } from "./dropMinimum";
import { documentContext, assertFilterResult } from "./testUtils";
import { CompletionItem } from "../CompletionSolution";
describe("postprocess", () => {
describe("dropBlank", () => {
const filter = dropBlank();
describe("dropMinimum", () => {
const filter = dropMinimum({ limitScope: null, minCompletionChars: 4, calculateReplaceRange: null });
const context = documentContext`
dummy
`;
context.language = "plaintext";
it("should return null if input is blank", async () => {
it("should return null if input is < 4 non-whitespace characters", async () => {
const expected = CompletionItem.createBlankItem(context);
await assertFilterResult(filter, context, "\n", expected);
await assertFilterResult(filter, context, "\t\n", expected);
await assertFilterResult(filter, context, "ab\t\n", expected);
});
it("should keep unchanged if input is not blank", async () => {
const completion = "Not blank";
it("should keep unchanged if input is >= 4 non-whitespace characters", async () => {
const completion = "Greater than 4";
const expected = completion;
await assertFilterResult(filter, context, completion, expected);
});

View File

@ -0,0 +1,15 @@
import { PostprocessFilter } from "./base";
import { CompletionItem } from "../CompletionSolution";
import { AgentConfig } from "../AgentConfig";
export function dropMinimum(config: AgentConfig["postprocess"]): PostprocessFilter {
return (item: CompletionItem): CompletionItem => {
if (
item.fullText.trim().length < config.minCompletionChars ||
item.text.trim().length < config.minCompletionChars
) {
return CompletionItem.createBlankItem(item.context);
}
return item;
};
}

View File

@ -1,7 +1,7 @@
description = 'Limit scope: rust 02'
[config]
# use default config
minCompletionChars = 1
[context]
filepath = 'stop.rs'

View File

@ -1,7 +1,7 @@
description = 'Limit scope: limit to block scope: case 03'
[config]
# use default config
minCompletionChars = 1
[context]
filepath = 'checks.js'

View File

@ -11,12 +11,12 @@ import { formatIndentation } from "./formatIndentation";
import { trimSpace } from "./trimSpace";
import { trimMultiLineInSingleLineMode } from "./trimMultiLineInSingleLineMode";
import { dropDuplicated } from "./dropDuplicated";
import { dropBlank } from "./dropBlank";
import { dropMinimum } from "./dropMinimum";
import { calculateReplaceRange } from "./calculateReplaceRange";
type ItemListFilter = (items: CompletionItem[]) => Promise<CompletionItem[]>;
function createListFilter(filterFactory: PostprocessFilterFactory, config: unknown): ItemListFilter {
function createListFilter(filterFactory: PostprocessFilterFactory, config: AgentConfig["postprocess"]): ItemListFilter {
const filter: PostprocessFilter = filterFactory(config);
return async (items: CompletionItem[]): Promise<CompletionItem[]> => {
return await items.mapAsync(filter);
@ -25,25 +25,25 @@ function createListFilter(filterFactory: PostprocessFilterFactory, config: unkno
export async function preCacheProcess(
items: CompletionItem[],
_config: AgentConfig["postprocess"],
config: AgentConfig["postprocess"],
): Promise<CompletionItem[]> {
const applyFilter = (filterFactory: PostprocessFilterFactory): ItemListFilter => {
return createListFilter(filterFactory, _config);
return createListFilter(filterFactory, config);
};
return Promise.resolve(items)
.then(applyFilter(trimMultiLineInSingleLineMode))
.then(applyFilter(removeLineEndsWithRepetition))
.then(applyFilter(dropDuplicated))
.then(applyFilter(trimSpace))
.then(applyFilter(dropBlank));
.then(applyFilter(dropMinimum));
}
export async function postCacheProcess(
items: CompletionItem[],
_config: AgentConfig["postprocess"],
config: AgentConfig["postprocess"],
): Promise<CompletionItem[]> {
const applyFilter = (filterFactory: PostprocessFilterFactory): ItemListFilter => {
return createListFilter(filterFactory, _config);
return createListFilter(filterFactory, config);
};
return Promise.resolve(items)
.then(applyFilter(removeRepetitiveBlocks))
@ -53,6 +53,6 @@ export async function postCacheProcess(
.then(applyFilter(formatIndentation))
.then(applyFilter(dropDuplicated))
.then(applyFilter(trimSpace))
.then(applyFilter(dropBlank))
.then(applyFilter(dropMinimum))
.then(applyFilter(calculateReplaceRange));
}