Skip to content

Commit

Permalink
feat(agent): add experimental postprocess limit scope by syntax. (#758)
Browse files Browse the repository at this point in the history
* feat(agent): add tree-sitter parser.

* feat(agent): make parser updating tree cache optional.

* feat(agent): add experimental limit scopy by syntax.

* test(agent): update unit test for limitScopeBySyntax.
  • Loading branch information
icycodes authored Nov 12, 2023
1 parent d7180ec commit 568b7b4
Show file tree
Hide file tree
Showing 26 changed files with 822 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/intellij-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
with:
lfs: true
- name: Setup JDK
uses: actions/setup-java@v3
with:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/tabby-agent-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
with:
lfs: true
- name: Setup Node.js
uses: actions/setup-node@v3
with:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/vscode-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
with:
lfs: true
- name: Setup Node.js
uses: actions/setup-node@v3
with:
Expand Down
1 change: 1 addition & 0 deletions clients/tabby-agent/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.wasm filter=lfs diff=lfs merge=lfs -text
5 changes: 3 additions & 2 deletions clients/tabby-agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
"@types/node": "^18.12.0",
"chai": "^4.3.7",
"dedent": "^0.7.0",
"esbuild-plugin-copy": "^2.1.1",
"esbuild-plugin-polyfill-node": "^0.3.0",
"mocha": "^10.2.0",
"openapi-typescript": "^6.6.1",
"prettier": "^3.0.0",
"rimraf": "^5.0.1",
"ts-node": "^10.9.1",
"tsup": "^7.1.0",
"typescript": "^5.0.3"
Expand All @@ -47,6 +47,7 @@
"rotating-file-stream": "^3.1.0",
"stats-logscale": "^1.0.7",
"toml": "^3.0.0",
"uuid": "^9.0.0"
"uuid": "^9.0.0",
"web-tree-sitter": "^0.20.8"
}
}
21 changes: 14 additions & 7 deletions clients/tabby-agent/src/AgentConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ export type AgentConfig = {
timeout: number;
};
postprocess: {
limitScopeByIndentation: {
// When completion is continuing the current line, limit the scope to:
// false(default): the line scope, meaning use the next indent level as the limit.
// true: the block scope, meaning use the current indent level as the limit.
experimentalKeepBlockScopeWhenCompletingLine: boolean;
limitScope: {
// Prefer to use syntax parser than indentation
experimentalSyntax: boolean;
indentation: {
// When completion is continuing the current line, limit the scope to:
// false(default): the line scope, meaning use the next indent level as the limit.
// true: the block scope, meaning use the current indent level as the limit.
experimentalKeepBlockScopeWhenCompletingLine: boolean;
};
};
};
logs: {
Expand Down Expand Up @@ -66,8 +70,11 @@ export const defaultAgentConfig: AgentConfig = {
timeout: 4000, // ms
},
postprocess: {
limitScopeByIndentation: {
experimentalKeepBlockScopeWhenCompletingLine: false,
limitScope: {
experimentalSyntax: false,
indentation: {
experimentalKeepBlockScopeWhenCompletingLine: false,
},
},
},
logs: {
Expand Down
2 changes: 1 addition & 1 deletion clients/tabby-agent/src/TabbyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ export class TabbyAgent extends EventEmitter implements Agent {
throw options.signal.reason;
}
// Build cache
this.completionCache.buildCache(context, completionResponse);
this.completionCache.buildCache(context, JSON.parse(JSON.stringify(completionResponse)));
}
}
// Postprocess (post-cache)
Expand Down
4 changes: 2 additions & 2 deletions clients/tabby-agent/src/postprocess/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { applyFilter } from "./base";
import { removeRepetitiveBlocks } from "./removeRepetitiveBlocks";
import { removeRepetitiveLines } from "./removeRepetitiveLines";
import { removeLineEndsWithRepetition } from "./removeLineEndsWithRepetition";
import { limitScopeByIndentation } from "./limitScopeByIndentation";
import { limitScope } from "./limitScope";
import { trimSpace } from "./trimSpace";
import { dropDuplicated } from "./dropDuplicated";
import { dropBlank } from "./dropBlank";
Expand All @@ -30,7 +30,7 @@ export async function postCacheProcess(
return Promise.resolve(response)
.then(applyFilter(removeRepetitiveBlocks(context), context))
.then(applyFilter(removeRepetitiveLines(context), context))
.then(applyFilter(limitScopeByIndentation(context, config["limitScopeByIndentation"]), context))
.then(applyFilter(limitScope(context, config["limitScope"]), context))
.then(applyFilter(dropDuplicated(context), context))
.then(applyFilter(trimSpace(context), context))
.then(applyFilter(dropBlank(), context));
Expand Down
24 changes: 24 additions & 0 deletions clients/tabby-agent/src/postprocess/limitScope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { CompletionContext } from "../Agent";
import { AgentConfig } from "../AgentConfig";
import { isBrowser } from "../env";
import { PostprocessFilter } from "./base";
import { limitScopeByIndentation } from "./limitScopeByIndentation";
import { limitScopeBySyntax, supportedLanguages } from "./limitScopeBySyntax";

export function limitScope(
context: CompletionContext,
config: AgentConfig["postprocess"]["limitScope"],
): PostprocessFilter {
return isBrowser
? (input) => {
// syntax parser is not supported in browser yet
return limitScopeByIndentation(context, config["indentation"])(input);
}
: (input) => {
if (config.experimentalSyntax && supportedLanguages.indexOf(context.language) >= 0) {
return limitScopeBySyntax(context)(input);
} else {
return limitScopeByIndentation(context, config["indentation"])(input);
}
};
}
13 changes: 8 additions & 5 deletions clients/tabby-agent/src/postprocess/limitScopeByIndentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function processContext(
lines: string[],
prefixLines: string[],
suffixLines: string[],
config: AgentConfig["postprocess"]["limitScopeByIndentation"],
config: AgentConfig["postprocess"]["limitScope"]["indentation"],
): { indentLevelLimit: number; allowClosingLine: (closingLine: string) => boolean } {
let allowClosingLine = false;
let result = { indentLevelLimit: 0, allowClosingLine: (closingLine: string) => allowClosingLine };
Expand Down Expand Up @@ -103,14 +103,14 @@ function processContext(

export function limitScopeByIndentation(
context: CompletionContext,
config: AgentConfig["postprocess"]["limitScopeByIndentation"],
config: AgentConfig["postprocess"]["limitScope"]["indentation"],
): PostprocessFilter {
return (input) => {
const { prefix, suffix, prefixLines, suffixLines } = context;
const { prefixLines, suffixLines } = context;
const inputLines = splitLines(input);
if (context.mode === "fill-in-line") {
if (inputLines.length > 1) {
logger.debug({ input, prefix, suffix }, "Drop content with multiple lines");
logger.debug({ inputLines, prefixLines, suffixLines }, "Drop content with multiple lines");
return null;
}
}
Expand Down Expand Up @@ -139,7 +139,10 @@ export function limitScopeByIndentation(
}
}
if (index < inputLines.length) {
logger.debug({ input, prefix, suffix, scopeEndAt: index }, "Remove content out of scope");
logger.debug(
{ inputLines, prefixLines, suffixLines, scopeLineCount: index },
"Remove content out of indent scope",
);
return inputLines.slice(0, index).join("").trimEnd();
}
return input;
Expand Down
Loading

0 comments on commit 568b7b4

Please sign in to comment.