tui: live bash output, token counter, structured lens view for side panel
This commit is contained in:
1 parent
79bd3b8ea7
commit
c57d238f7b
7 files changed
+284
-67
No files matched your search
@@ -30,6 +30,8 @@ pub enum Item {
|
||||
#[derive(Default)]
|
||||
pub struct Transcript {
|
||||
pub items: Vec<Item>,
|
||||
/// live output tails of running bash calls by command (copied from `State::live` per draw)
|
||||
pub live: std::collections::HashMap<String, String>,
|
||||
/// true while the last Assistant/Thinking item is still streaming
|
||||
streaming: bool,
|
||||
}
|
||||
@@ -97,6 +99,17 @@ impl Transcript {
|
||||
let mut out = vec![];
|
||||
for (n, it) in self.items.iter().enumerate() {
|
||||
render(it, expanded, width as usize, &mut out);
|
||||
if let Item::Tool {
|
||||
name,
|
||||
args,
|
||||
result: None,
|
||||
..
|
||||
} = it
|
||||
&& name == "bash"
|
||||
&& let Some(tail) = args["command"].as_str().and_then(|c| self.live.get(c))
|
||||
{
|
||||
live_tail(tail, width as usize, &mut out);
|
||||
}
|
||||
// consecutive tool calls stack without a gap
|
||||
let tool = |i: Option<&Item>| matches!(i, Some(Item::Tool { .. }));
|
||||
if !(tool(Some(it)) && tool(self.items.get(n + 1))) {
|
||||
@@ -120,13 +133,28 @@ impl Transcript {
|
||||
}
|
||||
}
|
||||
|
||||
/// `⠹ working 38s · bash esc to abort` — `frame` picks the spinner glyph.
|
||||
pub fn activity_line(what: &str, elapsed: Duration, frame: usize, width: u16) -> Line<'static> {
|
||||
/// `⠹ working 38s · bash · ↓ 4.1k tokens esc to abort` — `frame` picks the spinner
|
||||
/// glyph, `tokens` = output tokens of this run so far.
|
||||
pub fn activity_line(
|
||||
what: &str,
|
||||
elapsed: Duration,
|
||||
tokens: u64,
|
||||
frame: usize,
|
||||
width: u16,
|
||||
) -> Line<'static> {
|
||||
let th = theme::t();
|
||||
let tk = if tokens >= 1000 {
|
||||
format!("{:.1}k", tokens as f64 / 1000.0)
|
||||
} else {
|
||||
tokens.to_string()
|
||||
};
|
||||
let left = vec![
|
||||
Span::styled(format!("{} ", th.spin(frame)), th.accent()),
|
||||
Span::styled(format!("working {}s", elapsed.as_secs()), th.accent()),
|
||||
Span::styled(format!(" {} {what}", th.sym.info), th.sec()),
|
||||
Span::styled(
|
||||
format!(" {0} {what} {0} {1} {tk} tokens", th.sym.info, th.sym.down),
|
||||
th.sec(),
|
||||
),
|
||||
];
|
||||
let hint = Span::styled("esc to abort", th.dim());
|
||||
let lw: usize = left.iter().map(Span::width).sum();
|
||||
@@ -138,6 +166,25 @@ pub fn activity_line(what: &str, elapsed: Duration, frame: usize, width: u16) ->
|
||||
Line::from(l)
|
||||
}
|
||||
|
||||
/// Last [`LIVE_LINES`] non-empty lines of a running command, dim and indented.
|
||||
fn live_tail(tail: &str, width: usize, out: &mut Vec<Line<'static>>) {
|
||||
let th = theme::t();
|
||||
// carriage returns (progress bars): keep what's after the last one per line
|
||||
let ls: Vec<&str> = tail
|
||||
.lines()
|
||||
.map(|l| l.rsplit('\r').next().unwrap_or(l))
|
||||
.filter(|l| !l.trim().is_empty())
|
||||
.collect();
|
||||
for l in &ls[ls.len().saturating_sub(LIVE_LINES)..] {
|
||||
out.push(Line::styled(
|
||||
format!(" {}", fit(l, width.saturating_sub(4))),
|
||||
th.dim(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
const LIVE_LINES: usize = 2;
|
||||
|
||||
fn target(args: &Value) -> String {
|
||||
let s = ["path", "command", "pattern", "query"]
|
||||
.iter()
|
||||
@@ -727,8 +774,29 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn activity() {
|
||||
let l = activity_line("bash", Duration::from_millis(12_900), 2, 40);
|
||||
assert_eq!(text(&[l]), ["⠹ working 12s · bash esc to abort"]);
|
||||
let l = activity_line("bash", Duration::from_millis(12_900), 4130, 2, 60);
|
||||
assert_eq!(
|
||||
text(&[l]),
|
||||
["⠹ working 12s · bash · ↓ 4.1k tokens esc to abort"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn running_bash_shows_live_tail() {
|
||||
let mut t = Transcript::default();
|
||||
t.apply(&Event::ToolCall {
|
||||
id: "1".into(),
|
||||
name: "bash".into(),
|
||||
args: json!({"command": "cargo test"}),
|
||||
});
|
||||
t.live.insert(
|
||||
"cargo test".into(),
|
||||
" Compiling a\n\n Compiling b\nprogress 10%\rprogress 90%\n".into(),
|
||||
);
|
||||
let s = text(&t.lines(false, 40));
|
||||
assert_eq!(s[1..3], [" Compiling b", " progress 90%"], "{s:?}");
|
||||
t.live.clear();
|
||||
assert_eq!(t.lines(false, 40).len(), 2, "no tail when nothing is live");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user