@ai-sdk/gateway 4.0.0-beta.10 → 4.0.0-beta.108
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 +738 -4
- package/dist/index.d.ts +314 -42
- package/dist/index.js +1369 -397
- package/dist/index.js.map +1 -1
- package/docs/00-ai-gateway.mdx +447 -61
- package/package.json +14 -14
- package/src/errors/as-gateway-error.ts +2 -1
- package/src/errors/create-gateway-error.ts +17 -3
- package/src/errors/gateway-authentication-error.ts +8 -5
- package/src/errors/gateway-error.ts +8 -0
- package/src/errors/gateway-failed-dependency-error.ts +35 -0
- package/src/errors/gateway-forbidden-error.ts +34 -0
- package/src/errors/gateway-response-error.ts +1 -1
- package/src/errors/index.ts +2 -0
- package/src/errors/parse-auth-method.ts +1 -2
- package/src/gateway-config.ts +1 -1
- package/src/gateway-embedding-model-settings.ts +1 -0
- package/src/gateway-embedding-model.ts +62 -15
- package/src/gateway-fetch-metadata.ts +51 -38
- package/src/gateway-generation-info.ts +149 -0
- package/src/gateway-headers.ts +3 -0
- package/src/gateway-image-model-settings.ts +8 -1
- package/src/gateway-image-model.ts +46 -21
- package/src/gateway-language-model-settings.ts +44 -26
- package/src/gateway-language-model.ts +72 -42
- package/src/gateway-model-entry.ts +15 -3
- package/src/gateway-provider-options.ts +50 -78
- package/src/gateway-provider.ts +239 -35
- package/src/gateway-realtime-auth.ts +126 -0
- package/src/gateway-realtime-model-settings.ts +1 -0
- package/src/gateway-realtime-model.ts +107 -0
- package/src/gateway-reranking-model-settings.ts +7 -0
- package/src/gateway-reranking-model.ts +142 -0
- package/src/gateway-speech-model-settings.ts +1 -0
- package/src/gateway-speech-model.ts +145 -0
- package/src/gateway-spend-report.ts +193 -0
- package/src/gateway-transcription-model-settings.ts +1 -0
- package/src/gateway-transcription-model.ts +155 -0
- package/src/gateway-video-model-settings.ts +4 -0
- package/src/gateway-video-model.ts +29 -19
- package/src/index.ts +30 -5
- package/src/tool/parallel-search.ts +10 -11
- package/src/tool/perplexity-search.ts +10 -11
- package/dist/index.d.mts +0 -602
- package/dist/index.mjs +0 -1539
- package/dist/index.mjs.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,46 +1,69 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
1
|
+
// src/gateway-realtime-auth.ts
|
|
2
|
+
var GATEWAY_REALTIME_SUBPROTOCOL = "ai-gateway-realtime.v1";
|
|
3
|
+
var GATEWAY_AUTH_SUBPROTOCOL_PREFIX = "ai-gateway-auth.";
|
|
4
|
+
var GATEWAY_TEAM_SUBPROTOCOL_PREFIX = "ai-gateway-team.";
|
|
5
|
+
function getGatewayRealtimeProtocols(token, options) {
|
|
6
|
+
const protocols = [
|
|
7
|
+
GATEWAY_REALTIME_SUBPROTOCOL,
|
|
8
|
+
`${GATEWAY_AUTH_SUBPROTOCOL_PREFIX}${token}`
|
|
9
|
+
];
|
|
10
|
+
if (options == null ? void 0 : options.teamIdOrSlug) {
|
|
11
|
+
protocols.push(
|
|
12
|
+
`${GATEWAY_TEAM_SUBPROTOCOL_PREFIX}${encodeSubprotocolValue(options.teamIdOrSlug)}`
|
|
13
|
+
);
|
|
15
14
|
}
|
|
16
|
-
return
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
})
|
|
34
|
-
|
|
15
|
+
return protocols;
|
|
16
|
+
}
|
|
17
|
+
function getGatewayRealtimeAuthToken(secWebSocketProtocol) {
|
|
18
|
+
var _a11;
|
|
19
|
+
return ((_a11 = findProtocol(secWebSocketProtocol, GATEWAY_AUTH_SUBPROTOCOL_PREFIX)) == null ? void 0 : _a11.slice(
|
|
20
|
+
GATEWAY_AUTH_SUBPROTOCOL_PREFIX.length
|
|
21
|
+
)) || void 0;
|
|
22
|
+
}
|
|
23
|
+
function getGatewayRealtimeTeamIdOrSlug(secWebSocketProtocol) {
|
|
24
|
+
var _a11;
|
|
25
|
+
const encoded = (_a11 = findProtocol(
|
|
26
|
+
secWebSocketProtocol,
|
|
27
|
+
GATEWAY_TEAM_SUBPROTOCOL_PREFIX
|
|
28
|
+
)) == null ? void 0 : _a11.slice(GATEWAY_TEAM_SUBPROTOCOL_PREFIX.length);
|
|
29
|
+
if (!encoded) return void 0;
|
|
30
|
+
try {
|
|
31
|
+
return decodeSubprotocolValue(encoded) || void 0;
|
|
32
|
+
} catch (e) {
|
|
33
|
+
return void 0;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function findProtocol(secWebSocketProtocol, prefix) {
|
|
37
|
+
return secWebSocketProtocol == null ? void 0 : secWebSocketProtocol.split(",").map((protocol) => protocol.trim()).find((protocol) => protocol.startsWith(prefix));
|
|
38
|
+
}
|
|
39
|
+
function encodeSubprotocolValue(value) {
|
|
40
|
+
const bytes = new TextEncoder().encode(value);
|
|
41
|
+
let binary = "";
|
|
42
|
+
for (const byte of bytes) {
|
|
43
|
+
binary += String.fromCharCode(byte);
|
|
44
|
+
}
|
|
45
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/u, "");
|
|
46
|
+
}
|
|
47
|
+
function decodeSubprotocolValue(value) {
|
|
48
|
+
const base64 = value.replace(/-/g, "+").replace(/_/g, "/");
|
|
49
|
+
const padding = "=".repeat((4 - base64.length % 4) % 4);
|
|
50
|
+
const binary = atob(`${base64}${padding}`);
|
|
51
|
+
const bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0));
|
|
52
|
+
return new TextDecoder().decode(bytes);
|
|
53
|
+
}
|
|
35
54
|
|
|
36
55
|
// src/gateway-provider.ts
|
|
37
|
-
|
|
56
|
+
import {
|
|
57
|
+
loadOptionalSetting,
|
|
58
|
+
withoutTrailingSlash,
|
|
59
|
+
withUserAgentSuffix
|
|
60
|
+
} from "@ai-sdk/provider-utils";
|
|
38
61
|
|
|
39
62
|
// src/errors/as-gateway-error.ts
|
|
40
|
-
|
|
63
|
+
import { APICallError } from "@ai-sdk/provider";
|
|
41
64
|
|
|
42
65
|
// src/errors/create-gateway-error.ts
|
|
43
|
-
|
|
66
|
+
import { z as z2 } from "zod/v4";
|
|
44
67
|
|
|
45
68
|
// src/errors/gateway-error.ts
|
|
46
69
|
var marker = "vercel.ai.gateway.error";
|
|
@@ -51,13 +74,19 @@ var GatewayError = class _GatewayError extends (_b = Error, _a = symbol, _b) {
|
|
|
51
74
|
message,
|
|
52
75
|
statusCode = 500,
|
|
53
76
|
cause,
|
|
54
|
-
generationId
|
|
77
|
+
generationId,
|
|
78
|
+
isRetryable = statusCode != null && (statusCode === 408 || // request timeout
|
|
79
|
+
statusCode === 409 || // conflict
|
|
80
|
+
statusCode === 429 || // too many requests
|
|
81
|
+
statusCode >= 500)
|
|
82
|
+
// server error
|
|
55
83
|
}) {
|
|
56
84
|
super(generationId ? `${message} [${generationId}]` : message);
|
|
57
85
|
this[_a] = true;
|
|
58
86
|
this.statusCode = statusCode;
|
|
59
87
|
this.cause = cause;
|
|
60
88
|
this.generationId = generationId;
|
|
89
|
+
this.isRetryable = isRetryable;
|
|
61
90
|
}
|
|
62
91
|
/**
|
|
63
92
|
* Checks if the given error is a Gateway Error.
|
|
@@ -99,24 +128,24 @@ var GatewayAuthenticationError = class _GatewayAuthenticationError extends (_b2
|
|
|
99
128
|
static createContextualError({
|
|
100
129
|
apiKeyProvided,
|
|
101
130
|
oidcTokenProvided,
|
|
102
|
-
message = "Authentication failed",
|
|
103
131
|
statusCode = 401,
|
|
104
132
|
cause,
|
|
105
133
|
generationId
|
|
106
134
|
}) {
|
|
107
135
|
let contextualMessage;
|
|
108
136
|
if (apiKeyProvided) {
|
|
109
|
-
contextualMessage = `AI Gateway authentication failed: Invalid API key.
|
|
137
|
+
contextualMessage = `AI Gateway authentication failed: Invalid API key or token.
|
|
110
138
|
|
|
111
139
|
Create a new API key: https://vercel.com/d?to=%2F%5Bteam%5D%2F%7E%2Fai%2Fapi-keys
|
|
112
140
|
|
|
113
|
-
Provide via 'apiKey' option or 'AI_GATEWAY_API_KEY' environment variable.`;
|
|
141
|
+
Provide an API key or Vercel access token via 'apiKey' option or 'AI_GATEWAY_API_KEY' environment variable.`;
|
|
114
142
|
} else if (oidcTokenProvided) {
|
|
115
143
|
contextualMessage = `AI Gateway authentication failed: Invalid OIDC token.
|
|
116
144
|
|
|
117
145
|
Run 'npx vercel link' to link your project, then 'vc env pull' to fetch the token.
|
|
118
146
|
|
|
119
|
-
Alternatively, use an API key: https://vercel.com/d?to=%2F%5Bteam%5D%2F%7E%2Fai%2Fapi-keys
|
|
147
|
+
Alternatively, use an API key: https://vercel.com/d?to=%2F%5Bteam%5D%2F%7E%2Fai%2Fapi-keys
|
|
148
|
+
or pass a Vercel access token via the 'apiKey' option.`;
|
|
120
149
|
} else {
|
|
121
150
|
contextualMessage = `AI Gateway authentication failed: No authentication provided.
|
|
122
151
|
|
|
@@ -124,7 +153,10 @@ Option 1 - API key:
|
|
|
124
153
|
Create an API key: https://vercel.com/d?to=%2F%5Bteam%5D%2F%7E%2Fai%2Fapi-keys
|
|
125
154
|
Provide via 'apiKey' option or 'AI_GATEWAY_API_KEY' environment variable.
|
|
126
155
|
|
|
127
|
-
Option 2 -
|
|
156
|
+
Option 2 - Vercel access token:
|
|
157
|
+
Pass a Vercel personal access token or Vercel app access token via the 'apiKey' option.
|
|
158
|
+
|
|
159
|
+
Option 3 - OIDC token:
|
|
128
160
|
Run 'npx vercel link' to link your project, then 'vc env pull' to fetch the token.`;
|
|
129
161
|
}
|
|
130
162
|
return new _GatewayAuthenticationError({
|
|
@@ -183,15 +215,15 @@ var GatewayRateLimitError = class extends (_b4 = GatewayError, _a4 = symbol4, _b
|
|
|
183
215
|
};
|
|
184
216
|
|
|
185
217
|
// src/errors/gateway-model-not-found-error.ts
|
|
186
|
-
|
|
187
|
-
|
|
218
|
+
import { z } from "zod/v4";
|
|
219
|
+
import { lazySchema, zodSchema } from "@ai-sdk/provider-utils";
|
|
188
220
|
var name4 = "GatewayModelNotFoundError";
|
|
189
221
|
var marker5 = `vercel.ai.gateway.error.${name4}`;
|
|
190
222
|
var symbol5 = Symbol.for(marker5);
|
|
191
|
-
var modelNotFoundParamSchema =
|
|
192
|
-
() =>
|
|
193
|
-
|
|
194
|
-
modelId:
|
|
223
|
+
var modelNotFoundParamSchema = lazySchema(
|
|
224
|
+
() => zodSchema(
|
|
225
|
+
z.object({
|
|
226
|
+
modelId: z.string()
|
|
195
227
|
})
|
|
196
228
|
)
|
|
197
229
|
);
|
|
@@ -239,12 +271,58 @@ var GatewayInternalServerError = class extends (_b6 = GatewayError, _a6 = symbol
|
|
|
239
271
|
}
|
|
240
272
|
};
|
|
241
273
|
|
|
242
|
-
// src/errors/gateway-
|
|
243
|
-
var name6 = "
|
|
274
|
+
// src/errors/gateway-failed-dependency-error.ts
|
|
275
|
+
var name6 = "GatewayFailedDependencyError";
|
|
244
276
|
var marker7 = `vercel.ai.gateway.error.${name6}`;
|
|
245
277
|
var symbol7 = Symbol.for(marker7);
|
|
246
278
|
var _a7, _b7;
|
|
247
|
-
var
|
|
279
|
+
var GatewayFailedDependencyError = class extends (_b7 = GatewayError, _a7 = symbol7, _b7) {
|
|
280
|
+
constructor({
|
|
281
|
+
message = "Failed dependency",
|
|
282
|
+
statusCode = 424,
|
|
283
|
+
cause,
|
|
284
|
+
generationId
|
|
285
|
+
} = {}) {
|
|
286
|
+
super({ message, statusCode, cause, generationId });
|
|
287
|
+
this[_a7] = true;
|
|
288
|
+
// used in isInstance
|
|
289
|
+
this.name = name6;
|
|
290
|
+
this.type = "failed_dependency";
|
|
291
|
+
}
|
|
292
|
+
static isInstance(error) {
|
|
293
|
+
return GatewayError.hasMarker(error) && symbol7 in error;
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
// src/errors/gateway-forbidden-error.ts
|
|
298
|
+
var name7 = "GatewayForbiddenError";
|
|
299
|
+
var marker8 = `vercel.ai.gateway.error.${name7}`;
|
|
300
|
+
var symbol8 = Symbol.for(marker8);
|
|
301
|
+
var _a8, _b8;
|
|
302
|
+
var GatewayForbiddenError = class extends (_b8 = GatewayError, _a8 = symbol8, _b8) {
|
|
303
|
+
constructor({
|
|
304
|
+
message = "Forbidden",
|
|
305
|
+
statusCode = 403,
|
|
306
|
+
cause,
|
|
307
|
+
generationId
|
|
308
|
+
} = {}) {
|
|
309
|
+
super({ message, statusCode, cause, generationId });
|
|
310
|
+
this[_a8] = true;
|
|
311
|
+
// used in isInstance
|
|
312
|
+
this.name = name7;
|
|
313
|
+
this.type = "forbidden";
|
|
314
|
+
}
|
|
315
|
+
static isInstance(error) {
|
|
316
|
+
return GatewayError.hasMarker(error) && symbol8 in error;
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
// src/errors/gateway-response-error.ts
|
|
321
|
+
var name8 = "GatewayResponseError";
|
|
322
|
+
var marker9 = `vercel.ai.gateway.error.${name8}`;
|
|
323
|
+
var symbol9 = Symbol.for(marker9);
|
|
324
|
+
var _a9, _b9;
|
|
325
|
+
var GatewayResponseError = class extends (_b9 = GatewayError, _a9 = symbol9, _b9) {
|
|
248
326
|
constructor({
|
|
249
327
|
message = "Invalid response from Gateway",
|
|
250
328
|
statusCode = 502,
|
|
@@ -254,20 +332,24 @@ var GatewayResponseError = class extends (_b7 = GatewayError, _a7 = symbol7, _b7
|
|
|
254
332
|
generationId
|
|
255
333
|
} = {}) {
|
|
256
334
|
super({ message, statusCode, cause, generationId });
|
|
257
|
-
this[
|
|
335
|
+
this[_a9] = true;
|
|
258
336
|
// used in isInstance
|
|
259
|
-
this.name =
|
|
337
|
+
this.name = name8;
|
|
260
338
|
this.type = "response_error";
|
|
261
339
|
this.response = response;
|
|
262
340
|
this.validationError = validationError;
|
|
263
341
|
}
|
|
264
342
|
static isInstance(error) {
|
|
265
|
-
return GatewayError.hasMarker(error) &&
|
|
343
|
+
return GatewayError.hasMarker(error) && symbol9 in error;
|
|
266
344
|
}
|
|
267
345
|
};
|
|
268
346
|
|
|
269
347
|
// src/errors/create-gateway-error.ts
|
|
270
|
-
|
|
348
|
+
import {
|
|
349
|
+
lazySchema as lazySchema2,
|
|
350
|
+
safeValidateTypes,
|
|
351
|
+
zodSchema as zodSchema2
|
|
352
|
+
} from "@ai-sdk/provider-utils";
|
|
271
353
|
async function createGatewayErrorFromResponse({
|
|
272
354
|
response,
|
|
273
355
|
statusCode,
|
|
@@ -275,8 +357,8 @@ async function createGatewayErrorFromResponse({
|
|
|
275
357
|
cause,
|
|
276
358
|
authMethod
|
|
277
359
|
}) {
|
|
278
|
-
var
|
|
279
|
-
const parseResult = await
|
|
360
|
+
var _a11;
|
|
361
|
+
const parseResult = await safeValidateTypes({
|
|
280
362
|
value: response,
|
|
281
363
|
schema: gatewayErrorResponseSchema
|
|
282
364
|
});
|
|
@@ -294,7 +376,7 @@ async function createGatewayErrorFromResponse({
|
|
|
294
376
|
const validatedResponse = parseResult.value;
|
|
295
377
|
const errorType = validatedResponse.error.type;
|
|
296
378
|
const message = validatedResponse.error.message;
|
|
297
|
-
const generationId = (
|
|
379
|
+
const generationId = (_a11 = validatedResponse.generationId) != null ? _a11 : void 0;
|
|
298
380
|
switch (errorType) {
|
|
299
381
|
case "authentication_error":
|
|
300
382
|
return GatewayAuthenticationError.createContextualError({
|
|
@@ -319,7 +401,7 @@ async function createGatewayErrorFromResponse({
|
|
|
319
401
|
generationId
|
|
320
402
|
});
|
|
321
403
|
case "model_not_found": {
|
|
322
|
-
const modelResult = await
|
|
404
|
+
const modelResult = await safeValidateTypes({
|
|
323
405
|
value: validatedResponse.error.param,
|
|
324
406
|
schema: modelNotFoundParamSchema
|
|
325
407
|
});
|
|
@@ -338,6 +420,20 @@ async function createGatewayErrorFromResponse({
|
|
|
338
420
|
cause,
|
|
339
421
|
generationId
|
|
340
422
|
});
|
|
423
|
+
case "failed_dependency":
|
|
424
|
+
return new GatewayFailedDependencyError({
|
|
425
|
+
message,
|
|
426
|
+
statusCode,
|
|
427
|
+
cause,
|
|
428
|
+
generationId
|
|
429
|
+
});
|
|
430
|
+
case "forbidden":
|
|
431
|
+
return new GatewayForbiddenError({
|
|
432
|
+
message,
|
|
433
|
+
statusCode,
|
|
434
|
+
cause,
|
|
435
|
+
generationId
|
|
436
|
+
});
|
|
341
437
|
default:
|
|
342
438
|
return new GatewayInternalServerError({
|
|
343
439
|
message,
|
|
@@ -347,26 +443,41 @@ async function createGatewayErrorFromResponse({
|
|
|
347
443
|
});
|
|
348
444
|
}
|
|
349
445
|
}
|
|
350
|
-
var gatewayErrorResponseSchema = (
|
|
351
|
-
() => (
|
|
352
|
-
|
|
353
|
-
error:
|
|
354
|
-
message:
|
|
355
|
-
type:
|
|
356
|
-
param:
|
|
357
|
-
code:
|
|
446
|
+
var gatewayErrorResponseSchema = lazySchema2(
|
|
447
|
+
() => zodSchema2(
|
|
448
|
+
z2.object({
|
|
449
|
+
error: z2.object({
|
|
450
|
+
message: z2.string(),
|
|
451
|
+
type: z2.string().nullish(),
|
|
452
|
+
param: z2.unknown().nullish(),
|
|
453
|
+
code: z2.union([z2.string(), z2.number()]).nullish()
|
|
358
454
|
}),
|
|
359
|
-
generationId:
|
|
455
|
+
generationId: z2.string().nullish()
|
|
360
456
|
})
|
|
361
457
|
)
|
|
362
458
|
);
|
|
363
459
|
|
|
460
|
+
// src/errors/extract-api-call-response.ts
|
|
461
|
+
function extractApiCallResponse(error) {
|
|
462
|
+
if (error.data !== void 0) {
|
|
463
|
+
return error.data;
|
|
464
|
+
}
|
|
465
|
+
if (error.responseBody != null) {
|
|
466
|
+
try {
|
|
467
|
+
return JSON.parse(error.responseBody);
|
|
468
|
+
} catch (e) {
|
|
469
|
+
return error.responseBody;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return {};
|
|
473
|
+
}
|
|
474
|
+
|
|
364
475
|
// src/errors/gateway-timeout-error.ts
|
|
365
|
-
var
|
|
366
|
-
var
|
|
367
|
-
var
|
|
368
|
-
var
|
|
369
|
-
var GatewayTimeoutError = class _GatewayTimeoutError extends (
|
|
476
|
+
var name9 = "GatewayTimeoutError";
|
|
477
|
+
var marker10 = `vercel.ai.gateway.error.${name9}`;
|
|
478
|
+
var symbol10 = Symbol.for(marker10);
|
|
479
|
+
var _a10, _b10;
|
|
480
|
+
var GatewayTimeoutError = class _GatewayTimeoutError extends (_b10 = GatewayError, _a10 = symbol10, _b10) {
|
|
370
481
|
constructor({
|
|
371
482
|
message = "Request timed out",
|
|
372
483
|
statusCode = 408,
|
|
@@ -374,13 +485,13 @@ var GatewayTimeoutError = class _GatewayTimeoutError extends (_b8 = GatewayError
|
|
|
374
485
|
generationId
|
|
375
486
|
} = {}) {
|
|
376
487
|
super({ message, statusCode, cause, generationId });
|
|
377
|
-
this[
|
|
488
|
+
this[_a10] = true;
|
|
378
489
|
// used in isInstance
|
|
379
|
-
this.name =
|
|
490
|
+
this.name = name9;
|
|
380
491
|
this.type = "timeout_error";
|
|
381
492
|
}
|
|
382
493
|
static isInstance(error) {
|
|
383
|
-
return GatewayError.hasMarker(error) &&
|
|
494
|
+
return GatewayError.hasMarker(error) && symbol10 in error;
|
|
384
495
|
}
|
|
385
496
|
/**
|
|
386
497
|
* Creates a helpful timeout error message with troubleshooting guidance
|
|
@@ -420,7 +531,7 @@ function isTimeoutError(error) {
|
|
|
420
531
|
return false;
|
|
421
532
|
}
|
|
422
533
|
async function asGatewayError(error, authMethod) {
|
|
423
|
-
var
|
|
534
|
+
var _a11;
|
|
424
535
|
if (GatewayError.isInstance(error)) {
|
|
425
536
|
return error;
|
|
426
537
|
}
|
|
@@ -430,7 +541,7 @@ async function asGatewayError(error, authMethod) {
|
|
|
430
541
|
cause: error
|
|
431
542
|
});
|
|
432
543
|
}
|
|
433
|
-
if (
|
|
544
|
+
if (APICallError.isInstance(error)) {
|
|
434
545
|
if (error.cause && isTimeoutError(error.cause)) {
|
|
435
546
|
return GatewayTimeoutError.createTimeoutError({
|
|
436
547
|
originalMessage: error.message,
|
|
@@ -439,7 +550,7 @@ async function asGatewayError(error, authMethod) {
|
|
|
439
550
|
}
|
|
440
551
|
return await createGatewayErrorFromResponse({
|
|
441
552
|
response: extractApiCallResponse(error),
|
|
442
|
-
statusCode: (
|
|
553
|
+
statusCode: (_a11 = error.statusCode) != null ? _a11 : 500,
|
|
443
554
|
defaultMessage: "Gateway request failed",
|
|
444
555
|
cause: error,
|
|
445
556
|
authMethod
|
|
@@ -454,53 +565,65 @@ async function asGatewayError(error, authMethod) {
|
|
|
454
565
|
});
|
|
455
566
|
}
|
|
456
567
|
|
|
457
|
-
// src/
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
return error.data;
|
|
461
|
-
}
|
|
462
|
-
if (error.responseBody != null) {
|
|
463
|
-
try {
|
|
464
|
-
return JSON.parse(error.responseBody);
|
|
465
|
-
} catch (e) {
|
|
466
|
-
return error.responseBody;
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
return {};
|
|
470
|
-
}
|
|
568
|
+
// src/gateway-headers.ts
|
|
569
|
+
var GATEWAY_AUTH_METHOD_HEADER = "ai-gateway-auth-method";
|
|
570
|
+
var VERCEL_AI_GATEWAY_TEAM_HEADER = "x-vercel-ai-gateway-team";
|
|
471
571
|
|
|
472
572
|
// src/errors/parse-auth-method.ts
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
573
|
+
import { z as z3 } from "zod/v4";
|
|
574
|
+
import {
|
|
575
|
+
lazySchema as lazySchema3,
|
|
576
|
+
safeValidateTypes as safeValidateTypes2,
|
|
577
|
+
zodSchema as zodSchema3
|
|
578
|
+
} from "@ai-sdk/provider-utils";
|
|
476
579
|
async function parseAuthMethod(headers) {
|
|
477
|
-
const result = await (
|
|
580
|
+
const result = await safeValidateTypes2({
|
|
478
581
|
value: headers[GATEWAY_AUTH_METHOD_HEADER],
|
|
479
582
|
schema: gatewayAuthMethodSchema
|
|
480
583
|
});
|
|
481
584
|
return result.success ? result.value : void 0;
|
|
482
585
|
}
|
|
483
|
-
var gatewayAuthMethodSchema = (
|
|
484
|
-
() => (
|
|
586
|
+
var gatewayAuthMethodSchema = lazySchema3(
|
|
587
|
+
() => zodSchema3(z3.union([z3.literal("api-key"), z3.literal("oidc")]))
|
|
485
588
|
);
|
|
486
589
|
|
|
487
590
|
// src/gateway-fetch-metadata.ts
|
|
488
|
-
|
|
489
|
-
|
|
591
|
+
import {
|
|
592
|
+
createJsonErrorResponseHandler,
|
|
593
|
+
createJsonResponseHandler,
|
|
594
|
+
getFromApi,
|
|
595
|
+
lazySchema as lazySchema4,
|
|
596
|
+
resolve,
|
|
597
|
+
zodSchema as zodSchema4
|
|
598
|
+
} from "@ai-sdk/provider-utils";
|
|
599
|
+
import { z as z4 } from "zod/v4";
|
|
600
|
+
|
|
601
|
+
// src/gateway-model-entry.ts
|
|
602
|
+
var KNOWN_MODEL_TYPES = [
|
|
603
|
+
"embedding",
|
|
604
|
+
"image",
|
|
605
|
+
"language",
|
|
606
|
+
"reranking",
|
|
607
|
+
"speech",
|
|
608
|
+
"transcription",
|
|
609
|
+
"video"
|
|
610
|
+
];
|
|
611
|
+
|
|
612
|
+
// src/gateway-fetch-metadata.ts
|
|
490
613
|
var GatewayFetchMetadata = class {
|
|
491
614
|
constructor(config) {
|
|
492
615
|
this.config = config;
|
|
493
616
|
}
|
|
494
617
|
async getAvailableModels() {
|
|
495
618
|
try {
|
|
496
|
-
const { value } = await
|
|
619
|
+
const { value } = await getFromApi({
|
|
497
620
|
url: `${this.config.baseURL}/config`,
|
|
498
|
-
headers: await
|
|
499
|
-
successfulResponseHandler:
|
|
621
|
+
headers: this.config.headers ? await resolve(this.config.headers) : void 0,
|
|
622
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
500
623
|
gatewayAvailableModelsResponseSchema
|
|
501
624
|
),
|
|
502
|
-
failedResponseHandler:
|
|
503
|
-
errorSchema:
|
|
625
|
+
failedResponseHandler: createJsonErrorResponseHandler({
|
|
626
|
+
errorSchema: z4.any(),
|
|
504
627
|
errorToMessage: (data) => data
|
|
505
628
|
}),
|
|
506
629
|
fetch: this.config.fetch
|
|
@@ -513,14 +636,14 @@ var GatewayFetchMetadata = class {
|
|
|
513
636
|
async getCredits() {
|
|
514
637
|
try {
|
|
515
638
|
const baseUrl = new URL(this.config.baseURL);
|
|
516
|
-
const { value } = await
|
|
639
|
+
const { value } = await getFromApi({
|
|
517
640
|
url: `${baseUrl.origin}/v1/credits`,
|
|
518
|
-
headers: await
|
|
519
|
-
successfulResponseHandler:
|
|
641
|
+
headers: this.config.headers ? await resolve(this.config.headers) : void 0,
|
|
642
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
520
643
|
gatewayCreditsResponseSchema
|
|
521
644
|
),
|
|
522
|
-
failedResponseHandler:
|
|
523
|
-
errorSchema:
|
|
645
|
+
failedResponseHandler: createJsonErrorResponseHandler({
|
|
646
|
+
errorSchema: z4.any(),
|
|
524
647
|
errorToMessage: (data) => data
|
|
525
648
|
}),
|
|
526
649
|
fetch: this.config.fetch
|
|
@@ -531,19 +654,19 @@ var GatewayFetchMetadata = class {
|
|
|
531
654
|
}
|
|
532
655
|
}
|
|
533
656
|
};
|
|
534
|
-
var gatewayAvailableModelsResponseSchema = (
|
|
535
|
-
() => (
|
|
536
|
-
|
|
537
|
-
models:
|
|
538
|
-
|
|
539
|
-
id:
|
|
540
|
-
name:
|
|
541
|
-
description:
|
|
542
|
-
pricing:
|
|
543
|
-
input:
|
|
544
|
-
output:
|
|
545
|
-
input_cache_read:
|
|
546
|
-
input_cache_write:
|
|
657
|
+
var gatewayAvailableModelsResponseSchema = lazySchema4(
|
|
658
|
+
() => zodSchema4(
|
|
659
|
+
z4.object({
|
|
660
|
+
models: z4.array(
|
|
661
|
+
z4.object({
|
|
662
|
+
id: z4.string(),
|
|
663
|
+
name: z4.string(),
|
|
664
|
+
description: z4.string().nullish(),
|
|
665
|
+
pricing: z4.object({
|
|
666
|
+
input: z4.string(),
|
|
667
|
+
output: z4.string(),
|
|
668
|
+
input_cache_read: z4.string().nullish(),
|
|
669
|
+
input_cache_write: z4.string().nullish()
|
|
547
670
|
}).transform(
|
|
548
671
|
({ input, output, input_cache_read, input_cache_write }) => ({
|
|
549
672
|
input,
|
|
@@ -552,22 +675,26 @@ var gatewayAvailableModelsResponseSchema = (0, import_provider_utils4.lazySchema
|
|
|
552
675
|
...input_cache_write ? { cacheCreationInputTokens: input_cache_write } : {}
|
|
553
676
|
})
|
|
554
677
|
).nullish(),
|
|
555
|
-
specification:
|
|
556
|
-
specificationVersion:
|
|
557
|
-
provider:
|
|
558
|
-
modelId:
|
|
678
|
+
specification: z4.object({
|
|
679
|
+
specificationVersion: z4.literal("v4"),
|
|
680
|
+
provider: z4.string(),
|
|
681
|
+
modelId: z4.string()
|
|
559
682
|
}),
|
|
560
|
-
modelType:
|
|
683
|
+
modelType: z4.string().nullish()
|
|
561
684
|
})
|
|
685
|
+
).transform(
|
|
686
|
+
(models) => models.filter(
|
|
687
|
+
(m) => m.modelType == null || KNOWN_MODEL_TYPES.includes(m.modelType)
|
|
688
|
+
)
|
|
562
689
|
)
|
|
563
690
|
})
|
|
564
691
|
)
|
|
565
692
|
);
|
|
566
|
-
var gatewayCreditsResponseSchema = (
|
|
567
|
-
() => (
|
|
568
|
-
|
|
569
|
-
balance:
|
|
570
|
-
total_used:
|
|
693
|
+
var gatewayCreditsResponseSchema = lazySchema4(
|
|
694
|
+
() => zodSchema4(
|
|
695
|
+
z4.object({
|
|
696
|
+
balance: z4.string(),
|
|
697
|
+
total_used: z4.string()
|
|
571
698
|
}).transform(({ balance, total_used }) => ({
|
|
572
699
|
balance,
|
|
573
700
|
totalUsed: total_used
|
|
@@ -575,16 +702,238 @@ var gatewayCreditsResponseSchema = (0, import_provider_utils4.lazySchema)(
|
|
|
575
702
|
)
|
|
576
703
|
);
|
|
577
704
|
|
|
705
|
+
// src/gateway-spend-report.ts
|
|
706
|
+
import {
|
|
707
|
+
createJsonErrorResponseHandler as createJsonErrorResponseHandler2,
|
|
708
|
+
createJsonResponseHandler as createJsonResponseHandler2,
|
|
709
|
+
getFromApi as getFromApi2,
|
|
710
|
+
lazySchema as lazySchema5,
|
|
711
|
+
resolve as resolve2,
|
|
712
|
+
zodSchema as zodSchema5
|
|
713
|
+
} from "@ai-sdk/provider-utils";
|
|
714
|
+
import { z as z5 } from "zod/v4";
|
|
715
|
+
var GatewaySpendReport = class {
|
|
716
|
+
constructor(config) {
|
|
717
|
+
this.config = config;
|
|
718
|
+
}
|
|
719
|
+
async getSpendReport(params) {
|
|
720
|
+
try {
|
|
721
|
+
const baseUrl = new URL(this.config.baseURL);
|
|
722
|
+
const searchParams = new URLSearchParams();
|
|
723
|
+
searchParams.set("start_date", params.startDate);
|
|
724
|
+
searchParams.set("end_date", params.endDate);
|
|
725
|
+
if (params.groupBy) {
|
|
726
|
+
searchParams.set("group_by", params.groupBy);
|
|
727
|
+
}
|
|
728
|
+
if (params.datePart) {
|
|
729
|
+
searchParams.set("date_part", params.datePart);
|
|
730
|
+
}
|
|
731
|
+
if (params.userId) {
|
|
732
|
+
searchParams.set("user_id", params.userId);
|
|
733
|
+
}
|
|
734
|
+
if (params.model) {
|
|
735
|
+
searchParams.set("model", params.model);
|
|
736
|
+
}
|
|
737
|
+
if (params.provider) {
|
|
738
|
+
searchParams.set("provider", params.provider);
|
|
739
|
+
}
|
|
740
|
+
if (params.credentialType) {
|
|
741
|
+
searchParams.set("credential_type", params.credentialType);
|
|
742
|
+
}
|
|
743
|
+
if (params.tags && params.tags.length > 0) {
|
|
744
|
+
searchParams.set("tags", params.tags.join(","));
|
|
745
|
+
}
|
|
746
|
+
const { value } = await getFromApi2({
|
|
747
|
+
url: `${baseUrl.origin}/v1/report?${searchParams.toString()}`,
|
|
748
|
+
headers: this.config.headers ? await resolve2(this.config.headers) : void 0,
|
|
749
|
+
successfulResponseHandler: createJsonResponseHandler2(
|
|
750
|
+
gatewaySpendReportResponseSchema
|
|
751
|
+
),
|
|
752
|
+
failedResponseHandler: createJsonErrorResponseHandler2({
|
|
753
|
+
errorSchema: z5.any(),
|
|
754
|
+
errorToMessage: (data) => data
|
|
755
|
+
}),
|
|
756
|
+
fetch: this.config.fetch
|
|
757
|
+
});
|
|
758
|
+
return value;
|
|
759
|
+
} catch (error) {
|
|
760
|
+
throw await asGatewayError(error);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
};
|
|
764
|
+
var gatewaySpendReportResponseSchema = lazySchema5(
|
|
765
|
+
() => zodSchema5(
|
|
766
|
+
z5.object({
|
|
767
|
+
results: z5.array(
|
|
768
|
+
z5.object({
|
|
769
|
+
day: z5.string().optional(),
|
|
770
|
+
hour: z5.string().optional(),
|
|
771
|
+
user: z5.string().optional(),
|
|
772
|
+
model: z5.string().optional(),
|
|
773
|
+
tag: z5.string().optional(),
|
|
774
|
+
provider: z5.string().optional(),
|
|
775
|
+
credential_type: z5.enum(["byok", "system"]).optional(),
|
|
776
|
+
total_cost: z5.number(),
|
|
777
|
+
market_cost: z5.number().optional(),
|
|
778
|
+
input_tokens: z5.number().optional(),
|
|
779
|
+
output_tokens: z5.number().optional(),
|
|
780
|
+
cached_input_tokens: z5.number().optional(),
|
|
781
|
+
cache_creation_input_tokens: z5.number().optional(),
|
|
782
|
+
reasoning_tokens: z5.number().optional(),
|
|
783
|
+
request_count: z5.number().optional()
|
|
784
|
+
}).transform(
|
|
785
|
+
({
|
|
786
|
+
credential_type,
|
|
787
|
+
total_cost,
|
|
788
|
+
market_cost,
|
|
789
|
+
input_tokens,
|
|
790
|
+
output_tokens,
|
|
791
|
+
cached_input_tokens,
|
|
792
|
+
cache_creation_input_tokens,
|
|
793
|
+
reasoning_tokens,
|
|
794
|
+
request_count,
|
|
795
|
+
...rest
|
|
796
|
+
}) => ({
|
|
797
|
+
...rest,
|
|
798
|
+
...credential_type !== void 0 ? { credentialType: credential_type } : {},
|
|
799
|
+
totalCost: total_cost,
|
|
800
|
+
...market_cost !== void 0 ? { marketCost: market_cost } : {},
|
|
801
|
+
...input_tokens !== void 0 ? { inputTokens: input_tokens } : {},
|
|
802
|
+
...output_tokens !== void 0 ? { outputTokens: output_tokens } : {},
|
|
803
|
+
...cached_input_tokens !== void 0 ? { cachedInputTokens: cached_input_tokens } : {},
|
|
804
|
+
...cache_creation_input_tokens !== void 0 ? { cacheCreationInputTokens: cache_creation_input_tokens } : {},
|
|
805
|
+
...reasoning_tokens !== void 0 ? { reasoningTokens: reasoning_tokens } : {},
|
|
806
|
+
...request_count !== void 0 ? { requestCount: request_count } : {}
|
|
807
|
+
})
|
|
808
|
+
)
|
|
809
|
+
)
|
|
810
|
+
})
|
|
811
|
+
)
|
|
812
|
+
);
|
|
813
|
+
|
|
814
|
+
// src/gateway-generation-info.ts
|
|
815
|
+
import {
|
|
816
|
+
createJsonErrorResponseHandler as createJsonErrorResponseHandler3,
|
|
817
|
+
createJsonResponseHandler as createJsonResponseHandler3,
|
|
818
|
+
getFromApi as getFromApi3,
|
|
819
|
+
lazySchema as lazySchema6,
|
|
820
|
+
resolve as resolve3,
|
|
821
|
+
zodSchema as zodSchema6
|
|
822
|
+
} from "@ai-sdk/provider-utils";
|
|
823
|
+
import { z as z6 } from "zod/v4";
|
|
824
|
+
var GatewayGenerationInfoFetcher = class {
|
|
825
|
+
constructor(config) {
|
|
826
|
+
this.config = config;
|
|
827
|
+
}
|
|
828
|
+
async getGenerationInfo(params) {
|
|
829
|
+
try {
|
|
830
|
+
const baseUrl = new URL(this.config.baseURL);
|
|
831
|
+
const { value } = await getFromApi3({
|
|
832
|
+
url: `${baseUrl.origin}/v1/generation?id=${encodeURIComponent(params.id)}`,
|
|
833
|
+
headers: this.config.headers ? await resolve3(this.config.headers) : void 0,
|
|
834
|
+
successfulResponseHandler: createJsonResponseHandler3(
|
|
835
|
+
gatewayGenerationInfoResponseSchema
|
|
836
|
+
),
|
|
837
|
+
failedResponseHandler: createJsonErrorResponseHandler3({
|
|
838
|
+
errorSchema: z6.any(),
|
|
839
|
+
errorToMessage: (data) => data
|
|
840
|
+
}),
|
|
841
|
+
fetch: this.config.fetch
|
|
842
|
+
});
|
|
843
|
+
return value;
|
|
844
|
+
} catch (error) {
|
|
845
|
+
throw await asGatewayError(error);
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
};
|
|
849
|
+
var gatewayGenerationInfoResponseSchema = lazySchema6(
|
|
850
|
+
() => zodSchema6(
|
|
851
|
+
z6.object({
|
|
852
|
+
data: z6.object({
|
|
853
|
+
id: z6.string(),
|
|
854
|
+
total_cost: z6.number(),
|
|
855
|
+
upstream_inference_cost: z6.number(),
|
|
856
|
+
usage: z6.number(),
|
|
857
|
+
created_at: z6.string(),
|
|
858
|
+
model: z6.string(),
|
|
859
|
+
is_byok: z6.boolean(),
|
|
860
|
+
provider_name: z6.string(),
|
|
861
|
+
streamed: z6.boolean(),
|
|
862
|
+
finish_reason: z6.string(),
|
|
863
|
+
latency: z6.number(),
|
|
864
|
+
generation_time: z6.number(),
|
|
865
|
+
native_tokens_prompt: z6.number(),
|
|
866
|
+
native_tokens_completion: z6.number(),
|
|
867
|
+
native_tokens_reasoning: z6.number(),
|
|
868
|
+
native_tokens_cached: z6.number(),
|
|
869
|
+
native_tokens_cache_creation: z6.number(),
|
|
870
|
+
billable_web_search_calls: z6.number()
|
|
871
|
+
}).transform(
|
|
872
|
+
({
|
|
873
|
+
total_cost,
|
|
874
|
+
upstream_inference_cost,
|
|
875
|
+
created_at,
|
|
876
|
+
is_byok,
|
|
877
|
+
provider_name,
|
|
878
|
+
finish_reason,
|
|
879
|
+
generation_time,
|
|
880
|
+
native_tokens_prompt,
|
|
881
|
+
native_tokens_completion,
|
|
882
|
+
native_tokens_reasoning,
|
|
883
|
+
native_tokens_cached,
|
|
884
|
+
native_tokens_cache_creation,
|
|
885
|
+
billable_web_search_calls,
|
|
886
|
+
...rest
|
|
887
|
+
}) => ({
|
|
888
|
+
...rest,
|
|
889
|
+
totalCost: total_cost,
|
|
890
|
+
upstreamInferenceCost: upstream_inference_cost,
|
|
891
|
+
createdAt: created_at,
|
|
892
|
+
isByok: is_byok,
|
|
893
|
+
providerName: provider_name,
|
|
894
|
+
finishReason: finish_reason,
|
|
895
|
+
generationTime: generation_time,
|
|
896
|
+
promptTokens: native_tokens_prompt,
|
|
897
|
+
completionTokens: native_tokens_completion,
|
|
898
|
+
reasoningTokens: native_tokens_reasoning,
|
|
899
|
+
cachedTokens: native_tokens_cached,
|
|
900
|
+
cacheCreationTokens: native_tokens_cache_creation,
|
|
901
|
+
billableWebSearchCalls: billable_web_search_calls
|
|
902
|
+
})
|
|
903
|
+
)
|
|
904
|
+
}).transform(({ data }) => data)
|
|
905
|
+
)
|
|
906
|
+
);
|
|
907
|
+
|
|
578
908
|
// src/gateway-language-model.ts
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
909
|
+
import {
|
|
910
|
+
combineHeaders,
|
|
911
|
+
createEventSourceResponseHandler,
|
|
912
|
+
createJsonErrorResponseHandler as createJsonErrorResponseHandler4,
|
|
913
|
+
createJsonResponseHandler as createJsonResponseHandler4,
|
|
914
|
+
postJsonToApi,
|
|
915
|
+
resolve as resolve4,
|
|
916
|
+
serializeModelOptions,
|
|
917
|
+
WORKFLOW_SERIALIZE,
|
|
918
|
+
WORKFLOW_DESERIALIZE
|
|
919
|
+
} from "@ai-sdk/provider-utils";
|
|
920
|
+
import { z as z7 } from "zod/v4";
|
|
921
|
+
var GatewayLanguageModel = class _GatewayLanguageModel {
|
|
582
922
|
constructor(modelId, config) {
|
|
583
923
|
this.modelId = modelId;
|
|
584
924
|
this.config = config;
|
|
585
|
-
this.specificationVersion = "
|
|
925
|
+
this.specificationVersion = "v4";
|
|
586
926
|
this.supportedUrls = { "*/*": [/.*/] };
|
|
587
927
|
}
|
|
928
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
929
|
+
return serializeModelOptions({
|
|
930
|
+
modelId: model.modelId,
|
|
931
|
+
config: model.config
|
|
932
|
+
});
|
|
933
|
+
}
|
|
934
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
935
|
+
return new _GatewayLanguageModel(options.modelId, options.config);
|
|
936
|
+
}
|
|
588
937
|
get provider() {
|
|
589
938
|
return this.config.provider;
|
|
590
939
|
}
|
|
@@ -598,24 +947,24 @@ var GatewayLanguageModel = class {
|
|
|
598
947
|
async doGenerate(options) {
|
|
599
948
|
const { args, warnings } = await this.getArgs(options);
|
|
600
949
|
const { abortSignal } = options;
|
|
601
|
-
const resolvedHeaders = await (
|
|
950
|
+
const resolvedHeaders = this.config.headers ? await resolve4(this.config.headers) : void 0;
|
|
602
951
|
try {
|
|
603
952
|
const {
|
|
604
953
|
responseHeaders,
|
|
605
954
|
value: responseBody,
|
|
606
955
|
rawValue: rawResponse
|
|
607
|
-
} = await
|
|
956
|
+
} = await postJsonToApi({
|
|
608
957
|
url: this.getUrl(),
|
|
609
|
-
headers:
|
|
958
|
+
headers: combineHeaders(
|
|
610
959
|
resolvedHeaders,
|
|
611
960
|
options.headers,
|
|
612
961
|
this.getModelConfigHeaders(this.modelId, false),
|
|
613
|
-
await (
|
|
962
|
+
await resolve4(this.config.o11yHeaders)
|
|
614
963
|
),
|
|
615
964
|
body: args,
|
|
616
|
-
successfulResponseHandler: (
|
|
617
|
-
failedResponseHandler: (
|
|
618
|
-
errorSchema:
|
|
965
|
+
successfulResponseHandler: createJsonResponseHandler4(z7.any()),
|
|
966
|
+
failedResponseHandler: createJsonErrorResponseHandler4({
|
|
967
|
+
errorSchema: z7.any(),
|
|
619
968
|
errorToMessage: (data) => data
|
|
620
969
|
}),
|
|
621
970
|
...abortSignal && { abortSignal },
|
|
@@ -628,26 +977,29 @@ var GatewayLanguageModel = class {
|
|
|
628
977
|
warnings
|
|
629
978
|
};
|
|
630
979
|
} catch (error) {
|
|
631
|
-
throw await asGatewayError(
|
|
980
|
+
throw await asGatewayError(
|
|
981
|
+
error,
|
|
982
|
+
await parseAuthMethod(resolvedHeaders != null ? resolvedHeaders : {})
|
|
983
|
+
);
|
|
632
984
|
}
|
|
633
985
|
}
|
|
634
986
|
async doStream(options) {
|
|
635
987
|
const { args, warnings } = await this.getArgs(options);
|
|
636
988
|
const { abortSignal } = options;
|
|
637
|
-
const resolvedHeaders = await (
|
|
989
|
+
const resolvedHeaders = this.config.headers ? await resolve4(this.config.headers) : void 0;
|
|
638
990
|
try {
|
|
639
|
-
const { value: response, responseHeaders } = await
|
|
991
|
+
const { value: response, responseHeaders } = await postJsonToApi({
|
|
640
992
|
url: this.getUrl(),
|
|
641
|
-
headers:
|
|
993
|
+
headers: combineHeaders(
|
|
642
994
|
resolvedHeaders,
|
|
643
995
|
options.headers,
|
|
644
996
|
this.getModelConfigHeaders(this.modelId, true),
|
|
645
|
-
await (
|
|
997
|
+
await resolve4(this.config.o11yHeaders)
|
|
646
998
|
),
|
|
647
999
|
body: args,
|
|
648
|
-
successfulResponseHandler:
|
|
649
|
-
failedResponseHandler: (
|
|
650
|
-
errorSchema:
|
|
1000
|
+
successfulResponseHandler: createEventSourceResponseHandler(z7.any()),
|
|
1001
|
+
failedResponseHandler: createJsonErrorResponseHandler4({
|
|
1002
|
+
errorSchema: z7.any(),
|
|
651
1003
|
errorToMessage: (data) => data
|
|
652
1004
|
}),
|
|
653
1005
|
...abortSignal && { abortSignal },
|
|
@@ -683,29 +1035,30 @@ var GatewayLanguageModel = class {
|
|
|
683
1035
|
response: { headers: responseHeaders }
|
|
684
1036
|
};
|
|
685
1037
|
} catch (error) {
|
|
686
|
-
throw await asGatewayError(
|
|
1038
|
+
throw await asGatewayError(
|
|
1039
|
+
error,
|
|
1040
|
+
await parseAuthMethod(resolvedHeaders != null ? resolvedHeaders : {})
|
|
1041
|
+
);
|
|
687
1042
|
}
|
|
688
1043
|
}
|
|
689
|
-
isFilePart(part) {
|
|
690
|
-
return part && typeof part === "object" && "type" in part && part.type === "file";
|
|
691
|
-
}
|
|
692
1044
|
/**
|
|
693
|
-
* Encodes
|
|
694
|
-
* instance directly to avoid copying the file data.
|
|
1045
|
+
* Encodes inline `Uint8Array` file data to a base64 string in place.
|
|
695
1046
|
* @param options - The options to encode.
|
|
696
|
-
* @returns The options with the file
|
|
1047
|
+
* @returns The options with the file data encoded.
|
|
697
1048
|
*/
|
|
698
1049
|
maybeEncodeFileParts(options) {
|
|
699
1050
|
for (const message of options.prompt) {
|
|
1051
|
+
if (!Array.isArray(message.content)) {
|
|
1052
|
+
continue;
|
|
1053
|
+
}
|
|
700
1054
|
for (const part of message.content) {
|
|
701
|
-
if (
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
);
|
|
1055
|
+
if (part.type === "file" || part.type === "reasoning-file") {
|
|
1056
|
+
part.data = maybeBase64EncodeFileData(part.data);
|
|
1057
|
+
} else if (part.type === "tool-result" && part.output.type === "content") {
|
|
1058
|
+
for (const contentPart of part.output.value) {
|
|
1059
|
+
if (contentPart.type === "file") {
|
|
1060
|
+
contentPart.data = maybeBase64EncodeFileData(contentPart.data);
|
|
1061
|
+
}
|
|
709
1062
|
}
|
|
710
1063
|
}
|
|
711
1064
|
}
|
|
@@ -717,24 +1070,53 @@ var GatewayLanguageModel = class {
|
|
|
717
1070
|
}
|
|
718
1071
|
getModelConfigHeaders(modelId, streaming) {
|
|
719
1072
|
return {
|
|
720
|
-
"ai-language-model-specification-version": "
|
|
1073
|
+
"ai-language-model-specification-version": "4",
|
|
721
1074
|
"ai-language-model-id": modelId,
|
|
722
1075
|
"ai-language-model-streaming": String(streaming)
|
|
723
1076
|
};
|
|
724
1077
|
}
|
|
725
1078
|
};
|
|
1079
|
+
function maybeBase64EncodeFileData(data) {
|
|
1080
|
+
if (data.type === "data") {
|
|
1081
|
+
const bytes = data.data;
|
|
1082
|
+
if (bytes instanceof Uint8Array) {
|
|
1083
|
+
return { ...data, data: Buffer.from(bytes).toString("base64") };
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
return data;
|
|
1087
|
+
}
|
|
726
1088
|
|
|
727
1089
|
// src/gateway-embedding-model.ts
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
1090
|
+
import {
|
|
1091
|
+
combineHeaders as combineHeaders2,
|
|
1092
|
+
createJsonErrorResponseHandler as createJsonErrorResponseHandler5,
|
|
1093
|
+
createJsonResponseHandler as createJsonResponseHandler5,
|
|
1094
|
+
lazySchema as lazySchema7,
|
|
1095
|
+
postJsonToApi as postJsonToApi2,
|
|
1096
|
+
resolve as resolve5,
|
|
1097
|
+
serializeModelOptions as serializeModelOptions2,
|
|
1098
|
+
WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE2,
|
|
1099
|
+
WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE2,
|
|
1100
|
+
zodSchema as zodSchema7
|
|
1101
|
+
} from "@ai-sdk/provider-utils";
|
|
1102
|
+
import { z as z8 } from "zod/v4";
|
|
1103
|
+
var GatewayEmbeddingModel = class _GatewayEmbeddingModel {
|
|
731
1104
|
constructor(modelId, config) {
|
|
732
1105
|
this.modelId = modelId;
|
|
733
1106
|
this.config = config;
|
|
734
|
-
this.specificationVersion = "
|
|
1107
|
+
this.specificationVersion = "v4";
|
|
735
1108
|
this.maxEmbeddingsPerCall = 2048;
|
|
736
1109
|
this.supportsParallelCalls = true;
|
|
737
1110
|
}
|
|
1111
|
+
static [WORKFLOW_SERIALIZE2](model) {
|
|
1112
|
+
return serializeModelOptions2({
|
|
1113
|
+
modelId: model.modelId,
|
|
1114
|
+
config: model.config
|
|
1115
|
+
});
|
|
1116
|
+
}
|
|
1117
|
+
static [WORKFLOW_DESERIALIZE2](options) {
|
|
1118
|
+
return new _GatewayEmbeddingModel(options.modelId, options.config);
|
|
1119
|
+
}
|
|
738
1120
|
get provider() {
|
|
739
1121
|
return this.config.provider;
|
|
740
1122
|
}
|
|
@@ -744,30 +1126,30 @@ var GatewayEmbeddingModel = class {
|
|
|
744
1126
|
abortSignal,
|
|
745
1127
|
providerOptions
|
|
746
1128
|
}) {
|
|
747
|
-
var
|
|
748
|
-
const resolvedHeaders = await (
|
|
1129
|
+
var _a11, _b11;
|
|
1130
|
+
const resolvedHeaders = this.config.headers ? await resolve5(this.config.headers) : void 0;
|
|
749
1131
|
try {
|
|
750
1132
|
const {
|
|
751
1133
|
responseHeaders,
|
|
752
1134
|
value: responseBody,
|
|
753
1135
|
rawValue
|
|
754
|
-
} = await (
|
|
1136
|
+
} = await postJsonToApi2({
|
|
755
1137
|
url: this.getUrl(),
|
|
756
|
-
headers: (
|
|
1138
|
+
headers: combineHeaders2(
|
|
757
1139
|
resolvedHeaders,
|
|
758
1140
|
headers != null ? headers : {},
|
|
759
1141
|
this.getModelConfigHeaders(),
|
|
760
|
-
await (
|
|
1142
|
+
await resolve5(this.config.o11yHeaders)
|
|
761
1143
|
),
|
|
762
1144
|
body: {
|
|
763
1145
|
values,
|
|
764
1146
|
...providerOptions ? { providerOptions } : {}
|
|
765
1147
|
},
|
|
766
|
-
successfulResponseHandler: (
|
|
1148
|
+
successfulResponseHandler: createJsonResponseHandler5(
|
|
767
1149
|
gatewayEmbeddingResponseSchema
|
|
768
1150
|
),
|
|
769
|
-
failedResponseHandler: (
|
|
770
|
-
errorSchema:
|
|
1151
|
+
failedResponseHandler: createJsonErrorResponseHandler5({
|
|
1152
|
+
errorSchema: z8.any(),
|
|
771
1153
|
errorToMessage: (data) => data
|
|
772
1154
|
}),
|
|
773
1155
|
...abortSignal && { abortSignal },
|
|
@@ -775,13 +1157,16 @@ var GatewayEmbeddingModel = class {
|
|
|
775
1157
|
});
|
|
776
1158
|
return {
|
|
777
1159
|
embeddings: responseBody.embeddings,
|
|
778
|
-
usage: (
|
|
1160
|
+
usage: (_a11 = responseBody.usage) != null ? _a11 : void 0,
|
|
779
1161
|
providerMetadata: responseBody.providerMetadata,
|
|
780
1162
|
response: { headers: responseHeaders, body: rawValue },
|
|
781
|
-
warnings: []
|
|
1163
|
+
warnings: (_b11 = responseBody.warnings) != null ? _b11 : []
|
|
782
1164
|
};
|
|
783
1165
|
} catch (error) {
|
|
784
|
-
throw await asGatewayError(
|
|
1166
|
+
throw await asGatewayError(
|
|
1167
|
+
error,
|
|
1168
|
+
await parseAuthMethod(resolvedHeaders != null ? resolvedHeaders : {})
|
|
1169
|
+
);
|
|
785
1170
|
}
|
|
786
1171
|
}
|
|
787
1172
|
getUrl() {
|
|
@@ -789,32 +1174,73 @@ var GatewayEmbeddingModel = class {
|
|
|
789
1174
|
}
|
|
790
1175
|
getModelConfigHeaders() {
|
|
791
1176
|
return {
|
|
792
|
-
"ai-embedding-model-specification-version": "
|
|
1177
|
+
"ai-embedding-model-specification-version": "4",
|
|
793
1178
|
"ai-model-id": this.modelId
|
|
794
1179
|
};
|
|
795
1180
|
}
|
|
796
1181
|
};
|
|
797
|
-
var
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
1182
|
+
var gatewayEmbeddingWarningSchema = z8.discriminatedUnion("type", [
|
|
1183
|
+
z8.object({
|
|
1184
|
+
type: z8.literal("unsupported"),
|
|
1185
|
+
feature: z8.string(),
|
|
1186
|
+
details: z8.string().optional()
|
|
1187
|
+
}),
|
|
1188
|
+
z8.object({
|
|
1189
|
+
type: z8.literal("compatibility"),
|
|
1190
|
+
feature: z8.string(),
|
|
1191
|
+
details: z8.string().optional()
|
|
1192
|
+
}),
|
|
1193
|
+
z8.object({
|
|
1194
|
+
type: z8.literal("deprecated"),
|
|
1195
|
+
setting: z8.string(),
|
|
1196
|
+
message: z8.string()
|
|
1197
|
+
}),
|
|
1198
|
+
z8.object({
|
|
1199
|
+
type: z8.literal("other"),
|
|
1200
|
+
message: z8.string()
|
|
1201
|
+
})
|
|
1202
|
+
]);
|
|
1203
|
+
var gatewayEmbeddingResponseSchema = lazySchema7(
|
|
1204
|
+
() => zodSchema7(
|
|
1205
|
+
z8.object({
|
|
1206
|
+
embeddings: z8.array(z8.array(z8.number())),
|
|
1207
|
+
usage: z8.object({ tokens: z8.number() }).nullish(),
|
|
1208
|
+
warnings: z8.array(gatewayEmbeddingWarningSchema).optional(),
|
|
1209
|
+
providerMetadata: z8.record(z8.string(), z8.record(z8.string(), z8.unknown())).optional()
|
|
803
1210
|
})
|
|
804
1211
|
)
|
|
805
1212
|
);
|
|
806
1213
|
|
|
807
1214
|
// src/gateway-image-model.ts
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
1215
|
+
import {
|
|
1216
|
+
combineHeaders as combineHeaders3,
|
|
1217
|
+
convertUint8ArrayToBase64,
|
|
1218
|
+
createJsonResponseHandler as createJsonResponseHandler6,
|
|
1219
|
+
createJsonErrorResponseHandler as createJsonErrorResponseHandler6,
|
|
1220
|
+
postJsonToApi as postJsonToApi3,
|
|
1221
|
+
resolve as resolve6,
|
|
1222
|
+
serializeModelOptions as serializeModelOptions3,
|
|
1223
|
+
WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE3,
|
|
1224
|
+
WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE3
|
|
1225
|
+
} from "@ai-sdk/provider-utils";
|
|
1226
|
+
import { z as z9 } from "zod/v4";
|
|
1227
|
+
var GatewayImageModel = class _GatewayImageModel {
|
|
811
1228
|
constructor(modelId, config) {
|
|
812
1229
|
this.modelId = modelId;
|
|
813
1230
|
this.config = config;
|
|
814
|
-
this.specificationVersion = "
|
|
1231
|
+
this.specificationVersion = "v4";
|
|
815
1232
|
// Set a very large number to prevent client-side splitting of requests
|
|
816
1233
|
this.maxImagesPerCall = Number.MAX_SAFE_INTEGER;
|
|
817
1234
|
}
|
|
1235
|
+
static [WORKFLOW_SERIALIZE3](model) {
|
|
1236
|
+
return serializeModelOptions3({
|
|
1237
|
+
modelId: model.modelId,
|
|
1238
|
+
config: model.config
|
|
1239
|
+
});
|
|
1240
|
+
}
|
|
1241
|
+
static [WORKFLOW_DESERIALIZE3](options) {
|
|
1242
|
+
return new _GatewayImageModel(options.modelId, options.config);
|
|
1243
|
+
}
|
|
818
1244
|
get provider() {
|
|
819
1245
|
return this.config.provider;
|
|
820
1246
|
}
|
|
@@ -830,20 +1256,16 @@ var GatewayImageModel = class {
|
|
|
830
1256
|
headers,
|
|
831
1257
|
abortSignal
|
|
832
1258
|
}) {
|
|
833
|
-
var
|
|
834
|
-
const resolvedHeaders = await (
|
|
1259
|
+
var _a11, _b11, _c, _d;
|
|
1260
|
+
const resolvedHeaders = this.config.headers ? await resolve6(this.config.headers) : void 0;
|
|
835
1261
|
try {
|
|
836
|
-
const {
|
|
837
|
-
responseHeaders,
|
|
838
|
-
value: responseBody,
|
|
839
|
-
rawValue
|
|
840
|
-
} = await (0, import_provider_utils7.postJsonToApi)({
|
|
1262
|
+
const { responseHeaders, value: responseBody } = await postJsonToApi3({
|
|
841
1263
|
url: this.getUrl(),
|
|
842
|
-
headers: (
|
|
1264
|
+
headers: combineHeaders3(
|
|
843
1265
|
resolvedHeaders,
|
|
844
1266
|
headers != null ? headers : {},
|
|
845
1267
|
this.getModelConfigHeaders(),
|
|
846
|
-
await (
|
|
1268
|
+
await resolve6(this.config.o11yHeaders)
|
|
847
1269
|
),
|
|
848
1270
|
body: {
|
|
849
1271
|
prompt,
|
|
@@ -857,11 +1279,11 @@ var GatewayImageModel = class {
|
|
|
857
1279
|
},
|
|
858
1280
|
...mask && { mask: maybeEncodeImageFile(mask) }
|
|
859
1281
|
},
|
|
860
|
-
successfulResponseHandler: (
|
|
1282
|
+
successfulResponseHandler: createJsonResponseHandler6(
|
|
861
1283
|
gatewayImageResponseSchema
|
|
862
1284
|
),
|
|
863
|
-
failedResponseHandler: (
|
|
864
|
-
errorSchema:
|
|
1285
|
+
failedResponseHandler: createJsonErrorResponseHandler6({
|
|
1286
|
+
errorSchema: z9.any(),
|
|
865
1287
|
errorToMessage: (data) => data
|
|
866
1288
|
}),
|
|
867
1289
|
...abortSignal && { abortSignal },
|
|
@@ -870,7 +1292,7 @@ var GatewayImageModel = class {
|
|
|
870
1292
|
return {
|
|
871
1293
|
images: responseBody.images,
|
|
872
1294
|
// Always base64 strings from server
|
|
873
|
-
warnings: (
|
|
1295
|
+
warnings: (_a11 = responseBody.warnings) != null ? _a11 : [],
|
|
874
1296
|
providerMetadata: responseBody.providerMetadata,
|
|
875
1297
|
response: {
|
|
876
1298
|
timestamp: /* @__PURE__ */ new Date(),
|
|
@@ -879,14 +1301,17 @@ var GatewayImageModel = class {
|
|
|
879
1301
|
},
|
|
880
1302
|
...responseBody.usage != null && {
|
|
881
1303
|
usage: {
|
|
882
|
-
inputTokens: (
|
|
1304
|
+
inputTokens: (_b11 = responseBody.usage.inputTokens) != null ? _b11 : void 0,
|
|
883
1305
|
outputTokens: (_c = responseBody.usage.outputTokens) != null ? _c : void 0,
|
|
884
1306
|
totalTokens: (_d = responseBody.usage.totalTokens) != null ? _d : void 0
|
|
885
1307
|
}
|
|
886
1308
|
}
|
|
887
1309
|
};
|
|
888
1310
|
} catch (error) {
|
|
889
|
-
throw await asGatewayError(
|
|
1311
|
+
throw await asGatewayError(
|
|
1312
|
+
error,
|
|
1313
|
+
await parseAuthMethod(resolvedHeaders != null ? resolvedHeaders : {})
|
|
1314
|
+
);
|
|
890
1315
|
}
|
|
891
1316
|
}
|
|
892
1317
|
getUrl() {
|
|
@@ -894,7 +1319,7 @@ var GatewayImageModel = class {
|
|
|
894
1319
|
}
|
|
895
1320
|
getModelConfigHeaders() {
|
|
896
1321
|
return {
|
|
897
|
-
"ai-image-model-specification-version": "
|
|
1322
|
+
"ai-image-model-specification-version": "4",
|
|
898
1323
|
"ai-model-id": this.modelId
|
|
899
1324
|
};
|
|
900
1325
|
}
|
|
@@ -903,52 +1328,66 @@ function maybeEncodeImageFile(file) {
|
|
|
903
1328
|
if (file.type === "file" && file.data instanceof Uint8Array) {
|
|
904
1329
|
return {
|
|
905
1330
|
...file,
|
|
906
|
-
data:
|
|
1331
|
+
data: convertUint8ArrayToBase64(file.data)
|
|
907
1332
|
};
|
|
908
1333
|
}
|
|
909
1334
|
return file;
|
|
910
1335
|
}
|
|
911
|
-
var providerMetadataEntrySchema =
|
|
912
|
-
images:
|
|
913
|
-
}).catchall(
|
|
914
|
-
var gatewayImageWarningSchema =
|
|
915
|
-
|
|
916
|
-
type:
|
|
917
|
-
feature:
|
|
918
|
-
details:
|
|
1336
|
+
var providerMetadataEntrySchema = z9.object({
|
|
1337
|
+
images: z9.array(z9.unknown()).optional()
|
|
1338
|
+
}).catchall(z9.unknown());
|
|
1339
|
+
var gatewayImageWarningSchema = z9.discriminatedUnion("type", [
|
|
1340
|
+
z9.object({
|
|
1341
|
+
type: z9.literal("unsupported"),
|
|
1342
|
+
feature: z9.string(),
|
|
1343
|
+
details: z9.string().optional()
|
|
1344
|
+
}),
|
|
1345
|
+
z9.object({
|
|
1346
|
+
type: z9.literal("compatibility"),
|
|
1347
|
+
feature: z9.string(),
|
|
1348
|
+
details: z9.string().optional()
|
|
919
1349
|
}),
|
|
920
|
-
|
|
921
|
-
type:
|
|
922
|
-
|
|
923
|
-
|
|
1350
|
+
z9.object({
|
|
1351
|
+
type: z9.literal("deprecated"),
|
|
1352
|
+
setting: z9.string(),
|
|
1353
|
+
message: z9.string()
|
|
924
1354
|
}),
|
|
925
|
-
|
|
926
|
-
type:
|
|
927
|
-
message:
|
|
1355
|
+
z9.object({
|
|
1356
|
+
type: z9.literal("other"),
|
|
1357
|
+
message: z9.string()
|
|
928
1358
|
})
|
|
929
1359
|
]);
|
|
930
|
-
var gatewayImageUsageSchema =
|
|
931
|
-
inputTokens:
|
|
932
|
-
outputTokens:
|
|
933
|
-
totalTokens:
|
|
1360
|
+
var gatewayImageUsageSchema = z9.object({
|
|
1361
|
+
inputTokens: z9.number().nullish(),
|
|
1362
|
+
outputTokens: z9.number().nullish(),
|
|
1363
|
+
totalTokens: z9.number().nullish()
|
|
934
1364
|
});
|
|
935
|
-
var gatewayImageResponseSchema =
|
|
936
|
-
images:
|
|
1365
|
+
var gatewayImageResponseSchema = z9.object({
|
|
1366
|
+
images: z9.array(z9.string()),
|
|
937
1367
|
// Always base64 strings over the wire
|
|
938
|
-
warnings:
|
|
939
|
-
providerMetadata:
|
|
1368
|
+
warnings: z9.array(gatewayImageWarningSchema).optional(),
|
|
1369
|
+
providerMetadata: z9.record(z9.string(), providerMetadataEntrySchema).optional(),
|
|
940
1370
|
usage: gatewayImageUsageSchema.optional()
|
|
941
1371
|
});
|
|
942
1372
|
|
|
943
1373
|
// src/gateway-video-model.ts
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
1374
|
+
import {
|
|
1375
|
+
APICallError as APICallError2
|
|
1376
|
+
} from "@ai-sdk/provider";
|
|
1377
|
+
import {
|
|
1378
|
+
combineHeaders as combineHeaders4,
|
|
1379
|
+
convertUint8ArrayToBase64 as convertUint8ArrayToBase642,
|
|
1380
|
+
createJsonErrorResponseHandler as createJsonErrorResponseHandler7,
|
|
1381
|
+
parseJsonEventStream,
|
|
1382
|
+
postJsonToApi as postJsonToApi4,
|
|
1383
|
+
resolve as resolve7
|
|
1384
|
+
} from "@ai-sdk/provider-utils";
|
|
1385
|
+
import { z as z10 } from "zod/v4";
|
|
947
1386
|
var GatewayVideoModel = class {
|
|
948
1387
|
constructor(modelId, config) {
|
|
949
1388
|
this.modelId = modelId;
|
|
950
1389
|
this.config = config;
|
|
951
|
-
this.specificationVersion = "
|
|
1390
|
+
this.specificationVersion = "v4";
|
|
952
1391
|
// Set a very large number to prevent client-side splitting of requests
|
|
953
1392
|
this.maxVideosPerCall = Number.MAX_SAFE_INTEGER;
|
|
954
1393
|
}
|
|
@@ -968,16 +1407,16 @@ var GatewayVideoModel = class {
|
|
|
968
1407
|
headers,
|
|
969
1408
|
abortSignal
|
|
970
1409
|
}) {
|
|
971
|
-
var
|
|
972
|
-
const resolvedHeaders = await (
|
|
1410
|
+
var _a11;
|
|
1411
|
+
const resolvedHeaders = this.config.headers ? await resolve7(this.config.headers) : void 0;
|
|
973
1412
|
try {
|
|
974
|
-
const { responseHeaders, value: responseBody } = await (
|
|
1413
|
+
const { responseHeaders, value: responseBody } = await postJsonToApi4({
|
|
975
1414
|
url: this.getUrl(),
|
|
976
|
-
headers: (
|
|
1415
|
+
headers: combineHeaders4(
|
|
977
1416
|
resolvedHeaders,
|
|
978
1417
|
headers != null ? headers : {},
|
|
979
1418
|
this.getModelConfigHeaders(),
|
|
980
|
-
await (
|
|
1419
|
+
await resolve7(this.config.o11yHeaders),
|
|
981
1420
|
{ accept: "text/event-stream" }
|
|
982
1421
|
),
|
|
983
1422
|
body: {
|
|
@@ -997,14 +1436,14 @@ var GatewayVideoModel = class {
|
|
|
997
1436
|
requestBodyValues
|
|
998
1437
|
}) => {
|
|
999
1438
|
if (response.body == null) {
|
|
1000
|
-
throw new
|
|
1439
|
+
throw new APICallError2({
|
|
1001
1440
|
message: "SSE response body is empty",
|
|
1002
1441
|
url,
|
|
1003
1442
|
requestBodyValues,
|
|
1004
1443
|
statusCode: response.status
|
|
1005
1444
|
});
|
|
1006
1445
|
}
|
|
1007
|
-
const eventStream =
|
|
1446
|
+
const eventStream = parseJsonEventStream({
|
|
1008
1447
|
stream: response.body,
|
|
1009
1448
|
schema: gatewayVideoEventSchema
|
|
1010
1449
|
});
|
|
@@ -1012,7 +1451,7 @@ var GatewayVideoModel = class {
|
|
|
1012
1451
|
const { done, value: parseResult } = await reader.read();
|
|
1013
1452
|
reader.releaseLock();
|
|
1014
1453
|
if (done || !parseResult) {
|
|
1015
|
-
throw new
|
|
1454
|
+
throw new APICallError2({
|
|
1016
1455
|
message: "SSE stream ended without a data event",
|
|
1017
1456
|
url,
|
|
1018
1457
|
requestBodyValues,
|
|
@@ -1020,7 +1459,7 @@ var GatewayVideoModel = class {
|
|
|
1020
1459
|
});
|
|
1021
1460
|
}
|
|
1022
1461
|
if (!parseResult.success) {
|
|
1023
|
-
throw new
|
|
1462
|
+
throw new APICallError2({
|
|
1024
1463
|
message: "Failed to parse video SSE event",
|
|
1025
1464
|
cause: parseResult.error,
|
|
1026
1465
|
url,
|
|
@@ -1030,7 +1469,7 @@ var GatewayVideoModel = class {
|
|
|
1030
1469
|
}
|
|
1031
1470
|
const event = parseResult.value;
|
|
1032
1471
|
if (event.type === "error") {
|
|
1033
|
-
throw new
|
|
1472
|
+
throw new APICallError2({
|
|
1034
1473
|
message: event.message,
|
|
1035
1474
|
statusCode: event.statusCode,
|
|
1036
1475
|
url,
|
|
@@ -1055,8 +1494,8 @@ var GatewayVideoModel = class {
|
|
|
1055
1494
|
responseHeaders: Object.fromEntries([...response.headers])
|
|
1056
1495
|
};
|
|
1057
1496
|
},
|
|
1058
|
-
failedResponseHandler: (
|
|
1059
|
-
errorSchema:
|
|
1497
|
+
failedResponseHandler: createJsonErrorResponseHandler7({
|
|
1498
|
+
errorSchema: z10.any(),
|
|
1060
1499
|
errorToMessage: (data) => data
|
|
1061
1500
|
}),
|
|
1062
1501
|
...abortSignal && { abortSignal },
|
|
@@ -1064,7 +1503,7 @@ var GatewayVideoModel = class {
|
|
|
1064
1503
|
});
|
|
1065
1504
|
return {
|
|
1066
1505
|
videos: responseBody.videos,
|
|
1067
|
-
warnings: (
|
|
1506
|
+
warnings: (_a11 = responseBody.warnings) != null ? _a11 : [],
|
|
1068
1507
|
providerMetadata: responseBody.providerMetadata,
|
|
1069
1508
|
response: {
|
|
1070
1509
|
timestamp: /* @__PURE__ */ new Date(),
|
|
@@ -1073,7 +1512,10 @@ var GatewayVideoModel = class {
|
|
|
1073
1512
|
}
|
|
1074
1513
|
};
|
|
1075
1514
|
} catch (error) {
|
|
1076
|
-
throw await asGatewayError(
|
|
1515
|
+
throw await asGatewayError(
|
|
1516
|
+
error,
|
|
1517
|
+
await parseAuthMethod(resolvedHeaders != null ? resolvedHeaders : {})
|
|
1518
|
+
);
|
|
1077
1519
|
}
|
|
1078
1520
|
}
|
|
1079
1521
|
getUrl() {
|
|
@@ -1081,7 +1523,7 @@ var GatewayVideoModel = class {
|
|
|
1081
1523
|
}
|
|
1082
1524
|
getModelConfigHeaders() {
|
|
1083
1525
|
return {
|
|
1084
|
-
"ai-video-model-specification-version": "
|
|
1526
|
+
"ai-video-model-specification-version": "4",
|
|
1085
1527
|
"ai-model-id": this.modelId
|
|
1086
1528
|
};
|
|
1087
1529
|
}
|
|
@@ -1090,116 +1532,535 @@ function maybeEncodeVideoFile(file) {
|
|
|
1090
1532
|
if (file.type === "file" && file.data instanceof Uint8Array) {
|
|
1091
1533
|
return {
|
|
1092
1534
|
...file,
|
|
1093
|
-
data: (
|
|
1535
|
+
data: convertUint8ArrayToBase642(file.data)
|
|
1094
1536
|
};
|
|
1095
1537
|
}
|
|
1096
1538
|
return file;
|
|
1097
1539
|
}
|
|
1098
|
-
var providerMetadataEntrySchema2 =
|
|
1099
|
-
videos:
|
|
1100
|
-
}).catchall(
|
|
1101
|
-
var gatewayVideoDataSchema =
|
|
1102
|
-
|
|
1103
|
-
type:
|
|
1104
|
-
url:
|
|
1105
|
-
mediaType:
|
|
1540
|
+
var providerMetadataEntrySchema2 = z10.object({
|
|
1541
|
+
videos: z10.array(z10.unknown()).optional()
|
|
1542
|
+
}).catchall(z10.unknown());
|
|
1543
|
+
var gatewayVideoDataSchema = z10.union([
|
|
1544
|
+
z10.object({
|
|
1545
|
+
type: z10.literal("url"),
|
|
1546
|
+
url: z10.string(),
|
|
1547
|
+
mediaType: z10.string()
|
|
1106
1548
|
}),
|
|
1107
|
-
|
|
1108
|
-
type:
|
|
1109
|
-
data:
|
|
1110
|
-
mediaType:
|
|
1549
|
+
z10.object({
|
|
1550
|
+
type: z10.literal("base64"),
|
|
1551
|
+
data: z10.string(),
|
|
1552
|
+
mediaType: z10.string()
|
|
1111
1553
|
})
|
|
1112
1554
|
]);
|
|
1113
|
-
var gatewayVideoWarningSchema =
|
|
1114
|
-
|
|
1115
|
-
type:
|
|
1116
|
-
feature:
|
|
1117
|
-
details:
|
|
1555
|
+
var gatewayVideoWarningSchema = z10.discriminatedUnion("type", [
|
|
1556
|
+
z10.object({
|
|
1557
|
+
type: z10.literal("unsupported"),
|
|
1558
|
+
feature: z10.string(),
|
|
1559
|
+
details: z10.string().optional()
|
|
1118
1560
|
}),
|
|
1119
|
-
|
|
1120
|
-
type:
|
|
1121
|
-
feature:
|
|
1122
|
-
details:
|
|
1561
|
+
z10.object({
|
|
1562
|
+
type: z10.literal("compatibility"),
|
|
1563
|
+
feature: z10.string(),
|
|
1564
|
+
details: z10.string().optional()
|
|
1123
1565
|
}),
|
|
1124
|
-
|
|
1125
|
-
type:
|
|
1126
|
-
|
|
1566
|
+
z10.object({
|
|
1567
|
+
type: z10.literal("deprecated"),
|
|
1568
|
+
setting: z10.string(),
|
|
1569
|
+
message: z10.string()
|
|
1570
|
+
}),
|
|
1571
|
+
z10.object({
|
|
1572
|
+
type: z10.literal("other"),
|
|
1573
|
+
message: z10.string()
|
|
1127
1574
|
})
|
|
1128
1575
|
]);
|
|
1129
|
-
var gatewayVideoEventSchema =
|
|
1130
|
-
|
|
1131
|
-
type:
|
|
1132
|
-
videos:
|
|
1133
|
-
warnings:
|
|
1134
|
-
providerMetadata:
|
|
1576
|
+
var gatewayVideoEventSchema = z10.discriminatedUnion("type", [
|
|
1577
|
+
z10.object({
|
|
1578
|
+
type: z10.literal("result"),
|
|
1579
|
+
videos: z10.array(gatewayVideoDataSchema),
|
|
1580
|
+
warnings: z10.array(gatewayVideoWarningSchema).optional(),
|
|
1581
|
+
providerMetadata: z10.record(z10.string(), providerMetadataEntrySchema2).optional()
|
|
1135
1582
|
}),
|
|
1136
|
-
|
|
1137
|
-
type:
|
|
1138
|
-
message:
|
|
1139
|
-
errorType:
|
|
1140
|
-
statusCode:
|
|
1141
|
-
param:
|
|
1583
|
+
z10.object({
|
|
1584
|
+
type: z10.literal("error"),
|
|
1585
|
+
message: z10.string(),
|
|
1586
|
+
errorType: z10.string(),
|
|
1587
|
+
statusCode: z10.number(),
|
|
1588
|
+
param: z10.unknown().nullable()
|
|
1142
1589
|
})
|
|
1143
1590
|
]);
|
|
1144
1591
|
|
|
1592
|
+
// src/gateway-reranking-model.ts
|
|
1593
|
+
import {
|
|
1594
|
+
combineHeaders as combineHeaders5,
|
|
1595
|
+
createJsonErrorResponseHandler as createJsonErrorResponseHandler8,
|
|
1596
|
+
createJsonResponseHandler as createJsonResponseHandler7,
|
|
1597
|
+
lazySchema as lazySchema8,
|
|
1598
|
+
postJsonToApi as postJsonToApi5,
|
|
1599
|
+
resolve as resolve8,
|
|
1600
|
+
zodSchema as zodSchema8
|
|
1601
|
+
} from "@ai-sdk/provider-utils";
|
|
1602
|
+
import { z as z11 } from "zod/v4";
|
|
1603
|
+
var GatewayRerankingModel = class {
|
|
1604
|
+
constructor(modelId, config) {
|
|
1605
|
+
this.modelId = modelId;
|
|
1606
|
+
this.config = config;
|
|
1607
|
+
this.specificationVersion = "v4";
|
|
1608
|
+
}
|
|
1609
|
+
get provider() {
|
|
1610
|
+
return this.config.provider;
|
|
1611
|
+
}
|
|
1612
|
+
async doRerank({
|
|
1613
|
+
documents,
|
|
1614
|
+
query,
|
|
1615
|
+
topN,
|
|
1616
|
+
headers,
|
|
1617
|
+
abortSignal,
|
|
1618
|
+
providerOptions
|
|
1619
|
+
}) {
|
|
1620
|
+
var _a11;
|
|
1621
|
+
const resolvedHeaders = this.config.headers ? await resolve8(this.config.headers) : void 0;
|
|
1622
|
+
try {
|
|
1623
|
+
const {
|
|
1624
|
+
responseHeaders,
|
|
1625
|
+
value: responseBody,
|
|
1626
|
+
rawValue
|
|
1627
|
+
} = await postJsonToApi5({
|
|
1628
|
+
url: this.getUrl(),
|
|
1629
|
+
headers: combineHeaders5(
|
|
1630
|
+
resolvedHeaders,
|
|
1631
|
+
headers != null ? headers : {},
|
|
1632
|
+
this.getModelConfigHeaders(),
|
|
1633
|
+
await resolve8(this.config.o11yHeaders)
|
|
1634
|
+
),
|
|
1635
|
+
body: {
|
|
1636
|
+
documents,
|
|
1637
|
+
query,
|
|
1638
|
+
...topN != null ? { topN } : {},
|
|
1639
|
+
...providerOptions ? { providerOptions } : {}
|
|
1640
|
+
},
|
|
1641
|
+
successfulResponseHandler: createJsonResponseHandler7(
|
|
1642
|
+
gatewayRerankingResponseSchema
|
|
1643
|
+
),
|
|
1644
|
+
failedResponseHandler: createJsonErrorResponseHandler8({
|
|
1645
|
+
errorSchema: z11.any(),
|
|
1646
|
+
errorToMessage: (data) => data
|
|
1647
|
+
}),
|
|
1648
|
+
...abortSignal && { abortSignal },
|
|
1649
|
+
fetch: this.config.fetch
|
|
1650
|
+
});
|
|
1651
|
+
return {
|
|
1652
|
+
ranking: responseBody.ranking,
|
|
1653
|
+
providerMetadata: responseBody.providerMetadata,
|
|
1654
|
+
response: { headers: responseHeaders, body: rawValue },
|
|
1655
|
+
warnings: (_a11 = responseBody.warnings) != null ? _a11 : []
|
|
1656
|
+
};
|
|
1657
|
+
} catch (error) {
|
|
1658
|
+
throw await asGatewayError(
|
|
1659
|
+
error,
|
|
1660
|
+
await parseAuthMethod(resolvedHeaders != null ? resolvedHeaders : {})
|
|
1661
|
+
);
|
|
1662
|
+
}
|
|
1663
|
+
}
|
|
1664
|
+
getUrl() {
|
|
1665
|
+
return `${this.config.baseURL}/reranking-model`;
|
|
1666
|
+
}
|
|
1667
|
+
getModelConfigHeaders() {
|
|
1668
|
+
return {
|
|
1669
|
+
"ai-reranking-model-specification-version": "4",
|
|
1670
|
+
"ai-model-id": this.modelId
|
|
1671
|
+
};
|
|
1672
|
+
}
|
|
1673
|
+
};
|
|
1674
|
+
var gatewayRerankingWarningSchema = z11.discriminatedUnion("type", [
|
|
1675
|
+
z11.object({
|
|
1676
|
+
type: z11.literal("unsupported"),
|
|
1677
|
+
feature: z11.string(),
|
|
1678
|
+
details: z11.string().optional()
|
|
1679
|
+
}),
|
|
1680
|
+
z11.object({
|
|
1681
|
+
type: z11.literal("compatibility"),
|
|
1682
|
+
feature: z11.string(),
|
|
1683
|
+
details: z11.string().optional()
|
|
1684
|
+
}),
|
|
1685
|
+
z11.object({
|
|
1686
|
+
type: z11.literal("deprecated"),
|
|
1687
|
+
setting: z11.string(),
|
|
1688
|
+
message: z11.string()
|
|
1689
|
+
}),
|
|
1690
|
+
z11.object({
|
|
1691
|
+
type: z11.literal("other"),
|
|
1692
|
+
message: z11.string()
|
|
1693
|
+
})
|
|
1694
|
+
]);
|
|
1695
|
+
var gatewayRerankingResponseSchema = lazySchema8(
|
|
1696
|
+
() => zodSchema8(
|
|
1697
|
+
z11.object({
|
|
1698
|
+
ranking: z11.array(
|
|
1699
|
+
z11.object({
|
|
1700
|
+
index: z11.number(),
|
|
1701
|
+
relevanceScore: z11.number()
|
|
1702
|
+
})
|
|
1703
|
+
),
|
|
1704
|
+
warnings: z11.array(gatewayRerankingWarningSchema).optional(),
|
|
1705
|
+
providerMetadata: z11.record(z11.string(), z11.record(z11.string(), z11.unknown())).optional()
|
|
1706
|
+
})
|
|
1707
|
+
)
|
|
1708
|
+
);
|
|
1709
|
+
|
|
1710
|
+
// src/gateway-speech-model.ts
|
|
1711
|
+
import {
|
|
1712
|
+
combineHeaders as combineHeaders6,
|
|
1713
|
+
createJsonErrorResponseHandler as createJsonErrorResponseHandler9,
|
|
1714
|
+
createJsonResponseHandler as createJsonResponseHandler8,
|
|
1715
|
+
postJsonToApi as postJsonToApi6,
|
|
1716
|
+
resolve as resolve9
|
|
1717
|
+
} from "@ai-sdk/provider-utils";
|
|
1718
|
+
import { z as z12 } from "zod/v4";
|
|
1719
|
+
var GatewaySpeechModel = class {
|
|
1720
|
+
constructor(modelId, config) {
|
|
1721
|
+
this.modelId = modelId;
|
|
1722
|
+
this.config = config;
|
|
1723
|
+
this.specificationVersion = "v4";
|
|
1724
|
+
}
|
|
1725
|
+
get provider() {
|
|
1726
|
+
return this.config.provider;
|
|
1727
|
+
}
|
|
1728
|
+
async doGenerate({
|
|
1729
|
+
text,
|
|
1730
|
+
voice,
|
|
1731
|
+
outputFormat,
|
|
1732
|
+
instructions,
|
|
1733
|
+
speed,
|
|
1734
|
+
language,
|
|
1735
|
+
providerOptions,
|
|
1736
|
+
headers,
|
|
1737
|
+
abortSignal
|
|
1738
|
+
}) {
|
|
1739
|
+
var _a11;
|
|
1740
|
+
const resolvedHeaders = this.config.headers ? await resolve9(this.config.headers) : void 0;
|
|
1741
|
+
try {
|
|
1742
|
+
const {
|
|
1743
|
+
responseHeaders,
|
|
1744
|
+
value: responseBody,
|
|
1745
|
+
rawValue
|
|
1746
|
+
} = await postJsonToApi6({
|
|
1747
|
+
url: this.getUrl(),
|
|
1748
|
+
headers: combineHeaders6(
|
|
1749
|
+
resolvedHeaders,
|
|
1750
|
+
headers != null ? headers : {},
|
|
1751
|
+
this.getModelConfigHeaders(),
|
|
1752
|
+
await resolve9(this.config.o11yHeaders)
|
|
1753
|
+
),
|
|
1754
|
+
body: {
|
|
1755
|
+
text,
|
|
1756
|
+
...voice && { voice },
|
|
1757
|
+
...outputFormat && { outputFormat },
|
|
1758
|
+
...instructions && { instructions },
|
|
1759
|
+
...speed != null && { speed },
|
|
1760
|
+
...language && { language },
|
|
1761
|
+
...providerOptions && { providerOptions }
|
|
1762
|
+
},
|
|
1763
|
+
successfulResponseHandler: createJsonResponseHandler8(
|
|
1764
|
+
gatewaySpeechResponseSchema
|
|
1765
|
+
),
|
|
1766
|
+
failedResponseHandler: createJsonErrorResponseHandler9({
|
|
1767
|
+
errorSchema: z12.any(),
|
|
1768
|
+
errorToMessage: (data) => data
|
|
1769
|
+
}),
|
|
1770
|
+
...abortSignal && { abortSignal },
|
|
1771
|
+
fetch: this.config.fetch
|
|
1772
|
+
});
|
|
1773
|
+
return {
|
|
1774
|
+
audio: responseBody.audio,
|
|
1775
|
+
warnings: (_a11 = responseBody.warnings) != null ? _a11 : [],
|
|
1776
|
+
providerMetadata: responseBody.providerMetadata,
|
|
1777
|
+
response: {
|
|
1778
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
1779
|
+
modelId: this.modelId,
|
|
1780
|
+
headers: responseHeaders,
|
|
1781
|
+
body: rawValue
|
|
1782
|
+
}
|
|
1783
|
+
};
|
|
1784
|
+
} catch (error) {
|
|
1785
|
+
throw await asGatewayError(
|
|
1786
|
+
error,
|
|
1787
|
+
await parseAuthMethod(resolvedHeaders != null ? resolvedHeaders : {})
|
|
1788
|
+
);
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
getUrl() {
|
|
1792
|
+
return `${this.config.baseURL}/speech-model`;
|
|
1793
|
+
}
|
|
1794
|
+
getModelConfigHeaders() {
|
|
1795
|
+
return {
|
|
1796
|
+
"ai-speech-model-specification-version": "4",
|
|
1797
|
+
"ai-model-id": this.modelId
|
|
1798
|
+
};
|
|
1799
|
+
}
|
|
1800
|
+
};
|
|
1801
|
+
var providerMetadataEntrySchema3 = z12.object({}).catchall(z12.unknown());
|
|
1802
|
+
var gatewaySpeechWarningSchema = z12.discriminatedUnion("type", [
|
|
1803
|
+
z12.object({
|
|
1804
|
+
type: z12.literal("unsupported"),
|
|
1805
|
+
feature: z12.string(),
|
|
1806
|
+
details: z12.string().optional()
|
|
1807
|
+
}),
|
|
1808
|
+
z12.object({
|
|
1809
|
+
type: z12.literal("compatibility"),
|
|
1810
|
+
feature: z12.string(),
|
|
1811
|
+
details: z12.string().optional()
|
|
1812
|
+
}),
|
|
1813
|
+
z12.object({
|
|
1814
|
+
type: z12.literal("deprecated"),
|
|
1815
|
+
setting: z12.string(),
|
|
1816
|
+
message: z12.string()
|
|
1817
|
+
}),
|
|
1818
|
+
z12.object({
|
|
1819
|
+
type: z12.literal("other"),
|
|
1820
|
+
message: z12.string()
|
|
1821
|
+
})
|
|
1822
|
+
]);
|
|
1823
|
+
var gatewaySpeechResponseSchema = z12.object({
|
|
1824
|
+
audio: z12.string(),
|
|
1825
|
+
warnings: z12.array(gatewaySpeechWarningSchema).optional(),
|
|
1826
|
+
providerMetadata: z12.record(z12.string(), providerMetadataEntrySchema3).optional()
|
|
1827
|
+
});
|
|
1828
|
+
|
|
1829
|
+
// src/gateway-transcription-model.ts
|
|
1830
|
+
import {
|
|
1831
|
+
combineHeaders as combineHeaders7,
|
|
1832
|
+
convertUint8ArrayToBase64 as convertUint8ArrayToBase643,
|
|
1833
|
+
createJsonErrorResponseHandler as createJsonErrorResponseHandler10,
|
|
1834
|
+
createJsonResponseHandler as createJsonResponseHandler9,
|
|
1835
|
+
postJsonToApi as postJsonToApi7,
|
|
1836
|
+
resolve as resolve10
|
|
1837
|
+
} from "@ai-sdk/provider-utils";
|
|
1838
|
+
import { z as z13 } from "zod/v4";
|
|
1839
|
+
var GatewayTranscriptionModel = class {
|
|
1840
|
+
constructor(modelId, config) {
|
|
1841
|
+
this.modelId = modelId;
|
|
1842
|
+
this.config = config;
|
|
1843
|
+
this.specificationVersion = "v4";
|
|
1844
|
+
}
|
|
1845
|
+
get provider() {
|
|
1846
|
+
return this.config.provider;
|
|
1847
|
+
}
|
|
1848
|
+
async doGenerate({
|
|
1849
|
+
audio,
|
|
1850
|
+
mediaType,
|
|
1851
|
+
providerOptions,
|
|
1852
|
+
headers,
|
|
1853
|
+
abortSignal
|
|
1854
|
+
}) {
|
|
1855
|
+
var _a11, _b11, _c, _d;
|
|
1856
|
+
const resolvedHeaders = this.config.headers ? await resolve10(this.config.headers) : void 0;
|
|
1857
|
+
try {
|
|
1858
|
+
const {
|
|
1859
|
+
responseHeaders,
|
|
1860
|
+
value: responseBody,
|
|
1861
|
+
rawValue
|
|
1862
|
+
} = await postJsonToApi7({
|
|
1863
|
+
url: this.getUrl(),
|
|
1864
|
+
headers: combineHeaders7(
|
|
1865
|
+
resolvedHeaders,
|
|
1866
|
+
headers != null ? headers : {},
|
|
1867
|
+
this.getModelConfigHeaders(),
|
|
1868
|
+
await resolve10(this.config.o11yHeaders)
|
|
1869
|
+
),
|
|
1870
|
+
body: {
|
|
1871
|
+
audio: audio instanceof Uint8Array ? convertUint8ArrayToBase643(audio) : audio,
|
|
1872
|
+
mediaType,
|
|
1873
|
+
...providerOptions && { providerOptions }
|
|
1874
|
+
},
|
|
1875
|
+
successfulResponseHandler: createJsonResponseHandler9(
|
|
1876
|
+
gatewayTranscriptionResponseSchema
|
|
1877
|
+
),
|
|
1878
|
+
failedResponseHandler: createJsonErrorResponseHandler10({
|
|
1879
|
+
errorSchema: z13.any(),
|
|
1880
|
+
errorToMessage: (data) => data
|
|
1881
|
+
}),
|
|
1882
|
+
...abortSignal && { abortSignal },
|
|
1883
|
+
fetch: this.config.fetch
|
|
1884
|
+
});
|
|
1885
|
+
return {
|
|
1886
|
+
text: responseBody.text,
|
|
1887
|
+
segments: (_a11 = responseBody.segments) != null ? _a11 : [],
|
|
1888
|
+
language: (_b11 = responseBody.language) != null ? _b11 : void 0,
|
|
1889
|
+
durationInSeconds: (_c = responseBody.durationInSeconds) != null ? _c : void 0,
|
|
1890
|
+
warnings: (_d = responseBody.warnings) != null ? _d : [],
|
|
1891
|
+
providerMetadata: responseBody.providerMetadata,
|
|
1892
|
+
response: {
|
|
1893
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
1894
|
+
modelId: this.modelId,
|
|
1895
|
+
headers: responseHeaders,
|
|
1896
|
+
body: rawValue
|
|
1897
|
+
}
|
|
1898
|
+
};
|
|
1899
|
+
} catch (error) {
|
|
1900
|
+
throw await asGatewayError(
|
|
1901
|
+
error,
|
|
1902
|
+
await parseAuthMethod(resolvedHeaders != null ? resolvedHeaders : {})
|
|
1903
|
+
);
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
getUrl() {
|
|
1907
|
+
return `${this.config.baseURL}/transcription-model`;
|
|
1908
|
+
}
|
|
1909
|
+
getModelConfigHeaders() {
|
|
1910
|
+
return {
|
|
1911
|
+
"ai-transcription-model-specification-version": "4",
|
|
1912
|
+
"ai-model-id": this.modelId
|
|
1913
|
+
};
|
|
1914
|
+
}
|
|
1915
|
+
};
|
|
1916
|
+
var providerMetadataEntrySchema4 = z13.object({}).catchall(z13.unknown());
|
|
1917
|
+
var gatewayTranscriptionWarningSchema = z13.discriminatedUnion("type", [
|
|
1918
|
+
z13.object({
|
|
1919
|
+
type: z13.literal("unsupported"),
|
|
1920
|
+
feature: z13.string(),
|
|
1921
|
+
details: z13.string().optional()
|
|
1922
|
+
}),
|
|
1923
|
+
z13.object({
|
|
1924
|
+
type: z13.literal("compatibility"),
|
|
1925
|
+
feature: z13.string(),
|
|
1926
|
+
details: z13.string().optional()
|
|
1927
|
+
}),
|
|
1928
|
+
z13.object({
|
|
1929
|
+
type: z13.literal("deprecated"),
|
|
1930
|
+
setting: z13.string(),
|
|
1931
|
+
message: z13.string()
|
|
1932
|
+
}),
|
|
1933
|
+
z13.object({
|
|
1934
|
+
type: z13.literal("other"),
|
|
1935
|
+
message: z13.string()
|
|
1936
|
+
})
|
|
1937
|
+
]);
|
|
1938
|
+
var gatewayTranscriptionResponseSchema = z13.object({
|
|
1939
|
+
text: z13.string(),
|
|
1940
|
+
segments: z13.array(
|
|
1941
|
+
z13.object({
|
|
1942
|
+
text: z13.string(),
|
|
1943
|
+
startSecond: z13.number(),
|
|
1944
|
+
endSecond: z13.number()
|
|
1945
|
+
})
|
|
1946
|
+
).optional(),
|
|
1947
|
+
language: z13.string().nullish(),
|
|
1948
|
+
durationInSeconds: z13.number().nullish(),
|
|
1949
|
+
warnings: z13.array(gatewayTranscriptionWarningSchema).optional(),
|
|
1950
|
+
providerMetadata: z13.record(z13.string(), providerMetadataEntrySchema4).optional()
|
|
1951
|
+
});
|
|
1952
|
+
|
|
1953
|
+
// src/gateway-realtime-model.ts
|
|
1954
|
+
var GatewayRealtimeModel = class {
|
|
1955
|
+
constructor(modelId, config) {
|
|
1956
|
+
this.specificationVersion = "v4";
|
|
1957
|
+
this.modelId = modelId;
|
|
1958
|
+
this.provider = config.provider;
|
|
1959
|
+
this.config = config;
|
|
1960
|
+
}
|
|
1961
|
+
/**
|
|
1962
|
+
* Unlike providers with a dedicated ephemeral-secret endpoint (e.g. OpenAI),
|
|
1963
|
+
* the Gateway v0 realtime path does not mint a new client secret. The returned
|
|
1964
|
+
* token is the Gateway credential resolved by the provider (`apiKey`,
|
|
1965
|
+
* `AI_GATEWAY_API_KEY`, or Vercel OIDC token) and the WebSocket upgrade is
|
|
1966
|
+
* authenticated directly with that credential. The
|
|
1967
|
+
* `RealtimeModelV4ClientSecretOptions` are therefore intentionally unused:
|
|
1968
|
+
* `sessionConfig` is applied later via the normalized `session-update` event,
|
|
1969
|
+
* and `expiresAfterSeconds` has no Gateway-side equivalent.
|
|
1970
|
+
*/
|
|
1971
|
+
async doCreateClientSecret() {
|
|
1972
|
+
const { token } = await this.config.getAuthToken();
|
|
1973
|
+
return {
|
|
1974
|
+
token,
|
|
1975
|
+
url: toGatewayRealtimeUrl(this.config.baseURL, this.modelId)
|
|
1976
|
+
};
|
|
1977
|
+
}
|
|
1978
|
+
getWebSocketConfig(options) {
|
|
1979
|
+
return {
|
|
1980
|
+
url: options.url,
|
|
1981
|
+
protocols: getGatewayRealtimeProtocols(options.token, {
|
|
1982
|
+
teamIdOrSlug: this.config.teamIdOrSlug
|
|
1983
|
+
})
|
|
1984
|
+
};
|
|
1985
|
+
}
|
|
1986
|
+
parseServerEvent(raw) {
|
|
1987
|
+
return raw;
|
|
1988
|
+
}
|
|
1989
|
+
serializeClientEvent(event) {
|
|
1990
|
+
return event;
|
|
1991
|
+
}
|
|
1992
|
+
buildSessionConfig(config) {
|
|
1993
|
+
return config;
|
|
1994
|
+
}
|
|
1995
|
+
};
|
|
1996
|
+
function toGatewayRealtimeUrl(baseURL, modelId) {
|
|
1997
|
+
const url = new URL(`${baseURL.replace(/^http/, "ws")}/realtime-model`);
|
|
1998
|
+
url.searchParams.set("ai-model-id", modelId);
|
|
1999
|
+
return url.toString();
|
|
2000
|
+
}
|
|
2001
|
+
|
|
1145
2002
|
// src/tool/parallel-search.ts
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
2003
|
+
import {
|
|
2004
|
+
createProviderExecutedToolFactory,
|
|
2005
|
+
lazySchema as lazySchema9,
|
|
2006
|
+
zodSchema as zodSchema9
|
|
2007
|
+
} from "@ai-sdk/provider-utils";
|
|
2008
|
+
import { z as z14 } from "zod";
|
|
2009
|
+
var parallelSearchInputSchema = lazySchema9(
|
|
2010
|
+
() => zodSchema9(
|
|
2011
|
+
z14.object({
|
|
2012
|
+
objective: z14.string().describe(
|
|
1152
2013
|
"Natural-language description of the web research goal, including source or freshness guidance and broader context from the task. Maximum 5000 characters."
|
|
1153
2014
|
),
|
|
1154
|
-
search_queries:
|
|
2015
|
+
search_queries: z14.array(z14.string()).optional().describe(
|
|
1155
2016
|
"Optional search queries to supplement the objective. Maximum 200 characters per query."
|
|
1156
2017
|
),
|
|
1157
|
-
mode:
|
|
2018
|
+
mode: z14.enum(["one-shot", "agentic"]).optional().describe(
|
|
1158
2019
|
'Mode preset: "one-shot" for comprehensive results with longer excerpts (default), "agentic" for concise, token-efficient results for multi-step workflows.'
|
|
1159
2020
|
),
|
|
1160
|
-
max_results:
|
|
2021
|
+
max_results: z14.number().optional().describe(
|
|
1161
2022
|
"Maximum number of results to return (1-20). Defaults to 10 if not specified."
|
|
1162
2023
|
),
|
|
1163
|
-
source_policy:
|
|
1164
|
-
include_domains:
|
|
1165
|
-
exclude_domains:
|
|
1166
|
-
after_date:
|
|
2024
|
+
source_policy: z14.object({
|
|
2025
|
+
include_domains: z14.array(z14.string()).optional().describe("List of domains to include in search results."),
|
|
2026
|
+
exclude_domains: z14.array(z14.string()).optional().describe("List of domains to exclude from search results."),
|
|
2027
|
+
after_date: z14.string().optional().describe(
|
|
1167
2028
|
"Only include results published after this date (ISO 8601 format)."
|
|
1168
2029
|
)
|
|
1169
2030
|
}).optional().describe(
|
|
1170
2031
|
"Source policy for controlling which domains to include/exclude and freshness."
|
|
1171
2032
|
),
|
|
1172
|
-
excerpts:
|
|
1173
|
-
max_chars_per_result:
|
|
1174
|
-
max_chars_total:
|
|
2033
|
+
excerpts: z14.object({
|
|
2034
|
+
max_chars_per_result: z14.number().optional().describe("Maximum characters per result."),
|
|
2035
|
+
max_chars_total: z14.number().optional().describe("Maximum total characters across all results.")
|
|
1175
2036
|
}).optional().describe("Excerpt configuration for controlling result length."),
|
|
1176
|
-
fetch_policy:
|
|
1177
|
-
max_age_seconds:
|
|
2037
|
+
fetch_policy: z14.object({
|
|
2038
|
+
max_age_seconds: z14.number().optional().describe(
|
|
1178
2039
|
"Maximum age in seconds for cached content. Set to 0 to always fetch fresh content."
|
|
1179
2040
|
)
|
|
1180
2041
|
}).optional().describe("Fetch policy for controlling content freshness.")
|
|
1181
2042
|
})
|
|
1182
2043
|
)
|
|
1183
2044
|
);
|
|
1184
|
-
var parallelSearchOutputSchema = (
|
|
1185
|
-
() => (
|
|
1186
|
-
|
|
2045
|
+
var parallelSearchOutputSchema = lazySchema9(
|
|
2046
|
+
() => zodSchema9(
|
|
2047
|
+
z14.union([
|
|
1187
2048
|
// Success response
|
|
1188
|
-
|
|
1189
|
-
searchId:
|
|
1190
|
-
results:
|
|
1191
|
-
|
|
1192
|
-
url:
|
|
1193
|
-
title:
|
|
1194
|
-
excerpt:
|
|
1195
|
-
publishDate:
|
|
1196
|
-
relevanceScore:
|
|
2049
|
+
z14.object({
|
|
2050
|
+
searchId: z14.string(),
|
|
2051
|
+
results: z14.array(
|
|
2052
|
+
z14.object({
|
|
2053
|
+
url: z14.string(),
|
|
2054
|
+
title: z14.string(),
|
|
2055
|
+
excerpt: z14.string(),
|
|
2056
|
+
publishDate: z14.string().nullable().optional(),
|
|
2057
|
+
relevanceScore: z14.number().optional()
|
|
1197
2058
|
})
|
|
1198
2059
|
)
|
|
1199
2060
|
}),
|
|
1200
2061
|
// Error response
|
|
1201
|
-
|
|
1202
|
-
error:
|
|
2062
|
+
z14.object({
|
|
2063
|
+
error: z14.enum([
|
|
1203
2064
|
"api_error",
|
|
1204
2065
|
"rate_limit",
|
|
1205
2066
|
"timeout",
|
|
@@ -1207,13 +2068,13 @@ var parallelSearchOutputSchema = (0, import_provider_utils9.lazySchema)(
|
|
|
1207
2068
|
"configuration_error",
|
|
1208
2069
|
"unknown"
|
|
1209
2070
|
]),
|
|
1210
|
-
statusCode:
|
|
1211
|
-
message:
|
|
2071
|
+
statusCode: z14.number().optional(),
|
|
2072
|
+
message: z14.string()
|
|
1212
2073
|
})
|
|
1213
2074
|
])
|
|
1214
2075
|
)
|
|
1215
2076
|
);
|
|
1216
|
-
var parallelSearchToolFactory = (
|
|
2077
|
+
var parallelSearchToolFactory = createProviderExecutedToolFactory({
|
|
1217
2078
|
id: "gateway.parallel_search",
|
|
1218
2079
|
inputSchema: parallelSearchInputSchema,
|
|
1219
2080
|
outputSchema: parallelSearchOutputSchema
|
|
@@ -1221,82 +2082,86 @@ var parallelSearchToolFactory = (0, import_provider_utils9.createProviderToolFac
|
|
|
1221
2082
|
var parallelSearch = (config = {}) => parallelSearchToolFactory(config);
|
|
1222
2083
|
|
|
1223
2084
|
// src/tool/perplexity-search.ts
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
2085
|
+
import {
|
|
2086
|
+
createProviderExecutedToolFactory as createProviderExecutedToolFactory2,
|
|
2087
|
+
lazySchema as lazySchema10,
|
|
2088
|
+
zodSchema as zodSchema10
|
|
2089
|
+
} from "@ai-sdk/provider-utils";
|
|
2090
|
+
import { z as z15 } from "zod";
|
|
2091
|
+
var perplexitySearchInputSchema = lazySchema10(
|
|
2092
|
+
() => zodSchema10(
|
|
2093
|
+
z15.object({
|
|
2094
|
+
query: z15.union([z15.string(), z15.array(z15.string())]).describe(
|
|
1230
2095
|
"Search query (string) or multiple queries (array of up to 5 strings). Multi-query searches return combined results from all queries."
|
|
1231
2096
|
),
|
|
1232
|
-
max_results:
|
|
2097
|
+
max_results: z15.number().optional().describe(
|
|
1233
2098
|
"Maximum number of search results to return (1-20, default: 10)"
|
|
1234
2099
|
),
|
|
1235
|
-
max_tokens_per_page:
|
|
2100
|
+
max_tokens_per_page: z15.number().optional().describe(
|
|
1236
2101
|
"Maximum number of tokens to extract per search result page (256-2048, default: 2048)"
|
|
1237
2102
|
),
|
|
1238
|
-
max_tokens:
|
|
2103
|
+
max_tokens: z15.number().optional().describe(
|
|
1239
2104
|
"Maximum total tokens across all search results (default: 25000, max: 1000000)"
|
|
1240
2105
|
),
|
|
1241
|
-
country:
|
|
2106
|
+
country: z15.string().optional().describe(
|
|
1242
2107
|
"Two-letter ISO 3166-1 alpha-2 country code for regional search results (e.g., 'US', 'GB', 'FR')"
|
|
1243
2108
|
),
|
|
1244
|
-
search_domain_filter:
|
|
2109
|
+
search_domain_filter: z15.array(z15.string()).optional().describe(
|
|
1245
2110
|
"List of domains to include or exclude from search results (max 20). To include: ['nature.com', 'science.org']. To exclude: ['-example.com', '-spam.net']"
|
|
1246
2111
|
),
|
|
1247
|
-
search_language_filter:
|
|
2112
|
+
search_language_filter: z15.array(z15.string()).optional().describe(
|
|
1248
2113
|
"List of ISO 639-1 language codes to filter results (max 10, lowercase). Examples: ['en', 'fr', 'de']"
|
|
1249
2114
|
),
|
|
1250
|
-
search_after_date:
|
|
2115
|
+
search_after_date: z15.string().optional().describe(
|
|
1251
2116
|
"Include only results published after this date. Format: 'MM/DD/YYYY' (e.g., '3/1/2025'). Cannot be used with search_recency_filter."
|
|
1252
2117
|
),
|
|
1253
|
-
search_before_date:
|
|
2118
|
+
search_before_date: z15.string().optional().describe(
|
|
1254
2119
|
"Include only results published before this date. Format: 'MM/DD/YYYY' (e.g., '3/15/2025'). Cannot be used with search_recency_filter."
|
|
1255
2120
|
),
|
|
1256
|
-
last_updated_after_filter:
|
|
2121
|
+
last_updated_after_filter: z15.string().optional().describe(
|
|
1257
2122
|
"Include only results last updated after this date. Format: 'MM/DD/YYYY' (e.g., '3/1/2025'). Cannot be used with search_recency_filter."
|
|
1258
2123
|
),
|
|
1259
|
-
last_updated_before_filter:
|
|
2124
|
+
last_updated_before_filter: z15.string().optional().describe(
|
|
1260
2125
|
"Include only results last updated before this date. Format: 'MM/DD/YYYY' (e.g., '3/15/2025'). Cannot be used with search_recency_filter."
|
|
1261
2126
|
),
|
|
1262
|
-
search_recency_filter:
|
|
2127
|
+
search_recency_filter: z15.enum(["day", "week", "month", "year"]).optional().describe(
|
|
1263
2128
|
"Filter results by relative time period. Cannot be used with search_after_date or search_before_date."
|
|
1264
2129
|
)
|
|
1265
2130
|
})
|
|
1266
2131
|
)
|
|
1267
2132
|
);
|
|
1268
|
-
var perplexitySearchOutputSchema = (
|
|
1269
|
-
() => (
|
|
1270
|
-
|
|
2133
|
+
var perplexitySearchOutputSchema = lazySchema10(
|
|
2134
|
+
() => zodSchema10(
|
|
2135
|
+
z15.union([
|
|
1271
2136
|
// Success response
|
|
1272
|
-
|
|
1273
|
-
results:
|
|
1274
|
-
|
|
1275
|
-
title:
|
|
1276
|
-
url:
|
|
1277
|
-
snippet:
|
|
1278
|
-
date:
|
|
1279
|
-
lastUpdated:
|
|
2137
|
+
z15.object({
|
|
2138
|
+
results: z15.array(
|
|
2139
|
+
z15.object({
|
|
2140
|
+
title: z15.string(),
|
|
2141
|
+
url: z15.string(),
|
|
2142
|
+
snippet: z15.string(),
|
|
2143
|
+
date: z15.string().optional(),
|
|
2144
|
+
lastUpdated: z15.string().optional()
|
|
1280
2145
|
})
|
|
1281
2146
|
),
|
|
1282
|
-
id:
|
|
2147
|
+
id: z15.string()
|
|
1283
2148
|
}),
|
|
1284
2149
|
// Error response
|
|
1285
|
-
|
|
1286
|
-
error:
|
|
2150
|
+
z15.object({
|
|
2151
|
+
error: z15.enum([
|
|
1287
2152
|
"api_error",
|
|
1288
2153
|
"rate_limit",
|
|
1289
2154
|
"timeout",
|
|
1290
2155
|
"invalid_input",
|
|
1291
2156
|
"unknown"
|
|
1292
2157
|
]),
|
|
1293
|
-
statusCode:
|
|
1294
|
-
message:
|
|
2158
|
+
statusCode: z15.number().optional(),
|
|
2159
|
+
message: z15.string()
|
|
1295
2160
|
})
|
|
1296
2161
|
])
|
|
1297
2162
|
)
|
|
1298
2163
|
);
|
|
1299
|
-
var perplexitySearchToolFactory = (
|
|
2164
|
+
var perplexitySearchToolFactory = createProviderExecutedToolFactory2({
|
|
1300
2165
|
id: "gateway.perplexity_search",
|
|
1301
2166
|
inputSchema: perplexitySearchInputSchema,
|
|
1302
2167
|
outputSchema: perplexitySearchOutputSchema
|
|
@@ -1325,40 +2190,50 @@ var gatewayTools = {
|
|
|
1325
2190
|
};
|
|
1326
2191
|
|
|
1327
2192
|
// src/vercel-environment.ts
|
|
1328
|
-
|
|
1329
|
-
|
|
2193
|
+
import { getContext } from "@vercel/oidc";
|
|
2194
|
+
import { getVercelOidcToken } from "@vercel/oidc";
|
|
1330
2195
|
async function getVercelRequestId() {
|
|
1331
|
-
var
|
|
1332
|
-
return (
|
|
2196
|
+
var _a11;
|
|
2197
|
+
return (_a11 = getContext().headers) == null ? void 0 : _a11["x-vercel-id"];
|
|
1333
2198
|
}
|
|
1334
2199
|
|
|
1335
|
-
// src/gateway-provider.ts
|
|
1336
|
-
var import_provider_utils12 = require("@ai-sdk/provider-utils");
|
|
1337
|
-
|
|
1338
2200
|
// src/version.ts
|
|
1339
|
-
var VERSION = true ? "4.0.0-beta.
|
|
2201
|
+
var VERSION = true ? "4.0.0-beta.108" : "0.0.0-test";
|
|
1340
2202
|
|
|
1341
2203
|
// src/gateway-provider.ts
|
|
1342
2204
|
var AI_GATEWAY_PROTOCOL_VERSION = "0.0.1";
|
|
1343
|
-
function
|
|
1344
|
-
var
|
|
2205
|
+
function createGateway(options = {}) {
|
|
2206
|
+
var _a11, _b11;
|
|
1345
2207
|
let pendingMetadata = null;
|
|
1346
2208
|
let metadataCache = null;
|
|
1347
|
-
const cacheRefreshMillis = (
|
|
2209
|
+
const cacheRefreshMillis = (_a11 = options.metadataCacheRefreshMillis) != null ? _a11 : 1e3 * 60 * 5;
|
|
1348
2210
|
let lastFetchTime = 0;
|
|
1349
|
-
const baseURL = (
|
|
2211
|
+
const baseURL = (_b11 = withoutTrailingSlash(options.baseURL)) != null ? _b11 : "https://ai-gateway.vercel.sh/v4/ai";
|
|
2212
|
+
const createAuthHeaders = (auth) => withUserAgentSuffix(
|
|
2213
|
+
{
|
|
2214
|
+
Authorization: `Bearer ${auth.token}`,
|
|
2215
|
+
"ai-gateway-protocol-version": AI_GATEWAY_PROTOCOL_VERSION,
|
|
2216
|
+
[GATEWAY_AUTH_METHOD_HEADER]: auth.authMethod,
|
|
2217
|
+
...options.teamIdOrSlug != null ? { [VERCEL_AI_GATEWAY_TEAM_HEADER]: options.teamIdOrSlug } : {},
|
|
2218
|
+
...options.headers
|
|
2219
|
+
},
|
|
2220
|
+
`ai-sdk/gateway/${VERSION}`
|
|
2221
|
+
);
|
|
1350
2222
|
const getHeaders = async () => {
|
|
1351
2223
|
try {
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
2224
|
+
return createAuthHeaders(await getGatewayAuthToken(options));
|
|
2225
|
+
} catch (error) {
|
|
2226
|
+
throw GatewayAuthenticationError.createContextualError({
|
|
2227
|
+
apiKeyProvided: false,
|
|
2228
|
+
oidcTokenProvided: false,
|
|
2229
|
+
statusCode: 401,
|
|
2230
|
+
cause: error
|
|
2231
|
+
});
|
|
2232
|
+
}
|
|
2233
|
+
};
|
|
2234
|
+
const getRealtimeAuthToken = async () => {
|
|
2235
|
+
try {
|
|
2236
|
+
return await getGatewayAuthToken(options);
|
|
1362
2237
|
} catch (error) {
|
|
1363
2238
|
throw GatewayAuthenticationError.createContextualError({
|
|
1364
2239
|
apiKeyProvided: false,
|
|
@@ -1369,19 +2244,19 @@ function createGatewayProvider(options = {}) {
|
|
|
1369
2244
|
}
|
|
1370
2245
|
};
|
|
1371
2246
|
const createO11yHeaders = () => {
|
|
1372
|
-
const deploymentId =
|
|
2247
|
+
const deploymentId = loadOptionalSetting({
|
|
1373
2248
|
settingValue: void 0,
|
|
1374
2249
|
environmentVariableName: "VERCEL_DEPLOYMENT_ID"
|
|
1375
2250
|
});
|
|
1376
|
-
const environment =
|
|
2251
|
+
const environment = loadOptionalSetting({
|
|
1377
2252
|
settingValue: void 0,
|
|
1378
2253
|
environmentVariableName: "VERCEL_ENV"
|
|
1379
2254
|
});
|
|
1380
|
-
const region =
|
|
2255
|
+
const region = loadOptionalSetting({
|
|
1381
2256
|
settingValue: void 0,
|
|
1382
2257
|
environmentVariableName: "VERCEL_REGION"
|
|
1383
2258
|
});
|
|
1384
|
-
const projectId =
|
|
2259
|
+
const projectId = loadOptionalSetting({
|
|
1385
2260
|
settingValue: void 0,
|
|
1386
2261
|
environmentVariableName: "VERCEL_PROJECT_ID"
|
|
1387
2262
|
});
|
|
@@ -1406,8 +2281,8 @@ function createGatewayProvider(options = {}) {
|
|
|
1406
2281
|
});
|
|
1407
2282
|
};
|
|
1408
2283
|
const getAvailableModels = async () => {
|
|
1409
|
-
var
|
|
1410
|
-
const now = (_c = (
|
|
2284
|
+
var _a12, _b12, _c;
|
|
2285
|
+
const now = (_c = (_b12 = (_a12 = options._internal) == null ? void 0 : _a12.currentDate) == null ? void 0 : _b12.call(_a12).getTime()) != null ? _c : Date.now();
|
|
1411
2286
|
if (!pendingMetadata || now - lastFetchTime > cacheRefreshMillis) {
|
|
1412
2287
|
lastFetchTime = now;
|
|
1413
2288
|
pendingMetadata = new GatewayFetchMetadata({
|
|
@@ -1438,6 +2313,30 @@ function createGatewayProvider(options = {}) {
|
|
|
1438
2313
|
);
|
|
1439
2314
|
});
|
|
1440
2315
|
};
|
|
2316
|
+
const getSpendReport = async (params) => {
|
|
2317
|
+
return new GatewaySpendReport({
|
|
2318
|
+
baseURL,
|
|
2319
|
+
headers: getHeaders,
|
|
2320
|
+
fetch: options.fetch
|
|
2321
|
+
}).getSpendReport(params).catch(async (error) => {
|
|
2322
|
+
throw await asGatewayError(
|
|
2323
|
+
error,
|
|
2324
|
+
await parseAuthMethod(await getHeaders())
|
|
2325
|
+
);
|
|
2326
|
+
});
|
|
2327
|
+
};
|
|
2328
|
+
const getGenerationInfo = async (params) => {
|
|
2329
|
+
return new GatewayGenerationInfoFetcher({
|
|
2330
|
+
baseURL,
|
|
2331
|
+
headers: getHeaders,
|
|
2332
|
+
fetch: options.fetch
|
|
2333
|
+
}).getGenerationInfo(params).catch(async (error) => {
|
|
2334
|
+
throw await asGatewayError(
|
|
2335
|
+
error,
|
|
2336
|
+
await parseAuthMethod(await getHeaders())
|
|
2337
|
+
);
|
|
2338
|
+
});
|
|
2339
|
+
};
|
|
1441
2340
|
const provider = function(modelId) {
|
|
1442
2341
|
if (new.target) {
|
|
1443
2342
|
throw new Error(
|
|
@@ -1446,9 +2345,11 @@ function createGatewayProvider(options = {}) {
|
|
|
1446
2345
|
}
|
|
1447
2346
|
return createLanguageModel(modelId);
|
|
1448
2347
|
};
|
|
1449
|
-
provider.specificationVersion = "
|
|
2348
|
+
provider.specificationVersion = "v4";
|
|
1450
2349
|
provider.getAvailableModels = getAvailableModels;
|
|
1451
2350
|
provider.getCredits = getCredits;
|
|
2351
|
+
provider.getSpendReport = getSpendReport;
|
|
2352
|
+
provider.getGenerationInfo = getGenerationInfo;
|
|
1452
2353
|
provider.imageModel = (modelId) => {
|
|
1453
2354
|
return new GatewayImageModel(modelId, {
|
|
1454
2355
|
provider: "gateway",
|
|
@@ -1479,6 +2380,62 @@ function createGatewayProvider(options = {}) {
|
|
|
1479
2380
|
o11yHeaders: createO11yHeaders()
|
|
1480
2381
|
});
|
|
1481
2382
|
};
|
|
2383
|
+
const createRerankingModel = (modelId) => {
|
|
2384
|
+
return new GatewayRerankingModel(modelId, {
|
|
2385
|
+
provider: "gateway",
|
|
2386
|
+
baseURL,
|
|
2387
|
+
headers: getHeaders,
|
|
2388
|
+
fetch: options.fetch,
|
|
2389
|
+
o11yHeaders: createO11yHeaders()
|
|
2390
|
+
});
|
|
2391
|
+
};
|
|
2392
|
+
provider.rerankingModel = createRerankingModel;
|
|
2393
|
+
provider.reranking = createRerankingModel;
|
|
2394
|
+
const createSpeechModel = (modelId) => {
|
|
2395
|
+
return new GatewaySpeechModel(modelId, {
|
|
2396
|
+
provider: "gateway",
|
|
2397
|
+
baseURL,
|
|
2398
|
+
headers: getHeaders,
|
|
2399
|
+
fetch: options.fetch,
|
|
2400
|
+
o11yHeaders: createO11yHeaders()
|
|
2401
|
+
});
|
|
2402
|
+
};
|
|
2403
|
+
provider.speechModel = createSpeechModel;
|
|
2404
|
+
provider.speech = createSpeechModel;
|
|
2405
|
+
const createTranscriptionModel = (modelId) => {
|
|
2406
|
+
return new GatewayTranscriptionModel(modelId, {
|
|
2407
|
+
provider: "gateway",
|
|
2408
|
+
baseURL,
|
|
2409
|
+
headers: getHeaders,
|
|
2410
|
+
fetch: options.fetch,
|
|
2411
|
+
o11yHeaders: createO11yHeaders()
|
|
2412
|
+
});
|
|
2413
|
+
};
|
|
2414
|
+
provider.transcriptionModel = createTranscriptionModel;
|
|
2415
|
+
provider.transcription = createTranscriptionModel;
|
|
2416
|
+
const createRealtimeModel = (modelId) => {
|
|
2417
|
+
assertGatewayRealtimeServerEnvironment();
|
|
2418
|
+
return new GatewayRealtimeModel(modelId, {
|
|
2419
|
+
provider: "gateway.realtime",
|
|
2420
|
+
baseURL,
|
|
2421
|
+
teamIdOrSlug: options.teamIdOrSlug,
|
|
2422
|
+
getAuthToken: getRealtimeAuthToken
|
|
2423
|
+
});
|
|
2424
|
+
};
|
|
2425
|
+
provider.experimental_realtime = Object.assign(
|
|
2426
|
+
(modelId) => createRealtimeModel(modelId),
|
|
2427
|
+
{
|
|
2428
|
+
getToken: async (tokenOptions) => {
|
|
2429
|
+
const model = createRealtimeModel(tokenOptions.model);
|
|
2430
|
+
const secret = await model.doCreateClientSecret();
|
|
2431
|
+
return {
|
|
2432
|
+
token: secret.token,
|
|
2433
|
+
url: secret.url,
|
|
2434
|
+
...secret.expiresAt != null && { expiresAt: secret.expiresAt }
|
|
2435
|
+
};
|
|
2436
|
+
}
|
|
2437
|
+
}
|
|
2438
|
+
);
|
|
1482
2439
|
provider.chat = provider.languageModel;
|
|
1483
2440
|
provider.embedding = provider.embeddingModel;
|
|
1484
2441
|
provider.image = provider.imageModel;
|
|
@@ -1486,9 +2443,9 @@ function createGatewayProvider(options = {}) {
|
|
|
1486
2443
|
provider.tools = gatewayTools;
|
|
1487
2444
|
return provider;
|
|
1488
2445
|
}
|
|
1489
|
-
var gateway =
|
|
2446
|
+
var gateway = createGateway();
|
|
1490
2447
|
async function getGatewayAuthToken(options) {
|
|
1491
|
-
const apiKey =
|
|
2448
|
+
const apiKey = loadOptionalSetting({
|
|
1492
2449
|
settingValue: options.apiKey,
|
|
1493
2450
|
environmentVariableName: "AI_GATEWAY_API_KEY"
|
|
1494
2451
|
});
|
|
@@ -1498,23 +2455,38 @@ async function getGatewayAuthToken(options) {
|
|
|
1498
2455
|
authMethod: "api-key"
|
|
1499
2456
|
};
|
|
1500
2457
|
}
|
|
1501
|
-
const oidcToken = await
|
|
2458
|
+
const oidcToken = await getVercelOidcToken();
|
|
1502
2459
|
return {
|
|
1503
2460
|
token: oidcToken,
|
|
1504
2461
|
authMethod: "oidc"
|
|
1505
2462
|
};
|
|
1506
2463
|
}
|
|
1507
|
-
|
|
1508
|
-
|
|
2464
|
+
function assertGatewayRealtimeServerEnvironment() {
|
|
2465
|
+
if (typeof globalThis.window !== "undefined") {
|
|
2466
|
+
throw new Error(
|
|
2467
|
+
"AI Gateway realtime models cannot be used in browsers yet. Use gateway.experimental_realtime from server-side code only."
|
|
2468
|
+
);
|
|
2469
|
+
}
|
|
2470
|
+
}
|
|
2471
|
+
export {
|
|
2472
|
+
GATEWAY_AUTH_SUBPROTOCOL_PREFIX,
|
|
2473
|
+
GATEWAY_REALTIME_SUBPROTOCOL,
|
|
2474
|
+
GATEWAY_TEAM_SUBPROTOCOL_PREFIX,
|
|
1509
2475
|
GatewayAuthenticationError,
|
|
1510
2476
|
GatewayError,
|
|
2477
|
+
GatewayFailedDependencyError,
|
|
2478
|
+
GatewayForbiddenError,
|
|
1511
2479
|
GatewayInternalServerError,
|
|
1512
2480
|
GatewayInvalidRequestError,
|
|
1513
2481
|
GatewayModelNotFoundError,
|
|
1514
2482
|
GatewayRateLimitError,
|
|
1515
2483
|
GatewayResponseError,
|
|
2484
|
+
VERSION,
|
|
1516
2485
|
createGateway,
|
|
1517
|
-
createGatewayProvider,
|
|
1518
|
-
gateway
|
|
1519
|
-
|
|
2486
|
+
createGateway as createGatewayProvider,
|
|
2487
|
+
gateway,
|
|
2488
|
+
getGatewayRealtimeAuthToken,
|
|
2489
|
+
getGatewayRealtimeProtocols,
|
|
2490
|
+
getGatewayRealtimeTeamIdOrSlug
|
|
2491
|
+
};
|
|
1520
2492
|
//# sourceMappingURL=index.js.map
|