2023-06-02 03:58:34 +00:00
|
|
|
import {
|
|
|
|
|
CancelablePromise,
|
2023-06-07 16:11:31 +00:00
|
|
|
LogEventRequest as ApiLogEventRequest,
|
2023-06-02 03:58:34 +00:00
|
|
|
CompletionResponse as ApiCompletionResponse,
|
|
|
|
|
} from "./generated";
|
|
|
|
|
|
2023-08-11 11:39:17 +00:00
|
|
|
import { AgentConfig, PartialAgentConfig } from "./AgentConfig";
|
2023-06-06 13:29:04 +00:00
|
|
|
|
2023-08-11 11:39:17 +00:00
|
|
|
export type AgentInitOptions = Partial<{
|
|
|
|
|
config: PartialAgentConfig;
|
2023-06-15 15:53:21 +00:00
|
|
|
client: string;
|
2023-08-11 11:39:17 +00:00
|
|
|
}>;
|
2023-06-06 13:29:04 +00:00
|
|
|
|
2023-06-02 03:58:34 +00:00
|
|
|
export type CompletionRequest = {
|
|
|
|
|
filepath: string;
|
|
|
|
|
language: string;
|
|
|
|
|
text: string;
|
|
|
|
|
position: number;
|
2023-07-06 16:31:15 +00:00
|
|
|
maxPrefixLines?: number;
|
|
|
|
|
maxSuffixLines?: number;
|
2023-06-02 03:58:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type CompletionResponse = ApiCompletionResponse;
|
2023-05-24 01:50:57 +00:00
|
|
|
|
2023-06-07 16:11:31 +00:00
|
|
|
export type LogEventRequest = ApiLogEventRequest;
|
|
|
|
|
|
2023-08-11 11:39:17 +00:00
|
|
|
/**
|
|
|
|
|
* `notInitialized`: When the agent is not initialized.
|
|
|
|
|
* `ready`: When the agent get a valid response from the server, and is ready to use.
|
|
|
|
|
* `disconnected`: When the agent failed to connect to the server.
|
|
|
|
|
* `unauthorized`: When the server is set to a Tabby Cloud endpoint that requires auth,
|
|
|
|
|
* and no `Authorization` request header is provided in the agent config,
|
|
|
|
|
* and user has not completed the auth flow or the auth token is expired.
|
|
|
|
|
* See also `requestAuthUrl` and `waitForAuthToken`.
|
|
|
|
|
*/
|
2023-06-15 15:53:21 +00:00
|
|
|
export type AgentStatus = "notInitialized" | "ready" | "disconnected" | "unauthorized";
|
|
|
|
|
|
2023-05-24 01:50:57 +00:00
|
|
|
export interface AgentFunction {
|
2023-08-11 11:39:17 +00:00
|
|
|
/**
|
|
|
|
|
* Initialize agent. Client should call this method before calling any other methods.
|
|
|
|
|
* @param options
|
|
|
|
|
*/
|
|
|
|
|
initialize(options: AgentInitOptions): Promise<boolean>;
|
2023-07-03 03:19:09 +00:00
|
|
|
|
|
|
|
|
/**
|
2023-08-11 11:39:17 +00:00
|
|
|
* The agent configuration has the following levels, will be deep merged in the order:
|
2023-07-03 03:19:09 +00:00
|
|
|
* 1. Default config
|
|
|
|
|
* 2. User config file `~/.tabby/agent/config.toml` (not available in browser)
|
|
|
|
|
* 3. Agent `initialize` and `updateConfig` methods
|
2023-08-11 11:39:17 +00:00
|
|
|
*
|
|
|
|
|
* This method will update the 3rd level config.
|
|
|
|
|
* @param key the key of the config to update, can be nested with dot, e.g. `server.endpoint`
|
|
|
|
|
* @param value the value to set
|
|
|
|
|
*/
|
|
|
|
|
updateConfig(key: string, value: any): Promise<boolean>;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear the 3rd level config.
|
|
|
|
|
* @param key the key of the config to clear, can be nested with dot, e.g. `server.endpoint`
|
|
|
|
|
*/
|
|
|
|
|
clearConfig(key: string): Promise<boolean>;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @returns the current config
|
2023-07-03 03:19:09 +00:00
|
|
|
*/
|
2023-06-06 13:29:04 +00:00
|
|
|
getConfig(): AgentConfig;
|
2023-07-03 03:19:09 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @returns the current status
|
|
|
|
|
*/
|
2023-06-15 15:53:21 +00:00
|
|
|
getStatus(): AgentStatus;
|
|
|
|
|
|
|
|
|
|
/**
|
2023-08-11 11:39:17 +00:00
|
|
|
* Request auth url for Tabby Cloud endpoint. Only return value when the `AgentStatus` is `unauthorized`.
|
|
|
|
|
* Otherwise, return null. See also `AgentStatus`.
|
|
|
|
|
* @returns the auth url for redirecting, and the code for next step `waitingForAuth`
|
2023-06-15 15:53:21 +00:00
|
|
|
* @throws Error if agent is not initialized
|
|
|
|
|
*/
|
2023-06-24 21:43:13 +00:00
|
|
|
requestAuthUrl(): CancelablePromise<{ authUrl: string; code: string } | null>;
|
2023-06-15 15:53:21 +00:00
|
|
|
|
|
|
|
|
/**
|
2023-06-24 21:43:13 +00:00
|
|
|
* Wait for auth token to be ready after redirecting user to auth url,
|
2023-08-11 11:39:17 +00:00
|
|
|
* returns nothing, but `AgentStatus` will change to `ready` if resolved successfully.
|
2023-06-24 21:43:13 +00:00
|
|
|
* @param code from `requestAuthUrl`
|
|
|
|
|
* @throws Error if agent is not initialized
|
|
|
|
|
*/
|
|
|
|
|
waitForAuthToken(code: string): CancelablePromise<any>;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param request
|
2023-06-15 15:53:21 +00:00
|
|
|
* @returns
|
|
|
|
|
* @throws Error if agent is not initialized
|
|
|
|
|
*/
|
2023-05-24 01:50:57 +00:00
|
|
|
getCompletions(request: CompletionRequest): CancelablePromise<CompletionResponse>;
|
2023-06-15 15:53:21 +00:00
|
|
|
|
|
|
|
|
/**
|
2023-06-24 21:43:13 +00:00
|
|
|
* @param event
|
2023-06-15 15:53:21 +00:00
|
|
|
* @returns
|
|
|
|
|
* @throws Error if agent is not initialized
|
|
|
|
|
*/
|
2023-06-07 16:11:31 +00:00
|
|
|
postEvent(event: LogEventRequest): CancelablePromise<boolean>;
|
2023-05-24 01:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
2023-05-24 16:21:38 +00:00
|
|
|
export type StatusChangedEvent = {
|
2023-05-24 01:50:57 +00:00
|
|
|
event: "statusChanged";
|
2023-06-15 15:53:21 +00:00
|
|
|
status: AgentStatus;
|
2023-06-02 03:58:34 +00:00
|
|
|
};
|
2023-06-06 13:29:04 +00:00
|
|
|
export type ConfigUpdatedEvent = {
|
|
|
|
|
event: "configUpdated";
|
|
|
|
|
config: AgentConfig;
|
|
|
|
|
};
|
2023-08-11 11:39:17 +00:00
|
|
|
/**
|
|
|
|
|
* This event is emitted when the server is set to a Tabby Cloud endpoint that requires auth,
|
|
|
|
|
* and no `Authorization` request header is provided in the agent config.
|
|
|
|
|
*/
|
2023-06-24 21:43:13 +00:00
|
|
|
export type AuthRequiredEvent = {
|
|
|
|
|
event: "authRequired";
|
2023-07-03 03:19:09 +00:00
|
|
|
server: AgentConfig["server"];
|
2023-06-24 21:43:13 +00:00
|
|
|
};
|
2023-05-24 01:50:57 +00:00
|
|
|
|
2023-06-24 21:43:13 +00:00
|
|
|
export type AgentEvent = StatusChangedEvent | ConfigUpdatedEvent | AuthRequiredEvent;
|
|
|
|
|
export const agentEventNames: AgentEvent["event"][] = ["statusChanged", "configUpdated", "authRequired"];
|
2023-05-24 01:50:57 +00:00
|
|
|
|
|
|
|
|
export interface AgentEventEmitter {
|
|
|
|
|
on<T extends AgentEvent>(eventName: T["event"], callback: (event: T) => void): this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type Agent = AgentFunction & AgentEventEmitter;
|