Skip to content

Commit

Permalink
refactor: move vscode dev options to agent config. (TabbyML#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
icycodes authored Jul 6, 2023
1 parent 920ff9d commit 62e0ea9
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 144 deletions.
2 changes: 1 addition & 1 deletion clients/tabby-agent/dist/cli.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion clients/tabby-agent/dist/cli.js.map

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions clients/tabby-agent/dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ type AgentConfig = {
server: {
endpoint: string;
};
completion: {
maxPrefixLines: number;
maxSuffixLines: number;
};
logs: {
level: "debug" | "error" | "silent";
};
Expand All @@ -57,8 +61,8 @@ type CompletionRequest = {
language: string;
text: string;
position: number;
maxPrefixLines: number;
maxSuffixLines: number;
maxPrefixLines?: number;
maxSuffixLines?: number;
};
type CompletionResponse = CompletionResponse$1;
type LogEventRequest = LogEventRequest$1;
Expand Down
2 changes: 1 addition & 1 deletion clients/tabby-agent/dist/index.global.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion clients/tabby-agent/dist/index.global.js.map

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions clients/tabby-agent/dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion clients/tabby-agent/dist/index.js.map

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions clients/tabby-agent/dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43811,6 +43811,10 @@ var defaultAgentConfig = {
server: {
endpoint: "http://localhost:8080"
},
completion: {
maxPrefixLines: 20,
maxSuffixLines: 20
},
logs: {
level: "silent"
},
Expand Down Expand Up @@ -45566,8 +45570,8 @@ var _TabbyAgent = class extends EventEmitter {
});
}
createSegments(request2) {
const maxPrefixLines = request2.maxPrefixLines;
const maxSuffixLines = request2.maxSuffixLines;
const maxPrefixLines = request2.maxPrefixLines ?? this.config.completion.maxPrefixLines;
const maxSuffixLines = request2.maxSuffixLines ?? this.config.completion.maxSuffixLines;
const prefix = request2.text.slice(0, request2.position);
const prefixLines = splitLines(prefix);
const suffix = request2.text.slice(request2.position);
Expand Down
2 changes: 1 addition & 1 deletion clients/tabby-agent/dist/index.mjs.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions clients/tabby-agent/src/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export type CompletionRequest = {
language: string;
text: string;
position: number;
maxPrefixLines: number;
maxSuffixLines: number;
maxPrefixLines?: number;
maxSuffixLines?: number;
};

export type CompletionResponse = ApiCompletionResponse;
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 @@ -4,6 +4,10 @@ export type AgentConfig = {
server: {
endpoint: string;
};
completion: {
maxPrefixLines: number;
maxSuffixLines: number;
};
logs: {
level: "debug" | "error" | "silent";
};
Expand All @@ -16,6 +20,10 @@ export const defaultAgentConfig: AgentConfig = {
server: {
endpoint: "http://localhost:8080",
},
completion: {
maxPrefixLines: 20,
maxSuffixLines: 20,
},
logs: {
level: "silent",
},
Expand Down
4 changes: 2 additions & 2 deletions clients/tabby-agent/src/TabbyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ export class TabbyAgent extends EventEmitter implements Agent {

private createSegments(request: CompletionRequest): { prefix: string; suffix: string } {
// max lines in prefix and suffix configurable
const maxPrefixLines = request.maxPrefixLines;
const maxSuffixLines = request.maxSuffixLines;
const maxPrefixLines = request.maxPrefixLines ?? this.config.completion.maxPrefixLines;
const maxSuffixLines = request.maxSuffixLines ?? this.config.completion.maxSuffixLines;
const prefix = request.text.slice(0, request.position);
const prefixLines = splitLines(prefix);
const suffix = request.text.slice(request.position);
Expand Down
59 changes: 16 additions & 43 deletions clients/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,23 @@
"command": "tabby.openSettings",
"title": "Tabby: Open Settings"
},
{
"command": "tabby.openTabbyAgentSettings",
"title": "Tabby: Open Tabby Agent Settings"
},
{
"command": "tabby.gettingStarted",
"title": "Tabby: Getting Started"
}
],
"menus": {
"commandPalette": [
{
"command": "tabby.openTabbyAgentSettings",
"when": "!isWeb"
}
]
},
"walkthroughs": [
{
"id": "gettingStarted",
Expand Down Expand Up @@ -83,7 +95,7 @@
{
"id": "commands",
"title": "Commands",
"description": "Type `>Tabby:` in quick open prompt to list all Tabby commands. \n[Tabby commands](command:workbench.action.quickOpen?%5B%22%3ETabby%3A%22%5D)",
"description": "Type `>Tabby:` in command palette to list all Tabby commands. \n[Tabby commands](command:workbench.action.quickOpen?%5B%22%3ETabby%3A%22%5D)",
"media": {
"image": "assets/walkthroughs/commands.png",
"altText": "Tabby Commands"
Expand All @@ -97,54 +109,16 @@
"properties": {
"tabby.api.endpoint": {
"type": "string",
"default": "http://localhost:8080",
"format": "uri",
"pattern": "^https?:\\/\\/[^\\s]+$",
"default": "",
"pattern": "(^$)|(^https?:\\/\\/\\S+$)",
"patternErrorMessage": "Please enter a validate http or https URL.",
"description": "Specify API Endpoint of Tabby."
"markdownDescription": "Specify API Endpoint of Tabby. \nIf leave empty, server endpoint in [Tabby Agent Settings](command:tabby.openTabbyAgentSettings) will be used."
},
"tabby.codeCompletion": {
"type": "boolean",
"default": true,
"description": "Enable Tabby code completion or not."
},
"tabby.developerOptions": {
"type": "object",
"description": "Developer options for Tabby.",
"properties": {
"maxPrefixLines": {
"type": "number",
"default": 20,
"description": "Number of lines to include in the Prefix for completion requests"
},
"maxSuffixLines": {
"type": "number",
"default": 20,
"description": "Number of lines to include in the Suffix for completion requests"
},
"suggestionDelay": {
"type": "number",
"default": 150,
"minimum": 0,
"description": "Specifies the delay in milliseconds after which the request is sent to the tabby."
},
"agent": {
"type": "object",
"properties": {
"logs": {
"type": "string",
"enum": [
"debug",
"error",
"silent"
],
"default": "error",
"markdownDescription": "Specifies the log level for tabby-agent."
}
}
}
}
},
"tabby.usage.anonymousUsageTracking": {
"type": "boolean",
"default": false,
Expand Down Expand Up @@ -190,7 +164,6 @@
"webpack-cli": "^4.10.0"
},
"dependencies": {
"@sapphire/duration": "^1.1.0",
"@xstate/fsm": "^2.0.1",
"tabby-agent": "file:../tabby-agent"
}
Expand Down
5 changes: 2 additions & 3 deletions clients/vscode/src/TabbyCompletionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export class TabbyCompletionProvider implements InlineCompletionItemProvider {

// User Settings
private enabled: boolean = true;

// These settings will be move to tabby-agent
private suggestionDelay: number = 150;
private maxPrefixLines: number = 20;
private maxSuffixLines: number = 20;
Expand Down Expand Up @@ -79,9 +81,6 @@ export class TabbyCompletionProvider implements InlineCompletionItemProvider {
private updateConfiguration() {
const configuration = workspace.getConfiguration("tabby");
this.enabled = configuration.get("codeCompletion", true);
this.suggestionDelay = configuration.get("developerOptions.suggestionDelay", 150);
this.maxPrefixLines = configuration.get("developerOptions.maxPrefixLines", 20);
this.maxSuffixLines = configuration.get("developerOptions.maxSuffixLines", 20);
}

private toInlineCompletions(tabbyCompletion: CompletionResponse | null, range: Range): InlineCompletionItem[] {
Expand Down
8 changes: 1 addition & 7 deletions clients/vscode/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@ function getWorkspaceConfiguration(): Partial<AgentConfig> {
const configuration = workspace.getConfiguration("tabby");
const config: Partial<AgentConfig> = {};
const endpoint = configuration.get<string>("api.endpoint");
if (endpoint) {
if (endpoint && endpoint.trim().length > 0) {
config.server = {
endpoint,
};
}
const agentLogs = configuration.get<"debug" | "error" | "silent">("developerOptions.agent.logs");
if (agentLogs) {
config.logs = {
level: agentLogs,
};
}
const anonymousUsageTrackingDisabled = configuration.get<boolean>("usage.anonymousUsageTracking", false);
config.anonymousUsageTracking = {
disable: anonymousUsageTrackingDisabled,
Expand Down
87 changes: 22 additions & 65 deletions clients/vscode/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import {
ConfigurationTarget,
InputBoxValidationSeverity,
ProgressLocation,
QuickPickItem,
QuickPickItemKind,
Uri,
workspace,
window,
env,
commands,
} from "vscode";
import { strict as assert } from "assert";
import { Duration } from "@sapphire/duration";
import { CancelablePromise } from "tabby-agent";
import { agent } from "./agent";
import { notifications } from "./notifications";
Expand All @@ -34,67 +31,6 @@ const toggleEnabled: Command = {
},
};

const setSuggestionDelay: Command = {
command: "tabby.setSuggestionDelay",
callback: () => {
const configuration = workspace.getConfiguration("tabby");
const current = configuration.get("developerOptions.suggestionDelay", 150);
const items = {
Immediately: 0, // ms
Default: 150,
Slowly: 1000,
};
const createQuickPickItem = (value: number): QuickPickItem => {
const tags: string[] = [];
if (value == current) {
tags.push("Current");
}
Object.entries(items).forEach(([k, v]) => {
if (v == value) {
tags.push(k);
}
});
return {
label: value % 1000 == 0 ? `${value / 1000}s` : `${value}ms`,
description: tags.join(" "),
alwaysShow: true,
};
};
const buildQuickPickList = (input: string = "") => {
const list: QuickPickItem[] = [];
const customized = new Duration(input).offset || Number.parseInt(input);
if (customized >= 0) {
list.push(createQuickPickItem(customized));
}
if (current != customized) {
list.push(createQuickPickItem(current));
}
list.push({
label: "",
kind: QuickPickItemKind.Separator,
});
Object.values(items)
.filter((item) => item != current && item != customized)
.forEach((item) => list.push(createQuickPickItem(item)));
return list;
};
const quickPick = window.createQuickPick();
quickPick.placeholder = "Enter the delay after which the completion request is sent";
quickPick.matchOnDescription = true;
quickPick.items = buildQuickPickList();
quickPick.onDidChangeValue((input: string) => {
quickPick.items = buildQuickPickList(input);
});
quickPick.onDidAccept(() => {
quickPick.hide();
const delay = new Duration(quickPick.selectedItems[0].label).offset;
console.debug("Set suggestion delay: ", delay);
configuration.update("developerOptions.suggestionDelay", delay, configTarget, false);
});
quickPick.show();
},
};

const setApiEndpoint: Command = {
command: "tabby.setApiEndpoint",
callback: () => {
Expand Down Expand Up @@ -132,6 +68,27 @@ const openSettings: Command = {
},
};

const openTabbyAgentSettings: Command = {
command: "tabby.openTabbyAgentSettings",
callback: () => {
if (env.appHost !== "desktop") {
window.showWarningMessage("Tabby Agent config file is not supported on web.", { modal: true });
return;
}
const agentUserConfig = Uri.joinPath(Uri.file(require("os").homedir()), ".tabby", "agent", "config.toml");
workspace.fs.stat(agentUserConfig).then(
() => {
workspace.openTextDocument(agentUserConfig).then((document) => {
window.showTextDocument(document);
});
},
() => {
window.showWarningMessage("Tabby Agent config file not found.", { modal: true });
}
);
},
};

const gettingStarted: Command = {
command: "tabby.gettingStarted",
callback: () => {
Expand Down Expand Up @@ -219,8 +176,8 @@ export const tabbyCommands = () =>
[
toggleEnabled,
setApiEndpoint,
setSuggestionDelay,
openSettings,
openTabbyAgentSettings,
gettingStarted,
emitEvent,
openAuthPage,
Expand Down
4 changes: 1 addition & 3 deletions clients/vscode/src/notifications.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { commands, window, workspace, ConfigurationTarget } from "vscode";

const configTarget = ConfigurationTarget.Global;
import { commands, window } from "vscode";

function showInformationWhenLoading() {
window.showInformationMessage("Tabby is initializing.", "Settings").then((selection) => {
Expand Down
Loading

0 comments on commit 62e0ea9

Please sign in to comment.