Skip to content

Commit 502c4fa

Browse files
Juan Castañojuancastano
authored andcommitted
Clean up route file
1 parent 3b401bb commit 502c4fa

File tree

3 files changed

+629
-1169
lines changed

3 files changed

+629
-1169
lines changed

app/api/generate/route.ts

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// app/api/generate/route.ts
22
import { NextRequest, NextResponse } from 'next/server';
3-
import { processAppRequest } from '@/lib/openai';
3+
import { generateAppCode } from '@/lib/openai';
44
import { createSandbox } from '@/lib/e2b';
55
import { componentSchema } from '@/lib/schemas';
66
import { Benchify } from 'benchify';
@@ -22,35 +22,40 @@ const extendedComponentSchema = componentSchema.extend({
2222

2323
export async function POST(request: NextRequest) {
2424
try {
25-
const body = await request.json();
2625

27-
// Validate the request using extended schema
28-
const validationResult = extendedComponentSchema.safeParse(body);
26+
// Validate the request
27+
const validation = await validateRequest(request);
2928

30-
if (!validationResult.success) {
31-
return NextResponse.json(
32-
{ error: 'Invalid request format', details: validationResult.error.format() },
33-
{ status: 400 }
34-
);
29+
if (!validation.success) {
30+
return validation.error;
3531
}
3632

37-
const { description, existingFiles, editInstruction, useBuggyCode, useFixer } = validationResult.data;
33+
const { description, existingFiles, editInstruction, useBuggyCode, useFixer } = validation.data;
3834

3935
// Process the app request using centralized logic
40-
const filesToSandbox = await processAppRequest(description, existingFiles, editInstruction, useBuggyCode);
36+
const filesToSandbox = await generateAppCode(description, existingFiles, editInstruction, useBuggyCode);
4137

4238
let repairedFiles = filesToSandbox;
4339

4440
// Repair the generated code using Benchify's API if requested
4541
if (useFixer) {
4642
try {
43+
console.log("Trying fixer")
4744
const { data } = await benchify.fixer.run({
4845
files: filesToSandbox.map((file: { path: string; content: string }) => ({
4946
path: file.path,
5047
contents: file.content
51-
}))
48+
})),
49+
fixes: {
50+
css: false,
51+
imports: false,
52+
stringLiterals: true,
53+
tsSuggestions: false
54+
}
5255
});
5356

57+
console.log('🔧 Benchify fixer data:', data);
58+
5459
if (data) {
5560
const { success, diff } = data;
5661

@@ -90,4 +95,25 @@ export async function POST(request: NextRequest) {
9095
{ status: 500 }
9196
);
9297
}
93-
}
98+
}
99+
100+
101+
async function validateRequest(request: NextRequest) {
102+
const body = await request.json();
103+
const validationResult = extendedComponentSchema.safeParse(body);
104+
105+
if (!validationResult.success) {
106+
return {
107+
success: false as const,
108+
error: NextResponse.json(
109+
{ error: 'Invalid request format', details: validationResult.error.format() },
110+
{ status: 400 }
111+
)
112+
};
113+
}
114+
115+
return {
116+
success: true as const,
117+
data: validationResult.data
118+
};
119+
}

lib/openai.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const fileSchema = z.object({
1818
});
1919

2020
// Generate a new application using AI SDK
21-
export async function generateApp(
21+
export async function createNewApp(
2222
description: string,
2323
): Promise<Array<{ path: string; content: string }>> {
2424
console.log("Creating app with description: ", description);
@@ -108,7 +108,7 @@ export async function editApp(
108108
}
109109

110110
// Main function to handle both generation and editing
111-
export async function processAppRequest(
111+
export async function generateAppCode(
112112
description: string,
113113
existingFiles?: z.infer<typeof benchifyFileSchema>,
114114
editInstruction?: string,
@@ -147,7 +147,7 @@ export default App;`
147147
];
148148
} else {
149149
console.log('🤖 Calling AI to generate app...');
150-
return await generateApp(description);
150+
return await createNewApp(description);
151151
}
152152
}
153153
}

0 commit comments

Comments
 (0)