/**
 * SSE (Server-Sent Events) utilities for metered streaming payments.
 *
 * Provides event formatting/parsing, balance polling, the core
 * `serve()` loop that meters an async iterable into a ReadableStream
 * of SSE events, and helpers (`toResponse`, `fromRequest`) for
 * building HTTP responses from the stream.
 */
import type { Hex } from 'viem';
import * as ChannelStore from './ChannelStore.js';
import type { NeedVoucherEvent, SessionReceipt } from './Types.js';
/**
 * Format a session receipt as a Server-Sent Event.
 *
 * Produces a valid SSE event string with `event: payment-receipt`
 * and the receipt JSON as the `data` field.
 */
export declare function formatReceiptEvent(receipt: SessionReceipt): string;
/**
 * Format a need-voucher event as a Server-Sent Event.
 *
 * Emitted when the channel balance is exhausted mid-stream.
 * The client responds by sending a new voucher credential to
 * any mppx-protected endpoint.
 */
export declare function formatNeedVoucherEvent(params: NeedVoucherEvent): string;
/**
 * Format an application message as SSE, preserving embedded newlines.
 *
 * SSE requires multi-line payloads to be emitted as separate `data:` fields.
 */
export declare function formatMessageEvent(value: string): string;
/**
 * Parsed SSE event (discriminated union by `type`).
 */
export type SseEvent = {
    type: 'message';
    data: string;
} | {
    type: 'payment-need-voucher';
    data: NeedVoucherEvent;
} | {
    type: 'payment-receipt';
    data: SessionReceipt;
};
/**
 * Parse a raw SSE event string into a typed event.
 *
 * Handles the three event types used by mppx streaming:
 * - `message` (default / no event field) — application data
 * - `payment-need-voucher` — balance exhausted, client should send voucher
 * - `payment-receipt` — final receipt
 */
export declare function parseEvent(raw: string): SseEvent | null;
export type SessionController = {
    /**
     * Reserve voucher coverage for the next emitted chunk.
     *
     * The reservation blocks until sufficient voucher headroom exists, but the
     * charge is only committed once a chunk is actually emitted. If the stream
     * ends or aborts before that emission, the reservation is dropped.
     */
    charge(): Promise<void>;
};
/**
 * Wrap an async iterable with payment metering, producing an SSE stream.
 *
 * `generate` may be either:
 * - An `AsyncIterable<string>` — each yielded value is automatically charged
 *   (one `tickCost` per value).
 * - A callback `(stream: SessionController) => AsyncIterable<string>` — the
 *   generator controls when charges happen by calling `stream.charge()`.
 *
 * For each emitted value the stream:
 * 1. Reserves `tickCost` from the channel's available voucher headroom
 *    (auto or manual).
 * 2. If balance is sufficient, emits `event: message` with the value.
 * 3. If balance is exhausted, emits `event: payment-need-voucher`
 *    and polls store until the client tops up the channel.
 * 4. Commits the reserved charge immediately before the chunk is emitted.
 * 5. On generator completion, emits a final `event: payment-receipt`.
 *
 * Returns a `ReadableStream<Uint8Array>` suitable for use as an HTTP response body.
 */
export declare function serve(options: serve.Options): ReadableStream<Uint8Array>;
export declare namespace serve {
    type Options = {
        store: ChannelStore.ChannelStore;
        channelId: Hex;
        challengeId: string;
        tickCost: bigint;
        generate: AsyncIterable<string> | ((stream: SessionController) => AsyncIterable<string>);
        pollIntervalMs?: number | undefined;
        prepaidUnits?: number | undefined;
        signal?: AbortSignal | undefined;
    };
}
/**
 * Wrap a `ReadableStream<Uint8Array>` (from {@link serve}) in an HTTP
 * `Response` with the correct SSE headers.
 */
export declare function toResponse(body: ReadableStream<Uint8Array>): Response;
/**
 * Extract `channelId`, `challengeId`, and `tickCost` from a `Request`'s
 * `Authorization: Payment …` header.
 *
 * This is a convenience for callers that receive a raw `Request` and need
 * the parameters required by {@link serve}.
 */
export declare function fromRequest(request: Request): fromRequest.Context;
export declare namespace fromRequest {
    type Context = {
        challengeId: string;
        channelId: Hex;
        tickCost: bigint;
    };
}
/**
 * Check whether a `Response` carries an SSE event stream.
 *
 * Returns `true` when the `Content-Type` header starts with
 * `text/event-stream` (case-insensitive, ignoring charset params).
 */
export declare function isEventStream(response: Response): boolean;
/**
 * Parse an SSE `Response` body into an async iterable of `data:` payloads.
 *
 * Yields the raw `data:` field content for each SSE event in the stream.
 * Events whose data matches the `skip` predicate are silently dropped
 * (e.g. `[DONE]` sentinels used by OpenAI-compatible APIs).
 *
 * Each yielded value typically becomes one charge tick when fed to
 * {@link serve} via the SSE transport's auto-charge mode.
 *
 * @example
 * ```ts
 * const upstream = await fetch('https://api.example.com/stream')
 * for await (const data of Sse.iterateData(upstream)) {
 *   console.log(data)
 * }
 * ```
 */
export declare function iterateData(response: Response, options?: iterateData.Options): AsyncGenerator<string>;
export declare namespace iterateData {
    type Options = {
        /** Predicate to skip specific data payloads (e.g. `d => d === '[DONE]'`). */
        skip?: ((data: string) => boolean) | undefined;
    };
}
//# sourceMappingURL=Sse.d.ts.map