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