Add /btw: side question without tools, not saved, works mid-run

This commit is contained in:
hermes committed 2026-10-06 15:17:57 +00:00
1 parent b223fd1f22
commit 28dff172f2
4 files changed
+233 -1

No files matched your search

+51
View File
@@ -0,0 +1,51 @@
//! `/btw` overlay: the side question and its answer; esc closes (and cancels a pending request).
use crate::theme;
use ratatui::layout::Rect;
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Clear, Paragraph, Wrap};
pub struct Btw {
pub q: String,
/// None while the request is running
pub a: Option<Result<String, String>>,
pub scroll: u16,
pub task: tokio::task::JoinHandle<()>,
}
impl Btw {
pub fn close(self) {
self.task.abort();
}
pub fn draw(&self, f: &mut ratatui::Frame) {
let th = theme::t();
let a = f.area();
let w = (a.width * 7 / 10).clamp(40, 110).min(a.width);
let h = (a.height * 7 / 10).clamp(8, 40).min(a.height);
let r = Rect::new(a.x + (a.width - w) / 2, a.y + (a.height - h) / 2, w, h);
let mut body = vec![
Line::styled(format!("> {}", self.q), th.dim()),
Line::default(),
];
match &self.a {
None => body.push(Line::styled("thinking…", th.dim())),
Some(Ok(t)) => body.extend(t.lines().map(|l| Line::raw(l.to_string()))),
Some(Err(e)) => body.push(Line::styled(e.clone(), th.err())),
}
f.render_widget(Clear, r);
f.render_widget(
Paragraph::new(body)
.wrap(Wrap { trim: false })
.scroll((self.scroll, 0))
.block(
Block::bordered()
.border_style(th.dim())
.padding(ratatui::widgets::Padding::horizontal(1))
.title(Span::styled(" btw ", th.dim()))
.title_bottom(Span::styled(" ↑↓ scroll · esc close ", th.dim())),
),
r,
);
}
}
+58 -1
View File
@@ -1,6 +1,7 @@
//! Interactive terminal UI.
pub mod agents;
pub mod btw_view;
pub mod choose;
pub mod editor;
pub mod permit_view;
@@ -104,6 +105,7 @@ enum Ui {
),
Done(Result<(), String>),
Compacted(Result<bool, String>),
Btw(Result<(String, hx_ai::Usage), String>),
}
/// Forwards agent events to the UI loop.
@@ -169,6 +171,9 @@ struct App {
/// keyboard protocol was switched on (shift-enter is distinguishable)
kbd: bool,
stats: Option<(hx_core::stats::Summary, String)>,
/// `/btw` overlay and the agent's live snapshot it answers from
btw: Option<btw_view::Btw>,
snap: Option<tokio::sync::watch::Receiver<Arc<hx_core::agent::Snap>>>,
/// lens/context/MCP side panel (ctrl-l), wide terminals only
side: bool,
lens_lines: Vec<String>,
@@ -340,6 +345,8 @@ pub async fn run(setup: Setup) -> std::io::Result<()> {
probe: false,
kbd,
stats: None,
btw: None,
snap: Some(agent.snap()),
side: true,
lens_lines: vec![],
mcp,
@@ -490,6 +497,17 @@ pub async fn run(setup: Setup) -> std::io::Result<()> {
submit(&mut app, next, &ctx).await;
}
}
Ui::Btw(r) => {
if let Some(b) = &mut app.btw {
b.a = Some(match r {
Ok((t, u)) => {
app.st.usage.add(&u);
Ok(t)
}
Err(e) => Err(e),
});
}
}
Ui::Compacted(r) => {
app.busy = false;
app.t.push(match r {
@@ -912,6 +930,19 @@ fn on_key(app: &mut App, k: KeyEvent, abort: &AbortHandle) -> Option<String> {
if app.sessions.is_some() {
return sessions_key(app, k);
}
if let Some(b) = &mut app.btw {
match k.code {
KeyCode::Esc | KeyCode::Char('q') | KeyCode::Enter => {
if let Some(b) = app.btw.take() {
b.close();
}
}
KeyCode::Up => b.scroll = b.scroll.saturating_sub(1),
KeyCode::Down => b.scroll = b.scroll.saturating_add(1),
_ => {}
}
return None;
}
if app.stats.is_some() {
if matches!(k.code, KeyCode::Esc | KeyCode::Char('q')) {
app.stats = None;
@@ -1116,7 +1147,7 @@ async fn submit(app: &mut App, mut line: String, c: &Ctx<'_>) {
let (c, arg) = cmd
.split_once(' ')
.map_or((cmd, ""), |(a, b)| (a, b.trim()));
if app.busy && c != "quit" {
if app.busy && !matches!(c, "quit" | "btw") {
app.t.push(Item::Error("busy; esc to abort first".into()));
return;
}
@@ -1150,6 +1181,27 @@ async fn submit(app: &mut App, mut line: String, c: &Ctx<'_>) {
None => app.t.push(Item::Info("no edits to review".into())),
}
}
"btw" => {
if arg.is_empty() {
app.t.push(Item::Info("usage: /btw <question>".into()));
} else {
let Some(snap) = app.snap.as_ref().map(|r| r.borrow().clone()) else {
return;
};
let (q, tx) = (arg.to_string(), tx.clone());
let q2 = q.clone();
let task = tokio::spawn(async move {
let r = snap.btw(&q2).await.map_err(|e| e.to_string());
let _ = tx.send(Ui::Btw(r));
});
app.btw = Some(btw_view::Btw {
q,
a: None,
scroll: 0,
task,
});
}
}
"copy" => {
let last = app.t.items.iter().rev().find_map(|i| match i {
Item::Assistant(t) => Some(t.clone()),
@@ -1496,6 +1548,9 @@ fn draw(f: &mut ratatui::Frame, app: &mut App) {
if let Some(s) = &mut app.sessions {
s.draw(f);
}
if let Some(b) = &app.btw {
b.draw(f);
}
if let Some((s, t)) = &app.stats {
stats_view::draw(f, s, t);
}
@@ -2034,6 +2089,8 @@ mod tests {
probe: false,
kbd: false,
stats: None,
btw: None,
snap: None,
side: true,
lens_lines: vec![],
mcp: vec![],