oxi-tui 0.62.0

Terminal UI widgets and theme system for oxi, built on ratatui
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
//! Width-aware projection of chat messages into memoized ANSI tape rows.

use std::hash::{Hash, Hasher};

use ratatui::text::{Line, Span};

use crate::{
    render::terminal::TerminalCapabilities,
    theme::Theme,
    widgets::{
        chat::{ChatMessage, ContentBlock, MessageRole, StreamingState, ToolCallStatus},
        tool_renderer::{format_tool_call, format_tool_result},
    },
};

use super::{Component, Container, LiveRegion, RenderResult, style::styled_line_to_ansi};

struct MessageComponent {
    message: ChatMessage,
    streaming: bool,
    theme: Theme,
    caps: TerminalCapabilities,
    revision: u64,
}

impl MessageComponent {
    fn new(
        message: ChatMessage,
        streaming: bool,
        theme: &Theme,
        caps: &TerminalCapabilities,
    ) -> Self {
        Self {
            revision: message_revision(&message),
            message,
            streaming,
            theme: theme.clone(),
            caps: caps.clone(),
        }
    }

    fn styled_lines(&self, width: u16) -> Vec<Line<'static>> {
        let styles = self.theme.to_styles();
        let mut lines = Vec::new();
        let role_style = match self.message.role {
            MessageRole::User => styles.primary,
            MessageRole::Assistant => styles.normal,
            MessageRole::System => styles.muted,
        };
        for block in &self.message.content_blocks {
            match block {
                ContentBlock::Text { content } => {
                    let mut rendered =
                        crate::widgets::chat::markdown::md_lines(content, width, &styles);
                    for line in &mut rendered {
                        line.style = role_style.patch(line.style);
                    }
                    lines.extend(rendered);
                }
                ContentBlock::Thinking { content, collapsed } => {
                    let marker = if *collapsed {
                        styles.symbols.nav_expand
                    } else {
                        styles.symbols.nav_collapse
                    };
                    lines.push(Line::from(Span::styled(
                        format!("{marker} Thinking"),
                        styles.muted,
                    )));
                    if !collapsed {
                        lines.extend(crate::widgets::chat::markdown::md_lines(
                            content,
                            width.saturating_sub(2),
                            &styles,
                        ));
                    }
                }
                ContentBlock::ToolCall {
                    name,
                    arguments,
                    result,
                    status,
                    duration,
                    ..
                } => {
                    lines.extend(format_tool_call(name, arguments, width as usize, &styles));
                    if matches!(status, ToolCallStatus::Executing) {
                        lines.push(Line::from(Span::styled(
                            format!(
                                "  {} running{}",
                                styles.symbols.status_running,
                                duration
                                    .as_deref()
                                    .map_or(String::new(), |d| format!(" · {d}"))
                            ),
                            styles.muted,
                        )));
                    }
                    if let Some((output, is_error)) = result {
                        lines.extend(format_tool_result(
                            name,
                            output,
                            *is_error,
                            Some(arguments),
                            width as usize,
                            &styles,
                        ));
                    }
                }
                ContentBlock::ToolResult {
                    tool_name,
                    content,
                    is_error,
                } => {
                    lines.extend(format_tool_result(
                        tool_name,
                        content,
                        *is_error,
                        None,
                        width as usize,
                        &styles,
                    ));
                }
                ContentBlock::Error {
                    title,
                    message,
                    retryable,
                } => {
                    let suffix = if *retryable { " (retryable)" } else { "" };
                    lines.push(Line::from(Span::styled(
                        format!("{} {title}{suffix}", styles.symbols.status_error),
                        styles.error,
                    )));
                    lines.extend(
                        message
                            .lines()
                            .map(|line| Line::from(Span::styled(line.to_string(), styles.error))),
                    );
                }
                ContentBlock::Image {
                    mime_type,
                    base64_data,
                } => {
                    // Tape rows are ANSI-terminated and width-measured as text. Raw
                    // Kitty/iTerm2 escapes here would be counted as cells and their
                    // protocol terminators would collide with LINE_TERMINATOR.
                    // Keep this safe placeholder; the post-paint image writer and
                    // overlay viewer remain the raw-output paths.
                    lines.push(Line::from(Span::styled(
                        format!(
                            "{} image {mime_type} ({} bytes)",
                            styles.symbols.icon_file,
                            base64_data.len()
                        ),
                        styles.muted,
                    )));
                }
                ContentBlock::Dashboard { info } => {
                    lines.extend(crate::widgets::chat::dashboard::dashboard_lines(
                        info, width, &styles,
                    ));
                }
                ContentBlock::Advisory { body, severity, .. } => {
                    // Severity-colored badge card. `AdvisorSeverity` is mirrored
                    // in `oxi_tui::widgets::chat::types` to keep `oxi-tui` free
                    // of `oxi-agent` deps; severity mapping matches the plan:
                    // Nit -> muted, Concern -> warning, Blocker -> error.
                    let (label, badge_style) = match severity {
                        crate::widgets::chat::types::AdvisorSeverity::Nit => ("NIT", styles.muted),
                        crate::widgets::chat::types::AdvisorSeverity::Concern => {
                            ("CONCERN", styles.warning)
                        }
                        crate::widgets::chat::types::AdvisorSeverity::Blocker => {
                            ("BLOCKER", styles.error)
                        }
                    };
                    let prefix = format!("[{}] ", label);
                    let body_style = match severity {
                        crate::widgets::chat::types::AdvisorSeverity::Nit => styles.muted,
                        crate::widgets::chat::types::AdvisorSeverity::Concern => styles.warning,
                        crate::widgets::chat::types::AdvisorSeverity::Blocker => styles.error,
                    };
                    lines.push(Line::from(vec![
                        Span::styled(prefix, badge_style),
                        Span::styled(body.clone(), body_style),
                    ]));
                }
            }
        }
        lines
    }
}

impl Component for MessageComponent {
    fn render(&self, width: u16) -> RenderResult {
        RenderResult::new(
            self.styled_lines(width)
                .iter()
                .map(|line| styled_line_to_ansi(line, &self.caps))
                .collect(),
        )
    }

    fn revision(&self) -> u64 {
        self.revision
    }

    fn live_region(&self) -> LiveRegion {
        if self.streaming {
            LiveRegion::Mutable { start: 0 }
        } else {
            LiveRegion::None
        }
    }
}

fn message_revision(message: &ChatMessage) -> u64 {
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    format!("{message:?}").hash(&mut hasher);
    hasher.finish()
}

/// Memoized projection of finalized and streaming chat messages.
pub struct TranscriptRenderer {
    container: Container,
    fingerprint: u64,
}

impl TranscriptRenderer {
    /// Create an empty transcript renderer.
    #[must_use]
    pub fn new() -> Self {
        Self {
            container: Container::new(),
            fingerprint: 0,
        }
    }

    /// Synchronize the projection with canonical chat state.
    pub fn sync(
        &mut self,
        messages: &[ChatMessage],
        streaming: Option<&StreamingState>,
        theme: &Theme,
        caps: &TerminalCapabilities,
    ) {
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        format!("{theme:?}").hash(&mut hasher);
        format!("{caps:?}").hash(&mut hasher);
        for message in messages {
            message_revision(message).hash(&mut hasher);
        }
        streaming
            .map(|s| message_revision(&s.message))
            .hash(&mut hasher);
        let fingerprint = hasher.finish();
        if fingerprint == self.fingerprint {
            return;
        }

        self.container.clear();
        for message in messages {
            self.container.add(Box::new(MessageComponent::new(
                message.clone(),
                false,
                theme,
                caps,
            )));
        }
        if let Some(streaming) = streaming {
            self.container.add(Box::new(MessageComponent::new(
                streaming.message.clone(),
                true,
                theme,
                caps,
            )));
        }
        self.fingerprint = fingerprint;
    }

    /// Compose width-aware ANSI rows and live boundary.
    pub fn compose(&mut self, width: u16) -> (&RenderResult, LiveRegion) {
        self.container.compose(width)
    }
}

impl Default for TranscriptRenderer {
    fn default() -> Self {
        Self::new()
    }
}
#[cfg(test)]
mod tests {
    use super::*;

    fn msg(block: ContentBlock) -> ChatMessage {
        ChatMessage {
            role: MessageRole::Assistant,
            content_blocks: vec![block],
            timestamp: 0,
        }
    }

    #[test]
    fn text_wraps_to_width_and_stream_is_live() {
        let mut renderer = TranscriptRenderer::new();
        let streaming = StreamingState {
            message: msg(ContentBlock::Text {
                content: "one two three four".into(),
            }),
        };
        renderer.sync(
            &[],
            Some(&streaming),
            &Theme::dark(),
            &TerminalCapabilities::default(),
        );
        let (result, live) = renderer.compose(8);
        assert!(result.lines.len() > 1);
        assert_eq!(live, LiveRegion::Mutable { start: 0 });
    }

    #[test]
    fn every_block_variant_renders() {
        let blocks = vec![
            ContentBlock::Thinking {
                content: "reason".into(),
                collapsed: false,
            },
            ContentBlock::ToolCall {
                id: "1".into(),
                name: "read".into(),
                arguments: "{\"path\":\"a\"}".into(),
                result: Some(("ok".into(), false)),
                status: ToolCallStatus::Done,
                duration: Some("1ms".into()),
            },
            ContentBlock::ToolResult {
                tool_name: "read".into(),
                content: "ok".into(),
                is_error: false,
            },
            ContentBlock::Error {
                title: "bad".into(),
                message: "detail".into(),
                retryable: true,
            },
            ContentBlock::Image {
                mime_type: "image/png".into(),
                base64_data: "YWJj".into(),
            },
            ContentBlock::Advisory {
                body: "consider revisiting tests".into(),
                severity: crate::widgets::chat::types::AdvisorSeverity::Concern,
                timestamp_ms: 1_700_000_000_000,
            },
        ];
        let messages: Vec<_> = blocks.into_iter().map(msg).collect();
        let mut renderer = TranscriptRenderer::new();
        renderer.sync(
            &messages,
            None,
            &Theme::dark(),
            &TerminalCapabilities::default(),
        );
        let (result, live) = renderer.compose(80);
        let text = result.lines.join("\n");
        assert_eq!(live, LiveRegion::None);
        for needle in ["Thinking", "read", "bad", "image/png", "[CONCERN]"] {
            assert!(text.contains(needle), "missing {needle}");
        }
    }

    #[test]
    fn transcript_renders_mermaid_fence_as_diagram() {
        let mut renderer = TranscriptRenderer::new();
        renderer.sync(
            &[msg(ContentBlock::Text {
                content: "```mermaid\ngraph TD\n  A --> B\n```".into(),
            })],
            None,
            &Theme::dark(),
            &TerminalCapabilities::default(),
        );
        let (result, _) = renderer.compose(80);
        let text = result.lines.join("\n");
        assert!(text.contains("A"));
        assert!(text.contains("B"));
        assert!(!text.contains("```mermaid"));
    }

    #[test]
    fn transcript_renders_inline_and_display_latex() {
        let mut renderer = TranscriptRenderer::new();
        renderer.sync(
            &[msg(ContentBlock::Text {
                content: "inline $\\alpha^2$\n\n$$\\sum_{i=0}^n$$".into(),
            })],
            None,
            &Theme::dark(),
            &TerminalCapabilities::default(),
        );
        let (result, _) = renderer.compose(80);
        let text = result.lines.join("\n");
        assert!(text.contains("α²"), "rendered: {text}");
        assert!(text.contains("∑ᵢ₌₀ⁿ"), "rendered: {text}");
        assert!(!text.contains("\\alpha"));
    }

    #[test]
    fn transcript_image_is_safe_placeholder_even_when_protocol_is_available() {
        let mut renderer = TranscriptRenderer::new();
        let caps = TerminalCapabilities {
            image_protocol: Some(crate::render::terminal::ImageProtocol::Kitty),
            ..TerminalCapabilities::default()
        };
        renderer.sync(
            &[msg(ContentBlock::Image {
                mime_type: "image/png".into(),
                base64_data: "YWJj".into(),
            })],
            None,
            &Theme::dark(),
            &caps,
        );
        let (result, _) = renderer.compose(80);
        let text = result.lines.join("\n");
        assert!(text.contains("image/png"));
        // The placeholder text IS styled (has SGR ANSI codes from
        // `styled_line_to_ansi`), which is safe. What we must not see
        // is raw Kitty protocol escapes that would corrupt the tape
        // row structure. SGR sequences like `\x1b[38;5;240m` are
        // harmless and expected.
        assert!(
            !text.contains("\x1b_G"),
            "must not contain raw Kitty escapes in tape rows"
        );
        assert!(
            !text.contains("\x1b\\"),
            "must not contain raw image terminators"
        );
    }
}