oxi-tui 0.25.7

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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! Layout calculation — LayoutKind, LayoutEntry, compute_layout, measure_*.

use std::collections::HashSet;

use ratatui::style::Style;
use ratatui::text::Line;

use crate::widgets::chat::dashboard::{measure_dashboard, DashboardInfo};
use crate::widgets::chat::markdown::{filter_tool_json, md_lines};
use crate::widgets::chat::state::ChatViewState;
use crate::widgets::chat::types::{ContentBlock, MessageRole, ToolCallStatus};

// ── Layout calculation ────────────────────────────────────────────────

/// Measure the rendered height of pre-wrapped lines.
///
/// Lines are already wrapped to the correct width by `wrap_lines_styled()`,
/// so we just count them. No need to use `Paragraph::line_count` which
/// would try to re-wrap and produce incorrect results for CJK text.
pub(crate) fn measure_wrapped_height(lines: &[Line<'_>], _width: u16) -> u16 {
    lines.len() as u16
}

/// Calculate the layout: list of (y, height, block_ref) for each piece of content.
#[derive(Clone)]
pub(crate) struct LayoutEntry {
    pub y: u16,
    pub height: u16,
    pub kind: LayoutKind,
}

#[derive(Clone)]
pub(crate) enum LayoutKind {
    Spacer,
    Rule,
    #[allow(dead_code)]
    Label {
        text: String,
        style: Style,
    },
    Text {
        lines: Vec<Line<'static>>,
        is_user: bool,
    },
    ToolBox {
        name: String,
        arguments: String,
        result: Option<(String, bool)>,
        status: ToolCallStatus,
        duration: Option<String>,
        expanded: bool,
        key: String,
    },
    ToolResultBox {
        tool_name: String,
        content: String,
        is_error: bool,
    },
    ErrorBox {
        title: String,
        message: String,
        retryable: bool,
    },
    Thinking {
        content: String,
        collapsed: bool,
        key: String,
    },
    Image {
        mime_type: String,
        size_str: String,
    },
    #[allow(dead_code)]
    Spinner {
        frame: usize,
    },
    Dashboard {
        info: DashboardInfo,
    },
}

/// Check if a content block renders as a bordered box (tool calls, errors).
/// Consecutive box blocks need spacers between them for visual separation.
fn is_box_block(block: &ContentBlock) -> bool {
    matches!(
        block,
        ContentBlock::ToolCall { .. }
            | ContentBlock::ToolResult { .. }
            | ContentBlock::Error { .. }
    )
}

pub(crate) fn compute_layout(state: &ChatViewState, width: u16) -> Vec<LayoutEntry> {
    let mut entries = Vec::new();
    let mut y: u32 = 0;

    let mut rendered_any_message = false;
    let mut msg_idx: usize = 0;

    'outer: for msg in &state.messages {
        // Skip messages that have no visible content; they only create empty spacer rows.
        let has_visible_content = msg.content_blocks.iter().any(|b| match b {
            ContentBlock::Text { content } => !content.trim().is_empty(),
            ContentBlock::Thinking { content, .. } => !content.trim().is_empty(),
            _ => true,
        });
        if !has_visible_content {
            msg_idx += 1;
            continue;
        }

        if rendered_any_message {
            // Gap between messages — use a spacer for breathing room
            if y <= u16::MAX as u32 {
                entries.push(LayoutEntry {
                    y: y as u16,
                    height: 1,
                    kind: LayoutKind::Spacer,
                });
            }
            y += 1;
        }
        rendered_any_message = true;

        // User messages: left accent border, no label needed (single-user context)
        if msg.role == MessageRole::User {
            if y <= u16::MAX as u32 {
                entries.push(LayoutEntry {
                    y: y as u16,
                    height: 1,
                    kind: LayoutKind::Rule,
                });
            }
            y += 1;
        }
        let mut prev_was_box = false;
        for (blk_idx, block) in msg.content_blocks.iter().enumerate() {
            // Skip whitespace-only blocks (defensive; finish_streaming also removes them).
            let is_empty = match block {
                ContentBlock::Text { content } => content.trim().is_empty(),
                ContentBlock::Thinking { content, .. } => content.trim().is_empty(),
                _ => false,
            };
            if is_empty {
                continue;
            }

            // Insert spacer between consecutive box-type blocks (tool calls, errors)
            let is_box = is_box_block(block);
            if is_box && prev_was_box {
                if y <= u16::MAX as u32 {
                    entries.push(LayoutEntry {
                        y: y as u16,
                        height: 1,
                        kind: LayoutKind::Spacer,
                    });
                }
                y += 1;
            }
            prev_was_box = is_box;

            let key = format!("{}:{}", msg_idx, blk_idx);
            let mut kind = block_to_layout_kind(block, msg.role, width, &key);
            // Override collapsed/expanded state
            #[allow(clippy::collapsible_match)]
            match &mut kind {
                LayoutKind::Thinking {
                    ref mut collapsed,
                    ref key,
                    ..
                } =>
                {
                    #[allow(clippy::collapsible_match)]
                    if state.expanded_thinking.contains(key) {
                        *collapsed = false;
                    }
                }
                LayoutKind::ToolBox {
                    ref mut expanded,
                    ref key,
                    ..
                } =>
                {
                    #[allow(clippy::collapsible_match)]
                    if state.expanded_tools.contains(key) {
                        *expanded = true;
                    }
                }
                _ => {}
            }
            let h = measure_kind(&kind, width, &state.expanded_thinking);
            if y > u16::MAX as u32 {
                break 'outer;
            }
            entries.push(LayoutEntry {
                y: y as u16,
                height: h,
                kind,
            });
            y += h as u32;
        }
        msg_idx += 1;
    }

    if let Some(ref streaming) = state.streaming {
        // Only add a spacer if we actually rendered any history messages.
        if rendered_any_message {
            if y <= u16::MAX as u32 {
                entries.push(LayoutEntry {
                    y: y as u16,
                    height: 1,
                    kind: LayoutKind::Spacer,
                });
            }
            y += 1;
        }
        let mut prev_was_box = false;
        for (blk_idx, block) in streaming.message.content_blocks.iter().enumerate() {
            // Skip whitespace-only blocks (prevents large blank gaps during tool-only turns).
            let is_empty = match block {
                ContentBlock::Text { content } => content.trim().is_empty(),
                ContentBlock::Thinking { content, .. } => content.trim().is_empty(),
                _ => false,
            };
            if is_empty {
                continue;
            }

            // Insert spacer between consecutive box-type blocks (tool calls, errors)
            let is_box = is_box_block(block);
            if is_box && prev_was_box {
                if y <= u16::MAX as u32 {
                    entries.push(LayoutEntry {
                        y: y as u16,
                        height: 1,
                        kind: LayoutKind::Spacer,
                    });
                }
                y += 1;
            }
            prev_was_box = is_box;

            let key = format!("s:{}", blk_idx);
            let mut kind = block_to_layout_kind(block, MessageRole::Assistant, width, &key);
            #[allow(clippy::collapsible_match)]
            match &mut kind {
                LayoutKind::Thinking {
                    ref mut collapsed,
                    ref key,
                    ..
                } =>
                {
                    #[allow(clippy::collapsible_match)]
                    if state.expanded_thinking.contains(key) {
                        *collapsed = false;
                    }
                }
                LayoutKind::ToolBox {
                    ref mut expanded,
                    ref key,
                    ..
                } =>
                {
                    #[allow(clippy::collapsible_match)]
                    if state.expanded_tools.contains(key) {
                        *expanded = true;
                    }
                }
                _ => {}
            }
            let h = measure_kind(&kind, width, &state.expanded_thinking);
            if y <= u16::MAX as u32 {
                entries.push(LayoutEntry {
                    y: y as u16,
                    height: h,
                    kind,
                });
            }
            y += h as u32;
        }
        // Spinner removed: status now shown in the input separator line (render_input_area).
    }

    entries
}

fn block_to_layout_kind(
    block: &ContentBlock,
    role: MessageRole,
    width: u16,
    key: &str,
) -> LayoutKind {
    match block {
        ContentBlock::Text { content } => {
            // For user messages, Block::borders(LEFT) takes 1 column,
            // so text must be wrapped to width-1.
            let wrap_w = if role == MessageRole::User {
                width.saturating_sub(1)
            } else {
                width
            };
            let lines = md_lines(content, wrap_w);
            LayoutKind::Text {
                lines,
                is_user: role == MessageRole::User,
            }
        }
        ContentBlock::Thinking { content, collapsed } => {
            // The actual collapsed state is determined by the key lookup later
            // in compute_layout — we store the default here.
            LayoutKind::Thinking {
                content: content.clone(),
                collapsed: *collapsed,
                key: key.to_string(),
            }
        }
        ContentBlock::ToolCall {
            name,
            arguments,
            result,
            status,
            duration,
            ..
        } => LayoutKind::ToolBox {
            name: name.clone(),
            arguments: arguments.clone(),
            result: result.clone(),
            status: *status,
            duration: duration.clone(),
            expanded: false, // overridden in compute_layout
            key: key.to_string(),
        },
        ContentBlock::ToolResult {
            tool_name,
            content,
            is_error,
        } => LayoutKind::ToolResultBox {
            tool_name: tool_name.clone(),
            content: content.clone(),
            is_error: *is_error,
        },
        ContentBlock::Error {
            title,
            message,
            retryable,
        } => LayoutKind::ErrorBox {
            title: title.clone(),
            message: message.clone(),
            retryable: *retryable,
        },
        ContentBlock::Image {
            mime_type,
            base64_data,
        } => {
            let sz = base64_data.len() * 3 / 4;
            let sz_str = if sz >= 1_048_576 {
                format!("{:.1} MB", sz as f64 / 1_048_576.0)
            } else if sz >= 1024 {
                format!("{:.1} KB", sz as f64 / 1024.0)
            } else {
                format!("{} B", sz)
            };
            LayoutKind::Image {
                mime_type: mime_type.clone(),
                size_str: sz_str,
            }
        }
        ContentBlock::Dashboard { info } => LayoutKind::Dashboard { info: info.clone() },
    }
}

pub(crate) fn measure_kind(
    kind: &LayoutKind,
    width: u16,
    expanded_thinking: &HashSet<String>,
) -> u16 {
    match kind {
        LayoutKind::Spacer
        | LayoutKind::Rule
        | LayoutKind::Label { .. }
        | LayoutKind::Spinner { .. } => 1,
        LayoutKind::Text { lines, is_user } => {
            // User text: Block::borders(LEFT) takes 1 col, so inner = width-1
            // Assistant text: no block, renders at full width
            let w = if *is_user {
                width.saturating_sub(1)
            } else {
                width
            };
            measure_wrapped_height(lines, w)
        }
        LayoutKind::ToolBox {
            name,
            arguments,
            result,
            duration,
            expanded,
            ..
        } => {
            use crate::widgets::tool_renderer::{measure_call_height, measure_result_height};
            // inner width for the bordered block: borders take 2 cols
            let inner_w = width.saturating_sub(2) as usize;
            let call_h = measure_call_height(name, arguments, inner_w);
            let result_h = result.as_ref().map_or(0, |(r, is_err)| {
                if *expanded {
                    // Full result: show all lines, capped at 80
                    let total = r.lines().count();
                    let shown = total.min(80);
                    let ellipsis = if total > 80 { 1 } else { 0 };
                    shown as u16 + ellipsis
                } else if *is_err {
                    let total = r.lines().count();
                    total.min(4) as u16 + if total > 4 { 1 } else { 0 }
                } else {
                    measure_result_height(name, r, false)
                }
            });
            // Block::ALL adds top + bottom border (2 rows) + separator if result exists
            let separator_h = if result.is_some() { 1 } else { 0 };
            // Toggle hint line when result exists
            let toggle_h = if result.is_some() { 1 } else { 0 };
            let _ = duration;
            2 + call_h + separator_h + result_h + toggle_h
        }
        LayoutKind::ToolResultBox { content, .. } => {
            // 1 header + content lines (max 4) + optional ellipsis
            let n = content.lines().count().min(4);
            1 + n as u16 + if content.lines().count() > 4 { 1 } else { 0 }
        }
        LayoutKind::ErrorBox {
            message, retryable, ..
        } => {
            // Block::bordered() adds top + bottom border (2 rows)
            let n = message.lines().count().min(4);
            2 + n as u16 + if *retryable { 1 } else { 0 }
        }
        LayoutKind::Thinking {
            content,
            collapsed,
            key,
        } => {
            let is_expanded = expanded_thinking.contains(key);
            if *collapsed && !is_expanded {
                // Collapsed: header + one preview line
                let filtered = filter_tool_json(content);
                let line_count = filtered.lines().count();
                1 + if line_count > 0 { 1 } else { 0 }
            } else {
                // Expanded: header + filtered content rendered as markdown
                let filtered = filter_tool_json(content);
                let md = md_lines(&filtered, width);
                1 + md.len() as u16
            }
        }
        LayoutKind::Image { .. } => 2,
        LayoutKind::Dashboard { info } => measure_dashboard(info, width),
    }
}