@ai-sdk/gateway 1.0.0-alpha.6 → 1.0.0-alpha.8
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 +20 -0
- package/dist/index.d.mts +160 -2
- package/dist/index.d.ts +160 -2
- package/dist/index.js +421 -138
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +417 -140
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -4,8 +4,8 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
6
|
var __export = (target, all) => {
|
|
7
|
-
for (var
|
|
8
|
-
__defProp(target,
|
|
7
|
+
for (var name7 in all)
|
|
8
|
+
__defProp(target, name7, { get: all[name7], enumerable: true });
|
|
9
9
|
};
|
|
10
10
|
var __copyProps = (to, from, except, desc) => {
|
|
11
11
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
@@ -20,19 +20,332 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
|
+
GatewayAuthenticationError: () => GatewayAuthenticationError,
|
|
24
|
+
GatewayError: () => GatewayError,
|
|
25
|
+
GatewayInternalServerError: () => GatewayInternalServerError,
|
|
26
|
+
GatewayInvalidRequestError: () => GatewayInvalidRequestError,
|
|
27
|
+
GatewayModelNotFoundError: () => GatewayModelNotFoundError,
|
|
28
|
+
GatewayRateLimitError: () => GatewayRateLimitError,
|
|
29
|
+
GatewayResponseError: () => GatewayResponseError,
|
|
23
30
|
createGatewayProvider: () => createGatewayProvider,
|
|
24
31
|
gateway: () => gateway
|
|
25
32
|
});
|
|
26
33
|
module.exports = __toCommonJS(src_exports);
|
|
27
34
|
|
|
28
35
|
// src/gateway-provider.ts
|
|
29
|
-
var
|
|
36
|
+
var import_provider2 = require("@ai-sdk/provider");
|
|
30
37
|
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
31
|
-
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
|
32
38
|
|
|
33
|
-
// src/gateway-
|
|
34
|
-
var
|
|
39
|
+
// src/errors/as-gateway-error.ts
|
|
40
|
+
var import_provider = require("@ai-sdk/provider");
|
|
41
|
+
|
|
42
|
+
// src/errors/create-gateway-error.ts
|
|
43
|
+
var import_zod2 = require("zod");
|
|
44
|
+
|
|
45
|
+
// src/errors/gateway-error.ts
|
|
46
|
+
var marker = "vercel.ai.gateway.error";
|
|
47
|
+
var symbol = Symbol.for(marker);
|
|
48
|
+
var _a, _b;
|
|
49
|
+
var GatewayError = class _GatewayError extends (_b = Error, _a = symbol, _b) {
|
|
50
|
+
constructor({
|
|
51
|
+
message,
|
|
52
|
+
statusCode = 500,
|
|
53
|
+
cause
|
|
54
|
+
}) {
|
|
55
|
+
super(message);
|
|
56
|
+
this[_a] = true;
|
|
57
|
+
this.statusCode = statusCode;
|
|
58
|
+
this.cause = cause;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Checks if the given error is a Gateway Error.
|
|
62
|
+
* @param {unknown} error - The error to check.
|
|
63
|
+
* @returns {boolean} True if the error is a Gateway Error, false otherwise.
|
|
64
|
+
*/
|
|
65
|
+
static isInstance(error) {
|
|
66
|
+
return _GatewayError.hasMarker(error);
|
|
67
|
+
}
|
|
68
|
+
static hasMarker(error) {
|
|
69
|
+
return typeof error === "object" && error !== null && symbol in error && error[symbol] === true;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// src/errors/gateway-authentication-error.ts
|
|
74
|
+
var name = "GatewayAuthenticationError";
|
|
75
|
+
var marker2 = `vercel.ai.gateway.error.${name}`;
|
|
76
|
+
var symbol2 = Symbol.for(marker2);
|
|
77
|
+
var _a2, _b2;
|
|
78
|
+
var GatewayAuthenticationError = class extends (_b2 = GatewayError, _a2 = symbol2, _b2) {
|
|
79
|
+
constructor({
|
|
80
|
+
message = "Authentication failed",
|
|
81
|
+
statusCode = 401,
|
|
82
|
+
cause
|
|
83
|
+
} = {}) {
|
|
84
|
+
super({ message, statusCode, cause });
|
|
85
|
+
this[_a2] = true;
|
|
86
|
+
// used in isInstance
|
|
87
|
+
this.name = name;
|
|
88
|
+
this.type = "authentication_error";
|
|
89
|
+
}
|
|
90
|
+
static isInstance(error) {
|
|
91
|
+
return GatewayError.hasMarker(error) && symbol2 in error;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// src/errors/gateway-invalid-request-error.ts
|
|
96
|
+
var name2 = "GatewayInvalidRequestError";
|
|
97
|
+
var marker3 = `vercel.ai.gateway.error.${name2}`;
|
|
98
|
+
var symbol3 = Symbol.for(marker3);
|
|
99
|
+
var _a3, _b3;
|
|
100
|
+
var GatewayInvalidRequestError = class extends (_b3 = GatewayError, _a3 = symbol3, _b3) {
|
|
101
|
+
constructor({
|
|
102
|
+
message = "Invalid request",
|
|
103
|
+
statusCode = 400,
|
|
104
|
+
cause
|
|
105
|
+
} = {}) {
|
|
106
|
+
super({ message, statusCode, cause });
|
|
107
|
+
this[_a3] = true;
|
|
108
|
+
// used in isInstance
|
|
109
|
+
this.name = name2;
|
|
110
|
+
this.type = "invalid_request_error";
|
|
111
|
+
}
|
|
112
|
+
static isInstance(error) {
|
|
113
|
+
return GatewayError.hasMarker(error) && symbol3 in error;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// src/errors/gateway-rate-limit-error.ts
|
|
118
|
+
var name3 = "GatewayRateLimitError";
|
|
119
|
+
var marker4 = `vercel.ai.gateway.error.${name3}`;
|
|
120
|
+
var symbol4 = Symbol.for(marker4);
|
|
121
|
+
var _a4, _b4;
|
|
122
|
+
var GatewayRateLimitError = class extends (_b4 = GatewayError, _a4 = symbol4, _b4) {
|
|
123
|
+
constructor({
|
|
124
|
+
message = "Rate limit exceeded",
|
|
125
|
+
statusCode = 429,
|
|
126
|
+
cause
|
|
127
|
+
} = {}) {
|
|
128
|
+
super({ message, statusCode, cause });
|
|
129
|
+
this[_a4] = true;
|
|
130
|
+
// used in isInstance
|
|
131
|
+
this.name = name3;
|
|
132
|
+
this.type = "rate_limit_exceeded";
|
|
133
|
+
}
|
|
134
|
+
static isInstance(error) {
|
|
135
|
+
return GatewayError.hasMarker(error) && symbol4 in error;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// src/errors/gateway-model-not-found-error.ts
|
|
35
140
|
var import_zod = require("zod");
|
|
141
|
+
var name4 = "GatewayModelNotFoundError";
|
|
142
|
+
var marker5 = `vercel.ai.gateway.error.${name4}`;
|
|
143
|
+
var symbol5 = Symbol.for(marker5);
|
|
144
|
+
var modelNotFoundParamSchema = import_zod.z.object({
|
|
145
|
+
modelId: import_zod.z.string()
|
|
146
|
+
});
|
|
147
|
+
var _a5, _b5;
|
|
148
|
+
var GatewayModelNotFoundError = class extends (_b5 = GatewayError, _a5 = symbol5, _b5) {
|
|
149
|
+
constructor({
|
|
150
|
+
message = "Model not found",
|
|
151
|
+
statusCode = 404,
|
|
152
|
+
modelId,
|
|
153
|
+
cause
|
|
154
|
+
} = {}) {
|
|
155
|
+
super({ message, statusCode, cause });
|
|
156
|
+
this[_a5] = true;
|
|
157
|
+
// used in isInstance
|
|
158
|
+
this.name = name4;
|
|
159
|
+
this.type = "model_not_found";
|
|
160
|
+
this.modelId = modelId;
|
|
161
|
+
}
|
|
162
|
+
static isInstance(error) {
|
|
163
|
+
return GatewayError.hasMarker(error) && symbol5 in error;
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
// src/errors/gateway-internal-server-error.ts
|
|
168
|
+
var name5 = "GatewayInternalServerError";
|
|
169
|
+
var marker6 = `vercel.ai.gateway.error.${name5}`;
|
|
170
|
+
var symbol6 = Symbol.for(marker6);
|
|
171
|
+
var _a6, _b6;
|
|
172
|
+
var GatewayInternalServerError = class extends (_b6 = GatewayError, _a6 = symbol6, _b6) {
|
|
173
|
+
constructor({
|
|
174
|
+
message = "Internal server error",
|
|
175
|
+
statusCode = 500,
|
|
176
|
+
cause
|
|
177
|
+
} = {}) {
|
|
178
|
+
super({ message, statusCode, cause });
|
|
179
|
+
this[_a6] = true;
|
|
180
|
+
// used in isInstance
|
|
181
|
+
this.name = name5;
|
|
182
|
+
this.type = "internal_server_error";
|
|
183
|
+
}
|
|
184
|
+
static isInstance(error) {
|
|
185
|
+
return GatewayError.hasMarker(error) && symbol6 in error;
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
// src/errors/gateway-response-error.ts
|
|
190
|
+
var name6 = "GatewayResponseError";
|
|
191
|
+
var marker7 = `vercel.ai.gateway.error.${name6}`;
|
|
192
|
+
var symbol7 = Symbol.for(marker7);
|
|
193
|
+
var _a7, _b7;
|
|
194
|
+
var GatewayResponseError = class extends (_b7 = GatewayError, _a7 = symbol7, _b7) {
|
|
195
|
+
constructor({
|
|
196
|
+
message = "Invalid response from Gateway",
|
|
197
|
+
statusCode = 502,
|
|
198
|
+
response,
|
|
199
|
+
validationError,
|
|
200
|
+
cause
|
|
201
|
+
} = {}) {
|
|
202
|
+
super({ message, statusCode, cause });
|
|
203
|
+
this[_a7] = true;
|
|
204
|
+
// used in isInstance
|
|
205
|
+
this.name = name6;
|
|
206
|
+
this.type = "response_error";
|
|
207
|
+
this.response = response;
|
|
208
|
+
this.validationError = validationError;
|
|
209
|
+
}
|
|
210
|
+
static isInstance(error) {
|
|
211
|
+
return GatewayError.hasMarker(error) && symbol7 in error;
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
// src/errors/create-gateway-error.ts
|
|
216
|
+
function createGatewayErrorFromResponse({
|
|
217
|
+
response,
|
|
218
|
+
statusCode,
|
|
219
|
+
defaultMessage = "Gateway request failed",
|
|
220
|
+
cause
|
|
221
|
+
}) {
|
|
222
|
+
const parseResult = gatewayErrorResponseSchema.safeParse(response);
|
|
223
|
+
if (!parseResult.success) {
|
|
224
|
+
return new GatewayResponseError({
|
|
225
|
+
message: `Invalid error response format: ${defaultMessage}`,
|
|
226
|
+
statusCode,
|
|
227
|
+
response,
|
|
228
|
+
validationError: parseResult.error,
|
|
229
|
+
cause
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
const validatedResponse = parseResult.data;
|
|
233
|
+
const errorType = validatedResponse.error.type;
|
|
234
|
+
const message = validatedResponse.error.message;
|
|
235
|
+
switch (errorType) {
|
|
236
|
+
case "authentication_error":
|
|
237
|
+
return new GatewayAuthenticationError({ message, statusCode, cause });
|
|
238
|
+
case "invalid_request_error":
|
|
239
|
+
return new GatewayInvalidRequestError({ message, statusCode, cause });
|
|
240
|
+
case "rate_limit_exceeded":
|
|
241
|
+
return new GatewayRateLimitError({ message, statusCode, cause });
|
|
242
|
+
case "model_not_found": {
|
|
243
|
+
const modelResult = modelNotFoundParamSchema.safeParse(
|
|
244
|
+
validatedResponse.error.param
|
|
245
|
+
);
|
|
246
|
+
return new GatewayModelNotFoundError({
|
|
247
|
+
message,
|
|
248
|
+
statusCode,
|
|
249
|
+
modelId: modelResult.success ? modelResult.data.modelId : void 0,
|
|
250
|
+
cause
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
case "internal_server_error":
|
|
254
|
+
return new GatewayInternalServerError({ message, statusCode, cause });
|
|
255
|
+
default:
|
|
256
|
+
return new GatewayInternalServerError({ message, statusCode, cause });
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
var gatewayErrorResponseSchema = import_zod2.z.object({
|
|
260
|
+
error: import_zod2.z.object({
|
|
261
|
+
message: import_zod2.z.string(),
|
|
262
|
+
type: import_zod2.z.string().nullish(),
|
|
263
|
+
param: import_zod2.z.unknown().nullish(),
|
|
264
|
+
code: import_zod2.z.union([import_zod2.z.string(), import_zod2.z.number()]).nullish()
|
|
265
|
+
})
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
// src/errors/as-gateway-error.ts
|
|
269
|
+
function asGatewayError(error) {
|
|
270
|
+
var _a8;
|
|
271
|
+
if (GatewayError.isInstance(error)) {
|
|
272
|
+
return error;
|
|
273
|
+
}
|
|
274
|
+
if (import_provider.APICallError.isInstance(error)) {
|
|
275
|
+
return createGatewayErrorFromResponse({
|
|
276
|
+
response: extractApiCallResponse(error),
|
|
277
|
+
statusCode: (_a8 = error.statusCode) != null ? _a8 : 500,
|
|
278
|
+
defaultMessage: "Gateway request failed",
|
|
279
|
+
cause: error
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
return createGatewayErrorFromResponse({
|
|
283
|
+
response: {},
|
|
284
|
+
statusCode: 500,
|
|
285
|
+
defaultMessage: error instanceof Error ? `Gateway request failed: ${error.message}` : "Unknown Gateway error",
|
|
286
|
+
cause: error
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// src/errors/extract-api-call-response.ts
|
|
291
|
+
function extractApiCallResponse(error) {
|
|
292
|
+
if (error.data !== void 0) {
|
|
293
|
+
return error.data;
|
|
294
|
+
}
|
|
295
|
+
if (error.responseBody != null) {
|
|
296
|
+
try {
|
|
297
|
+
return JSON.parse(error.responseBody);
|
|
298
|
+
} catch (e) {
|
|
299
|
+
return error.responseBody;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return {};
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// src/gateway-fetch-metadata.ts
|
|
306
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
307
|
+
var import_zod3 = require("zod");
|
|
308
|
+
var GatewayFetchMetadata = class {
|
|
309
|
+
constructor(config) {
|
|
310
|
+
this.config = config;
|
|
311
|
+
}
|
|
312
|
+
async getAvailableModels() {
|
|
313
|
+
try {
|
|
314
|
+
const { value } = await (0, import_provider_utils.getFromApi)({
|
|
315
|
+
url: `${this.config.baseURL}/config`,
|
|
316
|
+
headers: await (0, import_provider_utils.resolve)(this.config.headers()),
|
|
317
|
+
successfulResponseHandler: (0, import_provider_utils.createJsonResponseHandler)(
|
|
318
|
+
gatewayFetchMetadataSchema
|
|
319
|
+
),
|
|
320
|
+
failedResponseHandler: (0, import_provider_utils.createJsonErrorResponseHandler)({
|
|
321
|
+
errorSchema: import_zod3.z.any(),
|
|
322
|
+
errorToMessage: (data) => data
|
|
323
|
+
}),
|
|
324
|
+
fetch: this.config.fetch
|
|
325
|
+
});
|
|
326
|
+
return value;
|
|
327
|
+
} catch (error) {
|
|
328
|
+
throw asGatewayError(error);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
var gatewayLanguageModelSpecificationSchema = import_zod3.z.object({
|
|
333
|
+
specificationVersion: import_zod3.z.literal("v2"),
|
|
334
|
+
provider: import_zod3.z.string(),
|
|
335
|
+
modelId: import_zod3.z.string()
|
|
336
|
+
});
|
|
337
|
+
var gatewayLanguageModelEntrySchema = import_zod3.z.object({
|
|
338
|
+
id: import_zod3.z.string(),
|
|
339
|
+
name: import_zod3.z.string(),
|
|
340
|
+
specification: gatewayLanguageModelSpecificationSchema
|
|
341
|
+
});
|
|
342
|
+
var gatewayFetchMetadataSchema = import_zod3.z.object({
|
|
343
|
+
models: import_zod3.z.array(gatewayLanguageModelEntrySchema)
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
// src/gateway-language-model.ts
|
|
347
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
348
|
+
var import_zod4 = require("zod");
|
|
36
349
|
var GatewayLanguageModel = class {
|
|
37
350
|
constructor(modelId, config) {
|
|
38
351
|
this.modelId = modelId;
|
|
@@ -45,70 +358,78 @@ var GatewayLanguageModel = class {
|
|
|
45
358
|
}
|
|
46
359
|
async doGenerate(options) {
|
|
47
360
|
const { abortSignal, ...body } = options;
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
361
|
+
try {
|
|
362
|
+
const {
|
|
363
|
+
responseHeaders,
|
|
364
|
+
value: responseBody,
|
|
365
|
+
rawValue: rawResponse
|
|
366
|
+
} = await (0, import_provider_utils2.postJsonToApi)({
|
|
367
|
+
url: this.getUrl(),
|
|
368
|
+
headers: (0, import_provider_utils2.combineHeaders)(
|
|
369
|
+
await (0, import_provider_utils2.resolve)(this.config.headers()),
|
|
370
|
+
options.headers,
|
|
371
|
+
this.getModelConfigHeaders(this.modelId, false),
|
|
372
|
+
this.config.o11yHeaders
|
|
373
|
+
),
|
|
374
|
+
body: this.maybeEncodeFileParts(body),
|
|
375
|
+
successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(import_zod4.z.any()),
|
|
376
|
+
failedResponseHandler: (0, import_provider_utils2.createJsonErrorResponseHandler)({
|
|
377
|
+
errorSchema: import_zod4.z.any(),
|
|
378
|
+
errorToMessage: (data) => data
|
|
379
|
+
}),
|
|
380
|
+
...abortSignal && { abortSignal },
|
|
381
|
+
fetch: this.config.fetch
|
|
382
|
+
});
|
|
383
|
+
return {
|
|
384
|
+
...responseBody,
|
|
385
|
+
request: { body },
|
|
386
|
+
response: { headers: responseHeaders, body: rawResponse },
|
|
387
|
+
warnings: []
|
|
388
|
+
};
|
|
389
|
+
} catch (error) {
|
|
390
|
+
throw asGatewayError(error);
|
|
391
|
+
}
|
|
75
392
|
}
|
|
76
393
|
async doStream(options) {
|
|
77
394
|
const { abortSignal, ...body } = options;
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
395
|
+
try {
|
|
396
|
+
const { value: response, responseHeaders } = await (0, import_provider_utils2.postJsonToApi)({
|
|
397
|
+
url: this.getUrl(),
|
|
398
|
+
headers: (0, import_provider_utils2.combineHeaders)(
|
|
399
|
+
await (0, import_provider_utils2.resolve)(this.config.headers()),
|
|
400
|
+
options.headers,
|
|
401
|
+
this.getModelConfigHeaders(this.modelId, true),
|
|
402
|
+
this.config.o11yHeaders
|
|
403
|
+
),
|
|
404
|
+
body: this.maybeEncodeFileParts(body),
|
|
405
|
+
successfulResponseHandler: (0, import_provider_utils2.createEventSourceResponseHandler)(import_zod4.z.any()),
|
|
406
|
+
failedResponseHandler: (0, import_provider_utils2.createJsonErrorResponseHandler)({
|
|
407
|
+
errorSchema: import_zod4.z.any(),
|
|
408
|
+
errorToMessage: (data) => data
|
|
409
|
+
}),
|
|
410
|
+
...abortSignal && { abortSignal },
|
|
411
|
+
fetch: this.config.fetch
|
|
412
|
+
});
|
|
413
|
+
return {
|
|
414
|
+
stream: response.pipeThrough(
|
|
415
|
+
new TransformStream({
|
|
416
|
+
transform(chunk, controller) {
|
|
417
|
+
if (chunk.success) {
|
|
418
|
+
controller.enqueue(chunk.value);
|
|
419
|
+
} else {
|
|
420
|
+
controller.error(
|
|
421
|
+
chunk.error
|
|
422
|
+
);
|
|
423
|
+
}
|
|
105
424
|
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
425
|
+
})
|
|
426
|
+
),
|
|
427
|
+
request: { body },
|
|
428
|
+
response: { headers: responseHeaders }
|
|
429
|
+
};
|
|
430
|
+
} catch (error) {
|
|
431
|
+
throw asGatewayError(error);
|
|
432
|
+
}
|
|
112
433
|
}
|
|
113
434
|
isFilePart(part) {
|
|
114
435
|
return part && typeof part === "object" && "type" in part && part.type === "file";
|
|
@@ -150,91 +471,44 @@ var GatewayLanguageModel = class {
|
|
|
150
471
|
|
|
151
472
|
// src/get-vercel-oidc-token.ts
|
|
152
473
|
async function getVercelOidcToken() {
|
|
153
|
-
var
|
|
154
|
-
const token = (
|
|
474
|
+
var _a8, _b8;
|
|
475
|
+
const token = (_b8 = (_a8 = getContext().headers) == null ? void 0 : _a8["x-vercel-oidc-token"]) != null ? _b8 : process.env.VERCEL_OIDC_TOKEN;
|
|
155
476
|
if (!token) {
|
|
156
|
-
throw new
|
|
157
|
-
`
|
|
158
|
-
|
|
477
|
+
throw new GatewayAuthenticationError({
|
|
478
|
+
message: `Failed to get Vercel OIDC token for AI Gateway access.
|
|
479
|
+
The token is expected to be provided via the 'VERCEL_OIDC_TOKEN' environment variable. It expires every 12 hours.
|
|
480
|
+
- make sure your Vercel project settings have OIDC enabled
|
|
481
|
+
- if you're running locally with 'vercel dev' the token is automatically obtained and refreshed for you
|
|
482
|
+
- if you're running locally with your own dev server script you can fetch/update the token by running 'vercel env pull'
|
|
483
|
+
- in production or preview the token is automatically obtained and refreshed for you`,
|
|
484
|
+
statusCode: 401
|
|
485
|
+
});
|
|
159
486
|
}
|
|
160
487
|
return token;
|
|
161
488
|
}
|
|
162
489
|
var SYMBOL_FOR_REQ_CONTEXT = Symbol.for("@vercel/request-context");
|
|
163
490
|
function getContext() {
|
|
164
|
-
var
|
|
491
|
+
var _a8, _b8, _c;
|
|
165
492
|
const fromSymbol = globalThis;
|
|
166
|
-
return (_c = (
|
|
493
|
+
return (_c = (_b8 = (_a8 = fromSymbol[SYMBOL_FOR_REQ_CONTEXT]) == null ? void 0 : _a8.get) == null ? void 0 : _b8.call(_a8)) != null ? _c : {};
|
|
167
494
|
}
|
|
168
495
|
|
|
169
|
-
// src/gateway-fetch-metadata.ts
|
|
170
|
-
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
171
|
-
var import_zod2 = require("zod");
|
|
172
|
-
var GatewayFetchMetadata = class {
|
|
173
|
-
constructor(config) {
|
|
174
|
-
this.config = config;
|
|
175
|
-
}
|
|
176
|
-
async getAvailableModels() {
|
|
177
|
-
const { value } = await (0, import_provider_utils2.getFromApi)({
|
|
178
|
-
url: `${this.config.baseURL}/config`,
|
|
179
|
-
headers: await (0, import_provider_utils2.resolve)(this.config.headers()),
|
|
180
|
-
successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
|
|
181
|
-
gatewayFetchMetadataSchema
|
|
182
|
-
),
|
|
183
|
-
failedResponseHandler: (0, import_provider_utils2.createJsonErrorResponseHandler)({
|
|
184
|
-
errorSchema: import_zod2.z.any(),
|
|
185
|
-
errorToMessage: (data) => data
|
|
186
|
-
}),
|
|
187
|
-
fetch: this.config.fetch
|
|
188
|
-
});
|
|
189
|
-
return value;
|
|
190
|
-
}
|
|
191
|
-
};
|
|
192
|
-
var gatewayLanguageModelSpecificationSchema = import_zod2.z.object({
|
|
193
|
-
specificationVersion: import_zod2.z.literal("v2"),
|
|
194
|
-
provider: import_zod2.z.string(),
|
|
195
|
-
modelId: import_zod2.z.string()
|
|
196
|
-
});
|
|
197
|
-
var gatewayLanguageModelEntrySchema = import_zod2.z.object({
|
|
198
|
-
id: import_zod2.z.string(),
|
|
199
|
-
name: import_zod2.z.string(),
|
|
200
|
-
specification: gatewayLanguageModelSpecificationSchema
|
|
201
|
-
});
|
|
202
|
-
var gatewayFetchMetadataSchema = import_zod2.z.object({
|
|
203
|
-
models: import_zod2.z.array(gatewayLanguageModelEntrySchema)
|
|
204
|
-
});
|
|
205
|
-
|
|
206
496
|
// src/gateway-provider.ts
|
|
207
497
|
var AI_GATEWAY_PROTOCOL_VERSION = "0.0.1";
|
|
208
498
|
async function getGatewayAuthToken(options) {
|
|
209
|
-
var
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
})) != null ? _a : await getVercelOidcToken();
|
|
215
|
-
} catch (error) {
|
|
216
|
-
if (error instanceof Error && error.message.includes("'x-vercel-oidc-token' header is missing")) {
|
|
217
|
-
const enhancedError = new Error(
|
|
218
|
-
`Failed to get Vercel OIDC token for AI Gateway access.
|
|
219
|
-
The token is expected to be provided via the 'VERCEL_OIDC_TOKEN' environment variable. It expires every 12 hours.
|
|
220
|
-
- make sure your Vercel project settings have OIDC enabled
|
|
221
|
-
- if you're running locally with 'vercel dev' the token is automatically obtained and refreshed for you
|
|
222
|
-
- if you're running locally with your own dev server script you can fetch/update the token by running 'vercel env pull'
|
|
223
|
-
- in production or preview the token is automatically obtained and refreshed for you`
|
|
224
|
-
);
|
|
225
|
-
enhancedError.cause = error;
|
|
226
|
-
throw enhancedError;
|
|
227
|
-
}
|
|
228
|
-
throw error;
|
|
229
|
-
}
|
|
499
|
+
var _a8;
|
|
500
|
+
return (_a8 = (0, import_provider_utils3.loadOptionalSetting)({
|
|
501
|
+
settingValue: options.apiKey,
|
|
502
|
+
environmentVariableName: "AI_GATEWAY_API_KEY"
|
|
503
|
+
})) != null ? _a8 : await getVercelOidcToken();
|
|
230
504
|
}
|
|
231
505
|
function createGatewayProvider(options = {}) {
|
|
232
|
-
var
|
|
506
|
+
var _a8, _b8;
|
|
233
507
|
let pendingMetadata = null;
|
|
234
508
|
let metadataCache = null;
|
|
235
|
-
const cacheRefreshMillis = (
|
|
509
|
+
const cacheRefreshMillis = (_a8 = options.metadataCacheRefreshMillis) != null ? _a8 : 1e3 * 60 * 5;
|
|
236
510
|
let lastFetchTime = 0;
|
|
237
|
-
const baseURL = (
|
|
511
|
+
const baseURL = (_b8 = (0, import_provider_utils3.withoutTrailingSlash)(options.baseURL)) != null ? _b8 : "https://ai-gateway.vercel.sh/v1/ai";
|
|
238
512
|
const getHeaders = async () => {
|
|
239
513
|
return {
|
|
240
514
|
Authorization: `Bearer ${await getGatewayAuthToken(options)}`,
|
|
@@ -268,8 +542,8 @@ function createGatewayProvider(options = {}) {
|
|
|
268
542
|
});
|
|
269
543
|
};
|
|
270
544
|
const getAvailableModels = async () => {
|
|
271
|
-
var
|
|
272
|
-
const now = (_c = (
|
|
545
|
+
var _a9, _b9, _c;
|
|
546
|
+
const now = (_c = (_b9 = (_a9 = options._internal) == null ? void 0 : _a9.currentDate) == null ? void 0 : _b9.call(_a9).getTime()) != null ? _c : Date.now();
|
|
273
547
|
if (!pendingMetadata || now - lastFetchTime > cacheRefreshMillis) {
|
|
274
548
|
lastFetchTime = now;
|
|
275
549
|
pendingMetadata = new GatewayFetchMetadata({
|
|
@@ -279,6 +553,8 @@ function createGatewayProvider(options = {}) {
|
|
|
279
553
|
}).getAvailableModels().then((metadata) => {
|
|
280
554
|
metadataCache = metadata;
|
|
281
555
|
return metadata;
|
|
556
|
+
}).catch((error) => {
|
|
557
|
+
throw asGatewayError(error);
|
|
282
558
|
});
|
|
283
559
|
}
|
|
284
560
|
return metadataCache ? Promise.resolve(metadataCache) : pendingMetadata;
|
|
@@ -293,17 +569,24 @@ function createGatewayProvider(options = {}) {
|
|
|
293
569
|
};
|
|
294
570
|
provider.getAvailableModels = getAvailableModels;
|
|
295
571
|
provider.imageModel = (modelId) => {
|
|
296
|
-
throw new
|
|
572
|
+
throw new import_provider2.NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
297
573
|
};
|
|
298
574
|
provider.languageModel = createLanguageModel;
|
|
299
575
|
provider.textEmbeddingModel = (modelId) => {
|
|
300
|
-
throw new
|
|
576
|
+
throw new import_provider2.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
|
|
301
577
|
};
|
|
302
578
|
return provider;
|
|
303
579
|
}
|
|
304
580
|
var gateway = createGatewayProvider();
|
|
305
581
|
// Annotate the CommonJS export names for ESM import in node:
|
|
306
582
|
0 && (module.exports = {
|
|
583
|
+
GatewayAuthenticationError,
|
|
584
|
+
GatewayError,
|
|
585
|
+
GatewayInternalServerError,
|
|
586
|
+
GatewayInvalidRequestError,
|
|
587
|
+
GatewayModelNotFoundError,
|
|
588
|
+
GatewayRateLimitError,
|
|
589
|
+
GatewayResponseError,
|
|
307
590
|
createGatewayProvider,
|
|
308
591
|
gateway
|
|
309
592
|
});
|