{"version":3,"file":"ai-sdk.cjs","names":["#schema","#validate","#toJsonSchema","#convertValidationResult","failureResult"],"sources":["../../../src/standard-schema/adapters/ai-sdk.ts"],"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<Input = unknown, Output = Input> implements StandardSchemaWithJSON<Input, Output> {\n  readonly #schema: Schema<Output>;\n  readonly '~standard': StandardSchemaWithJSONProps<Input, Output>;\n\n  constructor(schema: Schema<Output>) {\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<Output> | Promise<StandardSchemaV1.Result<Output>> {\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<OBJECT> | PromiseLike<ValidationResult<OBJECT>>\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<Output>;\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<Output> {\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<string, unknown> {\n    const { target } = options;\n\n    // Clone the schema to avoid mutations\n    const clonedSchema = JSON.parse(JSON.stringify(this.#schema.jsonSchema)) as Record<string, unknown>;\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<Output> {\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<T = unknown>(schema: Schema<T>): AiSdkSchemaWrapper<T, T> {\n  return new AiSdkSchemaWrapper<T, T>(schema);\n}\n"],"mappings":";;;;;AAQA,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCf,IAAa,qBAAb,MAAkH;CAChH;CACA;CAEA,YAAY,QAAwB;EAClC,KAAKA,UAAU;EAGf,KAAK,eAAe;GAClB,SAAS;GACT,QAAQ;GACR,UAAU,KAAKC,UAAU,KAAK,IAAI;GAClC,YAAY;IACV,OAAO,KAAKC,cAAc,KAAK,IAAI;IACnC,QAAQ,KAAKA,cAAc,KAAK,IAAI;GACtC;EACF;CACF;;;;;;;CAQA,UAAU,OAA4F;EAEpG,IAAI,CAAC,KAAKF,QAAQ,UAEhB,OAAO,EAAS,MAAgB;EAGlC,IAAI;GACF,MAAM,SAAS,KAAKA,QAAQ,SAAS,KAAK;GAK1C,IAAI,UAAU,OAAO,WAAW,YAAY,UAAU,UAAU,OAAO,OAAO,SAAS,YAErF,OAAO,QAAQ,QACb,MACF,CAAC,CACE,MAAK,QAAO,KAAKG,yBAAyB,GAAG,CAAC,CAAC,CAC/C,OAAO,UAAmB;IAGzB,OAAO,EACL,QAAQ,CAAC,EAAE,SAAS,4BAFN,iBAAiB,QAAQ,MAAM,UAAU,6BAEG,CAAC,EAC7D;GACF,CAAC;GAIL,OAAO,KAAKA,yBACV,MACF;EACF,SAAS,OAAO;GAGd,OAAO,EACL,QAAQ,CAAC,EAAE,SAAS,4BAFN,iBAAiB,QAAQ,MAAM,UAAU,6BAEG,CAAC,EAC7D;EACF;CACF;;;;;;;CAQA,yBACE,QACiC;EACjC,IAAI,OAAO,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