@mastra/schema-compat 1.2.7 → 1.2.8

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 (41) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/dist/_types/@internal_ai-v6/dist/index.d.ts +1695 -109
  3. package/dist/{chunk-MAUUSYX7.cjs → chunk-AR3LTBIK.cjs} +11 -7
  4. package/dist/chunk-AR3LTBIK.cjs.map +1 -0
  5. package/dist/{chunk-N4QSZBUR.js → chunk-DLZFWLZ2.js} +10 -4
  6. package/dist/chunk-DLZFWLZ2.js.map +1 -0
  7. package/dist/{chunk-YIDCYW4I.cjs → chunk-JO4RRSYO.cjs} +9 -3
  8. package/dist/chunk-JO4RRSYO.cjs.map +1 -0
  9. package/dist/{chunk-MZRYAZ7Z.js → chunk-MRY3A2NG.js} +10 -6
  10. package/dist/chunk-MRY3A2NG.js.map +1 -0
  11. package/dist/index.cjs +492 -182
  12. package/dist/index.cjs.map +1 -1
  13. package/dist/index.js +481 -171
  14. package/dist/index.js.map +1 -1
  15. package/dist/provider-compats/anthropic.d.ts +8 -2
  16. package/dist/provider-compats/anthropic.d.ts.map +1 -1
  17. package/dist/provider-compats/deepseek.d.ts +7 -2
  18. package/dist/provider-compats/deepseek.d.ts.map +1 -1
  19. package/dist/provider-compats/google.d.ts +6 -3
  20. package/dist/provider-compats/google.d.ts.map +1 -1
  21. package/dist/provider-compats/meta.d.ts +7 -2
  22. package/dist/provider-compats/meta.d.ts.map +1 -1
  23. package/dist/provider-compats/openai-reasoning.d.ts +1 -6
  24. package/dist/provider-compats/openai-reasoning.d.ts.map +1 -1
  25. package/dist/provider-compats/openai.d.ts +4 -17
  26. package/dist/provider-compats/openai.d.ts.map +1 -1
  27. package/dist/provider-compats/test-suite.d.ts +14 -0
  28. package/dist/provider-compats/test-suite.d.ts.map +1 -0
  29. package/dist/schema-compatibility.d.ts +15 -4
  30. package/dist/schema-compatibility.d.ts.map +1 -1
  31. package/dist/schema.cjs +7 -7
  32. package/dist/schema.js +1 -1
  33. package/dist/standard-schema/adapters/zod-v3.cjs +2 -2
  34. package/dist/standard-schema/adapters/zod-v3.d.ts.map +1 -1
  35. package/dist/standard-schema/adapters/zod-v3.js +1 -1
  36. package/dist/standard-schema/standard-schema.d.ts.map +1 -1
  37. package/package.json +11 -6
  38. package/dist/chunk-MAUUSYX7.cjs.map +0 -1
  39. package/dist/chunk-MZRYAZ7Z.js.map +0 -1
  40. package/dist/chunk-N4QSZBUR.js.map +0 -1
  41. package/dist/chunk-YIDCYW4I.cjs.map +0 -1
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import { toStandardSchema, standardSchemaToJSONSchema, isStandardSchemaWithJSON } from './chunk-MZRYAZ7Z.js';
2
- export { JSON_SCHEMA_LIBRARY_OPTIONS, isStandardJSONSchema, isStandardSchema, isStandardSchemaWithJSON, standardSchemaToJSONSchema, toStandardSchema } from './chunk-MZRYAZ7Z.js';
3
- import { zodToJsonSchema, ensureAllPropertiesRequired } from './chunk-7ZCSAPV3.js';
1
+ import { toStandardSchema, standardSchemaToJSONSchema, isStandardSchemaWithJSON } from './chunk-MRY3A2NG.js';
2
+ export { JSON_SCHEMA_LIBRARY_OPTIONS, isStandardJSONSchema, isStandardSchema, isStandardSchemaWithJSON, standardSchemaToJSONSchema, toStandardSchema } from './chunk-MRY3A2NG.js';
3
+ import { zodToJsonSchema } from './chunk-7ZCSAPV3.js';
4
4
  export { ensureAllPropertiesRequired, prepareJsonSchemaForOpenAIStrictMode } from './chunk-7ZCSAPV3.js';
5
5
  import { require_json_schema_traverse } from './chunk-K7I3YXWP.js';
6
6
  import { __toESM } from './chunk-5WRI5ZAA.js';
@@ -3611,7 +3611,8 @@ function applyCompatLayer({
3611
3611
  const standardSchema = toStandardSchema(schema);
3612
3612
  for (const compat of compatLayers) {
3613
3613
  if (compat.shouldApply()) {
3614
- return compat.processToJSONSchema(standardSchema);
3614
+ const compatSchema = compat.processToCompatSchema(standardSchema);
3615
+ return standardSchemaToJSONSchema(compatSchema);
3615
3616
  }
3616
3617
  }
3617
3618
  return standardSchemaToJSONSchema(standardSchema);
@@ -4638,9 +4639,6 @@ function isOneOfSchema(schema) {
4638
4639
  function isUnionSchema(schema) {
4639
4640
  return isAnyOfSchema(schema) || isOneOfSchema(schema) || Array.isArray(schema.type);
4640
4641
  }
4641
- function isEnumSchema(schema) {
4642
- return Array.isArray(schema.enum) && schema.enum.length > 0;
4643
- }
4644
4642
  function isAllOfSchema(schema) {
4645
4643
  return Array.isArray(schema.allOf) && schema.allOf.length > 0;
4646
4644
  }
@@ -4666,6 +4664,10 @@ var SchemaCompatLayer3 = class {
4666
4664
  this.v3Layer = new SchemaCompatLayer(model, this);
4667
4665
  this.v4Layer = new SchemaCompatLayer2(model, this);
4668
4666
  }
4667
+ preProcessJSONNode(_schema, _parentSchema) {
4668
+ }
4669
+ postProcessJSONNode(_schema, _parentSchema) {
4670
+ }
4669
4671
  /**
4670
4672
  * Gets the language model associated with this compatibility layer.
4671
4673
  *
@@ -4758,10 +4760,6 @@ var SchemaCompatLayer3 = class {
4758
4760
  return this.v3Layer.isIntersection(v);
4759
4761
  }
4760
4762
  }
4761
- preProcessJSONNode(_schema, _parentSchema) {
4762
- }
4763
- postProcessJSONNode(_schema, _parentSchema) {
4764
- }
4765
4763
  defaultZodObjectHandler(value, options = { passthrough: true }) {
4766
4764
  if ("_zod" in value) {
4767
4765
  return this.v4Layer.defaultZodObjectHandler(value, options);
@@ -4869,12 +4867,21 @@ var SchemaCompatLayer3 = class {
4869
4867
  return this.v3Layer.defaultZodIntersectionHandler(value);
4870
4868
  }
4871
4869
  }
4870
+ /**
4871
+ * @deprecated please use processToCompatSchema to usse StandardSchemaWithJSON
4872
+ * @param zodSchema
4873
+ * @returns
4874
+ */
4872
4875
  processToAISDKSchema(zodSchema2) {
4873
4876
  const processedSchema = this.processZodType(zodSchema2);
4874
4877
  return convertZodSchemaToAISDKSchema(processedSchema, this.getSchemaTarget());
4875
4878
  }
4876
- processToJSONSchema(zodSchema2, io = "input") {
4877
- const standardSchema = toStandardSchema(zodSchema2);
4879
+ /**
4880
+ * @param schema
4881
+ * @returns
4882
+ */
4883
+ processToJSONSchema(schema, io = "input") {
4884
+ const standardSchema = toStandardSchema(schema);
4878
4885
  const jsonSchema2 = standardSchemaToJSONSchema(standardSchema, {
4879
4886
  target: "draft-07",
4880
4887
  io
@@ -4882,16 +4889,33 @@ var SchemaCompatLayer3 = class {
4882
4889
  });
4883
4890
  (0, import_json_schema_traverse.default)(jsonSchema2, {
4884
4891
  cb: {
4885
- pre: (schema) => {
4886
- this.preProcessJSONNode(schema);
4892
+ pre: (schema2) => {
4893
+ this.preProcessJSONNode(schema2);
4887
4894
  },
4888
- post: (schema) => {
4889
- this.postProcessJSONNode(schema);
4895
+ post: (schema2) => {
4896
+ this.postProcessJSONNode(schema2);
4890
4897
  }
4891
4898
  }
4892
4899
  });
4893
4900
  return jsonSchema2;
4894
4901
  }
4902
+ processToCompatSchema(schema) {
4903
+ return {
4904
+ "~standard": {
4905
+ version: 1,
4906
+ vendor: "mastra",
4907
+ validate: (value) => toStandardSchema(schema)["~standard"].validate(value),
4908
+ jsonSchema: {
4909
+ input: () => {
4910
+ return this.processToJSONSchema(schema, "input");
4911
+ },
4912
+ output: () => {
4913
+ return this.processToJSONSchema(schema, "output");
4914
+ }
4915
+ }
4916
+ }
4917
+ };
4918
+ }
4895
4919
  // ==========================================
4896
4920
  // JSON Schema Default Handlers
4897
4921
  // ==========================================
@@ -4903,6 +4927,9 @@ var SchemaCompatLayer3 = class {
4903
4927
  if (schema.properties && schema.additionalProperties === void 0) {
4904
4928
  schema.additionalProperties = false;
4905
4929
  }
4930
+ if (!Object.keys(schema.properties ?? {}).length) {
4931
+ schema.required = [];
4932
+ }
4906
4933
  return schema;
4907
4934
  }
4908
4935
  /**
@@ -5125,6 +5152,7 @@ var SchemaCompatLayer3 = class {
5125
5152
  * and applies pre/post processing via traverse.
5126
5153
  *
5127
5154
  * Uses 'input' io mode so that fields with defaults are optional (appropriate for tool parameters).
5155
+ * @deprecated please use processToCompatSchema
5128
5156
  */
5129
5157
  toJSONSchema(zodSchema2) {
5130
5158
  const SCHEMA_TARGET_TO_STANDARD = {
@@ -5205,9 +5233,6 @@ function wrapSchemaWithNullTransform(schema) {
5205
5233
  }
5206
5234
  };
5207
5235
  }
5208
-
5209
- // src/provider-compats/openai.ts
5210
- var import_json_schema_traverse2 = __toESM(require_json_schema_traverse(), 1);
5211
5236
  function isOptional2(z11) {
5212
5237
  return (v) => v instanceof z11["ZodOptional"];
5213
5238
  }
@@ -5243,10 +5268,18 @@ function isIntersection(z11) {
5243
5268
  }
5244
5269
 
5245
5270
  // src/provider-compats/openai.ts
5271
+ var allowedStringFormats = [
5272
+ "date-time",
5273
+ "time",
5274
+ "date",
5275
+ "duration",
5276
+ "email",
5277
+ "hostname",
5278
+ "ipv4",
5279
+ "ipv6",
5280
+ "uuid"
5281
+ ];
5246
5282
  var OpenAISchemaCompatLayer = class extends SchemaCompatLayer3 {
5247
- constructor(model) {
5248
- super(model);
5249
- }
5250
5283
  getSchemaTarget() {
5251
5284
  return `jsonSchema7`;
5252
5285
  }
@@ -5324,43 +5357,44 @@ var OpenAISchemaCompatLayer = class extends SchemaCompatLayer3 {
5324
5357
  "ZodTuple"
5325
5358
  ]);
5326
5359
  }
5327
- /**
5328
- * Override to fix additionalProperties: {} which OpenAI doesn't support.
5329
- * Converts empty object {} to true to preserve passthrough intent.
5330
- */
5331
- processToJSONSchema(zodSchema2) {
5332
- const jsonSchema2 = super.processToJSONSchema(zodSchema2);
5333
- const fixedSchema = this.fixAdditionalProperties(jsonSchema2);
5334
- return ensureAllPropertiesRequired(fixedSchema);
5335
- }
5336
5360
  /**
5337
5361
  * Override to apply the same JSON Schema fixes (additionalProperties, required fields)
5338
5362
  * that processToJSONSchema applies. The base implementation skips JSON Schema traversal,
5339
5363
  * which causes OpenAI strict mode to reject tool schemas missing additionalProperties: false.
5340
5364
  */
5341
5365
  processToAISDKSchema(zodSchema2) {
5342
- const jsonSchemaResult = zodToJsonSchema(zodSchema2, this.getSchemaTarget());
5343
- const originalJsonSchema = JSON.parse(JSON.stringify(jsonSchemaResult));
5344
- (0, import_json_schema_traverse2.default)(jsonSchemaResult, {
5345
- cb: {
5346
- pre: (schema) => {
5347
- this.preProcessJSONNode(schema);
5348
- },
5349
- post: (schema) => {
5350
- this.postProcessJSONNode(schema);
5351
- }
5352
- }
5353
- });
5354
- const fixedSchema = this.fixAdditionalProperties(jsonSchemaResult);
5355
- const finalSchema = ensureAllPropertiesRequired(fixedSchema);
5356
- return jsonSchema(finalSchema, {
5366
+ const compat = this.processToCompatSchema(zodSchema2);
5367
+ const transformedJsonSchema = standardSchemaToJSONSchema(compat);
5368
+ return jsonSchema(transformedJsonSchema, {
5357
5369
  validate: (value) => {
5358
- const transformed = transformNullToUndefined(value, originalJsonSchema);
5370
+ const transformed = this.#traverse(value, transformedJsonSchema);
5359
5371
  const result = zodSchema2.safeParse(transformed);
5360
5372
  return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
5361
5373
  }
5362
5374
  });
5363
5375
  }
5376
+ processToCompatSchema(schema) {
5377
+ const originalStandardSchema = toStandardSchema(schema);
5378
+ return {
5379
+ "~standard": {
5380
+ version: 1,
5381
+ vendor: "mastra",
5382
+ validate: (value) => {
5383
+ const transformedJsonSchema = this.processToJSONSchema(schema, "input");
5384
+ const transformed = this.#traverse(value, transformedJsonSchema);
5385
+ return originalStandardSchema["~standard"].validate(transformed);
5386
+ },
5387
+ jsonSchema: {
5388
+ input: () => {
5389
+ return this.processToJSONSchema(schema, "input");
5390
+ },
5391
+ output: () => {
5392
+ return this.processToJSONSchema(schema, "output");
5393
+ }
5394
+ }
5395
+ }
5396
+ };
5397
+ }
5364
5398
  preProcessJSONNode(schema, _parentSchema) {
5365
5399
  if (isAllOfSchema(schema)) {
5366
5400
  this.defaultAllOfHandler(schema);
@@ -5369,13 +5403,12 @@ var OpenAISchemaCompatLayer = class extends SchemaCompatLayer3 {
5369
5403
  this.defaultObjectHandler(schema);
5370
5404
  } else if (isArraySchema(schema)) {
5371
5405
  this.defaultArrayHandler(schema);
5406
+ } else if (isNumberSchema(schema)) {
5407
+ this.defaultNumberHandler(schema);
5372
5408
  } else if (isStringSchema(schema)) {
5373
- const model = this.getModel();
5374
- if (model.modelId?.includes("gpt-4o-mini")) {
5375
- if (schema.format === "emoji") {
5409
+ if (schema.format) {
5410
+ if (!allowedStringFormats.includes(schema.format)) {
5376
5411
  delete schema.format;
5377
- }
5378
- if (schema.pattern) {
5379
5412
  delete schema.pattern;
5380
5413
  }
5381
5414
  }
@@ -5399,75 +5432,110 @@ var OpenAISchemaCompatLayer = class extends SchemaCompatLayer3 {
5399
5432
  }
5400
5433
  ];
5401
5434
  }
5402
- if (schema.type === "object" && schema.additionalProperties === void 0) {
5403
- schema.additionalProperties = false;
5404
- }
5405
5435
  if (isObjectSchema(schema)) {
5406
- const keys = Object.keys(schema.properties || {});
5407
- if (keys.length) {
5408
- for (const key of keys) {
5409
- if (!schema.required?.includes(key) && schema.properties?.[key]?.type) {
5410
- const prop = schema.properties[key];
5411
- const subSchema = {};
5412
- for (const propKey of Object.keys(prop)) {
5413
- if (propKey !== "anyOf") {
5414
- subSchema[propKey] = prop[propKey];
5436
+ schema.additionalProperties = false;
5437
+ if (schema.properties) {
5438
+ for (const key of Object.keys(schema.properties)) {
5439
+ const prop = schema.properties[key];
5440
+ if (!schema.required) {
5441
+ schema.required = [];
5442
+ }
5443
+ if (!schema.required?.includes(key)) {
5444
+ schema["x-optional"] = [...schema["x-optional"] || [], key];
5445
+ schema.required?.push(key);
5446
+ if (prop.type) {
5447
+ if (Array.isArray(prop.type)) {
5448
+ const types = [...prop.type];
5449
+ if (!types.includes("null")) {
5450
+ types.push("null");
5451
+ }
5452
+ const propSchema = { ...prop };
5453
+ delete propSchema.anyOf;
5454
+ delete propSchema.type;
5455
+ delete prop.type;
5456
+ prop.anyOf = types.map(
5457
+ (type) => type === "null" ? { type: "null" } : {
5458
+ ...propSchema,
5459
+ type
5460
+ }
5461
+ );
5462
+ } else if (prop.type !== "null") {
5463
+ const originalType = prop.type;
5464
+ const propSchema = { ...prop };
5465
+ delete propSchema.anyOf;
5466
+ delete propSchema.type;
5467
+ delete prop.type;
5468
+ prop.anyOf = [
5469
+ {
5470
+ ...propSchema,
5471
+ type: originalType
5472
+ },
5473
+ { type: "null" }
5474
+ ];
5415
5475
  }
5416
5476
  }
5417
- prop.anyOf = [subSchema, { type: "null" }];
5418
- for (const propKey of Object.keys(subSchema)) {
5419
- delete prop[propKey];
5420
- }
5421
5477
  }
5422
5478
  }
5423
- schema.required = keys;
5424
- }
5425
- if ("propertyNames" in schema) {
5426
- delete schema.propertyNames;
5427
5479
  }
5428
5480
  }
5429
5481
  }
5430
- /**
5431
- * Recursively fixes additionalProperties: {} to additionalProperties: true.
5432
- * OpenAI requires additionalProperties to be either:
5433
- * - false (no additional properties allowed)
5434
- * - true (any additional properties allowed)
5435
- * - an object with a "type" key (typed additional properties)
5436
- * An empty object {} is NOT valid.
5437
- */
5438
- fixAdditionalProperties(schema) {
5439
- if (typeof schema !== "object" || schema === null) {
5440
- return schema;
5482
+ #traverse(value, schema) {
5483
+ const resolved = this.#resolveAnyOf(schema);
5484
+ if ((isDateFormat(resolved) || resolved["x-date"] === true) && typeof value === "string") {
5485
+ return new Date(value);
5441
5486
  }
5442
- const result = { ...schema };
5443
- if (result.additionalProperties !== void 0 && typeof result.additionalProperties === "object" && result.additionalProperties !== null && !Array.isArray(result.additionalProperties) && Object.keys(result.additionalProperties).length === 0) {
5444
- result.additionalProperties = true;
5487
+ const isArrayType = resolved.type === "array" || Array.isArray(resolved.type) && resolved.type.includes("array");
5488
+ if (isArrayType) {
5489
+ if (!Array.isArray(value)) {
5490
+ return value;
5491
+ }
5492
+ return value.map((item) => this.#traverse(item, resolved.items));
5445
5493
  }
5446
- if (result.properties) {
5447
- result.properties = Object.fromEntries(
5448
- Object.entries(result.properties).map(([key, value]) => [
5449
- key,
5450
- this.fixAdditionalProperties(value)
5451
- ])
5452
- );
5494
+ const isObjectType = resolved.type === "object" || Array.isArray(resolved.type) && resolved.type.includes("object");
5495
+ if (!isObjectType) {
5496
+ return value;
5453
5497
  }
5454
- if (result.items) {
5455
- if (Array.isArray(result.items)) {
5456
- result.items = result.items.map((item) => this.fixAdditionalProperties(item));
5457
- } else {
5458
- result.items = this.fixAdditionalProperties(result.items);
5498
+ const properties = resolved.properties;
5499
+ if (!properties || !value) {
5500
+ return value;
5501
+ }
5502
+ const obj = value;
5503
+ const optionalProperties = resolved["x-optional"] ?? [];
5504
+ for (const key in obj) {
5505
+ if (optionalProperties.includes(key) && obj[key] === null) {
5506
+ obj[key] = void 0;
5507
+ } else if (properties[key]) {
5508
+ obj[key] = this.#traverse(obj[key], properties[key]);
5459
5509
  }
5460
5510
  }
5461
- if (result.additionalProperties && typeof result.additionalProperties === "object" && !Array.isArray(result.additionalProperties) && Object.keys(result.additionalProperties).length > 0) {
5462
- result.additionalProperties = this.fixAdditionalProperties(result.additionalProperties);
5511
+ return obj;
5512
+ }
5513
+ /**
5514
+ * If schema has anyOf, return the first non-null variant for traversal.
5515
+ * Otherwise return the schema itself.
5516
+ */
5517
+ #resolveAnyOf(schema) {
5518
+ if (Array.isArray(schema.anyOf)) {
5519
+ const nonNull = schema.anyOf.find((s) => s.type !== "null");
5520
+ if (nonNull) {
5521
+ return nonNull;
5522
+ }
5463
5523
  }
5464
- return result;
5524
+ return schema;
5465
5525
  }
5466
5526
  };
5527
+ function isDateFormat(schema) {
5528
+ return schema.format === "date-time" || schema.format === "date";
5529
+ }
5467
5530
  var OpenAIReasoningSchemaCompatLayer = class extends OpenAISchemaCompatLayer {
5468
5531
  getSchemaTarget() {
5469
5532
  return `openApi3`;
5470
5533
  }
5534
+ isReasoningModel() {
5535
+ const modelId = this.getModel().modelId;
5536
+ if (!modelId) return false;
5537
+ return modelId.includes(`o3`) || modelId.includes(`o4`) || modelId.includes(`o1`);
5538
+ }
5471
5539
  shouldApply() {
5472
5540
  const model = this.getModel();
5473
5541
  if (this.isReasoningModel() && (model.provider.includes(`openai`) || model.modelId?.includes(`openai`))) {
@@ -5540,30 +5608,6 @@ Argument was an "any" type, but you (the LLM) do not support "any", so it was ca
5540
5608
  }
5541
5609
  return this.defaultUnsupportedZodTypeHandler(value);
5542
5610
  }
5543
- processToJSONSchema(zodSchema2) {
5544
- const jsonSchema2 = super.processToJSONSchema(zodSchema2);
5545
- return ensureAllPropertiesRequired(jsonSchema2);
5546
- }
5547
- preProcessJSONNode(schema, _parentSchema) {
5548
- if (isAllOfSchema(schema)) {
5549
- this.defaultAllOfHandler(schema);
5550
- }
5551
- if (isObjectSchema(schema)) {
5552
- this.defaultObjectHandler(schema);
5553
- } else if (isArraySchema(schema)) {
5554
- this.defaultArrayHandler(schema);
5555
- } else if (isNumberSchema(schema)) {
5556
- this.defaultNumberHandler(schema);
5557
- } else if (isStringSchema(schema)) {
5558
- this.defaultStringHandler(schema);
5559
- }
5560
- }
5561
- postProcessJSONNode(schema) {
5562
- super.postProcessJSONNode(schema);
5563
- if (schema.type === "object" && schema.properties !== void 0) {
5564
- schema.additionalProperties = false;
5565
- }
5566
- }
5567
5611
  };
5568
5612
 
5569
5613
  // src/standard-schema-compat.ts
@@ -5642,7 +5686,40 @@ var AnthropicSchemaCompatLayer = class extends SchemaCompatLayer3 {
5642
5686
  }
5643
5687
  return this.defaultUnsupportedZodTypeHandler(value);
5644
5688
  }
5645
- preProcessJSONNode(schema, _parentSchema) {
5689
+ processToAISDKSchema(zodSchema2) {
5690
+ const compat = this.processToCompatSchema(zodSchema2);
5691
+ const transformedJsonSchema = standardSchemaToJSONSchema(compat);
5692
+ return jsonSchema(transformedJsonSchema, {
5693
+ validate: (value) => {
5694
+ const transformed = this.#traverse(value, transformedJsonSchema);
5695
+ const result = zodSchema2.safeParse(transformed);
5696
+ return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
5697
+ }
5698
+ });
5699
+ }
5700
+ processToCompatSchema(schema) {
5701
+ const originalStandardSchema = toStandardSchema(schema);
5702
+ return {
5703
+ "~standard": {
5704
+ version: 1,
5705
+ vendor: "mastra",
5706
+ validate: (value) => {
5707
+ const transformedJsonSchema = this.processToJSONSchema(schema, "input");
5708
+ const transformed = this.#traverse(value, transformedJsonSchema);
5709
+ return originalStandardSchema["~standard"].validate(transformed);
5710
+ },
5711
+ jsonSchema: {
5712
+ input: () => {
5713
+ return this.processToJSONSchema(schema, "input");
5714
+ },
5715
+ output: () => {
5716
+ return this.processToJSONSchema(schema, "output");
5717
+ }
5718
+ }
5719
+ }
5720
+ };
5721
+ }
5722
+ preProcessJSONNode(schema) {
5646
5723
  if (isAllOfSchema(schema)) {
5647
5724
  this.defaultAllOfHandler(schema);
5648
5725
  }
@@ -5650,24 +5727,68 @@ var AnthropicSchemaCompatLayer = class extends SchemaCompatLayer3 {
5650
5727
  this.defaultObjectHandler(schema);
5651
5728
  } else if (isArraySchema(schema)) {
5652
5729
  this.defaultArrayHandler(schema);
5730
+ } else if (isNumberSchema(schema)) {
5731
+ this.defaultNumberHandler(schema);
5653
5732
  } else if (isStringSchema(schema)) {
5654
- if (this.getModel().modelId.includes("claude-3.5-haiku")) {
5655
- this.defaultStringHandler(schema);
5656
- }
5733
+ this.defaultStringHandler(schema);
5657
5734
  }
5658
5735
  }
5659
5736
  postProcessJSONNode(schema) {
5660
5737
  if (isUnionSchema(schema)) {
5661
5738
  this.defaultUnionHandler(schema);
5662
5739
  }
5663
- if (isObjectSchema(schema)) {
5664
- if (schema.additionalProperties !== void 0 && typeof schema.additionalProperties === "object" && schema.additionalProperties !== null && Object.keys(schema.additionalProperties).length === 0) {
5665
- schema.additionalProperties = true;
5666
- }
5667
- if ("propertyNames" in schema) {
5668
- delete schema.propertyNames;
5740
+ }
5741
+ #traverse(value, schema) {
5742
+ const resolved = this.#resolveSchemaForValue(schema, value);
5743
+ if (resolved["x-date"] === true && typeof value === "string") {
5744
+ return new Date(value);
5745
+ }
5746
+ const isArrayType = resolved.type === "array" || Array.isArray(resolved.type) && resolved.type.includes("array");
5747
+ if (isArrayType) {
5748
+ if (!Array.isArray(value)) {
5749
+ return value;
5669
5750
  }
5751
+ return value.map((item) => this.#traverse(item, resolved.items));
5752
+ }
5753
+ const isObjectType = resolved.type === "object" || Array.isArray(resolved.type) && resolved.type.includes("object");
5754
+ if (!isObjectType) {
5755
+ return value;
5670
5756
  }
5757
+ const properties = resolved.properties;
5758
+ if (!properties || !value) {
5759
+ return value;
5760
+ }
5761
+ const obj = value;
5762
+ for (const key in obj) {
5763
+ if (properties[key]) {
5764
+ obj[key] = this.#traverse(obj[key], properties[key]);
5765
+ }
5766
+ }
5767
+ return obj;
5768
+ }
5769
+ // #resolveAnyOf(schema: Record<string, unknown>): Record<string, unknown> {
5770
+ // if (Array.isArray(schema.anyOf)) {
5771
+ // const nonNull = (schema.anyOf as Record<string, unknown>[]).find(s => s.type !== 'null');
5772
+ // if (nonNull) {
5773
+ // return nonNull;
5774
+ // }
5775
+ // }
5776
+ // return schema;
5777
+ // }
5778
+ #resolveSchemaForValue(schema, value) {
5779
+ if (!Array.isArray(schema.anyOf)) {
5780
+ return schema;
5781
+ }
5782
+ const variants = schema.anyOf;
5783
+ const nonNullVariants = variants.filter((variant) => variant.type !== "null");
5784
+ if (variants.length === 2 && nonNullVariants.length === 1) {
5785
+ return nonNullVariants[0];
5786
+ }
5787
+ const keys = value && typeof value === "object" ? Object.keys(value) : [];
5788
+ return nonNullVariants.find((variant) => {
5789
+ const properties = variant.properties;
5790
+ return !!properties && keys.some((key) => key in properties);
5791
+ }) ?? schema;
5671
5792
  }
5672
5793
  };
5673
5794
  var DeepSeekSchemaCompatLayer = class extends SchemaCompatLayer3 {
@@ -5696,7 +5817,40 @@ var DeepSeekSchemaCompatLayer = class extends SchemaCompatLayer3 {
5696
5817
  }
5697
5818
  return value;
5698
5819
  }
5699
- preProcessJSONNode(schema, _parentSchema) {
5820
+ processToAISDKSchema(zodSchema2) {
5821
+ const compat = this.processToCompatSchema(zodSchema2);
5822
+ const transformedJsonSchema = standardSchemaToJSONSchema(compat);
5823
+ return jsonSchema(transformedJsonSchema, {
5824
+ validate: (value) => {
5825
+ const transformed = this.#traverse(value, transformedJsonSchema);
5826
+ const result = zodSchema2.safeParse(transformed);
5827
+ return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
5828
+ }
5829
+ });
5830
+ }
5831
+ processToCompatSchema(schema) {
5832
+ const originalStandardSchema = toStandardSchema(schema);
5833
+ return {
5834
+ "~standard": {
5835
+ version: 1,
5836
+ vendor: "mastra",
5837
+ validate: (value) => {
5838
+ const transformedJsonSchema = this.processToJSONSchema(schema, "input");
5839
+ const transformed = this.#traverse(value, transformedJsonSchema);
5840
+ return originalStandardSchema["~standard"].validate(transformed);
5841
+ },
5842
+ jsonSchema: {
5843
+ input: () => {
5844
+ return this.processToJSONSchema(schema, "input");
5845
+ },
5846
+ output: () => {
5847
+ return this.processToJSONSchema(schema, "output");
5848
+ }
5849
+ }
5850
+ }
5851
+ };
5852
+ }
5853
+ preProcessJSONNode(schema) {
5700
5854
  if (isAllOfSchema(schema)) {
5701
5855
  this.defaultAllOfHandler(schema);
5702
5856
  }
@@ -5708,18 +5862,47 @@ var DeepSeekSchemaCompatLayer = class extends SchemaCompatLayer3 {
5708
5862
  this.defaultStringHandler(schema);
5709
5863
  }
5710
5864
  }
5711
- postProcessJSONNode(schema, _parentSchema) {
5865
+ postProcessJSONNode(schema) {
5712
5866
  if (isUnionSchema(schema)) {
5713
5867
  this.defaultUnionHandler(schema);
5714
5868
  }
5715
- if (isObjectSchema(schema)) {
5716
- if (schema.additionalProperties !== void 0 && typeof schema.additionalProperties === "object" && schema.additionalProperties !== null && Object.keys(schema.additionalProperties).length === 0) {
5717
- schema.additionalProperties = true;
5869
+ }
5870
+ #traverse(value, schema) {
5871
+ const resolved = this.#resolveAnyOf(schema);
5872
+ if (resolved["x-date"] === true && typeof value === "string") {
5873
+ return new Date(value);
5874
+ }
5875
+ const isArrayType = resolved.type === "array" || Array.isArray(resolved.type) && resolved.type.includes("array");
5876
+ if (isArrayType) {
5877
+ if (!Array.isArray(value)) {
5878
+ return value;
5879
+ }
5880
+ return value.map((item) => this.#traverse(item, resolved.items));
5881
+ }
5882
+ const isObjectType = resolved.type === "object" || Array.isArray(resolved.type) && resolved.type.includes("object");
5883
+ if (!isObjectType) {
5884
+ return value;
5885
+ }
5886
+ const properties = resolved.properties;
5887
+ if (!properties || !value) {
5888
+ return value;
5889
+ }
5890
+ const obj = value;
5891
+ for (const key in obj) {
5892
+ if (properties[key]) {
5893
+ obj[key] = this.#traverse(obj[key], properties[key]);
5718
5894
  }
5719
- if ("propertyNames" in schema) {
5720
- delete schema.propertyNames;
5895
+ }
5896
+ return obj;
5897
+ }
5898
+ #resolveAnyOf(schema) {
5899
+ if (Array.isArray(schema.anyOf)) {
5900
+ const nonNull = schema.anyOf.find((s) => s.type !== "null");
5901
+ if (nonNull) {
5902
+ return nonNull;
5721
5903
  }
5722
5904
  }
5905
+ return schema;
5723
5906
  }
5724
5907
  };
5725
5908
  function fixNullableUnionTypes(schema) {
@@ -5773,6 +5956,16 @@ function fixNullableUnionTypes(schema) {
5773
5956
  if (result.allOf && Array.isArray(result.allOf)) {
5774
5957
  result.allOf = result.allOf.map((s) => fixNullableUnionTypes(s));
5775
5958
  }
5959
+ if (result.anyOf && Array.isArray(result.anyOf)) {
5960
+ if (result.description) {
5961
+ for (const item of result.anyOf) {
5962
+ if (typeof item === "object" && item !== null && !item.description) {
5963
+ item.description = result.description;
5964
+ }
5965
+ }
5966
+ }
5967
+ return { anyOf: result.anyOf };
5968
+ }
5776
5969
  return result;
5777
5970
  }
5778
5971
  var GoogleSchemaCompatLayer = class extends SchemaCompatLayer3 {
@@ -5783,7 +5976,7 @@ var GoogleSchemaCompatLayer = class extends SchemaCompatLayer3 {
5783
5976
  return "jsonSchema7";
5784
5977
  }
5785
5978
  shouldApply() {
5786
- return this.getModel().provider.includes("google") || this.getModel().modelId.includes("google");
5979
+ return this.getModel().provider.includes("google") || this.getModel().modelId.includes("gemini-") || this.getModel().modelId.includes("google");
5787
5980
  }
5788
5981
  processZodType(value) {
5789
5982
  if (isOptional2(z)(value)) {
@@ -5814,43 +6007,100 @@ var GoogleSchemaCompatLayer = class extends SchemaCompatLayer3 {
5814
6007
  }
5815
6008
  return this.defaultUnsupportedZodTypeHandler(value);
5816
6009
  }
5817
- processToJSONSchema(zodSchema2, io = "input") {
5818
- const result = super.processToJSONSchema(zodSchema2, io);
5819
- return fixNullableUnionTypes(result);
5820
- }
6010
+ // public processToJSONSchema(zodSchema: PublicSchema<any>, io?: 'input' | 'output'): JSONSchema7 {
6011
+ // const result = super.processToJSONSchema(zodSchema, io);
6012
+ // // Fix union type arrays that Gemini doesn't support
6013
+ // return fixNullableUnionTypes(result as Record<string, any>) as JSONSchema7;
6014
+ // }
5821
6015
  processToAISDKSchema(zodSchema2) {
5822
- const result = super.processToAISDKSchema(zodSchema2);
5823
- const fixedJsonSchema = fixNullableUnionTypes(result.jsonSchema);
5824
- return { ...result, jsonSchema: fixedJsonSchema };
6016
+ const compat = this.processToCompatSchema(zodSchema2);
6017
+ const transformedJsonSchema = standardSchemaToJSONSchema(compat);
6018
+ const fixedJsonSchema = fixNullableUnionTypes(transformedJsonSchema);
6019
+ return jsonSchema(fixedJsonSchema, {
6020
+ validate: (value) => {
6021
+ const transformed = this.#traverse(value, fixedJsonSchema);
6022
+ const result = zodSchema2.safeParse(transformed);
6023
+ return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
6024
+ }
6025
+ });
5825
6026
  }
5826
- preProcessJSONNode(schema, _parentSchema) {
6027
+ processToCompatSchema(schema) {
6028
+ const originalStandardSchema = toStandardSchema(schema);
6029
+ return {
6030
+ "~standard": {
6031
+ version: 1,
6032
+ vendor: "mastra",
6033
+ validate: (value) => {
6034
+ const transformedJsonSchema = this.processToJSONSchema(schema, "input");
6035
+ const transformed = this.#traverse(value, transformedJsonSchema);
6036
+ return originalStandardSchema["~standard"].validate(transformed);
6037
+ },
6038
+ jsonSchema: {
6039
+ input: () => {
6040
+ return this.processToJSONSchema(schema, "input");
6041
+ },
6042
+ output: () => {
6043
+ return this.processToJSONSchema(schema, "output");
6044
+ }
6045
+ }
6046
+ }
6047
+ };
6048
+ }
6049
+ preProcessJSONNode(schema) {
5827
6050
  if (isAllOfSchema(schema)) {
5828
6051
  this.defaultAllOfHandler(schema);
5829
6052
  }
5830
6053
  if (isObjectSchema(schema)) {
5831
6054
  this.defaultObjectHandler(schema);
6055
+ } else if (isNumberSchema(schema)) {
6056
+ this.defaultNumberHandler(schema);
5832
6057
  } else if (isArraySchema(schema)) {
5833
6058
  this.defaultArrayHandler(schema);
5834
6059
  } else if (isStringSchema(schema)) {
5835
6060
  this.defaultStringHandler(schema);
5836
- } else if (isNumberSchema(schema)) {
5837
- this.defaultNumberHandler(schema);
5838
- } else if (isEnumSchema(schema)) {
5839
- schema.type = "string";
5840
6061
  }
5841
6062
  }
5842
- postProcessJSONNode(schema, _parentSchema) {
6063
+ postProcessJSONNode(schema) {
5843
6064
  if (isUnionSchema(schema)) {
5844
6065
  this.defaultUnionHandler(schema);
5845
6066
  }
5846
- if (isObjectSchema(schema)) {
5847
- if (schema.additionalProperties !== void 0 && typeof schema.additionalProperties === "object" && schema.additionalProperties !== null && Object.keys(schema.additionalProperties).length === 0) {
5848
- schema.additionalProperties = true;
6067
+ }
6068
+ #traverse(value, schema) {
6069
+ const resolved = this.#resolveAnyOf(schema);
6070
+ if (resolved["x-date"] === true && typeof value === "string") {
6071
+ return new Date(value);
6072
+ }
6073
+ const isArrayType = resolved.type === "array" || Array.isArray(resolved.type) && resolved.type.includes("array");
6074
+ if (isArrayType) {
6075
+ if (!Array.isArray(value)) {
6076
+ return value;
5849
6077
  }
5850
- if ("propertyNames" in schema) {
5851
- delete schema.propertyNames;
6078
+ return value.map((item) => this.#traverse(item, resolved.items));
6079
+ }
6080
+ const isObjectType = resolved.type === "object" || Array.isArray(resolved.type) && resolved.type.includes("object");
6081
+ if (!isObjectType) {
6082
+ return value;
6083
+ }
6084
+ const properties = resolved.properties;
6085
+ if (!properties || !value) {
6086
+ return value;
6087
+ }
6088
+ const obj = value;
6089
+ for (const key in obj) {
6090
+ if (properties[key]) {
6091
+ obj[key] = this.#traverse(obj[key], properties[key]);
5852
6092
  }
5853
6093
  }
6094
+ return obj;
6095
+ }
6096
+ #resolveAnyOf(schema) {
6097
+ if (Array.isArray(schema.anyOf)) {
6098
+ const nonNull = schema.anyOf.find((s) => s.type !== "null");
6099
+ if (nonNull) {
6100
+ return nonNull;
6101
+ }
6102
+ }
6103
+ return schema;
5854
6104
  }
5855
6105
  };
5856
6106
  var MetaSchemaCompatLayer = class extends SchemaCompatLayer3 {
@@ -5881,7 +6131,40 @@ var MetaSchemaCompatLayer = class extends SchemaCompatLayer3 {
5881
6131
  }
5882
6132
  return value;
5883
6133
  }
5884
- preProcessJSONNode(schema, _parentSchema) {
6134
+ processToAISDKSchema(zodSchema2) {
6135
+ const compat = this.processToCompatSchema(zodSchema2);
6136
+ const transformedJsonSchema = standardSchemaToJSONSchema(compat);
6137
+ return jsonSchema(transformedJsonSchema, {
6138
+ validate: (value) => {
6139
+ const transformed = this.#traverse(value, transformedJsonSchema);
6140
+ const result = zodSchema2.safeParse(transformed);
6141
+ return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
6142
+ }
6143
+ });
6144
+ }
6145
+ processToCompatSchema(schema) {
6146
+ const originalStandardSchema = toStandardSchema(schema);
6147
+ return {
6148
+ "~standard": {
6149
+ version: 1,
6150
+ vendor: "mastra",
6151
+ validate: (value) => {
6152
+ const transformedJsonSchema = this.processToJSONSchema(schema, "input");
6153
+ const transformed = this.#traverse(value, transformedJsonSchema);
6154
+ return originalStandardSchema["~standard"].validate(transformed);
6155
+ },
6156
+ jsonSchema: {
6157
+ input: () => {
6158
+ return this.processToJSONSchema(schema, "input");
6159
+ },
6160
+ output: () => {
6161
+ return this.processToJSONSchema(schema, "output");
6162
+ }
6163
+ }
6164
+ }
6165
+ };
6166
+ }
6167
+ preProcessJSONNode(schema) {
5885
6168
  if (isAllOfSchema(schema)) {
5886
6169
  this.defaultAllOfHandler(schema);
5887
6170
  }
@@ -5891,22 +6174,49 @@ var MetaSchemaCompatLayer = class extends SchemaCompatLayer3 {
5891
6174
  this.defaultArrayHandler(schema);
5892
6175
  } else if (isStringSchema(schema)) {
5893
6176
  this.defaultStringHandler(schema);
5894
- } else if (isNumberSchema(schema)) {
5895
- this.defaultNumberHandler(schema);
5896
6177
  }
5897
6178
  }
5898
- postProcessJSONNode(schema, _parentSchema) {
6179
+ postProcessJSONNode(schema) {
5899
6180
  if (isUnionSchema(schema)) {
5900
6181
  this.defaultUnionHandler(schema);
5901
6182
  }
5902
- if (isObjectSchema(schema)) {
5903
- if (schema.additionalProperties !== void 0 && typeof schema.additionalProperties === "object" && schema.additionalProperties !== null && Object.keys(schema.additionalProperties).length === 0) {
5904
- schema.additionalProperties = true;
6183
+ }
6184
+ #traverse(value, schema) {
6185
+ const resolved = this.#resolveAnyOf(schema);
6186
+ if (resolved["x-date"] === true && typeof value === "string") {
6187
+ return new Date(value);
6188
+ }
6189
+ const isArrayType = resolved.type === "array" || Array.isArray(resolved.type) && resolved.type.includes("array");
6190
+ if (isArrayType) {
6191
+ if (!Array.isArray(value)) {
6192
+ return value;
5905
6193
  }
5906
- if ("propertyNames" in schema) {
5907
- delete schema.propertyNames;
6194
+ return value.map((item) => this.#traverse(item, resolved.items));
6195
+ }
6196
+ const isObjectType = resolved.type === "object" || Array.isArray(resolved.type) && resolved.type.includes("object");
6197
+ if (!isObjectType) {
6198
+ return value;
6199
+ }
6200
+ const properties = resolved.properties;
6201
+ if (!properties || !value) {
6202
+ return value;
6203
+ }
6204
+ const obj = value;
6205
+ for (const key in obj) {
6206
+ if (properties[key]) {
6207
+ obj[key] = this.#traverse(obj[key], properties[key]);
5908
6208
  }
5909
6209
  }
6210
+ return obj;
6211
+ }
6212
+ #resolveAnyOf(schema) {
6213
+ if (Array.isArray(schema.anyOf)) {
6214
+ const nonNull = schema.anyOf.find((s) => s.type !== "null");
6215
+ if (nonNull) {
6216
+ return nonNull;
6217
+ }
6218
+ }
6219
+ return schema;
5910
6220
  }
5911
6221
  };
5912
6222