Skip to main content

rpi_cli/
tui_shell.rs

1//! Driver-generic TUI shell.
2//!
3//! The shell half of the unification: a single terminal UI that drives **any**
4//! [`SessionDriver`] — chrome (banner + status slot + footer), the editor dock,
5//! the input loop, and the slash-command surface. It renders the transcript
6//! through the shared [`TranscriptView`] and applies normalized [`UiEvent`]s, so
7//! the same shell serves the remote client today and the in-process host once
8//! its runner adopts the driver seam.
9//!
10//! Local-only capabilities (session tree and extension management) are not part of
11//! this shell: it is the transport-agnostic floor both frontends share.
12//!
13//! [`UiEvent`]: crate::transcript_view::UiEvent
14
15use std::sync::{Arc, Mutex};
16use std::time::{Duration, Instant};
17
18use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers, MouseEventKind};
19use rpi_tui::{
20    bold as tui_bold, Component, Container, Editor, Focusable, FooterComponent, ProcessTerminal,
21    ScrollView, StackChild, StackEntry, StatusIndicator, Text, TuiAltScreen, VStack, WorkingState,
22    TUI,
23};
24
25use crate::session_driver::{DriverCommand, DriverEvent, DriverStatus, SessionDriver};
26use crate::transcript_view::TranscriptView;
27
28mod dialogs;
29use dialogs::ShellDialogs;
30
31/// Drive the generic shell against `driver` until the user exits (or the
32/// driver's event stream ends). `addr` is a display label for the banner.
33pub async fn run_driver_tui<D: SessionDriver>(driver: &D, addr: &str) -> Result<(), String> {
34    let mut events = driver
35        .events()
36        .ok_or("driver did not yield an event stream")?;
37
38    // ---- Build the UI ----
39    let chrome = ChatChrome::new(addr);
40    let document = Arc::new(Container::new());
41    document.add_child(chrome.header.clone());
42    let transcript_container = Arc::new(Container::new());
43    document.add_child(transcript_container.clone());
44    let scroll = Arc::new(ScrollView::primary(document.clone()));
45
46    // Retained transcript view: the same pi-tui components the local TUI uses,
47    // updated in place from the driver's normalized event stream.
48    let view = TranscriptView::new(transcript_container.clone());
49
50    let keybindings = crate::interactive_tui::configured_keybindings();
51    let editor = Arc::new(Editor::new(
52        rpi_tui::EditorOptions::default(),
53        rpi_tui::EditorStyle::default(),
54        keybindings.clone(),
55    ));
56    let editor_slot = Arc::new(Container::new());
57    editor_slot.add_child(editor.clone());
58
59    // Layout mirrors the local TUI's bottom dock: transcript (grow) over
60    // status slot + editor + footer.
61    let root = VStack::from_children(vec![
62        StackChild::Entry(
63            StackEntry::new(scroll.clone())
64                .basis(0)
65                .grow(1)
66                .shrink(1)
67                .min_size(1),
68        ),
69        StackChild::Entry(StackEntry::new(chrome.status.clone())),
70        StackChild::Entry(StackEntry::new(editor_slot.clone()).shrink(0).min_size(3)),
71        StackChild::Entry(StackEntry::new(chrome.footer.clone())),
72    ]);
73    let root: Arc<dyn Component> = Arc::new(root);
74
75    let tui = Arc::new(TuiAltScreen::new(
76        Box::new(ProcessTerminal::new()),
77        false,
78        None,
79    ));
80    crate::transcript_navigation::install(&tui, keybindings.clone());
81    tui.set_layout_root(Some(root));
82    tui.set_focus(Some(editor.clone()));
83    editor.set_focused(true);
84    tui.start_render_scheduler();
85    tui.start_readerless();
86
87    // ---- Input plumbing ----
88    let (input_tx, mut input_rx) = tokio::sync::mpsc::unbounded_channel::<Event>();
89    let (submit_tx, mut submit_rx) = tokio::sync::mpsc::unbounded_channel::<String>();
90    editor.on_submit(Arc::new(move |text: &str| {
91        let _ = submit_tx.send(text.to_string());
92    }));
93
94    let reader_stop = tokio_util::sync::CancellationToken::new();
95    let stop_reader = reader_stop.clone();
96    let reader = tokio::task::spawn_blocking(move || {
97        while !stop_reader.is_cancelled() {
98            match crossterm::event::poll(Duration::from_millis(50)) {
99                Ok(true) => {}
100                Ok(false) => continue,
101                Err(_) => break,
102            }
103            match crossterm::event::read() {
104                Ok(event) => {
105                    // The size is adopted from `Event::Resize` in the run loop
106                    // below. Refreshing on every event used to invalidate the
107                    // painted frame on each keystroke (a full repaint per key)
108                    // without ever learning a new size.
109                    if input_tx.send(event).is_err() {
110                        break;
111                    }
112                }
113                Err(_) => break,
114            }
115        }
116    });
117
118    // Initial paint (the transcript starts empty; chrome shows connection state).
119    let mut status = DriverStatus::default();
120    chrome.refresh(addr, &status);
121    tui.request_render(true);
122
123    let outcome = run_loop(
124        driver,
125        &mut status,
126        &mut events,
127        &mut input_rx,
128        &mut submit_rx,
129        &tui,
130        &editor,
131        &editor_slot,
132        &chrome,
133        &view,
134        &keybindings,
135        addr,
136    )
137    .await;
138
139    reader_stop.cancel();
140    let _ = reader.await;
141    tui.stop(Default::default());
142    outcome
143}
144
145#[allow(clippy::too_many_arguments)]
146async fn run_loop<D: SessionDriver>(
147    driver: &D,
148    status: &mut DriverStatus,
149    events: &mut tokio::sync::mpsc::UnboundedReceiver<DriverEvent>,
150    input_rx: &mut tokio::sync::mpsc::UnboundedReceiver<Event>,
151    submit_rx: &mut tokio::sync::mpsc::UnboundedReceiver<String>,
152    tui: &Arc<TuiAltScreen>,
153    editor: &Arc<Editor>,
154    editor_slot: &Arc<Container>,
155    chrome: &ChatChrome,
156    view: &TranscriptView,
157    keybindings: &rpi_tui::Keybindings,
158    addr: &str,
159) -> Result<(), String> {
160    // Repaint cadence while a run is in flight so the in-editor spinner and the
161    // retry countdown animate between streamed tokens (mirrors the local render
162    // tick). Idle screens never request a frame.
163    let mut ticker = tokio::time::interval(Duration::from_millis(120));
164    ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
165    let (dialog_tx, mut dialog_rx) = tokio::sync::mpsc::unbounded_channel();
166    let mut dialogs = ShellDialogs::new(dialog_tx);
167    loop {
168        dialogs.sync_view(editor_slot, editor, tui);
169        tokio::select! {
170            maybe_event = events.recv() => {
171                let Some(event) = maybe_event else { break };
172                match event {
173                    DriverEvent::UiDialog(request) => dialogs.push(request),
174                    DriverEvent::UiDialogClosed(id) => dialogs.close(&id),
175                    DriverEvent::Ui(ui) => {
176                        if matches!(ui, crate::transcript_view::UiEvent::AgentEnd) { let _ = dialogs.clear(); }
177                        view.apply(&ui, tui.width());
178                    }
179                    DriverEvent::Status(next) => {
180                        *status = next;
181                        sync_working(editor, status.streaming);
182                        chrome.refresh(addr, status);
183                    }
184                    DriverEvent::Notice(message) => view.add_notice(&message),
185                    DriverEvent::Ended => break,
186                }
187                tui.request_render(false);
188            }
189            maybe_input = input_rx.recv() => {
190                let Some(event) = maybe_input else { break };
191                match event {
192                    Event::Resize(..) => {
193                        tui.note_resize();
194                    }
195                    Event::Mouse(mouse) => {
196                        if tui.handle_scroll_to_end_indicator_mouse_event(&mouse) { continue; }
197                        // Click-to-toggle per-message thinking (see the local
198                        // TUI's mouse arm): resolve before selection/scrolling.
199                        if tui.thinking_region_hit(&mouse).is_some_and(|id| view.toggle_message_thinking(&id)) {
200                            tui.request_render_reusing_scroll_content();
201                            continue;
202                        }
203                        match mouse.kind {
204                            MouseEventKind::ScrollUp => tui.scroll_by(-3),
205                            MouseEventKind::ScrollDown => tui.scroll_by(3),
206                            _ => {}
207                        }
208                        tui.request_render(false);
209                    }
210                    Event::Key(key) if key.kind != KeyEventKind::Release => {
211                        crate::key_trace::key(&key, "driver-tui");
212                        if is_ctrl(&key, 'c') {
213                            let had_dialog = dialogs.has_active();
214                            for cancel in dialogs.clear() {
215                                let _ = driver.send(cancel).await;
216                            }
217                            if status.streaming {
218                                view.add_notice("aborting…");
219                                let _ = driver.send(DriverCommand::Abort).await;
220                            } else if !had_dialog {
221                                break;
222                            }
223                        } else if !dialogs.handle_key(key)
224                            && !crate::transcript_navigation::handle_key(tui, keybindings, &key) {
225                            editor.handle_key(key);
226                        }
227                        tui.request_render(false);
228                    }
229                    Event::Paste(text) => {
230                        if !dialogs.handle_paste(&text) { editor.insert(&text); }
231                        tui.request_render(false);
232                    }
233                    other => {
234                        crate::key_trace::note(&format!("driver tui event {other:?}"));
235                    }
236                }
237            }
238            maybe_text = submit_rx.recv() => {
239                let Some(text) = maybe_text else { break };
240                // A normal editor submission queued before a question arrived
241                // must not bypass the active approval UI.
242                if dialogs.has_active() { continue; }
243                match handle_submit(driver, status, view, &text).await {
244                    Ok(SubmitAction::Continue) => {}
245                    Ok(SubmitAction::Quit) => break,
246                    Err(error) => view.add_notice(&format!("error: {error}")),
247                }
248                editor.clear();
249                tui.request_render(false);
250            }
251            Some(action) = dialog_rx.recv() => {
252                if let Some((id, command)) = dialogs.action(action) {
253                    match driver.send(command).await {
254                        Ok(()) => dialogs.close(&id),
255                        Err(error) => view.add_notice(&format!("error: {error}")),
256                    }
257                }
258            }
259            _ = ticker.tick() => {
260                // Size watchdog: an event can be missed (or arrive before the
261                // window rectangle moves), and a fullscreen frame must always
262                // match the real window.
263                tui.ensure_size();
264                let retrying = chrome.tick_retry();
265                if status.streaming || retrying {
266                    tui.request_render(false);
267                }
268            }
269            else => break,
270        }
271    }
272    for cancel in dialogs.clear() {
273        let _ = driver.send(cancel).await;
274    }
275    Ok(())
276}
277
278enum SubmitAction {
279    Continue,
280    Quit,
281}
282
283async fn handle_submit<D: SessionDriver>(
284    driver: &D,
285    status: &DriverStatus,
286    view: &TranscriptView,
287    text: &str,
288) -> Result<SubmitAction, String> {
289    let trimmed = text.trim();
290    if trimmed.is_empty() {
291        return Ok(SubmitAction::Continue);
292    }
293
294    if let Some(rest) = trimmed.strip_prefix('/') {
295        let (name, arg) = match rest.split_once(char::is_whitespace) {
296            Some((name, arg)) => (name, arg.trim()),
297            None => (rest, ""),
298        };
299        match name {
300            "exit" | "quit" | "q" => return Ok(SubmitAction::Quit),
301            "abort" => driver.send(DriverCommand::Abort).await?,
302            "state" => driver.send(DriverCommand::RefreshState).await?,
303            "model" if !arg.is_empty() => {
304                driver
305                    .send(DriverCommand::SetModel(arg.to_string()))
306                    .await?
307            }
308            "thinking" if !arg.is_empty() => {
309                driver
310                    .send(DriverCommand::SetThinkingLevel(arg.to_string()))
311                    .await?
312            }
313            "tools" => {
314                let tools = if status.active_tools.is_empty() {
315                    "(default)".to_string()
316                } else {
317                    status.active_tools.join(", ")
318                };
319                view.add_notice(&format!("active tools: {tools}"));
320            }
321            "help" => view
322                .add_notice("commands: /abort /state /model <id> /thinking <level> /tools /exit"),
323            "" => view.add_notice("empty command (try /help)"),
324            other => view.add_notice(&format!(
325                "unsupported command: /{other} (session-tree and reload commands are not available in remote mode; try /help)"
326            )),
327        }
328        return Ok(SubmitAction::Continue);
329    }
330
331    view.add_user(trimmed);
332    driver
333        .send(DriverCommand::Prompt(trimmed.to_string()))
334        .await?;
335    Ok(SubmitAction::Continue)
336}
337
338fn is_ctrl(key: &KeyEvent, ch: char) -> bool {
339    key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char(ch)
340}
341
342/// The non-transcript chrome (top banner + status slot + footer), built from
343/// the same `pi-tui` components the local TUI uses ([`StatusIndicator`],
344/// [`FooterComponent`]) so the two screens read the same.
345struct ChatChrome {
346    header: Arc<Text>,
347    status: Arc<Container>,
348    footer: Arc<FooterComponent>,
349    /// The live retry indicator, kept across refreshes so its countdown ticks.
350    retry: Mutex<Option<(u32, u32, Arc<StatusIndicator>)>>,
351}
352
353impl ChatChrome {
354    fn new(addr: &str) -> Self {
355        let footer = Arc::new(FooterComponent::new());
356        footer.set_hints(
357            "Enter: Send | Shift+Enter: New line | Ctrl+C: Clear/Exit | Esc: Abort | /help",
358        );
359        Self {
360            header: Arc::new(Text::new(format!("rpi · {addr} · connecting…"), 1, 0)),
361            status: Arc::new(Container::new()),
362            footer,
363            retry: Mutex::new(None),
364        }
365    }
366
367    /// Refresh header + footer + retry indicator from the driver status.
368    fn refresh(&self, addr: &str, status: &DriverStatus) {
369        let model = status.model.as_deref().unwrap_or("");
370        let thinking = status.thinking.as_deref().unwrap_or("off");
371        let connection = if status.ready { "ready" } else { "connecting" };
372        self.header.set_text(tui_bold(&format!(
373            "rpi · {addr} · {connection} · model {} · thinking {thinking}",
374            if model.is_empty() { "(default)" } else { model }
375        )));
376        self.footer.set_model(&short_model_name(model));
377        self.footer.set_thinking_level(Some(thinking));
378        self.footer.set_status("");
379
380        let mut slot = self.retry.lock().unwrap();
381        match status.retry {
382            Some((attempt, max, delay_ms)) => {
383                let unchanged =
384                    matches!(slot.as_ref(), Some((a, m, _)) if *a == attempt && *m == max);
385                if !unchanged {
386                    let indicator = Arc::new(StatusIndicator::retry(
387                        attempt,
388                        max,
389                        Duration::from_millis(delay_ms),
390                    ));
391                    self.status.clear();
392                    self.status.add_child(indicator.clone());
393                    *slot = Some((attempt, max, indicator));
394                }
395            }
396            None => {
397                if slot.take().is_some() {
398                    self.status.clear();
399                }
400            }
401        }
402    }
403
404    /// Tick the retry countdown; returns whether a retry is currently shown.
405    fn tick_retry(&self) -> bool {
406        match self.retry.lock().unwrap().as_ref() {
407            Some((_, _, indicator)) => {
408                indicator.tick_countdown();
409                true
410            }
411            None => false,
412        }
413    }
414}
415
416/// Keep the in-editor working indicator in step with the run state — the local
417/// TUI shows the spinner in the editor's top border, not the status slot.
418fn sync_working(editor: &Arc<Editor>, streaming: bool) {
419    if streaming {
420        if editor.working().is_none() {
421            editor.set_working(Some(WorkingState {
422                started_at: Instant::now(),
423                message: "Working…".to_string(),
424            }));
425        }
426    } else if editor.working().is_some() {
427        editor.set_working(None);
428    }
429}
430
431/// The trailing path segment of a model id (`provider/model` → `model`).
432fn short_model_name(id: &str) -> String {
433    id.rsplit([':', '/'])
434        .next()
435        .filter(|segment| !segment.is_empty())
436        .unwrap_or(id)
437        .to_string()
438}
439
440#[cfg(test)]
441mod tests {
442    use super::*;
443
444    #[test]
445    fn ctrl_detection_is_specific() {
446        assert!(is_ctrl(
447            &KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL),
448            'c'
449        ));
450        assert!(!is_ctrl(
451            &KeyEvent::new(KeyCode::Char('c'), KeyModifiers::NONE),
452            'c'
453        ));
454    }
455
456    #[test]
457    fn short_model_name_keeps_the_trailing_segment() {
458        assert_eq!(short_model_name("openai/gpt-5.2"), "gpt-5.2");
459        assert_eq!(short_model_name("anthropic:claude-4"), "claude-4");
460        assert_eq!(short_model_name("bare"), "bare");
461        assert_eq!(short_model_name(""), "");
462    }
463}