2023-05-24 01:50:57 +00:00
|
|
|
import axios from "axios";
|
2023-05-24 16:21:38 +00:00
|
|
|
import { EventEmitter } from "events";
|
2023-06-02 03:58:34 +00:00
|
|
|
import { v4 as uuid } from "uuid";
|
2023-06-06 13:29:04 +00:00
|
|
|
import deepEqual from "deep-equal";
|
|
|
|
|
import deepMerge from "deepmerge";
|
2023-06-07 16:11:31 +00:00
|
|
|
import { TabbyApi, CancelablePromise, ApiError, CancelError } from "./generated";
|
2023-06-06 13:29:04 +00:00
|
|
|
import { sleep, cancelable, splitLines, isBlank } from "./utils";
|
2023-06-07 16:11:31 +00:00
|
|
|
import { Agent, AgentEvent, AgentInitOptions, CompletionRequest, CompletionResponse, LogEventRequest } from "./Agent";
|
2023-06-06 13:29:04 +00:00
|
|
|
import { AgentConfig, defaultAgentConfig } from "./AgentConfig";
|
|
|
|
|
import { CompletionCache } from "./CompletionCache";
|
2023-06-08 17:19:10 +00:00
|
|
|
import { postprocess } from "./postprocess";
|
2023-06-06 14:25:31 +00:00
|
|
|
import { rootLogger, allLoggers } from "./logger";
|
2023-05-24 01:50:57 +00:00
|
|
|
|
|
|
|
|
export class TabbyAgent extends EventEmitter implements Agent {
|
2023-06-06 14:25:31 +00:00
|
|
|
private readonly logger = rootLogger.child({ component: "TabbyAgent" });
|
2023-06-06 13:29:04 +00:00
|
|
|
private config: AgentConfig = defaultAgentConfig;
|
2023-05-24 01:50:57 +00:00
|
|
|
private status: "connecting" | "ready" | "disconnected" = "connecting";
|
|
|
|
|
private api: TabbyApi;
|
2023-06-06 13:29:04 +00:00
|
|
|
private completionCache: CompletionCache = new CompletionCache();
|
2023-05-24 01:50:57 +00:00
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
2023-06-06 13:29:04 +00:00
|
|
|
this.onConfigUpdated();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private onConfigUpdated() {
|
2023-06-06 14:25:31 +00:00
|
|
|
allLoggers.forEach((logger) => (logger.level = this.config.logs.level));
|
2023-06-06 13:29:04 +00:00
|
|
|
this.api = new TabbyApi({ BASE: this.config.server.endpoint });
|
2023-05-24 01:50:57 +00:00
|
|
|
this.ping();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private changeStatus(status: "connecting" | "ready" | "disconnected") {
|
|
|
|
|
if (this.status != status) {
|
|
|
|
|
this.status = status;
|
|
|
|
|
const event: AgentEvent = { event: "statusChanged", status };
|
2023-06-06 14:25:31 +00:00
|
|
|
this.logger.debug({ event }, "Status changed");
|
2023-05-24 16:21:38 +00:00
|
|
|
super.emit("statusChanged", event);
|
2023-05-24 01:50:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async ping(tries: number = 0): Promise<boolean> {
|
|
|
|
|
try {
|
2023-06-06 13:29:04 +00:00
|
|
|
await axios.get(this.config.server.endpoint);
|
2023-05-24 01:50:57 +00:00
|
|
|
this.changeStatus("ready");
|
|
|
|
|
return true;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (tries > 5) {
|
|
|
|
|
this.changeStatus("disconnected");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
this.changeStatus("connecting");
|
|
|
|
|
const pingRetryDelay = 1000;
|
|
|
|
|
await sleep(pingRetryDelay);
|
|
|
|
|
return this.ping(tries + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-06 14:25:31 +00:00
|
|
|
private callApi<Request, Response>(
|
|
|
|
|
api: (request: Request) => CancelablePromise<Response>,
|
|
|
|
|
request: Request
|
|
|
|
|
): CancelablePromise<Response> {
|
|
|
|
|
this.logger.debug({ api: api.name, request }, "API request");
|
2023-06-07 16:11:31 +00:00
|
|
|
const promise = api.call(this.api.v1, request);
|
2023-05-29 02:09:44 +00:00
|
|
|
return cancelable(
|
2023-05-24 01:50:57 +00:00
|
|
|
promise
|
2023-06-06 14:25:31 +00:00
|
|
|
.then((response: Response) => {
|
|
|
|
|
this.logger.debug({ api: api.name, response }, "API response");
|
2023-05-24 01:50:57 +00:00
|
|
|
this.changeStatus("ready");
|
2023-06-06 14:25:31 +00:00
|
|
|
return response;
|
2023-05-24 01:50:57 +00:00
|
|
|
})
|
2023-06-07 16:11:31 +00:00
|
|
|
.catch((error: CancelError) => {
|
|
|
|
|
this.logger.debug({ api: api.name }, "API request canceled");
|
|
|
|
|
throw error;
|
|
|
|
|
})
|
2023-06-06 14:25:31 +00:00
|
|
|
.catch((error: ApiError) => {
|
|
|
|
|
this.logger.error({ api: api.name, error }, "API error");
|
2023-05-24 01:50:57 +00:00
|
|
|
this.changeStatus("disconnected");
|
2023-06-06 14:25:31 +00:00
|
|
|
throw error;
|
2023-05-29 02:09:44 +00:00
|
|
|
}),
|
|
|
|
|
() => {
|
2023-05-24 01:50:57 +00:00
|
|
|
promise.cancel();
|
2023-05-29 02:09:44 +00:00
|
|
|
}
|
|
|
|
|
);
|
2023-05-24 01:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-07 16:11:31 +00:00
|
|
|
private createSegments(request: CompletionRequest): { prefix: string; suffix: string } {
|
|
|
|
|
// max to 20 lines in prefix and max to 20 lines in suffix
|
2023-06-02 03:58:34 +00:00
|
|
|
const maxLines = 20;
|
|
|
|
|
const prefix = request.text.slice(0, request.position);
|
2023-06-07 16:11:31 +00:00
|
|
|
const prefixLines = splitLines(prefix);
|
|
|
|
|
const suffix = request.text.slice(request.position);
|
|
|
|
|
const suffixLines = splitLines(suffix);
|
|
|
|
|
return {
|
|
|
|
|
prefix: prefixLines.slice(Math.max(prefixLines.length - maxLines, 0)).join(""),
|
|
|
|
|
suffix: suffixLines.slice(0, maxLines).join(""),
|
|
|
|
|
};
|
2023-06-02 03:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-06 13:29:04 +00:00
|
|
|
public initialize(params: AgentInitOptions): boolean {
|
|
|
|
|
if (params.config) {
|
|
|
|
|
this.updateConfig(params.config);
|
|
|
|
|
}
|
2023-06-06 14:25:31 +00:00
|
|
|
if (params.client) {
|
|
|
|
|
// Client info is only used in logging for now
|
|
|
|
|
// `pino.Logger.setBindings` is not present in the browser
|
|
|
|
|
allLoggers.forEach((logger) => logger.setBindings && logger.setBindings({ client: params.client }));
|
|
|
|
|
}
|
|
|
|
|
this.logger.debug({ params }, "Initialized");
|
2023-06-06 13:29:04 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public updateConfig(config: AgentConfig): boolean {
|
2023-06-06 14:25:31 +00:00
|
|
|
const mergedConfig = deepMerge(this.config, config);
|
|
|
|
|
if (!deepEqual(this.config, mergedConfig)) {
|
|
|
|
|
this.config = mergedConfig;
|
2023-06-06 13:29:04 +00:00
|
|
|
this.onConfigUpdated();
|
|
|
|
|
const event: AgentEvent = { event: "configUpdated", config: this.config };
|
2023-06-06 14:25:31 +00:00
|
|
|
this.logger.debug({ event }, "Config updated");
|
2023-06-06 13:29:04 +00:00
|
|
|
super.emit("configUpdated", event);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2023-05-24 01:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-06 13:29:04 +00:00
|
|
|
public getConfig(): AgentConfig {
|
|
|
|
|
return this.config;
|
2023-05-24 01:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
2023-05-24 16:21:38 +00:00
|
|
|
public getStatus(): "connecting" | "ready" | "disconnected" {
|
|
|
|
|
return this.status;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-24 01:50:57 +00:00
|
|
|
public getCompletions(request: CompletionRequest): CancelablePromise<CompletionResponse> {
|
2023-05-29 02:09:44 +00:00
|
|
|
if (this.completionCache.has(request)) {
|
2023-06-06 14:25:31 +00:00
|
|
|
this.logger.debug({ request }, "Completion cache hit");
|
2023-05-29 02:09:44 +00:00
|
|
|
return new CancelablePromise((resolve) => {
|
|
|
|
|
resolve(this.completionCache.get(request));
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-06-07 16:11:31 +00:00
|
|
|
const segments = this.createSegments(request);
|
|
|
|
|
if (isBlank(segments.prefix)) {
|
|
|
|
|
this.logger.debug("Segment prefix is blank, returning empty completion response");
|
2023-06-02 03:58:34 +00:00
|
|
|
return new CancelablePromise((resolve) => {
|
|
|
|
|
resolve({
|
|
|
|
|
id: "agent-" + uuid(),
|
2023-06-06 13:29:04 +00:00
|
|
|
choices: [],
|
2023-06-02 03:58:34 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-06-07 16:11:31 +00:00
|
|
|
const promise = this.callApi(this.api.v1.completion, {
|
2023-06-06 14:25:31 +00:00
|
|
|
language: request.language,
|
2023-06-07 16:11:31 +00:00
|
|
|
segments,
|
2023-06-06 14:25:31 +00:00
|
|
|
});
|
2023-05-29 02:09:44 +00:00
|
|
|
return cancelable(
|
2023-06-08 17:19:10 +00:00
|
|
|
promise
|
|
|
|
|
.then((response) => {
|
|
|
|
|
return postprocess(request, response);
|
|
|
|
|
})
|
|
|
|
|
.then((response) => {
|
|
|
|
|
this.completionCache.set(request, response);
|
|
|
|
|
return response;
|
|
|
|
|
}),
|
2023-05-29 02:09:44 +00:00
|
|
|
() => {
|
|
|
|
|
promise.cancel();
|
|
|
|
|
}
|
|
|
|
|
);
|
2023-05-24 01:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-07 16:11:31 +00:00
|
|
|
public postEvent(request: LogEventRequest): CancelablePromise<boolean> {
|
|
|
|
|
return this.callApi(this.api.v1.event, request);
|
2023-05-24 01:50:57 +00:00
|
|
|
}
|
|
|
|
|
}
|