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-06-06 13:29:04 +00:00
|
|
|
import { AgentConfig } from "./AgentConfig";
|
|
|
|
|
|
|
|
|
|
export type AgentInitOptions = {
|
2023-06-15 15:53:21 +00:00
|
|
|
config: Partial<AgentConfig>;
|
|
|
|
|
client: string;
|
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-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-06-15 15:53:21 +00:00
|
|
|
initialize(options: Partial<AgentInitOptions>): Promise<boolean>;
|
2023-06-24 21:43:13 +00:00
|
|
|
updateConfig(config: Partial<AgentConfig>): Promise<boolean>;
|
2023-07-03 03:19:09 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @returns the current config
|
|
|
|
|
*
|
|
|
|
|
* Configuration precedence:
|
|
|
|
|
* 1. Default config
|
|
|
|
|
* 2. User config file `~/.tabby/agent/config.toml` (not available in browser)
|
|
|
|
|
* 3. Agent `initialize` and `updateConfig` methods
|
|
|
|
|
*/
|
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-06-24 21:43:13 +00:00
|
|
|
* @returns the auth url for redirecting, and the code for next step `waitingForAuth`, only return value when
|
|
|
|
|
* `AgentStatus` is `unauthorized`, return null otherwise
|
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,
|
|
|
|
|
* returns nothing, but `AgentStatus` will change to `ready` if resolved successfully
|
|
|
|
|
* @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-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;
|