mirror of
https://github.com/TabbyML/tabby
synced 2024-11-22 00:08:06 +00:00
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:
parent
d5bde52de4
commit
7227d0b171
2
clients/tabby-agent/src/AgentConfig.ts
vendored
2
clients/tabby-agent/src/AgentConfig.ts
vendored
@ -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: {
|
||||
|
5
clients/tabby-agent/src/postprocess/base.ts
vendored
5
clients/tabby-agent/src/postprocess/base.ts
vendored
@ -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");
|
||||
|
12
clients/tabby-agent/src/postprocess/dropBlank.ts
vendored
12
clients/tabby-agent/src/postprocess/dropBlank.ts
vendored
@ -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;
|
||||
};
|
||||
}
|
@ -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);
|
||||
});
|
15
clients/tabby-agent/src/postprocess/dropMinimum.ts
vendored
Normal file
15
clients/tabby-agent/src/postprocess/dropMinimum.ts
vendored
Normal 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;
|
||||
};
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
description = 'Limit scope: rust 02'
|
||||
|
||||
[config]
|
||||
# use default config
|
||||
minCompletionChars = 1
|
||||
|
||||
[context]
|
||||
filepath = 'stop.rs'
|
||||
|
@ -1,7 +1,7 @@
|
||||
description = 'Limit scope: limit to block scope: case 03'
|
||||
|
||||
[config]
|
||||
# use default config
|
||||
minCompletionChars = 1
|
||||
|
||||
[context]
|
||||
filepath = 'checks.js'
|
||||
|
16
clients/tabby-agent/src/postprocess/index.ts
vendored
16
clients/tabby-agent/src/postprocess/index.ts
vendored
@ -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));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user