cli_engineer 2.0.0

An autonomous CLI coding agent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
use anyhow::Result;
use clap::{Parser, ValueEnum};
use log::{error, info, warn, debug};
use std::sync::{Arc, Mutex};
use tokio::sync::oneshot;
use tokio::time::Duration;
use uuid::Uuid;
use walkdir::WalkDir;

use agentic_loop::AgenticLoop;
use artifact::ArtifactManager;
use config::Config;
use context::{ContextConfig, ContextManager};
use event_bus::{Event, EventBus, EventEmitter};
use llm_manager::{LLMManager, LLMProvider, LocalProvider};
use providers::{
    anthropic::AnthropicProvider, ollama::OllamaProvider, openai::OpenAIProvider, openrouter::OpenRouterProvider, gemini::GeminiProvider, xai::XAIProvider,
};
use ui_dashboard::DashboardUI;
use ui_enhanced::EnhancedUI;
use tool_manager::ToolManager;
mod logger_dashboard;

mod agentic_loop;
mod artifact;
mod command_executor;
mod concurrency;
mod config;
mod context;
mod event_bus;
mod executor;
mod interpreter;
mod iteration_context;
mod llm_manager;
mod logger;
mod planner;
mod providers;
mod reviewer;
mod mcp;
mod tool_manager;
mod ui_dashboard;
mod ui_enhanced;

#[derive(ValueEnum, Debug, Clone)]
enum CommandKind {
    #[clap(help = "Code generation")]
    Code,
    #[clap(help = "Refactoring")]
    Refactor,
    #[clap(help = "Code review")]
    Review,
    #[clap(help = "Documentation generation")]
    Docs,
    #[clap(help = "Security analysis")]
    Security,
}

#[derive(Parser, Debug)]
#[command(
    name = "cli_engineer",
    about = "Agentic CLI for software engineering automation",
    version = env!("CARGO_PKG_VERSION")
)]
struct Args {
    /// Enable verbose logging
    #[arg(short, long)]
    verbose: bool,
    /// Disable dashboard UI (use simple text output instead)
    #[arg(long)]
    no_dashboard: bool,
    /// Configuration file path
    #[arg(short, long)]
    config: Option<String>,
    /// Command to execute
    #[arg(value_enum)]
    command: CommandKind,
    /// Optional prompt describing the task
    #[arg(last = true)]
    prompt: Vec<String>,
}

#[tokio::main]
async fn main() -> Result<()> {
    // Load environment variables
    dotenv::dotenv().ok();

    // Parse command line arguments
    let args = Args::parse();

    // Create event bus
    let event_bus = Arc::new(EventBus::new(1000));

    // Initialize logger
    if !args.no_dashboard {
        let level = if args.verbose {
            log::LevelFilter::Info
        } else {
            log::LevelFilter::Warn
        };
        logger_dashboard::DashboardLogger::init_with_file(event_bus.clone(), level, args.verbose)
            .expect("Failed to init DashboardLogger");
    } else {
        if args.verbose {
            logger::init_with_file_logging(args.verbose);
        } else {
            logger::init(args.verbose);
        }
    }

    // Load configuration
    let config = Arc::new(Config::load(&args.config)?);

    let prompt = args.prompt.join(" ");

    if !args.no_dashboard {
        // Use dashboard UI when --no-dashboard is not specified
        let mut ui = DashboardUI::new(false);
        ui.set_event_bus(event_bus.clone());

        // Start UI
        ui.start()?;

        if matches!(args.command, CommandKind::Code) && prompt.is_empty() {
            ui.display_error("PROMPT required for code command")?;
            ui.finish()?;
            return Ok(());
        }

        let ui_ref = Arc::new(Mutex::new(ui));
        let ui_clone = ui_ref.clone();
        let (stop_tx, mut stop_rx) = oneshot::channel();

        // Start periodic UI updates
        let handle = tokio::spawn(async move {
            let mut interval = tokio::time::interval(Duration::from_millis(100));
            loop {
                tokio::select! {
                    _ = interval.tick() => {
                        if let Ok(mut ui_guard) = ui_clone.try_lock() {
                            let _ = ui_guard.throttled_render();
                        }
                    }
                    _ = &mut stop_rx => break,
                }
            }
        });

        let result = match args.command {
            CommandKind::Code => run_with_ui(prompt.clone(), config.clone(), event_bus.clone(), true, args.command).await,
            CommandKind::Refactor => {
                let p = if prompt.is_empty() {
                    "Analyze the current directory and perform recommended refactoring.".to_string()
                } else {
                    prompt.clone()
                };
                run_with_ui(
                    format!("Refactor codebase. {}", p),
                    config.clone(),
                    event_bus.clone(),
                    true,
                    args.command,
                )
                .await
            }
            CommandKind::Review => {
                let p = if prompt.is_empty() {
                    "ANALYSIS ONLY: Review the codebase files and create a comprehensive code review report. DO NOT generate, modify, or create any source code files. ONLY analyze existing code and document your findings, suggestions, and recommendations in code_review.md. Focus on code quality, best practices, potential issues, and improvement opportunities.".to_string()
                } else {
                    format!("ANALYSIS ONLY: Review the codebase with focus on: {}. DO NOT generate, modify, or create any source code files. ONLY analyze existing code and document your findings in code_review.md", prompt)
                };
                run_with_ui(p, config.clone(), event_bus.clone(), true, args.command).await
            }
            CommandKind::Docs => {
                let p = if prompt.is_empty() {
                    "Generate comprehensive documentation for the codebase. Create documentation files in a docs/ directory.".to_string()
                } else {
                    format!("Generate documentation for the codebase with these instructions: {}. Create documentation files in a docs/ directory.", prompt)
                };
                run_with_ui(p, config.clone(), event_bus.clone(), true, args.command).await
            }
            CommandKind::Security => {
                let p = if prompt.is_empty() {
                    "SECURITY ANALYSIS ONLY: Perform a comprehensive security analysis of the codebase. DO NOT generate, modify, or create any source code files. ONLY analyze existing code for vulnerabilities, security issues, and best practice violations. Document your findings, risk assessments, and security recommendations in security_report.md.".to_string()
                } else {
                    format!("SECURITY ANALYSIS ONLY: Perform a security analysis of the codebase focusing on: {}. DO NOT generate, modify, or create any source code files. ONLY analyze existing code and document your security findings in security_report.md", prompt)
                };
                run_with_ui(p, config.clone(), event_bus.clone(), true, args.command).await
            }
        };

        match result {
            Ok(_) => {
                let _ = stop_tx.send(());
                let _ = handle.await;
                if let Ok(mut ui_guard) = ui_ref.try_lock() {
                    ui_guard.finish()?;
                }
            }
            Err(e) => {
                let _ = stop_tx.send(());
                let _ = handle.await;
                if let Ok(mut ui_guard) = ui_ref.try_lock() {
                    ui_guard.display_error(&format!("{}", e))?;
                    ui_guard.finish()?;
                }
                return Err(e);
            }
        }
    } else {
        // Use simple text UI when --no-dashboard is specified
        let mut ui = if config.ui.colorful && config.ui.progress_bars && args.verbose {
            EnhancedUI::new(false)
        } else {
            EnhancedUI::new(true) // headless mode
        };
        ui.set_event_bus(event_bus.clone());

        // Start UI
        ui.start()?;

        if matches!(args.command, CommandKind::Code) && prompt.is_empty() {
            ui.display_error("PROMPT required for code command").await?;
            ui.finish();
            return Ok(());
        }

        let result = match args.command {
            CommandKind::Code => run_with_ui(prompt.clone(), config.clone(), event_bus.clone(), true, args.command).await,
            CommandKind::Refactor => {
                let p = if prompt.is_empty() {
                    "Analyze the current directory and perform recommended refactoring.".to_string()
                } else {
                    prompt.clone()
                };
                run_with_ui(
                    format!("Refactor codebase. {}", p),
                    config.clone(),
                    event_bus.clone(),
                    true,
                    args.command,
                )
                .await
            }
            CommandKind::Review => {
                let p = if prompt.is_empty() {
                    "ANALYSIS ONLY: Review the codebase files and create a comprehensive code review report. DO NOT generate, modify, or create any source code files. ONLY analyze existing code and document your findings, suggestions, and recommendations in code_review.md. Focus on code quality, best practices, potential issues, and improvement opportunities.".to_string()
                } else {
                    format!("ANALYSIS ONLY: Review the codebase with focus on: {}. DO NOT generate, modify, or create any source code files. ONLY analyze existing code and document your findings in code_review.md", prompt)
                };
                run_with_ui(p, config.clone(), event_bus.clone(), true, args.command).await
            }
            CommandKind::Docs => {
                let p = if prompt.is_empty() {
                    "Generate comprehensive documentation for the codebase. Create documentation files in a docs/ directory.".to_string()
                } else {
                    format!("Generate documentation for the codebase with these instructions: {}. Create documentation files in a docs/ directory.", prompt)
                };
                run_with_ui(p, config.clone(), event_bus.clone(), true, args.command).await
            }
            CommandKind::Security => {
                let p = if prompt.is_empty() {
                    "SECURITY ANALYSIS ONLY: Perform a comprehensive security analysis of the codebase. DO NOT generate, modify, or create any source code files. ONLY analyze existing code for vulnerabilities, security issues, and best practice violations. Document your findings, risk assessments, and security recommendations in security_report.md.".to_string()
                } else {
                    format!("SECURITY ANALYSIS ONLY: Perform a security analysis of the codebase focusing on: {}. DO NOT generate, modify, or create any source code files. ONLY analyze existing code and document your security findings in security_report.md", prompt)
                };
                run_with_ui(p, config.clone(), event_bus.clone(), true, args.command).await
            }
        };

        match result {
            Ok(_) => ui.finish(),
            Err(e) => {
                ui.display_error(&format!("{}", e)).await?;
                ui.finish();
                return Err(e);
            }
        }
    }

    Ok(())
}

async fn scan_and_populate_context(
    context_manager: &ContextManager,
    context_id: &str,
    event_bus: Arc<EventBus>,
) -> Result<(usize, String)> {
    let _ = event_bus
        .emit(Event::LogLine {
            level: "INFO".to_string(),
            message: "Scanning codebase for context...".to_string(),
        })
        .await;

    let mut file_count = 0;
    let mut file_list = Vec::new();
    let current_dir = std::env::current_dir()?;
    
    // Define extensions to scan
    let code_extensions = vec![
        "rs", "py", "js", "ts", "java", "c", "cpp", "h", "hpp", "go", 
        "rb", "php", "swift", "kt", "scala", "sh", "bash", "yaml", "yml",
        "json", "toml", "xml", "html", "css", "jsx", "tsx", "vue", "svelte", "md"
    ];
    
    let config_files = vec![
        "Cargo.toml", "package.json", "pom.xml", "build.gradle", 
        "requirements.txt", "setup.py", "Gemfile", "composer.json",
        "Makefile", "Dockerfile", ".gitignore", "README.md", "README"
    ];

    // Scan for code files
    for entry in WalkDir::new(&current_dir)
        .max_depth(5)
        .into_iter()
        .filter_entry(|e| {
            let name = e.file_name().to_string_lossy();
            !name.starts_with('.') && 
            name != "target" && 
            name != "node_modules" && 
            name != "venv" &&
            name != "dist" &&
            name != "build"
        })
    {
        let entry = entry?;
        let path = entry.path();
        
        if path.is_file() {
            let file_name = path.file_name().unwrap().to_string_lossy();
            let ext = path.extension()
                .and_then(|e| e.to_str())
                .unwrap_or("");
            
            // Check if it's a code file or config file
            let should_include = code_extensions.contains(&ext) || 
                                config_files.iter().any(|&cf| file_name == cf);
            
            if should_include {
                // Skip very large files
                let metadata = std::fs::metadata(&path)?;
                if metadata.len() > 100_000 {
                    info!("Skipping large file {:?} ({}KB)", path, metadata.len() / 1024);
                    continue;
                }
                
                match std::fs::read_to_string(&path) {
                    Ok(content) => {
                        let relative_path = path.strip_prefix(&current_dir)
                            .unwrap_or(path)
                            .to_string_lossy();
                        
                        let file_info = format!(
                            "File: {}\n```{}\n{}\n```",
                            relative_path,
                            ext.to_string(),
                            content
                        );
                        
                        context_manager
                            .add_message(context_id, "system".to_string(), file_info)
                            .await?;
                        
                        file_count += 1;
                        file_list.push(relative_path.to_string());
                        info!("Added {} to context ({} bytes)", relative_path, content.len());
                    }
                    Err(e) => {
                        warn!("Failed to read {:?}: {}", path, e);
                    }
                }
            }
        }
    }

    event_bus
        .emit(Event::LogLine {
            level: "INFO".to_string(),
            message: format!("Scanning complete. Added {} files to context", file_count),
        })
        .await?;
    
    info!("Scan complete: added {} files to context", file_count);
    
    // Create a summary of what was scanned
    let file_summary = if file_count > 0 {
        format!("\n\nThe following {} files from this codebase have been loaded into context:\n{}", 
                file_count, 
                file_list.join("\n"))
    } else {
        String::new()
    };
    
    Ok((file_count, file_summary))
}

async fn run_with_ui(prompt: String, config: Arc<Config>, event_bus: Arc<EventBus>, scan_codebase: bool, command: CommandKind) -> Result<()> {
    let (llm_manager, artifact_manager, context_manager, tool_manager) =
        setup_managers(&*config, event_bus.clone()).await?;

    let task_id = Uuid::new_v4().to_string();
    event_bus
        .emit(Event::TaskStarted {
            task_id: task_id.clone(),
            description: prompt.clone(),
        })
        .await?;
    info!("Emitting TaskStarted event for task: {}", prompt);

    // Create and run agentic loop
    let agentic_loop = AgenticLoop::new(
        llm_manager.clone(),
        config.execution.max_iterations,
        event_bus.clone(),
    )
    .with_context_manager(context_manager.clone())
    .with_config(config.clone())
    .with_artifact_manager(artifact_manager.clone())
    .with_tool_manager(tool_manager.clone())
    .with_command(command);
    info!("AgenticLoop instance created.");
    let ctx_id = context_manager
        .create_context(std::collections::HashMap::new())
        .await;
    info!("Context created. Running agentic loop...");

    // Emit execution started event
    event_bus
        .emit(Event::LogLine {
            level: "INFO".to_string(),
            message: "Execution started".to_string(),
        })
        .await?;

    // Scan and populate context if requested
    let mut enhanced_prompt = prompt;
    if scan_codebase {
        let (file_count, file_summary) = scan_and_populate_context(&context_manager, &ctx_id, event_bus.clone()).await?;
        if file_count > 0 {
            // Append file summary to the prompt so the planner knows what files exist
            enhanced_prompt = format!("{}{}", enhanced_prompt, file_summary);
        }
    }

    let result = agentic_loop.run(&enhanced_prompt, &ctx_id).await;
    info!("Agentic loop completed");

    match result {
        Ok(_) => {
            info!("Task completed successfully");
            event_bus
                .emit(Event::TaskCompleted {
                    task_id: task_id.clone(),
                    result: "Success".to_string(),
                })
                .await?;
        }
        Err(ref e) => {
            error!("Task failed: {}", e);
            event_bus
                .emit(Event::TaskFailed {
                    task_id,
                    error: e.to_string(),
                })
                .await?;
        }
    }

    // Cleanup artifacts if configured
    if config.execution.cleanup_on_exit {
        info!("Cleaning up artifacts...");
        artifact_manager.cleanup().await?;
    }

    result.map(|_| ())
}

async fn setup_managers(
    config: &Config,
    event_bus: Arc<EventBus>,
) -> Result<(Arc<LLMManager>, Arc<ArtifactManager>, Arc<ContextManager>, Arc<ToolManager>)> {
    // Initialize artifact manager
    let mut artifact_manager =
        ArtifactManager::new(std::env::current_dir()?.join(&config.execution.artifact_dir))?;
    artifact_manager.set_event_bus(event_bus.clone());
    let artifact_manager = Arc::new(artifact_manager);

    // Initialize context manager
    let context_config = ContextConfig {
        max_tokens: config.context.max_tokens,
        compression_threshold: config.context.compression_threshold,
        cache_enabled: config.context.cache_enabled,
        cache_dir: std::env::current_dir()?
            .join(".cli_engineer")
            .join("context_cache"),
    };

    let mut context_manager = ContextManager::new(context_config)?;
    context_manager.set_event_bus(event_bus.clone());

    // Initialize providers
    let mut providers: Vec<Box<dyn LLMProvider>> = Vec::new();

    if let Some(openrouter_config) = &config.ai_providers.openrouter {
        if openrouter_config.enabled {
            match OpenRouterProvider::new(
                Some(openrouter_config.model.clone()),
                openrouter_config.temperature,
                openrouter_config.max_tokens,
            ) {
                Ok(provider) => {
                    info!("OpenRouter provider initialized successfully");
                    providers.push(Box::new(provider));
                }
                Err(e) => {
                    warn!("Failed to initialize OpenRouter provider: {}. Skipping.", e);
                }
            }
        }
    }

    if let Some(gemini_config) = &config.ai_providers.gemini {
        if gemini_config.enabled {
            match GeminiProvider::new(
                Some(gemini_config.model.clone()),
                gemini_config.temperature,
                gemini_config.cost_per_1m_input_tokens,
                gemini_config.cost_per_1m_output_tokens,
                Some(event_bus.clone()),
            ) {
                Ok(provider) => {
                    info!("Gemini provider initialized successfully");
                    providers.push(Box::new(provider));
                }
                Err(e) => {
                    warn!("Failed to initialize Gemini provider: {}. Skipping.", e);
                }
            }
        }
    }

    if let Some(xai_config) = &config.ai_providers.xai {
        if xai_config.enabled {
            match XAIProvider::new(
                Some(xai_config.model.clone()),
                xai_config.temperature,
            ) {
                Ok(provider) => {
                    info!("xAI provider initialized successfully");
                    providers.push(Box::new(provider
                        .with_event_bus(event_bus.clone())
                        .with_cost_per_1m_input_tokens(xai_config.cost_per_1m_input_tokens.unwrap_or(0.0))
                        .with_cost_per_1m_output_tokens(xai_config.cost_per_1m_output_tokens.unwrap_or(0.0))));
                }
                Err(e) => {
                    warn!("Failed to initialize xAI provider: {}. Skipping.", e);
                }
            }
        }
    }

    if let Some(openai_config) = &config.ai_providers.openai {
        debug!("Found OpenAI config: enabled={}, model={}", openai_config.enabled, openai_config.model);
        if openai_config.enabled {
            debug!("OpenAI provider is enabled, initializing...");
            match OpenAIProvider::new(
                Some(openai_config.model.clone()),
                openai_config.temperature,
            ) {
                Ok(provider) => {
                    info!("OpenAI provider initialized successfully");
                    providers.push(Box::new(provider
                        .with_event_bus(event_bus.clone())
                        .with_cost_per_1m_input_tokens(openai_config.cost_per_1m_input_tokens.unwrap_or(0.0))
                        .with_cost_per_1m_output_tokens(openai_config.cost_per_1m_output_tokens.unwrap_or(0.0))));
                }
                Err(e) => {
                    warn!("Failed to initialize OpenAI provider: {}. Skipping.", e);
                }
            }
        } else {
            debug!("OpenAI provider is disabled in config");
        }
    } else {
        debug!("No OpenAI config found");
    }

    if let Some(anthropic_config) = &config.ai_providers.anthropic {
        debug!("Found Anthropic config: enabled={}, model={}", anthropic_config.enabled, anthropic_config.model);
        if anthropic_config.enabled {
            debug!("Anthropic provider is enabled, checking API key...");
            if let Ok(api_key) = std::env::var("ANTHROPIC_API_KEY") {
                debug!("API key found, initializing Anthropic provider");
                let provider = AnthropicProvider::new(
                    api_key,
                    anthropic_config.model.clone(),
                    anthropic_config.temperature.unwrap_or(0.7),
                    anthropic_config.cost_per_1m_input_tokens.unwrap_or(3.0),
                    anthropic_config.cost_per_1m_output_tokens.unwrap_or(15.0),
                    Some(event_bus.clone()),
                );
                info!("Anthropic provider initialized successfully");
                providers.push(Box::new(provider));
            } else {
                warn!("ANTHROPIC_API_KEY environment variable not set. Skipping Anthropic provider.");
            }
        } else {
            debug!("Anthropic provider is disabled in config");
        }
    } else {
        debug!("No Anthropic config found");
    }

    if let Some(ollama_config) = &config.ai_providers.ollama {
        if ollama_config.enabled {
            match OllamaProvider::new(
                Some(ollama_config.model.clone()),
                ollama_config.temperature,
                ollama_config.max_tokens,
                Some(event_bus.clone()),
            ) {
                Ok(provider) => {
                    info!("Ollama provider initialized successfully");
                    providers.push(Box::new(provider));
                }
                Err(e) => {
                    warn!("Failed to initialize Ollama provider: {}. Skipping.", e);
                }
            }
        }
    }

    if providers.is_empty() {
        error!("No AI providers configured, using LocalProvider");
        providers.push(Box::new(LocalProvider));
    }

    let llm_manager = Arc::new(LLMManager::new(
        providers,
        event_bus.clone(),
        Arc::new(config.clone()),
    ));
    context_manager.set_llm_manager(llm_manager.clone());
    let context_manager = Arc::new(context_manager);

    // Initialize tool manager
    let tool_manager = Arc::new(ToolManager::from_config(config).await?);
    info!("ToolManager initialized with {} tools", tool_manager.tool_names().len());

    Ok((llm_manager, artifact_manager, context_manager, tool_manager))
}