claude_cli: show what each claude tool runs #166
No files matched your search
@@ -94,6 +94,26 @@ fn prompt(msgs: &[Message], resumed: bool) -> String {
|
|||||||
s + "\nCurrent request:\n" + &msgs[last].text()
|
s + "\nCurrent request:\n" + &msgs[last].text()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `Bash: cargo test`, `Edit: src/a.rs`; other tools show their input, shortened.
|
||||||
|
fn tool_line(b: &Value) -> String {
|
||||||
|
let name = b["name"].as_str().unwrap_or("tool");
|
||||||
|
let i = &b["input"];
|
||||||
|
let d = [
|
||||||
|
"command",
|
||||||
|
"file_path",
|
||||||
|
"notebook_path",
|
||||||
|
"pattern",
|
||||||
|
"url",
|
||||||
|
"query",
|
||||||
|
"description",
|
||||||
|
]
|
||||||
|
.iter()
|
||||||
|
.find_map(|k| i[*k].as_str().map(String::from))
|
||||||
|
.unwrap_or_else(|| i.to_string());
|
||||||
|
let d: String = d.chars().take(300).collect();
|
||||||
|
format!("{name}: {d}")
|
||||||
|
}
|
||||||
|
|
||||||
fn usage(u: &Value) -> Usage {
|
fn usage(u: &Value) -> Usage {
|
||||||
let n = |k: &str| u[k].as_u64().unwrap_or(0);
|
let n = |k: &str| u[k].as_u64().unwrap_or(0);
|
||||||
Usage {
|
Usage {
|
||||||
@@ -170,8 +190,8 @@ impl Provider for ClaudeCli {
|
|||||||
match v["type"].as_str() {
|
match v["type"].as_str() {
|
||||||
Some("stream_event") => {
|
Some("stream_event") => {
|
||||||
let e = &v["event"];
|
let e = &v["event"];
|
||||||
match e["type"].as_str() {
|
if e["type"] == "content_block_delta" {
|
||||||
Some("content_block_delta") => match e["delta"]["type"].as_str() {
|
match e["delta"]["type"].as_str() {
|
||||||
Some("text_delta") => {
|
Some("text_delta") => {
|
||||||
let t = e["delta"]["text"].as_str().unwrap_or_default();
|
let t = e["delta"]["text"].as_str().unwrap_or_default();
|
||||||
text += t;
|
text += t;
|
||||||
@@ -184,17 +204,17 @@ impl Provider for ClaudeCli {
|
|||||||
tx.send(Ok(StreamEvent::ThinkingDelta { text: t.into() }));
|
tx.send(Ok(StreamEvent::ThinkingDelta { text: t.into() }));
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
}
|
||||||
// claude runs its tools itself; show them, don't hand them to hx
|
}
|
||||||
Some("content_block_start")
|
}
|
||||||
if e["content_block"]["type"] == "tool_use" =>
|
// complete tool calls (the stream events only carry partial JSON)
|
||||||
{
|
Some("assistant") => {
|
||||||
let n = e["content_block"]["name"].as_str().unwrap_or("tool");
|
for b in v["message"]["content"].as_array().into_iter().flatten() {
|
||||||
|
if b["type"] == "tool_use" {
|
||||||
let _ = tx.send(Ok(StreamEvent::TextDelta {
|
let _ = tx.send(Ok(StreamEvent::TextDelta {
|
||||||
text: format!("\n[claude tool: {n}]\n"),
|
text: format!("\n[claude {}]\n", tool_line(b)),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some("result") => {
|
Some("result") => {
|
||||||
@@ -250,6 +270,16 @@ impl Provider for ClaudeCli {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tool_line_shows_what_runs() {
|
||||||
|
let b = json!({"name": "Bash", "input": {"command": "cargo test", "description": "run"}});
|
||||||
|
assert_eq!(tool_line(&b), "Bash: cargo test");
|
||||||
|
let b = json!({"name": "Edit", "input": {"file_path": "a.rs", "old_string": "x"}});
|
||||||
|
assert_eq!(tool_line(&b), "Edit: a.rs");
|
||||||
|
let b = json!({"name": "X", "input": {"a": 1}});
|
||||||
|
assert_eq!(tool_line(&b), "X: {\"a\":1}");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn prompt_replays_history_only_on_fresh_session() {
|
fn prompt_replays_history_only_on_fresh_session() {
|
||||||
let m = vec![
|
let m = vec![
|
||||||
|
|||||||
@@ -10,6 +10,6 @@ chmod +x "$root/tests/fake_claude.py"
|
|||||||
cd "$tmp/repo"
|
cd "$tmp/repo"
|
||||||
export HOME="$tmp/home" HX_CLAUDE_BIN="$root/tests/fake_claude.py" FAKE_CLAUDE_LOG="$tmp/argv"
|
export HOME="$tmp/home" HX_CLAUDE_BIN="$root/tests/fake_claude.py" FAKE_CLAUDE_LOG="$tmp/argv"
|
||||||
"$root/target/debug/hx" -p "hi" -s "$tmp/s.jsonl" > "$tmp/out" 2> "$tmp/err" || { echo "FAIL run"; cat "$tmp/err"; exit 1; }
|
"$root/target/debug/hx" -p "hi" -s "$tmp/s.jsonl" > "$tmp/out" 2> "$tmp/err" || { echo "FAIL run"; cat "$tmp/err"; exit 1; }
|
||||||
grep -q "approve: allow" "$tmp/out" || { echo "FAIL text"; cat "$tmp/out" "$tmp/err"; exit 1; }
|
grep -q "claude Bash: echo hi-from-tool" "$tmp/out" && grep -q "approve: allow" "$tmp/out" || { echo "FAIL text"; cat "$tmp/out" "$tmp/err"; exit 1; }
|
||||||
grep -q '"--permission-prompt-tool"' "$tmp/argv" && grep -q '"--mcp-config"' "$tmp/argv" && grep -q '"stream-json"' "$tmp/argv" || { echo "FAIL argv"; cat "$tmp/argv"; exit 1; }
|
grep -q '"--permission-prompt-tool"' "$tmp/argv" && grep -q '"--mcp-config"' "$tmp/argv" && grep -q '"stream-json"' "$tmp/argv" || { echo "FAIL argv"; cat "$tmp/argv"; exit 1; }
|
||||||
echo "claude cli e2e ok"
|
echo "claude cli e2e ok"
|
||||||
@@ -20,7 +20,8 @@ if "--permission-prompt-tool" in sys.argv: # act like claude: ask hx whether `r
|
|||||||
reply = "approve: " + json.loads(lines[1]["result"]["content"][0]["text"])["behavior"]
|
reply = "approve: " + json.loads(lines[1]["result"]["content"][0]["text"])["behavior"]
|
||||||
out({"type": "system", "subtype": "init", "session_id": "S1"})
|
out({"type": "system", "subtype": "init", "session_id": "S1"})
|
||||||
ev = lambda e: out({"type": "stream_event", "session_id": "S1", "event": e})
|
ev = lambda e: out({"type": "stream_event", "session_id": "S1", "event": e})
|
||||||
ev({"type": "content_block_start", "content_block": {"type": "tool_use", "name": "Read"}})
|
out({"type": "assistant", "session_id": "S1", "message": {"content": [
|
||||||
|
{"type": "tool_use", "name": "Bash", "input": {"command": "echo hi-from-tool"}}]}})
|
||||||
ev({"type": "content_block_delta", "delta": {"type": "text_delta", "text": reply + " "}})
|
ev({"type": "content_block_delta", "delta": {"type": "text_delta", "text": reply + " "}})
|
||||||
ev({"type": "content_block_delta", "delta": {"type": "text_delta", "text": "from claude"}})
|
ev({"type": "content_block_delta", "delta": {"type": "text_delta", "text": "from claude"}})
|
||||||
out({"type": "result", "session_id": "S1", "is_error": False, "result": "hello from claude",
|
out({"type": "result", "session_id": "S1", "is_error": False, "result": "hello from claude",
|
||||||
|
|||||||
Reference in new issue
Block a user