claude_cli: permission prompts via --permission-prompt-tool, bridged to the hx TUI
This commit is contained in:
1 parent
b06c06ad80
commit
0032a5a952
8 files changed
+248
-46
No files matched your search
@@ -1,5 +1,5 @@
|
||||
#!/bin/sh
|
||||
# claude_cli = [] routes Claude through the `claude` binary (fake here): streamed text arrives, argv is right.
|
||||
# (print mode has no prompt, so the permission bridge allows) claude_cli = [] routes Claude through the `claude` binary (fake here): streamed text arrives, argv is right.
|
||||
set -eu
|
||||
root=$(cd "$(dirname "$0")/.." && pwd)
|
||||
tmp=$(mktemp -d)
|
||||
@@ -10,6 +10,6 @@ chmod +x "$root/tests/fake_claude.py"
|
||||
cd "$tmp/repo"
|
||||
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; }
|
||||
grep -q "hello from claude" "$tmp/out" || { echo "FAIL text"; cat "$tmp/out" "$tmp/err"; exit 1; }
|
||||
grep -q '"--mcp-config"' "$tmp/argv" && grep -q '"stream-json"' "$tmp/argv" || { echo "FAIL argv"; cat "$tmp/argv"; exit 1; }
|
||||
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; }
|
||||
echo "claude cli e2e ok"
|
||||
+14
-1
@@ -5,10 +5,23 @@ open(os.environ["FAKE_CLAUDE_LOG"], "a").write(json.dumps(sys.argv[1:]) + "\n")
|
||||
if "--version" in sys.argv:
|
||||
print("fake"); sys.exit(0)
|
||||
def out(o): print(json.dumps(o), flush=True)
|
||||
reply = "hello from claude"
|
||||
if "--permission-prompt-tool" in sys.argv: # act like claude: ask hx whether `rm -rf x` may run
|
||||
import subprocess
|
||||
cfg = json.loads(sys.argv[sys.argv.index("--mcp-config") + 1])["mcpServers"]["hx"]
|
||||
p = subprocess.Popen([cfg["command"], *cfg["args"]], stdin=subprocess.PIPE, stdout=subprocess.PIPE,
|
||||
text=True, env={**os.environ, **cfg["env"]})
|
||||
call = {"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "approve", "arguments":
|
||||
{"tool_name": "Bash", "input": {"command": "rm -rf x"}}}}
|
||||
p.stdin.write(json.dumps({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {}}) + "\n" + json.dumps(call) + "\n")
|
||||
p.stdin.flush()
|
||||
lines = [json.loads(p.stdout.readline()) for _ in range(2)]
|
||||
p.stdin.close()
|
||||
reply = "approve: " + json.loads(lines[1]["result"]["content"][0]["text"])["behavior"]
|
||||
out({"type": "system", "subtype": "init", "session_id": "S1"})
|
||||
ev = lambda e: out({"type": "stream_event", "session_id": "S1", "event": e})
|
||||
ev({"type": "content_block_start", "content_block": {"type": "tool_use", "name": "Read"}})
|
||||
ev({"type": "content_block_delta", "delta": {"type": "text_delta", "text": "hello "}})
|
||||
ev({"type": "content_block_delta", "delta": {"type": "text_delta", "text": reply + " "}})
|
||||
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",
|
||||
"usage": {"input_tokens": 3, "output_tokens": 4}})
|
||||
Reference in new issue
Block a user