{"version":3,"file":"mastra-core-imports.mjs","names":[],"sources":["../../../src/codemods/v1/mastra-core-imports.ts"],"sourcesContent":["import { createTransformer } from '../lib/create-transformer';\n\n/**\n * For v1 we removed all top-level exports from \"@mastra/core\" except for `Mastra` and `type Config`.\n * All other imports should use subpath imports, e.g. `import { Agent } from \"@mastra/core/agent\"`.\n *\n * This codemod updates all imports from \"@mastra/core\" to use the new subpath imports. It leaves imports to `Mastra` and `Config` unchanged.\n */\n\n// TODO: Do not hardcode this mapping, generate it from the package's exports in the future\nconst EXPORT_TO_SUBPATH: Record<string, string> = {\n  // Agent\n  Agent: '@mastra/core/agent',\n\n  // Tools\n  createTool: '@mastra/core/tools',\n  Tool: '@mastra/core/tools',\n\n  // Workflows\n  createWorkflow: '@mastra/core/workflows',\n  createStep: '@mastra/core/workflows',\n  Workflow: '@mastra/core/workflows',\n  Step: '@mastra/core/workflows',\n\n  // Request Context\n  RequestContext: '@mastra/core/request-context',\n\n  // Processors\n  BatchPartsProcessor: '@mastra/core/processors',\n  PIIDetector: '@mastra/core/processors',\n  ModerationProcessor: '@mastra/core/processors',\n  TokenLimiterProcessor: '@mastra/core/processors',\n  Processor: '@mastra/core/processors',\n  UnicodeNormalizer: '@mastra/core/processors',\n  SystemPromptScrubber: '@mastra/core/processors',\n  PromptInjectionDetector: '@mastra/core/processors',\n  LanguageDetector: '@mastra/core/processors',\n\n  // Voice\n  CompositeVoice: '@mastra/core/voice',\n\n  // Scorers/Evals\n  runEvals: '@mastra/core/evals',\n  createScorer: '@mastra/core/evals',\n\n  // Server\n  registerApiRoute: '@mastra/core/server',\n\n  // Observability\n  DefaultExporter: '@mastra/observability',\n  MastraStorageExporter: '@mastra/observability',\n  CloudExporter: '@mastra/observability',\n  MastraPlatformExporter: '@mastra/observability',\n\n  // Streaming\n  ChunkType: '@mastra/core/stream',\n  MastraMessageV2: '@mastra/core/stream',\n\n  // LLM/Models\n  ModelRouterEmbeddingModel: '@mastra/core/llm',\n};\n\nexport default createTransformer((fileInfo, api, options, context) => {\n  const { j, root } = context;\n\n  // Find all import declarations from '@mastra/core'\n  root\n    .find(j.ImportDeclaration, {\n      source: { value: '@mastra/core' },\n    })\n    .forEach(importPath => {\n      const node = importPath.node;\n      const specifiers = node.specifiers || [];\n      const declarationImportKind = node.importKind || 'value';\n\n      // Categorize specifiers into those that stay vs those that move\n      const { remainingSpecifiers, importsToMove } = categorizeImports(specifiers, declarationImportKind);\n\n      // Early return: No imports to move\n      if (importsToMove.length === 0) return;\n\n      context.hasChanges = true;\n\n      // Group imports by their target subpath\n      const groupedImports = groupImportsBySubpath(importsToMove);\n\n      // Create new import declarations for each subpath\n      const newImports = createNewImports(j, groupedImports, context);\n\n      // Insert new imports after the current one (in reverse to maintain order)\n      insertImports(j, importPath, newImports);\n\n      // Update or remove the original import\n      updateOriginalImport(j, importPath, node, remainingSpecifiers, context);\n    });\n});\n\n/**\n * Categorize import specifiers into those that stay vs those that move\n */\nfunction categorizeImports(specifiers: any[], declarationImportKind: 'type' | 'typeof' | 'value') {\n  const remainingSpecifiers: any[] = [];\n  const importsToMove: Array<{\n    subpath: string;\n    localName: string;\n    importedName: string;\n    importKind: 'type' | 'typeof' | 'value';\n    isDeclarationType: boolean;\n  }> = [];\n\n  specifiers.forEach(specifier => {\n    // Keep default and namespace imports as-is\n    if (specifier.type !== 'ImportSpecifier') {\n      remainingSpecifiers.push(specifier);\n      return;\n    }\n\n    const imported = specifier.imported;\n    const importedName = getImportedName(imported);\n    const localName = specifier.local?.name || importedName;\n    const specifierImportKind = specifier.importKind || 'value';\n\n    // Determine effective importKind:\n    // - If declaration is \"import type {}\", use 'type' for all specifiers\n    // - Otherwise, use the specifier's own importKind\n    const effectiveImportKind = declarationImportKind !== 'value' ? declarationImportKind : specifierImportKind;\n    const isDeclarationType = declarationImportKind !== 'value';\n\n    // Check if this import should be moved to a subpath\n    const newSubpath = EXPORT_TO_SUBPATH[importedName];\n\n    if (newSubpath) {\n      importsToMove.push({\n        subpath: newSubpath,\n        localName,\n        importedName,\n        importKind: effectiveImportKind,\n        isDeclarationType,\n      });\n    } else {\n      // This import stays at '@mastra/core' (e.g., Mastra, Config)\n      remainingSpecifiers.push(specifier);\n    }\n  });\n\n  return { remainingSpecifiers, importsToMove };\n}\n\n/**\n * Extract the imported name from an import specifier\n */\nfunction getImportedName(imported: any): string {\n  if (imported.type === 'Identifier') {\n    return imported.name;\n  }\n  // Handle string literal imports (edge case)\n  return imported.value || '';\n}\n\n/**\n * Group imports by their target subpath and importKind\n */\nfunction groupImportsBySubpath(\n  importsToMove: Array<{\n    subpath: string;\n    localName: string;\n    importedName: string;\n    importKind: 'type' | 'typeof' | 'value';\n    isDeclarationType: boolean;\n  }>,\n) {\n  const groupedImports = new Map<\n    string,\n    Array<{\n      localName: string;\n      importedName: string;\n      importKind: 'type' | 'typeof' | 'value';\n      isDeclarationType: boolean;\n    }>\n  >();\n\n  importsToMove.forEach(({ subpath, localName, importedName, importKind, isDeclarationType }) => {\n    // Create a key that includes both subpath and importKind to ensure separate import declarations\n    const key = `${subpath}::${importKind}::${isDeclarationType}`;\n    if (!groupedImports.has(key)) {\n      groupedImports.set(key, []);\n    }\n    groupedImports.get(key)!.push({ localName, importedName, importKind, isDeclarationType });\n  });\n\n  return groupedImports;\n}\n\n/**\n * Create new import declarations for each subpath and importKind\n */\nfunction createNewImports(\n  j: any,\n  groupedImports: Map<\n    string,\n    Array<{\n      localName: string;\n      importedName: string;\n      importKind: 'type' | 'typeof' | 'value';\n      isDeclarationType: boolean;\n    }>\n  >,\n  context: any,\n) {\n  const newImports: any[] = [];\n\n  groupedImports.forEach((imports, key) => {\n    // Extract subpath, importKind, and isDeclarationType from the composite key\n    const [subpath, importKind] = key.split('::');\n\n    const newSpecifiers = imports.map(({ localName, importedName }) => {\n      if (localName === importedName) {\n        // import { Agent } from '@mastra/core/agent'\n        return j.importSpecifier(j.identifier(importedName));\n      } else {\n        // import { Agent as MastraAgent } from '@mastra/core/agent'\n        return j.importSpecifier(j.identifier(importedName), j.identifier(localName));\n      }\n      // Note: We don't set importKind on specifiers since we're creating\n      // separate import declarations for each importKind. All specifiers in a type\n      // import group will be in an \"import type\" declaration.\n    });\n\n    const newImport = j.importDeclaration(newSpecifiers, j.stringLiteral(subpath));\n\n    // Set importKind on declaration if this is a type import (either declaration-level or inline)\n    if (importKind !== 'value') {\n      newImport.importKind = importKind;\n    }\n\n    newImports.push(newImport);\n\n    // Log which imports were moved to which subpath\n    const importList = imports.map(i => i.importedName).join(', ');\n    const kindLabel = importKind !== 'value' ? ` (${importKind})` : '';\n    context.messages.push(`Moved imports to '${subpath}'${kindLabel}: ${importList}`);\n  });\n\n  return newImports;\n}\n\n/**\n * Insert new imports after the current import (in reverse to maintain order)\n */\nfunction insertImports(j: any, importPath: any, newImports: any[]) {\n  newImports.reverse().forEach(newImport => {\n    j(importPath).insertAfter(newImport);\n  });\n}\n\n/**\n * Update or remove the original import declaration\n */\nfunction updateOriginalImport(j: any, importPath: any, node: any, remainingSpecifiers: any[], context: any) {\n  if (remainingSpecifiers.length > 0) {\n    // Keep the original import with only the remaining specifiers\n    node.specifiers = remainingSpecifiers;\n\n    const remainingList = extractRemainingImportNames(remainingSpecifiers);\n    if (remainingList) {\n      context.messages.push(`Kept at '@mastra/core': ${remainingList}`);\n    }\n  } else {\n    // Remove the original import entirely (all imports moved)\n    j(importPath).remove();\n    context.messages.push(`Removed original '@mastra/core' import (all imports moved to subpaths)`);\n  }\n}\n\n/**\n * Extract the names of remaining imports for logging\n */\nfunction extractRemainingImportNames(remainingSpecifiers: any[]): string {\n  return remainingSpecifiers\n    .filter(s => s.type === 'ImportSpecifier')\n    .map(s => s.imported?.name || s.local?.name)\n    .filter(Boolean)\n    .join(', ');\n}\n"],"mappings":";;;;;;;;AAUA,MAAM,oBAA4C;CAEhD,OAAO;CAGP,YAAY;CACZ,MAAM;CAGN,gBAAgB;CAChB,YAAY;CACZ,UAAU;CACV,MAAM;CAGN,gBAAgB;CAGhB,qBAAqB;CACrB,aAA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