|
| 1 | +"""Code review tools: review code, save results to database.""" |
| 2 | +import sqlite3 |
| 3 | +import json |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +DB_PATH = "code_reviews.db" |
| 7 | + |
| 8 | + |
| 9 | +def _get_db(): |
| 10 | + conn = sqlite3.connect(DB_PATH) |
| 11 | + conn.execute(""" |
| 12 | + CREATE TABLE IF NOT EXISTS reviews ( |
| 13 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 14 | + file_path TEXT, |
| 15 | + summary TEXT, |
| 16 | + bugs TEXT, |
| 17 | + improvements TEXT, |
| 18 | + score INTEGER, |
| 19 | + created_at TEXT |
| 20 | + ) |
| 21 | + """) |
| 22 | + return conn |
| 23 | + |
| 24 | + |
| 25 | +def review_code(code: str, file_path: str = "") -> dict: |
| 26 | + """Analyze code and return structured review. |
| 27 | +
|
| 28 | + This is a tool the agent calls — the actual LLM analysis happens |
| 29 | + through the agent's model. The returned dict is the structured |
| 30 | + output template. |
| 31 | + """ |
| 32 | + return { |
| 33 | + "code_snippet": code[:500], |
| 34 | + "file_path": file_path, |
| 35 | + "needs_review": True, |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | +def save_review(file_path: str, summary: str, bugs: str, |
| 40 | + improvements: str, score: int = 0) -> str: |
| 41 | + """Save a code review to the SQLite database. |
| 42 | +
|
| 43 | + Args: |
| 44 | + file_path: Path to the reviewed file |
| 45 | + summary: One-sentence summary |
| 46 | + bugs: Bug findings |
| 47 | + improvements: Suggested improvements |
| 48 | + score: Quality score (0-10) |
| 49 | + """ |
| 50 | + conn = _get_db() |
| 51 | + conn.execute( |
| 52 | + "INSERT INTO reviews (file_path, summary, bugs, improvements, score, created_at) " |
| 53 | + "VALUES (?, ?, ?, ?, ?, ?)", |
| 54 | + (file_path, summary, bugs, improvements, score, datetime.now().isoformat()) |
| 55 | + ) |
| 56 | + conn.commit() |
| 57 | + count = conn.execute("SELECT COUNT(*) FROM reviews").fetchone()[0] |
| 58 | + conn.close() |
| 59 | + return f"Review saved. Total reviews in database: {count}" |
0 commit comments