1
1
// app/api/generate/route.ts
2
2
import { NextRequest , NextResponse } from 'next/server' ;
3
- import { generateApp , editApp } from '@/lib/openai' ;
3
+ import { processAppRequest } from '@/lib/openai' ;
4
4
import { createSandbox } from '@/lib/e2b' ;
5
5
import { componentSchema } from '@/lib/schemas' ;
6
6
import { Benchify } from 'benchify' ;
@@ -12,27 +12,6 @@ const benchify = new Benchify({
12
12
apiKey : process . env . BENCHIFY_API_KEY ,
13
13
} ) ;
14
14
15
- const buggyCode = [
16
- {
17
- path : "src/App.tsx" ,
18
- content : `import React from 'react';
19
-
20
- const App = () => {
21
- const message = "Hello World; // Missing closing quote
22
- const title = 'Welcome to my app';
23
-
24
- return (
25
- <div>
26
- <h1>{title}</h1>
27
- <p>{message}</p>
28
- </div>
29
- );
30
- };
31
-
32
- export default App;`
33
- }
34
- ] ;
35
-
36
15
// Extended schema to support editing
37
16
const extendedComponentSchema = componentSchema . extend ( {
38
17
existingFiles : benchifyFileSchema . optional ( ) ,
@@ -41,18 +20,6 @@ const extendedComponentSchema = componentSchema.extend({
41
20
useFixer : z . boolean ( ) . optional ( ) . default ( false ) ,
42
21
} ) ;
43
22
44
- // Helper function to merge updated files with existing files
45
- function mergeFiles ( existingFiles : z . infer < typeof benchifyFileSchema > , updatedFiles : z . infer < typeof benchifyFileSchema > ) : z . infer < typeof benchifyFileSchema > {
46
- const existingMap = new Map ( existingFiles . map ( file => [ file . path , file ] ) ) ;
47
-
48
- // Apply updates
49
- updatedFiles . forEach ( updatedFile => {
50
- existingMap . set ( updatedFile . path , updatedFile ) ;
51
- } ) ;
52
-
53
- return Array . from ( existingMap . values ( ) ) ;
54
- }
55
-
56
23
export async function POST ( request : NextRequest ) {
57
24
try {
58
25
console . log ( '🚀 API route started' ) ;
@@ -80,31 +47,8 @@ export async function POST(request: NextRequest) {
80
47
useFixer
81
48
} ) ;
82
49
83
- let filesToSandbox ;
84
-
85
- // Determine if this is an edit request or new generation
86
- if ( existingFiles && editInstruction ) {
87
- // Edit existing code (including error fixes)
88
- console . log ( '📝 Processing edit request...' ) ;
89
- console . log ( 'Existing files:' , existingFiles . map ( f => ( { path : f . path , contentLength : f . content . length } ) ) ) ;
90
-
91
- const updatedFiles = await editApp ( existingFiles , editInstruction ) ;
92
- console . log ( 'Updated files from AI:' , updatedFiles . map ( f => ( { path : f . path , contentLength : f . content . length } ) ) ) ;
93
-
94
- // Merge the updated files with the existing files
95
- filesToSandbox = mergeFiles ( existingFiles , updatedFiles ) ;
96
- console . log ( 'Final merged files:' , filesToSandbox . map ( f => ( { path : f . path , contentLength : f . content . length } ) ) ) ;
97
- } else {
98
- // Generate new app
99
- console . log ( '🆕 Processing new generation request...' ) ;
100
- if ( useBuggyCode ) {
101
- console . log ( '🐛 Using buggy code as requested' ) ;
102
- filesToSandbox = buggyCode ;
103
- } else {
104
- console . log ( '🤖 Calling AI to generate app...' ) ;
105
- filesToSandbox = await generateApp ( description ) ;
106
- }
107
- }
50
+ // Process the app request using centralized logic
51
+ const filesToSandbox = await processAppRequest ( description , existingFiles , editInstruction , useBuggyCode ) ;
108
52
109
53
console . log ( '📦 Files ready for sandbox:' , filesToSandbox . length ) ;
110
54
@@ -115,7 +59,7 @@ export async function POST(request: NextRequest) {
115
59
console . log ( '🔧 Running Benchify fixer...' ) ;
116
60
try {
117
61
const { data } = await benchify . fixer . run ( {
118
- files : filesToSandbox . map ( file => ( {
62
+ files : filesToSandbox . map ( ( file : { path : string ; content : string } ) => ( {
119
63
path : file . path ,
120
64
contents : file . content
121
65
} ) )
@@ -126,7 +70,7 @@ export async function POST(request: NextRequest) {
126
70
127
71
if ( success && diff ) {
128
72
console . log ( '✅ Fixer applied successfully' ) ;
129
- repairedFiles = filesToSandbox . map ( file => {
73
+ repairedFiles = filesToSandbox . map ( ( file : { path : string ; content : string } ) => {
130
74
const patchResult = applyPatch ( file . content , diff ) ;
131
75
return {
132
76
...file ,
0 commit comments