user: ScreenView trait, overlay screens extracted from App

Convert F2-F5 screens to ScreenView trait with tick() method.
Each screen owns its view state (scroll, selection, expanded).
State persists across screen switches.

- ThalamusScreen: owns sampling_selected, scroll
- ConsciousScreen: owns scroll, selected, expanded
- SubconsciousScreen: owns selected, log_view, scroll
- UnconsciousScreen: owns scroll

Removed from App: Screen enum, debug_scroll, debug_selected,
debug_expanded, agent_selected, agent_log_view, sampling_selected,
set_screen(), per-screen key handling, draw dispatch.

App now only draws the interact (F1) screen. Overlay screens are
drawn by the event loop via ScreenView::tick. F-key routing and
screen instantiation to be wired in event_loop next.

InteractScreen (state-driven, reading from agent entries) is the
next step — will eliminate the input display race condition.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Kent Overstreet 2026-04-05 17:54:40 -04:00
parent 7458fe655f
commit 927cddd864
8 changed files with 388 additions and 439 deletions

View file

@ -9,18 +9,27 @@ use ratatui::{
text::Line,
widgets::{Block, Borders, Paragraph, Wrap},
Frame,
crossterm::event::{KeyCode, KeyEvent},
};
use super::{App, SCREEN_LEGEND};
use super::{App, ScreenAction, ScreenView, screen_legend};
impl App {
/// Read the live context state from the shared lock.
pub(crate) fn read_context_state(&self) -> Vec<crate::user::ui_channel::ContextSection> {
self.shared_context.read().map_or_else(|_| Vec::new(), |s| s.clone())
pub(crate) struct ConsciousScreen {
scroll: u16,
selected: Option<usize>,
expanded: std::collections::HashSet<usize>,
}
impl ConsciousScreen {
pub fn new() -> Self {
Self { scroll: 0, selected: None, expanded: std::collections::HashSet::new() }
}
/// Count total selectable items in the context state tree.
pub(crate) fn debug_item_count(&self, context_state: &[crate::user::ui_channel::ContextSection]) -> usize {
fn read_context_state(&self, app: &App) -> Vec<crate::user::ui_channel::ContextSection> {
app.shared_context.read().map_or_else(|_| Vec::new(), |s| s.clone())
}
fn item_count(&self, context_state: &[crate::user::ui_channel::ContextSection]) -> usize {
fn count_section(section: &crate::user::ui_channel::ContextSection, expanded: &std::collections::HashSet<usize>, idx: &mut usize) -> usize {
let my_idx = *idx;
*idx += 1;
@ -35,50 +44,39 @@ impl App {
let mut idx = 0;
let mut total = 0;
for section in context_state {
total += count_section(section, &self.debug_expanded, &mut idx);
total += count_section(section, &self.expanded, &mut idx);
}
total
}
/// Keep the viewport scrolled so the selected item is visible.
/// Assumes ~1 line per item plus a header offset of ~8 lines.
pub(crate) fn scroll_to_selected(&mut self, _item_count: usize) {
let header_lines = 8u16; // model info + context state header
if let Some(sel) = self.debug_selected {
fn scroll_to_selected(&mut self, _item_count: usize) {
let header_lines = 8u16;
if let Some(sel) = self.selected {
let sel_line = header_lines + sel as u16;
// Keep cursor within a comfortable range of the viewport
if sel_line < self.debug_scroll + 2 {
self.debug_scroll = sel_line.saturating_sub(2);
} else if sel_line > self.debug_scroll + 30 {
self.debug_scroll = sel_line.saturating_sub(15);
if sel_line < self.scroll + 2 {
self.scroll = sel_line.saturating_sub(2);
} else if sel_line > self.scroll + 30 {
self.scroll = sel_line.saturating_sub(15);
}
}
}
/// Render a context section as a tree node with optional children.
pub(crate) fn render_debug_section(
fn render_section(
&self,
section: &crate::user::ui_channel::ContextSection,
depth: usize,
start_idx: usize,
lines: &mut Vec<Line>,
idx: &mut usize,
) {
let my_idx = *idx;
let selected = self.debug_selected == Some(my_idx);
let expanded = self.debug_expanded.contains(&my_idx);
let selected = self.selected == Some(my_idx);
let expanded = self.expanded.contains(&my_idx);
let has_children = !section.children.is_empty();
let has_content = !section.content.is_empty();
let expandable = has_children || has_content;
let indent = " ".repeat(depth + 1);
let marker = if !expandable {
" "
} else if expanded {
""
} else {
""
};
let marker = if !expandable { " " } else if expanded { "" } else { "" };
let label = format!("{}{} {:30} {:>6} tokens", indent, marker, section.name, section.tokens);
let style = if selected {
Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD)
@ -91,7 +89,7 @@ impl App {
if expanded {
if has_children {
for child in &section.children {
self.render_debug_section(child, depth + 1, start_idx, lines, idx);
self.render_section(child, depth + 1, lines, idx);
}
} else if has_content {
let content_indent = format!("{}", " ".repeat(depth + 1));
@ -112,31 +110,66 @@ impl App {
}
}
}
}
/// Draw the debug screen — full-screen overlay with context and runtime info.
pub(crate) fn draw_debug(&self, frame: &mut Frame, size: Rect) {
impl ScreenView for ConsciousScreen {
fn label(&self) -> &'static str { "conscious" }
fn tick(&mut self, frame: &mut Frame, area: Rect,
key: Option<KeyEvent>, app: &App) -> Option<ScreenAction> {
// Handle keys
if let Some(key) = key {
let context_state = self.read_context_state(app);
let item_count = self.item_count(&context_state);
match key.code {
KeyCode::Up => {
self.selected = Some(self.selected.unwrap_or(0).saturating_sub(1));
self.scroll_to_selected(item_count);
}
KeyCode::Down => {
let max = item_count.saturating_sub(1);
self.selected = Some(self.selected.map_or(0, |s| (s + 1).min(max)));
self.scroll_to_selected(item_count);
}
KeyCode::Right | KeyCode::Enter => {
if let Some(sel) = self.selected {
self.expanded.insert(sel);
}
}
KeyCode::Left => {
if let Some(sel) = self.selected {
self.expanded.remove(&sel);
}
}
KeyCode::PageUp => { self.scroll = self.scroll.saturating_sub(20); }
KeyCode::PageDown => { self.scroll += 20; }
KeyCode::Esc => return Some(ScreenAction::Switch(0)),
_ => {}
}
}
// Draw
let mut lines: Vec<Line> = Vec::new();
let section = Style::default().fg(Color::Yellow);
let section_style = Style::default().fg(Color::Yellow);
// Model
lines.push(Line::styled("── Model ──", section));
let model_display = self.context_info.as_ref()
.map_or_else(|| self.status.model.clone(), |i| i.model.clone());
lines.push(Line::styled("── Model ──", section_style));
let model_display = app.context_info.as_ref()
.map_or_else(|| app.status.model.clone(), |i| i.model.clone());
lines.push(Line::raw(format!(" Current: {}", model_display)));
if let Some(ref info) = self.context_info {
if let Some(ref info) = app.context_info {
lines.push(Line::raw(format!(" Backend: {}", info.backend)));
lines.push(Line::raw(format!(" Prompt: {}", info.prompt_file)));
lines.push(Line::raw(format!(" Available: {}", info.available_models.join(", "))));
}
lines.push(Line::raw(""));
// Context state
lines.push(Line::styled("── Context State ──", section));
lines.push(Line::raw(format!(" Prompt tokens: {}K", self.status.prompt_tokens / 1000)));
if !self.status.context_budget.is_empty() {
lines.push(Line::raw(format!(" Budget: {}", self.status.context_budget)));
lines.push(Line::styled("── Context State ──", section_style));
lines.push(Line::raw(format!(" Prompt tokens: {}K", app.status.prompt_tokens / 1000)));
if !app.status.context_budget.is_empty() {
lines.push(Line::raw(format!(" Budget: {}", app.status.context_budget)));
}
let context_state = self.read_context_state();
let context_state = self.read_context_state(app);
if !context_state.is_empty() {
let total: usize = context_state.iter().map(|s| s.tokens).sum();
lines.push(Line::raw(""));
@ -146,32 +179,30 @@ impl App {
));
lines.push(Line::raw(""));
// Flatten tree into indexed entries for selection
let mut flat_idx = 0usize;
for section in &context_state {
self.render_debug_section(section, 0, flat_idx, &mut lines, &mut flat_idx);
self.render_section(section, 0, &mut lines, &mut flat_idx);
}
lines.push(Line::raw(format!(" {:23} {:>6} tokens", "────────", "──────")));
lines.push(Line::raw(format!(" {:23} {:>6} tokens", "Total", total)));
} else if let Some(ref info) = self.context_info {
} else if let Some(ref info) = app.context_info {
lines.push(Line::raw(format!(" System prompt: {:>6} chars", info.system_prompt_chars)));
lines.push(Line::raw(format!(" Context message: {:>6} chars", info.context_message_chars)));
}
lines.push(Line::raw(""));
// Runtime
lines.push(Line::styled("── Runtime ──", section));
lines.push(Line::styled("── Runtime ──", section_style));
lines.push(Line::raw(format!(
" DMN: {} ({}/{})",
self.status.dmn_state, self.status.dmn_turns, self.status.dmn_max_turns,
app.status.dmn_state, app.status.dmn_turns, app.status.dmn_max_turns,
)));
lines.push(Line::raw(format!(" Reasoning: {}", self.reasoning_effort)));
lines.push(Line::raw(format!(" Running processes: {}", self.running_processes)));
lines.push(Line::raw(format!(" Active tools: {}", self.active_tools.lock().unwrap().len())));
lines.push(Line::raw(format!(" Reasoning: {}", app.reasoning_effort)));
lines.push(Line::raw(format!(" Running processes: {}", app.running_processes)));
lines.push(Line::raw(format!(" Active tools: {}", app.active_tools.lock().unwrap().len())));
let block = Block::default()
.title_top(Line::from(SCREEN_LEGEND).left_aligned())
.title_top(Line::from(screen_legend()).left_aligned())
.title_top(Line::from(" context ").right_aligned())
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::Cyan));
@ -179,8 +210,9 @@ impl App {
let para = Paragraph::new(lines)
.block(block)
.wrap(Wrap { trim: false })
.scroll((self.debug_scroll, 0));
.scroll((self.scroll, 0));
frame.render_widget(para, size);
frame.render_widget(para, area);
None
}
}