1
1
'use client' ;
2
2
3
- import { AlertCircle , Code , FileX , Terminal } from 'lucide-react' ;
3
+ import { useState } from 'react' ;
4
+ import { AlertCircle , Code , FileX , Terminal , Wand2 } from 'lucide-react' ;
4
5
import { Alert , AlertDescription , AlertTitle } from '@/components/ui/alert' ;
5
6
import { Badge } from '@/components/ui/badge' ;
7
+ import { Button } from '@/components/ui/button' ;
6
8
import { ScrollArea } from '@/components/ui/scroll-area' ;
9
+ import { benchifyFileSchema } from '@/lib/schemas' ;
10
+ import { z } from 'zod' ;
7
11
8
12
interface BuildError {
9
13
type : 'typescript' | 'build' | 'runtime' ;
@@ -15,9 +19,13 @@ interface BuildError {
15
19
16
20
interface ErrorDisplayProps {
17
21
errors : BuildError [ ] ;
22
+ currentFiles ?: z . infer < typeof benchifyFileSchema > ;
23
+ onFixComplete ?: ( result : any ) => void ;
18
24
}
19
25
20
- export function ErrorDisplay ( { errors } : ErrorDisplayProps ) {
26
+ export function ErrorDisplay ( { errors, currentFiles, onFixComplete } : ErrorDisplayProps ) {
27
+ const [ isFixing , setIsFixing ] = useState ( false ) ;
28
+
21
29
const getErrorIcon = ( type : BuildError [ 'type' ] ) => {
22
30
switch ( type ) {
23
31
case 'typescript' :
@@ -52,15 +60,94 @@ export function ErrorDisplay({ errors }: ErrorDisplayProps) {
52
60
return acc ;
53
61
} , { } as Record < string , BuildError [ ] > ) ;
54
62
63
+ const handleFixWithAI = async ( ) => {
64
+ if ( ! currentFiles || errors . length === 0 || isFixing ) return ;
65
+
66
+ setIsFixing ( true ) ;
67
+
68
+ try {
69
+ // Format errors into an edit instruction
70
+ const errorDetails = errors . map ( error => {
71
+ let errorInfo = `${ error . type . toUpperCase ( ) } ERROR: ${ error . message } ` ;
72
+ if ( error . file ) {
73
+ errorInfo += ` (in ${ error . file } ` ;
74
+ if ( error . line ) errorInfo += ` at line ${ error . line } ` ;
75
+ if ( error . column ) errorInfo += `, column ${ error . column } ` ;
76
+ errorInfo += ')' ;
77
+ }
78
+ return errorInfo ;
79
+ } ) . join ( '\n\n' ) ;
80
+
81
+ const fixInstruction = `Fix the following build errors:
82
+
83
+ ${ errorDetails }
84
+
85
+ Please make the minimal changes necessary to resolve these errors while maintaining existing functionality.` ;
86
+
87
+ // Use the existing edit API
88
+ const response = await fetch ( '/api/generate' , {
89
+ method : 'POST' ,
90
+ headers : {
91
+ 'Content-Type' : 'application/json' ,
92
+ } ,
93
+ body : JSON . stringify ( {
94
+ type : 'component' ,
95
+ description : '' ,
96
+ existingFiles : currentFiles ,
97
+ editInstruction : fixInstruction ,
98
+ } ) ,
99
+ } ) ;
100
+
101
+ if ( ! response . ok ) {
102
+ throw new Error ( 'Failed to fix errors with AI' ) ;
103
+ }
104
+
105
+ const fixResult = await response . json ( ) ;
106
+
107
+ // Notify parent component of the fix result
108
+ if ( onFixComplete ) {
109
+ onFixComplete ( fixResult ) ;
110
+ }
111
+
112
+ } catch ( error ) {
113
+ console . error ( 'Error fixing with AI:' , error ) ;
114
+ // Could add error toast here
115
+ } finally {
116
+ setIsFixing ( false ) ;
117
+ }
118
+ } ;
119
+
55
120
return (
56
121
< div className = "w-full h-full flex items-center justify-center rounded-md border bg-background p-6" >
57
122
< div className = "w-full max-w-2xl" >
58
123
< div className = "text-center mb-6" >
59
124
< AlertCircle className = "h-12 w-12 text-destructive mx-auto mb-4" />
60
125
< h3 className = "text-lg font-semibold mb-2" > Build Errors Detected</ h3 >
61
- < p className = "text-muted-foreground text-sm" >
126
+ < p className = "text-muted-foreground text-sm mb-4 " >
62
127
Your project has some issues that need to be fixed before it can run properly.
63
128
</ p >
129
+
130
+ { /* Fix with AI Button */ }
131
+ { currentFiles && (
132
+ < Button
133
+ onClick = { handleFixWithAI }
134
+ disabled = { isFixing }
135
+ className = "mb-4"
136
+ size = "sm"
137
+ >
138
+ { isFixing ? (
139
+ < >
140
+ < div className = "animate-spin rounded-full h-4 w-4 border-b-2 border-current mr-2" />
141
+ Fixing with AI...
142
+ </ >
143
+ ) : (
144
+ < >
145
+ < Wand2 className = "h-4 w-4 mr-2" />
146
+ Fix with AI
147
+ </ >
148
+ ) }
149
+ </ Button >
150
+ ) }
64
151
</ div >
65
152
66
153
< ScrollArea className = "max-h-96" >
@@ -109,6 +196,7 @@ export function ErrorDisplay({ errors }: ErrorDisplayProps) {
109
196
< li > • Verify that all props are properly typed</ li >
110
197
< li > • Make sure all dependencies are correctly installed</ li >
111
198
< li > • Try regenerating the component with more specific requirements</ li >
199
+ { currentFiles && < li > • Use the "Fix with AI" button above for automatic error resolution</ li > }
112
200
</ ul >
113
201
</ div >
114
202
</ div >
0 commit comments