kandil_code 2.1.1

Intelligent development platform (CLI + TUI + Multi-Agent System) with cross-platform AI model benchmarking, system diagnostics, and advanced development tools
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
# 📄 PHASE_3_TUI_CODE.md

```markdown
# Phase 3: TUI Studio & Code Understanding

## Objectives
Build an interactive Terminal UI (TUI) using Ratatui for visual project management, and integrate Tree-sitter for code analysis. Enable `kandil tui` to launch a functional studio with file navigation, code preview, and AI chat panel.

## Prerequisites
- Phase 2 complete (templates and plugin system)
- Ollama running with llama3:70b
- Terminal that supports Unicode (iTerm2, Windows Terminal, GNOME Terminal)
- Basic understanding of terminal UI concepts

## Detailed Sub-Tasks

### Day 1-2: TUI Foundation & Layout

1. **Add Dependencies**
```bash
cargo add ratatui --features all-widgets
cargo add crossterm --features event-stream
cargo add syntect # Syntax highlighting
cargo add tree-sitter
cargo add tree-sitter-dart tree-sitter-python tree-sitter-javascript tree-sitter-rust
cargo add tui-textarea # For chat input
```

2. **Create TUI Application Structure**
```rust
// src/tui/mod.rs
pub mod studio;
pub mod widgets; // Custom widgets
pub mod events;  // Event handling

pub use studio::StudioApp;

// src/tui/events.rs
use crossterm::event::{self, Event, KeyCode, KeyModifiers};
use std::time::Duration;
use anyhow::Result;

pub enum AppEvent {
    Key(crossterm::event::KeyEvent),
    Mouse(crossterm::event::MouseEvent),
    Tick,
}

pub struct EventHandler {
    tick_rate: Duration,
}

impl EventHandler {
    pub fn new(tick_rate_ms: u64) -> Self {
        Self {
            tick_rate: Duration::from_millis(tick_rate_ms),
        }
    }
    
    pub async fn next(&self) -> Result<AppEvent> {
        if event::poll(self.tick_rate)? {
            match event::read()? {
                Event::Key(key) => {
                    // Handle Ctrl+C
                    if key.code == KeyCode::Char('c') 
                        && key.modifiers.contains(KeyModifiers::CONTROL) {
                        return Ok(AppEvent::Key(key));
                    }
                    Ok(AppEvent::Key(key))
                },
                Event::Mouse(mouse) => Ok(AppEvent::Mouse(mouse)),
                _ => self.next().await,
            }
        } else {
            Ok(AppEvent::Tick)
        }
    }
}
```

3. **Main Studio Layout**
```rust
// src/tui/studio.rs
use ratatui::backend::Backend;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::widgets::{Block, Borders, List, ListItem, Paragraph};
use ratatui::Frame;
use crossterm::event::KeyCode;
use anyhow::Result;

pub enum Panel {
    FileTree,
    CodePreview,
    Chat,
    AiPanel,
}

pub struct StudioApp {
    pub file_tree: Vec<String>,
    pub selected_file: usize,
    pub code_content: String,
    pub chat_history: Vec<(String, String)>, // (sender, message)
    pub chat_input: String,
    pub active_panel: Panel,
    pub workspace: crate::core::workspace::Workspace,
    pub should_quit: bool,
}

impl StudioApp {
    pub fn new() -> Result<Self> {
        let workspace = crate::core::workspace::Workspace::detect()?;
        
        Ok(Self {
            file_tree: Self::load_files(&workspace)?,
            selected_file: 0,
            code_content: String::new(),
            chat_history: vec![],
            chat_input: String::new(),
            active_panel: Panel::FileTree,
            workspace,
            should_quit: false,
        })
    }
    
    fn load_files(workspace: &crate::core::workspace::Workspace) -> Result<Vec<String>> {
        let mut files = Vec::new();
        
        // Only load first level for performance
        if let Ok(entries) = std::fs::read_dir(&workspace.root) {
            for entry in entries.flatten().take(100) {
                if let Some(name) = entry.file_name().to_str() {
                    files.push(name.to_string());
                }
            }
        }
        
        Ok(files)
    }
    
    pub async fn run<B: Backend>(mut self, terminal: &mut ratatui::Terminal<B>) -> Result<()> {
        use crate::tui::events::{EventHandler, AppEvent};
        
        let events = EventHandler::new(250); // 250ms tick rate
        
        while !self.should_quit {
            terminal.draw(|f| self.draw(f))?;
            
            match events.next().await? {
                AppEvent::Key(key) => self.handle_key(key).await?,
                AppEvent::Tick => self.on_tick().await?,
                _ => {}
            }
        }
        
        Ok(())
    }
    
    fn draw(&mut self, f: &mut Frame) {
        let chunks = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([
                Constraint::Percentage(25),  // File tree
                Constraint::Percentage(50),  // Code preview
                Constraint::Percentage(25),  // AI/Chat
            ])
            .split(f.size());
        
        self.draw_file_tree(f, chunks[0]);
        self.draw_code_preview(f, chunks[1]);
        self.draw_chat(f, chunks[2]);
    }
    
    fn draw_file_tree(&self, f: &mut Frame, area: Rect) {
        let items: Vec<ListItem> = self.file_tree
            .iter()
            .enumerate()
            .map(|(i, name)| {
                let style = if i == self.selected_file {
                    Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD)
                } else {
                    Style::default()
                };
                ListItem::new(name.as_str()).style(style)
            })
            .collect();
        
        let list = List::new(items)
            .block(Block::default()
                .borders(Borders::ALL)
                .title("📁 Files")
                .border_style(Style::default().fg(Color::Cyan)));
        
        f.render_widget(list, area);
    }
    
    fn draw_code_preview(&self, f: &mut Frame, area: Rect) {
        let block = Block::default()
            .borders(Borders::ALL)
            .title("📝 Code Preview")
            .border_style(Style::default().fg(Color::Green));
        
        let content = if self.code_content.is_empty() {
            "Select a file to preview".to_string()
        } else {
            self.code_content.clone()
        };
        
        let paragraph = Paragraph::new(content)
            .block(block)
            .wrap(ratatui::widgets::Wrap { trim: true });
        
        f.render_widget(paragraph, area);
    }
    
    fn draw_chat(&self, f: &mut Frame, area: Rect) {
        let block = Block::default()
            .borders(Borders::ALL)
            .title("💬 AI Assistant")
            .border_style(Style::default().fg(Color::Blue));
        
        let chat_text = self.chat_history
            .iter()
            .map(|(sender, msg)| format!("{}: {}\n", sender, msg))
            .collect::<String>();
        
        let paragraph = Paragraph::new(chat_text)
            .block(block);
        
        f.render_widget(paragraph, area);
    }
    
    async fn handle_key(&mut self, key: crossterm::event::KeyEvent) -> Result<()> {
        match key.code {
            KeyCode::Char('q') if key.modifiers.contains(crossterm::event::KeyModifiers::CONTROL) => {
                self.should_quit = true;
            }
            KeyCode::Up => self.navigate_up(),
            KeyCode::Down => self.navigate_down(),
            KeyCode::Enter => self.select_file().await?,
            KeyCode::Char('c') if matches!(self.active_panel, Panel::Chat) => {
                self.send_chat_message().await?;
            }
            KeyCode::Char(c) if matches!(self.active_panel, Panel::Chat) => {
                self.chat_input.push(c);
            }
            KeyCode::Backspace if matches!(self.active_panel, Panel::Chat) => {
                self.chat_input.pop();
            }
            _ => {}
        }
        Ok(())
    }
    
    fn navigate_up(&mut self) {
        if self.selected_file > 0 {
            self.selected_file -= 1;
        }
    }
    
    fn navigate_down(&mut self) {
        if self.selected_file < self.file_tree.len().saturating_sub(1) {
            self.selected_file += 1;
        }
    }
    
    async fn select_file(&mut self) -> Result<()> {
        if let Some(file_name) = self.file_tree.get(self.selected_file) {
            let path = format!("{}/{}", self.workspace.root, file_name);
            if std::path::Path::new(&path).is_file() {
                self.code_content = tokio::fs::read_to_string(&path).await?;
                
                // Auto-detect language and analyze
                let ext = file_name.split('.').last().unwrap_or("");
                self.analyze_code(&self.code_content, ext).await?;
            }
        }
        Ok(())
    }
    
    async fn analyze_code(&mut self, code: &str, ext: &str) -> Result<()> {
        use crate::adapters::ai::factory::AIProviderFactory;
        use crate::utils::config::Config;
        
        let config = Config::load()?;
        let factory = AIProviderFactory::new(config.ai);
        let ai = factory.create().await?;
        
        let prompt = format!(
            "Analyze this {} code for issues and best practices:\n{}",
            ext, &code[..code.len().min(2000)] // Limit to avoid token overflow
        );
        
        let analysis = ai.chat(&prompt, None).await?;
        self.chat_history.push(("AI Analyst".to_string(), analysis));
        
        Ok(())
    }
    
    async fn send_chat_message(&mut self) -> Result<()> {
        if !self.chat_input.is_empty() {
            let message = self.chat_input.clone();
            self.chat_history.push(("You".to_string(), message.clone()));
            self.chat_input.clear();
            
            // Send to AI
            use crate::adapters::ai::factory::AIProviderFactory;
            use crate::utils::config::Config;
            
            let config = Config::load()?;
            let factory = AIProviderFactory::new(config.ai);
            let ai = factory.create().await?;
            
            let response = ai.chat(&message, None).await?;
            self.chat_history.push(("AI".to_string(), response));
        }
        Ok(())
    }
    
    async fn on_tick(&mut self) -> Result<()> {
        // Background tasks, if any
        Ok(())
    }
}
```

### Day 3-4: Code Analyzer with Tree-sitter

1. **Language Detection & Parser Pool**
```rust
// src/code/analyzer.rs
use tree_sitter::{Parser, Language, Node};
use anyhow::Result;
use once_cell::sync::Lazy;

static PARSERS: Lazy<std::sync::Mutex<Vec<(String, Parser)>>> = Lazy::new(|| {
    std::sync::Mutex::new(Vec::new())
});

pub struct CodeAnalyzer {
    language: Option<Language>,
    extension: String,
}

impl CodeAnalyzer {
    pub fn new(extension: &str) -> Result<Self> {
        let language = match extension {
            "dart" => Some(unsafe { tree_sitter_dart() }),
            "py" => Some(unsafe { tree_sitter_python() }),
            "js" | "ts" => Some(unsafe { tree_sitter_javascript() }),
            "rs" => Some(unsafe { tree_sitter_rust() }),
            _ => None,
        };
        
        Ok(Self {
            language,
            extension: extension.to_string(),
        })
    }
    
    pub fn analyze_syntax(&mut self, code: &str) -> Result<SyntaxAnalysis> {
        let mut analysis = SyntaxAnalysis::default();
        
        if let Some(lang) = self.language {
            let mut parser = Parser::new();
            parser.set_language(lang)?;
            
            let tree = parser.parse(code, None)
                .ok_or_else(|| anyhow::anyhow!("Failed to parse code"))?;
            
            self.walk_tree(tree.root_node(), &mut analysis);
        }
        
        Ok(analysis)
    }
    
    fn walk_tree(&self, node: Node, analysis: &mut SyntaxAnalysis) {
        analysis.total_nodes += 1;
        
        match node.kind() {
            "function" | "function_definition" | "method" => {
                analysis.functions += 1;
            }
            "class" | "class_definition" => {
                analysis.classes += 1;
            }
            "comment" => {
                analysis.comments += 1;
            }
            "import" | "import_statement" | "use" => {
                analysis.imports += 1;
            }
            _ => {}
        }
        
        for child in node.children(&mut node.walk()) {
            self.walk_tree(child, analysis);
        }
    }
}

#[derive(Debug, Default)]
pub struct SyntaxAnalysis {
    pub total_nodes: usize,
    pub functions: usize,
    pub classes: usize,
    pub comments: usize,
    pub imports: usize,
}

impl SyntaxAnalysis {
    pub fn complexity_score(&self) -> f64 {
        // Simple cyclomatic complexity approximation
        (self.functions * 2 + self.classes * 3 + self.imports) as f64 / 
        (self.comments + 1) as f64
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_analyze_dart_simple() {
        let code = r#"
        void main() {
          print('Hello');
        }
        "#;
        
        let mut analyzer = CodeAnalyzer::new("dart").unwrap();
        let analysis = analyzer.analyze_syntax(code).unwrap();
        
        assert!(analysis.functions >= 1);
    }
}
```

2. **Syntax Highlighting with Syntect**
```rust
// src/tui/syntax.rs
use syntect::easy::HighlightLines;
use syntect::highlighting::{Style, ThemeSet};
use syntect::parsing::SyntaxSet;
use syntect::util::{as_24_bit_terminal_escaped, LinesWithEndings};
use once_cell::sync::Lazy;

static SYNTAX_SET: Lazy<SyntaxSet> = Lazy::new(SyntaxSet::load_defaults_newlines);
static THEME_SET: Lazy<ThemeSet> = Lazy::new(ThemeSet::load_defaults);

pub struct SyntaxHighlighter {
    syntax_set: &'static SyntaxSet,
    theme: &'static ThemeSet,
}

impl SyntaxHighlighter {
    pub fn new() -> Self {
        Self {
            syntax_set: &SYNTAX_SET,
            theme: &THEME_SET,
        }
    }
    
    pub fn highlight(&self, code: &str, ext: &str) -> Vec<String> {
        let syntax = self.syntax_set.find_syntax_by_extension(ext)
            .unwrap_or_else(|| self.syntax_set.find_syntax_plain_text());
        
        let theme = &self.theme.themes["base16-ocean.dark"];
        let mut highlighter = HighlightLines::new(syntax, theme);
        
        let mut lines = Vec::new();
        for line in LinesWithEndings::from(code) {
            let ranges: Vec<(Style, &str)> = highlighter.highlight_line(line, self.syntax_set)
                .unwrap_or_default();
            lines.push(as_24_bit_terminal_escaped(&ranges, false));
        }
        
        lines
    }
}
```

3. **Integrate Analysis into TUI**
```rust
// In src/tui/studio.rs
fn draw_code_preview(&self, f: &mut Frame, area: Rect) {
    let block = Block::default()
        .borders(Borders::ALL)
        .title("📝 Code Preview")
        .border_style(Style::default().fg(Color::Green));
    
    let text = if self.code_content.is_empty() {
        vec![Line::from("Select a file to preview")]
    } else {
        // Get file extension
        let ext = self.file_tree[self.selected_file]
            .split('.')
            .last()
            .unwrap_or("");
        
        // Highlight syntax
        let highlighter = SyntaxHighlighter::new();
        highlighter.highlight(&self.code_content, ext)
            .iter()
            .map(|line| Line::from(line.as_str()))
            .collect()
    };
    
    let paragraph = Paragraph::new(text)
        .block(block)
        .wrap(ratatui::widgets::Wrap { trim: false });
    
    f.render_widget(paragraph, area);
}
```

### Day 5-6: AI Integration in TUI

1. **AI Chat Panel**
```rust
// src/tui/chat.rs
use crate::adapters::ai::factory::AIProviderFactory;
use crate::utils::config::Config;
use anyhow::Result;

pub struct ChatPanel {
    history: Vec<(String, String)>, // (sender, message)
    input: String,
    is_typing: bool,
}

impl ChatPanel {
    pub fn new() -> Self {
        Self {
            history: vec![(
                "AI".to_string(),
                "Hello! I'm Kandil AI. Select a file and I'll analyze it.".to_string(),
            )],
            input: String::new(),
            is_typing: false,
        }
    }
    
    pub fn add_message(&mut self, sender: &str, message: String) {
        self.history.push((sender.to_string(), message));
        // Keep only last 50 messages
        if self.history.len() > 50 {
            self.history.drain(0..10);
        }
    }
    
    pub async fn send_message(&mut self) -> Result<()> {
        if self.input.is_empty() || self.is_typing {
            return Ok(());
        }
        
        let message = self.input.clone();
        self.add_message("You", message.clone());
        self.input.clear();
        self.is_typing = true;
        
        // Send to AI
        let config = Config::load()?;
        let factory = AIProviderFactory::new(config.ai);
        let ai = factory.create().await?;
        
        let response = ai.chat(&message, None).await?;
        self.add_message("AI", response);
        self.is_typing = false;
        
        Ok(())
    }
    
    pub fn handle_input(&mut self, c: char) {
        self.input.push(c);
    }
    
    pub fn backspace(&mut self) {
        self.input.pop();
    }
    
    pub fn render(&self) -> Paragraph {
        let text = self.history
            .iter()
            .map(|(sender, msg)| format!("{}: {}\n", sender, msg))
            .collect::<String>();
        
        let mut lines = text.lines().collect::<Vec<_>>();
        if self.is_typing {
            lines.push("AI is typing...");
        }
        
        Paragraph::new(lines.join("\n"))
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .title("💬 AI Assistant")
                    .border_style(Style::default().fg(Color::Blue))
            )
    }
}
```

### Day 7-8: Performance Optimization

1. **Lazy File Loading**
```rust
// src/tui/file_tree.rs
use std::fs;
use std::path::{Path, PathBuf};
use anyhow::Result;

pub struct FileTree {
    root: PathBuf,
    tree: Vec<TreeItem>,
    selected: usize,
}

struct TreeItem {
    path: PathBuf,
    depth: usize,
    is_dir: bool,
    is_expanded: bool,
}

impl FileTree {
    pub fn new(root: PathBuf) -> Result<Self> {
        let mut tree = Vec::new();
        Self::build_tree(&root, &mut tree, 0)?;
        
        Ok(Self { root, tree, selected: 0 })
    }
    
    fn build_tree(path: &Path, tree: &mut Vec<TreeItem>, depth: usize) -> Result<()> {
        if depth > 3 { return Ok(()); } // Limit depth
        
        for entry in fs::read_dir(path)? {
            let entry = entry?;
            let path = entry.path();
            let is_dir = path.is_dir();
            
            tree.push(TreeItem {
                path: path.clone(),
                depth,
                is_dir,
                is_expanded: depth < 1, // Expand root level only
            });
            
            if is_dir && depth < 2 {
                Self::build_tree(&path, tree, depth + 1)?;
            }
        }
        Ok(())
    }
    
    pub fn render(&self) -> List {
        let items: Vec<ListItem> = self.tree
            .iter()
            .enumerate()
            .map(|(i, item)| {
                let prefix = "  ".repeat(item.depth);
                let icon = if item.is_dir {
                    if item.is_expanded { "📂" } else { "📁" }
                } else {
                    "📄"
                };
                
                let line = format!("{}{} {}", prefix, icon, item.path.file_name()
                    .unwrap_or_default()
                    .to_string_lossy());
                
                let style = if i == self.selected {
                    Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD)
                } else {
                    Style::default()
                };
                
                ListItem::new(line).style(style)
            })
            .collect();
        
        List::new(items)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .title("📁 File Explorer")
                    .border_style(Style::default().fg(Color::Cyan))
            )
    }
}
```

2. **Async File Operations**
```rust
// Use tokio::fs for non-blocking I/O
async fn select_file(&mut self) -> Result<()> {
    if let Some(item) = self.file_tree.tree.get(self.file_tree.selected) {
        if !item.is_dir {
            self.code_content = tokio::fs::read_to_string(&item.path).await?;
            
            // Update file type
            self.workspace.project_type = self.detect_language_from_path(&item.path);
        }
    }
    Ok(())
}

fn detect_language_from_path(&self, path: &Path) -> String {
    match path.extension().and_then(|s| s.to_str()) {
        Some("dart") => "flutter".to_string(),
        Some("py") => "python".to_string(),
        Some("js" | "ts" | "jsx" | "tsx") => "js".to_string(),
        Some("rs") => "rust".to_string(),
        _ => "unknown".to_string(),
    }
}
```

### Day 9-10: Code Actions & AI Commands

1. **In-TUI AI Commands**
```rust
enum AiCommand {
    ExplainCode,
    GenerateTest,
    Refactor,
    FindBugs,
}

impl StudioApp {
    pub fn show_ai_menu(&mut self) {
        self.chat_history.push((
            "System".to_string(),
            "AI Commands: [e]xplain, [t]est, [r]efactor, [b]ugs".to_string(),
        ));
    }
    
    async fn execute_ai_command(&mut self, cmd: AiCommand) -> Result<()> {
        use AiCommand::*;
        
        if self.code_content.is_empty() {
            self.chat_history.push((
                "AI".to_string(),
                "No code selected. Navigate to a file first.".to_string(),
            ));
            return Ok(());
        }
        
        let prompt = match cmd {
            ExplainCode => format!("Explain this code:\n{}", &self.code_content[..1000]),
            GenerateTest => format!("Generate unit tests for:\n{}", &self.code_content[..1000]),
            Refactor => "Refactor this code to be more idiomatic".to_string(),
            FindBugs => "Find potential bugs and security issues".to_string(),
        };
        
        self.chat_history.push(("You".to_string(), format!("AI command: {:?}", cmd)));
        
        let config = crate::utils::config::Config::load()?;
        let factory = crate::adapters::ai::factory::AIProviderFactory::new(config.ai);
        let ai = factory.create().await?;
        
        let response = ai.chat(&prompt, None).await?;
        self.chat_history.push(("AI".to_string(), response));
        
        Ok(())
    }
}
```

### Day 11-14: Integration & Testing

1. **TUI Entry Point**
```rust
// src/cli/tui.rs
use crate::tui::studio::StudioApp;
use anyhow::Result;
use crossterm::{
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{backend::CrosstermBackend, Terminal};

pub async fn run_tui() -> Result<()> {
    // Setup terminal
    enable_raw_mode()?;
    let mut stdout = std::io::stdout();
    execute!(stdout, EnterAlternateScreen)?;
    
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;
    
    // Run app
    let app = StudioApp::new()?;
    let res = app.run(&mut terminal).await;
    
    // Cleanup
    disable_raw_mode()?;
    execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
    terminal.show_cursor()?;
    
    res
}
```

2. **Integration Tests**
```rust
// tests/integration/tui_test.rs
use kandil_code::tui::studio::StudioApp;
use ratatui::backend::TestBackend;
use ratatui::Terminal;

#[tokio::test]
async fn test_tui_creation() {
    let app = StudioApp::new().unwrap();
    assert!(!app.should_quit);
    assert!(!app.file_tree.is_empty());
}

#[test]
fn test_tui_render() {
    let backend = TestBackend::new(80, 24);
    let mut terminal = Terminal::new(backend).unwrap();
    
    let mut app = StudioApp::new().unwrap();
    terminal.draw(|f| app.draw(f)).unwrap();
    
    // Verify buffer is not empty
    let buffer = terminal.backend().buffer();
    assert!(!buffer.content.is_empty());
}
```

3. **Performance Benchmarks**
```rust
// benches/tui_render.rs
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use kandil_code::tui::studio::StudioApp;
use ratatui::backend::TestBackend;
use ratatui::Terminal;

fn bench_tui_render(c: &mut Criterion) {
    c.bench_function("tui_render", |b| {
        let mut terminal = Terminal::new(TestBackend::new(120, 40)).unwrap();
        let mut app = StudioApp::new().unwrap();
        
        b.iter(|| {
            terminal.draw(|f| app.draw(black_box(f))).unwrap();
        });
    });
}

criterion_group!(benches, bench_tui_render);
criterion_main!(benches);
```

## Tools & Dependencies
| Crate | Version | Purpose |
|-------|---------|---------|
| ratatui | 0.25 | TUI framework |
| crossterm | 0.27 | Cross-platform terminal ops |
| syntect | 5.2 | Syntax highlighting |
| tree-sitter | 0.20 | Code parsing |
| tui-textarea | 0.4 | Text input widget |
| once_cell | 1.19 | Lazy static parsers |
| criterion | 0.5 | Performance benchmarks |

## Testing Strategy
- **Unit**: Individual widget rendering (85% coverage)
- **Integration**: Full TUI lifecycle (init → render → cleanup)
- **Manual**: Test navigation on projects with 1000+ files
- **Performance**: Render time <16ms (60 FPS target)

## Deliverables
- `kandil tui` launches interactive interface
- ✅ File tree with lazy loading (100ms for 1000 files)
- ✅ Syntax highlighting for 4 languages
- ✅ AI chat panel with context awareness
- ✅ Code analysis integration
- ✅ Keyboard navigation (vim-style hjkl + arrows)
- ✅ Clean terminal restore on exit (no garbled output)
- ✅ 85% test coverage on TUI modules

## Timeline Breakdown
- **Days 1-3**: TUI layout + event system
- **Days 4-5**: Tree-sitter integration
- **Days 6-7**: Syntax highlighting
- **Days 8-9**: AI chat panel
- **Days 10-11**: Performance optimization
- **Days 12-14**: Testing & polish

## Success Criteria
- TUI renders in <100ms on startup
- File navigation at 60 FPS
- Syntax highlighting accurate for 90% of tokens
- AI responses appear in chat panel within 2s
- No memory leaks (check with `valgrind`)
- CI passes on Windows/macOS/Linux terminals
- `cargo tarpaulin` shows ≥85% coverage

## Potential Risks & Mitigations

| Risk | Mitigation |
|------|------------|
| Terminal incompatibility | Test on 5+ terminals; fallback to basic mode |
| Large files crash TUI | Limit preview to first 10,000 lines |
| Memory leak in event loop | Use `std::mem::drop` explicitly; run `valgrind` |
| AI response delays UI | Move AI calls to separate tokio task |
| Tree-sitter parse failures | Fallback to plain text; log errors |
| `crossterm` panic on resize | Add signal handler for SIGWINCH |

---

**Next**: Proceed to PHASE_4_REFACTOR_TESTS_MODELS.md after Phase 3 manual testing with real Flutter/Python projects.