2023-06-15 15:53:21 +00:00
|
|
|
import { ExtensionContext, workspace, env, version } from "vscode";
|
2023-09-28 02:15:39 +00:00
|
|
|
import { TabbyAgent, AgentInitOptions, PartialAgentConfig, ClientProperties, DataStore } from "tabby-agent";
|
2023-06-15 15:53:21 +00:00
|
|
|
|
2023-09-28 02:15:39 +00:00
|
|
|
function buildInitOptions(context: ExtensionContext): AgentInitOptions {
|
2023-06-15 15:53:21 +00:00
|
|
|
const configuration = workspace.getConfiguration("tabby");
|
2023-08-11 11:39:17 +00:00
|
|
|
const config: PartialAgentConfig = {};
|
2023-06-16 19:54:30 +00:00
|
|
|
const endpoint = configuration.get<string>("api.endpoint");
|
2023-07-06 16:31:15 +00:00
|
|
|
if (endpoint && endpoint.trim().length > 0) {
|
2023-06-15 15:53:21 +00:00
|
|
|
config.server = {
|
2023-06-16 19:54:30 +00:00
|
|
|
endpoint,
|
2023-06-15 15:53:21 +00:00
|
|
|
};
|
|
|
|
|
}
|
2023-06-16 19:54:30 +00:00
|
|
|
const anonymousUsageTrackingDisabled = configuration.get<boolean>("usage.anonymousUsageTracking", false);
|
2023-06-16 08:58:50 +00:00
|
|
|
config.anonymousUsageTracking = {
|
2023-06-16 19:54:30 +00:00
|
|
|
disable: anonymousUsageTrackingDisabled,
|
2023-06-16 08:58:50 +00:00
|
|
|
};
|
2023-09-28 02:15:39 +00:00
|
|
|
const clientProperties: ClientProperties = {
|
|
|
|
|
user: {
|
|
|
|
|
vscode: {
|
|
|
|
|
triggerMode: configuration.get("inlineCompletion.triggerMode", "automatic"),
|
|
|
|
|
keybindings: configuration.get("keybindings", "vscode-style"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
session: {
|
|
|
|
|
client: `${env.appName} ${env.appHost} ${version}, ${context.extension.id} ${context.extension.packageJSON.version}`,
|
|
|
|
|
ide: {
|
|
|
|
|
name: `${env.appName} ${env.appHost}`,
|
|
|
|
|
version: version,
|
|
|
|
|
},
|
|
|
|
|
tabby_plugin: {
|
|
|
|
|
name: context.extension.id,
|
|
|
|
|
version: context.extension.packageJSON.version,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return { config, clientProperties };
|
2023-06-15 15:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-26 10:01:38 +00:00
|
|
|
var instance: TabbyAgent | undefined = undefined;
|
2023-06-15 15:53:21 +00:00
|
|
|
|
|
|
|
|
export function agent(): TabbyAgent {
|
|
|
|
|
if (!instance) {
|
|
|
|
|
throw new Error("Tabby Agent not initialized");
|
|
|
|
|
}
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function createAgentInstance(context: ExtensionContext): Promise<TabbyAgent> {
|
|
|
|
|
if (!instance) {
|
|
|
|
|
const extensionDataStore: DataStore = {
|
|
|
|
|
data: {},
|
|
|
|
|
load: async function () {
|
|
|
|
|
this.data = context.globalState.get("data", {});
|
|
|
|
|
},
|
|
|
|
|
save: async function () {
|
|
|
|
|
context.globalState.update("data", this.data);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const agent = await TabbyAgent.create({ dataStore: env.appHost === "desktop" ? undefined : extensionDataStore });
|
2023-09-28 02:15:39 +00:00
|
|
|
const initPromise = agent.initialize(buildInitOptions(context));
|
2023-08-11 11:39:17 +00:00
|
|
|
workspace.onDidChangeConfiguration(async (event) => {
|
|
|
|
|
await initPromise;
|
|
|
|
|
const configuration = workspace.getConfiguration("tabby");
|
|
|
|
|
if (event.affectsConfiguration("tabby.api.endpoint")) {
|
|
|
|
|
const endpoint = configuration.get<string>("api.endpoint");
|
|
|
|
|
if (endpoint && endpoint.trim().length > 0) {
|
|
|
|
|
agent.updateConfig("server.endpoint", endpoint);
|
|
|
|
|
} else {
|
|
|
|
|
agent.clearConfig("server.endpoint");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (event.affectsConfiguration("tabby.usage.anonymousUsageTracking")) {
|
|
|
|
|
const anonymousUsageTrackingDisabled = configuration.get<boolean>("usage.anonymousUsageTracking", false);
|
|
|
|
|
agent.updateConfig("anonymousUsageTracking.disable", anonymousUsageTrackingDisabled);
|
2023-06-15 15:53:21 +00:00
|
|
|
}
|
2023-09-28 02:15:39 +00:00
|
|
|
if (event.affectsConfiguration("tabby.inlineCompletion.triggerMode")) {
|
|
|
|
|
const triggerMode = configuration.get<string>("inlineCompletion.triggerMode", "automatic");
|
|
|
|
|
agent.updateClientProperties("user", "vscode.triggerMode", triggerMode);
|
|
|
|
|
}
|
|
|
|
|
if (event.affectsConfiguration("tabby.keybindings")) {
|
|
|
|
|
const keybindings = configuration.get<string>("keybindings", "vscode-style");
|
|
|
|
|
agent.updateClientProperties("user", "vscode.keybindings", keybindings);
|
|
|
|
|
}
|
2023-06-15 15:53:21 +00:00
|
|
|
});
|
|
|
|
|
instance = agent;
|
|
|
|
|
}
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
2023-09-26 10:01:38 +00:00
|
|
|
|
|
|
|
|
export async function disposeAgentInstance(): Promise<void> {
|
|
|
|
|
if (instance) {
|
|
|
|
|
await instance.finalize();
|
|
|
|
|
instance = undefined;
|
|
|
|
|
}
|
|
|
|
|
}
|