Skip to content

Commit

Permalink
feat(agent): support read clipboard as prompt (#885)
Browse files Browse the repository at this point in the history
* feat(agent): add support for reading clipboard text as prompt.

* fix: add clipboard in context hash calculation.

* fix: set default allowed clipboard chars to 2000.
  • Loading branch information
icycodes authored Nov 24, 2023
1 parent 0857a10 commit 52cf5fb
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion clients/intellij/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"devDependencies": {
"cpy-cli": "^4.2.0",
"rimraf": "^5.0.1",
"tabby-agent": "1.1.1"
"tabby-agent": "1.2.0-dev"
}
}
2 changes: 1 addition & 1 deletion clients/tabby-agent/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tabby-agent",
"version": "1.1.1",
"version": "1.2.0-dev",
"description": "Generic client agent for Tabby AI coding assistant IDE extensions.",
"repository": "https://github.com/TabbyML/tabby",
"main": "./dist/index.js",
Expand Down
8 changes: 8 additions & 0 deletions clients/tabby-agent/src/AgentConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export type AgentConfig = {
experimentalStripAutoClosingCharacters: boolean;
maxPrefixLines: number;
maxSuffixLines: number;
clipboard: {
minChars: number;
maxChars: number;
};
};
debounce: {
mode: "adaptive" | "fixed";
Expand Down Expand Up @@ -66,6 +70,10 @@ export const defaultAgentConfig: AgentConfig = {
experimentalStripAutoClosingCharacters: false,
maxPrefixLines: 20,
maxSuffixLines: 20,
clipboard: {
minChars: 3,
maxChars: 2000,
},
},
debounce: {
mode: "adaptive",
Expand Down
16 changes: 11 additions & 5 deletions clients/tabby-agent/src/CompletionContext.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { splitLines, autoClosingPairOpenings, autoClosingPairClosings } from "./utils";
import { splitLines, autoClosingPairClosings } from "./utils";
import hashObject from "object-hash";

export type CompletionRequest = {
filepath: string;
language: string;
text: string;
position: number;
clipboard?: string;
manually?: boolean;
};

Expand Down Expand Up @@ -43,6 +44,8 @@ export class CompletionContext {
prefixLines: string[];
suffixLines: string[];

clipboard: string;

// "default": the cursor is at the end of the line
// "fill-in-line": the cursor is not at the end of the line, except auto closed characters
// In this case, we assume the completion should be a single line, so multiple lines completion will be dropped.
Expand All @@ -60,13 +63,16 @@ export class CompletionContext {
this.prefixLines = splitLines(this.prefix);
this.suffixLines = splitLines(this.suffix);

this.clipboard = request.clipboard?.trim() ?? "";

const lineEnd = isAtLineEndExcludingAutoClosedChar(this.suffixLines[0] ?? "");
this.mode = lineEnd ? "default" : "fill-in-line";
this.hash = hashObject({
filepath: request.filepath,
language: request.language,
text: request.text,
position: request.position,
filepath: this.filepath,
language: this.language,
text: this.text,
position: this.position,
clipboard: this.clipboard,
});
}
}
10 changes: 8 additions & 2 deletions clients/tabby-agent/src/TabbyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export class TabbyAgent extends EventEmitter implements Agent {
}
}

private createSegments(context: CompletionContext): { prefix: string; suffix: string } {
private createSegments(context: CompletionContext): { prefix: string; suffix: string; clipboard?: string } {
// max lines in prefix and suffix configurable
const maxPrefixLines = this.config.completion.prompt.maxPrefixLines;
const maxSuffixLines = this.config.completion.prompt.maxSuffixLines;
Expand All @@ -287,7 +287,13 @@ export class TabbyAgent extends EventEmitter implements Agent {
} else {
suffix = suffixLines.slice(0, maxSuffixLines).join("");
}
return { prefix, suffix };

let clipboard = undefined;
const clipboardConfig = this.config.completion.prompt.clipboard;
if (context.clipboard.length >= clipboardConfig.minChars && context.clipboard.length <= clipboardConfig.maxChars) {
clipboard = context.clipboard;
}
return { prefix, suffix, clipboard };
}

public async initialize(options: AgentInitOptions): Promise<boolean> {
Expand Down
2 changes: 1 addition & 1 deletion clients/vim/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"devDependencies": {
"cpy-cli": "^4.2.0",
"rimraf": "^5.0.1",
"tabby-agent": "1.1.1"
"tabby-agent": "1.2.0-dev"
}
}
4 changes: 2 additions & 2 deletions clients/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"repository": "https://github.com/TabbyML/tabby",
"bugs": "https://github.com/TabbyML/tabby/issues",
"license": "Apache-2.0",
"version": "1.1.3",
"version": "1.2.0-dev",
"keywords": [
"ai",
"autocomplete",
Expand Down Expand Up @@ -226,6 +226,6 @@
},
"dependencies": {
"@xstate/fsm": "^2.0.1",
"tabby-agent": "1.1.1"
"tabby-agent": "1.2.0-dev"
}
}
2 changes: 2 additions & 0 deletions clients/vscode/src/TabbyCompletionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Position,
Range,
TextDocument,
env,
workspace,
} from "vscode";
import { EventEmitter } from "events";
Expand Down Expand Up @@ -66,6 +67,7 @@ export class TabbyCompletionProvider extends EventEmitter implements InlineCompl
language: document.languageId, // https://code.visualstudio.com/docs/languages/identifiers
text: document.getText(),
position: document.offsetAt(position),
clipboard: await env.clipboard.readText(),
manually: context.triggerKind === InlineCompletionTriggerKind.Invoke,
};

Expand Down

0 comments on commit 52cf5fb

Please sign in to comment.