workers-ai-provider 3.1.13 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,385 @@
1
+ import { LanguageModelV3 } from "@ai-sdk/provider";
2
+
3
+ //#region src/gateway-providers.d.ts
4
+ /**
5
+ * Registry of Cloudflare AI Gateway providers.
6
+ *
7
+ * One table drives both delegate surfaces:
8
+ *
9
+ * - **Slug delegate** (`wai("openai/gpt-5")`): `resolverKey` is the slug prefix
10
+ * the user types. `runCatalog` providers dispatch through the resumable run
11
+ * path (`env.AI.run`, unified billing, `cf-aig-run-id`); the rest go through
12
+ * the gateway path (`env.AI.gateway().run`, BYOK, no resume). `wireFormat`
13
+ * selects the built-in `@ai-sdk/*` parser; absent ⇒ the provider is reachable
14
+ * only via the bring-your-own-provider wrapper (it isn't chat/completions
15
+ * shaped, e.g. audio/image providers).
16
+ * - **Bring-your-own-provider** (`createGatewayProvider`): `hostPattern` +
17
+ * `transformEndpoint` map a wrapped provider's request URL to the gateway
18
+ * `provider` id + endpoint path.
19
+ *
20
+ * Slugs mirror the AI Gateway provider directory
21
+ * (developers.cloudflare.com/ai-gateway/usage/providers/); endpoint transforms
22
+ * mirror `ai-gateway-provider`'s provider table. `runCatalog` / `billing` flags
23
+ * follow the documented unified-billing list (OpenAI, Anthropic, Google AI
24
+ * Studio, Google Vertex, xAI/Grok, Groq) and are otherwise conservative — the
25
+ * e2e suite confirms them live, since resume is undocumented upstream.
26
+ */
27
+ /** Response wire format the slug delegate can parse with a built-in `@ai-sdk/*` provider. */
28
+ type WireFormat = "openai" | "anthropic" | "google";
29
+ /** How a provider is billed + keyed when reached through the gateway. */
30
+ type Billing = "unified" | "byok";
31
+ interface GatewayProviderInfo {
32
+ /**
33
+ * Slug prefix the user types in `wai("<resolverKey>/<model>")`. For
34
+ * `runCatalog` providers this is also the run-catalog author (so
35
+ * `env.AI.run("<resolverKey>/<model>")` resolves).
36
+ */
37
+ resolverKey: string;
38
+ /** Provider id for the gateway universal endpoint (`env.AI.gateway().run([{ provider }])`). */
39
+ gatewayProviderId: string;
40
+ /**
41
+ * Built-in parser wire format. `openai` covers the whole OpenAI-compatible
42
+ * long tail (deepseek, grok, groq, mistral, perplexity, …). Absent ⇒ reachable
43
+ * only via the bring-your-own-provider wrapper (provider-native, non-chat, or a
44
+ * gateway-path URL shape we don't reproduce reliably from the slug delegate).
45
+ */
46
+ wireFormat?: WireFormat;
47
+ /**
48
+ * Wire format the unified-billing **run path** (`env.AI.run`) emits for this
49
+ * provider — which is NOT always the provider's native format. Cloudflare's
50
+ * unified catalog normalizes most providers to OpenAI chat-completions (so
51
+ * `google` is parsed with the `openai` plugin on the run path), but passes
52
+ * **Anthropic through natively** (`content[].text`, native tool shape), so
53
+ * anthropic must be parsed with the `anthropic` plugin. Defaults to `"openai"`
54
+ * for run-catalog providers when omitted. Only meaningful when `runCatalog`.
55
+ */
56
+ runWireFormat?: WireFormat;
57
+ /**
58
+ * Base URL the wire-format builder should target so the request URL it
59
+ * generates host-strips (via {@link transformEndpoint}) to the provider's
60
+ * gateway-native endpoint. Omit to use the `@ai-sdk` provider's default (the
61
+ * provider's own host — correct for `openai`/`anthropic`/`google`). Required
62
+ * for OpenAI-wire providers that share the `openai` plugin but live on a
63
+ * different host (deepseek, grok, groq, mistral, perplexity, …).
64
+ */
65
+ baseURL?: string;
66
+ /** On the unified-billing resumable run catalog (`env.AI.run`, `cf-aig-run-id`). */
67
+ runCatalog: boolean;
68
+ /**
69
+ * Whether the provider has a gateway path (`env.AI.gateway().run`). `false` ⇒
70
+ * **run-path only**: the provider is on the unified run catalog but is not a
71
+ * native gateway provider, so caching, server-side fallback, and
72
+ * `transport: "gateway"` are unavailable and the delegate rejects them with a
73
+ * clear error (rather than failing upstream). Defaults to `true`.
74
+ */
75
+ gatewayPath?: boolean;
76
+ /** Billing model when reached through the gateway. */
77
+ billing: Billing;
78
+ /** Header(s) carrying the upstream provider key (stripped on the gateway path unless BYOK-forwarded). */
79
+ authHeaders: string[];
80
+ /** Host matcher for bring-your-own-provider URL detection. */
81
+ hostPattern?: RegExp;
82
+ /** Strip the provider host, leaving the gateway endpoint path (+ query). */
83
+ transformEndpoint?: (url: string) => string;
84
+ }
85
+ /**
86
+ * The provider table. Order matters only for `detectProviderByUrl` (first match
87
+ * wins); slugs are looked up by `resolverKey`.
88
+ */
89
+ declare const GATEWAY_PROVIDERS: GatewayProviderInfo[];
90
+ /** Look up a provider by the slug prefix the user typed (honoring aliases). */
91
+ declare function findProviderBySlug(resolverKey: string): GatewayProviderInfo | undefined;
92
+ /** Detect the gateway provider from a wrapped provider's request URL (BYOG). */
93
+ declare function detectProviderByUrl(url: string): GatewayProviderInfo | undefined;
94
+ /** All slug keys with a built-in parser (auto-wireable by the slug delegate). */
95
+ declare function wireableProviders(): GatewayProviderInfo[];
96
+ //#endregion
97
+ //#region src/resumable-stream.d.ts
98
+ type ResumeExpiredPolicy = "error" | "accept-partial";
99
+ interface ResumableStreamOptions {
100
+ /** Cloudflare AI binding (e.g. `env.AI`) — used for the resume fetch. */
101
+ binding: Ai;
102
+ /** Gateway id the run was issued under. */
103
+ gateway: string;
104
+ /** The `cf-aig-run-id` of the run to resume. */
105
+ runId: string;
106
+ /**
107
+ * Initial run-path response body. Omit for **cross-invocation re-attach**: the
108
+ * stream then starts by fetching `resume?from={fromEvent}` directly (e.g. a new
109
+ * Durable Object invocation re-attaching to a run after eviction).
110
+ */
111
+ initial?: ReadableStream<Uint8Array>;
112
+ /**
113
+ * SSE event index to (re-)attach from. Defaults to `0`. Used as the starting
114
+ * `from` when `initial` is omitted, and as the base offset for the event
115
+ * counter (so a later reconnect resumes from the correct absolute index).
116
+ */
117
+ fromEvent?: number;
118
+ /** What to do when the resume buffer has expired (404). Defaults to `"error"`. */
119
+ onResumeExpired?: ResumeExpiredPolicy;
120
+ /** Max reconnect attempts before giving up. Defaults to 5. */
121
+ maxReconnects?: number;
122
+ /** Fired before each reconnect with the resume `from` index and attempt number. */
123
+ onReconnect?: (fromEvent: number, attempt: number) => void;
124
+ /**
125
+ * Fired with the cumulative SSE event offset whenever complete events are
126
+ * emitted. Use it to persist `{ runId, eventOffset }` for cross-invocation
127
+ * re-attach (throttle your own writes — this can fire per chunk).
128
+ */
129
+ onProgress?: (eventOffset: number) => void;
130
+ }
131
+ declare function createResumableStream(options: ResumableStreamOptions): ReadableStream<Uint8Array>;
132
+ //#endregion
133
+ //#region src/errors.d.ts
134
+ /**
135
+ * Typed errors for the gateway delegate.
136
+ *
137
+ * - {@link WorkersAIGatewayError} — a single dispatch failed. Carries a coarse
138
+ * {@link GatewayErrorCode}, a `recoverable` hint (whether a retry/fallback is
139
+ * worth attempting), and the parsed gateway/provider envelope.
140
+ * - {@link WorkersAIFallbackError} — every model in a client-side fallback chain
141
+ * failed. Carries the per-attempt tree so callers can see exactly what was
142
+ * tried and why each leg failed.
143
+ */
144
+ /** Coarse classification of a gateway/provider failure. */
145
+ type GatewayErrorCode = "auth" | "rate-limit" | "not-found" | "bad-request" | "provider-error" | "gateway-error" | "resume-expired" | "unknown";
146
+ /** Context attached to a {@link WorkersAIGatewayError}. */
147
+ interface GatewayErrorContext {
148
+ /** Gateway provider id (e.g. `"openai"`, `"google-ai-studio"`). */
149
+ provider?: string;
150
+ /** Provider-native model id. */
151
+ modelId?: string;
152
+ /** Transport the failed dispatch used. */
153
+ transport?: "run" | "gateway";
154
+ /** HTTP status, if any. */
155
+ status?: number | null;
156
+ /** `cf-aig-log-id` for cross-referencing in the dashboard. */
157
+ logId?: string | null;
158
+ /** `cf-aig-run-id`, if the run path issued one. */
159
+ runId?: string | null;
160
+ }
161
+ /** A single dispatch failure through AI Gateway (run or gateway path). */
162
+ declare class WorkersAIGatewayError extends Error {
163
+ readonly code: GatewayErrorCode;
164
+ /** Whether a retry or fallback to another model is worth attempting. */
165
+ readonly recoverable: boolean;
166
+ readonly status: number | null;
167
+ readonly context: GatewayErrorContext;
168
+ /** Parsed gateway/provider error envelope (or raw text). */
169
+ readonly raw?: unknown;
170
+ readonly cause?: unknown;
171
+ constructor(code: GatewayErrorCode, message: string, opts?: {
172
+ recoverable?: boolean;
173
+ status?: number | null;
174
+ context?: GatewayErrorContext;
175
+ raw?: unknown;
176
+ cause?: unknown;
177
+ });
178
+ /**
179
+ * Classify an arbitrary thrown value. Understands AI SDK `APICallError`
180
+ * (reads `statusCode` / `responseBody` / `isRetryable`); falls back to a
181
+ * recoverable `gateway-error` for transport/connection failures so a fallback
182
+ * chain keeps trying.
183
+ */
184
+ static fromUnknown(e: unknown): WorkersAIGatewayError;
185
+ /** Build from an HTTP `Response` (reads the body for the envelope). */
186
+ static fromResponse(resp: Response, context?: GatewayErrorContext): Promise<WorkersAIGatewayError>;
187
+ }
188
+ /** One leg of a client-side fallback chain. */
189
+ interface FallbackAttempt {
190
+ /** The model slug attempted. */
191
+ model: string;
192
+ /** Transport used for this attempt. */
193
+ transport: "run" | "gateway";
194
+ /** Whether this attempt succeeded. */
195
+ ok: boolean;
196
+ /** HTTP status, if any. */
197
+ status?: number | null;
198
+ /** The classified error, when the attempt failed. */
199
+ error?: WorkersAIGatewayError;
200
+ }
201
+ /** Every model in a client-side fallback chain failed. */
202
+ declare class WorkersAIFallbackError extends Error {
203
+ /** The ordered attempt tree (primary first, then each fallback). */
204
+ readonly attempts: FallbackAttempt[];
205
+ constructor(attempts: FallbackAttempt[], message?: string);
206
+ /** The last (most recent) attempt's error, if any. */
207
+ get lastError(): WorkersAIGatewayError | undefined;
208
+ }
209
+ //#endregion
210
+ //#region src/client-fallback.d.ts
211
+ /** One model in a client-side fallback chain. */
212
+ interface FallbackLeg {
213
+ /** The model slug this leg dispatches. */
214
+ slug: string;
215
+ /** The built AI SDK model. */
216
+ model: LanguageModelV3;
217
+ /** Transport the leg uses. */
218
+ transport: Transport;
219
+ }
220
+ /**
221
+ * Wrap a chain of models so a failed *pre-stream* dispatch falls through to the
222
+ * next model, preserving resume on each leg's own transport. If every leg fails,
223
+ * throws a {@link WorkersAIFallbackError} carrying the full attempt tree.
224
+ *
225
+ * Fallback triggers on `doGenerate`/`doStream` rejection (the dispatch never
226
+ * produced a stream). Errors that surface *mid-stream* — after content has
227
+ * already been emitted — are not recoverable here and propagate as-is.
228
+ */
229
+ declare function createClientFallbackModel(legs: FallbackLeg[]): LanguageModelV3;
230
+ //#endregion
231
+ //#region src/gateway-delegate.d.ts
232
+ /**
233
+ * Gateway delegate — route AI SDK catalog models through Cloudflare AI Gateway,
234
+ * with capability-driven transport selection.
235
+ *
236
+ * Two transports back the same model, chosen from the requested options:
237
+ *
238
+ * - **Run path** `env.AI.run(slug, body, { returnRawResponse })` — resumable
239
+ * streaming (`cf-aig-run-id`). The default.
240
+ * - **Gateway path** `env.AI.gateway(id).run([entry, …fallback])` — server-side
241
+ * fallback and caching. Does not surface `cf-aig-run-id`, so resume is off.
242
+ *
243
+ * The SAME `@ai-sdk/*` provider parses the response on either path, so there is no
244
+ * per-provider or per-path response parsing here. Provider plugins (which import
245
+ * `@ai-sdk/openai`, `@ai-sdk/anthropic`, …) are injected from sub-path modules
246
+ * (`workers-ai-provider/openai`, …) so those AI SDK packages stay OPTIONAL peer
247
+ * dependencies — you only install the ones you use.
248
+ *
249
+ * @example
250
+ * ```ts
251
+ * import { createGatewayDelegate } from "workers-ai-provider/gateway-delegate";
252
+ * import { openai } from "workers-ai-provider/openai";
253
+ * import { streamText } from "ai";
254
+ *
255
+ * const wai = createGatewayDelegate({
256
+ * binding: env.AI,
257
+ * gateway: "my-gateway",
258
+ * providers: [openai],
259
+ * });
260
+ *
261
+ * const result = streamText({ model: wai("openai/gpt-5"), prompt: "Hello" });
262
+ * // result.response.headers["cf-aig-run-id"] is set — resume from there.
263
+ * ```
264
+ */
265
+ interface ParsedSlug {
266
+ /** First path segment — the registry resolver key (selects provider + wire format). */
267
+ resolverKey: string;
268
+ /** Remaining segments — the provider-native model id. */
269
+ modelId: string;
270
+ }
271
+ /**
272
+ * Parse a `vendor/model` slug. The first segment is the resolver key (which
273
+ * registry entry handles it); the rest is the provider-native model id. Routing
274
+ * providers keep multi-segment model ids, e.g. `openrouter/anthropic/claude`.
275
+ */
276
+ declare function parseSlug(slug: string): ParsedSlug;
277
+ /**
278
+ * Adapts a `@ai-sdk/*` provider to the delegate, keyed by the response wire
279
+ * format it parses. Imported from a sub-path module (e.g.
280
+ * `workers-ai-provider/openai`) so the AI SDK package stays an optional peer
281
+ * dependency. One plugin serves every registry provider of that wire format —
282
+ * the `openai` plugin covers the whole OpenAI-compatible long tail (deepseek,
283
+ * grok, groq, mistral, perplexity, openrouter, …).
284
+ */
285
+ interface ProviderPlugin {
286
+ /** The response wire format this builder parses. */
287
+ readonly wireFormat: WireFormat;
288
+ /**
289
+ * Build the AI SDK model, wiring the gateway-dispatching `fetch`. `baseURL`
290
+ * (when provided by the registry) targets the provider's host so the request
291
+ * URL host-strips to its gateway-native endpoint — pass it to the underlying
292
+ * `@ai-sdk` provider.
293
+ */
294
+ create(args: {
295
+ modelId: string;
296
+ fetch: typeof globalThis.fetch;
297
+ baseURL?: string;
298
+ }): LanguageModelV3;
299
+ }
300
+ type Transport = "run" | "gateway";
301
+ interface FallbackOptions {
302
+ /** `"client"` keeps resume (sequential run-path attempts); `"server"` uses the gateway path. */
303
+ mode: "client" | "server";
304
+ /** Ordered model slugs to try after the primary. */
305
+ models: string[];
306
+ }
307
+ interface DispatchInfo {
308
+ transport: Transport;
309
+ resumeEnabled: boolean;
310
+ warnings: string[];
311
+ runId: string | null;
312
+ status: number | null;
313
+ cfStep: string | null;
314
+ cacheStatus: string | null;
315
+ logId: string | null;
316
+ }
317
+ interface DelegateCallOptions {
318
+ /** Resumable streaming (run path). Defaults to the delegate's `resume` (true). */
319
+ resume?: boolean;
320
+ /** Cross-model fallback. `"server"` mode uses the gateway path (disables resume). */
321
+ fallback?: FallbackOptions;
322
+ /** Gateway-path response caching (seconds). Forces the gateway path. */
323
+ cacheTtl?: number;
324
+ /** Bypass gateway cache. Forces the gateway path. */
325
+ skipCache?: boolean;
326
+ /** Escape hatch: force a transport. */
327
+ transport?: Transport;
328
+ /**
329
+ * Run path only: behavior when the resume buffer has expired (404) after a
330
+ * mid-stream drop. `"error"` (default) surfaces a `GatewayDelegateError`;
331
+ * `"accept-partial"` ends the stream cleanly with whatever was delivered.
332
+ */
333
+ onResumeExpired?: ResumeExpiredPolicy;
334
+ /** Extra request headers (run path: `extraHeaders`; gateway path: entry headers). */
335
+ extraHeaders?: Record<string, string>;
336
+ /**
337
+ * Gateway path only: forward the upstream provider key instead of stripping it.
338
+ * Required for BYOK providers (not on unified billing). Supply the key via
339
+ * `extraHeaders` (e.g. `{ authorization: "Bearer …" }`); without `byok` the
340
+ * delegate strips provider auth headers so unified billing applies.
341
+ */
342
+ byok?: boolean;
343
+ /** Override the delegate's gateway for this model. */
344
+ gateway?: GatewayOptions | string;
345
+ /**
346
+ * Custom metadata attached to the gateway log for this request (spend
347
+ * attribution, tenant ids, etc.). Merges over any `metadata` already set via
348
+ * `gateway: { metadata }`. Applied on both transports (run path: gateway
349
+ * options; gateway path: `cf-aig-metadata` header). `bigint` values are
350
+ * coerced to strings for the header form.
351
+ */
352
+ metadata?: Record<string, number | string | boolean | null | bigint>;
353
+ /** Force gateway log collection on/off for this request (both transports). */
354
+ collectLog?: boolean;
355
+ /** Called once per dispatch with the resolved transport + gateway headers. */
356
+ onDispatch?: (info: DispatchInfo) => void;
357
+ /**
358
+ * Run path only: fired with the cumulative SSE event offset as the resumable
359
+ * stream advances. Pair with `onDispatch` (for `runId`) to persist
360
+ * `{ runId, eventOffset }` for cross-invocation re-attach after eviction.
361
+ * Throttle your own writes — this can fire per chunk.
362
+ */
363
+ onProgress?: (eventOffset: number) => void;
364
+ }
365
+ interface Selection {
366
+ transport: Transport;
367
+ resumeEnabled: boolean;
368
+ warnings: string[];
369
+ }
370
+ /**
371
+ * Resolve the transport from the requested options. Gateway-only features (server
372
+ * fallback, caching) force the gateway path and disable resume — with a loud
373
+ * warning if resume was merely defaulted, or a thrown error if it was explicitly
374
+ * requested.
375
+ */
376
+ declare function selectTransport(opts: DelegateCallOptions, resumeExplicitlyTrue: boolean, runCatalog?: boolean, gatewayAvailable?: boolean): Selection;
377
+ type GatewayDelegateErrorKind = "config" | "dispatch" | "provider" | "resume-expired";
378
+ declare class GatewayDelegateError extends Error {
379
+ readonly kind: GatewayDelegateErrorKind;
380
+ readonly cause?: unknown;
381
+ constructor(kind: GatewayDelegateErrorKind, message: string, cause?: unknown);
382
+ }
383
+ //#endregion
384
+ export { WireFormat as C, wireableProviders as E, GatewayProviderInfo as S, findProviderBySlug as T, ResumableStreamOptions as _, ParsedSlug as a, Billing as b, parseSlug as c, createClientFallbackModel as d, FallbackAttempt as f, WorkersAIGatewayError as g, WorkersAIFallbackError as h, GatewayDelegateError as i, selectTransport as l, GatewayErrorContext as m, DispatchInfo as n, ProviderPlugin as o, GatewayErrorCode as p, FallbackOptions as r, Transport as s, DelegateCallOptions as t, FallbackLeg as u, ResumeExpiredPolicy as v, detectProviderByUrl as w, GATEWAY_PROVIDERS as x, createResumableStream as y };
385
+ //# sourceMappingURL=gateway-delegate-BfaUTwDZ.d.mts.map