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