matrixcode-tui 0.4.13

MatrixCode TUI - Terminal UI library for AI Code Agent
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
use std::collections::HashMap;
use std::io::Stdout;
use std::time::{Duration, Instant};

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

use matrixcode_core::{AgentEvent, cancel::CancellationToken};

use crate::ANIM_MS;
use crate::types::{Activity, ApproveMode, AskQuestion, Message, Role, SubmitMode};

pub struct TuiApp {
    pub(crate) activity: Activity,
    pub(crate) activity_detail: String,
    /// Full tool input for display (not truncated)
    pub(crate) activity_input: Option<serde_json::Value>,
    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) current_request_tokens: u64, // Tokens for current request (real-time)
    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,
    // Timing
    pub(crate) request_start: Option<Instant>,
    pub(crate) tool_start: Option<Instant>, // When current tool execution started
    // 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,
    // Input history (Up/Down arrow navigation)
    pub(crate) input_history: Vec<String>,
    pub(crate) history_index: Option<usize>, // None = not browsing history
    pub(crate) history_draft: String,        // Saves current input when entering history mode
    // Scroll state
    pub(crate) scroll_offset: u16,
    pub(crate) auto_scroll: bool,
    pub(crate) max_scroll: std::cell::Cell<u16>,
    pub(crate) new_message_while_scrolled: std::cell::Cell<bool>, // Flag for notification when scrolled up
    // Thinking display state
    pub(crate) thinking_collapsed: bool,
    // Dirty flag for rendering optimization - only redraw when something changed
    pub(crate) dirty: std::cell::Cell<bool>,
    // Approval mode
    pub(crate) approve_mode: ApproveMode,
    // Shared approve mode atomic - directly updates agent's mode in real-time
    pub(crate) shared_approve_mode: Option<std::sync::Arc<std::sync::atomic::AtomicU8>>,
    // Ask tool channel
    pub(crate) ask_tx: Option<tokio::sync::mpsc::Sender<String>>,
    pub(crate) waiting_for_ask: bool,
    pub(crate) ask_options: Vec<crate::types::AskOption>,
    pub(crate) ask_selected_index: usize,
    pub(crate) ask_multi_select: bool, // Whether this is a multi-select question
    pub(crate) ask_submit_mode: SubmitMode, // How to submit selection
    pub(crate) ask_other_input_active: bool, // Whether user is typing custom input for "Other" option
    // Multi-question support
    pub(crate) ask_questions: Vec<AskQuestion>, // Queue of questions
    pub(crate) current_question_idx: usize,     // Current question index
    // Todo tracking for progress display
    pub(crate) todo_items: Vec<TodoItem>,
    // 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>,
    // Debug mode
    pub(crate) debug_mode: bool,
}

/// Todo item for progress tracking
#[derive(Clone)]
#[allow(dead_code)]  // Fields used in serialization, not directly read
pub struct TodoItem {
    pub content: String,
    pub status: String, // "pending", "in_progress", "completed"
}

/// 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(),
            activity_input: None,
            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,
            current_request_tokens: 0,
            cache_read: 0,
            cache_created: 0,
            context_size: 200_000,
            api_calls: 0,
            compressions: 0,
            memory_saves: 0,
            tool_calls: 0,
            request_start: None,
            tool_start: None,
            frame: 0,
            last_anim: Instant::now(),
            show_welcome: true,
            exit: false,
            cursor_pos: 0,
            input_history: Vec::new(),
            history_index: None,
            history_draft: String::new(),
            scroll_offset: 0,
            auto_scroll: true,
            max_scroll: std::cell::Cell::new(0),
            new_message_while_scrolled: std::cell::Cell::new(false),
            thinking_collapsed: false, // Default: expanded to show thinking content
            dirty: std::cell::Cell::new(true), // Initial render needed
            approve_mode: ApproveMode::Ask,
            shared_approve_mode: None,
            ask_tx: None,
            waiting_for_ask: false,
            ask_options: Vec::new(),
            ask_selected_index: 0,
            ask_multi_select: false,
            ask_submit_mode: SubmitMode::default(),
            ask_other_input_active: false,
            ask_questions: Vec::new(),
            current_question_idx: 0,
            todo_items: Vec::new(),
            tx,
            rx,
            cancel,
            pending_messages: Vec::new(),
            loop_task: None,
            cron_tasks: Vec::new(),
            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
    }

    /// Set shared approve mode atomic for real-time mode switching during agent execution.
    pub fn with_shared_approve_mode(
        mut self,
        shared: std::sync::Arc<std::sync::atomic::AtomicU8>,
    ) -> Self {
        self.shared_approve_mode = Some(shared);
        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
    }

    /// Set debug mode from environment or config
    pub fn with_debug_mode(mut self, debug_mode: bool) -> Self {
        self.debug_mode = debug_mode;
        self
    }

    pub fn load_messages(&mut self, core_messages: Vec<matrixcode_core::Message>) {
        // Build mapping from tool_use_id to tool name
        let mut tool_names: HashMap<String, String> = HashMap::new();

        // First pass: collect tool names from ToolUse blocks
        for msg in &core_messages {
            if let matrixcode_core::MessageContent::Blocks(blocks) = &msg.content {
                for b in blocks {
                    if let matrixcode_core::ContentBlock::ToolUse { id, name, .. } = b {
                        tool_names.insert(id.clone(), name.clone());
                    }
                }
            }
        }

        // Second pass: process messages
        for msg in core_messages {
            // Handle different content block types separately
            match &msg.content {
                matrixcode_core::MessageContent::Text(t) => {
                    if t.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(),
                            detail: None,
                            is_error: false,
                        },
                    };
                    // Restore input history from user messages
                    if role == Role::User
                        && !t.starts_with('/')
                        && self.input_history.last().map(|s| s.as_str()) != Some(t)
                    {
                        self.input_history.push(t.clone());
                    }
                    self.messages.push(Message {
                        role,
                        content: t.clone(),
                    });
                }
                matrixcode_core::MessageContent::Blocks(blocks) => {
                    // Process each block separately to maintain proper message types
                    for b in blocks {
                        match b {
                            matrixcode_core::ContentBlock::Text { text } => {
                                if text.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(),
                                        detail: None,
                                        is_error: false,
                                    },
                                };
                                // Restore input history from user messages
                                if role == Role::User
                                    && !text.starts_with('/')
                                    && self.input_history.last().map(|s| s.as_str()) != Some(text)
                                {
                                    self.input_history.push(text.clone());
                                }
                                self.messages.push(Message {
                                    role,
                                    content: text.clone(),
                                });
                            }
                            matrixcode_core::ContentBlock::Thinking { thinking, .. } => {
                                if thinking.is_empty() {
                                    continue;
                                }
                                // Create separate Thinking message for proper rendering
                                self.messages.push(Message {
                                    role: Role::Thinking,
                                    content: thinking.clone(),
                                });
                            }
                            matrixcode_core::ContentBlock::ToolUse { name: _, .. } => {
                                // Skip tool_use blocks - metadata only (already collected in first pass)
                            }
                            matrixcode_core::ContentBlock::ToolResult {
                                content,
                                tool_use_id,
                                ..
                            } => {
                                if content.is_empty() {
                                    continue;
                                }
                                // Try to determine if this is an error from content
                                let is_error = content.contains("error")
                                    || content.contains("failed")
                                    || content.contains("Error");
                                // Use tool name from mapping, or fallback to tool_use_id
                                let name =
                                    tool_names.get(tool_use_id).cloned().unwrap_or_else(|| {
                                        // Fallback: try to guess from tool_use_id prefix
                                        if tool_use_id.starts_with("bash") {
                                            "bash".into()
                                        } else if tool_use_id.starts_with("read") {
                                            "read".into()
                                        } else if tool_use_id.starts_with("write") {
                                            "write".into()
                                        } else if tool_use_id.starts_with("edit") {
                                            "edit".into()
                                        } else {
                                            "tool".into()
                                        }
                                    });
                                self.messages.push(Message {
                                    role: Role::Tool {
                                        name,
                                        detail: None,
                                        is_error,
                                    },
                                    content: content.clone(),
                                });
                            }
                            _ => {}
                        }
                    }
                }
            }
        }
        if !self.messages.is_empty() {
            self.show_welcome = false;
        }
    }

    /// Set token stats from restored session metadata.
    pub fn set_token_stats(&mut self, input_tokens: u64, total_output_tokens: u64, _message_count: usize) {
        self.tokens_in = input_tokens;
        self.session_total_out = total_output_tokens;
    }

    pub fn run(&mut self, term: &mut Terminal<CrosstermBackend<Stdout>>) -> Result<()> {
        loop {
            // Animation frame - cycle through 10 frames for spinner
            // Always render when animation frame updates (for spinner)
            let anim_update = self.last_anim.elapsed().as_millis() >= ANIM_MS as u128;
            if anim_update {
                self.frame = (self.frame + 1) % 10;
                self.last_anim = Instant::now();
                self.dirty.set(true);
            }

            // Handle events - mark dirty on any user input
            if event::poll(Duration::from_millis(ANIM_MS as u64))? {
                match event::read()? {
                    Event::Key(k) => {
                        self.on_key(k);
                        self.dirty.set(true);
                    }
                    Event::Mouse(m) => {
                        self.on_mouse(m);
                        self.dirty.set(true);
                    }
                    Event::Paste(text) => {
                        self.on_paste(&text);
                        self.dirty.set(true);
                    }
                    _ => {}
                }
            }

            // Process agent events - mark dirty on any event
            let mut had_event = false;
            while let Ok(e) = self.rx.try_recv() {
                log::debug!("TUI received event: type={:?}", e.event_type);
                self.on_event(e);
                had_event = true;
            }
            if had_event {
                log::debug!("TUI: had events, marking dirty");
                self.dirty.set(true);
            }

            // Only render if dirty (something changed)
            if self.dirty.get() {
                term.draw(|f| self.draw(f))?;
                self.dirty.set(false);
            }

            if self.exit {
                break;
            }
        }
        Ok(())
    }
    fn on_mouse(&mut self, m: MouseEvent) {
        // If Shift is held, let terminal handle mouse for text selection
        if m.modifiers.contains(event::KeyModifiers::SHIFT) {
            return;
        }

        match m.kind {
            MouseEventKind::ScrollUp => {
                if self.auto_scroll {
                    self.auto_scroll = false;
                    self.scroll_offset = self.max_scroll.get().max(50);
                }
                self.scroll_offset = self.scroll_offset.saturating_sub(3);
            }
            MouseEventKind::ScrollDown => {
                if !self.auto_scroll {
                    self.scroll_offset = self.scroll_offset.saturating_add(3);
                    let max = self.max_scroll.get();
                    if max > 0 && self.scroll_offset >= max {
                        self.auto_scroll = true;
                        self.scroll_offset = 0;
                    }
                }
            }
            _ => {}
        }
    }
}