🤖 Open-source, local-first alternative to Grok Bot, OpenAI dots and Claude Duet. Your own AI bots on any model: GPT, Claude, Gemini, Grok, DeepSeek, Kimi, GLM and 70+ more through one Modelflare key. Reach them in the app, WeChat or Feishu; they remember you, work in the background, run commands and use your computer. Pay per use.
Python ai-agentai-assistantchatbotclaudecomputer-usedeepseekdesktop-appdshelectronfeishugeminigptgrokllmlocal-firstmodelflaremulti-modelopenai-compatiblepersonal-ai-agentwechat
Computer Use: Cua Driver 0.37.0 as default backend on all platforms; auto-run by default
Marla502f1ad41827
diff --git a/README.md b/README.mdindex a01c09e..1670892 100644--- a/README.md+++ b/README.md@@ -23,7 +23,7 @@ src/main.ts 命令行入口electron/ 桌面版主进程(启动器 + 托盘)ui/ 管理界面(Preact + htm,无构建步骤)-scripts/ setup / build-magpie / electron-smoke+scripts/ setup / build-magpie / fetch-cua-driver / electron-smokevendor/magpie magpie 源码(git 子模块,构建时编译)```@@ -74,6 +74,21 @@ docker exec amb cat /data/ui-token # 打开 http://<主机>:3600/?token=<令- 隔离与安全:magpie 使用本程序数据目录内独立的 HOME/配置目录;**不读取**你电脑上已有的 Claude/Codex 等订阅凭据,**不改写**任何 agent 配置,关闭自动更新与账户发现;界面代理还会拦截 magpie 的 agent 接入、账户、更新等接口。- API Key 只保存在 magpie 的隔离配置中,不经过命令行参数、不写日志。+## 电脑操作(Computer Use)++每个机器人可在「机器人 → 设置 → 电脑操作」中开关、选择后端和审批方式;`/status` 与设置页会显示当前后端、是否可用及原因。++- **后端**+ - **Cua Driver(默认,三平台)**:随安装包附带官方预编译的 [cua-driver](https://github.com/trycua/cua/tree/main/libs/cua-driver) 0.37.0(MIT),构建时由 `scripts/fetch-cua-driver.mjs` 按平台/架构下载并校验 SHA256。作为内置 MCP 服务器 `cua-driver mcp` 运行,始终使用 `CUA_DRIVER_PERMISSION_MODE=bounded` + 能力清单,**从不**使用 `--dangerously-bypass-approvals`;子进程环境经过清理,不传任何密钥,关闭遥测,禁用 `run_script`。只开放所需的 24 个工具(读取窗口/截图、启动应用、点击、输入、按键、组合键、滚动、拖拽、设值、菜单等),其余工具(脚本、结束进程、剪贴板、浏览器注入、录制、更新等)对模型不可见且会被拒绝。清单列出本机已安装的图形应用(Linux 读 .desktop、macOS 读 /Applications、Windows 读开始菜单),并允许整屏输入;清单最长 24 小时有效,到期前自动重启驱动。+ - **Codex Computer Use 桥(可选,仅 macOS)**:kimi-computer-use,需要本机已安装 OpenAI Codex Computer Use;不可用时自动回退到 Cua Driver。+- **审批**:默认所有电脑操作(读取、截图、点击、输入、按键、滚动、拖拽、设值)**自动执行,无需审批**;可选「点击、输入等改变界面的操作先问我」(读取界面仍自动)。升级时已有机器人(含默认机器人)会被迁移为自动执行。权限预设为「只读」的机器人只开放读取类工具。+- **平台说明**+ - macOS:需在「系统设置 → 隐私与安全性」中给本应用授予「辅助功能」与「屏幕录制」权限;以 `--direct` 方式运行驱动(尚未在真机验证)。+ - Linux:X11 下通过 AT-SPI 读取界面(需要 at-spi2-core,应用需开启无障碍);Wayland 会降级——只能操作 XWayland 应用的前台窗口,界面会给出提示。没有图形会话(DISPLAY/WAYLAND_DISPLAY)时不可用。+ - Windows:必须在已登录用户的交互式桌面会话中运行(服务/计划任务的非交互会话不可用)。+- 截图只发给(支持视觉的)模型使用,不会被转发到微信/飞书聊天中;只有用户要求时才会作为文件发送。+- 未打包:cua-perception、OmniParser(AGPL)以及任何 Cua Spaces(FSL)组件。+## 飞书应用配置1. 打开 [飞书开放平台](https://open.feishu.cn/app) → 创建企业自建应用,添加「机器人」能力。diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.mdindex 4db5848..e98a549 100644--- a/THIRD_PARTY_NOTICES.md+++ b/THIRD_PARTY_NOTICES.md@@ -163,3 +163,38 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.```++## Cua Driver(跨平台电脑操作驱动 — MIT)++来源:https://github.com/trycua/cua (libs/cua-driver,发布版 cua-driver-rs-v0.37.0)++安装包按平台随附官方预编译的 `cua-driver` 可执行文件(未修改),以及其 LICENSE 与 THIRD_PARTY_NOTICES.md(位于安装目录 resources/cua-driver/)。构建脚本 scripts/fetch-cua-driver.mjs 固定版本并校验每个发布包的 SHA256。本项目的 dsh-runtime/plugins/amb-cua-mcp.mjs 只是在其 stdio MCP 前面做工具过滤与清单生成,未复制其代码。++**未随附**:cua-perception、OmniParser 等任何 AGPL 组件,以及 Cua Spaces 等 FSL 许可的内容。++```+MIT License++Copyright (c) 2025 Cua AI, Inc.++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.+```++Cua Driver 自身依赖的第三方组件声明见其随附的 THIRD_PARTY_NOTICES.md。+diff --git a/dsh-runtime/plugins/amb-computer-use-gate.mjs b/dsh-runtime/plugins/amb-computer-use-gate.mjsindex 9be5a2e..1d64cc3 100644--- a/dsh-runtime/plugins/amb-computer-use-gate.mjs+++ b/dsh-runtime/plugins/amb-computer-use-gate.mjs@@ -1,26 +1,27 @@-// anymodel bot: approval gate for the bundled Computer Use MCP tools (MIT, part of anymodel bot).+// anymodel bot: optional "ask before input actions" gate for Computer Use (MIT, part of anymodel bot).//-// The upstream bridge (songkeys/kimi-computer-use) auto-accepts the Computer Use service's own-// app-access prompts, so every desktop action is gated here instead: the call is turned into a-// normal DSH approval request, which amb-bridge routes to the chat (y/n code or card buttons).-// AMB_CU_APPROVAL=readonly-free lets read-only observation tools (list_apps, get_app_state) through.+// Computer Use actions run automatically by default (no approval). The host loads this plugin only+// when a bot sets computerUse.approval = "ask-input": then every Computer Use tool that is not a+// read-only observation (AMB_CU_READ_TOOLS, comma-separated raw tool names) becomes a normal DSH+// approval request, which amb-bridge routes to the chat. Works for both backends (Cua Driver and+// the Codex bridge); both are registered under the same MCP server name.export const name = 'amb-computer-use-gate'const PREFIX = 'mcp__computer-use__'-const READONLY = new Set(['list_apps', 'get_app_state'])export function apply(ctx) {- const relaxed = process.env.AMB_CU_APPROVAL === 'readonly-free'+ if (process.env.AMB_CU_APPROVAL !== 'ask-input') return+ const read = new Set(String(process.env.AMB_CU_READ_TOOLS ?? '').split(',').map((s) => s.trim()).filter(Boolean))ctx.on('tools/pre-execute', async (exec, next) => {const name = String(exec?.name ?? '')if (!name.startsWith(PREFIX)) return next()const downstream = await next()if (downstream?.kind !== 'allow') return downstream- if (relaxed && READONLY.has(name.slice(PREFIX.length))) return downstreamconst action = name.slice(PREFIX.length)+ if (read.has(action)) return downstreamreturn {kind: 'ask',- reason: `Computer Use action "${action}" controls this Mac and needs the user's approval`,+ reason: `Computer Use input action "${action}" needs the user's approval (this bot asks before input actions)`,displayReason: { en: `Computer Use: ${action}`, zh: `电脑操作:${action}` },}}, { prepend: true })diff --git a/dsh-runtime/plugins/amb-cua-mcp.mjs b/dsh-runtime/plugins/amb-cua-mcp.mjsnew file mode 100644index 0000000..0e19a65--- /dev/null+++ b/dsh-runtime/plugins/amb-cua-mcp.mjs@@ -0,0 +1,169 @@+#!/usr/bin/env node+// anymodel bot: stdio MCP front for the bundled Cua Driver (MIT, part of anymodel bot).+//+// DSH's mcp-client launches this script instead of `cua-driver mcp` directly. It:+// 1. asks the bundled binary which tools exist on this platform (`cua-driver list-tools`),+// 2. writes a bounded-mode capability manifest (v3) allowing only our reviewed tool set+// (intersected with what exists; unknown names would make the driver refuse to start),+// 3. runs `cua-driver mcp` with CUA_DRIVER_PERMISSION_MODE=bounded + that manifest,+// 4. hides every other tool from tools/list (the full list is ~230 KB of schemas) and+// refuses calls to them before they reach the driver,+// 5. restarts the driver transparently before the manifest's 24 h lifetime runs out.+// Never passes --dangerously-bypass-approvals; telemetry and update checks are off.+// Config comes from the JSON file named by AMB_CUA_CONFIG (written by the host):+// { bin, manifest, tools: string[], apps: [{executable}|{bundle_id}], display: boolean, direct?: boolean }+import { spawn, execFileSync } from 'node:child_process'+import fs from 'node:fs'+import readline from 'node:readline'+import { fileURLToPath } from 'node:url'++const LIFETIME_MS = 23 * 3600_000++const yq = (s) => JSON.stringify(String(s))++/** Bounded-mode capability manifest (YAML, version 3). */+export function manifestYaml({ tools, apps = [], display = false }) {+ const lines = ['# generated by anymodel bot; do not edit', 'version: 3', 'expires_after: 24h', 'idle_timeout: 24h', 'allow:', ' tools:']+ for (const t of tools) lines.push(` - ${yq(t)}`)+ lines.push('resources:')+ if (apps.length) {+ lines.push(' apps:')+ for (const a of apps) {+ lines.push(a.bundle_id ? ` - bundle_id: ${yq(a.bundle_id)}` : ` - executable: ${yq(a.executable)}`)+ lines.push(' launch: true', ' windows: all', ' terminate: driver_launched')+ }+ }+ lines.push(' desktop:', ` display: ${display ? 'true' : 'false'}`)+ return lines.join('\n') + '\n'+}++/** Parse `cua-driver list-tools` output ("name: description" per line). */+export function parseToolList(text) {+ const out = new Set()+ for (const line of String(text).split(/\r?\n/)) {+ const m = /^([a-z][a-z0-9_]*):\s/.exec(line)+ if (m) out.add(m[1])+ }+ return out+}++/** Environment for the driver: what we were given, minus our own config, plus the hard settings. */+export function childEnv(base, cfg) {+ const env = { ...base }+ for (const k of Object.keys(env)) {+ if (k.startsWith('AMB_') || k === 'ELECTRON_RUN_AS_NODE' || /KEY|PASSWORD|SECRET|TOKEN/i.test(k)) delete env[k]+ }+ delete env.CUA_DRIVER_DANGEROUSLY_BYPASS_APPROVALS+ Object.assign(env, {+ CUA_DRIVER_PERMISSION_MODE: 'bounded',+ CUA_DRIVER_CAPABILITY_MANIFEST_FILE: cfg.manifest,+ CUA_DRIVER_CAPABILITY_MANIFEST_APPROVED: '1',+ CUA_DRIVER_DISABLE_RUN_SCRIPT: '1',+ DO_NOT_TRACK: '1',+ CUA_TELEMETRY_ENABLED: '0',+ CUA_DRIVER_RS_TELEMETRY_ENABLED: '0',+ })+ return env+}++/** Line filter between the MCP client and the driver. Pure (no I/O) so it can be unit-tested. */+export function createFilter(allowed) {+ const listIds = new Set()+ return {+ /** client → driver: returns { forward?: msg, reply?: msg } */+ fromClient(msg) {+ if (msg && msg.method === 'tools/list' && msg.id !== undefined) listIds.add(msg.id)+ if (msg && msg.method === 'tools/call' && msg.id !== undefined) {+ const name = String(msg.params?.name ?? '')+ if (!allowed.has(name)) {+ return { reply: { jsonrpc: '2.0', id: msg.id, result: { isError: true, content: [{ type: 'text', text: `Tool '${name}' is not enabled in anymodel bot's Computer Use (Cua Driver).` }] } } }+ }+ }+ return { forward: msg }+ },+ /** driver → client */+ fromDriver(msg) {+ if (msg && msg.id !== undefined && listIds.has(msg.id) && msg.result && Array.isArray(msg.result.tools)) {+ listIds.delete(msg.id)+ return { ...msg, result: { ...msg.result, tools: msg.result.tools.filter((t) => allowed.has(t.name)) } }+ }+ return msg+ },+ }+}++function main() {+ const cfg = JSON.parse(fs.readFileSync(process.env.AMB_CUA_CONFIG, 'utf8'))+ const env = childEnv(process.env, cfg)+ let available+ try {+ available = parseToolList(execFileSync(cfg.bin, ['list-tools'], { env, encoding: 'utf8', timeout: 30_000, stdio: ['ignore', 'pipe', 'ignore'] }))+ } catch (e) {+ process.stderr.write(`amb-cua-mcp: cua-driver list-tools failed: ${e?.message ?? e}\n`)+ process.exit(1)+ }+ const tools = cfg.tools.filter((t) => available.has(t))+ const allowed = new Set(tools)+ fs.writeFileSync(cfg.manifest, manifestYaml({ tools, apps: cfg.apps ?? [], display: !!cfg.display }), { mode: 0o600 })+ const filter = createFilter(allowed)+ const args = ['mcp', ...(cfg.direct ? ['--direct'] : [])]++ let child+ let startedAt = 0+ let initMsg+ let initializedMsg+ const pending = new Set()+ const send = (msg) => process.stdout.write(JSON.stringify(msg) + '\n')++ const start = (replay) => {+ startedAt = Date.now()+ const c = spawn(cfg.bin, args, { env, stdio: ['pipe', 'pipe', 'inherit'] })+ child = c+ const rl = readline.createInterface({ input: c.stdout })+ rl.on('line', (line) => {+ if (!line.trim()) return+ let msg+ try { msg = JSON.parse(line) } catch { return }+ if (typeof msg.id === 'string' && msg.id.startsWith('amb-reinit-')) return+ if (msg.id !== undefined && (msg.result !== undefined || msg.error !== undefined)) pending.delete(msg.id)+ send(filter.fromDriver(msg))+ })+ c.on('exit', (code) => {+ if (child !== c) return+ child = undefined+ for (const id of pending) send({ jsonrpc: '2.0', id, error: { code: -32603, message: `cua-driver exited (${code})` } })+ pending.clear()+ })+ if (replay && initMsg) {+ c.stdin.write(JSON.stringify({ ...initMsg, id: `amb-reinit-${startedAt}` }) + '\n')+ if (initializedMsg) c.stdin.write(JSON.stringify(initializedMsg) + '\n')+ }+ }+ start(false)++ const rl = readline.createInterface({ input: process.stdin })+ rl.on('line', (line) => {+ if (!line.trim()) return+ let msg+ try { msg = JSON.parse(line) } catch { return }+ if (msg.method === 'initialize') initMsg = msg+ if (msg.method === 'notifications/initialized') initializedMsg = msg+ const r = filter.fromClient(msg)+ if (r.reply) return send(r.reply)+ // The manifest lives at most 24 h: start a fresh driver (same manifest) before it expires.+ if (child && msg.method === 'tools/call' && Date.now() - startedAt > LIFETIME_MS) {+ const old = child+ child = undefined+ for (const id of pending) send({ jsonrpc: '2.0', id, error: { code: -32603, message: 'cua-driver restarted' } })+ pending.clear()+ old.kill()+ }+ if (!child) start(msg.method !== 'initialize')+ if (msg.id !== undefined && msg.method) pending.add(msg.id)+ child.stdin.write(JSON.stringify(r.forward) + '\n')+ })+ rl.on('close', () => { child?.kill(); process.exit(0) })+ for (const s of ['SIGTERM', 'SIGINT']) process.on(s, () => { child?.kill(); process.exit(0) })+}++if (process.argv[1] && fileURLToPath(import.meta.url) === fs.realpathSync(process.argv[1])) main()diff --git a/package.json b/package.jsonindex b3865f7..b77843d 100644--- a/package.json+++ b/package.json@@ -21,12 +21,13 @@"typecheck": "tsc -p tsconfig.json --noEmit","test": "vitest run","electron:dev": "npm run build && electron .",- "dist": "npm run build && node scripts/prepare-dsh.mjs && node scripts/build-magpie.mjs --target current && electron-builder",+ "dist": "npm run build && node scripts/prepare-dsh.mjs && node scripts/build-magpie.mjs --target current && node scripts/fetch-cua-driver.mjs --target current && electron-builder","electron:smoke": "npm run build && node scripts/electron-smoke.mjs",- "dist:linux": "npm run build && node scripts/prepare-dsh.mjs && node scripts/build-magpie.mjs --target linux/amd64,linux/arm64 && electron-builder --linux",- "dist:mac": "npm run build && node scripts/prepare-dsh.mjs && node scripts/build-magpie.mjs --target darwin/amd64,darwin/arm64 && electron-builder --mac",- "dist:win": "npm run build && node scripts/prepare-dsh.mjs && node scripts/build-magpie.mjs --target windows/amd64,windows/arm64 && electron-builder --win",- "prepare:dsh": "node scripts/prepare-dsh.mjs"+ "dist:linux": "npm run build && node scripts/prepare-dsh.mjs && node scripts/build-magpie.mjs --target linux/amd64,linux/arm64 && node scripts/fetch-cua-driver.mjs --target linux/x64,linux/arm64 && electron-builder --linux",+ "dist:mac": "npm run build && node scripts/prepare-dsh.mjs && node scripts/build-magpie.mjs --target darwin/amd64,darwin/arm64 && node scripts/fetch-cua-driver.mjs --target darwin/x64,darwin/arm64 && electron-builder --mac",+ "dist:win": "npm run build && node scripts/prepare-dsh.mjs && node scripts/build-magpie.mjs --target windows/amd64,windows/arm64 && node scripts/fetch-cua-driver.mjs --target win32/x64,win32/arm64 && electron-builder --win",+ "prepare:dsh": "node scripts/prepare-dsh.mjs",+ "fetch:cua": "node scripts/fetch-cua-driver.mjs"},"dependencies": {"@larksuiteoapi/node-sdk": "1.76.0",@@ -120,6 +121,16 @@"NOTICE","README.md"]+ },+ {+ "from": "build/cua-driver/darwin-${arch}",+ "to": "cua-driver",+ "filter": [+ "cua-driver",+ "cua-driver.exe",+ "LICENSE",+ "THIRD_PARTY_NOTICES.md"+ ]}]},@@ -155,6 +166,16 @@"!**/*.map","!**/*.d.ts"]+ },+ {+ "from": "build/cua-driver/win32-${arch}",+ "to": "cua-driver",+ "filter": [+ "cua-driver",+ "cua-driver.exe",+ "LICENSE",+ "THIRD_PARTY_NOTICES.md"+ ]}]},@@ -200,6 +221,16 @@"!**/*.map","!**/*.d.ts"]+ },+ {+ "from": "build/cua-driver/linux-${arch}",+ "to": "cua-driver",+ "filter": [+ "cua-driver",+ "cua-driver.exe",+ "LICENSE",+ "THIRD_PARTY_NOTICES.md"+ ]}],"desktop": {diff --git a/scripts/fetch-cua-driver.mjs b/scripts/fetch-cua-driver.mjsnew file mode 100644index 0000000..eef0600--- /dev/null+++ b/scripts/fetch-cua-driver.mjs@@ -0,0 +1,113 @@+#!/usr/bin/env node+// 下载并校验官方预编译的 Cua Driver(trycua/cua,MIT),只解出 cua-driver(.exe)、LICENSE、THIRD_PARTY_NOTICES.md+// 到 build/cua-driver/<os>-<electron arch>/,供 electron-builder extraResources 打包。+// 不下载/不打包 cua-perception(含 AGPL 的 OmniParser)或 Cua Spaces(FSL)的任何内容。+//+// 用法:node scripts/fetch-cua-driver.mjs [--target current|linux/x64,linux/arm64,darwin/x64,darwin/arm64,win32/x64,win32/arm64]+import { execFileSync } from "node:child_process";+import crypto from "node:crypto";+import fs from "node:fs";+import os from "node:os";+import path from "node:path";+import { fileURLToPath } from "node:url";++export const CUA_VERSION = "0.37.0";+const BASE = `https://github.com/trycua/cua/releases/download/cua-driver-rs-v${CUA_VERSION}/`;+// 官方发布页的 SHA256(固定版本,校验失败即中止构建)+export const ASSETS = {+ "darwin-arm64": { file: `cua-driver-rs-${CUA_VERSION}-darwin-arm64.tar.gz`, sha256: "1e9b8c58cc830e080ada881ec76286529f5f1264fe9ecc371b0ae95cff98928b" },+ "darwin-x64": { file: `cua-driver-rs-${CUA_VERSION}-darwin-x86_64.tar.gz`, sha256: "2f9f0d652b2a571d210a3280028774cd640f805284868e8beef160a83fc07bfd" },+ "linux-arm64": { file: `cua-driver-rs-${CUA_VERSION}-linux-arm64.tar.gz`, sha256: "b20393b9b0c317daa05934cd6ee131b4250e34abb5967c596494634f3114fa89" },+ "linux-x64": { file: `cua-driver-rs-${CUA_VERSION}-linux-x86_64.tar.gz`, sha256: "d9421e143df6da3c695f5007f4db25fa619473d62965927e880c1dbfeeab1bf2" },+ "win32-arm64": { file: `cua-driver-rs-${CUA_VERSION}-windows-arm64.zip`, sha256: "612818fb1ebea702b5934c1d982a815375f25ac0e1b1389249c9f20f21354b70" },+ "win32-x64": { file: `cua-driver-rs-${CUA_VERSION}-windows-x86_64.zip`, sha256: "109ab57e5c0a6e13a73e2ce2daf01b338027366eb1a20c6e0b413a10610bc8e2" },+};+const KEEP = ["cua-driver", "cua-driver.exe", "LICENSE", "THIRD_PARTY_NOTICES.md"];++const root = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");+const outRoot = path.join(root, "build", "cua-driver");+const cacheDir = path.join(outRoot, ".cache");++function sha256(file) {+ return crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex");+}++async function download(asset) {+ fs.mkdirSync(cacheDir, { recursive: true });+ const dest = path.join(cacheDir, asset.file);+ // 本地已有(缓存或 AMB_CUA_CACHE 目录)且哈希一致就复用+ for (const cand of [dest, process.env.AMB_CUA_CACHE && path.join(process.env.AMB_CUA_CACHE, asset.file)].filter(Boolean)) {+ if (fs.existsSync(cand) && sha256(cand) === asset.sha256) {+ if (cand !== dest) fs.copyFileSync(cand, dest);+ return dest;+ }+ }+ const res = await fetch(BASE + asset.file, { redirect: "follow" });+ if (!res.ok) throw new Error(`下载失败 ${asset.file}: HTTP ${res.status}`);+ const buf = Buffer.from(await res.arrayBuffer());+ const got = crypto.createHash("sha256").update(buf).digest("hex");+ if (got !== asset.sha256) throw new Error(`SHA256 不匹配 ${asset.file}: 期望 ${asset.sha256},实际 ${got}`);+ fs.writeFileSync(dest, buf);+ return dest;+}++function extract(archive, into) {+ if (archive.endsWith(".zip")) {+ try {+ execFileSync("unzip", ["-q", "-o", archive, "-d", into], { stdio: "inherit" });+ } catch {+ execFileSync("tar", ["-xf", archive, "-C", into], { stdio: "inherit" }); // Windows 自带 bsdtar 也能解 zip+ }+ } else execFileSync("tar", ["-xzf", archive, "-C", into], { stdio: "inherit" });+}++function findFiles(dir, names, found = new Map()) {+ for (const e of fs.readdirSync(dir, { withFileTypes: true })) {+ const p = path.join(dir, e.name);+ if (e.isDirectory()) findFiles(p, names, found);+ else if (names.includes(e.name) && !found.has(e.name)) found.set(e.name, p);+ }+ return found;+}++export async function fetchOne(key) {+ const asset = ASSETS[key];+ if (!asset) throw new Error(`不支持的平台 ${key}`);+ const archive = await download(asset);+ if (sha256(archive) !== asset.sha256) throw new Error(`SHA256 不匹配 ${asset.file}`);+ const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "cua-x-"));+ try {+ extract(archive, tmp);+ const found = findFiles(tmp, KEEP);+ const bin = found.get(key.startsWith("win32") ? "cua-driver.exe" : "cua-driver");+ if (!bin) throw new Error(`${asset.file} 里没有 cua-driver 可执行文件`);+ const out = path.join(outRoot, key);+ fs.rmSync(out, { recursive: true, force: true });+ fs.mkdirSync(out, { recursive: true });+ for (const [name, p] of found) {+ if (name.startsWith("cua-driver") && p !== bin) continue;+ fs.copyFileSync(p, path.join(out, name));+ }+ if (!key.startsWith("win32")) fs.chmodSync(path.join(out, "cua-driver"), 0o755);+ console.log(`cua-driver ${CUA_VERSION} ${key} → ${path.relative(root, out)}(SHA256 已校验)`);+ return out;+ } finally {+ fs.rmSync(tmp, { recursive: true, force: true });+ }+}++function targets(arg) {+ if (!arg || arg === "current") return [`${process.platform}-${process.arch}`];+ return arg.split(",").map((t) => t.trim().replace("/", "-").replace("windows", "win32").replace("amd64", "x64").replace("x86_64", "x64"));+}++if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {+ const i = process.argv.indexOf("--target");+ const list = targets(i > 0 ? process.argv[i + 1] : undefined);+ try {+ for (const k of list) await fetchOne(k);+ } catch (e) {+ console.error(String(e?.message ?? e));+ process.exit(1);+ }+}diff --git a/src/agent/computer-use.ts b/src/agent/computer-use.tsindex 67ee2d7..e63f972 100644--- a/src/agent/computer-use.ts+++ b/src/agent/computer-use.ts@@ -1,17 +1,21 @@import fs from "node:fs";import os from "node:os";import path from "node:path";+import { cuaRoots, detectCua, CUA_READ_TOOLS, type CuaStatus } from "./cua-driver.js";+import type { CuBackend } from "./settings.js";/**- * 电脑操作(Computer Use):随附 songkeys/kimi-computer-use(MIT)的 stdio MCP 桥,- * 它把本机已安装的 OpenAI「Codex Computer Use」服务暴露成 MCP 工具。仅 macOS 14.4+ 可用,- * 且需要用户自己安装 Codex Computer Use;否则整项功能关闭并在界面给出原因。+ * 电脑操作(Computer Use)有两个后端:+ * - cua(默认,三个平台):随附的 Cua Driver,见 cua-driver.ts;+ * - codex(仅 macOS,可选):随附 songkeys/kimi-computer-use(MIT)的 stdio MCP 桥,+ * 它把本机已安装的 OpenAI「Codex Computer Use」服务暴露成 MCP 工具,需 macOS 14.4+ 且用户自己装好 Codex Computer Use。+ * 两个后端都注册为同名 MCP 服务器 computer-use;默认所有操作自动执行,不需审批。*/export const CU_SERVER = "computer-use";export const CU_TOOL_PREFIX = `mcp__${CU_SERVER}__`;-/** 只读取界面、不改变任何东西的工具(设置为 readonly-free 时免审批) */+/** Codex 桥里只读取界面、不改变任何东西的工具(ask-input 模式下也免审批) */export const CU_READONLY_TOOLS = ["list_apps", "get_app_state"];-export const CU_UNAVAILABLE = "Computer Use: 需要 macOS 14.4+ 和已安装的 Codex Computer Use";+export const CU_UNAVAILABLE = "Codex Computer Use 桥: 需要 macOS 14.4+ 和已安装的 Codex Computer Use";export interface CuProbe {platform: NodeJS.Platform;@@ -109,7 +113,7 @@ export function detectComputerUse(roots: string[], probe: CuProbe = defaultProbeif (!direct && !launcher) return off("未找到 ChatGPT/Codex 应用自带的 codex 启动器");return {available: true,- reason: "Computer Use: 可用(操作 macOS 应用;默认每次操作需审批)",+ reason: "Codex Computer Use 桥: 可用(macOS)",client,launcher,codexHome,@@ -123,7 +127,7 @@ export function detectComputerUse(roots: string[], probe: CuProbe = defaultProbeexport function adaptSkill(md: string): string {const body = md.replaceAll("mcp__plugin-kimi-computer-use_computer-use__", CU_TOOL_PREFIX).replace(/The bridge automatically handles the service's app-access elicitation\.[^\n]*\n[^\n]*\n[^\n]*\n/,- "Each action may need the user's approval in chat before it runs; wait for it. Do not try to manage the OpenAI app's allowlist.\n",+ "Actions run automatically. If this bot is set to ask before input actions, a call may wait for the user's approval in chat; wait for it. Do not try to manage the OpenAI app's allowlist.\n",);const end = body.startsWith("---") ? body.indexOf("\n---", 3) : -1;const note = "\n<!-- Adapted from songkeys/kimi-computer-use skills/computer-use/SKILL.md (MIT, Copyright (c) 2026 Songkeys). -->\n";@@ -151,3 +155,30 @@ export function mcpPatchLines(st: CuStatus, nodePath: string, yamlStr: (s: strin" failOnStartupError: false",];}++export interface CuResolved {+ /** 实际使用的后端(Codex 桥不可用时回退到 Cua Driver) */+ backend: CuBackend;+ available: boolean;+ reason: string;+ notice?: string;+ /** 本机可选的后端 */+ backends: Array<{ id: CuBackend; available: boolean; reason: string }>;+ cua: CuaStatus;+ codex: CuStatus;+ /** ask-input 模式下免审批的只读工具 */+ readTools: string[];+}++/** 按机器人设置的后端解析本机的电脑操作能力。 */+export function resolveComputerUse(want: CuBackend, runtimeDir: string): CuResolved {+ const cua = detectCua(cuaRoots(runtimeDir));+ const codex = detectComputerUse(bundledRoots(runtimeDir));+ const backends: CuResolved["backends"] = [{ id: "cua", available: cua.available, reason: cua.reason }];+ if (process.platform === "darwin") backends.push({ id: "codex", available: codex.available, reason: codex.reason });+ if (want === "codex" && codex.available) {+ return { backend: "codex", available: true, reason: codex.reason, backends, cua, codex, readTools: CU_READONLY_TOOLS };+ }+ const fallback = want === "codex" ? `(Codex 桥不可用,已改用 Cua Driver:${codex.reason})` : "";+ return { backend: "cua", available: cua.available, reason: cua.reason + fallback, notice: cua.notice, backends, cua, codex, readTools: CUA_READ_TOOLS };+}diff --git a/src/agent/cua-driver.ts b/src/agent/cua-driver.tsnew file mode 100644index 0000000..71fc745--- /dev/null+++ b/src/agent/cua-driver.ts@@ -0,0 +1,306 @@+import { execFileSync } from "node:child_process";+import fs from "node:fs";+import os from "node:os";+import path from "node:path";++/**+ * Cua Driver(trycua/cua libs/cua-driver,MIT):默认的电脑操作后端,三个平台都用官方预编译的+ * cua-driver 二进制(构建时按固定版本下载并校验 SHA256,见 scripts/fetch-cua-driver.mjs)。+ * 以 `cua-driver mcp` 作为 DSH 内置 MCP 服务器运行,始终是 bounded 模式 + 我们生成的能力清单;+ * 中间隔一层 dsh-runtime/plugins/amb-cua-mcp.mjs,只暴露下面审核过的工具。+ * 不随附 cua-perception / OmniParser(AGPL)或 Cua Spaces(FSL)的任何东西。+ */+export const CUA_VERSION = "0.37.0";++/** 只读取界面(含截图)的工具 */+export const CUA_READ_TOOLS = [+ "list_apps",+ "list_windows",+ "get_window_state",+ "get_desktop_state",+ "get_screen_size",+ "get_cursor_position",+ "zoom",+ "verify_state",+ "check_permissions",+];+/** 会改变界面的输入工具(ask-input 模式下逐个审批) */+export const CUA_INPUT_TOOLS = [+ "launch_app",+ "bring_to_front",+ "click",+ "double_click",+ "right_click",+ "type_text",+ "press_key",+ "hotkey",+ "scroll",+ "drag",+ "set_value",+ "invoke_menu",+ "move_cursor",+ "set_window_frame",+ "run_steps",+];+/**+ * 明确不开放:browser_*(另需独立运行时)、剪贴板、kill_app、run_script、录制/回放、+ * parse_visual_regions(感知/视觉模型)、安装扩展/ffmpeg、检查更新、读写配置、会话提权等。+ */+export const CUA_TOOLS = [...CUA_READ_TOOLS, ...CUA_INPUT_TOOLS];++export interface CuaProbe {+ platform: NodeJS.Platform;+ arch: string;+ env: Record<string, string | undefined>;+ homedir: string;+ isFile(p: string): boolean;+}++export interface CuaStatus {+ available: boolean;+ /** 给界面与 /status 的说明(中文) */+ reason: string;+ /** 平台提示(权限、Wayland 降级等) */+ notice?: string;+ bin?: string;+ /** macOS:用 --direct 让授权归属到本应用(未在真机验证) */+ direct?: boolean;+}++export function defaultCuaProbe(): CuaProbe {+ return {+ platform: process.platform,+ arch: process.arch,+ env: process.env,+ homedir: os.homedir(),+ isFile: (p) => {+ try {+ return fs.statSync(p).isFile();+ } catch {+ return false;+ }+ },+ };+}++/** electron-builder 的 ${os}-${arch} 目录名,例如 linux-x64、darwin-arm64、win32-x64 */+export function platKey(platform: string, arch: string): string {+ return `${platform}-${arch}`;+}++/** 二进制所在目录候选:打包后是 resources/cua-driver,开发时是 build/cua-driver/<plat-arch> */+export function cuaRoots(runtimeDir: string, platform = process.platform, arch = process.arch): string[] {+ const up = path.dirname(path.resolve(runtimeDir));+ const res = (process as any).resourcesPath as string | undefined;+ return [+ process.env.AMB_CUA_DRIVER_DIR,+ res ? path.join(res, "cua-driver") : undefined,+ path.join(up, "cua-driver"),+ path.join(up, "build", "cua-driver", platKey(platform, arch)),+ ].filter((x): x is string => !!x);+}++const LINUX_ATSPI = "需要 AT-SPI(at-spi2-core)才能读取窗口元素";++export function detectCua(roots: string[], probe: CuaProbe = defaultCuaProbe()): CuaStatus {+ const off = (why: string): CuaStatus => ({ available: false, reason: `Cua Driver: 不可用(${why})` });+ if (!["linux", "darwin", "win32"].includes(probe.platform)) return off(`不支持 ${probe.platform}`);+ const exe = probe.platform === "win32" ? "cua-driver.exe" : "cua-driver";+ const root = roots.find((r) => probe.isFile(path.join(r, exe)));+ if (!root) return off(`安装包缺少 ${probe.platform}-${probe.arch} 的 cua-driver ${CUA_VERSION}`);+ const bin = path.join(root, exe);+ const env = probe.env;+ if (probe.platform === "linux") {+ const x11 = !!env.DISPLAY;+ const wayland = !!env.WAYLAND_DISPLAY || env.XDG_SESSION_TYPE === "wayland";+ if (!x11 && !wayland) return off("没有图形会话:DISPLAY / WAYLAND_DISPLAY 均未设置");+ const notice = wayland+ ? `Wayland 会话:功能降级——窗口读取依赖 AT-SPI,键鼠输入走 XWayland,只能操作前台窗口;${LINUX_ATSPI}`+ : `X11 会话;${LINUX_ATSPI}`;+ return { available: true, reason: "Cua Driver: 可用(Linux)", notice, bin };+ }+ if (probe.platform === "darwin") {+ return {+ available: true,+ reason: "Cua Driver: 可用(macOS)",+ notice:+ "首次使用需在「系统设置 → 隐私与安全性」给本应用打开「辅助功能」和「屏幕录制」。以 --direct 模式运行,授权应归属本应用(尚未在真机验证)。",+ bin,+ direct: true,+ };+ }+ // Windows:服务 / 会话 0 里没有可操作的桌面+ if (!env.SESSIONNAME) return off("需要在已登录用户的交互式会话中运行(检测到服务或非交互会话)");+ return { available: true, reason: "Cua Driver: 可用(Windows)", notice: "需在已登录用户的交互式桌面会话中运行;无法操作以管理员权限运行的窗口(除非本应用也以管理员运行)。", bin };+}++export type CuaApp = { executable: string } | { bundle_id: string };++/** Linux:从 .desktop 文件的 Exec= 解析可执行文件的规范绝对路径(清单只接受这种身份)。 */+export function linuxDesktopApps(env: Record<string, string | undefined>, homedir: string, fsx: Pick<typeof fs, "readdirSync" | "readFileSync" | "realpathSync" | "existsSync"> = fs): CuaApp[] {+ const dataDirs = [path.join(env.XDG_DATA_HOME || path.join(homedir, ".local", "share")), ...(env.XDG_DATA_DIRS || "/usr/local/share:/usr/share").split(":")];+ const pathDirs = (env.PATH || "/usr/local/bin:/usr/bin:/bin").split(":").filter(Boolean);+ const out = new Set<string>();+ const resolve = (cmd: string): string | undefined => {+ const cand = cmd.includes("/") ? [cmd] : pathDirs.map((d) => path.join(d, cmd));+ for (const c of cand) {+ try {+ if (fsx.existsSync(c)) return fsx.realpathSync(c);+ } catch {+ /* 下一个 */+ }+ }+ return undefined;+ };+ for (const d of dataDirs) {+ const dir = path.join(d, "applications");+ let names: string[] = [];+ try {+ names = fsx.readdirSync(dir) as unknown as string[];+ } catch {+ continue;+ }+ for (const n of names) {+ if (!n.endsWith(".desktop")) continue;+ let text = "";+ try {+ text = String(fsx.readFileSync(path.join(dir, n), "utf8"));+ } catch {+ continue;+ }+ const entry = text.split(/\n\[/)[0];+ if (/^NoDisplay=true/m.test(entry) || /^Hidden=true/m.test(entry)) continue;+ const exec = /^Exec=(.*)$/m.exec(entry)?.[1];+ if (!exec) continue;+ const toks = exec.trim().replace(/^"([^"]+)"/, "$1").split(/\s+/);+ let i = 0;+ if (toks[0] === "env" || toks[0]?.endsWith("/env")) {+ i = 1;+ while (toks[i] && /^[A-Za-z_][A-Za-z0-9_]*=/.test(toks[i])) i++;+ }+ const cmd = toks[i];+ if (!cmd || /^(flatpak|snap|sh|bash)$/.test(path.basename(cmd))) continue;+ const real = resolve(cmd);+ if (real) out.add(real);+ }+ }+ return [...out].sort().map((executable) => ({ executable }));+}++/** macOS:/Applications 等目录下 .app 的 CFBundleIdentifier(未在真机验证)。 */+export function macApps(homedir: string): CuaApp[] {+ const dirs = ["/Applications", "/Applications/Utilities", "/System/Applications", "/System/Applications/Utilities", path.join(homedir, "Applications")];+ const out = new Set<string>();+ for (const d of dirs) {+ let names: string[] = [];+ try {+ names = fs.readdirSync(d);+ } catch {+ continue;+ }+ for (const n of names) {+ if (!n.endsWith(".app")) continue;+ const plist = path.join(d, n, "Contents", "Info.plist");+ let id = "";+ try {+ const raw = fs.readFileSync(plist);+ if (raw.subarray(0, 6).toString() === "bplist") {+ id = execFileSync("/usr/bin/plutil", ["-extract", "CFBundleIdentifier", "raw", "-o", "-", plist], { encoding: "utf8", timeout: 5000 }).trim();+ } else {+ id = /<key>CFBundleIdentifier<\/key>\s*<string>([^<]+)<\/string>/.exec(raw.toString("utf8"))?.[1]?.trim() ?? "";+ }+ } catch {+ continue;+ }+ if (id) out.add(id);+ }+ }+ return [...out].sort().map((bundle_id) => ({ bundle_id }));+}++/** Windows:开始菜单快捷方式指向的 .exe + 几个系统自带程序(未在真机验证)。 */+export function windowsApps(env: Record<string, string | undefined>): CuaApp[] {+ const out = new Set<string>();+ const win = env.SystemRoot || env.WINDIR || "C:\\Windows";+ for (const p of ["notepad.exe", "mspaint.exe", "write.exe", "explorer.exe"].map((x) => (x === "explorer.exe" ? path.win32.join(win, x) : path.win32.join(win, "System32", x)))) {+ if (fs.existsSync(p)) out.add(p);+ }+ try {+ const ps =+ "$s=New-Object -ComObject WScript.Shell;" ++ "$d=@([Environment]::GetFolderPath('CommonStartMenu'),[Environment]::GetFolderPath('StartMenu'));" ++ "Get-ChildItem -Path $d -Recurse -Filter *.lnk -ErrorAction SilentlyContinue|ForEach-Object{try{$s.CreateShortcut($_.FullName).TargetPath}catch{}}";+ const text = execFileSync("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", ps], { encoding: "utf8", timeout: 20000, windowsHide: true });+ for (const line of text.split(/\r?\n/)) {+ const p = line.trim();+ if (/\.exe$/i.test(p) && fs.existsSync(p)) out.add(p);+ }+ } catch {+ /* PowerShell 不可用时只用系统程序 */+ }+ return [...out].sort().map((executable) => ({ executable }));+}++export function discoverApps(platform: NodeJS.Platform = process.platform, env = process.env, homedir = os.homedir()): CuaApp[] {+ const self = process.execPath;+ const apps = platform === "linux" ? linuxDesktopApps(env, homedir) : platform === "darwin" ? macApps(homedir) : platform === "win32" ? windowsApps(env) : [];+ return apps.filter((a) => !("executable" in a) || a.executable !== self).slice(0, 800);+}++export interface CuaConfig {+ bin: string;+ manifest: string;+ tools: string[];+ apps: CuaApp[];+ display: boolean;+ direct: boolean;+}++/** 给 amb-cua-mcp.mjs 的配置。readonly 预设下只开放读取工具。 */+export function cuaConfig(st: CuaStatus, dir: string, opts: { readonly: boolean; apps: CuaApp[] }): CuaConfig {+ return {+ bin: st.bin!,+ manifest: path.join(dir, "manifest.yaml"),+ tools: opts.readonly ? [...CUA_READ_TOOLS] : [...CUA_TOOLS],+ apps: opts.apps,+ // 允许整屏截图与屏幕坐标输入(list_windows 也需要)+ display: true,+ direct: !!st.direct,+ };+}++/** 图形会话相关的环境变量(DSH 子进程的环境里没有这些,需要显式传给 MCP 子进程)。不含任何密钥。 */+export const CUA_PASS_ENV = [+ "DISPLAY",+ "WAYLAND_DISPLAY",+ "XDG_RUNTIME_DIR",+ "XDG_SESSION_TYPE",+ "XDG_CURRENT_DESKTOP",+ "DBUS_SESSION_BUS_ADDRESS",+ "XAUTHORITY",+ "SESSIONNAME",+ "SystemRoot",+ "WINDIR",+ "USERPROFILE",+ "LOCALAPPDATA",+ "APPDATA",+ "TMPDIR",+ "TEMP",+ "TMP",+];++export function cuaMcpEnv(parent: Record<string, string | undefined>, home: string, configFile: string): Record<string, string> {+ const env: Record<string, string> = {};+ for (const k of CUA_PASS_ENV) {+ const v = parent[k];+ if (v && !/KEY|PASSWORD|SECRET|TOKEN/i.test(k)) env[k] = v;+ }+ // 真实用户主目录:cua-driver 启动的应用会继承它(DSH 子进程的 HOME 是机器人自己的目录)+ env.HOME = home;+ env.AMB_CUA_CONFIG = configFile;+ env.DO_NOT_TRACK = "1";+ env.CUA_TELEMETRY_ENABLED = "0";+ env.CUA_DRIVER_RS_TELEMETRY_ENABLED = "0";+ if (process.versions.electron) env.ELECTRON_RUN_AS_NODE = "1";+ return env;+}diff --git a/src/agent/host.ts b/src/agent/host.tsindex 328a9b9..bd4d051 100644--- a/src/agent/host.ts+++ b/src/agent/host.ts@@ -5,7 +5,9 @@ import { pathToFileURL } from "node:url";import { logger } from "../log.js";import type { Store } from "../store/db.js";import type { AgentBridge } from "./bridge.js";-import { adaptSkill, bundledRoots, detectComputerUse, mcpPatchLines, type CuStatus } from "./computer-use.js";+import os from "node:os";+import { adaptSkill, mcpPatchLines, resolveComputerUse, type CuResolved } from "./computer-use.js";+import { cuaConfig, cuaMcpEnv, discoverApps } from "./cua-driver.js";import { DEFAULT_SETTINGS, permissionMode, type BotSettings } from "./settings.js";const log = logger("agent.host");@@ -242,37 +244,69 @@ export class AgentHost {return f;}- /** 当前机器上 Computer Use 的可用性(不看本机器人的开关)。 */- computerUseStatus(): CuStatus {- return detectComputerUse(bundledRoots(this.o.runtimeDir));+ /** 当前机器上本机器人所选后端的可用性(不看本机器人的开关)。 */+ computerUseStatus(): CuResolved {+ return resolveComputerUse(this.settings().computerUse.backend, this.o.runtimeDir);}/** 本机器人实际是否启用 Computer Use:系统支持且设置里开着。 */- private computerUseActive(): CuStatus | undefined {+ private computerUseActive(): CuResolved | undefined {if (!this.settings().computerUse.enabled) return undefined;const st = this.computerUseStatus();return st.available ? st : undefined;}+ private cuReadTools = "";/**- * Computer Use:插入 mcp-client(随附的 kimi-computer-use 桥,用宿主自己的 node/Electron 运行)- * 与审批闸门;把改写过工具名的 SKILL.md 放进 DSH 主目录的用户技能目录。不可用时删掉这份技能。+ * Computer Use:插入 mcp-client(Cua Driver 经 amb-cua-mcp.mjs,或 macOS 上的 Codex 桥;都用宿主自己的+ * node/Electron 运行),并写入对应的 SKILL.md。默认所有操作自动执行;只有设置为 ask-input 时才加载审批闸门。*/private computerUseLines(): string[] {const skillDir = path.join(this.home, "skills", "computer-use");const st = this.computerUseActive();+ this.cuReadTools = st ? st.readTools.join(",") : "";if (!st) {fs.rmSync(skillDir, { recursive: true, force: true });return [];}+ let lines: string[];try {fs.mkdirSync(skillDir, { recursive: true });- fs.writeFileSync(path.join(skillDir, "SKILL.md"), adaptSkill(fs.readFileSync(st.skill!, "utf8")));+ if (st.backend === "codex") {+ fs.writeFileSync(path.join(skillDir, "SKILL.md"), adaptSkill(fs.readFileSync(st.codex.skill!, "utf8")));+ lines = mcpPatchLines(st.codex, process.execPath, yamlStr);+ } else {+ fs.writeFileSync(path.join(skillDir, "SKILL.md"), CUA_SKILL);+ lines = this.cuaLines(st);+ }} catch (e) {- log.warn("computer-use skill copy failed", { err: String(e) });+ log.warn("computer-use setup failed", { err: String(e) });+ return [];+ }+ if (this.settings().computerUse.approval === "ask-input") {+ lines.push(" - id: amb-computer-use-gate", ` name: ${yamlStr(path.join(this.o.runtimeDir, "plugins", "amb-computer-use-gate.mjs"))}`);}+ return lines;+ }++ /** Cua Driver:写配置(清单在 amb-cua-mcp.mjs 启动时按本平台实有工具生成),返回 mcp-client 配置行。 */+ private cuaLines(st: CuResolved): string[] {+ const dir = path.join(this.home, "cua");+ fs.mkdirSync(dir, { recursive: true, mode: 0o700 });+ const cfg = cuaConfig(st.cua, dir, { readonly: this.settings().permission === "readonly", apps: discoverApps() });+ const cfgFile = path.join(dir, "config.json");+ fs.writeFileSync(cfgFile, JSON.stringify(cfg, null, 2), { mode: 0o600 });+ const env = cuaMcpEnv(process.env, os.homedir(), cfgFile);return [- ...mcpPatchLines(st, process.execPath, yamlStr),- " - id: amb-computer-use-gate",- ` name: ${yamlStr(path.join(this.o.runtimeDir, "plugins", "amb-computer-use-gate.mjs"))}`,+ " - id: amb-computer-use",+ " name: '@deepseek-ai/dsh-mcp-client'",+ " config:",+ " transport: stdio",+ " serverName: computer-use",+ ` command: ${yamlStr(process.execPath)}`,+ ` args: [${yamlStr(path.join(this.o.runtimeDir, "plugins", "amb-cua-mcp.mjs"))}]`,+ " env:",+ ...Object.entries(env).map(([k, v]) => ` ${k}: ${yamlStr(v)}`),+ " toolCallTimeoutMs: 120000",+ " failOnStartupError: false",];}@@ -333,6 +367,7 @@ exec "$REAL" "$@"DSH_PERMISSION_MODE: permissionMode(this.settings().permission),AMB_MEMORY: this.settings().memory.enabled ? "1" : "0",AMB_CU_APPROVAL: this.settings().computerUse.approval,+ AMB_CU_READ_TOOLS: this.cuReadTools,AMB_MAGPIE_KEY: this.o.bridge.token,AMB_BRIDGE_URL: this.o.bridge.url,AMB_BRIDGE_TOKEN: this.o.bridge.token,@@ -349,7 +384,7 @@ exec "$REAL" "$@"/** 影响子进程启动参数的设置签名 */private settingsSig(): string {const st = this.settings();- return `${st.permission}|${st.memory.enabled}|${st.computerUse.enabled}|${st.computerUse.approval}`;+ return `${st.permission}|${st.memory.enabled}|${st.computerUse.enabled}|${st.computerUse.backend}|${st.computerUse.approval}`;}private sig = "";@@ -562,3 +597,27 @@ exec "$REAL" "$@"this.state = "stopped";}}++/** Cua Driver 后端的技能说明(由 anymodel bot 编写;工具来自 trycua/cua 的 cua-driver,MIT)。 */+const CUA_SKILL = `---+name: computer-use+description: Operate native desktop apps on this computer (read windows, click, type, press keys, scroll, drag) through the bundled Cua Driver. Use when the user asks you to do something in a GUI application on their computer.+---++# Computer Use (Cua Driver)++Tools are named \`mcp__computer-use__<tool>\`. Actions run automatically; no approval is needed unless this bot is set to ask before input actions (then wait for the user's answer).++## Loop+1. Find the target: \`list_windows\` (or \`list_apps\`), or start one with \`launch_app\`.+2. Read it narrowly: \`get_window_state({pid, window_id, query:"<what you need>"})\`. Rows look like \`[N] role "label"\`; the element token for row N is \`<snapshot_id>:N\` (e.g. "s0000002a:11"), never a bare number.+3. Act with the token on the same window: \`click\`, \`type_text\`, \`set_value\`, \`press_key\`, \`hotkey\`, \`scroll\`, \`drag\`, \`invoke_menu\`. Prefer \`run_steps({steps:[{tool,args},...], observe:{}})\` to act and see what changed in one call.+4. Verify at checkpoints with \`verify_state\` or a targeted \`get_window_state\` (use \`since:"latest"\` for just the changes). Use pixel coordinates only for surfaces missing from the accessibility tree, from a fresh screenshot of the same window.++## Rules+- Screenshots are for you to look at. Do not send them to the user unless they ask; if they do, save the image into the workspace and present that file.+- Only apps installed on this computer are in scope; if a call says the app or resource is outside the capability manifest, tell the user instead of looking for a workaround.+- Do not type passwords, payment details or other secrets unless the user explicitly gave them for this task. Do not confirm purchases, deletions or messages to other people without the user asking for exactly that.+- On Linux, reading window contents needs AT-SPI; on Wayland only the foreground window can be driven. On macOS the app needs Accessibility and Screen Recording permission; if \`check_permissions\` reports them missing, tell the user how to grant them.+`;+diff --git a/src/agent/manager.ts b/src/agent/manager.tsindex 30061e0..ae23121 100644--- a/src/agent/manager.ts+++ b/src/agent/manager.ts@@ -1,4 +1,4 @@-import { bundledRoots, CU_UNAVAILABLE, detectComputerUse } from "./computer-use.js";+import { resolveComputerUse } from "./computer-use.js";import type { Store } from "../store/db.js";import { logger } from "../log.js";import { AgentBridge } from "./bridge.js";@@ -106,14 +106,27 @@ export class AgentManager {}/** Computer Use 的系统可用性 + 本机器人开关,供界面与 /status 显示 */- computerUse(botId: string): { available: boolean; enabled: boolean; active: boolean; approval: string; reason: string } {+ computerUse(botId: string): ComputerUseInfo {const st = this.settings(botId).computerUse;- const sys = this.o.runtimeDir- ? detectComputerUse(bundledRoots(this.o.runtimeDir))- : { available: false, reason: `${CU_UNAVAILABLE}(代理运行时缺失)` };+ if (!this.o.runtimeDir) {+ const reason = "Computer Use: 不可用(代理运行时缺失)";+ return { available: false, enabled: st.enabled, active: false, backend: st.backend, selectedBackend: st.backend, approval: st.approval, reason, backends: [] };+ }+ const sys = resolveComputerUse(st.backend, this.o.runtimeDir);const active = this.available && sys.available && st.enabled;- const reason = sys.available && !st.enabled ? "Computer Use: 已在本机器人设置中关闭" : sys.reason;- return { available: sys.available, enabled: st.enabled, active, approval: st.approval, reason };+ const mode = st.approval === "ask-input" ? "点击/输入前先问我" : "操作自动执行";+ const reason = !st.enabled ? "Computer Use: 已在本机器人设置中关闭" : sys.available ? `${sys.reason};${mode}` : sys.reason;+ return {+ available: sys.available,+ enabled: st.enabled,+ active,+ backend: sys.backend,+ selectedBackend: st.backend,+ approval: st.approval,+ reason,+ notice: sys.notice,+ backends: sys.backends,+ };}settings(botId: string): BotSettings {@@ -452,3 +465,17 @@ export class AgentManager {return { done: true, outcome: row.status === "allowed" ? "allowed-once" : row.status === "cancelled" ? "cancelled" : "rejected" };}}++export interface ComputerUseInfo {+ available: boolean;+ enabled: boolean;+ active: boolean;+ /** 实际使用的后端 */+ backend: string;+ /** 设置里选的后端 */+ selectedBackend: string;+ approval: string;+ reason: string;+ notice?: string;+ backends: Array<{ id: string; available: boolean; reason: string }>;+}diff --git a/src/agent/settings.ts b/src/agent/settings.tsindex 168cff6..c8552d2 100644--- a/src/agent/settings.ts+++ b/src/agent/settings.ts@@ -1,5 +1,7 @@/** 每个机器人的身份与行为设置(存在 bots.settings JSON 列,网页可编辑)。 */export type PermissionPreset = "readonly" | "workspace";+export type CuBackend = "cua" | "codex";+export type CuApproval = "auto" | "ask-input";export interface BotSettings {/** 权限预设:readonly = 只读(不能改文件、命令只读沙箱);workspace = 可写本机器人工作区(默认),越界操作需审批 */@@ -14,11 +16,13 @@ export interface BotSettings {steward: {enabled: boolean;};- /** 电脑操作(仅 macOS,需已安装 Codex Computer Use) */+ /** 电脑操作 */computerUse: {enabled: boolean;- /** always = 每个操作都需审批(默认);readonly-free = 只在读取界面(截图/列应用)时免审批,点击输入等仍需审批 */- approval: "always" | "readonly-free";+ /** cua = 随附的 Cua Driver(默认,三个平台);codex = macOS 上的 Codex Computer Use 桥(kimi-computer-use) */+ backend: CuBackend;+ /** auto = 所有操作自动执行(默认);ask-input = 点击/输入等改变界面的操作先问用户,读取界面仍自动 */+ approval: CuApproval;};/** 后台小任务(记忆整理、上下文管家、交接摘要)用的模型;空 = 用对话模型 */utilityModel: string;@@ -28,7 +32,7 @@ export const DEFAULT_SETTINGS: BotSettings = {permission: "workspace",memory: { enabled: true, review: true, teamWrite: false },steward: { enabled: true },- computerUse: { enabled: true, approval: "always" },+ computerUse: { enabled: true, backend: "cua", approval: "auto" },utilityModel: "",};@@ -48,7 +52,9 @@ export function normalizeSettings(raw: unknown): BotSettings {steward: { enabled: b(st.enabled, DEFAULT_SETTINGS.steward.enabled) },computerUse: {enabled: b(cu.enabled, DEFAULT_SETTINGS.computerUse.enabled),- approval: cu.approval === "readonly-free" ? "readonly-free" : "always",+ backend: cu.backend === "codex" ? "codex" : "cua",+ // 旧值(always / readonly-free)一律迁移为自动执行+ approval: cu.approval === "ask-input" ? "ask-input" : "auto",},utilityModel: typeof r.utilityModel === "string" ? r.utilityModel.trim().slice(0, 200) : "",};diff --git a/src/core/runtime.ts b/src/core/runtime.tsindex 5390261..9702a69 100644--- a/src/core/runtime.ts+++ b/src/core/runtime.ts@@ -210,7 +210,8 @@ export class BotRuntime {const a = this.opts.agent;const mode = a?.available ? "代理模式(DeepSeek Harness,工作区沙箱)" : "纯对话模式";const pend = a ? a.approvals.pending(convId).map((r) => `${r.kind === "approval" ? "待批准" : "待回答"} ${r.code}`) : [];- const cu = a?.available ? [a.computerUse(botId).reason] : [];+ const cui = a?.available ? a.computerUse(botId) : undefined;+ const cu = cui ? [`电脑操作:${cui.backend === "codex" ? "Codex 桥" : "Cua Driver"},${cui.active ? "已启用" : "未启用"}——${cui.reason}`, ...(cui.notice && cui.enabled ? [cui.notice] : [])] : [];return [`当前模型:${m}${convModel ? "(会话设置)" : ""}`, `运行方式:${mode}`, ...cu, ...(pend.length ? [`未决:${pend.join(",")}`] : [])].join("\n");}case "/model": {diff --git a/src/store/db.ts b/src/store/db.tsindex 49a0c09..0f1c5b7 100644--- a/src/store/db.ts+++ b/src/store/db.ts@@ -153,6 +153,24 @@ export class Store {const cols = (this.db.prepare(`PRAGMA table_info(${table})`).all() as Row[]).map((r) => String(r.name));if (!cols.includes(col)) this.db.exec(`ALTER TABLE ${table} ADD COLUMN ${col} ${def}`);}+ this.migrateComputerUseAuto();+ }++ /** 一次性迁移:电脑操作改为默认自动执行(旧的 always / readonly-free 审批设置一律改成 auto)。 */+ private migrateComputerUseAuto(): void {+ const KEY = "migration.computer-use-auto";+ if (this.getKV(KEY)) return;+ for (const r of this.db.prepare("SELECT id, settings FROM bots").all() as Row[]) {+ let raw: any = {};+ try {+ raw = JSON.parse(String(r.settings ?? "{}"));+ } catch {+ raw = {};+ }+ if (raw && typeof raw === "object" && raw.computerUse && typeof raw.computerUse === "object") raw.computerUse.approval = "auto";+ this.db.prepare("UPDATE bots SET settings = ? WHERE id = ?").run(JSON.stringify(normalizeSettings(raw)), String(r.id));+ }+ this.setKV(KEY, String(Date.now()));}close(): void {diff --git a/test/cua-driver.unit.test.ts b/test/cua-driver.unit.test.tsnew file mode 100644index 0000000..1e1b844--- /dev/null+++ b/test/cua-driver.unit.test.ts@@ -0,0 +1,128 @@+import fs from "node:fs";+import os from "node:os";+import path from "node:path";+import { describe, expect, it } from "vitest";+import { cuaConfig, cuaMcpEnv, cuaRoots, CUA_INPUT_TOOLS, CUA_READ_TOOLS, CUA_TOOLS, detectCua, linuxDesktopApps, type CuaProbe } from "../src/agent/cua-driver.js";+import { Store } from "../src/store/db.js";++const proxy = path.join(__dirname, "..", "dsh-runtime", "plugins", "amb-cua-mcp.mjs");++describe("cua-driver detection", () => {+ const probe = (o: Partial<CuaProbe> & { files?: string[] } = {}): CuaProbe => {+ const files = new Set(o.files ?? []);+ return { platform: "linux", arch: "x64", env: {}, homedir: "/home/u", isFile: (p) => files.has(p), ...o };+ };+ it("needs the bundled binary", () => {+ expect(detectCua(["/r"], probe({ env: { DISPLAY: ":0" } })).reason).toMatch(/缺少 linux-x64/);+ });+ it("linux: X11 ok, Wayland degraded, no display unavailable", () => {+ const files = ["/r/cua-driver"];+ expect(detectCua(["/r"], probe({ files })).available).toBe(false);+ const x = detectCua(["/r"], probe({ files, env: { DISPLAY: ":0" } }));+ expect(x).toMatchObject({ available: true, bin: "/r/cua-driver" });+ expect(x.notice).toMatch(/AT-SPI/);+ const w = detectCua(["/r"], probe({ files, env: { WAYLAND_DISPLAY: "wayland-0", XDG_SESSION_TYPE: "wayland", DISPLAY: ":0" } }));+ expect(w.available).toBe(true);+ expect(w.notice).toMatch(/Wayland.*降级.*XWayland.*前台/);+ });+ it("macOS: permission guidance and --direct (unverified)", () => {+ const m = detectCua(["/r"], probe({ platform: "darwin", arch: "arm64", files: ["/r/cua-driver"] }));+ expect(m).toMatchObject({ available: true, direct: true });+ expect(m.notice).toMatch(/辅助功能.*屏幕录制/);+ expect(m.notice).toMatch(/未在真机验证/);+ });+ it("windows: needs an interactive session", () => {+ const files = ["/r/cua-driver.exe"];+ expect(detectCua(["/r"], probe({ platform: "win32", files })).reason).toMatch(/交互式会话/);+ expect(detectCua(["/r"], probe({ platform: "win32", files, env: { SESSIONNAME: "Console" } })).available).toBe(true);+ });+ it("roots cover packaged and dev layouts", () => {+ expect(cuaRoots("/a/dsh-runtime", "linux", "x64")).toContain("/a/build/cua-driver/linux-x64");+ expect(cuaRoots("/r/dsh")).toContain("/r/cua-driver");+ });+});++describe("cua-driver config", () => {+ it("tool sets exclude risky tools; readonly preset gets read tools only", () => {+ for (const bad of ["run_script", "kill_app", "clipboard_read", "browser_click", "parse_visual_regions", "install_extension", "check_for_update", "set_config", "start_recording", "replay_trajectory"]) {+ expect(CUA_TOOLS).not.toContain(bad);+ }+ const st = { available: true, reason: "", bin: "/r/cua-driver" };+ expect(cuaConfig(st, "/h/cua", { readonly: true, apps: [] }).tools).toEqual(CUA_READ_TOOLS);+ const c = cuaConfig(st, "/h/cua", { readonly: false, apps: [{ executable: "/usr/bin/x" }] });+ expect(c.tools).toEqual([...CUA_READ_TOOLS, ...CUA_INPUT_TOOLS]);+ expect(c.manifest).toBe(path.join("/h/cua", "manifest.yaml"));+ });+ it("child env carries the display but no secrets, telemetry off", () => {+ const env = cuaMcpEnv({ DISPLAY: ":77", MODEL_PROVIDER_API_KEY: "sk-x", AWS_SECRET: "y", DBUS_SESSION_BUS_ADDRESS: "unix:path=/tmp/b", PATH: "/usr/bin" }, "/home/u", "/h/cua/config.json");+ expect(env).toMatchObject({ DISPLAY: ":77", DBUS_SESSION_BUS_ADDRESS: "unix:path=/tmp/b", HOME: "/home/u", DO_NOT_TRACK: "1", CUA_DRIVER_RS_TELEMETRY_ENABLED: "0", AMB_CUA_CONFIG: "/h/cua/config.json" });+ expect(JSON.stringify(env)).not.toMatch(/sk-x|AWS_SECRET|API_KEY/);+ });+ it("discovers linux apps from .desktop files as canonical paths", () => {+ const d = fs.mkdtempSync(path.join(os.tmpdir(), "cua-apps-"));+ fs.mkdirSync(path.join(d, "share", "applications"), { recursive: true });+ fs.mkdirSync(path.join(d, "bin"));+ fs.writeFileSync(path.join(d, "bin", "editor"), "");+ fs.symlinkSync(path.join(d, "bin", "editor"), path.join(d, "bin", "ed-link"));+ const w = (n: string, t: string) => fs.writeFileSync(path.join(d, "share", "applications", n), t);+ w("a.desktop", "[Desktop Entry]\nExec=ed-link %F\n");+ w("b.desktop", "[Desktop Entry]\nExec=env FOO=1 editor\n");+ w("c.desktop", "[Desktop Entry]\nNoDisplay=true\nExec=hidden\n");+ w("d.desktop", "[Desktop Entry]\nExec=flatpak run org.x\n");+ w("e.desktop", "[Desktop Entry]\nExec=missing-prog\n");+ const apps = linuxDesktopApps({ XDG_DATA_HOME: path.join(d, "none"), XDG_DATA_DIRS: path.join(d, "share"), PATH: path.join(d, "bin") }, "/nonexistent");+ expect(apps).toEqual([{ executable: fs.realpathSync(path.join(d, "bin", "editor")) }]);+ });+});++describe("cua MCP front (amb-cua-mcp.mjs)", () => {+ it("writes a bounded v3 manifest", async () => {+ const m = await import(proxy);+ const y = m.manifestYaml({ tools: ["click", "list_windows"], apps: [{ executable: "/usr/bin/x" }, { bundle_id: "com.a.b" }], display: true });+ expect(y).toContain("version: 3\nexpires_after: 24h\nidle_timeout: 24h");+ expect(y).toContain(' - "click"');+ expect(y).toContain(' - executable: "/usr/bin/x"');+ expect(y).toContain(' - bundle_id: "com.a.b"');+ expect(y).toContain("terminate: driver_launched");+ expect(y).toContain(" desktop:\n display: true");+ });+ it("parses list-tools and builds a bounded, telemetry-free env", async () => {+ const m = await import(proxy);+ expect([...m.parseToolList("click: Click a thing\nbrowser_x: y\n indented: no\nnot a tool\n")]).toEqual(["click", "browser_x"]);+ const env = m.childEnv({ PATH: "/bin", AMB_CUA_CONFIG: "/c", OPENAI_API_KEY: "k", CUA_DRIVER_DANGEROUSLY_BYPASS_APPROVALS: "1", ELECTRON_RUN_AS_NODE: "1" }, { manifest: "/m.yaml" });+ expect(env).toMatchObject({ PATH: "/bin", CUA_DRIVER_PERMISSION_MODE: "bounded", CUA_DRIVER_CAPABILITY_MANIFEST_FILE: "/m.yaml", CUA_DRIVER_CAPABILITY_MANIFEST_APPROVED: "1", DO_NOT_TRACK: "1", CUA_DRIVER_RS_TELEMETRY_ENABLED: "0", CUA_DRIVER_DISABLE_RUN_SCRIPT: "1" });+ for (const k of ["AMB_CUA_CONFIG", "OPENAI_API_KEY", "CUA_DRIVER_DANGEROUSLY_BYPASS_APPROVALS", "ELECTRON_RUN_AS_NODE"]) expect(env[k]).toBeUndefined();+ });+ it("hides and refuses tools outside the allow list", async () => {+ const m = await import(proxy);+ const f = m.createFilter(new Set(["click"]));+ expect(f.fromClient({ jsonrpc: "2.0", id: 1, method: "tools/list" }).forward).toBeTruthy();+ const listed = f.fromDriver({ jsonrpc: "2.0", id: 1, result: { tools: [{ name: "click" }, { name: "run_script" }] } });+ expect(listed.result.tools.map((t: any) => t.name)).toEqual(["click"]);+ const denied = f.fromClient({ jsonrpc: "2.0", id: 2, method: "tools/call", params: { name: "kill_app" } });+ expect(denied.forward).toBeUndefined();+ expect(denied.reply.result.isError).toBe(true);+ expect(f.fromClient({ jsonrpc: "2.0", id: 3, method: "tools/call", params: { name: "click" } }).forward).toBeTruthy();+ });+});++describe("settings migration", () => {+ it("moves existing bots to auto-run once", () => {+ const d = fs.mkdtempSync(path.join(os.tmpdir(), "cua-mig-"));+ const f = path.join(d, "t.db");+ const s1 = new Store(f);+ const b = s1.createBot({ name: "x" } as any);+ s1.db.prepare("UPDATE bots SET settings = ? WHERE id = ?").run(JSON.stringify({ computerUse: { enabled: false, approval: "always" } }), b.id);+ s1.db.prepare("DELETE FROM kv WHERE key = 'migration.computer-use-auto'").run();+ s1.close();+ const s2 = new Store(f);+ const raw = JSON.parse(String((s2.db.prepare("SELECT settings FROM bots WHERE id = ?").get(b.id) as any).settings));+ expect(raw.computerUse).toEqual({ enabled: false, backend: "cua", approval: "auto" });+ // 之后用户自己选的 ask-input 不会再被改回+ s2.db.prepare("UPDATE bots SET settings = ? WHERE id = ?").run(JSON.stringify({ computerUse: { approval: "ask-input" } }), b.id);+ s2.close();+ const s3 = new Store(f);+ expect(s3.getBot(b.id)!.settings.computerUse.approval).toBe("ask-input");+ s3.close();+ });+});diff --git a/test/phase3.unit.test.ts b/test/phase3.unit.test.tsindex 0eb85a8..3fc694c 100644--- a/test/phase3.unit.test.ts+++ b/test/phase3.unit.test.ts@@ -91,7 +91,11 @@ describe("settings", () => {const s = normalizeSettings({ permission: "full", memory: { review: false }, computerUse: { approval: "never" } });expect(s.permission).toBe("workspace");expect(s.memory).toEqual({ enabled: true, review: false, teamWrite: false });- expect(s.computerUse).toEqual({ enabled: true, approval: "always" });+ expect(s.computerUse).toEqual({ enabled: true, backend: "cua", approval: "auto" });+ // 旧的审批设置迁移为自动执行+ expect(normalizeSettings({ computerUse: { approval: "always" } }).computerUse.approval).toBe("auto");+ expect(normalizeSettings({ computerUse: { approval: "readonly-free" } }).computerUse.approval).toBe("auto");+ expect(normalizeSettings({ computerUse: { approval: "ask-input", backend: "codex" } }).computerUse).toEqual({ enabled: true, backend: "codex", approval: "ask-input" });expect(permissionMode("readonly")).toBe("read-only");expect(permissionMode("workspace")).toBe("workspace-write");});@@ -178,24 +182,30 @@ describe("computer use detection", () => {});describe("computer use approval gate plugin", () => {- it("asks for actions, lets readonly through when relaxed", async () => {+ it("is a no-op by default and asks only for input actions in ask-input mode", async () => {const plugin = path.join(__dirname, "..", "dsh-runtime", "plugins", "amb-computer-use-gate.mjs");- const run = async (mode: string | undefined, tool: string) => {- const prev = process.env.AMB_CU_APPROVAL;+ const run = async (mode: string | undefined, tool: string, read = "list_apps,get_app_state,get_window_state") => {+ const prev = { a: process.env.AMB_CU_APPROVAL, r: process.env.AMB_CU_READ_TOOLS };if (mode) process.env.AMB_CU_APPROVAL = mode;else delete process.env.AMB_CU_APPROVAL;+ process.env.AMB_CU_READ_TOOLS = read;let hook: any;const mod = await import(plugin + `?t=${Math.random()}`);mod.apply({ on: (_ev: string, fn: any) => (hook = fn) });- if (prev === undefined) delete process.env.AMB_CU_APPROVAL;- else process.env.AMB_CU_APPROVAL = prev;- return hook({ name: tool }, async () => ({ kind: "allow" }));+ for (const [k, v] of [["AMB_CU_APPROVAL", prev.a], ["AMB_CU_READ_TOOLS", prev.r]] as const) {+ if (v === undefined) delete process.env[k];+ else process.env[k] = v;+ }+ return hook ? hook({ name: tool }, async () => ({ kind: "allow" })) : { kind: "allow", noHook: true };};- expect((await run(undefined, "mcp__computer-use__click")).kind).toBe("ask");- expect((await run(undefined, "mcp__computer-use__get_app_state")).kind).toBe("ask");- expect((await run("readonly-free", "mcp__computer-use__get_app_state")).kind).toBe("allow");- expect((await run("readonly-free", "mcp__computer-use__type_text")).kind).toBe("ask");- expect((await run(undefined, "read_file")).kind).toBe("allow");+ // 默认(auto):不注册钩子,所有操作直接执行+ expect(await run(undefined, "mcp__computer-use__click")).toMatchObject({ kind: "allow", noHook: true });+ expect(await run("auto", "mcp__computer-use__type_text")).toMatchObject({ kind: "allow", noHook: true });+ expect((await run("ask-input", "mcp__computer-use__get_window_state")).kind).toBe("allow");+ expect((await run("ask-input", "mcp__computer-use__get_app_state")).kind).toBe("allow");+ expect((await run("ask-input", "mcp__computer-use__type_text")).kind).toBe("ask");+ expect((await run("ask-input", "mcp__computer-use__click")).kind).toBe("ask");+ expect((await run("ask-input", "read_file")).kind).toBe("allow");});});diff --git a/ui/app.js b/ui/app.jsindex f704038..b3b221c 100644--- a/ui/app.js+++ b/ui/app.js@@ -119,12 +119,17 @@ function BotSettings({ botId, models }) {<option value="">与对话模型相同</option>${[...new Set([...(models || []), s.utilityModel].filter(Boolean))].map((m) => html`<option value=${m}>${m}</option>`)}</select>- <label>电脑操作(Computer Use,仅 macOS)</label>- <p class="muted">${cu ? cu.reason : ""}</p>+ <label>电脑操作(Computer Use)</label>+ <p class="muted">${cu ? (cu.available ? "✓ " : "✗ ") + cu.reason : ""}</p>+ ${cu && cu.notice && html`<p class="muted">ℹ️ ${cu.notice}</p>`}${chk("启用电脑操作", s.computerUse.enabled, (v) => ({ computerUse: { enabled: v } }))}+ <select value=${s.computerUse.backend} disabled=${!s.computerUse.enabled} onChange=${(e) => put({ computerUse: { backend: e.target.value } })}>+ <option value="cua">Cua Driver(默认,随附,macOS / Windows / Linux)</option>+ ${cu && cu.backends.some((b) => b.id === "codex") && html`<option value="codex">Codex Computer Use 桥(仅 macOS${cu.backends.find((b) => b.id === "codex").available ? "" : ",本机不可用"})</option>`}+ </select><select value=${s.computerUse.approval} disabled=${!s.computerUse.enabled} onChange=${(e) => put({ computerUse: { approval: e.target.value } })}>- <option value="always">每个操作都需要审批(默认)</option>- <option value="readonly-free">查看界面(列应用/读取窗口)免审批,点击输入等仍需审批</option>+ <option value="auto">所有操作自动执行,无需审批(默认)</option>+ <option value="ask-input">点击、输入等改变界面的操作先问我;读取界面仍自动</option></select><p class="muted">${msg}</p></div>`;