-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai-code-reviewer.js
More file actions
351 lines (313 loc) · 9.77 KB
/
Copy pathai-code-reviewer.js
File metadata and controls
351 lines (313 loc) · 9.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**
* Rahyana AI API - بررسیکننده کد هوش مصنوعی
*
* این پروژه یک سیستم کامل برای بررسی کد با AI شامل:
* - بررسی کیفیت کد
* - شناسایی مشکلات امنیتی
* - پیشنهادات بهبود
* - بررسی بهترین شیوهها
* - تحلیل عملکرد
* - بررسی قابلیت نگهداری
*/
// Configuration - supports environment variable or placeholder
const API_KEY = process.env.RAHYANA_API_KEY || 'YOUR_API_KEY_HERE'; // کلید API خود را اینجا قرار دهید یا از متغیر محیطی RAHYANA_API_KEY استفاده کنید
const BASE_URL = process.env.RAHYANA_BASE_URL || 'https://rahyana.ir/api/v1';
class AICodeReviewer {
constructor(apiKey, baseUrl) {
this.apiKey = apiKey;
this.baseUrl = baseUrl;
this.reviews = [];
this.standards = {
javascript: 'ES2025',
security: 'OWASP',
performance: 'Google V8'
};
}
// بررسی کلی کد
async reviewCode(sourceCode, language = 'javascript') {
try {
const prompt = `
کد ${language} زیر را بررسی کنید:
\`\`\`${language}
${sourceCode}
\`\`\`
بررسی شامل:
1. کیفیت کد و خوانایی
2. مشکلات امنیتی
3. بهینهسازی عملکرد
4. بهترین شیوهها
5. قابلیت نگهداری
6. تستپذیری
7. مستندسازی
8. امتیاز کلی (1-10)
`;
const response = await fetch(`${this.baseUrl}/chat/completions`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
'HTTP-Referer': 'https://ai-review-app.com',
'X-Title': 'AI Code Reviewer'
},
body: JSON.stringify({
model: 'openai/gpt-4o',
messages: [
{
role: 'system',
content: 'شما یک متخصص بررسی کد و کیفیت نرمافزار هستید.'
},
{
role: 'user',
content: prompt
}
],
temperature: 0.2,
max_tokens: 2000
})
});
if (!response.ok) {
throw new Error(`خطای HTTP! وضعیت: ${response.status}`);
}
const data = await response.json();
const review = {
timestamp: new Date(),
code: sourceCode,
language,
review: data.choices[0].message.content,
score: this.extractScore(data.choices[0].message.content)
};
this.reviews.push(review);
return review;
} catch (error) {
console.error('خطا در بررسی کد:', error);
throw error;
}
}
// بررسی امنیتی
async securityReview(sourceCode) {
try {
const prompt = `
کد زیر را از نظر امنیت بررسی کنید:
\`\`\`javascript
${sourceCode}
\`\`\`
بررسی امنیتی شامل:
1. تزریق SQL
2. XSS
3. CSRF
4. Authentication bypass
5. Authorization flaws
6. Input validation
7. Data exposure
8. Cryptographic issues
9. Rate limiting
10. OWASP Top 10
`;
const response = await fetch(`${this.baseUrl}/chat/completions`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
'HTTP-Referer': 'https://ai-review-app.com',
'X-Title': 'AI Code Reviewer'
},
body: JSON.stringify({
model: 'openai/gpt-4o',
messages: [
{
role: 'system',
content: 'شما یک متخصص امنیت نرمافزار و تحلیل آسیبپذیری هستید.'
},
{
role: 'user',
content: prompt
}
],
temperature: 0.1,
max_tokens: 1500
})
});
if (!response.ok) {
throw new Error(`خطای HTTP! وضعیت: ${response.status}`);
}
const data = await response.json();
return data.choices[0].message.content;
} catch (error) {
console.error('خطا در بررسی امنیتی:', error);
throw error;
}
}
// بررسی عملکرد
async performanceReview(sourceCode) {
try {
const prompt = `
کد زیر را از نظر عملکرد بررسی کنید:
\`\`\`javascript
${sourceCode}
\`\`\`
بررسی عملکرد شامل:
1. پیچیدگی زمانی
2. پیچیدگی مکانی
3. Memory leaks
4. Inefficient algorithms
5. Unnecessary computations
6. Database queries
7. Network calls
8. Caching opportunities
9. Async/await usage
10. Event loop blocking
`;
const response = await fetch(`${this.baseUrl}/chat/completions`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
'HTTP-Referer': 'https://ai-review-app.com',
'X-Title': 'AI Code Reviewer'
},
body: JSON.stringify({
model: 'openai/gpt-4o',
messages: [
{
role: 'system',
content: 'شما یک متخصص بهینهسازی عملکرد و تحلیل کد هستید.'
},
{
role: 'user',
content: prompt
}
],
temperature: 0.2,
max_tokens: 1500
})
});
if (!response.ok) {
throw new Error(`خطای HTTP! وضعیت: ${response.status}`);
}
const data = await response.json();
return data.choices[0].message.content;
} catch (error) {
console.error('خطا در بررسی عملکرد:', error);
throw error;
}
}
// بررسی بهترین شیوهها
async bestPracticesReview(sourceCode, language = 'javascript') {
try {
const prompt = `
کد ${language} زیر را از نظر بهترین شیوهها بررسی کنید:
\`\`\`${language}
${sourceCode}
\`\`\`
بررسی شامل:
1. Code style و formatting
2. Naming conventions
3. Function design
4. Error handling
5. Logging
6. Testing
7. Documentation
8. Modularity
9. SOLID principles
10. Design patterns
`;
const response = await fetch(`${this.baseUrl}/chat/completions`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
'HTTP-Referer': 'https://ai-review-app.com',
'X-Title': 'AI Code Reviewer'
},
body: JSON.stringify({
model: 'openai/gpt-4o',
messages: [
{
role: 'system',
content: `شما یک متخصص بهترین شیوههای ${language} و clean code هستید.`
},
{
role: 'user',
content: prompt
}
],
temperature: 0.3,
max_tokens: 1500
})
});
if (!response.ok) {
throw new Error(`خطای HTTP! وضعیت: ${response.status}`);
}
const data = await response.json();
return data.choices[0].message.content;
} catch (error) {
console.error('خطا در بررسی بهترین شیوهها:', error);
throw error;
}
}
// تولید گزارش کلی
async generateReviewReport() {
try {
const recentReviews = this.reviews.slice(-10);
const avgScore = recentReviews.reduce((sum, r) => sum + (r.score || 0), 0) / recentReviews.length;
const prompt = `
گزارش کلی بررسی کد بر اساس بررسیهای زیر:
${JSON.stringify(recentReviews, null, 2)}
گزارش شامل:
1. خلاصه کلی
2. امتیاز متوسط: ${avgScore.toFixed(2)}
3. نقاط قوت
4. نقاط ضعف
5. اولویتهای بهبود
6. توصیههای کلی
7. برنامه بهبود
`;
const response = await fetch(`${this.baseUrl}/chat/completions`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
'HTTP-Referer': 'https://ai-review-app.com',
'X-Title': 'AI Code Reviewer'
},
body: JSON.stringify({
model: 'openai/gpt-4o',
messages: [
{
role: 'system',
content: 'شما یک متخصص تحلیل کیفیت کد و تولید گزارش هستید.'
},
{
role: 'user',
content: prompt
}
],
temperature: 0.4,
max_tokens: 2000
})
});
if (!response.ok) {
throw new Error(`خطای HTTP! وضعیت: ${response.status}`);
}
const data = await response.json();
return data.choices[0].message.content;
} catch (error) {
console.error('خطا در تولید گزارش:', error);
throw error;
}
}
// استخراج امتیاز از متن
extractScore(text) {
const scoreMatch = text.match(/(?:امتیاز|score|rating)[:\s]*(\d+(?:\.\d+)?)/i);
return scoreMatch ? parseFloat(scoreMatch[1]) : 5;
}
// دریافت بررسیهای انجام شده
getReviews() {
return this.reviews;
}
// پاک کردن بررسیها
clearReviews() {
this.reviews = [];
}
}
export default AICodeReviewer;