@mastra/schema-compat 0.0.0-fix-message-list-args-missing-20250807205055 → 0.0.0-fix-zod-to-json-schema-ref-strategy-20250910193441

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 (63) hide show
  1. package/CHANGELOG.md +89 -1
  2. package/README.md +0 -4
  3. package/dist/chunk-7YUR5KZ5.cjs +34 -0
  4. package/dist/chunk-7YUR5KZ5.cjs.map +1 -0
  5. package/dist/chunk-GWTUXMDD.js +28 -0
  6. package/dist/chunk-GWTUXMDD.js.map +1 -0
  7. package/dist/index.cjs +842 -83
  8. package/dist/index.cjs.map +1 -1
  9. package/dist/index.d.ts +10 -8
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +841 -84
  12. package/dist/index.js.map +1 -1
  13. package/dist/provider-compats/anthropic.d.ts +6 -4
  14. package/dist/provider-compats/anthropic.d.ts.map +1 -1
  15. package/dist/provider-compats/deepseek.d.ts +6 -4
  16. package/dist/provider-compats/deepseek.d.ts.map +1 -1
  17. package/dist/provider-compats/google.d.ts +6 -4
  18. package/dist/provider-compats/google.d.ts.map +1 -1
  19. package/dist/provider-compats/meta.d.ts +6 -4
  20. package/dist/provider-compats/meta.d.ts.map +1 -1
  21. package/dist/provider-compats/openai-reasoning.d.ts +6 -4
  22. package/dist/provider-compats/openai-reasoning.d.ts.map +1 -1
  23. package/dist/provider-compats/openai.d.ts +6 -4
  24. package/dist/provider-compats/openai.d.ts.map +1 -1
  25. package/dist/schema-compatibility-v3.d.ts +319 -0
  26. package/dist/schema-compatibility-v3.d.ts.map +1 -0
  27. package/dist/schema-compatibility-v4.d.ts +310 -0
  28. package/dist/schema-compatibility-v4.d.ts.map +1 -0
  29. package/dist/schema-compatibility.d.ts +76 -133
  30. package/dist/schema-compatibility.d.ts.map +1 -1
  31. package/dist/types.d.ts +6 -0
  32. package/dist/types.d.ts.map +1 -0
  33. package/dist/utils-test-suite.d.ts +2 -0
  34. package/dist/utils-test-suite.d.ts.map +1 -0
  35. package/dist/utils.d.ts +17 -5
  36. package/dist/utils.d.ts.map +1 -1
  37. package/dist/zod-to-json.cjs +12 -0
  38. package/dist/zod-to-json.cjs.map +1 -0
  39. package/dist/zod-to-json.d.ts +6 -0
  40. package/dist/zod-to-json.d.ts.map +1 -0
  41. package/dist/zod-to-json.js +3 -0
  42. package/dist/zod-to-json.js.map +1 -0
  43. package/dist/zodTypes.d.ts +21 -0
  44. package/dist/zodTypes.d.ts.map +1 -0
  45. package/package.json +31 -8
  46. package/.turbo/turbo-build.log +0 -4
  47. package/eslint.config.js +0 -6
  48. package/src/index.ts +0 -39
  49. package/src/provider-compats/anthropic.ts +0 -43
  50. package/src/provider-compats/deepseek.ts +0 -35
  51. package/src/provider-compats/google.ts +0 -63
  52. package/src/provider-compats/meta.ts +0 -36
  53. package/src/provider-compats/openai-reasoning.ts +0 -91
  54. package/src/provider-compats/openai.ts +0 -55
  55. package/src/provider-compats.test.ts +0 -407
  56. package/src/schema-compatibility.test.ts +0 -487
  57. package/src/schema-compatibility.ts +0 -594
  58. package/src/utils.test.ts +0 -460
  59. package/src/utils.ts +0 -205
  60. package/tsconfig.build.json +0 -9
  61. package/tsconfig.json +0 -5
  62. package/tsup.config.ts +0 -17
  63. package/vitest.config.ts +0 -7
@@ -1,594 +0,0 @@
1
- import type { Schema } from 'ai';
2
- import type { JSONSchema7 } from 'json-schema';
3
- import { z, ZodOptional, ZodObject, ZodArray, ZodUnion, ZodString, ZodNumber, ZodDate, ZodDefault, ZodNull } from 'zod';
4
- import type { ZodTypeAny } from 'zod';
5
- import type { Targets } from 'zod-to-json-schema';
6
- import { convertZodSchemaToAISDKSchema } from './utils';
7
-
8
- /**
9
- * All supported string validation check types that can be processed or converted to descriptions.
10
- * @constant
11
- */
12
- export const ALL_STRING_CHECKS = ['regex', 'emoji', 'email', 'url', 'uuid', 'cuid', 'min', 'max'] as const;
13
-
14
- /**
15
- * All supported number validation check types that can be processed or converted to descriptions.
16
- * @constant
17
- */
18
- export const ALL_NUMBER_CHECKS = [
19
- 'min', // gte internally
20
- 'max', // lte internally
21
- 'multipleOf',
22
- ] as const;
23
-
24
- /**
25
- * All supported array validation check types that can be processed or converted to descriptions.
26
- * @constant
27
- */
28
- export const ALL_ARRAY_CHECKS = ['min', 'max', 'length'] as const;
29
-
30
- export const isOptional = (v: ZodTypeAny): v is ZodOptional<any> => v instanceof ZodOptional;
31
- export const isObj = (v: ZodTypeAny): v is ZodObject<any, any, any> => v instanceof ZodObject;
32
- export const isNull = (v: ZodTypeAny): v is ZodNull => v instanceof ZodNull;
33
- export const isArr = (v: ZodTypeAny): v is ZodArray<any, any> => v instanceof ZodArray;
34
- export const isUnion = (v: ZodTypeAny): v is ZodUnion<[ZodTypeAny, ...ZodTypeAny[]]> => v instanceof ZodUnion;
35
- export const isString = (v: ZodTypeAny): v is ZodString => v instanceof ZodString;
36
- export const isNumber = (v: ZodTypeAny): v is ZodNumber => v instanceof ZodNumber;
37
- export const isDate = (v: ZodTypeAny): v is ZodDate => v instanceof ZodDate;
38
- export const isDefault = (v: ZodTypeAny): v is ZodDefault<any> => v instanceof ZodDefault;
39
-
40
- /**
41
- * Zod types that are not supported by most AI model providers and should be avoided.
42
- * @constant
43
- */
44
- export const UNSUPPORTED_ZOD_TYPES = ['ZodIntersection', 'ZodNever', 'ZodNull', 'ZodTuple', 'ZodUndefined'] as const;
45
-
46
- /**
47
- * Zod types that are generally supported by AI model providers.
48
- * @constant
49
- */
50
- export const SUPPORTED_ZOD_TYPES = [
51
- 'ZodObject',
52
- 'ZodArray',
53
- 'ZodUnion',
54
- 'ZodString',
55
- 'ZodNumber',
56
- 'ZodDate',
57
- 'ZodAny',
58
- 'ZodDefault',
59
- ] as const;
60
-
61
- /**
62
- * All Zod types (both supported and unsupported).
63
- * @constant
64
- */
65
- export const ALL_ZOD_TYPES = [...SUPPORTED_ZOD_TYPES, ...UNSUPPORTED_ZOD_TYPES] as const;
66
-
67
- /**
68
- * Type representing string validation checks.
69
- */
70
- export type StringCheckType = (typeof ALL_STRING_CHECKS)[number];
71
-
72
- /**
73
- * Type representing number validation checks.
74
- */
75
- export type NumberCheckType = (typeof ALL_NUMBER_CHECKS)[number];
76
-
77
- /**
78
- * Type representing array validation checks.
79
- */
80
- export type ArrayCheckType = (typeof ALL_ARRAY_CHECKS)[number];
81
-
82
- /**
83
- * Type representing unsupported Zod schema types.
84
- */
85
- export type UnsupportedZodType = (typeof UNSUPPORTED_ZOD_TYPES)[number];
86
-
87
- /**
88
- * Type representing supported Zod schema types.
89
- */
90
- export type SupportedZodType = (typeof SUPPORTED_ZOD_TYPES)[number];
91
-
92
- /**
93
- * Type representing all Zod schema types (supported and unsupported).
94
- */
95
- export type AllZodType = (typeof ALL_ZOD_TYPES)[number];
96
-
97
- /**
98
- * Utility type to extract the shape of a Zod object schema.
99
- */
100
- export type ZodShape<T extends z.AnyZodObject> = T['shape'];
101
-
102
- /**
103
- * Utility type to extract the keys from a Zod object shape.
104
- */
105
- export type ShapeKey<T extends z.AnyZodObject> = keyof ZodShape<T>;
106
-
107
- /**
108
- * Utility type to extract the value types from a Zod object shape.
109
- */
110
- export type ShapeValue<T extends z.AnyZodObject> = ZodShape<T>[ShapeKey<T>];
111
-
112
- // Add constraint types at the top
113
-
114
- type StringConstraints = {
115
- minLength?: number;
116
- maxLength?: number;
117
- email?: boolean;
118
- url?: boolean;
119
- uuid?: boolean;
120
- cuid?: boolean;
121
- emoji?: boolean;
122
- regex?: { pattern: string; flags?: string };
123
- };
124
-
125
- type NumberConstraints = {
126
- gt?: number;
127
- gte?: number;
128
- lt?: number;
129
- lte?: number;
130
- multipleOf?: number;
131
- };
132
-
133
- type ArrayConstraints = {
134
- minLength?: number;
135
- maxLength?: number;
136
- exactLength?: number;
137
- };
138
-
139
- type DateConstraints = {
140
- minDate?: string;
141
- maxDate?: string;
142
- dateFormat?: string;
143
- };
144
-
145
- export type ModelInformation = {
146
- modelId: string;
147
- provider: string;
148
- supportsStructuredOutputs: boolean;
149
- };
150
-
151
- /**
152
- * Abstract base class for creating schema compatibility layers for different AI model providers.
153
- *
154
- * This class provides a framework for transforming Zod schemas to work with specific AI model
155
- * provider requirements and limitations. Each provider may have different support levels for
156
- * JSON Schema features, validation constraints, and data types.
157
- *
158
- * @abstract
159
- *
160
- * @example
161
- * ```typescript
162
- * import { SchemaCompatLayer } from '@mastra/schema-compat';
163
- * import type { LanguageModelV1 } from 'ai';
164
- *
165
- * class CustomProviderCompat extends SchemaCompatLayer {
166
- * constructor(model: LanguageModelV1) {
167
- * super(model);
168
- * }
169
- *
170
- * shouldApply(): boolean {
171
- * return this.getModel().provider === 'custom-provider';
172
- * }
173
- *
174
- * getSchemaTarget() {
175
- * return 'jsonSchema7';
176
- * }
177
- *
178
- * processZodType<T extends z.AnyZodObject>(value: z.ZodTypeAny): ShapeValue<T> {
179
- * // Custom processing logic for this provider
180
- * switch (value._def.typeName) {
181
- * case 'ZodString':
182
- * return this.defaultZodStringHandler(value, ['email', 'url']);
183
- * default:
184
- * return this.defaultUnsupportedZodTypeHandler(value);
185
- * }
186
- * }
187
- * }
188
- * ```
189
- */
190
- export abstract class SchemaCompatLayer {
191
- private model: ModelInformation;
192
-
193
- /**
194
- * Creates a new schema compatibility instance.
195
- *
196
- * @param model - The language model this compatibility layer applies to
197
- */
198
- constructor(model: ModelInformation) {
199
- this.model = model;
200
- }
201
-
202
- /**
203
- * Gets the language model associated with this compatibility layer.
204
- *
205
- * @returns The language model instance
206
- */
207
- getModel(): ModelInformation {
208
- return this.model;
209
- }
210
-
211
- /**
212
- * Determines whether this compatibility layer should be applied for the current model.
213
- *
214
- * @returns True if this compatibility layer should be used, false otherwise
215
- * @abstract
216
- */
217
- abstract shouldApply(): boolean;
218
-
219
- /**
220
- * Returns the JSON Schema target format for this provider.
221
- *
222
- * @returns The schema target format, or undefined to use the default 'jsonSchema7'
223
- * @abstract
224
- */
225
- abstract getSchemaTarget(): Targets | undefined;
226
-
227
- /**
228
- * Processes a specific Zod type according to the provider's requirements.
229
- *
230
- * @param value - The Zod type to process
231
- * @returns The processed Zod type
232
- * @abstract
233
- */
234
- abstract processZodType(value: ZodTypeAny): ZodTypeAny;
235
-
236
- /**
237
- * Default handler for Zod object types. Recursively processes all properties in the object.
238
- *
239
- * @param value - The Zod object to process
240
- * @returns The processed Zod object
241
- */
242
- public defaultZodObjectHandler(
243
- value: ZodObject<any, any, any>,
244
- options: { passthrough?: boolean } = { passthrough: true },
245
- ): ZodObject<any, any, any> {
246
- const processedShape = Object.entries(value.shape).reduce<Record<string, ZodTypeAny>>((acc, [key, propValue]) => {
247
- acc[key] = this.processZodType(propValue as ZodTypeAny);
248
- return acc;
249
- }, {});
250
-
251
- let result: ZodObject<any, any, any> = z.object(processedShape);
252
-
253
- if (value._def.unknownKeys === 'strict') {
254
- result = result.strict();
255
- }
256
- if (value._def.catchall && !(value._def.catchall instanceof z.ZodNever)) {
257
- result = result.catchall(value._def.catchall);
258
- }
259
-
260
- if (value.description) {
261
- result = result.describe(value.description);
262
- }
263
-
264
- if (options.passthrough && value._def.unknownKeys === 'passthrough') {
265
- result = result.passthrough();
266
- }
267
-
268
- return result;
269
- }
270
-
271
- /**
272
- * Merges validation constraints into a parameter description.
273
- *
274
- * This helper method converts validation constraints that may not be supported
275
- * by a provider into human-readable descriptions.
276
- *
277
- * @param description - The existing parameter description
278
- * @param constraints - The validation constraints to merge
279
- * @returns The updated description with constraints, or undefined if no constraints
280
- */
281
- public mergeParameterDescription(
282
- description: string | undefined,
283
- constraints:
284
- | NumberConstraints
285
- | StringConstraints
286
- | ArrayConstraints
287
- | DateConstraints
288
- | { defaultValue?: unknown },
289
- ): string | undefined {
290
- if (Object.keys(constraints).length > 0) {
291
- return (description ? description + '\n' : '') + JSON.stringify(constraints);
292
- } else {
293
- return description;
294
- }
295
- }
296
-
297
- /**
298
- * Default handler for unsupported Zod types. Throws an error for specified unsupported types.
299
- *
300
- * @param value - The Zod type to check
301
- * @param throwOnTypes - Array of type names to throw errors for
302
- * @returns The original value if not in the throw list
303
- * @throws Error if the type is in the unsupported list
304
- */
305
- public defaultUnsupportedZodTypeHandler<T extends z.AnyZodObject>(
306
- value: z.ZodTypeAny,
307
- throwOnTypes: readonly UnsupportedZodType[] = UNSUPPORTED_ZOD_TYPES,
308
- ): ShapeValue<T> {
309
- if (throwOnTypes.includes(value._def?.typeName as UnsupportedZodType)) {
310
- throw new Error(`${this.model.modelId} does not support zod type: ${value._def?.typeName}`);
311
- }
312
- return value as ShapeValue<T>;
313
- }
314
-
315
- /**
316
- * Default handler for Zod array types. Processes array constraints according to provider support.
317
- *
318
- * @param value - The Zod array to process
319
- * @param handleChecks - Array constraints to convert to descriptions vs keep as validation
320
- * @returns The processed Zod array
321
- */
322
- public defaultZodArrayHandler(
323
- value: ZodArray<any, any>,
324
- handleChecks: readonly ArrayCheckType[] = ALL_ARRAY_CHECKS,
325
- ): ZodArray<any, any> {
326
- const zodArrayDef = value._def;
327
- const processedType = this.processZodType(zodArrayDef.type);
328
-
329
- let result = z.array(processedType);
330
-
331
- const constraints: ArrayConstraints = {};
332
-
333
- if (zodArrayDef.minLength?.value !== undefined) {
334
- if (handleChecks.includes('min')) {
335
- constraints.minLength = zodArrayDef.minLength.value;
336
- } else {
337
- result = result.min(zodArrayDef.minLength.value);
338
- }
339
- }
340
-
341
- if (zodArrayDef.maxLength?.value !== undefined) {
342
- if (handleChecks.includes('max')) {
343
- constraints.maxLength = zodArrayDef.maxLength.value;
344
- } else {
345
- result = result.max(zodArrayDef.maxLength.value);
346
- }
347
- }
348
-
349
- if (zodArrayDef.exactLength?.value !== undefined) {
350
- if (handleChecks.includes('length')) {
351
- constraints.exactLength = zodArrayDef.exactLength.value;
352
- } else {
353
- result = result.length(zodArrayDef.exactLength.value);
354
- }
355
- }
356
-
357
- const description = this.mergeParameterDescription(value.description, constraints);
358
- if (description) {
359
- result = result.describe(description);
360
- }
361
- return result;
362
- }
363
-
364
- /**
365
- * Default handler for Zod union types. Processes all union options.
366
- *
367
- * @param value - The Zod union to process
368
- * @returns The processed Zod union
369
- * @throws Error if union has fewer than 2 options
370
- */
371
- public defaultZodUnionHandler(value: ZodUnion<[ZodTypeAny, ...ZodTypeAny[]]>): ZodTypeAny {
372
- const processedOptions = value._def.options.map((option: ZodTypeAny) => this.processZodType(option));
373
- if (processedOptions.length < 2) throw new Error('Union must have at least 2 options');
374
- let result = z.union(processedOptions as [ZodTypeAny, ZodTypeAny, ...ZodTypeAny[]]);
375
- if (value.description) {
376
- result = result.describe(value.description);
377
- }
378
- return result;
379
- }
380
-
381
- /**
382
- * Default handler for Zod string types. Processes string validation constraints.
383
- *
384
- * @param value - The Zod string to process
385
- * @param handleChecks - String constraints to convert to descriptions vs keep as validation
386
- * @returns The processed Zod string
387
- */
388
- public defaultZodStringHandler(
389
- value: ZodString,
390
- handleChecks: readonly StringCheckType[] = ALL_STRING_CHECKS,
391
- ): ZodString {
392
- const constraints: StringConstraints = {};
393
- const checks = value._def.checks || [];
394
- type ZodStringCheck = (typeof checks)[number];
395
- const newChecks: ZodStringCheck[] = [];
396
- for (const check of checks) {
397
- if ('kind' in check) {
398
- if (handleChecks.includes(check.kind as StringCheckType)) {
399
- switch (check.kind) {
400
- case 'regex': {
401
- constraints.regex = {
402
- pattern: check.regex.source,
403
- flags: check.regex.flags,
404
- };
405
- break;
406
- }
407
- case 'emoji': {
408
- constraints.emoji = true;
409
- break;
410
- }
411
- case 'email': {
412
- constraints.email = true;
413
- break;
414
- }
415
- case 'url': {
416
- constraints.url = true;
417
- break;
418
- }
419
- case 'uuid': {
420
- constraints.uuid = true;
421
- break;
422
- }
423
- case 'cuid': {
424
- constraints.cuid = true;
425
- break;
426
- }
427
- case 'min': {
428
- constraints.minLength = check.value;
429
- break;
430
- }
431
- case 'max': {
432
- constraints.maxLength = check.value;
433
- break;
434
- }
435
- }
436
- } else {
437
- newChecks.push(check);
438
- }
439
- }
440
- }
441
- let result = z.string();
442
- for (const check of newChecks) {
443
- result = result._addCheck(check);
444
- }
445
- const description = this.mergeParameterDescription(value.description, constraints);
446
- if (description) {
447
- result = result.describe(description);
448
- }
449
- return result;
450
- }
451
-
452
- /**
453
- * Default handler for Zod number types. Processes number validation constraints.
454
- *
455
- * @param value - The Zod number to process
456
- * @param handleChecks - Number constraints to convert to descriptions vs keep as validation
457
- * @returns The processed Zod number
458
- */
459
- public defaultZodNumberHandler(
460
- value: ZodNumber,
461
- handleChecks: readonly NumberCheckType[] = ALL_NUMBER_CHECKS,
462
- ): ZodNumber {
463
- const constraints: NumberConstraints = {};
464
- const checks = value._def.checks || [];
465
- type ZodNumberCheck = (typeof checks)[number];
466
- const newChecks: ZodNumberCheck[] = [];
467
- for (const check of checks) {
468
- if ('kind' in check) {
469
- if (handleChecks.includes(check.kind as NumberCheckType)) {
470
- switch (check.kind) {
471
- case 'min':
472
- if (check.inclusive) {
473
- constraints.gte = check.value;
474
- } else {
475
- constraints.gt = check.value;
476
- }
477
- break;
478
- case 'max':
479
- if (check.inclusive) {
480
- constraints.lte = check.value;
481
- } else {
482
- constraints.lt = check.value;
483
- }
484
- break;
485
- case 'multipleOf': {
486
- constraints.multipleOf = check.value;
487
- break;
488
- }
489
- }
490
- } else {
491
- newChecks.push(check);
492
- }
493
- }
494
- }
495
- let result = z.number();
496
- for (const check of newChecks) {
497
- switch (check.kind) {
498
- case 'int':
499
- result = result.int();
500
- break;
501
- case 'finite':
502
- result = result.finite();
503
- break;
504
- default:
505
- result = result._addCheck(check);
506
- }
507
- }
508
- const description = this.mergeParameterDescription(value.description, constraints);
509
- if (description) {
510
- result = result.describe(description);
511
- }
512
- return result;
513
- }
514
-
515
- /**
516
- * Default handler for Zod date types. Converts dates to ISO strings with constraint descriptions.
517
- *
518
- * @param value - The Zod date to process
519
- * @returns A Zod string schema representing the date in ISO format
520
- */
521
- public defaultZodDateHandler(value: ZodDate): ZodString {
522
- const constraints: DateConstraints = {};
523
- const checks = value._def.checks || [];
524
- type ZodDateCheck = (typeof checks)[number];
525
- const newChecks: ZodDateCheck[] = [];
526
- for (const check of checks) {
527
- if ('kind' in check) {
528
- switch (check.kind) {
529
- case 'min':
530
- const minDate = new Date(check.value);
531
- if (!isNaN(minDate.getTime())) {
532
- constraints.minDate = minDate.toISOString();
533
- }
534
- break;
535
- case 'max':
536
- const maxDate = new Date(check.value);
537
- if (!isNaN(maxDate.getTime())) {
538
- constraints.maxDate = maxDate.toISOString();
539
- }
540
- break;
541
- default:
542
- newChecks.push(check);
543
- }
544
- }
545
- }
546
- constraints.dateFormat = 'date-time';
547
- let result = z.string().describe('date-time');
548
- const description = this.mergeParameterDescription(value.description, constraints);
549
- if (description) {
550
- result = result.describe(description);
551
- }
552
- return result;
553
- }
554
-
555
- /**
556
- * Default handler for Zod optional types. Processes the inner type and maintains optionality.
557
- *
558
- * @param value - The Zod optional to process
559
- * @param handleTypes - Types that should be processed vs passed through
560
- * @returns The processed Zod optional
561
- */
562
- public defaultZodOptionalHandler(
563
- value: ZodOptional<any>,
564
- handleTypes: readonly AllZodType[] = SUPPORTED_ZOD_TYPES,
565
- ): ZodTypeAny {
566
- if (handleTypes.includes(value._def.innerType._def.typeName as AllZodType)) {
567
- return this.processZodType(value._def.innerType).optional();
568
- } else {
569
- return value;
570
- }
571
- }
572
-
573
- /**
574
- * Processes a Zod object schema and converts it to an AI SDK Schema.
575
- *
576
- * @param zodSchema - The Zod object schema to process
577
- * @returns An AI SDK Schema with provider-specific compatibility applied
578
- */
579
- public processToAISDKSchema(zodSchema: z.ZodSchema): Schema {
580
- const processedSchema = this.processZodType(zodSchema);
581
-
582
- return convertZodSchemaToAISDKSchema(processedSchema, this.getSchemaTarget());
583
- }
584
-
585
- /**
586
- * Processes a Zod object schema and converts it to a JSON Schema.
587
- *
588
- * @param zodSchema - The Zod object schema to process
589
- * @returns A JSONSchema7 object with provider-specific compatibility applied
590
- */
591
- public processToJSONSchema(zodSchema: z.ZodSchema): JSONSchema7 {
592
- return this.processToAISDKSchema(zodSchema).jsonSchema;
593
- }
594
- }