Canvas(macOS 应用)

macOS 应用通过 WKWebView 嵌入了一个由 agent 控制的 Canvas 面板。它是一个轻量级的可视化工作区,用于 HTML/CSS/JS、A2UI 以及小型交互界面。

Canvas 的存储位置

Canvas 状态存储在 Application Support 下:

  • ~/Library/Application Support/OpenClaw/canvas/<session>/...

Canvas 面板通过自定义 URL scheme 提供文件服务:

  • openclaw-canvas://<session>/<path>

示例:

  • openclaw-canvas://main/<canvasRoot>/main/index.html
  • openclaw-canvas://main/assets/app.css<canvasRoot>/main/assets/app.css
  • openclaw-canvas://main/widgets/todo/<canvasRoot>/main/widgets/todo/index.html

如果根目录下没有 index.html,应用会显示一个内置脚手架页面

面板行为

  • 无边框、可调整大小的面板,锚定在菜单栏(或鼠标光标)附近。
  • 按会话记忆窗口大小和位置。
  • 本地 canvas 文件变更时自动重载。
  • 同一时间只显示一个 Canvas 面板(会话按需切换)。

Canvas 可以在 Settings → Allow Canvas 中禁用。禁用后 canvas 节点命令会返回 CANVAS_DISABLED

Agent API

Canvas 通过 Gateway WebSocket 暴露,agent 可以:

  • 显示/隐藏面板
  • 导航到指定路径或 URL
  • 执行 JavaScript
  • 截取快照图片

CLI 示例:

openclaw nodes canvas present --node <id>
openclaw nodes canvas navigate --node <id> --url "/"
openclaw nodes canvas eval --node <id> --js "document.title"
openclaw nodes canvas snapshot --node <id>

说明:

  • canvas.navigate 支持本地 canvas 路径http(s) URL 以及 file:// URL。
  • 传入 "/" 时显示本地脚手架或 index.html

Canvas 中的 A2UI

A2UI 由 Gateway canvas host 承载,在 Canvas 面板内渲染。当 Gateway 广播了 Canvas host 地址时,macOS 应用在首次打开时会自动导航到 A2UI host 页面。

默认 A2UI host URL:

http://<gateway-host>:18789/__openclaw__/a2ui/

A2UI 命令(v0.8)

Canvas 当前接受 A2UI v0.8 的 server→client 消息:

  • beginRendering
  • surfaceUpdate
  • dataModelUpdate
  • deleteSurface

createSurface(v0.9)暂不支持。

CLI 示例:

cat > /tmp/a2ui-v0.8.jsonl <<'EOFA2'
{"surfaceUpdate":{"surfaceId":"main","components":[{"id":"root","component":{"Column":{"children":{"explicitList":["title","content"]}}}},{"id":"title","component":{"Text":{"text":{"literalString":"Canvas (A2UI v0.8)"},"usageHint":"h1"}}},{"id":"content","component":{"Text":{"text":{"literalString":"If you can read this, A2UI push works."},"usageHint":"body"}}}]}}
{"beginRendering":{"surfaceId":"main","root":"root"}}
EOFA2

openclaw nodes canvas a2ui push --jsonl /tmp/a2ui-v0.8.jsonl --node <id>

快速验证:

openclaw nodes canvas a2ui push --node <id> --text "Hello from A2UI"

从 Canvas 触发 Agent 运行

Canvas 可以通过 deep link 触发新的 agent 运行:

  • openclaw://agent?...

JavaScript 示例:

window.location.href = "openclaw://agent?message=Review%20this%20design";

除非提供有效的 key,否则应用会弹出确认对话框。

安全说明

  • Canvas scheme 阻止目录遍历;文件必须位于会话根目录下。
  • 本地 Canvas 内容使用自定义 scheme(无需 loopback 服务器)。
  • 外部 http(s) URL 只有在显式导航时才允许加载。