1
1
// app/api/generate/route.ts
2
2
import { NextRequest , NextResponse } from 'next/server' ;
3
3
import { generateApp } from '@/lib/openai' ;
4
- import { repairCode } from '@/lib/benchify' ;
5
4
import { createSandbox } from '@/lib/e2b' ;
6
5
import { componentSchema } from '@/lib/schemas' ;
7
- import { benchifyFileSchema } from '@/lib/schemas' ;
6
+ import { Benchify } from 'benchify' ;
7
+ import { applyPatch } from 'diff' ;
8
+
9
+ const benchify = new Benchify ( {
10
+ apiKey : process . env . BENCHIFY_API_KEY ,
11
+ } ) ;
8
12
9
13
export async function POST ( request : NextRequest ) {
10
14
try {
@@ -25,15 +29,30 @@ export async function POST(request: NextRequest) {
25
29
// Generate the Vue app using OpenAI
26
30
const generatedFiles = await generateApp ( description ) ;
27
31
28
- // Parse through schema before passing to repair
29
- const validatedFiles = benchifyFileSchema . parse ( generatedFiles ) ;
32
+ // Repair the generated code using Benchify's API
33
+ const { data } = await benchify . fixer . run ( {
34
+ files : generatedFiles . map ( file => ( {
35
+ path : file . path ,
36
+ contents : file . content
37
+ } ) )
38
+ } ) ;
30
39
31
- // // Repair the generated code using Benchify's API
32
- // const { repairedFiles, buildOutput } = await repairCode(validatedFiles);
40
+ let repairedFiles = generatedFiles ;
41
+ if ( data ) {
42
+ const { success, diff } = data ;
33
43
34
- const { sbxId, template, url, allFiles } = await createSandbox ( { files : generatedFiles } ) ;
44
+ if ( success && diff ) {
45
+ repairedFiles = generatedFiles . map ( file => {
46
+ const patchResult = applyPatch ( file . content , diff ) ;
47
+ return {
48
+ ...file ,
49
+ content : typeof patchResult === 'string' ? patchResult : file . content
50
+ } ;
51
+ } ) ;
52
+ }
53
+ }
35
54
36
- console . log ( "Preview URL: " , url ) ;
55
+ const { sbxId , template , url, allFiles } = await createSandbox ( { files : repairedFiles } ) ;
37
56
38
57
// Return the results to the client
39
58
return NextResponse . json ( {
0 commit comments