import { A as A2AError, e as RestHttpStatusCode } from './error-CquguH6H.js'; import { L as Logger, b as LogContext } from './logger-DM9C11Ou.js'; /** * Shared Server-Sent Events (SSE) utilities for both JSON-RPC and REST transports. * This module provides common SSE formatting functions and headers. */ /** * Standard HTTP headers for Server-Sent Events (SSE) streaming responses. * These headers ensure proper SSE behavior across different proxies and clients. */ declare const SSE_HEADERS: { readonly 'Content-Type': "text/event-stream"; readonly 'Cache-Control': "no-cache"; readonly Connection: "keep-alive"; readonly 'X-Accel-Buffering': "no"; }; /** * Formats a data event for Server-Sent Events (SSE) protocol. * Creates a standard SSE event with an ID and JSON-stringified data. * * @param event - The event data to send (will be JSON stringified) * @returns Formatted SSE event string following the SSE specification * * @example * ```ts * formatSSEEvent({ kind: 'message', text: 'Hello' }) * // Returns: "data: {\"kind\":\"message\",\"text\":\"Hello\"}\n\n" * * formatSSEEvent({ result: 'success' }, 'custom-id') * // Returns: "data: {\"result\":\"success\"}\n\n" * ``` */ declare function formatSSEEvent(event: unknown): string; /** * Formats an error event for Server-Sent Events (SSE) protocol. * Error events use the "error" event type to distinguish them from data events, * allowing clients to handle errors differently. * * @param error - The error object (will be JSON stringified) * @returns Formatted SSE error event string with custom event type * * @example * ```ts * formatSSEErrorEvent({ code: -32603, message: 'Internal error' }) * // Returns: "event: error\ndata: {\"code\":-32603,\"message\":\"Internal error\"}\n\n" * ``` */ declare function formatSSEErrorEvent(error: unknown): string; /** * Structured SSE event for frameworks that need it (like Hono's streamSSE). * This matches Hono's SSEMessage interface. */ interface SSEEventData { /** Event ID (optional, typically a timestamp) */ id?: string; /** Event type (optional, 'error' for errors) */ event?: string; /** JSON-stringified data */ data: string; /** Retry interval in milliseconds (optional) */ retry?: number; } /** * Creates a structured SSE event object. * Use this for frameworks like Hono that need structured event data. * * @param data - The data to include in the event * @param includeId - Whether to include a timestamp ID (default: true) * @returns Structured SSE event object */ declare function createSSEEventData(data: unknown, includeId?: boolean): SSEEventData; /** * Creates a structured SSE error event object. * Use this for frameworks like Hono that need structured event data. * * @param error - The error data to include * @param includeId - Whether to include a timestamp ID (default: true) * @returns Structured SSE error event object */ declare function createSSEErrorEventData(error: unknown, includeId?: boolean): SSEEventData; /** * Converts a structured SSE event to a string for raw writing. * Use this when you need to write SSE events as raw strings. * * @param event - The structured SSE event * @returns Formatted SSE string */ declare function sseEventToString(event: SSEEventData): string; /** * Streaming Utilities for A2A * * Provides portable streaming abstractions that work across different runtimes. * The core abstraction is the StreamConsumer which can be implemented for * different output targets (Express res.write, Web ReadableStream, Hono streamSSE, etc.) * * The interface supports both sync and async writes to enable backpressure handling * in frameworks that support it (like Hono's streamSSE). */ /** * HTTP error response body format. */ interface HTTPError { readonly code: number; readonly message: string; readonly data?: Record; } /** * Interface for consuming SSE events. * Implemented by different adapters for their specific output mechanism. * * The write method can return a Promise to support backpressure handling. * When write returns a Promise, the stream processor will await it before * continuing, allowing frameworks like Hono to apply backpressure. * * @example * ```ts * // Sync consumer (Express, basic ReadableStream) * const consumer: StreamConsumer = { * write: (event) => res.write(formatEvent(event)), * end: () => res.end(), * isWritable: () => !res.writableEnded, * }; * * // Async consumer with backpressure (Hono streamSSE) * const consumer: StreamConsumer = { * write: async (event) => await sseStream.writeSSE(event), * end: () => {}, // Hono handles stream end * isWritable: () => !aborted, * }; * ``` */ interface StreamConsumer { /** * Write an SSE event to the output. * Can return a Promise to support backpressure - the processor will await it. */ write(event: SSEEventData): void | Promise; /** Signal the end of the stream */ end(): void; /** Check if the stream is still writable */ isWritable(): boolean; } /** * Result when stream processing succeeds (no early error). */ interface StreamSuccessResult { /** No early error occurred */ readonly earlyError: false; } /** * Result when an early error occurs before streaming starts. */ interface StreamEarlyErrorResult { /** An early error occurred */ readonly earlyError: true; /** The A2A error */ readonly error: A2AError; /** HTTP status code for the error response */ readonly statusCode: RestHttpStatusCode; /** Error body for the HTTP response */ readonly errorBody: HTTPError; } /** * Result of stream processing. * Discriminated union based on earlyError. */ type StreamResult = StreamSuccessResult | StreamEarlyErrorResult; /** * Options for processing a stream. */ interface ProcessStreamOptions { /** Logger for error reporting */ logger: Logger; /** Log context for error messages */ logContext: LogContext; /** Whether to include event IDs (default: true) */ includeIds?: boolean; /** Callback called after first event succeeds, before streaming continues */ onStreamStart?: () => void; } /** * Processes an async generator stream, handling errors appropriately. * Returns early error info if the first event fails, otherwise streams events. * * This is the core streaming logic that can be used by any adapter. * It supports both sync and async consumers - if write() returns a Promise, * it will be awaited to enable backpressure handling. * * @param stream - The async generator to process * @param consumer - The output consumer (Express res, ReadableStream controller, Hono streamSSE, etc.) * @param options - Processing options * @returns StreamResult indicating success or early error */ declare function processStream(stream: AsyncGenerator, consumer: StreamConsumer, options: ProcessStreamOptions): Promise; /** * Creates an SSE data event. */ declare function createSSEEvent(data: unknown, includeId?: boolean): SSEEventData; /** * Creates an SSE error event. */ declare function createSSEErrorEvent(error: unknown, includeId?: boolean): SSEEventData; /** * Creates a StreamConsumer for Express Response. * This allows the core streaming logic to work with Express. */ declare function createExpressStreamConsumer(res: { write(chunk: string): boolean; end(): void; writableEnded: boolean; }, formatEvent?: (event: SSEEventData) => string): StreamConsumer; /** * Creates a StreamConsumer for ReadableStream controller. * This is a sync consumer - no backpressure support. */ declare function createWebStreamConsumer(controller: ReadableStreamDefaultController, encoder?: TextEncoder): StreamConsumer; export { type HTTPError as H, type ProcessStreamOptions as P, type SSEEventData as S, createSSEErrorEventData as a, SSE_HEADERS as b, createSSEEventData as c, formatSSEErrorEvent as d, type StreamConsumer as e, formatSSEEvent as f, type StreamResult as g, type StreamSuccessResult as h, type StreamEarlyErrorResult as i, createSSEEvent as j, createSSEErrorEvent as k, createExpressStreamConsumer as l, createWebStreamConsumer as m, processStream as p, sseEventToString as s };