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BAAA,CAAG,OAAA,CAAQ,MAAM,CAAA;AACxC,MAAA,KAAA,MAAW,QAAQ,QAAA,EAAU;AAC3B,QAAA,IAAI,IAAA,CAAK,QAAA,CAAS,OAAO,CAAA,EAAG;AAC1B,UAAA,MAAMA,oBAAG,MAAA,CAAOG,qBAAA,CAAK,IAAA,CAAK,MAAA,EAAQ,IAAI,CAAC,CAAA;AAAA,QACzC;AAAA,MACF;AAAA,IACF,CAAA,CAAA,MAAQ;AAAA,IAER;AAEA,IAAA,KAAA,MAAW,CAAC,QAAA,EAAUC,OAAM,KAAK,MAAA,CAAO,OAAA,CAAQ,sBAAsB,CAAA,EAAG;AACvE,MAAA,MAAM,eAAeD,qBAAA,CAAK,IAAA,CAAK,MAAA,EAAQ,CAAA,EAAG,QAAQ,CAAA,KAAA,CAAO,CAAA;AACzD,MAAA,MAAM,eAAA,CAAgB,YAAA,EAAc,IAAA,CAAK,SAAA,CAAU,EAAE,UAAA,EAAYC,OAAAA,EAAO,EAAG,IAAA,EAAM,CAAC,CAAA,EAAG,OAAO,CAAA;AAAA,IAC9F;AAAA,EACF;AACF","file":"registry-generator-LJFBZ6Z3.cjs","sourcesContent":["/**\n * Shared provider registry generation logic\n * Used by both the CLI generation script and runtime refresh\n */\n\nimport fs from 'node:fs/promises';\nimport path from 'node:path';\nimport type { AttachmentCapabilities, MastraModelGatewayInterface, ProviderConfig } from './gateways/base.js';\nimport { getGatewayId, shouldEnableGateway } from './gateways/index.js';\n\ninterface GatewayWithAttachmentCapabilities {\n getAttachmentCapabilities(): AttachmentCapabilities;\n}\n\nfunction hasAttachmentCapabilities(\n gateway: MastraModelGatewayInterface,\n): gateway is MastraModelGatewayInterface & GatewayWithAttachmentCapabilities {\n return (\n 'getAttachmentCapabilities' in gateway &&\n typeof (gateway as { getAttachmentCapabilities?: unknown }).getAttachmentCapabilities === 'function'\n );\n}\n\n/**\n * Write a file atomically using the write-to-temp-then-rename pattern.\n * This prevents file corruption when multiple processes write to the same file concurrently.\n *\n * The rename operation is atomic on POSIX systems when source and destination\n * are on the same filesystem.\n *\n * @param filePath - The target file path\n * @param content - The content to write\n * @param encoding - The encoding to use (default: 'utf-8')\n */\nexport async function atomicWriteFile(\n filePath: string,\n content: string,\n encoding: BufferEncoding = 'utf-8',\n): Promise {\n // Create a unique temp file name using PID, timestamp, and random suffix to avoid collisions\n const randomSuffix = Math.random().toString(36).substring(2, 15);\n const tempPath = `${filePath}.${process.pid}.${Date.now()}.${randomSuffix}.tmp`;\n\n try {\n // Write to temp file first\n await fs.writeFile(tempPath, content, encoding);\n\n // Atomically rename temp file to target path\n // This is atomic on POSIX when both paths are on the same filesystem\n await fs.rename(tempPath, filePath);\n } catch (error) {\n // Clean up temp file if it exists\n try {\n await fs.unlink(tempPath);\n } catch {\n // Ignore cleanup errors\n }\n throw error;\n }\n}\n\n/**\n * Fetch providers from all enabled gateways with silent retry logic.\n * Retries up to 3 times per gateway with exponential backoff. If all\n * retries are exhausted the gateway is silently skipped (no error logging)\n * since the bundled registry already contains all model data.\n * @param gateways - Array of gateway instances to fetch from\n * @returns Object containing providers and models records\n */\nexport async function fetchProvidersFromGateways(gateways: MastraModelGatewayInterface[]): Promise<{\n providers: Record;\n models: Record;\n attachmentCapabilities: AttachmentCapabilities;\n}> {\n const enabledGateways: MastraModelGatewayInterface[] = [];\n\n for (const gateway of gateways) {\n if (shouldEnableGateway(gateway)) {\n enabledGateways.push(gateway);\n }\n }\n\n const allProviders: Record = {};\n const allModels: Record = {};\n const allAttachmentCapabilities: AttachmentCapabilities = {};\n\n const maxRetries = 3;\n\n for (const gateway of enabledGateways) {\n let providers: Record | null = null;\n\n for (let attempt = 1; attempt <= maxRetries; attempt++) {\n try {\n providers = await gateway.fetchProviders();\n break;\n } catch {\n if (attempt < maxRetries) {\n const delayMs = Math.min(1000 * Math.pow(2, attempt - 1), 5000);\n await new Promise(resolve => setTimeout(resolve, delayMs));\n }\n }\n }\n\n // If all retries failed, silently skip this gateway — the bundled\n // registry already contains all model data.\n if (!providers) continue;\n\n const gatewayId = getGatewayId(gateway);\n // models.dev is a provider registry, not a true gateway - don't prefix its providers\n const isProviderRegistry = gatewayId === 'models.dev';\n\n // Collect attachment capabilities if the gateway exposes them\n const gatewayAttachmentCaps = hasAttachmentCapabilities(gateway) ? gateway.getAttachmentCapabilities() : undefined;\n\n for (const [providerId, config] of Object.entries(providers)) {\n // For true gateways, use gateway id as prefix (e.g., \"netlify/anthropic\")\n // Special case: if providerId matches gateway id, it's a unified gateway (e.g., azure-openai returning {azure-openai: {...}})\n // In this case, use just the gateway ID to avoid duplication (azure-openai, not azure-openai/azure-openai)\n const typeProviderId = isProviderRegistry\n ? providerId\n : providerId === gatewayId\n ? gatewayId\n : `${gatewayId}/${providerId}`;\n\n allProviders[typeProviderId] = config;\n // Sort models alphabetically for consistent ordering\n allModels[typeProviderId] = config.models.sort();\n\n // Merge attachment capabilities for this provider if available\n if (gatewayAttachmentCaps?.[providerId]) {\n allAttachmentCapabilities[typeProviderId] = gatewayAttachmentCaps[providerId];\n }\n }\n }\n\n return { providers: allProviders, models: allModels, attachmentCapabilities: allAttachmentCapabilities };\n}\n\n/**\n * Generate TypeScript type definitions content\n * @param models - Record of provider IDs to model arrays\n * @returns Generated TypeScript type definitions as a string\n */\nexport function generateTypesContent(models: Record): string {\n const providerModelsEntries = Object.entries(models)\n .map(([provider, modelList]) => {\n const modelsList = modelList.map(m => `'${m}'`);\n\n // Quote provider key if it's not a valid JavaScript identifier\n // Valid identifiers must start with a letter, underscore, or dollar sign\n const needsQuotes = !/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(provider);\n const providerKey = needsQuotes ? `'${provider}'` : provider;\n\n // Format array based on length (prettier printWidth: 120)\n const singleLine = ` readonly ${providerKey}: readonly [${modelsList.join(', ')}];`;\n\n // If single line exceeds 120 chars, format as multi-line\n if (singleLine.length > 120) {\n const formattedModels = modelList.map(m => ` '${m}',`).join('\\n');\n return ` readonly ${providerKey}: readonly [\\n${formattedModels}\\n ];`;\n }\n\n return singleLine;\n })\n .join('\\n');\n\n return `/**\n * THIS FILE IS AUTO-GENERATED - DO NOT EDIT\n * Generated from model gateway providers\n */\n\n/**\n * Provider models mapping type\n * This is derived from the JSON data and provides type-safe access\n */\nexport type ProviderModelsMap = {\n${providerModelsEntries}\n};\n\n/**\n * Union type of all registered provider IDs\n */\nexport type Provider = keyof ProviderModelsMap;\n\n/**\n * Provider models mapping interface\n */\nexport interface ProviderModels {\n [key: string]: string[];\n}\n\n/**\n * OpenAI-compatible model ID type\n * Dynamically derived from ProviderModelsMap\n * Full provider/model paths (e.g., \"openai/gpt-4o\", \"anthropic/claude-3-5-sonnet-20241022\")\n */\nexport type ModelRouterModelId =\n | {\n [P in Provider]: \\`\\${P}/\\${ProviderModelsMap[P][number]}\\`;\n }[Provider]\n | \\`mastra/\\${ProviderModelsMap['openrouter'][number]}\\`\n | (string & {});\n\n/**\n * Extract the model part from a ModelRouterModelId for a specific provider\n * Dynamically derived from ProviderModelsMap\n * Example: ModelForProvider<'openai'> = 'gpt-4o' | 'gpt-4-turbo' | ...\n */\nexport type ModelForProvider

= ProviderModelsMap[P][number];\n`;\n}\n\n/**\n * Write registry files to disk (JSON and .d.ts)\n * @param jsonPath - Path to write the JSON file\n * @param typesPath - Path to write the .d.ts file\n * @param providers - Provider configurations\n * @param models - Model lists by provider\n */\nexport async function writeRegistryFiles(\n jsonPath: string,\n typesPath: string,\n providers: Record,\n models: Record,\n attachmentCapabilities?: AttachmentCapabilities,\n): Promise {\n // 0. Ensure directories exist\n const jsonDir = path.dirname(jsonPath);\n const typesDir = path.dirname(typesPath);\n await fs.mkdir(jsonDir, { recursive: true });\n await fs.mkdir(typesDir, { recursive: true });\n\n // 1. Write JSON file atomically to prevent corruption from concurrent writes\n const registryData = {\n providers,\n models,\n version: '1.0.0',\n };\n\n await atomicWriteFile(jsonPath, JSON.stringify(registryData, null, 2), 'utf-8');\n\n // 2. Generate .d.ts file with type-only declarations (also atomic)\n const typeContent = generateTypesContent(models);\n await atomicWriteFile(typesPath, typeContent, 'utf-8');\n\n // 3. Write per-provider capability files into a capabilities/ directory\n if (attachmentCapabilities && Object.keys(attachmentCapabilities).length > 0) {\n const capDir = path.join(jsonDir, 'capabilities');\n await fs.mkdir(capDir, { recursive: true });\n\n // Clean out stale provider files from previous runs\n try {\n const existing = await fs.readdir(capDir);\n for (const file of existing) {\n if (file.endsWith('.json')) {\n await fs.unlink(path.join(capDir, file));\n }\n }\n } catch {\n // Directory may not exist yet — ignore\n }\n\n for (const [provider, models] of Object.entries(attachmentCapabilities)) {\n const providerFile = path.join(capDir, `${provider}.json`);\n await atomicWriteFile(providerFile, JSON.stringify({ attachment: models }, null, 2), 'utf-8');\n }\n }\n}\n"]}