gigi-cli 1.0.0

Gigi — A Claude Code-like AI coding assistant CLI in Rust
use anyhow::Result;
use colored::*;

use crate::agent::Agent;

// =============================================================================
// /review — Ask the AI to review recent git changes
// =============================================================================

pub async fn handle(agent: &mut Agent, args: &str) -> Result<()> {
    println!("{}", "Fetching changes for review...".dimmed());

    // Get recent changes
    let diff_cmd = if args.is_empty() {
        // Default: review uncommitted changes
        "git diff --stat && echo '---FULL DIFF---' && git diff"
    } else {
        // Review specific range or branch
        args
    };

    let diff_result = agent
        .tools
        .execute(
            "bash",
            serde_json::json!({
                "command": diff_cmd
            }),
        )
        .await?;

    let diff_content = extract_stdout_stderr(&diff_result.content);

    if diff_content.trim().is_empty() {
        println!("{}", "No changes to review.".yellow());
        return Ok(());
    }

    let prompt = format!(
        "Please review the following code changes. Look for:\n\
         - Bugs or logic errors\n\
         - Security issues\n\
         - Performance concerns\n\
         - Code style improvements\n\
         - Missing error handling\n\n\
         Provide actionable feedback.\n\n{}",
        diff_content
    );

    agent.run_turn(&prompt).await?;

    Ok(())
}

fn extract_stdout_stderr(content: &str) -> String {
    if let Ok(val) = serde_json::from_str::<serde_json::Value>(content) {
        let stdout = val["stdout"].as_str().unwrap_or("");
        let stderr = val["stderr"].as_str().unwrap_or("");
        if !stdout.is_empty() && !stderr.is_empty() {
            format!("{}\n{}", stdout, stderr)
        } else if !stdout.is_empty() {
            stdout.to_string()
        } else {
            stderr.to_string()
        }
    } else {
        content.to_string()
    }
}