"""测 show_ui 的三种收尾方式:不等 / 等 / 不弹。 ★ 等待模式是**阻塞**的:脚本会挂在那儿等用户在手机上点。 所以它单独一个命令跑(另一个窗口截图 + 点按钮)。 """ 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" # 按钮故意写得很朴素(只靠骨架的 .db-btn 样式)—— # 就是要验证"模型不写样式时,按钮是不是也明显、也能点"。 HTML_PLAIN = """

挑一个

下面这些是回传按钮,点了值会回到 AI 手里。

""" def call(path, payload=None, method="POST", timeout=300): 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_show(args, label, timeout=300): print(f"\n--- {label} ---", flush=True) print(" 参数:", json.dumps(args, ensure_ascii=False)[:120], flush=True) t0 = time.time() res = call("/api/tools/run", { "name": "show_ui", "args": args, "conversationId": CONV, }, timeout=timeout) dt = time.time() - t0 print(f" 耗时 {dt:.1f}s -> {json.dumps(res, ensure_ascii=False)[:500]}", flush=True) return res which = sys.argv[1] if len(sys.argv) > 1 else "nowait" call("/api/settings", {"uiTools": {"enabled": True}}, "PUT") if which == "nowait": # 不等:应该**立刻**返回(耗时接近 0) res = run_show({"title": "不等待", "html": HTML_PLAIN}, "wait=false(默认)") r = (res or {}).get("result") or {} if r.get("shown") and not r.get("waited"): print(" ✓ 立刻返回了(没等待)") else: print(" ✗ 行为不对:", r) elif which == "wait": # 等:会挂在这儿直到用户在手机上点,或 60 秒超时 res = run_show( {"title": "等待中", "html": HTML_PLAIN, "wait": True, "timeout": 60}, "wait=true(等用户点,最多 60s)", ) r = (res or {}).get("result") or {} print() if r.get("timeout"): print(" → 超时了(用户没点)") elif r.get("value"): print(f" ✓ 用户点了:{r['value']!r}") elif r.get("closed"): print(" ✓ 用户直接关掉了(value 是空串)") else: print(" ✗ 结果不对:", r)