From 12a37e2330b11a72814634340229dc532fe73638 Mon Sep 17 00:00:00 2001 From: Zhiming Ma Date: Sun, 10 Sep 2023 10:25:23 +0800 Subject: [PATCH] refactor(agent): Move data path from ~/.tabby/agent to ~/.tabby-client/agent. Add config.toml template. (#420) --- clients/tabby-agent/src/Agent.ts | 2 +- clients/tabby-agent/src/AgentConfig.ts | 59 +++++++++++++++++++++++++- clients/tabby-agent/src/TabbyAgent.ts | 2 +- clients/tabby-agent/src/dataStore.ts | 2 +- clients/tabby-agent/src/logger.ts | 4 +- clients/vscode/src/commands.ts | 2 +- 6 files changed, 63 insertions(+), 8 deletions(-) diff --git a/clients/tabby-agent/src/Agent.ts b/clients/tabby-agent/src/Agent.ts index 417ac6b67e21..ad6310f80274 100644 --- a/clients/tabby-agent/src/Agent.ts +++ b/clients/tabby-agent/src/Agent.ts @@ -62,7 +62,7 @@ export interface AgentFunction { /** * The agent configuration has the following levels, will be deep merged in the order: * 1. Default config - * 2. User config file `~/.tabby/agent/config.toml` (not available in browser) + * 2. User config file `~/.tabby-client/agent/config.toml` (not available in browser) * 3. Agent `initialize` and `updateConfig` methods * * This method will update the 3rd level config. diff --git a/clients/tabby-agent/src/AgentConfig.ts b/clients/tabby-agent/src/AgentConfig.ts index 63eeb4047f39..0e95d5a16518 100644 --- a/clients/tabby-agent/src/AgentConfig.ts +++ b/clients/tabby-agent/src/AgentConfig.ts @@ -66,6 +66,49 @@ export const defaultAgentConfig: AgentConfig = { }, }; +const configTomlTemplate = `## Tabby agent configuration file + +## You can uncomment any block to enable settings. +## Configurations in this file has lower priority than in IDE settings. + +## Server +## You can set the server endpoint and request timeout here. +# [server] +# endpoint = "http://localhost:8080" # http or https URL +# requestTimeout = 30000 # ms + +## You can add custom request headers, e.g. for authentication. +# [server.requestHeaders] +# Authorization = "Bearer eyJhbGciOiJ..........." + +## Completion +## You can set the prompt context to send to the server for completion. +# [completion.prompt] +# maxPrefixLines = 20 +# maxSuffixLines = 20 + +## You can set the debounce mode for auto completion requests when typing. +# [completion.debounce] +# mode = "adaptive" # or "fixed" +# interval = 250 # ms, only used when mode is "fixed" + +## You can set the timeout for completion requests. +# [completion.timeout] +# auto = 5000 # ms, for auto completion when typing +# manually = 30000 # ms, for manually triggered completion + +## Logs +## You can set the log level here. The log file is located at ~/.tabby-client/agent/logs/. +# [logs] +# level = "silent" # or "error" or "debug" + +## Anonymous usage tracking +## You can disable anonymous usage tracking here. +# [anonymousUsageTracking] +# disable = false # set to true to disable + +`; + export const userAgentConfig = isBrowser ? null : (() => { @@ -95,7 +138,19 @@ export const userAgentConfig = isBrowser this.data = toml.parse(fileContent); super.emit("updated", this.data); } catch (error) { - this.logger.error({ error }, "Failed to load config file"); + if (error.code === "ENOENT") { + await this.createTemplate(); + } else { + this.logger.error({ error }, "Failed to load config file"); + } + } + } + + async createTemplate() { + try { + await fs.outputFile(this.filepath, configTomlTemplate); + } catch (error) { + this.logger.error({ error }, "Failed to create config template file"); } } @@ -108,6 +163,6 @@ export const userAgentConfig = isBrowser } } - const configFile = require("path").join(require("os").homedir(), ".tabby", "agent", "config.toml"); + const configFile = require("path").join(require("os").homedir(), ".tabby-client", "agent", "config.toml"); return new ConfigFile(configFile); })(); diff --git a/clients/tabby-agent/src/TabbyAgent.ts b/clients/tabby-agent/src/TabbyAgent.ts index 7d11335dfc70..41c55eee5a62 100644 --- a/clients/tabby-agent/src/TabbyAgent.ts +++ b/clients/tabby-agent/src/TabbyAgent.ts @@ -38,7 +38,7 @@ export class TabbyAgent extends EventEmitter implements Agent { private readonly logger = rootLogger.child({ component: "TabbyAgent" }); private anonymousUsageLogger: AnonymousUsageLogger; private config: AgentConfig = defaultAgentConfig; - private userConfig: PartialAgentConfig = {}; // config from `~/.tabby/agent/config.toml` + private userConfig: PartialAgentConfig = {}; // config from `~/.tabby-client/agent/config.toml` private clientConfig: PartialAgentConfig = {}; // config from `initialize` and `updateConfig` method private status: AgentStatus = "notInitialized"; private issues: AgentIssue["name"][] = []; diff --git a/clients/tabby-agent/src/dataStore.ts b/clients/tabby-agent/src/dataStore.ts index 77143baff6af..edb4be2cbc9f 100644 --- a/clients/tabby-agent/src/dataStore.ts +++ b/clients/tabby-agent/src/dataStore.ts @@ -14,7 +14,7 @@ export interface DataStore { export const dataStore: DataStore = isBrowser ? null : (() => { - const dataFile = require("path").join(require("os").homedir(), ".tabby", "agent", "data.json"); + const dataFile = require("path").join(require("os").homedir(), ".tabby-client", "agent", "data.json"); const fs = require("fs-extra"); return { data: {}, diff --git a/clients/tabby-agent/src/logger.ts b/clients/tabby-agent/src/logger.ts index 29785d42c043..e873bdcb9d25 100644 --- a/clients/tabby-agent/src/logger.ts +++ b/clients/tabby-agent/src/logger.ts @@ -8,10 +8,10 @@ const stream = isBrowser || isTest ? null : /** - * Default rotating file locate at `~/.tabby/agent/logs/`. + * Default rotating file locate at `~/.tabby-client/agent/logs/`. */ require("rotating-file-stream").createStream("tabby-agent.log", { - path: require("path").join(require("os").homedir(), ".tabby", "agent", "logs"), + path: require("path").join(require("os").homedir(), ".tabby-client", "agent", "logs"), size: "10M", interval: "1d", }); diff --git a/clients/vscode/src/commands.ts b/clients/vscode/src/commands.ts index c8f25602cacc..17c97c3a45a3 100644 --- a/clients/vscode/src/commands.ts +++ b/clients/vscode/src/commands.ts @@ -75,7 +75,7 @@ const openTabbyAgentSettings: Command = { 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"); + const agentUserConfig = Uri.joinPath(Uri.file(require("os").homedir()), ".tabby-client", "agent", "config.toml"); workspace.fs.stat(agentUserConfig).then( () => { workspace.openTextDocument(agentUserConfig).then((document) => {