@mastra/schema-compat 0.10.2-alpha.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 +23 -0
- package/CHANGELOG.md +7 -0
- package/LICENSE.md +46 -0
- package/README.md +148 -0
- package/dist/_tsup-dts-rollup.d.cts +473 -0
- package/dist/_tsup-dts-rollup.d.ts +473 -0
- package/dist/index.cjs +726 -0
- package/dist/index.d.cts +25 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +709 -0
- package/eslint.config.js +6 -0
- package/package.json +56 -0
- package/src/index.ts +32 -0
- package/src/provider-compats/anthropic.ts +54 -0
- package/src/provider-compats/deepseek.ts +41 -0
- package/src/provider-compats/google.ts +54 -0
- package/src/provider-compats/meta.ts +43 -0
- package/src/provider-compats/openai-reasoning.ts +86 -0
- package/src/provider-compats/openai.ts +61 -0
- package/src/provider-compats.test.ts +312 -0
- package/src/schema-compatibility.test.ts +350 -0
- package/src/schema-compatibility.ts +587 -0
- package/src/utils.test.ts +328 -0
- package/src/utils.ts +218 -0
- package/tsconfig.json +5 -0
- package/vitest.config.ts +7 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,726 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var zod = require('zod');
|
|
4
|
+
var ai = require('ai');
|
|
5
|
+
var zodFromJsonSchema = require('zod-from-json-schema');
|
|
6
|
+
var zodToJsonSchema = require('zod-to-json-schema');
|
|
7
|
+
|
|
8
|
+
// src/schema-compatibility.ts
|
|
9
|
+
function convertZodSchemaToAISDKSchema(zodSchema, target = "jsonSchema7") {
|
|
10
|
+
return ai.jsonSchema(
|
|
11
|
+
zodToJsonSchema.zodToJsonSchema(zodSchema, {
|
|
12
|
+
$refStrategy: "none",
|
|
13
|
+
target
|
|
14
|
+
}),
|
|
15
|
+
{
|
|
16
|
+
validate: (value) => {
|
|
17
|
+
const result = zodSchema.safeParse(value);
|
|
18
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
function isZodType(value) {
|
|
24
|
+
return typeof value === "object" && value !== null && "_def" in value && "parse" in value && typeof value.parse === "function" && "safeParse" in value && typeof value.safeParse === "function";
|
|
25
|
+
}
|
|
26
|
+
function convertSchemaToZod(schema) {
|
|
27
|
+
if (isZodType(schema)) {
|
|
28
|
+
return schema;
|
|
29
|
+
} else {
|
|
30
|
+
const jsonSchemaToConvert = "jsonSchema" in schema ? schema.jsonSchema : schema;
|
|
31
|
+
try {
|
|
32
|
+
return zodFromJsonSchema.convertJsonSchemaToZod(jsonSchemaToConvert);
|
|
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
|
+
const convertedSchema = convertSchemaToZod(schema);
|
|
49
|
+
if (convertedSchema instanceof zod.z.ZodObject) {
|
|
50
|
+
zodSchema = convertedSchema;
|
|
51
|
+
} else {
|
|
52
|
+
zodSchema = zod.z.object({ value: convertedSchema });
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
if (schema instanceof zod.z.ZodObject) {
|
|
56
|
+
zodSchema = schema;
|
|
57
|
+
} else {
|
|
58
|
+
zodSchema = zod.z.object({ value: schema });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
for (const compat of compatLayers) {
|
|
62
|
+
if (compat.shouldApply()) {
|
|
63
|
+
return mode === "jsonSchema" ? compat.processToJSONSchema(zodSchema) : compat.processToAISDKSchema(zodSchema);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (mode === "jsonSchema") {
|
|
67
|
+
return zodToJsonSchema.zodToJsonSchema(zodSchema, { $refStrategy: "none", target: "jsonSchema7" });
|
|
68
|
+
} else {
|
|
69
|
+
return convertZodSchemaToAISDKSchema(zodSchema);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// src/schema-compatibility.ts
|
|
74
|
+
var ALL_STRING_CHECKS = ["regex", "emoji", "email", "url", "uuid", "cuid", "min", "max"];
|
|
75
|
+
var ALL_NUMBER_CHECKS = [
|
|
76
|
+
"min",
|
|
77
|
+
// gte internally
|
|
78
|
+
"max",
|
|
79
|
+
// lte internally
|
|
80
|
+
"multipleOf"
|
|
81
|
+
];
|
|
82
|
+
var ALL_ARRAY_CHECKS = ["min", "max", "length"];
|
|
83
|
+
var UNSUPPORTED_ZOD_TYPES = ["ZodIntersection", "ZodNever", "ZodNull", "ZodTuple", "ZodUndefined"];
|
|
84
|
+
var SUPPORTED_ZOD_TYPES = [
|
|
85
|
+
"ZodObject",
|
|
86
|
+
"ZodArray",
|
|
87
|
+
"ZodUnion",
|
|
88
|
+
"ZodString",
|
|
89
|
+
"ZodNumber",
|
|
90
|
+
"ZodDate",
|
|
91
|
+
"ZodAny",
|
|
92
|
+
"ZodDefault"
|
|
93
|
+
];
|
|
94
|
+
var ALL_ZOD_TYPES = [...SUPPORTED_ZOD_TYPES, ...UNSUPPORTED_ZOD_TYPES];
|
|
95
|
+
var SchemaCompatLayer = class {
|
|
96
|
+
model;
|
|
97
|
+
/**
|
|
98
|
+
* Creates a new schema compatibility instance.
|
|
99
|
+
*
|
|
100
|
+
* @param model - The language model this compatibility layer applies to
|
|
101
|
+
*/
|
|
102
|
+
constructor(model) {
|
|
103
|
+
this.model = model;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Gets the language model associated with this compatibility layer.
|
|
107
|
+
*
|
|
108
|
+
* @returns The language model instance
|
|
109
|
+
*/
|
|
110
|
+
getModel() {
|
|
111
|
+
return this.model;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Applies compatibility transformations to a Zod object schema.
|
|
115
|
+
*
|
|
116
|
+
* @param zodSchema - The Zod object schema to transform
|
|
117
|
+
* @returns Object containing the transformed schema
|
|
118
|
+
* @private
|
|
119
|
+
*/
|
|
120
|
+
applyZodSchemaCompatibility(zodSchema) {
|
|
121
|
+
const newSchema = zod.z.object(
|
|
122
|
+
Object.entries(zodSchema.shape || {}).reduce(
|
|
123
|
+
(acc, [key, value]) => ({
|
|
124
|
+
...acc,
|
|
125
|
+
[key]: this.processZodType(value)
|
|
126
|
+
}),
|
|
127
|
+
{}
|
|
128
|
+
)
|
|
129
|
+
);
|
|
130
|
+
return { schema: newSchema };
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Default handler for Zod object types. Recursively processes all properties in the object.
|
|
134
|
+
*
|
|
135
|
+
* @param value - The Zod object to process
|
|
136
|
+
* @returns The processed Zod object
|
|
137
|
+
*/
|
|
138
|
+
defaultZodObjectHandler(value) {
|
|
139
|
+
const zodObject = value;
|
|
140
|
+
const processedShape = Object.entries(zodObject.shape || {}).reduce(
|
|
141
|
+
(acc, [key, propValue]) => {
|
|
142
|
+
const typedPropValue = propValue;
|
|
143
|
+
const processedValue = this.processZodType(typedPropValue);
|
|
144
|
+
acc[key] = processedValue;
|
|
145
|
+
return acc;
|
|
146
|
+
},
|
|
147
|
+
{}
|
|
148
|
+
);
|
|
149
|
+
let result = zod.z.object(processedShape);
|
|
150
|
+
if (value.description) {
|
|
151
|
+
result = result.describe(value.description);
|
|
152
|
+
}
|
|
153
|
+
return result;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Merges validation constraints into a parameter description.
|
|
157
|
+
*
|
|
158
|
+
* This helper method converts validation constraints that may not be supported
|
|
159
|
+
* by a provider into human-readable descriptions.
|
|
160
|
+
*
|
|
161
|
+
* @param description - The existing parameter description
|
|
162
|
+
* @param constraints - The validation constraints to merge
|
|
163
|
+
* @returns The updated description with constraints, or undefined if no constraints
|
|
164
|
+
*/
|
|
165
|
+
mergeParameterDescription(description, constraints) {
|
|
166
|
+
if (Object.keys(constraints).length > 0) {
|
|
167
|
+
return (description ? description + "\n" : "") + JSON.stringify(constraints);
|
|
168
|
+
} else {
|
|
169
|
+
return description;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Default handler for unsupported Zod types. Throws an error for specified unsupported types.
|
|
174
|
+
*
|
|
175
|
+
* @param value - The Zod type to check
|
|
176
|
+
* @param throwOnTypes - Array of type names to throw errors for
|
|
177
|
+
* @returns The original value if not in the throw list
|
|
178
|
+
* @throws Error if the type is in the unsupported list
|
|
179
|
+
*/
|
|
180
|
+
defaultUnsupportedZodTypeHandler(value, throwOnTypes = UNSUPPORTED_ZOD_TYPES) {
|
|
181
|
+
if (throwOnTypes.includes(value._def.typeName)) {
|
|
182
|
+
throw new Error(`${this.model.modelId} does not support zod type: ${value._def.typeName}`);
|
|
183
|
+
}
|
|
184
|
+
return value;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Default handler for Zod array types. Processes array constraints according to provider support.
|
|
188
|
+
*
|
|
189
|
+
* @param value - The Zod array to process
|
|
190
|
+
* @param handleChecks - Array constraints to convert to descriptions vs keep as validation
|
|
191
|
+
* @returns The processed Zod array
|
|
192
|
+
*/
|
|
193
|
+
defaultZodArrayHandler(value, handleChecks = ALL_ARRAY_CHECKS) {
|
|
194
|
+
const zodArray = value._def;
|
|
195
|
+
const arrayType = zodArray.type;
|
|
196
|
+
const constraints = {};
|
|
197
|
+
if (zodArray.minLength?.value !== void 0 && handleChecks.includes("min")) {
|
|
198
|
+
constraints.minLength = zodArray.minLength.value;
|
|
199
|
+
}
|
|
200
|
+
if (zodArray.maxLength?.value !== void 0 && handleChecks.includes("max")) {
|
|
201
|
+
constraints.maxLength = zodArray.maxLength.value;
|
|
202
|
+
}
|
|
203
|
+
if (zodArray.exactLength?.value !== void 0 && handleChecks.includes("length")) {
|
|
204
|
+
constraints.exactLength = zodArray.exactLength.value;
|
|
205
|
+
}
|
|
206
|
+
const processedType = arrayType._def.typeName === "ZodObject" ? this.processZodType(arrayType) : arrayType;
|
|
207
|
+
let result = zod.z.array(processedType);
|
|
208
|
+
if (zodArray.minLength?.value !== void 0 && !handleChecks.includes("min")) {
|
|
209
|
+
result = result.min(zodArray.minLength.value);
|
|
210
|
+
}
|
|
211
|
+
if (zodArray.maxLength?.value !== void 0 && !handleChecks.includes("max")) {
|
|
212
|
+
result = result.max(zodArray.maxLength.value);
|
|
213
|
+
}
|
|
214
|
+
if (zodArray.exactLength?.value !== void 0 && !handleChecks.includes("length")) {
|
|
215
|
+
result = result.length(zodArray.exactLength.value);
|
|
216
|
+
}
|
|
217
|
+
const description = this.mergeParameterDescription(value.description, constraints);
|
|
218
|
+
if (description) {
|
|
219
|
+
result = result.describe(description);
|
|
220
|
+
}
|
|
221
|
+
return result;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Default handler for Zod union types. Processes all union options.
|
|
225
|
+
*
|
|
226
|
+
* @param value - The Zod union to process
|
|
227
|
+
* @returns The processed Zod union
|
|
228
|
+
* @throws Error if union has fewer than 2 options
|
|
229
|
+
*/
|
|
230
|
+
defaultZodUnionHandler(value) {
|
|
231
|
+
const zodUnion = value;
|
|
232
|
+
const processedOptions = zodUnion._def.options.map((option) => this.processZodType(option));
|
|
233
|
+
if (processedOptions.length < 2) throw new Error("Union must have at least 2 options");
|
|
234
|
+
let result = zod.z.union(processedOptions);
|
|
235
|
+
if (value.description) {
|
|
236
|
+
result = result.describe(value.description);
|
|
237
|
+
}
|
|
238
|
+
return result;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Default handler for Zod string types. Processes string validation constraints.
|
|
242
|
+
*
|
|
243
|
+
* @param value - The Zod string to process
|
|
244
|
+
* @param handleChecks - String constraints to convert to descriptions vs keep as validation
|
|
245
|
+
* @returns The processed Zod string
|
|
246
|
+
*/
|
|
247
|
+
defaultZodStringHandler(value, handleChecks = ALL_STRING_CHECKS) {
|
|
248
|
+
const zodString = value;
|
|
249
|
+
const constraints = {};
|
|
250
|
+
const checks = zodString._def.checks || [];
|
|
251
|
+
const newChecks = [];
|
|
252
|
+
for (const check of checks) {
|
|
253
|
+
if ("kind" in check) {
|
|
254
|
+
if (handleChecks.includes(check.kind)) {
|
|
255
|
+
switch (check.kind) {
|
|
256
|
+
case "regex": {
|
|
257
|
+
constraints.regex = {
|
|
258
|
+
pattern: check.regex.source,
|
|
259
|
+
flags: check.regex.flags
|
|
260
|
+
};
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
263
|
+
case "emoji": {
|
|
264
|
+
constraints.emoji = true;
|
|
265
|
+
break;
|
|
266
|
+
}
|
|
267
|
+
case "email": {
|
|
268
|
+
constraints.email = true;
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
case "url": {
|
|
272
|
+
constraints.url = true;
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
case "uuid": {
|
|
276
|
+
constraints.uuid = true;
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
case "cuid": {
|
|
280
|
+
constraints.cuid = true;
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
case "min": {
|
|
284
|
+
constraints.minLength = check.value;
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
case "max": {
|
|
288
|
+
constraints.maxLength = check.value;
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
} else {
|
|
293
|
+
newChecks.push(check);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
let result = zod.z.string();
|
|
298
|
+
for (const check of newChecks) {
|
|
299
|
+
result = result._addCheck(check);
|
|
300
|
+
}
|
|
301
|
+
const description = this.mergeParameterDescription(value.description, constraints);
|
|
302
|
+
if (description) {
|
|
303
|
+
result = result.describe(description);
|
|
304
|
+
}
|
|
305
|
+
return result;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Default handler for Zod number types. Processes number validation constraints.
|
|
309
|
+
*
|
|
310
|
+
* @param value - The Zod number to process
|
|
311
|
+
* @param handleChecks - Number constraints to convert to descriptions vs keep as validation
|
|
312
|
+
* @returns The processed Zod number
|
|
313
|
+
*/
|
|
314
|
+
defaultZodNumberHandler(value, handleChecks = ALL_NUMBER_CHECKS) {
|
|
315
|
+
const zodNumber = value;
|
|
316
|
+
const constraints = {};
|
|
317
|
+
const checks = zodNumber._def.checks || [];
|
|
318
|
+
const newChecks = [];
|
|
319
|
+
for (const check of checks) {
|
|
320
|
+
if ("kind" in check) {
|
|
321
|
+
if (handleChecks.includes(check.kind)) {
|
|
322
|
+
switch (check.kind) {
|
|
323
|
+
case "min":
|
|
324
|
+
if (check.inclusive) {
|
|
325
|
+
constraints.gte = check.value;
|
|
326
|
+
} else {
|
|
327
|
+
constraints.gt = check.value;
|
|
328
|
+
}
|
|
329
|
+
break;
|
|
330
|
+
case "max":
|
|
331
|
+
if (check.inclusive) {
|
|
332
|
+
constraints.lte = check.value;
|
|
333
|
+
} else {
|
|
334
|
+
constraints.lt = check.value;
|
|
335
|
+
}
|
|
336
|
+
break;
|
|
337
|
+
case "multipleOf": {
|
|
338
|
+
constraints.multipleOf = check.value;
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
} else {
|
|
343
|
+
newChecks.push(check);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
let result = zod.z.number();
|
|
348
|
+
for (const check of newChecks) {
|
|
349
|
+
switch (check.kind) {
|
|
350
|
+
case "int":
|
|
351
|
+
result = result.int();
|
|
352
|
+
break;
|
|
353
|
+
case "finite":
|
|
354
|
+
result = result.finite();
|
|
355
|
+
break;
|
|
356
|
+
default:
|
|
357
|
+
result = result._addCheck(check);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
const description = this.mergeParameterDescription(value.description, constraints);
|
|
361
|
+
if (description) {
|
|
362
|
+
result = result.describe(description);
|
|
363
|
+
}
|
|
364
|
+
return result;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Default handler for Zod date types. Converts dates to ISO strings with constraint descriptions.
|
|
368
|
+
*
|
|
369
|
+
* @param value - The Zod date to process
|
|
370
|
+
* @returns A Zod string schema representing the date in ISO format
|
|
371
|
+
*/
|
|
372
|
+
defaultZodDateHandler(value) {
|
|
373
|
+
const zodDate = value;
|
|
374
|
+
const constraints = {};
|
|
375
|
+
const checks = zodDate._def.checks || [];
|
|
376
|
+
for (const check of checks) {
|
|
377
|
+
if ("kind" in check) {
|
|
378
|
+
switch (check.kind) {
|
|
379
|
+
case "min":
|
|
380
|
+
const minDate = new Date(check.value);
|
|
381
|
+
if (!isNaN(minDate.getTime())) {
|
|
382
|
+
constraints.minDate = minDate.toISOString();
|
|
383
|
+
}
|
|
384
|
+
break;
|
|
385
|
+
case "max":
|
|
386
|
+
const maxDate = new Date(check.value);
|
|
387
|
+
if (!isNaN(maxDate.getTime())) {
|
|
388
|
+
constraints.maxDate = maxDate.toISOString();
|
|
389
|
+
}
|
|
390
|
+
break;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
constraints.dateFormat = "date-time";
|
|
395
|
+
let result = zod.z.string().describe("date-time");
|
|
396
|
+
const description = this.mergeParameterDescription(value.description, constraints);
|
|
397
|
+
if (description) {
|
|
398
|
+
result = result.describe(description);
|
|
399
|
+
}
|
|
400
|
+
return result;
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Default handler for Zod optional types. Processes the inner type and maintains optionality.
|
|
404
|
+
*
|
|
405
|
+
* @param value - The Zod optional to process
|
|
406
|
+
* @param handleTypes - Types that should be processed vs passed through
|
|
407
|
+
* @returns The processed Zod optional
|
|
408
|
+
*/
|
|
409
|
+
defaultZodOptionalHandler(value, handleTypes = SUPPORTED_ZOD_TYPES) {
|
|
410
|
+
if (handleTypes.includes(value._def.innerType._def.typeName)) {
|
|
411
|
+
return this.processZodType(value._def.innerType).optional();
|
|
412
|
+
} else {
|
|
413
|
+
return value;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Processes a Zod object schema and converts it to an AI SDK Schema.
|
|
418
|
+
*
|
|
419
|
+
* @param zodSchema - The Zod object schema to process
|
|
420
|
+
* @returns An AI SDK Schema with provider-specific compatibility applied
|
|
421
|
+
*/
|
|
422
|
+
processToAISDKSchema(zodSchema) {
|
|
423
|
+
const { schema } = this.applyZodSchemaCompatibility(zodSchema);
|
|
424
|
+
return convertZodSchemaToAISDKSchema(schema, this.getSchemaTarget());
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Processes a Zod object schema and converts it to a JSON Schema.
|
|
428
|
+
*
|
|
429
|
+
* @param zodSchema - The Zod object schema to process
|
|
430
|
+
* @returns A JSONSchema7 object with provider-specific compatibility applied
|
|
431
|
+
*/
|
|
432
|
+
processToJSONSchema(zodSchema) {
|
|
433
|
+
return this.processToAISDKSchema(zodSchema).jsonSchema;
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
// src/provider-compats/anthropic.ts
|
|
438
|
+
var AnthropicSchemaCompatLayer = class extends SchemaCompatLayer {
|
|
439
|
+
constructor(model) {
|
|
440
|
+
super(model);
|
|
441
|
+
}
|
|
442
|
+
getSchemaTarget() {
|
|
443
|
+
return "jsonSchema7";
|
|
444
|
+
}
|
|
445
|
+
shouldApply() {
|
|
446
|
+
return this.getModel().modelId.includes("claude");
|
|
447
|
+
}
|
|
448
|
+
processZodType(value) {
|
|
449
|
+
switch (value._def.typeName) {
|
|
450
|
+
case "ZodOptional":
|
|
451
|
+
const handleTypes = ["ZodObject", "ZodArray", "ZodUnion", "ZodNever", "ZodUndefined"];
|
|
452
|
+
if (this.getModel().modelId.includes("claude-3.5-haiku")) handleTypes.push("ZodString");
|
|
453
|
+
if (this.getModel().modelId.includes("claude-3.7")) handleTypes.push("ZodTuple");
|
|
454
|
+
return this.defaultZodOptionalHandler(value, handleTypes);
|
|
455
|
+
case "ZodObject": {
|
|
456
|
+
return this.defaultZodObjectHandler(value);
|
|
457
|
+
}
|
|
458
|
+
case "ZodArray": {
|
|
459
|
+
return this.defaultZodArrayHandler(value, []);
|
|
460
|
+
}
|
|
461
|
+
case "ZodUnion": {
|
|
462
|
+
return this.defaultZodUnionHandler(value);
|
|
463
|
+
}
|
|
464
|
+
// the claude-3.5-haiku model support these properties but the model doesn't respect them, but it respects them when they're
|
|
465
|
+
// added to the tool description
|
|
466
|
+
case "ZodString": {
|
|
467
|
+
if (this.getModel().modelId.includes("claude-3.5-haiku")) {
|
|
468
|
+
return this.defaultZodStringHandler(value, ["max", "min"]);
|
|
469
|
+
} else {
|
|
470
|
+
return value;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
default:
|
|
474
|
+
if (this.getModel().modelId.includes("claude-3.7")) {
|
|
475
|
+
return this.defaultUnsupportedZodTypeHandler(value, ["ZodNever", "ZodTuple", "ZodUndefined"]);
|
|
476
|
+
} else {
|
|
477
|
+
return this.defaultUnsupportedZodTypeHandler(value, ["ZodNever", "ZodUndefined"]);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
// src/provider-compats/deepseek.ts
|
|
484
|
+
var DeepSeekSchemaCompatLayer = class extends SchemaCompatLayer {
|
|
485
|
+
constructor(model) {
|
|
486
|
+
super(model);
|
|
487
|
+
}
|
|
488
|
+
getSchemaTarget() {
|
|
489
|
+
return "jsonSchema7";
|
|
490
|
+
}
|
|
491
|
+
shouldApply() {
|
|
492
|
+
return this.getModel().modelId.includes("deepseek") && !this.getModel().modelId.includes("r1");
|
|
493
|
+
}
|
|
494
|
+
processZodType(value) {
|
|
495
|
+
switch (value._def.typeName) {
|
|
496
|
+
case "ZodOptional":
|
|
497
|
+
return this.defaultZodOptionalHandler(value, ["ZodObject", "ZodArray", "ZodUnion", "ZodString", "ZodNumber"]);
|
|
498
|
+
case "ZodObject": {
|
|
499
|
+
return this.defaultZodObjectHandler(value);
|
|
500
|
+
}
|
|
501
|
+
case "ZodArray": {
|
|
502
|
+
return this.defaultZodArrayHandler(value, ["min", "max"]);
|
|
503
|
+
}
|
|
504
|
+
case "ZodUnion": {
|
|
505
|
+
return this.defaultZodUnionHandler(value);
|
|
506
|
+
}
|
|
507
|
+
case "ZodString": {
|
|
508
|
+
return this.defaultZodStringHandler(value);
|
|
509
|
+
}
|
|
510
|
+
default:
|
|
511
|
+
return value;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
// src/provider-compats/google.ts
|
|
517
|
+
var GoogleSchemaCompatLayer = class extends SchemaCompatLayer {
|
|
518
|
+
constructor(model) {
|
|
519
|
+
super(model);
|
|
520
|
+
}
|
|
521
|
+
getSchemaTarget() {
|
|
522
|
+
return "jsonSchema7";
|
|
523
|
+
}
|
|
524
|
+
shouldApply() {
|
|
525
|
+
return this.getModel().provider.includes("google") || this.getModel().modelId.includes("google");
|
|
526
|
+
}
|
|
527
|
+
processZodType(value) {
|
|
528
|
+
switch (value._def.typeName) {
|
|
529
|
+
case "ZodOptional":
|
|
530
|
+
return this.defaultZodOptionalHandler(value, [
|
|
531
|
+
"ZodObject",
|
|
532
|
+
"ZodArray",
|
|
533
|
+
"ZodUnion",
|
|
534
|
+
"ZodString",
|
|
535
|
+
"ZodNumber",
|
|
536
|
+
...UNSUPPORTED_ZOD_TYPES
|
|
537
|
+
]);
|
|
538
|
+
case "ZodObject": {
|
|
539
|
+
return this.defaultZodObjectHandler(value);
|
|
540
|
+
}
|
|
541
|
+
case "ZodArray": {
|
|
542
|
+
return this.defaultZodArrayHandler(value, []);
|
|
543
|
+
}
|
|
544
|
+
case "ZodUnion": {
|
|
545
|
+
return this.defaultZodUnionHandler(value);
|
|
546
|
+
}
|
|
547
|
+
// Google models support these properties but the model doesn't respect them, but it respects them when they're
|
|
548
|
+
// added to the tool description
|
|
549
|
+
case "ZodString": {
|
|
550
|
+
return this.defaultZodStringHandler(value);
|
|
551
|
+
}
|
|
552
|
+
case "ZodNumber": {
|
|
553
|
+
return this.defaultZodNumberHandler(value);
|
|
554
|
+
}
|
|
555
|
+
default:
|
|
556
|
+
return this.defaultUnsupportedZodTypeHandler(value);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
// src/provider-compats/meta.ts
|
|
562
|
+
var MetaSchemaCompatLayer = class extends SchemaCompatLayer {
|
|
563
|
+
constructor(model) {
|
|
564
|
+
super(model);
|
|
565
|
+
}
|
|
566
|
+
getSchemaTarget() {
|
|
567
|
+
return "jsonSchema7";
|
|
568
|
+
}
|
|
569
|
+
shouldApply() {
|
|
570
|
+
return this.getModel().modelId.includes("meta");
|
|
571
|
+
}
|
|
572
|
+
processZodType(value) {
|
|
573
|
+
switch (value._def.typeName) {
|
|
574
|
+
case "ZodOptional":
|
|
575
|
+
return this.defaultZodOptionalHandler(value, ["ZodObject", "ZodArray", "ZodUnion", "ZodString", "ZodNumber"]);
|
|
576
|
+
case "ZodObject": {
|
|
577
|
+
return this.defaultZodObjectHandler(value);
|
|
578
|
+
}
|
|
579
|
+
case "ZodArray": {
|
|
580
|
+
return this.defaultZodArrayHandler(value, ["min", "max"]);
|
|
581
|
+
}
|
|
582
|
+
case "ZodUnion": {
|
|
583
|
+
return this.defaultZodUnionHandler(value);
|
|
584
|
+
}
|
|
585
|
+
case "ZodNumber": {
|
|
586
|
+
return this.defaultZodNumberHandler(value);
|
|
587
|
+
}
|
|
588
|
+
case "ZodString": {
|
|
589
|
+
return this.defaultZodStringHandler(value);
|
|
590
|
+
}
|
|
591
|
+
default:
|
|
592
|
+
return value;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
};
|
|
596
|
+
|
|
597
|
+
// src/provider-compats/openai.ts
|
|
598
|
+
var OpenAISchemaCompatLayer = class extends SchemaCompatLayer {
|
|
599
|
+
constructor(model) {
|
|
600
|
+
super(model);
|
|
601
|
+
}
|
|
602
|
+
getSchemaTarget() {
|
|
603
|
+
return `jsonSchema7`;
|
|
604
|
+
}
|
|
605
|
+
shouldApply() {
|
|
606
|
+
if (!this.getModel().supportsStructuredOutputs && (this.getModel().provider.includes(`openai`) || this.getModel().modelId.includes(`openai`))) {
|
|
607
|
+
return true;
|
|
608
|
+
}
|
|
609
|
+
return false;
|
|
610
|
+
}
|
|
611
|
+
processZodType(value) {
|
|
612
|
+
switch (value._def.typeName) {
|
|
613
|
+
case "ZodOptional":
|
|
614
|
+
return this.defaultZodOptionalHandler(value, [
|
|
615
|
+
"ZodObject",
|
|
616
|
+
"ZodArray",
|
|
617
|
+
"ZodUnion",
|
|
618
|
+
"ZodString",
|
|
619
|
+
"ZodNever",
|
|
620
|
+
"ZodUndefined",
|
|
621
|
+
"ZodTuple"
|
|
622
|
+
]);
|
|
623
|
+
case "ZodObject": {
|
|
624
|
+
return this.defaultZodObjectHandler(value);
|
|
625
|
+
}
|
|
626
|
+
case "ZodUnion": {
|
|
627
|
+
return this.defaultZodUnionHandler(value);
|
|
628
|
+
}
|
|
629
|
+
case "ZodArray": {
|
|
630
|
+
return this.defaultZodArrayHandler(value);
|
|
631
|
+
}
|
|
632
|
+
case "ZodString": {
|
|
633
|
+
const model = this.getModel();
|
|
634
|
+
const checks = ["emoji"];
|
|
635
|
+
if (model.modelId.includes("gpt-4o-mini")) {
|
|
636
|
+
checks.push("regex");
|
|
637
|
+
}
|
|
638
|
+
return this.defaultZodStringHandler(value, checks);
|
|
639
|
+
}
|
|
640
|
+
default:
|
|
641
|
+
return this.defaultUnsupportedZodTypeHandler(value, ["ZodNever", "ZodUndefined", "ZodTuple"]);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
};
|
|
645
|
+
var OpenAIReasoningSchemaCompatLayer = class extends SchemaCompatLayer {
|
|
646
|
+
constructor(model) {
|
|
647
|
+
super(model);
|
|
648
|
+
}
|
|
649
|
+
getSchemaTarget() {
|
|
650
|
+
return `openApi3`;
|
|
651
|
+
}
|
|
652
|
+
isReasoningModel() {
|
|
653
|
+
return this.getModel().modelId.includes(`o3`) || this.getModel().modelId.includes(`o4`);
|
|
654
|
+
}
|
|
655
|
+
shouldApply() {
|
|
656
|
+
if ((this.getModel().supportsStructuredOutputs || this.isReasoningModel()) && (this.getModel().provider.includes(`openai`) || this.getModel().modelId.includes(`openai`))) {
|
|
657
|
+
return true;
|
|
658
|
+
}
|
|
659
|
+
return false;
|
|
660
|
+
}
|
|
661
|
+
processZodType(value) {
|
|
662
|
+
switch (value._def.typeName) {
|
|
663
|
+
case "ZodOptional":
|
|
664
|
+
const innerZodType = this.processZodType(value._def.innerType);
|
|
665
|
+
return innerZodType.nullable();
|
|
666
|
+
case "ZodObject": {
|
|
667
|
+
return this.defaultZodObjectHandler(value);
|
|
668
|
+
}
|
|
669
|
+
case "ZodArray": {
|
|
670
|
+
return this.defaultZodArrayHandler(value);
|
|
671
|
+
}
|
|
672
|
+
case "ZodUnion": {
|
|
673
|
+
return this.defaultZodUnionHandler(value);
|
|
674
|
+
}
|
|
675
|
+
case "ZodDefault": {
|
|
676
|
+
const defaultDef = value._def;
|
|
677
|
+
const innerType = defaultDef.innerType;
|
|
678
|
+
const defaultValue = defaultDef.defaultValue();
|
|
679
|
+
const constraints = {};
|
|
680
|
+
if (defaultValue !== void 0) {
|
|
681
|
+
constraints.defaultValue = defaultValue;
|
|
682
|
+
}
|
|
683
|
+
const description = this.mergeParameterDescription(value.description, constraints);
|
|
684
|
+
let result = this.processZodType(innerType);
|
|
685
|
+
if (description) {
|
|
686
|
+
result = result.describe(description);
|
|
687
|
+
}
|
|
688
|
+
return result;
|
|
689
|
+
}
|
|
690
|
+
case "ZodNumber": {
|
|
691
|
+
return this.defaultZodNumberHandler(value);
|
|
692
|
+
}
|
|
693
|
+
case "ZodString": {
|
|
694
|
+
return this.defaultZodStringHandler(value);
|
|
695
|
+
}
|
|
696
|
+
case "ZodDate": {
|
|
697
|
+
return this.defaultZodDateHandler(value);
|
|
698
|
+
}
|
|
699
|
+
case "ZodAny": {
|
|
700
|
+
return zod.z.string().describe(
|
|
701
|
+
(value.description ?? "") + `
|
|
702
|
+
Argument was an "any" type, but you (the LLM) do not support "any", so it was cast to a "string" type`
|
|
703
|
+
);
|
|
704
|
+
}
|
|
705
|
+
default:
|
|
706
|
+
return this.defaultUnsupportedZodTypeHandler(value);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
};
|
|
710
|
+
|
|
711
|
+
exports.ALL_ARRAY_CHECKS = ALL_ARRAY_CHECKS;
|
|
712
|
+
exports.ALL_NUMBER_CHECKS = ALL_NUMBER_CHECKS;
|
|
713
|
+
exports.ALL_STRING_CHECKS = ALL_STRING_CHECKS;
|
|
714
|
+
exports.ALL_ZOD_TYPES = ALL_ZOD_TYPES;
|
|
715
|
+
exports.AnthropicSchemaCompatLayer = AnthropicSchemaCompatLayer;
|
|
716
|
+
exports.DeepSeekSchemaCompatLayer = DeepSeekSchemaCompatLayer;
|
|
717
|
+
exports.GoogleSchemaCompatLayer = GoogleSchemaCompatLayer;
|
|
718
|
+
exports.MetaSchemaCompatLayer = MetaSchemaCompatLayer;
|
|
719
|
+
exports.OpenAIReasoningSchemaCompatLayer = OpenAIReasoningSchemaCompatLayer;
|
|
720
|
+
exports.OpenAISchemaCompatLayer = OpenAISchemaCompatLayer;
|
|
721
|
+
exports.SUPPORTED_ZOD_TYPES = SUPPORTED_ZOD_TYPES;
|
|
722
|
+
exports.SchemaCompatLayer = SchemaCompatLayer;
|
|
723
|
+
exports.UNSUPPORTED_ZOD_TYPES = UNSUPPORTED_ZOD_TYPES;
|
|
724
|
+
exports.applyCompatLayer = applyCompatLayer;
|
|
725
|
+
exports.convertSchemaToZod = convertSchemaToZod;
|
|
726
|
+
exports.convertZodSchemaToAISDKSchema = convertZodSchemaToAISDKSchema;
|