claux 20260706.0.0

Terminal AI coding assistant with tool execution
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
//! Shared UI drawing helpers for the TUI.

use ratatui::{
    layout::{Constraint, Direction, Layout},
    style::{Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph, Wrap},
    Frame,
};

use super::chat::{ChatApp, ChatMessage, Mode, ToolStatus};
use super::markdown;

/// Rendered lines for `ChatApp::messages`, cached across frames.
///
/// Rendering history means a markdown parse per message plus a word-wrap
/// pass over everything; at a few hundred messages that costs tens of
/// milliseconds, and draw_chat runs on a 50ms tick while streaming. The
/// cache makes each frame O(streaming tail): it is rebuilt only when the
/// message list changes (`rev`) or the text width changes.
pub struct HistoryCache {
    pub rev: u64,
    pub width: u16,
    pub lines: Vec<Line<'static>>,
    /// Rendered row count of each line at `width`, so the draw path can
    /// select just the visible window without re-wrapping history.
    pub line_rows: Vec<u16>,
    pub rows: u16,
}

/// Exact rendered row count for one line word-wrapped at `width`.
/// WordWrapper wraps logical lines independently, so per-line counts of
/// separate slices add up exactly.
fn count_line_rows(line: &Line<'static>, width: u16) -> u16 {
    Paragraph::new(vec![line.clone()])
        .wrap(Wrap { trim: false })
        .line_count(width) as u16
}

/// Draw the chat screen.
pub fn draw_chat(f: &mut Frame, app: &mut ChatApp) {
    let input_height = if app.permission_details.is_some() {
        let detail_lines = app
            .permission_details
            .as_ref()
            .map(|d| d.len())
            .unwrap_or(0);
        // Content: summary + blank + details + blank + y/n/a options,
        // plus 2 border rows. Undersizing this clips the options line,
        // leaving the user with a question and no visible answers.
        (detail_lines as u16 + 6).min(f.area().height / 2)
    } else {
        3
    };

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1),
            Constraint::Min(1),
            Constraint::Length(input_height),
            Constraint::Length(1),
        ])
        .split(f.area());

    // Header
    let header = Paragraph::new(Line::from(vec![
        Span::styled(
            " claux ",
            Style::default()
                .fg(app.theme.assistant_bold)
                .add_modifier(Modifier::BOLD),
        ),
        Span::styled(
            format!("v{}", app.version),
            Style::default().fg(app.theme.dim),
        ),
    ]));
    f.render_widget(header, chunks[0]);

    // Messages area
    let msg_area = chunks[1];
    let inner_width = msg_area.width.saturating_sub(2).max(1);

    // Rebuild the history cache only when messages or width changed
    let cache_valid = app
        .history_cache
        .as_ref()
        .is_some_and(|c| c.rev == app.messages_rev && c.width == inner_width);
    if !cache_valid {
        let lines = history_lines(app);
        let line_rows: Vec<u16> = lines
            .iter()
            .map(|l| count_line_rows(l, inner_width))
            .collect();
        let rows = line_rows.iter().sum();
        app.history_cache = Some(HistoryCache {
            rev: app.messages_rev,
            width: inner_width,
            lines,
            line_rows,
            rows,
        });
    }

    // Streaming buffer (the per-frame tail, rendered fresh each draw)
    let history_empty = app.messages.is_empty();
    let mut tail_lines: Vec<Line> = Vec::new();
    if !app.stream_buffer.is_empty() {
        if !history_empty {
            tail_lines.push(Line::from(""));
        }
        tail_lines.push(Line::from(vec![
            Span::styled("", Style::default().fg(app.theme.success)),
            Span::styled(
                format!("{} ", app.model),
                Style::default()
                    .fg(app.theme.success)
                    .add_modifier(Modifier::BOLD),
            ),
        ]));
        let rendered = markdown::render(&app.stream_buffer, Style::default().fg(app.theme.success));
        for line in rendered {
            let mut indented = vec![Span::raw("  ")];
            indented.extend(line.spans);
            tail_lines.push(Line::from(indented));
        }
        tail_lines.push(Line::from(Span::styled(
            "",
            Style::default().fg(app.theme.success),
        )));
    }
    let tail_rows: Vec<u16> = tail_lines
        .iter()
        .map(|l| count_line_rows(l, inner_width))
        .collect();

    if !app.manual_scroll {
        app.scroll = 0;
    }
    let visible_height = msg_area.height;
    let manual_scroll = app.manual_scroll;
    let user_scroll = app.scroll;

    // Select only the logical lines that intersect the viewport and hand
    // the renderer a local offset. Passing everything makes the render
    // itself O(history): ratatui re-wraps every row above the scroll
    // offset each frame to find the window.
    let (render_lines, local_offset, total_rows) = {
        let cache = app
            .history_cache
            .as_ref()
            .expect("history cache just built");

        let total_rows = cache.rows + tail_rows.iter().sum::<u16>();
        let max_scroll = total_rows.saturating_sub(visible_height);
        let scroll_offset = if manual_scroll {
            max_scroll.saturating_sub(user_scroll.min(max_scroll))
        } else {
            max_scroll
        };

        let line_count = cache.lines.len() + tail_lines.len();
        let rows_at = |i: usize| -> u16 {
            if i < cache.line_rows.len() {
                cache.line_rows[i]
            } else {
                tail_rows[i - cache.line_rows.len()]
            }
        };

        // Skip whole lines that end above the viewport
        let mut first = 0usize;
        let mut skipped: u16 = 0;
        while first < line_count && skipped + rows_at(first) <= scroll_offset {
            skipped += rows_at(first);
            first += 1;
        }
        let local_offset = scroll_offset - skipped;

        // Take lines until the viewport is covered
        let mut render_lines: Vec<Line<'static>> = Vec::new();
        let mut covered: u16 = 0;
        let needed = local_offset.saturating_add(visible_height);
        let mut i = first;
        while i < line_count && covered < needed {
            let line = if i < cache.lines.len() {
                cache.lines[i].clone()
            } else {
                tail_lines[i - cache.lines.len()].clone()
            };
            covered += rows_at(i);
            render_lines.push(line);
            i += 1;
        }

        (render_lines, local_offset, total_rows)
    };

    app.total_lines = total_rows;

    let messages_widget = Paragraph::new(render_lines)
        .block(
            Block::default()
                .borders(Borders::LEFT | Borders::RIGHT)
                .border_style(Style::default().fg(app.theme.dim)),
        )
        .wrap(Wrap { trim: false })
        .scroll((local_offset, 0));
    f.render_widget(messages_widget, msg_area);

    draw_input_and_status(f, app, &chunks);
}

/// Render `app.messages` into styled lines. Called only on cache misses.
fn history_lines(app: &ChatApp) -> Vec<Line<'static>> {
    let mut lines: Vec<Line> = Vec::new();

    for msg in &app.messages {
        if !lines.is_empty() {
            lines.push(Line::from(""));
        }

        match msg {
            ChatMessage::Text { role, content } => match role.as_str() {
                "user" => {
                    let bubble_lines: Vec<Line> = content
                        .lines()
                        .map(|line| {
                            Line::from(Span::styled(
                                format!("  {line}"),
                                Style::default()
                                    .fg(app.theme.user_message_fg)
                                    .bg(app.theme.user_message_bg),
                            ))
                        })
                        .collect();

                    lines.push(Line::from(vec![
                        Span::styled("", Style::default().fg(app.theme.user)),
                        Span::styled(
                            "You",
                            Style::default()
                                .fg(app.theme.user)
                                .add_modifier(Modifier::BOLD),
                        ),
                    ]));
                    for line in bubble_lines {
                        lines.push(line);
                    }
                }
                "assistant" => {
                    lines.push(Line::from(vec![
                        Span::styled("", Style::default().fg(app.theme.assistant)),
                        Span::styled(
                            format!("{} ", app.model),
                            Style::default()
                                .fg(app.theme.assistant_bold)
                                .add_modifier(Modifier::BOLD),
                        ),
                    ]));
                    let rendered = markdown::render(content, Style::default().fg(app.theme.fg));
                    for line in rendered {
                        let mut indented = vec![Span::raw("  ")];
                        indented.extend(line.spans);
                        lines.push(Line::from(indented));
                    }
                }
                "system" => {
                    lines.push(Line::from(Span::styled(
                        "",
                        Style::default().fg(app.theme.warning),
                    )));
                    for line in content.lines() {
                        lines.push(Line::from(Span::styled(
                            format!("  {line}"),
                            Style::default().fg(app.theme.warning),
                        )));
                    }
                }
                "error" => {
                    lines.push(Line::from(Span::styled(
                        "",
                        Style::default().fg(app.theme.error),
                    )));
                    for line in content.lines() {
                        lines.push(Line::from(Span::styled(
                            format!("  {line}"),
                            Style::default().fg(app.theme.error),
                        )));
                    }
                }
                _ => {
                    lines.push(Line::from(Span::styled(
                        "",
                        Style::default().fg(app.theme.dim),
                    )));
                    for line in content.lines() {
                        lines.push(Line::from(Span::styled(
                            format!("  {line}"),
                            Style::default().fg(app.theme.fg),
                        )));
                    }
                }
            },
            ChatMessage::Tool {
                name,
                summary,
                status,
            } => {
                let (indicator, indicator_color) = match status {
                    ToolStatus::Running => ("", app.theme.warning),
                    ToolStatus::Success => ("", app.theme.tool_success),
                    ToolStatus::Error => ("", app.theme.tool_error),
                };
                lines.push(Line::from(vec![
                    Span::styled(
                        format!("{indicator} "),
                        Style::default().fg(indicator_color),
                    ),
                    Span::styled(
                        format!("{name} "),
                        Style::default()
                            .fg(app.theme.tool_name)
                            .add_modifier(Modifier::BOLD),
                    ),
                    Span::styled(summary.clone(), Style::default().fg(app.theme.tool_summary)),
                ]));
            }
        }
    }

    lines
}

/// Draw the input (or permission) box and the status bar.
fn draw_input_and_status(f: &mut Frame, app: &mut ChatApp, chunks: &[ratatui::layout::Rect]) {
    // Input area
    if let (Some(ref prompt), Some(ref details)) = (&app.permission_prompt, &app.permission_details)
    {
        let mut perm_lines: Vec<Line> = Vec::new();
        perm_lines.push(Line::from(vec![
            Span::styled("", Style::default().fg(app.theme.warning)),
            Span::styled(
                prompt.as_str(),
                Style::default()
                    .fg(app.theme.warning)
                    .add_modifier(Modifier::BOLD),
            ),
        ]));
        perm_lines.push(Line::from(""));
        for detail in details {
            let style = if detail.starts_with("  +") {
                Style::default().fg(app.theme.success)
            } else if detail.starts_with("  -") {
                Style::default().fg(app.theme.error)
            } else if detail.ends_with(':') {
                Style::default()
                    .fg(app.theme.fg)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(app.theme.dim)
            };
            perm_lines.push(Line::from(Span::styled(detail.clone(), style)));
        }
        perm_lines.push(Line::from(""));
        perm_lines.push(Line::from(vec![
            Span::styled("  (y)es  ", Style::default().fg(app.theme.success)),
            Span::styled("(n)o  ", Style::default().fg(app.theme.error)),
            Span::styled("(a)lways allow", Style::default().fg(app.theme.warning)),
        ]));

        let perm_widget = Paragraph::new(perm_lines).block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(app.theme.warning))
                .title(" Permission Required "),
        );
        f.render_widget(perm_widget, chunks[2]);
    } else {
        let input_text = if app.mode == Mode::Streaming {
            let steer = app.steer_buf.lock().expect("steer buffer poisoned");
            if steer.is_empty() {
                "... (type to steer, Enter to queue, Ctrl+C to interrupt)".to_string()
            } else {
                steer.clone()
            }
        } else {
            app.input.clone()
        };

        let input_widget = Paragraph::new(input_text)
            .style(Style::default().fg(app.theme.fg))
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .border_style(Style::default().fg(if app.mode == Mode::Input {
                        app.theme.user
                    } else {
                        app.theme.dim
                    }))
                    .title(" > "),
            );
        f.render_widget(input_widget, chunks[2]);
    }

    if app.mode == Mode::Input {
        f.set_cursor_position((chunks[2].x + app.cursor as u16 + 1, chunks[2].y + 1));
    }

    // Status bar
    let thinking_indicator = if app.thinking {
        let spinner = ["", "", "", "", "", "", "", "", "", ""];
        let idx = (chrono::Local::now().timestamp_millis() / 100) as usize % spinner.len();
        format!(" {} ", spinner[idx])
    } else {
        " ".to_string()
    };

    let status = Paragraph::new(Line::from(vec![
        Span::styled(thinking_indicator, Style::default().fg(app.theme.success)),
        Span::styled(
            format!(" {} ", app.status),
            Style::default().fg(app.theme.dim),
        ),
    ]));
    f.render_widget(status, chunks[3]);
}

#[cfg(test)]
mod perf_probe {
    use super::*;
    use crate::theme::Theme;

    #[test]
    fn time_draw_with_large_history() {
        let mut app = ChatApp::new("test-model", Theme::dark());
        for i in 0..150 {
            app.add_message(
                "user",
                &format!(
                    "question {i} about the codebase, with some length to it so wrapping happens"
                ),
            );
            app.add_message(
                "assistant",
                &format!("Here is a **detailed** answer {i} with `inline code`, a list:\n- point one about the design\n- point two with more words that will wrap on narrow terminals\n\nAnd a table:\n| a | b |\n|---|---|\n| {i} | value |\n\nPlus a trailing paragraph that is long enough to wrap at eighty columns for sure, repeating itself to add width and weight to the rendering cost."),
            );
            app.add_tool("Bash", "cargo test", ToolStatus::Success);
        }
        app.stream_buffer = "streaming tail ".repeat(20);
        app.mode = Mode::Streaming;

        // warm up
        let _ = tuishot::render_to_buffer(120, 40, |f| draw_chat(f, &mut app));

        let n = 20;
        let start = std::time::Instant::now();
        for _ in 0..n {
            let _ = tuishot::render_to_buffer(120, 40, |f| draw_chat(f, &mut app));
        }
        let per_draw = start.elapsed() / n;
        println!("per-draw: {per_draw:?} for {} messages", app.messages.len());

        // Regression guard: warm-cache draws must stay O(viewport), not
        // O(history). Before the history cache + viewport slicing this
        // measured ~58ms in a debug build; sliced it's ~3ms. The bound is
        // loose to tolerate slow CI, but tight enough to catch a return
        // to per-frame full-history rendering.
        assert!(
            per_draw < std::time::Duration::from_millis(25),
            "draw with large history too slow: {per_draw:?}"
        );
    }
}