TUI: permission prompt, /diff review, /stats dashboard, lens/context/MCP side panel
This commit is contained in:
1 parent
4a8e920ab8
commit
5fd7044a2f
15 files changed
+945
-2
No files matched your search
@@ -3,10 +3,13 @@
|
||||
pub mod agents;
|
||||
pub mod choose;
|
||||
pub mod editor;
|
||||
pub mod permit_view;
|
||||
pub mod picker;
|
||||
pub mod popup;
|
||||
pub mod review;
|
||||
pub mod scroll;
|
||||
pub mod sessions;
|
||||
pub mod stats_view;
|
||||
pub mod status;
|
||||
pub mod theme;
|
||||
pub mod transcript;
|
||||
@@ -50,6 +53,8 @@ pub struct Setup {
|
||||
pub open_sessions: bool,
|
||||
/// Capture the mouse (wheel scrolling); terminal text selection then needs shift-drag.
|
||||
pub mouse: bool,
|
||||
/// MCP servers for the side panel: (name, connected).
|
||||
pub mcp: Vec<(String, bool)>,
|
||||
}
|
||||
|
||||
/// Screen regions of the last draw, for routing the mouse wheel.
|
||||
@@ -85,6 +90,10 @@ pub type LspStatus = Box<
|
||||
enum Ui {
|
||||
Ev(Event),
|
||||
Ask(String, tokio::sync::oneshot::Sender<bool>),
|
||||
Permit(
|
||||
hx_core::permit::Request,
|
||||
tokio::sync::oneshot::Sender<hx_core::permit::Decision>,
|
||||
),
|
||||
Choose(
|
||||
hx_core::Question,
|
||||
tokio::sync::oneshot::Sender<Option<Vec<String>>>,
|
||||
@@ -140,6 +149,17 @@ struct App {
|
||||
/// condensed lens state for the footer, refreshed by a timer
|
||||
lens: (String, bool),
|
||||
/// open `ask_user` popup and where its answer goes
|
||||
/// pending permission prompt for bash/write/edit
|
||||
perm: Option<(
|
||||
permit_view::Prompt,
|
||||
tokio::sync::oneshot::Sender<hx_core::permit::Decision>,
|
||||
)>,
|
||||
review: Option<review::Review>,
|
||||
stats: Option<(hx_core::stats::Summary, String)>,
|
||||
/// lens/context/MCP side panel (ctrl-l), wide terminals only
|
||||
side: bool,
|
||||
lens_lines: Vec<String>,
|
||||
mcp: Vec<(String, bool)>,
|
||||
choose: Option<(
|
||||
choose::Choose,
|
||||
tokio::sync::oneshot::Sender<Option<Vec<String>>>,
|
||||
@@ -214,6 +234,7 @@ pub async fn run(setup: Setup) -> std::io::Result<()> {
|
||||
specs,
|
||||
open_sessions,
|
||||
mouse,
|
||||
mcp,
|
||||
} = setup;
|
||||
let (tx, mut rx) = mpsc::unbounded_channel();
|
||||
agent.bus.add(Forward(tx.clone()));
|
||||
@@ -229,6 +250,14 @@ pub async fn run(setup: Setup) -> std::io::Result<()> {
|
||||
Box::pin(async move { orx.await.unwrap_or(false) })
|
||||
}));
|
||||
}
|
||||
{
|
||||
let tx = tx.clone();
|
||||
let _ = agent.cx.state.permit.set(std::sync::Arc::new(move |r| {
|
||||
let (otx, orx) = tokio::sync::oneshot::channel();
|
||||
let _ = tx.send(Ui::Permit(r, otx));
|
||||
Box::pin(async move { orx.await.unwrap_or(hx_core::permit::Decision::Deny) })
|
||||
}));
|
||||
}
|
||||
{
|
||||
let tx = tx.clone();
|
||||
let _ = agent.cx.state.choose.set(std::sync::Arc::new(move |q| {
|
||||
@@ -275,6 +304,12 @@ pub async fn run(setup: Setup) -> std::io::Result<()> {
|
||||
ask: None,
|
||||
lens: (String::new(), false),
|
||||
choose: None,
|
||||
perm: None,
|
||||
review: None,
|
||||
stats: None,
|
||||
side: true,
|
||||
lens_lines: vec![],
|
||||
mcp,
|
||||
state: agent.cx.state.clone(),
|
||||
sub_t: Default::default(),
|
||||
agents: None,
|
||||
@@ -321,6 +356,20 @@ pub async fn run(setup: Setup) -> std::io::Result<()> {
|
||||
let Some(Ok(ev)) = k else { break };
|
||||
dirty = true;
|
||||
match ev {
|
||||
TEvent::Key(k) if k.kind != KeyEventKind::Release && app.perm.is_some() => {
|
||||
if let Some((p, _)) = &mut app.perm
|
||||
&& let Some(d) = p.key(k)
|
||||
&& let Some((p, reply)) = app.perm.take()
|
||||
{
|
||||
app.t.push(Item::Info(format!(
|
||||
"{} {} {}",
|
||||
permit_view::Prompt::label(d),
|
||||
p.req.tool,
|
||||
p.req.detail
|
||||
)));
|
||||
let _ = reply.send(d);
|
||||
}
|
||||
}
|
||||
TEvent::Key(k) if k.kind != KeyEventKind::Release && app.choose.is_some() => {
|
||||
if let Some((c, _)) = &mut app.choose
|
||||
&& let choose::Act::Done(ans) = c.key(k)
|
||||
@@ -357,6 +406,7 @@ pub async fn run(setup: Setup) -> std::io::Result<()> {
|
||||
dirty = true;
|
||||
match u {
|
||||
Ui::Ask(q, reply) => app.ask = Some((q, reply)),
|
||||
Ui::Permit(r, reply) => app.perm = Some((permit_view::Prompt::new(r), reply)),
|
||||
Ui::Choose(q, reply) => app.choose = Some((choose::Choose::new(q), reply)),
|
||||
Ui::Ev(e) => {
|
||||
if let Event::SubUsage(u) = &e {
|
||||
@@ -409,6 +459,7 @@ pub async fn run(setup: Setup) -> std::io::Result<()> {
|
||||
let to = std::time::Duration::from_millis(200);
|
||||
if let Ok(v) = tokio::time::timeout(to, lsp_status()).await {
|
||||
let s = status::lens_summary(&v);
|
||||
app.lens_lines = v;
|
||||
dirty |= s != app.lens;
|
||||
app.lens = s;
|
||||
}
|
||||
@@ -668,6 +719,32 @@ fn on_key(app: &mut App, k: KeyEvent, abort: &AbortHandle) -> Option<String> {
|
||||
if app.sessions.is_some() {
|
||||
return sessions_key(app, k);
|
||||
}
|
||||
if app.stats.is_some() {
|
||||
if matches!(k.code, KeyCode::Esc | KeyCode::Char('q')) {
|
||||
app.stats = None;
|
||||
}
|
||||
return None;
|
||||
}
|
||||
if let Some(r) = &mut app.review {
|
||||
match r.key(k) {
|
||||
review::Act::Close => app.review = None,
|
||||
review::Act::Reverted(p) => {
|
||||
app.state
|
||||
.edits
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner())
|
||||
.retain(|e| e.path != p);
|
||||
app.t.push(Item::Info(format!("reverted {}", p.display())));
|
||||
}
|
||||
review::Act::Error(e) => app.t.push(Item::Error(e)),
|
||||
review::Act::None => {}
|
||||
}
|
||||
return None;
|
||||
}
|
||||
if ctrl && k.code == KeyCode::Char('l') {
|
||||
app.side = !app.side;
|
||||
return None;
|
||||
}
|
||||
if ctrl && k.code == KeyCode::Char('g') {
|
||||
app.popup = None;
|
||||
app.agents = match (app.agents.take(), app.state.subs.list().len()) {
|
||||
@@ -850,6 +927,23 @@ async fn submit(app: &mut App, mut line: String, c: &Ctx<'_>) {
|
||||
let h = format!("{}\n\nkeys: {KEYS} · ctrl-p picker", cmd::help(&app.specs));
|
||||
show(app, "help", h);
|
||||
}
|
||||
"stats" => {
|
||||
let sess = agent.lock().await;
|
||||
let sum = hx_core::stats::summary(sess.session());
|
||||
app.stats = Some((sum, app.title.clone().unwrap_or_default()));
|
||||
}
|
||||
"diff" => {
|
||||
let edits = app
|
||||
.state
|
||||
.edits
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner())
|
||||
.clone();
|
||||
match review::Review::new(&edits) {
|
||||
Some(r) => app.review = Some(r),
|
||||
None => app.t.push(Item::Info("no edits to review".into())),
|
||||
}
|
||||
}
|
||||
"usage" => {
|
||||
let r = status::report(&app.st, status::now_secs());
|
||||
show(app, "usage", r);
|
||||
@@ -1052,6 +1146,19 @@ fn draw(f: &mut ratatui::Frame, app: &mut App) {
|
||||
app.areas.panel = side;
|
||||
m
|
||||
}
|
||||
None if app.wide && app.side => {
|
||||
let [m, side] =
|
||||
Layout::horizontal([Constraint::Min(60), Constraint::Length(34)]).areas(main);
|
||||
let block = Block::new()
|
||||
.borders(Borders::LEFT)
|
||||
.border_style(th.dim())
|
||||
.title(Span::styled(" lens · ctrl-l ", th.dim()));
|
||||
let body = block.inner(side);
|
||||
f.render_widget(block, side);
|
||||
f.render_widget(Paragraph::new(side_lines(app)), body);
|
||||
app.areas.panel = Rect::default();
|
||||
m
|
||||
}
|
||||
_ => {
|
||||
app.areas.panel = Rect::default();
|
||||
main
|
||||
@@ -1142,6 +1249,56 @@ fn draw(f: &mut ratatui::Frame, app: &mut App) {
|
||||
if let Some(s) = &mut app.sessions {
|
||||
s.draw(f);
|
||||
}
|
||||
if let Some((s, t)) = &app.stats {
|
||||
stats_view::draw(f, s, t);
|
||||
}
|
||||
if let Some(r) = &app.review {
|
||||
r.draw(f);
|
||||
}
|
||||
if let Some((p, _)) = &app.perm {
|
||||
p.draw(f, input.y);
|
||||
}
|
||||
}
|
||||
|
||||
/// Right-hand overview on wide terminals: lens state, context numbers, MCP servers.
|
||||
fn side_lines(app: &App) -> Vec<Line<'static>> {
|
||||
let th = theme::t();
|
||||
let head = |t: &str| Line::styled(t.to_string(), th.accent());
|
||||
let mut l = vec![head("LSP / LINT")];
|
||||
if app.lens_lines.is_empty() {
|
||||
l.push(Line::styled("(none started)", th.dim()));
|
||||
}
|
||||
l.extend(app.lens_lines.iter().map(|x| {
|
||||
let bad = x.contains("dead") || x.contains("error") || x.contains("missing");
|
||||
Line::styled(x.clone(), if bad { th.warn() } else { th.plain() })
|
||||
}));
|
||||
let u = &app.st.usage;
|
||||
let tot = u.input_tokens + u.cache_read_tokens + u.cache_write_tokens;
|
||||
l.push(Line::default());
|
||||
l.push(head("CONTEXT"));
|
||||
l.push(Line::raw(format!(
|
||||
"window {}k / {}k",
|
||||
app.st.ctx_tokens / 1000,
|
||||
app.st.ctx_max / 1000
|
||||
)));
|
||||
if let Some(p) = (100 * u.cache_read_tokens).checked_div(tot) {
|
||||
l.push(Line::raw(format!("cache {p}%")));
|
||||
}
|
||||
l.push(Line::raw(format!("output {}k", u.output_tokens / 1000)));
|
||||
if !app.mcp.is_empty() {
|
||||
l.push(Line::default());
|
||||
l.push(head("MCP"));
|
||||
l.extend(app.mcp.iter().map(|(n, ok)| {
|
||||
Line::from(vec![
|
||||
Span::styled(
|
||||
if *ok { "● " } else { "○ " },
|
||||
if *ok { th.ok() } else { th.err() },
|
||||
),
|
||||
Span::raw(n.clone()),
|
||||
])
|
||||
}));
|
||||
}
|
||||
l
|
||||
}
|
||||
|
||||
/// `/skills`: each skill with its file, then the searched dirs (so a missing skill is easy to debug).
|
||||
@@ -1247,6 +1404,62 @@ mod tests {
|
||||
assert!(rows[9].ends_with("Claude · m1"), "{all}");
|
||||
}
|
||||
|
||||
fn screen(app: &mut App, w: u16, h: u16) -> String {
|
||||
let mut term = ratatui::Terminal::new(TestBackend::new(w, h)).unwrap();
|
||||
term.draw(|f| draw(f, app)).unwrap();
|
||||
let b = term.backend().buffer().clone();
|
||||
(0..h)
|
||||
.map(|y| {
|
||||
(0..w)
|
||||
.map(|x| b[(x, y)].symbol().to_string())
|
||||
.collect::<String>()
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn side_panel_only_on_wide_terminals() {
|
||||
let mut app = test_app();
|
||||
app.mcp = vec![("git".into(), true), ("db".into(), false)];
|
||||
app.lens_lines = vec!["rust-analyzer @ /p: running".into()];
|
||||
assert!(screen(&mut app, 160, 50).contains("MCP"));
|
||||
assert!(!screen(&mut app, 80, 24).contains("MCP"));
|
||||
app.side = false;
|
||||
assert!(!screen(&mut app, 160, 50).contains("MCP"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overlays_render_at_both_sizes() {
|
||||
let mut app = test_app();
|
||||
app.review = review::Review::new(&[hx_core::permit::FileEdit {
|
||||
path: "/x/a.rs".into(),
|
||||
old: Some("a\n".into()),
|
||||
new: "b\n".into(),
|
||||
}]);
|
||||
let (tx, _rx) = tokio::sync::oneshot::channel();
|
||||
app.perm = Some((
|
||||
permit_view::Prompt::new(hx_core::permit::Request {
|
||||
tool: "bash".into(),
|
||||
detail: "rm -rf target".into(),
|
||||
rule: "bash:rm".into(),
|
||||
}),
|
||||
tx,
|
||||
));
|
||||
for (w, h) in [(80, 24), (160, 50)] {
|
||||
let s = screen(&mut app, w, h);
|
||||
assert!(
|
||||
s.contains("permission") && s.contains("rm -rf target"),
|
||||
"{s}"
|
||||
);
|
||||
assert!(s.contains("review · 1 files · +1 -1"), "{s}");
|
||||
}
|
||||
app.review = None;
|
||||
app.perm = None;
|
||||
app.stats = Some((hx_core::stats::Summary::default(), "s".into()));
|
||||
assert!(screen(&mut app, 80, 24).contains("stats · s"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ctrl_g_opens_agents_list_detail_and_back() {
|
||||
use hx_core::tool::{Tool, ToolCtx};
|
||||
@@ -1478,6 +1691,12 @@ mod tests {
|
||||
ask: None,
|
||||
lens: (String::new(), false),
|
||||
choose: None,
|
||||
perm: None,
|
||||
review: None,
|
||||
stats: None,
|
||||
side: true,
|
||||
lens_lines: vec![],
|
||||
mcp: vec![],
|
||||
state: Default::default(),
|
||||
sub_t: Default::default(),
|
||||
agents: None,
|
||||
|
||||
Reference in new issue
Block a user