"""让 AI 主动弹出一个 WebView 界面(自己写 HTML)。 ## 这个工具存在的意义 普通的对话是一来一回的文本。有些东西**用界面表达比用文字好得多**: 一张自己算出来的图表、一个把结果排版好的报告卡片、一个可以点的菜单、 一段带样式的对比表。以前模型只能吐 markdown,表格一宽就乱。 这个工具让模型**直接写 HTML**,App 用一个全屏 WebView 渲染出来。 ## 用法约定(也是给模型看的) · `html` 里写**片段**就行(不用 /
),会自动套一层带样式的骨架 —— 这样模型不用每次重复写 viewport、暗色主题这些 · 想要完整控制就传 `full_html=true`,那时 html 必须是一份完整文档 · 界面里可以放按钮:`` —— 用户点了之后,那个 `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 TOOL = { "name": "show_ui", "description": ( "在用户手机/平板上弹出一个界面,自己写 HTML 渲染(全屏 WebView)。" "适合:图表、排版好的报告卡片、对比表、可点的菜单、任何用文字说不清的东西。" "html 里只写片段即可(不用 html/body 标签,会自动套一层带样式的骨架);" "想完全控制就传 full_html=true。" "界面里可以放 ," "用户点了之后那个值会回到你手里。" "调用后界面立刻显示、本工具立刻返回,用户看到之后再继续对话。" ), "parameters": { "type": "object", "properties": { "html": { "type": "string", "description": "要渲染的 HTML(片段即可,除非 full_html=true)", }, "title": { "type": "string", "description": "顶部标题栏文字,可留空", }, "full_html": { "type": "boolean", "description": "true 表示 html 是一份完整文档,不再套骨架。默认 false", }, }, "required": ["html"], }, } def run(args: dict) -> dict: html = str(args.get("html") or "").strip() if not html: raise ValueError("html 不能为空") res = call_device( { "action": "ui_screen", "html": html, "title": str(args.get("title") or ""), "full_html": bool(args.get("full_html")), "conversationId": conversation_id(), } ) if res.get("unavailable"): raise RuntimeError(res.get("error") or "当前环境没有界面能力") if res.get("error"): raise RuntimeError(str(res["error"])) return { "shown": True, "bytes": len(html), "note": "界面已弹出,用户看完会自己关掉。你不需要再重复内容。", } if __name__ == "__main__": serve(TOOL, run)