/** * Which provider runtime a request is being sent to. * `mistral` and `moonshotai` use the OpenAI-compatible wire format and are * therefore treated as "openai" for error classification purposes; they are * listed here so call sites can pass accurate labels without a cast. */ export type ProviderKind = "anthropic" | "openai" | "google" | "mistral" | "moonshotai"; /** Default deadline for one stream attempt to return response headers. */ export declare const DEFAULT_PROVIDER_STREAM_HEADERS_TIMEOUT_MS = 30000; /** * Ceiling on the wall time replays may spend waiting for stream response * headers. * * The budget bounds the total only when it is at least as large as the * per-attempt deadline, which the defaults guarantee (40s against 30s). The * first attempt always keeps its configured deadline, so a caller that raises * `headersTimeoutMs` above the budget gets that longer first attempt and the * effective ceiling becomes `max(headersTimeoutMs, totalHeadersBudgetMs)`. * Shortening the first attempt instead would sacrifice a provider that was * going to answer, for a replay that may never fire. */ export declare const DEFAULT_PROVIDER_STREAM_TOTAL_HEADERS_BUDGET_MS = 40000; /** * Base class for typed provider errors. The `retryable` flag is the * primary signal for callers (or a retry wrapper) to decide whether to * re-issue the request. `retryAfterMs` is set when the provider gave an * explicit delay hint (Retry-After header, Retry-Info trailer). */ export declare class ProviderError extends Error { readonly provider: ProviderKind; readonly status: number; readonly retryable: boolean; readonly retryAfterMs?: number; /** * Bounded structured provider response used by the internal error classifier. * Kept non-enumerable so logs and JSON serialization retain the generic error. */ readonly responseBody?: string; constructor(options: { provider: ProviderKind; status: number; message: string; retryable: boolean; retryAfterMs?: number; }); } /** Provider reports it is overloaded (Anthropic 529, OpenAI/Google 503). */ export declare class ProviderOverloadedError extends ProviderError { } /** Provider is rate limiting this API key (OpenAI/Google 429 with Retry-After). */ export declare class ProviderRateLimitError extends ProviderError { } /** Provider account quota is exhausted — non-retryable. */ export declare class ProviderQuotaError extends ProviderError { } /** Non-retryable 4xx/5xx that doesn't fit another bucket. */ export declare class ProviderRequestError extends ProviderError { } /** Parses retry after ms. */ export declare function parseRetryAfterMs(header: string | null): number | undefined; /** * Inspect a non-2xx response and build the most specific ProviderError * subclass we can. Reads the response body as text (it's already dead * on the wire by this point). Body classification handles the cases * where HTTP status alone is ambiguous — notably OpenAI * `insufficient_quota` vs `rate_limit_exceeded` both arriving as 429. */ export declare function buildProviderError(provider: ProviderKind, response: Response, abortSignal?: AbortSignal): Promise; /** * Wait out a retry delay, rejecting the moment the caller cancels. Shared with * providers that must replay a request the SSE body failed, so every retry * path honors cancellation the same way. */ export declare function waitForProviderStreamRetry(delayMs: number, abortSignal: AbortSignal): Promise; /** * Request and parse a bounded JSON response. * * The request has a five-minute default deadline and a 32 MiB default body * limit. Provider HTTP errors, timeouts, malformed JSON, and oversized bodies * reject with a contextual `ProviderError` without exposing response payloads. */ export declare function requestJson(options: { url: string; fetchImpl: typeof globalThis.fetch; init: RequestInit; providerLabel: string; providerKind: ProviderKind; /** Model this request is for. Reported when a deadline elapses. */ modelId?: string; timeoutMs?: number; maxResponseBytes?: number; }): Promise; /** * Request a streaming response. When the request body is replayable, * typed retryable failures are retried up to two times before provider output * is exposed. Each attempt gets a fresh stream header deadline, and replays are * capped so the whole header wait stays inside one shared budget. * ReadableStream request bodies are not retried because fetch can * consume them on the first attempt. * * Response headers and error bodies have a 30-second default per-attempt * deadline, and replays are additionally capped so the whole header wait stays * inside a 40-second default budget. The first attempt always runs on the full * per-attempt deadline; only replays are shortened to fit the budget. After * headers arrive, caller cancellation remains connected to the returned body; * consumer cancellation aborts the request and cancels the upstream body. */ export declare function requestStream(options: { url: string; fetchImpl: typeof globalThis.fetch; init: RequestInit; providerLabel: string; providerKind: ProviderKind; /** Model this request is for. Reported when a deadline elapses. */ modelId?: string; headersTimeoutMs?: number; /** * Ceiling on the wall time replays may spend waiting for headers. Defaults to * 40 seconds. Set it at or above `headersTimeoutMs` for it to bound the total, * because the first attempt always keeps its own deadline. */ totalHeadersBudgetMs?: number; }): Promise>; //# sourceMappingURL=provider-http.d.ts.map