@ai-sdk/gateway 4.0.120 → 4.0.121

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ai-sdk/gateway",
3
3
  "private": false,
4
- "version": "4.0.120",
4
+ "version": "4.0.121",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
7
7
  "sideEffects": false,
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "dependencies": {
33
33
  "@ai-sdk/provider": "4.0.31",
34
- "@ai-sdk/provider-utils": "5.0.64",
34
+ "@ai-sdk/provider-utils": "5.0.65",
35
35
  "@vercel/oidc": "3.2.0"
36
36
  },
37
37
  "devDependencies": {
@@ -14,6 +14,8 @@ import {
14
14
  type SharedV4ProviderOptions,
15
15
  } from '@ai-sdk/provider';
16
16
  import {
17
+ lazySchema,
18
+ zodSchema,
17
19
  combineHeaders,
18
20
  convertAsyncIteratorToReadableStream,
19
21
  createJsonErrorResponseHandler,
@@ -485,55 +487,66 @@ async function* convertGatewayBatchResultLines(
485
487
  }
486
488
  }
487
489
 
488
- const gatewayBatchItemResultLineSchema = z
489
- .object({
490
- type: z.literal('text'),
491
- id: z.string(),
492
- status: z.enum(['cancelled', 'expired', 'failed', 'succeeded']),
493
- })
494
- .catchall(z.unknown());
495
-
496
- const gatewayBatchErrorSchema = z.object({
497
- message: z.string(),
498
- type: z.string().nullish(),
499
- code: z.string().nullish(),
500
- statusCode: z.number().nullish(),
501
- });
502
-
503
- const gatewayBatchRequestCountsSchema = z.object({
504
- total: z.number().nullish(),
505
- pending: z.number().nullish(),
506
- completed: z.number().nullish(),
507
- failed: z.number().nullish(),
508
- });
509
-
510
- const gatewayBatchProviderMetadataSchema = z.record(
511
- z.string(),
512
- z.record(z.string(), z.unknown()),
490
+ const gatewayBatchItemResultLineSchema = lazySchema(() =>
491
+ zodSchema(
492
+ z
493
+ .object({
494
+ type: z.literal('text'),
495
+ id: z.string(),
496
+ status: z.enum(['cancelled', 'expired', 'failed', 'succeeded']),
497
+ })
498
+ .catchall(z.unknown()),
499
+ ),
513
500
  );
514
501
 
515
- const gatewayBatchStatusFieldsSchema = z.object({
516
- status: z.enum(['completed', 'failed', 'pending']),
517
- rawStatus: z.string().nullish(),
518
- requestCounts: gatewayBatchRequestCountsSchema.nullish(),
519
- error: gatewayBatchErrorSchema.nullish(),
520
- createdAt: z.string().nullish(),
521
- expiresAt: z.string().nullish(),
522
- providerMetadata: gatewayBatchProviderMetadataSchema.nullish(),
523
- });
524
-
525
- const gatewayBatchStartResponseSchema = gatewayBatchStatusFieldsSchema.extend({
526
- batchId: z.string(),
527
- warnings: z
528
- .array(
529
- z
530
- .object({
531
- requestId: z.string().nullish(),
532
- warning: z.unknown(),
533
- })
534
- .catchall(z.unknown()),
535
- )
536
- .nullish(),
537
- });
538
-
539
- const gatewayBatchStatusResponseSchema = gatewayBatchStatusFieldsSchema;
502
+ const gatewayBatchErrorSchema = () =>
503
+ z.object({
504
+ message: z.string(),
505
+ type: z.string().nullish(),
506
+ code: z.string().nullish(),
507
+ statusCode: z.number().nullish(),
508
+ });
509
+
510
+ const gatewayBatchRequestCountsSchema = () =>
511
+ z.object({
512
+ total: z.number().nullish(),
513
+ pending: z.number().nullish(),
514
+ completed: z.number().nullish(),
515
+ failed: z.number().nullish(),
516
+ });
517
+
518
+ const gatewayBatchProviderMetadataSchema = () =>
519
+ z.record(z.string(), z.record(z.string(), z.unknown()));
520
+
521
+ const gatewayBatchStatusFieldsSchema = () =>
522
+ z.object({
523
+ status: z.enum(['completed', 'failed', 'pending']),
524
+ rawStatus: z.string().nullish(),
525
+ requestCounts: gatewayBatchRequestCountsSchema().nullish(),
526
+ error: gatewayBatchErrorSchema().nullish(),
527
+ createdAt: z.string().nullish(),
528
+ expiresAt: z.string().nullish(),
529
+ providerMetadata: gatewayBatchProviderMetadataSchema().nullish(),
530
+ });
531
+
532
+ const gatewayBatchStartResponseSchema = lazySchema(() =>
533
+ zodSchema(
534
+ gatewayBatchStatusFieldsSchema().extend({
535
+ batchId: z.string(),
536
+ warnings: z
537
+ .array(
538
+ z
539
+ .object({
540
+ requestId: z.string().nullish(),
541
+ warning: z.unknown(),
542
+ })
543
+ .catchall(z.unknown()),
544
+ )
545
+ .nullish(),
546
+ }),
547
+ ),
548
+ );
549
+
550
+ const gatewayBatchStatusResponseSchema = lazySchema(() =>
551
+ zodSchema(gatewayBatchStatusFieldsSchema()),
552
+ );
@@ -135,52 +135,54 @@ export class GatewayDecisionModel implements DecisionModelV4 {
135
135
  }
136
136
  }
137
137
 
138
- const gatewayDecisionAnswerSchema = z.discriminatedUnion('type', [
139
- z.object({
140
- type: z.literal('choice'),
141
- choice: z.string(),
142
- probabilities: z.record(z.string(), z.number()).optional(),
143
- }),
144
- z.object({
145
- type: z.literal('score'),
146
- score: z.number(),
147
- probabilities: z.record(z.string(), z.number()).optional(),
148
- }),
149
- z.object({
150
- type: z.literal('boolean'),
151
- probability: z.number(),
152
- }),
153
- z.object({
154
- type: z.literal('refusal'),
155
- }),
156
- ]);
138
+ const gatewayDecisionAnswerSchema = () =>
139
+ z.discriminatedUnion('type', [
140
+ z.object({
141
+ type: z.literal('choice'),
142
+ choice: z.string(),
143
+ probabilities: z.record(z.string(), z.number()).optional(),
144
+ }),
145
+ z.object({
146
+ type: z.literal('score'),
147
+ score: z.number(),
148
+ probabilities: z.record(z.string(), z.number()).optional(),
149
+ }),
150
+ z.object({
151
+ type: z.literal('boolean'),
152
+ probability: z.number(),
153
+ }),
154
+ z.object({
155
+ type: z.literal('refusal'),
156
+ }),
157
+ ]);
157
158
 
158
- const gatewayDecisionWarningSchema = z.discriminatedUnion('type', [
159
- z.object({
160
- type: z.literal('unsupported'),
161
- feature: z.string(),
162
- details: z.string().optional(),
163
- }),
164
- z.object({
165
- type: z.literal('compatibility'),
166
- feature: z.string(),
167
- details: z.string().optional(),
168
- }),
169
- z.object({
170
- type: z.literal('deprecated'),
171
- setting: z.string(),
172
- message: z.string(),
173
- }),
174
- z.object({
175
- type: z.literal('other'),
176
- message: z.string(),
177
- }),
178
- ]);
159
+ const gatewayDecisionWarningSchema = () =>
160
+ z.discriminatedUnion('type', [
161
+ z.object({
162
+ type: z.literal('unsupported'),
163
+ feature: z.string(),
164
+ details: z.string().optional(),
165
+ }),
166
+ z.object({
167
+ type: z.literal('compatibility'),
168
+ feature: z.string(),
169
+ details: z.string().optional(),
170
+ }),
171
+ z.object({
172
+ type: z.literal('deprecated'),
173
+ setting: z.string(),
174
+ message: z.string(),
175
+ }),
176
+ z.object({
177
+ type: z.literal('other'),
178
+ message: z.string(),
179
+ }),
180
+ ]);
179
181
 
180
182
  const gatewayDecisionResponseSchema = lazySchema(() =>
181
183
  zodSchema(
182
184
  z.object({
183
- answers: z.record(z.string(), gatewayDecisionAnswerSchema),
185
+ answers: z.record(z.string(), gatewayDecisionAnswerSchema()),
184
186
  model: z.string().optional(),
185
187
  rounding: z
186
188
  .object({
@@ -194,7 +196,7 @@ const gatewayDecisionResponseSchema = lazySchema(() =>
194
196
  outputTokens: z.number().optional(),
195
197
  })
196
198
  .optional(),
197
- warnings: z.array(gatewayDecisionWarningSchema).optional(),
199
+ warnings: z.array(gatewayDecisionWarningSchema()).optional(),
198
200
  providerMetadata: z
199
201
  .record(z.string(), z.record(z.string(), z.unknown()))
200
202
  .optional(),
@@ -123,34 +123,35 @@ export class GatewayEmbeddingModel implements EmbeddingModelV4 {
123
123
  }
124
124
  }
125
125
 
126
- const gatewayEmbeddingWarningSchema = z.discriminatedUnion('type', [
127
- z.object({
128
- type: z.literal('unsupported'),
129
- feature: z.string(),
130
- details: z.string().optional(),
131
- }),
132
- z.object({
133
- type: z.literal('compatibility'),
134
- feature: z.string(),
135
- details: z.string().optional(),
136
- }),
137
- z.object({
138
- type: z.literal('deprecated'),
139
- setting: z.string(),
140
- message: z.string(),
141
- }),
142
- z.object({
143
- type: z.literal('other'),
144
- message: z.string(),
145
- }),
146
- ]);
126
+ const gatewayEmbeddingWarningSchema = () =>
127
+ z.discriminatedUnion('type', [
128
+ z.object({
129
+ type: z.literal('unsupported'),
130
+ feature: z.string(),
131
+ details: z.string().optional(),
132
+ }),
133
+ z.object({
134
+ type: z.literal('compatibility'),
135
+ feature: z.string(),
136
+ details: z.string().optional(),
137
+ }),
138
+ z.object({
139
+ type: z.literal('deprecated'),
140
+ setting: z.string(),
141
+ message: z.string(),
142
+ }),
143
+ z.object({
144
+ type: z.literal('other'),
145
+ message: z.string(),
146
+ }),
147
+ ]);
147
148
 
148
149
  const gatewayEmbeddingResponseSchema = lazySchema(() =>
149
150
  zodSchema(
150
151
  z.object({
151
152
  embeddings: z.array(z.array(z.number())),
152
153
  usage: z.object({ tokens: z.number() }).nullish(),
153
- warnings: z.array(gatewayEmbeddingWarningSchema).optional(),
154
+ warnings: z.array(gatewayEmbeddingWarningSchema()).optional(),
154
155
  providerMetadata: z
155
156
  .record(z.string(), z.record(z.string(), z.unknown()))
156
157
  .optional(),
@@ -4,6 +4,8 @@ import type {
4
4
  ImageModelV4ProviderMetadata,
5
5
  } from '@ai-sdk/provider';
6
6
  import {
7
+ lazySchema,
8
+ zodSchema,
7
9
  combineHeaders,
8
10
  convertUint8ArrayToBase64,
9
11
  createJsonErrorResponseHandler,
@@ -154,46 +156,53 @@ function maybeEncodeImageFile(file: ImageModelV4File) {
154
156
  return file;
155
157
  }
156
158
 
157
- const providerMetadataEntrySchema = z
158
- .object({
159
- images: z.array(z.unknown()).optional(),
160
- })
161
- .catchall(z.unknown());
159
+ const providerMetadataEntrySchema = () =>
160
+ z
161
+ .object({
162
+ images: z.array(z.unknown()).optional(),
163
+ })
164
+ .catchall(z.unknown());
162
165
 
163
- const gatewayImageWarningSchema = z.discriminatedUnion('type', [
164
- z.object({
165
- type: z.literal('unsupported'),
166
- feature: z.string(),
167
- details: z.string().optional(),
168
- }),
169
- z.object({
170
- type: z.literal('compatibility'),
171
- feature: z.string(),
172
- details: z.string().optional(),
173
- }),
174
- z.object({
175
- type: z.literal('deprecated'),
176
- setting: z.string(),
177
- message: z.string(),
178
- }),
179
- z.object({
180
- type: z.literal('other'),
181
- message: z.string(),
182
- }),
183
- ]);
166
+ const gatewayImageWarningSchema = () =>
167
+ z.discriminatedUnion('type', [
168
+ z.object({
169
+ type: z.literal('unsupported'),
170
+ feature: z.string(),
171
+ details: z.string().optional(),
172
+ }),
173
+ z.object({
174
+ type: z.literal('compatibility'),
175
+ feature: z.string(),
176
+ details: z.string().optional(),
177
+ }),
178
+ z.object({
179
+ type: z.literal('deprecated'),
180
+ setting: z.string(),
181
+ message: z.string(),
182
+ }),
183
+ z.object({
184
+ type: z.literal('other'),
185
+ message: z.string(),
186
+ }),
187
+ ]);
184
188
 
185
- const gatewayImageUsageSchema = z.object({
186
- inputTokens: z.number().nullish(),
187
- outputTokens: z.number().nullish(),
188
- totalTokens: z.number().nullish(),
189
- });
189
+ const gatewayImageUsageSchema = () =>
190
+ z.object({
191
+ inputTokens: z.number().nullish(),
192
+ outputTokens: z.number().nullish(),
193
+ totalTokens: z.number().nullish(),
194
+ });
190
195
 
191
- const gatewayImageResponseSchema = z.object({
192
- images: z.array(z.string()), // Always base64 strings over the wire
193
- isRetryable: z.boolean().optional(),
194
- warnings: z.array(gatewayImageWarningSchema).optional(),
195
- providerMetadata: z
196
- .record(z.string(), providerMetadataEntrySchema)
197
- .optional(),
198
- usage: gatewayImageUsageSchema.optional(),
199
- });
196
+ const gatewayImageResponseSchema = lazySchema(() =>
197
+ zodSchema(
198
+ z.object({
199
+ images: z.array(z.string()), // Always base64 strings over the wire
200
+ isRetryable: z.boolean().optional(),
201
+ warnings: z.array(gatewayImageWarningSchema()).optional(),
202
+ providerMetadata: z
203
+ .record(z.string(), providerMetadataEntrySchema())
204
+ .optional(),
205
+ usage: gatewayImageUsageSchema().optional(),
206
+ }),
207
+ ),
208
+ );
@@ -11,7 +11,7 @@ export const gatewayDecisionProviderOptionsSchema = lazySchema(() =>
11
11
  zodSchema(
12
12
  z
13
13
  .object({
14
- models: gatewayModelFallbacksSchema.optional(),
14
+ models: gatewayModelFallbacksSchema().optional(),
15
15
  })
16
16
  .catchall(z.unknown()),
17
17
  ),
@@ -156,79 +156,81 @@ type GatewayModelFallbackList<QUESTION_ID extends string> =
156
156
  | string[]
157
157
  | [ConditionalGatewayModelFallback<QUESTION_ID>, ...string[]];
158
158
 
159
- const probabilitySchema = z.number().finite().min(0).max(1);
160
- const questionSchema = z
161
- .string()
162
- .min(1)
163
- .max(DECISION_FALLBACK_MAX_QUESTION_LENGTH);
164
- const directConditionSchema = z.union([
159
+ const probabilitySchema = () => z.number().finite().min(0).max(1);
160
+ const questionSchema = () =>
161
+ z.string().min(1).max(DECISION_FALLBACK_MAX_QUESTION_LENGTH);
162
+ const directConditionSchema = () =>
163
+ z.union([
164
+ z
165
+ .object({
166
+ question: questionSchema().optional(),
167
+ confidenceBelow: probabilitySchema(),
168
+ })
169
+ .strict(),
170
+ z
171
+ .object({
172
+ question: questionSchema().optional(),
173
+ probabilityBetween: z
174
+ .array(probabilitySchema())
175
+ .length(2)
176
+ .refine(([minimum, maximum]) => minimum <= maximum, {
177
+ message:
178
+ 'probabilityBetween minimum must be less than or equal to maximum',
179
+ }),
180
+ })
181
+ .strict(),
182
+ ]) as ZodType<DecisionFallbackCondition>;
183
+
184
+ const groupBeyondMaxDepthSchema = () =>
185
+ z
186
+ .union([
187
+ z.object({ any: z.unknown() }),
188
+ z.object({ all: z.unknown() }),
189
+ z.object({ atLeast: z.unknown() }),
190
+ ])
191
+ .superRefine((_, context) => {
192
+ context.addIssue({
193
+ code: 'custom',
194
+ message: `conditions can be nested at most ${DECISION_FALLBACK_MAX_CONDITION_DEPTH} levels deep`,
195
+ });
196
+ });
197
+
198
+ const conditionalModelFallbackSchema = () =>
165
199
  z
166
200
  .object({
167
- question: questionSchema.optional(),
168
- confidenceBelow: probabilitySchema,
201
+ model: z.string().min(1),
202
+ when: conditionSchema(1),
169
203
  })
170
- .strict(),
204
+ .strict();
205
+
206
+ const gatewayModelFallbacksSchema = () =>
171
207
  z
172
- .object({
173
- question: questionSchema.optional(),
174
- probabilityBetween: z
175
- .array(probabilitySchema)
176
- .length(2)
177
- .refine(([minimum, maximum]) => minimum <= maximum, {
208
+ .array(z.union([z.string(), conditionalModelFallbackSchema()]))
209
+ .superRefine((entries, context) => {
210
+ const conditionalIndexes = entries.flatMap((entry, index) =>
211
+ typeof entry === 'string' ? [] : [index],
212
+ );
213
+ if (conditionalIndexes.length > 1) {
214
+ context.addIssue({
215
+ code: 'custom',
216
+ message: 'models supports at most one conditional decision fallback',
217
+ });
218
+ }
219
+ if (conditionalIndexes[0] !== undefined && conditionalIndexes[0] !== 0) {
220
+ context.addIssue({
221
+ code: 'custom',
178
222
  message:
179
- 'probabilityBetween minimum must be less than or equal to maximum',
180
- }),
181
- })
182
- .strict(),
183
- ]) as ZodType<DecisionFallbackCondition>;
184
-
185
- const groupBeyondMaxDepthSchema = z
186
- .union([
187
- z.object({ any: z.unknown() }),
188
- z.object({ all: z.unknown() }),
189
- z.object({ atLeast: z.unknown() }),
190
- ])
191
- .superRefine((_, context) => {
192
- context.addIssue({
193
- code: 'custom',
194
- message: `conditions can be nested at most ${DECISION_FALLBACK_MAX_CONDITION_DEPTH} levels deep`,
223
+ 'a conditional decision fallback must be the first models entry',
224
+ path: [conditionalIndexes[0]],
225
+ });
226
+ }
195
227
  });
196
- });
197
-
198
- const conditionalModelFallbackSchema = z
199
- .object({
200
- model: z.string().min(1),
201
- when: conditionSchema(1),
202
- })
203
- .strict();
204
-
205
- const gatewayModelFallbacksSchema = z
206
- .array(z.union([z.string(), conditionalModelFallbackSchema]))
207
- .superRefine((entries, context) => {
208
- const conditionalIndexes = entries.flatMap((entry, index) =>
209
- typeof entry === 'string' ? [] : [index],
210
- );
211
- if (conditionalIndexes.length > 1) {
212
- context.addIssue({
213
- code: 'custom',
214
- message: 'models supports at most one conditional decision fallback',
215
- });
216
- }
217
- if (conditionalIndexes[0] !== undefined && conditionalIndexes[0] !== 0) {
218
- context.addIssue({
219
- code: 'custom',
220
- message:
221
- 'a conditional decision fallback must be the first models entry',
222
- path: [conditionalIndexes[0]],
223
- });
224
- }
225
- });
226
228
 
227
229
  function conditionSchema(depth: number): ZodType<DecisionFallbackCondition> {
228
230
  if (depth === DECISION_FALLBACK_MAX_CONDITION_DEPTH) {
229
231
  return z.union([
230
- directConditionSchema,
231
- groupBeyondMaxDepthSchema,
232
+ directConditionSchema(),
233
+ groupBeyondMaxDepthSchema(),
232
234
  ]) as ZodType<DecisionFallbackCondition>;
233
235
  }
234
236
 
@@ -239,7 +241,7 @@ function conditionSchema(depth: number): ZodType<DecisionFallbackCondition> {
239
241
  .max(DECISION_FALLBACK_MAX_CONDITIONS_PER_LIST);
240
242
 
241
243
  return z.union([
242
- directConditionSchema,
244
+ directConditionSchema(),
243
245
  z.object({ any: conditionListSchema }).strict(),
244
246
  z.object({ all: conditionListSchema }).strict(),
245
247
  z
@@ -1,4 +1,6 @@
1
1
  import {
2
+ lazySchema,
3
+ zodSchema,
2
4
  createJsonErrorResponseHandler,
3
5
  createJsonResponseHandler,
4
6
  getErrorMessage,
@@ -309,10 +311,14 @@ export interface GatewayProviderSettings {
309
311
  const AI_GATEWAY_PROTOCOL_VERSION = '0.0.1';
310
312
 
311
313
  /** Response shape of `POST /v1/realtime/client-secrets`. `expiresAt` is epoch seconds. */
312
- const gatewayClientSecretResponseSchema = z.object({
313
- token: z.string(),
314
- expiresAt: z.number().nullish(),
315
- });
314
+ const gatewayClientSecretResponseSchema = lazySchema(() =>
315
+ zodSchema(
316
+ z.object({
317
+ token: z.string(),
318
+ expiresAt: z.number().nullish(),
319
+ }),
320
+ ),
321
+ );
316
322
 
317
323
  /**
318
324
  * Create a remote provider instance.
@@ -103,27 +103,28 @@ export class GatewayRerankingModel implements RerankingModelV4 {
103
103
  }
104
104
  }
105
105
 
106
- const gatewayRerankingWarningSchema = z.discriminatedUnion('type', [
107
- z.object({
108
- type: z.literal('unsupported'),
109
- feature: z.string(),
110
- details: z.string().optional(),
111
- }),
112
- z.object({
113
- type: z.literal('compatibility'),
114
- feature: z.string(),
115
- details: z.string().optional(),
116
- }),
117
- z.object({
118
- type: z.literal('deprecated'),
119
- setting: z.string(),
120
- message: z.string(),
121
- }),
122
- z.object({
123
- type: z.literal('other'),
124
- message: z.string(),
125
- }),
126
- ]);
106
+ const gatewayRerankingWarningSchema = () =>
107
+ z.discriminatedUnion('type', [
108
+ z.object({
109
+ type: z.literal('unsupported'),
110
+ feature: z.string(),
111
+ details: z.string().optional(),
112
+ }),
113
+ z.object({
114
+ type: z.literal('compatibility'),
115
+ feature: z.string(),
116
+ details: z.string().optional(),
117
+ }),
118
+ z.object({
119
+ type: z.literal('deprecated'),
120
+ setting: z.string(),
121
+ message: z.string(),
122
+ }),
123
+ z.object({
124
+ type: z.literal('other'),
125
+ message: z.string(),
126
+ }),
127
+ ]);
127
128
 
128
129
  const gatewayRerankingResponseSchema = lazySchema(() =>
129
130
  zodSchema(
@@ -134,7 +135,7 @@ const gatewayRerankingResponseSchema = lazySchema(() =>
134
135
  relevanceScore: z.number(),
135
136
  }),
136
137
  ),
137
- warnings: z.array(gatewayRerankingWarningSchema).optional(),
138
+ warnings: z.array(gatewayRerankingWarningSchema()).optional(),
138
139
  providerMetadata: z
139
140
  .record(z.string(), z.record(z.string(), z.unknown()))
140
141
  .optional(),