chenbhao Claude Big Pickle commited on
Commit
9685a3e
·
1 Parent(s): b788554

chore: docs — add Computer Use Python runtime documentation

Browse files

Co-Authored-By: Claude Big Pickle <noreply@anthropic.com>

docs/README.md CHANGED
@@ -48,6 +48,7 @@
48
  | 文档 | 说明 |
49
  |------|------|
50
  | [HTTP/WS 服务器](server/overview.md) | Bun.serve()、REST API、WebSocket、服务层 |
 
51
  | [Provider 代理](server/proxy-provider.md) | Anthropic ↔ OpenAI 协议转换、多提供商支持 |
52
 
53
  ### 后端服务
 
48
  | 文档 | 说明 |
49
  |------|------|
50
  | [HTTP/WS 服务器](server/overview.md) | Bun.serve()、REST API、WebSocket、服务层 |
51
+ | [Computer Use 系统](server/computer-use.md) | Python Runtime 辅助脚本架构、跨平台设计、JSON 通信协议 |
52
  | [Provider 代理](server/proxy-provider.md) | Anthropic ↔ OpenAI 协议转换、多提供商支持 |
53
 
54
  ### 后端服务
docs/server/computer-use.md ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Computer Use 系统
2
+
3
+ ## 概述
4
+
5
+ Computer Use 功能允许 AI 模型直接控制桌面 — 截取屏幕、移动鼠标、点击、键盘输入、窗口管理等。系统由两部分组成:
6
+
7
+ 1. **TypeScript API 层** (`src/server/api/computer-use.ts`) — 环境检测、Python venv 创建、依赖安装
8
+ 2. **Python Runtime 辅助脚本** (`runtime/`) — 通过子进程 `spawn` 执行,通过 stdout JSON 协议通信
9
+
10
+ ## Python Runtime 辅助脚本
11
+
12
+ ### 架构
13
+
14
+ ```
15
+ ┌─────────────────────────────────────────────┐
16
+ │ CLI 进程 (Bun/TypeScript) │
17
+ │ src/server/api/computer-use.ts │
18
+ │ │ │
19
+ │ │ child_process.spawn("python3", ...) │
20
+ │ ▼ │
21
+ │ ~/.claude/.runtime/ │
22
+ │ ├── mac_helper.py (macOS) │
23
+ │ ├── win_helper.py (Windows) │
24
+ │ ├── requirements.txt │
25
+ │ └── venv/ │
26
+ │ └── pip install pyautogui mss ... │
27
+ │ │ │
28
+ │ ▼ stdout (JSON) │
29
+ │ {"ok": true, "result": ...} │
30
+ └─────────────────────────────────────────────┘
31
+ ```
32
+
33
+ **编译时**: 脚本通过 Bun 的 `with { type: 'text' }` 嵌入 bundle(`computer-use.ts:27-33`)。
34
+
35
+ **运行时**: 首次使用 Computer Use 时,`ensureRuntimeFiles()` 将脚本提取到 `~/.claude/.runtime/`,创建 Python venv 并安装依赖。之后每次调用通过 `child_process.spawn` 启动 `python3 mac_helper.py <command> --payload <json>`。
36
+
37
+ ### 文件说明
38
+
39
+ | 文件 | 平台 | 行数 | 依赖 |
40
+ |------|------|------|------|
41
+ | `runtime/mac_helper.py` | macOS | ~775 | pyautogui, mss, Pillow, pyobjc (Quartz/AppKit) |
42
+ | `runtime/win_helper.py` | Windows | ~770 | pyautogui, mss, Pillow, pywin32, psutil, screeninfo, pyperclip |
43
+ | `runtime/test_helpers.py` | 跨平台 | ~322 | unittest (标准库) |
44
+ | `runtime/requirements.txt` | macOS | — | pip 依赖声明 |
45
+ | `runtime/requirements-win.txt` | Windows | — | pip 依赖声明 |
46
+
47
+ ### 通信协议
48
+
49
+ 严格的 JSON 行协议。每次调用:
50
+
51
+ ```
52
+ $ python3 mac_helper.py screenshot --payload '{"displayId": null, "targetWidth": 1024, "targetHeight": 768}'
53
+ {"ok": true, "result": {"base64": "...", "width": 1024, "height": 768, ...}}
54
+ ```
55
+
56
+ 失败时返回:
57
+ ```json
58
+ {"ok": false, "error": {"code": "runtime_error", "message": "..."}}
59
+ ```
60
+
61
+ ### 命令列表
62
+
63
+ 两个 helper 暴露完全相同的命令集:
64
+
65
+ **屏幕捕获:**
66
+ - `list_displays` — 列举所有显示器(分辨率、缩放因子、原点坐标)
67
+ - `get_display_size` — 获取指定显示器的尺寸
68
+ - `screenshot` — 截取全屏(可选 resize)
69
+ - `resolve_prepare_capture` — 带 fallback 的屏幕捕获
70
+ - `zoom` — 截取指定区域
71
+
72
+ **窗口管理:**
73
+ - `list_windows` — 列举可见窗口(标题、位置、所属应用)
74
+ - `find_window_displays` — 查询窗口所在的显示器
75
+ - `frontmost_app` — 获取当前前台应用
76
+ - `app_under_point` — 获取屏幕坐标下的应用
77
+ - `list_installed_apps` — 列举已安装应用
78
+ - `list_running_apps` — 列举运行中的应用
79
+ - `open_app` — 打开指定应用
80
+
81
+ **鼠标控制:**
82
+ - `click` — 点击(支持修饰键、多击)
83
+ - `drag` — 拖拽
84
+ - `move_mouse` — 移动鼠标
85
+ - `scroll` — 滚动(支持水平和垂直)
86
+ - `mouse_down` / `mouse_up` — 鼠标按键按下/释放
87
+ - `cursor_position` — 获取当前光标位置
88
+
89
+ **键盘控制:**
90
+ - `key` — 按键组合(如 `cmd+v`)
91
+ - `hold_key` — 按住键指定时长
92
+ - `type` — 输入文本
93
+
94
+ **剪贴板:**
95
+ - `read_clipboard` — 读取剪贴板文本
96
+ - `write_clipboard` — 写入剪贴板
97
+ - `paste_clipboard` — 执行粘贴(cmd+v / ctrl+v)
98
+
99
+ **权限检测:**
100
+ - `check_permissions` — 检测 Accessibility / Screen Recording 权限
101
+
102
+ **空操作(桌面应用兼容):**
103
+ - `prepare_for_action` — 返回空数组
104
+ - `preview_hide_set` — 返回空数组
105
+
106
+ ### 跨平台设计
107
+
108
+ 两套 helper 遵循 **同一 JSON 协议**,差异仅限平台相关底层实现:
109
+
110
+ | 能力 | macOS (`mac_helper.py`) | Windows (`win_helper.py`) |
111
+ |------|------------------------|---------------------------|
112
+ | 显示器枚举 | Quartz `CGGetActiveDisplayList` | `screeninfo.get_monitors` |
113
+ | 窗口枚举 | Quartz `CGWindowListCopyWindowInfo` | `win32gui.EnumWindows` |
114
+ | 应用管理 | `NSWorkspace` (AppKit) | `psutil` + `winreg` |
115
+ | 剪贴板 | `NSPasteboard` (AppKit) | `pyperclip` |
116
+ | 修饰键 | `command` | `win` |
117
+ | 粘贴快捷键 | osascript `cmd+v` | pyautogui `ctrl+v` |
118
+ | 权限模型 | TCC (Transparency, Consent, and Control) | 始终返回 `True` |
119
+
120
+ `test_helpers.py` 通过静态分析验证两者的 KEY_MAP 一致性、命令集完整性和辅助函数签名。
121
+
122
+ ### 与桌面 HTTP API 的关系
123
+
124
+ 桌面服务器通过 `/api/computer-use/status` (GET) 和 `/api/computer-use/setup` (POST) 管理 Python runtime 的安装和状态检测。实际执行 Computer Use 动作时,CLI 进程直接 `spawn` helper 脚本,不经过 HTTP 层。
docs/server/overview.md CHANGED
@@ -107,7 +107,7 @@ VersperClaw 桌面服务器是一个同进程 HTTP + WebSocket 服务器,基
107
  | `/api/teams` | 团队配置 |
108
  | `/api/providers` | Provider 提供商配置 |
109
  | `/api/adapters` | 适配器管理 |
110
- | `/api/computer-use` | Computer Use 功能控制 |
111
  | `/api/haha-oauth` | haha 自定义 OAuth 认证 |
112
  | `/api/haha-openai-oauth` | haha OpenAI OAuth 认证 |
113
  | `/api/h5-access` | H5 访问策略 |
 
107
  | `/api/teams` | 团队配置 |
108
  | `/api/providers` | Provider 提供商配置 |
109
  | `/api/adapters` | 适配器管理 |
110
+ | `/api/computer-use` | [Computer Use 功能控制](computer-use.md) — 环境检测、Python venv 安装、桌面控制 |
111
  | `/api/haha-oauth` | haha 自定义 OAuth 认证 |
112
  | `/api/haha-openai-oauth` | haha OpenAI OAuth 认证 |
113
  | `/api/h5-access` | H5 访问策略 |