consciousness/src/user/context.rs
Kent Overstreet 31a41fa042 ActiveTools wrapper: replace SharedActiveTools Arc<Mutex<Vec>>
New ActiveTools struct with proper methods: push, remove, abort_all,
take_finished, take_foreground, iter, len. Lives directly on AgentState,
no separate Arc<Mutex> needed.

TUI reads active tools through agent.state.try_lock(). Turn loop uses
helpers instead of manual index iteration.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
2026-04-08 16:45:56 -04:00

138 lines
5.6 KiB
Rust

use ratatui::{
layout::Rect,
style::{Color, Style},
text::Line,
Frame,
};
use super::{App, ScreenView, screen_legend};
use super::widgets::{SectionTree, SectionView, section_to_view, pane_block, render_scrollable, tree_legend};
use crate::agent::context::{AstNode, NodeBody, Ast};
pub(crate) struct ConsciousScreen {
agent: std::sync::Arc<crate::agent::Agent>,
tree: SectionTree,
}
impl ConsciousScreen {
pub fn new(agent: std::sync::Arc<crate::agent::Agent>) -> Self {
Self { agent, tree: SectionTree::new() }
}
fn read_context_views(&self) -> Vec<SectionView> {
let ctx = match self.agent.context.try_lock() {
Ok(ctx) => ctx,
Err(_) => return Vec::new(),
};
let mut views: Vec<SectionView> = Vec::new();
views.push(section_to_view("System", ctx.system()));
views.push(section_to_view("Identity", ctx.identity()));
views.push(section_to_view("Journal", ctx.journal()));
// Memory nodes extracted from conversation
let mut mem_children: Vec<SectionView> = Vec::new();
let mut scored = 0usize;
let mut unscored = 0usize;
for node in ctx.conversation() {
if let AstNode::Leaf(leaf) = node {
if let NodeBody::Memory { key, score, text } = leaf.body() {
let status = match score {
Some(s) => { scored += 1; format!("score: {:.2}", s) }
None => { unscored += 1; String::new() }
};
mem_children.push(SectionView {
name: key.clone(),
tokens: node.tokens(),
content: text.clone(),
children: Vec::new(),
status,
});
}
}
}
if !mem_children.is_empty() {
let mem_tokens: usize = mem_children.iter().map(|c| c.tokens).sum();
views.push(SectionView {
name: format!("Memory nodes ({})", mem_children.len()),
tokens: mem_tokens,
content: String::new(),
children: mem_children,
status: format!("{} scored, {} unscored", scored, unscored),
});
}
views.push(section_to_view("Conversation", ctx.conversation()));
views
}
}
impl ScreenView for ConsciousScreen {
fn label(&self) -> &'static str { "conscious" }
fn tick(&mut self, frame: &mut Frame, area: Rect,
events: &[ratatui::crossterm::event::Event], app: &mut App) {
for event in events {
if let ratatui::crossterm::event::Event::Key(key) = event {
if key.kind != ratatui::crossterm::event::KeyEventKind::Press { continue; }
let context_state = self.read_context_views();
self.tree.handle_nav(key.code, &context_state, area.height);
}
}
let mut lines: Vec<Line> = Vec::new();
let section_style = Style::default().fg(Color::Yellow);
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) = 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(""));
lines.push(Line::styled("── Context State ──", section_style));
lines.push(Line::raw(format!(" Prompt tokens: {}K", app.status.prompt_tokens / 1000)));
let context_state = self.read_context_views();
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(""));
self.tree.render_sections(&context_state, &mut lines);
lines.push(Line::raw(format!(" {:23} {:>6} tokens", "────────", "──────")));
lines.push(Line::raw(format!(" {:23} {:>6} tokens", "Total", total)));
} 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(""));
lines.push(Line::styled("── Runtime ──", section_style));
lines.push(Line::raw(format!(
" DMN: {} ({}/{})",
app.status.dmn_state, app.status.dmn_turns, app.status.dmn_max_turns,
)));
lines.push(Line::raw(format!(" Reasoning: {}", app.reasoning_effort)));
lines.push(Line::raw(format!(" Running processes: {}", app.running_processes)));
let tool_count = app.agent.state.try_lock()
.map(|st| st.active_tools.len()).unwrap_or(0);
lines.push(Line::raw(format!(" Active tools: {}", tool_count)));
let block = pane_block("context")
.title_top(Line::from(screen_legend()).left_aligned())
.title_bottom(tree_legend());
render_scrollable(frame, area, lines, block, self.tree.scroll);
}
}