用户要求"AI 询问界面不要限时"。v2.0.44 改了界面侧(AiSurface 无限等)和
工具侧(ask_user 的 timeout 默认 0),但**漏了内核的工具执行超时**:
实测:弹窗 135 秒确实还在,但工具调用在 60.0 秒返回
{"ok":false,"error":"工具执行超时(60s)"} —— 用户后来点的选项根本
回不到模型手里,模型已经拿着"超时"自己往下写了。
改动:
- builtin_tools.run_with_timeout:timeout_s <= 0 = 不限时(thread.join() 不带参)
- tools/ask_user.py、tools/show_ui.py:声明 WAIT_FOR_USER = True
- tools_runtime:新增 _waits_for_user(),会等用户的工具把超时预算设成 0;
普通工具仍是 60 秒(死循环的安全网没丢)
验证(scripts/probe-ask-user-forever.py,真机跑):
修之前 60.0 秒 → {"ok":false,"error":"工具执行超时(60s)"}
修之后 66.2 秒 → {"ok":true,"result":{...,"choice":"B","timeout":false}}
守门:scripts/verify-ui-tools.py 新增一节(声明在不在、豁免逻辑在不在、
预算 0 是不是真的无限等、普通工具的 60 秒还在不在)。
文档:docs/AI界面能力.md 补"不限时要三处一起改"和客户端长连接为什么不断
(15 秒心跳 vs 120 秒 readTimeout)。
版本 2.0.45。
211 lines
9.3 KiB
Python
211 lines
9.3 KiB
Python
"""让 AI 主动弹出一个 WebView 界面(自己写 HTML)。
|
||
|
||
## 这个工具存在的意义
|
||
|
||
普通的对话是一来一回的文本。有些东西**用界面表达比用文字好得多**:
|
||
一张自己算出来的图表、一个把结果排版好的报告卡片、一个可以点的菜单、
|
||
一段带样式的对比表。以前模型只能吐 markdown,表格一宽就乱。
|
||
|
||
这个工具让模型**直接写 HTML**,App 用一个全屏 WebView 渲染出来。
|
||
|
||
## 用法约定(也是给模型看的)
|
||
|
||
· `html` 里写**片段**就行(不用 <html>/<body>),会自动套一层带样式的骨架 ——
|
||
这样模型不用每次重复写 viewport、暗色主题这些
|
||
· 想要完整控制就传 `full_html=true`,那时 html 必须是一份完整文档
|
||
· 界面里可以放按钮:`<button data-doubao-result="选了A">A</button>`
|
||
—— 用户点了之后,那个 `data-doubao-result` 的值会回到模型手里
|
||
(走 [ask_user] 那条回传通道,所以不需要模型自己写 JS)
|
||
|
||
## 电脑端
|
||
|
||
桌面版的界面在桌面窗口里,这个工具**明确返回不可用**,不假装成功。
|
||
"""
|
||
|
||
import os
|
||
import sys
|
||
|
||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||
from _protocol import serve # noqa: E402
|
||
from _ui_bridge import call_device, conversation_id # noqa: E402
|
||
|
||
# wait=true 时的等待秒数上限 / 下限。0 = 不限时(默认,见 run)。
|
||
MIN_TIMEOUT_S = 5.0
|
||
MAX_TIMEOUT_S = 3600.0
|
||
|
||
# ★★ 告诉内核:这个工具在 wait=true 时**会停下来等用户**,别拿 60 秒的
|
||
# 工具超时卡它(wait=false 时它立刻就返回,不受影响)。
|
||
# 不声明的后果和 ask_user 那边一样:界面还开着,模型却已经收到"工具超时"。
|
||
# 见 android/kernel/tools_runtime.py 的 _waits_for_user。
|
||
WAIT_FOR_USER = True
|
||
|
||
TOOL = {
|
||
"name": "show_ui",
|
||
"description": (
|
||
"在用户手机/平板上弹出一个界面,自己写 HTML 渲染(全屏 WebView,**支持 JavaScript**)。"
|
||
"适合:图表、排版好的报告卡片、对比表、可点的菜单、任何用文字说不清的东西。"
|
||
"\n\n用法:"
|
||
"\n· html 里只写片段即可(不用 html/body 标签,会自动套一层带样式的骨架);"
|
||
"想完全控制就传 full_html=true。"
|
||
"\n· **可以用 <script>**:canvas 画图、SVG 动画、算完再渲染都行。"
|
||
"\n· 用户的选择会回到你手里 —— 页面里调:"
|
||
"\n Doubao.sendToAi('值') 或 Doubao.pick('值') → 回传值给你"
|
||
"\n Doubao.close() → 关掉这个界面"
|
||
"\n 也可以不写 JS,直接放按钮:"
|
||
"\n <button data-doubao-result=\"值\">文字</button>"
|
||
"\n· 普通链接(http/https)**在界面里打开**,用户可以点进去看,顶部有返回键。"
|
||
"\n"
|
||
"\n★★ **三种收尾方式,按需要选**(这是这个工具最重要的一个参数):"
|
||
"\n"
|
||
"\n wait=false(默认)—— 不等,弹出就继续。适合图表、报告这类"
|
||
"\n \"给用户看\"的东西。用户看完自己关,你这一轮已经答完了。"
|
||
"\n 用户后来说话时,你能看到他在界面上点过什么(会作为上下文附给你)。"
|
||
"\n"
|
||
"\n wait=true —— **等用户操作**(点回传按钮或关掉界面,**默认不限时,一直等**)。"
|
||
"\n 适合\"弹出几个选项让用户挑,然后我接着干\"。用户点了什么会**立刻**"
|
||
"\n 作为本工具的返回值给你,你能在**同一个回合里**继续处理。"
|
||
"\n 用户直接关掉界面(没点任何按钮)时返回 value=空串、closed=true。"
|
||
"\n ★ 别自己传 timeout —— 用户在想事情的时候界面自己消失很难受。"
|
||
"\n 只有\"用户不在就算了\"的场景才传(秒,上限 3600)。"
|
||
"\n"
|
||
"\n 也可以**什么都不做**:不弹界面,直接文字回答。"
|
||
"\n"
|
||
"\n★ 要放让用户点的按钮时,把按钮做得**明显**:"
|
||
"\n 深色底 + 亮色字(或亮色底 + 深色字),padding 至少 14px、字号至少 16px。"
|
||
"\n 纯文字当按钮、或者暗底暗字,用户根本看不出那儿能点。"
|
||
),
|
||
"parameters": {
|
||
"type": "object",
|
||
"properties": {
|
||
"html": {
|
||
"type": "string",
|
||
"description": "要渲染的 HTML(片段即可,除非 full_html=true)",
|
||
},
|
||
"title": {
|
||
"type": "string",
|
||
"description": "顶部标题栏文字,可留空",
|
||
},
|
||
"full_html": {
|
||
"type": "boolean",
|
||
"description": "true 表示 html 是一份完整文档,不再套骨架。默认 false",
|
||
},
|
||
"wait": {
|
||
"type": "boolean",
|
||
"description": (
|
||
"false(默认)弹出就返回,不等用户;"
|
||
"true 等用户点回传按钮或关掉界面再返回(**默认不限时**)"
|
||
),
|
||
},
|
||
"timeout": {
|
||
"type": "number",
|
||
"description": (
|
||
"wait=true 时最多等多少秒。**默认 0 = 不限时**,一般别传。"
|
||
"只有确实要\"用户不在就算了\"时才传(5–3600)"
|
||
),
|
||
},
|
||
},
|
||
"required": ["html"],
|
||
},
|
||
}
|
||
|
||
|
||
def _timeout_seconds(args: dict) -> float:
|
||
"""
|
||
算等多久。**0 = 不限时**(默认)。
|
||
|
||
★ 模型传了才限时,并且夹在 [MIN_TIMEOUT_S, MAX_TIMEOUT_S]。
|
||
传负数 / 0 / 垃圾值一律退回"不限时"—— 那些都是模型写错了,
|
||
不该让它把界面变成一闪而过。
|
||
"""
|
||
raw = args.get("timeout")
|
||
if raw in (None, "", 0, "0"):
|
||
return 0.0
|
||
try:
|
||
sec = float(raw)
|
||
except (TypeError, ValueError):
|
||
return 0.0
|
||
if sec <= 0:
|
||
return 0.0
|
||
return max(MIN_TIMEOUT_S, min(MAX_TIMEOUT_S, sec))
|
||
|
||
|
||
def run(args: dict) -> dict:
|
||
html = str(args.get("html") or "").strip()
|
||
if not html:
|
||
raise ValueError("html 不能为空")
|
||
|
||
wait = bool(args.get("wait"))
|
||
# ★★ 默认 0 = **不限时**(用户要求"AI 询问界面不要限时")。
|
||
# 以前默认 120 秒、上限 600,结果是用户还在看界面它自己没了。
|
||
# 模型显式传了才限时:下限 5 秒(低于这个值用户来不及看清界面),
|
||
# 上限 1 小时(再长没意义,那条 HTTP 工作线程会一直占着)。
|
||
timeout = _timeout_seconds(args)
|
||
|
||
payload = {
|
||
"action": "ui_screen",
|
||
"html": html,
|
||
"title": str(args.get("title") or ""),
|
||
"full_html": bool(args.get("full_html")),
|
||
"conversationId": conversation_id(),
|
||
"wait": wait,
|
||
}
|
||
if wait:
|
||
payload["timeoutMs"] = int(timeout * 1000)
|
||
|
||
res = call_device(payload)
|
||
if res.get("unavailable"):
|
||
raise RuntimeError(res.get("error") or "当前环境没有界面能力")
|
||
if res.get("error"):
|
||
raise RuntimeError(str(res["error"]))
|
||
|
||
out = {"shown": True, "bytes": len(html)}
|
||
|
||
# ★★ 磁盘上那份的 id —— 用户关掉界面之后还能再打开它。
|
||
#
|
||
# 这个 id 会一路回到模型的工具返回值、再进 App 的工具痕迹记录,
|
||
# 消息操作栏据此显示「界面」按钮。
|
||
# (用户的原话:"AI 写的界面可以再点开")
|
||
screen_id = str(res.get("screenId") or "")
|
||
if screen_id:
|
||
out["screenId"] = screen_id
|
||
|
||
if not wait:
|
||
# 不等待:这一轮就到此为止。用户之后说话时,他在界面上点过什么
|
||
# 会作为上下文附给模型(内核的待交付队列),所以这里如实说明。
|
||
out["note"] = (
|
||
"界面已弹出,你没有等用户。用户看完会自己关掉;"
|
||
"他在界面上点过什么,等他下次说话时会作为上下文给你。"
|
||
"现在可以正常收尾这一轮了。"
|
||
"(这个界面已经存下来了,用户随时能再打开,不用你再生成一遍。)"
|
||
)
|
||
return out
|
||
|
||
# ---- 等待模式:用户点了什么 / 关掉了 / 超时 ----
|
||
out["waited"] = True
|
||
out["timeout"] = bool(res.get("timeout"))
|
||
out["closed"] = bool(res.get("closed"))
|
||
value = str(res.get("value") or "")
|
||
out["value"] = value
|
||
|
||
if out["timeout"]:
|
||
mins = timeout / 60
|
||
waited = f"{timeout:.0f} 秒" if timeout < 120 else f"约 {mins:.0f} 分钟"
|
||
out["note"] = (
|
||
f"等了{waited}用户也没操作(你显式设了 timeout),界面已收起。"
|
||
"当作用户暂时没空处理,别反复重弹 —— 正常收尾,等他自己来说。"
|
||
)
|
||
elif value:
|
||
out["note"] = (
|
||
"用户点了这个。**现在就按这个值继续处理**,不用再问一遍、也不用重复界面内容。"
|
||
)
|
||
else:
|
||
out["note"] = (
|
||
"用户没点任何按钮,直接把界面关掉了。"
|
||
"通常表示他不想选 / 想自己说 —— 别重弹同一个界面,用一句话收尾就行。"
|
||
)
|
||
return out
|
||
|
||
|
||
if __name__ == "__main__":
|
||
serve(TOOL, run)
|