// ๐Ÿš€ Smart File Rebuilder Handler v20 - BRAIN UNIFIED // Enhanced with v20 Brain Integration, Structural Refactoring & Complex Component Logic // .vscode-extension/cipher-autonomous-dev/src/handlers/core/smartFileRebuilderHandler.ts import * as path from "path"; import * as vscode from "vscode"; import { RebuildRecommendation, RebuildStrategy, SupportedFileType, } from "../../shared/types"; import { calculateComplexity, ensureDirectoryExists, getBrainInterface, getFileType, } from "../../shared/utils"; /** * ๐Ÿ”ง v20 ENHANCED: Smart File Rebuilding with Brain Integration * Now includes structural refactoring, conditional hook fixes, and brain learning */ export async function smartFileRebuilderHandler(): Promise { console.log( "๐Ÿš€ [v20] SmartFileRebuilder BRAIN UNIFIED - Starting intelligent rebuild..." ); const editor = vscode.window.activeTextEditor; if (!editor) { vscode.window.showErrorMessage("No active file to rebuild"); return; } const fileName = editor.document.fileName; const baseName = path.basename(fileName); const content = editor.document.getText(); // โœ… v20 BRAIN INITIALIZATION await initializeBrainSystem(); try { // Check if file type is supported if (!isFileTypeSupported(fileName, "rebuild")) { const fileType = getFileTypeSupport(fileName); if (fileType && fileType.canAnalyze) { const action = await vscode.window.showWarningMessage( `๐Ÿ” ${baseName} can be analyzed but not auto-rebuilt yet. Would you like to analyze instead?`, "Analyze File", "Manual Review", "Cancel" ); if (action === "Analyze File") { await analyzeCurrentFileForRebuild(editor); await performBrainLearning("analyze-unsupported-type", true, { fileName: baseName, fileType: path.extname(fileName), action: "delegated-to-analysis", }); } else if (action === "Manual Review") { vscode.window.showInformationMessage( `๐Ÿ“ ${baseName} requires manual review. Consider using Claude for complex rebuilds.` ); await performBrainLearning("manual-review-required", true, { fileName: baseName, reason: "complex-file-type", }); } return; } else { vscode.window.showErrorMessage( `โŒ File type ${path.extname(fileName)} not supported for analysis or rebuilding yet.` ); await performBrainLearning("unsupported-file-type", false, { fileName: baseName, fileType: path.extname(fileName), }); return; } } // โœ… v20 ENHANCED: Assess complexity and structural issues const complexity = calculateComplexity(content); const structuralIssues = await assessStructuralIssues(content, fileName); console.log( `๐Ÿ” [v20] File complexity: ${complexity}/10, Structural issues: ${structuralIssues.length}` ); const strategy = await selectRebuildStrategy(editor, structuralIssues); if (!strategy) { await performBrainLearning("strategy-selection-cancelled", false, { fileName: baseName, }); return; } vscode.window.showInformationMessage( `๐Ÿ”„ Smart rebuilding ${baseName} with ${strategy.type} strategy...` ); let rebuildSuccess = false; let rebuildMetadata: any = {}; if (strategy.type === "smart") { rebuildMetadata = await smartRebuildFile( editor, strategy, structuralIssues ); rebuildSuccess = true; } else if (strategy.type === "full") { rebuildMetadata = await fullRebuildFile( editor, strategy, structuralIssues ); rebuildSuccess = true; } else { rebuildMetadata = await preservativeRebuildFile( editor, strategy, structuralIssues ); rebuildSuccess = true; } // โœ… v20 BRAIN LEARNING: Learn from rebuild success await performBrainLearning(`rebuild-${strategy.type}`, rebuildSuccess, { fileName: baseName, complexity, structuralIssues: structuralIssues.length, strategy: strategy.type, enhancedTypes: strategy.enhanceTypes, performanceOptimized: strategy.addPerformanceOptimizations, ...rebuildMetadata, }); vscode.window.showInformationMessage( `โœ… ${baseName} rebuilt successfully with ${strategy.type} strategy!` ); } catch (error) { console.error(`๐Ÿšจ [v20] Rebuild failed:`, error); vscode.window.showErrorMessage(`Rebuild failed: ${error}`); await performBrainLearning("rebuild-error", false, { fileName: baseName, error: error?.toString(), errorType: "unexpected-error", }); } } /** * ๐Ÿง  v20 BRAIN INITIALIZATION */ async function initializeBrainSystem(): Promise { console.log(`๐Ÿง  [v20] CRITICAL FIX: Calling initializeBrainSystem()...`); try { const { initializeBrainSystem } = await import("../../shared/utils"); const initialized = await initializeBrainSystem(); console.log( `๐Ÿง  [v20] Brain initialization result: ${initialized ? "โœ… SUCCESS" : "โŒ FAILED"}` ); } catch (initError) { console.log(`๐Ÿง  [v20] Brain initialization failed: ${initError}`); } } /** * ๐Ÿง  v20 BRAIN LEARNING */ async function performBrainLearning( action: string, success: boolean, metadata: any ): Promise { console.log( `๐Ÿš€ [v20] SmartFileRebuilder Brain Learning - Action: ${action}, Success: ${success}` ); try { const brainInterface = getBrainInterface(); if (brainInterface) { await brainInterface.learnFromAction( `smart-rebuild-${action}`, success ? "success" : "failure", { handler: "SmartFileRebuilder", timestamp: new Date().toISOString(), ...metadata, } ); console.log(`โœ… [v20] Brain learning SUCCESS for ${action}`); } else { console.log(`โš ๏ธ [v20] Brain interface not available for learning`); } } catch (error) { console.log(`๐Ÿšจ [v20] Brain learning failed: ${error}`); } } /** * ๐Ÿ” v20 ENHANCED: Assess Structural Issues * Detects conditional hooks, component architecture problems, etc. */ async function assessStructuralIssues( content: string, fileName: string ): Promise { const issues: StructuralIssue[] = []; const lines = content.split("\n"); const isReact = fileName.includes(".tsx") || fileName.includes(".jsx") || content.includes("React"); if (isReact) { // โœ… DETECT CONDITIONAL HOOKS (BadHooks.tsx case!) lines.forEach((line, index) => { if ( line.includes("if") && (line.includes("useState") || line.includes("useEffect")) ) { issues.push({ type: "conditional-hook", line: index + 1, description: "Hook called conditionally - violates Rules of Hooks", severity: "error", fixStrategy: "move-hooks-to-top-level", }); } }); // โœ… DETECT MISSING DEPENDENCY ARRAYS let inUseEffect = false; let useEffectStart = -1; lines.forEach((line, index) => { if (line.includes("useEffect")) { inUseEffect = true; useEffectStart = index; } if (inUseEffect && line.includes("});") && !line.includes(", [")) { issues.push({ type: "missing-dependency-array", line: index + 1, description: "useEffect missing dependency array", severity: "warning", fixStrategy: "add-dependency-array", }); inUseEffect = false; } }); // โœ… DETECT COMPONENT STRUCTURE ISSUES const hasMultipleReturns = (content.match(/return \(/g) || []).length > 1; if (hasMultipleReturns) { issues.push({ type: "multiple-returns", line: -1, description: "Component has multiple return statements", severity: "warning", fixStrategy: "consolidate-returns", }); } } console.log(`๐Ÿ” [v20] Structural assessment found ${issues.length} issues`); return issues; } interface StructuralIssue { type: string; line: number; description: string; severity: "error" | "warning"; fixStrategy: string; } /** * ๐ŸŽฏ v20 ENHANCED: Select Rebuild Strategy with Structural Awareness */ async function selectRebuildStrategy( editor: vscode.TextEditor, structuralIssues: StructuralIssue[] ): Promise { const fileName = path.basename(editor.document.fileName); const content = editor.document.getText(); const complexity = calculateComplexity(content); const fileType = getFileType(fileName); const recommendations: RebuildRecommendation[] = []; const hasStructuralIssues = structuralIssues.length > 0; const hasConditionalHooks = structuralIssues.some( (i) => i.type === "conditional-hook" ); // โœ… v20 ENHANCED: Recommendations based on structural issues if (hasConditionalHooks) { recommendations.push({ label: "๐Ÿš€ Smart Structural Rebuild (REQUIRED)", description: `Fix ${structuralIssues.length} structural issues including conditional hooks`, strategy: { type: "smart", preserveComments: true, enhanceTypes: true, addPerformanceOptimizations: true, includeTests: false, fixStructuralIssues: true, }, }); } else if (complexity <= 3 && !hasStructuralIssues) { recommendations.push({ label: "๐Ÿ”ง Smart Rebuild (Recommended)", description: "Intelligent enhancement with type safety and performance", strategy: { type: "smart", preserveComments: true, enhanceTypes: true, addPerformanceOptimizations: true, includeTests: false, fixStructuralIssues: false, }, }); } recommendations.push({ label: "๐Ÿš€ Full Architectural Rebuild", description: `Complete reconstruction with all modern features${hasStructuralIssues ? " + structural fixes" : ""}`, strategy: { type: "full", preserveComments: false, enhanceTypes: true, addPerformanceOptimizations: true, includeTests: true, fixStructuralIssues: true, }, }); if (!hasConditionalHooks) { recommendations.push({ label: "๐Ÿ›ก๏ธ Preservative Rebuild", description: "Careful enhancement while preserving existing structure", strategy: { type: "preserve", preserveComments: true, enhanceTypes: false, addPerformanceOptimizations: false, includeTests: false, fixStructuralIssues: false, }, }); } const issuesSummary = hasStructuralIssues ? ` | Issues: ${structuralIssues.length} (${structuralIssues.map((i) => i.type).join(", ")})` : ""; const selected = await vscode.window.showQuickPick(recommendations, { placeHolder: `Select rebuild strategy for ${fileName} (Complexity: ${complexity}/10${issuesSummary})`, }); return selected?.strategy; } /** * ๐Ÿง  v20 ENHANCED: Smart Rebuild with Structural Fixes */ async function smartRebuildFile( editor: vscode.TextEditor, strategy: RebuildStrategy, structuralIssues: StructuralIssue[] ): Promise { const fileName = editor.document.fileName; const fileType = getFileTypeSupport(fileName); const content = editor.document.getText(); const isReact = fileName.includes(".tsx") || fileName.includes(".jsx"); let rebuiltContent: string; let rebuildMetadata: any = { originalLines: content.split("\n").length, structuralIssuesFixed: 0, }; // โœ… v20 STRUCTURAL FIXES: Handle React components with conditional hooks if (isReact && structuralIssues.some((i) => i.type === "conditional-hook")) { console.log("๐Ÿ”ง [v20] Applying structural fixes for conditional hooks..."); rebuiltContent = await rebuildReactComponentWithStructuralFixes( fileName, content, strategy, structuralIssues ); rebuildMetadata.structuralIssuesFixed = structuralIssues.filter( (i) => i.type === "conditional-hook" ).length; rebuildMetadata.fixType = "structural-react-component"; } else { // โœ… Standard rebuild based on file type switch (fileType?.parser) { case "typescript": rebuiltContent = await rebuildTypeScriptFile( fileName, content, strategy ); rebuildMetadata.fixType = "typescript-enhancement"; break; case "javascript": rebuiltContent = await rebuildJavaScriptFile( fileName, content, strategy ); rebuildMetadata.fixType = "javascript-enhancement"; break; case "json": rebuiltContent = await rebuildJsonFile(fileName, content, strategy); rebuildMetadata.fixType = "json-enhancement"; break; case "markdown": rebuiltContent = await rebuildMarkdownFile(fileName, content, strategy); rebuildMetadata.fixType = "markdown-enhancement"; break; default: rebuiltContent = await rebuildGenericFile(fileName, content, strategy); rebuildMetadata.fixType = "generic-enhancement"; } } rebuildMetadata.finalLines = rebuiltContent.split("\n").length; rebuildMetadata.linesChanged = Math.abs( rebuildMetadata.finalLines - rebuildMetadata.originalLines ); await applyFileChanges(editor, rebuiltContent); // Generate companion files if requested if (strategy.includeTests) { await generateCompanionTestFile(fileName, content); rebuildMetadata.testsGenerated = true; } return rebuildMetadata; } /** * ๐Ÿš€ v20 NEW: React Component Structural Fixes (BadHooks.tsx solution!) */ async function rebuildReactComponentWithStructuralFixes( fileName: string, originalContent: string, strategy: RebuildStrategy, structuralIssues: StructuralIssue[] ): Promise { const componentName = path.basename(fileName).replace(/\.(tsx?|jsx?)$/, ""); const lines = originalContent.split("\n"); // โœ… EXTRACT HOOK DECLARATIONS FROM CONDITIONALS const extractedHooks: string[] = []; const conditionalHookIssues = structuralIssues.filter( (i) => i.type === "conditional-hook" ); conditionalHookIssues.forEach((issue) => { const line = lines[issue.line - 1]; if (line) { // Extract the hook declaration const hookMatch = line.match(/(const \[.*?\] = use\w+.*?;)/); if (hookMatch) { extractedHooks.push(hookMatch[1].trim()); } } }); console.log( `๐Ÿ”ง [v20] Extracted ${extractedHooks.length} hooks from conditionals` ); // โœ… GENERATE ENHANCED COMPONENT WITH PROPER HOOK PLACEMENT return generateStructurallyFixedReactComponent( componentName, originalContent, strategy, extractedHooks ); } /** * โš›๏ธ v20 ENHANCED: Generate Structurally Fixed React Component */ function generateStructurallyFixedReactComponent( componentName: string, originalContent: string, strategy: RebuildStrategy, extractedHooks: string[] ): string { const hasHooks = originalContent.includes("useState") || originalContent.includes("useEffect") || extractedHooks.length > 0; const hasProps = originalContent.includes("Props") || originalContent.includes("props"); const preserveComments = strategy.preserveComments; const typeDefinitions = strategy.enhanceTypes ? ` interface ${componentName}Props { ${hasProps ? "// Enhanced prop definitions with strict typing" : "// Add your props here"} className?: string; children?: React.ReactNode; } interface ${componentName}State { isLoaded: boolean; isActive: boolean; error?: string; }` : ""; const imports = strategy.addPerformanceOptimizations ? `import React, { memo, useCallback, useMemo, useRef, useLayoutEffect, useState, useEffect } from 'react';` : `import React, { useState, useEffect } from 'react';`; // โœ… v20 STRUCTURAL FIX: Hooks at top level (no conditionals!) const hooksAtTopLevel = extractedHooks.length > 0 ? ` // โœ… v20 STRUCTURAL FIX: Moved hooks to top level (Rules of Hooks compliance) ${extractedHooks.join("\n ")} ` : ""; const componentLogic = hasHooks ? ` ${hooksAtTopLevel} const [state, setState] = useState<${componentName}State>({ isLoaded: false, isActive: false }); const componentRef = useRef(null); ${ strategy.addPerformanceOptimizations ? ` // โœ… v20 PERFORMANCE: Memoized event handlers const handleAction = useCallback(() => { setState(prev => ({ ...prev, isActive: !prev.isActive })); }, []); const handleLoad = useCallback(() => { setState(prev => ({ ...prev, isLoaded: true })); }, []); // โœ… v20 PERFORMANCE: Memoized computed values const computedProps = useMemo(() => ({ status: state.isActive ? 'active' : 'inactive', className: \`\${className} \${state.isActive ? 'active' : ''}\`.trim() }), [state.isActive, className]);` : ` const handleAction = () => { setState(prev => ({ ...prev, isActive: !prev.isActive })); }; const handleLoad = () => { setState(prev => ({ ...prev, isLoaded: true })); };` } // โœ… v20 STRUCTURAL FIX: Proper useEffect with dependency array useEffect(() => { handleLoad(); }, [${strategy.addPerformanceOptimizations ? "handleLoad" : ""}]);` : ` const [isLoaded, setIsLoaded] = useState(false); const [isActive, setIsActive] = useState(false); useEffect(() => { setIsLoaded(true); }, []);`; return `// ๐Ÿš€ ${componentName} - v20 Structurally Enhanced by Cipher Smart Rebuild ${preserveComments ? `// Original implementation preserved and enhanced with structural fixes` : ""} ${imports} ${typeDefinitions} const ${componentName}: React.FC<${strategy.enhanceTypes ? `${componentName}Props` : "any"}> = (${ strategy.enhanceTypes ? `{ className = '', children, ...props }` : "props" }) => { ${componentLogic} ${ strategy.addPerformanceOptimizations && hasHooks ? ` // โœ… v20 PERFORMANCE: Development monitoring useLayoutEffect(() => { if (process.env.NODE_ENV === 'development') { console.log('๐Ÿš€ ${componentName} rendered with structural fixes'); } });` : "" } if (!${hasHooks ? "state.isLoaded" : "isLoaded"}) { return (
Loading ${componentName}...
); } return (

๐Ÿš€ ${componentName}

v20 Structurally Enhanced by Cipher Smart Rebuild

๐ŸŽฏ Structural Enhancements

  • โœ… Hooks moved to top-level (Rules of Hooks compliance)
  • โœ… Proper dependency arrays in useEffect
  • โœ… Modern React patterns with TypeScript
  • ${strategy.addPerformanceOptimizations ? "
  • โœ… Performance optimizations applied
  • " : ""}

๐Ÿ“Š Status

Status: {${strategy.addPerformanceOptimizations ? "computedProps.status" : hasHooks ? 'state.isActive ? "active" : "inactive"' : 'isActive ? "active" : "inactive"'}}
Loaded: โœ… Ready
Structurally Fixed: ๐Ÿš€ v20 Enhanced
${ strategy.addPerformanceOptimizations ? `
Optimized: โšก Performance Enhanced
` : "" }
{children && (
{children}
)}
๐Ÿš€ v20 Structurally Enhanced by Cipher Smart Rebuild | ${new Date().toLocaleDateString()} ${strategy.addPerformanceOptimizations ? " | โšก Performance Optimized" : ""}
); }; export default ${strategy.addPerformanceOptimizations ? `memo(${componentName})` : componentName}; ${ preserveComments ? ` // Original implementation preserved as reference: /* ${originalContent} */` : "" }`; } // โœ… Continue with existing methods but enhanced... /** * ๐Ÿ—๏ธ TypeScript File Rebuilding (Enhanced) */ async function rebuildTypeScriptFile( fileName: string, content: string, strategy: RebuildStrategy ): Promise { const baseName = path.basename(fileName).replace(/\.(tsx?|jsx?)$/, ""); const isReact = fileName.includes(".tsx") || fileName.includes(".jsx"); if (isReact) { return generateEnhancedReactComponent(baseName, content, strategy); } else { return generateEnhancedTypeScriptModule(baseName, content, strategy); } } /** * โš›๏ธ Enhanced React Component Generation (Existing but enhanced) */ function generateEnhancedReactComponent( componentName: string, originalContent: string, strategy: RebuildStrategy ): string { const hasHooks = originalContent.includes("useState") || originalContent.includes("useEffect"); const hasProps = originalContent.includes("Props") || originalContent.includes("props"); const preserveComments = strategy.preserveComments; const typeDefinitions = strategy.enhanceTypes ? ` interface ${componentName}Props { ${hasProps ? "// Enhanced prop definitions with strict typing" : "// Add your props here"} className?: string; children?: React.ReactNode; } interface ${componentName}State { isLoaded: boolean; isActive: boolean; error?: string; }` : ""; const performanceOptimizations = strategy.addPerformanceOptimizations ? ` import React, { memo, useCallback, useMemo, useRef, useLayoutEffect, useState, useEffect } from 'react';` : ` import React, { useState, useEffect } from 'react';`; const componentLogic = hasHooks ? ` const [state, setState] = useState<${componentName}State>({ isLoaded: false, isActive: false }); const componentRef = useRef(null); ${ strategy.addPerformanceOptimizations ? ` // Memoized event handlers for performance const handleAction = useCallback(() => { setState(prev => ({ ...prev, isActive: !prev.isActive })); }, []); const handleLoad = useCallback(() => { setState(prev => ({ ...prev, isLoaded: true })); }, []); // Memoized computed values const computedProps = useMemo(() => ({ status: state.isActive ? 'active' : 'inactive', className: \`\${className} \${state.isActive ? 'active' : ''}\`.trim() }), [state.isActive, className]);` : ` const handleAction = () => { setState(prev => ({ ...prev, isActive: !prev.isActive })); }; const handleLoad = () => { setState(prev => ({ ...prev, isLoaded: true })); };` } useEffect(() => { handleLoad(); }, [${strategy.addPerformanceOptimizations ? "handleLoad" : ""}]);` : ` const [isLoaded, setIsLoaded] = useState(false); const [isActive, setIsActive] = useState(false); useEffect(() => { setIsLoaded(true); }, []);`; return `// ๐ŸŽต ${componentName} - Enhanced by Cipher Smart Rebuild ${preserveComments ? `// Original implementation preserved and enhanced` : ""} ${performanceOptimizations} ${typeDefinitions} const ${componentName}: React.FC<${strategy.enhanceTypes ? `${componentName}Props` : "any"}> = (${ strategy.enhanceTypes ? `{ className = '', children, ...props }` : "props" }) => { ${componentLogic} ${ strategy.addPerformanceOptimizations && hasHooks ? ` // Performance monitoring useLayoutEffect(() => { if (process.env.NODE_ENV === 'development') { console.log('๐ŸŽต ${componentName} rendered'); } });` : "" } if (!${hasHooks ? "state.isLoaded" : "isLoaded"}) { return (
Loading ${componentName}...
); } return (

๐ŸŽต ${componentName}

Enhanced by Cipher Smart Rebuild

๐ŸŽฏ Enhanced Features

Smart rebuilding with modern patterns and optimizations

๐Ÿ“Š Status

Status: {${strategy.addPerformanceOptimizations ? "computedProps.status" : hasHooks ? 'state.isActive ? "active" : "inactive"' : 'isActive ? "active" : "inactive"'}}
Loaded: โœ… Ready
${ strategy.addPerformanceOptimizations ? `
Optimized: โšก Enhanced
` : "" }
{children && (
{children}
)}
๐Ÿ”ง Enhanced by Cipher Smart Rebuild | ${new Date().toLocaleDateString()} ${strategy.addPerformanceOptimizations ? " | โšก Performance Optimized" : ""}
); }; export default ${strategy.addPerformanceOptimizations ? `memo(${componentName})` : componentName}; ${ preserveComments ? ` // Original implementation preserved as reference: /* ${originalContent} */` : "" }`; } // โœ… Continue with all existing methods (rebuildJavaScriptFile, rebuildJsonFile, etc.) // ... [Rest of existing methods with same enhancements] /** * ๐Ÿ”„ Full Rebuild Implementation (Enhanced) */ async function fullRebuildFile( editor: vscode.TextEditor, strategy: RebuildStrategy, structuralIssues: StructuralIssue[] ): Promise { const fileName = path.basename(editor.document.fileName); const originalContent = editor.document.getText(); // โœ… v20 ENHANCEMENT: Handle structural issues in full rebuild let newContent: string; if (structuralIssues.length > 0) { newContent = await generateStructurallyFixedReactComponent( fileName.replace(/\.(tsx?|jsx?)$/, ""), originalContent, strategy, [] ); } else { newContent = await generateEnhancedFileContent( fileName, originalContent, strategy ); } await applyFileChanges(editor, newContent); await createBackupFile(editor, originalContent); return { originalLines: originalContent.split("\n").length, finalLines: newContent.split("\n").length, structuralIssuesFixed: structuralIssues.length, backupCreated: true, fixType: "full-rebuild", }; } /** * ๐Ÿ›ก๏ธ Preservative Rebuild Implementation (Enhanced) */ async function preservativeRebuildFile( editor: vscode.TextEditor, strategy: RebuildStrategy, structuralIssues: StructuralIssue[] ): Promise { const content = editor.document.getText(); const enhancedContent = await enhanceExistingFile(content, strategy); await applyFileChanges(editor, enhancedContent); return { originalLines: content.split("\n").length, finalLines: enhancedContent.split("\n").length, preservativeEnhancement: true, fixType: "preservative-rebuild", }; } // โœ… All existing utility functions remain the same... // Support utilities, file type checks, etc. const SUPPORTED_FILE_TYPES: SupportedFileType[] = [ { extension: ".ts", canAnalyze: true, canRebuild: true, parser: "typescript", priority: 1, }, { extension: ".tsx", canAnalyze: true, canRebuild: true, parser: "typescript", priority: 1, }, { extension: ".js", canAnalyze: true, canRebuild: true, parser: "javascript", priority: 1, }, { extension: ".jsx", canAnalyze: true, canRebuild: true, parser: "javascript", priority: 1, }, { extension: ".json", canAnalyze: true, canRebuild: true, parser: "json", priority: 1, }, { extension: ".md", canAnalyze: true, canRebuild: true, parser: "markdown", priority: 1, }, { extension: ".html", canAnalyze: true, canRebuild: true, parser: "generic", priority: 1, }, { extension: ".css", canAnalyze: true, canRebuild: true, parser: "generic", priority: 1, }, { extension: ".scss", canAnalyze: true, canRebuild: true, parser: "generic", priority: 1, }, ]; function getFileTypeSupport(fileName: string): SupportedFileType | null { const extension = path.extname(fileName).toLowerCase(); return ( SUPPORTED_FILE_TYPES.find((type) => type.extension === extension) || null ); } function isFileTypeSupported( fileName: string, operation: "analyze" | "rebuild" ): boolean { const fileType = getFileTypeSupport(fileName); if (!fileType) return false; return operation === "analyze" ? fileType.canAnalyze : fileType.canRebuild; } async function applyFileChanges( editor: vscode.TextEditor, newContent: string ): Promise { const edit = new vscode.WorkspaceEdit(); const fullRange = new vscode.Range( 0, 0, editor.document.lineCount, editor.document.lineAt(editor.document.lineCount - 1).text.length ); edit.replace(editor.document.uri, fullRange, newContent); await vscode.workspace.applyEdit(edit); } async function analyzeCurrentFileForRebuild( editor: vscode.TextEditor ): Promise { // Import and call the analyze current file handler const { analyzeCurrentFileHandler } = await import( "../core/analyzeCurrentFileHandler" ); await analyzeCurrentFileHandler(); } async function generateEnhancedFileContent( fileName: string, originalContent: string, strategy: RebuildStrategy ): Promise { const componentName = fileName.replace(/\.(tsx?|jsx?)$/, ""); return generateEnhancedReactComponent( componentName, originalContent, strategy ); } async function createBackupFile( editor: vscode.TextEditor, content: string ): Promise { const workspaceFolder = vscode.workspace.workspaceFolders?.[0]; if (!workspaceFolder) return; const backupDir = vscode.Uri.joinPath(workspaceFolder.uri, "cipher-backups"); await ensureDirectoryExists(backupDir); const fileName = path.basename(editor.document.fileName); const backupFile = vscode.Uri.joinPath( backupDir, `${fileName}.backup-${Date.now()}` ); await vscode.workspace.fs.writeFile(backupFile, Buffer.from(content)); } async function enhanceExistingFile( content: string, strategy: RebuildStrategy ): Promise { let enhanced = content; if ( strategy.enhanceTypes && !content.includes("interface") && content.includes("React") ) { enhanced = enhanced.replace( /function (\w+)\(/, "interface $1Props {\n // Enhanced props\n}\n\nfunction $1(" ); } if ( strategy.addPerformanceOptimizations && content.includes("React") && !content.includes("memo") ) { enhanced = enhanced.replace( /export default (\w+)/, "export default React.memo($1)" ); } return enhanced; } async function generateCompanionTestFile( fileName: string, content: string ): Promise { const workspaceFolder = vscode.workspace.workspaceFolders?.[0]; if (!workspaceFolder) return; const baseName = path.basename(fileName, path.extname(fileName)); const testDir = vscode.Uri.joinPath(workspaceFolder.uri, "tests"); await ensureDirectoryExists(testDir); const testContent = `// ๐Ÿงช ${baseName} Tests - Generated by Cipher Smart Rebuild v20 import { render, screen, fireEvent } from '@testing-library/react'; import '@testing-library/jest-dom'; import ${baseName} from '../${fileName}'; describe('${baseName}', () => { test('renders without crashing', () => { render(<${baseName} />); expect(screen.getByText('${baseName}')).toBeInTheDocument(); }); test('handles user interactions', () => { render(<${baseName} />); const button = screen.getByRole('button'); fireEvent.click(button); // Add specific assertions }); test('structural fixes applied correctly', () => { render(<${baseName} />); // Test that hooks are properly structured expect(screen.getByText(/Structurally Enhanced/)).toBeInTheDocument(); }); }); // Generated by Cipher Smart Rebuild v20 with Brain Integration`; const testFile = vscode.Uri.joinPath(testDir, `${baseName}.test.tsx`); await vscode.workspace.fs.writeFile(testFile, Buffer.from(testContent)); } // โœ… Enhanced RebuildStrategy interface for structural fixes declare module "../../shared/types" { interface RebuildStrategy { type: | "refactor" | "rewrite" | "optimize" | "enhance" | "smart" | "full" | "preserve" | "songsterr" | "audio"; preserveComments: boolean; enhanceTypes: boolean; addPerformanceOptimizations: boolean; includeTests: boolean; fixStructuralIssues?: boolean; // โœ… v20 NEW } } // โœ… Additional file type implementations (JavaScript, JSON, Markdown, Generic) async function rebuildJavaScriptFile( fileName: string, content: string, strategy: RebuildStrategy ): Promise { const baseName = path.basename(fileName).replace(/\.(js|jsx)$/, ""); const isReact = fileName.includes(".jsx") || content.includes("React") || content.includes("jsx"); if (isReact) { return generateEnhancedJavaScriptReactComponent( baseName, content, strategy ); } else { return generateEnhancedJavaScriptModule(baseName, content, strategy); } } function generateEnhancedJavaScriptReactComponent( componentName: string, originalContent: string, strategy: RebuildStrategy ): string { return `// ๐ŸŽต ${componentName} - Enhanced by Cipher Smart Rebuild v20 import React, { useState, useEffect${strategy.addPerformanceOptimizations ? ", useCallback, memo" : ""} } from 'react'; const ${componentName} = (${strategy.enhanceTypes ? '{ className = "", ...props }' : "props"}) => { const [isLoaded, setIsLoaded] = useState(false); const [isActive, setIsActive] = useState(false); useEffect(() => { setIsLoaded(true); }, []); ${ strategy.addPerformanceOptimizations ? ` const handleAction = useCallback(() => { setIsActive(prev => !prev); }, []);` : ` const handleAction = () => { setIsActive(prev => !prev); };` } if (!isLoaded) { return (
Loading ${componentName}...
); } return (

๐ŸŽต ${componentName}

Enhanced by Cipher Smart Rebuild v20

); }; export default ${strategy.addPerformanceOptimizations ? `memo(${componentName})` : componentName}; ${ strategy.preserveComments ? ` // Original content preserved: /* ${originalContent} */` : "" }`; } function generateEnhancedJavaScriptModule( fileName: string, originalContent: string, strategy: RebuildStrategy ): string { return `// ๐ŸŽต ${fileName} - Enhanced by Cipher Smart Rebuild v20 // Generated: ${new Date().toISOString()} ${ strategy.addPerformanceOptimizations ? ` // Performance optimizations const performanceCache = new Map(); function optimizedFunction(fn) { return function(...args) { const key = JSON.stringify(args); if (performanceCache.has(key)) { return performanceCache.get(key); } const result = fn.apply(this, args); performanceCache.set(key, result); return result; }; }` : "" } ${originalContent} // Enhanced exports module.exports = { ...module.exports, enhanced: true, rebuiltBy: 'Cipher Smart Rebuild v20', rebuiltAt: '${new Date().toISOString()}' };`; } async function rebuildJsonFile( fileName: string, content: string, strategy: RebuildStrategy ): Promise { try { const parsed = JSON.parse(content); const enhanced = { ...parsed, _cipher: { enhanced: true, rebuiltBy: "Cipher Smart Rebuild v20", rebuiltAt: new Date().toISOString(), strategy: strategy.type, preservedOriginal: strategy.preserveComments, brainIntegrated: true, }, }; return JSON.stringify(enhanced, null, 2); } catch (error) { return `{ "name": "rebuilt-${path.basename(fileName, ".json")}", "version": "1.0.0", "description": "Rebuilt by Cipher Smart Rebuild v20", "_cipher": { "originalContentHadErrors": true, "rebuiltBy": "Cipher Smart Rebuild v20", "rebuiltAt": "${new Date().toISOString()}", "brainIntegrated": true } }`; } } async function rebuildMarkdownFile( fileName: string, content: string, strategy: RebuildStrategy ): Promise { const title = path.basename(fileName, ".md"); return `# ${title.charAt(0).toUpperCase() + title.slice(1)} > **Enhanced by Cipher Smart Rebuild v20 with Brain Integration** | ${new Date().toLocaleDateString()} ## Overview ${content.includes("##") ? "Original content enhanced and restructured:" : "Generated from original content:"} ${strategy.preserveComments ? content : content.replace(//g, "")} ## v20 Enhanced Features - โœ… Improved structure and formatting - ๐ŸŽต Cipher Smart Rebuild v20 optimization - ๐Ÿง  Brain integration for continuous learning - ๐Ÿ“– Better readability ${strategy.enhanceTypes ? "- ๐Ÿท๏ธ Enhanced type definitions" : ""} ${strategy.addPerformanceOptimizations ? "- โšก Performance considerations" : ""} ## Rebuild Information - **Strategy:** ${strategy.type} - **Enhanced Types:** ${strategy.enhanceTypes ? "Yes" : "No"} - **Performance Optimized:** ${strategy.addPerformanceOptimizations ? "Yes" : "No"} - **Comments Preserved:** ${strategy.preserveComments ? "Yes" : "No"} - **Brain Integrated:** Yes (v20) --- *Rebuilt by Cipher Smart Rebuild v20 with Brain Integration* *Generated: ${new Date().toISOString()}*`; } async function rebuildGenericFile( fileName: string, content: string, strategy: RebuildStrategy ): Promise { const baseName = path.basename(fileName); const extension = path.extname(fileName); return `/* ๐ŸŽต ${baseName} - Enhanced by Cipher Smart Rebuild v20 * Generated: ${new Date().toISOString()} * File type: ${extension} * Strategy: ${strategy.type} * Brain Integrated: Yes */ ${content} /* Enhanced with Cipher Smart Rebuild v20 * Strategy: ${strategy.type} * Enhanced Types: ${strategy.enhanceTypes} * Performance Optimized: ${strategy.addPerformanceOptimizations} * Comments Preserved: ${strategy.preserveComments} * Brain Learning: Active */`; } function generateEnhancedTypeScriptModule( moduleName: string, originalContent: string, strategy: RebuildStrategy ): string { const hasExports = originalContent.includes("export"); const hasInterfaces = originalContent.includes("interface"); return `// ๐ŸŽต ${moduleName} - Enhanced by Cipher Smart Rebuild v20 // Generated: ${new Date().toISOString()} ${ strategy.enhanceTypes && !hasInterfaces ? ` interface ${moduleName}Config { enabled: boolean; options: Record; } interface ${moduleName}Result { success: boolean; data?: any; error?: string; }` : "" } ${ strategy.addPerformanceOptimizations ? ` // Performance-optimized implementation const cache = new Map(); function memoize any>(fn: T): T { return ((...args: any[]) => { const key = JSON.stringify(args); if (cache.has(key)) { return cache.get(key); } const result = fn(...args); cache.set(key, result); return result; }) as T; }` : "" } ${originalContent} ${ strategy.enhanceTypes && hasExports ? ` // Enhanced exports with better typing export type { ${moduleName}Config, ${moduleName}Result };` : "" } ${ strategy.addPerformanceOptimizations ? ` // Performance utilities export { memoize };` : "" } // Enhanced with Cipher Smart Rebuild v20 export default { enhanced: true, rebuiltBy: 'Cipher Smart Rebuild v20', rebuiltAt: '${new Date().toISOString()}', brainIntegrated: true, ${strategy.addPerformanceOptimizations ? "optimized: true," : ""} ${strategy.enhanceTypes ? "typeSafe: true," : ""} };`; }