agcodex-tui 0.1.0

Terminal User Interface for AGCodex with mode switching support
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
//! History Browser widget for visual timeline navigation of conversation
//!
//! This widget provides a tree-like visualization of the conversation history,
//! showing branches and allowing navigation through different conversation paths.

use std::collections::HashMap;
use std::time::SystemTime;

use agcodex_core::models::ResponseItem;
use ratatui::buffer::Buffer;
use ratatui::layout::Alignment;
use ratatui::layout::Constraint;
use ratatui::layout::Direction;
use ratatui::layout::Layout;
use ratatui::layout::Rect;
use ratatui::style::Color;
use ratatui::style::Modifier;
use ratatui::style::Style;
use ratatui::symbols;
use ratatui::text::Line;
use ratatui::text::Span;
use ratatui::text::Text;
use ratatui::widgets::Block;
use ratatui::widgets::BorderType;
use ratatui::widgets::Borders;
use ratatui::widgets::Clear;
use ratatui::widgets::Paragraph;
use ratatui::widgets::Widget;
use ratatui::widgets::WidgetRef;
use uuid::Uuid;

/// Represents a node in the conversation tree
#[derive(Debug, Clone)]
pub struct HistoryNode {
    /// Unique identifier for this node
    pub id: Uuid,
    /// Index in the linear conversation
    pub index: usize,
    /// The actual response item
    pub item: ResponseItem,
    /// Parent node ID (None for root)
    pub parent: Option<Uuid>,
    /// Children node IDs (for branches)
    pub children: Vec<Uuid>,
    /// Timestamp of creation
    pub timestamp: SystemTime,
    /// Branch name if this is a branch point
    pub branch_name: Option<String>,
    /// Whether this node is on the active path
    pub is_active: bool,
}

impl HistoryNode {
    /// Create a new history node
    pub fn new(index: usize, item: ResponseItem, parent: Option<Uuid>) -> Self {
        Self {
            id: Uuid::new_v4(),
            index,
            item,
            parent,
            children: Vec::new(),
            timestamp: SystemTime::now(),
            branch_name: None,
            is_active: true,
        }
    }

    /// Get a preview of the node content
    pub fn preview(&self) -> String {
        match &self.item {
            ResponseItem::Message { role, content, .. } => {
                let text = content
                    .iter()
                    .filter_map(|c| match c {
                        agcodex_core::models::ContentItem::InputText { text }
                        | agcodex_core::models::ContentItem::OutputText { text } => {
                            Some(text.as_str())
                        }
                        agcodex_core::models::ContentItem::InputImage { .. } => Some("[Image]"),
                    })
                    .collect::<Vec<_>>()
                    .join(" ");

                let preview = if text.len() > 80 {
                    format!("{}...", &text[..77])
                } else {
                    text
                };

                format!("[{}] {}", role, preview)
            }
            ResponseItem::Reasoning { .. } => "[Reasoning]".to_string(),
            ResponseItem::FunctionCall { name, .. } => format!("[Function: {}]", name),
            ResponseItem::LocalShellCall { action, .. } => format!("[Shell: {:?}]", action),
            ResponseItem::FunctionCallOutput { .. } => "[Function Output]".to_string(),
            ResponseItem::Other => "[Other]".to_string(),
        }
    }

    /// Get the role for coloring
    pub const fn role(&self) -> &str {
        match &self.item {
            ResponseItem::Message { role, .. } => role.as_str(),
            ResponseItem::Reasoning { .. } => "reasoning",
            ResponseItem::FunctionCall { .. } => "function",
            ResponseItem::LocalShellCall { .. } => "shell",
            ResponseItem::FunctionCallOutput { .. } => "output",
            ResponseItem::Other => "other",
        }
    }
}

/// Conversation tree structure for history browsing
#[derive(Debug, Clone)]
pub struct ConversationTree {
    /// All nodes indexed by ID
    nodes: HashMap<Uuid, HistoryNode>,
    /// Root node ID
    root: Option<Uuid>,
    /// Currently active path (node IDs from root to current)
    active_path: Vec<Uuid>,
    /// Currently selected node for preview
    selected_node: Option<Uuid>,
}

impl ConversationTree {
    /// Create a new conversation tree
    pub fn new() -> Self {
        Self {
            nodes: HashMap::new(),
            root: None,
            active_path: Vec::new(),
            selected_node: None,
        }
    }

    /// Add a node to the tree
    pub fn add_node(&mut self, node: HistoryNode) -> Uuid {
        let id = node.id;

        // Update parent's children list
        if let Some(parent_id) = node.parent {
            if let Some(parent) = self.nodes.get_mut(&parent_id) {
                parent.children.push(id);
            }
        } else {
            // This is a root node
            self.root = Some(id);
        }

        // Mark as active and add to active path
        if node.is_active {
            self.active_path.push(id);
        }

        self.nodes.insert(id, node);
        id
    }

    /// Create a branch from a specific node
    pub fn create_branch(
        &mut self,
        from_node: Uuid,
        branch_name: String,
        item: ResponseItem,
    ) -> Option<Uuid> {
        let parent_node = self.nodes.get(&from_node)?;
        let new_index = parent_node.index + 1;

        let mut new_node = HistoryNode::new(new_index, item, Some(from_node));
        new_node.branch_name = Some(branch_name);
        new_node.is_active = false; // New branches start inactive

        Some(self.add_node(new_node))
    }

    /// Switch to a different branch
    pub fn switch_to_branch(&mut self, target_node: Uuid) -> bool {
        if !self.nodes.contains_key(&target_node) {
            return false;
        }

        // Mark old path as inactive
        for id in &self.active_path {
            if let Some(node) = self.nodes.get_mut(id) {
                node.is_active = false;
            }
        }

        // Build new active path from root to target
        let mut new_path = Vec::new();
        let mut current = Some(target_node);

        while let Some(node_id) = current {
            new_path.push(node_id);
            current = self.nodes.get(&node_id).and_then(|n| n.parent);
        }

        new_path.reverse();

        // Mark new path as active
        for id in &new_path {
            if let Some(node) = self.nodes.get_mut(id) {
                node.is_active = true;
            }
        }

        self.active_path = new_path;
        true
    }

    /// Get nodes for rendering (in tree order)
    pub fn get_render_nodes(&self) -> Vec<(usize, Uuid, bool)> {
        let mut result = Vec::new();
        if let Some(root_id) = self.root {
            self.collect_nodes_recursive(root_id, 0, &mut result);
        }
        result
    }

    /// Recursively collect nodes for rendering
    fn collect_nodes_recursive(
        &self,
        node_id: Uuid,
        depth: usize,
        result: &mut Vec<(usize, Uuid, bool)>,
    ) {
        if let Some(node) = self.nodes.get(&node_id) {
            let is_selected = self.selected_node == Some(node_id);
            result.push((depth, node_id, is_selected));

            // Add children
            for child_id in &node.children {
                self.collect_nodes_recursive(*child_id, depth + 1, result);
            }
        }
    }
}

impl Default for ConversationTree {
    fn default() -> Self {
        Self::new()
    }
}

/// History Browser widget for visual timeline navigation
pub struct HistoryBrowser {
    /// The conversation tree structure
    tree: ConversationTree,
    /// Whether the browser is visible
    visible: bool,
    /// Scroll offset for the tree view
    scroll_offset: usize,
    /// Maximum visible items
    max_visible: usize,
    /// Show preview pane
    show_preview: bool,
    /// Preview context lines
    preview_context: usize,
}

impl HistoryBrowser {
    /// Create a new history browser
    pub fn new() -> Self {
        Self {
            tree: ConversationTree::new(),
            visible: false,
            scroll_offset: 0,
            max_visible: 20,
            show_preview: true,
            preview_context: 3,
        }
    }

    /// Show the history browser with conversation items
    pub fn show(&mut self, items: Vec<ResponseItem>) {
        self.tree = ConversationTree::new();

        let mut parent: Option<Uuid> = None;
        for (index, item) in items.into_iter().enumerate() {
            let node = HistoryNode::new(index, item, parent);
            parent = Some(self.tree.add_node(node));
        }

        self.visible = true;

        // Select the last node by default
        if let Some(last_id) = self.tree.active_path.last() {
            self.tree.selected_node = Some(*last_id);
        }
    }

    /// Hide the history browser
    pub const fn hide(&mut self) {
        self.visible = false;
        self.scroll_offset = 0;
    }

    /// Check if the browser is visible
    pub const fn is_visible(&self) -> bool {
        self.visible
    }

    /// Move selection up in the tree
    pub fn move_up(&mut self) {
        let nodes = self.tree.get_render_nodes();
        if let Some(current_idx) = nodes.iter().position(|(_, _, selected)| *selected)
            && current_idx > 0
        {
            let (_, prev_node, _) = nodes[current_idx - 1];
            self.tree.selected_node = Some(prev_node);
            self.ensure_visible(current_idx - 1, nodes.len());
        }
    }

    /// Move selection down in the tree
    pub fn move_down(&mut self) {
        let nodes = self.tree.get_render_nodes();
        if let Some(current_idx) = nodes.iter().position(|(_, _, selected)| *selected)
            && current_idx < nodes.len() - 1
        {
            let (_, next_node, _) = nodes[current_idx + 1];
            self.tree.selected_node = Some(next_node);
            self.ensure_visible(current_idx + 1, nodes.len());
        }
    }

    /// Ensure selected item is visible
    fn ensure_visible(&mut self, index: usize, total: usize) {
        if index < self.scroll_offset {
            self.scroll_offset = index;
        } else if index >= self.scroll_offset + self.max_visible {
            self.scroll_offset = index.saturating_sub(self.max_visible - 1);
        }

        // Clamp scroll offset
        self.scroll_offset = self
            .scroll_offset
            .min(total.saturating_sub(self.max_visible));
    }

    /// Get the currently selected node
    pub fn selected_node(&self) -> Option<&HistoryNode> {
        self.tree
            .selected_node
            .and_then(|id| self.tree.nodes.get(&id))
    }

    /// Create a branch from the selected node
    pub fn create_branch_from_selected(&mut self, name: String, item: ResponseItem) -> bool {
        if let Some(selected_id) = self.tree.selected_node {
            self.tree.create_branch(selected_id, name, item).is_some()
        } else {
            false
        }
    }

    /// Switch to the selected branch
    pub fn switch_to_selected_branch(&mut self) -> bool {
        if let Some(selected_id) = self.tree.selected_node {
            self.tree.switch_to_branch(selected_id)
        } else {
            false
        }
    }

    /// Toggle preview pane
    pub const fn toggle_preview(&mut self) {
        self.show_preview = !self.show_preview;
    }
}

impl Default for HistoryBrowser {
    fn default() -> Self {
        Self::new()
    }
}

impl Widget for HistoryBrowser {
    fn render(self, area: Rect, buf: &mut Buffer) {
        WidgetRef::render_ref(&self, area, buf);
    }
}

impl WidgetRef for HistoryBrowser {
    fn render_ref(&self, area: Rect, buf: &mut Buffer) {
        if !self.visible {
            return;
        }

        // Clear the background
        Clear.render(area, buf);

        // Create the main block
        let block = Block::default()
            .title("History Browser (Ctrl+H)")
            .borders(Borders::ALL)
            .border_type(BorderType::Rounded)
            .border_style(Style::default().fg(Color::Magenta));

        let inner = block.inner(area);
        block.render(area, buf);

        // Split into tree view and preview if enabled
        let chunks = if self.show_preview {
            Layout::default()
                .direction(Direction::Horizontal)
                .constraints([Constraint::Percentage(60), Constraint::Percentage(40)])
                .split(inner)
                .to_vec()
        } else {
            vec![inner]
        };

        // Render the tree view
        self.render_tree_view(chunks[0], buf);

        // Render preview if enabled
        if self.show_preview && chunks.len() > 1 {
            self.render_preview(chunks[1], buf);
        }
    }
}

impl HistoryBrowser {
    /// Render the tree view of conversation history
    fn render_tree_view(&self, area: Rect, buf: &mut Buffer) {
        let nodes = self.tree.get_render_nodes();

        // Create lines for the tree visualization
        let mut lines = Vec::new();

        // Add header
        lines.push(Line::from(vec![
            Span::styled("↑↓", Style::default().fg(Color::Gray)),
            Span::raw(" Navigate | "),
            Span::styled("Enter", Style::default().fg(Color::Gray)),
            Span::raw(" Jump | "),
            Span::styled("b", Style::default().fg(Color::Gray)),
            Span::raw(" Branch | "),
            Span::styled("p", Style::default().fg(Color::Gray)),
            Span::raw(" Preview"),
        ]));
        lines.push(Line::from(""));

        // Render visible nodes
        let end = (self.scroll_offset + self.max_visible).min(nodes.len());
        for (depth, node_id, is_selected) in &nodes[self.scroll_offset..end] {
            let node = match self.tree.nodes.get(node_id) {
                Some(n) => n,
                None => continue,
            };

            let indent = "  ".repeat(*depth);

            // Choose tree symbols
            let symbol = if node.children.is_empty() {
                symbols::line::VERTICAL_RIGHT
            } else if node.children.len() > 1 {
                "├┬"
            } else {
                symbols::line::VERTICAL_RIGHT
            };

            // Style based on role and selection
            let (role_style, preview_style) = if *is_selected {
                (
                    get_role_style(node.role()).add_modifier(Modifier::BOLD | Modifier::REVERSED),
                    Style::default().add_modifier(Modifier::REVERSED),
                )
            } else if node.is_active {
                (
                    get_role_style(node.role()).add_modifier(Modifier::BOLD),
                    Style::default(),
                )
            } else {
                (
                    get_role_style(node.role()).add_modifier(Modifier::DIM),
                    Style::default().fg(Color::DarkGray),
                )
            };

            // Build the line
            let mut spans = vec![
                Span::raw(indent),
                Span::styled(symbol, Style::default().fg(Color::Gray)),
                Span::raw(" "),
                Span::styled(format!("#{} ", node.index + 1), role_style),
            ];

            // Add branch name if present
            if let Some(branch_name) = &node.branch_name {
                spans.push(Span::styled(
                    format!("[{}] ", branch_name),
                    Style::default().fg(Color::Yellow),
                ));
            }

            spans.push(Span::styled(node.preview(), preview_style));

            lines.push(Line::from(spans));
        }

        // Add scroll indicators if needed
        if self.scroll_offset > 0 {
            lines[1] = Line::from(vec![
                Span::styled("â–² ", Style::default().fg(Color::Yellow)),
                Span::raw("More above..."),
            ]);
        }

        if end < nodes.len() {
            lines.push(Line::from(vec![
                Span::styled("â–¼ ", Style::default().fg(Color::Yellow)),
                Span::raw("More below..."),
            ]));
        }

        let tree_text = Text::from(lines);
        let tree_paragraph = Paragraph::new(tree_text);
        tree_paragraph.render(area, buf);
    }

    /// Render the preview pane
    fn render_preview(&self, area: Rect, buf: &mut Buffer) {
        let block = Block::default()
            .title("Preview")
            .borders(Borders::ALL)
            .border_style(Style::default().fg(Color::Gray));

        let inner = block.inner(area);
        block.render(area, buf);

        if let Some(node) = self.selected_node() {
            // Get full message content
            let content = match &node.item {
                ResponseItem::Message { role, content, .. } => {
                    let text = content
                        .iter()
                        .filter_map(|c| match c {
                            agcodex_core::models::ContentItem::InputText { text }
                            | agcodex_core::models::ContentItem::OutputText { text } => {
                                Some(text.as_str())
                            }
                            agcodex_core::models::ContentItem::InputImage { .. } => Some("[Image]"),
                        })
                        .collect::<Vec<_>>()
                        .join("\n");

                    format!("Role: {}\n\n{}", role, text)
                }
                ResponseItem::Reasoning { content, .. } => match content {
                    Some(items) => {
                        let reasoning_text = items
                            .iter()
                            .map(|item| format!("{:?}", item))
                            .collect::<Vec<_>>()
                            .join("\n");
                        format!("Reasoning:\n\n{}", reasoning_text)
                    }
                    None => "Reasoning: (empty)".to_string(),
                },
                ResponseItem::FunctionCall {
                    name, arguments, ..
                } => {
                    format!("Function Call: {}\nArguments: {}", name, arguments)
                }
                ResponseItem::LocalShellCall { action, .. } => {
                    format!("Shell Command:\n{:?}", action)
                }
                ResponseItem::FunctionCallOutput {
                    call_id, output, ..
                } => {
                    format!("Function Output: {}\n\n{}", call_id, output)
                }
                ResponseItem::Other => "Other content".to_string(),
            };

            // Create preview text with word wrapping
            let wrapped_lines: Vec<String> = content
                .lines()
                .flat_map(|line| {
                    if line.len() <= inner.width as usize {
                        vec![line.to_string()]
                    } else {
                        // Simple word wrap
                        let mut wrapped = Vec::new();
                        let mut current = String::new();
                        for word in line.split_whitespace() {
                            if current.len() + word.len() + 1 > inner.width as usize
                                && !current.is_empty()
                            {
                                wrapped.push(current.clone());
                                current.clear();
                            }
                            if !current.is_empty() {
                                current.push(' ');
                            }
                            current.push_str(word);
                        }
                        if !current.is_empty() {
                            wrapped.push(current);
                        }
                        wrapped
                    }
                })
                .collect();

            // Take only what fits in the preview area
            let preview_lines: Vec<Line> = wrapped_lines
                .iter()
                .take(inner.height as usize)
                .map(|s| Line::from(s.as_str()))
                .collect();

            let preview_text = Text::from(preview_lines);
            let preview_paragraph = Paragraph::new(preview_text).alignment(Alignment::Left);
            preview_paragraph.render(inner, buf);
        } else {
            let no_selection = Paragraph::new("No message selected")
                .style(Style::default().fg(Color::DarkGray))
                .alignment(Alignment::Center);
            no_selection.render(inner, buf);
        }
    }
}

/// Get style for a role
fn get_role_style(role: &str) -> Style {
    match role {
        "user" => Style::default().fg(Color::Green),
        "assistant" => Style::default().fg(Color::Blue),
        "system" => Style::default().fg(Color::Yellow),
        "reasoning" => Style::default().fg(Color::Magenta),
        "function" | "shell" => Style::default().fg(Color::Cyan),
        "output" => Style::default().fg(Color::Gray),
        _ => Style::default(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use agcodex_core::models::ContentItem;

    fn create_test_message(role: &str, content: &str) -> ResponseItem {
        ResponseItem::Message {
            id: None,
            role: role.to_string(),
            content: vec![ContentItem::OutputText {
                text: content.to_string(),
            }],
        }
    }

    #[test]
    fn test_history_node_creation() {
        let item = create_test_message("user", "Test message");
        let node = HistoryNode::new(0, item, None);

        assert_eq!(node.index, 0);
        assert!(node.parent.is_none());
        assert!(node.children.is_empty());
        assert!(node.is_active);
    }

    #[test]
    fn test_conversation_tree_building() {
        let mut tree = ConversationTree::new();

        let root_item = create_test_message("user", "First message");
        let root_node = HistoryNode::new(0, root_item, None);
        let root_id = tree.add_node(root_node);

        assert_eq!(tree.root, Some(root_id));
        assert_eq!(tree.active_path, vec![root_id]);

        let child_item = create_test_message("assistant", "Response");
        let child_node = HistoryNode::new(1, child_item, Some(root_id));
        let child_id = tree.add_node(child_node);

        assert_eq!(tree.active_path, vec![root_id, child_id]);
        assert_eq!(tree.nodes.get(&root_id).unwrap().children, vec![child_id]);
    }

    #[test]
    fn test_branching() {
        let mut tree = ConversationTree::new();

        let root_item = create_test_message("user", "Root");
        let root_node = HistoryNode::new(0, root_item, None);
        let root_id = tree.add_node(root_node);

        let branch_item = create_test_message("assistant", "Branch");
        let branch_id = tree.create_branch(root_id, "Alternative".to_string(), branch_item);

        assert!(branch_id.is_some());
        let branch_id = branch_id.unwrap();

        assert_eq!(tree.nodes.get(&root_id).unwrap().children.len(), 1);
        assert_eq!(
            tree.nodes.get(&branch_id).unwrap().branch_name,
            Some("Alternative".to_string())
        );
        assert!(!tree.nodes.get(&branch_id).unwrap().is_active);
    }

    #[test]
    fn test_history_browser_visibility() {
        let mut browser = HistoryBrowser::new();
        assert!(!browser.is_visible());

        browser.show(vec![
            create_test_message("user", "Hello"),
            create_test_message("assistant", "Hi"),
        ]);
        assert!(browser.is_visible());

        browser.hide();
        assert!(!browser.is_visible());
    }
}