/** * db4ai CapnWeb RPC Transport Layer * * Provides CapnWeb-based RPC transport for AI operations with * serialization, request correlation, batching, streaming, and error handling. * * @packageDocumentation */ /** * CapnWeb message type codes. */ export declare const MESSAGE_TYPE_CODES: { readonly request: 1; readonly response: 2; readonly batch: 3; readonly stream: 4; readonly ping: 5; readonly pong: 6; readonly subscribe: 7; readonly unsubscribe: 8; readonly error: 9; }; /** * CapnWeb protocol magic bytes ('CAPN'). */ export declare const CAPNWEB_MAGIC = 1128353870; /** * CapnWeb protocol version. */ export declare const CAPNWEB_VERSION = 1; /** * CapnWeb header size in bytes. */ export declare const HEADER_SIZE = 8; /** * Maximum message size (10 MB). */ export declare const MAX_MESSAGE_SIZE: number; /** * Error codes for RPC errors. */ export declare const ERROR_CODES: { readonly INTERNAL_ERROR: "INTERNAL_ERROR"; readonly METHOD_NOT_FOUND: "METHOD_NOT_FOUND"; readonly INVALID_REQUEST: "INVALID_REQUEST"; readonly TIMEOUT: "TIMEOUT"; readonly CONNECTION_LOST: "CONNECTION_LOST"; readonly SERIALIZATION_ERROR: "SERIALIZATION_ERROR"; readonly UNAUTHORIZED: "UNAUTHORIZED"; readonly RATE_LIMITED: "RATE_LIMITED"; readonly PARSE_ERROR: "PARSE_ERROR"; readonly NETWORK_ERROR: "NETWORK_ERROR"; }; export type ErrorCode = (typeof ERROR_CODES)[keyof typeof ERROR_CODES]; /** * RPC request message. */ export interface RPCRequest { type: 'request'; id: string; path: string[]; args: unknown[]; meta?: Record; timestamp: number; } /** * RPC response message. */ export interface RPCResponse { type: 'response'; id: string; success: boolean; result?: unknown; error?: RPCErrorInfo; timestamp: number; } /** * RPC error information. */ export interface RPCErrorInfo { code: string; message: string; data?: unknown; stack?: string; } /** * RPC batch request. */ export interface RPCBatchRequest { type: 'batch'; requests: RPCRequest[]; timestamp: number; } /** * RPC batch response. */ export interface RPCBatchResponse { type: 'batch'; responses: RPCResponse[]; timestamp: number; } /** * RPC stream chunk. */ export interface RPCStreamChunk { type: 'stream'; id: string; chunk: unknown; index: number; done: boolean; timestamp: number; } /** * Any RPC message type. */ export type RPCMessage = RPCRequest | RPCResponse | RPCBatchRequest | RPCBatchResponse | RPCStreamChunk; /** * Transport options. */ export interface TransportOptions { /** Base URL for the transport */ url: string; /** Request timeout in milliseconds */ timeout?: number; /** Custom headers */ headers?: Record; /** Maximum retries for transient errors */ maxRetries?: number; /** Retry delay in milliseconds */ retryDelay?: number; /** Retry backoff multiplier */ retryBackoff?: number; } /** * Batch options. */ export interface BatchOptions { /** Maximum batch size */ maxSize?: number; /** Batch collection window in milliseconds */ window?: number; } /** * Stream handler function. */ export type StreamHandler = (chunk: T, index: number, done: boolean) => void; /** * Backpressure handler function. */ export type BackpressureHandler = (paused: boolean) => void; /** * CapnWeb binary serializer for RPC messages. */ export declare class CapnWebSerializer { private textEncoder; private textDecoder; /** * Serialize an RPC message to CapnWeb binary format. */ serialize(message: RPCMessage): ArrayBuffer; /** * Deserialize a CapnWeb binary message to an RPC message. */ deserialize(data: ArrayBuffer): RPCMessage; /** * Check if data is in CapnWeb format. */ isCapnWebFormat(data: ArrayBuffer): boolean; /** * Get the content type for CapnWeb format. */ get contentType(): string; } /** * JSON serializer for RPC messages. */ export declare class JsonSerializer { private textEncoder; private textDecoder; /** * Serialize an RPC message to JSON. */ serialize(message: RPCMessage): ArrayBuffer; /** * Deserialize a JSON message to an RPC message. */ deserialize(data: ArrayBuffer): RPCMessage; /** * Get the content type for JSON format. */ get contentType(): string; } /** * RPC error with code and optional data. */ export declare class RPCError extends Error { readonly code: string; readonly data?: unknown; readonly retryable: boolean; constructor(code: string, message: string, data?: unknown); private isRetryableCode; static fromResponse(response: RPCResponse): RPCError; } /** * Manages request/response correlation. */ export declare class RequestCorrelator { private pending; private requestCounter; private readonly timeout; constructor(timeout?: number); /** * Generate a unique request ID. */ generateId(): string; /** * Track a pending request. */ track(id: string): Promise; /** * Resolve a pending request with a response. */ resolve(id: string, response: RPCResponse): boolean; /** * Reject a pending request with an error. */ reject(id: string, error: Error): boolean; /** * Get the number of pending requests. */ get pendingCount(): number; /** * Check if a request is pending. */ isPending(id: string): boolean; /** * Cancel all pending requests. */ cancelAll(error?: Error): void; } /** * Batches multiple requests into a single batch request. */ export declare class RequestBatcher { private batch; private batchTimer; private readonly maxSize; private readonly window; private readonly onFlush; constructor(onFlush: (batch: RPCBatchRequest) => Promise, options?: BatchOptions); /** * Add a request to the batch. */ add(request: RPCRequest): Promise; /** * Flush the current batch. */ flush(): Promise; /** * Get the current batch size. */ get size(): number; } /** * Manages streaming responses. */ export declare class StreamManager { private streams; private readonly highWaterMark; constructor(highWaterMark?: number); /** * Register a stream handler. */ register(id: string, handler: StreamHandler, backpressureHandler?: BackpressureHandler): void; /** * Handle a stream chunk. */ handleChunk(chunk: RPCStreamChunk): boolean; /** * Resume a paused stream. */ resume(id: string): void; /** * Cancel a stream. */ cancel(id: string): void; /** * Check if a stream is active. */ isActive(id: string): boolean; /** * Check if a stream is paused. */ isPaused(id: string): boolean; } /** * CapnWeb RPC transport for AI operations. */ export declare class CapnWebTransport { private readonly url; private readonly timeout; private readonly headers; private readonly maxRetries; private readonly retryDelay; private readonly retryBackoff; private readonly serializer; private readonly correlator; private readonly streamManager; private batcher; constructor(options: TransportOptions); /** * Enable request batching. */ enableBatching(options?: BatchOptions): void; /** * Disable request batching. */ disableBatching(): void; /** * Send an RPC request. */ call(path: string[], args: unknown[], meta?: Record): Promise; /** * Send a streaming request. */ callStream(path: string[], args: unknown[], handler: StreamHandler, backpressureHandler?: BackpressureHandler): Promise; /** * Send a single request with retry logic. */ private sendRequest; /** * Send a batch request. */ private sendBatch; /** * Perform the actual fetch request. */ private doFetch; /** * Delay helper. */ private delay; /** * Get the serializer. */ get Serializer(): CapnWebSerializer; } /** * Create a new CapnWeb transport. */ export declare function createCapnWebTransport(options: TransportOptions): CapnWebTransport; /** * Create a new request correlator. */ export declare function createRequestCorrelator(timeout?: number): RequestCorrelator; /** * Create a new request batcher. */ export declare function createRequestBatcher(onFlush: (batch: RPCBatchRequest) => Promise, options?: BatchOptions): RequestBatcher; /** * Create a new stream manager. */ export declare function createStreamManager(highWaterMark?: number): StreamManager; /** * Create a new CapnWeb serializer. */ export declare function createCapnWebSerializer(): CapnWebSerializer; /** * Create a new JSON serializer. */ export declare function createJsonSerializer(): JsonSerializer; //# sourceMappingURL=rpc.d.ts.map