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AAEA,IAAA,OAAO,YAAA;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKA,SAAA,GAA4B;AAC1B,IAAA,OAAO,IAAA,CAAK,OAAA;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,aAAA,GAA6B;AAC3B,IAAA,OAAO,KAAK,OAAA,CAAQ,UAAA;AAAA,EACtB;AACF;AAmCO,SAAS,iBAA8B,MAAA,EAA6C;AACzF,EAAA,OAAO,IAAI,mBAAyB,MAAM,CAAA;AAC5C","file":"chunk-FS3P4V5M.cjs","sourcesContent":["import type { Schema } from '@internal/ai-v6';\nimport type { StandardSchemaV1, StandardJSONSchemaV1 } from '@standard-schema/spec';\nimport type { JSONSchema7 } from 'json-schema';\nimport type { StandardSchemaWithJSON, StandardSchemaWithJSONProps } from '../standard-schema.types';\n\n/**\n * Vendor name for AI SDK wrapped schemas.\n */\nconst VENDOR = 'ai-sdk';\n\n/**\n * A wrapper class that makes AI SDK Schema compatible with @standard-schema/spec.\n *\n * This class implements both `StandardSchemaV1` (validation) and `StandardJSONSchemaV1`\n * (JSON Schema conversion) interfaces. It wraps an AI SDK Schema and adapts its\n * validation method and jsonSchema property to the standard-schema interface.\n *\n * @typeParam T - The TypeScript type that the AI SDK Schema represents\n *\n * @example\n * ```typescript\n * import { jsonSchema } from '@internal/ai-v6';\n * import { toStandardSchema } from '@mastra/schema-compat/adapters/ai-sdk';\n *\n * // Create an AI SDK schema\n * const aiSdkSchema = jsonSchema<{ name: string; age: number }>({\n * type: 'object',\n * properties: {\n * name: { type: 'string' },\n * age: { type: 'number' },\n * },\n * required: ['name', 'age'],\n * });\n *\n * // Convert to standard-schema\n * const standardSchema = toStandardSchema(aiSdkSchema);\n *\n * // Use validation (from StandardSchemaV1)\n * const result = standardSchema['~standard'].validate({ name: 'John', age: 30 });\n *\n * // Get JSON Schema (from StandardJSONSchemaV1)\n * const jsonSchema = standardSchema['~standard'].jsonSchema.output({ target: 'draft-07' });\n * ```\n */\nexport class AiSdkSchemaWrapper implements StandardSchemaWithJSON {\n readonly #schema: Schema;\n readonly '~standard': StandardSchemaWithJSONProps;\n\n constructor(schema: Schema) {\n this.#schema = schema;\n\n // Create the ~standard property\n this['~standard'] = {\n version: 1,\n vendor: VENDOR,\n validate: this.#validate.bind(this),\n jsonSchema: {\n input: this.#toJsonSchema.bind(this),\n output: this.#toJsonSchema.bind(this),\n },\n };\n }\n\n /**\n * Validates a value against the AI SDK Schema.\n *\n * @param value - The value to validate\n * @returns A result object with either the validated value or validation issues\n */\n #validate(value: unknown): StandardSchemaV1.Result | Promise> {\n // Check if the schema has a validate method (it's optional on AI SDK Schema)\n if (!this.#schema.validate) {\n // If no validate method, we can't validate - just pass through\n return { value: value as Output };\n }\n\n try {\n const result = this.#schema.validate(value);\n\n // Handle both sync and async validation results\n // The AI SDK Schema.validate returns ValidationResult | PromiseLike>\n // We need to check if it's a thenable (promise-like)\n if (result && typeof result === 'object' && 'then' in result && typeof result.then === 'function') {\n // Wrap PromiseLike in a proper Promise to satisfy the StandardSchemaV1 interface\n return Promise.resolve(\n result as PromiseLike<{ success: true; value: Output } | { success: false; error: Error }>,\n )\n .then(res => this.#convertValidationResult(res))\n .catch((error: unknown) => {\n // Convert rejected promises to the expected { issues: [...] } shape\n const message = error instanceof Error ? error.message : 'Unknown validation error';\n return {\n issues: [{ message: `Schema validation error: ${message}` }],\n } as StandardSchemaV1.Result;\n });\n }\n\n // It's a sync result\n return this.#convertValidationResult(\n result as { success: true; value: Output } | { success: false; error: Error },\n );\n } catch (error) {\n // If validation fails unexpectedly, return a validation error\n const message = error instanceof Error ? error.message : 'Unknown validation error';\n return {\n issues: [{ message: `Schema validation error: ${message}` }],\n };\n }\n }\n\n /**\n * Converts an AI SDK ValidationResult to a StandardSchemaV1.Result.\n *\n * @param result - The AI SDK validation result\n * @returns A StandardSchemaV1.Result\n */\n #convertValidationResult(\n result: { success: true; value: Output } | { success: false; error: Error },\n ): StandardSchemaV1.Result {\n if (result.success) {\n return { value: result.value };\n }\n\n // Convert the AI SDK error to a Standard Schema issue\n // Cast to the failure type since TypeScript can't narrow discriminated unions with private field access\n const failureResult = result as { success: false; error: Error };\n return {\n issues: [{ message: failureResult.error.message }],\n };\n }\n\n /**\n * Returns the JSON Schema in the requested target format.\n *\n * @param options - Options including the target format\n * @returns The JSON Schema as a Record\n */\n #toJsonSchema(options: StandardJSONSchemaV1.Options): Record {\n const { target } = options;\n\n // Clone the schema to avoid mutations\n const clonedSchema = JSON.parse(JSON.stringify(this.#schema.jsonSchema)) as Record;\n\n // Add $schema if not present, based on target\n if (!clonedSchema.$schema) {\n switch (target) {\n case 'draft-07':\n clonedSchema.$schema = 'http://json-schema.org/draft-07/schema#';\n break;\n case 'draft-2020-12':\n clonedSchema.$schema = 'https://json-schema.org/draft/2020-12/schema';\n break;\n case 'openapi-3.0':\n // OpenAPI 3.0 doesn't use $schema\n break;\n default:\n // For unknown targets, don't add $schema\n break;\n }\n }\n\n return clonedSchema;\n }\n\n /**\n * Returns the original AI SDK Schema.\n */\n getSchema(): Schema {\n return this.#schema;\n }\n\n /**\n * Returns the original JSON Schema from the AI SDK Schema.\n */\n getJsonSchema(): JSONSchema7 {\n return this.#schema.jsonSchema as JSONSchema7;\n }\n}\n\n/**\n * Wraps an AI SDK Schema to implement the full @standard-schema/spec interface.\n *\n * This function creates a wrapper that implements both `StandardSchemaV1` (validation)\n * and `StandardJSONSchemaV1` (JSON Schema conversion) interfaces.\n *\n * @typeParam T - The TypeScript type that the AI SDK Schema represents\n * @param schema - The AI SDK Schema to wrap\n * @returns A wrapper implementing StandardSchemaWithJSON\n *\n * @example\n * ```typescript\n * import { jsonSchema } from '@internal/ai-v6';\n * import { toStandardSchema } from '@mastra/schema-compat/adapters/ai-sdk';\n *\n * const aiSdkSchema = jsonSchema<{ name: string; age: number }>({\n * type: 'object',\n * properties: {\n * name: { type: 'string' },\n * age: { type: 'number' },\n * },\n * required: ['name', 'age'],\n * });\n *\n * const standardSchema = toStandardSchema(aiSdkSchema);\n *\n * // Validate data\n * const result = standardSchema['~standard'].validate({ name: 'John', age: 30 });\n *\n * // Get JSON Schema\n * const jsonSchema = standardSchema['~standard'].jsonSchema.output({ target: 'draft-07' });\n * ```\n */\nexport function toStandardSchema(schema: Schema): AiSdkSchemaWrapper {\n return new AiSdkSchemaWrapper(schema);\n}\n"]}