@ai-sdk/gateway 1.0.0-beta.1 → 1.0.0-beta.3
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 +18 -0
- package/dist/index.d.mts +15 -29
- package/dist/index.d.ts +15 -29
- package/dist/index.js +151 -56
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +132 -37
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @ai-sdk/gateway
|
|
2
2
|
|
|
3
|
+
## 1.0.0-beta.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- f3639fa: feat (providers/gateway): improve oidc api key client auth flow
|
|
8
|
+
- d454e4b: fix (providers/gateway): fix timestamp error when streaming objects
|
|
9
|
+
|
|
10
|
+
## 1.0.0-beta.2
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- c91586a: chore (providers/gateway): update language model ids
|
|
15
|
+
- d1a034f: feature: using Zod 4 for internal stuff
|
|
16
|
+
- Updated dependencies [0571b98]
|
|
17
|
+
- Updated dependencies [39a4fab]
|
|
18
|
+
- Updated dependencies [d1a034f]
|
|
19
|
+
- @ai-sdk/provider-utils@3.0.0-beta.2
|
|
20
|
+
|
|
3
21
|
## 1.0.0-beta.1
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { LanguageModelV2, ProviderV2 } from '@ai-sdk/provider';
|
|
2
2
|
import { FetchFunction } from '@ai-sdk/provider-utils';
|
|
3
|
-
import { z, ZodError } from 'zod';
|
|
3
|
+
import { z, ZodError } from 'zod/v4';
|
|
4
4
|
|
|
5
|
-
type GatewayModelId = 'alibaba/qwen-3-14b' | 'alibaba/qwen-3-235b' | 'alibaba/qwen-3-30b' | 'alibaba/qwen-3-32b' | 'alibaba/qwq-32b' | 'amazon/nova-lite' | 'amazon/nova-micro' | 'amazon/nova-pro' | 'anthropic/claude-3-haiku' | 'anthropic/claude-3-opus' | 'anthropic/claude-3
|
|
5
|
+
type GatewayModelId = 'alibaba/qwen-3-14b' | 'alibaba/qwen-3-235b' | 'alibaba/qwen-3-30b' | 'alibaba/qwen-3-32b' | 'alibaba/qwq-32b' | 'amazon/nova-lite' | 'amazon/nova-micro' | 'amazon/nova-pro' | 'anthropic/claude-3-haiku' | 'anthropic/claude-3-opus' | 'anthropic/claude-3.5-haiku' | 'anthropic/claude-3.5-sonnet' | 'anthropic/claude-3.7-sonnet' | 'anthropic/claude-4-opus' | 'anthropic/claude-4-sonnet' | 'cohere/command-a' | 'cohere/command-r' | 'cohere/command-r-plus' | 'deepseek/deepseek-r1' | 'deepseek/deepseek-r1-distill-llama-70b' | 'deepseek/deepseek-v3' | 'google/gemini-2.0-flash' | 'google/gemini-2.0-flash-lite' | 'google/gemini-2.5-flash' | 'google/gemini-2.5-pro' | 'google/gemma-2-9b' | 'inception/mercury-coder-small' | 'meta/llama-3-70b' | 'meta/llama-3-8b' | 'meta/llama-3.1-70b' | 'meta/llama-3.1-8b' | 'meta/llama-3.2-11b' | 'meta/llama-3.2-1b' | 'meta/llama-3.2-3b' | 'meta/llama-3.2-90b' | 'meta/llama-3.3-70b' | 'meta/llama-4-maverick' | 'meta/llama-4-scout' | 'mistral/codestral' | 'mistral/magistral-medium' | 'mistral/magistral-small' | 'mistral/ministral-3b' | 'mistral/ministral-8b' | 'mistral/mistral-large' | 'mistral/mistral-saba-24b' | 'mistral/mistral-small' | 'mistral/mixtral-8x22b-instruct' | 'mistral/pixtral-12b' | 'mistral/pixtral-large' | 'morph/morph-v2' | 'openai/gpt-3.5-turbo' | 'openai/gpt-3.5-turbo-instruct' | 'openai/gpt-4-turbo' | 'openai/gpt-4.1' | 'openai/gpt-4.1-mini' | 'openai/gpt-4.1-nano' | 'openai/gpt-4o' | 'openai/gpt-4o-mini' | 'openai/o1' | 'openai/o3' | 'openai/o3-mini' | 'openai/o4-mini' | 'perplexity/sonar' | 'perplexity/sonar-pro' | 'perplexity/sonar-reasoning' | 'perplexity/sonar-reasoning-pro' | 'xai/grok-2' | 'xai/grok-2-vision' | 'xai/grok-3' | 'xai/grok-3-fast' | 'xai/grok-3-mini' | 'xai/grok-3-mini-fast' | (string & {});
|
|
6
6
|
|
|
7
7
|
interface GatewayLanguageModelEntry {
|
|
8
8
|
/**
|
|
@@ -91,33 +91,9 @@ declare const gatewayErrorResponseSchema: z.ZodObject<{
|
|
|
91
91
|
message: z.ZodString;
|
|
92
92
|
type: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
93
93
|
param: z.ZodOptional<z.ZodNullable<z.ZodUnknown>>;
|
|
94
|
-
code: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodNumber]>>>;
|
|
95
|
-
},
|
|
96
|
-
|
|
97
|
-
code?: string | number | null | undefined;
|
|
98
|
-
type?: string | null | undefined;
|
|
99
|
-
param?: unknown;
|
|
100
|
-
}, {
|
|
101
|
-
message: string;
|
|
102
|
-
code?: string | number | null | undefined;
|
|
103
|
-
type?: string | null | undefined;
|
|
104
|
-
param?: unknown;
|
|
105
|
-
}>;
|
|
106
|
-
}, "strip", z.ZodTypeAny, {
|
|
107
|
-
error: {
|
|
108
|
-
message: string;
|
|
109
|
-
code?: string | number | null | undefined;
|
|
110
|
-
type?: string | null | undefined;
|
|
111
|
-
param?: unknown;
|
|
112
|
-
};
|
|
113
|
-
}, {
|
|
114
|
-
error: {
|
|
115
|
-
message: string;
|
|
116
|
-
code?: string | number | null | undefined;
|
|
117
|
-
type?: string | null | undefined;
|
|
118
|
-
param?: unknown;
|
|
119
|
-
};
|
|
120
|
-
}>;
|
|
94
|
+
code: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
|
|
95
|
+
}, z.core.$strip>;
|
|
96
|
+
}, z.core.$strip>;
|
|
121
97
|
type GatewayErrorResponse = z.infer<typeof gatewayErrorResponseSchema>;
|
|
122
98
|
|
|
123
99
|
declare const symbol$5: unique symbol;
|
|
@@ -134,6 +110,16 @@ declare class GatewayAuthenticationError extends GatewayError {
|
|
|
134
110
|
cause?: unknown;
|
|
135
111
|
});
|
|
136
112
|
static isInstance(error: unknown): error is GatewayAuthenticationError;
|
|
113
|
+
/**
|
|
114
|
+
* Creates a contextual error message when authentication fails
|
|
115
|
+
*/
|
|
116
|
+
static createContextualError({ apiKeyProvided, oidcTokenProvided, message, statusCode, cause, }: {
|
|
117
|
+
apiKeyProvided: boolean;
|
|
118
|
+
oidcTokenProvided: boolean;
|
|
119
|
+
message?: string;
|
|
120
|
+
statusCode?: number;
|
|
121
|
+
cause?: unknown;
|
|
122
|
+
}): GatewayAuthenticationError;
|
|
137
123
|
}
|
|
138
124
|
|
|
139
125
|
declare const symbol$4: unique symbol;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { LanguageModelV2, ProviderV2 } from '@ai-sdk/provider';
|
|
2
2
|
import { FetchFunction } from '@ai-sdk/provider-utils';
|
|
3
|
-
import { z, ZodError } from 'zod';
|
|
3
|
+
import { z, ZodError } from 'zod/v4';
|
|
4
4
|
|
|
5
|
-
type GatewayModelId = 'alibaba/qwen-3-14b' | 'alibaba/qwen-3-235b' | 'alibaba/qwen-3-30b' | 'alibaba/qwen-3-32b' | 'alibaba/qwq-32b' | 'amazon/nova-lite' | 'amazon/nova-micro' | 'amazon/nova-pro' | 'anthropic/claude-3-haiku' | 'anthropic/claude-3-opus' | 'anthropic/claude-3
|
|
5
|
+
type GatewayModelId = 'alibaba/qwen-3-14b' | 'alibaba/qwen-3-235b' | 'alibaba/qwen-3-30b' | 'alibaba/qwen-3-32b' | 'alibaba/qwq-32b' | 'amazon/nova-lite' | 'amazon/nova-micro' | 'amazon/nova-pro' | 'anthropic/claude-3-haiku' | 'anthropic/claude-3-opus' | 'anthropic/claude-3.5-haiku' | 'anthropic/claude-3.5-sonnet' | 'anthropic/claude-3.7-sonnet' | 'anthropic/claude-4-opus' | 'anthropic/claude-4-sonnet' | 'cohere/command-a' | 'cohere/command-r' | 'cohere/command-r-plus' | 'deepseek/deepseek-r1' | 'deepseek/deepseek-r1-distill-llama-70b' | 'deepseek/deepseek-v3' | 'google/gemini-2.0-flash' | 'google/gemini-2.0-flash-lite' | 'google/gemini-2.5-flash' | 'google/gemini-2.5-pro' | 'google/gemma-2-9b' | 'inception/mercury-coder-small' | 'meta/llama-3-70b' | 'meta/llama-3-8b' | 'meta/llama-3.1-70b' | 'meta/llama-3.1-8b' | 'meta/llama-3.2-11b' | 'meta/llama-3.2-1b' | 'meta/llama-3.2-3b' | 'meta/llama-3.2-90b' | 'meta/llama-3.3-70b' | 'meta/llama-4-maverick' | 'meta/llama-4-scout' | 'mistral/codestral' | 'mistral/magistral-medium' | 'mistral/magistral-small' | 'mistral/ministral-3b' | 'mistral/ministral-8b' | 'mistral/mistral-large' | 'mistral/mistral-saba-24b' | 'mistral/mistral-small' | 'mistral/mixtral-8x22b-instruct' | 'mistral/pixtral-12b' | 'mistral/pixtral-large' | 'morph/morph-v2' | 'openai/gpt-3.5-turbo' | 'openai/gpt-3.5-turbo-instruct' | 'openai/gpt-4-turbo' | 'openai/gpt-4.1' | 'openai/gpt-4.1-mini' | 'openai/gpt-4.1-nano' | 'openai/gpt-4o' | 'openai/gpt-4o-mini' | 'openai/o1' | 'openai/o3' | 'openai/o3-mini' | 'openai/o4-mini' | 'perplexity/sonar' | 'perplexity/sonar-pro' | 'perplexity/sonar-reasoning' | 'perplexity/sonar-reasoning-pro' | 'xai/grok-2' | 'xai/grok-2-vision' | 'xai/grok-3' | 'xai/grok-3-fast' | 'xai/grok-3-mini' | 'xai/grok-3-mini-fast' | (string & {});
|
|
6
6
|
|
|
7
7
|
interface GatewayLanguageModelEntry {
|
|
8
8
|
/**
|
|
@@ -91,33 +91,9 @@ declare const gatewayErrorResponseSchema: z.ZodObject<{
|
|
|
91
91
|
message: z.ZodString;
|
|
92
92
|
type: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
93
93
|
param: z.ZodOptional<z.ZodNullable<z.ZodUnknown>>;
|
|
94
|
-
code: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodNumber]>>>;
|
|
95
|
-
},
|
|
96
|
-
|
|
97
|
-
code?: string | number | null | undefined;
|
|
98
|
-
type?: string | null | undefined;
|
|
99
|
-
param?: unknown;
|
|
100
|
-
}, {
|
|
101
|
-
message: string;
|
|
102
|
-
code?: string | number | null | undefined;
|
|
103
|
-
type?: string | null | undefined;
|
|
104
|
-
param?: unknown;
|
|
105
|
-
}>;
|
|
106
|
-
}, "strip", z.ZodTypeAny, {
|
|
107
|
-
error: {
|
|
108
|
-
message: string;
|
|
109
|
-
code?: string | number | null | undefined;
|
|
110
|
-
type?: string | null | undefined;
|
|
111
|
-
param?: unknown;
|
|
112
|
-
};
|
|
113
|
-
}, {
|
|
114
|
-
error: {
|
|
115
|
-
message: string;
|
|
116
|
-
code?: string | number | null | undefined;
|
|
117
|
-
type?: string | null | undefined;
|
|
118
|
-
param?: unknown;
|
|
119
|
-
};
|
|
120
|
-
}>;
|
|
94
|
+
code: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
|
|
95
|
+
}, z.core.$strip>;
|
|
96
|
+
}, z.core.$strip>;
|
|
121
97
|
type GatewayErrorResponse = z.infer<typeof gatewayErrorResponseSchema>;
|
|
122
98
|
|
|
123
99
|
declare const symbol$5: unique symbol;
|
|
@@ -134,6 +110,16 @@ declare class GatewayAuthenticationError extends GatewayError {
|
|
|
134
110
|
cause?: unknown;
|
|
135
111
|
});
|
|
136
112
|
static isInstance(error: unknown): error is GatewayAuthenticationError;
|
|
113
|
+
/**
|
|
114
|
+
* Creates a contextual error message when authentication fails
|
|
115
|
+
*/
|
|
116
|
+
static createContextualError({ apiKeyProvided, oidcTokenProvided, message, statusCode, cause, }: {
|
|
117
|
+
apiKeyProvided: boolean;
|
|
118
|
+
oidcTokenProvided: boolean;
|
|
119
|
+
message?: string;
|
|
120
|
+
statusCode?: number;
|
|
121
|
+
cause?: unknown;
|
|
122
|
+
}): GatewayAuthenticationError;
|
|
137
123
|
}
|
|
138
124
|
|
|
139
125
|
declare const symbol$4: unique symbol;
|
package/dist/index.js
CHANGED
|
@@ -41,7 +41,7 @@ var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
|
41
41
|
var import_provider = require("@ai-sdk/provider");
|
|
42
42
|
|
|
43
43
|
// src/errors/create-gateway-error.ts
|
|
44
|
-
var
|
|
44
|
+
var import_v42 = require("zod/v4");
|
|
45
45
|
|
|
46
46
|
// src/errors/gateway-error.ts
|
|
47
47
|
var marker = "vercel.ai.gateway.error";
|
|
@@ -76,7 +76,7 @@ var name = "GatewayAuthenticationError";
|
|
|
76
76
|
var marker2 = `vercel.ai.gateway.error.${name}`;
|
|
77
77
|
var symbol2 = Symbol.for(marker2);
|
|
78
78
|
var _a2, _b2;
|
|
79
|
-
var GatewayAuthenticationError = class extends (_b2 = GatewayError, _a2 = symbol2, _b2) {
|
|
79
|
+
var GatewayAuthenticationError = class _GatewayAuthenticationError extends (_b2 = GatewayError, _a2 = symbol2, _b2) {
|
|
80
80
|
constructor({
|
|
81
81
|
message = "Authentication failed",
|
|
82
82
|
statusCode = 401,
|
|
@@ -91,6 +91,54 @@ var GatewayAuthenticationError = class extends (_b2 = GatewayError, _a2 = symbol
|
|
|
91
91
|
static isInstance(error) {
|
|
92
92
|
return GatewayError.hasMarker(error) && symbol2 in error;
|
|
93
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* Creates a contextual error message when authentication fails
|
|
96
|
+
*/
|
|
97
|
+
static createContextualError({
|
|
98
|
+
apiKeyProvided,
|
|
99
|
+
oidcTokenProvided,
|
|
100
|
+
message = "Authentication failed",
|
|
101
|
+
statusCode = 401,
|
|
102
|
+
cause
|
|
103
|
+
}) {
|
|
104
|
+
let contextualMessage;
|
|
105
|
+
if (apiKeyProvided) {
|
|
106
|
+
contextualMessage = `AI Gateway authentication failed: Invalid API key provided.
|
|
107
|
+
|
|
108
|
+
The token is expected to be provided via the 'apiKey' option or 'AI_GATEWAY_API_KEY' environment variable.`;
|
|
109
|
+
} else if (oidcTokenProvided) {
|
|
110
|
+
contextualMessage = `AI Gateway authentication failed: Invalid OIDC token provided.
|
|
111
|
+
|
|
112
|
+
The token is expected to be provided via the 'VERCEL_OIDC_TOKEN' environment variable. It expires every 12 hours.
|
|
113
|
+
- make sure your Vercel project settings have OIDC enabled
|
|
114
|
+
- if running locally with 'vercel dev', the token is automatically obtained and refreshed
|
|
115
|
+
- if running locally with your own dev server, run 'vercel env pull' to fetch the token
|
|
116
|
+
- in production/preview, the token is automatically obtained and refreshed
|
|
117
|
+
|
|
118
|
+
Alternative: Provide an API key via 'apiKey' option or 'AI_GATEWAY_API_KEY' environment variable.`;
|
|
119
|
+
} else {
|
|
120
|
+
contextualMessage = `AI Gateway authentication failed: No authentication provided.
|
|
121
|
+
|
|
122
|
+
Provide either an API key or OIDC token.
|
|
123
|
+
|
|
124
|
+
API key instructions:
|
|
125
|
+
|
|
126
|
+
The token is expected to be provided via the 'apiKey' option or 'AI_GATEWAY_API_KEY' environment variable.
|
|
127
|
+
|
|
128
|
+
OIDC token instructions:
|
|
129
|
+
|
|
130
|
+
The token is expected to be provided via the 'VERCEL_OIDC_TOKEN' environment variable. It expires every 12 hours.
|
|
131
|
+
- make sure your Vercel project settings have OIDC enabled
|
|
132
|
+
- if running locally with 'vercel dev', the token is automatically obtained and refreshed
|
|
133
|
+
- if running locally with your own dev server, run 'vercel env pull' to fetch the token
|
|
134
|
+
- in production/preview, the token is automatically obtained and refreshed`;
|
|
135
|
+
}
|
|
136
|
+
return new _GatewayAuthenticationError({
|
|
137
|
+
message: contextualMessage,
|
|
138
|
+
statusCode,
|
|
139
|
+
cause
|
|
140
|
+
});
|
|
141
|
+
}
|
|
94
142
|
};
|
|
95
143
|
|
|
96
144
|
// src/errors/gateway-invalid-request-error.ts
|
|
@@ -138,12 +186,12 @@ var GatewayRateLimitError = class extends (_b4 = GatewayError, _a4 = symbol4, _b
|
|
|
138
186
|
};
|
|
139
187
|
|
|
140
188
|
// src/errors/gateway-model-not-found-error.ts
|
|
141
|
-
var
|
|
189
|
+
var import_v4 = require("zod/v4");
|
|
142
190
|
var name4 = "GatewayModelNotFoundError";
|
|
143
191
|
var marker5 = `vercel.ai.gateway.error.${name4}`;
|
|
144
192
|
var symbol5 = Symbol.for(marker5);
|
|
145
|
-
var modelNotFoundParamSchema =
|
|
146
|
-
modelId:
|
|
193
|
+
var modelNotFoundParamSchema = import_v4.z.object({
|
|
194
|
+
modelId: import_v4.z.string()
|
|
147
195
|
});
|
|
148
196
|
var _a5, _b5;
|
|
149
197
|
var GatewayModelNotFoundError = class extends (_b5 = GatewayError, _a5 = symbol5, _b5) {
|
|
@@ -218,7 +266,8 @@ function createGatewayErrorFromResponse({
|
|
|
218
266
|
response,
|
|
219
267
|
statusCode,
|
|
220
268
|
defaultMessage = "Gateway request failed",
|
|
221
|
-
cause
|
|
269
|
+
cause,
|
|
270
|
+
authMethod
|
|
222
271
|
}) {
|
|
223
272
|
const parseResult = gatewayErrorResponseSchema.safeParse(response);
|
|
224
273
|
if (!parseResult.success) {
|
|
@@ -235,7 +284,12 @@ function createGatewayErrorFromResponse({
|
|
|
235
284
|
const message = validatedResponse.error.message;
|
|
236
285
|
switch (errorType) {
|
|
237
286
|
case "authentication_error":
|
|
238
|
-
return
|
|
287
|
+
return GatewayAuthenticationError.createContextualError({
|
|
288
|
+
apiKeyProvided: authMethod === "api-key",
|
|
289
|
+
oidcTokenProvided: authMethod === "oidc",
|
|
290
|
+
statusCode,
|
|
291
|
+
cause
|
|
292
|
+
});
|
|
239
293
|
case "invalid_request_error":
|
|
240
294
|
return new GatewayInvalidRequestError({ message, statusCode, cause });
|
|
241
295
|
case "rate_limit_exceeded":
|
|
@@ -257,17 +311,17 @@ function createGatewayErrorFromResponse({
|
|
|
257
311
|
return new GatewayInternalServerError({ message, statusCode, cause });
|
|
258
312
|
}
|
|
259
313
|
}
|
|
260
|
-
var gatewayErrorResponseSchema =
|
|
261
|
-
error:
|
|
262
|
-
message:
|
|
263
|
-
type:
|
|
264
|
-
param:
|
|
265
|
-
code:
|
|
314
|
+
var gatewayErrorResponseSchema = import_v42.z.object({
|
|
315
|
+
error: import_v42.z.object({
|
|
316
|
+
message: import_v42.z.string(),
|
|
317
|
+
type: import_v42.z.string().nullish(),
|
|
318
|
+
param: import_v42.z.unknown().nullish(),
|
|
319
|
+
code: import_v42.z.union([import_v42.z.string(), import_v42.z.number()]).nullish()
|
|
266
320
|
})
|
|
267
321
|
});
|
|
268
322
|
|
|
269
323
|
// src/errors/as-gateway-error.ts
|
|
270
|
-
function asGatewayError(error) {
|
|
324
|
+
function asGatewayError(error, authMethod) {
|
|
271
325
|
var _a8;
|
|
272
326
|
if (GatewayError.isInstance(error)) {
|
|
273
327
|
return error;
|
|
@@ -277,14 +331,16 @@ function asGatewayError(error) {
|
|
|
277
331
|
response: extractApiCallResponse(error),
|
|
278
332
|
statusCode: (_a8 = error.statusCode) != null ? _a8 : 500,
|
|
279
333
|
defaultMessage: "Gateway request failed",
|
|
280
|
-
cause: error
|
|
334
|
+
cause: error,
|
|
335
|
+
authMethod
|
|
281
336
|
});
|
|
282
337
|
}
|
|
283
338
|
return createGatewayErrorFromResponse({
|
|
284
339
|
response: {},
|
|
285
340
|
statusCode: 500,
|
|
286
341
|
defaultMessage: error instanceof Error ? `Gateway request failed: ${error.message}` : "Unknown Gateway error",
|
|
287
|
-
cause: error
|
|
342
|
+
cause: error,
|
|
343
|
+
authMethod
|
|
288
344
|
});
|
|
289
345
|
}
|
|
290
346
|
|
|
@@ -305,7 +361,7 @@ function extractApiCallResponse(error) {
|
|
|
305
361
|
|
|
306
362
|
// src/gateway-fetch-metadata.ts
|
|
307
363
|
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
308
|
-
var
|
|
364
|
+
var import_v43 = require("zod/v4");
|
|
309
365
|
var GatewayFetchMetadata = class {
|
|
310
366
|
constructor(config) {
|
|
311
367
|
this.config = config;
|
|
@@ -319,7 +375,7 @@ var GatewayFetchMetadata = class {
|
|
|
319
375
|
gatewayFetchMetadataSchema
|
|
320
376
|
),
|
|
321
377
|
failedResponseHandler: (0, import_provider_utils.createJsonErrorResponseHandler)({
|
|
322
|
-
errorSchema:
|
|
378
|
+
errorSchema: import_v43.z.any(),
|
|
323
379
|
errorToMessage: (data) => data
|
|
324
380
|
}),
|
|
325
381
|
fetch: this.config.fetch
|
|
@@ -330,23 +386,39 @@ var GatewayFetchMetadata = class {
|
|
|
330
386
|
}
|
|
331
387
|
}
|
|
332
388
|
};
|
|
333
|
-
var gatewayLanguageModelSpecificationSchema =
|
|
334
|
-
specificationVersion:
|
|
335
|
-
provider:
|
|
336
|
-
modelId:
|
|
389
|
+
var gatewayLanguageModelSpecificationSchema = import_v43.z.object({
|
|
390
|
+
specificationVersion: import_v43.z.literal("v2"),
|
|
391
|
+
provider: import_v43.z.string(),
|
|
392
|
+
modelId: import_v43.z.string()
|
|
337
393
|
});
|
|
338
|
-
var gatewayLanguageModelEntrySchema =
|
|
339
|
-
id:
|
|
340
|
-
name:
|
|
394
|
+
var gatewayLanguageModelEntrySchema = import_v43.z.object({
|
|
395
|
+
id: import_v43.z.string(),
|
|
396
|
+
name: import_v43.z.string(),
|
|
341
397
|
specification: gatewayLanguageModelSpecificationSchema
|
|
342
398
|
});
|
|
343
|
-
var gatewayFetchMetadataSchema =
|
|
344
|
-
models:
|
|
399
|
+
var gatewayFetchMetadataSchema = import_v43.z.object({
|
|
400
|
+
models: import_v43.z.array(gatewayLanguageModelEntrySchema)
|
|
345
401
|
});
|
|
346
402
|
|
|
347
403
|
// src/gateway-language-model.ts
|
|
348
404
|
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
349
|
-
var
|
|
405
|
+
var import_v44 = require("zod/v4");
|
|
406
|
+
|
|
407
|
+
// src/errors/parse-auth-method.ts
|
|
408
|
+
var import_zod = require("zod");
|
|
409
|
+
var GATEWAY_AUTH_METHOD_HEADER = "x-ai-gateway-auth-method";
|
|
410
|
+
function parseAuthMethod(headers) {
|
|
411
|
+
const result = gatewayAuthMethodSchema.safeParse(
|
|
412
|
+
headers[GATEWAY_AUTH_METHOD_HEADER]
|
|
413
|
+
);
|
|
414
|
+
return result.success ? result.data : void 0;
|
|
415
|
+
}
|
|
416
|
+
var gatewayAuthMethodSchema = import_zod.z.union([
|
|
417
|
+
import_zod.z.literal("api-key"),
|
|
418
|
+
import_zod.z.literal("oidc")
|
|
419
|
+
]);
|
|
420
|
+
|
|
421
|
+
// src/gateway-language-model.ts
|
|
350
422
|
var GatewayLanguageModel = class {
|
|
351
423
|
constructor(modelId, config) {
|
|
352
424
|
this.modelId = modelId;
|
|
@@ -359,6 +431,7 @@ var GatewayLanguageModel = class {
|
|
|
359
431
|
}
|
|
360
432
|
async doGenerate(options) {
|
|
361
433
|
const { abortSignal, ...body } = options;
|
|
434
|
+
const resolvedHeaders = await (0, import_provider_utils2.resolve)(this.config.headers());
|
|
362
435
|
try {
|
|
363
436
|
const {
|
|
364
437
|
responseHeaders,
|
|
@@ -367,15 +440,15 @@ var GatewayLanguageModel = class {
|
|
|
367
440
|
} = await (0, import_provider_utils2.postJsonToApi)({
|
|
368
441
|
url: this.getUrl(),
|
|
369
442
|
headers: (0, import_provider_utils2.combineHeaders)(
|
|
370
|
-
|
|
443
|
+
resolvedHeaders,
|
|
371
444
|
options.headers,
|
|
372
445
|
this.getModelConfigHeaders(this.modelId, false),
|
|
373
446
|
await (0, import_provider_utils2.resolve)(this.config.o11yHeaders)
|
|
374
447
|
),
|
|
375
448
|
body: this.maybeEncodeFileParts(body),
|
|
376
|
-
successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
|
|
449
|
+
successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(import_v44.z.any()),
|
|
377
450
|
failedResponseHandler: (0, import_provider_utils2.createJsonErrorResponseHandler)({
|
|
378
|
-
errorSchema:
|
|
451
|
+
errorSchema: import_v44.z.any(),
|
|
379
452
|
errorToMessage: (data) => data
|
|
380
453
|
}),
|
|
381
454
|
...abortSignal && { abortSignal },
|
|
@@ -388,24 +461,25 @@ var GatewayLanguageModel = class {
|
|
|
388
461
|
warnings: []
|
|
389
462
|
};
|
|
390
463
|
} catch (error) {
|
|
391
|
-
throw asGatewayError(error);
|
|
464
|
+
throw asGatewayError(error, parseAuthMethod(resolvedHeaders));
|
|
392
465
|
}
|
|
393
466
|
}
|
|
394
467
|
async doStream(options) {
|
|
395
468
|
const { abortSignal, ...body } = options;
|
|
469
|
+
const resolvedHeaders = await (0, import_provider_utils2.resolve)(this.config.headers());
|
|
396
470
|
try {
|
|
397
471
|
const { value: response, responseHeaders } = await (0, import_provider_utils2.postJsonToApi)({
|
|
398
472
|
url: this.getUrl(),
|
|
399
473
|
headers: (0, import_provider_utils2.combineHeaders)(
|
|
400
|
-
|
|
474
|
+
resolvedHeaders,
|
|
401
475
|
options.headers,
|
|
402
476
|
this.getModelConfigHeaders(this.modelId, true),
|
|
403
477
|
await (0, import_provider_utils2.resolve)(this.config.o11yHeaders)
|
|
404
478
|
),
|
|
405
479
|
body: this.maybeEncodeFileParts(body),
|
|
406
|
-
successfulResponseHandler: (0, import_provider_utils2.createEventSourceResponseHandler)(
|
|
480
|
+
successfulResponseHandler: (0, import_provider_utils2.createEventSourceResponseHandler)(import_v44.z.any()),
|
|
407
481
|
failedResponseHandler: (0, import_provider_utils2.createJsonErrorResponseHandler)({
|
|
408
|
-
errorSchema:
|
|
482
|
+
errorSchema: import_v44.z.any(),
|
|
409
483
|
errorToMessage: (data) => data
|
|
410
484
|
}),
|
|
411
485
|
...abortSignal && { abortSignal },
|
|
@@ -420,6 +494,9 @@ var GatewayLanguageModel = class {
|
|
|
420
494
|
if (streamPart.type === "raw" && !options.includeRawChunks) {
|
|
421
495
|
return;
|
|
422
496
|
}
|
|
497
|
+
if (streamPart.type === "response-metadata" && streamPart.timestamp && typeof streamPart.timestamp === "string") {
|
|
498
|
+
streamPart.timestamp = new Date(streamPart.timestamp);
|
|
499
|
+
}
|
|
423
500
|
controller.enqueue(streamPart);
|
|
424
501
|
} else {
|
|
425
502
|
controller.error(
|
|
@@ -433,7 +510,7 @@ var GatewayLanguageModel = class {
|
|
|
433
510
|
response: { headers: responseHeaders }
|
|
434
511
|
};
|
|
435
512
|
} catch (error) {
|
|
436
|
-
throw asGatewayError(error);
|
|
513
|
+
throw asGatewayError(error, parseAuthMethod(resolvedHeaders));
|
|
437
514
|
}
|
|
438
515
|
}
|
|
439
516
|
isFilePart(part) {
|
|
@@ -480,12 +557,7 @@ async function getVercelOidcToken() {
|
|
|
480
557
|
const token = (_b8 = (_a8 = getContext().headers) == null ? void 0 : _a8["x-vercel-oidc-token"]) != null ? _b8 : process.env.VERCEL_OIDC_TOKEN;
|
|
481
558
|
if (!token) {
|
|
482
559
|
throw new GatewayAuthenticationError({
|
|
483
|
-
message:
|
|
484
|
-
The token is expected to be provided via the 'VERCEL_OIDC_TOKEN' environment variable. It expires every 12 hours.
|
|
485
|
-
- make sure your Vercel project settings have OIDC enabled
|
|
486
|
-
- if you're running locally with 'vercel dev' the token is automatically obtained and refreshed for you
|
|
487
|
-
- if you're running locally with your own dev server script you can fetch/update the token by running 'vercel env pull'
|
|
488
|
-
- in production or preview the token is automatically obtained and refreshed for you`,
|
|
560
|
+
message: "OIDC token not available",
|
|
489
561
|
statusCode: 401
|
|
490
562
|
});
|
|
491
563
|
}
|
|
@@ -504,13 +576,6 @@ function getContext() {
|
|
|
504
576
|
|
|
505
577
|
// src/gateway-provider.ts
|
|
506
578
|
var AI_GATEWAY_PROTOCOL_VERSION = "0.0.1";
|
|
507
|
-
async function getGatewayAuthToken(options) {
|
|
508
|
-
var _a8;
|
|
509
|
-
return (_a8 = (0, import_provider_utils3.loadOptionalSetting)({
|
|
510
|
-
settingValue: options.apiKey,
|
|
511
|
-
environmentVariableName: "AI_GATEWAY_API_KEY"
|
|
512
|
-
})) != null ? _a8 : await getVercelOidcToken();
|
|
513
|
-
}
|
|
514
579
|
function createGatewayProvider(options = {}) {
|
|
515
580
|
var _a8, _b8;
|
|
516
581
|
let pendingMetadata = null;
|
|
@@ -519,11 +584,20 @@ function createGatewayProvider(options = {}) {
|
|
|
519
584
|
let lastFetchTime = 0;
|
|
520
585
|
const baseURL = (_b8 = (0, import_provider_utils3.withoutTrailingSlash)(options.baseURL)) != null ? _b8 : "https://ai-gateway.vercel.sh/v1/ai";
|
|
521
586
|
const getHeaders = async () => {
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
587
|
+
const auth = await getGatewayAuthToken(options);
|
|
588
|
+
if (auth) {
|
|
589
|
+
return {
|
|
590
|
+
Authorization: `Bearer ${auth.token}`,
|
|
591
|
+
"ai-gateway-protocol-version": AI_GATEWAY_PROTOCOL_VERSION,
|
|
592
|
+
[GATEWAY_AUTH_METHOD_HEADER]: auth.authMethod,
|
|
593
|
+
...options.headers
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
throw GatewayAuthenticationError.createContextualError({
|
|
597
|
+
apiKeyProvided: false,
|
|
598
|
+
oidcTokenProvided: false,
|
|
599
|
+
statusCode: 401
|
|
600
|
+
});
|
|
527
601
|
};
|
|
528
602
|
const createLanguageModel = (modelId) => {
|
|
529
603
|
const deploymentId = (0, import_provider_utils3.loadOptionalSetting)({
|
|
@@ -566,8 +640,8 @@ function createGatewayProvider(options = {}) {
|
|
|
566
640
|
}).getAvailableModels().then((metadata) => {
|
|
567
641
|
metadataCache = metadata;
|
|
568
642
|
return metadata;
|
|
569
|
-
}).catch((error) => {
|
|
570
|
-
throw asGatewayError(error);
|
|
643
|
+
}).catch(async (error) => {
|
|
644
|
+
throw asGatewayError(error, parseAuthMethod(await getHeaders()));
|
|
571
645
|
});
|
|
572
646
|
}
|
|
573
647
|
return metadataCache ? Promise.resolve(metadataCache) : pendingMetadata;
|
|
@@ -591,6 +665,27 @@ function createGatewayProvider(options = {}) {
|
|
|
591
665
|
return provider;
|
|
592
666
|
}
|
|
593
667
|
var gateway = createGatewayProvider();
|
|
668
|
+
async function getGatewayAuthToken(options) {
|
|
669
|
+
const apiKey = (0, import_provider_utils3.loadOptionalSetting)({
|
|
670
|
+
settingValue: options.apiKey,
|
|
671
|
+
environmentVariableName: "AI_GATEWAY_API_KEY"
|
|
672
|
+
});
|
|
673
|
+
if (apiKey) {
|
|
674
|
+
return {
|
|
675
|
+
token: apiKey,
|
|
676
|
+
authMethod: "api-key"
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
try {
|
|
680
|
+
const oidcToken = await getVercelOidcToken();
|
|
681
|
+
return {
|
|
682
|
+
token: oidcToken,
|
|
683
|
+
authMethod: "oidc"
|
|
684
|
+
};
|
|
685
|
+
} catch (error) {
|
|
686
|
+
return null;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
594
689
|
// Annotate the CommonJS export names for ESM import in node:
|
|
595
690
|
0 && (module.exports = {
|
|
596
691
|
GatewayAuthenticationError,
|