ai-gateway-proxy 0.7.0 → 0.7.1
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 +28 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -113,7 +113,7 @@ import { generateText } from "ai";
|
|
|
113
113
|
|
|
114
114
|
const gateway = createGateway({
|
|
115
115
|
baseURL: "https://your-app.example.com/api/ai",
|
|
116
|
-
//
|
|
116
|
+
// Reaches your route only: the proxy sends its own credential upstream. See Protecting the route.
|
|
117
117
|
apiKey: "unused",
|
|
118
118
|
});
|
|
119
119
|
|
|
@@ -123,6 +123,33 @@ const { text } = await generateText({
|
|
|
123
123
|
});
|
|
124
124
|
```
|
|
125
125
|
|
|
126
|
+
### Protecting the route
|
|
127
|
+
|
|
128
|
+
The proxy authenticates itself to the gateway but does not check who calls it, so a route set up as above answers anyone who can reach it, on your gateway credit. Check the caller before handing the request to the proxy. The client's `apiKey` reaches your route as `authorization: Bearer <apiKey>`, and the proxy replaces that header upstream, so it can carry the caller's own token:
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
import { createGatewayProxy } from "ai-gateway-proxy";
|
|
132
|
+
|
|
133
|
+
const proxy = createGatewayProxy();
|
|
134
|
+
|
|
135
|
+
async function handler(request: Request, context: { params: Promise<{ segments: string[] }> }) {
|
|
136
|
+
const authorization = request.headers.get("authorization");
|
|
137
|
+
const token = authorization?.startsWith("Bearer ") ? authorization.slice(7) : undefined;
|
|
138
|
+
if (!token || !(await isValidSession(token))) {
|
|
139
|
+
// Not authentication_error: @ai-sdk/gateway replaces that message with advice about gateway keys.
|
|
140
|
+
return Response.json(
|
|
141
|
+
{ error: { message: "Sign in to use the AI route", type: "invalid_request_error" } },
|
|
142
|
+
{ status: 401 },
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
return proxy(request, context);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export { handler as GET, handler as POST, handler as PUT, handler as DELETE, handler as PATCH };
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
`isValidSession` stands for your own session or token check. On the client, pass the caller's token as `apiKey`, as in `createGateway({ baseURL, apiKey: sessionToken })`. A rejected call throws a `GatewayInvalidRequestError` with `statusCode` 401 and your message, and the AI SDK does not retry it. Do not reject callers through the `apiKey` option instead: a missing key answers `500`, which clients retry.
|
|
152
|
+
|
|
126
153
|
Some gateway client calls do not go through the proxy:
|
|
127
154
|
|
|
128
155
|
- Streaming speech and transcription (the gateway models' `doStream`) open a WebSocket to `speech-model` or `transcription-model` under `baseURL`. The proxy handles HTTP requests only, so it cannot carry those connections. Generating speech or a transcription without streaming does go through the proxy.
|