Skip to main content
R

dsh-computer-control

regularsyr7/dsh-computer-control

Give a DSH harness real control of a Mac: screenshots, mouse and keyboard through a bundled Swift HID helper, app and whitelisted shell launching, and system info — every call returns a self-check that proves what actually happened.

Install

dsh plugin --profile web add github:regularsyr7/dsh-computer-control

README

@rainybox/dsh-computer-control

Give a DSH harness real control of this Mac: screenshots, mouse and keyboard, app launching, shell execution, system info. Every call returns a self_check field — a real echo proving what actually happened, not a claim.

macOS only. The mouse/keyboard half is a small Swift helper compiled on first use.

Install

npm i @rainybox/dsh-computer-control
# or from source:
git clone https://github.com/RegularsYr7/dsh-computer-control.git

Then link it into your DSH profile and add it to the bundle stack:

cd ~/.dsh/profiles/desktop
pnpm add link:~/Developer/dsh-plugins/dsh-computer-control
# add "@rainybox/dsh-computer-control" to dsh.profile.bundles in package.json

Restart DSH. The tool computer_control appears with ten actions.

Permissions (grant these first)

CapabilitySystem Settings pathSymptom when missing
Screen RecordingPrivacy & Security → Screen Recording → enable DSH.appscreencapture yields an empty file; the tool reports it
AccessibilityPrivacy & Security → Accessibility → enable DSH.apphelper exits 3: accessibility permission not granted
Input MonitoringPrivacy & Security → Input Monitoring → enable DSH.appsynthesized events are dropped by the system

action=status returns accessibility / post_event_access / listen_event_access; all three must be true for input to land.


中文说明

让 DSH harness 直接操作这台 Mac:截屏、键鼠、启动程序、执行命令、读系统信息。 每个工具调用都返回 self_check 字段——用真实回显证明"这次到底做了什么",不是声明。

  • 插件名:@rainybox/dsh-computer-control
  • 版本:0.1.0
  • 暴露给 harness 的工具:computer_control(单一工具,action 分发十个动作)
  • 依赖:macOS 自带 /usr/sbin/screencapture、/usr/bin/osascript、/usr/sbin/system_profiler;键鼠走插件自带的 Swift 助手(首次调用自动编译,需要本机有 swiftc)

目录结构

dsh-computer-control/
├── package.json                 # name/version/main + dsh.bundle.patch 挂载声明
├── cordis.patch.yml             # 向 profile 层栈 insert 本插件
├── lib/index.js                 # 宿主入口(转发 src/index.js)
├── src/index.js                 # Cordis 插件:defineTool(computer_control) + exec 白名单 + 审计
├── native/DshInputHelper.swift  # 键鼠助手源码(CoreGraphics HID + Accessibility)
├── native/bin/dsh-input-helper  # 编译产物(build-helper.mjs 生成;缺失时首次调用自动编译)
├── scripts/build-helper.mjs     # swiftc -O 编译,按 mtime 跳过重复编译
└── README.md

动作一览

action作用关键参数self_check 证明什么
status助手与权限自检–helper、accessibility、screencapture_exit
screenshot全屏/区域/指定显示器截图save_path、region="x,y,w,h"、displayPNG 头校验的宽高与字节数 + 坐标换算比例
move移动鼠标x、y助手回显的坐标
click点击x、y、button、count回显的坐标/按钮/次数
type输入文本(支持中文,\n 回车)text、delay_ms助手回显 chars=N 与请求长度对比
key组合键combo="cmd+shift+4"回显 keyCode 与修饰键
scroll滚轮dy、dx,可选中点 x/y回显的滚动量
launch启动 App/文件app、argsopen 退出码 + 目标名
exec执行 shell 命令command、cwd、timeout_ms、confirm退出码 + echo 通道探针
sysinfo系统信息detail解析出的机型/芯片/内存/显示器/系统版本

坐标与焦点(踩过的坑,务必先读)

  1. 键鼠坐标是"屏幕点",截图是"像素"。Retina 上 1 点 = 2 像素;screenshot 的 self_check 会打印 点 = 像素 / pixel_scale(屏幕 W×H 点)。拿像素坐标直接点会点到别处。
  2. 键盘事件跟随"当前前台 App"。osascript activate 报 frontmost=true 之后也可能在几百毫秒内被 其他窗口(通知、聊天面板、全屏 App)抢走焦点。可靠流程是: screenshot → click 目标输入区 → screenshot 确认菜单栏/窗口已切换 → type。 在写代码/写文档的场景里,宁可先 click 两次再 type。
  3. 多屏/多空间:screencapture 抓的是当前主屏;display 参数只在 screencapture -D 下生效。
  4. AXPress 不等于导航。对某些 App(实测 Microsoft Outlook)用 perform action "AXPress" 到侧栏行 不报错也不切换视图;可靠做法是取元素 position 后用 computer_control 的 click 点真实坐标, 再用窗口标题回读校验。

权限(首次一定会用到)

能力系统设置路径缺失时的表现
屏幕录制隐私与安全性 → 屏幕录制 → 勾选 DSH.appscreencapture 产出空图,工具明确报"截图内容异常"
辅助功能隐私与安全性 → 辅助功能 → 勾选 DSH.app键鼠助手 exit 3,报"accessibility permission not granted"
输入监听隐私与安全性 → 输入监听 → 勾选 DSH.app键鼠事件被系统丢弃

action=status 里 accessibility / post_event_access / listen_event_access 三个字段为 true 才算真的能模拟输入。

exec 白名单与显式确认

exec 默认只放行只读、本地无害的前缀(ls、cat、grep、ps、echo、sw_vers、open、 osascript -e、screencapture、sips、say…),并按 && || ; | 换行 逐段校验; 出现 $(...) 或反引号直接拒绝(防命令替换绕过)。

不在白名单的命令不会执行,而是报错并提示:

命令不在白名单,未执行:rm -rf /tmp/x; 白名单是只读/本地无害前缀(见 $DSH_HOME/computer-control/config.json)。
确需执行时,把 confirm 参数设成与 command 完全相同的字符串。

传 confirm="<与 command 完全相同的字符串>" 才放行,且审计日志里会标注"显式确认"。

私有配置与审计

  • 配置:$DSH_HOME/computer-control/config.json
    {
      "allowed_prefixes": ["ls", "cat", "grep", "..."],
      "default_timeout_ms": 120000,
      "max_output_bytes": 200000
    }
    
  • 审计:$DSH_HOME/computer-control/audit.jsonl(每次 exec/type/click/key 一行,含时间、动作、参数摘要、退出码)
  • 截图默认落:$DSH_HOME/computer-control/shots/<timestamp>.png

安装与注册

npm i @rainybox/dsh-computer-control

# 或者从源码链接进 profile
cd ~/.dsh/profiles/desktop
pnpm add link:~/Developer/dsh-plugins/dsh-computer-control

# 把插件放进 profile 的 bundle 层栈(package.json 的 dsh.profile.bundles 末尾追加)
#    "@rainybox/dsh-computer-control"

cordis.patch.yml:

- insert:
    - id: '@rainybox/dsh-computer-control'
      name: '@rainybox/dsh-computer-control'

注意:entry id 必须与包名一致;而且在 dsh.profile.bundles 里没有列出来的包根本不会被 import—— 只在 package.json 里装了依赖不够,apply() 一次都不会跑。

注册后 harness 启动即挂载,无需额外注册命令。

最小调用示例

// 1. 截屏
{"action": "screenshot", "save_path": "/tmp/shot.png", "region": "0,0,1280,720"}
// → 已截屏 1280×720 → /tmp/shot.png
// 自检:PNG 头校验通过:1280×720, 436093 字节 > 1024;坐标换算:点 = 像素 / 1(屏幕 2560×1440 点)

// 2. 执行命令(白名单)
{"action": "exec", "command": "echo dsh-exec-ok && uname -m"}
// → 退出码 0;stdout: dsh-exec-ok / arm64
// 自检:echo 通道 = dsh-exec-ok;命令退出码 = 0

// 3. 输入文本(先 click 再 type,最后用 system_profiler/osascript 回读校验)
{"action": "type", "text": "DSH harness 键盘输入 OK 2026-11-30"}
// → 已键入 30 个字符
// 自检:助手回显 chars=30(请求 30 字符)

已知限制

  • 仓库自带 arm64 编译产物;换到其他架构(如 x86_64)时,首次调用键鼠动作会触发 Swift 编译(约 3–8 秒,需要本机装有 Xcode Command Line Tools),产物落在 native/bin/,之后毫秒级。
  • 助手只做单屏 HID 注入;需要拖动/长按等复杂手势时,用多次 move + click 组合,或自行扩展 Swift 命令。
  • type 走 Unicode 字符串注入,个别只认物理键位的 App(部分游戏、密码框)可能不接收——这属于目标程序行为,不是权限问题。
  • 仅在 macOS 上实现;其他平台 ensureHelper() 直接报错。

License

MIT © Rainy

Related plugins