import { LanguageModelV3 } from "@ai-sdk/provider"; import { FetchFunction } from "@ai-sdk/provider-utils"; //#region ../gateway-core/dist/index.d.mts //#endregion //#region src/resumable-stream.d.ts type ResumeExpiredPolicy = "error" | "accept-partial"; interface ResumableStreamOptions { /** Cloudflare AI binding (e.g. `env.AI`) — used for the resume fetch. */ binding: Ai; /** Gateway id the run was issued under. */ gateway: string; /** The `cf-aig-run-id` of the run to resume. */ runId: string; /** * Initial run-path response body. Omit for **cross-invocation re-attach**: the * stream then starts by fetching `resume?from={fromEvent}` directly (e.g. a new * Durable Object invocation re-attaching to a run after eviction). */ initial?: ReadableStream; /** * SSE event index to (re-)attach from. Defaults to `0`. Used as the starting * `from` when `initial` is omitted, and as the base offset for the event * counter (so a later reconnect resumes from the correct absolute index). */ fromEvent?: number; /** What to do when the resume buffer has expired (404). Defaults to `"error"`. */ onResumeExpired?: ResumeExpiredPolicy; /** Max reconnect attempts before giving up. Defaults to 5. */ maxReconnects?: number; /** Fired before each reconnect with the resume `from` index and attempt number. */ onReconnect?: (fromEvent: number, attempt: number) => void; /** * Fired with the cumulative SSE event offset whenever complete events are * emitted. Use it to persist `{ runId, eventOffset }` for cross-invocation * re-attach (throttle your own writes — this can fire per chunk). */ onProgress?: (eventOffset: number) => void; /** * Abort signal for the consuming request. When it aborts (or the downstream * consumer cancels the wrapped stream), the engine stops **without** * reconnecting — an intentional cancel must never trigger a resume reconnect. * The signal is also forwarded to the resume fetch. */ signal?: AbortSignal; } //#endregion //#region src/index.d.ts declare class AiGatewayInternalFetchError extends Error {} declare class AiGatewayDoesNotExist extends Error {} declare class AiGatewayUnauthorizedError extends Error {} type InternalLanguageModelV3 = LanguageModelV3 & { config?: { fetch?: FetchFunction | undefined; }; }; declare class AiGatewayChatLanguageModel implements LanguageModelV3 { readonly specificationVersion = "v3"; readonly defaultObjectGenerationMode = "json"; readonly supportedUrls: Record | PromiseLike>; readonly models: InternalLanguageModelV3[]; readonly config: AiGatewaySettings; get modelId(): string; get provider(): string; constructor(models: LanguageModelV3[], config: AiGatewaySettings); processModelRequest(options: Parameters[0], modelMethod: "doStream" | "doGenerate"): Promise>>; doStream(options: Parameters[0]): Promise>>; doGenerate(options: Parameters[0]): Promise>>; } /** * AI Gateway provider for the Vercel AI SDK. Wraps one or more `@ai-sdk/*` * language models and routes their requests through Cloudflare's AI Gateway * universal endpoint, with cross-vendor server-side fallback (the first model * that succeeds wins, selected via `cf-aig-step`). */ interface AiGateway { (models: LanguageModelV3 | LanguageModelV3[]): LanguageModelV3; chat(models: LanguageModelV3 | LanguageModelV3[]): LanguageModelV3; } type AiGatewayRetries = { maxAttempts?: 1 | 2 | 3 | 4 | 5; retryDelayMs?: number; backoff?: "constant" | "linear" | "exponential"; }; /** @deprecated Misspelling — use {@link AiGatewayRetries} instead. */ type AiGatewayReties = AiGatewayRetries; type AiGatewayOptions = { cacheKey?: string; cacheTtl?: number; skipCache?: boolean; metadata?: Record; collectLog?: boolean; eventId?: string; requestTimeoutMs?: number; retries?: AiGatewayRetries; /** * BYOK stored-key alias to authenticate with → `cf-aig-byok-alias`. Selects a * non-`default` key configured for the provider on the gateway. */ byokAlias?: string; /** * Per-request Zero Data Retention override (Unified Billing only) → * `cf-aig-zdr`. `true` forces ZDR-capable upstreams; `false` disables it for * this request. */ zdr?: boolean; }; type AiGatewayAPISettings = { gateway: string; accountId: string; apiKey?: string; options?: AiGatewayOptions; }; /** * Opt-in resumable streaming for the binding (run) path. Supply the full * Cloudflare AI binding (`env.AI`) plus the gateway id; when a streaming run * surfaces a `cf-aig-run-id`, transient mid-stream drops reconnect transparently * via the gateway resume endpoint. The reconnect uses `binding.fetch(...)`, so * this must be the AI binding itself — not the `env.AI.gateway()` * sub-binding passed as `binding` above. */ type AiGatewayResumeSettings = { /** Full Cloudflare AI binding (`env.AI`) used for the resume reconnect fetch. */binding: ResumableStreamOptions["binding"]; /** Gateway id the run was issued under (the `env.AI.gateway()` name). */ gateway: string; /** What to do when the resume buffer has expired (404). Defaults to `"error"`. */ onResumeExpired?: ResumeExpiredPolicy; /** Max reconnect attempts before giving up. Defaults to 5. */ maxReconnects?: number; }; type AiGatewayBindingSettings = { binding: { run(data: unknown, options?: { signal?: AbortSignal; }): Promise; }; options?: AiGatewayOptions; /** * Opt-in resumable streaming. No-op on the REST/API-key path (which has no * `cf-aig-run-id`). See {@link AiGatewayResumeSettings}. */ resume?: AiGatewayResumeSettings; }; type AiGatewaySettings = AiGatewayAPISettings | AiGatewayBindingSettings; declare function createAiGateway(options: AiGatewaySettings): AiGateway; /** * Translate {@link AiGatewayOptions} into the `cf-aig-*` request headers the AI * Gateway universal endpoint understands. Delegates to the shared * `@cloudflare/gateway-core` header builder so the header names and semantics * stay identical to the `workers-ai-provider` delegate (e.g. the current * `cf-aig-cache-ttl` / `cf-aig-skip-cache` names rather than the deprecated * `cf-cache-ttl` / `cf-skip-cache`). */ declare function parseAiGatewayOptions(options: AiGatewayOptions): Headers; //#endregion export { AiGateway, AiGatewayAPISettings, AiGatewayBindingSettings, AiGatewayChatLanguageModel, AiGatewayDoesNotExist, AiGatewayInternalFetchError, AiGatewayOptions, AiGatewayResumeSettings, AiGatewayReties, AiGatewayRetries, AiGatewaySettings, AiGatewayUnauthorizedError, createAiGateway, parseAiGatewayOptions }; //# sourceMappingURL=index.d.mts.map