@macpaw/ai-sdk 1.0.0-rc.2 → 1.0.0-rc.5

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.
package/CHANGELOG.md CHANGED
@@ -1,9 +1,10 @@
1
- # [1.0.0-rc.2](https://github.com/MacPaw/ai-sdk-typescript/compare/v1.0.0-rc.1...v1.0.0-rc.2) (2026-04-01)
1
+ # [1.0.0-rc.5](https://github.com/MacPaw/ai-sdk-typescript/compare/v1.0.0-rc.4...v1.0.0-rc.5) (2026-06-08)
2
2
 
3
3
 
4
- ### Features
4
+ ### Bug Fixes
5
5
 
6
- * update Node.js version in release workflow and enhance tool version visibility ([a86be3a](https://github.com/MacPaw/ai-sdk-typescript/commit/a86be3a06107e92af665a1bcbb1303730f3a5f56))
6
+ * **gateway:** route AI requests to /ai/v1 instead of /ai/api/v1 ([3d75197](https://github.com/MacPaw/ai-sdk-typescript/commit/3d75197a1fca800272f6ddcb63879d393f678fd0))
7
+ * **gateway:** route AI requests to /ai/v1 instead of /ai/api/v1 ([#94](https://github.com/MacPaw/ai-sdk-typescript/issues/94)) ([910c509](https://github.com/MacPaw/ai-sdk-typescript/commit/910c50918c692b6256f18ee06b5abe95c7fe008c))
7
8
 
8
9
  # @macpaw/ai-sdk
9
10
 
package/MIGRATION.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  This package is a **small Vercel AI SDK extension** for MacPaw AI Gateway. Upstream **`ai`**, **`@ai-sdk/openai`**, **`@ai-sdk/react`** (or **`ai/react`**) stay the source for core APIs and hooks.
4
4
 
5
+ That also means upstream UI/hooks/version migrations stay upstream. If your installed `ai` / `@ai-sdk/react` version changes hook or schema APIs, follow the upstream versioned docs for those packages rather than expecting `@macpaw/ai-sdk` to redefine them.
6
+
5
7
  ## Entry points
6
8
 
7
9
  | Path | Role |
@@ -22,14 +24,18 @@ The following paths are **not** published in current versions:
22
24
  Replace `createAIGatewayClient` with:
23
25
 
24
26
  1. **Vercel flows** — `createAIGatewayProvider` / `createGatewayProvider` + `generateText` / `streamText` / `embed` from `ai` where the OpenAI-compatible surface is enough.
25
- 2. **Raw JSON or multipart** — `createGatewayFetch({ baseURL, getAuthToken, ... })` then `gatewayFetch('/api/v1/...', { method, headers, body })` (e.g. `FormData` for image edits or audio).
27
+ 2. **Raw JSON or multipart** — `createGatewayFetch({ baseURL, getAuthToken, ... })` then `gatewayFetch('/v1/...', { method, headers, body })` (e.g. `FormData` for image edits or audio).
26
28
 
27
29
  Types for request/response bodies can come from your app, from OpenAI SDK types, or from gateway OpenAPI — they are not re-exported from this package today.
28
30
 
31
+ If you want the default production Gateway URL in fetch-only flows, use the public `resolveGatewayBaseURL()` helper and pass the resolved value into `createGatewayFetch()`.
32
+
29
33
  ## If you used `@macpaw/ai-sdk/runtime`
30
34
 
31
35
  Internals (`executeRequestPipeline`, etc.) are not part of the public API. Prefer `createGatewayFetch` or provider options (`middleware`, `retry`, `timeout`, `fetch`).
32
36
 
37
+ The same applies to source-only error helpers: if a helper is not exported from `@macpaw/ai-sdk`, treat it as internal even if you see it in the repository source.
38
+
33
39
  ## Quick import cheatsheet
34
40
 
35
41
  ```ts
package/README.md CHANGED
@@ -30,6 +30,8 @@ npm install @macpaw/ai-sdk
30
30
 
31
31
  Also install upstream packages you call directly, for example `ai`, `@ai-sdk/openai`, `@ai-sdk/react`.
32
32
 
33
+ This package does not pin or wrap the upstream UI/hooks API from `ai` / `@ai-sdk/react`. Follow the versioned upstream docs for the exact major version you install there. If your chosen upstream version requires version-specific imports or patterns (for example schema helpers), use the upstream guidance for those APIs.
34
+
33
35
  ## Quick start (Vercel AI SDK)
34
36
 
35
37
  ```ts
@@ -85,12 +87,12 @@ Internal resolution: `resolveConfig()` in `gateway-config.ts`.
85
87
 
86
88
  ## `createGatewayFetch` — raw HTTP / multipart
87
89
 
88
- Same auth, retry, middleware, and error normalization as the provider path. Use **relative** URLs under the gateway root (e.g. `'/api/v1/images/edits'`) or absolute URLs that stay under the same gateway origin.
90
+ Same auth, retry, middleware, and error normalization as the provider path. The base already includes `/ai`, so use **relative** URLs under it (e.g. `'/v1/images/edits'`, which resolves to `https://api.macpaw.com/ai/v1/images/edits`) or absolute URLs that stay under the same gateway origin.
89
91
 
90
92
  ```ts
91
- import { createGatewayFetch } from '@macpaw/ai-sdk';
93
+ import { createGatewayFetch, resolveGatewayBaseURL } from '@macpaw/ai-sdk';
92
94
 
93
- const baseURL = 'https://api.macpaw.com/ai'; // or resolve via env: 'production'
95
+ const baseURL = resolveGatewayBaseURL(undefined, 'production', 'gatewayFetch');
94
96
  const gatewayFetch = createGatewayFetch({
95
97
  baseURL,
96
98
  getAuthToken: async () => token,
@@ -101,11 +103,13 @@ form.append('image', imageBlob, 'photo.png');
101
103
  form.append('prompt', 'Add a hat');
102
104
  form.append('model', 'openai/dall-e-2');
103
105
 
104
- const res = await gatewayFetch('/api/v1/images/edits', { method: 'POST', body: form });
106
+ const res = await gatewayFetch('/v1/images/edits', { method: 'POST', body: form });
105
107
  ```
106
108
 
107
109
  Non-gateway absolute URLs are passed through without injecting Bearer auth (placeholder key is stripped). See `gateway-fetch.ts`.
108
110
 
111
+ `createGatewayFetch` requires a resolved `baseURL`. Use the exported `resolveGatewayBaseURL()` helper if you want the same `'production'` shortcut that provider factories support.
112
+
109
113
  ## `createGatewayProvider` — prefixed model IDs
110
114
 
111
115
  Bare model IDs get a default Gateway prefix per provider constant; IDs that already contain `/` are unchanged.
@@ -147,6 +151,8 @@ Extends `GatewayProviderSettings` plus OpenAI provider settings (without `apiKey
147
151
  - `normalizeErrors` — default `true`; non-OK Gateway responses throw typed errors
148
152
  - `createOpenAI` — optional override of `createOpenAI` from `@ai-sdk/openai` (tests/advanced)
149
153
 
154
+ Use `normalizeErrors: false` only when you intentionally want to inspect raw failed `Response` objects in provider-driven tests or adapters. Auth refresh and retry behavior still stay on; only typed non-OK error throwing is relaxed.
155
+
150
156
  ## Middleware
151
157
 
152
158
  ```ts
@@ -199,6 +205,8 @@ AIGatewayModule.forRoot({
199
205
  });
200
206
  ```
201
207
 
208
+ If your Nest app uses TypeScript subpath exports strictly, make sure its `tsconfig` uses a modern resolver such as `moduleResolution: "Node16"`, `"NodeNext"`, or `"bundler"` so `@macpaw/ai-sdk/nestjs` resolves correctly.
209
+
202
210
  Inject **`GatewayProviderSettings`** (not an HTTP client) and build providers in the service:
203
211
 
204
212
  ```ts
@@ -225,6 +233,8 @@ export class ChatService {
225
233
 
226
234
  `AIGatewayExceptionFilter` maps `AIGatewayError` to JSON HTTP responses. See `examples/nestjs/` for a copy-paste skeleton.
227
235
 
236
+ Only documented root exports are public API. Source-level helpers such as `parseErrorResponseFromResponse` and `parseStreamErrorPayload` may exist internally, but they are not supported import targets unless exported from `@macpaw/ai-sdk`.
237
+
228
238
  ## Examples
229
239
 
230
240
  From the repo root:
@@ -57,6 +57,9 @@ interface GatewayProviderSettings {
57
57
  */
58
58
  fetch?: typeof fetch;
59
59
  }
60
+ /** Default base URL for the production environment. */
61
+ declare const DEFAULT_BASE_URLS: Record<Environment, string>;
62
+ declare function resolveGatewayBaseURL(baseURL: string | undefined, env: Environment | undefined, consumerName: string): string;
60
63
 
61
64
  /**
62
65
  * Error types for the AI Gateway SDK.
@@ -178,4 +181,4 @@ declare function isAIGatewayError(value: unknown): value is AIGatewayError;
178
181
  */
179
182
  declare function parseErrorResponse(statusCode: number, body: unknown): never;
180
183
 
181
- export { AIGatewayError as A, CreditsError as C, ErrorCode as E, type GatewayProviderSettings as G, type Middleware as M, type NormalizedErrorMetadata as N, type OpenAIErrorResponse as O, RateLimitError as R, AuthError as a, GatewayApiCode as b, type GatewayApiErrorItem as c, type GatewayApiErrorResponse as d, GatewayValidationError as e, ModelNotAllowedError as f, type RetryConfig as g, isAIGatewayError as i, parseErrorResponse as p };
184
+ export { AIGatewayError as A, CreditsError as C, DEFAULT_BASE_URLS as D, type Environment as E, type GatewayProviderSettings as G, type Middleware as M, type NormalizedErrorMetadata as N, type OpenAIErrorResponse as O, RateLimitError as R, AuthError as a, ErrorCode as b, GatewayApiCode as c, type GatewayApiErrorItem as d, type GatewayApiErrorResponse as e, GatewayValidationError as f, ModelNotAllowedError as g, type RetryConfig as h, isAIGatewayError as i, parseErrorResponse as p, resolveGatewayBaseURL as r };
@@ -57,6 +57,9 @@ interface GatewayProviderSettings {
57
57
  */
58
58
  fetch?: typeof fetch;
59
59
  }
60
+ /** Default base URL for the production environment. */
61
+ declare const DEFAULT_BASE_URLS: Record<Environment, string>;
62
+ declare function resolveGatewayBaseURL(baseURL: string | undefined, env: Environment | undefined, consumerName: string): string;
60
63
 
61
64
  /**
62
65
  * Error types for the AI Gateway SDK.
@@ -178,4 +181,4 @@ declare function isAIGatewayError(value: unknown): value is AIGatewayError;
178
181
  */
179
182
  declare function parseErrorResponse(statusCode: number, body: unknown): never;
180
183
 
181
- export { AIGatewayError as A, CreditsError as C, ErrorCode as E, type GatewayProviderSettings as G, type Middleware as M, type NormalizedErrorMetadata as N, type OpenAIErrorResponse as O, RateLimitError as R, AuthError as a, GatewayApiCode as b, type GatewayApiErrorItem as c, type GatewayApiErrorResponse as d, GatewayValidationError as e, ModelNotAllowedError as f, type RetryConfig as g, isAIGatewayError as i, parseErrorResponse as p };
184
+ export { AIGatewayError as A, CreditsError as C, DEFAULT_BASE_URLS as D, type Environment as E, type GatewayProviderSettings as G, type Middleware as M, type NormalizedErrorMetadata as N, type OpenAIErrorResponse as O, RateLimitError as R, AuthError as a, ErrorCode as b, GatewayApiCode as c, type GatewayApiErrorItem as d, type GatewayApiErrorResponse as e, GatewayValidationError as f, ModelNotAllowedError as g, type RetryConfig as h, isAIGatewayError as i, parseErrorResponse as p, resolveGatewayBaseURL as r };
package/dist/index.cjs CHANGED
@@ -130,6 +130,11 @@ function hasHeaderCaseInsensitive(headers, name) {
130
130
  const lower = name.toLowerCase();
131
131
  return Object.keys(headers).some((k) => k.toLowerCase() === lower);
132
132
  }
133
+ function deleteAllAuthorizationHeaders(headers) {
134
+ for (const key of Object.keys(headers)) {
135
+ if (key.toLowerCase() === "authorization") delete headers[key];
136
+ }
137
+ }
133
138
  function shouldAutoSetJsonContentType(body, headers) {
134
139
  if (body == null || hasHeaderCaseInsensitive(headers, "content-type")) return false;
135
140
  const isFormData = typeof FormData !== "undefined" && body instanceof FormData;
@@ -244,13 +249,11 @@ async function executeRequest(config, request, behavior, forceRefreshToken) {
244
249
  }
245
250
  const finalHeaders = { ...currentRequest.headers };
246
251
  if (behavior.includeAuth) {
247
- const token = await config.getAuthToken(forceRefreshToken);
248
- if (token) {
252
+ deleteAllAuthorizationHeaders(finalHeaders);
253
+ const rawToken = await config.getAuthToken(forceRefreshToken);
254
+ const token = normalizeBearerToken(rawToken);
255
+ if (token && token !== GATEWAY_PLACEHOLDER_API_KEY) {
249
256
  finalHeaders.Authorization = `Bearer ${token}`;
250
- } else {
251
- for (const key of Object.keys(finalHeaders)) {
252
- if (key.toLowerCase() === "authorization") delete finalHeaders[key];
253
- }
254
257
  }
255
258
  }
256
259
  return executeFetch(config.fetchImpl, { ...currentRequest, headers: finalHeaders });
@@ -280,14 +283,29 @@ async function executeRequest(config, request, behavior, forceRefreshToken) {
280
283
  }
281
284
 
282
285
  // src/gateway-fetch.ts
286
+ var GATEWAY_PLACEHOLDER_API_KEY = "ai-gateway-auth-via-fetch";
283
287
  function resolveRequestUrl(input) {
284
288
  if (typeof input === "string") return input;
285
289
  if (input instanceof URL) return input.href;
286
290
  return input.url;
287
291
  }
292
+ function normalizeBearerToken(raw) {
293
+ if (raw == null) return null;
294
+ let s = String(raw).trim();
295
+ if (!s) return null;
296
+ if (s.startsWith('"') && s.endsWith('"') && s.length >= 2 || s.startsWith("'") && s.endsWith("'") && s.length >= 2) {
297
+ s = s.slice(1, -1).trim();
298
+ }
299
+ s = s.replace(/[,;]+\s*$/g, "").trim();
300
+ return s.length > 0 ? s : null;
301
+ }
288
302
  function stripPlaceholderAuthorization(headers, placeholder) {
289
303
  const auth = headers.get("authorization");
290
- if (auth === `Bearer ${placeholder}`) {
304
+ if (!auth) return;
305
+ const m = auth.match(/^Bearer\s+(\S+)/i);
306
+ if (!m) return;
307
+ const credential = normalizeBearerToken(m[1]);
308
+ if (credential === placeholder) {
291
309
  headers.delete("authorization");
292
310
  }
293
311
  }
@@ -306,7 +324,6 @@ function isGatewayUrl(url, gatewayBaseUrl) {
306
324
  const requestPath = url.pathname.replace(/\/$/, "");
307
325
  return url.origin === gatewayBaseUrl.origin && (requestPath === gatewayPath || requestPath.startsWith(`${gatewayPath}/`));
308
326
  }
309
- var GATEWAY_PLACEHOLDER_API_KEY = "ai-gateway-auth-via-fetch";
310
327
  function createGatewayFetch(options) {
311
328
  const { baseURL, normalizeErrors = true } = options;
312
329
  const base = baseURL.replace(/\/$/, "");
@@ -325,9 +342,7 @@ function createGatewayFetch(options) {
325
342
  headers.set(key, value);
326
343
  }
327
344
  }
328
- if (!isGatewayRequest) {
329
- stripPlaceholderAuthorization(headers, GATEWAY_PLACEHOLDER_API_KEY);
330
- }
345
+ stripPlaceholderAuthorization(headers, GATEWAY_PLACEHOLDER_API_KEY);
331
346
  return executeRequestPipeline(
332
347
  resolvedConfig,
333
348
  {
@@ -359,7 +374,7 @@ function createAIGatewayProvider(options) {
359
374
  name: options.name,
360
375
  organization: options.organization,
361
376
  project: options.project,
362
- baseURL: `${baseURL.replace(/\/$/, "")}/api/${DEFAULT_API_VERSION}`,
377
+ baseURL: `${baseURL.replace(/\/$/, "")}/${DEFAULT_API_VERSION}`,
363
378
  fetch: customFetch,
364
379
  apiKey: GATEWAY_PLACEHOLDER_API_KEY
365
380
  });
@@ -474,10 +489,12 @@ Object.defineProperty(exports, "parseErrorResponse", {
474
489
  enumerable: true,
475
490
  get: function () { return chunkN26BDEG5_cjs.parseErrorResponse; }
476
491
  });
492
+ exports.DEFAULT_BASE_URLS = DEFAULT_BASE_URLS;
477
493
  exports.GATEWAY_PLACEHOLDER_API_KEY = GATEWAY_PLACEHOLDER_API_KEY;
478
494
  exports.GATEWAY_PROVIDERS = GATEWAY_PROVIDERS;
479
495
  exports.createAIGatewayProvider = createAIGatewayProvider;
480
496
  exports.createGatewayFetch = createGatewayFetch;
481
497
  exports.createGatewayProvider = createGatewayProvider;
498
+ exports.resolveGatewayBaseURL = resolveGatewayBaseURL;
482
499
  //# sourceMappingURL=index.cjs.map
483
500
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/gateway-config.ts","../src/gateway-retry.ts","../src/gateway-request.ts","../src/gateway-fetch.ts","../src/gateway-provider.ts"],"names":["AuthError","parseErrorResponseFromResponse","builtinCreateOpenAI"],"mappings":";;;;;;AAyBO,IAAM,aAAA,GAAuC;AAAA,EAClD,WAAA,EAAa,CAAA;AAAA,EACb,cAAA,EAAgB,GAAA;AAAA,EAChB,UAAA,EAAY,GAAA;AAAA,EACZ,mBAAmB,CAAC,GAAA,EAAK,GAAA,EAAK,GAAA,EAAK,KAAK,GAAG;AAC7C,CAAA;AAGO,SAAS,qBAAqB,MAAA,EAAsD;AACzF,EAAA,MAAM,IAAI,IAAA,CAAK,KAAA,CAAM,MAAA,CAAO,MAAA,CAAO,WAAW,CAAC,CAAA;AAC/C,EAAA,MAAM,WAAA,GAAc,OAAO,QAAA,CAAS,CAAC,KAAK,CAAA,IAAK,CAAA,GAAI,IAAI,aAAA,CAAc,WAAA;AACrE,EAAA,OAAO,EAAE,GAAG,MAAA,EAAQ,WAAA,EAAY;AAClC;AAyDO,SAAS,cAAc,MAAA,EAAuE;AACnG,EAAA,OAAO;AAAA,IACL,SAAS,MAAA,CAAO,OAAA;AAAA,IAChB,cAAc,MAAA,CAAO,YAAA;AAAA,IACrB,KAAA,EAAO,MAAA,CAAO,KAAA,KAAU,KAAA,GAAQ,KAAA,GAAQ,oBAAA,CAAqB,EAAE,GAAG,aAAA,EAAe,GAAG,MAAA,CAAO,KAAA,EAAO,CAAA;AAAA,IAClG,YAAY,CAAC,GAAI,MAAA,CAAO,UAAA,IAAc,EAAG,CAAA;AAAA,I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,QAAA,EAAU;AAC/B,YAAA,IAAA,CAAK,CAAC,CAAA,GAAI,aAAA,CAAc,MAAA,EAAQ,IAAA,CAAK,CAAC,CAAW,CAAA;AAAA,UACnD;AAEA,UAAA,OAAQ,KAAA,CAAmC,KAAA,CAAM,QAAA,EAAU,IAAI,CAAA;AAAA,QACjE,CAAA;AAAA,MACF;AACA,MAAA,OAAO,KAAA;AAAA,IACT,CAAA;AAAA,IACA,GAAA,CAAI,SAAS,IAAA,EAAM;AACjB,MAAA,OAAO,IAAA,IAAS,QAAA;AAAA,IAClB;AAAA,GACD,CAAA;AACH;AAgBO,SAAS,qBAAA,CACd,UACA,OAAA,EACgB;AAChB,EAAA,IAAI,QAAA,KAAa,kBAAkB,iBAAA,EAAmB;AACpD,IAAA,OAAO,6BAAA;AAAA,MACJ,OAAA,CAA2C,WAAA;AAAA,MAC5C;AAAA,KACF;AAAA,EACF;AAEA,EAAA,OAAO,6BAAA;AAAA,IACL,kCAAkC,QAA4C,CAAA;AAAA,IAC9E;AAAA,GACF;AACF","file":"index.cjs","sourcesContent":["/**\n * Configuration types and resolver for the AI Gateway SDK.\n *\n * GatewayProviderSettings is the public API — 8 fields.\n * ResolvedConfig is the internal resolved form used by the request pipeline.\n */\n\n/**\n * Supported gateway environments.\n * Only 'production' is exposed — non-production setups use a direct `baseURL`\n * instead, keeping env-specific URL management out of this library.\n */\nexport type Environment = 'production';\n\nexport interface RetryConfig {\n /** Max number of attempts (including first). Default 3. */\n maxAttempts?: number;\n /** Initial delay in ms. Default 1000. */\n initialDelayMs?: number;\n /** Max delay in ms. Default 30000. */\n maxDelayMs?: number;\n /** Retry only on these status codes (and network errors). Default: 429, 5xx. */\n retryableStatuses?: number[];\n}\n\nexport const DEFAULT_RETRY: Required<RetryConfig> = {\n maxAttempts: 3,\n initialDelayMs: 1000,\n maxDelayMs: 30000,\n retryableStatuses: [429, 500, 502, 503, 504],\n};\n\n/** Coerces invalid `maxAttempts` (0, negative, NaN) to the default. */\nexport function normalizeRetryConfig(merged: Required<RetryConfig>): Required<RetryConfig> {\n const n = Math.floor(Number(merged.maxAttempts));\n const maxAttempts = Number.isFinite(n) && n >= 1 ? n : DEFAULT_RETRY.maxAttempts;\n return { ...merged, maxAttempts };\n}\n\nexport interface RequestConfig {\n url: string;\n method: string;\n headers: Record<string, string>;\n body?: RequestInit['body'];\n signal?: AbortSignal | undefined;\n}\n\nexport type Middleware = (\n config: RequestConfig,\n next: (config: RequestConfig) => Promise<Response>,\n) => Promise<Response>;\n\n/**\n * Configuration for AI Gateway providers and NestJS module.\n *\n * Exactly 8 fields — covers auth, routing, request behaviour and middleware.\n */\nexport interface GatewayProviderSettings {\n /** Gateway base URL. Required if env is not set. */\n baseURL?: string;\n /** 'production' uses the default MacPaw Gateway URL. */\n env?: Environment;\n /**\n * Returns Bearer token for auth.\n * Called with `forceRefresh=true` after 401 — provide a fresh token.\n */\n getAuthToken: (forceRefresh?: boolean) => Promise<string | null>;\n /** Extra headers sent with every request. Do not set Authorization here. */\n headers?: Record<string, string>;\n /** Retry policy. Set to false to disable. */\n retry?: RetryConfig | false;\n /** Request timeout in ms. Default: 60000. */\n timeout?: number;\n /** Request interceptors. Run in order before each request. */\n middleware?: Middleware[];\n /**\n * Custom fetch implementation.\n * Use for testing or low-level request customization.\n */\n fetch?: typeof fetch;\n}\n\n/** Internal resolved form — used only by the request pipeline. */\nexport interface ResolvedConfig {\n baseURL: string;\n getAuthToken: (forceRefresh?: boolean) => Promise<string | null>;\n /** Already normalized — all fields present, maxAttempts ≥ 1. */\n retry: Required<RetryConfig> | false;\n middleware: Middleware[];\n headers?: Record<string, string>;\n timeout: number;\n fetchImpl?: typeof fetch;\n}\n\nexport function resolveConfig(config: GatewayProviderSettings & { baseURL: string }): ResolvedConfig {\n return {\n baseURL: config.baseURL,\n getAuthToken: config.getAuthToken,\n retry: config.retry === false ? false : normalizeRetryConfig({ ...DEFAULT_RETRY, ...config.retry }),\n middleware: [...(config.middleware ?? [])],\n headers: config.headers,\n timeout: config.timeout ?? 60_000,\n fetchImpl: config.fetch,\n };\n}\n\n/** Default base URL for the production environment. */\nexport const DEFAULT_BASE_URLS: Record<Environment, string> = {\n production: 'https://api.macpaw.com/ai',\n};\n\nexport function resolveGatewayBaseURL(\n baseURL: string | undefined,\n env: Environment | undefined,\n consumerName: string,\n): string {\n const resolved = baseURL ?? (env ? DEFAULT_BASE_URLS[env] : undefined);\n if (!resolved) {\n throw new Error(\n `${consumerName} requires baseURL or env (production). For non-production environments, pass baseURL directly.`,\n );\n }\n return resolved;\n}\n","/**\n * Retry with exponential backoff + jitter.\n * Only retries on 429, 5xx and network errors; never on 4xx (401, 402, etc.).\n *\n * Jitter prevents thundering herd when many clients retry simultaneously.\n * Respects Retry-After metadata from 429 responses.\n */\n\nimport type { RetryConfig } from './gateway-config';\nimport type { AIGatewayError } from './gateway-errors';\n\nfunction delay(ms: number, signal?: AbortSignal): Promise<void> {\n return new Promise((resolve, reject) => {\n if (signal?.aborted) {\n reject(signal.reason);\n return;\n }\n const timer = setTimeout(resolve, ms);\n signal?.addEventListener(\n 'abort',\n () => {\n clearTimeout(timer);\n reject(signal.reason);\n },\n { once: true },\n );\n });\n}\n\nfunction isRetryableStatus(status: number, retryableStatuses: number[]): boolean {\n return retryableStatuses.includes(status);\n}\n\n/** Add random jitter: 0-25% of the base delay to avoid thundering herd */\nfunction addJitter(delayMs: number): number {\n const jitter = delayMs * 0.25 * Math.random();\n return delayMs + jitter;\n}\n\nexport async function withRetry<T>(\n fn: () => Promise<T>,\n options: {\n /** Already-normalized config — all fields must be present. */\n retryConfig: Required<RetryConfig>;\n signal?: AbortSignal;\n isNetworkError?: (err: unknown) => boolean;\n },\n): Promise<T> {\n const { maxAttempts, initialDelayMs, maxDelayMs, retryableStatuses } = options.retryConfig;\n let lastError: unknown;\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n try {\n return await fn();\n } catch (err) {\n lastError = err;\n if (attempt === maxAttempts) break;\n const status = (err as { statusCode?: number })?.statusCode;\n const isRetryable =\n status != null ? isRetryableStatus(status, retryableStatuses) : (options.isNetworkError?.(err) ?? false);\n if (!isRetryable) throw err;\n\n const retryAfterSeconds = (err as AIGatewayError)?.retryAfter;\n let backoff: number;\n if (retryAfterSeconds != null && retryAfterSeconds > 0) {\n // Server-specified delay — use exactly, no jitter (contract compliance).\n backoff = retryAfterSeconds * 1000;\n } else {\n backoff = addJitter(Math.min(initialDelayMs * Math.pow(2, attempt - 1), maxDelayMs));\n }\n\n await delay(backoff, options.signal);\n }\n }\n throw lastError;\n}\n","/**\n * Shared request execution pipeline for the AI Gateway SDK.\n *\n * Handles: auth injection, Bearer token refresh on 401, middleware chain,\n * timeout, retry, and error normalization. Inlines abort, request-id,\n * and fetch transport as private helpers.\n */\n\nimport type { RequestConfig, ResolvedConfig } from './gateway-config';\nimport { AuthError, parseErrorResponseFromResponse } from './gateway-errors';\nimport { withRetry } from './gateway-retry';\n\n// ─── Private helpers ──────────────────────────────────────────────────────────\n\n/** Combine multiple AbortSignals — aborts when ANY fires. Uses native AbortSignal.any when available. */\nfunction anySignal(signals: AbortSignal[]): AbortSignal {\n const any = (AbortSignal as unknown as { any?: (signals: AbortSignal[]) => AbortSignal }).any;\n if (typeof any === 'function') {\n return any(signals);\n }\n const controller = new AbortController();\n const handlers: Array<[AbortSignal, () => void]> = [];\n function cleanup() {\n for (const [sig, handler] of handlers) sig.removeEventListener('abort', handler);\n handlers.length = 0;\n }\n for (const signal of signals) {\n if (signal.aborted) {\n controller.abort(signal.reason);\n return controller.signal;\n }\n const handler = () => {\n cleanup();\n controller.abort(signal.reason);\n };\n handlers.push([signal, handler]);\n signal.addEventListener('abort', handler, { once: true });\n }\n controller.signal.addEventListener('abort', cleanup, { once: true });\n return controller.signal;\n}\n\nlet _counter = 0;\n/** Generate a unique request ID for correlation. */\nfunction generateRequestId(prefix: string = 'sdk'): string {\n if (typeof globalThis.crypto?.randomUUID === 'function') {\n return `${prefix}-${globalThis.crypto.randomUUID()}`;\n }\n const timestamp = Date.now().toString(36);\n const count = (_counter++).toString(36);\n const random = Math.random().toString(36).slice(2, 8);\n return `${prefix}-${timestamp}-${count}-${random}`;\n}\n\n/** Case-insensitive header key lookup. */\nfunction hasHeaderCaseInsensitive(headers: Record<string, string>, name: string): boolean {\n const lower = name.toLowerCase();\n return Object.keys(headers).some((k) => k.toLowerCase() === lower);\n}\n\nfunction shouldAutoSetJsonContentType(body: RequestInit['body'], headers: Record<string, string>): boolean {\n if (body == null || hasHeaderCaseInsensitive(headers, 'content-type')) return false;\n const isFormData = typeof FormData !== 'undefined' && body instanceof FormData;\n const isBlob = typeof Blob !== 'undefined' && body instanceof Blob;\n return !isFormData && !isBlob;\n}\n\nfunction redactSensitiveHeaders(headers: Record<string, string>): Record<string, string> {\n const redacted: Record<string, string> = {};\n for (const [key, value] of Object.entries(headers)) {\n redacted[key] = key.toLowerCase() === 'authorization' ? '[REDACTED]' : value;\n }\n return redacted;\n}\n\nconst NODE_NETWORK_CODES = new Set([\n 'ECONNREFUSED',\n 'ECONNRESET',\n 'ENOTFOUND',\n 'EPIPE',\n 'ETIMEDOUT',\n 'ENETUNREACH',\n 'EAI_AGAIN',\n 'UND_ERR_CONNECT_TIMEOUT',\n]);\n\nconst FETCH_NETWORK_TYPEERROR_HINTS = [\n 'failed to fetch',\n 'fetch failed',\n 'load failed',\n 'networkerror',\n 'network error when attempting to fetch',\n];\n\nfunction hasRetryableNodeErrorCode(err: unknown): boolean {\n let cur: unknown = err;\n for (let depth = 0; depth < 5 && cur != null; depth++) {\n const code = (cur as { code?: string })?.code;\n if (typeof code === 'string' && NODE_NETWORK_CODES.has(code)) return true;\n cur = cur instanceof Error && cur.cause !== undefined ? cur.cause : undefined;\n }\n return false;\n}\n\nfunction isFetchFailureTypeError(err: unknown): boolean {\n if (!(err instanceof TypeError)) return false;\n const msg = String(err.message).toLowerCase();\n return FETCH_NETWORK_TYPEERROR_HINTS.some((hint) => msg.includes(hint));\n}\n\nfunction isNetworkError(err: unknown): boolean {\n return hasRetryableNodeErrorCode(err) || isFetchFailureTypeError(err);\n}\n\n/** Execute a single HTTP request using the configured fetch implementation. */\nasync function executeFetch(fetchImpl: typeof fetch | undefined, requestConfig: RequestConfig): Promise<Response> {\n const impl = fetchImpl ?? globalThis.fetch;\n if (typeof impl === 'undefined') {\n throw new Error(\n '@macpaw/ai-sdk requires a global `fetch` implementation. ' +\n 'Use Node.js 18+ or install a polyfill like `undici`.',\n );\n }\n return impl(requestConfig.url, {\n method: requestConfig.method,\n headers: requestConfig.headers,\n body: requestConfig.body,\n signal: requestConfig.signal,\n });\n}\n\n// ─── Public types ─────────────────────────────────────────────────────────────\n\nexport interface ExecuteRequestInput {\n url: string;\n method: string;\n body?: RequestInit['body'];\n headers?: Record<string, string>;\n signal?: AbortSignal;\n}\n\nexport interface ExecuteRequestBehavior {\n /** Merge config-level default headers before per-request headers. */\n includeConfigHeaders?: boolean;\n /** Resolve and inject Bearer auth via `getAuthToken()`. */\n includeAuth?: boolean;\n /** Auto-add `X-Request-ID` when missing. */\n includeRequestId?: boolean;\n /** Convert non-OK responses into normalized gateway errors. */\n normalizeErrors?: boolean;\n /** Retry once on auth failure by forcing a fresh token. */\n allowAuthRetry?: boolean;\n /** Request ID prefix for correlation, e.g. `sdk` or `provider`. */\n requestIdPrefix?: string;\n}\n\n// ─── Pipeline ─────────────────────────────────────────────────────────────────\n\nexport async function executeRequestPipeline(\n config: ResolvedConfig,\n request: ExecuteRequestInput,\n behavior?: ExecuteRequestBehavior,\n): Promise<Response> {\n const b: Required<ExecuteRequestBehavior> = {\n includeConfigHeaders: behavior?.includeConfigHeaders ?? true,\n includeAuth: behavior?.includeAuth ?? true,\n includeRequestId: behavior?.includeRequestId ?? true,\n normalizeErrors: behavior?.normalizeErrors ?? true,\n allowAuthRetry: behavior?.allowAuthRetry ?? true,\n requestIdPrefix: behavior?.requestIdPrefix ?? 'sdk',\n };\n return executeWithAuth(config, request, b, false);\n}\n\nasync function executeWithAuth(\n config: ResolvedConfig,\n request: ExecuteRequestInput,\n behavior: Required<ExecuteRequestBehavior>,\n isTokenRetry: boolean,\n): Promise<Response> {\n try {\n return await executeRequest(config, request, behavior, isTokenRetry);\n } catch (err) {\n if (behavior.allowAuthRetry && !isTokenRetry && err instanceof AuthError) {\n // ReadableStream bodies are single-consumer: once the first request reads\n // the stream it is consumed and cannot be replayed on the retry attempt.\n if (typeof ReadableStream !== 'undefined' && request.body instanceof ReadableStream) {\n throw err;\n }\n return executeWithAuth(config, request, behavior, true);\n }\n throw err;\n }\n}\n\nasync function executeRequest(\n config: ResolvedConfig,\n request: ExecuteRequestInput,\n behavior: Required<ExecuteRequestBehavior>,\n forceRefreshToken: boolean,\n): Promise<Response> {\n const headers: Record<string, string> = {\n ...(behavior.includeConfigHeaders ? config.headers : undefined),\n ...request.headers,\n };\n\n if (shouldAutoSetJsonContentType(request.body, headers)) {\n headers['Content-Type'] = 'application/json';\n }\n\n if (behavior.includeRequestId && !hasHeaderCaseInsensitive(headers, 'x-request-id')) {\n headers['X-Request-ID'] = generateRequestId(behavior.requestIdPrefix);\n }\n\n const timeoutMs = config.timeout;\n const userSignal = request.signal;\n\n async function doRequest(): Promise<Response> {\n const timeoutController = new AbortController();\n const timeoutId = setTimeout(\n () => timeoutController.abort(new Error(`Request timed out after ${timeoutMs}ms`)),\n timeoutMs,\n );\n\n const signal = userSignal ? anySignal([userSignal, timeoutController.signal]) : timeoutController.signal;\n\n const requestConfig: RequestConfig = {\n url: request.url,\n method: request.method,\n headers: { ...headers },\n body: request.body,\n signal,\n };\n\n const { middleware } = config;\n let index = 0;\n\n const next = async (currentRequest: RequestConfig): Promise<Response> => {\n if (index < middleware.length) {\n const middlewareItem = middleware[index++];\n return middlewareItem(currentRequest, next);\n }\n const finalHeaders = { ...currentRequest.headers };\n if (behavior.includeAuth) {\n const token = await config.getAuthToken(forceRefreshToken);\n if (token) {\n finalHeaders.Authorization = `Bearer ${token}`;\n } else {\n for (const key of Object.keys(finalHeaders)) {\n if (key.toLowerCase() === 'authorization') delete finalHeaders[key];\n }\n }\n }\n return executeFetch(config.fetchImpl, { ...currentRequest, headers: finalHeaders });\n };\n\n try {\n const response = await next(requestConfig);\n\n if (!response.ok) {\n const retryableStatuses = config.retry !== false ? config.retry.retryableStatuses : [];\n // Transport logic must fire regardless of normalizeErrors:\n // - 401 must throw AuthError so executeWithAuth can refresh the token\n // - retryable statuses must throw so withRetry can schedule retry attempts\n const isTransportCritical =\n (behavior.allowAuthRetry && response.status === 401) ||\n (config.retry !== false && retryableStatuses.includes(response.status));\n\n if (behavior.normalizeErrors || isTransportCritical) {\n await parseErrorResponseFromResponse(response);\n }\n }\n\n return response;\n } finally {\n clearTimeout(timeoutId);\n }\n }\n\n if (config.retry) {\n // doRequest() re-runs the full middleware chain on each retry attempt.\n // Middleware with side effects (metrics, audit logs) will fire per attempt.\n return withRetry(doRequest, {\n retryConfig: config.retry,\n signal: userSignal,\n isNetworkError,\n });\n }\n\n return doRequest();\n}\n\n/** Redact Authorization for safe logging/debugging. */\nexport { redactSensitiveHeaders };\n","/**\n * Custom fetch factory for use with Vercel AI SDK and other OpenAI-compatible clients.\n * Use this with createOpenAI({ baseURL, fetch: createGatewayFetch(...) }) or similar.\n *\n * AI Gateway HTTP paths are under /api/v1 (e.g. /api/v1/chat/completions).\n * So baseURL should be the gateway root, e.g. https://api.macpaw.com/ai\n *\n * Internally delegates to the shared request pipeline while guarding against\n * leaking gateway auth/headers to non-gateway hosts.\n */\n\nimport type { GatewayProviderSettings } from './gateway-config';\nimport { resolveConfig } from './gateway-config';\nimport { executeRequestPipeline } from './gateway-request';\n\n/**\n * Config for `createGatewayFetch`.\n * Extends GatewayProviderSettings with baseURL required (already resolved)\n * and normalizeErrors (provider-specific behavior).\n */\nexport interface GatewayFetchConfig extends GatewayProviderSettings {\n /** Resolved Gateway base URL (required — use resolveGatewayBaseURL first). */\n baseURL: string;\n /**\n * Normalize gateway error responses into `AIGatewayError`. Default: true.\n * When enabled, non-OK gateway responses throw instead of returning a failed Response.\n */\n normalizeErrors?: boolean;\n}\n\ntype FetchInput = string | URL | Request | { url: string };\n\nfunction resolveRequestUrl(input: FetchInput): string {\n if (typeof input === 'string') return input;\n if (input instanceof URL) return input.href;\n return input.url;\n}\n\nfunction stripPlaceholderAuthorization(headers: Headers, placeholder: string): void {\n const auth = headers.get('authorization');\n if (auth === `Bearer ${placeholder}`) {\n headers.delete('authorization');\n }\n}\n\nfunction headersToRecord(headers: Headers): Record<string, string> {\n const record: Record<string, string> = {};\n for (const [key, value] of headers.entries()) {\n record[key] = value;\n }\n return record;\n}\n\nfunction joinBaseUrl(baseURL: string, path: string): string {\n return `${baseURL}${path.startsWith('/') ? '' : '/'}${path}`;\n}\n\nfunction isGatewayUrl(url: URL, gatewayBaseUrl: URL): boolean {\n const gatewayPath = gatewayBaseUrl.pathname.replace(/\\/$/, '');\n const requestPath = url.pathname.replace(/\\/$/, '');\n return (\n url.origin === gatewayBaseUrl.origin && (requestPath === gatewayPath || requestPath.startsWith(`${gatewayPath}/`))\n );\n}\n\nexport const GATEWAY_PLACEHOLDER_API_KEY = 'ai-gateway-auth-via-fetch';\n\nexport function createGatewayFetch(\n options: GatewayFetchConfig,\n): (input: FetchInput, init?: RequestInit) => Promise<Response> {\n const { baseURL, normalizeErrors = true } = options;\n const base = baseURL.replace(/\\/$/, '');\n const gatewayBaseUrl = new URL(base);\n const resolvedConfig = resolveConfig({ ...options, baseURL: base });\n\n return async function gatewayFetch(input: FetchInput, init?: RequestInit): Promise<Response> {\n const rawUrl = resolveRequestUrl(input);\n const isAbsolute = rawUrl.startsWith('http://') || rawUrl.startsWith('https://');\n const resolvedUrl = new URL(isAbsolute ? rawUrl : joinBaseUrl(base, rawUrl));\n const isGatewayRequest = isGatewayUrl(resolvedUrl, gatewayBaseUrl);\n\n const request = typeof Request !== 'undefined' && input instanceof Request ? input : undefined;\n const requestClone = request?.clone();\n const headers = new Headers(requestClone?.headers);\n\n if (init?.headers) {\n for (const [key, value] of new Headers(init.headers).entries()) {\n headers.set(key, value);\n }\n }\n\n if (!isGatewayRequest) {\n stripPlaceholderAuthorization(headers, GATEWAY_PLACEHOLDER_API_KEY);\n }\n\n return executeRequestPipeline(\n resolvedConfig,\n {\n url: resolvedUrl.toString(),\n method: init?.method ?? requestClone?.method ?? 'GET',\n headers: headersToRecord(headers),\n body: init?.body ?? requestClone?.body,\n signal: init?.signal ?? requestClone?.signal,\n },\n {\n includeConfigHeaders: isGatewayRequest,\n includeAuth: isGatewayRequest,\n includeRequestId: isGatewayRequest,\n normalizeErrors: isGatewayRequest && normalizeErrors,\n allowAuthRetry: isGatewayRequest,\n requestIdPrefix: 'provider',\n },\n );\n };\n}\n","/**\n * AI Gateway provider factory.\n *\n * `createGatewayProvider()` routes bare model IDs through the gateway's\n * OpenAI-compatible endpoint with provider-specific prefixes.\n * `createAIGatewayProvider()` is the low-level escape hatch for direct\n * OpenAI-compatible provider construction.\n */\n\nimport { createOpenAI as builtinCreateOpenAI } from '@ai-sdk/openai';\nimport type { OpenAIProvider, OpenAIProviderSettings } from '@ai-sdk/openai';\nimport { createGatewayFetch, GATEWAY_PLACEHOLDER_API_KEY } from './gateway-fetch';\nimport type { Environment, GatewayProviderSettings } from './gateway-config';\nimport { resolveGatewayBaseURL } from './gateway-config';\n\n// ─── AIGatewayProvider (low-level) ────────────────────────────────────────────\n\nconst DEFAULT_API_VERSION = 'v1';\n\nexport interface AIGatewayProviderOptions\n extends Omit<OpenAIProviderSettings, 'apiKey' | 'baseURL' | 'fetch'>, GatewayProviderSettings {\n /**\n * Optional override for the OpenAI provider factory.\n * Uses `createOpenAI` from `@ai-sdk/openai` by default.\n */\n createOpenAI?: typeof builtinCreateOpenAI;\n /**\n * Normalize gateway error responses into `AIGatewayError`. Default: true.\n * When enabled, non-OK gateway responses throw instead of returning a failed Response.\n */\n normalizeErrors?: boolean;\n}\n\n/**\n * Creates a Vercel AI SDK-compatible provider backed by AI Gateway.\n *\n * The returned value is a fully typed `OpenAIProvider`, so existing apps can\n * keep using their `ai-sdk` helpers and model-selection patterns.\n */\nexport function createAIGatewayProvider(options: AIGatewayProviderOptions): OpenAIProvider {\n const baseURL = resolveGatewayBaseURL(options.baseURL, options.env as Environment, 'AIGatewayProvider');\n const customFetch = createGatewayFetch({ ...options, baseURL, normalizeErrors: options.normalizeErrors });\n\n const openAI = options.createOpenAI ?? builtinCreateOpenAI;\n\n return openAI({\n name: options.name,\n organization: options.organization,\n project: options.project,\n baseURL: `${baseURL.replace(/\\/$/, '')}/api/${DEFAULT_API_VERSION}`,\n fetch: customFetch,\n apiKey: GATEWAY_PLACEHOLDER_API_KEY,\n });\n}\n\n// ─── GatewayProvider (prefixed routing) ───────────────────────────────────────\n\nexport interface GatewayProviderBaseOptions extends AIGatewayProviderOptions {\n /**\n * Override the model ID prefix used for Gateway routing.\n * Default: the provider's canonical prefix (e.g. `'anthropic'`).\n *\n * Model IDs that already contain `/` are sent as-is.\n */\n modelPrefix?: string;\n}\n\nexport interface GatewayOpenAICompatibleOptions extends Omit<GatewayProviderBaseOptions, 'modelPrefix'> {\n /**\n * Required for openai-compatible backends because there is no single canonical\n * default prefix. Examples: `openai`, `fireworks_ai`, `openrouter`.\n */\n modelPrefix: string;\n}\n\nexport const GATEWAY_PROVIDERS = {\n ANTHROPIC: 'anthropic',\n GOOGLE: 'google',\n XAI: 'xai',\n GROQ: 'groq',\n MISTRAL: 'mistral',\n AMAZON_BEDROCK: 'amazon-bedrock',\n AZURE: 'azure',\n COHERE: 'cohere',\n PERPLEXITY: 'perplexity',\n DEEPSEEK: 'deepseek',\n TOGETHERAI: 'togetherai',\n OPENAI_COMPATIBLE: 'openai-compatible',\n} as const;\n\nexport type GatewayProvider = (typeof GATEWAY_PROVIDERS)[keyof typeof GATEWAY_PROVIDERS];\n\nexport type GatewayProviderWithDefaultPrefix = Exclude<GatewayProvider, typeof GATEWAY_PROVIDERS.OPENAI_COMPATIBLE>;\n\nexport interface GatewayProviderOptionsMap {\n [GATEWAY_PROVIDERS.OPENAI_COMPATIBLE]: GatewayOpenAICompatibleOptions;\n}\n\nexport type GatewayProviderOptions<P extends GatewayProvider> = P extends keyof GatewayProviderOptionsMap\n ? GatewayProviderOptionsMap[P]\n : GatewayProviderBaseOptions;\n\nconst GATEWAY_PROVIDER_DEFAULT_PREFIXES: Record<GatewayProviderWithDefaultPrefix, string> = {\n [GATEWAY_PROVIDERS.ANTHROPIC]: 'anthropic',\n [GATEWAY_PROVIDERS.GOOGLE]: 'google',\n [GATEWAY_PROVIDERS.XAI]: 'xai',\n [GATEWAY_PROVIDERS.GROQ]: 'groq',\n [GATEWAY_PROVIDERS.MISTRAL]: 'mistral',\n [GATEWAY_PROVIDERS.AMAZON_BEDROCK]: 'bedrock',\n [GATEWAY_PROVIDERS.AZURE]: 'azure',\n [GATEWAY_PROVIDERS.COHERE]: 'cohere',\n [GATEWAY_PROVIDERS.PERPLEXITY]: 'perplexity',\n [GATEWAY_PROVIDERS.DEEPSEEK]: 'deepseek',\n [GATEWAY_PROVIDERS.TOGETHERAI]: 'togetherai',\n};\n\nfunction prefixModelId(prefix: string, modelId: string): string {\n return modelId.includes('/') ? modelId : `${prefix}/${modelId}`;\n}\n\nfunction createPrefixedGatewayProvider(defaultPrefix: string, options: GatewayProviderBaseOptions): OpenAIProvider {\n const { modelPrefix, ...providerOptions } = options;\n const prefix = modelPrefix ?? defaultPrefix;\n const provider = createAIGatewayProvider(providerOptions);\n\n const wrapped = ((modelId: string) => provider(prefixModelId(prefix, modelId))) as OpenAIProvider;\n\n return new Proxy(wrapped, {\n apply(_target, _thisArg, args: [string, ...unknown[]]) {\n const [modelId, ...rest] = args;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (provider as (...a: unknown[]) => any)(prefixModelId(prefix, modelId), ...rest);\n },\n get(_target, prop) {\n const value = Reflect.get(provider, prop, provider);\n if (typeof value === 'function') {\n return (...args: unknown[]) => {\n if (typeof args[0] === 'string') {\n args[0] = prefixModelId(prefix, args[0] as string);\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (value as (...a: unknown[]) => any).apply(provider, args);\n };\n }\n return value;\n },\n has(_target, prop) {\n return prop in (provider as object);\n },\n });\n}\n\n/**\n * Creates an `OpenAIProvider` backed by AI Gateway for a supported provider.\n *\n * Bare model IDs are automatically prefixed with the provider's canonical\n * Gateway routing prefix. Model IDs that already contain `/` are passed as-is.\n */\nexport function createGatewayProvider(\n provider: GatewayProviderWithDefaultPrefix,\n options: GatewayProviderBaseOptions,\n): OpenAIProvider;\nexport function createGatewayProvider(\n provider: typeof GATEWAY_PROVIDERS.OPENAI_COMPATIBLE,\n options: GatewayOpenAICompatibleOptions,\n): OpenAIProvider;\nexport function createGatewayProvider(\n provider: GatewayProvider,\n options: GatewayProviderBaseOptions | GatewayOpenAICompatibleOptions,\n): OpenAIProvider {\n if (provider === GATEWAY_PROVIDERS.OPENAI_COMPATIBLE) {\n return createPrefixedGatewayProvider(\n (options as GatewayOpenAICompatibleOptions).modelPrefix,\n options as GatewayOpenAICompatibleOptions,\n );\n }\n\n return createPrefixedGatewayProvider(\n GATEWAY_PROVIDER_DEFAULT_PREFIXES[provider as GatewayProviderWithDefaultPrefix],\n options as GatewayProviderBaseOptions,\n );\n}\n"]}
1
+ {"version":3,"sources":["../src/gateway-config.ts","../src/gateway-retry.ts","../src/gateway-request.ts","../src/gateway-fetch.ts","../src/gateway-provider.ts"],"names":["AuthError","parseErrorResponseFromResponse","builtinCreateOpenAI"],"mappings":";;;;;;AAyBO,IAAM,aAAA,GAAuC;AAAA,EAClD,WAAA,EAAa,CAAA;AAAA,EACb,cAAA,EAAgB,GAAA;AAAA,EAChB,UAAA,EAAY,GAAA;AAAA,EACZ,mBAAmB,CAAC,GAAA,EAAK,GAAA,EAAK,GAAA,EAAK,KAAK,GAAG;AAC7C,CAAA;AAGO,SAAS,qBAAqB,MAAA,EAAsD;AACzF,EAAA,MAAM,IAAI,IAAA,CAAK,KAAA,CAAM,MAAA,CAAO,MAAA,CAAO,W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AI,SAAS,IAAA,EAAM;AACjB,MAAA,MAAM,KAAA,GAAQ,OAAA,CAAQ,GAAA,CAAI,QAAA,EAAU,MAAM,QAAQ,CAAA;AAClD,MAAA,IAAI,OAAO,UAAU,UAAA,EAAY;AAC/B,QAAA,OAAO,IAAI,IAAA,KAAoB;AAC7B,UAAA,IAAI,OAAO,IAAA,CAAK,CAAC,CAAA,KAAM,QAAA,EAAU;AAC/B,YAAA,IAAA,CAAK,CAAC,CAAA,GAAI,aAAA,CAAc,MAAA,EAAQ,IAAA,CAAK,CAAC,CAAW,CAAA;AAAA,UACnD;AAEA,UAAA,OAAQ,KAAA,CAAmC,KAAA,CAAM,QAAA,EAAU,IAAI,CAAA;AAAA,QACjE,CAAA;AAAA,MACF;AACA,MAAA,OAAO,KAAA;AAAA,IACT,CAAA;AAAA,IACA,GAAA,CAAI,SAAS,IAAA,EAAM;AACjB,MAAA,OAAO,IAAA,IAAS,QAAA;AAAA,IAClB;AAAA,GACD,CAAA;AACH;AAgBO,SAAS,qBAAA,CACd,UACA,OAAA,EACgB;AAChB,EAAA,IAAI,QAAA,KAAa,kBAAkB,iBAAA,EAAmB;AACpD,IAAA,OAAO,6BAAA;AAAA,MACJ,OAAA,CAA2C,WAAA;AAAA,MAC5C;AAAA,KACF;AAAA,EACF;AAEA,EAAA,OAAO,6BAAA;AAAA,IACL,kCAAkC,QAA4C,CAAA;AAAA,IAC9E;AAAA,GACF;AACF","file":"index.cjs","sourcesContent":["/**\n * Configuration types and resolver for the AI Gateway SDK.\n *\n * GatewayProviderSettings is the public API — 8 fields.\n * ResolvedConfig is the internal resolved form used by the request pipeline.\n */\n\n/**\n * Supported gateway environments.\n * Only 'production' is exposed — non-production setups use a direct `baseURL`\n * instead, keeping env-specific URL management out of this library.\n */\nexport type Environment = 'production';\n\nexport interface RetryConfig {\n /** Max number of attempts (including first). Default 3. */\n maxAttempts?: number;\n /** Initial delay in ms. Default 1000. */\n initialDelayMs?: number;\n /** Max delay in ms. Default 30000. */\n maxDelayMs?: number;\n /** Retry only on these status codes (and network errors). Default: 429, 5xx. */\n retryableStatuses?: number[];\n}\n\nexport const DEFAULT_RETRY: Required<RetryConfig> = {\n maxAttempts: 3,\n initialDelayMs: 1000,\n maxDelayMs: 30000,\n retryableStatuses: [429, 500, 502, 503, 504],\n};\n\n/** Coerces invalid `maxAttempts` (0, negative, NaN) to the default. */\nexport function normalizeRetryConfig(merged: Required<RetryConfig>): Required<RetryConfig> {\n const n = Math.floor(Number(merged.maxAttempts));\n const maxAttempts = Number.isFinite(n) && n >= 1 ? n : DEFAULT_RETRY.maxAttempts;\n return { ...merged, maxAttempts };\n}\n\nexport interface RequestConfig {\n url: string;\n method: string;\n headers: Record<string, string>;\n body?: RequestInit['body'];\n signal?: AbortSignal | undefined;\n}\n\nexport type Middleware = (\n config: RequestConfig,\n next: (config: RequestConfig) => Promise<Response>,\n) => Promise<Response>;\n\n/**\n * Configuration for AI Gateway providers and NestJS module.\n *\n * Exactly 8 fields — covers auth, routing, request behaviour and middleware.\n */\nexport interface GatewayProviderSettings {\n /** Gateway base URL. Required if env is not set. */\n baseURL?: string;\n /** 'production' uses the default MacPaw Gateway URL. */\n env?: Environment;\n /**\n * Returns Bearer token for auth.\n * Called with `forceRefresh=true` after 401 — provide a fresh token.\n */\n getAuthToken: (forceRefresh?: boolean) => Promise<string | null>;\n /** Extra headers sent with every request. Do not set Authorization here. */\n headers?: Record<string, string>;\n /** Retry policy. Set to false to disable. */\n retry?: RetryConfig | false;\n /** Request timeout in ms. Default: 60000. */\n timeout?: number;\n /** Request interceptors. Run in order before each request. */\n middleware?: Middleware[];\n /**\n * Custom fetch implementation.\n * Use for testing or low-level request customization.\n */\n fetch?: typeof fetch;\n}\n\n/** Internal resolved form — used only by the request pipeline. */\nexport interface ResolvedConfig {\n baseURL: string;\n getAuthToken: (forceRefresh?: boolean) => Promise<string | null>;\n /** Already normalized — all fields present, maxAttempts ≥ 1. */\n retry: Required<RetryConfig> | false;\n middleware: Middleware[];\n headers?: Record<string, string>;\n timeout: number;\n fetchImpl?: typeof fetch;\n}\n\nexport function resolveConfig(config: GatewayProviderSettings & { baseURL: string }): ResolvedConfig {\n return {\n baseURL: config.baseURL,\n getAuthToken: config.getAuthToken,\n retry: config.retry === false ? false : normalizeRetryConfig({ ...DEFAULT_RETRY, ...config.retry }),\n middleware: [...(config.middleware ?? [])],\n headers: config.headers,\n timeout: config.timeout ?? 60_000,\n fetchImpl: config.fetch,\n };\n}\n\n/** Default base URL for the production environment. */\nexport const DEFAULT_BASE_URLS: Record<Environment, string> = {\n production: 'https://api.macpaw.com/ai',\n};\n\nexport function resolveGatewayBaseURL(\n baseURL: string | undefined,\n env: Environment | undefined,\n consumerName: string,\n): string {\n const resolved = baseURL ?? (env ? DEFAULT_BASE_URLS[env] : undefined);\n if (!resolved) {\n throw new Error(\n `${consumerName} requires baseURL or env (production). For non-production environments, pass baseURL directly.`,\n );\n }\n return resolved;\n}\n","/**\n * Retry with exponential backoff + jitter.\n * Only retries on 429, 5xx and network errors; never on 4xx (401, 402, etc.).\n *\n * Jitter prevents thundering herd when many clients retry simultaneously.\n * Respects Retry-After metadata from 429 responses.\n */\n\nimport type { RetryConfig } from './gateway-config';\nimport type { AIGatewayError } from './gateway-errors';\n\nfunction delay(ms: number, signal?: AbortSignal): Promise<void> {\n return new Promise((resolve, reject) => {\n if (signal?.aborted) {\n reject(signal.reason);\n return;\n }\n const timer = setTimeout(resolve, ms);\n signal?.addEventListener(\n 'abort',\n () => {\n clearTimeout(timer);\n reject(signal.reason);\n },\n { once: true },\n );\n });\n}\n\nfunction isRetryableStatus(status: number, retryableStatuses: number[]): boolean {\n return retryableStatuses.includes(status);\n}\n\n/** Add random jitter: 0-25% of the base delay to avoid thundering herd */\nfunction addJitter(delayMs: number): number {\n const jitter = delayMs * 0.25 * Math.random();\n return delayMs + jitter;\n}\n\nexport async function withRetry<T>(\n fn: () => Promise<T>,\n options: {\n /** Already-normalized config — all fields must be present. */\n retryConfig: Required<RetryConfig>;\n signal?: AbortSignal;\n isNetworkError?: (err: unknown) => boolean;\n },\n): Promise<T> {\n const { maxAttempts, initialDelayMs, maxDelayMs, retryableStatuses } = options.retryConfig;\n let lastError: unknown;\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n try {\n return await fn();\n } catch (err) {\n lastError = err;\n if (attempt === maxAttempts) break;\n const status = (err as { statusCode?: number })?.statusCode;\n const isRetryable =\n status != null ? isRetryableStatus(status, retryableStatuses) : (options.isNetworkError?.(err) ?? false);\n if (!isRetryable) throw err;\n\n const retryAfterSeconds = (err as AIGatewayError)?.retryAfter;\n let backoff: number;\n if (retryAfterSeconds != null && retryAfterSeconds > 0) {\n // Server-specified delay — use exactly, no jitter (contract compliance).\n backoff = retryAfterSeconds * 1000;\n } else {\n backoff = addJitter(Math.min(initialDelayMs * Math.pow(2, attempt - 1), maxDelayMs));\n }\n\n await delay(backoff, options.signal);\n }\n }\n throw lastError;\n}\n","/**\n * Shared request execution pipeline for the AI Gateway SDK.\n *\n * Handles: auth injection, Bearer token refresh on 401, middleware chain,\n * timeout, retry, and error normalization. Inlines abort, request-id,\n * and fetch transport as private helpers.\n */\n\nimport type { RequestConfig, ResolvedConfig } from './gateway-config';\nimport { AuthError, parseErrorResponseFromResponse } from './gateway-errors';\nimport { GATEWAY_PLACEHOLDER_API_KEY, normalizeBearerToken } from './gateway-fetch';\nimport { withRetry } from './gateway-retry';\n\n// ─── Private helpers ──────────────────────────────────────────────────────────\n\n/** Combine multiple AbortSignals — aborts when ANY fires. Uses native AbortSignal.any when available. */\nfunction anySignal(signals: AbortSignal[]): AbortSignal {\n const any = (AbortSignal as unknown as { any?: (signals: AbortSignal[]) => AbortSignal }).any;\n if (typeof any === 'function') {\n return any(signals);\n }\n const controller = new AbortController();\n const handlers: Array<[AbortSignal, () => void]> = [];\n function cleanup() {\n for (const [sig, handler] of handlers) sig.removeEventListener('abort', handler);\n handlers.length = 0;\n }\n for (const signal of signals) {\n if (signal.aborted) {\n controller.abort(signal.reason);\n return controller.signal;\n }\n const handler = () => {\n cleanup();\n controller.abort(signal.reason);\n };\n handlers.push([signal, handler]);\n signal.addEventListener('abort', handler, { once: true });\n }\n controller.signal.addEventListener('abort', cleanup, { once: true });\n return controller.signal;\n}\n\nlet _counter = 0;\n/** Generate a unique request ID for correlation. */\nfunction generateRequestId(prefix: string = 'sdk'): string {\n if (typeof globalThis.crypto?.randomUUID === 'function') {\n return `${prefix}-${globalThis.crypto.randomUUID()}`;\n }\n const timestamp = Date.now().toString(36);\n const count = (_counter++).toString(36);\n const random = Math.random().toString(36).slice(2, 8);\n return `${prefix}-${timestamp}-${count}-${random}`;\n}\n\n/** Case-insensitive header key lookup. */\nfunction hasHeaderCaseInsensitive(headers: Record<string, string>, name: string): boolean {\n const lower = name.toLowerCase();\n return Object.keys(headers).some((k) => k.toLowerCase() === lower);\n}\n\n/**\n * Drops every `Authorization` key regardless of casing.\n * Some runtimes would emit two header fields if both `authorization` and `Authorization`\n * exist on the same object; servers then merge them into one value with a comma.\n */\nfunction deleteAllAuthorizationHeaders(headers: Record<string, string>): void {\n for (const key of Object.keys(headers)) {\n if (key.toLowerCase() === 'authorization') delete headers[key];\n }\n}\n\nfunction shouldAutoSetJsonContentType(body: RequestInit['body'], headers: Record<string, string>): boolean {\n if (body == null || hasHeaderCaseInsensitive(headers, 'content-type')) return false;\n const isFormData = typeof FormData !== 'undefined' && body instanceof FormData;\n const isBlob = typeof Blob !== 'undefined' && body instanceof Blob;\n return !isFormData && !isBlob;\n}\n\nfunction redactSensitiveHeaders(headers: Record<string, string>): Record<string, string> {\n const redacted: Record<string, string> = {};\n for (const [key, value] of Object.entries(headers)) {\n redacted[key] = key.toLowerCase() === 'authorization' ? '[REDACTED]' : value;\n }\n return redacted;\n}\n\nconst NODE_NETWORK_CODES = new Set([\n 'ECONNREFUSED',\n 'ECONNRESET',\n 'ENOTFOUND',\n 'EPIPE',\n 'ETIMEDOUT',\n 'ENETUNREACH',\n 'EAI_AGAIN',\n 'UND_ERR_CONNECT_TIMEOUT',\n]);\n\nconst FETCH_NETWORK_TYPEERROR_HINTS = [\n 'failed to fetch',\n 'fetch failed',\n 'load failed',\n 'networkerror',\n 'network error when attempting to fetch',\n];\n\nfunction hasRetryableNodeErrorCode(err: unknown): boolean {\n let cur: unknown = err;\n for (let depth = 0; depth < 5 && cur != null; depth++) {\n const code = (cur as { code?: string })?.code;\n if (typeof code === 'string' && NODE_NETWORK_CODES.has(code)) return true;\n cur = cur instanceof Error && cur.cause !== undefined ? cur.cause : undefined;\n }\n return false;\n}\n\nfunction isFetchFailureTypeError(err: unknown): boolean {\n if (!(err instanceof TypeError)) return false;\n const msg = String(err.message).toLowerCase();\n return FETCH_NETWORK_TYPEERROR_HINTS.some((hint) => msg.includes(hint));\n}\n\nfunction isNetworkError(err: unknown): boolean {\n return hasRetryableNodeErrorCode(err) || isFetchFailureTypeError(err);\n}\n\n/** Execute a single HTTP request using the configured fetch implementation. */\nasync function executeFetch(fetchImpl: typeof fetch | undefined, requestConfig: RequestConfig): Promise<Response> {\n const impl = fetchImpl ?? globalThis.fetch;\n if (typeof impl === 'undefined') {\n throw new Error(\n '@macpaw/ai-sdk requires a global `fetch` implementation. ' +\n 'Use Node.js 18+ or install a polyfill like `undici`.',\n );\n }\n return impl(requestConfig.url, {\n method: requestConfig.method,\n headers: requestConfig.headers,\n body: requestConfig.body,\n signal: requestConfig.signal,\n });\n}\n\n// ─── Public types ─────────────────────────────────────────────────────────────\n\nexport interface ExecuteRequestInput {\n url: string;\n method: string;\n body?: RequestInit['body'];\n headers?: Record<string, string>;\n signal?: AbortSignal;\n}\n\nexport interface ExecuteRequestBehavior {\n /** Merge config-level default headers before per-request headers. */\n includeConfigHeaders?: boolean;\n /** Resolve and inject Bearer auth via `getAuthToken()`. */\n includeAuth?: boolean;\n /** Auto-add `X-Request-ID` when missing. */\n includeRequestId?: boolean;\n /** Convert non-OK responses into normalized gateway errors. */\n normalizeErrors?: boolean;\n /** Retry once on auth failure by forcing a fresh token. */\n allowAuthRetry?: boolean;\n /** Request ID prefix for correlation, e.g. `sdk` or `provider`. */\n requestIdPrefix?: string;\n}\n\n// ─── Pipeline ─────────────────────────────────────────────────────────────────\n\nexport async function executeRequestPipeline(\n config: ResolvedConfig,\n request: ExecuteRequestInput,\n behavior?: ExecuteRequestBehavior,\n): Promise<Response> {\n const b: Required<ExecuteRequestBehavior> = {\n includeConfigHeaders: behavior?.includeConfigHeaders ?? true,\n includeAuth: behavior?.includeAuth ?? true,\n includeRequestId: behavior?.includeRequestId ?? true,\n normalizeErrors: behavior?.normalizeErrors ?? true,\n allowAuthRetry: behavior?.allowAuthRetry ?? true,\n requestIdPrefix: behavior?.requestIdPrefix ?? 'sdk',\n };\n return executeWithAuth(config, request, b, false);\n}\n\nasync function executeWithAuth(\n config: ResolvedConfig,\n request: ExecuteRequestInput,\n behavior: Required<ExecuteRequestBehavior>,\n isTokenRetry: boolean,\n): Promise<Response> {\n try {\n return await executeRequest(config, request, behavior, isTokenRetry);\n } catch (err) {\n if (behavior.allowAuthRetry && !isTokenRetry && err instanceof AuthError) {\n // ReadableStream bodies are single-consumer: once the first request reads\n // the stream it is consumed and cannot be replayed on the retry attempt.\n if (typeof ReadableStream !== 'undefined' && request.body instanceof ReadableStream) {\n throw err;\n }\n return executeWithAuth(config, request, behavior, true);\n }\n throw err;\n }\n}\n\nasync function executeRequest(\n config: ResolvedConfig,\n request: ExecuteRequestInput,\n behavior: Required<ExecuteRequestBehavior>,\n forceRefreshToken: boolean,\n): Promise<Response> {\n const headers: Record<string, string> = {\n ...(behavior.includeConfigHeaders ? config.headers : undefined),\n ...request.headers,\n };\n\n if (shouldAutoSetJsonContentType(request.body, headers)) {\n headers['Content-Type'] = 'application/json';\n }\n\n if (behavior.includeRequestId && !hasHeaderCaseInsensitive(headers, 'x-request-id')) {\n headers['X-Request-ID'] = generateRequestId(behavior.requestIdPrefix);\n }\n\n const timeoutMs = config.timeout;\n const userSignal = request.signal;\n\n async function doRequest(): Promise<Response> {\n const timeoutController = new AbortController();\n const timeoutId = setTimeout(\n () => timeoutController.abort(new Error(`Request timed out after ${timeoutMs}ms`)),\n timeoutMs,\n );\n\n const signal = userSignal ? anySignal([userSignal, timeoutController.signal]) : timeoutController.signal;\n\n const requestConfig: RequestConfig = {\n url: request.url,\n method: request.method,\n headers: { ...headers },\n body: request.body,\n signal,\n };\n\n const { middleware } = config;\n let index = 0;\n\n const next = async (currentRequest: RequestConfig): Promise<Response> => {\n if (index < middleware.length) {\n const middlewareItem = middleware[index++];\n return middlewareItem(currentRequest, next);\n }\n const finalHeaders = { ...currentRequest.headers };\n if (behavior.includeAuth) {\n deleteAllAuthorizationHeaders(finalHeaders);\n const rawToken = await config.getAuthToken(forceRefreshToken);\n const token = normalizeBearerToken(rawToken);\n if (token && token !== GATEWAY_PLACEHOLDER_API_KEY) {\n finalHeaders.Authorization = `Bearer ${token}`;\n }\n }\n return executeFetch(config.fetchImpl, { ...currentRequest, headers: finalHeaders });\n };\n\n try {\n const response = await next(requestConfig);\n\n if (!response.ok) {\n const retryableStatuses = config.retry !== false ? config.retry.retryableStatuses : [];\n // Transport logic must fire regardless of normalizeErrors:\n // - 401 must throw AuthError so executeWithAuth can refresh the token\n // - retryable statuses must throw so withRetry can schedule retry attempts\n const isTransportCritical =\n (behavior.allowAuthRetry && response.status === 401) ||\n (config.retry !== false && retryableStatuses.includes(response.status));\n\n if (behavior.normalizeErrors || isTransportCritical) {\n await parseErrorResponseFromResponse(response);\n }\n }\n\n return response;\n } finally {\n clearTimeout(timeoutId);\n }\n }\n\n if (config.retry) {\n // doRequest() re-runs the full middleware chain on each retry attempt.\n // Middleware with side effects (metrics, audit logs) will fire per attempt.\n return withRetry(doRequest, {\n retryConfig: config.retry,\n signal: userSignal,\n isNetworkError,\n });\n }\n\n return doRequest();\n}\n\n/** Redact Authorization for safe logging/debugging. */\nexport { redactSensitiveHeaders };\n","/**\n * Custom fetch factory for use with Vercel AI SDK and other OpenAI-compatible clients.\n * Use this with createOpenAI({ baseURL, fetch: createGatewayFetch(...) }) or similar.\n *\n * AI Gateway HTTP paths are under /ai/v1 (e.g. /ai/v1/chat/completions).\n * So baseURL should be the gateway root, e.g. https://api.macpaw.com/ai\n *\n * Internally delegates to the shared request pipeline while guarding against\n * leaking gateway auth/headers to non-gateway hosts.\n */\n\nimport type { GatewayProviderSettings } from './gateway-config';\nimport { resolveConfig } from './gateway-config';\nimport { executeRequestPipeline } from './gateway-request';\n\nexport const GATEWAY_PLACEHOLDER_API_KEY = 'ai-gateway-auth-via-fetch';\n\n/**\n * Config for `createGatewayFetch`.\n * Extends GatewayProviderSettings with baseURL required (already resolved)\n * and normalizeErrors (provider-specific behavior).\n */\nexport interface GatewayFetchConfig extends GatewayProviderSettings {\n /** Resolved Gateway base URL (required — use resolveGatewayBaseURL first). */\n baseURL: string;\n /**\n * Normalize gateway error responses into `AIGatewayError`. Default: true.\n * When enabled, non-OK gateway responses throw instead of returning a failed Response.\n */\n normalizeErrors?: boolean;\n}\n\ntype FetchInput = string | URL | Request | { url: string };\n\nfunction resolveRequestUrl(input: FetchInput): string {\n if (typeof input === 'string') return input;\n if (input instanceof URL) return input.href;\n return input.url;\n}\n\n/**\n * Trims, strips a single layer of surrounding ASCII quotes, and removes\n * trailing comma/semicolon noise often introduced by .env or copy-paste.\n */\nexport function normalizeBearerToken(raw: string | null | undefined): string | null {\n if (raw == null) return null;\n let s = String(raw).trim();\n if (!s) return null;\n\n if (\n (s.startsWith('\"') && s.endsWith('\"') && s.length >= 2) ||\n (s.startsWith(\"'\") && s.endsWith(\"'\") && s.length >= 2)\n ) {\n s = s.slice(1, -1).trim();\n }\n\n s = s.replace(/[,;]+\\s*$/g, '').trim();\n return s.length > 0 ? s : null;\n}\n\n/**\n * Removes Authorization when it is only the OpenAI apiKey placeholder (optional trailing comma),\n * so a real Bearer from getAuthToken() is not merged with a junk value.\n */\nfunction stripPlaceholderAuthorization(headers: Headers, placeholder: string): void {\n const auth = headers.get('authorization');\n if (!auth) return;\n const m = auth.match(/^Bearer\\s+(\\S+)/i);\n if (!m) return;\n const credential = normalizeBearerToken(m[1]);\n if (credential === placeholder) {\n headers.delete('authorization');\n }\n}\n\nfunction headersToRecord(headers: Headers): Record<string, string> {\n const record: Record<string, string> = {};\n for (const [key, value] of headers.entries()) {\n record[key] = value;\n }\n return record;\n}\n\nfunction joinBaseUrl(baseURL: string, path: string): string {\n return `${baseURL}${path.startsWith('/') ? '' : '/'}${path}`;\n}\n\nfunction isGatewayUrl(url: URL, gatewayBaseUrl: URL): boolean {\n const gatewayPath = gatewayBaseUrl.pathname.replace(/\\/$/, '');\n const requestPath = url.pathname.replace(/\\/$/, '');\n return (\n url.origin === gatewayBaseUrl.origin && (requestPath === gatewayPath || requestPath.startsWith(`${gatewayPath}/`))\n );\n}\n\nexport function createGatewayFetch(\n options: GatewayFetchConfig,\n): (input: FetchInput, init?: RequestInit) => Promise<Response> {\n const { baseURL, normalizeErrors = true } = options;\n const base = baseURL.replace(/\\/$/, '');\n const gatewayBaseUrl = new URL(base);\n const resolvedConfig = resolveConfig({ ...options, baseURL: base });\n\n return async function gatewayFetch(input: FetchInput, init?: RequestInit): Promise<Response> {\n const rawUrl = resolveRequestUrl(input);\n const isAbsolute = rawUrl.startsWith('http://') || rawUrl.startsWith('https://');\n const resolvedUrl = new URL(isAbsolute ? rawUrl : joinBaseUrl(base, rawUrl));\n const isGatewayRequest = isGatewayUrl(resolvedUrl, gatewayBaseUrl);\n\n const request = typeof Request !== 'undefined' && input instanceof Request ? input : undefined;\n const requestClone = request?.clone();\n const headers = new Headers(requestClone?.headers);\n\n if (init?.headers) {\n for (const [key, value] of new Headers(init.headers).entries()) {\n headers.set(key, value);\n }\n }\n\n // Always strip the OpenAI placeholder before entering the shared pipeline.\n // For gateway requests the real Bearer is injected later via getAuthToken();\n // for non-gateway requests we also avoid leaking the sentinel downstream.\n stripPlaceholderAuthorization(headers, GATEWAY_PLACEHOLDER_API_KEY);\n\n return executeRequestPipeline(\n resolvedConfig,\n {\n url: resolvedUrl.toString(),\n method: init?.method ?? requestClone?.method ?? 'GET',\n headers: headersToRecord(headers),\n body: init?.body ?? requestClone?.body,\n signal: init?.signal ?? requestClone?.signal,\n },\n {\n includeConfigHeaders: isGatewayRequest,\n includeAuth: isGatewayRequest,\n includeRequestId: isGatewayRequest,\n normalizeErrors: isGatewayRequest && normalizeErrors,\n allowAuthRetry: isGatewayRequest,\n requestIdPrefix: 'provider',\n },\n );\n };\n}\n","/**\n * AI Gateway provider factory.\n *\n * `createGatewayProvider()` routes bare model IDs through the gateway's\n * OpenAI-compatible endpoint with provider-specific prefixes.\n * `createAIGatewayProvider()` is the low-level escape hatch for direct\n * OpenAI-compatible provider construction.\n */\n\nimport { createOpenAI as builtinCreateOpenAI } from '@ai-sdk/openai';\nimport type { OpenAIProvider, OpenAIProviderSettings } from '@ai-sdk/openai';\nimport { createGatewayFetch, GATEWAY_PLACEHOLDER_API_KEY } from './gateway-fetch';\nimport type { Environment, GatewayProviderSettings } from './gateway-config';\nimport { resolveGatewayBaseURL } from './gateway-config';\n\n// ─── AIGatewayProvider (low-level) ────────────────────────────────────────────\n\nconst DEFAULT_API_VERSION = 'v1';\n\nexport interface AIGatewayProviderOptions\n extends Omit<OpenAIProviderSettings, 'apiKey' | 'baseURL' | 'fetch'>, GatewayProviderSettings {\n /**\n * Optional override for the OpenAI provider factory.\n * Uses `createOpenAI` from `@ai-sdk/openai` by default.\n */\n createOpenAI?: typeof builtinCreateOpenAI;\n /**\n * Normalize gateway error responses into `AIGatewayError`. Default: true.\n * When enabled, non-OK gateway responses throw instead of returning a failed Response.\n */\n normalizeErrors?: boolean;\n}\n\n/**\n * Creates a Vercel AI SDK-compatible provider backed by AI Gateway.\n *\n * The returned value is a fully typed `OpenAIProvider`, so existing apps can\n * keep using their `ai-sdk` helpers and model-selection patterns.\n */\nexport function createAIGatewayProvider(options: AIGatewayProviderOptions): OpenAIProvider {\n const baseURL = resolveGatewayBaseURL(options.baseURL, options.env as Environment, 'AIGatewayProvider');\n const customFetch = createGatewayFetch({ ...options, baseURL, normalizeErrors: options.normalizeErrors });\n\n const openAI = options.createOpenAI ?? builtinCreateOpenAI;\n\n return openAI({\n name: options.name,\n organization: options.organization,\n project: options.project,\n baseURL: `${baseURL.replace(/\\/$/, '')}/${DEFAULT_API_VERSION}`,\n fetch: customFetch,\n apiKey: GATEWAY_PLACEHOLDER_API_KEY,\n });\n}\n\n// ─── GatewayProvider (prefixed routing) ───────────────────────────────────────\n\nexport interface GatewayProviderBaseOptions extends AIGatewayProviderOptions {\n /**\n * Override the model ID prefix used for Gateway routing.\n * Default: the provider's canonical prefix (e.g. `'anthropic'`).\n *\n * Model IDs that already contain `/` are sent as-is.\n */\n modelPrefix?: string;\n}\n\nexport interface GatewayOpenAICompatibleOptions extends Omit<GatewayProviderBaseOptions, 'modelPrefix'> {\n /**\n * Required for openai-compatible backends because there is no single canonical\n * default prefix. Examples: `openai`, `fireworks_ai`, `openrouter`.\n */\n modelPrefix: string;\n}\n\nexport const GATEWAY_PROVIDERS = {\n ANTHROPIC: 'anthropic',\n GOOGLE: 'google',\n XAI: 'xai',\n GROQ: 'groq',\n MISTRAL: 'mistral',\n AMAZON_BEDROCK: 'amazon-bedrock',\n AZURE: 'azure',\n COHERE: 'cohere',\n PERPLEXITY: 'perplexity',\n DEEPSEEK: 'deepseek',\n TOGETHERAI: 'togetherai',\n OPENAI_COMPATIBLE: 'openai-compatible',\n} as const;\n\nexport type GatewayProvider = (typeof GATEWAY_PROVIDERS)[keyof typeof GATEWAY_PROVIDERS];\n\nexport type GatewayProviderWithDefaultPrefix = Exclude<GatewayProvider, typeof GATEWAY_PROVIDERS.OPENAI_COMPATIBLE>;\n\nexport interface GatewayProviderOptionsMap {\n [GATEWAY_PROVIDERS.OPENAI_COMPATIBLE]: GatewayOpenAICompatibleOptions;\n}\n\nexport type GatewayProviderOptions<P extends GatewayProvider> = P extends keyof GatewayProviderOptionsMap\n ? GatewayProviderOptionsMap[P]\n : GatewayProviderBaseOptions;\n\nconst GATEWAY_PROVIDER_DEFAULT_PREFIXES: Record<GatewayProviderWithDefaultPrefix, string> = {\n [GATEWAY_PROVIDERS.ANTHROPIC]: 'anthropic',\n [GATEWAY_PROVIDERS.GOOGLE]: 'google',\n [GATEWAY_PROVIDERS.XAI]: 'xai',\n [GATEWAY_PROVIDERS.GROQ]: 'groq',\n [GATEWAY_PROVIDERS.MISTRAL]: 'mistral',\n [GATEWAY_PROVIDERS.AMAZON_BEDROCK]: 'bedrock',\n [GATEWAY_PROVIDERS.AZURE]: 'azure',\n [GATEWAY_PROVIDERS.COHERE]: 'cohere',\n [GATEWAY_PROVIDERS.PERPLEXITY]: 'perplexity',\n [GATEWAY_PROVIDERS.DEEPSEEK]: 'deepseek',\n [GATEWAY_PROVIDERS.TOGETHERAI]: 'togetherai',\n};\n\nfunction prefixModelId(prefix: string, modelId: string): string {\n return modelId.includes('/') ? modelId : `${prefix}/${modelId}`;\n}\n\nfunction createPrefixedGatewayProvider(defaultPrefix: string, options: GatewayProviderBaseOptions): OpenAIProvider {\n const { modelPrefix, ...providerOptions } = options;\n const prefix = modelPrefix ?? defaultPrefix;\n const provider = createAIGatewayProvider(providerOptions);\n\n const wrapped = ((modelId: string) => provider(prefixModelId(prefix, modelId))) as OpenAIProvider;\n\n return new Proxy(wrapped, {\n apply(_target, _thisArg, args: [string, ...unknown[]]) {\n const [modelId, ...rest] = args;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (provider as (...a: unknown[]) => any)(prefixModelId(prefix, modelId), ...rest);\n },\n get(_target, prop) {\n const value = Reflect.get(provider, prop, provider);\n if (typeof value === 'function') {\n return (...args: unknown[]) => {\n if (typeof args[0] === 'string') {\n args[0] = prefixModelId(prefix, args[0] as string);\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (value as (...a: unknown[]) => any).apply(provider, args);\n };\n }\n return value;\n },\n has(_target, prop) {\n return prop in (provider as object);\n },\n });\n}\n\n/**\n * Creates an `OpenAIProvider` backed by AI Gateway for a supported provider.\n *\n * Bare model IDs are automatically prefixed with the provider's canonical\n * Gateway routing prefix. Model IDs that already contain `/` are passed as-is.\n */\nexport function createGatewayProvider(\n provider: GatewayProviderWithDefaultPrefix,\n options: GatewayProviderBaseOptions,\n): OpenAIProvider;\nexport function createGatewayProvider(\n provider: typeof GATEWAY_PROVIDERS.OPENAI_COMPATIBLE,\n options: GatewayOpenAICompatibleOptions,\n): OpenAIProvider;\nexport function createGatewayProvider(\n provider: GatewayProvider,\n options: GatewayProviderBaseOptions | GatewayOpenAICompatibleOptions,\n): OpenAIProvider {\n if (provider === GATEWAY_PROVIDERS.OPENAI_COMPATIBLE) {\n return createPrefixedGatewayProvider(\n (options as GatewayOpenAICompatibleOptions).modelPrefix,\n options as GatewayOpenAICompatibleOptions,\n );\n }\n\n return createPrefixedGatewayProvider(\n GATEWAY_PROVIDER_DEFAULT_PREFIXES[provider as GatewayProviderWithDefaultPrefix],\n options as GatewayProviderBaseOptions,\n );\n}\n"]}
package/dist/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { OpenAIProviderSettings, createOpenAI, OpenAIProvider } from '@ai-sdk/openai';
2
- import { G as GatewayProviderSettings } from './gateway-errors-DdgDIyQw.cjs';
3
- export { A as AIGatewayError, a as AuthError, C as CreditsError, E as ErrorCode, b as GatewayApiCode, c as GatewayApiErrorItem, d as GatewayApiErrorResponse, e as GatewayValidationError, M as Middleware, f as ModelNotAllowedError, N as NormalizedErrorMetadata, O as OpenAIErrorResponse, R as RateLimitError, g as RetryConfig, i as isAIGatewayError, p as parseErrorResponse } from './gateway-errors-DdgDIyQw.cjs';
2
+ import { G as GatewayProviderSettings } from './gateway-errors-CPtIbrWq.cjs';
3
+ export { A as AIGatewayError, a as AuthError, C as CreditsError, D as DEFAULT_BASE_URLS, E as Environment, b as ErrorCode, c as GatewayApiCode, d as GatewayApiErrorItem, e as GatewayApiErrorResponse, f as GatewayValidationError, M as Middleware, g as ModelNotAllowedError, N as NormalizedErrorMetadata, O as OpenAIErrorResponse, R as RateLimitError, h as RetryConfig, i as isAIGatewayError, p as parseErrorResponse, r as resolveGatewayBaseURL } from './gateway-errors-CPtIbrWq.cjs';
4
4
 
5
5
  /**
6
6
  * AI Gateway provider factory.
@@ -79,13 +79,14 @@ declare function createGatewayProvider(provider: typeof GATEWAY_PROVIDERS.OPENAI
79
79
  * Custom fetch factory for use with Vercel AI SDK and other OpenAI-compatible clients.
80
80
  * Use this with createOpenAI({ baseURL, fetch: createGatewayFetch(...) }) or similar.
81
81
  *
82
- * AI Gateway HTTP paths are under /api/v1 (e.g. /api/v1/chat/completions).
82
+ * AI Gateway HTTP paths are under /ai/v1 (e.g. /ai/v1/chat/completions).
83
83
  * So baseURL should be the gateway root, e.g. https://api.macpaw.com/ai
84
84
  *
85
85
  * Internally delegates to the shared request pipeline while guarding against
86
86
  * leaking gateway auth/headers to non-gateway hosts.
87
87
  */
88
88
 
89
+ declare const GATEWAY_PLACEHOLDER_API_KEY = "ai-gateway-auth-via-fetch";
89
90
  /**
90
91
  * Config for `createGatewayFetch`.
91
92
  * Extends GatewayProviderSettings with baseURL required (already resolved)
@@ -103,7 +104,6 @@ interface GatewayFetchConfig extends GatewayProviderSettings {
103
104
  type FetchInput = string | URL | Request | {
104
105
  url: string;
105
106
  };
106
- declare const GATEWAY_PLACEHOLDER_API_KEY = "ai-gateway-auth-via-fetch";
107
107
  declare function createGatewayFetch(options: GatewayFetchConfig): (input: FetchInput, init?: RequestInit) => Promise<Response>;
108
108
 
109
109
  export { type AIGatewayProviderOptions, GATEWAY_PLACEHOLDER_API_KEY, GATEWAY_PROVIDERS, type GatewayFetchConfig, type GatewayOpenAICompatibleOptions, type GatewayProvider, type GatewayProviderBaseOptions, type GatewayProviderOptions, type GatewayProviderOptionsMap, GatewayProviderSettings, type GatewayProviderWithDefaultPrefix, createAIGatewayProvider, createGatewayFetch, createGatewayProvider };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { OpenAIProviderSettings, createOpenAI, OpenAIProvider } from '@ai-sdk/openai';
2
- import { G as GatewayProviderSettings } from './gateway-errors-DdgDIyQw.js';
3
- export { A as AIGatewayError, a as AuthError, C as CreditsError, E as ErrorCode, b as GatewayApiCode, c as GatewayApiErrorItem, d as GatewayApiErrorResponse, e as GatewayValidationError, M as Middleware, f as ModelNotAllowedError, N as NormalizedErrorMetadata, O as OpenAIErrorResponse, R as RateLimitError, g as RetryConfig, i as isAIGatewayError, p as parseErrorResponse } from './gateway-errors-DdgDIyQw.js';
2
+ import { G as GatewayProviderSettings } from './gateway-errors-CPtIbrWq.js';
3
+ export { A as AIGatewayError, a as AuthError, C as CreditsError, D as DEFAULT_BASE_URLS, E as Environment, b as ErrorCode, c as GatewayApiCode, d as GatewayApiErrorItem, e as GatewayApiErrorResponse, f as GatewayValidationError, M as Middleware, g as ModelNotAllowedError, N as NormalizedErrorMetadata, O as OpenAIErrorResponse, R as RateLimitError, h as RetryConfig, i as isAIGatewayError, p as parseErrorResponse, r as resolveGatewayBaseURL } from './gateway-errors-CPtIbrWq.js';
4
4
 
5
5
  /**
6
6
  * AI Gateway provider factory.
@@ -79,13 +79,14 @@ declare function createGatewayProvider(provider: typeof GATEWAY_PROVIDERS.OPENAI
79
79
  * Custom fetch factory for use with Vercel AI SDK and other OpenAI-compatible clients.
80
80
  * Use this with createOpenAI({ baseURL, fetch: createGatewayFetch(...) }) or similar.
81
81
  *
82
- * AI Gateway HTTP paths are under /api/v1 (e.g. /api/v1/chat/completions).
82
+ * AI Gateway HTTP paths are under /ai/v1 (e.g. /ai/v1/chat/completions).
83
83
  * So baseURL should be the gateway root, e.g. https://api.macpaw.com/ai
84
84
  *
85
85
  * Internally delegates to the shared request pipeline while guarding against
86
86
  * leaking gateway auth/headers to non-gateway hosts.
87
87
  */
88
88
 
89
+ declare const GATEWAY_PLACEHOLDER_API_KEY = "ai-gateway-auth-via-fetch";
89
90
  /**
90
91
  * Config for `createGatewayFetch`.
91
92
  * Extends GatewayProviderSettings with baseURL required (already resolved)
@@ -103,7 +104,6 @@ interface GatewayFetchConfig extends GatewayProviderSettings {
103
104
  type FetchInput = string | URL | Request | {
104
105
  url: string;
105
106
  };
106
- declare const GATEWAY_PLACEHOLDER_API_KEY = "ai-gateway-auth-via-fetch";
107
107
  declare function createGatewayFetch(options: GatewayFetchConfig): (input: FetchInput, init?: RequestInit) => Promise<Response>;
108
108
 
109
109
  export { type AIGatewayProviderOptions, GATEWAY_PLACEHOLDER_API_KEY, GATEWAY_PROVIDERS, type GatewayFetchConfig, type GatewayOpenAICompatibleOptions, type GatewayProvider, type GatewayProviderBaseOptions, type GatewayProviderOptions, type GatewayProviderOptionsMap, GatewayProviderSettings, type GatewayProviderWithDefaultPrefix, createAIGatewayProvider, createGatewayFetch, createGatewayProvider };
package/dist/index.js CHANGED
@@ -129,6 +129,11 @@ function hasHeaderCaseInsensitive(headers, name) {
129
129
  const lower = name.toLowerCase();
130
130
  return Object.keys(headers).some((k) => k.toLowerCase() === lower);
131
131
  }
132
+ function deleteAllAuthorizationHeaders(headers) {
133
+ for (const key of Object.keys(headers)) {
134
+ if (key.toLowerCase() === "authorization") delete headers[key];
135
+ }
136
+ }
132
137
  function shouldAutoSetJsonContentType(body, headers) {
133
138
  if (body == null || hasHeaderCaseInsensitive(headers, "content-type")) return false;
134
139
  const isFormData = typeof FormData !== "undefined" && body instanceof FormData;
@@ -243,13 +248,11 @@ async function executeRequest(config, request, behavior, forceRefreshToken) {
243
248
  }
244
249
  const finalHeaders = { ...currentRequest.headers };
245
250
  if (behavior.includeAuth) {
246
- const token = await config.getAuthToken(forceRefreshToken);
247
- if (token) {
251
+ deleteAllAuthorizationHeaders(finalHeaders);
252
+ const rawToken = await config.getAuthToken(forceRefreshToken);
253
+ const token = normalizeBearerToken(rawToken);
254
+ if (token && token !== GATEWAY_PLACEHOLDER_API_KEY) {
248
255
  finalHeaders.Authorization = `Bearer ${token}`;
249
- } else {
250
- for (const key of Object.keys(finalHeaders)) {
251
- if (key.toLowerCase() === "authorization") delete finalHeaders[key];
252
- }
253
256
  }
254
257
  }
255
258
  return executeFetch(config.fetchImpl, { ...currentRequest, headers: finalHeaders });
@@ -279,14 +282,29 @@ async function executeRequest(config, request, behavior, forceRefreshToken) {
279
282
  }
280
283
 
281
284
  // src/gateway-fetch.ts
285
+ var GATEWAY_PLACEHOLDER_API_KEY = "ai-gateway-auth-via-fetch";
282
286
  function resolveRequestUrl(input) {
283
287
  if (typeof input === "string") return input;
284
288
  if (input instanceof URL) return input.href;
285
289
  return input.url;
286
290
  }
291
+ function normalizeBearerToken(raw) {
292
+ if (raw == null) return null;
293
+ let s = String(raw).trim();
294
+ if (!s) return null;
295
+ if (s.startsWith('"') && s.endsWith('"') && s.length >= 2 || s.startsWith("'") && s.endsWith("'") && s.length >= 2) {
296
+ s = s.slice(1, -1).trim();
297
+ }
298
+ s = s.replace(/[,;]+\s*$/g, "").trim();
299
+ return s.length > 0 ? s : null;
300
+ }
287
301
  function stripPlaceholderAuthorization(headers, placeholder) {
288
302
  const auth = headers.get("authorization");
289
- if (auth === `Bearer ${placeholder}`) {
303
+ if (!auth) return;
304
+ const m = auth.match(/^Bearer\s+(\S+)/i);
305
+ if (!m) return;
306
+ const credential = normalizeBearerToken(m[1]);
307
+ if (credential === placeholder) {
290
308
  headers.delete("authorization");
291
309
  }
292
310
  }
@@ -305,7 +323,6 @@ function isGatewayUrl(url, gatewayBaseUrl) {
305
323
  const requestPath = url.pathname.replace(/\/$/, "");
306
324
  return url.origin === gatewayBaseUrl.origin && (requestPath === gatewayPath || requestPath.startsWith(`${gatewayPath}/`));
307
325
  }
308
- var GATEWAY_PLACEHOLDER_API_KEY = "ai-gateway-auth-via-fetch";
309
326
  function createGatewayFetch(options) {
310
327
  const { baseURL, normalizeErrors = true } = options;
311
328
  const base = baseURL.replace(/\/$/, "");
@@ -324,9 +341,7 @@ function createGatewayFetch(options) {
324
341
  headers.set(key, value);
325
342
  }
326
343
  }
327
- if (!isGatewayRequest) {
328
- stripPlaceholderAuthorization(headers, GATEWAY_PLACEHOLDER_API_KEY);
329
- }
344
+ stripPlaceholderAuthorization(headers, GATEWAY_PLACEHOLDER_API_KEY);
330
345
  return executeRequestPipeline(
331
346
  resolvedConfig,
332
347
  {
@@ -358,7 +373,7 @@ function createAIGatewayProvider(options) {
358
373
  name: options.name,
359
374
  organization: options.organization,
360
375
  project: options.project,
361
- baseURL: `${baseURL.replace(/\/$/, "")}/api/${DEFAULT_API_VERSION}`,
376
+ baseURL: `${baseURL.replace(/\/$/, "")}/${DEFAULT_API_VERSION}`,
362
377
  fetch: customFetch,
363
378
  apiKey: GATEWAY_PLACEHOLDER_API_KEY
364
379
  });
@@ -433,6 +448,6 @@ function createGatewayProvider(provider, options) {
433
448
  );
434
449
  }
435
450
 
436
- export { GATEWAY_PLACEHOLDER_API_KEY, GATEWAY_PROVIDERS, createAIGatewayProvider, createGatewayFetch, createGatewayProvider };
451
+ export { DEFAULT_BASE_URLS, GATEWAY_PLACEHOLDER_API_KEY, GATEWAY_PROVIDERS, createAIGatewayProvider, createGatewayFetch, createGatewayProvider, resolveGatewayBaseURL };
437
452
  //# sourceMappingURL=index.js.map
438
453
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/gateway-config.ts","../src/gateway-retry.ts","../src/gateway-request.ts","../src/gateway-fetch.ts","../src/gateway-provider.ts"],"names":["builtinCreateOpenAI"],"mappings":";;;;;AAyBO,IAAM,aAAA,GAAuC;AAAA,EAClD,WAAA,EAAa,CAAA;AAAA,EACb,cAAA,EAAgB,GAAA;AAAA,EAChB,UAAA,EAAY,GAAA;AAAA,EACZ,mBAAmB,CAAC,GAAA,EAAK,GAAA,EAAK,GAAA,EAAK,KAAK,GAAG;AAC7C,CAAA;AAGO,SAAS,qBAAqB,MAAA,EAAsD;AACzF,EAAA,MAAM,IAAI,IAAA,CAAK,KAAA,CAAM,MAAA,CAAO,MAAA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*\n * GatewayProviderSettings is the public API — 8 fields.\n * ResolvedConfig is the internal resolved form used by the request pipeline.\n */\n\n/**\n * Supported gateway environments.\n * Only 'production' is exposed — non-production setups use a direct `baseURL`\n * instead, keeping env-specific URL management out of this library.\n */\nexport type Environment = 'production';\n\nexport interface RetryConfig {\n /** Max number of attempts (including first). Default 3. */\n maxAttempts?: number;\n /** Initial delay in ms. Default 1000. */\n initialDelayMs?: number;\n /** Max delay in ms. Default 30000. */\n maxDelayMs?: number;\n /** Retry only on these status codes (and network errors). Default: 429, 5xx. */\n retryableStatuses?: number[];\n}\n\nexport const DEFAULT_RETRY: Required<RetryConfig> = {\n maxAttempts: 3,\n initialDelayMs: 1000,\n maxDelayMs: 30000,\n retryableStatuses: [429, 500, 502, 503, 504],\n};\n\n/** Coerces invalid `maxAttempts` (0, negative, NaN) to the default. */\nexport function normalizeRetryConfig(merged: Required<RetryConfig>): Required<RetryConfig> {\n const n = Math.floor(Number(merged.maxAttempts));\n const maxAttempts = Number.isFinite(n) && n >= 1 ? n : DEFAULT_RETRY.maxAttempts;\n return { ...merged, maxAttempts };\n}\n\nexport interface RequestConfig {\n url: string;\n method: string;\n headers: Record<string, string>;\n body?: RequestInit['body'];\n signal?: AbortSignal | undefined;\n}\n\nexport type Middleware = (\n config: RequestConfig,\n next: (config: RequestConfig) => Promise<Response>,\n) => Promise<Response>;\n\n/**\n * Configuration for AI Gateway providers and NestJS module.\n *\n * Exactly 8 fields — covers auth, routing, request behaviour and middleware.\n */\nexport interface GatewayProviderSettings {\n /** Gateway base URL. Required if env is not set. */\n baseURL?: string;\n /** 'production' uses the default MacPaw Gateway URL. */\n env?: Environment;\n /**\n * Returns Bearer token for auth.\n * Called with `forceRefresh=true` after 401 — provide a fresh token.\n */\n getAuthToken: (forceRefresh?: boolean) => Promise<string | null>;\n /** Extra headers sent with every request. Do not set Authorization here. */\n headers?: Record<string, string>;\n /** Retry policy. Set to false to disable. */\n retry?: RetryConfig | false;\n /** Request timeout in ms. Default: 60000. */\n timeout?: number;\n /** Request interceptors. Run in order before each request. */\n middleware?: Middleware[];\n /**\n * Custom fetch implementation.\n * Use for testing or low-level request customization.\n */\n fetch?: typeof fetch;\n}\n\n/** Internal resolved form — used only by the request pipeline. */\nexport interface ResolvedConfig {\n baseURL: string;\n getAuthToken: (forceRefresh?: boolean) => Promise<string | null>;\n /** Already normalized — all fields present, maxAttempts ≥ 1. */\n retry: Required<RetryConfig> | false;\n middleware: Middleware[];\n headers?: Record<string, string>;\n timeout: number;\n fetchImpl?: typeof fetch;\n}\n\nexport function resolveConfig(config: GatewayProviderSettings & { baseURL: string }): ResolvedConfig {\n return {\n baseURL: config.baseURL,\n getAuthToken: config.getAuthToken,\n retry: config.retry === false ? false : normalizeRetryConfig({ ...DEFAULT_RETRY, ...config.retry }),\n middleware: [...(config.middleware ?? [])],\n headers: config.headers,\n timeout: config.timeout ?? 60_000,\n fetchImpl: config.fetch,\n };\n}\n\n/** Default base URL for the production environment. */\nexport const DEFAULT_BASE_URLS: Record<Environment, string> = {\n production: 'https://api.macpaw.com/ai',\n};\n\nexport function resolveGatewayBaseURL(\n baseURL: string | undefined,\n env: Environment | undefined,\n consumerName: string,\n): string {\n const resolved = baseURL ?? (env ? DEFAULT_BASE_URLS[env] : undefined);\n if (!resolved) {\n throw new Error(\n `${consumerName} requires baseURL or env (production). For non-production environments, pass baseURL directly.`,\n );\n }\n return resolved;\n}\n","/**\n * Retry with exponential backoff + jitter.\n * Only retries on 429, 5xx and network errors; never on 4xx (401, 402, etc.).\n *\n * Jitter prevents thundering herd when many clients retry simultaneously.\n * Respects Retry-After metadata from 429 responses.\n */\n\nimport type { RetryConfig } from './gateway-config';\nimport type { AIGatewayError } from './gateway-errors';\n\nfunction delay(ms: number, signal?: AbortSignal): Promise<void> {\n return new Promise((resolve, reject) => {\n if (signal?.aborted) {\n reject(signal.reason);\n return;\n }\n const timer = setTimeout(resolve, ms);\n signal?.addEventListener(\n 'abort',\n () => {\n clearTimeout(timer);\n reject(signal.reason);\n },\n { once: true },\n );\n });\n}\n\nfunction isRetryableStatus(status: number, retryableStatuses: number[]): boolean {\n return retryableStatuses.includes(status);\n}\n\n/** Add random jitter: 0-25% of the base delay to avoid thundering herd */\nfunction addJitter(delayMs: number): number {\n const jitter = delayMs * 0.25 * Math.random();\n return delayMs + jitter;\n}\n\nexport async function withRetry<T>(\n fn: () => Promise<T>,\n options: {\n /** Already-normalized config — all fields must be present. */\n retryConfig: Required<RetryConfig>;\n signal?: AbortSignal;\n isNetworkError?: (err: unknown) => boolean;\n },\n): Promise<T> {\n const { maxAttempts, initialDelayMs, maxDelayMs, retryableStatuses } = options.retryConfig;\n let lastError: unknown;\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n try {\n return await fn();\n } catch (err) {\n lastError = err;\n if (attempt === maxAttempts) break;\n const status = (err as { statusCode?: number })?.statusCode;\n const isRetryable =\n status != null ? isRetryableStatus(status, retryableStatuses) : (options.isNetworkError?.(err) ?? false);\n if (!isRetryable) throw err;\n\n const retryAfterSeconds = (err as AIGatewayError)?.retryAfter;\n let backoff: number;\n if (retryAfterSeconds != null && retryAfterSeconds > 0) {\n // Server-specified delay — use exactly, no jitter (contract compliance).\n backoff = retryAfterSeconds * 1000;\n } else {\n backoff = addJitter(Math.min(initialDelayMs * Math.pow(2, attempt - 1), maxDelayMs));\n }\n\n await delay(backoff, options.signal);\n }\n }\n throw lastError;\n}\n","/**\n * Shared request execution pipeline for the AI Gateway SDK.\n *\n * Handles: auth injection, Bearer token refresh on 401, middleware chain,\n * timeout, retry, and error normalization. Inlines abort, request-id,\n * and fetch transport as private helpers.\n */\n\nimport type { RequestConfig, ResolvedConfig } from './gateway-config';\nimport { AuthError, parseErrorResponseFromResponse } from './gateway-errors';\nimport { withRetry } from './gateway-retry';\n\n// ─── Private helpers ──────────────────────────────────────────────────────────\n\n/** Combine multiple AbortSignals — aborts when ANY fires. Uses native AbortSignal.any when available. */\nfunction anySignal(signals: AbortSignal[]): AbortSignal {\n const any = (AbortSignal as unknown as { any?: (signals: AbortSignal[]) => AbortSignal }).any;\n if (typeof any === 'function') {\n return any(signals);\n }\n const controller = new AbortController();\n const handlers: Array<[AbortSignal, () => void]> = [];\n function cleanup() {\n for (const [sig, handler] of handlers) sig.removeEventListener('abort', handler);\n handlers.length = 0;\n }\n for (const signal of signals) {\n if (signal.aborted) {\n controller.abort(signal.reason);\n return controller.signal;\n }\n const handler = () => {\n cleanup();\n controller.abort(signal.reason);\n };\n handlers.push([signal, handler]);\n signal.addEventListener('abort', handler, { once: true });\n }\n controller.signal.addEventListener('abort', cleanup, { once: true });\n return controller.signal;\n}\n\nlet _counter = 0;\n/** Generate a unique request ID for correlation. */\nfunction generateRequestId(prefix: string = 'sdk'): string {\n if (typeof globalThis.crypto?.randomUUID === 'function') {\n return `${prefix}-${globalThis.crypto.randomUUID()}`;\n }\n const timestamp = Date.now().toString(36);\n const count = (_counter++).toString(36);\n const random = Math.random().toString(36).slice(2, 8);\n return `${prefix}-${timestamp}-${count}-${random}`;\n}\n\n/** Case-insensitive header key lookup. */\nfunction hasHeaderCaseInsensitive(headers: Record<string, string>, name: string): boolean {\n const lower = name.toLowerCase();\n return Object.keys(headers).some((k) => k.toLowerCase() === lower);\n}\n\nfunction shouldAutoSetJsonContentType(body: RequestInit['body'], headers: Record<string, string>): boolean {\n if (body == null || hasHeaderCaseInsensitive(headers, 'content-type')) return false;\n const isFormData = typeof FormData !== 'undefined' && body instanceof FormData;\n const isBlob = typeof Blob !== 'undefined' && body instanceof Blob;\n return !isFormData && !isBlob;\n}\n\nfunction redactSensitiveHeaders(headers: Record<string, string>): Record<string, string> {\n const redacted: Record<string, string> = {};\n for (const [key, value] of Object.entries(headers)) {\n redacted[key] = key.toLowerCase() === 'authorization' ? '[REDACTED]' : value;\n }\n return redacted;\n}\n\nconst NODE_NETWORK_CODES = new Set([\n 'ECONNREFUSED',\n 'ECONNRESET',\n 'ENOTFOUND',\n 'EPIPE',\n 'ETIMEDOUT',\n 'ENETUNREACH',\n 'EAI_AGAIN',\n 'UND_ERR_CONNECT_TIMEOUT',\n]);\n\nconst FETCH_NETWORK_TYPEERROR_HINTS = [\n 'failed to fetch',\n 'fetch failed',\n 'load failed',\n 'networkerror',\n 'network error when attempting to fetch',\n];\n\nfunction hasRetryableNodeErrorCode(err: unknown): boolean {\n let cur: unknown = err;\n for (let depth = 0; depth < 5 && cur != null; depth++) {\n const code = (cur as { code?: string })?.code;\n if (typeof code === 'string' && NODE_NETWORK_CODES.has(code)) return true;\n cur = cur instanceof Error && cur.cause !== undefined ? cur.cause : undefined;\n }\n return false;\n}\n\nfunction isFetchFailureTypeError(err: unknown): boolean {\n if (!(err instanceof TypeError)) return false;\n const msg = String(err.message).toLowerCase();\n return FETCH_NETWORK_TYPEERROR_HINTS.some((hint) => msg.includes(hint));\n}\n\nfunction isNetworkError(err: unknown): boolean {\n return hasRetryableNodeErrorCode(err) || isFetchFailureTypeError(err);\n}\n\n/** Execute a single HTTP request using the configured fetch implementation. */\nasync function executeFetch(fetchImpl: typeof fetch | undefined, requestConfig: RequestConfig): Promise<Response> {\n const impl = fetchImpl ?? globalThis.fetch;\n if (typeof impl === 'undefined') {\n throw new Error(\n '@macpaw/ai-sdk requires a global `fetch` implementation. ' +\n 'Use Node.js 18+ or install a polyfill like `undici`.',\n );\n }\n return impl(requestConfig.url, {\n method: requestConfig.method,\n headers: requestConfig.headers,\n body: requestConfig.body,\n signal: requestConfig.signal,\n });\n}\n\n// ─── Public types ─────────────────────────────────────────────────────────────\n\nexport interface ExecuteRequestInput {\n url: string;\n method: string;\n body?: RequestInit['body'];\n headers?: Record<string, string>;\n signal?: AbortSignal;\n}\n\nexport interface ExecuteRequestBehavior {\n /** Merge config-level default headers before per-request headers. */\n includeConfigHeaders?: boolean;\n /** Resolve and inject Bearer auth via `getAuthToken()`. */\n includeAuth?: boolean;\n /** Auto-add `X-Request-ID` when missing. */\n includeRequestId?: boolean;\n /** Convert non-OK responses into normalized gateway errors. */\n normalizeErrors?: boolean;\n /** Retry once on auth failure by forcing a fresh token. */\n allowAuthRetry?: boolean;\n /** Request ID prefix for correlation, e.g. `sdk` or `provider`. */\n requestIdPrefix?: string;\n}\n\n// ─── Pipeline ─────────────────────────────────────────────────────────────────\n\nexport async function executeRequestPipeline(\n config: ResolvedConfig,\n request: ExecuteRequestInput,\n behavior?: ExecuteRequestBehavior,\n): Promise<Response> {\n const b: Required<ExecuteRequestBehavior> = {\n includeConfigHeaders: behavior?.includeConfigHeaders ?? true,\n includeAuth: behavior?.includeAuth ?? true,\n includeRequestId: behavior?.includeRequestId ?? true,\n normalizeErrors: behavior?.normalizeErrors ?? true,\n allowAuthRetry: behavior?.allowAuthRetry ?? true,\n requestIdPrefix: behavior?.requestIdPrefix ?? 'sdk',\n };\n return executeWithAuth(config, request, b, false);\n}\n\nasync function executeWithAuth(\n config: ResolvedConfig,\n request: ExecuteRequestInput,\n behavior: Required<ExecuteRequestBehavior>,\n isTokenRetry: boolean,\n): Promise<Response> {\n try {\n return await executeRequest(config, request, behavior, isTokenRetry);\n } catch (err) {\n if (behavior.allowAuthRetry && !isTokenRetry && err instanceof AuthError) {\n // ReadableStream bodies are single-consumer: once the first request reads\n // the stream it is consumed and cannot be replayed on the retry attempt.\n if (typeof ReadableStream !== 'undefined' && request.body instanceof ReadableStream) {\n throw err;\n }\n return executeWithAuth(config, request, behavior, true);\n }\n throw err;\n }\n}\n\nasync function executeRequest(\n config: ResolvedConfig,\n request: ExecuteRequestInput,\n behavior: Required<ExecuteRequestBehavior>,\n forceRefreshToken: boolean,\n): Promise<Response> {\n const headers: Record<string, string> = {\n ...(behavior.includeConfigHeaders ? config.headers : undefined),\n ...request.headers,\n };\n\n if (shouldAutoSetJsonContentType(request.body, headers)) {\n headers['Content-Type'] = 'application/json';\n }\n\n if (behavior.includeRequestId && !hasHeaderCaseInsensitive(headers, 'x-request-id')) {\n headers['X-Request-ID'] = generateRequestId(behavior.requestIdPrefix);\n }\n\n const timeoutMs = config.timeout;\n const userSignal = request.signal;\n\n async function doRequest(): Promise<Response> {\n const timeoutController = new AbortController();\n const timeoutId = setTimeout(\n () => timeoutController.abort(new Error(`Request timed out after ${timeoutMs}ms`)),\n timeoutMs,\n );\n\n const signal = userSignal ? anySignal([userSignal, timeoutController.signal]) : timeoutController.signal;\n\n const requestConfig: RequestConfig = {\n url: request.url,\n method: request.method,\n headers: { ...headers },\n body: request.body,\n signal,\n };\n\n const { middleware } = config;\n let index = 0;\n\n const next = async (currentRequest: RequestConfig): Promise<Response> => {\n if (index < middleware.length) {\n const middlewareItem = middleware[index++];\n return middlewareItem(currentRequest, next);\n }\n const finalHeaders = { ...currentRequest.headers };\n if (behavior.includeAuth) {\n const token = await config.getAuthToken(forceRefreshToken);\n if (token) {\n finalHeaders.Authorization = `Bearer ${token}`;\n } else {\n for (const key of Object.keys(finalHeaders)) {\n if (key.toLowerCase() === 'authorization') delete finalHeaders[key];\n }\n }\n }\n return executeFetch(config.fetchImpl, { ...currentRequest, headers: finalHeaders });\n };\n\n try {\n const response = await next(requestConfig);\n\n if (!response.ok) {\n const retryableStatuses = config.retry !== false ? config.retry.retryableStatuses : [];\n // Transport logic must fire regardless of normalizeErrors:\n // - 401 must throw AuthError so executeWithAuth can refresh the token\n // - retryable statuses must throw so withRetry can schedule retry attempts\n const isTransportCritical =\n (behavior.allowAuthRetry && response.status === 401) ||\n (config.retry !== false && retryableStatuses.includes(response.status));\n\n if (behavior.normalizeErrors || isTransportCritical) {\n await parseErrorResponseFromResponse(response);\n }\n }\n\n return response;\n } finally {\n clearTimeout(timeoutId);\n }\n }\n\n if (config.retry) {\n // doRequest() re-runs the full middleware chain on each retry attempt.\n // Middleware with side effects (metrics, audit logs) will fire per attempt.\n return withRetry(doRequest, {\n retryConfig: config.retry,\n signal: userSignal,\n isNetworkError,\n });\n }\n\n return doRequest();\n}\n\n/** Redact Authorization for safe logging/debugging. */\nexport { redactSensitiveHeaders };\n","/**\n * Custom fetch factory for use with Vercel AI SDK and other OpenAI-compatible clients.\n * Use this with createOpenAI({ baseURL, fetch: createGatewayFetch(...) }) or similar.\n *\n * AI Gateway HTTP paths are under /api/v1 (e.g. /api/v1/chat/completions).\n * So baseURL should be the gateway root, e.g. https://api.macpaw.com/ai\n *\n * Internally delegates to the shared request pipeline while guarding against\n * leaking gateway auth/headers to non-gateway hosts.\n */\n\nimport type { GatewayProviderSettings } from './gateway-config';\nimport { resolveConfig } from './gateway-config';\nimport { executeRequestPipeline } from './gateway-request';\n\n/**\n * Config for `createGatewayFetch`.\n * Extends GatewayProviderSettings with baseURL required (already resolved)\n * and normalizeErrors (provider-specific behavior).\n */\nexport interface GatewayFetchConfig extends GatewayProviderSettings {\n /** Resolved Gateway base URL (required — use resolveGatewayBaseURL first). */\n baseURL: string;\n /**\n * Normalize gateway error responses into `AIGatewayError`. Default: true.\n * When enabled, non-OK gateway responses throw instead of returning a failed Response.\n */\n normalizeErrors?: boolean;\n}\n\ntype FetchInput = string | URL | Request | { url: string };\n\nfunction resolveRequestUrl(input: FetchInput): string {\n if (typeof input === 'string') return input;\n if (input instanceof URL) return input.href;\n return input.url;\n}\n\nfunction stripPlaceholderAuthorization(headers: Headers, placeholder: string): void {\n const auth = headers.get('authorization');\n if (auth === `Bearer ${placeholder}`) {\n headers.delete('authorization');\n }\n}\n\nfunction headersToRecord(headers: Headers): Record<string, string> {\n const record: Record<string, string> = {};\n for (const [key, value] of headers.entries()) {\n record[key] = value;\n }\n return record;\n}\n\nfunction joinBaseUrl(baseURL: string, path: string): string {\n return `${baseURL}${path.startsWith('/') ? '' : '/'}${path}`;\n}\n\nfunction isGatewayUrl(url: URL, gatewayBaseUrl: URL): boolean {\n const gatewayPath = gatewayBaseUrl.pathname.replace(/\\/$/, '');\n const requestPath = url.pathname.replace(/\\/$/, '');\n return (\n url.origin === gatewayBaseUrl.origin && (requestPath === gatewayPath || requestPath.startsWith(`${gatewayPath}/`))\n );\n}\n\nexport const GATEWAY_PLACEHOLDER_API_KEY = 'ai-gateway-auth-via-fetch';\n\nexport function createGatewayFetch(\n options: GatewayFetchConfig,\n): (input: FetchInput, init?: RequestInit) => Promise<Response> {\n const { baseURL, normalizeErrors = true } = options;\n const base = baseURL.replace(/\\/$/, '');\n const gatewayBaseUrl = new URL(base);\n const resolvedConfig = resolveConfig({ ...options, baseURL: base });\n\n return async function gatewayFetch(input: FetchInput, init?: RequestInit): Promise<Response> {\n const rawUrl = resolveRequestUrl(input);\n const isAbsolute = rawUrl.startsWith('http://') || rawUrl.startsWith('https://');\n const resolvedUrl = new URL(isAbsolute ? rawUrl : joinBaseUrl(base, rawUrl));\n const isGatewayRequest = isGatewayUrl(resolvedUrl, gatewayBaseUrl);\n\n const request = typeof Request !== 'undefined' && input instanceof Request ? input : undefined;\n const requestClone = request?.clone();\n const headers = new Headers(requestClone?.headers);\n\n if (init?.headers) {\n for (const [key, value] of new Headers(init.headers).entries()) {\n headers.set(key, value);\n }\n }\n\n if (!isGatewayRequest) {\n stripPlaceholderAuthorization(headers, GATEWAY_PLACEHOLDER_API_KEY);\n }\n\n return executeRequestPipeline(\n resolvedConfig,\n {\n url: resolvedUrl.toString(),\n method: init?.method ?? requestClone?.method ?? 'GET',\n headers: headersToRecord(headers),\n body: init?.body ?? requestClone?.body,\n signal: init?.signal ?? requestClone?.signal,\n },\n {\n includeConfigHeaders: isGatewayRequest,\n includeAuth: isGatewayRequest,\n includeRequestId: isGatewayRequest,\n normalizeErrors: isGatewayRequest && normalizeErrors,\n allowAuthRetry: isGatewayRequest,\n requestIdPrefix: 'provider',\n },\n );\n };\n}\n","/**\n * AI Gateway provider factory.\n *\n * `createGatewayProvider()` routes bare model IDs through the gateway's\n * OpenAI-compatible endpoint with provider-specific prefixes.\n * `createAIGatewayProvider()` is the low-level escape hatch for direct\n * OpenAI-compatible provider construction.\n */\n\nimport { createOpenAI as builtinCreateOpenAI } from '@ai-sdk/openai';\nimport type { OpenAIProvider, OpenAIProviderSettings } from '@ai-sdk/openai';\nimport { createGatewayFetch, GATEWAY_PLACEHOLDER_API_KEY } from './gateway-fetch';\nimport type { Environment, GatewayProviderSettings } from './gateway-config';\nimport { resolveGatewayBaseURL } from './gateway-config';\n\n// ─── AIGatewayProvider (low-level) ────────────────────────────────────────────\n\nconst DEFAULT_API_VERSION = 'v1';\n\nexport interface AIGatewayProviderOptions\n extends Omit<OpenAIProviderSettings, 'apiKey' | 'baseURL' | 'fetch'>, GatewayProviderSettings {\n /**\n * Optional override for the OpenAI provider factory.\n * Uses `createOpenAI` from `@ai-sdk/openai` by default.\n */\n createOpenAI?: typeof builtinCreateOpenAI;\n /**\n * Normalize gateway error responses into `AIGatewayError`. Default: true.\n * When enabled, non-OK gateway responses throw instead of returning a failed Response.\n */\n normalizeErrors?: boolean;\n}\n\n/**\n * Creates a Vercel AI SDK-compatible provider backed by AI Gateway.\n *\n * The returned value is a fully typed `OpenAIProvider`, so existing apps can\n * keep using their `ai-sdk` helpers and model-selection patterns.\n */\nexport function createAIGatewayProvider(options: AIGatewayProviderOptions): OpenAIProvider {\n const baseURL = resolveGatewayBaseURL(options.baseURL, options.env as Environment, 'AIGatewayProvider');\n const customFetch = createGatewayFetch({ ...options, baseURL, normalizeErrors: options.normalizeErrors });\n\n const openAI = options.createOpenAI ?? builtinCreateOpenAI;\n\n return openAI({\n name: options.name,\n organization: options.organization,\n project: options.project,\n baseURL: `${baseURL.replace(/\\/$/, '')}/api/${DEFAULT_API_VERSION}`,\n fetch: customFetch,\n apiKey: GATEWAY_PLACEHOLDER_API_KEY,\n });\n}\n\n// ─── GatewayProvider (prefixed routing) ───────────────────────────────────────\n\nexport interface GatewayProviderBaseOptions extends AIGatewayProviderOptions {\n /**\n * Override the model ID prefix used for Gateway routing.\n * Default: the provider's canonical prefix (e.g. `'anthropic'`).\n *\n * Model IDs that already contain `/` are sent as-is.\n */\n modelPrefix?: string;\n}\n\nexport interface GatewayOpenAICompatibleOptions extends Omit<GatewayProviderBaseOptions, 'modelPrefix'> {\n /**\n * Required for openai-compatible backends because there is no single canonical\n * default prefix. Examples: `openai`, `fireworks_ai`, `openrouter`.\n */\n modelPrefix: string;\n}\n\nexport const GATEWAY_PROVIDERS = {\n ANTHROPIC: 'anthropic',\n GOOGLE: 'google',\n XAI: 'xai',\n GROQ: 'groq',\n MISTRAL: 'mistral',\n AMAZON_BEDROCK: 'amazon-bedrock',\n AZURE: 'azure',\n COHERE: 'cohere',\n PERPLEXITY: 'perplexity',\n DEEPSEEK: 'deepseek',\n TOGETHERAI: 'togetherai',\n OPENAI_COMPATIBLE: 'openai-compatible',\n} as const;\n\nexport type GatewayProvider = (typeof GATEWAY_PROVIDERS)[keyof typeof GATEWAY_PROVIDERS];\n\nexport type GatewayProviderWithDefaultPrefix = Exclude<GatewayProvider, typeof GATEWAY_PROVIDERS.OPENAI_COMPATIBLE>;\n\nexport interface GatewayProviderOptionsMap {\n [GATEWAY_PROVIDERS.OPENAI_COMPATIBLE]: GatewayOpenAICompatibleOptions;\n}\n\nexport type GatewayProviderOptions<P extends GatewayProvider> = P extends keyof GatewayProviderOptionsMap\n ? GatewayProviderOptionsMap[P]\n : GatewayProviderBaseOptions;\n\nconst GATEWAY_PROVIDER_DEFAULT_PREFIXES: Record<GatewayProviderWithDefaultPrefix, string> = {\n [GATEWAY_PROVIDERS.ANTHROPIC]: 'anthropic',\n [GATEWAY_PROVIDERS.GOOGLE]: 'google',\n [GATEWAY_PROVIDERS.XAI]: 'xai',\n [GATEWAY_PROVIDERS.GROQ]: 'groq',\n [GATEWAY_PROVIDERS.MISTRAL]: 'mistral',\n [GATEWAY_PROVIDERS.AMAZON_BEDROCK]: 'bedrock',\n [GATEWAY_PROVIDERS.AZURE]: 'azure',\n [GATEWAY_PROVIDERS.COHERE]: 'cohere',\n [GATEWAY_PROVIDERS.PERPLEXITY]: 'perplexity',\n [GATEWAY_PROVIDERS.DEEPSEEK]: 'deepseek',\n [GATEWAY_PROVIDERS.TOGETHERAI]: 'togetherai',\n};\n\nfunction prefixModelId(prefix: string, modelId: string): string {\n return modelId.includes('/') ? modelId : `${prefix}/${modelId}`;\n}\n\nfunction createPrefixedGatewayProvider(defaultPrefix: string, options: GatewayProviderBaseOptions): OpenAIProvider {\n const { modelPrefix, ...providerOptions } = options;\n const prefix = modelPrefix ?? defaultPrefix;\n const provider = createAIGatewayProvider(providerOptions);\n\n const wrapped = ((modelId: string) => provider(prefixModelId(prefix, modelId))) as OpenAIProvider;\n\n return new Proxy(wrapped, {\n apply(_target, _thisArg, args: [string, ...unknown[]]) {\n const [modelId, ...rest] = args;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (provider as (...a: unknown[]) => any)(prefixModelId(prefix, modelId), ...rest);\n },\n get(_target, prop) {\n const value = Reflect.get(provider, prop, provider);\n if (typeof value === 'function') {\n return (...args: unknown[]) => {\n if (typeof args[0] === 'string') {\n args[0] = prefixModelId(prefix, args[0] as string);\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (value as (...a: unknown[]) => any).apply(provider, args);\n };\n }\n return value;\n },\n has(_target, prop) {\n return prop in (provider as object);\n },\n });\n}\n\n/**\n * Creates an `OpenAIProvider` backed by AI Gateway for a supported provider.\n *\n * Bare model IDs are automatically prefixed with the provider's canonical\n * Gateway routing prefix. Model IDs that already contain `/` are passed as-is.\n */\nexport function createGatewayProvider(\n provider: GatewayProviderWithDefaultPrefix,\n options: GatewayProviderBaseOptions,\n): OpenAIProvider;\nexport function createGatewayProvider(\n provider: typeof GATEWAY_PROVIDERS.OPENAI_COMPATIBLE,\n options: GatewayOpenAICompatibleOptions,\n): OpenAIProvider;\nexport function createGatewayProvider(\n provider: GatewayProvider,\n options: GatewayProviderBaseOptions | GatewayOpenAICompatibleOptions,\n): OpenAIProvider {\n if (provider === GATEWAY_PROVIDERS.OPENAI_COMPATIBLE) {\n return createPrefixedGatewayProvider(\n (options as GatewayOpenAICompatibleOptions).modelPrefix,\n options as GatewayOpenAICompatibleOptions,\n );\n }\n\n return createPrefixedGatewayProvider(\n GATEWAY_PROVIDER_DEFAULT_PREFIXES[provider as GatewayProviderWithDefaultPrefix],\n options as GatewayProviderBaseOptions,\n );\n}\n"]}
1
+ {"version":3,"sources":["../src/gateway-config.ts","../src/gateway-retry.ts","../src/gateway-request.ts","../src/gateway-fetch.ts","../src/gateway-provider.ts"],"names":["builtinCreateOpenAI"],"mappings":";;;;;AAyBO,IAAM,aAAA,GAAuC;AAAA,EAClD,WAAA,EAAa,CAAA;AAAA,EACb,cAAA,EAAgB,GAAA;AAAA,EAChB,UAAA,EAAY,GAAA;AAAA,EACZ,mBAAmB,CAAC,GAAA,EAAK,GAAA,EAAK,GAAA,EAAK,KAAK,GAAG;AAC7C,CAAA;AAGO,SAAS,qBAAqB,MAAA,EAAsD;AACzF,EAAA,MAAM,IAAI,IAAA,CAAK,KAAA,CAAM,MAAA,CAAO,MAAA,CAAO,WAAW,CAAC,CAAA;AAC/C,EAAA,MAAM,WAAA,GAAc,OAAO,QAAA,CAAS,CAAC,KAAK,CAAA,IAAK,CAAA,GAAI,IAAI,aAAA,CAAc,WAAA;AACrE,EAAA,OAAO,EAAE,GAAG,MAAA,EAAQ,WAAA,EAAY;AAClC;AAyDO,SAAS,cAAc,MAAA,EAAuE;AACnG,EAAA,OAAO;AAAA,IACL,SAAS,MAAA,CAAO,OAAA;AAAA,IAChB,cAAc,MAAA,CAAO,YAAA;AAAA,IACrB,KAAA,EAAO,MAAA,CAAO,KAAA,KAAU,KAAA,GAAQ,KAAA,GAAQ,oBAAA,CAAqB,EAAE,GAAG,aAAA,EAAe,GAAG,MAAA,CAAO,KAAA,EAAO,CAAA;AAAA,IAClG,YAAY,CAAC,GAAI,MAAA,CAAO,UAAA,IAAc,EAAG,CAAA;AAAA,IACzC,SAAS,MAAA,CAAO,OAAA;AAAA,IAChB,OAAA,EAAS,OAAO,OAAA,IAAW,GAAA;AAAA,IAC3B,WAAW,MAAA,CAAO;AAAA,GACpB;AACF;AAGO,IAAM,iBAAA,GAAiD;AAAA,EAC5D,UAAA,EAAY;AACd;AAEO,SAA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,6BAAA;AAAA,MACJ,OAAA,CAA2C,WAAA;AAAA,MAC5C;AAAA,KACF;AAAA,EACF;AAEA,EAAA,OAAO,6BAAA;AAAA,IACL,kCAAkC,QAA4C,CAAA;AAAA,IAC9E;AAAA,GACF;AACF","file":"index.js","sourcesContent":["/**\n * Configuration types and resolver for the AI Gateway SDK.\n *\n * GatewayProviderSettings is the public API — 8 fields.\n * ResolvedConfig is the internal resolved form used by the request pipeline.\n */\n\n/**\n * Supported gateway environments.\n * Only 'production' is exposed — non-production setups use a direct `baseURL`\n * instead, keeping env-specific URL management out of this library.\n */\nexport type Environment = 'production';\n\nexport interface RetryConfig {\n /** Max number of attempts (including first). Default 3. */\n maxAttempts?: number;\n /** Initial delay in ms. Default 1000. */\n initialDelayMs?: number;\n /** Max delay in ms. Default 30000. */\n maxDelayMs?: number;\n /** Retry only on these status codes (and network errors). Default: 429, 5xx. */\n retryableStatuses?: number[];\n}\n\nexport const DEFAULT_RETRY: Required<RetryConfig> = {\n maxAttempts: 3,\n initialDelayMs: 1000,\n maxDelayMs: 30000,\n retryableStatuses: [429, 500, 502, 503, 504],\n};\n\n/** Coerces invalid `maxAttempts` (0, negative, NaN) to the default. */\nexport function normalizeRetryConfig(merged: Required<RetryConfig>): Required<RetryConfig> {\n const n = Math.floor(Number(merged.maxAttempts));\n const maxAttempts = Number.isFinite(n) && n >= 1 ? n : DEFAULT_RETRY.maxAttempts;\n return { ...merged, maxAttempts };\n}\n\nexport interface RequestConfig {\n url: string;\n method: string;\n headers: Record<string, string>;\n body?: RequestInit['body'];\n signal?: AbortSignal | undefined;\n}\n\nexport type Middleware = (\n config: RequestConfig,\n next: (config: RequestConfig) => Promise<Response>,\n) => Promise<Response>;\n\n/**\n * Configuration for AI Gateway providers and NestJS module.\n *\n * Exactly 8 fields — covers auth, routing, request behaviour and middleware.\n */\nexport interface GatewayProviderSettings {\n /** Gateway base URL. Required if env is not set. */\n baseURL?: string;\n /** 'production' uses the default MacPaw Gateway URL. */\n env?: Environment;\n /**\n * Returns Bearer token for auth.\n * Called with `forceRefresh=true` after 401 — provide a fresh token.\n */\n getAuthToken: (forceRefresh?: boolean) => Promise<string | null>;\n /** Extra headers sent with every request. Do not set Authorization here. */\n headers?: Record<string, string>;\n /** Retry policy. Set to false to disable. */\n retry?: RetryConfig | false;\n /** Request timeout in ms. Default: 60000. */\n timeout?: number;\n /** Request interceptors. Run in order before each request. */\n middleware?: Middleware[];\n /**\n * Custom fetch implementation.\n * Use for testing or low-level request customization.\n */\n fetch?: typeof fetch;\n}\n\n/** Internal resolved form — used only by the request pipeline. */\nexport interface ResolvedConfig {\n baseURL: string;\n getAuthToken: (forceRefresh?: boolean) => Promise<string | null>;\n /** Already normalized — all fields present, maxAttempts ≥ 1. */\n retry: Required<RetryConfig> | false;\n middleware: Middleware[];\n headers?: Record<string, string>;\n timeout: number;\n fetchImpl?: typeof fetch;\n}\n\nexport function resolveConfig(config: GatewayProviderSettings & { baseURL: string }): ResolvedConfig {\n return {\n baseURL: config.baseURL,\n getAuthToken: config.getAuthToken,\n retry: config.retry === false ? false : normalizeRetryConfig({ ...DEFAULT_RETRY, ...config.retry }),\n middleware: [...(config.middleware ?? [])],\n headers: config.headers,\n timeout: config.timeout ?? 60_000,\n fetchImpl: config.fetch,\n };\n}\n\n/** Default base URL for the production environment. */\nexport const DEFAULT_BASE_URLS: Record<Environment, string> = {\n production: 'https://api.macpaw.com/ai',\n};\n\nexport function resolveGatewayBaseURL(\n baseURL: string | undefined,\n env: Environment | undefined,\n consumerName: string,\n): string {\n const resolved = baseURL ?? (env ? DEFAULT_BASE_URLS[env] : undefined);\n if (!resolved) {\n throw new Error(\n `${consumerName} requires baseURL or env (production). For non-production environments, pass baseURL directly.`,\n );\n }\n return resolved;\n}\n","/**\n * Retry with exponential backoff + jitter.\n * Only retries on 429, 5xx and network errors; never on 4xx (401, 402, etc.).\n *\n * Jitter prevents thundering herd when many clients retry simultaneously.\n * Respects Retry-After metadata from 429 responses.\n */\n\nimport type { RetryConfig } from './gateway-config';\nimport type { AIGatewayError } from './gateway-errors';\n\nfunction delay(ms: number, signal?: AbortSignal): Promise<void> {\n return new Promise((resolve, reject) => {\n if (signal?.aborted) {\n reject(signal.reason);\n return;\n }\n const timer = setTimeout(resolve, ms);\n signal?.addEventListener(\n 'abort',\n () => {\n clearTimeout(timer);\n reject(signal.reason);\n },\n { once: true },\n );\n });\n}\n\nfunction isRetryableStatus(status: number, retryableStatuses: number[]): boolean {\n return retryableStatuses.includes(status);\n}\n\n/** Add random jitter: 0-25% of the base delay to avoid thundering herd */\nfunction addJitter(delayMs: number): number {\n const jitter = delayMs * 0.25 * Math.random();\n return delayMs + jitter;\n}\n\nexport async function withRetry<T>(\n fn: () => Promise<T>,\n options: {\n /** Already-normalized config — all fields must be present. */\n retryConfig: Required<RetryConfig>;\n signal?: AbortSignal;\n isNetworkError?: (err: unknown) => boolean;\n },\n): Promise<T> {\n const { maxAttempts, initialDelayMs, maxDelayMs, retryableStatuses } = options.retryConfig;\n let lastError: unknown;\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n try {\n return await fn();\n } catch (err) {\n lastError = err;\n if (attempt === maxAttempts) break;\n const status = (err as { statusCode?: number })?.statusCode;\n const isRetryable =\n status != null ? isRetryableStatus(status, retryableStatuses) : (options.isNetworkError?.(err) ?? false);\n if (!isRetryable) throw err;\n\n const retryAfterSeconds = (err as AIGatewayError)?.retryAfter;\n let backoff: number;\n if (retryAfterSeconds != null && retryAfterSeconds > 0) {\n // Server-specified delay — use exactly, no jitter (contract compliance).\n backoff = retryAfterSeconds * 1000;\n } else {\n backoff = addJitter(Math.min(initialDelayMs * Math.pow(2, attempt - 1), maxDelayMs));\n }\n\n await delay(backoff, options.signal);\n }\n }\n throw lastError;\n}\n","/**\n * Shared request execution pipeline for the AI Gateway SDK.\n *\n * Handles: auth injection, Bearer token refresh on 401, middleware chain,\n * timeout, retry, and error normalization. Inlines abort, request-id,\n * and fetch transport as private helpers.\n */\n\nimport type { RequestConfig, ResolvedConfig } from './gateway-config';\nimport { AuthError, parseErrorResponseFromResponse } from './gateway-errors';\nimport { GATEWAY_PLACEHOLDER_API_KEY, normalizeBearerToken } from './gateway-fetch';\nimport { withRetry } from './gateway-retry';\n\n// ─── Private helpers ──────────────────────────────────────────────────────────\n\n/** Combine multiple AbortSignals — aborts when ANY fires. Uses native AbortSignal.any when available. */\nfunction anySignal(signals: AbortSignal[]): AbortSignal {\n const any = (AbortSignal as unknown as { any?: (signals: AbortSignal[]) => AbortSignal }).any;\n if (typeof any === 'function') {\n return any(signals);\n }\n const controller = new AbortController();\n const handlers: Array<[AbortSignal, () => void]> = [];\n function cleanup() {\n for (const [sig, handler] of handlers) sig.removeEventListener('abort', handler);\n handlers.length = 0;\n }\n for (const signal of signals) {\n if (signal.aborted) {\n controller.abort(signal.reason);\n return controller.signal;\n }\n const handler = () => {\n cleanup();\n controller.abort(signal.reason);\n };\n handlers.push([signal, handler]);\n signal.addEventListener('abort', handler, { once: true });\n }\n controller.signal.addEventListener('abort', cleanup, { once: true });\n return controller.signal;\n}\n\nlet _counter = 0;\n/** Generate a unique request ID for correlation. */\nfunction generateRequestId(prefix: string = 'sdk'): string {\n if (typeof globalThis.crypto?.randomUUID === 'function') {\n return `${prefix}-${globalThis.crypto.randomUUID()}`;\n }\n const timestamp = Date.now().toString(36);\n const count = (_counter++).toString(36);\n const random = Math.random().toString(36).slice(2, 8);\n return `${prefix}-${timestamp}-${count}-${random}`;\n}\n\n/** Case-insensitive header key lookup. */\nfunction hasHeaderCaseInsensitive(headers: Record<string, string>, name: string): boolean {\n const lower = name.toLowerCase();\n return Object.keys(headers).some((k) => k.toLowerCase() === lower);\n}\n\n/**\n * Drops every `Authorization` key regardless of casing.\n * Some runtimes would emit two header fields if both `authorization` and `Authorization`\n * exist on the same object; servers then merge them into one value with a comma.\n */\nfunction deleteAllAuthorizationHeaders(headers: Record<string, string>): void {\n for (const key of Object.keys(headers)) {\n if (key.toLowerCase() === 'authorization') delete headers[key];\n }\n}\n\nfunction shouldAutoSetJsonContentType(body: RequestInit['body'], headers: Record<string, string>): boolean {\n if (body == null || hasHeaderCaseInsensitive(headers, 'content-type')) return false;\n const isFormData = typeof FormData !== 'undefined' && body instanceof FormData;\n const isBlob = typeof Blob !== 'undefined' && body instanceof Blob;\n return !isFormData && !isBlob;\n}\n\nfunction redactSensitiveHeaders(headers: Record<string, string>): Record<string, string> {\n const redacted: Record<string, string> = {};\n for (const [key, value] of Object.entries(headers)) {\n redacted[key] = key.toLowerCase() === 'authorization' ? '[REDACTED]' : value;\n }\n return redacted;\n}\n\nconst NODE_NETWORK_CODES = new Set([\n 'ECONNREFUSED',\n 'ECONNRESET',\n 'ENOTFOUND',\n 'EPIPE',\n 'ETIMEDOUT',\n 'ENETUNREACH',\n 'EAI_AGAIN',\n 'UND_ERR_CONNECT_TIMEOUT',\n]);\n\nconst FETCH_NETWORK_TYPEERROR_HINTS = [\n 'failed to fetch',\n 'fetch failed',\n 'load failed',\n 'networkerror',\n 'network error when attempting to fetch',\n];\n\nfunction hasRetryableNodeErrorCode(err: unknown): boolean {\n let cur: unknown = err;\n for (let depth = 0; depth < 5 && cur != null; depth++) {\n const code = (cur as { code?: string })?.code;\n if (typeof code === 'string' && NODE_NETWORK_CODES.has(code)) return true;\n cur = cur instanceof Error && cur.cause !== undefined ? cur.cause : undefined;\n }\n return false;\n}\n\nfunction isFetchFailureTypeError(err: unknown): boolean {\n if (!(err instanceof TypeError)) return false;\n const msg = String(err.message).toLowerCase();\n return FETCH_NETWORK_TYPEERROR_HINTS.some((hint) => msg.includes(hint));\n}\n\nfunction isNetworkError(err: unknown): boolean {\n return hasRetryableNodeErrorCode(err) || isFetchFailureTypeError(err);\n}\n\n/** Execute a single HTTP request using the configured fetch implementation. */\nasync function executeFetch(fetchImpl: typeof fetch | undefined, requestConfig: RequestConfig): Promise<Response> {\n const impl = fetchImpl ?? globalThis.fetch;\n if (typeof impl === 'undefined') {\n throw new Error(\n '@macpaw/ai-sdk requires a global `fetch` implementation. ' +\n 'Use Node.js 18+ or install a polyfill like `undici`.',\n );\n }\n return impl(requestConfig.url, {\n method: requestConfig.method,\n headers: requestConfig.headers,\n body: requestConfig.body,\n signal: requestConfig.signal,\n });\n}\n\n// ─── Public types ─────────────────────────────────────────────────────────────\n\nexport interface ExecuteRequestInput {\n url: string;\n method: string;\n body?: RequestInit['body'];\n headers?: Record<string, string>;\n signal?: AbortSignal;\n}\n\nexport interface ExecuteRequestBehavior {\n /** Merge config-level default headers before per-request headers. */\n includeConfigHeaders?: boolean;\n /** Resolve and inject Bearer auth via `getAuthToken()`. */\n includeAuth?: boolean;\n /** Auto-add `X-Request-ID` when missing. */\n includeRequestId?: boolean;\n /** Convert non-OK responses into normalized gateway errors. */\n normalizeErrors?: boolean;\n /** Retry once on auth failure by forcing a fresh token. */\n allowAuthRetry?: boolean;\n /** Request ID prefix for correlation, e.g. `sdk` or `provider`. */\n requestIdPrefix?: string;\n}\n\n// ─── Pipeline ─────────────────────────────────────────────────────────────────\n\nexport async function executeRequestPipeline(\n config: ResolvedConfig,\n request: ExecuteRequestInput,\n behavior?: ExecuteRequestBehavior,\n): Promise<Response> {\n const b: Required<ExecuteRequestBehavior> = {\n includeConfigHeaders: behavior?.includeConfigHeaders ?? true,\n includeAuth: behavior?.includeAuth ?? true,\n includeRequestId: behavior?.includeRequestId ?? true,\n normalizeErrors: behavior?.normalizeErrors ?? true,\n allowAuthRetry: behavior?.allowAuthRetry ?? true,\n requestIdPrefix: behavior?.requestIdPrefix ?? 'sdk',\n };\n return executeWithAuth(config, request, b, false);\n}\n\nasync function executeWithAuth(\n config: ResolvedConfig,\n request: ExecuteRequestInput,\n behavior: Required<ExecuteRequestBehavior>,\n isTokenRetry: boolean,\n): Promise<Response> {\n try {\n return await executeRequest(config, request, behavior, isTokenRetry);\n } catch (err) {\n if (behavior.allowAuthRetry && !isTokenRetry && err instanceof AuthError) {\n // ReadableStream bodies are single-consumer: once the first request reads\n // the stream it is consumed and cannot be replayed on the retry attempt.\n if (typeof ReadableStream !== 'undefined' && request.body instanceof ReadableStream) {\n throw err;\n }\n return executeWithAuth(config, request, behavior, true);\n }\n throw err;\n }\n}\n\nasync function executeRequest(\n config: ResolvedConfig,\n request: ExecuteRequestInput,\n behavior: Required<ExecuteRequestBehavior>,\n forceRefreshToken: boolean,\n): Promise<Response> {\n const headers: Record<string, string> = {\n ...(behavior.includeConfigHeaders ? config.headers : undefined),\n ...request.headers,\n };\n\n if (shouldAutoSetJsonContentType(request.body, headers)) {\n headers['Content-Type'] = 'application/json';\n }\n\n if (behavior.includeRequestId && !hasHeaderCaseInsensitive(headers, 'x-request-id')) {\n headers['X-Request-ID'] = generateRequestId(behavior.requestIdPrefix);\n }\n\n const timeoutMs = config.timeout;\n const userSignal = request.signal;\n\n async function doRequest(): Promise<Response> {\n const timeoutController = new AbortController();\n const timeoutId = setTimeout(\n () => timeoutController.abort(new Error(`Request timed out after ${timeoutMs}ms`)),\n timeoutMs,\n );\n\n const signal = userSignal ? anySignal([userSignal, timeoutController.signal]) : timeoutController.signal;\n\n const requestConfig: RequestConfig = {\n url: request.url,\n method: request.method,\n headers: { ...headers },\n body: request.body,\n signal,\n };\n\n const { middleware } = config;\n let index = 0;\n\n const next = async (currentRequest: RequestConfig): Promise<Response> => {\n if (index < middleware.length) {\n const middlewareItem = middleware[index++];\n return middlewareItem(currentRequest, next);\n }\n const finalHeaders = { ...currentRequest.headers };\n if (behavior.includeAuth) {\n deleteAllAuthorizationHeaders(finalHeaders);\n const rawToken = await config.getAuthToken(forceRefreshToken);\n const token = normalizeBearerToken(rawToken);\n if (token && token !== GATEWAY_PLACEHOLDER_API_KEY) {\n finalHeaders.Authorization = `Bearer ${token}`;\n }\n }\n return executeFetch(config.fetchImpl, { ...currentRequest, headers: finalHeaders });\n };\n\n try {\n const response = await next(requestConfig);\n\n if (!response.ok) {\n const retryableStatuses = config.retry !== false ? config.retry.retryableStatuses : [];\n // Transport logic must fire regardless of normalizeErrors:\n // - 401 must throw AuthError so executeWithAuth can refresh the token\n // - retryable statuses must throw so withRetry can schedule retry attempts\n const isTransportCritical =\n (behavior.allowAuthRetry && response.status === 401) ||\n (config.retry !== false && retryableStatuses.includes(response.status));\n\n if (behavior.normalizeErrors || isTransportCritical) {\n await parseErrorResponseFromResponse(response);\n }\n }\n\n return response;\n } finally {\n clearTimeout(timeoutId);\n }\n }\n\n if (config.retry) {\n // doRequest() re-runs the full middleware chain on each retry attempt.\n // Middleware with side effects (metrics, audit logs) will fire per attempt.\n return withRetry(doRequest, {\n retryConfig: config.retry,\n signal: userSignal,\n isNetworkError,\n });\n }\n\n return doRequest();\n}\n\n/** Redact Authorization for safe logging/debugging. */\nexport { redactSensitiveHeaders };\n","/**\n * Custom fetch factory for use with Vercel AI SDK and other OpenAI-compatible clients.\n * Use this with createOpenAI({ baseURL, fetch: createGatewayFetch(...) }) or similar.\n *\n * AI Gateway HTTP paths are under /ai/v1 (e.g. /ai/v1/chat/completions).\n * So baseURL should be the gateway root, e.g. https://api.macpaw.com/ai\n *\n * Internally delegates to the shared request pipeline while guarding against\n * leaking gateway auth/headers to non-gateway hosts.\n */\n\nimport type { GatewayProviderSettings } from './gateway-config';\nimport { resolveConfig } from './gateway-config';\nimport { executeRequestPipeline } from './gateway-request';\n\nexport const GATEWAY_PLACEHOLDER_API_KEY = 'ai-gateway-auth-via-fetch';\n\n/**\n * Config for `createGatewayFetch`.\n * Extends GatewayProviderSettings with baseURL required (already resolved)\n * and normalizeErrors (provider-specific behavior).\n */\nexport interface GatewayFetchConfig extends GatewayProviderSettings {\n /** Resolved Gateway base URL (required — use resolveGatewayBaseURL first). */\n baseURL: string;\n /**\n * Normalize gateway error responses into `AIGatewayError`. Default: true.\n * When enabled, non-OK gateway responses throw instead of returning a failed Response.\n */\n normalizeErrors?: boolean;\n}\n\ntype FetchInput = string | URL | Request | { url: string };\n\nfunction resolveRequestUrl(input: FetchInput): string {\n if (typeof input === 'string') return input;\n if (input instanceof URL) return input.href;\n return input.url;\n}\n\n/**\n * Trims, strips a single layer of surrounding ASCII quotes, and removes\n * trailing comma/semicolon noise often introduced by .env or copy-paste.\n */\nexport function normalizeBearerToken(raw: string | null | undefined): string | null {\n if (raw == null) return null;\n let s = String(raw).trim();\n if (!s) return null;\n\n if (\n (s.startsWith('\"') && s.endsWith('\"') && s.length >= 2) ||\n (s.startsWith(\"'\") && s.endsWith(\"'\") && s.length >= 2)\n ) {\n s = s.slice(1, -1).trim();\n }\n\n s = s.replace(/[,;]+\\s*$/g, '').trim();\n return s.length > 0 ? s : null;\n}\n\n/**\n * Removes Authorization when it is only the OpenAI apiKey placeholder (optional trailing comma),\n * so a real Bearer from getAuthToken() is not merged with a junk value.\n */\nfunction stripPlaceholderAuthorization(headers: Headers, placeholder: string): void {\n const auth = headers.get('authorization');\n if (!auth) return;\n const m = auth.match(/^Bearer\\s+(\\S+)/i);\n if (!m) return;\n const credential = normalizeBearerToken(m[1]);\n if (credential === placeholder) {\n headers.delete('authorization');\n }\n}\n\nfunction headersToRecord(headers: Headers): Record<string, string> {\n const record: Record<string, string> = {};\n for (const [key, value] of headers.entries()) {\n record[key] = value;\n }\n return record;\n}\n\nfunction joinBaseUrl(baseURL: string, path: string): string {\n return `${baseURL}${path.startsWith('/') ? '' : '/'}${path}`;\n}\n\nfunction isGatewayUrl(url: URL, gatewayBaseUrl: URL): boolean {\n const gatewayPath = gatewayBaseUrl.pathname.replace(/\\/$/, '');\n const requestPath = url.pathname.replace(/\\/$/, '');\n return (\n url.origin === gatewayBaseUrl.origin && (requestPath === gatewayPath || requestPath.startsWith(`${gatewayPath}/`))\n );\n}\n\nexport function createGatewayFetch(\n options: GatewayFetchConfig,\n): (input: FetchInput, init?: RequestInit) => Promise<Response> {\n const { baseURL, normalizeErrors = true } = options;\n const base = baseURL.replace(/\\/$/, '');\n const gatewayBaseUrl = new URL(base);\n const resolvedConfig = resolveConfig({ ...options, baseURL: base });\n\n return async function gatewayFetch(input: FetchInput, init?: RequestInit): Promise<Response> {\n const rawUrl = resolveRequestUrl(input);\n const isAbsolute = rawUrl.startsWith('http://') || rawUrl.startsWith('https://');\n const resolvedUrl = new URL(isAbsolute ? rawUrl : joinBaseUrl(base, rawUrl));\n const isGatewayRequest = isGatewayUrl(resolvedUrl, gatewayBaseUrl);\n\n const request = typeof Request !== 'undefined' && input instanceof Request ? input : undefined;\n const requestClone = request?.clone();\n const headers = new Headers(requestClone?.headers);\n\n if (init?.headers) {\n for (const [key, value] of new Headers(init.headers).entries()) {\n headers.set(key, value);\n }\n }\n\n // Always strip the OpenAI placeholder before entering the shared pipeline.\n // For gateway requests the real Bearer is injected later via getAuthToken();\n // for non-gateway requests we also avoid leaking the sentinel downstream.\n stripPlaceholderAuthorization(headers, GATEWAY_PLACEHOLDER_API_KEY);\n\n return executeRequestPipeline(\n resolvedConfig,\n {\n url: resolvedUrl.toString(),\n method: init?.method ?? requestClone?.method ?? 'GET',\n headers: headersToRecord(headers),\n body: init?.body ?? requestClone?.body,\n signal: init?.signal ?? requestClone?.signal,\n },\n {\n includeConfigHeaders: isGatewayRequest,\n includeAuth: isGatewayRequest,\n includeRequestId: isGatewayRequest,\n normalizeErrors: isGatewayRequest && normalizeErrors,\n allowAuthRetry: isGatewayRequest,\n requestIdPrefix: 'provider',\n },\n );\n };\n}\n","/**\n * AI Gateway provider factory.\n *\n * `createGatewayProvider()` routes bare model IDs through the gateway's\n * OpenAI-compatible endpoint with provider-specific prefixes.\n * `createAIGatewayProvider()` is the low-level escape hatch for direct\n * OpenAI-compatible provider construction.\n */\n\nimport { createOpenAI as builtinCreateOpenAI } from '@ai-sdk/openai';\nimport type { OpenAIProvider, OpenAIProviderSettings } from '@ai-sdk/openai';\nimport { createGatewayFetch, GATEWAY_PLACEHOLDER_API_KEY } from './gateway-fetch';\nimport type { Environment, GatewayProviderSettings } from './gateway-config';\nimport { resolveGatewayBaseURL } from './gateway-config';\n\n// ─── AIGatewayProvider (low-level) ────────────────────────────────────────────\n\nconst DEFAULT_API_VERSION = 'v1';\n\nexport interface AIGatewayProviderOptions\n extends Omit<OpenAIProviderSettings, 'apiKey' | 'baseURL' | 'fetch'>, GatewayProviderSettings {\n /**\n * Optional override for the OpenAI provider factory.\n * Uses `createOpenAI` from `@ai-sdk/openai` by default.\n */\n createOpenAI?: typeof builtinCreateOpenAI;\n /**\n * Normalize gateway error responses into `AIGatewayError`. Default: true.\n * When enabled, non-OK gateway responses throw instead of returning a failed Response.\n */\n normalizeErrors?: boolean;\n}\n\n/**\n * Creates a Vercel AI SDK-compatible provider backed by AI Gateway.\n *\n * The returned value is a fully typed `OpenAIProvider`, so existing apps can\n * keep using their `ai-sdk` helpers and model-selection patterns.\n */\nexport function createAIGatewayProvider(options: AIGatewayProviderOptions): OpenAIProvider {\n const baseURL = resolveGatewayBaseURL(options.baseURL, options.env as Environment, 'AIGatewayProvider');\n const customFetch = createGatewayFetch({ ...options, baseURL, normalizeErrors: options.normalizeErrors });\n\n const openAI = options.createOpenAI ?? builtinCreateOpenAI;\n\n return openAI({\n name: options.name,\n organization: options.organization,\n project: options.project,\n baseURL: `${baseURL.replace(/\\/$/, '')}/${DEFAULT_API_VERSION}`,\n fetch: customFetch,\n apiKey: GATEWAY_PLACEHOLDER_API_KEY,\n });\n}\n\n// ─── GatewayProvider (prefixed routing) ───────────────────────────────────────\n\nexport interface GatewayProviderBaseOptions extends AIGatewayProviderOptions {\n /**\n * Override the model ID prefix used for Gateway routing.\n * Default: the provider's canonical prefix (e.g. `'anthropic'`).\n *\n * Model IDs that already contain `/` are sent as-is.\n */\n modelPrefix?: string;\n}\n\nexport interface GatewayOpenAICompatibleOptions extends Omit<GatewayProviderBaseOptions, 'modelPrefix'> {\n /**\n * Required for openai-compatible backends because there is no single canonical\n * default prefix. Examples: `openai`, `fireworks_ai`, `openrouter`.\n */\n modelPrefix: string;\n}\n\nexport const GATEWAY_PROVIDERS = {\n ANTHROPIC: 'anthropic',\n GOOGLE: 'google',\n XAI: 'xai',\n GROQ: 'groq',\n MISTRAL: 'mistral',\n AMAZON_BEDROCK: 'amazon-bedrock',\n AZURE: 'azure',\n COHERE: 'cohere',\n PERPLEXITY: 'perplexity',\n DEEPSEEK: 'deepseek',\n TOGETHERAI: 'togetherai',\n OPENAI_COMPATIBLE: 'openai-compatible',\n} as const;\n\nexport type GatewayProvider = (typeof GATEWAY_PROVIDERS)[keyof typeof GATEWAY_PROVIDERS];\n\nexport type GatewayProviderWithDefaultPrefix = Exclude<GatewayProvider, typeof GATEWAY_PROVIDERS.OPENAI_COMPATIBLE>;\n\nexport interface GatewayProviderOptionsMap {\n [GATEWAY_PROVIDERS.OPENAI_COMPATIBLE]: GatewayOpenAICompatibleOptions;\n}\n\nexport type GatewayProviderOptions<P extends GatewayProvider> = P extends keyof GatewayProviderOptionsMap\n ? GatewayProviderOptionsMap[P]\n : GatewayProviderBaseOptions;\n\nconst GATEWAY_PROVIDER_DEFAULT_PREFIXES: Record<GatewayProviderWithDefaultPrefix, string> = {\n [GATEWAY_PROVIDERS.ANTHROPIC]: 'anthropic',\n [GATEWAY_PROVIDERS.GOOGLE]: 'google',\n [GATEWAY_PROVIDERS.XAI]: 'xai',\n [GATEWAY_PROVIDERS.GROQ]: 'groq',\n [GATEWAY_PROVIDERS.MISTRAL]: 'mistral',\n [GATEWAY_PROVIDERS.AMAZON_BEDROCK]: 'bedrock',\n [GATEWAY_PROVIDERS.AZURE]: 'azure',\n [GATEWAY_PROVIDERS.COHERE]: 'cohere',\n [GATEWAY_PROVIDERS.PERPLEXITY]: 'perplexity',\n [GATEWAY_PROVIDERS.DEEPSEEK]: 'deepseek',\n [GATEWAY_PROVIDERS.TOGETHERAI]: 'togetherai',\n};\n\nfunction prefixModelId(prefix: string, modelId: string): string {\n return modelId.includes('/') ? modelId : `${prefix}/${modelId}`;\n}\n\nfunction createPrefixedGatewayProvider(defaultPrefix: string, options: GatewayProviderBaseOptions): OpenAIProvider {\n const { modelPrefix, ...providerOptions } = options;\n const prefix = modelPrefix ?? defaultPrefix;\n const provider = createAIGatewayProvider(providerOptions);\n\n const wrapped = ((modelId: string) => provider(prefixModelId(prefix, modelId))) as OpenAIProvider;\n\n return new Proxy(wrapped, {\n apply(_target, _thisArg, args: [string, ...unknown[]]) {\n const [modelId, ...rest] = args;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (provider as (...a: unknown[]) => any)(prefixModelId(prefix, modelId), ...rest);\n },\n get(_target, prop) {\n const value = Reflect.get(provider, prop, provider);\n if (typeof value === 'function') {\n return (...args: unknown[]) => {\n if (typeof args[0] === 'string') {\n args[0] = prefixModelId(prefix, args[0] as string);\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (value as (...a: unknown[]) => any).apply(provider, args);\n };\n }\n return value;\n },\n has(_target, prop) {\n return prop in (provider as object);\n },\n });\n}\n\n/**\n * Creates an `OpenAIProvider` backed by AI Gateway for a supported provider.\n *\n * Bare model IDs are automatically prefixed with the provider's canonical\n * Gateway routing prefix. Model IDs that already contain `/` are passed as-is.\n */\nexport function createGatewayProvider(\n provider: GatewayProviderWithDefaultPrefix,\n options: GatewayProviderBaseOptions,\n): OpenAIProvider;\nexport function createGatewayProvider(\n provider: typeof GATEWAY_PROVIDERS.OPENAI_COMPATIBLE,\n options: GatewayOpenAICompatibleOptions,\n): OpenAIProvider;\nexport function createGatewayProvider(\n provider: GatewayProvider,\n options: GatewayProviderBaseOptions | GatewayOpenAICompatibleOptions,\n): OpenAIProvider {\n if (provider === GATEWAY_PROVIDERS.OPENAI_COMPATIBLE) {\n return createPrefixedGatewayProvider(\n (options as GatewayOpenAICompatibleOptions).modelPrefix,\n options as GatewayOpenAICompatibleOptions,\n );\n }\n\n return createPrefixedGatewayProvider(\n GATEWAY_PROVIDER_DEFAULT_PREFIXES[provider as GatewayProviderWithDefaultPrefix],\n options as GatewayProviderBaseOptions,\n );\n}\n"]}
@@ -1,5 +1,5 @@
1
1
  import { ModuleMetadata, Type, InjectionToken, OptionalFactoryDependency, DynamicModule, ExceptionFilter, ArgumentsHost } from '@nestjs/common';
2
- import { G as GatewayProviderSettings, A as AIGatewayError } from '../gateway-errors-DdgDIyQw.cjs';
2
+ import { G as GatewayProviderSettings, A as AIGatewayError } from '../gateway-errors-CPtIbrWq.cjs';
3
3
 
4
4
  /**
5
5
  * Synchronous module configuration.
@@ -1,5 +1,5 @@
1
1
  import { ModuleMetadata, Type, InjectionToken, OptionalFactoryDependency, DynamicModule, ExceptionFilter, ArgumentsHost } from '@nestjs/common';
2
- import { G as GatewayProviderSettings, A as AIGatewayError } from '../gateway-errors-DdgDIyQw.js';
2
+ import { G as GatewayProviderSettings, A as AIGatewayError } from '../gateway-errors-CPtIbrWq.js';
3
3
 
4
4
  /**
5
5
  * Synchronous module configuration.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@macpaw/ai-sdk",
3
- "version": "1.0.0-rc.2",
3
+ "version": "1.0.0-rc.5",
4
4
  "description": "Vercel AI SDK extension layer for AI Gateway with MacPaw and Setapp auth flows",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -56,7 +56,7 @@
56
56
  },
57
57
  "packageManager": "pnpm@10.15.1",
58
58
  "publishConfig": {
59
- "access": "restricted"
59
+ "access": "public"
60
60
  },
61
61
  "scripts": {
62
62
  "build": "tsup",
@@ -100,8 +100,8 @@
100
100
  "url": "https://github.com/MacPaw/ai-sdk-typescript/issues"
101
101
  },
102
102
  "devDependencies": {
103
- "@ai-sdk/openai": "3.0.49",
104
- "@ai-sdk/provider": "3.0.8",
103
+ "@ai-sdk/openai": "3.0.63",
104
+ "@ai-sdk/provider": "3.0.10",
105
105
  "@eslint/js": "^10.0.1",
106
106
  "@nestjs/common": "^11.1.17",
107
107
  "@semantic-release/changelog": "^6.0.3",
@@ -9,6 +9,7 @@ Apply these rules when integrating MacPaw AI Gateway.
9
9
  - `@macpaw/ai-sdk/nestjs`: NestJS module and decorators.
10
10
  - Install `@ai-sdk/openai` when using `createAIGatewayProvider` or `createGatewayProvider`.
11
11
  - Never use `createAIGatewayClient`, `@macpaw/ai-sdk/client`, `runtime`, `types`, or `testing`.
12
+ - For UI hooks or schema helpers, follow the versioned upstream docs for the installed `ai` / `@ai-sdk/react` major version; this package does not redefine those APIs.
12
13
 
13
14
  ## Choose one integration path
14
15
 
@@ -22,7 +23,7 @@ Apply these rules when integrating MacPaw AI Gateway.
22
23
  - Do not invent a token source. Ask once if unclear.
23
24
  - Do not place gateway tokens in browser-only code.
24
25
  - `env` supports only `'production'`; use `baseURL` for staging/custom hosts.
25
- - `createGatewayFetch` requires `baseURL`.
26
+ - `createGatewayFetch` requires a resolved `baseURL`; prefer `resolveGatewayBaseURL()` when you want the default production host.
26
27
  - Remove legacy imports and dependencies only after confirming all usages are migrated.
27
28
 
28
29
  ## Canonical snippets
@@ -9,13 +9,14 @@ Use this guidance when integrating MacPaw AI Gateway into this project.
9
9
  - Keep generation primitives on upstream `ai` / `@ai-sdk/*`.
10
10
  - Install `@ai-sdk/openai` when using `createAIGatewayProvider` or `createGatewayProvider`; those paths depend on the OpenAI-compatible provider package.
11
11
  - Do not use `createAIGatewayClient`, `@macpaw/ai-sdk/client`, `runtime`, `types`, or `testing`; those surfaces do not exist.
12
+ - For UI hooks or schema helpers, follow the versioned upstream docs for the installed `ai` / `@ai-sdk/react` major version; this package does not redefine those APIs.
12
13
 
13
14
  ## Guardrails
14
15
 
15
16
  - Do not invent a token source. If server-side token retrieval is unclear, ask one concise question.
16
17
  - Do not put real gateway tokens in browser-only code.
17
18
  - Use `env: 'production'` only for the default MacPaw host. Use `baseURL` for staging or custom hosts.
18
- - `createGatewayFetch` requires a resolved `baseURL`; do not pass only `env`.
19
+ - `createGatewayFetch` requires a resolved `baseURL`; do not pass only `env`. Prefer `resolveGatewayBaseURL()` when you want the default production host.
19
20
  - Remove old provider dependencies only after verifying there are no remaining usages.
20
21
 
21
22
  ## Preferred paths
@@ -15,6 +15,7 @@ Detect the app shape, choose one integration path, apply the smallest correct pa
15
15
  - Use `@macpaw/ai-sdk/provider` only as a compatibility alias.
16
16
  - Use `@macpaw/ai-sdk/nestjs` for `AIGatewayModule`, `@InjectAIGateway()`, and `AIGatewayExceptionFilter`.
17
17
  - Do not use `createAIGatewayClient`, `@macpaw/ai-sdk/client`, `runtime`, `types`, or `testing`; they do not exist.
18
+ - For UI hooks or schema helpers, follow the versioned upstream docs for the installed `ai` / `@ai-sdk/react` major version; this package does not redefine those APIs.
18
19
 
19
20
  ## Guardrails
20
21
 
@@ -22,7 +23,7 @@ Detect the app shape, choose one integration path, apply the smallest correct pa
22
23
  - Do not place real tokens in browser-only code. Frontend-only apps need a backend/BFF token source.
23
24
  - Do not remove direct OpenAI/Anthropic paths unless all usages are migrated or the user asked for it.
24
25
  - Use `baseURL` for staging/custom hosts. `env` supports only `'production'`.
25
- - `createGatewayFetch` requires a resolved `baseURL`; do not pass only `env`.
26
+ - `createGatewayFetch` requires a resolved `baseURL`; do not pass only `env`. Prefer `resolveGatewayBaseURL()` when you want the default production host.
26
27
 
27
28
  ## Choose one path
28
29