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
@@ -312,6 +312,9 @@ impl Tool for AskUser {
|
||||
}
|
||||
}
|
||||
|
||||
/// Bytes of running bash output kept for the live view.
|
||||
const LIVE_TAIL: usize = 2048;
|
||||
|
||||
pub struct Bash;
|
||||
#[async_trait]
|
||||
impl Tool for Bash {
|
||||
@@ -338,14 +341,37 @@ impl Tool for Bash {
|
||||
// own process group so timeouts kill children too
|
||||
#[cfg(unix)]
|
||||
c.process_group(0);
|
||||
let child = match c.spawn() {
|
||||
let mut child = match c.spawn() {
|
||||
Ok(c) => c,
|
||||
Err(e) => return ToolResult::err(format!("spawn: {e}")),
|
||||
};
|
||||
let pid = child.id();
|
||||
let res = tokio::time::timeout(Duration::from_secs(secs), child.wait_with_output()).await;
|
||||
// read output as it comes so the UI can show the last lines live (state.live)
|
||||
let mut so = child.stdout.take().expect("piped");
|
||||
let live = &cx.state.live;
|
||||
let run = async {
|
||||
use tokio::io::AsyncReadExt;
|
||||
let mut out = vec![];
|
||||
let mut chunk = [0u8; 8192];
|
||||
loop {
|
||||
match so.read(&mut chunk).await {
|
||||
Ok(0) | Err(_) => break,
|
||||
Ok(n) => {
|
||||
out.extend_from_slice(&chunk[..n]);
|
||||
let start = out.len().saturating_sub(LIVE_TAIL);
|
||||
live.lock().unwrap_or_else(|e| e.into_inner()).insert(
|
||||
cmd.to_string(),
|
||||
String::from_utf8_lossy(&out[start..]).into(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
child.wait().await.map(|st| (out, st))
|
||||
};
|
||||
let res = tokio::time::timeout(Duration::from_secs(secs), run).await;
|
||||
live.lock().unwrap_or_else(|e| e.into_inner()).remove(cmd);
|
||||
let (out, code) = match res {
|
||||
Ok(Ok(o)) => (o.stdout, o.status.code()),
|
||||
Ok(Ok((o, st))) => (o, st.code()),
|
||||
Ok(Err(e)) => return ToolResult::err(format!("wait: {e}")),
|
||||
Err(_) => {
|
||||
#[cfg(unix)]
|
||||
@@ -449,6 +475,22 @@ mod tests {
|
||||
use super::*;
|
||||
use std::time::Instant;
|
||||
|
||||
#[tokio::test]
|
||||
async fn bash_streams_live_tail() {
|
||||
let d = tempfile::tempdir().unwrap();
|
||||
let c = cx(d.path());
|
||||
let cmd = "echo one; sleep 0.4; echo two";
|
||||
let st = c.state.clone();
|
||||
let peek = tokio::spawn(async move {
|
||||
tokio::time::sleep(Duration::from_millis(200)).await;
|
||||
st.live.lock().unwrap().get(cmd).cloned()
|
||||
});
|
||||
let r = Bash.call(json!({"command": cmd}), &c).await;
|
||||
assert_eq!(peek.await.unwrap().as_deref(), Some("one\n"));
|
||||
assert_eq!(r.content, "one\ntwo\n");
|
||||
assert!(c.state.live.lock().unwrap().is_empty(), "cleared when done");
|
||||
}
|
||||
|
||||
fn cx(d: &Path) -> ToolCtx {
|
||||
ToolCtx {
|
||||
cwd: d.into(),
|
||||
|
||||
Reference in new issue
Block a user