'use strict'; var chunkDURT53SS_cjs = require('./chunk-DURT53SS.cjs'); var fs = require('fs/promises'); var path = require('path'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } var fs__default = /*#__PURE__*/_interopDefault(fs); var path__default = /*#__PURE__*/_interopDefault(path); function hasAttachmentCapabilities(gateway) { return "getAttachmentCapabilities" in gateway && typeof gateway.getAttachmentCapabilities === "function"; } async function atomicWriteFile(filePath, content, encoding = "utf-8") { const randomSuffix = Math.random().toString(36).substring(2, 15); const tempPath = `${filePath}.${process.pid}.${Date.now()}.${randomSuffix}.tmp`; try { await fs__default.default.writeFile(tempPath, content, encoding); await fs__default.default.rename(tempPath, filePath); } catch (error) { try { await fs__default.default.unlink(tempPath); } catch { } throw error; } } async function fetchProvidersFromGateways(gateways) { const enabledGateways = []; for (const gateway of gateways) { if (chunkDURT53SS_cjs.shouldEnableGateway(gateway)) { enabledGateways.push(gateway); } } const allProviders = {}; const allModels = {}; const allAttachmentCapabilities = {}; const maxRetries = 3; for (const gateway of enabledGateways) { let providers = null; for (let attempt = 1; attempt <= maxRetries; attempt++) { try { providers = await gateway.fetchProviders(); break; } catch { if (attempt < maxRetries) { const delayMs = Math.min(1e3 * Math.pow(2, attempt - 1), 5e3); await new Promise((resolve) => setTimeout(resolve, delayMs)); } } } if (!providers) continue; const gatewayId = chunkDURT53SS_cjs.getGatewayId(gateway); const isProviderRegistry = gatewayId === "models.dev"; const gatewayAttachmentCaps = hasAttachmentCapabilities(gateway) ? gateway.getAttachmentCapabilities() : void 0; for (const [providerId, config] of Object.entries(providers)) { const typeProviderId = isProviderRegistry ? providerId : providerId === gatewayId ? gatewayId : `${gatewayId}/${providerId}`; allProviders[typeProviderId] = config; allModels[typeProviderId] = config.models.sort(); if (gatewayAttachmentCaps?.[providerId]) { allAttachmentCapabilities[typeProviderId] = gatewayAttachmentCaps[providerId]; } } } return { providers: allProviders, models: allModels, attachmentCapabilities: allAttachmentCapabilities }; } function generateTypesContent(models) { const providerModelsEntries = Object.entries(models).map(([provider, modelList]) => { const modelsList = modelList.map((m) => `'${m}'`); const needsQuotes = !/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(provider); const providerKey = needsQuotes ? `'${provider}'` : provider; const singleLine = ` readonly ${providerKey}: readonly [${modelsList.join(", ")}];`; if (singleLine.length > 120) { const formattedModels = modelList.map((m) => ` '${m}',`).join("\n"); return ` readonly ${providerKey}: readonly [ ${formattedModels} ];`; } return singleLine; }).join("\n"); return `/** * THIS FILE IS AUTO-GENERATED - DO NOT EDIT * Generated from model gateway providers */ /** * Provider models mapping type * This is derived from the JSON data and provides type-safe access */ export type ProviderModelsMap = { ${providerModelsEntries} }; /** * Union type of all registered provider IDs */ export type Provider = keyof ProviderModelsMap; /** * Provider models mapping interface */ export interface ProviderModels { [key: string]: string[]; } /** * OpenAI-compatible model ID type * Dynamically derived from ProviderModelsMap * Full provider/model paths (e.g., "openai/gpt-4o", "anthropic/claude-3-5-sonnet-20241022") */ export type ModelRouterModelId = | { [P in Provider]: \`\${P}/\${ProviderModelsMap[P][number]}\`; }[Provider] | \`mastra/\${ProviderModelsMap['openrouter'][number]}\` | (string & {}); /** * Extract the model part from a ModelRouterModelId for a specific provider * Dynamically derived from ProviderModelsMap * Example: ModelForProvider<'openai'> = 'gpt-4o' | 'gpt-4-turbo' | ... */ export type ModelForProvider

= ProviderModelsMap[P][number]; `; } async function writeRegistryFiles(jsonPath, typesPath, providers, models, attachmentCapabilities) { const jsonDir = path__default.default.dirname(jsonPath); const typesDir = path__default.default.dirname(typesPath); await fs__default.default.mkdir(jsonDir, { recursive: true }); await fs__default.default.mkdir(typesDir, { recursive: true }); const registryData = { providers, models, version: "1.0.0" }; await atomicWriteFile(jsonPath, JSON.stringify(registryData, null, 2), "utf-8"); const typeContent = generateTypesContent(models); await atomicWriteFile(typesPath, typeContent, "utf-8"); if (attachmentCapabilities && Object.keys(attachmentCapabilities).length > 0) { const capDir = path__default.default.join(jsonDir, "capabilities"); await fs__default.default.mkdir(capDir, { recursive: true }); try { const existing = await fs__default.default.readdir(capDir); for (const file of existing) { if (file.endsWith(".json")) { await fs__default.default.unlink(path__default.default.join(capDir, file)); } } } catch { } for (const [provider, models2] of Object.entries(attachmentCapabilities)) { const providerFile = path__default.default.join(capDir, `${provider}.json`); await atomicWriteFile(providerFile, JSON.stringify({ attachment: models2 }, null, 2), "utf-8"); } } } exports.atomicWriteFile = atomicWriteFile; exports.fetchProvidersFromGateways = fetchProvidersFromGateways; exports.generateTypesContent = generateTypesContent; exports.writeRegistryFiles = writeRegistryFiles; //# sourceMappingURL=registry-generator-LJFBZ6Z3.cjs.map //# sourceMappingURL=registry-generator-LJFBZ6Z3.cjs.map