Skip to content

Commit 144f61f

Browse files
author
Juan Castaño
committed
Fixed a few errors
1 parent 05eae9b commit 144f61f

File tree

3 files changed

+52
-122
lines changed

3 files changed

+52
-122
lines changed

app/api/generate/route.ts

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const benchify = new Benchify({
1212
apiKey: process.env.BENCHIFY_API_KEY,
1313
});
1414

15-
const debug = true;
15+
const debug = false;
1616
const buggyCode = [
1717
{
1818
path: "src/App.tsx",
@@ -54,12 +54,14 @@ function mergeFiles(existingFiles: z.infer<typeof benchifyFileSchema>, updatedFi
5454

5555
export async function POST(request: NextRequest) {
5656
try {
57+
console.log('🚀 API route started');
5758
const body = await request.json();
5859

5960
// Validate the request using extended schema
6061
const validationResult = extendedComponentSchema.safeParse(body);
6162

6263
if (!validationResult.success) {
64+
console.log('❌ Validation failed:', validationResult.error.format());
6365
return NextResponse.json(
6466
{ error: 'Invalid request format', details: validationResult.error.format() },
6567
{ status: 400 }
@@ -68,7 +70,7 @@ export async function POST(request: NextRequest) {
6870

6971
const { description, existingFiles, editInstruction } = validationResult.data;
7072

71-
console.log('API Request:', {
73+
console.log('✅ Validation passed, API Request:', {
7274
isEdit: !!(existingFiles && editInstruction),
7375
filesCount: existingFiles?.length || 0,
7476
editInstruction: editInstruction || 'none',
@@ -80,7 +82,7 @@ export async function POST(request: NextRequest) {
8082
// Determine if this is an edit request or new generation
8183
if (existingFiles && editInstruction) {
8284
// Edit existing code (including error fixes)
83-
console.log('Processing edit request...');
85+
console.log('📝 Processing edit request...');
8486
console.log('Existing files:', existingFiles.map(f => ({ path: f.path, contentLength: f.content.length })));
8587

8688
const updatedFiles = await editApp(existingFiles, editInstruction);
@@ -91,38 +93,44 @@ export async function POST(request: NextRequest) {
9193
console.log('Final merged files:', filesToSandbox.map(f => ({ path: f.path, contentLength: f.content.length })));
9294
} else {
9395
// Generate new app
94-
console.log('Processing new generation request...');
96+
console.log('🆕 Processing new generation request...');
9597
if (debug) {
98+
console.log('🐛 Debug mode: using buggy code');
9699
filesToSandbox = buggyCode;
97100
} else {
101+
console.log('🤖 Calling AI to generate app...');
98102
filesToSandbox = await generateApp(description);
99103
}
100104
}
101105

106+
console.log('📦 Files ready for sandbox:', filesToSandbox.length);
107+
102108
// Repair the generated code using Benchify's API
103-
const { data } = await benchify.fixer.run({
104-
files: filesToSandbox.map(file => ({
105-
path: file.path,
106-
contents: file.content
107-
}))
108-
});
109+
// const { data } = await benchify.fixer.run({
110+
// files: filesToSandbox.map(file => ({
111+
// path: file.path,
112+
// contents: file.content
113+
// }))
114+
// });
109115

110116
let repairedFiles = filesToSandbox;
111-
if (data) {
112-
const { success, diff } = data;
113-
114-
if (success && diff) {
115-
repairedFiles = filesToSandbox.map(file => {
116-
const patchResult = applyPatch(file.content, diff);
117-
return {
118-
...file,
119-
content: typeof patchResult === 'string' ? patchResult : file.content
120-
};
121-
});
122-
}
123-
}
124-
117+
// if (data) {
118+
// const { success, diff } = data;
119+
120+
// if (success && diff) {
121+
// repairedFiles = filesToSandbox.map(file => {
122+
// const patchResult = applyPatch(file.content, diff);
123+
// return {
124+
// ...file,
125+
// content: typeof patchResult === 'string' ? patchResult : file.content
126+
// };
127+
// });
128+
// }
129+
// }
130+
131+
console.log('🏗️ Creating sandbox...');
125132
const sandboxResult = await createSandbox({ files: repairedFiles });
133+
console.log('✅ Sandbox created successfully');
126134

127135
// Return the results to the client
128136
return NextResponse.json({
@@ -135,7 +143,7 @@ export async function POST(request: NextRequest) {
135143
...(editInstruction && { editInstruction }),
136144
});
137145
} catch (error) {
138-
console.error('Error generating app:', error);
146+
console.error('💥 Error in API route:', error);
139147
return NextResponse.json(
140148
{
141149
error: 'Failed to generate app',

lib/e2b.ts

Lines changed: 5 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,17 @@ export async function createSandbox({ files }: { files: z.infer<typeof benchifyF
154154

155155
console.log('=== COMPILATION ERROR CHECK ===');
156156

157-
// Try multiple approaches to detect compilation errors
157+
// Simplified approach - just try one quick check
158158
try {
159-
// Approach 1: Try to build the project
160-
console.log('Trying npm run build...');
161-
const buildCheck = await sandbox.commands.run('cd /app && timeout 10s npm run build 2>&1 || true');
162-
console.log('Build check output:', buildCheck.stdout);
163-
console.log('Build check stderr:', buildCheck.stderr);
159+
console.log('Trying quick build check...');
160+
const buildCheck = await sandbox.commands.run('cd /app && timeout 5s npm run build 2>&1 || true');
161+
console.log('Build check output:', buildCheck.stdout?.substring(0, 500));
164162

165163
if (buildCheck.stdout && (
166164
buildCheck.stdout.includes('Unterminated string constant') ||
167165
buildCheck.stdout.includes('SyntaxError') ||
168166
buildCheck.stdout.includes('Unexpected token') ||
169-
buildCheck.stdout.includes('Parse error') ||
170-
buildCheck.stdout.includes('[plugin:vite:') ||
171-
buildCheck.stdout.includes('Transform failed') ||
172-
buildCheck.stdout.includes('Build failed')
167+
buildCheck.stdout.includes('[plugin:vite:')
173168
)) {
174169
hasCompilationError = true;
175170
compilationErrorOutput = buildCheck.stdout;
@@ -179,93 +174,9 @@ export async function createSandbox({ files }: { files: z.infer<typeof benchifyF
179174
console.log('Build check failed:', buildError);
180175
}
181176

182-
// Approach 2: Try to check TypeScript compilation
183-
if (!hasCompilationError) {
184-
try {
185-
console.log('Trying tsc --noEmit...');
186-
const tscCheck = await sandbox.commands.run('cd /app && timeout 10s npx tsc --noEmit 2>&1 || true');
187-
console.log('TypeScript check output:', tscCheck.stdout);
188-
console.log('TypeScript check stderr:', tscCheck.stderr);
189-
190-
if (tscCheck.stdout && (
191-
tscCheck.stdout.includes('error TS') ||
192-
tscCheck.stdout.includes('Unterminated string constant') ||
193-
tscCheck.stdout.includes('SyntaxError')
194-
)) {
195-
hasCompilationError = true;
196-
compilationErrorOutput = tscCheck.stdout;
197-
console.log('✅ Found compilation error in TypeScript check');
198-
}
199-
} catch (tscError) {
200-
console.log('TypeScript check failed:', tscError);
201-
}
202-
}
203-
204-
// Approach 3: Try to parse files directly with Babel/ESLint
205-
if (!hasCompilationError) {
206-
try {
207-
console.log('Trying to parse main files...');
208-
const parseCheck = await sandbox.commands.run('cd /app && timeout 10s npx babel src/App.tsx --presets=@babel/preset-typescript 2>&1 || true');
209-
console.log('Parse check output:', parseCheck.stdout);
210-
console.log('Parse check stderr:', parseCheck.stderr);
211-
212-
if (parseCheck.stderr && (
213-
parseCheck.stderr.includes('Unterminated string constant') ||
214-
parseCheck.stderr.includes('SyntaxError') ||
215-
parseCheck.stderr.includes('Unexpected token')
216-
)) {
217-
hasCompilationError = true;
218-
compilationErrorOutput = parseCheck.stderr;
219-
console.log('✅ Found compilation error in parse check');
220-
}
221-
} catch (parseError) {
222-
console.log('Parse check failed:', parseError);
223-
}
224-
}
225-
226-
// Approach 4: Check if the dev server is actually serving errors
227-
if (!hasCompilationError) {
228-
try {
229-
console.log('Checking dev server response for errors...');
230-
const responseCheck = await sandbox.commands.run('cd /app && timeout 5s curl -s http://localhost:5173 2>&1 || true');
231-
console.log('Response check output:', responseCheck.stdout);
232-
233-
if (responseCheck.stdout && (
234-
responseCheck.stdout.includes('SyntaxError') ||
235-
responseCheck.stdout.includes('Unterminated string') ||
236-
responseCheck.stdout.includes('Parse error') ||
237-
responseCheck.stdout.includes('Transform failed')
238-
)) {
239-
hasCompilationError = true;
240-
compilationErrorOutput = responseCheck.stdout;
241-
console.log('✅ Found compilation error in dev server response');
242-
}
243-
} catch (responseError) {
244-
console.log('Response check failed:', responseError);
245-
}
246-
}
247-
248-
// Approach 5: Check Vite logs more thoroughly
249-
if (!hasCompilationError) {
250-
try {
251-
console.log('Checking for Vite process logs...');
252-
const viteLogsCheck = await sandbox.commands.run('cd /app && ps aux | grep vite');
253-
console.log('Vite processes:', viteLogsCheck.stdout);
254-
255-
// Try to get logs from the running Vite process
256-
const viteLogCheck = await sandbox.commands.run('cd /app && timeout 3s strace -p $(pgrep -f "vite --host") 2>&1 | head -20 || true');
257-
console.log('Vite process trace:', viteLogCheck.stdout);
258-
} catch (viteError) {
259-
console.log('Vite log check failed:', viteError);
260-
}
261-
}
262-
263177
console.log('=== COMPILATION ERROR CHECK SUMMARY ===');
264178
console.log('Has compilation error:', hasCompilationError);
265179
console.log('Compilation error output length:', compilationErrorOutput.length);
266-
if (compilationErrorOutput) {
267-
console.log('Compilation error preview:', compilationErrorOutput.substring(0, 500));
268-
}
269180

270181
console.log('Dev server started, output checked');
271182
console.log('Total build errors found:', buildErrors.length);

lib/prompts.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,26 @@ IMPORTANT: Only generate these application files:
2626
- src/App.tsx (main app component)
2727
- src/index.css (with Tailwind imports)
2828
- src/components/* (your React components)
29-
- package.json (only for additional dependencies you need)
3029
31-
DO NOT generate configuration files like:
30+
The following are already provided in the template and should NOT be generated:
31+
- package.json (already includes react, react-dom, vite, tailwindcss, @tailwindcss/vite, typescript)
3232
- vite.config.ts
3333
- tsconfig files
3434
- eslint configs
3535
- index.html
3636
37-
These configuration files are already part of the template.
37+
Only generate a package.json if you need additional dependencies beyond:
38+
- react, react-dom (UI framework)
39+
- vite, @vitejs/plugin-react (build tool)
40+
- tailwindcss, @tailwindcss/vite (styling)
41+
- typescript (type checking)
42+
43+
If you do need additional packages, generate a minimal package.json with only:
44+
{
45+
"dependencies": {
46+
"package-name": "version"
47+
}
48+
}
3849
3950
RESPONSE FORMAT:
4051
You must return a valid JSON array of file objects. Each file object must have exactly this structure:

0 commit comments

Comments
 (0)