"""端到端测 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
" "" "", }) if which in ("all", "end"): run_tool("end_conversation", {"reason": "这件事已经办完了,没有后续可聊的。"}) print("\n=== 确认落盘 ===", flush=True) full = call(f"/api/conversations/{CONV}", method="GET").get("conversation") or {} print(" ended =", full.get("ended"), "| endedReason =", full.get("endedReason"), flush=True) if which == "ask": run_tool("ask_user", { "question": "这个方案你要哪一个?", "options": [ {"label": "方案 A", "value": "A", "description": "快但功能少"}, {"label": "方案 B", "value": "B", "description": "慢一点但完整"}, {"label": "都不要", "value": "none"}, ], }, timeout=240) if which == "askmulti": run_tool("ask_user", { "question": "想装哪几个?(可多选)", "multiple": True, "options": ["python", "git", "nmap", "nodejs"], }, timeout=240)