{"version":3,"file":"runtime-context.mjs","names":[],"sources":["../../../src/codemods/v1/runtime-context.ts"],"sourcesContent":["import { createTransformer } from '../lib/create-transformer';\n\n/**\n * The `RuntimeContext` class has been renamed to `RequestContext`, and all parameter names have been updated from `runtimeContext` to `requestContext` across all APIs.\n */\n\nexport default createTransformer((fileInfo, api, options, context) => {\n  const { j, root } = context;\n\n  // Track whether RuntimeContext was imported from @mastra/core/runtime-context\n  let hasRuntimeContextImport = false;\n\n  // 1. Update import declarations from runtime-context to request-context\n  root.find(j.ImportDeclaration).forEach(importPath => {\n    const node = importPath.node;\n\n    // Early return: Only process imports from @mastra/core/runtime-context\n    if (node.source.value !== '@mastra/core/runtime-context') return;\n\n    // Update the import path\n    node.source.value = '@mastra/core/request-context';\n    context.hasChanges = true;\n\n    // Update RuntimeContext to RequestContext in import specifiers\n    node.specifiers?.forEach(specifier => {\n      if (specifier.type !== 'ImportSpecifier') return;\n\n      const imported = specifier.imported;\n      if (imported.type === 'Identifier' && imported.name === 'RuntimeContext') {\n        hasRuntimeContextImport = true;\n        imported.name = 'RequestContext';\n        context.messages.push(`Updated import: RuntimeContext → RequestContext from '@mastra/core/request-context'`);\n      }\n    });\n  });\n\n  // Early return: Only proceed if RuntimeContext was imported from Mastra\n  if (!hasRuntimeContextImport) return;\n\n  // 2. Rename RuntimeContext type/class references\n  renameIdentifiers(j, root, context, 'RuntimeContext', 'RequestContext', 'type');\n\n  // 3. Rename runtimeContext variable/parameter identifiers\n  renameIdentifiers(j, root, context, 'runtimeContext', 'requestContext', 'variable/parameter');\n\n  // 4. Rename string literal 'runtimeContext' to 'requestContext' in Mastra middleware\n  renameMiddlewareStringLiterals(j, root, context);\n});\n\n/**\n * Helper to rename all occurrences of an identifier\n */\nfunction renameIdentifiers(j: any, root: any, context: any, oldName: string, newName: string, description: string) {\n  const identifiers = root.find(j.Identifier, { name: oldName });\n  const count = identifiers.length;\n\n  if (count === 0) return;\n\n  identifiers.forEach((path: any) => {\n    path.node.name = newName;\n  });\n\n  context.hasChanges = true;\n  context.messages.push(`Renamed ${count} ${oldName} ${description} references to ${newName}`);\n}\n\n/**\n * Helper to rename 'runtimeContext' string literals in Mastra middleware handlers\n */\nfunction renameMiddlewareStringLiterals(j: any, root: any, context: any) {\n  let stringLiteralCount = 0;\n\n  // Find all new Mastra({ ... }) expressions\n  root\n    .find(j.NewExpression, {\n      callee: { type: 'Identifier', name: 'Mastra' },\n    })\n    .forEach((mastraPath: any) => {\n      const configArg = mastraPath.node.arguments[0];\n      if (!configArg || configArg.type !== 'ObjectExpression') return;\n\n      // Process this Mastra config to find and rename context.get() calls\n      const contextParamNames = new Set<string>();\n      stringLiteralCount += processNode(configArg, contextParamNames, context);\n    });\n\n  if (stringLiteralCount > 0) {\n    context.messages.push(\n      `Renamed ${stringLiteralCount} string literal 'runtimeContext' to 'requestContext' in Mastra server.middleware`,\n    );\n  }\n}\n\n/**\n * Recursively search for handler properties and rename context.get() calls\n */\nfunction processNode(node: any, contextParamNames: Set<string>, context: any): number {\n  if (!node || typeof node !== 'object') return 0;\n\n  let count = 0;\n\n  // Check if this is a handler property\n  if (isHandlerProperty(node)) {\n    const paramName = extractFirstParamName(node.value);\n    if (paramName) {\n      contextParamNames.add(paramName);\n    }\n  }\n\n  // Check if this is a context.get('runtimeContext') call\n  if (isContextGetCall(node, contextParamNames)) {\n    if (renameStringLiteralArg(node, context)) {\n      count++;\n    }\n  }\n\n  // Recursively process all object properties\n  for (const key in node) {\n    if (!shouldProcessKey(key, node)) continue;\n\n    const value = node[key];\n    if (Array.isArray(value)) {\n      value.forEach(item => {\n        count += processNode(item, contextParamNames, context);\n      });\n    } else if (value && typeof value === 'object') {\n      count += processNode(value, contextParamNames, context);\n    }\n  }\n\n  return count;\n}\n\n/**\n * Check if a node is a handler property (Property or ObjectProperty with key 'handler')\n */\nfunction isHandlerProperty(node: any): boolean {\n  return (node.type === 'Property' || node.type === 'ObjectProperty') && node.key?.name === 'handler';\n}\n\n/**\n * Extract the first parameter name from a function expression\n */\nfunction extractFirstParamName(handlerValue: any): string | null {\n  if (\n    !handlerValue ||\n    (handlerValue.type !== 'ArrowFunctionExpression' && handlerValue.type !== 'FunctionExpression')\n  ) {\n    return null;\n  }\n\n  if (!handlerValue.params || handlerValue.params.length === 0) {\n    return null;\n  }\n\n  const firstParam = handlerValue.params[0];\n  if (firstParam?.type === 'Identifier') {\n    return firstParam.name;\n  }\n\n  return null;\n}\n\n/**\n * Check if a node is a context.get() call expression\n */\nfunction isContextGetCall(node: any, contextParamNames: Set<string>): boolean {\n  if (node.type !== 'CallExpression') return false;\n\n  const callee = node.callee;\n  if (!callee || callee.type !== 'MemberExpression') return false;\n\n  const object = callee.object;\n  if (!object || object.type !== 'Identifier') return false;\n\n  if (!contextParamNames.has(object.name)) return false;\n\n  const property = callee.property;\n  if (!property || property.type !== 'Identifier' || property.name !== 'get') return false;\n\n  return true;\n}\n\n/**\n * Rename the first string argument from 'runtimeContext' to 'requestContext'\n */\nfunction renameStringLiteralArg(node: any, context: any): boolean {\n  const firstArg = node.arguments?.[0];\n  if (!firstArg) return false;\n\n  const isRuntimeContextLiteral =\n    (firstArg.type === 'StringLiteral' && firstArg.value === 'runtimeContext') ||\n    (firstArg.type === 'Literal' && firstArg.value === 'runtimeContext');\n\n  if (!isRuntimeContextLiteral) return false;\n\n  // Rename the value\n  firstArg.value = 'requestContext';\n\n  // Update the raw value if it exists\n  if (firstArg.extra?.raw) {\n    const quote = firstArg.extra.raw.charAt(0);\n    firstArg.extra.raw = `${quote}requestContext${quote}`;\n  }\n\n  context.hasChanges = true;\n  return true;\n}\n\n/**\n * Check if we should process this object key during recursion\n */\nfunction shouldProcessKey(key: string, node: any): boolean {\n  // Skip non-own properties\n  if (!node.hasOwnProperty(key)) return false;\n\n  // Skip metadata properties that don't contain code\n  if (key === 'loc' || key === 'comments') return false;\n\n  return true;\n}\n"],"mappings":";;;;;AAMA,IAAA,0BAAe,mBAAmB,UAAU,KAAK,SAAS,YAAY;CACpE,MAAM,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