@macpaw/ai-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +39 -0
- package/LICENSE +21 -0
- package/MIGRATION.md +52 -0
- package/README.md +272 -0
- package/dist/chunk-KGOVQRMH.js +287 -0
- package/dist/chunk-KGOVQRMH.js.map +1 -0
- package/dist/chunk-N26BDEG5.cjs +302 -0
- package/dist/chunk-N26BDEG5.cjs.map +1 -0
- package/dist/gateway-errors-DdgDIyQw.d.cts +181 -0
- package/dist/gateway-errors-DdgDIyQw.d.ts +181 -0
- package/dist/index.cjs +483 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +109 -0
- package/dist/index.d.ts +109 -0
- package/dist/index.js +438 -0
- package/dist/index.js.map +1 -0
- package/dist/nestjs/index.cjs +149 -0
- package/dist/nestjs/index.cjs.map +1 -0
- package/dist/nestjs/index.d.cts +117 -0
- package/dist/nestjs/index.d.ts +117 -0
- package/dist/nestjs/index.js +145 -0
- package/dist/nestjs/index.js.map +1 -0
- package/package.json +128 -0
- package/scripts/setup.mjs +90 -0
- package/templates/AGENTS.md +51 -0
- package/templates/CLAUDE.md +55 -0
- package/templates/cursor/skills/integrate-ai-gateway/SKILL.md +90 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# @macpaw/ai-sdk
|
|
2
|
+
|
|
3
|
+
> Changelog is maintained by [semantic-release](https://github.com/semantic-release/semantic-release). Versions and entries are added automatically on release; do not edit version headings manually.
|
|
4
|
+
|
|
5
|
+
## Unreleased
|
|
6
|
+
|
|
7
|
+
### Changed
|
|
8
|
+
|
|
9
|
+
- Flat `src/` architecture — no `src/api/` or `src/client/` subdirectories.
|
|
10
|
+
- `createGatewayFetch` replaces the old low-level client as the escape-hatch for custom HTTP calls.
|
|
11
|
+
- `@ai-sdk/openai` is now a required peer dependency (used directly in `gateway-provider.ts`).
|
|
12
|
+
- `withRetry` now accepts `Required<RetryConfig>` (already-normalised config); callers must not pass un-normalised objects.
|
|
13
|
+
- Auth retry guard: requests with a streaming body (`ReadableStream`) throw `AuthError` immediately instead of attempting a silent re-play of the consumed stream.
|
|
14
|
+
- Test coverage added for `gateway-errors`, `gateway-retry`, `gateway-request` internals, and NestJS decorators.
|
|
15
|
+
- Removed `@macpaw/ai-sdk/types` subpath — domain types are exported from the root entry.
|
|
16
|
+
- Removed `@macpaw/ai-sdk/runtime` subpath — there is no separate runtime surface.
|
|
17
|
+
- Removed lifecycle hooks (`onRequest`, `onResponse`, `onError`, `onRetry`) — not part of the public API.
|
|
18
|
+
- Removed pluggable transport layer — `GatewayProviderSettings.fetch` covers custom fetch injection.
|
|
19
|
+
- Removed `autoRefreshToken` / `tokenCacheTTL` options — token refresh is handled automatically on 401.
|
|
20
|
+
- Vitest fails when no tests are discovered, tightening release-time verification.
|
|
21
|
+
|
|
22
|
+
## 0.1.0
|
|
23
|
+
|
|
24
|
+
### Added
|
|
25
|
+
|
|
26
|
+
- `createAIGatewayProvider` / `createGatewayProvider` — Vercel AI SDK provider for the MacPaw AI Gateway.
|
|
27
|
+
- `GATEWAY_PROVIDERS` — map of supported model providers.
|
|
28
|
+
- `createGatewayFetch` / `GATEWAY_PLACEHOLDER_API_KEY` — escape-hatch for custom HTTP calls to the gateway.
|
|
29
|
+
- Error class hierarchy: `AIGatewayError`, `AuthError`, `CreditsError`, `RateLimitError`, `ModelNotAllowedError`, `GatewayValidationError`.
|
|
30
|
+
- `parseErrorResponse` / `parseStreamErrorPayload` / `parseErrorResponseFromResponse` — normalise both Gateway API and OpenAI proxy error shapes.
|
|
31
|
+
- Auth via `getAuthToken` callback; automatic token refresh on 401 with a single retry.
|
|
32
|
+
- Retry with exponential backoff and jitter (`withRetry`, `RetryConfig`).
|
|
33
|
+
- Middleware pipeline (`Middleware` type, applied per-request).
|
|
34
|
+
- Per-request timeout via `AbortController`; combined `AbortSignal` support via `anySignal`.
|
|
35
|
+
- `X-Request-ID` auto-generation and propagation.
|
|
36
|
+
- NestJS integration: `AIGatewayModule`, `AI_GATEWAY_CONFIG` token, `@InjectAIGateway()` decorator, `AIGatewayExceptionFilter`.
|
|
37
|
+
- Exported types: `GatewayProviderSettings`, `RetryConfig`, `Middleware`, `ErrorCode`, `NormalizedErrorMetadata`, etc.
|
|
38
|
+
- Dual ESM + CJS output via tsup.
|
|
39
|
+
- Strict TypeScript targeting ES2022.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026, MacPaw Way Ltd.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/MIGRATION.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Integration guide — `@macpaw/ai-sdk`
|
|
2
|
+
|
|
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
|
+
|
|
5
|
+
## Entry points
|
|
6
|
+
|
|
7
|
+
| Path | Role |
|
|
8
|
+
| ------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
9
|
+
| `@macpaw/ai-sdk` | **Use this** — `createAIGatewayProvider`, `createGatewayProvider`, `createGatewayFetch`, `GATEWAY_PROVIDERS`, errors, `GatewayProviderSettings` |
|
|
10
|
+
| `@macpaw/ai-sdk/provider` | **Compatibility alias** — same bundle and exports as the root |
|
|
11
|
+
| `@macpaw/ai-sdk/nestjs` | NestJS module, `@InjectAIGateway()`, `AIGatewayExceptionFilter` |
|
|
12
|
+
|
|
13
|
+
The following paths are **not** published in current versions:
|
|
14
|
+
|
|
15
|
+
- `@macpaw/ai-sdk/client`
|
|
16
|
+
- `@macpaw/ai-sdk/runtime`
|
|
17
|
+
- `@macpaw/ai-sdk/types`
|
|
18
|
+
- `@macpaw/ai-sdk/testing`
|
|
19
|
+
|
|
20
|
+
## If you used the old HTTP client
|
|
21
|
+
|
|
22
|
+
Replace `createAIGatewayClient` with:
|
|
23
|
+
|
|
24
|
+
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).
|
|
26
|
+
|
|
27
|
+
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
|
+
|
|
29
|
+
## If you used `@macpaw/ai-sdk/runtime`
|
|
30
|
+
|
|
31
|
+
Internals (`executeRequestPipeline`, etc.) are not part of the public API. Prefer `createGatewayFetch` or provider options (`middleware`, `retry`, `timeout`, `fetch`).
|
|
32
|
+
|
|
33
|
+
## Quick import cheatsheet
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { generateText } from 'ai';
|
|
37
|
+
import { createAIGatewayProvider } from '@macpaw/ai-sdk';
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Equivalent legacy path:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { createAIGatewayProvider } from '@macpaw/ai-sdk/provider';
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## NestJS
|
|
47
|
+
|
|
48
|
+
`@InjectAIGateway()` provides **`GatewayProviderSettings`**. Build `createAIGatewayProvider(config)` (or `createGatewayFetch`) inside services — there is no injected `AIGatewayClient` type in this package.
|
|
49
|
+
|
|
50
|
+
## React
|
|
51
|
+
|
|
52
|
+
Keep `useChat`, `useCompletion`, etc. on **`@ai-sdk/react`** or **`ai/react`**. Configure the server route to use a gateway-backed model from `createAIGatewayProvider`.
|
package/README.md
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
# @macpaw/ai-sdk
|
|
2
|
+
|
|
3
|
+
[](https://github.com/macpaw/ai-sdk-typescript/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/@macpaw/ai-sdk)
|
|
5
|
+
[](./LICENSE)
|
|
6
|
+
|
|
7
|
+
Thin **Vercel AI SDK** extension for **MacPaw AI Gateway**: OpenAI-compatible providers (`createAIGatewayProvider`, `createGatewayProvider`), a **`createGatewayFetch`** bridge for any HTTP client, shared **auth / retry / middleware / errors**, and optional **NestJS** wiring.
|
|
8
|
+
|
|
9
|
+
Core generation APIs stay on upstream **`ai`** and **`@ai-sdk/*`**. This package only adds Gateway-specific construction and the fetch pipeline.
|
|
10
|
+
|
|
11
|
+
## Package entry points
|
|
12
|
+
|
|
13
|
+
| Import | Use for |
|
|
14
|
+
| ------------------------- | --------------------------------------------------------------------- |
|
|
15
|
+
| `@macpaw/ai-sdk` | **Canonical** — providers, `createGatewayFetch`, errors, config types |
|
|
16
|
+
| `@macpaw/ai-sdk/provider` | **Alias** of the root entry (same `dist`; for older snippets) |
|
|
17
|
+
| `@macpaw/ai-sdk/nestjs` | `AIGatewayModule`, `@InjectAIGateway()`, `AIGatewayExceptionFilter` |
|
|
18
|
+
|
|
19
|
+
Upstream **`ai`**, **`@ai-sdk/openai`**, **`@ai-sdk/react`** (or **`ai/react`**) remain the home for Vercel primitives and React hooks.
|
|
20
|
+
|
|
21
|
+
There is **no** published `@macpaw/ai-sdk/client`, `@macpaw/ai-sdk/runtime`, `@macpaw/ai-sdk/types`, or `@macpaw/ai-sdk/testing` in this version — use `createGatewayFetch` + `fetch` (or the OpenAI SDK with custom `fetch`) for raw HTTP and multipart. See [MIGRATION.md](./MIGRATION.md).
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pnpm add @macpaw/ai-sdk
|
|
27
|
+
# or
|
|
28
|
+
npm install @macpaw/ai-sdk
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Also install upstream packages you call directly, for example `ai`, `@ai-sdk/openai`, `@ai-sdk/react`.
|
|
32
|
+
|
|
33
|
+
## Quick start (Vercel AI SDK)
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { generateText, streamText } from 'ai';
|
|
37
|
+
import { createAIGatewayProvider, ErrorCode } from '@macpaw/ai-sdk';
|
|
38
|
+
|
|
39
|
+
const gateway = createAIGatewayProvider({
|
|
40
|
+
env: 'production',
|
|
41
|
+
getAuthToken: async () => (await getSetappSession()).accessToken,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const { text } = await generateText({
|
|
45
|
+
model: gateway('openai/gpt-4.1-nano'),
|
|
46
|
+
prompt: 'Hello from AI Gateway',
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const result = streamText({
|
|
50
|
+
model: gateway('openai/gpt-4.1-nano'),
|
|
51
|
+
prompt: 'Write a poem',
|
|
52
|
+
});
|
|
53
|
+
for await (const delta of result.textStream) {
|
|
54
|
+
process.stdout.write(delta);
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Features
|
|
59
|
+
|
|
60
|
+
- **Vercel-first** — `OpenAIProvider` from `@ai-sdk/openai` + custom `fetch`
|
|
61
|
+
- **Auth** — `getAuthToken(forceRefresh?)`; one automatic retry on **401** with `forceRefresh === true`
|
|
62
|
+
- **Retry** — exponential backoff for **429** and **5xx** (and some network errors); not for 401/402
|
|
63
|
+
- **Middleware** — `(config, next) => Promise<Response>` chain before `fetch`
|
|
64
|
+
- **Errors** — Gateway JSON and OpenAI-shaped bodies → `AIGatewayError` subclasses + `ErrorCode`
|
|
65
|
+
- **Request ID** — `X-Request-ID` on Gateway requests when missing
|
|
66
|
+
- **Timeout** — per attempt, combined with caller `AbortSignal`
|
|
67
|
+
- **Tree-shakeable** — ESM + CJS
|
|
68
|
+
|
|
69
|
+
## Configuration (`GatewayProviderSettings`)
|
|
70
|
+
|
|
71
|
+
Used by `createAIGatewayProvider`, `createGatewayProvider`, `createGatewayFetch`, and Nest `AIGatewayModule`.
|
|
72
|
+
|
|
73
|
+
| Field | Purpose |
|
|
74
|
+
| -------------- | ---------------------------------------------------------------- |
|
|
75
|
+
| `getAuthToken` | Required — `Promise<string \| null>`; `true` = refresh after 401 |
|
|
76
|
+
| `env` | `'production'` → default base URL `https://api.macpaw.com/ai` |
|
|
77
|
+
| `baseURL` | Override gateway root (staging, etc.) |
|
|
78
|
+
| `headers` | Extra headers (do not set `Authorization` here) |
|
|
79
|
+
| `retry` | `RetryConfig` or `false` |
|
|
80
|
+
| `timeout` | ms per attempt (default `60000`) |
|
|
81
|
+
| `middleware` | Interceptor stack |
|
|
82
|
+
| `fetch` | Custom `fetch` implementation |
|
|
83
|
+
|
|
84
|
+
Internal resolution: `resolveConfig()` in `gateway-config.ts`.
|
|
85
|
+
|
|
86
|
+
## `createGatewayFetch` — raw HTTP / multipart
|
|
87
|
+
|
|
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.
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
import { createGatewayFetch } from '@macpaw/ai-sdk';
|
|
92
|
+
|
|
93
|
+
const baseURL = 'https://api.macpaw.com/ai'; // or resolve via env: 'production'
|
|
94
|
+
const gatewayFetch = createGatewayFetch({
|
|
95
|
+
baseURL,
|
|
96
|
+
getAuthToken: async () => token,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const form = new FormData();
|
|
100
|
+
form.append('image', imageBlob, 'photo.png');
|
|
101
|
+
form.append('prompt', 'Add a hat');
|
|
102
|
+
form.append('model', 'openai/dall-e-2');
|
|
103
|
+
|
|
104
|
+
const res = await gatewayFetch('/api/v1/images/edits', { method: 'POST', body: form });
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Non-gateway absolute URLs are passed through without injecting Bearer auth (placeholder key is stripped). See `gateway-fetch.ts`.
|
|
108
|
+
|
|
109
|
+
## `createGatewayProvider` — prefixed model IDs
|
|
110
|
+
|
|
111
|
+
Bare model IDs get a default Gateway prefix per provider constant; IDs that already contain `/` are unchanged.
|
|
112
|
+
|
|
113
|
+
| Constant | Default prefix |
|
|
114
|
+
| ------------------------------------- | ------------------------------------- |
|
|
115
|
+
| `GATEWAY_PROVIDERS.ANTHROPIC` | `anthropic` |
|
|
116
|
+
| `GATEWAY_PROVIDERS.GOOGLE` | `google` |
|
|
117
|
+
| `GATEWAY_PROVIDERS.XAI` | `xai` |
|
|
118
|
+
| `GATEWAY_PROVIDERS.GROQ` | `groq` |
|
|
119
|
+
| `GATEWAY_PROVIDERS.MISTRAL` | `mistral` |
|
|
120
|
+
| `GATEWAY_PROVIDERS.AMAZON_BEDROCK` | `bedrock` |
|
|
121
|
+
| `GATEWAY_PROVIDERS.AZURE` | `azure` |
|
|
122
|
+
| `GATEWAY_PROVIDERS.COHERE` | `cohere` |
|
|
123
|
+
| `GATEWAY_PROVIDERS.PERPLEXITY` | `perplexity` |
|
|
124
|
+
| `GATEWAY_PROVIDERS.DEEPSEEK` | `deepseek` |
|
|
125
|
+
| `GATEWAY_PROVIDERS.TOGETHERAI` | `togetherai` |
|
|
126
|
+
| `GATEWAY_PROVIDERS.OPENAI_COMPATIBLE` | **requires** `modelPrefix` in options |
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
import { generateText } from 'ai';
|
|
130
|
+
import { createGatewayProvider, GATEWAY_PROVIDERS } from '@macpaw/ai-sdk';
|
|
131
|
+
|
|
132
|
+
const anthropic = createGatewayProvider(GATEWAY_PROVIDERS.ANTHROPIC, {
|
|
133
|
+
env: 'production',
|
|
134
|
+
getAuthToken: async () => token,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
await generateText({
|
|
138
|
+
model: anthropic('claude-sonnet-4-20250514'),
|
|
139
|
+
prompt: 'Hello',
|
|
140
|
+
});
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Provider options (`AIGatewayProviderOptions`)
|
|
144
|
+
|
|
145
|
+
Extends `GatewayProviderSettings` plus OpenAI provider settings (without `apiKey` / `baseURL` / `fetch`, which are wired by the SDK):
|
|
146
|
+
|
|
147
|
+
- `normalizeErrors` — default `true`; non-OK Gateway responses throw typed errors
|
|
148
|
+
- `createOpenAI` — optional override of `createOpenAI` from `@ai-sdk/openai` (tests/advanced)
|
|
149
|
+
|
|
150
|
+
## Middleware
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
import type { Middleware } from '@macpaw/ai-sdk';
|
|
154
|
+
|
|
155
|
+
const loggingMiddleware: Middleware = async (config, next) => {
|
|
156
|
+
const response = await next(config);
|
|
157
|
+
console.log(config.method, config.url, response.status);
|
|
158
|
+
return response;
|
|
159
|
+
};
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Error handling
|
|
163
|
+
|
|
164
|
+
| `ErrorCode` | Typical HTTP | Meaning |
|
|
165
|
+
| --------------------------------------------- | ------------ | -------------------------------------- |
|
|
166
|
+
| `AuthRequired` | 401 | Token missing / expired |
|
|
167
|
+
| `InsufficientCredits` / `SubscriptionExpired` | 402 | Billing / subscription |
|
|
168
|
+
| `ModelNotAllowed` | 403 | Model denied |
|
|
169
|
+
| `RateLimited` | 429 | Rate limit (`retryAfter` when present) |
|
|
170
|
+
| `Validation` | 422 | Validation body |
|
|
171
|
+
| … | … | See `gateway-errors.ts` |
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
import { AIGatewayError, ErrorCode, isAIGatewayError } from '@macpaw/ai-sdk';
|
|
175
|
+
|
|
176
|
+
try {
|
|
177
|
+
// ...
|
|
178
|
+
} catch (e) {
|
|
179
|
+
if (isAIGatewayError(e) && e.code === ErrorCode.InsufficientCredits) {
|
|
180
|
+
// e.metadata.paymentUrl, e.requestId, etc.
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## NestJS
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
pnpm add @macpaw/ai-sdk @nestjs/common rxjs
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Register once (global by default):
|
|
192
|
+
|
|
193
|
+
```ts
|
|
194
|
+
import { AIGatewayModule } from '@macpaw/ai-sdk/nestjs';
|
|
195
|
+
|
|
196
|
+
AIGatewayModule.forRoot({
|
|
197
|
+
env: 'production',
|
|
198
|
+
getAuthToken: async () => process.env.SETAPP_TOKEN!,
|
|
199
|
+
});
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
Inject **`GatewayProviderSettings`** (not an HTTP client) and build providers in the service:
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
import { Injectable } from '@nestjs/common';
|
|
206
|
+
import { InjectAIGateway } from '@macpaw/ai-sdk/nestjs';
|
|
207
|
+
import type { GatewayProviderSettings } from '@macpaw/ai-sdk';
|
|
208
|
+
import { createAIGatewayProvider } from '@macpaw/ai-sdk';
|
|
209
|
+
import { generateText } from 'ai';
|
|
210
|
+
|
|
211
|
+
@Injectable()
|
|
212
|
+
export class ChatService {
|
|
213
|
+
constructor(@InjectAIGateway() private readonly config: GatewayProviderSettings) {}
|
|
214
|
+
|
|
215
|
+
async complete(prompt: string) {
|
|
216
|
+
const gateway = createAIGatewayProvider(this.config);
|
|
217
|
+
const { text } = await generateText({
|
|
218
|
+
model: gateway('openai/gpt-4.1-nano'),
|
|
219
|
+
prompt,
|
|
220
|
+
});
|
|
221
|
+
return text;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
`AIGatewayExceptionFilter` maps `AIGatewayError` to JSON HTTP responses. See `examples/nestjs/` for a copy-paste skeleton.
|
|
227
|
+
|
|
228
|
+
## Examples
|
|
229
|
+
|
|
230
|
+
From the repo root:
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
pnpm build
|
|
234
|
+
pnpm example:provider
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Set `AI_GATEWAY_TOKEN` or `SETAPP_TOKEN`. Optional: `AI_GATEWAY_BASE_URL`, `AI_GATEWAY_MODEL`.
|
|
238
|
+
|
|
239
|
+
See [`examples/README.md`](./examples/README.md).
|
|
240
|
+
|
|
241
|
+
## Release & quality
|
|
242
|
+
|
|
243
|
+
- CI: `typecheck`, `lint`, `test`, coverage, `build` on Node 18 / 20 / 22
|
|
244
|
+
- `pnpm verify:release` — full local gate before publish
|
|
245
|
+
- `pnpm size:pack` — dry-run npm pack
|
|
246
|
+
|
|
247
|
+
## AI assistant setup
|
|
248
|
+
|
|
249
|
+
Templates for **Cursor** (`.cursor/skills/`), **Claude Code** (`CLAUDE.md`), and **OpenAI Codex** (`AGENTS.md`) ship under `templates/`. After installing the package:
|
|
250
|
+
|
|
251
|
+
```bash
|
|
252
|
+
pnpm exec macpaw-ai-setup
|
|
253
|
+
# or: npx macpaw-ai-setup
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
Use `macpaw-ai-setup cursor`, `claude`, or `codex` to install only one target. Existing root `CLAUDE.md` / `AGENTS.md` files get Gateway sections appended, not replaced.
|
|
257
|
+
|
|
258
|
+
The installed instructions enforce the current package surface and the main auth guardrails:
|
|
259
|
+
|
|
260
|
+
- prefer `@macpaw/ai-sdk` / `@macpaw/ai-sdk/nestjs`
|
|
261
|
+
- keep generation primitives on upstream `ai` / `@ai-sdk/*`
|
|
262
|
+
- do not use removed subpaths such as `client`, `runtime`, `types`, or `testing`
|
|
263
|
+
- do not invent a token source or expose gateway tokens to browser-only code
|
|
264
|
+
- use `baseURL` for staging/custom hosts; `env` supports only `'production'`
|
|
265
|
+
|
|
266
|
+
## Versioning
|
|
267
|
+
|
|
268
|
+
[Semantic Versioning](https://semver.org/). Releases via [semantic-release](https://github.com/semantic-release/semantic-release) and Conventional Commits.
|
|
269
|
+
|
|
270
|
+
## License
|
|
271
|
+
|
|
272
|
+
MIT © 2026 [MacPaw Way Ltd](https://macpaw.com). See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : /* @__PURE__ */ Symbol.for("Symbol." + name);
|
|
5
|
+
var __typeError = (msg) => {
|
|
6
|
+
throw TypeError(msg);
|
|
7
|
+
};
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
10
|
+
var __decoratorStart = (base) => [, , , __create(base?.[__knownSymbol("metadata")] ?? null)];
|
|
11
|
+
var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
|
|
12
|
+
var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
|
|
13
|
+
var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
|
|
14
|
+
var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
|
|
15
|
+
var __runInitializers = (array, flags, self, value) => {
|
|
16
|
+
for (var i = 0, fns = array[flags >> 1], n = fns && fns.length; i < n; i++) flags & 1 ? fns[i].call(self) : value = fns[i].call(self, value);
|
|
17
|
+
return value;
|
|
18
|
+
};
|
|
19
|
+
var __decorateElement = (array, flags, name, decorators, target, extra) => {
|
|
20
|
+
var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
|
|
21
|
+
var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
|
|
22
|
+
var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
|
|
23
|
+
var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : { get [name]() {
|
|
24
|
+
return __privateGet(this, extra);
|
|
25
|
+
}, set [name](x) {
|
|
26
|
+
return __privateSet(this, extra, x);
|
|
27
|
+
} }, name));
|
|
28
|
+
k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name);
|
|
29
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
30
|
+
ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
|
|
31
|
+
if (k) {
|
|
32
|
+
ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => name in x };
|
|
33
|
+
if (k ^ 3) access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name];
|
|
34
|
+
if (k > 2) access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y;
|
|
35
|
+
}
|
|
36
|
+
it = (0, decorators[i])(k ? k < 4 ? p ? extra : desc[key] : k > 4 ? void 0 : { get: desc.get, set: desc.set } : target, ctx), done._ = 1;
|
|
37
|
+
if (k ^ 4 || it === void 0) __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
|
|
38
|
+
else if (typeof it !== "object" || it === null) __typeError("Object expected");
|
|
39
|
+
else __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
|
|
40
|
+
}
|
|
41
|
+
return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
|
|
42
|
+
};
|
|
43
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
44
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
45
|
+
var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
|
|
46
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
47
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
48
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
49
|
+
|
|
50
|
+
// src/gateway-errors.ts
|
|
51
|
+
var GatewayApiCode = {
|
|
52
|
+
BadRequest: "BAD_REQUEST",
|
|
53
|
+
Unauthorized: "UNAUTHORIZED",
|
|
54
|
+
InsufficientCredits: "INSUFFICIENT_CREDITS",
|
|
55
|
+
Forbidden: "FORBIDDEN",
|
|
56
|
+
Validation: "VALIDATION",
|
|
57
|
+
RateLimitExceeded: "RATE_LIMIT_EXCEEDED",
|
|
58
|
+
InternalServerError: "INTERNAL_SERVER_ERROR",
|
|
59
|
+
ServiceUnavailable: "SERVICE_UNAVAILABLE",
|
|
60
|
+
Timeout: "TIMEOUT",
|
|
61
|
+
NotFound: "NOT_FOUND",
|
|
62
|
+
Conflict: "CONFLICT"
|
|
63
|
+
};
|
|
64
|
+
var ErrorCode = {
|
|
65
|
+
AuthRequired: "AUTH_REQUIRED",
|
|
66
|
+
InsufficientCredits: "INSUFFICIENT_CREDITS",
|
|
67
|
+
SubscriptionExpired: "SUBSCRIPTION_EXPIRED",
|
|
68
|
+
ModelNotAllowed: "MODEL_NOT_ALLOWED",
|
|
69
|
+
RateLimited: "RATE_LIMITED",
|
|
70
|
+
BadRequest: "BAD_REQUEST",
|
|
71
|
+
Validation: "VALIDATION",
|
|
72
|
+
Forbidden: "FORBIDDEN",
|
|
73
|
+
InternalServerError: "INTERNAL_SERVER_ERROR",
|
|
74
|
+
ServiceUnavailable: "SERVICE_UNAVAILABLE",
|
|
75
|
+
Timeout: "TIMEOUT",
|
|
76
|
+
NotFound: "NOT_FOUND",
|
|
77
|
+
Conflict: "CONFLICT"
|
|
78
|
+
};
|
|
79
|
+
var AIGatewayError = class extends Error {
|
|
80
|
+
constructor(message, code, statusCode, metadata = {}, options) {
|
|
81
|
+
super(message);
|
|
82
|
+
__publicField(this, "code");
|
|
83
|
+
__publicField(this, "statusCode");
|
|
84
|
+
__publicField(this, "metadata");
|
|
85
|
+
this.name = "AIGatewayError";
|
|
86
|
+
this.code = code;
|
|
87
|
+
this.statusCode = statusCode;
|
|
88
|
+
this.metadata = metadata;
|
|
89
|
+
if (options?.cause !== void 0) {
|
|
90
|
+
this.cause = options.cause;
|
|
91
|
+
}
|
|
92
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
93
|
+
}
|
|
94
|
+
get paymentUrl() {
|
|
95
|
+
return this.metadata.paymentUrl;
|
|
96
|
+
}
|
|
97
|
+
/** Suggested delay before retrying, in seconds. `undefined` if not provided by the server. */
|
|
98
|
+
get retryAfter() {
|
|
99
|
+
return this.metadata.retryAfter;
|
|
100
|
+
}
|
|
101
|
+
get requestId() {
|
|
102
|
+
return this.metadata.requestId;
|
|
103
|
+
}
|
|
104
|
+
toJSON() {
|
|
105
|
+
return {
|
|
106
|
+
name: this.name,
|
|
107
|
+
message: this.message,
|
|
108
|
+
code: this.code,
|
|
109
|
+
statusCode: this.statusCode,
|
|
110
|
+
metadata: this.metadata
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
var AuthError = class extends AIGatewayError {
|
|
115
|
+
constructor(message, statusCode, metadata, options) {
|
|
116
|
+
super(message, ErrorCode.AuthRequired, statusCode, metadata, options);
|
|
117
|
+
this.name = "AuthError";
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
var CreditsError = class extends AIGatewayError {
|
|
121
|
+
constructor(message, statusCode, code, metadata, options) {
|
|
122
|
+
super(message, code, statusCode, metadata, options);
|
|
123
|
+
this.name = "CreditsError";
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
var RateLimitError = class extends AIGatewayError {
|
|
127
|
+
constructor(message, statusCode, metadata, options) {
|
|
128
|
+
super(message, ErrorCode.RateLimited, statusCode, metadata, options);
|
|
129
|
+
this.name = "RateLimitError";
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
var ModelNotAllowedError = class extends AIGatewayError {
|
|
133
|
+
constructor(message, statusCode, metadata, options) {
|
|
134
|
+
super(message, ErrorCode.ModelNotAllowed, statusCode, metadata, options);
|
|
135
|
+
this.name = "ModelNotAllowedError";
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
var GatewayValidationError = class extends AIGatewayError {
|
|
139
|
+
constructor(message, statusCode, metadata, options) {
|
|
140
|
+
super(message, ErrorCode.Validation, statusCode, metadata, options);
|
|
141
|
+
this.name = "GatewayValidationError";
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
function isAIGatewayError(value) {
|
|
145
|
+
return value instanceof AIGatewayError;
|
|
146
|
+
}
|
|
147
|
+
function mapGatewayApiCodeToNormalized(code, statusCode) {
|
|
148
|
+
switch (code) {
|
|
149
|
+
case GatewayApiCode.Unauthorized:
|
|
150
|
+
return ErrorCode.AuthRequired;
|
|
151
|
+
case GatewayApiCode.InsufficientCredits:
|
|
152
|
+
return statusCode === 402 ? ErrorCode.InsufficientCredits : ErrorCode.SubscriptionExpired;
|
|
153
|
+
case GatewayApiCode.Forbidden:
|
|
154
|
+
return ErrorCode.ModelNotAllowed;
|
|
155
|
+
case GatewayApiCode.RateLimitExceeded:
|
|
156
|
+
return ErrorCode.RateLimited;
|
|
157
|
+
case GatewayApiCode.BadRequest:
|
|
158
|
+
return ErrorCode.BadRequest;
|
|
159
|
+
case GatewayApiCode.Validation:
|
|
160
|
+
return ErrorCode.Validation;
|
|
161
|
+
case GatewayApiCode.InternalServerError:
|
|
162
|
+
return ErrorCode.InternalServerError;
|
|
163
|
+
case GatewayApiCode.ServiceUnavailable:
|
|
164
|
+
return ErrorCode.ServiceUnavailable;
|
|
165
|
+
case GatewayApiCode.Timeout:
|
|
166
|
+
return ErrorCode.Timeout;
|
|
167
|
+
case GatewayApiCode.NotFound:
|
|
168
|
+
return ErrorCode.NotFound;
|
|
169
|
+
case GatewayApiCode.Conflict:
|
|
170
|
+
return ErrorCode.Conflict;
|
|
171
|
+
default:
|
|
172
|
+
return statusCode >= 500 ? ErrorCode.InternalServerError : ErrorCode.BadRequest;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
function mapOpenAIErrorToNormalized(type, statusCode) {
|
|
176
|
+
if (type === "authentication_error") return ErrorCode.AuthRequired;
|
|
177
|
+
if (type === "rate_limit_error") return ErrorCode.RateLimited;
|
|
178
|
+
if (type === "team_model_access_denied") return ErrorCode.ModelNotAllowed;
|
|
179
|
+
if (type === "api_error") return ErrorCode.InternalServerError;
|
|
180
|
+
if (type === "invalid_request_error") return ErrorCode.BadRequest;
|
|
181
|
+
if (statusCode === 401) return ErrorCode.AuthRequired;
|
|
182
|
+
if (statusCode === 429) return ErrorCode.RateLimited;
|
|
183
|
+
if (statusCode === 403) return ErrorCode.ModelNotAllowed;
|
|
184
|
+
return statusCode !== void 0 && statusCode >= 400 && statusCode < 500 ? ErrorCode.BadRequest : ErrorCode.InternalServerError;
|
|
185
|
+
}
|
|
186
|
+
function parseRetryAfterHeader(value) {
|
|
187
|
+
const seconds = Number(value);
|
|
188
|
+
if (!Number.isNaN(seconds) && seconds >= 0) return seconds;
|
|
189
|
+
const date = Date.parse(value);
|
|
190
|
+
if (!Number.isNaN(date)) {
|
|
191
|
+
const delta = Math.ceil((date - Date.now()) / 1e3);
|
|
192
|
+
return delta > 0 ? delta : 0;
|
|
193
|
+
}
|
|
194
|
+
return void 0;
|
|
195
|
+
}
|
|
196
|
+
function createTypedError(message, code, statusCode, meta, options) {
|
|
197
|
+
switch (code) {
|
|
198
|
+
case ErrorCode.AuthRequired:
|
|
199
|
+
return new AuthError(message, statusCode, meta, options);
|
|
200
|
+
case ErrorCode.InsufficientCredits:
|
|
201
|
+
case ErrorCode.SubscriptionExpired:
|
|
202
|
+
return new CreditsError(message, statusCode, code, meta, options);
|
|
203
|
+
case ErrorCode.RateLimited:
|
|
204
|
+
return new RateLimitError(message, statusCode, meta, options);
|
|
205
|
+
case ErrorCode.ModelNotAllowed:
|
|
206
|
+
return new ModelNotAllowedError(message, statusCode, meta, options);
|
|
207
|
+
case ErrorCode.Validation:
|
|
208
|
+
return new GatewayValidationError(message, statusCode, meta, options);
|
|
209
|
+
default:
|
|
210
|
+
return new AIGatewayError(message, code, statusCode, meta, options);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
async function parseErrorResponseFromResponse(response) {
|
|
214
|
+
const contentType = response.headers.get("Content-Type") ?? "";
|
|
215
|
+
let body;
|
|
216
|
+
if (contentType.includes("application/json")) {
|
|
217
|
+
try {
|
|
218
|
+
body = await response.json();
|
|
219
|
+
} catch {
|
|
220
|
+
body = { message: response.statusText };
|
|
221
|
+
}
|
|
222
|
+
} else {
|
|
223
|
+
body = { message: await response.text().catch(() => response.statusText) };
|
|
224
|
+
}
|
|
225
|
+
const retryAfterHeader = response.headers.get("Retry-After");
|
|
226
|
+
let headerRetryAfter;
|
|
227
|
+
if (retryAfterHeader) {
|
|
228
|
+
headerRetryAfter = parseRetryAfterHeader(retryAfterHeader);
|
|
229
|
+
}
|
|
230
|
+
try {
|
|
231
|
+
parseErrorResponse(response.status, body);
|
|
232
|
+
} catch (error) {
|
|
233
|
+
if (error instanceof AIGatewayError && error.retryAfter == null && headerRetryAfter != null) {
|
|
234
|
+
throw createTypedError(
|
|
235
|
+
error.message,
|
|
236
|
+
error.code,
|
|
237
|
+
error.statusCode,
|
|
238
|
+
{ ...error.metadata, retryAfter: headerRetryAfter },
|
|
239
|
+
error.cause != null ? { cause: error.cause } : void 0
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
throw error;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
function parseErrorResponse(statusCode, body) {
|
|
246
|
+
const meta = {};
|
|
247
|
+
if (body && typeof body === "object") {
|
|
248
|
+
if ("request_id" in body && typeof body.request_id === "string") {
|
|
249
|
+
meta.requestId = body.request_id;
|
|
250
|
+
}
|
|
251
|
+
if ("path" in body && typeof body.path === "string") {
|
|
252
|
+
meta.path = body.path;
|
|
253
|
+
}
|
|
254
|
+
if ("timestamp" in body && typeof body.timestamp === "string") {
|
|
255
|
+
meta.timestamp = body.timestamp;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
const gatewayBody = body;
|
|
259
|
+
if (gatewayBody?.statusCode != null && typeof gatewayBody.message === "string" && gatewayBody.code) {
|
|
260
|
+
const code = mapGatewayApiCodeToNormalized(gatewayBody.code, gatewayBody.statusCode);
|
|
261
|
+
if (gatewayBody.errors?.length) meta.errors = gatewayBody.errors;
|
|
262
|
+
const firstError = gatewayBody.errors?.[0];
|
|
263
|
+
if (firstError?.metadata) {
|
|
264
|
+
if (typeof firstError.metadata.paymentUrl === "string") meta.paymentUrl = firstError.metadata.paymentUrl;
|
|
265
|
+
if (typeof firstError.metadata.retryAfter === "number") meta.retryAfter = firstError.metadata.retryAfter;
|
|
266
|
+
}
|
|
267
|
+
throw createTypedError(gatewayBody.message, code, gatewayBody.statusCode, meta);
|
|
268
|
+
}
|
|
269
|
+
const oai = body;
|
|
270
|
+
if (oai?.error?.message) {
|
|
271
|
+
const code = mapOpenAIErrorToNormalized(oai.error.type, statusCode);
|
|
272
|
+
if (oai.request_id) meta.requestId = oai.request_id;
|
|
273
|
+
throw createTypedError(oai.error.message, code, statusCode, meta);
|
|
274
|
+
}
|
|
275
|
+
const message = typeof body?.message === "string" ? body.message : `Request failed with status ${statusCode}`;
|
|
276
|
+
let fallbackCode;
|
|
277
|
+
if (statusCode === 401) fallbackCode = ErrorCode.AuthRequired;
|
|
278
|
+
else if (statusCode === 403) fallbackCode = ErrorCode.Forbidden;
|
|
279
|
+
else if (statusCode === 429) fallbackCode = ErrorCode.RateLimited;
|
|
280
|
+
else if (statusCode >= 500) fallbackCode = ErrorCode.InternalServerError;
|
|
281
|
+
else fallbackCode = ErrorCode.BadRequest;
|
|
282
|
+
throw createTypedError(message, fallbackCode, statusCode, meta);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export { AIGatewayError, AuthError, CreditsError, ErrorCode, GatewayApiCode, GatewayValidationError, ModelNotAllowedError, RateLimitError, __decorateElement, __decoratorStart, __runInitializers, isAIGatewayError, parseErrorResponse, parseErrorResponseFromResponse };
|
|
286
|
+
//# sourceMappingURL=chunk-KGOVQRMH.js.map
|
|
287
|
+
//# sourceMappingURL=chunk-KGOVQRMH.js.map
|