@celljs/ta-computer-use 4.42.0 → 4.43.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +1 -1
- package/README.md +1 -1
- package/esm/host.d.ts +31 -0
- package/esm/host.js +39 -0
- package/host/index.mjs +487 -0
- package/package.json +5 -3
package/README.en.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Cell - Ta Computer Use
|
|
2
2
|
|
|
3
|
-
A built-in pi package for Ta (enabled by default, can be disabled in Web client Settings → Extensions): lets Ta use desktop applications on this machine via [Cua Driver](https://cua.ai/cua-driver) in the background. It lists apps and windows, reads window accessibility trees, performs clicks, types text, scrolls, and takes screenshots, without taking your mouse and keyboard.
|
|
3
|
+
A built-in pi package for Ta (enabled by default, can be disabled in Web client Settings → Extensions): lets Ta use desktop applications on this machine via [Cua Driver](https://cua.ai/cua-driver) in the background. It lists apps and windows, reads window accessibility trees, performs clicks, types text, scrolls, and takes screenshots, without taking your mouse and keyboard. One pi extension (`src/extension.ts` → `extension/index.mjs`) does it, modifying nothing of Ta's core, so pi can use it as well; a host (`pi.host`, `src/host.ts`) gives Cua Driver to Ta's other packages by calls: `driver()` the program, the socket of its daemon and its environment (none when nothing is installed), `ensureDriver()` starts the daemon when it is not running. ta-reflex reads and works the windows of desktop apps with it; with the package off its host does not run, so no other package gets the driver.
|
|
4
4
|
|
|
5
5
|
[Ta Desktop](../../apps/ta-desktop/README.md) comes with Cua Driver (fixed version) and works out of the box. For the terminal Ta, use your own installation:
|
|
6
6
|
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Cell - Ta Computer Use
|
|
2
2
|
|
|
3
|
-
Ta 自带的 pi 包(默认开启,可在 Web 客户端的 设置 → 扩展 关闭):让 Ta 经 [Cua Driver](https://cua.ai/cua-driver)
|
|
3
|
+
Ta 自带的 pi 包(默认开启,可在 Web 客户端的 设置 → 扩展 关闭):让 Ta 经 [Cua Driver](https://cua.ai/cua-driver) 在后台使用这台电脑上的桌面应用——列出应用和窗口、读取窗口的辅助功能树、点击、输入、滚动、截图,不抢用户的鼠标和键盘。一个 pi 扩展(`src/extension.ts` → `extension/index.mjs`),不改 Ta 的核心,pi 也能装;另有一个宿主(`pi.host`,`src/host.ts`),把 Cua Driver 经调用交给 Ta 的其他包:`driver()` 给出程序、守护进程的 socket 和环境变量(没装时为空),`ensureDriver()` 在守护进程没运行时启动它。ta-reflex 用它读写桌面应用的窗口;包关着时宿主不运行,别的包也就拿不到驱动。
|
|
4
4
|
|
|
5
5
|
[Ta Desktop](../../apps/ta-desktop/README.md) 自带 Cua Driver(固定版本),装好就能用。终端里的 Ta 用用户自己装的那一份:
|
|
6
6
|
|
package/esm/host.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type CuaDriverProgram } from './cua-driver.js';
|
|
2
|
+
import { type CuaDriverSetup } from './extension.js';
|
|
3
|
+
/** How Cua Driver is reached, for another package of this computer that drives its windows itself (ta-reflex). */
|
|
4
|
+
export interface CuaDriverReach {
|
|
5
|
+
/** The program to run `call <tool> <json>` with. */
|
|
6
|
+
program: string;
|
|
7
|
+
/** The socket of the daemon Ta runs (Ta Desktop on macOS); none: the program's own. */
|
|
8
|
+
socket?: string;
|
|
9
|
+
env?: Record<string, string>;
|
|
10
|
+
}
|
|
11
|
+
/** What the host of the package needs: tests give their own. */
|
|
12
|
+
export interface ComputerUseHostOptions {
|
|
13
|
+
locate(): CuaDriverProgram | undefined;
|
|
14
|
+
platform: NodeJS.Platform;
|
|
15
|
+
setup(program: CuaDriverProgram, platform: NodeJS.Platform): CuaDriverSetup;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The server side of the package (`pi.host`): Cua Driver for the other packages of Ta, by calls
|
|
19
|
+
* (`callPackageHost('@celljs/ta-computer-use', 'driver')`), never by bundling this package. It runs only while the
|
|
20
|
+
* package is on, so a package that asks gets nothing once the user turned computer use off.
|
|
21
|
+
*/
|
|
22
|
+
export declare class ComputerUseHost {
|
|
23
|
+
protected readonly options: ComputerUseHostOptions;
|
|
24
|
+
constructor(options?: ComputerUseHostOptions);
|
|
25
|
+
/** How to reach the driver now; undefined where none is installed. Looked for at each call, as installing one needs no restart. */
|
|
26
|
+
driver(): CuaDriverReach | undefined;
|
|
27
|
+
/** The daemon of the driver running, started if it is not; nothing to do for a driver without one. */
|
|
28
|
+
ensureDriver(): Promise<void>;
|
|
29
|
+
protected setup(): CuaDriverSetup | undefined;
|
|
30
|
+
}
|
|
31
|
+
export default function activate(): ComputerUseHost;
|
package/esm/host.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { CuaDriver } from './cua-driver.js';
|
|
2
|
+
import { setupOf } from './extension.js';
|
|
3
|
+
const DEFAULTS = { locate: () => CuaDriver.locate(), platform: process.platform, setup: setupOf };
|
|
4
|
+
/**
|
|
5
|
+
* The server side of the package (`pi.host`): Cua Driver for the other packages of Ta, by calls
|
|
6
|
+
* (`callPackageHost('@celljs/ta-computer-use', 'driver')`), never by bundling this package. It runs only while the
|
|
7
|
+
* package is on, so a package that asks gets nothing once the user turned computer use off.
|
|
8
|
+
*/
|
|
9
|
+
export class ComputerUseHost {
|
|
10
|
+
constructor(options = DEFAULTS) {
|
|
11
|
+
this.options = options;
|
|
12
|
+
}
|
|
13
|
+
/** How to reach the driver now; undefined where none is installed. Looked for at each call, as installing one needs no restart. */
|
|
14
|
+
driver() {
|
|
15
|
+
const setup = this.setup();
|
|
16
|
+
if (!setup) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
const daemon = setup.daemon;
|
|
20
|
+
return {
|
|
21
|
+
program: daemon ? `${daemon.app}/Contents/MacOS/cua-driver` : setup.program.path,
|
|
22
|
+
...(daemon ? { socket: daemon.socket } : {}),
|
|
23
|
+
...(setup.env ? { env: { ...setup.env } } : {}),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/** The daemon of the driver running, started if it is not; nothing to do for a driver without one. */
|
|
27
|
+
async ensureDriver() {
|
|
28
|
+
var _a, _b;
|
|
29
|
+
await ((_b = (_a = this.setup()) === null || _a === void 0 ? void 0 : _a.daemon) === null || _b === void 0 ? void 0 : _b.ensure());
|
|
30
|
+
}
|
|
31
|
+
setup() {
|
|
32
|
+
const program = this.options.locate();
|
|
33
|
+
return program ? this.options.setup(program, this.options.platform) : undefined;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export default function activate() {
|
|
37
|
+
return new ComputerUseHost();
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=host.js.map
|
package/host/index.mjs
ADDED
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
// src/cua-driver.ts
|
|
2
|
+
import { statSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
var CuaDriver;
|
|
6
|
+
((CuaDriver) => {
|
|
7
|
+
CuaDriver.SERVER = "cua-driver";
|
|
8
|
+
CuaDriver.NAMESPACE = "mcp__cua_driver";
|
|
9
|
+
CuaDriver.SECTION = "computer_use";
|
|
10
|
+
CuaDriver.BUNDLED_ENV = "TA_CUA_DRIVER";
|
|
11
|
+
function prompt(platform) {
|
|
12
|
+
return [
|
|
13
|
+
"Cua Driver lets you use the desktop apps of this computer in the background, without taking the user's pointer or keyboard: list apps and windows, read a window's accessibility tree, click, type, scroll and take screenshots.",
|
|
14
|
+
`Its tools are \`tools.${CuaDriver.NAMESPACE}__*\` in codemode scripts. It starts the first time a script names \`${CuaDriver.NAMESPACE}\`: begin with \`describeNamespace("${CuaDriver.NAMESPACE}")\` for its tools and how to use them.`,
|
|
15
|
+
"Use it only when a task needs a desktop app, not for files or commands.",
|
|
16
|
+
...PLATFORM_NOTES[platform] ? [PLATFORM_NOTES[platform]] : []
|
|
17
|
+
].join(`
|
|
18
|
+
`);
|
|
19
|
+
}
|
|
20
|
+
CuaDriver.prompt = prompt;
|
|
21
|
+
const PLATFORM_NOTES = {
|
|
22
|
+
darwin: "If a call waits for the permissions of macOS, Ta has asked the user for them; do not ask again.",
|
|
23
|
+
win32: "Windows does not let it control the windows of apps run as administrator unless Ta runs as administrator too.",
|
|
24
|
+
linux: "It needs the desktop session of the user (X11, or Wayland with XWayland) and its accessibility bus (AT-SPI)."
|
|
25
|
+
};
|
|
26
|
+
CuaDriver.BUNDLED_ENV_VARS = { CUA_DRIVER_RS_TELEMETRY_ENABLED: "0", CUA_DRIVER_RS_UPDATE_CHECK: "0" };
|
|
27
|
+
CuaDriver.DESCRIPTION = "Use the desktop apps of this computer in the background (Cua Driver)";
|
|
28
|
+
CuaDriver.INSTALL = {
|
|
29
|
+
posix: '/bin/bash -c "$(curl -fsSL https://cua.ai/driver/install.sh)"',
|
|
30
|
+
win32: "irm https://cua.ai/driver/install.ps1 | iex"
|
|
31
|
+
};
|
|
32
|
+
function locate(host = currentHost()) {
|
|
33
|
+
if (host.bundled && host.isFile(host.bundled)) {
|
|
34
|
+
return { path: host.bundled, bundled: true };
|
|
35
|
+
}
|
|
36
|
+
const windows = host.platform === "win32";
|
|
37
|
+
const name = windows ? "cua-driver.exe" : "cua-driver";
|
|
38
|
+
const dirs = (host.path ?? "").split(windows ? ";" : ":").filter(Boolean);
|
|
39
|
+
const candidates = [...dirs.map((dir) => join(dir, name)), join(host.home, ".local", "bin", name)];
|
|
40
|
+
if (host.platform === "darwin") {
|
|
41
|
+
const app = "CuaDriver.app/Contents/MacOS/cua-driver";
|
|
42
|
+
candidates.push(join("/Applications", app), join(host.home, "Applications", app));
|
|
43
|
+
}
|
|
44
|
+
const own = candidates.find((candidate) => host.isFile(candidate));
|
|
45
|
+
return own ? { path: own, bundled: false } : undefined;
|
|
46
|
+
}
|
|
47
|
+
CuaDriver.locate = locate;
|
|
48
|
+
function appOf(program) {
|
|
49
|
+
const inside = "/Contents/MacOS/cua-driver";
|
|
50
|
+
const app = program.slice(0, -inside.length);
|
|
51
|
+
return program.endsWith(inside) && app.endsWith(".app") ? app : undefined;
|
|
52
|
+
}
|
|
53
|
+
CuaDriver.appOf = appOf;
|
|
54
|
+
function reaches(reach, input) {
|
|
55
|
+
if (reach === undefined) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
if (reach === "all") {
|
|
59
|
+
return JSON.stringify(input ?? null).includes(CuaDriver.NAMESPACE);
|
|
60
|
+
}
|
|
61
|
+
return reach.some((word) => word.includes(CuaDriver.NAMESPACE));
|
|
62
|
+
}
|
|
63
|
+
CuaDriver.reaches = reaches;
|
|
64
|
+
function status(program, platform) {
|
|
65
|
+
if (program) {
|
|
66
|
+
const which = program.bundled ? `the one that comes with Ta Desktop (${program.path})` : program.path;
|
|
67
|
+
return `Cua Driver: ${which}. Ta starts it the first time it uses a desktop app, and checks what the system needs then.`;
|
|
68
|
+
}
|
|
69
|
+
const install = platform === "win32" ? CuaDriver.INSTALL.win32 : CuaDriver.INSTALL.posix;
|
|
70
|
+
return `Ta uses desktop apps through Cua Driver, which is not installed. Install it with \`${install}\`, or use Ta Desktop, which comes with it; Ta finds it on the next message, with no restart.`;
|
|
71
|
+
}
|
|
72
|
+
CuaDriver.status = status;
|
|
73
|
+
function currentHost() {
|
|
74
|
+
return { platform: process.platform, path: process.env.PATH, home: homedir(), bundled: process.env[CuaDriver.BUNDLED_ENV], isFile };
|
|
75
|
+
}
|
|
76
|
+
function isFile(path) {
|
|
77
|
+
try {
|
|
78
|
+
return statSync(path).isFile();
|
|
79
|
+
} catch {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
})(CuaDriver ||= {});
|
|
84
|
+
|
|
85
|
+
// src/extension.ts
|
|
86
|
+
import { execFile as execFile3 } from "node:child_process";
|
|
87
|
+
import { realpathSync } from "node:fs";
|
|
88
|
+
import { join as join3 } from "node:path";
|
|
89
|
+
|
|
90
|
+
// src/cua-driver-access.ts
|
|
91
|
+
import { execFile } from "node:child_process";
|
|
92
|
+
function ofLanguage(tag) {
|
|
93
|
+
if (/^zh/i.test(tag)) {
|
|
94
|
+
return /(?:tw|hk|mo|hant)/i.test(tag) ? "zh-TW" : "zh-CN";
|
|
95
|
+
}
|
|
96
|
+
if (/^ja/i.test(tag)) {
|
|
97
|
+
return "ja-JP";
|
|
98
|
+
}
|
|
99
|
+
return "en-US";
|
|
100
|
+
}
|
|
101
|
+
var CHECK_TIMEOUT_MS = 5000;
|
|
102
|
+
var DOCTOR_TIMEOUT_MS = 15000;
|
|
103
|
+
var POLL_MS = 1000;
|
|
104
|
+
var GRACE_MS = 3000;
|
|
105
|
+
var GRACE_POLL_MS = 100;
|
|
106
|
+
var WAIT_MS = 3 * 60 * 1000;
|
|
107
|
+
var DESKTOP_PROBES = /display|x11|wayland|at-spi|accessib|session|desktop|automation|uia/i;
|
|
108
|
+
var MISSING_LIBRARY = /error while loading shared libraries: (\S+?):/;
|
|
109
|
+
function askMac(env = process.env) {
|
|
110
|
+
const language = ofLanguage(env.TA_LANGUAGE || env.LC_ALL || env.LANG || "");
|
|
111
|
+
return language === "zh-CN" ? "Ta 正在请 macOS 允许 Cua Driver 使用这台 Mac:请在 Cua Driver 打开的窗口或“系统设置”里,打开“辅助功能”(用于点击和输入)和“屏幕录制”(用于查看窗口)中的 Cua Driver。开好后 Ta 会自动继续;点“停止”可放弃。" : language === "zh-TW" ? "Ta 正在請 macOS 允許 Cua Driver 使用這台 Mac:請在 Cua Driver 開啟的視窗或「系統設定」裡,開啟「輔助功能」(用於點選和輸入)和「螢幕錄製」(用於檢視視窗)中的 Cua Driver。開好後 Ta 會自動繼續;點「停止」可放棄。" : language === "ja-JP" ? "Ta は macOS に Cua Driver がこの Mac を使用することを許可するよう要求しています。Cua Driver が開いたウィンドウまたは「システム設定」で、「アクセシビリティ」(クリックと入力用)と「画面録画」(ウィンドウ表示用)の Cua Driver をオンにしてください。有効にした後、Ta は自動的に続行します。「停止」をクリックすると中止できます。" : "Ta is asking macOS to let Cua Driver use this Mac: turn Cua Driver on under Accessibility (to click and type) and Screen Recording (to see windows), in the window Cua Driver opened or in System Settings. Ta goes on by itself once you have; Stop gives up.";
|
|
112
|
+
}
|
|
113
|
+
var NO_ACCESSIBILITY = "Cua Driver has no Accessibility permission of macOS, which it needs to read and use apps: ask the user to turn Cua Driver on in System Settings → Privacy & Security → Accessibility (`/computer-use` in Ta opens it), then try again.";
|
|
114
|
+
var NO_SCREEN_RECORDING = "Cua Driver has no Screen Recording permission of macOS: it reads and uses apps, but takes no screenshots. Turn it on in System Settings → Privacy & Security → Screen & System Audio Recording.";
|
|
115
|
+
|
|
116
|
+
class CuaDriverAccess {
|
|
117
|
+
program;
|
|
118
|
+
options;
|
|
119
|
+
run;
|
|
120
|
+
constructor(program, options) {
|
|
121
|
+
this.program = program;
|
|
122
|
+
this.options = options;
|
|
123
|
+
this.run = options.run ?? runProgram;
|
|
124
|
+
}
|
|
125
|
+
check(notify, signal) {
|
|
126
|
+
return this.options.platform === "darwin" ? this.checkMac(notify, signal) : this.checkDesktop();
|
|
127
|
+
}
|
|
128
|
+
async permissions() {
|
|
129
|
+
const socket = this.options.socket ? ["--socket", this.options.socket] : [];
|
|
130
|
+
const result = await this.run(this.program, ["call", "check_permissions", '{"prompt":false}', ...socket], CHECK_TIMEOUT_MS, this.options.env);
|
|
131
|
+
return CuaDriverAccess.parsePermissions(result);
|
|
132
|
+
}
|
|
133
|
+
async checkMac(notify, signal) {
|
|
134
|
+
let state = await this.permissions();
|
|
135
|
+
const grace = Date.now() + (this.options.graceMs ?? GRACE_MS);
|
|
136
|
+
while (state?.pending && Date.now() < grace && !signal?.aborted) {
|
|
137
|
+
await sleep(GRACE_POLL_MS, signal);
|
|
138
|
+
state = await this.permissions();
|
|
139
|
+
}
|
|
140
|
+
if (!state || state.accessibility && state.screenRecording) {
|
|
141
|
+
return { ready: true, notes: [], known: state !== undefined };
|
|
142
|
+
}
|
|
143
|
+
notify(askMac());
|
|
144
|
+
if (!state.pending) {
|
|
145
|
+
this.options.openSettings?.(state.accessibility ? "Privacy_ScreenCapture" : "Privacy_Accessibility");
|
|
146
|
+
}
|
|
147
|
+
const deadline = Date.now() + (this.options.waitMs ?? WAIT_MS);
|
|
148
|
+
while (!signal?.aborted && Date.now() < deadline && !(state?.accessibility && (state.screenRecording || !state.pending))) {
|
|
149
|
+
await sleep(this.options.pollMs ?? POLL_MS, signal);
|
|
150
|
+
await this.options.beforeLook?.();
|
|
151
|
+
state = await this.permissions() ?? state;
|
|
152
|
+
}
|
|
153
|
+
if (state?.accessibility) {
|
|
154
|
+
return { ready: true, notes: state.screenRecording ? [] : [NO_SCREEN_RECORDING], known: true };
|
|
155
|
+
}
|
|
156
|
+
return { ready: false, reason: signal?.aborted ? "Stopped while waiting for the permissions of macOS." : NO_ACCESSIBILITY };
|
|
157
|
+
}
|
|
158
|
+
async checkDesktop() {
|
|
159
|
+
const result = await this.run(this.program, ["doctor", "--json"], DOCTOR_TIMEOUT_MS, this.options.env);
|
|
160
|
+
return CuaDriverAccess.parseDoctor(result, this.options.platform);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
((CuaDriverAccess) => {
|
|
164
|
+
function parsePermissions(result) {
|
|
165
|
+
if (`${result.stdout}${result.stderr}`.includes("permissions_pending")) {
|
|
166
|
+
return { accessibility: false, screenRecording: false, pending: true };
|
|
167
|
+
}
|
|
168
|
+
try {
|
|
169
|
+
const answer = JSON.parse(result.stdout);
|
|
170
|
+
if (typeof answer.accessibility !== "boolean") {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
return { accessibility: answer.accessibility, screenRecording: answer.screen_recording === true, pending: false };
|
|
174
|
+
} catch {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
CuaDriverAccess.parsePermissions = parsePermissions;
|
|
179
|
+
function parseDoctor(result, platform) {
|
|
180
|
+
const library = MISSING_LIBRARY.exec(result.stderr)?.[1];
|
|
181
|
+
if (library) {
|
|
182
|
+
const install = platform === "linux" ? " On Debian or Ubuntu: `sudo apt install libx11-6 libxi6 libxkbcommon0 at-spi2-core`." : "";
|
|
183
|
+
return { ready: false, reason: `Cua Driver cannot start: the system has no ${library}.${install}` };
|
|
184
|
+
}
|
|
185
|
+
let probes;
|
|
186
|
+
try {
|
|
187
|
+
probes = JSON.parse(result.stdout).probes ?? [];
|
|
188
|
+
} catch {
|
|
189
|
+
return { ready: true, notes: [], known: true };
|
|
190
|
+
}
|
|
191
|
+
const text = (probe) => [`${probe.label}: ${probe.message}`, probe.detail].filter(Boolean).join(" — ");
|
|
192
|
+
const failed = probes.filter((probe) => probe.status === "error" || probe.status === "warn" && /display server/i.test(probe.label ?? ""));
|
|
193
|
+
if (failed.length > 0) {
|
|
194
|
+
return { ready: false, reason: `Cua Driver cannot use the desktop: ${failed.map(text).join("; ")}` };
|
|
195
|
+
}
|
|
196
|
+
const warned = probes.filter((probe) => probe.status === "warn" && DESKTOP_PROBES.test(probe.label ?? ""));
|
|
197
|
+
return { ready: true, notes: warned.map((probe) => `Cua Driver: ${text(probe)}`), known: true };
|
|
198
|
+
}
|
|
199
|
+
CuaDriverAccess.parseDoctor = parseDoctor;
|
|
200
|
+
})(CuaDriverAccess ||= {});
|
|
201
|
+
function runProgram(program, args, timeoutMs, env) {
|
|
202
|
+
return new Promise((resolve) => {
|
|
203
|
+
execFile(program, args, { timeout: timeoutMs, maxBuffer: 4 * 1024 * 1024, env: { ...process.env, ...env } }, (error, stdout, stderr) => {
|
|
204
|
+
const code = error ? typeof error.code === "number" ? error.code : -1 : 0;
|
|
205
|
+
resolve({ code, stdout: String(stdout), stderr: String(stderr) });
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
function sleep(ms, signal) {
|
|
210
|
+
return new Promise((resolve) => {
|
|
211
|
+
const timer = setTimeout(done, ms);
|
|
212
|
+
signal?.addEventListener("abort", done, { once: true });
|
|
213
|
+
function done() {
|
|
214
|
+
clearTimeout(timer);
|
|
215
|
+
signal?.removeEventListener("abort", done);
|
|
216
|
+
resolve();
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// src/cua-driver-daemon.ts
|
|
222
|
+
import { execFile as execFile2 } from "node:child_process";
|
|
223
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync } from "node:fs";
|
|
224
|
+
import { connect } from "node:net";
|
|
225
|
+
import { homedir as homedir2 } from "node:os";
|
|
226
|
+
import { dirname, join as join2 } from "node:path";
|
|
227
|
+
var START_TIMEOUT_MS = 1e4;
|
|
228
|
+
var POLL_MS2 = 20;
|
|
229
|
+
var IDLE_MS = 10 * 60 * 1000;
|
|
230
|
+
|
|
231
|
+
class CuaDriverDaemon {
|
|
232
|
+
source;
|
|
233
|
+
options;
|
|
234
|
+
socket;
|
|
235
|
+
pidFile;
|
|
236
|
+
dir;
|
|
237
|
+
app;
|
|
238
|
+
starting;
|
|
239
|
+
launched = false;
|
|
240
|
+
verified = false;
|
|
241
|
+
idle;
|
|
242
|
+
launch;
|
|
243
|
+
constructor(source, options = {}) {
|
|
244
|
+
this.source = source;
|
|
245
|
+
this.options = options;
|
|
246
|
+
this.dir = options.dir ?? join2(homedir2(), "Library", "Caches", "ta");
|
|
247
|
+
this.app = CuaDriverDaemon.nested(source) ? join2(this.stageDir, CuaDriverDaemon.version(source), "CuaDriver.app") : source;
|
|
248
|
+
this.socket = join2(this.dir, "cua-driver.sock");
|
|
249
|
+
this.pidFile = join2(this.dir, "cua-driver.pid");
|
|
250
|
+
this.launch = options.launch ?? openApp;
|
|
251
|
+
}
|
|
252
|
+
async ensure() {
|
|
253
|
+
if (await this.alive() && (this.verified || await this.adopt())) {
|
|
254
|
+
this.touch();
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
clearTimeout(this.idle);
|
|
258
|
+
this.starting ??= this.start().finally(() => {
|
|
259
|
+
this.starting = undefined;
|
|
260
|
+
});
|
|
261
|
+
await this.starting;
|
|
262
|
+
this.touch();
|
|
263
|
+
}
|
|
264
|
+
alive() {
|
|
265
|
+
if (!existsSync(this.socket)) {
|
|
266
|
+
return Promise.resolve(false);
|
|
267
|
+
}
|
|
268
|
+
return new Promise((resolve) => {
|
|
269
|
+
const socket = connect(this.socket);
|
|
270
|
+
socket.once("connect", () => {
|
|
271
|
+
socket.destroy();
|
|
272
|
+
resolve(true);
|
|
273
|
+
});
|
|
274
|
+
socket.on("error", () => resolve(false));
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
stop() {
|
|
278
|
+
clearTimeout(this.idle);
|
|
279
|
+
if (!this.launched) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
this.launched = false;
|
|
283
|
+
const pid = this.pid();
|
|
284
|
+
if (pid) {
|
|
285
|
+
try {
|
|
286
|
+
process.kill(pid, "SIGTERM");
|
|
287
|
+
} catch {}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
async reap() {
|
|
291
|
+
const program = this.program;
|
|
292
|
+
const own = this.pid();
|
|
293
|
+
const list = await new Promise((resolve) => execFile2("ps", ["-axo", "pid=,command="], (_error, stdout) => resolve(String(stdout ?? ""))));
|
|
294
|
+
for (const line of list.split(`
|
|
295
|
+
`)) {
|
|
296
|
+
const [, pid, command] = /^\s*(\d+)\s+(.*)$/.exec(line) ?? [];
|
|
297
|
+
if (pid && Number(pid) !== own && command.startsWith(`${program} serve`) && !command.includes(this.socket)) {
|
|
298
|
+
try {
|
|
299
|
+
process.kill(Number(pid), "SIGTERM");
|
|
300
|
+
} catch {}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
async adopt() {
|
|
305
|
+
const running = await this.identify();
|
|
306
|
+
if (!running || running.executable === this.program) {
|
|
307
|
+
this.verified = true;
|
|
308
|
+
return true;
|
|
309
|
+
}
|
|
310
|
+
try {
|
|
311
|
+
process.kill(running.pid, "SIGTERM");
|
|
312
|
+
} catch {}
|
|
313
|
+
return false;
|
|
314
|
+
}
|
|
315
|
+
identify() {
|
|
316
|
+
return new Promise((resolve) => execFile2(this.program, ["call", "check_permissions", '{"prompt":false}', "--socket", this.socket], { timeout: 5000, env: { ...process.env, ...this.options.env } }, (_error, stdout) => {
|
|
317
|
+
try {
|
|
318
|
+
const { source } = JSON.parse(String(stdout));
|
|
319
|
+
resolve(source?.executable && source.pid ? { executable: source.executable, pid: source.pid } : undefined);
|
|
320
|
+
} catch {
|
|
321
|
+
resolve(undefined);
|
|
322
|
+
}
|
|
323
|
+
}));
|
|
324
|
+
}
|
|
325
|
+
get program() {
|
|
326
|
+
return join2(this.app, "Contents", "MacOS", "cua-driver");
|
|
327
|
+
}
|
|
328
|
+
async verify() {
|
|
329
|
+
const pid = this.pid();
|
|
330
|
+
const command = pid ? await this.commandOf(pid) : undefined;
|
|
331
|
+
const program = this.program;
|
|
332
|
+
if (command === undefined || command.includes(`${program} serve`)) {
|
|
333
|
+
this.verified = true;
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
this.stop();
|
|
337
|
+
throw new Error(`macOS started another copy of CuaDriver.app (${command.split(" serve")[0]}). Run \`${program} doctor --fix\`, which makes macOS forget the other copies, then try again.`);
|
|
338
|
+
}
|
|
339
|
+
commandOf(pid) {
|
|
340
|
+
return new Promise((resolve) => execFile2("ps", ["-o", "command=", "-p", String(pid)], (error, stdout) => resolve(error ? undefined : String(stdout).trim())));
|
|
341
|
+
}
|
|
342
|
+
touch() {
|
|
343
|
+
clearTimeout(this.idle);
|
|
344
|
+
this.idle = setTimeout(() => this.stop(), this.options.idleMs ?? IDLE_MS);
|
|
345
|
+
this.idle.unref?.();
|
|
346
|
+
}
|
|
347
|
+
pid() {
|
|
348
|
+
try {
|
|
349
|
+
return Number(readFileSync(this.pidFile, "utf8").trim()) || undefined;
|
|
350
|
+
} catch {
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
get stageDir() {
|
|
355
|
+
return this.options.stageDir ?? join2(homedir2(), "Library", "Application Support", "Ta", "cua-driver");
|
|
356
|
+
}
|
|
357
|
+
async stage() {
|
|
358
|
+
if (this.app === this.source || existsSync(this.app)) {
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
const versions = dirname(dirname(this.app));
|
|
362
|
+
const temp = `${dirname(this.app)}.part`;
|
|
363
|
+
rmSync(temp, { recursive: true, force: true });
|
|
364
|
+
mkdirSync(temp, { recursive: true });
|
|
365
|
+
await run("cp", ["-cR", this.source, temp]).catch(() => run("cp", ["-R", this.source, temp]));
|
|
366
|
+
rmSync(dirname(this.app), { recursive: true, force: true });
|
|
367
|
+
renameSync(temp, dirname(this.app));
|
|
368
|
+
for (const name of readdirSync(versions)) {
|
|
369
|
+
if (join2(versions, name) !== dirname(this.app)) {
|
|
370
|
+
rmSync(join2(versions, name), { recursive: true, force: true });
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
async start() {
|
|
375
|
+
await this.stage();
|
|
376
|
+
mkdirSync(this.dir, { recursive: true });
|
|
377
|
+
rmSync(this.socket, { force: true });
|
|
378
|
+
this.verified = false;
|
|
379
|
+
await this.launch(this.app, ["serve", "--socket", this.socket, "--pid-file", this.pidFile], this.options.env ?? {});
|
|
380
|
+
if (!this.launched) {
|
|
381
|
+
this.launched = true;
|
|
382
|
+
process.once("exit", () => this.stop());
|
|
383
|
+
}
|
|
384
|
+
const deadline = Date.now() + START_TIMEOUT_MS;
|
|
385
|
+
while (Date.now() < deadline) {
|
|
386
|
+
if (await this.alive()) {
|
|
387
|
+
return this.verify();
|
|
388
|
+
}
|
|
389
|
+
await new Promise((resolve) => setTimeout(resolve, POLL_MS2));
|
|
390
|
+
}
|
|
391
|
+
throw new Error(`Cua Driver did not start in ${START_TIMEOUT_MS / 1000} s: run \`${join2(this.app, "Contents/MacOS/cua-driver")} doctor\`.`);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
((CuaDriverDaemon) => {
|
|
395
|
+
function nested(app) {
|
|
396
|
+
return /\.app\//.test(dirname(app));
|
|
397
|
+
}
|
|
398
|
+
CuaDriverDaemon.nested = nested;
|
|
399
|
+
function version(app) {
|
|
400
|
+
try {
|
|
401
|
+
return /<key>CFBundleVersion<\/key>\s*<string>([^<]+)<\/string>/.exec(readFileSync(join2(app, "Contents", "Info.plist"), "utf8"))?.[1] ?? "current";
|
|
402
|
+
} catch {
|
|
403
|
+
return "current";
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
CuaDriverDaemon.version = version;
|
|
407
|
+
})(CuaDriverDaemon ||= {});
|
|
408
|
+
var LSREGISTER = "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister";
|
|
409
|
+
async function openApp(app, args, env) {
|
|
410
|
+
await run(LSREGISTER, ["-f", app]).catch(() => {
|
|
411
|
+
return;
|
|
412
|
+
});
|
|
413
|
+
const vars = Object.entries(env).flatMap(([name, value]) => ["--env", `${name}=${value}`]);
|
|
414
|
+
await run("open", ["-g", "-n", "-a", app, ...vars, "--args", ...args]);
|
|
415
|
+
}
|
|
416
|
+
function run(program, args) {
|
|
417
|
+
return new Promise((resolve, reject) => execFile2(program, args, (error, stdout) => error ? reject(error) : resolve(String(stdout))));
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// src/extension.ts
|
|
421
|
+
var setups = new Map;
|
|
422
|
+
function setupOf(found, platform) {
|
|
423
|
+
const program = { ...found, path: realPath(found.path) };
|
|
424
|
+
let setup = setups.get(program.path);
|
|
425
|
+
if (!setup) {
|
|
426
|
+
const env = program.bundled ? { ...CuaDriver.BUNDLED_ENV_VARS } : undefined;
|
|
427
|
+
const app = program.bundled && platform === "darwin" ? CuaDriver.appOf(program.path) : undefined;
|
|
428
|
+
const daemon = app ? new CuaDriverDaemon(app, { env }) : undefined;
|
|
429
|
+
const access = new CuaDriverAccess(daemon ? join3(daemon.app, "Contents", "MacOS", "cua-driver") : program.path, {
|
|
430
|
+
platform,
|
|
431
|
+
socket: daemon?.socket,
|
|
432
|
+
env,
|
|
433
|
+
beforeLook: daemon ? () => daemon.ensure() : undefined,
|
|
434
|
+
openSettings: (pane) => execFile3("open", [`x-apple.systempreferences:com.apple.preference.security?${pane}`], () => {
|
|
435
|
+
return;
|
|
436
|
+
})
|
|
437
|
+
});
|
|
438
|
+
const args = daemon ? ["mcp", "--embedded", "--socket", daemon.socket] : ["mcp"];
|
|
439
|
+
setup = { program, daemon, access, args, env, ready: false };
|
|
440
|
+
setups.set(program.path, setup);
|
|
441
|
+
}
|
|
442
|
+
return setup;
|
|
443
|
+
}
|
|
444
|
+
function realPath(path) {
|
|
445
|
+
try {
|
|
446
|
+
return realpathSync(path);
|
|
447
|
+
} catch {
|
|
448
|
+
return path;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
var DEFAULTS = { locate: () => CuaDriver.locate(), platform: process.platform, setup: setupOf };
|
|
452
|
+
|
|
453
|
+
// src/host.ts
|
|
454
|
+
var DEFAULTS2 = { locate: () => CuaDriver.locate(), platform: process.platform, setup: setupOf };
|
|
455
|
+
|
|
456
|
+
class ComputerUseHost {
|
|
457
|
+
options;
|
|
458
|
+
constructor(options = DEFAULTS2) {
|
|
459
|
+
this.options = options;
|
|
460
|
+
}
|
|
461
|
+
driver() {
|
|
462
|
+
const setup = this.setup();
|
|
463
|
+
if (!setup) {
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
const daemon = setup.daemon;
|
|
467
|
+
return {
|
|
468
|
+
program: daemon ? `${daemon.app}/Contents/MacOS/cua-driver` : setup.program.path,
|
|
469
|
+
...daemon ? { socket: daemon.socket } : {},
|
|
470
|
+
...setup.env ? { env: { ...setup.env } } : {}
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
async ensureDriver() {
|
|
474
|
+
await this.setup()?.daemon?.ensure();
|
|
475
|
+
}
|
|
476
|
+
setup() {
|
|
477
|
+
const program = this.options.locate();
|
|
478
|
+
return program ? this.options.setup(program, this.options.platform) : undefined;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
function activate() {
|
|
482
|
+
return new ComputerUseHost;
|
|
483
|
+
}
|
|
484
|
+
export {
|
|
485
|
+
activate as default,
|
|
486
|
+
ComputerUseHost
|
|
487
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/ta-computer-use",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.43.0",
|
|
4
4
|
"description": "A pi package for Ta: the desktop apps of this computer, used in the background through Cua Driver, which starts only when the agent first needs it",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package"
|
|
@@ -16,12 +16,13 @@
|
|
|
16
16
|
"license": "MIT",
|
|
17
17
|
"files": [
|
|
18
18
|
"esm",
|
|
19
|
+
"host",
|
|
19
20
|
"extension",
|
|
20
21
|
"!**/*.map"
|
|
21
22
|
],
|
|
22
23
|
"scripts": {
|
|
23
|
-
"build": "tsc -b && bun ../../scripts/build-esm.ts && bun build src/extension.ts --format esm --target node --outfile extension/index.mjs",
|
|
24
|
-
"clean": "rm -rf lib extension *.tsbuildinfo",
|
|
24
|
+
"build": "tsc -b && bun ../../scripts/build-esm.ts && bun build src/extension.ts --format esm --target node --outfile extension/index.mjs && bun build src/host.ts --format esm --target node --outfile host/index.mjs",
|
|
25
|
+
"clean": "rm -rf lib host extension *.tsbuildinfo",
|
|
25
26
|
"test": "bun test ./lib",
|
|
26
27
|
"watch": "tsc -b -w --preserveWatchOutput"
|
|
27
28
|
},
|
|
@@ -50,6 +51,7 @@
|
|
|
50
51
|
}
|
|
51
52
|
},
|
|
52
53
|
"icon": "MousePointerClick",
|
|
54
|
+
"host": "./host/index.mjs",
|
|
53
55
|
"extensions": [
|
|
54
56
|
"./extension/index.mjs"
|
|
55
57
|
],
|