M7: json + rpc modes, subprocess extensions (closes #52 #53 #54)
ci / check (pull_request) Canceled after 0s
ci / check (push) Canceled after 0s

This commit is contained in:
hermes committed 2026-09-30 21:40:20 +00:00
1 parent cf8268d17b
commit a01d0cbb5b
15 files changed
+723 -3

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
#!/usr/bin/env python3
"""Sample hx extension (stdio JSON-RPC). Blocks bash commands matching a pattern and adds a `now` tool.
~/.hx/config.toml:
[[extensions]]
command = ["python3", "/path/to/ext_guard.py", "rm -rf"]
"""
import json, sys, time
deny = sys.argv[1] if len(sys.argv) > 1 else "rm -rf"
for line in sys.stdin:
m = json.loads(line)
p = m.get("params") or {}
meth = m.get("method")
if meth == "initialize":
r = {"subscribe": ["tool_call"],
"tools": [{"name": "now", "description": "Current unix time",
"inputSchema": {"type": "object", "properties": {}}}]}
elif meth == "event":
e = p["event"]
cmd = e["args"].get("command") or e["args"].get("cmd") or ""
if e["name"].lower() == "bash" and deny in cmd:
r = {"action": "block", "reason": f"ext_guard: `{deny}` is not allowed"}
else:
r = {"action": "continue"}
elif meth == "tool/call":
r = {"content": str(int(time.time())), "is_error": False}
else:
print(json.dumps({"jsonrpc": "2.0", "id": m.get("id"), "error": {"code": -32601, "message": "unknown"}}), flush=True)
continue
print(json.dumps({"jsonrpc": "2.0", "id": m["id"], "result": r}), flush=True)
+38
View File
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
"""Minimal client for `hx --mode rpc`: sends a prompt, prints streamed text, then session state.
Usage: python3 examples/rpc_client.py [hx-binary] "prompt"
"""
import json, subprocess, sys
hx = sys.argv[1] if len(sys.argv) > 2 else "hx"
p = subprocess.Popen([hx, "--mode", "rpc"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
nid = 0
def call(method, params=None):
global nid
nid += 1
p.stdin.write(json.dumps({"jsonrpc": "2.0", "id": nid, "method": method, "params": params or {}}) + "\n")
p.stdin.flush()
while True:
m = json.loads(p.stdout.readline())
if m.get("method") == "event":
e = m["params"]
if e["type"] == "stream" and e["event"]["type"] == "text_delta":
print(e["event"]["text"], end="", flush=True)
elif e["type"] == "tool_call":
print(f"\n> {e['name']} {json.dumps(e['args'])[:80]}", flush=True)
continue
if m.get("id") == nid:
if "error" in m:
raise SystemExit(f"{method}: {m['error']['message']}")
return m["result"]
r = call("prompt", {"text": sys.argv[-1]})
print()
s = call("state")
print(f"STATE messages={s['messages']} model={s['model']} tools={len(s['tools'])}")
print(f"BUSY_CHECK {call('set_model', {'model': 'claude-x'})['model']}")
p.stdin.close()
p.wait(timeout=30)