/** * JSON-POST → SSE transport for OpenAI-wire streaming endpoints (chat * completions, responses, azure responses). Replaces the `openai` SDK client: * * - Retries: `fetchWithRetry` (Retry-After/quota-hint aware; 5xx/408/429 and * transient network errors). Default 6 total attempts — parity with the * SDK's former `maxRetries: 5`. * - SSE decode: `readSseJson` (spec-compliant framing, `[DONE]`-aware). * `onSseEvent` observers now receive real wire frames instead of events * re-synthesized from decoded SDK objects. * - Errors: {@link OpenAIHttpError} exposes `status`/`headers`/`code` * structurally (ProviderHttpError contract — `extractHttpStatusFromError`, * retry-after extraction, copilot transient classification) and carries the * captured response body for the strict-tools fallback and the responses * chain-state detectors, which regex over `error.message`. */ import { type SseEventObserver } from "@oh-my-pi/pi-utils"; import * as AIError from "../error"; import { OpenAIHttpError } from "../error"; export { OpenAIHttpError }; import type { FetchImpl } from "../types"; export interface OpenAIStreamRequestInit { url: string; headers: Record; /** JSON request body; serialized once per call (retries resend the same bytes). */ body: unknown; signal: AbortSignal; fetch?: FetchImpl; /** * Total attempts (initial + retries). Defaults to {@link DEFAULT_MAX_ATTEMPTS}. * Pass `1` when a first-event watchdog is armed so retries cannot silently * extend the caller's deadline (mirrors the old `maxRetries: 0` hint). */ maxAttempts?: number; /** Raw wire-frame observer (`onSseEvent` debug pipeline). */ onSseEvent?: SseEventObserver; } export interface OpenAIStreamHandle { /** Decoded `data:` payloads; terminates on `[DONE]` or stream end. */ events: AsyncGenerator; response: Response; /** `x-request-id` response header (the SDK's former `request_id`). */ requestId: string | null; } /** * POST a JSON body and stream back decoded SSE events. * * Throws {@link OpenAIHttpError} on a non-2xx terminal response. Aborts on * `signal` propagate from `fetchWithRetry`/`readSseJson`; callers own the * watchdog timers and abort-reason bookkeeping. */ export declare function postOpenAIStream(init: OpenAIStreamRequestInit): Promise>; /** Decode a non-2xx response into an {@link OpenAIHttpError} without consuming it twice. */ export declare function captureOpenAIHttpError(response: Response): Promise;