-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_reviewer.py
More file actions
545 lines (433 loc) · 20 KB
/
code_reviewer.py
File metadata and controls
545 lines (433 loc) · 20 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# code_reviewer.py (Enhanced with File Size Analysis)
"""
Enhanced Code Reviewer for Wolfkit - Now with multi-file analysis capabilities and file size monitoring
Maintains backward compatibility while adding module and project-level analysis with comprehensive file metrics
"""
import os
import json
from datetime import datetime
from typing import List, Tuple, Dict, Optional
from pathlib import Path
from enum import Enum
try:
from openai import OpenAI
OPENAI_AVAILABLE = True
except ImportError:
OPENAI_AVAILABLE = False
try:
from dotenv import load_dotenv
load_dotenv()
DOTENV_AVAILABLE = True
except ImportError:
DOTENV_AVAILABLE = False
# Import our new multi-file analysis modules
from multi_file_analyzer import MultiFileAnalyzer, AnalysisResult
from code_context_analyzer import CodeContextAnalyzer
from dependency_mapper import DependencyMapper
from file_metrics_analyzer import generate_file_size_report_section
class AnalysisScope(Enum):
"""Enumeration of analysis scopes"""
SINGLE = "single"
MODULE = "module"
PROJECT = "project"
class CodeReviewer:
"""
Enhanced code reviewer with multi-file analysis capabilities and file size monitoring
"""
def __init__(self):
self.client = None
self.model = os.getenv("OPENAI_MODEL", "gpt-4o-mini")
self.reports_dir = "./reports"
self.multi_file_analyzer = None
self.context_analyzer = None
self.dependency_mapper = None
self._setup_client()
self._ensure_reports_dir()
self._setup_multi_file_components()
def _setup_client(self):
"""Initialize OpenAI client with API key from environment"""
if not OPENAI_AVAILABLE:
return
api_key = os.getenv("OPENAI_API_KEY")
if api_key:
try:
self.client = OpenAI(api_key=api_key)
except Exception as e:
print(f"Failed to initialize OpenAI client: {e}")
def _ensure_reports_dir(self):
"""Create reports directory if it doesn't exist"""
os.makedirs(self.reports_dir, exist_ok=True)
def _setup_multi_file_components(self):
"""Initialize multi-file analysis components"""
if self.client:
self.multi_file_analyzer = MultiFileAnalyzer(self.client)
self.context_analyzer = CodeContextAnalyzer()
self.dependency_mapper = DependencyMapper()
# === ENHANCED ANALYSIS METHODS ===
def analyze_files(self, file_paths: List[str], scope: AnalysisScope = AnalysisScope.SINGLE) -> Tuple[bool, str, str]:
"""
Enhanced file analysis with configurable scope
Args:
file_paths: List of file paths to analyze
scope: Analysis scope (SINGLE, MODULE, or PROJECT)
Returns:
Tuple of (success, report_path, message)
"""
if not file_paths:
return False, "", "No files provided for analysis"
if scope == AnalysisScope.SINGLE:
return self._analyze_files_individually(file_paths)
elif scope == AnalysisScope.MODULE:
return self._analyze_files_as_module(file_paths)
elif scope == AnalysisScope.PROJECT:
# For project analysis, use the parent directory of the files
project_path = self._determine_project_path(file_paths)
return self._analyze_entire_project(project_path)
else:
return False, "", f"Unknown analysis scope: {scope}"
def analyze_module(self, file_paths: List[str]) -> Tuple[bool, str, str]:
"""
Analyze multiple files as a cohesive module
Args:
file_paths: List of file paths to analyze as a module
Returns:
Tuple of (success, report_path, message)
"""
return self.analyze_files(file_paths, AnalysisScope.MODULE)
def analyze_project(self, project_path: str) -> Tuple[bool, str, str]:
"""
Analyze entire project for comprehensive review
Args:
project_path: Path to the project root directory
Returns:
Tuple of (success, report_path, message)
"""
if not os.path.exists(project_path):
return False, "", f"Project path does not exist: {project_path}"
return self._analyze_entire_project(project_path)
def configure_file_size_analysis(self, enabled: bool = True, preset: str = "standard",
custom_thresholds: dict = None):
"""
Configure file size analysis settings
Args:
enabled: Whether to include file size analysis
preset: Preset name for thresholds
custom_thresholds: Custom threshold values
"""
if self.multi_file_analyzer:
self.multi_file_analyzer.include_file_analysis = enabled
if enabled:
if custom_thresholds:
self.multi_file_analyzer.update_file_size_settings(custom_thresholds=custom_thresholds)
else:
self.multi_file_analyzer.update_file_size_settings(preset=preset)
# === IMPLEMENTATION METHODS ===
def _analyze_files_individually(self, file_paths: List[str]) -> Tuple[bool, str, str]:
"""Original single-file analysis method"""
if not self.client:
return False, "", "OpenAI client not available. Please check your .env file contains OPENAI_API_KEY."
# Generate timestamp for report
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
report_filename = f"wolfkit_analysis_{timestamp}.md"
report_path = os.path.join(self.reports_dir, report_filename)
analyses = []
successful_analyses = 0
for file_path in file_paths:
success, result = self._analyze_single_file(file_path)
if success:
analyses.append(result)
successful_analyses += 1
else:
analyses.append(f"### Error analyzing `{os.path.basename(file_path)}`\n\n❌ {result}\n\n---\n")
# Generate report header
report_content = f"""# Wolfkit AI Code Review (Individual Files)
**Generated:** {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
**Files Analyzed:** {len(file_paths)}
**Successful:** {successful_analyses}
**Model Used:** {self.model}
**Analysis Scope:** Individual file analysis
---
"""
# Add all analyses
report_content += "\n\n---\n\n".join(analyses)
try:
# Write report to file
with open(report_path, 'w', encoding='utf-8') as f:
f.write(report_content)
message = f"Individual analysis complete! {successful_analyses}/{len(file_paths)} files analyzed successfully."
return True, report_path, message
except Exception as e:
return False, "", f"Failed to write report: {str(e)}"
def _analyze_files_as_module(self, file_paths: List[str]) -> Tuple[bool, str, str]:
"""Analyze files as a cohesive module"""
if not self.multi_file_analyzer:
return False, "", "Multi-file analysis not available. Please check your .env file contains OPENAI_API_KEY."
try:
# Perform module analysis
result = self.multi_file_analyzer.analyze_as_module(file_paths)
if not result.success:
return False, "", result.error_message
# Generate report
report_path = self._generate_multi_file_report(result, "Module")
return True, report_path, f"Module analysis complete! {len(file_paths)} files analyzed as cohesive module."
except Exception as e:
return False, "", f"Module analysis failed: {str(e)}"
def _analyze_entire_project(self, project_path: str) -> Tuple[bool, str, str]:
"""Analyze entire project"""
if not self.multi_file_analyzer:
return False, "", "Project analysis not available. Please check your .env file contains OPENAI_API_KEY."
try:
# Perform project analysis
result = self.multi_file_analyzer.analyze_as_project(project_path)
if not result.success:
return False, "", result.error_message
# Generate report
report_path = self._generate_multi_file_report(result, "Project")
total_files = result.context_summary.get('total_files', 0)
return True, report_path, f"Project analysis complete! {total_files} files analyzed for architectural review."
except Exception as e:
return False, "", f"Project analysis failed: {str(e)}"
def _generate_multi_file_report(self, result: AnalysisResult, analysis_type: str) -> str:
"""
Generate enhanced report for multi-file analysis with file size integration
"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
report_filename = f"wolfkit_{analysis_type.lower()}_analysis_{timestamp}.md"
report_path = os.path.join(self.reports_dir, report_filename)
# Build report content with file size analysis
report_content = f"""# Wolfkit AI Code Review ({analysis_type} Analysis)
**Generated:** {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
**Analysis Type:** {analysis_type}
**Model Used:** {self.model}
## Analysis Summary
- **Target Files:** {len(result.target_files)}
- **Analysis Scope:** {result.analysis_scope}
"""
# Add context summary
if result.context_summary:
summary = result.context_summary
if summary.get('framework'):
report_content += f"- **Framework:** {summary['framework']}\n"
if summary.get('total_files'):
report_content += f"- **Total Context Files:** {summary['total_files']}\n"
if summary.get('external_deps'):
report_content += f"- **External Dependencies:** {summary['external_deps']}\n"
if summary.get('missing_imports'):
report_content += f"- **Missing Imports Found:** {summary['missing_imports']}\n"
# NEW: Add file size summary to header
if summary.get('files_needing_action') is not None:
report_content += f"- **Files Needing Size Attention:** {summary['files_needing_action']}\n"
if summary.get('architecture_health'):
report_content += f"- **Architecture Health:** {summary['architecture_health']}\n"
report_content += f"""
---
## Target Files
"""
# List target files with size indicators
for file_path in result.target_files:
rel_path = Path(file_path).name
# Add size indicator if file metrics available
size_indicator = ""
if hasattr(result, 'file_metrics') and result.file_metrics:
for file_metric in result.file_metrics.files_by_category.get('warning', []) + \
result.file_metrics.files_by_category.get('critical', []) + \
result.file_metrics.files_by_category.get('dangerous', []):
if rel_path in file_metric.file_path:
if file_metric.size_category.value == "dangerous":
size_indicator = " 🚨"
elif file_metric.size_category.value == "critical":
size_indicator = " 🔥"
elif file_metric.size_category.value == "warning":
size_indicator = " ⚠️"
break
report_content += f"- `{rel_path}`{size_indicator}\n"
report_content += f"""
---
{result.analysis_content}
---
"""
# NEW: Add file size analysis section if available
if hasattr(result, 'file_metrics') and result.file_metrics:
file_size_section = generate_file_size_report_section(result.file_metrics)
report_content += f"\n{file_size_section}\n---\n"
report_content += f"""
*This {analysis_type.lower()} analysis was generated by Wolfkit's enhanced code review system with cross-file context awareness and comprehensive file size monitoring.*
"""
# Write report
with open(report_path, 'w', encoding='utf-8') as f:
f.write(report_content)
return report_path
def _determine_project_path(self, file_paths: List[str]) -> str:
"""Determine project root path from file paths"""
if not file_paths:
return os.getcwd()
# Find common parent directory
common_path = Path(os.path.commonpath(file_paths))
# If common path is a file, use its parent
if common_path.is_file():
common_path = common_path.parent
return str(common_path)
# === ORIGINAL METHODS (PRESERVED FOR BACKWARD COMPATIBILITY) ===
def _get_file_type_prompt(self, file_extension: str) -> str:
"""Return file-type specific analysis prompt"""
base_prompt = """You are an expert code reviewer. Analyze the provided code file and identify:
1. **Syntax Errors**: Any obvious syntax issues
2. **Missing Dependencies**: Undefined variables, functions, or imports
3. **Logic Issues**: Common programming mistakes or inconsistencies
4. **Structure Problems**: Missing entry points, circular references
5. **Best Practices**: Simple improvements that could prevent issues
Focus on issues that would prevent the code from running or cause immediate problems.
Be concise but specific. Use clear categories and bullet points.
Return your analysis in this markdown format:
### Analysis of `{filename}`
**File Type:** [language]
**Syntax Check:** ✅ Valid / ❌ Issues found
**Issues Found:**
- ❌ [Critical Issue]: Description
- ⚠️ [Warning]: Description
- ✅ [Good Practice Found]: Description
**Summary:**
Brief overall assessment and main recommendations.
---
"""
prompts = {
'.py': base_prompt + """
Pay special attention to:
- Import statements and module availability
- Function definitions vs calls
- Indentation and Python syntax
- Missing main() blocks or entry points
""",
'.js': base_prompt + """
Pay special attention to:
- Variable declarations (let, const, var)
- Function definitions vs calls
- Missing semicolons or brackets
- Async/await usage
""",
'.ts': base_prompt + """
Pay special attention to:
- TypeScript type annotations
- Interface definitions
- Import/export statements
- Type mismatches
""",
'.html': base_prompt + """
Pay special attention to:
- Tag structure and nesting
- Missing closing tags
- Script and link references
- Form structure
""",
'.css': base_prompt + """
Pay special attention to:
- Selector syntax
- Property names and values
- Missing semicolons or brackets
- CSS rule structure
""",
'.json': base_prompt + """
Pay special attention to:
- JSON syntax validity
- Proper quotation marks
- Comma placement
- Bracket/brace matching
"""
}
return prompts.get(file_extension.lower(), base_prompt)
def _analyze_single_file(self, file_path: str) -> Tuple[bool, str]:
"""Analyze a single file with LLM"""
if not self.client:
return False, "OpenAI client not available. Check API key in .env file."
try:
# Read file content
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
filename = os.path.basename(file_path)
file_extension = Path(file_path).suffix
# Get appropriate prompt for file type
prompt = self._get_file_type_prompt(file_extension)
# Prepare the full prompt
full_prompt = f"{prompt}\n\nFile to analyze: `{filename}`\n\n```{file_extension[1:]}\n{content}\n```"
# Send to OpenAI
response = self.client.chat.completions.create(
model=self.model,
messages=[
{"role": "system", "content": "You are an expert code reviewer focused on finding issues that prevent code from running."},
{"role": "user", "content": full_prompt}
],
temperature=0.1
)
analysis = response.choices[0].message.content
return True, analysis.replace("{filename}", filename)
except FileNotFoundError:
return False, f"File not found: {file_path}"
except Exception as e:
return False, f"Error analyzing {file_path}: {str(e)}"
def check_configuration(self) -> Tuple[bool, str]:
"""Check if the reviewer is properly configured"""
issues = []
if not OPENAI_AVAILABLE:
issues.append("OpenAI package not installed (pip install openai)")
if not DOTENV_AVAILABLE:
issues.append("python-dotenv package not installed (pip install python-dotenv)")
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
issues.append("OPENAI_API_KEY not found in .env file")
elif not api_key.startswith("sk-"):
issues.append("OPENAI_API_KEY appears to be invalid")
if not self.client:
issues.append("OpenAI client failed to initialize")
if issues:
return False, "Configuration issues found:\n" + "\n".join(f"- {issue}" for issue in issues)
# Check multi-file capabilities
capabilities = []
if self.multi_file_analyzer:
capabilities.append("✅ Multi-file analysis ready")
capabilities.append("✅ Module analysis ready")
capabilities.append("✅ Project analysis ready")
capabilities.append("✅ File size analysis ready")
else:
capabilities.append("⚠️ Multi-file analysis limited (check API key)")
return True, "Code reviewer is properly configured and ready to use.\n" + "\n".join(capabilities)
def get_analysis_capabilities(self) -> Dict[str, any]:
"""Get enhanced analysis capabilities including file metrics"""
base_capabilities = {
'single_file': True,
'openai_available': OPENAI_AVAILABLE,
'client_configured': self.client is not None
}
if self.multi_file_analyzer:
multi_file_caps = self.multi_file_analyzer.get_analysis_capabilities()
base_capabilities.update(multi_file_caps)
else:
base_capabilities.update({
'ai_available': False,
'supported_scopes': ['single'],
'max_files_per_analysis': 1,
'file_size_analysis': False,
'file_size_presets': [],
'current_preset': 'none'
})
return base_capabilities
# === CONVENIENCE FUNCTIONS FOR CONTROLLER INTEGRATION ===
def analyze_files(file_paths: List[str]) -> Tuple[bool, str, str]:
"""Convenience function for single-file analysis (backward compatibility)"""
reviewer = CodeReviewer()
return reviewer.analyze_files(file_paths, AnalysisScope.SINGLE)
def analyze_module(file_paths: List[str]) -> Tuple[bool, str, str]:
"""Convenience function for module analysis"""
reviewer = CodeReviewer()
return reviewer.analyze_module(file_paths)
def analyze_project(project_path: str) -> Tuple[bool, str, str]:
"""Convenience function for project analysis"""
reviewer = CodeReviewer()
return reviewer.analyze_project(project_path)
def check_reviewer_config() -> Tuple[bool, str]:
"""Convenience function for checking configuration"""
reviewer = CodeReviewer()
return reviewer.check_configuration()
def get_reviewer_capabilities() -> Dict[str, any]:
"""Convenience function for getting capabilities"""
reviewer = CodeReviewer()
return reviewer.get_analysis_capabilities()