UNPKG

workers-ai-provider

Version:

Workers AI Provider for the vercel AI SDK

80 lines 3.01 kB
//#region src/gateway-provider.d.ts /** * Bring-your-own-provider: route any `@ai-sdk/*` provider's HTTP traffic through * Cloudflare AI Gateway, without the catalog slug delegate. The provider keeps * its own request/response shaping; this only swaps the transport. * * Use it for providers the slug delegate cannot auto-wire (bedrock, replicate, * audio/image providers, anything provider-native), or when you want full control * of the underlying `@ai-sdk` provider. This is the gateway path only — BYOK and * caching are available, resume (`cf-aig-run-id`) is not. * * @example * ```ts * import { createOpenAI } from "@ai-sdk/openai"; * import { createGatewayFetch } from "workers-ai-provider/gateway"; * * const openai = createOpenAI({ * apiKey: env.OPENAI_API_KEY, // forwarded when byok: true * fetch: createGatewayFetch({ binding: env.AI, gateway: "my-gw", byok: true }), * }); * const model = openai("gpt-5"); * ``` */ interface GatewayFetchConfig { /** A Cloudflare AI binding (e.g. `env.AI`). */ binding: Ai; /** Gateway id (or options). */ gateway: GatewayOptions | string; /** * Force a gateway provider id instead of detecting it from the request URL. * Required when the wrapped provider's host is not in the registry. */ provider?: string; /** * Forward the upstream provider key (Authorization / x-api-key / …) instead of * stripping it. Required for BYOK providers. Defaults to `false` (strip, so * unified billing / the gateway's stored key applies). */ byok?: boolean; /** Extra headers added to every gateway entry. */ extraHeaders?: Record<string, string>; /** Gateway-path response caching (seconds). */ cacheTtl?: number; /** Bypass gateway cache. */ skipCache?: boolean; } /** * A `fetch` that dispatches the wrapped provider's request through AI Gateway. * Detects the gateway provider id from the request URL (or uses `config.provider`), * strips the provider host to the endpoint path, and forwards the body verbatim. */ declare function createGatewayFetch(config: GatewayFetchConfig): typeof globalThis.fetch; /** * Wrap an `@ai-sdk/*` provider factory so its traffic flows through AI Gateway. * A thin convenience over {@link createGatewayFetch} — it injects the gateway * `fetch` (and a placeholder `apiKey` unless you supply one for BYOK). * * @example * ```ts * import { createOpenAI } from "@ai-sdk/openai"; * import { createGatewayProvider } from "workers-ai-provider/gateway"; * * const openai = createGatewayProvider(createOpenAI, { * binding: env.AI, * gateway: "my-gw", * }); * const model = openai("gpt-5"); * ``` */ declare function createGatewayProvider<T>(factory: (opts: { apiKey?: string; baseURL?: string; fetch: typeof globalThis.fetch; }) => T, config: GatewayFetchConfig & { apiKey?: string; baseURL?: string; }): T; //#endregion export { GatewayFetchConfig, createGatewayFetch, createGatewayProvider }; //# sourceMappingURL=gateway-provider.d.mts.map