@mastra/schema-compat
Version:
Tool schema compatibility layer for Mastra.ai
158 lines (157 loc) • 5 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
//#region src/standard-schema/adapters/ai-sdk.ts
/**
* Vendor name for AI SDK wrapped schemas.
*/
const VENDOR = "ai-sdk";
/**
* A wrapper class that makes AI SDK Schema compatible with @standard-schema/spec.
*
* This class implements both `StandardSchemaV1` (validation) and `StandardJSONSchemaV1`
* (JSON Schema conversion) interfaces. It wraps an AI SDK Schema and adapts its
* validation method and jsonSchema property to the standard-schema interface.
*
* @typeParam T - The TypeScript type that the AI SDK Schema represents
*
* @example
* ```typescript
* import { jsonSchema } from '@internal/ai-v6';
* import { toStandardSchema } from '@mastra/schema-compat/adapters/ai-sdk';
*
* // Create an AI SDK schema
* const aiSdkSchema = jsonSchema<{ name: string; age: number }>({
* type: 'object',
* properties: {
* name: { type: 'string' },
* age: { type: 'number' },
* },
* required: ['name', 'age'],
* });
*
* // Convert to standard-schema
* const standardSchema = toStandardSchema(aiSdkSchema);
*
* // Use validation (from StandardSchemaV1)
* const result = standardSchema['~standard'].validate({ name: 'John', age: 30 });
*
* // Get JSON Schema (from StandardJSONSchemaV1)
* const jsonSchema = standardSchema['~standard'].jsonSchema.output({ target: 'draft-07' });
* ```
*/
var AiSdkSchemaWrapper = class {
#schema;
"~standard";
constructor(schema) {
this.#schema = schema;
this["~standard"] = {
version: 1,
vendor: VENDOR,
validate: this.#validate.bind(this),
jsonSchema: {
input: this.#toJsonSchema.bind(this),
output: this.#toJsonSchema.bind(this)
}
};
}
/**
* Validates a value against the AI SDK Schema.
*
* @param value - The value to validate
* @returns A result object with either the validated value or validation issues
*/
#validate(value) {
if (!this.#schema.validate) return { value };
try {
const result = this.#schema.validate(value);
if (result && typeof result === "object" && "then" in result && typeof result.then === "function") return Promise.resolve(result).then((res) => this.#convertValidationResult(res)).catch((error) => {
return { issues: [{ message: `Schema validation error: ${error instanceof Error ? error.message : "Unknown validation error"}` }] };
});
return this.#convertValidationResult(result);
} catch (error) {
return { issues: [{ message: `Schema validation error: ${error instanceof Error ? error.message : "Unknown validation error"}` }] };
}
}
/**
* Converts an AI SDK ValidationResult to a StandardSchemaV1.Result.
*
* @param result - The AI SDK validation result
* @returns A StandardSchemaV1.Result
*/
#convertValidationResult(result) {
if (result.success) return { value: result.value };
return { issues: [{ message: result.error.message }] };
}
/**
* Returns the JSON Schema in the requested target format.
*
* @param options - Options including the target format
* @returns The JSON Schema as a Record
*/
#toJsonSchema(options) {
const { target } = options;
const clonedSchema = JSON.parse(JSON.stringify(this.#schema.jsonSchema));
if (!clonedSchema.$schema) switch (target) {
case "draft-07":
clonedSchema.$schema = "http://json-schema.org/draft-07/schema#";
break;
case "draft-2020-12":
clonedSchema.$schema = "https://json-schema.org/draft/2020-12/schema";
break;
case "openapi-3.0": break;
default: break;
}
return clonedSchema;
}
/**
* Returns the original AI SDK Schema.
*/
getSchema() {
return this.#schema;
}
/**
* Returns the original JSON Schema from the AI SDK Schema.
*/
getJsonSchema() {
return this.#schema.jsonSchema;
}
};
/**
* Wraps an AI SDK Schema to implement the full @standard-schema/spec interface.
*
* This function creates a wrapper that implements both `StandardSchemaV1` (validation)
* and `StandardJSONSchemaV1` (JSON Schema conversion) interfaces.
*
* @typeParam T - The TypeScript type that the AI SDK Schema represents
* @param schema - The AI SDK Schema to wrap
* @returns A wrapper implementing StandardSchemaWithJSON
*
* @example
* ```typescript
* import { jsonSchema } from '@internal/ai-v6';
* import { toStandardSchema } from '@mastra/schema-compat/adapters/ai-sdk';
*
* const aiSdkSchema = jsonSchema<{ name: string; age: number }>({
* type: 'object',
* properties: {
* name: { type: 'string' },
* age: { type: 'number' },
* },
* required: ['name', 'age'],
* });
*
* const standardSchema = toStandardSchema(aiSdkSchema);
*
* // Validate data
* const result = standardSchema['~standard'].validate({ name: 'John', age: 30 });
*
* // Get JSON Schema
* const jsonSchema = standardSchema['~standard'].jsonSchema.output({ target: 'draft-07' });
* ```
*/
function toStandardSchema(schema) {
return new AiSdkSchemaWrapper(schema);
}
//#endregion
exports.AiSdkSchemaWrapper = AiSdkSchemaWrapper;
exports.toStandardSchema = toStandardSchema;
//# sourceMappingURL=ai-sdk.cjs.map