import { Observable, Subject } from 'rxjs';
import type { ActionImpl, AnyVariable } from '../types';
export declare class TaskCancelledError extends Error {
    task_id: string;
    constructor(message: string, task_id: string);
}
export declare class TaskError extends Error {
    task_id: string;
    constructor(message: string, task_id: string);
}
interface InitMessage {
    message: {
        channel: string;
    };
    type: 'init';
}
interface PingPongMessage {
    message: null;
    type: 'ping' | 'pong';
}
export declare enum TaskStatus {
    CANCELED = "CANCELED",
    COMPLETE = "COMPLETE",
    ERROR = "ERROR",
    PROGRESS = "PROGRESS"
}
type TaskNotificationMessageContent = {
    status: TaskStatus.COMPLETE;
    task_id: string;
} | {
    status: TaskStatus.ERROR;
    task_id: string;
    error: string;
} | {
    status: TaskStatus.PROGRESS;
    task_id: string;
    progress: number;
    message: string;
} | {
    status: TaskStatus.CANCELED;
    task_id: string;
};
export interface TaskNotificationMessage {
    message: TaskNotificationMessageContent;
    type: 'message';
}
export interface ProgressNotificationMessage {
    type: 'message';
    message: Extract<TaskNotificationMessageContent, {
        status: TaskStatus.PROGRESS;
    }>;
}
export interface ServerErrorMessage {
    message: {
        error: string;
        time: string;
    };
    type: 'message';
}
export interface VariableRequestMessage {
    message: {
        /** Channel to send in the response */
        __rchan: string;
        variable: AnyVariable<any>;
    };
    type: 'message';
}
export interface ActionMessage {
    message: {
        /**
         * Action implementation instance
         */
        action: ActionImpl | null;
        /**
         * Execution uid
         */
        uid: string;
    };
    type: 'message';
}
export interface BackendStoreMessage {
    message: {
        store_uid: string;
        value: any;
        sequence_number: number;
    };
    type: 'message';
}
export interface BackendStorePatchMessage {
    message: {
        store_uid: string;
        patches: Array<{
            op: 'add' | 'remove' | 'replace' | 'move' | 'copy' | 'test';
            path: string;
            value?: any;
            from?: string;
        }>;
        sequence_number: number;
    };
    type: 'message';
}
export interface ServerVariableMessage {
    message: {
        __type: 'ServerVariable';
        uid: string;
        sequence_number: number;
    };
    type: 'message';
}
export interface CustomMessage {
    message: {
        data: any;
        kind: string;
        /** Optional ID, returned from server custom messages coming as a response for a custom client message */
        __response_for?: string;
        /** Optional ID, should be included as `__response_for` when the server responds to this message */
        __rchan?: string;
    };
    type: 'custom';
}
export type WebSocketMessage = InitMessage | PingPongMessage | TaskNotificationMessage | ProgressNotificationMessage | ServerErrorMessage | ServerVariableMessage | VariableRequestMessage | ActionMessage | BackendStoreMessage | BackendStorePatchMessage | CustomMessage;
export declare function isInitMessage(message: WebSocketMessage): message is InitMessage;
export declare function isTaskNotification(message: WebSocketMessage): message is TaskNotificationMessage;
export declare function isServerVariableMessage(message: WebSocketMessage): message is ServerVariableMessage;
export declare function isServerErrorMessage(message: WebSocketMessage): message is ServerErrorMessage;
export declare function isVariableRequestMessage(message: WebSocketMessage): message is VariableRequestMessage;
export declare function isActionMessage(message: WebSocketMessage): message is ActionMessage;
export declare function isBackendStoreMessage(message: WebSocketMessage): message is BackendStoreMessage;
export declare function isBackendStorePatchMessage(message: WebSocketMessage): message is BackendStorePatchMessage;
export declare function isCustomMessage(message: WebSocketMessage): message is CustomMessage;
export interface WebSocketClientInterface {
    actionMessages$: (executionId: string) => Observable<ActionImpl | null>;
    backendStoreMessages$(): Observable<BackendStoreMessage['message']>;
    backendStorePatchMessages$(): Observable<BackendStorePatchMessage['message']>;
    serverVariableMessages$(): Observable<ServerVariableMessage['message']>;
    channel$: () => Observable<string>;
    customMessages$: () => Observable<CustomMessage>;
    getChannel: () => Promise<string>;
    progressUpdates$: (...task_ids: string[]) => Observable<ProgressNotificationMessage>;
    sendCustomMessage(kind: string, data: any, awaitResponse?: boolean): Promise<CustomMessage | null>;
    sendMessage(value: any, channel: string, chunkCount?: number): void;
    sendVariable: (value: any, channel: string) => void;
    serverErrors$: () => Observable<ServerErrorMessage>;
    taskStatusUpdates$: (...task_ids: string[]) => Observable<TaskStatus>;
    variableRequests$: () => Observable<VariableRequestMessage>;
    waitForTask: (task_id: string) => Promise<any>;
}
/**
 * The WebsocketClient class exposes an interface for easily dealing with the websocket connection to the Dara backend.
 * It provides easy helpers for fetching the current channel and working with tasks.
 */
export declare class WebSocketClient implements WebSocketClientInterface {
    #private;
    channel: Promise<string>;
    messages$: Subject<WebSocketMessage>;
    socket: WebSocket;
    liveReload: boolean;
    closeHandler: ((event: CloseEvent) => void) | null;
    maxAttempts: number;
    maxAttemptsReached: boolean;
    constructor(_socketUrl: string, _liveReload?: boolean);
    initialize(isReconnect?: boolean): WebSocket;
    verifySessionAfterFailedConnect(): Promise<void>;
    addReconnectResumeListeners(): void;
    removeReconnectResumeListeners(): void;
    resumeReconnectBurst(): void;
    onResumeSignal(): void;
    onVisibilityResumeSignal(): void;
    /**
     * Close handler to attempt to reconnect on WS closed
     */
    onClose(): void;
    /**
     * Forcefully close the websocket connection, first clearing the closehandler
     */
    close(): void;
    /**
     * Get the channel setup for this websocket client instance
     */
    getChannel(): Promise<string>;
    backendStoreMessages$(): Observable<BackendStoreMessage['message']>;
    backendStorePatchMessages$(): Observable<BackendStorePatchMessage['message']>;
    serverVariableMessages$(): Observable<ServerVariableMessage['message']>;
    /**
     * Get the observable to receive the new channel when the socket reconnects
     */
    channel$(): Observable<string>;
    /**
     * Get the observable to receive status updates for given tasks
     *
     * @param task_ids the ids of tasks to receive updates from
     */
    taskStatusUpdates$(...task_ids: string[]): Observable<TaskStatus>;
    /**
     * Get the observable to receive progress updates for given tasks
     *
     * @param task_ids the ids of the task to receive updates from
     */
    progressUpdates$(...task_ids: string[]): Observable<ProgressNotificationMessage>;
    /**
     * Get the observable to receive server error messages
     */
    serverErrors$(): Observable<ServerErrorMessage>;
    /**
     * Get the observable to receive variable request messages
     */
    variableRequests$(): Observable<VariableRequestMessage>;
    /**
     * Get the observable to receive action implementations to execute for a given execution id
     *
     * @param executionId id of the execution to receive action implementations for
     */
    actionMessages$(executionId: string): Observable<ActionImpl | null>;
    /**
     * Get the observable to receive custom messages
     */
    customMessages$(): Observable<CustomMessage>;
    /**
     * Returns a promise that will resolve when the task is completed. If the task is cancelled or errored then this will throw an
     * error to signify that.
     *
     * @param task_id the id of the task to wait for
     */
    waitForTask(task_id: string): Promise<any>;
    /**
     * Send variable value to the backend
     *
     * @param value variable value to send
     * @param channel return channel to identify the message
     */
    sendVariable(value: any, channel: string): void;
    /**
     * Send an internal message to the backend. This is used to respond to sendAndWait calls from the backend.
     *
     * @param value variable value to send
     * @param channel return channel to identify the message
     * @param chunkCount total number of chunks this message has been split into
     */
    sendMessage(value: any, channel: string, chunkCount?: number): void;
    /**
     * Send custom message to the backend
     *
     * @param kind kind of custom message
     * @param data custom message data
     * @param awaitResponse whether to await a response for this message
     */
    sendCustomMessage(kind: string, data: any, awaitResponse?: boolean): Promise<CustomMessage | null>;
}
/**
 * Set up websocket connection and handlers.
 *
 * @param liveReload whether to enable live reload
 */
export declare function setupWebsocket(liveReload: boolean): WebSocketClient;
export {};
//# sourceMappingURL=websocket.d.ts.map