perspt_tui/
ui.rs

1//! TUI module - Primary entry point for Perspt TUI
2//!
3//! Provides a unified interface for both Chat and Agent modes.
4
5use crate::agent_app::AgentApp;
6use crate::chat_app::ChatApp;
7use anyhow::Result;
8use perspt_core::GenAIProvider;
9
10/// Application mode
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum AppMode {
13    /// Interactive chat with LLM
14    Chat,
15    /// SRBN Agent orchestration
16    Agent,
17}
18
19/// Run the TUI in chat mode
20///
21/// # Arguments
22/// * `provider` - The GenAI provider for LLM communication
23/// * `model` - The model identifier to use
24///
25/// # Example
26/// ```no_run
27/// use perspt_tui::run_chat_tui;
28/// use perspt_core::GenAIProvider;
29///
30/// #[tokio::main]
31/// async fn main() {
32///     let provider = GenAIProvider::new().unwrap();
33///     run_chat_tui(provider, "gemini-2.0-flash".to_string()).await.unwrap();
34/// }
35/// ```
36pub async fn run_chat_tui(provider: GenAIProvider, model: String) -> Result<()> {
37    use crossterm::event::{
38        DisableMouseCapture, EnableBracketedPaste, EnableMouseCapture, KeyboardEnhancementFlags,
39        PopKeyboardEnhancementFlags, PushKeyboardEnhancementFlags,
40    };
41    use ratatui::crossterm::execute;
42    use std::io::stdout;
43
44    // Enable mouse capture for scroll wheel support
45    execute!(stdout(), EnableMouseCapture)?;
46
47    // Enable bracketed paste for multi-line paste handling
48    execute!(stdout(), EnableBracketedPaste)?;
49
50    // Enable keyboard enhancement for better modifier detection
51    // This allows reliable Ctrl+Enter, Shift+Tab detection
52    let _ = execute!(
53        stdout(),
54        PushKeyboardEnhancementFlags(
55            KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES
56                | KeyboardEnhancementFlags::REPORT_EVENT_TYPES
57        )
58    );
59
60    let mut terminal = ratatui::init();
61    let mut app = ChatApp::new(provider, model);
62
63    let result = app.run(&mut terminal).await;
64
65    // Restore terminal
66    let _ = execute!(stdout(), PopKeyboardEnhancementFlags);
67    ratatui::restore();
68    execute!(stdout(), DisableMouseCapture)?;
69
70    result
71}
72
73/// Run the TUI in agent mode (legacy wrapper)
74///
75/// This function provides backward compatibility for the agent TUI.
76/// It uses demo data as the orchestrator integration is pending.
77pub fn run_agent_tui() -> std::io::Result<()> {
78    let mut terminal = ratatui::init();
79    let mut app = AgentApp::new();
80
81    // Demo data for now
82    use crate::task_tree::TaskStatus;
83    app.task_tree.add_task(
84        "root".to_string(),
85        "Implement authentication".to_string(),
86        0,
87    );
88    app.task_tree
89        .add_task("auth-1".to_string(), "Create JWT module".to_string(), 1);
90    app.task_tree
91        .add_task("auth-2".to_string(), "Add password hashing".to_string(), 1);
92    app.task_tree.update_status("root", TaskStatus::Running);
93    app.task_tree.update_status("auth-1", TaskStatus::Completed);
94
95    app.dashboard.total_nodes = 3;
96    app.dashboard.completed_nodes = 1;
97    app.dashboard.current_node = Some("auth-2".to_string());
98    app.dashboard.update_energy(0.5);
99    app.dashboard
100        .log("Started task: Implement authentication".to_string());
101    app.dashboard.log("OK: JWT module completed".to_string());
102
103    let result = app.run(&mut terminal);
104    ratatui::restore();
105    result
106}
107
108/// Legacy placeholder function - redirects to agent TUI
109#[deprecated(note = "Use run_chat_tui or run_agent_tui instead")]
110pub fn run_tui() -> Result<()> {
111    run_agent_tui().map_err(|e| anyhow::anyhow!("TUI error: {}", e))
112}