ai-gateway-proxy 0.0.1 → 0.0.2
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/README.md +181 -0
- package/package.json +6 -1
package/README.md
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# ai-gateway-proxy
|
|
2
|
+
|
|
3
|
+
A lightweight proxy handler for [Vercel AI Gateway](https://vercel.com/docs/ai/ai-gateway).
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Request/Response Hooks** - Transform requests and responses with `beforeRequest`, `afterResponse`, and `onError` hooks
|
|
8
|
+
- **Streaming Support** - Full SSE streaming with content aggregation using AI SDK V3 protocol
|
|
9
|
+
- **Next.js Integration** - Works seamlessly with Next.js App Router catch-all routes
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install ai-gateway-proxy
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm add ai-gateway-proxy
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
yarn add ai-gateway-proxy
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
Create a catch-all route at `app/api/ai/[...segments]/route.ts`:
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { createGatewayProxy } from "ai-gateway-proxy";
|
|
31
|
+
|
|
32
|
+
export const { GET, POST, PUT, DELETE, PATCH } = createGatewayProxy();
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Authentication
|
|
36
|
+
|
|
37
|
+
The proxy automatically handles authentication using one of the following methods (in priority order):
|
|
38
|
+
|
|
39
|
+
### API Key
|
|
40
|
+
|
|
41
|
+
Set the `AI_GATEWAY_API_KEY` environment variable:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
AI_GATEWAY_API_KEY=your-api-key
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### OIDC (Vercel)
|
|
48
|
+
|
|
49
|
+
When deployed on Vercel, the proxy automatically uses [Vercel OIDC](https://vercel.com/docs/security/oidc) for authentication. No additional configuration is required.
|
|
50
|
+
|
|
51
|
+
## Configuration
|
|
52
|
+
|
|
53
|
+
### Custom Base URL
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
createGatewayProxy({
|
|
57
|
+
baseUrl: "https://ai-gateway.vercel.sh/v1/ai",
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Additional Headers
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
createGatewayProxy({
|
|
65
|
+
headers: {
|
|
66
|
+
"x-custom-header": "value",
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Request Transformation
|
|
72
|
+
|
|
73
|
+
Transform the request body before sending to the gateway:
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
createGatewayProxy({
|
|
77
|
+
beforeRequest: ({ request }) => {
|
|
78
|
+
// Modify the request body
|
|
79
|
+
return {
|
|
80
|
+
...request,
|
|
81
|
+
// Add custom modifications
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Response Transformation
|
|
88
|
+
|
|
89
|
+
Transform the response after receiving from the gateway. For streaming responses, this is called when the stream completes with aggregated content:
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
createGatewayProxy({
|
|
93
|
+
afterResponse: ({ request, response }) => {
|
|
94
|
+
// Log usage, modify metadata, etc.
|
|
95
|
+
console.log("Usage:", response.usage);
|
|
96
|
+
return response;
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Error Handling
|
|
102
|
+
|
|
103
|
+
Custom error handling with the `onError` hook:
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
createGatewayProxy({
|
|
107
|
+
onError: ({ request, error, status }) => {
|
|
108
|
+
// Log errors, modify error response, or return a custom Response
|
|
109
|
+
console.error("Gateway error:", error);
|
|
110
|
+
|
|
111
|
+
// Return modified error
|
|
112
|
+
return error;
|
|
113
|
+
|
|
114
|
+
// Or return a custom Response
|
|
115
|
+
// return new Response("Custom error", { status: 500 });
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## API Reference
|
|
121
|
+
|
|
122
|
+
### `createGatewayProxy(options?)`
|
|
123
|
+
|
|
124
|
+
Creates a proxy handler for the AI Gateway.
|
|
125
|
+
|
|
126
|
+
#### Options
|
|
127
|
+
|
|
128
|
+
| Option | Type | Default | Description |
|
|
129
|
+
|--------|------|---------|-------------|
|
|
130
|
+
| `baseUrl` | `string` | `"https://ai-gateway.vercel.sh/v1/ai"` | The base URL of the AI Gateway |
|
|
131
|
+
| `headers` | `Record<string, string>` | `{}` | Additional headers to include in requests |
|
|
132
|
+
| `beforeRequest` | `(ctx) => request` | - | Transform request before sending |
|
|
133
|
+
| `afterResponse` | `(ctx) => response` | - | Transform response after receiving |
|
|
134
|
+
| `onError` | `(ctx) => error \| Response` | - | Custom error handling |
|
|
135
|
+
|
|
136
|
+
#### Returns
|
|
137
|
+
|
|
138
|
+
A handler function with named exports for HTTP methods: `GET`, `POST`, `PUT`, `DELETE`, `PATCH`.
|
|
139
|
+
|
|
140
|
+
### `getGatewayAuthToken()`
|
|
141
|
+
|
|
142
|
+
Retrieves the authentication token for the AI Gateway.
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
import { getGatewayAuthToken } from "ai-gateway-proxy";
|
|
146
|
+
|
|
147
|
+
const auth = await getGatewayAuthToken();
|
|
148
|
+
// { token: "...", authMethod: "api-key" | "oidc" }
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### `StreamContentAggregator`
|
|
152
|
+
|
|
153
|
+
Aggregates streaming content into a complete response. Used internally by the proxy for `afterResponse` hooks on streaming requests.
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
import { StreamContentAggregator } from "ai-gateway-proxy";
|
|
157
|
+
|
|
158
|
+
const aggregator = new StreamContentAggregator();
|
|
159
|
+
aggregator.process(streamPart);
|
|
160
|
+
const response = aggregator.getResponse();
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### `createStreamTransformer(body, requestBody, afterResponse)`
|
|
164
|
+
|
|
165
|
+
Creates a transform stream that passes through SSE events while aggregating content and calling the `afterResponse` hook on completion.
|
|
166
|
+
|
|
167
|
+
## Types
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
import type {
|
|
171
|
+
CreateGatewayProxyOptions,
|
|
172
|
+
CreateGatewayProxyFn,
|
|
173
|
+
CreateGatewayProxyResult,
|
|
174
|
+
GatewayResponse,
|
|
175
|
+
GatewayError,
|
|
176
|
+
} from "ai-gateway-proxy";
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## License
|
|
180
|
+
|
|
181
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-gateway-proxy",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/SferaDev/SferaDev.git",
|
|
10
|
+
"directory": "packages/ai-gateway-proxy"
|
|
11
|
+
},
|
|
7
12
|
"exports": {
|
|
8
13
|
".": {
|
|
9
14
|
"types": "./dist/index.d.ts",
|