"""测「AI 写进网页里的接口」——用户在 AI 自己的界面里选的值能不能回到模型。 三步: 1. 用 show_ui 弹一个界面,页面里有 Doubao.sendToAi / Doubao.pick / Doubao.close 2. (手动)在手机上点那些按钮 3. 查内核队列 —— 值应该排在那里,等下一轮对话交给模型 """ import json import sys 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" HTML = """

接口测试

点下面的按钮,值应该会回到 AI 手里(下一轮对话时)。

(还没点)
""" def call(path, payload=None, method="POST", timeout=120): 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")) which = sys.argv[1] if len(sys.argv) > 1 else "show" if which == "show": # 打开界面能力(否则工具被第二道拦截挡住) call("/api/settings", {"uiTools": {"enabled": True}}, method="PUT") res = call("/api/tools/run", { "name": "show_ui", "args": {"title": "接口测试", "html": HTML}, "conversationId": CONV, }) print("show_ui ->", json.dumps(res, ensure_ascii=False)[:200]) print("\n现在去手机上点按钮,然后跑:") print(" python scripts/probe-ui-result.py check") print(" python scripts/probe-ui-result.py send # 发一句话,看模型能不能看到那些值") elif which == "check": q = call(f"/api/ui-result?conversationId={CONV}", method="GET") print("队列里现在有:", json.dumps(q, ensure_ascii=False)) n = len(q.get("pending") or []) print(f" {n} 条" + (" ✓ 回流成功" if n else " ✗ 一条都没有 —— 回流没成功")) elif which == "send": # 发一句话,触发一轮对话;agent_loop 会把队列里的值带上 # # ★ 不需要真的配 API key:我们要验证的是"队列有没有被取走、有没有拼进 # 发给模型的消息里"。模型调用失败是另一回事(会以 error 事件返回), # 而**取队列发生在调用之前** —— 所以看队列空了就说明接上了。 print("发一句话(触发一轮对话)…") try: res = call("/api/chat", { "conversationId": CONV, "message": "我刚才选了什么?", "stream": False, }, timeout=180) print(json.dumps(res, ensure_ascii=False)[:800]) except Exception as exc: # noqa: BLE001 print("请求失败(可能是没配 API key,不影响本项验证):", exc) q = call(f"/api/ui-result?conversationId={CONV}", method="GET") n = len(q.get("pending") or []) print("\n发完之后队列:", json.dumps(q, ensure_ascii=False)) if n == 0: print(" ✓ 队列被取走了 —— 接进上下文了") else: print(f" ✗ 队列还有 {n} 条 —— 没被取走,回流链路断了")