@mastra/schema-compat 0.0.0-add-save-score-validation-on-stores-20250911031242

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.
Files changed (46) hide show
  1. package/CHANGELOG.md +177 -0
  2. package/LICENSE.md +15 -0
  3. package/README.md +144 -0
  4. package/dist/chunk-7YUR5KZ5.cjs +34 -0
  5. package/dist/chunk-7YUR5KZ5.cjs.map +1 -0
  6. package/dist/chunk-GWTUXMDD.js +28 -0
  7. package/dist/chunk-GWTUXMDD.js.map +1 -0
  8. package/dist/index.cjs +1424 -0
  9. package/dist/index.cjs.map +1 -0
  10. package/dist/index.d.ts +11 -0
  11. package/dist/index.d.ts.map +1 -0
  12. package/dist/index.js +1399 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/provider-compats/anthropic.d.ts +13 -0
  15. package/dist/provider-compats/anthropic.d.ts.map +1 -0
  16. package/dist/provider-compats/deepseek.d.ts +13 -0
  17. package/dist/provider-compats/deepseek.d.ts.map +1 -0
  18. package/dist/provider-compats/google.d.ts +13 -0
  19. package/dist/provider-compats/google.d.ts.map +1 -0
  20. package/dist/provider-compats/meta.d.ts +13 -0
  21. package/dist/provider-compats/meta.d.ts.map +1 -0
  22. package/dist/provider-compats/openai-reasoning.d.ts +14 -0
  23. package/dist/provider-compats/openai-reasoning.d.ts.map +1 -0
  24. package/dist/provider-compats/openai.d.ts +13 -0
  25. package/dist/provider-compats/openai.d.ts.map +1 -0
  26. package/dist/schema-compatibility-v3.d.ts +319 -0
  27. package/dist/schema-compatibility-v3.d.ts.map +1 -0
  28. package/dist/schema-compatibility-v4.d.ts +310 -0
  29. package/dist/schema-compatibility-v4.d.ts.map +1 -0
  30. package/dist/schema-compatibility.d.ts +228 -0
  31. package/dist/schema-compatibility.d.ts.map +1 -0
  32. package/dist/types.d.ts +6 -0
  33. package/dist/types.d.ts.map +1 -0
  34. package/dist/utils-test-suite.d.ts +2 -0
  35. package/dist/utils-test-suite.d.ts.map +1 -0
  36. package/dist/utils.d.ts +96 -0
  37. package/dist/utils.d.ts.map +1 -0
  38. package/dist/zod-to-json.cjs +12 -0
  39. package/dist/zod-to-json.cjs.map +1 -0
  40. package/dist/zod-to-json.d.ts +6 -0
  41. package/dist/zod-to-json.d.ts.map +1 -0
  42. package/dist/zod-to-json.js +3 -0
  43. package/dist/zod-to-json.js.map +1 -0
  44. package/dist/zodTypes.d.ts +21 -0
  45. package/dist/zodTypes.d.ts.map +1 -0
  46. package/package.json +80 -0
package/dist/index.cjs ADDED
@@ -0,0 +1,1424 @@
1
+ 'use strict';
2
+
3
+ var chunk7YUR5KZ5_cjs = require('./chunk-7YUR5KZ5.cjs');
4
+ var zod = require('zod');
5
+ var ai = require('ai');
6
+ var zodFromJsonSchema = require('zod-from-json-schema');
7
+ var zodFromJsonSchemaV3 = require('zod-from-json-schema-v3');
8
+ var v4 = require('zod/v4');
9
+
10
+ function convertZodSchemaToAISDKSchema(zodSchema, target = "jsonSchema7") {
11
+ const jsonSchemaToUse = chunk7YUR5KZ5_cjs.zodToJsonSchema(zodSchema, target);
12
+ return ai.jsonSchema(jsonSchemaToUse, {
13
+ validate: (value) => {
14
+ const result = zodSchema.safeParse(value);
15
+ return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
16
+ }
17
+ });
18
+ }
19
+ function isZodType(value) {
20
+ return typeof value === "object" && value !== null && "_def" in value && "parse" in value && typeof value.parse === "function" && "safeParse" in value && typeof value.safeParse === "function";
21
+ }
22
+ function convertSchemaToZod(schema) {
23
+ if (isZodType(schema)) {
24
+ return schema;
25
+ } else {
26
+ const jsonSchemaToConvert = "jsonSchema" in schema ? schema.jsonSchema : schema;
27
+ try {
28
+ if ("toJSONSchema" in zod.z) {
29
+ return zodFromJsonSchema.convertJsonSchemaToZod(jsonSchemaToConvert);
30
+ } else {
31
+ return zodFromJsonSchemaV3.convertJsonSchemaToZod(jsonSchemaToConvert);
32
+ }
33
+ } catch (e) {
34
+ const errorMessage = `[Schema Builder] Failed to convert schema parameters to Zod. Original schema: ${JSON.stringify(jsonSchemaToConvert)}`;
35
+ console.error(errorMessage, e);
36
+ throw new Error(errorMessage + (e instanceof Error ? `
37
+ ${e.stack}` : "\nUnknown error object"));
38
+ }
39
+ }
40
+ }
41
+ function applyCompatLayer({
42
+ schema,
43
+ compatLayers,
44
+ mode
45
+ }) {
46
+ let zodSchema;
47
+ if (!isZodType(schema)) {
48
+ zodSchema = convertSchemaToZod(schema);
49
+ } else {
50
+ zodSchema = schema;
51
+ }
52
+ for (const compat of compatLayers) {
53
+ if (compat.shouldApply()) {
54
+ return mode === "jsonSchema" ? compat.processToJSONSchema(zodSchema) : compat.processToAISDKSchema(zodSchema);
55
+ }
56
+ }
57
+ if (mode === "jsonSchema") {
58
+ return chunk7YUR5KZ5_cjs.zodToJsonSchema(zodSchema, "jsonSchema7");
59
+ } else {
60
+ return convertZodSchemaToAISDKSchema(zodSchema);
61
+ }
62
+ }
63
+
64
+ // src/schema-compatibility-v3.ts
65
+ var ALL_STRING_CHECKS = ["regex", "emoji", "email", "url", "uuid", "cuid", "min", "max"];
66
+ var ALL_NUMBER_CHECKS = [
67
+ "min",
68
+ // gte internally
69
+ "max",
70
+ // lte internally
71
+ "multipleOf"
72
+ ];
73
+ var ALL_ARRAY_CHECKS = ["min", "max", "length"];
74
+ var isOptional = (v) => v instanceof zod.ZodOptional;
75
+ var isObj = (v) => v instanceof zod.ZodObject;
76
+ var isArr = (v) => v instanceof zod.ZodArray;
77
+ var isUnion = (v) => v instanceof zod.ZodUnion;
78
+ var isString = (v) => v instanceof zod.ZodString;
79
+ var isNumber = (v) => v instanceof zod.ZodNumber;
80
+ var UNSUPPORTED_ZOD_TYPES = ["ZodIntersection", "ZodNever", "ZodNull", "ZodTuple", "ZodUndefined"];
81
+ var SUPPORTED_ZOD_TYPES = [
82
+ "ZodObject",
83
+ "ZodArray",
84
+ "ZodUnion",
85
+ "ZodString",
86
+ "ZodNumber",
87
+ "ZodDate",
88
+ "ZodAny",
89
+ "ZodDefault"
90
+ ];
91
+ var ALL_ZOD_TYPES = [...SUPPORTED_ZOD_TYPES, ...UNSUPPORTED_ZOD_TYPES];
92
+ var SchemaCompatLayer = class {
93
+ model;
94
+ parent;
95
+ /**
96
+ * Creates a new schema compatibility instance.
97
+ *
98
+ * @param model - The language model this compatibility layer applies to
99
+ */
100
+ constructor(model, parent) {
101
+ this.model = model;
102
+ this.parent = parent;
103
+ }
104
+ /**
105
+ * Gets the language model associated with this compatibility layer.
106
+ *
107
+ * @returns The language model instance
108
+ */
109
+ getModel() {
110
+ return this.model;
111
+ }
112
+ getUnsupportedZodTypes() {
113
+ return UNSUPPORTED_ZOD_TYPES;
114
+ }
115
+ /**
116
+ * Type guard for optional Zod types
117
+ */
118
+ isOptional(v) {
119
+ return v instanceof zod.ZodOptional;
120
+ }
121
+ /**
122
+ * Type guard for object Zod types
123
+ */
124
+ isObj(v) {
125
+ return v instanceof zod.ZodObject;
126
+ }
127
+ /**
128
+ * Type guard for null Zod types
129
+ */
130
+ isNull(v) {
131
+ return v instanceof zod.ZodNull;
132
+ }
133
+ /**
134
+ * Type guard for array Zod types
135
+ */
136
+ isArr(v) {
137
+ return v instanceof zod.ZodArray;
138
+ }
139
+ /**
140
+ * Type guard for union Zod types
141
+ */
142
+ isUnion(v) {
143
+ return v instanceof zod.ZodUnion;
144
+ }
145
+ /**
146
+ * Type guard for string Zod types
147
+ */
148
+ isString(v) {
149
+ return v instanceof zod.ZodString;
150
+ }
151
+ /**
152
+ * Type guard for number Zod types
153
+ */
154
+ isNumber(v) {
155
+ return v instanceof zod.ZodNumber;
156
+ }
157
+ /**
158
+ * Type guard for date Zod types
159
+ */
160
+ isDate(v) {
161
+ return v instanceof zod.ZodDate;
162
+ }
163
+ /**
164
+ * Type guard for default Zod types
165
+ */
166
+ isDefault(v) {
167
+ return v instanceof zod.ZodDefault;
168
+ }
169
+ /**
170
+ * Determines whether this compatibility layer should be applied for the current model.
171
+ *
172
+ * @returns True if this compatibility layer should be used, false otherwise
173
+ * @abstract
174
+ */
175
+ shouldApply() {
176
+ return this.parent.shouldApply();
177
+ }
178
+ /**
179
+ * Returns the JSON Schema target format for this provider.
180
+ *
181
+ * @returns The schema target format, or undefined to use the default 'jsonSchema7'
182
+ * @abstract
183
+ */
184
+ getSchemaTarget() {
185
+ return this.parent.getSchemaTarget();
186
+ }
187
+ /**
188
+ * Processes a specific Zod type according to the provider's requirements.
189
+ *
190
+ * @param value - The Zod type to process
191
+ * @returns The processed Zod type
192
+ * @abstract
193
+ */
194
+ processZodType(value) {
195
+ return this.parent.processZodType(value);
196
+ }
197
+ /**
198
+ * Default handler for Zod object types. Recursively processes all properties in the object.
199
+ *
200
+ * @param value - The Zod object to process
201
+ * @returns The processed Zod object
202
+ */
203
+ defaultZodObjectHandler(value, options = { passthrough: true }) {
204
+ const processedShape = Object.entries(value.shape).reduce((acc, [key, propValue]) => {
205
+ acc[key] = this.processZodType(propValue);
206
+ return acc;
207
+ }, {});
208
+ let result = zod.z.object(processedShape);
209
+ if (value._def.unknownKeys === "strict") {
210
+ result = result.strict();
211
+ }
212
+ if (value._def.catchall && !(value._def.catchall instanceof zod.z.ZodNever)) {
213
+ result = result.catchall(value._def.catchall);
214
+ }
215
+ if (value.description) {
216
+ result = result.describe(value.description);
217
+ }
218
+ if (options.passthrough && value._def.unknownKeys === "passthrough") {
219
+ result = result.passthrough();
220
+ }
221
+ return result;
222
+ }
223
+ /**
224
+ * Merges validation constraints into a parameter description.
225
+ *
226
+ * This helper method converts validation constraints that may not be supported
227
+ * by a provider into human-readable descriptions.
228
+ *
229
+ * @param description - The existing parameter description
230
+ * @param constraints - The validation constraints to merge
231
+ * @returns The updated description with constraints, or undefined if no constraints
232
+ */
233
+ mergeParameterDescription(description, constraints) {
234
+ if (Object.keys(constraints).length > 0) {
235
+ return (description ? description + "\n" : "") + JSON.stringify(constraints);
236
+ } else {
237
+ return description;
238
+ }
239
+ }
240
+ /**
241
+ * Default handler for unsupported Zod types. Throws an error for specified unsupported types.
242
+ *
243
+ * @param value - The Zod type to check
244
+ * @param throwOnTypes - Array of type names to throw errors for
245
+ * @returns The original value if not in the throw list
246
+ * @throws Error if the type is in the unsupported list
247
+ */
248
+ defaultUnsupportedZodTypeHandler(value, throwOnTypes = UNSUPPORTED_ZOD_TYPES) {
249
+ if (throwOnTypes.includes(value._def?.typeName)) {
250
+ throw new Error(`${this.model.modelId} does not support zod type: ${value._def?.typeName}`);
251
+ }
252
+ return value;
253
+ }
254
+ /**
255
+ * Default handler for Zod array types. Processes array constraints according to provider support.
256
+ *
257
+ * @param value - The Zod array to process
258
+ * @param handleChecks - Array constraints to convert to descriptions vs keep as validation
259
+ * @returns The processed Zod array
260
+ */
261
+ defaultZodArrayHandler(value, handleChecks = ALL_ARRAY_CHECKS) {
262
+ const zodArrayDef = value._def;
263
+ const processedType = this.processZodType(zodArrayDef.type);
264
+ let result = zod.z.array(processedType);
265
+ const constraints = {};
266
+ if (zodArrayDef.minLength?.value !== void 0) {
267
+ if (handleChecks.includes("min")) {
268
+ constraints.minLength = zodArrayDef.minLength.value;
269
+ } else {
270
+ result = result.min(zodArrayDef.minLength.value);
271
+ }
272
+ }
273
+ if (zodArrayDef.maxLength?.value !== void 0) {
274
+ if (handleChecks.includes("max")) {
275
+ constraints.maxLength = zodArrayDef.maxLength.value;
276
+ } else {
277
+ result = result.max(zodArrayDef.maxLength.value);
278
+ }
279
+ }
280
+ if (zodArrayDef.exactLength?.value !== void 0) {
281
+ if (handleChecks.includes("length")) {
282
+ constraints.exactLength = zodArrayDef.exactLength.value;
283
+ } else {
284
+ result = result.length(zodArrayDef.exactLength.value);
285
+ }
286
+ }
287
+ const description = this.mergeParameterDescription(value.description, constraints);
288
+ if (description) {
289
+ result = result.describe(description);
290
+ }
291
+ return result;
292
+ }
293
+ /**
294
+ * Default handler for Zod union types. Processes all union options.
295
+ *
296
+ * @param value - The Zod union to process
297
+ * @returns The processed Zod union
298
+ * @throws Error if union has fewer than 2 options
299
+ */
300
+ defaultZodUnionHandler(value) {
301
+ const processedOptions = value._def.options.map((option) => this.processZodType(option));
302
+ if (processedOptions.length < 2) throw new Error("Union must have at least 2 options");
303
+ let result = zod.z.union(processedOptions);
304
+ if (value.description) {
305
+ result = result.describe(value.description);
306
+ }
307
+ return result;
308
+ }
309
+ /**
310
+ * Default handler for Zod string types. Processes string validation constraints.
311
+ *
312
+ * @param value - The Zod string to process
313
+ * @param handleChecks - String constraints to convert to descriptions vs keep as validation
314
+ * @returns The processed Zod string
315
+ */
316
+ defaultZodStringHandler(value, handleChecks = ALL_STRING_CHECKS) {
317
+ const constraints = {};
318
+ const checks = value._def.checks || [];
319
+ const newChecks = [];
320
+ for (const check of checks) {
321
+ if ("kind" in check) {
322
+ if (handleChecks.includes(check.kind)) {
323
+ switch (check.kind) {
324
+ case "regex": {
325
+ constraints.regex = {
326
+ pattern: check.regex.source,
327
+ flags: check.regex.flags
328
+ };
329
+ break;
330
+ }
331
+ case "emoji": {
332
+ constraints.emoji = true;
333
+ break;
334
+ }
335
+ case "email": {
336
+ constraints.email = true;
337
+ break;
338
+ }
339
+ case "url": {
340
+ constraints.url = true;
341
+ break;
342
+ }
343
+ case "uuid": {
344
+ constraints.uuid = true;
345
+ break;
346
+ }
347
+ case "cuid": {
348
+ constraints.cuid = true;
349
+ break;
350
+ }
351
+ case "min": {
352
+ constraints.minLength = check.value;
353
+ break;
354
+ }
355
+ case "max": {
356
+ constraints.maxLength = check.value;
357
+ break;
358
+ }
359
+ }
360
+ } else {
361
+ newChecks.push(check);
362
+ }
363
+ }
364
+ }
365
+ let result = zod.z.string();
366
+ for (const check of newChecks) {
367
+ result = result._addCheck(check);
368
+ }
369
+ const description = this.mergeParameterDescription(value.description, constraints);
370
+ if (description) {
371
+ result = result.describe(description);
372
+ }
373
+ return result;
374
+ }
375
+ /**
376
+ * Default handler for Zod number types. Processes number validation constraints.
377
+ *
378
+ * @param value - The Zod number to process
379
+ * @param handleChecks - Number constraints to convert to descriptions vs keep as validation
380
+ * @returns The processed Zod number
381
+ */
382
+ defaultZodNumberHandler(value, handleChecks = ALL_NUMBER_CHECKS) {
383
+ const constraints = {};
384
+ const checks = value._def.checks || [];
385
+ const newChecks = [];
386
+ for (const check of checks) {
387
+ if ("kind" in check) {
388
+ if (handleChecks.includes(check.kind)) {
389
+ switch (check.kind) {
390
+ case "min":
391
+ if (check.inclusive) {
392
+ constraints.gte = check.value;
393
+ } else {
394
+ constraints.gt = check.value;
395
+ }
396
+ break;
397
+ case "max":
398
+ if (check.inclusive) {
399
+ constraints.lte = check.value;
400
+ } else {
401
+ constraints.lt = check.value;
402
+ }
403
+ break;
404
+ case "multipleOf": {
405
+ constraints.multipleOf = check.value;
406
+ break;
407
+ }
408
+ }
409
+ } else {
410
+ newChecks.push(check);
411
+ }
412
+ }
413
+ }
414
+ let result = zod.z.number();
415
+ for (const check of newChecks) {
416
+ switch (check.kind) {
417
+ case "int":
418
+ result = result.int();
419
+ break;
420
+ case "finite":
421
+ result = result.finite();
422
+ break;
423
+ default:
424
+ result = result._addCheck(check);
425
+ }
426
+ }
427
+ const description = this.mergeParameterDescription(value.description, constraints);
428
+ if (description) {
429
+ result = result.describe(description);
430
+ }
431
+ return result;
432
+ }
433
+ /**
434
+ * Default handler for Zod date types. Converts dates to ISO strings with constraint descriptions.
435
+ *
436
+ * @param value - The Zod date to process
437
+ * @returns A Zod string schema representing the date in ISO format
438
+ */
439
+ defaultZodDateHandler(value) {
440
+ const constraints = {};
441
+ const checks = value._def.checks || [];
442
+ for (const check of checks) {
443
+ if ("kind" in check) {
444
+ switch (check.kind) {
445
+ case "min":
446
+ const minDate = new Date(check.value);
447
+ if (!isNaN(minDate.getTime())) {
448
+ constraints.minDate = minDate.toISOString();
449
+ }
450
+ break;
451
+ case "max":
452
+ const maxDate = new Date(check.value);
453
+ if (!isNaN(maxDate.getTime())) {
454
+ constraints.maxDate = maxDate.toISOString();
455
+ }
456
+ break;
457
+ }
458
+ }
459
+ }
460
+ constraints.dateFormat = "date-time";
461
+ let result = zod.z.string().describe("date-time");
462
+ const description = this.mergeParameterDescription(value.description, constraints);
463
+ if (description) {
464
+ result = result.describe(description);
465
+ }
466
+ return result;
467
+ }
468
+ /**
469
+ * Default handler for Zod optional types. Processes the inner type and maintains optionality.
470
+ *
471
+ * @param value - The Zod optional to process
472
+ * @param handleTypes - Types that should be processed vs passed through
473
+ * @returns The processed Zod optional
474
+ */
475
+ defaultZodOptionalHandler(value, handleTypes = SUPPORTED_ZOD_TYPES) {
476
+ if (handleTypes.includes(value._def.innerType._def.typeName)) {
477
+ return this.processZodType(value._def.innerType).optional();
478
+ } else {
479
+ return value;
480
+ }
481
+ }
482
+ /**
483
+ * Processes a Zod object schema and converts it to an AI SDK Schema.
484
+ *
485
+ * @param zodSchema - The Zod object schema to process
486
+ * @returns An AI SDK Schema with provider-specific compatibility applied
487
+ */
488
+ processToAISDKSchema(zodSchema) {
489
+ const processedSchema = this.processZodType(zodSchema);
490
+ return convertZodSchemaToAISDKSchema(processedSchema, this.getSchemaTarget());
491
+ }
492
+ /**
493
+ * Processes a Zod object schema and converts it to a JSON Schema.
494
+ *
495
+ * @param zodSchema - The Zod object schema to process
496
+ * @returns A JSONSchema7 object with provider-specific compatibility applied
497
+ */
498
+ processToJSONSchema(zodSchema) {
499
+ return this.processToAISDKSchema(zodSchema).jsonSchema;
500
+ }
501
+ };
502
+ var ALL_STRING_CHECKS2 = [
503
+ "regex",
504
+ "emoji",
505
+ "email",
506
+ "url",
507
+ "uuid",
508
+ "cuid",
509
+ "min_length",
510
+ "max_length",
511
+ "string_format"
512
+ ];
513
+ var ALL_NUMBER_CHECKS2 = ["greater_than", "less_than", "multiple_of"];
514
+ var ALL_ARRAY_CHECKS2 = ["min", "max", "length"];
515
+ var UNSUPPORTED_ZOD_TYPES2 = ["ZodIntersection", "ZodNever", "ZodNull", "ZodTuple", "ZodUndefined"];
516
+ var SUPPORTED_ZOD_TYPES2 = [
517
+ "ZodObject",
518
+ "ZodArray",
519
+ "ZodUnion",
520
+ "ZodString",
521
+ "ZodNumber",
522
+ "ZodDate",
523
+ "ZodAny",
524
+ "ZodDefault"
525
+ ];
526
+ var SchemaCompatLayer2 = class {
527
+ model;
528
+ parent;
529
+ /**
530
+ * Creates a new schema compatibility instance.
531
+ *
532
+ * @param model - The language model this compatibility layer applies to
533
+ */
534
+ constructor(model, parent) {
535
+ this.model = model;
536
+ this.parent = parent;
537
+ }
538
+ /**
539
+ * Gets the language model associated with this compatibility layer.
540
+ *
541
+ * @returns The language model instance
542
+ */
543
+ getModel() {
544
+ return this.model;
545
+ }
546
+ getUnsupportedZodTypes() {
547
+ return UNSUPPORTED_ZOD_TYPES2;
548
+ }
549
+ /**
550
+ * Type guard for optional Zod types
551
+ */
552
+ isOptional(v) {
553
+ return v instanceof v4.ZodOptional;
554
+ }
555
+ /**
556
+ * Type guard for object Zod types
557
+ */
558
+ isObj(v) {
559
+ return v instanceof v4.ZodObject;
560
+ }
561
+ /**
562
+ * Type guard for null Zod types
563
+ */
564
+ isNull(v) {
565
+ return v instanceof v4.ZodNull;
566
+ }
567
+ /**
568
+ * Type guard for array Zod types
569
+ */
570
+ isArr(v) {
571
+ return v instanceof v4.ZodArray;
572
+ }
573
+ /**
574
+ * Type guard for union Zod types
575
+ */
576
+ isUnion(v) {
577
+ return v instanceof v4.ZodUnion;
578
+ }
579
+ /**
580
+ * Type guard for string Zod types
581
+ */
582
+ isString(v) {
583
+ return v instanceof v4.ZodString;
584
+ }
585
+ /**
586
+ * Type guard for number Zod types
587
+ */
588
+ isNumber(v) {
589
+ return v instanceof v4.ZodNumber;
590
+ }
591
+ /**
592
+ * Type guard for date Zod types
593
+ */
594
+ isDate(v) {
595
+ return v instanceof v4.ZodDate;
596
+ }
597
+ /**
598
+ * Type guard for default Zod types
599
+ */
600
+ isDefault(v) {
601
+ return v instanceof v4.ZodDefault;
602
+ }
603
+ /**
604
+ * Determines whether this compatibility layer should be applied for the current model.
605
+ *
606
+ * @returns True if this compatibility layer should be used, false otherwise
607
+ * @abstract
608
+ */
609
+ shouldApply() {
610
+ return this.parent.shouldApply();
611
+ }
612
+ /**
613
+ * Returns the JSON Schema target format for this provider.
614
+ *
615
+ * @returns The schema target format, or undefined to use the default 'jsonSchema7'
616
+ * @abstract
617
+ */
618
+ getSchemaTarget() {
619
+ return this.parent.getSchemaTarget();
620
+ }
621
+ /**
622
+ * Processes a specific Zod type according to the provider's requirements.
623
+ *
624
+ * @param value - The Zod type to process
625
+ * @returns The processed Zod type
626
+ * @abstract
627
+ */
628
+ processZodType(value) {
629
+ return this.parent.processZodType(value);
630
+ }
631
+ /**
632
+ * Default handler for Zod object types. Recursively processes all properties in the object.
633
+ *
634
+ * @param value - The Zod object to process
635
+ * @returns The processed Zod object
636
+ */
637
+ defaultZodObjectHandler(value, options = { passthrough: true }) {
638
+ const processedShape = Object.entries(value.shape).reduce((acc, [key, propValue]) => {
639
+ acc[key] = this.processZodType(propValue);
640
+ return acc;
641
+ }, {});
642
+ let result = v4.z.object(processedShape);
643
+ if (value._zod.def.catchall instanceof v4.z.ZodNever) {
644
+ result = v4.z.strictObject(processedShape);
645
+ }
646
+ if (value._zod.def.catchall && !(value._zod.def.catchall instanceof v4.z.ZodNever)) {
647
+ result = result.catchall(value._zod.def.catchall);
648
+ }
649
+ if (value.description) {
650
+ result = result.describe(value.description);
651
+ }
652
+ if (options.passthrough && value._zod.def.catchall instanceof v4.z.ZodUnknown) {
653
+ result = v4.z.looseObject(processedShape);
654
+ }
655
+ return result;
656
+ }
657
+ /**
658
+ * Merges validation constraints into a parameter description.
659
+ *
660
+ * This helper method converts validation constraints that may not be supported
661
+ * by a provider into human-readable descriptions.
662
+ *
663
+ * @param description - The existing parameter description
664
+ * @param constraints - The validation constraints to merge
665
+ * @returns The updated description with constraints, or undefined if no constraints
666
+ */
667
+ mergeParameterDescription(description, constraints) {
668
+ if (Object.keys(constraints).length > 0) {
669
+ return (description ? description + "\n" : "") + JSON.stringify(constraints);
670
+ } else {
671
+ return description;
672
+ }
673
+ }
674
+ /**
675
+ * Default handler for unsupported Zod types. Throws an error for specified unsupported types.
676
+ *
677
+ * @param value - The Zod type to check
678
+ * @param throwOnTypes - Array of type names to throw errors for
679
+ * @returns The original value if not in the throw list
680
+ * @throws Error if the type is in the unsupported list
681
+ */
682
+ defaultUnsupportedZodTypeHandler(value, throwOnTypes = UNSUPPORTED_ZOD_TYPES2) {
683
+ if (throwOnTypes.includes(value.constructor.name)) {
684
+ throw new Error(`${this.model.modelId} does not support zod type: ${value.constructor.name}`);
685
+ }
686
+ return value;
687
+ }
688
+ /**
689
+ * Default handler for Zod array types. Processes array constraints according to provider support.
690
+ *
691
+ * @param value - The Zod array to process
692
+ * @param handleChecks - Array constraints to convert to descriptions vs keep as validation
693
+ * @returns The processed Zod array
694
+ */
695
+ defaultZodArrayHandler(value, handleChecks = ALL_ARRAY_CHECKS2) {
696
+ const zodArrayDef = value._zod.def;
697
+ const processedType = this.processZodType(zodArrayDef.element);
698
+ let result = v4.z.array(processedType);
699
+ const constraints = {};
700
+ if (zodArrayDef.checks) {
701
+ for (const check of zodArrayDef.checks) {
702
+ if (check._zod.def.check === "min_length") {
703
+ if (handleChecks.includes("min")) {
704
+ constraints.minLength = check._zod.def.minimum;
705
+ } else {
706
+ result = result.min(check._zod.def.minimum);
707
+ }
708
+ }
709
+ if (check._zod.def.check === "max_length") {
710
+ if (handleChecks.includes("max")) {
711
+ constraints.maxLength = check._zod.def.maximum;
712
+ } else {
713
+ result = result.max(check._zod.def.maximum);
714
+ }
715
+ }
716
+ if (check._zod.def.check === "length_equals") {
717
+ if (handleChecks.includes("length")) {
718
+ constraints.exactLength = check._zod.def.length;
719
+ } else {
720
+ result = result.length(check._zod.def.length);
721
+ }
722
+ }
723
+ }
724
+ }
725
+ const metaDescription = value.meta()?.description;
726
+ const legacyDescription = value.description;
727
+ const description = this.mergeParameterDescription(metaDescription || legacyDescription, constraints);
728
+ if (description) {
729
+ result = result.describe(description);
730
+ }
731
+ return result;
732
+ }
733
+ /**
734
+ * Default handler for Zod union types. Processes all union options.
735
+ *
736
+ * @param value - The Zod union to process
737
+ * @returns The processed Zod union
738
+ * @throws Error if union has fewer than 2 options
739
+ */
740
+ defaultZodUnionHandler(value) {
741
+ const processedOptions = value._zod.def.options.map((option) => this.processZodType(option));
742
+ if (processedOptions.length < 2) throw new Error("Union must have at least 2 options");
743
+ let result = v4.z.union(processedOptions);
744
+ if (value.description) {
745
+ result = result.describe(value.description);
746
+ }
747
+ return result;
748
+ }
749
+ /**
750
+ * Default handler for Zod string types. Processes string validation constraints.
751
+ *
752
+ * @param value - The Zod string to process
753
+ * @param handleChecks - String constraints to convert to descriptions vs keep as validation
754
+ * @returns The processed Zod string
755
+ */
756
+ defaultZodStringHandler(value, handleChecks = ALL_STRING_CHECKS2) {
757
+ const constraints = {};
758
+ const checks = value._zod.def.checks || [];
759
+ const newChecks = [];
760
+ if (checks) {
761
+ for (const check of checks) {
762
+ if (handleChecks.includes(check._zod.def.check)) {
763
+ switch (check._zod.def.check) {
764
+ case "min_length":
765
+ constraints.minLength = check._zod.def.minimum;
766
+ break;
767
+ case "max_length":
768
+ constraints.maxLength = check._zod.def.maximum;
769
+ break;
770
+ case "string_format":
771
+ {
772
+ switch (check._zod.def.format) {
773
+ case "email":
774
+ constraints.email = true;
775
+ break;
776
+ case "url":
777
+ constraints.url = true;
778
+ break;
779
+ case "emoji":
780
+ constraints.emoji = true;
781
+ break;
782
+ case "uuid":
783
+ constraints.uuid = true;
784
+ break;
785
+ case "cuid":
786
+ constraints.cuid = true;
787
+ break;
788
+ case "regex":
789
+ constraints.regex = {
790
+ // @ts-expect-error - fix later
791
+ pattern: check._zod.def.pattern,
792
+ // @ts-expect-error - fix later
793
+ flags: check._zod.def.flags
794
+ };
795
+ break;
796
+ }
797
+ }
798
+ break;
799
+ }
800
+ } else {
801
+ newChecks.push(check);
802
+ }
803
+ }
804
+ }
805
+ let result = v4.z.string();
806
+ for (const check of newChecks) {
807
+ result = result.check(check);
808
+ }
809
+ const metaDescription = value.meta()?.description;
810
+ const legacyDescription = value.description;
811
+ const description = this.mergeParameterDescription(metaDescription || legacyDescription, constraints);
812
+ if (description) {
813
+ result = result.describe(description);
814
+ }
815
+ return result;
816
+ }
817
+ /**
818
+ * Default handler for Zod number types. Processes number validation constraints.
819
+ *
820
+ * @param value - The Zod number to process
821
+ * @param handleChecks - Number constraints to convert to descriptions vs keep as validation
822
+ * @returns The processed Zod number
823
+ */
824
+ defaultZodNumberHandler(value, handleChecks = ALL_NUMBER_CHECKS2) {
825
+ const constraints = {};
826
+ const checks = value._zod.def.checks || [];
827
+ const newChecks = [];
828
+ if (checks) {
829
+ for (const check of checks) {
830
+ if (handleChecks.includes(check._zod.def.check)) {
831
+ switch (check._zod.def.check) {
832
+ case "greater_than":
833
+ if (check._zod.def.inclusive) {
834
+ constraints.gte = check._zod.def.value;
835
+ } else {
836
+ constraints.gt = check._zod.def.value;
837
+ }
838
+ break;
839
+ case "less_than":
840
+ if (check._zod.def.inclusive) {
841
+ constraints.lte = check._zod.def.value;
842
+ } else {
843
+ constraints.lt = check._zod.def.value;
844
+ }
845
+ break;
846
+ case "multiple_of": {
847
+ constraints.multipleOf = check._zod.def.value;
848
+ break;
849
+ }
850
+ }
851
+ } else {
852
+ newChecks.push(check);
853
+ }
854
+ }
855
+ }
856
+ let result = v4.z.number();
857
+ for (const check of newChecks) {
858
+ switch (check._zod.def.check) {
859
+ case "number_format": {
860
+ switch (check._zod.def.format) {
861
+ case "safeint":
862
+ result = result.int();
863
+ break;
864
+ }
865
+ break;
866
+ }
867
+ default:
868
+ result = result.check(check);
869
+ }
870
+ }
871
+ const description = this.mergeParameterDescription(value.description, constraints);
872
+ if (description) {
873
+ result = result.describe(description);
874
+ }
875
+ return result;
876
+ }
877
+ /**
878
+ * Default handler for Zod date types. Converts dates to ISO strings with constraint descriptions.
879
+ *
880
+ * @param value - The Zod date to process
881
+ * @returns A Zod string schema representing the date in ISO format
882
+ */
883
+ defaultZodDateHandler(value) {
884
+ const constraints = {};
885
+ const checks = value._zod.def.checks || [];
886
+ if (checks) {
887
+ for (const check of checks) {
888
+ switch (check._zod.def.check) {
889
+ case "less_than":
890
+ const minDate = new Date(check._zod.def.value);
891
+ if (!isNaN(minDate.getTime())) {
892
+ constraints.minDate = minDate.toISOString();
893
+ }
894
+ break;
895
+ case "greater_than":
896
+ const maxDate = new Date(check._zod.def.value);
897
+ if (!isNaN(maxDate.getTime())) {
898
+ constraints.maxDate = maxDate.toISOString();
899
+ }
900
+ break;
901
+ }
902
+ }
903
+ }
904
+ constraints.dateFormat = "date-time";
905
+ let result = v4.z.string().describe("date-time");
906
+ const description = this.mergeParameterDescription(value.description, constraints);
907
+ if (description) {
908
+ result = result.describe(description);
909
+ }
910
+ return result;
911
+ }
912
+ /**
913
+ * Default handler for Zod optional types. Processes the inner type and maintains optionality.
914
+ *
915
+ * @param value - The Zod optional to process
916
+ * @param handleTypes - Types that should be processed vs passed through
917
+ * @returns The processed Zod optional
918
+ */
919
+ defaultZodOptionalHandler(value, handleTypes = SUPPORTED_ZOD_TYPES2) {
920
+ if (handleTypes.includes(value.constructor.name)) {
921
+ return this.processZodType(value._zod.def.innerType).optional();
922
+ } else {
923
+ return value;
924
+ }
925
+ }
926
+ /**
927
+ * Processes a Zod object schema and converts it to an AI SDK Schema.
928
+ *
929
+ * @param zodSchema - The Zod object schema to process
930
+ * @returns An AI SDK Schema with provider-specific compatibility applied
931
+ */
932
+ processToAISDKSchema(zodSchema) {
933
+ const processedSchema = this.processZodType(zodSchema);
934
+ return convertZodSchemaToAISDKSchema(processedSchema, this.getSchemaTarget());
935
+ }
936
+ /**
937
+ * Processes a Zod object schema and converts it to a JSON Schema.
938
+ *
939
+ * @param zodSchema - The Zod object schema to process
940
+ * @returns A JSONSchema7 object with provider-specific compatibility applied
941
+ */
942
+ processToJSONSchema(zodSchema) {
943
+ return this.processToAISDKSchema(zodSchema).jsonSchema;
944
+ }
945
+ };
946
+
947
+ // src/schema-compatibility.ts
948
+ var SchemaCompatLayer3 = class {
949
+ model;
950
+ v3Layer;
951
+ v4Layer;
952
+ /**
953
+ * Creates a new schema compatibility instance.
954
+ *
955
+ * @param model - The language model this compatibility layer applies to
956
+ */
957
+ constructor(model) {
958
+ this.model = model;
959
+ this.v3Layer = new SchemaCompatLayer(model, this);
960
+ this.v4Layer = new SchemaCompatLayer2(model, this);
961
+ }
962
+ /**
963
+ * Gets the language model associated with this compatibility layer.
964
+ *
965
+ * @returns The language model instance
966
+ */
967
+ getModel() {
968
+ return this.model;
969
+ }
970
+ getUnsupportedZodTypes(v) {
971
+ if ("_zod" in v) {
972
+ return this.v4Layer.getUnsupportedZodTypes();
973
+ } else {
974
+ return this.v3Layer.getUnsupportedZodTypes();
975
+ }
976
+ }
977
+ isOptional(v) {
978
+ if ("_zod" in v) {
979
+ return this.v4Layer.isOptional(v);
980
+ } else {
981
+ return this.v3Layer.isOptional(v);
982
+ }
983
+ }
984
+ isObj(v) {
985
+ if ("_zod" in v) {
986
+ return this.v4Layer.isObj(v);
987
+ } else {
988
+ return this.v3Layer.isObj(v);
989
+ }
990
+ }
991
+ isNull(v) {
992
+ if ("_zod" in v) {
993
+ return this.v4Layer.isNull(v);
994
+ } else {
995
+ return this.v3Layer.isNull(v);
996
+ }
997
+ }
998
+ isArr(v) {
999
+ if ("_zod" in v) {
1000
+ return this.v4Layer.isArr(v);
1001
+ } else {
1002
+ return this.v3Layer.isArr(v);
1003
+ }
1004
+ }
1005
+ isUnion(v) {
1006
+ if ("_zod" in v) {
1007
+ return this.v4Layer.isUnion(v);
1008
+ } else {
1009
+ return this.v3Layer.isUnion(v);
1010
+ }
1011
+ }
1012
+ isString(v) {
1013
+ if ("_zod" in v) {
1014
+ return this.v4Layer.isString(v);
1015
+ } else {
1016
+ return this.v3Layer.isString(v);
1017
+ }
1018
+ }
1019
+ isNumber(v) {
1020
+ if ("_zod" in v) {
1021
+ return this.v4Layer.isNumber(v);
1022
+ } else {
1023
+ return this.v3Layer.isNumber(v);
1024
+ }
1025
+ }
1026
+ isDate(v) {
1027
+ if ("_zod" in v) {
1028
+ return this.v4Layer.isDate(v);
1029
+ } else {
1030
+ return this.v3Layer.isDate(v);
1031
+ }
1032
+ }
1033
+ isDefault(v) {
1034
+ if ("_zod" in v) {
1035
+ return this.v4Layer.isDefault(v);
1036
+ } else {
1037
+ return this.v3Layer.isDefault(v);
1038
+ }
1039
+ }
1040
+ defaultZodObjectHandler(value, options = { passthrough: true }) {
1041
+ if ("_zod" in value) {
1042
+ return this.v4Layer.defaultZodObjectHandler(value, options);
1043
+ } else {
1044
+ return this.v3Layer.defaultZodObjectHandler(value, options);
1045
+ }
1046
+ }
1047
+ /**
1048
+ * Merges validation constraints into a parameter description.
1049
+ *
1050
+ * This helper method converts validation constraints that may not be supported
1051
+ * by a provider into human-readable descriptions.
1052
+ *
1053
+ * @param description - The existing parameter description
1054
+ * @param constraints - The validation constraints to merge
1055
+ * @returns The updated description with constraints, or undefined if no constraints
1056
+ */
1057
+ mergeParameterDescription(description, constraints) {
1058
+ return this.v3Layer.mergeParameterDescription(description, constraints);
1059
+ }
1060
+ /**
1061
+ * Default handler for unsupported Zod types. Throws an error for specified unsupported types.
1062
+ *
1063
+ * @param value - The Zod type to check
1064
+ * @param throwOnTypes - Array of type names to throw errors for
1065
+ * @returns The original value if not in the throw list
1066
+ * @throws Error if the type is in the unsupported list
1067
+ */
1068
+ defaultUnsupportedZodTypeHandler(value, throwOnTypes) {
1069
+ if ("_zod" in value) {
1070
+ return this.v4Layer.defaultUnsupportedZodTypeHandler(
1071
+ // @ts-expect-error - fix later
1072
+ value,
1073
+ throwOnTypes ?? UNSUPPORTED_ZOD_TYPES2
1074
+ );
1075
+ } else {
1076
+ return this.v3Layer.defaultUnsupportedZodTypeHandler(
1077
+ value,
1078
+ throwOnTypes ?? UNSUPPORTED_ZOD_TYPES
1079
+ );
1080
+ }
1081
+ }
1082
+ defaultZodArrayHandler(value, handleChecks = ALL_ARRAY_CHECKS) {
1083
+ if ("_zod" in value) {
1084
+ return this.v4Layer.defaultZodArrayHandler(value, handleChecks);
1085
+ } else {
1086
+ return this.v3Layer.defaultZodArrayHandler(value, handleChecks);
1087
+ }
1088
+ }
1089
+ defaultZodUnionHandler(value) {
1090
+ if ("_zod" in value) {
1091
+ return this.v4Layer.defaultZodUnionHandler(value);
1092
+ } else {
1093
+ return this.v3Layer.defaultZodUnionHandler(value);
1094
+ }
1095
+ }
1096
+ defaultZodStringHandler(value, handleChecks = ALL_STRING_CHECKS) {
1097
+ if ("_zod" in value) {
1098
+ return this.v4Layer.defaultZodStringHandler(value);
1099
+ } else {
1100
+ return this.v3Layer.defaultZodStringHandler(value, handleChecks);
1101
+ }
1102
+ }
1103
+ defaultZodNumberHandler(value, handleChecks = ALL_NUMBER_CHECKS) {
1104
+ if ("_zod" in value) {
1105
+ return this.v4Layer.defaultZodNumberHandler(value);
1106
+ } else {
1107
+ return this.v3Layer.defaultZodNumberHandler(value, handleChecks);
1108
+ }
1109
+ }
1110
+ defaultZodDateHandler(value) {
1111
+ if ("_zod" in value) {
1112
+ return this.v4Layer.defaultZodDateHandler(value);
1113
+ } else {
1114
+ return this.v3Layer.defaultZodDateHandler(value);
1115
+ }
1116
+ }
1117
+ defaultZodOptionalHandler(value, handleTypes) {
1118
+ if ("_zod" in value) {
1119
+ return this.v4Layer.defaultZodOptionalHandler(value, handleTypes ?? SUPPORTED_ZOD_TYPES2);
1120
+ } else {
1121
+ return this.v3Layer.defaultZodOptionalHandler(value, handleTypes ?? SUPPORTED_ZOD_TYPES);
1122
+ }
1123
+ }
1124
+ /**
1125
+ * Processes a Zod object schema and converts it to an AI SDK Schema.
1126
+ *
1127
+ * @param zodSchema - The Zod object schema to process
1128
+ * @returns An AI SDK Schema with provider-specific compatibility applied
1129
+ */
1130
+ processToAISDKSchema(zodSchema) {
1131
+ const processedSchema = this.processZodType(zodSchema);
1132
+ return convertZodSchemaToAISDKSchema(processedSchema, this.getSchemaTarget());
1133
+ }
1134
+ /**
1135
+ * Processes a Zod object schema and converts it to a JSON Schema.
1136
+ *
1137
+ * @param zodSchema - The Zod object schema to process
1138
+ * @returns A JSONSchema7 object with provider-specific compatibility applied
1139
+ */
1140
+ processToJSONSchema(zodSchema) {
1141
+ return this.processToAISDKSchema(zodSchema).jsonSchema;
1142
+ }
1143
+ };
1144
+
1145
+ // src/zodTypes.ts
1146
+ function isOptional2(z10) {
1147
+ return (v) => v instanceof z10["ZodOptional"];
1148
+ }
1149
+ function isObj2(z10) {
1150
+ return (v) => v instanceof z10["ZodObject"];
1151
+ }
1152
+ function isNull(z10) {
1153
+ return (v) => v instanceof z10["ZodNull"];
1154
+ }
1155
+ function isArr2(z10) {
1156
+ return (v) => v instanceof z10["ZodArray"];
1157
+ }
1158
+ function isUnion2(z10) {
1159
+ return (v) => v instanceof z10["ZodUnion"];
1160
+ }
1161
+ function isString2(z10) {
1162
+ return (v) => v instanceof z10["ZodString"];
1163
+ }
1164
+ function isNumber2(z10) {
1165
+ return (v) => v instanceof z10["ZodNumber"];
1166
+ }
1167
+ function isDate(z10) {
1168
+ return (v) => v instanceof z10["ZodDate"];
1169
+ }
1170
+ function isDefault(z10) {
1171
+ return (v) => v instanceof z10["ZodDefault"];
1172
+ }
1173
+
1174
+ // src/provider-compats/anthropic.ts
1175
+ var AnthropicSchemaCompatLayer = class extends SchemaCompatLayer3 {
1176
+ constructor(model) {
1177
+ super(model);
1178
+ }
1179
+ getSchemaTarget() {
1180
+ return "jsonSchema7";
1181
+ }
1182
+ shouldApply() {
1183
+ return this.getModel().modelId.includes("claude");
1184
+ }
1185
+ processZodType(value) {
1186
+ if (isOptional2(zod.z)(value)) {
1187
+ const handleTypes = [
1188
+ "ZodObject",
1189
+ "ZodArray",
1190
+ "ZodUnion",
1191
+ "ZodNever",
1192
+ "ZodUndefined",
1193
+ "ZodTuple"
1194
+ ];
1195
+ if (this.getModel().modelId.includes("claude-3.5-haiku")) handleTypes.push("ZodString");
1196
+ return this.defaultZodOptionalHandler(value, handleTypes);
1197
+ } else if (isObj2(zod.z)(value)) {
1198
+ return this.defaultZodObjectHandler(value);
1199
+ } else if (isArr2(zod.z)(value)) {
1200
+ return this.defaultZodArrayHandler(value, []);
1201
+ } else if (isUnion2(zod.z)(value)) {
1202
+ return this.defaultZodUnionHandler(value);
1203
+ } else if (isString2(zod.z)(value)) {
1204
+ if (this.getModel().modelId.includes("claude-3.5-haiku")) {
1205
+ return this.defaultZodStringHandler(value, ["max", "min"]);
1206
+ } else {
1207
+ return value;
1208
+ }
1209
+ }
1210
+ return this.defaultUnsupportedZodTypeHandler(value, [
1211
+ "ZodNever",
1212
+ "ZodTuple",
1213
+ "ZodUndefined"
1214
+ ]);
1215
+ }
1216
+ };
1217
+ var DeepSeekSchemaCompatLayer = class extends SchemaCompatLayer3 {
1218
+ constructor(model) {
1219
+ super(model);
1220
+ }
1221
+ getSchemaTarget() {
1222
+ return "jsonSchema7";
1223
+ }
1224
+ shouldApply() {
1225
+ return this.getModel().modelId.includes("deepseek") && !this.getModel().modelId.includes("r1");
1226
+ }
1227
+ processZodType(value) {
1228
+ if (isOptional2(zod.z)(value)) {
1229
+ return this.defaultZodOptionalHandler(value, ["ZodObject", "ZodArray", "ZodUnion", "ZodString", "ZodNumber"]);
1230
+ } else if (isObj2(zod.z)(value)) {
1231
+ return this.defaultZodObjectHandler(value);
1232
+ } else if (isArr2(zod.z)(value)) {
1233
+ return this.defaultZodArrayHandler(value, ["min", "max"]);
1234
+ } else if (isUnion2(zod.z)(value)) {
1235
+ return this.defaultZodUnionHandler(value);
1236
+ } else if (isString2(zod.z)(value)) {
1237
+ return this.defaultZodStringHandler(value);
1238
+ }
1239
+ return value;
1240
+ }
1241
+ };
1242
+ var GoogleSchemaCompatLayer = class extends SchemaCompatLayer3 {
1243
+ constructor(model) {
1244
+ super(model);
1245
+ }
1246
+ getSchemaTarget() {
1247
+ return "jsonSchema7";
1248
+ }
1249
+ shouldApply() {
1250
+ return this.getModel().provider.includes("google") || this.getModel().modelId.includes("google");
1251
+ }
1252
+ processZodType(value) {
1253
+ if (isOptional2(zod.z)(value)) {
1254
+ return this.defaultZodOptionalHandler(value, ["ZodObject", "ZodArray", "ZodUnion", "ZodString", "ZodNumber"]);
1255
+ } else if (isNull(zod.z)(value)) {
1256
+ return zod.z.any().refine((v) => v === null, { message: "must be null" }).describe(value.description || "must be null");
1257
+ } else if (isObj2(zod.z)(value)) {
1258
+ return this.defaultZodObjectHandler(value);
1259
+ } else if (isArr2(zod.z)(value)) {
1260
+ return this.defaultZodArrayHandler(value, []);
1261
+ } else if (isUnion2(zod.z)(value)) {
1262
+ return this.defaultZodUnionHandler(value);
1263
+ } else if (isString2(zod.z)(value)) {
1264
+ return this.defaultZodStringHandler(value);
1265
+ } else if (isNumber2(zod.z)(value)) {
1266
+ return this.defaultZodNumberHandler(value);
1267
+ }
1268
+ return this.defaultUnsupportedZodTypeHandler(value);
1269
+ }
1270
+ };
1271
+ var MetaSchemaCompatLayer = class extends SchemaCompatLayer3 {
1272
+ constructor(model) {
1273
+ super(model);
1274
+ }
1275
+ getSchemaTarget() {
1276
+ return "jsonSchema7";
1277
+ }
1278
+ shouldApply() {
1279
+ return this.getModel().modelId.includes("meta");
1280
+ }
1281
+ processZodType(value) {
1282
+ if (isOptional2(zod.z)(value)) {
1283
+ return this.defaultZodOptionalHandler(value, ["ZodObject", "ZodArray", "ZodUnion", "ZodString", "ZodNumber"]);
1284
+ } else if (isObj2(zod.z)(value)) {
1285
+ return this.defaultZodObjectHandler(value);
1286
+ } else if (isArr2(zod.z)(value)) {
1287
+ return this.defaultZodArrayHandler(value, ["min", "max"]);
1288
+ } else if (isUnion2(zod.z)(value)) {
1289
+ return this.defaultZodUnionHandler(value);
1290
+ } else if (isNumber2(zod.z)(value)) {
1291
+ return this.defaultZodNumberHandler(value);
1292
+ } else if (isString2(zod.z)(value)) {
1293
+ return this.defaultZodStringHandler(value);
1294
+ }
1295
+ return value;
1296
+ }
1297
+ };
1298
+ var OpenAISchemaCompatLayer = class extends SchemaCompatLayer3 {
1299
+ constructor(model) {
1300
+ super(model);
1301
+ }
1302
+ getSchemaTarget() {
1303
+ return `jsonSchema7`;
1304
+ }
1305
+ shouldApply() {
1306
+ if (!this.getModel().supportsStructuredOutputs && (this.getModel().provider.includes(`openai`) || this.getModel().modelId.includes(`openai`))) {
1307
+ return true;
1308
+ }
1309
+ return false;
1310
+ }
1311
+ processZodType(value) {
1312
+ if (isOptional2(zod.z)(value)) {
1313
+ return this.defaultZodOptionalHandler(value, [
1314
+ "ZodObject",
1315
+ "ZodArray",
1316
+ "ZodUnion",
1317
+ "ZodString",
1318
+ "ZodNever",
1319
+ "ZodUndefined",
1320
+ "ZodTuple"
1321
+ ]);
1322
+ } else if (isObj2(zod.z)(value)) {
1323
+ return this.defaultZodObjectHandler(value);
1324
+ } else if (isUnion2(zod.z)(value)) {
1325
+ return this.defaultZodUnionHandler(value);
1326
+ } else if (isArr2(zod.z)(value)) {
1327
+ return this.defaultZodArrayHandler(value);
1328
+ } else if (isString2(zod.z)(value)) {
1329
+ const model = this.getModel();
1330
+ const checks = ["emoji"];
1331
+ if (model.modelId.includes("gpt-4o-mini")) {
1332
+ return this.defaultZodStringHandler(value, ["emoji", "regex"]);
1333
+ }
1334
+ return this.defaultZodStringHandler(value, checks);
1335
+ }
1336
+ return this.defaultUnsupportedZodTypeHandler(value, [
1337
+ "ZodNever",
1338
+ "ZodUndefined",
1339
+ "ZodTuple"
1340
+ ]);
1341
+ }
1342
+ };
1343
+ var OpenAIReasoningSchemaCompatLayer = class extends SchemaCompatLayer3 {
1344
+ constructor(model) {
1345
+ super(model);
1346
+ }
1347
+ getSchemaTarget() {
1348
+ return `openApi3`;
1349
+ }
1350
+ isReasoningModel() {
1351
+ return this.getModel().modelId.includes(`o3`) || this.getModel().modelId.includes(`o4`) || this.getModel().modelId.includes(`o1`);
1352
+ }
1353
+ shouldApply() {
1354
+ if ((this.getModel().supportsStructuredOutputs || this.isReasoningModel()) && (this.getModel().provider.includes(`openai`) || this.getModel().modelId.includes(`openai`))) {
1355
+ return true;
1356
+ }
1357
+ return false;
1358
+ }
1359
+ processZodType(value) {
1360
+ if (isOptional2(zod.z)(value)) {
1361
+ const innerZodType = this.processZodType(value._def.innerType);
1362
+ return innerZodType.nullable();
1363
+ } else if (isObj2(zod.z)(value)) {
1364
+ return this.defaultZodObjectHandler(value, { passthrough: false });
1365
+ } else if (isArr2(zod.z)(value)) {
1366
+ return this.defaultZodArrayHandler(value);
1367
+ } else if (isUnion2(zod.z)(value)) {
1368
+ return this.defaultZodUnionHandler(value);
1369
+ } else if (isDefault(zod.z)(value)) {
1370
+ const defaultDef = value._def;
1371
+ const innerType = defaultDef.innerType;
1372
+ const defaultValue = defaultDef.defaultValue();
1373
+ const constraints = {};
1374
+ if (defaultValue !== void 0) {
1375
+ constraints.defaultValue = defaultValue;
1376
+ }
1377
+ const description = this.mergeParameterDescription(value.description, constraints);
1378
+ let result = this.processZodType(innerType);
1379
+ if (description) {
1380
+ result = result.describe(description);
1381
+ }
1382
+ return result;
1383
+ } else if (isNumber2(zod.z)(value)) {
1384
+ return this.defaultZodNumberHandler(value);
1385
+ } else if (isString2(zod.z)(value)) {
1386
+ return this.defaultZodStringHandler(value);
1387
+ } else if (isDate(zod.z)(value)) {
1388
+ return this.defaultZodDateHandler(value);
1389
+ } else if (value.constructor.name === "ZodAny") {
1390
+ return zod.z.string().describe(
1391
+ (value.description ?? "") + `
1392
+ Argument was an "any" type, but you (the LLM) do not support "any", so it was cast to a "string" type`
1393
+ );
1394
+ }
1395
+ return this.defaultUnsupportedZodTypeHandler(value);
1396
+ }
1397
+ };
1398
+
1399
+ exports.ALL_ARRAY_CHECKS = ALL_ARRAY_CHECKS;
1400
+ exports.ALL_NUMBER_CHECKS = ALL_NUMBER_CHECKS;
1401
+ exports.ALL_STRING_CHECKS = ALL_STRING_CHECKS;
1402
+ exports.ALL_ZOD_TYPES = ALL_ZOD_TYPES;
1403
+ exports.AnthropicSchemaCompatLayer = AnthropicSchemaCompatLayer;
1404
+ exports.DeepSeekSchemaCompatLayer = DeepSeekSchemaCompatLayer;
1405
+ exports.GoogleSchemaCompatLayer = GoogleSchemaCompatLayer;
1406
+ exports.MetaSchemaCompatLayer = MetaSchemaCompatLayer;
1407
+ exports.OpenAIReasoningSchemaCompatLayer = OpenAIReasoningSchemaCompatLayer;
1408
+ exports.OpenAISchemaCompatLayer = OpenAISchemaCompatLayer;
1409
+ exports.SUPPORTED_ZOD_TYPES = SUPPORTED_ZOD_TYPES;
1410
+ exports.SchemaCompatLayer = SchemaCompatLayer3;
1411
+ exports.SchemaCompatLayerV3 = SchemaCompatLayer;
1412
+ exports.SchemaCompatLayerV4 = SchemaCompatLayer2;
1413
+ exports.UNSUPPORTED_ZOD_TYPES = UNSUPPORTED_ZOD_TYPES;
1414
+ exports.applyCompatLayer = applyCompatLayer;
1415
+ exports.convertSchemaToZod = convertSchemaToZod;
1416
+ exports.convertZodSchemaToAISDKSchema = convertZodSchemaToAISDKSchema;
1417
+ exports.isArr = isArr;
1418
+ exports.isNumber = isNumber;
1419
+ exports.isObj = isObj;
1420
+ exports.isOptional = isOptional;
1421
+ exports.isString = isString;
1422
+ exports.isUnion = isUnion;
1423
+ //# sourceMappingURL=index.cjs.map
1424
+ //# sourceMappingURL=index.cjs.map