@mastra/schema-compat 0.10.5 → 0.10.6

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.
@@ -1,507 +0,0 @@
1
- import type { JSONSchema7 } from 'json-schema';
2
- import type { LanguageModelV1 } from 'ai';
3
- import type { Schema } from 'ai';
4
- import type { Targets } from 'zod-to-json-schema';
5
- import { z } from 'zod';
6
- import { ZodArray } from 'zod';
7
- import { ZodDate } from 'zod';
8
- import { ZodDefault } from 'zod';
9
- import { ZodNull } from 'zod';
10
- import { ZodNumber } from 'zod';
11
- import { ZodObject } from 'zod';
12
- import { ZodOptional } from 'zod';
13
- import type { ZodSchema } from 'zod';
14
- import { ZodString } from 'zod';
15
- import type { ZodTypeAny } from 'zod';
16
- import { ZodUnion } from 'zod';
17
-
18
- /**
19
- * All supported array validation check types that can be processed or converted to descriptions.
20
- * @constant
21
- */
22
- declare const ALL_ARRAY_CHECKS: readonly ["min", "max", "length"];
23
- export { ALL_ARRAY_CHECKS }
24
- export { ALL_ARRAY_CHECKS as ALL_ARRAY_CHECKS_alias_1 }
25
-
26
- /**
27
- * All supported number validation check types that can be processed or converted to descriptions.
28
- * @constant
29
- */
30
- declare const ALL_NUMBER_CHECKS: readonly ["min", "max", "multipleOf"];
31
- export { ALL_NUMBER_CHECKS }
32
- export { ALL_NUMBER_CHECKS as ALL_NUMBER_CHECKS_alias_1 }
33
-
34
- /**
35
- * All supported string validation check types that can be processed or converted to descriptions.
36
- * @constant
37
- */
38
- declare const ALL_STRING_CHECKS: readonly ["regex", "emoji", "email", "url", "uuid", "cuid", "min", "max"];
39
- export { ALL_STRING_CHECKS }
40
- export { ALL_STRING_CHECKS as ALL_STRING_CHECKS_alias_1 }
41
-
42
- /**
43
- * All Zod types (both supported and unsupported).
44
- * @constant
45
- */
46
- declare const ALL_ZOD_TYPES: readonly ["ZodObject", "ZodArray", "ZodUnion", "ZodString", "ZodNumber", "ZodDate", "ZodAny", "ZodDefault", "ZodIntersection", "ZodNever", "ZodNull", "ZodTuple", "ZodUndefined"];
47
- export { ALL_ZOD_TYPES }
48
- export { ALL_ZOD_TYPES as ALL_ZOD_TYPES_alias_1 }
49
-
50
- /**
51
- * Type representing all Zod schema types (supported and unsupported).
52
- */
53
- declare type AllZodType = (typeof ALL_ZOD_TYPES)[number];
54
- export { AllZodType }
55
- export { AllZodType as AllZodType_alias_1 }
56
-
57
- declare class AnthropicSchemaCompatLayer extends SchemaCompatLayer {
58
- constructor(model: LanguageModelV1);
59
- getSchemaTarget(): Targets | undefined;
60
- shouldApply(): boolean;
61
- processZodType(value: ZodTypeAny): ZodTypeAny;
62
- }
63
- export { AnthropicSchemaCompatLayer }
64
- export { AnthropicSchemaCompatLayer as AnthropicSchemaCompatLayer_alias_1 }
65
-
66
- /**
67
- * Processes a schema using provider compatibility layers and converts it to an AI SDK Schema.
68
- *
69
- * @param options - Configuration object for schema processing
70
- * @param options.schema - The schema to process (AI SDK Schema or Zod object schema)
71
- * @param options.compatLayers - Array of compatibility layers to try
72
- * @param options.mode - Must be 'aiSdkSchema'
73
- * @returns Processed schema as an AI SDK Schema
74
- */
75
- declare function applyCompatLayer(options: {
76
- schema: Schema | z.ZodSchema;
77
- compatLayers: SchemaCompatLayer[];
78
- mode: 'aiSdkSchema';
79
- }): Schema;
80
-
81
- /**
82
- * Processes a schema using provider compatibility layers and converts it to a JSON Schema.
83
- *
84
- * @param options - Configuration object for schema processing
85
- * @param options.schema - The schema to process (AI SDK Schema or Zod object schema)
86
- * @param options.compatLayers - Array of compatibility layers to try
87
- * @param options.mode - Must be 'jsonSchema'
88
- * @returns Processed schema as a JSONSchema7
89
- */
90
- declare function applyCompatLayer(options: {
91
- schema: Schema | z.ZodSchema;
92
- compatLayers: SchemaCompatLayer[];
93
- mode: 'jsonSchema';
94
- }): JSONSchema7;
95
- export { applyCompatLayer }
96
- export { applyCompatLayer as applyCompatLayer_alias_1 }
97
-
98
- /**
99
- * Type representing array validation checks.
100
- */
101
- declare type ArrayCheckType = (typeof ALL_ARRAY_CHECKS)[number];
102
- export { ArrayCheckType }
103
- export { ArrayCheckType as ArrayCheckType_alias_1 }
104
-
105
- declare type ArrayConstraints = {
106
- minLength?: number;
107
- maxLength?: number;
108
- exactLength?: number;
109
- };
110
-
111
- /**
112
- * Converts an AI SDK Schema or Zod schema to a Zod schema.
113
- *
114
- * If the input is already a Zod schema, it returns it unchanged.
115
- * If the input is an AI SDK Schema, it extracts the JSON schema and converts it to Zod.
116
- *
117
- * @param schema - The schema to convert (AI SDK Schema or Zod schema)
118
- * @returns A Zod schema equivalent of the input
119
- * @throws Error if the conversion fails
120
- *
121
- * @example
122
- * ```typescript
123
- * import { jsonSchema } from 'ai';
124
- * import { convertSchemaToZod } from '@mastra/schema-compat';
125
- *
126
- * const aiSchema = jsonSchema({
127
- * type: 'object',
128
- * properties: {
129
- * name: { type: 'string' }
130
- * }
131
- * });
132
- *
133
- * const zodSchema = convertSchemaToZod(aiSchema);
134
- * ```
135
- */
136
- declare function convertSchemaToZod(schema: Schema | z.ZodSchema): z.ZodType;
137
- export { convertSchemaToZod }
138
- export { convertSchemaToZod as convertSchemaToZod_alias_1 }
139
-
140
- /**
141
- * Converts a Zod schema to an AI SDK Schema with validation support.
142
- *
143
- * This function mirrors the behavior of Vercel's AI SDK zod-schema utility but allows
144
- * customization of the JSON Schema target format.
145
- *
146
- * @param zodSchema - The Zod schema to convert
147
- * @param target - The JSON Schema target format (defaults to 'jsonSchema7')
148
- * @returns An AI SDK Schema object with built-in validation
149
- *
150
- * @example
151
- * ```typescript
152
- * import { z } from 'zod';
153
- * import { convertZodSchemaToAISDKSchema } from '@mastra/schema-compat';
154
- *
155
- * const userSchema = z.object({
156
- * name: z.string(),
157
- * age: z.number().min(0)
158
- * });
159
- *
160
- * const aiSchema = convertZodSchemaToAISDKSchema(userSchema);
161
- * ```
162
- */
163
- declare function convertZodSchemaToAISDKSchema(zodSchema: ZodSchema, target?: Targets): Schema<any>;
164
- export { convertZodSchemaToAISDKSchema }
165
- export { convertZodSchemaToAISDKSchema as convertZodSchemaToAISDKSchema_alias_1 }
166
-
167
- declare type DateConstraints = {
168
- minDate?: string;
169
- maxDate?: string;
170
- dateFormat?: string;
171
- };
172
-
173
- declare class DeepSeekSchemaCompatLayer extends SchemaCompatLayer {
174
- constructor(model: LanguageModelV1);
175
- getSchemaTarget(): Targets | undefined;
176
- shouldApply(): boolean;
177
- processZodType(value: ZodTypeAny): ZodTypeAny;
178
- }
179
- export { DeepSeekSchemaCompatLayer }
180
- export { DeepSeekSchemaCompatLayer as DeepSeekSchemaCompatLayer_alias_1 }
181
-
182
- declare class GoogleSchemaCompatLayer extends SchemaCompatLayer {
183
- constructor(model: LanguageModelV1);
184
- getSchemaTarget(): Targets | undefined;
185
- shouldApply(): boolean;
186
- processZodType(value: ZodTypeAny): ZodTypeAny;
187
- }
188
- export { GoogleSchemaCompatLayer }
189
- export { GoogleSchemaCompatLayer as GoogleSchemaCompatLayer_alias_1 }
190
-
191
- declare const isArr: (v: ZodTypeAny) => v is ZodArray<any, any>;
192
- export { isArr }
193
- export { isArr as isArr_alias_1 }
194
-
195
- export declare const isDate: (v: ZodTypeAny) => v is ZodDate;
196
-
197
- export declare const isDefault: (v: ZodTypeAny) => v is ZodDefault<any>;
198
-
199
- export declare const isNull: (v: ZodTypeAny) => v is ZodNull;
200
-
201
- declare const isNumber: (v: ZodTypeAny) => v is ZodNumber;
202
- export { isNumber }
203
- export { isNumber as isNumber_alias_1 }
204
-
205
- declare const isObj: (v: ZodTypeAny) => v is ZodObject<any, any, any>;
206
- export { isObj }
207
- export { isObj as isObj_alias_1 }
208
-
209
- declare const isOptional: (v: ZodTypeAny) => v is ZodOptional<any>;
210
- export { isOptional }
211
- export { isOptional as isOptional_alias_1 }
212
-
213
- declare const isString: (v: ZodTypeAny) => v is ZodString;
214
- export { isString }
215
- export { isString as isString_alias_1 }
216
-
217
- declare const isUnion: (v: ZodTypeAny) => v is ZodUnion<[ZodTypeAny, ...ZodTypeAny[]]>;
218
- export { isUnion }
219
- export { isUnion as isUnion_alias_1 }
220
-
221
- declare class MetaSchemaCompatLayer extends SchemaCompatLayer {
222
- constructor(model: LanguageModelV1);
223
- getSchemaTarget(): Targets | undefined;
224
- shouldApply(): boolean;
225
- processZodType(value: ZodTypeAny): ZodTypeAny;
226
- }
227
- export { MetaSchemaCompatLayer }
228
- export { MetaSchemaCompatLayer as MetaSchemaCompatLayer_alias_1 }
229
-
230
- /**
231
- * Type representing number validation checks.
232
- */
233
- declare type NumberCheckType = (typeof ALL_NUMBER_CHECKS)[number];
234
- export { NumberCheckType }
235
- export { NumberCheckType as NumberCheckType_alias_1 }
236
-
237
- declare type NumberConstraints = {
238
- gt?: number;
239
- gte?: number;
240
- lt?: number;
241
- lte?: number;
242
- multipleOf?: number;
243
- };
244
-
245
- declare class OpenAIReasoningSchemaCompatLayer extends SchemaCompatLayer {
246
- constructor(model: LanguageModelV1);
247
- getSchemaTarget(): Targets | undefined;
248
- isReasoningModel(): boolean;
249
- shouldApply(): boolean;
250
- processZodType(value: ZodTypeAny): ZodTypeAny;
251
- }
252
- export { OpenAIReasoningSchemaCompatLayer }
253
- export { OpenAIReasoningSchemaCompatLayer as OpenAIReasoningSchemaCompatLayer_alias_1 }
254
-
255
- declare class OpenAISchemaCompatLayer extends SchemaCompatLayer {
256
- constructor(model: LanguageModelV1);
257
- getSchemaTarget(): Targets | undefined;
258
- shouldApply(): boolean;
259
- processZodType(value: ZodTypeAny): ZodTypeAny;
260
- }
261
- export { OpenAISchemaCompatLayer }
262
- export { OpenAISchemaCompatLayer as OpenAISchemaCompatLayer_alias_1 }
263
-
264
- /**
265
- * Abstract base class for creating schema compatibility layers for different AI model providers.
266
- *
267
- * This class provides a framework for transforming Zod schemas to work with specific AI model
268
- * provider requirements and limitations. Each provider may have different support levels for
269
- * JSON Schema features, validation constraints, and data types.
270
- *
271
- * @abstract
272
- *
273
- * @example
274
- * ```typescript
275
- * import { SchemaCompatLayer } from '@mastra/schema-compat';
276
- * import type { LanguageModelV1 } from 'ai';
277
- *
278
- * class CustomProviderCompat extends SchemaCompatLayer {
279
- * constructor(model: LanguageModelV1) {
280
- * super(model);
281
- * }
282
- *
283
- * shouldApply(): boolean {
284
- * return this.getModel().provider === 'custom-provider';
285
- * }
286
- *
287
- * getSchemaTarget() {
288
- * return 'jsonSchema7';
289
- * }
290
- *
291
- * processZodType<T extends z.AnyZodObject>(value: z.ZodTypeAny): ShapeValue<T> {
292
- * // Custom processing logic for this provider
293
- * switch (value._def.typeName) {
294
- * case 'ZodString':
295
- * return this.defaultZodStringHandler(value, ['email', 'url']);
296
- * default:
297
- * return this.defaultUnsupportedZodTypeHandler(value);
298
- * }
299
- * }
300
- * }
301
- * ```
302
- */
303
- declare abstract class SchemaCompatLayer {
304
- private model;
305
- /**
306
- * Creates a new schema compatibility instance.
307
- *
308
- * @param model - The language model this compatibility layer applies to
309
- */
310
- constructor(model: LanguageModelV1);
311
- /**
312
- * Gets the language model associated with this compatibility layer.
313
- *
314
- * @returns The language model instance
315
- */
316
- getModel(): LanguageModelV1;
317
- /**
318
- * Determines whether this compatibility layer should be applied for the current model.
319
- *
320
- * @returns True if this compatibility layer should be used, false otherwise
321
- * @abstract
322
- */
323
- abstract shouldApply(): boolean;
324
- /**
325
- * Returns the JSON Schema target format for this provider.
326
- *
327
- * @returns The schema target format, or undefined to use the default 'jsonSchema7'
328
- * @abstract
329
- */
330
- abstract getSchemaTarget(): Targets | undefined;
331
- /**
332
- * Processes a specific Zod type according to the provider's requirements.
333
- *
334
- * @param value - The Zod type to process
335
- * @returns The processed Zod type
336
- * @abstract
337
- */
338
- abstract processZodType(value: ZodTypeAny): ZodTypeAny;
339
- /**
340
- * Default handler for Zod object types. Recursively processes all properties in the object.
341
- *
342
- * @param value - The Zod object to process
343
- * @returns The processed Zod object
344
- */
345
- defaultZodObjectHandler(value: ZodObject<any, any, any>, options?: {
346
- passthrough?: boolean;
347
- }): ZodObject<any, any, any>;
348
- /**
349
- * Merges validation constraints into a parameter description.
350
- *
351
- * This helper method converts validation constraints that may not be supported
352
- * by a provider into human-readable descriptions.
353
- *
354
- * @param description - The existing parameter description
355
- * @param constraints - The validation constraints to merge
356
- * @returns The updated description with constraints, or undefined if no constraints
357
- */
358
- mergeParameterDescription(description: string | undefined, constraints: NumberConstraints | StringConstraints | ArrayConstraints | DateConstraints | {
359
- defaultValue?: unknown;
360
- }): string | undefined;
361
- /**
362
- * Default handler for unsupported Zod types. Throws an error for specified unsupported types.
363
- *
364
- * @param value - The Zod type to check
365
- * @param throwOnTypes - Array of type names to throw errors for
366
- * @returns The original value if not in the throw list
367
- * @throws Error if the type is in the unsupported list
368
- */
369
- defaultUnsupportedZodTypeHandler<T extends z.AnyZodObject>(value: z.ZodTypeAny, throwOnTypes?: readonly UnsupportedZodType[]): ShapeValue<T>;
370
- /**
371
- * Default handler for Zod array types. Processes array constraints according to provider support.
372
- *
373
- * @param value - The Zod array to process
374
- * @param handleChecks - Array constraints to convert to descriptions vs keep as validation
375
- * @returns The processed Zod array
376
- */
377
- defaultZodArrayHandler(value: ZodArray<any, any>, handleChecks?: readonly ArrayCheckType[]): ZodArray<any, any>;
378
- /**
379
- * Default handler for Zod union types. Processes all union options.
380
- *
381
- * @param value - The Zod union to process
382
- * @returns The processed Zod union
383
- * @throws Error if union has fewer than 2 options
384
- */
385
- defaultZodUnionHandler(value: ZodUnion<[ZodTypeAny, ...ZodTypeAny[]]>): ZodTypeAny;
386
- /**
387
- * Default handler for Zod string types. Processes string validation constraints.
388
- *
389
- * @param value - The Zod string to process
390
- * @param handleChecks - String constraints to convert to descriptions vs keep as validation
391
- * @returns The processed Zod string
392
- */
393
- defaultZodStringHandler(value: ZodString, handleChecks?: readonly StringCheckType[]): ZodString;
394
- /**
395
- * Default handler for Zod number types. Processes number validation constraints.
396
- *
397
- * @param value - The Zod number to process
398
- * @param handleChecks - Number constraints to convert to descriptions vs keep as validation
399
- * @returns The processed Zod number
400
- */
401
- defaultZodNumberHandler(value: ZodNumber, handleChecks?: readonly NumberCheckType[]): ZodNumber;
402
- /**
403
- * Default handler for Zod date types. Converts dates to ISO strings with constraint descriptions.
404
- *
405
- * @param value - The Zod date to process
406
- * @returns A Zod string schema representing the date in ISO format
407
- */
408
- defaultZodDateHandler(value: ZodDate): ZodString;
409
- /**
410
- * Default handler for Zod optional types. Processes the inner type and maintains optionality.
411
- *
412
- * @param value - The Zod optional to process
413
- * @param handleTypes - Types that should be processed vs passed through
414
- * @returns The processed Zod optional
415
- */
416
- defaultZodOptionalHandler(value: ZodOptional<any>, handleTypes?: readonly AllZodType[]): ZodTypeAny;
417
- /**
418
- * Processes a Zod object schema and converts it to an AI SDK Schema.
419
- *
420
- * @param zodSchema - The Zod object schema to process
421
- * @returns An AI SDK Schema with provider-specific compatibility applied
422
- */
423
- processToAISDKSchema(zodSchema: z.ZodSchema): Schema;
424
- /**
425
- * Processes a Zod object schema and converts it to a JSON Schema.
426
- *
427
- * @param zodSchema - The Zod object schema to process
428
- * @returns A JSONSchema7 object with provider-specific compatibility applied
429
- */
430
- processToJSONSchema(zodSchema: z.ZodSchema): JSONSchema7;
431
- }
432
- export { SchemaCompatLayer }
433
- export { SchemaCompatLayer as SchemaCompatLayer_alias_1 }
434
-
435
- /**
436
- * Utility type to extract the keys from a Zod object shape.
437
- */
438
- declare type ShapeKey<T extends z.AnyZodObject> = keyof ZodShape<T>;
439
- export { ShapeKey }
440
- export { ShapeKey as ShapeKey_alias_1 }
441
-
442
- /**
443
- * Utility type to extract the value types from a Zod object shape.
444
- */
445
- declare type ShapeValue<T extends z.AnyZodObject> = ZodShape<T>[ShapeKey<T>];
446
- export { ShapeValue }
447
- export { ShapeValue as ShapeValue_alias_1 }
448
-
449
- /**
450
- * Type representing string validation checks.
451
- */
452
- declare type StringCheckType = (typeof ALL_STRING_CHECKS)[number];
453
- export { StringCheckType }
454
- export { StringCheckType as StringCheckType_alias_1 }
455
-
456
- declare type StringConstraints = {
457
- minLength?: number;
458
- maxLength?: number;
459
- email?: boolean;
460
- url?: boolean;
461
- uuid?: boolean;
462
- cuid?: boolean;
463
- emoji?: boolean;
464
- regex?: {
465
- pattern: string;
466
- flags?: string;
467
- };
468
- };
469
-
470
- /**
471
- * Zod types that are generally supported by AI model providers.
472
- * @constant
473
- */
474
- declare const SUPPORTED_ZOD_TYPES: readonly ["ZodObject", "ZodArray", "ZodUnion", "ZodString", "ZodNumber", "ZodDate", "ZodAny", "ZodDefault"];
475
- export { SUPPORTED_ZOD_TYPES }
476
- export { SUPPORTED_ZOD_TYPES as SUPPORTED_ZOD_TYPES_alias_1 }
477
-
478
- /**
479
- * Type representing supported Zod schema types.
480
- */
481
- declare type SupportedZodType = (typeof SUPPORTED_ZOD_TYPES)[number];
482
- export { SupportedZodType }
483
- export { SupportedZodType as SupportedZodType_alias_1 }
484
-
485
- /**
486
- * Zod types that are not supported by most AI model providers and should be avoided.
487
- * @constant
488
- */
489
- declare const UNSUPPORTED_ZOD_TYPES: readonly ["ZodIntersection", "ZodNever", "ZodNull", "ZodTuple", "ZodUndefined"];
490
- export { UNSUPPORTED_ZOD_TYPES }
491
- export { UNSUPPORTED_ZOD_TYPES as UNSUPPORTED_ZOD_TYPES_alias_1 }
492
-
493
- /**
494
- * Type representing unsupported Zod schema types.
495
- */
496
- declare type UnsupportedZodType = (typeof UNSUPPORTED_ZOD_TYPES)[number];
497
- export { UnsupportedZodType }
498
- export { UnsupportedZodType as UnsupportedZodType_alias_1 }
499
-
500
- /**
501
- * Utility type to extract the shape of a Zod object schema.
502
- */
503
- declare type ZodShape<T extends z.AnyZodObject> = T['shape'];
504
- export { ZodShape }
505
- export { ZodShape as ZodShape_alias_1 }
506
-
507
- export { }