UNPKG

@hybrid-compute/remote

Version:

Remote compute backend using fetch or WebSocket transport for distributed task execution.

82 lines 2.89 kB
import { ComputeBackendInterface } from '@hybrid-compute/core'; import { RemoteComputeOptionsInterface } from './types.js'; export * from './types.js'; /** * RemoteCompute is a backend that delegates compute tasks to a remote API * using either HTTP requests (fetch) or a persistent WebSocket connection. * * It supports bidirectional communication, which is useful for low-latency * or streaming scenarios using WebSocket, or traditional stateless interaction * using fetch. * * @remarks * WebSocket-based transport allows concurrent request handling via an internal * request/response map using `id`. This is useful when running multiple tasks in parallel. * * Fetch transport is simpler and more interoperable with typical REST APIs. * * @example Fetch transport * ```ts * const remote = new RemoteCompute({ * transport: 'fetch', * endpoint: 'https://api.example.com/compute', * canRunTasks: ['translateText'] * }); * * const result = await remote.runTask('translateText', { text: 'hello' }); * ``` * * @example WebSocket transport * ```ts * const remote = new RemoteCompute({ * transport: 'websocket', * endpoint: 'wss://api.example.com/ws', * canRunTasks: ['analyzeSentiment'] * }); * * const result = await remote.runTask('analyzeSentiment', { text: 'It works!' }); * ``` * * @see https://developer.mozilla.org/en-US/docs/Web/API/fetch * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket */ export declare class RemoteCompute implements ComputeBackendInterface { private transport; private endpoint; private canRunSet; private socket?; private pending; private nextId; /** * Initializes the remote compute backend. * * @param options - Transport type and endpoint configuration. */ constructor(options: RemoteComputeOptionsInterface); /** * Determines if this backend is allowed to handle the given task. * * @param taskName - Name of the task. * @returns `true` if task is permitted, or if no restrictions are set. */ canRun(taskName: string): boolean; /** * Executes the specified task using remote communication. * * @typeParam Input - The input data structure expected by the task. * @typeParam Output - The output structure returned by the task. * * @param taskName - Name of the remote task. * @param input - Input data to send. * @returns A promise resolving to the result from the server. */ runTask<Input, Output>(taskName: string, input: Input): Promise<Output>; } /** * Factory to create a RemoteCompute instance with given options. * * @param options - Remote connection configuration. * @returns Instance of RemoteCompute. */ export declare function createRemoteCompute(options: RemoteComputeOptionsInterface): RemoteCompute; //# sourceMappingURL=index.d.ts.map