"""Code review tool - AI-powered code quality analysis."""
import json
from pathlib import Path
from .file_ops import read_file
from utils.llm_client import LLMClient
def load_prompt(prompt_name: str) -> str:
"""Load a prompt template from the prompts directory."""
prompt_path = Path(__file__).parent.parent / "prompts" / f"{prompt_name}.txt"
with open(prompt_path, 'r') as f:
return f.read()
def review_code(file_path: str, ai_provider: str = "gemini") -> dict:
"""Review code for quality, security, and best practices.
Args:
file_path: Path to the code file to review
ai_provider: AI provider to use (gemini, claude, openai, etc.)
Returns:
Dictionary with review results including issues, score, and assessment
"""
# Read the code
try:
code = read_file(file_path)
except Exception as e:
return {"error": f"Failed to read file: {str(e)}"}
# Load prompt template and fill in variables
prompt_template = load_prompt("code_review")
prompt = prompt_template.format(code=code)
llm = LLMClient(default_provider=ai_provider)
try:
# Use structured JSON output mode for reliable parsing
response = llm.complete(prompt, provider=ai_provider, temperature=0.3, json_mode=True)
result = json.loads(response)
return {
"status": "success",
"file_path": file_path,
"issues": result.get("issues", []),
"strengths": result.get("strengths", []),
"overall_assessment": result.get("overall_assessment", ""),
"score": result.get("score", 0)
}
except Exception as e:
return {
"status": "error",
"error": f"Code review failed: {str(e)}"
}