@mastra/schema-compat 0.10.2-alpha.2 → 0.10.2
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/.turbo/turbo-build.log +7 -7
- package/CHANGELOG.md +13 -0
- package/dist/_tsup-dts-rollup.d.cts +55 -26
- package/dist/_tsup-dts-rollup.d.ts +55 -26
- package/dist/index.cjs +178 -240
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +174 -242
- package/package.json +4 -4
- package/src/index.ts +7 -0
- package/src/provider-compats/anthropic.ts +27 -32
- package/src/provider-compats/deepseek.ts +15 -21
- package/src/provider-compats/google.ts +34 -33
- package/src/provider-compats/meta.ts +17 -24
- package/src/provider-compats/openai-reasoning.ts +51 -50
- package/src/provider-compats/openai.ts +28 -33
- package/src/schema-compatibility.test.ts +161 -40
- package/src/schema-compatibility.ts +83 -91
- package/src/utils.test.ts +129 -23
- package/src/utils.ts +8 -21
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
|
-
|
|
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
|
-
|
|
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
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
|
193
|
-
const
|
|
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
|
-
|
|
207
|
-
|
|
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 (
|
|
210
|
-
|
|
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 (
|
|
213
|
-
|
|
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
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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
|
|
422
|
-
return convertZodSchemaToAISDKSchema(
|
|
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
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
return this.
|
|
461
|
-
}
|
|
462
|
-
|
|
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
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
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
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
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
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
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
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
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
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
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
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
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
|
|
3
|
+
"version": "0.10.2",
|
|
4
4
|
"description": "Tool schema compatibility layer for Mastra.ai",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -42,10 +42,10 @@
|
|
|
42
42
|
"eslint": "^9.28.0",
|
|
43
43
|
"tsup": "^8.5.0",
|
|
44
44
|
"typescript": "^5.8.2",
|
|
45
|
-
"vitest": "^3.
|
|
46
|
-
"zod": "^3.
|
|
45
|
+
"vitest": "^3.2.2",
|
|
46
|
+
"zod": "^3.25.56",
|
|
47
47
|
"ai": "4.3.16",
|
|
48
|
-
"@internal/lint": "0.0.
|
|
48
|
+
"@internal/lint": "0.0.11"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|
|
51
51
|
"build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting",
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { LanguageModelV1 } from 'ai';
|
|
2
|
-
import type {
|
|
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 {
|
|
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
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
}
|