@ai-sdk/gateway 0.0.0-02dba89b-20251009204516
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 +631 -0
- package/LICENSE +13 -0
- package/README.md +36 -0
- package/dist/index.d.mts +264 -0
- package/dist/index.d.ts +264 -0
- package/dist/index.js +841 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +826 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +67 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,826 @@
|
|
|
1
|
+
// src/gateway-provider.ts
|
|
2
|
+
import { NoSuchModelError } from "@ai-sdk/provider";
|
|
3
|
+
import {
|
|
4
|
+
loadOptionalSetting,
|
|
5
|
+
withoutTrailingSlash
|
|
6
|
+
} from "@ai-sdk/provider-utils";
|
|
7
|
+
|
|
8
|
+
// src/errors/as-gateway-error.ts
|
|
9
|
+
import { APICallError } from "@ai-sdk/provider";
|
|
10
|
+
|
|
11
|
+
// src/errors/create-gateway-error.ts
|
|
12
|
+
import { z as z2 } from "zod/v4";
|
|
13
|
+
|
|
14
|
+
// src/errors/gateway-error.ts
|
|
15
|
+
var marker = "vercel.ai.gateway.error";
|
|
16
|
+
var symbol = Symbol.for(marker);
|
|
17
|
+
var _a, _b;
|
|
18
|
+
var GatewayError = class _GatewayError extends (_b = Error, _a = symbol, _b) {
|
|
19
|
+
constructor({
|
|
20
|
+
message,
|
|
21
|
+
statusCode = 500,
|
|
22
|
+
cause
|
|
23
|
+
}) {
|
|
24
|
+
super(message);
|
|
25
|
+
this[_a] = true;
|
|
26
|
+
this.statusCode = statusCode;
|
|
27
|
+
this.cause = cause;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Checks if the given error is a Gateway Error.
|
|
31
|
+
* @param {unknown} error - The error to check.
|
|
32
|
+
* @returns {boolean} True if the error is a Gateway Error, false otherwise.
|
|
33
|
+
*/
|
|
34
|
+
static isInstance(error) {
|
|
35
|
+
return _GatewayError.hasMarker(error);
|
|
36
|
+
}
|
|
37
|
+
static hasMarker(error) {
|
|
38
|
+
return typeof error === "object" && error !== null && symbol in error && error[symbol] === true;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// src/errors/gateway-authentication-error.ts
|
|
43
|
+
var name = "GatewayAuthenticationError";
|
|
44
|
+
var marker2 = `vercel.ai.gateway.error.${name}`;
|
|
45
|
+
var symbol2 = Symbol.for(marker2);
|
|
46
|
+
var _a2, _b2;
|
|
47
|
+
var GatewayAuthenticationError = class _GatewayAuthenticationError extends (_b2 = GatewayError, _a2 = symbol2, _b2) {
|
|
48
|
+
constructor({
|
|
49
|
+
message = "Authentication failed",
|
|
50
|
+
statusCode = 401,
|
|
51
|
+
cause
|
|
52
|
+
} = {}) {
|
|
53
|
+
super({ message, statusCode, cause });
|
|
54
|
+
this[_a2] = true;
|
|
55
|
+
// used in isInstance
|
|
56
|
+
this.name = name;
|
|
57
|
+
this.type = "authentication_error";
|
|
58
|
+
}
|
|
59
|
+
static isInstance(error) {
|
|
60
|
+
return GatewayError.hasMarker(error) && symbol2 in error;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Creates a contextual error message when authentication fails
|
|
64
|
+
*/
|
|
65
|
+
static createContextualError({
|
|
66
|
+
apiKeyProvided,
|
|
67
|
+
oidcTokenProvided,
|
|
68
|
+
message = "Authentication failed",
|
|
69
|
+
statusCode = 401,
|
|
70
|
+
cause
|
|
71
|
+
}) {
|
|
72
|
+
let contextualMessage;
|
|
73
|
+
if (apiKeyProvided) {
|
|
74
|
+
contextualMessage = `AI Gateway authentication failed: Invalid API key provided.
|
|
75
|
+
|
|
76
|
+
The token is expected to be provided via the 'apiKey' option or 'AI_GATEWAY_API_KEY' environment variable.`;
|
|
77
|
+
} else if (oidcTokenProvided) {
|
|
78
|
+
contextualMessage = `AI Gateway authentication failed: Invalid OIDC token provided.
|
|
79
|
+
|
|
80
|
+
The token is expected to be provided via the 'VERCEL_OIDC_TOKEN' environment variable. It expires every 12 hours.
|
|
81
|
+
- make sure your Vercel project settings have OIDC enabled
|
|
82
|
+
- if running locally with 'vercel dev', the token is automatically obtained and refreshed
|
|
83
|
+
- if running locally with your own dev server, run 'vercel env pull' to fetch the token
|
|
84
|
+
- in production/preview, the token is automatically obtained and refreshed
|
|
85
|
+
|
|
86
|
+
Alternative: Provide an API key via 'apiKey' option or 'AI_GATEWAY_API_KEY' environment variable.`;
|
|
87
|
+
} else {
|
|
88
|
+
contextualMessage = `AI Gateway authentication failed: No authentication provided.
|
|
89
|
+
|
|
90
|
+
Provide either an API key or OIDC token.
|
|
91
|
+
|
|
92
|
+
API key instructions:
|
|
93
|
+
|
|
94
|
+
The token is expected to be provided via the 'apiKey' option or 'AI_GATEWAY_API_KEY' environment variable.
|
|
95
|
+
|
|
96
|
+
OIDC token instructions:
|
|
97
|
+
|
|
98
|
+
The token is expected to be provided via the 'VERCEL_OIDC_TOKEN' environment variable. It expires every 12 hours.
|
|
99
|
+
- make sure your Vercel project settings have OIDC enabled
|
|
100
|
+
- if running locally with 'vercel dev', the token is automatically obtained and refreshed
|
|
101
|
+
- if running locally with your own dev server, run 'vercel env pull' to fetch the token
|
|
102
|
+
- in production/preview, the token is automatically obtained and refreshed`;
|
|
103
|
+
}
|
|
104
|
+
return new _GatewayAuthenticationError({
|
|
105
|
+
message: contextualMessage,
|
|
106
|
+
statusCode,
|
|
107
|
+
cause
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
// src/errors/gateway-invalid-request-error.ts
|
|
113
|
+
var name2 = "GatewayInvalidRequestError";
|
|
114
|
+
var marker3 = `vercel.ai.gateway.error.${name2}`;
|
|
115
|
+
var symbol3 = Symbol.for(marker3);
|
|
116
|
+
var _a3, _b3;
|
|
117
|
+
var GatewayInvalidRequestError = class extends (_b3 = GatewayError, _a3 = symbol3, _b3) {
|
|
118
|
+
constructor({
|
|
119
|
+
message = "Invalid request",
|
|
120
|
+
statusCode = 400,
|
|
121
|
+
cause
|
|
122
|
+
} = {}) {
|
|
123
|
+
super({ message, statusCode, cause });
|
|
124
|
+
this[_a3] = true;
|
|
125
|
+
// used in isInstance
|
|
126
|
+
this.name = name2;
|
|
127
|
+
this.type = "invalid_request_error";
|
|
128
|
+
}
|
|
129
|
+
static isInstance(error) {
|
|
130
|
+
return GatewayError.hasMarker(error) && symbol3 in error;
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
// src/errors/gateway-rate-limit-error.ts
|
|
135
|
+
var name3 = "GatewayRateLimitError";
|
|
136
|
+
var marker4 = `vercel.ai.gateway.error.${name3}`;
|
|
137
|
+
var symbol4 = Symbol.for(marker4);
|
|
138
|
+
var _a4, _b4;
|
|
139
|
+
var GatewayRateLimitError = class extends (_b4 = GatewayError, _a4 = symbol4, _b4) {
|
|
140
|
+
constructor({
|
|
141
|
+
message = "Rate limit exceeded",
|
|
142
|
+
statusCode = 429,
|
|
143
|
+
cause
|
|
144
|
+
} = {}) {
|
|
145
|
+
super({ message, statusCode, cause });
|
|
146
|
+
this[_a4] = true;
|
|
147
|
+
// used in isInstance
|
|
148
|
+
this.name = name3;
|
|
149
|
+
this.type = "rate_limit_exceeded";
|
|
150
|
+
}
|
|
151
|
+
static isInstance(error) {
|
|
152
|
+
return GatewayError.hasMarker(error) && symbol4 in error;
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
// src/errors/gateway-model-not-found-error.ts
|
|
157
|
+
import { z } from "zod/v4";
|
|
158
|
+
var name4 = "GatewayModelNotFoundError";
|
|
159
|
+
var marker5 = `vercel.ai.gateway.error.${name4}`;
|
|
160
|
+
var symbol5 = Symbol.for(marker5);
|
|
161
|
+
var modelNotFoundParamSchema = z.object({
|
|
162
|
+
modelId: z.string()
|
|
163
|
+
});
|
|
164
|
+
var _a5, _b5;
|
|
165
|
+
var GatewayModelNotFoundError = class extends (_b5 = GatewayError, _a5 = symbol5, _b5) {
|
|
166
|
+
constructor({
|
|
167
|
+
message = "Model not found",
|
|
168
|
+
statusCode = 404,
|
|
169
|
+
modelId,
|
|
170
|
+
cause
|
|
171
|
+
} = {}) {
|
|
172
|
+
super({ message, statusCode, cause });
|
|
173
|
+
this[_a5] = true;
|
|
174
|
+
// used in isInstance
|
|
175
|
+
this.name = name4;
|
|
176
|
+
this.type = "model_not_found";
|
|
177
|
+
this.modelId = modelId;
|
|
178
|
+
}
|
|
179
|
+
static isInstance(error) {
|
|
180
|
+
return GatewayError.hasMarker(error) && symbol5 in error;
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
// src/errors/gateway-internal-server-error.ts
|
|
185
|
+
var name5 = "GatewayInternalServerError";
|
|
186
|
+
var marker6 = `vercel.ai.gateway.error.${name5}`;
|
|
187
|
+
var symbol6 = Symbol.for(marker6);
|
|
188
|
+
var _a6, _b6;
|
|
189
|
+
var GatewayInternalServerError = class extends (_b6 = GatewayError, _a6 = symbol6, _b6) {
|
|
190
|
+
constructor({
|
|
191
|
+
message = "Internal server error",
|
|
192
|
+
statusCode = 500,
|
|
193
|
+
cause
|
|
194
|
+
} = {}) {
|
|
195
|
+
super({ message, statusCode, cause });
|
|
196
|
+
this[_a6] = true;
|
|
197
|
+
// used in isInstance
|
|
198
|
+
this.name = name5;
|
|
199
|
+
this.type = "internal_server_error";
|
|
200
|
+
}
|
|
201
|
+
static isInstance(error) {
|
|
202
|
+
return GatewayError.hasMarker(error) && symbol6 in error;
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
// src/errors/gateway-response-error.ts
|
|
207
|
+
var name6 = "GatewayResponseError";
|
|
208
|
+
var marker7 = `vercel.ai.gateway.error.${name6}`;
|
|
209
|
+
var symbol7 = Symbol.for(marker7);
|
|
210
|
+
var _a7, _b7;
|
|
211
|
+
var GatewayResponseError = class extends (_b7 = GatewayError, _a7 = symbol7, _b7) {
|
|
212
|
+
constructor({
|
|
213
|
+
message = "Invalid response from Gateway",
|
|
214
|
+
statusCode = 502,
|
|
215
|
+
response,
|
|
216
|
+
validationError,
|
|
217
|
+
cause
|
|
218
|
+
} = {}) {
|
|
219
|
+
super({ message, statusCode, cause });
|
|
220
|
+
this[_a7] = true;
|
|
221
|
+
// used in isInstance
|
|
222
|
+
this.name = name6;
|
|
223
|
+
this.type = "response_error";
|
|
224
|
+
this.response = response;
|
|
225
|
+
this.validationError = validationError;
|
|
226
|
+
}
|
|
227
|
+
static isInstance(error) {
|
|
228
|
+
return GatewayError.hasMarker(error) && symbol7 in error;
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
// src/errors/create-gateway-error.ts
|
|
233
|
+
function createGatewayErrorFromResponse({
|
|
234
|
+
response,
|
|
235
|
+
statusCode,
|
|
236
|
+
defaultMessage = "Gateway request failed",
|
|
237
|
+
cause,
|
|
238
|
+
authMethod
|
|
239
|
+
}) {
|
|
240
|
+
const parseResult = gatewayErrorResponseSchema.safeParse(response);
|
|
241
|
+
if (!parseResult.success) {
|
|
242
|
+
return new GatewayResponseError({
|
|
243
|
+
message: `Invalid error response format: ${defaultMessage}`,
|
|
244
|
+
statusCode,
|
|
245
|
+
response,
|
|
246
|
+
validationError: parseResult.error,
|
|
247
|
+
cause
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
const validatedResponse = parseResult.data;
|
|
251
|
+
const errorType = validatedResponse.error.type;
|
|
252
|
+
const message = validatedResponse.error.message;
|
|
253
|
+
switch (errorType) {
|
|
254
|
+
case "authentication_error":
|
|
255
|
+
return GatewayAuthenticationError.createContextualError({
|
|
256
|
+
apiKeyProvided: authMethod === "api-key",
|
|
257
|
+
oidcTokenProvided: authMethod === "oidc",
|
|
258
|
+
statusCode,
|
|
259
|
+
cause
|
|
260
|
+
});
|
|
261
|
+
case "invalid_request_error":
|
|
262
|
+
return new GatewayInvalidRequestError({ message, statusCode, cause });
|
|
263
|
+
case "rate_limit_exceeded":
|
|
264
|
+
return new GatewayRateLimitError({ message, statusCode, cause });
|
|
265
|
+
case "model_not_found": {
|
|
266
|
+
const modelResult = modelNotFoundParamSchema.safeParse(
|
|
267
|
+
validatedResponse.error.param
|
|
268
|
+
);
|
|
269
|
+
return new GatewayModelNotFoundError({
|
|
270
|
+
message,
|
|
271
|
+
statusCode,
|
|
272
|
+
modelId: modelResult.success ? modelResult.data.modelId : void 0,
|
|
273
|
+
cause
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
case "internal_server_error":
|
|
277
|
+
return new GatewayInternalServerError({ message, statusCode, cause });
|
|
278
|
+
default:
|
|
279
|
+
return new GatewayInternalServerError({ message, statusCode, cause });
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
var gatewayErrorResponseSchema = z2.object({
|
|
283
|
+
error: z2.object({
|
|
284
|
+
message: z2.string(),
|
|
285
|
+
type: z2.string().nullish(),
|
|
286
|
+
param: z2.unknown().nullish(),
|
|
287
|
+
code: z2.union([z2.string(), z2.number()]).nullish()
|
|
288
|
+
})
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
// src/errors/as-gateway-error.ts
|
|
292
|
+
function asGatewayError(error, authMethod) {
|
|
293
|
+
var _a8;
|
|
294
|
+
if (GatewayError.isInstance(error)) {
|
|
295
|
+
return error;
|
|
296
|
+
}
|
|
297
|
+
if (APICallError.isInstance(error)) {
|
|
298
|
+
return createGatewayErrorFromResponse({
|
|
299
|
+
response: extractApiCallResponse(error),
|
|
300
|
+
statusCode: (_a8 = error.statusCode) != null ? _a8 : 500,
|
|
301
|
+
defaultMessage: "Gateway request failed",
|
|
302
|
+
cause: error,
|
|
303
|
+
authMethod
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
return createGatewayErrorFromResponse({
|
|
307
|
+
response: {},
|
|
308
|
+
statusCode: 500,
|
|
309
|
+
defaultMessage: error instanceof Error ? `Gateway request failed: ${error.message}` : "Unknown Gateway error",
|
|
310
|
+
cause: error,
|
|
311
|
+
authMethod
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// src/errors/extract-api-call-response.ts
|
|
316
|
+
function extractApiCallResponse(error) {
|
|
317
|
+
if (error.data !== void 0) {
|
|
318
|
+
return error.data;
|
|
319
|
+
}
|
|
320
|
+
if (error.responseBody != null) {
|
|
321
|
+
try {
|
|
322
|
+
return JSON.parse(error.responseBody);
|
|
323
|
+
} catch (e) {
|
|
324
|
+
return error.responseBody;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return {};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// src/errors/parse-auth-method.ts
|
|
331
|
+
import { z as z3 } from "zod/v4";
|
|
332
|
+
var GATEWAY_AUTH_METHOD_HEADER = "ai-gateway-auth-method";
|
|
333
|
+
function parseAuthMethod(headers) {
|
|
334
|
+
const result = gatewayAuthMethodSchema.safeParse(
|
|
335
|
+
headers[GATEWAY_AUTH_METHOD_HEADER]
|
|
336
|
+
);
|
|
337
|
+
return result.success ? result.data : void 0;
|
|
338
|
+
}
|
|
339
|
+
var gatewayAuthMethodSchema = z3.union([
|
|
340
|
+
z3.literal("api-key"),
|
|
341
|
+
z3.literal("oidc")
|
|
342
|
+
]);
|
|
343
|
+
|
|
344
|
+
// src/gateway-fetch-metadata.ts
|
|
345
|
+
import {
|
|
346
|
+
createJsonErrorResponseHandler,
|
|
347
|
+
createJsonResponseHandler,
|
|
348
|
+
getFromApi,
|
|
349
|
+
resolve
|
|
350
|
+
} from "@ai-sdk/provider-utils";
|
|
351
|
+
import { z as z4 } from "zod/v4";
|
|
352
|
+
var GatewayFetchMetadata = class {
|
|
353
|
+
constructor(config) {
|
|
354
|
+
this.config = config;
|
|
355
|
+
}
|
|
356
|
+
async getAvailableModels() {
|
|
357
|
+
try {
|
|
358
|
+
const { value } = await getFromApi({
|
|
359
|
+
url: `${this.config.baseURL}/config`,
|
|
360
|
+
headers: await resolve(this.config.headers()),
|
|
361
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
362
|
+
gatewayFetchMetadataSchema
|
|
363
|
+
),
|
|
364
|
+
failedResponseHandler: createJsonErrorResponseHandler({
|
|
365
|
+
errorSchema: z4.any(),
|
|
366
|
+
errorToMessage: (data) => data
|
|
367
|
+
}),
|
|
368
|
+
fetch: this.config.fetch
|
|
369
|
+
});
|
|
370
|
+
return value;
|
|
371
|
+
} catch (error) {
|
|
372
|
+
throw asGatewayError(error);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
async getCredits() {
|
|
376
|
+
try {
|
|
377
|
+
const baseUrl = new URL(this.config.baseURL);
|
|
378
|
+
const { value } = await getFromApi({
|
|
379
|
+
url: `${baseUrl.origin}/v1/credits`,
|
|
380
|
+
headers: await resolve(this.config.headers()),
|
|
381
|
+
successfulResponseHandler: createJsonResponseHandler(gatewayCreditsSchema),
|
|
382
|
+
failedResponseHandler: createJsonErrorResponseHandler({
|
|
383
|
+
errorSchema: z4.any(),
|
|
384
|
+
errorToMessage: (data) => data
|
|
385
|
+
}),
|
|
386
|
+
fetch: this.config.fetch
|
|
387
|
+
});
|
|
388
|
+
return value;
|
|
389
|
+
} catch (error) {
|
|
390
|
+
throw asGatewayError(error);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
var gatewayLanguageModelSpecificationSchema = z4.object({
|
|
395
|
+
specificationVersion: z4.literal("v2"),
|
|
396
|
+
provider: z4.string(),
|
|
397
|
+
modelId: z4.string()
|
|
398
|
+
});
|
|
399
|
+
var gatewayLanguageModelPricingSchema = z4.object({
|
|
400
|
+
input: z4.string(),
|
|
401
|
+
output: z4.string(),
|
|
402
|
+
input_cache_read: z4.string().nullish(),
|
|
403
|
+
input_cache_write: z4.string().nullish()
|
|
404
|
+
}).transform(({ input, output, input_cache_read, input_cache_write }) => ({
|
|
405
|
+
input,
|
|
406
|
+
output,
|
|
407
|
+
...input_cache_read ? { cachedInputTokens: input_cache_read } : {},
|
|
408
|
+
...input_cache_write ? { cacheCreationInputTokens: input_cache_write } : {}
|
|
409
|
+
}));
|
|
410
|
+
var gatewayLanguageModelEntrySchema = z4.object({
|
|
411
|
+
id: z4.string(),
|
|
412
|
+
name: z4.string(),
|
|
413
|
+
description: z4.string().nullish(),
|
|
414
|
+
pricing: gatewayLanguageModelPricingSchema.nullish(),
|
|
415
|
+
specification: gatewayLanguageModelSpecificationSchema,
|
|
416
|
+
modelType: z4.enum(["language", "embedding", "image"]).nullish()
|
|
417
|
+
});
|
|
418
|
+
var gatewayFetchMetadataSchema = z4.object({
|
|
419
|
+
models: z4.array(gatewayLanguageModelEntrySchema)
|
|
420
|
+
});
|
|
421
|
+
var gatewayCreditsSchema = z4.object({
|
|
422
|
+
balance: z4.string(),
|
|
423
|
+
total_used: z4.string()
|
|
424
|
+
}).transform(({ balance, total_used }) => ({
|
|
425
|
+
balance,
|
|
426
|
+
totalUsed: total_used
|
|
427
|
+
}));
|
|
428
|
+
|
|
429
|
+
// src/gateway-language-model.ts
|
|
430
|
+
import {
|
|
431
|
+
combineHeaders,
|
|
432
|
+
createEventSourceResponseHandler,
|
|
433
|
+
createJsonErrorResponseHandler as createJsonErrorResponseHandler2,
|
|
434
|
+
createJsonResponseHandler as createJsonResponseHandler2,
|
|
435
|
+
postJsonToApi,
|
|
436
|
+
resolve as resolve2
|
|
437
|
+
} from "@ai-sdk/provider-utils";
|
|
438
|
+
import { z as z5 } from "zod/v4";
|
|
439
|
+
var GatewayLanguageModel = class {
|
|
440
|
+
constructor(modelId, config) {
|
|
441
|
+
this.modelId = modelId;
|
|
442
|
+
this.config = config;
|
|
443
|
+
this.specificationVersion = "v2";
|
|
444
|
+
this.supportedUrls = { "*/*": [/.*/] };
|
|
445
|
+
}
|
|
446
|
+
get provider() {
|
|
447
|
+
return this.config.provider;
|
|
448
|
+
}
|
|
449
|
+
async getArgs(options) {
|
|
450
|
+
const { abortSignal: _abortSignal, ...optionsWithoutSignal } = options;
|
|
451
|
+
return {
|
|
452
|
+
args: this.maybeEncodeFileParts(optionsWithoutSignal),
|
|
453
|
+
warnings: []
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
async doGenerate(options) {
|
|
457
|
+
const { args, warnings } = await this.getArgs(options);
|
|
458
|
+
const { abortSignal } = options;
|
|
459
|
+
const resolvedHeaders = await resolve2(this.config.headers());
|
|
460
|
+
try {
|
|
461
|
+
const {
|
|
462
|
+
responseHeaders,
|
|
463
|
+
value: responseBody,
|
|
464
|
+
rawValue: rawResponse
|
|
465
|
+
} = await postJsonToApi({
|
|
466
|
+
url: this.getUrl(),
|
|
467
|
+
headers: combineHeaders(
|
|
468
|
+
resolvedHeaders,
|
|
469
|
+
options.headers,
|
|
470
|
+
this.getModelConfigHeaders(this.modelId, false),
|
|
471
|
+
await resolve2(this.config.o11yHeaders)
|
|
472
|
+
),
|
|
473
|
+
body: args,
|
|
474
|
+
successfulResponseHandler: createJsonResponseHandler2(z5.any()),
|
|
475
|
+
failedResponseHandler: createJsonErrorResponseHandler2({
|
|
476
|
+
errorSchema: z5.any(),
|
|
477
|
+
errorToMessage: (data) => data
|
|
478
|
+
}),
|
|
479
|
+
...abortSignal && { abortSignal },
|
|
480
|
+
fetch: this.config.fetch
|
|
481
|
+
});
|
|
482
|
+
return {
|
|
483
|
+
...responseBody,
|
|
484
|
+
request: { body: args },
|
|
485
|
+
response: { headers: responseHeaders, body: rawResponse },
|
|
486
|
+
warnings
|
|
487
|
+
};
|
|
488
|
+
} catch (error) {
|
|
489
|
+
throw asGatewayError(error, parseAuthMethod(resolvedHeaders));
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
async doStream(options) {
|
|
493
|
+
const { args, warnings } = await this.getArgs(options);
|
|
494
|
+
const { abortSignal } = options;
|
|
495
|
+
const resolvedHeaders = await resolve2(this.config.headers());
|
|
496
|
+
try {
|
|
497
|
+
const { value: response, responseHeaders } = await postJsonToApi({
|
|
498
|
+
url: this.getUrl(),
|
|
499
|
+
headers: combineHeaders(
|
|
500
|
+
resolvedHeaders,
|
|
501
|
+
options.headers,
|
|
502
|
+
this.getModelConfigHeaders(this.modelId, true),
|
|
503
|
+
await resolve2(this.config.o11yHeaders)
|
|
504
|
+
),
|
|
505
|
+
body: args,
|
|
506
|
+
successfulResponseHandler: createEventSourceResponseHandler(z5.any()),
|
|
507
|
+
failedResponseHandler: createJsonErrorResponseHandler2({
|
|
508
|
+
errorSchema: z5.any(),
|
|
509
|
+
errorToMessage: (data) => data
|
|
510
|
+
}),
|
|
511
|
+
...abortSignal && { abortSignal },
|
|
512
|
+
fetch: this.config.fetch
|
|
513
|
+
});
|
|
514
|
+
return {
|
|
515
|
+
stream: response.pipeThrough(
|
|
516
|
+
new TransformStream({
|
|
517
|
+
start(controller) {
|
|
518
|
+
if (warnings.length > 0) {
|
|
519
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
520
|
+
}
|
|
521
|
+
},
|
|
522
|
+
transform(chunk, controller) {
|
|
523
|
+
if (chunk.success) {
|
|
524
|
+
const streamPart = chunk.value;
|
|
525
|
+
if (streamPart.type === "raw" && !options.includeRawChunks) {
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
if (streamPart.type === "response-metadata" && streamPart.timestamp && typeof streamPart.timestamp === "string") {
|
|
529
|
+
streamPart.timestamp = new Date(streamPart.timestamp);
|
|
530
|
+
}
|
|
531
|
+
controller.enqueue(streamPart);
|
|
532
|
+
} else {
|
|
533
|
+
controller.error(
|
|
534
|
+
chunk.error
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
})
|
|
539
|
+
),
|
|
540
|
+
request: { body: args },
|
|
541
|
+
response: { headers: responseHeaders }
|
|
542
|
+
};
|
|
543
|
+
} catch (error) {
|
|
544
|
+
throw asGatewayError(error, parseAuthMethod(resolvedHeaders));
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
isFilePart(part) {
|
|
548
|
+
return part && typeof part === "object" && "type" in part && part.type === "file";
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Encodes file parts in the prompt to base64. Mutates the passed options
|
|
552
|
+
* instance directly to avoid copying the file data.
|
|
553
|
+
* @param options - The options to encode.
|
|
554
|
+
* @returns The options with the file parts encoded.
|
|
555
|
+
*/
|
|
556
|
+
maybeEncodeFileParts(options) {
|
|
557
|
+
for (const message of options.prompt) {
|
|
558
|
+
for (const part of message.content) {
|
|
559
|
+
if (this.isFilePart(part)) {
|
|
560
|
+
const filePart = part;
|
|
561
|
+
if (filePart.data instanceof Uint8Array) {
|
|
562
|
+
const buffer = Uint8Array.from(filePart.data);
|
|
563
|
+
const base64Data = Buffer.from(buffer).toString("base64");
|
|
564
|
+
filePart.data = new URL(
|
|
565
|
+
`data:${filePart.mediaType || "application/octet-stream"};base64,${base64Data}`
|
|
566
|
+
);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
return options;
|
|
572
|
+
}
|
|
573
|
+
getUrl() {
|
|
574
|
+
return `${this.config.baseURL}/language-model`;
|
|
575
|
+
}
|
|
576
|
+
getModelConfigHeaders(modelId, streaming) {
|
|
577
|
+
return {
|
|
578
|
+
"ai-language-model-specification-version": "2",
|
|
579
|
+
"ai-language-model-id": modelId,
|
|
580
|
+
"ai-language-model-streaming": String(streaming)
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
};
|
|
584
|
+
|
|
585
|
+
// src/gateway-embedding-model.ts
|
|
586
|
+
import {
|
|
587
|
+
combineHeaders as combineHeaders2,
|
|
588
|
+
createJsonResponseHandler as createJsonResponseHandler3,
|
|
589
|
+
createJsonErrorResponseHandler as createJsonErrorResponseHandler3,
|
|
590
|
+
postJsonToApi as postJsonToApi2,
|
|
591
|
+
resolve as resolve3
|
|
592
|
+
} from "@ai-sdk/provider-utils";
|
|
593
|
+
import { z as z6 } from "zod/v4";
|
|
594
|
+
var GatewayEmbeddingModel = class {
|
|
595
|
+
constructor(modelId, config) {
|
|
596
|
+
this.modelId = modelId;
|
|
597
|
+
this.config = config;
|
|
598
|
+
this.specificationVersion = "v2";
|
|
599
|
+
this.maxEmbeddingsPerCall = 2048;
|
|
600
|
+
this.supportsParallelCalls = true;
|
|
601
|
+
}
|
|
602
|
+
get provider() {
|
|
603
|
+
return this.config.provider;
|
|
604
|
+
}
|
|
605
|
+
async doEmbed({
|
|
606
|
+
values,
|
|
607
|
+
headers,
|
|
608
|
+
abortSignal,
|
|
609
|
+
providerOptions
|
|
610
|
+
}) {
|
|
611
|
+
var _a8;
|
|
612
|
+
const resolvedHeaders = await resolve3(this.config.headers());
|
|
613
|
+
try {
|
|
614
|
+
const {
|
|
615
|
+
responseHeaders,
|
|
616
|
+
value: responseBody,
|
|
617
|
+
rawValue
|
|
618
|
+
} = await postJsonToApi2({
|
|
619
|
+
url: this.getUrl(),
|
|
620
|
+
headers: combineHeaders2(
|
|
621
|
+
resolvedHeaders,
|
|
622
|
+
headers != null ? headers : {},
|
|
623
|
+
this.getModelConfigHeaders(),
|
|
624
|
+
await resolve3(this.config.o11yHeaders)
|
|
625
|
+
),
|
|
626
|
+
body: {
|
|
627
|
+
input: values.length === 1 ? values[0] : values,
|
|
628
|
+
...providerOptions ? { providerOptions } : {}
|
|
629
|
+
},
|
|
630
|
+
successfulResponseHandler: createJsonResponseHandler3(
|
|
631
|
+
gatewayEmbeddingResponseSchema
|
|
632
|
+
),
|
|
633
|
+
failedResponseHandler: createJsonErrorResponseHandler3({
|
|
634
|
+
errorSchema: z6.any(),
|
|
635
|
+
errorToMessage: (data) => data
|
|
636
|
+
}),
|
|
637
|
+
...abortSignal && { abortSignal },
|
|
638
|
+
fetch: this.config.fetch
|
|
639
|
+
});
|
|
640
|
+
return {
|
|
641
|
+
embeddings: responseBody.embeddings,
|
|
642
|
+
usage: (_a8 = responseBody.usage) != null ? _a8 : void 0,
|
|
643
|
+
providerMetadata: responseBody.providerMetadata,
|
|
644
|
+
response: { headers: responseHeaders, body: rawValue }
|
|
645
|
+
};
|
|
646
|
+
} catch (error) {
|
|
647
|
+
throw asGatewayError(error, parseAuthMethod(resolvedHeaders));
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
getUrl() {
|
|
651
|
+
return `${this.config.baseURL}/embedding-model`;
|
|
652
|
+
}
|
|
653
|
+
getModelConfigHeaders() {
|
|
654
|
+
return {
|
|
655
|
+
"ai-embedding-model-specification-version": "2",
|
|
656
|
+
"ai-model-id": this.modelId
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
};
|
|
660
|
+
var gatewayEmbeddingResponseSchema = z6.object({
|
|
661
|
+
embeddings: z6.array(z6.array(z6.number())),
|
|
662
|
+
usage: z6.object({ tokens: z6.number() }).nullish(),
|
|
663
|
+
providerMetadata: z6.record(z6.string(), z6.record(z6.string(), z6.unknown())).optional()
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
// src/vercel-environment.ts
|
|
667
|
+
import { getContext } from "@vercel/oidc";
|
|
668
|
+
import { getVercelOidcToken } from "@vercel/oidc";
|
|
669
|
+
async function getVercelRequestId() {
|
|
670
|
+
var _a8;
|
|
671
|
+
return (_a8 = getContext().headers) == null ? void 0 : _a8["x-vercel-id"];
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
// src/gateway-provider.ts
|
|
675
|
+
import { withUserAgentSuffix } from "@ai-sdk/provider-utils";
|
|
676
|
+
|
|
677
|
+
// src/version.ts
|
|
678
|
+
var VERSION = true ? "0.0.0-02dba89b-20251009204516" : "0.0.0-test";
|
|
679
|
+
|
|
680
|
+
// src/gateway-provider.ts
|
|
681
|
+
var AI_GATEWAY_PROTOCOL_VERSION = "0.0.1";
|
|
682
|
+
function createGatewayProvider(options = {}) {
|
|
683
|
+
var _a8, _b8;
|
|
684
|
+
let pendingMetadata = null;
|
|
685
|
+
let metadataCache = null;
|
|
686
|
+
const cacheRefreshMillis = (_a8 = options.metadataCacheRefreshMillis) != null ? _a8 : 1e3 * 60 * 5;
|
|
687
|
+
let lastFetchTime = 0;
|
|
688
|
+
const baseURL = (_b8 = withoutTrailingSlash(options.baseURL)) != null ? _b8 : "https://ai-gateway.vercel.sh/v1/ai";
|
|
689
|
+
const getHeaders = async () => {
|
|
690
|
+
const auth = await getGatewayAuthToken(options);
|
|
691
|
+
if (auth) {
|
|
692
|
+
return withUserAgentSuffix(
|
|
693
|
+
{
|
|
694
|
+
Authorization: `Bearer ${auth.token}`,
|
|
695
|
+
"ai-gateway-protocol-version": AI_GATEWAY_PROTOCOL_VERSION,
|
|
696
|
+
[GATEWAY_AUTH_METHOD_HEADER]: auth.authMethod,
|
|
697
|
+
...options.headers
|
|
698
|
+
},
|
|
699
|
+
`ai-sdk/gateway/${VERSION}`
|
|
700
|
+
);
|
|
701
|
+
}
|
|
702
|
+
throw GatewayAuthenticationError.createContextualError({
|
|
703
|
+
apiKeyProvided: false,
|
|
704
|
+
oidcTokenProvided: false,
|
|
705
|
+
statusCode: 401
|
|
706
|
+
});
|
|
707
|
+
};
|
|
708
|
+
const createO11yHeaders = () => {
|
|
709
|
+
const deploymentId = loadOptionalSetting({
|
|
710
|
+
settingValue: void 0,
|
|
711
|
+
environmentVariableName: "VERCEL_DEPLOYMENT_ID"
|
|
712
|
+
});
|
|
713
|
+
const environment = loadOptionalSetting({
|
|
714
|
+
settingValue: void 0,
|
|
715
|
+
environmentVariableName: "VERCEL_ENV"
|
|
716
|
+
});
|
|
717
|
+
const region = loadOptionalSetting({
|
|
718
|
+
settingValue: void 0,
|
|
719
|
+
environmentVariableName: "VERCEL_REGION"
|
|
720
|
+
});
|
|
721
|
+
return async () => {
|
|
722
|
+
const requestId = await getVercelRequestId();
|
|
723
|
+
return {
|
|
724
|
+
...deploymentId && { "ai-o11y-deployment-id": deploymentId },
|
|
725
|
+
...environment && { "ai-o11y-environment": environment },
|
|
726
|
+
...region && { "ai-o11y-region": region },
|
|
727
|
+
...requestId && { "ai-o11y-request-id": requestId }
|
|
728
|
+
};
|
|
729
|
+
};
|
|
730
|
+
};
|
|
731
|
+
const createLanguageModel = (modelId) => {
|
|
732
|
+
return new GatewayLanguageModel(modelId, {
|
|
733
|
+
provider: "gateway",
|
|
734
|
+
baseURL,
|
|
735
|
+
headers: getHeaders,
|
|
736
|
+
fetch: options.fetch,
|
|
737
|
+
o11yHeaders: createO11yHeaders()
|
|
738
|
+
});
|
|
739
|
+
};
|
|
740
|
+
const getAvailableModels = async () => {
|
|
741
|
+
var _a9, _b9, _c;
|
|
742
|
+
const now = (_c = (_b9 = (_a9 = options._internal) == null ? void 0 : _a9.currentDate) == null ? void 0 : _b9.call(_a9).getTime()) != null ? _c : Date.now();
|
|
743
|
+
if (!pendingMetadata || now - lastFetchTime > cacheRefreshMillis) {
|
|
744
|
+
lastFetchTime = now;
|
|
745
|
+
pendingMetadata = new GatewayFetchMetadata({
|
|
746
|
+
baseURL,
|
|
747
|
+
headers: getHeaders,
|
|
748
|
+
fetch: options.fetch
|
|
749
|
+
}).getAvailableModels().then((metadata) => {
|
|
750
|
+
metadataCache = metadata;
|
|
751
|
+
return metadata;
|
|
752
|
+
}).catch(async (error) => {
|
|
753
|
+
throw asGatewayError(error, parseAuthMethod(await getHeaders()));
|
|
754
|
+
});
|
|
755
|
+
}
|
|
756
|
+
return metadataCache ? Promise.resolve(metadataCache) : pendingMetadata;
|
|
757
|
+
};
|
|
758
|
+
const getCredits = async () => {
|
|
759
|
+
return new GatewayFetchMetadata({
|
|
760
|
+
baseURL,
|
|
761
|
+
headers: getHeaders,
|
|
762
|
+
fetch: options.fetch
|
|
763
|
+
}).getCredits().catch(async (error) => {
|
|
764
|
+
throw asGatewayError(error, parseAuthMethod(await getHeaders()));
|
|
765
|
+
});
|
|
766
|
+
};
|
|
767
|
+
const provider = function(modelId) {
|
|
768
|
+
if (new.target) {
|
|
769
|
+
throw new Error(
|
|
770
|
+
"The Gateway Provider model function cannot be called with the new keyword."
|
|
771
|
+
);
|
|
772
|
+
}
|
|
773
|
+
return createLanguageModel(modelId);
|
|
774
|
+
};
|
|
775
|
+
provider.getAvailableModels = getAvailableModels;
|
|
776
|
+
provider.getCredits = getCredits;
|
|
777
|
+
provider.imageModel = (modelId) => {
|
|
778
|
+
throw new NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
779
|
+
};
|
|
780
|
+
provider.languageModel = createLanguageModel;
|
|
781
|
+
provider.textEmbeddingModel = (modelId) => {
|
|
782
|
+
return new GatewayEmbeddingModel(modelId, {
|
|
783
|
+
provider: "gateway",
|
|
784
|
+
baseURL,
|
|
785
|
+
headers: getHeaders,
|
|
786
|
+
fetch: options.fetch,
|
|
787
|
+
o11yHeaders: createO11yHeaders()
|
|
788
|
+
});
|
|
789
|
+
};
|
|
790
|
+
return provider;
|
|
791
|
+
}
|
|
792
|
+
var gateway = createGatewayProvider();
|
|
793
|
+
async function getGatewayAuthToken(options) {
|
|
794
|
+
const apiKey = loadOptionalSetting({
|
|
795
|
+
settingValue: options.apiKey,
|
|
796
|
+
environmentVariableName: "AI_GATEWAY_API_KEY"
|
|
797
|
+
});
|
|
798
|
+
if (apiKey) {
|
|
799
|
+
return {
|
|
800
|
+
token: apiKey,
|
|
801
|
+
authMethod: "api-key"
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
try {
|
|
805
|
+
const oidcToken = await getVercelOidcToken();
|
|
806
|
+
return {
|
|
807
|
+
token: oidcToken,
|
|
808
|
+
authMethod: "oidc"
|
|
809
|
+
};
|
|
810
|
+
} catch (e) {
|
|
811
|
+
return null;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
export {
|
|
815
|
+
GatewayAuthenticationError,
|
|
816
|
+
GatewayError,
|
|
817
|
+
GatewayInternalServerError,
|
|
818
|
+
GatewayInvalidRequestError,
|
|
819
|
+
GatewayModelNotFoundError,
|
|
820
|
+
GatewayRateLimitError,
|
|
821
|
+
GatewayResponseError,
|
|
822
|
+
createGatewayProvider as createGateway,
|
|
823
|
+
createGatewayProvider,
|
|
824
|
+
gateway
|
|
825
|
+
};
|
|
826
|
+
//# sourceMappingURL=index.mjs.map
|