/** * 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/index.js"; import { OpenAIHttpError } from "../error/index.js"; export { OpenAIHttpError }; import type { FetchImpl } from "../types.js"; 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; /** Optional caller-specific gate composed with shared transport retry exclusions. */ shouldRetryResponse?: (response: Response, bodyText: string) => boolean | Promise; /** * Raw wire-frame observer (`onSseEvent` debug pipeline). Leave it unset * when no diagnostic listener exists: any observer turns on per-line raw * capture for every frame. */ onSseEvent?: SseEventObserver; /** Called when the stream ends on the OpenAI `[DONE]` sentinel; independent of {@link onSseEvent}. */ onDoneSentinel?: () => void; } 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;