"""端到端测 AI 界面三件套:宿主机 → 内核 /api/tools/run → 安卓桥。 ★ 为什么从宿主机打而不是在设备上敲:PowerShell/adb 的多层引号会把 JSON 搅烂(这一路上已经踩过好几次)。adb forward 之后在宿主机上用 Python 打, 引号只有一层,看得清。 ★ ask_user 是**阻塞**的:脚本会挂在那儿等用户在手机上点。 所以它放在最后,并且单独一个命令跑(另一个终端截图看界面)。 """ import json import sys import threading import time import urllib.request try: sys.stdout.reconfigure(encoding="utf-8", errors="replace") except Exception: pass URL = "http://127.0.0.1:15199" CONV = "c_main" def call(path, payload=None, method="POST", timeout=180): data = json.dumps(payload, ensure_ascii=False).encode("utf-8") if payload is not None else None req = urllib.request.Request( URL + path, data=data, method=method, headers={"Content-Type": "application/json; charset=utf-8"}, ) with urllib.request.urlopen(req, timeout=timeout) as r: return json.loads(r.read().decode("utf-8")) def run_tool(name, args, timeout=150): print(f"\n--- {name} ---", flush=True) print(" 参数:", json.dumps(args, ensure_ascii=False)[:160], flush=True) t0 = time.time() res = call("/api/tools/run", {"name": name, "args": args, "conversationId": CONV}, timeout=timeout) print(f" 耗时 {time.time() - t0:.1f}s -> {json.dumps(res, ensure_ascii=False)[:300]}", flush=True) return res which = sys.argv[1] if len(sys.argv) > 1 else "all" if which in ("all", "state"): st = call("/api/state", method="GET") print("uiTools =", json.dumps((st.get("settings") or {}).get("uiTools"), ensure_ascii=False)) if which in ("all", "screen"): run_tool("show_ui", { "title": "测试界面", "html": "
这是 AI 写的 HTML。
" "| 项 | 值 |
|---|---|
| 甲 | 1 |