@mastra/schema-compat 0.11.2-alpha.1 → 0.11.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.
Files changed (39) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/{chunk-MKYBUMTK.js → chunk-F4D2N3FC.js} +5 -4
  3. package/dist/chunk-F4D2N3FC.js.map +1 -0
  4. package/dist/{chunk-V7Y3FXBJ.cjs → chunk-WRGSGOLQ.cjs} +5 -4
  5. package/dist/chunk-WRGSGOLQ.cjs.map +1 -0
  6. package/dist/index.cjs +3 -3
  7. package/dist/index.js +1 -1
  8. package/dist/zod-to-json.cjs +2 -2
  9. package/dist/zod-to-json.d.ts.map +1 -1
  10. package/dist/zod-to-json.js +1 -1
  11. package/package.json +14 -1
  12. package/.turbo/turbo-build.log +0 -4
  13. package/dist/chunk-MKYBUMTK.js.map +0 -1
  14. package/dist/chunk-V7Y3FXBJ.cjs.map +0 -1
  15. package/eslint.config.js +0 -6
  16. package/src/index.ts +0 -40
  17. package/src/provider-compats/anthropic.ts +0 -61
  18. package/src/provider-compats/deepseek.ts +0 -40
  19. package/src/provider-compats/google.ts +0 -49
  20. package/src/provider-compats/meta.ts +0 -41
  21. package/src/provider-compats/openai-reasoning.ts +0 -85
  22. package/src/provider-compats/openai.ts +0 -65
  23. package/src/provider-compats.test.ts +0 -407
  24. package/src/schema-compatibility-v3.ts +0 -664
  25. package/src/schema-compatibility-v4.test.ts +0 -476
  26. package/src/schema-compatibility-v4.ts +0 -706
  27. package/src/schema-compatibility.test.ts +0 -471
  28. package/src/schema-compatibility.ts +0 -471
  29. package/src/types.ts +0 -5
  30. package/src/utils-test-suite.ts +0 -467
  31. package/src/utils-v3.test.ts +0 -9
  32. package/src/utils-v4.test.ts +0 -9
  33. package/src/utils.ts +0 -211
  34. package/src/zod-to-json.ts +0 -28
  35. package/src/zodTypes.ts +0 -56
  36. package/tsconfig.build.json +0 -9
  37. package/tsconfig.json +0 -5
  38. package/tsup.config.ts +0 -22
  39. package/vitest.config.ts +0 -7
@@ -1,467 +0,0 @@
1
- import { jsonSchema } from 'ai';
2
- import type { Schema } from 'ai';
3
- import { MockLanguageModelV1 } from 'ai/test';
4
- import { describe, it, expect, beforeEach, vi } from 'vitest';
5
- import { z } from 'zod';
6
- import type { ZodType as ZodTypeV3 } from 'zod/v3';
7
- import type { ZodType as ZodTypeV4 } from 'zod/v4';
8
- import { SchemaCompatLayer } from './schema-compatibility';
9
- import type { ModelInformation } from './types';
10
- import { convertZodSchemaToAISDKSchema, convertSchemaToZod, applyCompatLayer, isZodType } from './utils';
11
-
12
- type ZodType = ZodTypeV3 | ZodTypeV4;
13
-
14
- const mockModel = new MockLanguageModelV1({
15
- modelId: 'test-model',
16
- defaultObjectGenerationMode: 'json',
17
- });
18
-
19
- class MockSchemaCompatibility extends SchemaCompatLayer {
20
- constructor(
21
- model: ModelInformation,
22
- private shouldApplyValue: boolean = true,
23
- ) {
24
- super(model);
25
- }
26
-
27
- shouldApply(): boolean {
28
- return this.shouldApplyValue;
29
- }
30
-
31
- getSchemaTarget() {
32
- return 'jsonSchema7' as const;
33
- }
34
-
35
- processZodType(value: ZodType): any {
36
- if (value.constructor.name === 'ZodString') {
37
- return z.string().describe('processed string');
38
- }
39
- if (value instanceof z.ZodObject) {
40
- return this.defaultZodObjectHandler(value);
41
- }
42
- if (value instanceof z.ZodArray) {
43
- return this.defaultZodArrayHandler(value);
44
- }
45
- return value;
46
- }
47
- }
48
-
49
- export function runTestSuite() {
50
- describe('Builder Functions', () => {
51
- describe('convertZodSchemaToAISDKSchema', () => {
52
- it('should convert simple Zod schema to AI SDK schema', () => {
53
- const zodSchema = z.object({
54
- name: z.string(),
55
- age: z.number(),
56
- });
57
-
58
- const result = convertZodSchemaToAISDKSchema(zodSchema);
59
-
60
- expect(result).toHaveProperty('jsonSchema');
61
- expect(result).toHaveProperty('validate');
62
- expect(typeof result.validate).toBe('function');
63
- });
64
-
65
- it('should create schema with validation function', () => {
66
- const zodSchema = z.object({
67
- email: z.string().email(),
68
- });
69
-
70
- const result = convertZodSchemaToAISDKSchema(zodSchema);
71
-
72
- expect(result.validate).toBeDefined();
73
-
74
- const validResult = result.validate!({ email: 'test@example.com' });
75
- expect(validResult.success).toBe(true);
76
- if (validResult.success) {
77
- expect(validResult.value).toEqual({ email: 'test@example.com' });
78
- }
79
-
80
- const invalidResult = result.validate!({ email: 'invalid-email' });
81
- expect(invalidResult.success).toBe(false);
82
- });
83
-
84
- it('should handle custom targets', () => {
85
- const zodSchema = z.object({
86
- name: z.string(),
87
- });
88
-
89
- const result = convertZodSchemaToAISDKSchema(zodSchema, 'openApi3');
90
-
91
- expect(result).toHaveProperty('jsonSchema');
92
- expect(result).toHaveProperty('validate');
93
- });
94
-
95
- it('should handle complex nested schemas', () => {
96
- const zodSchema = z.object({
97
- user: z.object({
98
- name: z.string(),
99
- preferences: z.object({
100
- theme: z.enum(['light', 'dark']),
101
- notifications: z.boolean(),
102
- }),
103
- }),
104
- tags: z.array(z.string()),
105
- });
106
-
107
- const result = convertZodSchemaToAISDKSchema(zodSchema);
108
-
109
- expect(result).toHaveProperty('jsonSchema');
110
- expect(result.jsonSchema).toHaveProperty('properties');
111
- });
112
-
113
- it('should handle array schemas', () => {
114
- const zodSchema = z.array(z.string());
115
- const result = convertZodSchemaToAISDKSchema(zodSchema);
116
- expect(result.jsonSchema.type).toBe('array');
117
- expect((result.jsonSchema.items as any)?.type).toBe('string');
118
- });
119
- });
120
-
121
- describe('convertSchemaToZod', () => {
122
- it('should return Zod schema unchanged', () => {
123
- const zodSchema = z.object({
124
- name: z.string(),
125
- });
126
-
127
- const result = convertSchemaToZod(zodSchema);
128
-
129
- expect(result).toBe(zodSchema);
130
- });
131
-
132
- it('should convert AI SDK schema to Zod', () => {
133
- const aiSchema: Schema = jsonSchema({
134
- type: 'object',
135
- properties: {
136
- name: { type: 'string' },
137
- age: { type: 'number' },
138
- },
139
- required: ['name'],
140
- });
141
-
142
- const result = convertSchemaToZod(aiSchema);
143
-
144
- expect(isZodType(result)).toBe(true);
145
- const parseResult = result.safeParse({ name: 'John', age: 30 });
146
- expect(parseResult.success).toBe(true);
147
- });
148
-
149
- it('should handle complex JSON schema conversion', () => {
150
- const complexSchema: Schema = jsonSchema({
151
- type: 'object',
152
- properties: {
153
- user: {
154
- type: 'object',
155
- properties: {
156
- name: { type: 'string' },
157
- email: { type: 'string', format: 'email' },
158
- },
159
- required: ['name'],
160
- },
161
- tags: {
162
- type: 'array',
163
- items: { type: 'string' },
164
- },
165
- },
166
- required: ['user'],
167
- });
168
-
169
- const result = convertSchemaToZod(complexSchema);
170
-
171
- expect(isZodType(result)).toBe(true);
172
-
173
- const validData = {
174
- user: { name: 'John', email: 'john@example.com' },
175
- tags: ['tag1', 'tag2'],
176
- };
177
- const parseResult = result.safeParse(validData);
178
- expect(parseResult.success).toBe(true);
179
- });
180
-
181
- it('should convert AI SDK array schema to Zod', () => {
182
- const aiSchema: Schema = jsonSchema({
183
- type: 'array',
184
- items: {
185
- type: 'string',
186
- },
187
- });
188
-
189
- const result = convertSchemaToZod(aiSchema);
190
-
191
- expect(result.constructor.name).toBe('ZodArray');
192
- // @ts-expect-error - this is a test so we can ignore the type error
193
- expect(result.element.constructor.name).toBe('ZodString');
194
- });
195
- });
196
-
197
- describe('applyCompatLayer', () => {
198
- let mockCompatibility: MockSchemaCompatibility;
199
-
200
- beforeEach(() => {
201
- mockCompatibility = new MockSchemaCompatibility({
202
- modelId: mockModel.modelId,
203
- supportsStructuredOutputs: mockModel.supportsStructuredOutputs ?? false,
204
- provider: mockModel.provider,
205
- });
206
- });
207
-
208
- it('should process Zod object schema with compatibility', () => {
209
- const zodSchema = z.object({
210
- name: z.string(),
211
- age: z.number(),
212
- });
213
-
214
- const result = applyCompatLayer({
215
- schema: zodSchema,
216
- compatLayers: [mockCompatibility],
217
- mode: 'aiSdkSchema',
218
- });
219
-
220
- expect(result).toHaveProperty('jsonSchema');
221
- expect(result).toHaveProperty('validate');
222
- });
223
-
224
- it('should process AI SDK schema with compatibility', () => {
225
- const aiSchema: Schema = jsonSchema({
226
- type: 'object',
227
- properties: {
228
- name: { type: 'string' },
229
- },
230
- });
231
-
232
- const result = applyCompatLayer({
233
- schema: aiSchema,
234
- compatLayers: [mockCompatibility],
235
- mode: 'jsonSchema',
236
- });
237
-
238
- expect(typeof result).toBe('object');
239
- expect(result).toHaveProperty('type');
240
- });
241
-
242
- it('should handle object schema with string property', () => {
243
- const stringSchema = z.object({ value: z.string() });
244
-
245
- const result = applyCompatLayer({
246
- schema: stringSchema,
247
- compatLayers: [mockCompatibility],
248
- mode: 'aiSdkSchema',
249
- });
250
-
251
- expect(result).toHaveProperty('jsonSchema');
252
- expect(result).toHaveProperty('validate');
253
- });
254
-
255
- it('should return processed schema when compatibility applies', () => {
256
- const zodSchema = z.object({
257
- name: z.string(),
258
- });
259
-
260
- const result = applyCompatLayer({
261
- schema: zodSchema,
262
- compatLayers: [mockCompatibility],
263
- mode: 'aiSdkSchema',
264
- });
265
-
266
- expect(result).toHaveProperty('jsonSchema');
267
- expect(result).toHaveProperty('validate');
268
- });
269
-
270
- it('should return fallback when no compatibility applies', () => {
271
- const nonApplyingCompatibility = new MockSchemaCompatibility(
272
- {
273
- modelId: mockModel.modelId,
274
- supportsStructuredOutputs: mockModel.supportsStructuredOutputs ?? false,
275
- provider: mockModel.provider,
276
- },
277
- false,
278
- );
279
- const zodSchema = z.object({
280
- name: z.string(),
281
- });
282
-
283
- const result = applyCompatLayer({
284
- schema: zodSchema,
285
- compatLayers: [nonApplyingCompatibility],
286
- mode: 'aiSdkSchema',
287
- });
288
-
289
- expect(result).toHaveProperty('jsonSchema');
290
- expect(result).toHaveProperty('validate');
291
- });
292
-
293
- it('should handle jsonSchema mode', () => {
294
- const zodSchema = z.object({
295
- name: z.string(),
296
- });
297
-
298
- const result = applyCompatLayer({
299
- schema: zodSchema,
300
- compatLayers: [mockCompatibility],
301
- mode: 'jsonSchema',
302
- });
303
-
304
- expect(typeof result).toBe('object');
305
- expect(result).toHaveProperty('type');
306
- });
307
-
308
- it('should handle empty compatLayers array', () => {
309
- const zodSchema = z.object({
310
- name: z.string(),
311
- });
312
-
313
- const result = applyCompatLayer({
314
- schema: zodSchema,
315
- compatLayers: [],
316
- mode: 'aiSdkSchema',
317
- });
318
-
319
- expect(result).toHaveProperty('jsonSchema');
320
- expect(result).toHaveProperty('validate');
321
- });
322
-
323
- it('should handle complex schema with multiple compatLayers', () => {
324
- const compat1 = new MockSchemaCompatibility(
325
- {
326
- modelId: mockModel.modelId,
327
- supportsStructuredOutputs: mockModel.supportsStructuredOutputs ?? true,
328
- provider: mockModel.provider,
329
- },
330
- false,
331
- );
332
- const compat2 = new MockSchemaCompatibility(
333
- {
334
- modelId: mockModel.modelId,
335
- supportsStructuredOutputs: mockModel.supportsStructuredOutputs ?? false,
336
- provider: mockModel.provider,
337
- },
338
- true,
339
- );
340
-
341
- vi.spyOn(compat1, 'processZodType');
342
- vi.spyOn(compat2, 'processZodType');
343
-
344
- const zodSchema = z.object({
345
- name: z.string(),
346
- settings: z.object({
347
- theme: z.string(),
348
- notifications: z.boolean(),
349
- }),
350
- });
351
-
352
- const result = applyCompatLayer({
353
- schema: zodSchema,
354
- compatLayers: [compat1, compat2],
355
- mode: 'aiSdkSchema',
356
- });
357
-
358
- expect(result).toHaveProperty('jsonSchema');
359
- expect(result).toHaveProperty('validate');
360
- expect(compat1.processZodType).not.toHaveBeenCalled();
361
- expect(compat2.processZodType).toHaveBeenCalled();
362
- });
363
-
364
- it('should process Zod array schema with compatibility', () => {
365
- const arraySchema = z.array(z.string());
366
-
367
- const result = applyCompatLayer({
368
- schema: arraySchema,
369
- compatLayers: [mockCompatibility],
370
- mode: 'aiSdkSchema',
371
- });
372
-
373
- expect(result.jsonSchema.type).toBe('array');
374
- if (
375
- result.jsonSchema.items &&
376
- !Array.isArray(result.jsonSchema.items) &&
377
- typeof result.jsonSchema.items === 'object'
378
- ) {
379
- expect(result.jsonSchema.items.type).toBe('string');
380
- expect(result.jsonSchema.items.description).toBe('processed string');
381
- } else {
382
- expect.fail('items is not a single schema object');
383
- }
384
- });
385
-
386
- it('should process AI SDK array schema with compatibility', () => {
387
- const aiSchema: Schema = jsonSchema({
388
- type: 'array',
389
- items: {
390
- type: 'string',
391
- },
392
- });
393
-
394
- const result = applyCompatLayer({
395
- schema: aiSchema,
396
- compatLayers: [mockCompatibility],
397
- mode: 'aiSdkSchema',
398
- });
399
-
400
- expect(result.jsonSchema.type).toBe('array');
401
- if (
402
- result.jsonSchema.items &&
403
- !Array.isArray(result.jsonSchema.items) &&
404
- typeof result.jsonSchema.items === 'object'
405
- ) {
406
- expect(result.jsonSchema.items.type).toBe('string');
407
- expect(result.jsonSchema.items.description).toBe('processed string');
408
- } else {
409
- expect.fail('items is not a single schema object');
410
- }
411
- });
412
-
413
- it('should handle a complex array of objects schema', () => {
414
- const complexArraySchema = z.array(
415
- z.object({
416
- id: z.string(),
417
- user: z.object({
418
- name: z.string(),
419
- }),
420
- }),
421
- );
422
-
423
- const result = applyCompatLayer({
424
- schema: complexArraySchema,
425
- compatLayers: [mockCompatibility],
426
- mode: 'aiSdkSchema',
427
- });
428
-
429
- const { jsonSchema } = result;
430
- expect(jsonSchema.type).toBe('array');
431
-
432
- const items = jsonSchema.items;
433
- if (items && !Array.isArray(items) && typeof items === 'object') {
434
- expect(items.type).toBe('object');
435
- expect(items.properties).toHaveProperty('id');
436
- expect(items.properties).toHaveProperty('user');
437
-
438
- const idProperty = items.properties!.id as any;
439
- expect(idProperty.description).toBe('processed string');
440
-
441
- const userProperty = items.properties!.user as any;
442
- expect(userProperty.type).toBe('object');
443
- expect(userProperty.properties).toHaveProperty('name');
444
-
445
- const nameProperty = userProperty.properties.name as any;
446
- expect(nameProperty.description).toBe('processed string');
447
- } else {
448
- expect.fail('items is not a single schema object');
449
- }
450
- });
451
-
452
- it('should handle a scalar zod schema', () => {
453
- const scalarSchema = z.string().email();
454
-
455
- const result = applyCompatLayer({
456
- schema: scalarSchema,
457
- compatLayers: [mockCompatibility],
458
- mode: 'aiSdkSchema',
459
- });
460
-
461
- const { jsonSchema } = result;
462
- expect(jsonSchema.type).toBe('string');
463
- expect(jsonSchema.description).toBe('processed string');
464
- });
465
- });
466
- });
467
- }
@@ -1,9 +0,0 @@
1
- import { vi } from 'vitest';
2
- import { z as zV3 } from 'zod/v3';
3
- import { runTestSuite } from './utils-test-suite';
4
-
5
- vi.mock('zod', () => ({
6
- z: zV3,
7
- }));
8
-
9
- runTestSuite();
@@ -1,9 +0,0 @@
1
- import { vi } from 'vitest';
2
- import { z as zV4 } from 'zod/v4';
3
- import { runTestSuite } from './utils-test-suite';
4
-
5
- vi.mock('zod', () => ({
6
- z: zV4,
7
- }));
8
-
9
- runTestSuite();
package/src/utils.ts DELETED
@@ -1,211 +0,0 @@
1
- import { jsonSchema } from 'ai';
2
- import type { Schema } from 'ai';
3
- import type { JSONSchema7 } from 'json-schema';
4
- import { z } from 'zod';
5
- import type { ZodSchema as ZodSchemaV3, ZodType as ZodTypeV3 } from 'zod/v3';
6
- import type { ZodType as ZodSchemaV4, ZodType as ZodTypeV4 } from 'zod/v4';
7
- import { convertJsonSchemaToZod } from 'zod-from-json-schema';
8
- import { convertJsonSchemaToZod as convertJsonSchemaToZodV3 } from 'zod-from-json-schema-v3';
9
- import type { JSONSchema } from 'zod-from-json-schema-v3';
10
- import type { Targets } from 'zod-to-json-schema';
11
- import type { SchemaCompatLayer } from './schema-compatibility';
12
- import { zodToJsonSchema } from './zod-to-json';
13
-
14
- type ZodSchema = ZodSchemaV3 | ZodSchemaV4;
15
- type ZodType = ZodTypeV3 | ZodTypeV4;
16
-
17
- /**
18
- * Converts a Zod schema to an AI SDK Schema with validation support.
19
- *
20
- * This function mirrors the behavior of Vercel's AI SDK zod-schema utility but allows
21
- * customization of the JSON Schema target format.
22
- *
23
- * @param zodSchema - The Zod schema to convert
24
- * @param target - The JSON Schema target format (defaults to 'jsonSchema7')
25
- * @returns An AI SDK Schema object with built-in validation
26
- *
27
- * @example
28
- * ```typescript
29
- * import { z } from 'zod';
30
- * import { convertZodSchemaToAISDKSchema } from '@mastra/schema-compat';
31
- *
32
- * const userSchema = z.object({
33
- * name: z.string(),
34
- * age: z.number().min(0)
35
- * });
36
- *
37
- * const aiSchema = convertZodSchemaToAISDKSchema(userSchema);
38
- * ```
39
- */
40
- // mirrors https://github.com/vercel/ai/blob/main/packages/ui-utils/src/zod-schema.ts#L21 but with a custom target
41
- export function convertZodSchemaToAISDKSchema(zodSchema: ZodSchema, target: Targets = 'jsonSchema7') {
42
- const jsonSchemaToUse = zodToJsonSchema(zodSchema, target) as JSONSchema7;
43
-
44
- return jsonSchema(jsonSchemaToUse, {
45
- validate: value => {
46
- const result = zodSchema.safeParse(value);
47
- return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
48
- },
49
- });
50
- }
51
-
52
- /**
53
- * Checks if a value is a Zod type by examining its properties and methods.
54
- *
55
- * @param value - The value to check
56
- * @returns True if the value is a Zod type, false otherwise
57
- * @internal
58
- */
59
- export function isZodType(value: unknown): value is ZodType {
60
- // Check if it's a Zod schema by looking for common Zod properties and methods
61
- return (
62
- typeof value === 'object' &&
63
- value !== null &&
64
- '_def' in value &&
65
- 'parse' in value &&
66
- typeof (value as any).parse === 'function' &&
67
- 'safeParse' in value &&
68
- typeof (value as any).safeParse === 'function'
69
- );
70
- }
71
-
72
- /**
73
- * Converts an AI SDK Schema or Zod schema to a Zod schema.
74
- *
75
- * If the input is already a Zod schema, it returns it unchanged.
76
- * If the input is an AI SDK Schema, it extracts the JSON schema and converts it to Zod.
77
- *
78
- * @param schema - The schema to convert (AI SDK Schema or Zod schema)
79
- * @returns A Zod schema equivalent of the input
80
- * @throws Error if the conversion fails
81
- *
82
- * @example
83
- * ```typescript
84
- * import { jsonSchema } from 'ai';
85
- * import { convertSchemaToZod } from '@mastra/schema-compat';
86
- *
87
- * const aiSchema = jsonSchema({
88
- * type: 'object',
89
- * properties: {
90
- * name: { type: 'string' }
91
- * }
92
- * });
93
- *
94
- * const zodSchema = convertSchemaToZod(aiSchema);
95
- * ```
96
- */
97
- export function convertSchemaToZod(schema: Schema | ZodSchema): ZodType {
98
- if (isZodType(schema)) {
99
- return schema;
100
- } else {
101
- const jsonSchemaToConvert = ('jsonSchema' in schema ? schema.jsonSchema : schema) as JSONSchema;
102
- try {
103
- if ('toJSONSchema' in z) {
104
- // @ts-expect-error - zod type issue
105
- return convertJsonSchemaToZod(jsonSchemaToConvert);
106
- } else {
107
- return convertJsonSchemaToZodV3(jsonSchemaToConvert);
108
- }
109
- } catch (e: unknown) {
110
- const errorMessage = `[Schema Builder] Failed to convert schema parameters to Zod. Original schema: ${JSON.stringify(jsonSchemaToConvert)}`;
111
- console.error(errorMessage, e);
112
- throw new Error(errorMessage + (e instanceof Error ? `\n${e.stack}` : '\nUnknown error object'));
113
- }
114
- }
115
- }
116
-
117
- /**
118
- * Processes a schema using provider compatibility layers and converts it to an AI SDK Schema.
119
- *
120
- * @param options - Configuration object for schema processing
121
- * @param options.schema - The schema to process (AI SDK Schema or Zod object schema)
122
- * @param options.compatLayers - Array of compatibility layers to try
123
- * @param options.mode - Must be 'aiSdkSchema'
124
- * @returns Processed schema as an AI SDK Schema
125
- */
126
- export function applyCompatLayer(options: {
127
- schema: Schema | ZodSchema;
128
- compatLayers: SchemaCompatLayer[];
129
- mode: 'aiSdkSchema';
130
- }): Schema;
131
-
132
- /**
133
- * Processes a schema using provider compatibility layers and converts it to a JSON Schema.
134
- *
135
- * @param options - Configuration object for schema processing
136
- * @param options.schema - The schema to process (AI SDK Schema or Zod object schema)
137
- * @param options.compatLayers - Array of compatibility layers to try
138
- * @param options.mode - Must be 'jsonSchema'
139
- * @returns Processed schema as a JSONSchema7
140
- */
141
- export function applyCompatLayer(options: {
142
- schema: Schema | ZodSchema;
143
- compatLayers: SchemaCompatLayer[];
144
- mode: 'jsonSchema';
145
- }): JSONSchema7;
146
-
147
- /**
148
- * Processes a schema using provider compatibility layers and converts it to the specified format.
149
- *
150
- * This function automatically applies the first matching compatibility layer from the provided
151
- * list based on the model configuration. If no compatibility applies, it falls back to
152
- * standard conversion.
153
- *
154
- * @param options - Configuration object for schema processing
155
- * @param options.schema - The schema to process (AI SDK Schema or Zod object schema)
156
- * @param options.compatLayers - Array of compatibility layers to try
157
- * @param options.mode - Output format: 'jsonSchema' for JSONSchema7 or 'aiSdkSchema' for AI SDK Schema
158
- * @returns Processed schema in the requested format
159
- *
160
- * @example
161
- * ```typescript
162
- * import { z } from 'zod';
163
- * import { applyCompatLayer, OpenAISchemaCompatLayer, AnthropicSchemaCompatLayer } from '@mastra/schema-compat';
164
- *
165
- * const schema = z.object({
166
- * query: z.string().email(),
167
- * limit: z.number().min(1).max(100)
168
- * });
169
- *
170
- * const compatLayers = [
171
- * new OpenAISchemaCompatLayer(model),
172
- * new AnthropicSchemaCompatLayer(model)
173
- * ];
174
- *
175
- * const result = applyCompatLayer({
176
- * schema,
177
- * compatLayers,
178
- * mode: 'aiSdkSchema'
179
- * });
180
- * ```
181
- */
182
- export function applyCompatLayer({
183
- schema,
184
- compatLayers,
185
- mode,
186
- }: {
187
- schema: Schema | ZodSchema;
188
- compatLayers: SchemaCompatLayer[];
189
- mode: 'jsonSchema' | 'aiSdkSchema';
190
- }): JSONSchema7 | Schema {
191
- let zodSchema: ZodSchema;
192
-
193
- if (!isZodType(schema)) {
194
- // Convert non-zod schema to Zod
195
- zodSchema = convertSchemaToZod(schema);
196
- } else {
197
- zodSchema = schema;
198
- }
199
-
200
- for (const compat of compatLayers) {
201
- if (compat.shouldApply()) {
202
- return mode === 'jsonSchema' ? compat.processToJSONSchema(zodSchema) : compat.processToAISDKSchema(zodSchema);
203
- }
204
- }
205
- // If no compatibility applied, convert back to appropriate format
206
- if (mode === 'jsonSchema') {
207
- return zodToJsonSchema(zodSchema, 'jsonSchema7') as JSONSchema7;
208
- } else {
209
- return convertZodSchemaToAISDKSchema(zodSchema);
210
- }
211
- }