import type { Subprocess } from "bun"; import type { RefCountedWorkerHandle } from "./worker-client.js"; /** Requests every shared worker answers inline: a tagged `ping`, and `shutdown` (a client found a stale tag). */ export type SharedWorkerControl = { type: "ping"; id: string; } | { type: "shutdown"; id: string; }; /** Reply to `ping`, carrying the worker's launch tag. */ export type SharedWorkerPong = { type: "pong"; id: string; tag: string; }; /** Error surfaced when a worker closes the connection (idle exit, replacement, crash); clients reconnect on demand. */ export declare const WORKER_CONNECTION_CLOSED = "worker connection closed"; /** A spawned worker process and the file its stdout/stderr go to. */ export interface SpawnedWorker { proc: Subprocess; logPath: string; } /** How to start one shared worker. */ export interface SharedWorkerLaunch { /** Names the worker in errors, e.g. `tiny onnx worker for lfm2.5-230m`. */ label: string; /** Launch identity echoed in `pong`; a live worker answering with another tag is told to shut down and replaced. */ tag: string; /** Start the worker (normally via {@link spawnDetached}) so it binds `endpoint` and logs to `logPath`. */ spawn(endpoint: string, logPath: string): Promise; } /** Stdout/stderr log file of the worker called `name` (kept beside its endpoint, never derived from a pipe name). */ export declare function sharedWorkerLogPath(runtimeDir: string, name: string): string; /** * Handle that resolves its backing worker asynchronously. Clients need a * handle synchronously (they subscribe and send in the same tick), but reaching * a shared worker means probing and possibly spawning; sends queue until the * connection lands, and a failed connect surfaces through `onError`. * `terminate()` only drops this connection — the worker keeps serving others. */ export declare class LazyWorkerHandle implements RefCountedWorkerHandle { #private; constructor(connect: () => Promise>); send(message: Req): void; onMessage(handler: (message: Res) => void): () => void; onError(handler: (error: Error) => void): () => void; terminate(): Promise; ref(): void; unref(): void; } /** * Detach a worker so it outlives this omp process; its output goes to * `logPath`, truncated first unless `append` (for a worker that may lose a * start race to a live one writing the same log). */ export declare function spawnDetached(cmd: string[], cwd: string | undefined, env: Record, logPath: string, options?: { append?: boolean; }): SpawnedWorker; /** * Connect to the worker called `name` under `runtimeDir`, spawning it when * absent or replacing it when its launch tag is stale. A concurrent omp * process may win the spawn race; our child then fails to bind (`EADDRINUSE`) * and exits while the probe adopts the winner. */ export declare function connectSharedWorker(launch: SharedWorkerLaunch, runtimeDir: string, name: string): Promise>; /** * Distribution smoke test: spawn the worker into a scratch directory under * `runtimeDir`, wait for its tagged `pong`, then kill it. Proves the hidden * CLI selector boots in binary, source-link, and tarball installs. */ export declare function smokeTestSharedWorker(launch: SharedWorkerLaunch, runtimeDir: string, name: string, timeoutMs: number): Promise;