@mastra/schema-compat 1.3.14 → 1.3.15-alpha.0

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/dist/index.js CHANGED
@@ -5150,18 +5150,22 @@ function schemaValuesEqual(left, right, keyword) {
5150
5150
  const rightRecord = right;
5151
5151
  return leftEntries.length === Object.keys(rightRecord).length && leftEntries.every(([key, value]) => Object.prototype.hasOwnProperty.call(rightRecord, key) && schemaValuesEqual(value, rightRecord[key], key));
5152
5152
  }
5153
+ function isOpenAICompatibleModel(model) {
5154
+ const provider = model.provider ?? "";
5155
+ const modelId = model.modelId ?? "";
5156
+ if (provider.includes("openai") || modelId.includes("openai")) return true;
5157
+ return /^azure\.(chat|responses|completion)$/.test(provider) && /(^|[-_./:])(gpt|chatgpt)(?=$|[-_./:]|\d)|(^|[-_./:])o[1-9](?=$|[-_.:])/i.test(modelId) && !/(^|[-_./:])(claude|deepseek|meta|llama|codellama|mistral|ministral|mixtral|codestral|phi|grok|cohere)(?=$|[-_./:]|\d)/i.test(modelId);
5158
+ }
5153
5159
  var OpenAISchemaCompatLayer = class extends SchemaCompatLayer {
5154
5160
  getSchemaTarget() {
5155
5161
  return `jsonSchema7`;
5156
5162
  }
5157
5163
  isReasoningModel() {
5158
- const modelId = this.getModel().modelId;
5159
- if (!modelId) return false;
5160
- return modelId.includes(`o3`) || modelId.includes(`o4`) || modelId.includes(`o1`);
5164
+ return /(^|[-_./:])o[1-9](?=$|[-_.:])/i.test(this.getModel().modelId ?? "");
5161
5165
  }
5162
5166
  shouldApply() {
5163
5167
  const model = this.getModel();
5164
- if (!this.isReasoningModel() && (model.provider.includes(`openai`) || model.modelId?.includes(`openai`) || model.provider.includes(`groq`))) return true;
5168
+ if (!this.isReasoningModel() && (isOpenAICompatibleModel(model) || model.provider.includes(`groq`))) return true;
5165
5169
  return false;
5166
5170
  }
5167
5171
  processZodType(value) {
@@ -5388,6 +5392,15 @@ var OpenAISchemaCompatLayer = class extends SchemaCompatLayer {
5388
5392
  delete prop.type;
5389
5393
  prop.anyOf = [propSchema, { type: "null" }];
5390
5394
  }
5395
+ } else if (prop.type === void 0 && Array.isArray(prop.anyOf)) {
5396
+ if (!prop.anyOf.some((branch) => typeof branch !== "boolean" && branch.type === "null")) if (genericConstraintKeywords.some((keyword) => keyword in prop)) {
5397
+ const branch = { anyOf: prop.anyOf };
5398
+ for (const keyword of genericConstraintKeywords) if (keyword in prop) {
5399
+ branch[keyword] = prop[keyword];
5400
+ delete prop[keyword];
5401
+ }
5402
+ prop.anyOf = [branch, { type: "null" }];
5403
+ } else prop.anyOf = [...prop.anyOf, { type: "null" }];
5391
5404
  }
5392
5405
  }
5393
5406
  }
@@ -5450,14 +5463,9 @@ var OpenAIReasoningSchemaCompatLayer = class extends OpenAISchemaCompatLayer {
5450
5463
  getSchemaTarget() {
5451
5464
  return `openApi3`;
5452
5465
  }
5453
- isReasoningModel() {
5454
- const modelId = this.getModel().modelId;
5455
- if (!modelId) return false;
5456
- return modelId.includes(`o3`) || modelId.includes(`o4`) || modelId.includes(`o1`);
5457
- }
5458
5466
  shouldApply() {
5459
5467
  const model = this.getModel();
5460
- if (this.isReasoningModel() && (model.provider.includes(`openai`) || model.modelId?.includes(`openai`))) return true;
5468
+ if (this.isReasoningModel() && isOpenAICompatibleModel(model)) return true;
5461
5469
  return false;
5462
5470
  }
5463
5471
  processZodType(value) {
@@ -5545,10 +5553,11 @@ function extractZodSchema(schema) {
5545
5553
  * ```
5546
5554
  */
5547
5555
  function applyOpenAICompatTransforms(schema, modelInfo) {
5548
- if (!(modelInfo.provider?.includes("openai") || modelInfo.modelId?.includes("openai"))) return schema;
5556
+ if (!isOpenAICompatibleModel(modelInfo)) return schema;
5549
5557
  const zodSchema = extractZodSchema(schema);
5550
5558
  if (!zodSchema) return schema;
5551
- const compatLayer = /^o[1-5]/.test(modelInfo.modelId) ? new OpenAIReasoningSchemaCompatLayer(modelInfo) : new OpenAISchemaCompatLayer(modelInfo);
5559
+ const reasoningLayer = new OpenAIReasoningSchemaCompatLayer(modelInfo);
5560
+ const compatLayer = reasoningLayer.isReasoningModel() ? reasoningLayer : new OpenAISchemaCompatLayer(modelInfo);
5552
5561
  if (!compatLayer.shouldApply()) return schema;
5553
5562
  return toStandardSchema(compatLayer.processZodType(zodSchema));
5554
5563
  }