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
mod agents;
mod dag;
mod feedback_loop;
mod memory;
mod types;

use agents::claude_agent::ClaudeAgent;
use agents::gemini_agent::GeminiAgent;
use agents::gpt4_agent::{CognitiveAgent, GPT4Agent};
use feedback_loop::run_feedback_loop;
use memory::SymbolicContext;

fn main() {
    println!("🧠 SOMA Core - Multi-Agent Code Editing Demo");
    println!("===========================================\n");

    // Test 1: Basic agent connection
    let agents = dag::connect_agents();
    println!("✅ Connected Cognitive Agents:");
    for agent in agents {
        println!("   - {:?}: {:?}", agent.id, agent.capabilities);
    }
    println!();

    // Test 2: Topology demonstration
    let nodes = vec![
        dag::Node {
            id: "n1".into(),
            agent: Some(types::AgentKind::Claude),
        },
        dag::Node {
            id: "n2".into(),
            agent: Some(types::AgentKind::Gemini),
        },
        dag::Node {
            id: "n3".into(),
            agent: None,
        },
    ];
    println!("📊 Node Topology Analysis:");
    dag::print_node_topologies(&nodes);
    println!();

    // Test 3: Agent Code Editing Demo
    println!("🤖 Agent Code Editing Demonstrations:");
    println!("=====================================\n");

    demo_agent_edits();

    // Test 4: Multi-agent consensus system
    println!("\n🔄 Multi-Agent Consensus System:");
    println!("=================================");

    demo_consensus_system();
}

fn demo_agent_edits() {
    let agents: Vec<Box<dyn CognitiveAgent>> = vec![
        Box::new(GPT4Agent),
        Box::new(ClaudeAgent),
        Box::new(GeminiAgent),
    ];

    // Test different task contexts
    let contexts = vec![
        ("performance", "Performance optimization task"),
        ("readability", "Code readability improvement task"),
        ("testing", "Testing and validation task"),
        ("safety", "Memory safety improvement task"),
        ("architecture", "Architectural design task"),
        ("validation", "Input validation task"),
    ];

    for (task_type, description) in contexts {
        println!("📝 Task: {}", description);

        let mut ctx = SymbolicContext::new();
        ctx.set("current_task", task_type);

        for (i, agent) in agents.iter().enumerate() {
            let agent_name = match i {
                0 => "GPT-4",
                1 => "Claude",
                2 => "Gemini",
                _ => "Unknown",
            };

            let edit = agent.propose_edit(&ctx);
            println!("   🔧 {} suggests:", agent_name);
            println!("      File: {}", edit.file);
            println!("      Lines: {:?}", edit.line_range);
            println!("      Reason: {}", edit.reason);
            println!("      Confidence: {:.1}%", edit.confidence * 100.0);
            println!(
                "      Code Preview: {}",
                edit.new_code
                    .lines()
                    .next()
                    .unwrap_or("")
                    .chars()
                    .take(60)
                    .collect::<String>()
            );
            if edit.new_code.len() > 60 {
                println!(
                    "        ... (+ {} more characters)",
                    edit.new_code.len() - 60
                );
            }
            println!();
        }
        println!("   ──────────────────────────────────────");
    }
}

fn demo_consensus_system() {
    println!("Testing consensus-based code editing...\n");

    // Scenario 1: Performance task where GPT-4 and Claude might agree
    let mut ctx = SymbolicContext::new();
    ctx.set("current_task", "performance optimization for critical path");
    ctx.set("target_file", "src/ops.rs");
    ctx.set("focus_area", "loop optimization");

    println!("📋 Scenario: Performance optimization consensus");
    println!("Context: {}", ctx.resolve_or_default("current_task", ""));

    // Run the full feedback loop
    run_feedback_loop(&ctx, &[]);

    // Display results
    if std::path::Path::new("meta_log.json").exists() {
        let log_content = std::fs::read_to_string("meta_log.json").unwrap();
        let log: serde_json::Value = serde_json::from_str(&log_content).unwrap();

        println!("\n📊 Agent Proposals:");
        if let Some(proposals) = log["proposals"].as_array() {
            for (i, proposal) in proposals.iter().enumerate() {
                let agent_name = match i {
                    0 => "GPT-4",
                    1 => "Claude",
                    2 => "Gemini",
                    _ => "Unknown",
                };
                println!(
                    "   {}{} (confidence: {}%)",
                    agent_name,
                    proposal["reason"].as_str().unwrap_or(""),
                    (proposal["confidence"].as_f64().unwrap_or(0.0) * 100.0) as i32
                );
            }
        }

        println!("\n🎯 Consensus Results:");
        if let Some(merged) = log["merged"].as_array() {
            if merged.is_empty() {
                println!("   ❌ No consensus reached - all agents proposed different edits");
            } else {
                println!("{} edit(s) reached consensus", merged.len());
                for edit in merged {
                    println!(
                        "      📁 {}: {}",
                        edit["file"].as_str().unwrap_or(""),
                        edit["reason"].as_str().unwrap_or("")
                    );
                }
            }
        }

        // Clean up
        std::fs::remove_file("meta_log.json").ok();
    }

    println!(
        "\n💡 Note: The consensus system requires 2+ agents to agree on the same file/line range"
    );
    println!(
        "    Each agent has different specializations, so consensus indicates high-value changes!"
    );
}