matrixcode-tui 0.3.5

Terminal UI for MatrixCode
Documentation
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
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
use std::io::Stdout;
use std::time::{Duration, Instant};

use anyhow::Result;
use ratatui::{
    backend::CrosstermBackend,
    crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers, MouseEvent, MouseEventKind},
    Terminal,
};

use matrixcode_core::{AgentEvent, EventData, EventType, cancel::CancellationToken};
use ratatui::crossterm::event::MouseButton;

use crate::types::{Activity, ApproveMode, Role, Message};
use crate::utils::{truncate, extract_tool_detail, fmt_tokens};
use crate::ANIM_MS;

pub struct TuiApp {
    pub(crate) activity: Activity,
    pub(crate) activity_detail: String,
    pub(crate) messages: Vec<Message>,
    pub(crate) thinking: String,
    pub(crate) streaming: String,
    pub(crate) input: String,
    pub(crate) model: String,
    // Token stats
    pub(crate) tokens_in: u64,
    pub(crate) tokens_out: u64,
    pub(crate) session_total_out: u64,
    pub(crate) cache_read: u64,
    pub(crate) cache_created: u64,
    pub(crate) context_size: u64,
    // Debug stats
    pub(crate) api_calls: u64,
    pub(crate) compressions: u64,
    pub(crate) memory_saves: u64,
    pub(crate) tool_calls: u64,
    // UI state
    pub(crate) frame: usize,
    pub(crate) last_anim: Instant,
    pub(crate) show_welcome: bool,
    pub(crate) exit: bool,
    // Input cursor position (character index in input string)
    pub(crate) cursor_pos: usize,
    // Scroll state
    pub(crate) scroll_offset: u16,
    pub(crate) auto_scroll: bool,
    pub(crate) max_scroll: std::cell::Cell<u16>,
    // Thinking display state
    pub(crate) thinking_collapsed: bool,
    // Approval mode
    pub(crate) approve_mode: ApproveMode,
    // Ask tool channel
    pub(crate) ask_tx: Option<tokio::sync::mpsc::Sender<String>>,
    pub(crate) waiting_for_ask: bool,
    // Channels
    pub(crate) tx: tokio::sync::mpsc::Sender<String>,
    pub(crate) rx: tokio::sync::mpsc::Receiver<AgentEvent>,
    pub(crate) cancel: CancellationToken,
    // Message queue for pending inputs while AI is processing
    pub(crate) pending_messages: Vec<String>,
    // Loop task state
    pub(crate) loop_task: Option<LoopTask>,
    // Cron tasks state
    pub(crate) cron_tasks: Vec<CronTask>,
    // Selection state
    pub(crate) selection: Option<Selection>,
    pub(crate) selecting: bool,  // True while mouse dragging
    pub(crate) msg_area_top: std::cell::Cell<u16>,  // Messages area top Y (computed in draw)
    // Debug mode
    pub(crate) debug_mode: bool,
}

/// Text selection in messages area
#[derive(Clone, Copy, Debug)]
pub struct Selection {
    pub start_line: usize,
    pub start_col: usize,
    pub end_line: usize,
    pub end_col: usize,
}

impl Selection {
    pub fn new(start_line: usize, start_col: usize) -> Self {
        Self {
            start_line,
            start_col,
            end_line: start_line,
            end_col: start_col,
        }
    }
    
    pub fn extend_to(&mut self, line: usize, col: usize) {
        self.end_line = line;
        self.end_col = col;
    }
    
    #[allow(dead_code)]
    pub fn is_empty(&self) -> bool {
        self.start_line == self.end_line && self.start_col == self.end_col
    }
    
    pub fn normalized(&self) -> Self {
        // Normalize so start <= end
        if self.start_line > self.end_line || 
           (self.start_line == self.end_line && self.start_col > self.end_col) {
            Self {
                start_line: self.end_line,
                start_col: self.end_col,
                end_line: self.start_line,
                end_col: self.start_col,
            }
        } else {
            *self
        }
    }
    
    #[allow(dead_code)]
    pub fn contains(&self, line: usize, col: usize) -> bool {
        let norm = self.normalized();
        if line < norm.start_line || line > norm.end_line {
            return false;
        }
        if line == norm.start_line && line == norm.end_line {
            return col >= norm.start_col && col <= norm.end_col;
        }
        if line == norm.start_line {
            return col >= norm.start_col;
        }
        if line == norm.end_line {
            return col <= norm.end_col;
        }
        true  // Middle line
    }
}

/// Loop task - repeatedly send message
#[derive(Clone)]
pub struct LoopTask {
    pub message: String,
    pub interval_secs: u64,
    pub count: u64,
    pub max_count: Option<u64>,
    pub cancel_token: CancellationToken,
}

/// Cron task - scheduled message sending
#[derive(Clone)]
pub struct CronTask {
    pub id: usize,
    pub message: String,
    pub minute_interval: u64,  // Simplified: run every N minutes
    #[allow(dead_code)]
    pub next_run: Instant,  // For future use: precise scheduling
    pub cancel_token: CancellationToken,
}

impl TuiApp {
    pub fn new(
        tx: tokio::sync::mpsc::Sender<String>,
        rx: tokio::sync::mpsc::Receiver<AgentEvent>,
        cancel: CancellationToken,
    ) -> Self {
        Self {
            activity: Activity::Idle,
            activity_detail: String::new(),
            messages: Vec::new(),
            thinking: String::new(),
            streaming: String::new(),
            input: String::new(),
            model: "claude-sonnet-4".into(),
            tokens_in: 0,
            tokens_out: 0,
            session_total_out: 0,
            cache_read: 0,
            cache_created: 0,
            context_size: 200_000,
            api_calls: 0,
            compressions: 0,
            memory_saves: 0,
            tool_calls: 0,
            frame: 0,
            last_anim: Instant::now(),
            show_welcome: true,
            exit: false,
            cursor_pos: 0,
            scroll_offset: 0,
            auto_scroll: true,
            max_scroll: std::cell::Cell::new(0),
            thinking_collapsed: false,  // Default: expanded
            approve_mode: ApproveMode::Ask,
            ask_tx: None,
            waiting_for_ask: false,
            tx, rx, cancel,
            pending_messages: Vec::new(),
            loop_task: None,
            cron_tasks: Vec::new(),
            selection: None,
            selecting: false,
            msg_area_top: std::cell::Cell::new(0),
            debug_mode: false,
        }
    }

    pub fn with_ask_channel(mut self, ask_tx: tokio::sync::mpsc::Sender<String>) -> Self {
        self.ask_tx = Some(ask_tx);
        self
    }

    pub fn with_config(mut self, model: &str, _think: bool, _max_tokens: u32, context_size: Option<u64>) -> Self {
        self.model = model.to_string();
        self.context_size = context_size.unwrap_or_else(|| {
            let m = model.to_ascii_lowercase();
            if m.contains("1m") || m.contains("opus-4-7") {
                1_000_000
            } else if m.contains("claude-3") || m.contains("claude-4") || m.contains("claude-sonnet") {
                200_000
            } else {
                128_000
            }
        });
        self
    }

    pub fn load_messages(&mut self, core_messages: Vec<matrixcode_core::Message>) {
        for msg in core_messages {
            let content = match &msg.content {
                matrixcode_core::MessageContent::Text(t) => t.clone(),
                matrixcode_core::MessageContent::Blocks(blocks) => {
                    blocks.iter().filter_map(|b| match b {
                        matrixcode_core::ContentBlock::Text { text } => Some(text.clone()),
                        _ => None,
                    }).collect::<Vec<_>>().join("\n")
                }
            };
            if content.is_empty() { continue; }
            let role = match msg.role {
                matrixcode_core::Role::User => Role::User,
                matrixcode_core::Role::Assistant => Role::Assistant,
                matrixcode_core::Role::System => Role::System,
                matrixcode_core::Role::Tool => Role::Tool { name: "tool".into(), is_error: false },
            };
            self.messages.push(Message { role, content });
        }
        if !self.messages.is_empty() {
            self.show_welcome = false;
        }
    }

    /// Get selected text from messages
    fn get_selected_text(&self, selection: Selection) -> String {
        let norm = selection.normalized();
        
        // We need to reconstruct the text from rendered lines
        // This is tricky because we don't store the rendered lines
        // For simplicity, we'll extract from message content based on approximate line mapping
        
        let mut result = String::new();
        
        // Build all text lines from messages (approximate - doesn't account for wrapping)
        let mut all_text: Vec<String> = Vec::new();
        for msg in &self.messages {
            let icon = msg.role.icon();
            let label = msg.role.label();
            all_text.push(format!("{} {}", icon, label));
            for line in msg.content.lines() {
                all_text.push(format!("  {}", line));
            }
            all_text.push(String::new());  // Empty line between messages
        }
        
        // Extract selected range
        for i in norm.start_line..=norm.end_line {
            if let Some(line) = all_text.get(i) {
                if i == norm.start_line && i == norm.end_line {
                    // Single line selection
                    if norm.start_col < line.len() && norm.end_col <= line.len() {
                        result.push_str(&line[norm.start_col..norm.end_col]);
                    }
                } else if i == norm.start_line {
                    // First line of multi-line selection
                    if norm.start_col < line.len() {
                        result.push_str(&line[norm.start_col..]);
                    }
                    result.push('\n');
                } else if i == norm.end_line {
                    // Last line of multi-line selection
                    if norm.end_col <= line.len() {
                        result.push_str(&line[..norm.end_col]);
                    }
                } else {
                    // Middle line
                    result.push_str(line);
                    result.push('\n');
                }
            }
        }
        
        result
    }

    pub fn run(&mut self, term: &mut Terminal<CrosstermBackend<Stdout>>) -> Result<()> {
        loop {
            // Animation frame
            if self.last_anim.elapsed().as_millis() >= ANIM_MS as u128 {
                self.frame = (self.frame + 1) % 10;
                self.last_anim = Instant::now();
            }

            term.draw(|f| self.draw(f))?;

            // Handle events
            if event::poll(Duration::from_millis(16))? {
                match event::read()? {
                    Event::Key(k) => self.on_key(k),
                    Event::Mouse(m) => self.on_mouse(m, self.msg_area_top.get()),
                    Event::Paste(text) => self.on_paste(&text),
                    _ => {}
                }
            }

            // Process agent events
            while let Ok(e) = self.rx.try_recv() {
                self.on_event(e);
            }

            if self.exit { break; }
        }
        Ok(())
    }

    fn on_key(&mut self, k: KeyEvent) {
        if k.kind != KeyEventKind::Press { return; }

        match k.code {
            // Enter: send or newline
            KeyCode::Enter => {
                if k.modifiers.contains(KeyModifiers::SHIFT) {
                    // Shift+Enter: insert newline at cursor position
                    self.ensure_char_boundary();
                    self.input.insert(self.cursor_pos, '\n');
                    self.cursor_pos += 1;  // '\n' is 1 byte
                } else if !self.input.trim().is_empty() {
                    self.send_input();
                }
            }

            // Escape: interrupt or clear input
            KeyCode::Esc => {
                if self.activity == Activity::Asking {
                    // Abort approval request
                    self.waiting_for_ask = false;
                    self.activity = Activity::Idle;
                    self.messages.push(Message { role: Role::System, content: "⚠️ Approval aborted".into() });
                    if let Some(ask_tx) = &self.ask_tx {
                        ask_tx.try_send("abort".to_string()).ok();
                    }
                } else if self.activity != Activity::Idle {
                    self.cancel.cancel();
                    self.cancel.reset();
                    self.activity = Activity::Idle;
                    self.messages.push(Message { role: Role::System, content: "⚠️ Interrupted".into() });
                } else {
                    self.input.clear();
                    self.cursor_pos = 0;
                }
            }

            // Ctrl+C: copy selection or interrupt
            KeyCode::Char('c') if k.modifiers.contains(KeyModifiers::CONTROL) => {
                if let Some(sel) = self.selection {
                    // Copy selected text
                    let selected_text = self.get_selected_text(sel);
                    if !selected_text.is_empty() {
                        // Try to copy to clipboard
                        let clipboard_result = arboard::Clipboard::new()
                            .and_then(|mut cb| cb.set_text(&selected_text));
                        
                        match clipboard_result {
                            Ok(_) => {
                                self.messages.push(Message {
                                    role: Role::System,
                                    content: format!("📋 Copied {} chars to clipboard", selected_text.len())
                                });
                            }
                            Err(_) => {
                                self.messages.push(Message {
                                    role: Role::System,
                                    content: format!("📋 Copied {} chars (clipboard unavailable)", selected_text.len())
                                });
                            }
                        }
                        self.selection = None;
                        self.selecting = false;
                    }
                } else if self.activity != Activity::Idle {
                    self.cancel.cancel();
                    self.cancel.reset();
                    self.activity = Activity::Idle;
                    self.messages.push(Message { role: Role::System, content: "⚠️ Interrupted".into() });
                }
            }

            // Ctrl+D: exit
            KeyCode::Char('d') if k.modifiers.contains(KeyModifiers::CONTROL) => {
                self.exit = true;
            }

            // Ctrl+V: paste from clipboard
            KeyCode::Char('v') if k.modifiers.contains(KeyModifiers::CONTROL) => {
                // Try to get text from clipboard
                if let Ok(mut clipboard) = arboard::Clipboard::new() {
                    if let Ok(text) = clipboard.get_text() {
                        self.on_paste(&text);
                    }
                }
            }

            // Backspace: delete char before cursor
            KeyCode::Backspace => {
                if self.cursor_pos > 0 {
                    let prev_pos = self.prev_char_boundary();
                    self.input.drain(prev_pos..self.cursor_pos);
                    self.cursor_pos = prev_pos;
                }
            }

            // Delete: delete char at cursor
            KeyCode::Delete => {
                if self.cursor_pos < self.input.len() {
                    let next_pos = self.next_char_boundary();
                    self.input.drain(self.cursor_pos..next_pos);
                }
            }

            // Left arrow: move cursor left (one character)
            KeyCode::Left => {
                if self.cursor_pos > 0 {
                    self.cursor_pos = self.prev_char_boundary();
                }
            }

            // Right arrow: move cursor right (one character)
            KeyCode::Right => {
                if self.cursor_pos < self.input.len() {
                    self.cursor_pos = self.next_char_boundary();
                }
            }

            // Up arrow: move cursor to previous line (in multiline input)
            KeyCode::Up if !k.modifiers.contains(KeyModifiers::ALT) => {
                if self.input.contains('\n') {
                    let (current_line_num, col_chars, _) = self.get_line_info();
                    if current_line_num > 1 {
                        let char_pos = self.byte_pos_to_char_pos();
                        let input_chars: Vec<char> = self.input.chars().collect();
                        let before_cursor_str: String = input_chars[..char_pos.min(input_chars.len())].iter().collect();
                        
                        // Previous line is before the last '\n' in before_cursor_str
                        let prev_lines_str = &before_cursor_str[..before_cursor_str.rfind('\n').unwrap_or(0)];
                        let prev_line_start_char = prev_lines_str.chars().count();
                        
                        // Find previous line length
                        let prev_line_end_char = char_pos.saturating_sub(col_chars).saturating_sub(1); // -1 for the newline
                        let prev_line_len_chars = prev_line_end_char.saturating_sub(prev_line_start_char);
                        
                        // Move to same column (or end if shorter)
                        let target_char_pos = prev_line_start_char + col_chars.min(prev_line_len_chars);
                        self.cursor_pos = self.char_pos_to_byte_pos(target_char_pos);
                    }
                }
            }

            // Down arrow: move cursor to next line (in multiline input)
            KeyCode::Down if !k.modifiers.contains(KeyModifiers::ALT) => {
                if self.input.contains('\n') {
                    let (current_line_num, col_chars, total_lines) = self.get_line_info();
                    if current_line_num < total_lines {
                        let char_pos = self.byte_pos_to_char_pos();
                        let input_chars: Vec<char> = self.input.chars().collect();
                        
                        // Boundary check: char_pos must not exceed input_chars.len()
                        let safe_char_pos = char_pos.min(input_chars.len());
                        
                        // Find next line start
                        let remaining_chars = &input_chars[safe_char_pos..];
                        let next_line_start_char = remaining_chars.iter().position(|c| *c == '\n')
                            .map(|i| safe_char_pos + i + 1)
                            .unwrap_or_else(|| input_chars.len());
                        
                        // Find next line end
                        let next_line_chars = &input_chars[next_line_start_char..];
                        let next_line_end_char = next_line_chars.iter().position(|c| *c == '\n')
                            .map(|i| next_line_start_char + i)
                            .unwrap_or_else(|| input_chars.len());
                        
                        let next_line_len_chars = next_line_end_char.saturating_sub(next_line_start_char);
                        
                        // Move to same column (or end if shorter)
                        let target_char_pos = next_line_start_char + col_chars.min(next_line_len_chars);
                        self.cursor_pos = self.char_pos_to_byte_pos(target_char_pos);
                    }
                }
            }

            // Regular character input (except when Alt/Ctrl is held)
            KeyCode::Char(c) if !k.modifiers.contains(KeyModifiers::ALT) && !k.modifiers.contains(KeyModifiers::CONTROL) => {
                self.ensure_char_boundary();
                self.input.insert(self.cursor_pos, c);
                self.cursor_pos += c.len_utf8();
            }

            // Alt+M: toggle approve mode
            KeyCode::Char('m') if k.modifiers.contains(KeyModifiers::ALT) => {
                self.approve_mode = self.approve_mode.next();
                self.tx.try_send(format!("/mode:{}", self.approve_mode.label())).ok();
            }

            // Alt+T: toggle thinking collapse
            KeyCode::Char('t') if k.modifiers.contains(KeyModifiers::ALT) => {
                self.thinking_collapsed = !self.thinking_collapsed;
            }

            // Shift+Tab / BackTab: toggle approve mode
            KeyCode::Tab if k.modifiers.contains(KeyModifiers::SHIFT) => {
                self.approve_mode = self.approve_mode.next();
                self.tx.try_send(format!("/mode:{}", self.approve_mode.label())).ok();
            }
            KeyCode::BackTab => {
                self.approve_mode = self.approve_mode.next();
                self.tx.try_send(format!("/mode:{}", self.approve_mode.label())).ok();
            }

            // Scroll: PageUp
            KeyCode::PageUp => {
                if self.auto_scroll {
                    self.scroll_offset = self.max_scroll.get();
                    self.auto_scroll = false;
                }
                self.scroll_offset = self.scroll_offset.saturating_sub(10);
            }

            // Scroll: PageDown
            KeyCode::PageDown => {
                if !self.auto_scroll {
                    self.scroll_offset = self.scroll_offset.saturating_add(10);
                    if self.scroll_offset >= self.max_scroll.get() {
                        self.auto_scroll = true;
                        self.scroll_offset = 0;
                    }
                }
            }

            // Scroll: Alt+Up (or Up when not idle)
            KeyCode::Up if k.modifiers.contains(KeyModifiers::ALT) => {
                if self.auto_scroll {
                    self.scroll_offset = self.max_scroll.get();
                    self.auto_scroll = false;
                }
                self.scroll_offset = self.scroll_offset.saturating_sub(1);
            }

            // Scroll: Alt+Down (or Down when not idle)
            KeyCode::Down if k.modifiers.contains(KeyModifiers::ALT) => {
                if !self.auto_scroll {
                    self.scroll_offset = self.scroll_offset.saturating_add(1);
                    if self.scroll_offset >= self.max_scroll.get() {
                        self.auto_scroll = true;
                        self.scroll_offset = 0;
                    }
                }
            }

            // Home: move cursor to start (if input has content) or scroll to top
            KeyCode::Home => {
                if !self.input.is_empty() {
                    self.cursor_pos = 0;
                } else {
                    self.auto_scroll = false;
                    self.scroll_offset = 0;
                }
            }

            // End: move cursor to end (if input has content) or scroll to bottom
            KeyCode::End => {
                if !self.input.is_empty() {
                    self.cursor_pos = self.input.len();
                } else {
                    self.auto_scroll = true;
                    self.scroll_offset = 0;
                }
            }

            _ => {}
        }
    }

    // ============================================================================
    // Unicode-safe cursor position helpers
    // ============================================================================
    
    /// Ensure cursor_pos is at a valid UTF-8 character boundary.
    /// If not, move to the nearest valid boundary.
    fn ensure_char_boundary(&mut self) {
        if !self.input.is_char_boundary(self.cursor_pos) {
            self.cursor_pos = self.input.char_indices()
                .rfind(|(i, _)| *i <= self.cursor_pos)
                .map(|(i, _)| i)
                .unwrap_or(0);
        }
    }
    
    /// Find the byte position of the previous character boundary.
    /// Returns 0 if cursor is at the start.
    fn prev_char_boundary(&self) -> usize {
        self.input.char_indices()
            .rfind(|(i, _)| *i < self.cursor_pos)
            .map(|(i, _)| i)
            .unwrap_or(0)
    }
    
    /// Find the byte position of the next character boundary.
    /// Returns input.len() if cursor is at the end.
    fn next_char_boundary(&self) -> usize {
        self.input.char_indices()
            .find(|(i, _)| *i > self.cursor_pos)
            .map(|(i, _)| i)
            .unwrap_or_else(|| self.input.len())
    }
    
    /// Convert byte position to character position (count of chars before cursor).
    fn byte_pos_to_char_pos(&self) -> usize {
        self.input.char_indices().take(self.cursor_pos).count()
    }
    
    /// Convert character position to byte position.
    fn char_pos_to_byte_pos(&self, char_pos: usize) -> usize {
        self.input.char_indices()
            .nth(char_pos)
            .map(|(i, _)| i)
            .unwrap_or_else(|| self.input.len())
    }
    
    /// Get current line info: (current_line_number, column_in_chars, total_lines)
    fn get_line_info(&self) -> (usize, usize, usize) {
        let char_pos = self.byte_pos_to_char_pos();
        let before_cursor_str: String = self.input.chars().take(char_pos).collect();
        let current_line_num = before_cursor_str.lines().count();
        let total_lines = self.input.lines().count();
        let current_line_start_char = before_cursor_str.rfind('\n')
            .map(|i| before_cursor_str[i+1..].chars().count())
            .unwrap_or(0);
        let col_chars = char_pos - current_line_start_char;
        (current_line_num, col_chars, total_lines)
    }

    fn send_input(&mut self) {
        self.show_welcome = false;
        let input = self.input.trim().to_string();
        self.input.clear();
        self.cursor_pos = 0;

        if self.waiting_for_ask {
            // Respond to approval/ask question
            self.waiting_for_ask = false;
            self.messages.push(Message { role: Role::User, content: input.clone() });
            if let Some(ask_tx) = &self.ask_tx {
                ask_tx.try_send(input).ok();
            }
            self.activity = Activity::Thinking;
            self.auto_scroll = true;
        } else if input.starts_with('/') {
            // Command
            self.handle_command(&input);
        } else if self.activity == Activity::Idle {
            // Send immediately
            self.messages.push(Message { role: Role::User, content: input.clone() });
            self.tx.try_send(input).ok();
            self.activity = Activity::Thinking;
            self.auto_scroll = true;
        } else {
            // Queue message (AI is processing)
            self.pending_messages.push(input.clone());
        }
    }

    fn on_mouse(&mut self, m: MouseEvent, msg_area_y: u16) {
        match m.kind {
            MouseEventKind::ScrollUp => {
                // Scroll up = view earlier content = decrease offset
                if self.auto_scroll {
                    self.scroll_offset = self.max_scroll.get();
                    self.auto_scroll = false;
                }
                self.scroll_offset = self.scroll_offset.saturating_sub(3);
                self.selection = None;  // Clear selection on scroll
            }
            MouseEventKind::ScrollDown => {
                // Scroll down = view newer content = increase offset
                if !self.auto_scroll {
                    self.scroll_offset = self.scroll_offset.saturating_add(3);
                    if self.scroll_offset >= self.max_scroll.get() {
                        self.auto_scroll = true;
                        self.scroll_offset = 0;
                    }
                }
                self.selection = None;  // Clear selection on scroll
            }
            MouseEventKind::Down(MouseButton::Left) => {
                // Start selection in messages area
                if m.row >= msg_area_y {
                    // If auto_scroll is on, sync scroll_offset first before disabling it
                    if self.auto_scroll {
                        self.scroll_offset = self.max_scroll.get();
                    }
                    let line = self.scroll_offset as usize + (m.row - msg_area_y) as usize;
                    let col = m.column as usize;
                    self.selection = Some(Selection::new(line, col));
                    self.selecting = true;
                    self.auto_scroll = false;  // Stop auto scroll when selecting
                }
            }
            MouseEventKind::Drag(MouseButton::Left) => {
                // Extend selection
                if self.selecting && m.row >= msg_area_y {
                    // Sync scroll_offset if auto_scroll was on
                    if self.auto_scroll {
                        self.scroll_offset = self.max_scroll.get();
                        self.auto_scroll = false;
                    }
                    let line = self.scroll_offset as usize + (m.row - msg_area_y) as usize;
                    let col = m.column as usize;
                    if let Some(ref mut sel) = self.selection {
                        sel.extend_to(line, col);
                    }
                }
            }
            MouseEventKind::Up(MouseButton::Left) => {
                self.selecting = false;
            }
            _ => {}
        }
    }

    fn on_paste(&mut self, text: &str) {
        self.ensure_char_boundary();
        self.input.insert_str(self.cursor_pos, text);
        self.cursor_pos += text.len();  // cursor_pos is byte position
    }

    fn handle_command(&mut self, cmd: &str) {
        let parts: Vec<&str> = cmd.split_whitespace().collect();
        let command = parts.first().copied().unwrap_or("");
        let args = &parts[1..];

        match command {
            "/exit" | "/quit" | "/q" => {
                self.exit = true;
            }
            "/clear" => {
                if self.activity == Activity::Idle {
                    self.messages.clear();
                    self.pending_messages.clear();
                    self.messages.push(Message { role: Role::System, content: "✓ Messages cleared".into() });
                } else {
                    self.messages.push(Message { role: Role::System, content: "⚠️ Cannot clear while AI is processing".into() });
                }
                self.auto_scroll = true;
            }
            "/history" => {
                let user_count = self.messages.iter().filter(|m| m.role == Role::User).count();
                let assistant_count = self.messages.iter().filter(|m| m.role == Role::Assistant).count();
                let tool_count = self.messages.iter().filter(|m| matches!(m.role, Role::Tool { .. })).count();
                let queue_count = self.pending_messages.len();
                self.messages.push(Message {
                    role: Role::System,
                    content: format!(
                        "📊 Session: {} user, {} assistant, {} tools, {} queued, {} output tokens",
                        user_count, assistant_count, tool_count, queue_count, fmt_tokens(self.session_total_out)
                    )
                });
                self.auto_scroll = true;
            }
            "/mode" => {
                if args.is_empty() {
                    self.messages.push(Message {
                        role: Role::System,
                        content: format!("Current mode: {} (use /mode ask|auto|strict)", self.approve_mode.label())
                    });
                } else {
                    match args[0] {
                        "ask" => self.approve_mode = ApproveMode::Ask,
                        "auto" => self.approve_mode = ApproveMode::Auto,
                        "strict" => self.approve_mode = ApproveMode::Strict,
                        _ => {
                            self.messages.push(Message {
                                role: Role::System,
                                content: "Invalid mode. Use: ask, auto, strict".into()
                            });
                            return;
                        }
                    }
                    self.tx.try_send(format!("/mode:{}", self.approve_mode.label())).ok();
                    self.messages.push(Message {
                        role: Role::System,
                        content: format!("✓ Mode: {}", self.approve_mode.label())
                    });
                }
                self.auto_scroll = true;
            }
            "/model" => {
                if args.is_empty() {
                    self.messages.push(Message {
                        role: Role::System,
                        content: format!("Model: {} (context: {})", self.model, fmt_tokens(self.context_size))
                    });
                } else if self.activity == Activity::Idle {
                    let new_model = args.join(" ");
                    self.model = new_model.clone();
                    self.messages.push(Message {
                        role: Role::System,
                        content: format!("✓ Model: {}", new_model)
                    });
                } else {
                    self.messages.push(Message {
                        role: Role::System,
                        content: "⚠️ Cannot change model while AI is processing".into()
                    });
                }
                self.auto_scroll = true;
            }
            "/compact" | "/compress" => {
                self.tx.try_send("/compact".to_string()).ok();
                self.auto_scroll = true;
            }
            "/init" => {
                // Initialize project configuration
                if args.is_empty() {
                    // Generate project overview
                    self.tx.try_send("/init".to_string()).ok();
                    self.messages.push(Message {
                        role: Role::System,
                        content: "🔄 Generating project overview...".into()
                    });
                } else if args[0] == "status" {
                    // Show project status
                    self.tx.try_send("/init status".to_string()).ok();
                    self.messages.push(Message {
                        role: Role::System,
                        content: "⏳ Checking project status...".into()
                    });
                } else if args[0] == "reset" || args[0] == "clear" {
                    // Reset configuration
                    self.tx.try_send("/init reset".to_string()).ok();
                    self.messages.push(Message {
                        role: Role::System,
                        content: "⏳ Resetting project configuration...".into()
                    });
                } else {
                    self.messages.push(Message {
                        role: Role::System,
                        content: "Unknown init command. Use: /init, /init status, /init reset".into()
                    });
                }
                self.auto_scroll = true;
            }
            "/debug" => {
                // Toggle debug mode
                self.debug_mode = !self.debug_mode;
                self.messages.push(Message {
                    role: Role::System,
                    content: format!("🔧 Debug mode: {} (api/tools counts {})",
                        if self.debug_mode { "ON" } else { "OFF" },
                        if self.debug_mode { "visible" } else { "hidden" }
                    )
                });
                self.auto_scroll = true;
            }
            "/retry" => {
                // Process pending queue
                if !self.pending_messages.is_empty() && self.activity == Activity::Idle {
                    let next_msg = self.pending_messages.remove(0);
                    self.messages.push(Message { role: Role::User, content: next_msg.clone() });
                    self.tx.try_send(next_msg).ok();
                    self.activity = Activity::Thinking;
                    self.auto_scroll = true;
                    self.messages.push(Message {
                        role: Role::System,
                        content: if self.pending_messages.is_empty() {
                            "✓ Retry: processing last queued message".into()
                        } else {
                            format!("⏳ Retry: {} messages remaining", self.pending_messages.len())
                        }
                    });
                } else if self.pending_messages.is_empty() {
                    self.messages.push(Message { role: Role::System, content: "No pending messages to retry".into() });
                } else {
                    self.messages.push(Message { role: Role::System, content: "AI is busy, please wait".into() });
                }
                self.auto_scroll = true;
            }
            "/new" => {
                if self.activity == Activity::Idle {
                    self.messages.clear();
                    self.pending_messages.clear();
                    self.tokens_in = 0;
                    self.tokens_out = 0;
                    self.session_total_out = 0;
                    self.tx.try_send("/new".to_string()).ok();
                    self.messages.push(Message { role: Role::System, content: "✓ New session".into() });
                } else {
                    self.messages.push(Message { role: Role::System, content: "⚠️ Cannot start new session while AI is processing".into() });
                }
                self.auto_scroll = true;
            }
            "/help" => {
                self.messages.push(Message {
                    role: Role::System,
                    content: concat!(
                        "📖 Commands:\n",
                        "  /help     - Show this help\n",
                        "  /exit     - Exit MatrixCode\n",
                        "  /clear    - Clear messages\n",
                        "  /history  - Show session history\n",
                        "  /mode     - Change approve mode (ask/auto/strict)\n",
                        "  /model    - Show/change model\n",
                        "  /compact  - Compress context\n",
                        "  /retry    - Retry last queued message\n",
                        "  /new      - Start new session\n",
                        "  /init     - Initialize/reset project\n",
                        "  /skills   - List loaded skills\n",
                        "  /memory   - View/manage memories\n",
                        "  /overview - View project overview\n",
                        "  /save     - Save current session\n",
                        "  /sessions - List saved sessions\n",
                        "  /load <id>- Load a session\n",
                        "  /debug    - Toggle debug mode\n",
                        "  /loop     - Start/stop loop task\n",
                        "  /cron     - Manage scheduled tasks\n",
                        "\n",
                        "⌨️ Shortcuts:\n",
                        "  Enter=send │ Shift+Enter=newline │ PgUp/PgDn=scroll\n",
                        "  Home/End=top/bot │ Alt+M=mode │ Alt+T=thinking\n",
                        "  Esc=interrupt │ Ctrl+D=exit"
                    ).into()
                });
                self.auto_scroll = true;
            }
            "/skills" => {
                // Send to backend for processing (shows loaded skills, not tools)
                self.tx.try_send("/skills".to_string()).ok();
                self.auto_scroll = true;
            }
            "/memory" => {
                // Send to backend for processing
                self.tx.try_send("/memory".to_string()).ok();
                self.auto_scroll = true;
            }
            "/overview" => {
                // Send to backend for processing
                self.tx.try_send("/overview".to_string()).ok();
                self.auto_scroll = true;
            }
            "/save" => {
                // Send to backend for processing
                self.tx.try_send("/save".to_string()).ok();
                self.auto_scroll = true;
            }
            "/sessions" | "/resume" => {
                // Send to backend for processing
                self.tx.try_send("/sessions".to_string()).ok();
                self.auto_scroll = true;
            }
            "/loop" => {
                if args.is_empty() {
                    self.messages.push(Message {
                        role: Role::System,
                        content: "/loop <message> [interval] [count] - Start loop\n/loop stop - Stop loop\n/loop status - Show status".into()
                    });
                } else if args[0] == "stop" {
                    // Take ownership to avoid borrow conflict
                    let task = self.loop_task.take();
                    if let Some(ref task) = task {
                        task.cancel_token.cancel();
                        self.messages.push(Message {
                            role: Role::System,
                            content: format!("✓ Loop stopped (executed {} times)", task.count)
                        });
                        self.loop_task = None;  // Already taken
                    } else {
                        self.messages.push(Message { role: Role::System, content: "No active loop".into() });
                    }
                } else if args[0] == "status" {
                    if let Some(ref task) = self.loop_task {
                        self.messages.push(Message {
                            role: Role::System,
                            content: format!(
                                "🔄 Loop active: '{}' every {}s, count {}{}",
                                truncate(&task.message, 30),
                                task.interval_secs,
                                task.count,
                                task.max_count.map(|m| format!(" (max {})", m)).unwrap_or_default()
                            )
                        });
                    } else {
                        self.messages.push(Message { role: Role::System, content: "No active loop".into() });
                    }
                } else {
                    // Start new loop: /loop "message" [interval] [max_count]
                    if self.loop_task.is_some() {
                        self.messages.push(Message { role: Role::System, content: "⚠️ Loop already active. Use /loop stop first".into() });
                    } else {
                        let message = args[0].to_string();
                        let interval_secs: u64 = args.get(1)
                            .and_then(|s| s.parse().ok())
                            .unwrap_or(60);
                        let max_count: Option<u64> = args.get(2)
                            .and_then(|s| s.parse().ok());
                        
                        let cancel_token = CancellationToken::new();
                        self.loop_task = Some(LoopTask {
                            message: message.clone(),
                            interval_secs,
                            count: 0,
                            max_count,
                            cancel_token: cancel_token.clone(),
                        });
                        
                        // Spawn background task
                        let tx = self.tx.clone();
                        let msg = message.clone();
                        tokio::spawn(async move {
                            loop {
                                if cancel_token.is_cancelled() {
                                    break;
                                }
                                // Send message
                                tx.try_send(msg.clone()).ok();
                                // Wait interval
                                tokio::time::sleep(Duration::from_secs(interval_secs)).await;
                            }
                        });
                        
                        self.messages.push(Message {
                            role: Role::System,
                            content: format!(
                                "🔄 Loop started: '{}' every {}s{}",
                                truncate(&message, 30),
                                interval_secs,
                                max_count.map(|m| format!(" (max {})", m)).unwrap_or_default()
                            )
                        });
                    }
                }
                self.auto_scroll = true;
            }
            "/cron" => {
                if args.is_empty() {
                    self.messages.push(Message {
                        role: Role::System,
                        content: "/cron add <message> <minutes> - Add cron task\n/cron list - List tasks\n/cron remove <id> - Remove task\n/cron clear - Clear all".into()
                    });
                } else if args[0] == "list" {
                    if self.cron_tasks.is_empty() {
                        self.messages.push(Message { role: Role::System, content: "No cron tasks".into() });
                    } else {
                        let list: Vec<String> = self.cron_tasks.iter()
                            .map(|t| format!("#{}: '{}' every {}min", t.id, truncate(&t.message, 20), t.minute_interval))
                            .collect();
                        self.messages.push(Message {
                            role: Role::System,
                            content: format!("📋 Cron tasks:\n{}", list.join("\n"))
                        });
                    }
                } else if args[0] == "remove" || args[0] == "rm" {
                    let id: usize = args.get(1)
                        .and_then(|s| s.parse().ok())
                        .unwrap_or(0);
                    if let Some(pos) = self.cron_tasks.iter().position(|t| t.id == id) {
                        let task = &self.cron_tasks[pos];
                        task.cancel_token.cancel();
                        self.cron_tasks.remove(pos);
                        self.messages.push(Message {
                            role: Role::System,
                            content: format!("✓ Cron task #{} removed", id)
                        });
                    } else {
                        self.messages.push(Message { role: Role::System, content: format!("Cron task #{} not found", id) });
                    }
                } else if args[0] == "clear" {
                    for task in &self.cron_tasks {
                        task.cancel_token.cancel();
                    }
                    let count = self.cron_tasks.len();
                    self.cron_tasks.clear();
                    self.messages.push(Message {
                        role: Role::System,
                        content: format!("{} cron tasks cleared", count)
                    });
                } else if args[0] == "add" {
                    // /cron add "message" 5
                    if args.len() < 3 {
                        self.messages.push(Message {
                            role: Role::System,
                            content: "Usage: /cron add <message> <minutes>".into()
                        });
                    } else {
                        let message = args[1].to_string();
                        let minute_interval: u64 = args.get(2)
                            .and_then(|s| s.parse().ok())
                            .unwrap_or(5);
                        
                        let id = self.cron_tasks.iter().map(|t| t.id).max().unwrap_or(0) + 1;
                        let cancel_token = CancellationToken::new();
                        
                        let task = CronTask {
                            id,
                            message: message.clone(),
                            minute_interval,
                            next_run: Instant::now() + Duration::from_secs(minute_interval * 60),
                            cancel_token: cancel_token.clone(),
                        };
                        
                        self.cron_tasks.push(task.clone());
                        
                        // Spawn background task
                        let tx = self.tx.clone();
                        let msg = message.clone();
                        let interval_secs = minute_interval * 60;
                        tokio::spawn(async move {
                            // Initial delay to next_run
                            tokio::time::sleep(Duration::from_secs(interval_secs)).await;
                            loop {
                                if cancel_token.is_cancelled() {
                                    break;
                                }
                                // Send message
                                tx.try_send(msg.clone()).ok();
                                // Wait interval
                                tokio::time::sleep(Duration::from_secs(interval_secs)).await;
                            }
                        });
                        
                        self.messages.push(Message {
                            role: Role::System,
                            content: format!("✓ Cron #{} added: '{}' every {}min", id, truncate(&message, 30), minute_interval)
                        });
                    }
                } else {
                    self.messages.push(Message {
                        role: Role::System,
                        content: "Unknown cron command. Use: add, list, remove, clear".into()
                    });
                }
                self.auto_scroll = true;
            }
            _ => {
                self.messages.push(Message {
                    role: Role::System,
                    content: format!("Unknown: {}. Type /help", command)
                });
                self.auto_scroll = true;
            }
        }
    }

    fn on_event(&mut self, e: AgentEvent) {
        match e.event_type {
            EventType::ThinkingStart => {
                self.activity = Activity::Thinking;
                self.thinking.clear();
            }
            EventType::ThinkingDelta => {
                if let Some(EventData::Thinking { delta, .. }) = e.data {
                    self.thinking.push_str(&delta);
                    self.activity = Activity::Thinking;
                }
            }
            EventType::ThinkingEnd => {
                if !self.thinking.is_empty() {
                    self.messages.push(Message { role: Role::Thinking, content: self.thinking.clone() });
                    self.thinking.clear();
                }
            }
            EventType::TextStart => {
                self.streaming.clear();
                self.activity = Activity::Thinking;
            }
            EventType::TextDelta => {
                if let Some(EventData::Text { delta }) = e.data {
                    self.streaming.push_str(&delta);
                    self.activity = Activity::Thinking;
                }
            }
            EventType::TextEnd => {
                if !self.streaming.is_empty() {
                    self.messages.push(Message { role: Role::Assistant, content: self.streaming.clone() });
                    self.streaming.clear();
                }
            }
            EventType::ToolUseStart => {
                if let Some(EventData::ToolUse { name, input, .. }) = e.data {
                    self.activity = Activity::from_tool(&name);
                    self.activity_detail = extract_tool_detail(&name, input.as_ref());
                }
            }
            EventType::ToolResult => {
                if let Some(EventData::ToolResult { content, is_error, .. }) = e.data {
                    let tool_name = self.activity.label();
                    self.messages.push(Message {
                        role: Role::Tool { name: tool_name, is_error },
                        content: content  // Keep full content, draw.rs will summarize
                    });
                    self.tool_calls += 1;
                    self.activity = Activity::Thinking;
                    self.activity_detail.clear();
                }
            }
            EventType::SessionEnded => {
                // Flush remaining content
                if !self.streaming.is_empty() {
                    self.messages.push(Message { role: Role::Assistant, content: self.streaming.clone() });
                    self.streaming.clear();
                }
                if !self.thinking.is_empty() {
                    self.messages.push(Message { role: Role::Thinking, content: self.thinking.clone() });
                    self.thinking.clear();
                }

                // Process queue or go idle
                if !self.pending_messages.is_empty() {
                    let next_msg = self.pending_messages.remove(0);
                    self.messages.push(Message { role: Role::User, content: next_msg.clone() });
                    self.tx.try_send(next_msg).ok();
                    self.activity = Activity::Thinking;
                    self.auto_scroll = true;
                    // Show queue status
                    self.messages.push(Message {
                        role: Role::System,
                        content: if self.pending_messages.is_empty() {
                            "✓ Queue completed".into()
                        } else {
                            format!("⏳ Processing queue ({} left)", self.pending_messages.len())
                        }
                    });
                } else {
                    self.activity = Activity::Idle;
                }
                self.activity_detail.clear();
            }
            EventType::Error => {
                if let Some(EventData::Error { message, .. }) = e.data {
                    self.messages.push(Message { role: Role::System, content: format!("❌ Error: {}", message) });
                    self.streaming.clear();
                    self.thinking.clear();
                }
                self.activity = Activity::Idle;
                self.activity_detail.clear();
                
                // Check queue after error - user may want to retry
                if !self.pending_messages.is_empty() {
                    self.messages.push(Message {
                        role: Role::System,
                        content: format!("⚠️ Queue paused ({} messages). Send '/retry' to process.", self.pending_messages.len())
                    });
                }
            }
            EventType::Usage => {
                if let Some(EventData::Usage { input_tokens, output_tokens, cache_creation_input_tokens, cache_read_input_tokens }) = e.data {
                    self.tokens_in = input_tokens;
                    self.tokens_out = output_tokens;
                    self.session_total_out += output_tokens;
                    
                    // Update cache stats (only when actually reported by API)
                    // Note: DashScope doesn't support prompt caching, so these may always be 0
                    let cache_read = cache_read_input_tokens.unwrap_or(0);
                    let cache_created = cache_creation_input_tokens.unwrap_or(0);
                    
                    self.cache_read += cache_read;
                    self.cache_created += cache_created;
                    self.api_calls += 1;
                }
            }
            EventType::CompressionCompleted => {
                if let Some(EventData::Compression { original_tokens, compressed_tokens, ratio }) = e.data {
                    self.compressions += 1;
                    // Update token display to reflect compressed state
                    self.tokens_in = compressed_tokens;
                    self.messages.push(Message {
                        role: Role::System,
                        content: format!("📦 Compressed: {}{} tokens ({:.0}% saved)\n  Context: {} tokens remaining",
                            fmt_tokens(original_tokens), fmt_tokens(compressed_tokens), (1.0 - ratio) * 100.0,
                            fmt_tokens(compressed_tokens))
                    });
                    self.auto_scroll = true;
                }
            }
            EventType::CompressionTriggered => {
                if let Some(EventData::Progress { message, .. }) = e.data {
                    self.messages.push(Message {
                        role: Role::System,
                        content: format!("{}", message)
                    });
                    self.auto_scroll = true;
                }
            }
            EventType::Progress => {
                if let Some(EventData::Progress { message, .. }) = e.data {
                    self.messages.push(Message {
                        role: Role::System,
                        content: message
                    });
                    self.auto_scroll = true;
                }
            }
            EventType::MemoryLoaded => {
                if let Some(EventData::Memory { entries_count, .. }) = e.data
                    && entries_count > 0 {
                    self.memory_saves += 1;
                    self.messages.push(Message {
                        role: Role::System,
                        content: format!("🧠 Memory: {} entries", entries_count)
                    });
                    self.auto_scroll = true;
                }
            }
            EventType::MemoryDetected => {
                if let Some(EventData::Memory { summary, entries_count }) = e.data {
                    self.memory_saves += 1;
                    self.messages.push(Message {
                        role: Role::System,
                        content: format!("🧠 Detected {} memories: {}", entries_count, summary)
                    });
                    self.auto_scroll = true;
                }
            }
            EventType::KeywordsExtracted => {
                // Keywords extraction is an internal operation, don't show to user
                // Only update internal state if needed (for debug mode, could show)
                if self.debug_mode {
                    if let Some(EventData::Keywords { keywords, source }) = e.data {
                        let preview = truncate(&source, 50);
                        self.messages.push(Message {
                            role: Role::System,
                            content: format!("🔍 Keywords: {} from '{}'", keywords.join(", "), preview)
                        });
                    }
                }
            }
            EventType::AskQuestion => {
                if let Some(EventData::AskQuestion { question, options }) = e.data {
                    // Detect if this is an approval request
                    let is_approval = question.contains("requires approval") || question.contains("Allow?");
                    let has_options = options.is_some();
                    
                    // Format the display
                    let mut content = if is_approval {
                        // Extract tool name and risk level
                        let lines: Vec<&str> = question.lines().collect();
                        let header = lines.first().copied().unwrap_or("");
                        let detail = lines.get(1).copied().unwrap_or("");
                        format!("{}\n{}", header, detail)
                    } else if has_options {
                        format!("{}", question)
                    } else {
                        question.clone()
                    };
                    
                    // Show options if available
                    if let Some(ref opts) = options && let Some(arr) = opts.as_array() {
                        content.push_str("\n\nOptions:");
                        for opt in arr {
                            let id = opt["id"].as_str().unwrap_or("?");
                            let label = opt["label"].as_str().unwrap_or("");
                            let desc = opt["description"].as_str().unwrap_or("");
                            let desc_text = if desc.is_empty() { String::new() } else { format!("({})", desc) };
                            content.push_str(&format!("\n  [{}] {} {}", id, label, desc_text));
                        }
                    }
                    
                    // Add hint for approval
                    if is_approval {
                        content.push_str("\n\n[y] Approve  [n] Reject  [a] Abort");
                    } else if has_options {
                        content.push_str("\n\nEnter option ID (e.g., 'A' or 'B')");
                    }
                    
                    self.messages.push(Message { role: Role::System, content });
                    self.waiting_for_ask = true;
                    self.activity = Activity::Asking;
                    self.auto_scroll = true;
                }
            }
            _ => {}
        }
    }
}