soma-core 2.0.0

World's first production-ready self-aware development system with meta-cognitive capabilities and cognitive reasoning engine for intelligent development platforms
Documentation
// examples/cli_demo.rs
// Demonstrates the CLI functionality with simulated user interactions

use soma_core::agents::claude_agent::ClaudeAgent;
use soma_core::agents::gemini_agent::GeminiAgent;
use soma_core::agents::gpt4_agent::{CognitiveAgent, GPT4Agent, ProposedEdit};
use soma_core::feedback_loop::{apply_consensus_edits, show_file_diff};
use soma_core::memory::SymbolicContext;
use std::fs;

fn simulate_agent_collaboration() -> Result<(), Box<dyn std::error::Error>> {
    println!("🎭 SIMULATED CLI DEMO - Agent Collaboration");
    println!("{}", "=".repeat(60));
    println!("This shows exactly what happens when you use the interactive CLI!\n");

    // Initialize agents
    let agents: Vec<(String, Box<dyn CognitiveAgent>)> = vec![
        ("GPT-4".to_string(), Box::new(GPT4Agent)),
        ("Claude".to_string(), Box::new(ClaudeAgent)),
        ("Gemini".to_string(), Box::new(GeminiAgent)),
    ];

    let test_file = "examples/test_code.rs";

    // Step 1: Show file content (like the CLI does)
    println!("📄 Current file content: {}", test_file);
    println!("{}", "=".repeat(50));
    let content = fs::read_to_string(test_file)?;
    for (i, line) in content.lines().enumerate() {
        println!("{:3}: {}", i + 1, line);
    }

    // Step 2: Simulate user choosing "performance" focus
    println!("\n🎯 User selects: Performance optimization");
    let mut ctx = SymbolicContext::new();
    ctx.set("current_task", "performance");

    // Step 3: Collect agent proposals (like the CLI does)
    println!("\n🤖 Agents are analyzing and proposing improvements...");
    println!("{}", "=".repeat(50));

    let mut proposals = Vec::new();
    for (agent_name, agent) in &agents {
        println!("🔍 {} is thinking...", agent_name);
        let proposal = agent.propose_edit(&ctx);
        proposals.push((agent_name.clone(), proposal));
    }

    // Step 4: Show proposals to user (like the CLI does)
    println!("\n📋 Agent Proposals - Simulated User Review");
    println!("{}", "=".repeat(50));

    let mut user_selected = Vec::new();

    for (i, (agent_name, proposal)) in proposals.iter().enumerate() {
        println!("\n{}. 🤖 {} proposes:", i + 1, agent_name);
        println!("   📁 File: {}", proposal.file);
        println!("   📍 Lines: {:?}", proposal.line_range);
        println!("   💡 Reason: {}", proposal.reason);
        println!("   📊 Confidence: {:.1}%", proposal.confidence * 100.0);
        println!("   📝 Proposed code:");
        for (line_num, line) in proposal.new_code.lines().enumerate() {
            println!("      {}: {}", line_num + 1, line);
        }

        // Simulate user decision (approve GPT-4's performance fix)
        if agent_name == "GPT-4" && proposal.reason.contains("Performance") {
            println!("\n   ✅ User approves this edit!");
            user_selected.push(proposal.clone());
        } else {
            println!("\n   ❌ User rejects this edit.");
        }
    }

    // Step 5: Apply selected edits (like the CLI does)
    if !user_selected.is_empty() {
        println!("\n🔧 Applying {} selected edit(s)...", user_selected.len());
        println!("{}", "=".repeat(40));

        let edit_refs: Vec<&ProposedEdit> = user_selected.iter().collect();
        let backup_paths = apply_consensus_edits(&edit_refs)?;

        if let Some(backup_path) = backup_paths.first() {
            println!("\n📊 Changes Applied - Before vs After:");
            show_file_diff(test_file, backup_path)?;

            println!("\n📄 Updated file content:");
            println!("{}", "=".repeat(50));
            let final_content = fs::read_to_string(test_file)?;
            for (i, line) in final_content.lines().enumerate() {
                println!("{:3}: {}", i + 1, line);
            }

            println!("\n💾 User chooses to keep changes: YES");
            println!("✅ Changes saved! Backup removed.");
            fs::remove_file(backup_path)?;
        }
    }

    println!("\n🎉 Collaboration Complete!");
    println!("💡 This is exactly what happens in the interactive CLI!");
    println!("   • You see each agent's individual proposals");
    println!("   • You make the final decision on each edit");
    println!("   • Changes are applied and you see the diff");
    println!("   • You choose whether to keep or revert changes");

    Ok(())
}

fn show_cli_usage() {
    println!("\n📚 How to Use the Interactive CLI:");
    println!("{}", "=".repeat(40));
    println!("\n🚀 To start the real interactive CLI, run:");
    println!("   cargo run --bin soma-cli");
    println!("\n📋 Available options:");
    println!("   1. 📄 Analyze & improve a specific file");
    println!("      • Enter file path (e.g., examples/test_code.rs)");
    println!("      • Choose improvement focus (performance, safety, etc.)");
    println!("      • Review each agent's proposal individually");
    println!("      • Approve/reject each suggestion");
    println!("      • See before/after changes");
    println!("      • Keep or revert changes");
    println!("\n   2. 🎯 Set custom improvement task");
    println!("      • Describe what you want improved");
    println!("      • Agents propose specific solutions");
    println!("      • You make final decisions");
    println!("\n   3. 🤖 View agent capabilities");
    println!("      • See what each agent specializes in");
    println!("      • Understand their different perspectives");
    println!("\n💡 Your vote is always decisive - agents propose, you decide!");
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // First show the simulation
    simulate_agent_collaboration()?;

    // Then show usage instructions
    show_cli_usage();

    Ok(())
}