1
1
// app/api/generate/route.ts
2
2
import { NextRequest , NextResponse } from 'next/server' ;
3
- import { processAppRequest } from '@/lib/openai' ;
3
+ import { generateAppCode } from '@/lib/openai' ;
4
4
import { createSandbox } from '@/lib/e2b' ;
5
5
import { componentSchema } from '@/lib/schemas' ;
6
6
import { Benchify } from 'benchify' ;
@@ -22,35 +22,40 @@ const extendedComponentSchema = componentSchema.extend({
22
22
23
23
export async function POST ( request : NextRequest ) {
24
24
try {
25
- const body = await request . json ( ) ;
26
25
27
- // Validate the request using extended schema
28
- const validationResult = extendedComponentSchema . safeParse ( body ) ;
26
+ // Validate the request
27
+ const validation = await validateRequest ( request ) ;
29
28
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 ;
35
31
}
36
32
37
- const { description, existingFiles, editInstruction, useBuggyCode, useFixer } = validationResult . data ;
33
+ const { description, existingFiles, editInstruction, useBuggyCode, useFixer } = validation . data ;
38
34
39
35
// 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 ) ;
41
37
42
38
let repairedFiles = filesToSandbox ;
43
39
44
40
// Repair the generated code using Benchify's API if requested
45
41
if ( useFixer ) {
46
42
try {
43
+ console . log ( "Trying fixer" )
47
44
const { data } = await benchify . fixer . run ( {
48
45
files : filesToSandbox . map ( ( file : { path : string ; content : string } ) => ( {
49
46
path : file . path ,
50
47
contents : file . content
51
- } ) )
48
+ } ) ) ,
49
+ fixes : {
50
+ css : false ,
51
+ imports : false ,
52
+ stringLiterals : true ,
53
+ tsSuggestions : false
54
+ }
52
55
} ) ;
53
56
57
+ console . log ( '🔧 Benchify fixer data:' , data ) ;
58
+
54
59
if ( data ) {
55
60
const { success, diff } = data ;
56
61
@@ -90,4 +95,25 @@ export async function POST(request: NextRequest) {
90
95
{ status : 500 }
91
96
) ;
92
97
}
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
+ }
0 commit comments