@@ -24,6 +24,7 @@ export default function AIFeedback({
24
24
const [ isOpen , setIsOpen ] = useState ( false ) ;
25
25
const [ isGenerating , setIsGenerating ] = useState ( false ) ;
26
26
const [ generatedDocs , setGeneratedDocs ] = useState < string | null > ( null ) ;
27
+ const [ error , setError ] = useState < string | null > ( null ) ;
27
28
28
29
const generateStructureDocs = (
29
30
structure : StructureMap ,
@@ -46,61 +47,50 @@ export default function AIFeedback({
46
47
47
48
const generatePrompt = ( structure : StructureMap ) : string => {
48
49
const structureDocs = generateStructureDocs ( structure ) ;
49
- return `As an expert software architect, provide a 2-3 sentence feedback on the repository structure.
50
+ return `You are an expert software architect. Analyze this repository structure and provide concise, actionable feedback on:
51
+ 1. Code organization and modularity.
52
+ 2. Best practices or areas for improvement.
53
+ 3. Architectural concerns, if any.
50
54
51
55
Project Structure:
52
-
56
+
53
57
${ structureDocs }
54
-
55
- Feedback:` ;
56
- } ;
57
-
58
- const extractFeedback = ( response : string ) : string => {
59
- const feedbackIndex = response . indexOf ( 'Feedback:' ) ;
60
- return feedbackIndex !== - 1
61
- ? response . slice ( feedbackIndex + 9 ) . trim ( )
62
- : response . trim ( ) ;
58
+
59
+ Provide your feedback in a structured format, using bullet points or numbered lists. Keep your response practical and to the point, limited to 2-3 sentences.` ;
63
60
} ;
64
61
65
62
const handleGenerate = async ( ) => {
63
+ // Reset previous states
66
64
setIsGenerating ( true ) ;
65
+ setGeneratedDocs ( null ) ;
66
+ setError ( null ) ;
67
67
68
68
try {
69
69
const prompt = generatePrompt ( structureMap ) ;
70
- const response = await fetch (
71
- 'https://api-inference.huggingface.co/models/Qwen/Qwen2.5-1.5B-Instruct' ,
72
- {
73
- method : 'POST' ,
74
- headers : {
75
- 'Content-Type' : 'application/json' ,
76
- Authorization : `Bearer ${ process . env . NEXT_PUBLIC_HUGGINGFACE_API_KEY } ` ,
77
- } ,
78
- body : JSON . stringify ( {
79
- inputs : prompt ,
80
- parameters : {
81
- max_length : 12000 ,
82
- temperature : 0.9 ,
83
- top_p : 0.95 ,
84
- } ,
85
- } ) ,
70
+ const response = await fetch ( '/api/feedback' , {
71
+ method : 'POST' ,
72
+ headers : {
73
+ 'Content-Type' : 'application/json' ,
86
74
} ,
87
- ) ;
75
+ body : JSON . stringify ( { prompt } ) ,
76
+ } ) ;
77
+
78
+ const responseData = await response . json ( ) ;
88
79
89
80
if ( ! response . ok ) {
90
- throw new Error ( 'Failed to generate feedback' ) ;
81
+ throw new Error ( responseData . error || 'Failed to generate feedback' ) ;
91
82
}
92
83
93
- const result = await response . json ( ) ;
94
-
95
- if ( result && result [ 0 ] ?. generated_text ) {
96
- const feedback = extractFeedback ( result [ 0 ] . generated_text ) ;
97
- setGeneratedDocs ( feedback ) ;
98
- } else {
99
- throw new Error ( 'Invalid response format' ) ;
100
- }
84
+ setGeneratedDocs ( responseData . feedback ) ;
101
85
} catch ( error ) {
102
86
console . error ( 'Error generating feedback:' , error ) ;
103
- setGeneratedDocs ( 'Error generating feedback. Please try again.' ) ;
87
+
88
+ const errorMessage = error instanceof Error
89
+ ? error . message
90
+ : 'An unexpected error occurred' ;
91
+
92
+ setError ( errorMessage ) ;
93
+ setGeneratedDocs ( null ) ;
104
94
} finally {
105
95
setIsGenerating ( false ) ;
106
96
}
@@ -109,38 +99,41 @@ Feedback:`;
109
99
return (
110
100
< >
111
101
< Button onClick = { ( ) => setIsOpen ( true ) } className = "mt-4 w-full md:w-auto" >
112
- < MessageSquare className = "h-4 w-4" /> Generate Feedback
102
+ < MessageSquare className = "h-4 w-4 mr-2 " /> Generate Feedback
113
103
</ Button >
114
104
< Dialog open = { isOpen } onOpenChange = { setIsOpen } >
115
105
< DialogContent className = "sm:max-w-[800px] max-h-[90vh] flex flex-col" >
116
106
< DialogHeader >
117
- < DialogTitle > Generate AI Feedback </ DialogTitle >
107
+ < DialogTitle > AI Repository Analysis </ DialogTitle >
118
108
< DialogDescription >
119
- Get feedback on your repository structure.
120
- < em className = "block mt-2" >
121
- Instructions: First, generate your repository structure, then
122
- click Generate Feedback.
123
- </ em >
109
+ Get expert feedback on your repository structure and organization using Gemini AI.
124
110
</ DialogDescription >
125
111
</ DialogHeader >
126
112
127
113
< div className = "flex-grow overflow-hidden my-6" >
128
114
{ isGenerating ? (
129
115
< div className = "flex flex-col items-center justify-center h-full space-y-4" >
130
116
< Loader2 className = "h-6 w-6 animate-spin" />
131
- < p className = "text-sm text-gray-500" > Generating feedback...</ p >
117
+ < p className = "text-sm text-gray-500" > Analyzing repository structure...</ p >
118
+ </ div >
119
+ ) : error ? (
120
+ < div className = "text-center text-red-500" >
121
+ < p > Error: { error } </ p >
122
+ < p className = "text-sm text-gray-500 mt-2" > Please try again or check your API setup.</ p >
132
123
</ div >
133
124
) : generatedDocs ? (
134
125
< div className = "h-full overflow-y-auto" >
135
126
< Card >
136
127
< div className = "p-4 h-full overflow-y-auto" >
137
- < p className = "text-sm whitespace-pre-wrap" >
138
- { generatedDocs }
139
- </ p >
128
+ < p className = "text-sm whitespace-pre-wrap" > { generatedDocs } </ p >
140
129
</ div >
141
130
</ Card >
142
131
</ div >
143
- ) : null }
132
+ ) : (
133
+ < div className = "text-center text-gray-500" >
134
+ Click Generate to analyze your repository structure
135
+ </ div >
136
+ ) }
144
137
</ div >
145
138
146
139
< DialogFooter >
@@ -151,18 +144,17 @@ Feedback:`;
151
144
>
152
145
{ isGenerating ? (
153
146
< >
154
- < Loader2 className = "h-4 w-4 animate-spin" />
155
- Generating ...
147
+ < Loader2 className = "h-4 w-4 mr-2 animate-spin" />
148
+ Analyzing ...
156
149
</ >
157
150
) : (
158
- 'Generate'
151
+ 'Generate Analysis '
159
152
) }
160
153
</ Button >
161
154
</ DialogFooter >
162
155
< p className = "mt-4 text-sm text-gray-600" >
163
156
< em >
164
- Note: This feedback is generated by an AI model and may not always
165
- be accurate. Use it as a general guideline.
157
+ Note: This feedback is AI-generated and meant as a general guide.
166
158
</ em >
167
159
</ p >
168
160
</ DialogContent >
0 commit comments