ai-gateway-proxy 0.5.0 → 0.6.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/README.md +2 -2
- package/dist/index.cjs +26 -1
- package/dist/index.mjs +26 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -155,7 +155,7 @@ The proxy gets a credential on every request with `getGatewayAuthToken()`, in th
|
|
|
155
155
|
| `beforeRawRequest` | none | `({ kind, path, modelId, request }) => unknown`. Runs on the calls that are not language model calls; see below. |
|
|
156
156
|
| `afterRawResponse` | none | `({ kind, path, modelId, request, response }) => Response \| undefined`. Runs on the response to those calls; see below. |
|
|
157
157
|
|
|
158
|
-
Each hook may also return a promise. `beforeRequest`, `afterResponse` and `onError` are typed for language model calls, and every hook runs only for requests with a body. Their `modelId` is the client's `ai-language-model-id` header, such as `openai/gpt-5`, since the call body does not name the model; it is `undefined` when the request has no such header. A `GET` or `HEAD` request, such as the provider's `GET /config` model list, and any other request whose body is missing or empty, such as a bodyless `DELETE`, is forwarded without a body and without running any hook, and the upstream status and body come back unchanged, except that a redirect is answered with a 502 as below. A request body that neither `beforeRequest` nor `beforeRawRequest` receives is sent upstream as the client sent it; once either hook runs, the body it returns or leaves in place is serialised again.
|
|
158
|
+
Each hook may also return a promise. `beforeRequest`, `afterResponse` and `onError` are typed for language model calls, and every hook runs only for requests with a body. Their `modelId` is the client's `ai-language-model-id` header, such as `openai/gpt-5`, since the call body does not name the model; it is `undefined` when the request has no such header. A `GET` or `HEAD` request, such as the provider's `GET /config` model list, and any other request whose body is missing or empty, such as a bodyless `DELETE`, is forwarded without a body and without running any hook, and the upstream status and body come back unchanged, except that a redirect is answered with a 502 and a gateway `authentication_error` is retyped as below. A request body that neither `beforeRequest` nor `beforeRawRequest` receives is sent upstream as the client sent it; once either hook runs, the body it returns or leaves in place is serialised again.
|
|
159
159
|
|
|
160
160
|
Path matching fails closed: the language model hooks run on every request with a body except one whose path relative to `baseUrl` is exactly one of the paths `@ai-sdk/gateway` uses for calls that are not language model calls:
|
|
161
161
|
|
|
@@ -250,7 +250,7 @@ Apart from forwarding the gateway's own responses, the handler can answer with a
|
|
|
250
250
|
| Status | Type | Message | When |
|
|
251
251
|
| --- | --- | --- | --- |
|
|
252
252
|
| 500 | `proxy_error` | `AI Gateway authentication not configured: set AI_GATEWAY_API_KEY or deploy on Vercel with OIDC enabled` | `getGatewayAuthToken()` returned `null`. The type is not `authentication_error`, which `@ai-sdk/gateway` replaces with advice about the client's own key. |
|
|
253
|
-
| upstream, such as 401 | `proxy_error` | `AI Gateway rejected the proxy's credential: <message>` | The gateway answered a
|
|
253
|
+
| upstream, such as 401 | `proxy_error` | `AI Gateway rejected the proxy's credential: <message>` | The gateway answered with an error typed `authentication_error`, for a call with a body or a bodyless one such as `GET /config`; see `onError` above. The other fields of the error are kept. |
|
|
254
254
|
| 400 | `invalid_request_error` | `Invalid path` | The path is not a safe relative path under `baseUrl`. |
|
|
255
255
|
| 400 | `invalid_request_error` | `Invalid JSON body` | A request other than `GET` or `HEAD` has a non-empty body that is not JSON. |
|
|
256
256
|
| 400 | `invalid_request_error` | `Invalid request body: a language model call must be a JSON object` | The body is JSON but not an object, such as an array or `null`, on a path that runs `beforeRequest`, `afterResponse` or `onError`, and one of them is set. No hook runs and the gateway is not called. |
|
package/dist/index.cjs
CHANGED
|
@@ -515,6 +515,7 @@ const NULL_BODY_STATUS = new Set([
|
|
|
515
515
|
const ANSWERED_CALL_FAILURE_STATUS = 424;
|
|
516
516
|
// Successful bodies that are a sequence of values rather than one JSON value: event streams and JSON Lines.
|
|
517
517
|
const STREAM_CONTENT_TYPE = /^\s*(?:text\/event-stream|application\/(?:x-ndjson|jsonl|jsonlines|x-jsonlines))\s*(?:;|$)/i;
|
|
518
|
+
const JSON_CONTENT_TYPE = /^\s*application\/(?:[\w.+-]+\+)?json\s*(?:;|$)/i;
|
|
518
519
|
/**
|
|
519
520
|
* Extracts the path from the request using either a custom extractor or route params.
|
|
520
521
|
*/ async function extractPathFromRequest(request, context, options) {
|
|
@@ -652,11 +653,35 @@ function proxyFailureResponse(request, error) {
|
|
|
652
653
|
if (isRedirect(res)) {
|
|
653
654
|
return redirectRefusedResponse(res);
|
|
654
655
|
}
|
|
655
|
-
return
|
|
656
|
+
return await bodylessUpstreamResponse(res);
|
|
656
657
|
} catch (error) {
|
|
657
658
|
return proxyFailureResponse(request, error);
|
|
658
659
|
}
|
|
659
660
|
}
|
|
661
|
+
/**
|
|
662
|
+
* Passes a bodyless request's response through, except that a JSON error typed
|
|
663
|
+
* `authentication_error` is retyped as for a call with a body. Only JSON error bodies are read.
|
|
664
|
+
*/ async function bodylessUpstreamResponse(res) {
|
|
665
|
+
const contentType = res.headers.get("content-type") ?? "application/json";
|
|
666
|
+
if (res.ok || !res.body || !JSON_CONTENT_TYPE.test(contentType)) {
|
|
667
|
+
return passThroughResponse(res);
|
|
668
|
+
}
|
|
669
|
+
const text = await res.text();
|
|
670
|
+
const parsed = parseJsonOrUndefined(text);
|
|
671
|
+
const body = isGatewayError(parsed) && parsed.error.type === "authentication_error" ? JSON.stringify(retypeProxyCredentialError(parsed)) : text;
|
|
672
|
+
return new Response(body, {
|
|
673
|
+
status: res.status,
|
|
674
|
+
statusText: res.statusText,
|
|
675
|
+
headers: responseHeaders(res, contentType)
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
function parseJsonOrUndefined(text) {
|
|
679
|
+
try {
|
|
680
|
+
return JSON.parse(text);
|
|
681
|
+
} catch {
|
|
682
|
+
return undefined;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
660
685
|
function isGatewayError(value) {
|
|
661
686
|
if (typeof value !== "object" || value === null) {
|
|
662
687
|
return false;
|
package/dist/index.mjs
CHANGED
|
@@ -513,6 +513,7 @@ const NULL_BODY_STATUS = new Set([
|
|
|
513
513
|
const ANSWERED_CALL_FAILURE_STATUS = 424;
|
|
514
514
|
// Successful bodies that are a sequence of values rather than one JSON value: event streams and JSON Lines.
|
|
515
515
|
const STREAM_CONTENT_TYPE = /^\s*(?:text\/event-stream|application\/(?:x-ndjson|jsonl|jsonlines|x-jsonlines))\s*(?:;|$)/i;
|
|
516
|
+
const JSON_CONTENT_TYPE = /^\s*application\/(?:[\w.+-]+\+)?json\s*(?:;|$)/i;
|
|
516
517
|
/**
|
|
517
518
|
* Extracts the path from the request using either a custom extractor or route params.
|
|
518
519
|
*/ async function extractPathFromRequest(request, context, options) {
|
|
@@ -650,11 +651,35 @@ function proxyFailureResponse(request, error) {
|
|
|
650
651
|
if (isRedirect(res)) {
|
|
651
652
|
return redirectRefusedResponse(res);
|
|
652
653
|
}
|
|
653
|
-
return
|
|
654
|
+
return await bodylessUpstreamResponse(res);
|
|
654
655
|
} catch (error) {
|
|
655
656
|
return proxyFailureResponse(request, error);
|
|
656
657
|
}
|
|
657
658
|
}
|
|
659
|
+
/**
|
|
660
|
+
* Passes a bodyless request's response through, except that a JSON error typed
|
|
661
|
+
* `authentication_error` is retyped as for a call with a body. Only JSON error bodies are read.
|
|
662
|
+
*/ async function bodylessUpstreamResponse(res) {
|
|
663
|
+
const contentType = res.headers.get("content-type") ?? "application/json";
|
|
664
|
+
if (res.ok || !res.body || !JSON_CONTENT_TYPE.test(contentType)) {
|
|
665
|
+
return passThroughResponse(res);
|
|
666
|
+
}
|
|
667
|
+
const text = await res.text();
|
|
668
|
+
const parsed = parseJsonOrUndefined(text);
|
|
669
|
+
const body = isGatewayError(parsed) && parsed.error.type === "authentication_error" ? JSON.stringify(retypeProxyCredentialError(parsed)) : text;
|
|
670
|
+
return new Response(body, {
|
|
671
|
+
status: res.status,
|
|
672
|
+
statusText: res.statusText,
|
|
673
|
+
headers: responseHeaders(res, contentType)
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
function parseJsonOrUndefined(text) {
|
|
677
|
+
try {
|
|
678
|
+
return JSON.parse(text);
|
|
679
|
+
} catch {
|
|
680
|
+
return undefined;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
658
683
|
function isGatewayError(value) {
|
|
659
684
|
if (typeof value !== "object" || value === null) {
|
|
660
685
|
return false;
|