@mastra/schema-compat 0.10.2-alpha.2 → 0.10.2-alpha.3

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/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { z } from 'zod';
1
+ import { ZodOptional, ZodObject, ZodArray, ZodUnion, ZodString, ZodNumber, z, ZodDefault, ZodDate } from 'zod';
2
2
  import { jsonSchema } from 'ai';
3
3
  import { convertJsonSchemaToZod } from 'zod-from-json-schema';
4
4
  import { zodToJsonSchema } from 'zod-to-json-schema';
@@ -43,18 +43,9 @@ function applyCompatLayer({
43
43
  }) {
44
44
  let zodSchema;
45
45
  if (!isZodType(schema)) {
46
- const convertedSchema = convertSchemaToZod(schema);
47
- if (convertedSchema instanceof z.ZodObject) {
48
- zodSchema = convertedSchema;
49
- } else {
50
- zodSchema = z.object({ value: convertedSchema });
51
- }
46
+ zodSchema = convertSchemaToZod(schema);
52
47
  } else {
53
- if (schema instanceof z.ZodObject) {
54
- zodSchema = schema;
55
- } else {
56
- zodSchema = z.object({ value: schema });
57
- }
48
+ zodSchema = schema;
58
49
  }
59
50
  for (const compat of compatLayers) {
60
51
  if (compat.shouldApply()) {
@@ -78,6 +69,14 @@ var ALL_NUMBER_CHECKS = [
78
69
  "multipleOf"
79
70
  ];
80
71
  var ALL_ARRAY_CHECKS = ["min", "max", "length"];
72
+ var isOptional = (v) => v instanceof ZodOptional;
73
+ var isObj = (v) => v instanceof ZodObject;
74
+ var isArr = (v) => v instanceof ZodArray;
75
+ var isUnion = (v) => v instanceof ZodUnion;
76
+ var isString = (v) => v instanceof ZodString;
77
+ var isNumber = (v) => v instanceof ZodNumber;
78
+ var isDate = (v) => v instanceof ZodDate;
79
+ var isDefault = (v) => v instanceof ZodDefault;
81
80
  var UNSUPPORTED_ZOD_TYPES = ["ZodIntersection", "ZodNever", "ZodNull", "ZodTuple", "ZodUndefined"];
82
81
  var SUPPORTED_ZOD_TYPES = [
83
82
  "ZodObject",
@@ -108,25 +107,6 @@ var SchemaCompatLayer = class {
108
107
  getModel() {
109
108
  return this.model;
110
109
  }
111
- /**
112
- * Applies compatibility transformations to a Zod object schema.
113
- *
114
- * @param zodSchema - The Zod object schema to transform
115
- * @returns Object containing the transformed schema
116
- * @private
117
- */
118
- applyZodSchemaCompatibility(zodSchema) {
119
- const newSchema = z.object(
120
- Object.entries(zodSchema.shape || {}).reduce(
121
- (acc, [key, value]) => ({
122
- ...acc,
123
- [key]: this.processZodType(value)
124
- }),
125
- {}
126
- )
127
- );
128
- return { schema: newSchema };
129
- }
130
110
  /**
131
111
  * Default handler for Zod object types. Recursively processes all properties in the object.
132
112
  *
@@ -134,17 +114,17 @@ var SchemaCompatLayer = class {
134
114
  * @returns The processed Zod object
135
115
  */
136
116
  defaultZodObjectHandler(value) {
137
- const zodObject = value;
138
- const processedShape = Object.entries(zodObject.shape || {}).reduce(
139
- (acc, [key, propValue]) => {
140
- const typedPropValue = propValue;
141
- const processedValue = this.processZodType(typedPropValue);
142
- acc[key] = processedValue;
143
- return acc;
144
- },
145
- {}
146
- );
117
+ const processedShape = Object.entries(value.shape).reduce((acc, [key, propValue]) => {
118
+ acc[key] = this.processZodType(propValue);
119
+ return acc;
120
+ }, {});
147
121
  let result = z.object(processedShape);
122
+ if (value._def.unknownKeys === "strict") {
123
+ result = result.strict();
124
+ }
125
+ if (value._def.catchall && !(value._def.catchall instanceof z.ZodNever)) {
126
+ result = result.catchall(value._def.catchall);
127
+ }
148
128
  if (value.description) {
149
129
  result = result.describe(value.description);
150
130
  }
@@ -189,28 +169,30 @@ var SchemaCompatLayer = class {
189
169
  * @returns The processed Zod array
190
170
  */
191
171
  defaultZodArrayHandler(value, handleChecks = ALL_ARRAY_CHECKS) {
192
- const zodArray = value._def;
193
- const arrayType = zodArray.type;
194
- const constraints = {};
195
- if (zodArray.minLength?.value !== void 0 && handleChecks.includes("min")) {
196
- constraints.minLength = zodArray.minLength.value;
197
- }
198
- if (zodArray.maxLength?.value !== void 0 && handleChecks.includes("max")) {
199
- constraints.maxLength = zodArray.maxLength.value;
200
- }
201
- if (zodArray.exactLength?.value !== void 0 && handleChecks.includes("length")) {
202
- constraints.exactLength = zodArray.exactLength.value;
203
- }
204
- const processedType = arrayType._def.typeName === "ZodObject" ? this.processZodType(arrayType) : arrayType;
172
+ const zodArrayDef = value._def;
173
+ const processedType = this.processZodType(zodArrayDef.type);
205
174
  let result = z.array(processedType);
206
- if (zodArray.minLength?.value !== void 0 && !handleChecks.includes("min")) {
207
- result = result.min(zodArray.minLength.value);
175
+ const constraints = {};
176
+ if (zodArrayDef.minLength?.value !== void 0) {
177
+ if (handleChecks.includes("min")) {
178
+ constraints.minLength = zodArrayDef.minLength.value;
179
+ } else {
180
+ result = result.min(zodArrayDef.minLength.value);
181
+ }
208
182
  }
209
- if (zodArray.maxLength?.value !== void 0 && !handleChecks.includes("max")) {
210
- result = result.max(zodArray.maxLength.value);
183
+ if (zodArrayDef.maxLength?.value !== void 0) {
184
+ if (handleChecks.includes("max")) {
185
+ constraints.maxLength = zodArrayDef.maxLength.value;
186
+ } else {
187
+ result = result.max(zodArrayDef.maxLength.value);
188
+ }
211
189
  }
212
- if (zodArray.exactLength?.value !== void 0 && !handleChecks.includes("length")) {
213
- result = result.length(zodArray.exactLength.value);
190
+ if (zodArrayDef.exactLength?.value !== void 0) {
191
+ if (handleChecks.includes("length")) {
192
+ constraints.exactLength = zodArrayDef.exactLength.value;
193
+ } else {
194
+ result = result.length(zodArrayDef.exactLength.value);
195
+ }
214
196
  }
215
197
  const description = this.mergeParameterDescription(value.description, constraints);
216
198
  if (description) {
@@ -226,8 +208,7 @@ var SchemaCompatLayer = class {
226
208
  * @throws Error if union has fewer than 2 options
227
209
  */
228
210
  defaultZodUnionHandler(value) {
229
- const zodUnion = value;
230
- const processedOptions = zodUnion._def.options.map((option) => this.processZodType(option));
211
+ const processedOptions = value._def.options.map((option) => this.processZodType(option));
231
212
  if (processedOptions.length < 2) throw new Error("Union must have at least 2 options");
232
213
  let result = z.union(processedOptions);
233
214
  if (value.description) {
@@ -243,9 +224,8 @@ var SchemaCompatLayer = class {
243
224
  * @returns The processed Zod string
244
225
  */
245
226
  defaultZodStringHandler(value, handleChecks = ALL_STRING_CHECKS) {
246
- const zodString = value;
247
227
  const constraints = {};
248
- const checks = zodString._def.checks || [];
228
+ const checks = value._def.checks || [];
249
229
  const newChecks = [];
250
230
  for (const check of checks) {
251
231
  if ("kind" in check) {
@@ -310,9 +290,8 @@ var SchemaCompatLayer = class {
310
290
  * @returns The processed Zod number
311
291
  */
312
292
  defaultZodNumberHandler(value, handleChecks = ALL_NUMBER_CHECKS) {
313
- const zodNumber = value;
314
293
  const constraints = {};
315
- const checks = zodNumber._def.checks || [];
294
+ const checks = value._def.checks || [];
316
295
  const newChecks = [];
317
296
  for (const check of checks) {
318
297
  if ("kind" in check) {
@@ -368,9 +347,8 @@ var SchemaCompatLayer = class {
368
347
  * @returns A Zod string schema representing the date in ISO format
369
348
  */
370
349
  defaultZodDateHandler(value) {
371
- const zodDate = value;
372
350
  const constraints = {};
373
- const checks = zodDate._def.checks || [];
351
+ const checks = value._def.checks || [];
374
352
  for (const check of checks) {
375
353
  if ("kind" in check) {
376
354
  switch (check.kind) {
@@ -418,8 +396,8 @@ var SchemaCompatLayer = class {
418
396
  * @returns An AI SDK Schema with provider-specific compatibility applied
419
397
  */
420
398
  processToAISDKSchema(zodSchema) {
421
- const { schema } = this.applyZodSchemaCompatibility(zodSchema);
422
- return convertZodSchemaToAISDKSchema(schema, this.getSchemaTarget());
399
+ const processedSchema = this.processZodType(zodSchema);
400
+ return convertZodSchemaToAISDKSchema(processedSchema, this.getSchemaTarget());
423
401
  }
424
402
  /**
425
403
  * Processes a Zod object schema and converts it to a JSON Schema.
@@ -444,36 +422,28 @@ var AnthropicSchemaCompatLayer = class extends SchemaCompatLayer {
444
422
  return this.getModel().modelId.includes("claude");
445
423
  }
446
424
  processZodType(value) {
447
- switch (value._def.typeName) {
448
- case "ZodOptional":
449
- const handleTypes = ["ZodObject", "ZodArray", "ZodUnion", "ZodNever", "ZodUndefined"];
450
- if (this.getModel().modelId.includes("claude-3.5-haiku")) handleTypes.push("ZodString");
451
- if (this.getModel().modelId.includes("claude-3.7")) handleTypes.push("ZodTuple");
452
- return this.defaultZodOptionalHandler(value, handleTypes);
453
- case "ZodObject": {
454
- return this.defaultZodObjectHandler(value);
455
- }
456
- case "ZodArray": {
457
- return this.defaultZodArrayHandler(value, []);
458
- }
459
- case "ZodUnion": {
460
- return this.defaultZodUnionHandler(value);
461
- }
462
- // the claude-3.5-haiku model support these properties but the model doesn't respect them, but it respects them when they're
463
- // added to the tool description
464
- case "ZodString": {
465
- if (this.getModel().modelId.includes("claude-3.5-haiku")) {
466
- return this.defaultZodStringHandler(value, ["max", "min"]);
467
- } else {
468
- return value;
469
- }
425
+ if (isOptional(value)) {
426
+ const handleTypes = ["ZodObject", "ZodArray", "ZodUnion", "ZodNever", "ZodUndefined"];
427
+ if (this.getModel().modelId.includes("claude-3.5-haiku")) handleTypes.push("ZodString");
428
+ if (this.getModel().modelId.includes("claude-3.7")) handleTypes.push("ZodTuple");
429
+ return this.defaultZodOptionalHandler(value, handleTypes);
430
+ } else if (isObj(value)) {
431
+ return this.defaultZodObjectHandler(value);
432
+ } else if (isArr(value)) {
433
+ return this.defaultZodArrayHandler(value, []);
434
+ } else if (isUnion(value)) {
435
+ return this.defaultZodUnionHandler(value);
436
+ } else if (isString(value)) {
437
+ if (this.getModel().modelId.includes("claude-3.5-haiku")) {
438
+ return this.defaultZodStringHandler(value, ["max", "min"]);
439
+ } else {
440
+ return value;
470
441
  }
471
- default:
472
- if (this.getModel().modelId.includes("claude-3.7")) {
473
- return this.defaultUnsupportedZodTypeHandler(value, ["ZodNever", "ZodTuple", "ZodUndefined"]);
474
- } else {
475
- return this.defaultUnsupportedZodTypeHandler(value, ["ZodNever", "ZodUndefined"]);
476
- }
442
+ }
443
+ if (this.getModel().modelId.includes("claude-3.7")) {
444
+ return this.defaultUnsupportedZodTypeHandler(value, ["ZodNever", "ZodTuple", "ZodUndefined"]);
445
+ } else {
446
+ return this.defaultUnsupportedZodTypeHandler(value, ["ZodNever", "ZodUndefined"]);
477
447
  }
478
448
  }
479
449
  };
@@ -490,24 +460,18 @@ var DeepSeekSchemaCompatLayer = class extends SchemaCompatLayer {
490
460
  return this.getModel().modelId.includes("deepseek") && !this.getModel().modelId.includes("r1");
491
461
  }
492
462
  processZodType(value) {
493
- switch (value._def.typeName) {
494
- case "ZodOptional":
495
- return this.defaultZodOptionalHandler(value, ["ZodObject", "ZodArray", "ZodUnion", "ZodString", "ZodNumber"]);
496
- case "ZodObject": {
497
- return this.defaultZodObjectHandler(value);
498
- }
499
- case "ZodArray": {
500
- return this.defaultZodArrayHandler(value, ["min", "max"]);
501
- }
502
- case "ZodUnion": {
503
- return this.defaultZodUnionHandler(value);
504
- }
505
- case "ZodString": {
506
- return this.defaultZodStringHandler(value);
507
- }
508
- default:
509
- return value;
463
+ if (isOptional(value)) {
464
+ return this.defaultZodOptionalHandler(value, ["ZodObject", "ZodArray", "ZodUnion", "ZodString", "ZodNumber"]);
465
+ } else if (isObj(value)) {
466
+ return this.defaultZodObjectHandler(value);
467
+ } else if (isArr(value)) {
468
+ return this.defaultZodArrayHandler(value, ["min", "max"]);
469
+ } else if (isUnion(value)) {
470
+ return this.defaultZodUnionHandler(value);
471
+ } else if (isString(value)) {
472
+ return this.defaultZodStringHandler(value);
510
473
  }
474
+ return value;
511
475
  }
512
476
  };
513
477
 
@@ -523,36 +487,27 @@ var GoogleSchemaCompatLayer = class extends SchemaCompatLayer {
523
487
  return this.getModel().provider.includes("google") || this.getModel().modelId.includes("google");
524
488
  }
525
489
  processZodType(value) {
526
- switch (value._def.typeName) {
527
- case "ZodOptional":
528
- return this.defaultZodOptionalHandler(value, [
529
- "ZodObject",
530
- "ZodArray",
531
- "ZodUnion",
532
- "ZodString",
533
- "ZodNumber",
534
- ...UNSUPPORTED_ZOD_TYPES
535
- ]);
536
- case "ZodObject": {
537
- return this.defaultZodObjectHandler(value);
538
- }
539
- case "ZodArray": {
540
- return this.defaultZodArrayHandler(value, []);
541
- }
542
- case "ZodUnion": {
543
- return this.defaultZodUnionHandler(value);
544
- }
545
- // Google models support these properties but the model doesn't respect them, but it respects them when they're
546
- // added to the tool description
547
- case "ZodString": {
548
- return this.defaultZodStringHandler(value);
549
- }
550
- case "ZodNumber": {
551
- return this.defaultZodNumberHandler(value);
552
- }
553
- default:
554
- return this.defaultUnsupportedZodTypeHandler(value);
555
- }
490
+ if (isOptional(value)) {
491
+ return this.defaultZodOptionalHandler(value, [
492
+ "ZodObject",
493
+ "ZodArray",
494
+ "ZodUnion",
495
+ "ZodString",
496
+ "ZodNumber",
497
+ ...UNSUPPORTED_ZOD_TYPES
498
+ ]);
499
+ } else if (isObj(value)) {
500
+ return this.defaultZodObjectHandler(value);
501
+ } else if (isArr(value)) {
502
+ return this.defaultZodArrayHandler(value, []);
503
+ } else if (isUnion(value)) {
504
+ return this.defaultZodUnionHandler(value);
505
+ } else if (isString(value)) {
506
+ return this.defaultZodStringHandler(value);
507
+ } else if (isNumber(value)) {
508
+ return this.defaultZodNumberHandler(value);
509
+ }
510
+ return this.defaultUnsupportedZodTypeHandler(value);
556
511
  }
557
512
  };
558
513
 
@@ -568,27 +523,20 @@ var MetaSchemaCompatLayer = class extends SchemaCompatLayer {
568
523
  return this.getModel().modelId.includes("meta");
569
524
  }
570
525
  processZodType(value) {
571
- switch (value._def.typeName) {
572
- case "ZodOptional":
573
- return this.defaultZodOptionalHandler(value, ["ZodObject", "ZodArray", "ZodUnion", "ZodString", "ZodNumber"]);
574
- case "ZodObject": {
575
- return this.defaultZodObjectHandler(value);
576
- }
577
- case "ZodArray": {
578
- return this.defaultZodArrayHandler(value, ["min", "max"]);
579
- }
580
- case "ZodUnion": {
581
- return this.defaultZodUnionHandler(value);
582
- }
583
- case "ZodNumber": {
584
- return this.defaultZodNumberHandler(value);
585
- }
586
- case "ZodString": {
587
- return this.defaultZodStringHandler(value);
588
- }
589
- default:
590
- return value;
526
+ if (isOptional(value)) {
527
+ return this.defaultZodOptionalHandler(value, ["ZodObject", "ZodArray", "ZodUnion", "ZodString", "ZodNumber"]);
528
+ } else if (isObj(value)) {
529
+ return this.defaultZodObjectHandler(value);
530
+ } else if (isArr(value)) {
531
+ return this.defaultZodArrayHandler(value, ["min", "max"]);
532
+ } else if (isUnion(value)) {
533
+ return this.defaultZodUnionHandler(value);
534
+ } else if (isNumber(value)) {
535
+ return this.defaultZodNumberHandler(value);
536
+ } else if (isString(value)) {
537
+ return this.defaultZodStringHandler(value);
591
538
  }
539
+ return value;
592
540
  }
593
541
  };
594
542
 
@@ -607,37 +555,31 @@ var OpenAISchemaCompatLayer = class extends SchemaCompatLayer {
607
555
  return false;
608
556
  }
609
557
  processZodType(value) {
610
- switch (value._def.typeName) {
611
- case "ZodOptional":
612
- return this.defaultZodOptionalHandler(value, [
613
- "ZodObject",
614
- "ZodArray",
615
- "ZodUnion",
616
- "ZodString",
617
- "ZodNever",
618
- "ZodUndefined",
619
- "ZodTuple"
620
- ]);
621
- case "ZodObject": {
622
- return this.defaultZodObjectHandler(value);
623
- }
624
- case "ZodUnion": {
625
- return this.defaultZodUnionHandler(value);
626
- }
627
- case "ZodArray": {
628
- return this.defaultZodArrayHandler(value);
629
- }
630
- case "ZodString": {
631
- const model = this.getModel();
632
- const checks = ["emoji"];
633
- if (model.modelId.includes("gpt-4o-mini")) {
634
- checks.push("regex");
635
- }
636
- return this.defaultZodStringHandler(value, checks);
637
- }
638
- default:
639
- return this.defaultUnsupportedZodTypeHandler(value, ["ZodNever", "ZodUndefined", "ZodTuple"]);
640
- }
558
+ if (isOptional(value)) {
559
+ return this.defaultZodOptionalHandler(value, [
560
+ "ZodObject",
561
+ "ZodArray",
562
+ "ZodUnion",
563
+ "ZodString",
564
+ "ZodNever",
565
+ "ZodUndefined",
566
+ "ZodTuple"
567
+ ]);
568
+ } else if (isObj(value)) {
569
+ return this.defaultZodObjectHandler(value);
570
+ } else if (isUnion(value)) {
571
+ return this.defaultZodUnionHandler(value);
572
+ } else if (isArr(value)) {
573
+ return this.defaultZodArrayHandler(value);
574
+ } else if (isString(value)) {
575
+ const model = this.getModel();
576
+ const checks = ["emoji"];
577
+ if (model.modelId.includes("gpt-4o-mini")) {
578
+ checks.push("regex");
579
+ }
580
+ return this.defaultZodStringHandler(value, checks);
581
+ }
582
+ return this.defaultUnsupportedZodTypeHandler(value, ["ZodNever", "ZodUndefined", "ZodTuple"]);
641
583
  }
642
584
  };
643
585
  var OpenAIReasoningSchemaCompatLayer = class extends SchemaCompatLayer {
@@ -657,53 +599,43 @@ var OpenAIReasoningSchemaCompatLayer = class extends SchemaCompatLayer {
657
599
  return false;
658
600
  }
659
601
  processZodType(value) {
660
- switch (value._def.typeName) {
661
- case "ZodOptional":
662
- const innerZodType = this.processZodType(value._def.innerType);
663
- return innerZodType.nullable();
664
- case "ZodObject": {
665
- return this.defaultZodObjectHandler(value);
666
- }
667
- case "ZodArray": {
668
- return this.defaultZodArrayHandler(value);
669
- }
670
- case "ZodUnion": {
671
- return this.defaultZodUnionHandler(value);
672
- }
673
- case "ZodDefault": {
674
- const defaultDef = value._def;
675
- const innerType = defaultDef.innerType;
676
- const defaultValue = defaultDef.defaultValue();
677
- const constraints = {};
678
- if (defaultValue !== void 0) {
679
- constraints.defaultValue = defaultValue;
680
- }
681
- const description = this.mergeParameterDescription(value.description, constraints);
682
- let result = this.processZodType(innerType);
683
- if (description) {
684
- result = result.describe(description);
685
- }
686
- return result;
687
- }
688
- case "ZodNumber": {
689
- return this.defaultZodNumberHandler(value);
690
- }
691
- case "ZodString": {
692
- return this.defaultZodStringHandler(value);
693
- }
694
- case "ZodDate": {
695
- return this.defaultZodDateHandler(value);
696
- }
697
- case "ZodAny": {
698
- return z.string().describe(
699
- (value.description ?? "") + `
602
+ if (isOptional(value)) {
603
+ const innerZodType = this.processZodType(value._def.innerType);
604
+ return innerZodType.nullable();
605
+ } else if (isObj(value)) {
606
+ return this.defaultZodObjectHandler(value);
607
+ } else if (isArr(value)) {
608
+ return this.defaultZodArrayHandler(value);
609
+ } else if (isUnion(value)) {
610
+ return this.defaultZodUnionHandler(value);
611
+ } else if (isDefault(value)) {
612
+ const defaultDef = value._def;
613
+ const innerType = defaultDef.innerType;
614
+ const defaultValue = defaultDef.defaultValue();
615
+ const constraints = {};
616
+ if (defaultValue !== void 0) {
617
+ constraints.defaultValue = defaultValue;
618
+ }
619
+ const description = this.mergeParameterDescription(value.description, constraints);
620
+ let result = this.processZodType(innerType);
621
+ if (description) {
622
+ result = result.describe(description);
623
+ }
624
+ return result;
625
+ } else if (isNumber(value)) {
626
+ return this.defaultZodNumberHandler(value);
627
+ } else if (isString(value)) {
628
+ return this.defaultZodStringHandler(value);
629
+ } else if (isDate(value)) {
630
+ return this.defaultZodDateHandler(value);
631
+ } else if (value._def.typeName === "ZodAny") {
632
+ return z.string().describe(
633
+ (value.description ?? "") + `
700
634
  Argument was an "any" type, but you (the LLM) do not support "any", so it was cast to a "string" type`
701
- );
702
- }
703
- default:
704
- return this.defaultUnsupportedZodTypeHandler(value);
635
+ );
705
636
  }
637
+ return this.defaultUnsupportedZodTypeHandler(value);
706
638
  }
707
639
  };
708
640
 
709
- export { ALL_ARRAY_CHECKS, ALL_NUMBER_CHECKS, ALL_STRING_CHECKS, ALL_ZOD_TYPES, AnthropicSchemaCompatLayer, DeepSeekSchemaCompatLayer, GoogleSchemaCompatLayer, MetaSchemaCompatLayer, OpenAIReasoningSchemaCompatLayer, OpenAISchemaCompatLayer, SUPPORTED_ZOD_TYPES, SchemaCompatLayer, UNSUPPORTED_ZOD_TYPES, applyCompatLayer, convertSchemaToZod, convertZodSchemaToAISDKSchema };
641
+ export { ALL_ARRAY_CHECKS, ALL_NUMBER_CHECKS, ALL_STRING_CHECKS, ALL_ZOD_TYPES, AnthropicSchemaCompatLayer, DeepSeekSchemaCompatLayer, GoogleSchemaCompatLayer, MetaSchemaCompatLayer, OpenAIReasoningSchemaCompatLayer, OpenAISchemaCompatLayer, SUPPORTED_ZOD_TYPES, SchemaCompatLayer, UNSUPPORTED_ZOD_TYPES, applyCompatLayer, convertSchemaToZod, convertZodSchemaToAISDKSchema, isArr, isNumber, isObj, isOptional, isString, isUnion };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/schema-compat",
3
- "version": "0.10.2-alpha.2",
3
+ "version": "0.10.2-alpha.3",
4
4
  "description": "Tool schema compatibility layer for Mastra.ai",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -42,8 +42,8 @@
42
42
  "eslint": "^9.28.0",
43
43
  "tsup": "^8.5.0",
44
44
  "typescript": "^5.8.2",
45
- "vitest": "^3.1.2",
46
- "zod": "^3.24.3",
45
+ "vitest": "^3.2.2",
46
+ "zod": "^3.25.56",
47
47
  "ai": "4.3.16",
48
48
  "@internal/lint": "0.0.10"
49
49
  },
package/src/index.ts CHANGED
@@ -18,6 +18,13 @@ export {
18
18
  type ZodShape,
19
19
  type ShapeKey,
20
20
  type ShapeValue,
21
+ // Re-usable type predicates
22
+ isOptional,
23
+ isObj,
24
+ isArr,
25
+ isUnion,
26
+ isString,
27
+ isNumber,
21
28
  } from './schema-compatibility';
22
29
 
23
30
  // Utility functions
@@ -1,8 +1,8 @@
1
1
  import type { LanguageModelV1 } from 'ai';
2
- import type { z } from 'zod';
2
+ import type { ZodTypeAny } from 'zod';
3
3
  import type { Targets } from 'zod-to-json-schema';
4
- import { SchemaCompatLayer } from '../schema-compatibility';
5
- import type { ShapeValue, AllZodType } from '../schema-compatibility';
4
+ import { SchemaCompatLayer, isArr, isObj, isOptional, isString, isUnion } from '../schema-compatibility';
5
+ import type { AllZodType } from '../schema-compatibility';
6
6
 
7
7
  export class AnthropicSchemaCompatLayer extends SchemaCompatLayer {
8
8
  constructor(model: LanguageModelV1) {
@@ -17,38 +17,33 @@ export class AnthropicSchemaCompatLayer extends SchemaCompatLayer {
17
17
  return this.getModel().modelId.includes('claude');
18
18
  }
19
19
 
20
- processZodType<T extends z.AnyZodObject>(value: z.ZodTypeAny): ShapeValue<T> {
21
- switch (value._def.typeName) {
22
- case 'ZodOptional':
23
- const handleTypes: AllZodType[] = ['ZodObject', 'ZodArray', 'ZodUnion', 'ZodNever', 'ZodUndefined'];
24
- if (this.getModel().modelId.includes('claude-3.5-haiku')) handleTypes.push('ZodString');
25
- if (this.getModel().modelId.includes('claude-3.7')) handleTypes.push('ZodTuple');
26
-
27
- return this.defaultZodOptionalHandler(value, handleTypes);
28
- case 'ZodObject': {
29
- return this.defaultZodObjectHandler(value);
30
- }
31
- case 'ZodArray': {
32
- return this.defaultZodArrayHandler(value, []);
33
- }
34
- case 'ZodUnion': {
35
- return this.defaultZodUnionHandler(value);
36
- }
20
+ processZodType(value: ZodTypeAny): ZodTypeAny {
21
+ if (isOptional(value)) {
22
+ const handleTypes: AllZodType[] = ['ZodObject', 'ZodArray', 'ZodUnion', 'ZodNever', 'ZodUndefined'];
23
+ if (this.getModel().modelId.includes('claude-3.5-haiku')) handleTypes.push('ZodString');
24
+ if (this.getModel().modelId.includes('claude-3.7')) handleTypes.push('ZodTuple');
25
+ return this.defaultZodOptionalHandler(value, handleTypes);
26
+ } else if (isObj(value)) {
27
+ return this.defaultZodObjectHandler(value);
28
+ } else if (isArr(value)) {
29
+ return this.defaultZodArrayHandler(value, []);
30
+ } else if (isUnion(value)) {
31
+ return this.defaultZodUnionHandler(value);
32
+ } else if (isString(value)) {
37
33
  // the claude-3.5-haiku model support these properties but the model doesn't respect them, but it respects them when they're
38
34
  // added to the tool description
39
- case 'ZodString': {
40
- if (this.getModel().modelId.includes('claude-3.5-haiku')) {
41
- return this.defaultZodStringHandler(value, ['max', 'min']);
42
- } else {
43
- return value as ShapeValue<T>;
44
- }
35
+
36
+ if (this.getModel().modelId.includes('claude-3.5-haiku')) {
37
+ return this.defaultZodStringHandler(value, ['max', 'min']);
38
+ } else {
39
+ return value;
45
40
  }
46
- default:
47
- if (this.getModel().modelId.includes('claude-3.7')) {
48
- return this.defaultUnsupportedZodTypeHandler(value, ['ZodNever', 'ZodTuple', 'ZodUndefined']);
49
- } else {
50
- return this.defaultUnsupportedZodTypeHandler(value, ['ZodNever', 'ZodUndefined']);
51
- }
41
+ }
42
+
43
+ if (this.getModel().modelId.includes('claude-3.7')) {
44
+ return this.defaultUnsupportedZodTypeHandler(value, ['ZodNever', 'ZodTuple', 'ZodUndefined']);
45
+ } else {
46
+ return this.defaultUnsupportedZodTypeHandler(value, ['ZodNever', 'ZodUndefined']);
52
47
  }
53
48
  }
54
49
  }