Mechanical rename: src/agent/ -> src/user/, all crate::agent:: -> crate::user:: references updated. Binary poc-agent renamed to consciousness with CLI name and user-facing strings updated. Co-Authored-By: Proof of Concept <poc@bcachefs.org>
186 lines
7.6 KiB
Rust
186 lines
7.6 KiB
Rust
// context_screen.rs — F2 context/debug overlay
|
|
//
|
|
// Full-screen overlay showing model info, context window breakdown,
|
|
// and runtime state. Supports tree navigation with expand/collapse.
|
|
|
|
use ratatui::{
|
|
layout::Rect,
|
|
style::{Color, Modifier, Style},
|
|
text::Line,
|
|
widgets::{Block, Borders, Paragraph, Wrap},
|
|
Frame,
|
|
};
|
|
|
|
use super::{App, 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())
|
|
}
|
|
|
|
/// 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 count_section(section: &crate::user::ui_channel::ContextSection, expanded: &std::collections::HashSet<usize>, idx: &mut usize) -> usize {
|
|
let my_idx = *idx;
|
|
*idx += 1;
|
|
let mut total = 1;
|
|
if expanded.contains(&my_idx) {
|
|
for child in §ion.children {
|
|
total += count_section(child, expanded, idx);
|
|
}
|
|
}
|
|
total
|
|
}
|
|
let mut idx = 0;
|
|
let mut total = 0;
|
|
for section in context_state {
|
|
total += count_section(section, &self.debug_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 {
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Render a context section as a tree node with optional children.
|
|
pub(crate) fn render_debug_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 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 label = format!("{}{} {:30} {:>6} tokens", indent, marker, section.name, section.tokens);
|
|
let style = if selected {
|
|
Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD)
|
|
} else {
|
|
Style::default()
|
|
};
|
|
lines.push(Line::styled(label, style));
|
|
*idx += 1;
|
|
|
|
if expanded {
|
|
if has_children {
|
|
for child in §ion.children {
|
|
self.render_debug_section(child, depth + 1, start_idx, lines, idx);
|
|
}
|
|
} else if has_content {
|
|
let content_indent = format!("{} │ ", " ".repeat(depth + 1));
|
|
let content_lines: Vec<&str> = section.content.lines().collect();
|
|
let show = content_lines.len().min(50);
|
|
for line in &content_lines[..show] {
|
|
lines.push(Line::styled(
|
|
format!("{}{}", content_indent, line),
|
|
Style::default().fg(Color::DarkGray),
|
|
));
|
|
}
|
|
if content_lines.len() > 50 {
|
|
lines.push(Line::styled(
|
|
format!("{}... ({} more lines)", content_indent, content_lines.len() - 50),
|
|
Style::default().fg(Color::DarkGray),
|
|
));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Draw the debug screen — full-screen overlay with context and runtime info.
|
|
pub(crate) fn draw_debug(&self, frame: &mut Frame, size: Rect) {
|
|
let mut lines: Vec<Line> = Vec::new();
|
|
let section = 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::raw(format!(" Current: {}", model_display)));
|
|
if let Some(ref info) = self.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)));
|
|
}
|
|
let context_state = self.read_context_state();
|
|
if !context_state.is_empty() {
|
|
let total: usize = context_state.iter().map(|s| s.tokens).sum();
|
|
lines.push(Line::raw(""));
|
|
lines.push(Line::styled(
|
|
" (↑/↓ select, →/Enter expand, ← collapse, PgUp/PgDn scroll)",
|
|
Style::default().fg(Color::DarkGray),
|
|
));
|
|
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);
|
|
}
|
|
|
|
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 {
|
|
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::raw(format!(
|
|
" DMN: {} ({}/{})",
|
|
self.status.dmn_state, self.status.dmn_turns, self.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.len())));
|
|
|
|
let block = Block::default()
|
|
.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));
|
|
|
|
let para = Paragraph::new(lines)
|
|
.block(block)
|
|
.wrap(Wrap { trim: false })
|
|
.scroll((self.debug_scroll, 0));
|
|
|
|
frame.render_widget(para, size);
|
|
}
|
|
}
|