${mainComponentName ? `<${mainComponentName} />` : '
\n
Welcome to your React App
\n
Your components have been created but need to be added here.
\n
'}
{/* Generated components: ${componentFiles.map(f => f.path).join(', ')} */}
);
}
export default App;`;
try {
// Use provider pattern if available
if (sandbox.writeFile) {
await sandbox.writeFile('src/App.jsx', appContent);
} else if (sandbox.writeFiles) {
await sandbox.writeFiles([{
path: 'src/App.jsx',
content: Buffer.from(appContent)
}]);
}
console.log('Auto-generated: src/App.jsx');
results.filesCreated.push('src/App.jsx (auto-generated)');
} catch (error) {
results.errors.push(`Failed to create App.jsx: ${(error as Error).message}`);
}
// Don't auto-generate App.css - we're using Tailwind CSS
// Only create index.css if it doesn't exist
const indexCssInParsed = parsed.files.some(f => {
const normalized = f.path.replace(/^\//, '').replace(/^src\//, '');
return normalized === 'index.css' || f.path === 'src/index.css';
});
const indexCssExists = global.existingFiles.has('src/index.css') ||
global.existingFiles.has('index.css');
if (!isEdit && !indexCssInParsed && !indexCssExists) {
try {
const indexCssContent = `@tailwind base;
@tailwind components;
@tailwind utilities;
:root {
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;
color-scheme: dark;
color: rgba(255, 255, 255, 0.87);
background-color: #0a0a0a;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-width: 320px;
min-height: 100vh;
}`;
// Use provider pattern if available
if (sandbox.writeFile) {
await sandbox.writeFile('src/index.css', indexCssContent);
} else if (sandbox.writeFiles) {
await sandbox.writeFiles([{
path: 'src/index.css',
content: Buffer.from(indexCssContent)
}]);
}
console.log('Auto-generated: src/index.css');
results.filesCreated.push('src/index.css (with Tailwind)');
} catch (error) {
console.error('Failed to create index.css:', error);
results.errors.push('Failed to create index.css with Tailwind');
}
}
}
// Execute commands
for (const cmd of parsed.commands) {
try {
// Parse command and arguments
const commandParts = cmd.trim().split(/\s+/);
const cmdName = commandParts[0];
const args = commandParts.slice(1);
// Execute command using sandbox
let result;
if (sandbox.runCommand && typeof sandbox.runCommand === 'function') {
// Check if this is a provider pattern sandbox
const testResult = await sandbox.runCommand(cmd);
if (testResult && typeof testResult === 'object' && 'stdout' in testResult) {
// Provider returns CommandResult directly
result = testResult;
} else {
// Direct sandbox - expects object with cmd and args
result = await sandbox.runCommand({
cmd: cmdName,
args
});
}
}
console.log(`Executed: ${cmd}`);
// Handle result based on type
let stdout = '';
let stderr = '';
if (result) {
if (typeof result.stdout === 'string') {
stdout = result.stdout;
stderr = result.stderr || '';
} else if (typeof result.stdout === 'function') {
stdout = await result.stdout();
stderr = await result.stderr();
}
}
if (stdout) console.log(stdout);
if (stderr) console.log(`Errors: ${stderr}`);
results.commandsExecuted.push(cmd);
} catch (error) {
results.errors.push(`Failed to execute ${cmd}: ${(error as Error).message}`);
}
}
// Check for missing imports in App.jsx
const missingImports: string[] = [];
const appFile = parsed.files.find(f =>
f.path === 'src/App.jsx' || f.path === 'App.jsx'
);
if (appFile) {
// Extract imports from App.jsx
const importRegex = /import\s+(?:\w+|\{[^}]+\})\s+from\s+['"]([^'"]+)['"]/g;
let match;
const imports: string[] = [];
while ((match = importRegex.exec(appFile.content)) !== null) {
const importPath = match[1];
if (importPath.startsWith('./') || importPath.startsWith('../')) {
imports.push(importPath);
}
}
// Check if all imported files exist
for (const imp of imports) {
// Skip CSS imports for this check
if (imp.endsWith('.css')) continue;
// Convert import path to expected file paths
const basePath = imp.replace('./', 'src/');
const possiblePaths = [
basePath + '.jsx',
basePath + '.js',
basePath + '/index.jsx',
basePath + '/index.js'
];
const fileExists = parsed.files.some(f =>
possiblePaths.some(path => f.path === path)
);
if (!fileExists) {
missingImports.push(imp);
}
}
}
// Prepare response
const responseData: any = {
success: true,
results,
explanation: parsed.explanation,
structure: parsed.structure,
message: `Applied ${results.filesCreated.length} files successfully`
};
// Handle missing imports automatically
if (missingImports.length > 0) {
console.warn('[apply-ai-code] Missing imports detected:', missingImports);
// Automatically generate missing components
try {
console.log('[apply-ai-code] Auto-generating missing components...');
const autoCompleteResponse = await fetch(
`${request.nextUrl.origin}/api/auto-complete-components`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
missingImports,
model: 'claude-sonnet-4-20250514'
})
}
);
const autoCompleteData = await autoCompleteResponse.json();
if (autoCompleteData.success) {
responseData.autoCompleted = true;
responseData.autoCompletedComponents = autoCompleteData.components;
responseData.message = `Applied ${results.filesCreated.length} files + auto-generated ${autoCompleteData.files} missing components`;
// Add auto-completed files to results
results.filesCreated.push(...autoCompleteData.components);
} else {
// If auto-complete fails, still warn the user
responseData.warning = `Missing ${missingImports.length} imported components: ${missingImports.join(', ')}`;
responseData.missingImports = missingImports;
}
} catch (error) {
console.error('[apply-ai-code] Auto-complete failed:', error);
responseData.warning = `Missing ${missingImports.length} imported components: ${missingImports.join(', ')}`;
responseData.missingImports = missingImports;
}
}
// Track applied files in conversation state
if (global.conversationState && results.filesCreated.length > 0) {
// Update the last message metadata with edited files
const messages = global.conversationState.context.messages;
if (messages.length > 0) {
const lastMessage = messages[messages.length - 1];
if (lastMessage.role === 'user') {
lastMessage.metadata = {
...lastMessage.metadata,
editedFiles: results.filesCreated
};
}
}
// Track applied code in project evolution
if (global.conversationState.context.projectEvolution) {
global.conversationState.context.projectEvolution.majorChanges.push({
timestamp: Date.now(),
description: parsed.explanation || 'Code applied',
filesAffected: results.filesCreated
});
}
// Update last updated timestamp
global.conversationState.lastUpdated = Date.now();
console.log('[apply-ai-code] Updated conversation state with applied files:', results.filesCreated);
}
return NextResponse.json(responseData);
} catch (error) {
console.error('Apply AI code error:', error);
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to parse AI code' },
{ status: 500 }
);
}
}
================================================
FILE: app/api/apply-ai-code-stream/route.ts
================================================
import { NextRequest, NextResponse } from 'next/server';
import { parseMorphEdits, applyMorphEditToFile } from '@/lib/morph-fast-apply';
// Sandbox import not needed - using global sandbox from sandbox-manager
import type { SandboxState } from '@/types/sandbox';
import type { ConversationState } from '@/types/conversation';
import { sandboxManager } from '@/lib/sandbox/sandbox-manager';
declare global {
var conversationState: ConversationState | null;
var activeSandboxProvider: any;
var existingFiles: Set