import { CancelablePromise, LogEventRequest as ApiLogEventRequest, CompletionResponse as ApiCompletionResponse, } from "./generated"; import { AgentConfig } from "./AgentConfig"; export type AgentInitOptions = { config: Partial; client: string; }; export type CompletionRequest = { filepath: string; language: string; text: string; position: number; maxPrefixLines: number; maxSuffixLines: number; }; export type CompletionResponse = ApiCompletionResponse; export type LogEventRequest = ApiLogEventRequest; export type AgentStatus = "notInitialized" | "ready" | "disconnected" | "unauthorized"; export interface AgentFunction { initialize(options: Partial): Promise; updateConfig(config: Partial): Promise; getConfig(): AgentConfig; getStatus(): AgentStatus; /** * @returns the auth url for redirecting, and the code for next step `waitingForAuth`, only return value when * `AgentStatus` is `unauthorized`, return null otherwise * @throws Error if agent is not initialized */ requestAuthUrl(): CancelablePromise<{ authUrl: string; code: string } | null>; /** * 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; /** * @param request * @returns * @throws Error if agent is not initialized */ getCompletions(request: CompletionRequest): CancelablePromise; /** * @param event * @returns * @throws Error if agent is not initialized */ postEvent(event: LogEventRequest): CancelablePromise; } export type StatusChangedEvent = { event: "statusChanged"; status: AgentStatus; }; export type ConfigUpdatedEvent = { event: "configUpdated"; config: AgentConfig; }; export type AuthRequiredEvent = { event: "authRequired"; server: AgentConfig["server"] }; export type AgentEvent = StatusChangedEvent | ConfigUpdatedEvent | AuthRequiredEvent; export const agentEventNames: AgentEvent["event"][] = ["statusChanged", "configUpdated", "authRequired"]; export interface AgentEventEmitter { on(eventName: T["event"], callback: (event: T) => void): this; } export type Agent = AgentFunction & AgentEventEmitter;