/** * `Bridge` — the RPC request/response correlation layer over a `Channel`. * * Replaces `post-robot`. Tiny and per-instance: an incrementing request id, a * pending-promise map, one channel listener, a timeout per request. No * module-global state, so many bridges (one per mini-app instance) coexist. * * Used by both ends: * - the client calls `handshake()`, `request()`, `requestPermission()`; * - the host listens via `on('rpc:request' | 'handshake:init' | ...)` and * replies / pushes events with `send()`. */ import { Channel } from './channels'; import { ApnaMessage, HandshakeAck, MessageType, PermissionGrant, RpcError, StreamStart } from './protocol'; export interface BridgeOptions { /** Per-running-instance id stamped on outbound requests. Generated if omitted. */ instanceId?: string; /** Per-request timeout in milliseconds. Default 30000. */ timeoutMs?: number; } /** Inbound message types a consumer can subscribe to via `on()`. */ declare type InboundType = typeof MessageType.HandshakeInit | typeof MessageType.RpcRequest | typeof MessageType.StreamStart | typeof MessageType.StreamStop | typeof MessageType.PermissionRequest | typeof MessageType.Event; /** A bridge-level error carrying the protocol error `code`. */ export declare class BridgeError extends Error { readonly code: string; constructor(error: RpcError); } /** Mint a per-running-instance id (uses `crypto.randomUUID` when available). */ export declare function generateInstanceId(): string; export declare class Bridge { /** Per-running-instance id stamped on every outbound request. */ readonly instanceId: string; private readonly channel; private readonly timeoutMs; private readonly pending; private readonly streams; private readonly listeners; private handshakePending; private nextId; private offMessage; private disposed; constructor(channel: Channel, options?: BridgeOptions); /** Open a session: send `handshake:init`, resolve with the host's `handshake:ack`. */ handshake(payload: { appId: string; sdkVersion: string; }): Promise; /** Invoke a host capability; resolves with its return value. */ request(capability: string, args?: unknown[]): Promise; /** Proactively ask the host to prompt for one or more gated capabilities. */ requestPermission(capabilities: string[]): Promise; /** * Start a host-backed stream over the bridge. The returned function closes * the stream locally and asks the host to release its subscription. */ stream(capability: string, args: unknown[] | undefined, onEvent: (data: unknown) => void): () => void; /** Subscribe to an inbound message type. Returns an unsubscribe function. */ on(type: T, handler: (message: Extract) => void): () => void; /** Low-level passthrough — used by the host to send responses / events. */ send(message: ApnaMessage): void; /** Host helper: subscribe to client stream starts. */ onStreamStart(handler: (message: StreamStart) => void): () => void; /** Host helper: emit one payload for an active client stream. */ emitStreamEvent(stream: Pick, payload: unknown): void; /** Host helper: stop an active client stream, optionally with an error. */ stopStream(stream: Pick, error?: RpcError): void; /** Reject all pending promises, drop listeners, and detach the channel. */ dispose(): void; private readonly route; private settle; private settleHandshake; private deliverStreamEvent; private handleStreamStop; private dispatch; } export {};