Skip to main content

soma_core/cli/
edit_classification.rs

1// CLI interface for EditClassificationSystem
2// Provides interactive edit classification and filtering capabilities
3
4use crate::classification::edit_classifier_working::{
5    EditClassificationSystem, ClassifiedEdit
6};
7use crate::classification::types::EditCategory;
8use crate::edit_control::ModifiableEdit;
9use anyhow::Result;
10use crossterm::{
11    cursor, execute,
12    style::{Color, ResetColor, SetForegroundColor},
13    terminal::{Clear, ClearType},
14};
15use std::io::stdout;
16
17/// Interactive CLI for edit classification
18pub struct EditClassificationCLI {
19    classification_system: EditClassificationSystem,
20}
21
22impl EditClassificationCLI {
23    pub fn new() -> Result<Self> {
24        let classification_system = EditClassificationSystem::new();
25        Ok(Self { classification_system })
26    }
27    
28    /// Main interactive classification workflow
29    pub fn run_interactive_classification(&mut self, edits: Vec<ModifiableEdit>) -> Result<Vec<ClassifiedEdit>> {
30        self.display_header()?;
31        
32        println!("šŸ” Classifying {} edits with intelligent analysis...\n", edits.len());
33        
34        // Classify all edits
35        let classified_edits = self.classification_system.classify_batch(&edits)?;
36        
37        // Display results
38        self.display_classification_results(&classified_edits)?;
39        
40        Ok(classified_edits)
41    }
42    
43    /// Classify a single edit with detailed display
44    pub fn classify_single_edit(&mut self, edit: &ModifiableEdit) -> Result<ClassifiedEdit> {
45        self.display_header()?;
46        
47        println!("šŸ” Analyzing edit: {}", edit.base_edit.file);
48        
49        let classified = self.classification_system.classify_edit(edit)?;
50        
51        self.display_detailed_classification(&classified)?;
52        
53        Ok(classified)
54    }
55    
56    /// Display classification system configuration
57    pub fn show_configuration(&self) -> Result<()> {
58        self.display_header()?;
59        
60        println!("āš™ļø  Edit Classification System Configuration");
61        println!("===========================================\n");
62        
63        let config = self.classification_system.config();
64        
65        println!("🧠 Cognitive Analysis: {}", 
66            if config.use_cognitive_analysis { "āœ… Enabled" } else { "āŒ Disabled" });
67        println!("šŸŽÆ Auto-Classification Threshold: {:.1}%", 
68            config.auto_classification_threshold * 100.0);
69        println!("šŸ“¦ Max Batch Size: {}", 
70            config.max_batch_size);
71        println!("šŸ“ Verbose Logging: {}", 
72            if config.verbose_logging { "āœ… Enabled" } else { "āŒ Disabled" });
73        println!("āš ļø  Risk Strictness: {:.1}%", 
74            config.risk_strictness * 100.0);
75        
76        println!("\nšŸ“Š Pattern Database:");
77        println!("   • File patterns: Rust, TOML, Markdown, JSON, YAML");
78        println!("   • Security patterns: 9 patterns detected");
79        println!("   • Performance patterns: 8 patterns detected");
80        
81        println!("\nšŸ”¢ Scoring Algorithm:");
82        println!("   • Risk Weight: 40.0%");
83        println!("   • Complexity Weight: 30.0%");
84        println!("   • Size Weight: 20.0%");
85        println!("   • Cognitive Weight: 10.0%");
86        
87        Ok(())
88    }
89    
90    fn display_header(&self) -> Result<()> {
91        execute!(stdout(), Clear(ClearType::All), cursor::MoveTo(0, 0))?;
92        
93        println!("šŸš€ SOMA-CORE Edit Classification System");
94        println!("=====================================");
95        println!("Phase 3 Development - Smart Filtering & Priority\n");
96        
97        Ok(())
98    }
99    
100    fn display_classification_results(&self, classified_edits: &[ClassifiedEdit]) -> Result<()> {
101        println!("šŸ“Š Classification Results:");
102        println!("========================\n");
103        
104        // Display statistics
105        let stats = self.classification_system.get_classification_stats(classified_edits);
106        println!("šŸ“ˆ Statistics:");
107        println!("   • Total Edits: {}", stats.total_count);
108        println!("   • Average Risk: {:.2}", stats.average_risk);
109        println!("   • Average Priority: {:.2}", stats.average_priority);
110        println!("   • Average Confidence: {:.2}\n", stats.average_confidence);
111        
112        // Display each classified edit
113        for (i, classified) in classified_edits.iter().enumerate() {
114            self.display_classified_edit_summary(i + 1, classified)?;
115        }
116        
117        Ok(())
118    }
119    
120    fn display_classified_edit_summary(&self, index: usize, classified: &ClassifiedEdit) -> Result<()> {
121        let category_color = match classified.category {
122            EditCategory::Critical { .. } => Color::Red,
123            EditCategory::Experimental { .. } => Color::Yellow,
124            EditCategory::Safe { .. } => Color::Green,
125            EditCategory::Cosmetic { .. } => Color::Blue,
126        };
127        
128        execute!(stdout(), SetForegroundColor(category_color))?;
129        print!("{}. {}", index, classified.edit.base_edit.file);
130        execute!(stdout(), ResetColor)?;
131        
132        println!();
133        println!("   šŸ“‚ Category: {:?}", classified.category);
134        println!("   āš ļø  Risk: {:.2}", classified.risk_assessment.overall_score);
135        println!("   šŸŽÆ Priority: {:.2}", classified.priority_score);
136        println!("   šŸ”’ Confidence: {:.2}\n", classified.classification_confidence);
137        
138        Ok(())
139    }
140    
141    fn display_detailed_classification(&self, classified: &ClassifiedEdit) -> Result<()> {
142        println!("\nšŸ“‹ Detailed Classification Analysis:");
143        println!("===================================\n");
144        
145        println!("šŸ“‚ File: {}", classified.edit.base_edit.file);
146        
147        println!("\nšŸ“Š Classification:");
148        println!("   Category: {:?}", classified.category);
149        println!("   Priority Score: {:.3}", classified.priority_score);
150        println!("   Classification Confidence: {:.3}", classified.classification_confidence);
151        
152        println!("\nāš ļø  Risk Assessment:");
153        println!("   Overall Risk Score: {:.3}", classified.risk_assessment.overall_score);
154        println!("   Explanation: {}", classified.risk_assessment.explanation);
155        
156        if let Some(ref cognitive) = classified.cognitive_analysis {
157            println!("\n🧠 Cognitive Analysis:");
158            for (key, value) in cognitive.flatten() {
159                println!("   {}: {}", key, value);
160            }
161        }
162        
163        Ok(())
164    }
165}
166
167impl Default for EditClassificationCLI {
168    fn default() -> Self {
169        Self::new().expect("Failed to create EditClassificationCLI")
170    }
171}