@celljs/ta-computer-use 4.35.0 → 4.36.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/esm/cua-driver-access.d.ts +71 -0
- package/esm/cua-driver-access.js +162 -0
- package/esm/cua-driver-daemon.d.ts +77 -0
- package/esm/cua-driver-daemon.js +246 -0
- package/esm/cua-driver.d.ts +55 -0
- package/esm/cua-driver.js +109 -0
- package/esm/extension.d.ts +38 -0
- package/esm/extension.js +169 -0
- package/esm/package.json +3 -0
- package/esm/pi-types.d.ts +2 -0
- package/esm/pi-types.js +2 -0
- package/package.json +2 -2
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/** What a run of the program gave; `code` is -1 when it could not run or took too long. */
|
|
2
|
+
export interface CuaDriverRun {
|
|
3
|
+
code: number;
|
|
4
|
+
stdout: string;
|
|
5
|
+
stderr: string;
|
|
6
|
+
}
|
|
7
|
+
export type CuaDriverRunner = (program: string, args: string[], timeoutMs: number, env?: Readonly<Record<string, string>>) => Promise<CuaDriverRun>;
|
|
8
|
+
/**
|
|
9
|
+
* Whether Ta may go on with a call that uses Cua Driver; `notes` are for the user, once. `known` is false
|
|
10
|
+
* when the system could not tell (the daemon of the user's own install is not running yet): ask again next time.
|
|
11
|
+
*/
|
|
12
|
+
export type CuaDriverReadiness = {
|
|
13
|
+
ready: true;
|
|
14
|
+
notes: string[];
|
|
15
|
+
known: boolean;
|
|
16
|
+
} | {
|
|
17
|
+
ready: false;
|
|
18
|
+
reason: string;
|
|
19
|
+
};
|
|
20
|
+
/** The two permissions of macOS, as `check_permissions` answers; `pending` while its guide is still open. */
|
|
21
|
+
export interface MacPermissions {
|
|
22
|
+
accessibility: boolean;
|
|
23
|
+
screenRecording: boolean;
|
|
24
|
+
pending: boolean;
|
|
25
|
+
}
|
|
26
|
+
export interface CuaDriverAccessOptions {
|
|
27
|
+
platform: NodeJS.Platform;
|
|
28
|
+
/** The socket of the daemon to ask, on macOS; without one, the program's default daemon. */
|
|
29
|
+
socket?: string;
|
|
30
|
+
/** Added to the environment of each run. */
|
|
31
|
+
env?: Readonly<Record<string, string>>;
|
|
32
|
+
run?: CuaDriverRunner;
|
|
33
|
+
/** Opens a pane of System Settings, on macOS. */
|
|
34
|
+
openSettings?: (pane: 'Privacy_Accessibility' | 'Privacy_ScreenCapture') => void;
|
|
35
|
+
/** Before each look while waiting, on macOS: the daemon of Ta Desktop restarts if it quit for a permission. */
|
|
36
|
+
beforeLook?: () => Promise<void>;
|
|
37
|
+
pollMs?: number;
|
|
38
|
+
waitMs?: number;
|
|
39
|
+
graceMs?: number;
|
|
40
|
+
}
|
|
41
|
+
/** What the user reads while Ta waits for the permissions: in the language of Ta's pages (`TA_LANGUAGE`) or the computer's. */
|
|
42
|
+
export declare function askMac(env?: NodeJS.ProcessEnv): string;
|
|
43
|
+
export declare const 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 \u2192 Privacy & Security \u2192 Accessibility (`/computer-use` in Ta opens it), then try again.";
|
|
44
|
+
export declare const 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 \u2192 Privacy & Security \u2192 Screen & System Audio Recording.";
|
|
45
|
+
/**
|
|
46
|
+
* Checks what the system needs before Ta first uses Cua Driver, and asks the user for it, so that a
|
|
47
|
+
* missing permission is something the user is told how to fix and not a failed call.
|
|
48
|
+
* - macOS: the Accessibility and Screen Recording permissions of CuaDriver.app. A daemon without them
|
|
49
|
+
* opens its own guide; Ta tells the user, waits while they turn the switches on, and goes on.
|
|
50
|
+
* - Linux and Windows: no permission, but a desktop session; Linux also needs the accessibility bus and
|
|
51
|
+
* some X11 libraries. `cua-driver doctor` says what is missing.
|
|
52
|
+
*/
|
|
53
|
+
export declare class CuaDriverAccess {
|
|
54
|
+
readonly program: string;
|
|
55
|
+
protected readonly options: CuaDriverAccessOptions;
|
|
56
|
+
protected readonly run: CuaDriverRunner;
|
|
57
|
+
constructor(program: string, options: CuaDriverAccessOptions);
|
|
58
|
+
check(notify: (message: string) => void, signal?: AbortSignal): Promise<CuaDriverReadiness>;
|
|
59
|
+
/** What the daemon says; undefined when it cannot tell, which does not stop Ta. */
|
|
60
|
+
permissions(): Promise<MacPermissions | undefined>;
|
|
61
|
+
protected checkMac(notify: (message: string) => void, signal?: AbortSignal): Promise<CuaDriverReadiness>;
|
|
62
|
+
protected checkDesktop(): Promise<CuaDriverReadiness>;
|
|
63
|
+
}
|
|
64
|
+
export declare namespace CuaDriverAccess {
|
|
65
|
+
function parsePermissions(result: CuaDriverRun): MacPermissions | undefined;
|
|
66
|
+
/**
|
|
67
|
+
* Not ready when the program cannot start (a library is missing), when a probe fails, or when there is
|
|
68
|
+
* no desktop session; the other warnings about the desktop are notes. A report it cannot read does not stop Ta.
|
|
69
|
+
*/
|
|
70
|
+
function parseDoctor(result: CuaDriverRun, platform: NodeJS.Platform): CuaDriverReadiness;
|
|
71
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
/**
|
|
3
|
+
* Resolves a language tag to one of the four supported languages.
|
|
4
|
+
* Rules: zh with -tw/-hk/-mo/-hant suffix → zh-TW; other zh → zh-CN; ja → ja-JP; others → en-US.
|
|
5
|
+
*/
|
|
6
|
+
function ofLanguage(tag) {
|
|
7
|
+
if (/^zh/i.test(tag)) {
|
|
8
|
+
return /(?:tw|hk|mo|hant)/i.test(tag) ? 'zh-TW' : 'zh-CN';
|
|
9
|
+
}
|
|
10
|
+
if (/^ja/i.test(tag)) {
|
|
11
|
+
return 'ja-JP';
|
|
12
|
+
}
|
|
13
|
+
return 'en-US';
|
|
14
|
+
}
|
|
15
|
+
const CHECK_TIMEOUT_MS = 5000;
|
|
16
|
+
const DOCTOR_TIMEOUT_MS = 15000;
|
|
17
|
+
const POLL_MS = 1000;
|
|
18
|
+
/** A daemon that just started answers permissions_pending for a few hundred ms even when it has both. */
|
|
19
|
+
const GRACE_MS = 3000;
|
|
20
|
+
const GRACE_POLL_MS = 100;
|
|
21
|
+
/** How long a call waits for the user to give the permissions of macOS. */
|
|
22
|
+
const WAIT_MS = 3 * 60 * 1000;
|
|
23
|
+
/** The probes of `cua-driver doctor` about reaching the desktop, which the user can fix. */
|
|
24
|
+
const DESKTOP_PROBES = /display|x11|wayland|at-spi|accessib|session|desktop|automation|uia/i;
|
|
25
|
+
const MISSING_LIBRARY = /error while loading shared libraries: (\S+?):/;
|
|
26
|
+
/** What the user reads while Ta waits for the permissions: in the language of Ta's pages (`TA_LANGUAGE`) or the computer's. */
|
|
27
|
+
export function askMac(env = process.env) {
|
|
28
|
+
const language = ofLanguage(env.TA_LANGUAGE || env.LC_ALL || env.LANG || '');
|
|
29
|
+
return language === 'zh-CN'
|
|
30
|
+
? 'Ta 正在请 macOS 允许 Cua Driver 使用这台 Mac:请在 Cua Driver 打开的窗口或“系统设置”里,打开“辅助功能”(用于点击和输入)和“屏幕录制”(用于查看窗口)中的 Cua Driver。开好后 Ta 会自动继续;点“停止”可放弃。'
|
|
31
|
+
: language === 'zh-TW'
|
|
32
|
+
? 'Ta 正在請 macOS 允許 Cua Driver 使用這台 Mac:請在 Cua Driver 開啟的視窗或「系統設定」裡,開啟「輔助功能」(用於點選和輸入)和「螢幕錄製」(用於檢視視窗)中的 Cua Driver。開好後 Ta 會自動繼續;點「停止」可放棄。'
|
|
33
|
+
: language === 'ja-JP'
|
|
34
|
+
? 'Ta は macOS に Cua Driver がこの Mac を使用することを許可するよう要求しています。Cua Driver が開いたウィンドウまたは「システム設定」で、「アクセシビリティ」(クリックと入力用)と「画面録画」(ウィンドウ表示用)の Cua Driver をオンにしてください。有効にした後、Ta は自動的に続行します。「停止」をクリックすると中止できます。'
|
|
35
|
+
: '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.';
|
|
36
|
+
}
|
|
37
|
+
export const 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.';
|
|
38
|
+
export const 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.';
|
|
39
|
+
/**
|
|
40
|
+
* Checks what the system needs before Ta first uses Cua Driver, and asks the user for it, so that a
|
|
41
|
+
* missing permission is something the user is told how to fix and not a failed call.
|
|
42
|
+
* - macOS: the Accessibility and Screen Recording permissions of CuaDriver.app. A daemon without them
|
|
43
|
+
* opens its own guide; Ta tells the user, waits while they turn the switches on, and goes on.
|
|
44
|
+
* - Linux and Windows: no permission, but a desktop session; Linux also needs the accessibility bus and
|
|
45
|
+
* some X11 libraries. `cua-driver doctor` says what is missing.
|
|
46
|
+
*/
|
|
47
|
+
export class CuaDriverAccess {
|
|
48
|
+
constructor(program, options) {
|
|
49
|
+
var _a;
|
|
50
|
+
this.program = program;
|
|
51
|
+
this.options = options;
|
|
52
|
+
this.run = (_a = options.run) !== null && _a !== void 0 ? _a : runProgram;
|
|
53
|
+
}
|
|
54
|
+
check(notify, signal) {
|
|
55
|
+
return this.options.platform === 'darwin' ? this.checkMac(notify, signal) : this.checkDesktop();
|
|
56
|
+
}
|
|
57
|
+
/** What the daemon says; undefined when it cannot tell, which does not stop Ta. */
|
|
58
|
+
async permissions() {
|
|
59
|
+
const socket = this.options.socket ? ['--socket', this.options.socket] : [];
|
|
60
|
+
const result = await this.run(this.program, ['call', 'check_permissions', '{"prompt":false}', ...socket], CHECK_TIMEOUT_MS, this.options.env);
|
|
61
|
+
return CuaDriverAccess.parsePermissions(result);
|
|
62
|
+
}
|
|
63
|
+
async checkMac(notify, signal) {
|
|
64
|
+
var _a, _b, _c, _d;
|
|
65
|
+
var _e, _f, _g, _h;
|
|
66
|
+
let state = await this.permissions();
|
|
67
|
+
const grace = Date.now() + ((_e = this.options.graceMs) !== null && _e !== void 0 ? _e : GRACE_MS);
|
|
68
|
+
while ((state === null || state === void 0 ? void 0 : state.pending) && Date.now() < grace && !(signal === null || signal === void 0 ? void 0 : signal.aborted)) {
|
|
69
|
+
await sleep(GRACE_POLL_MS, signal);
|
|
70
|
+
state = await this.permissions();
|
|
71
|
+
}
|
|
72
|
+
if (!state || (state.accessibility && state.screenRecording)) {
|
|
73
|
+
return { ready: true, notes: [], known: state !== undefined };
|
|
74
|
+
}
|
|
75
|
+
notify(askMac());
|
|
76
|
+
if (!state.pending) {
|
|
77
|
+
// The guide was closed before: System Settings, where Cua Driver is listed since it asked once.
|
|
78
|
+
(_b = (_a = this.options).openSettings) === null || _b === void 0 ? void 0 : _b.call(_a, state.accessibility ? 'Privacy_ScreenCapture' : 'Privacy_Accessibility');
|
|
79
|
+
}
|
|
80
|
+
const deadline = Date.now() + ((_f = this.options.waitMs) !== null && _f !== void 0 ? _f : WAIT_MS);
|
|
81
|
+
// Until both are on, or the guide is closed with Accessibility on: Screen Recording is optional.
|
|
82
|
+
while (!(signal === null || signal === void 0 ? void 0 : signal.aborted) && Date.now() < deadline && !((state === null || state === void 0 ? void 0 : state.accessibility) && (state.screenRecording || !state.pending))) {
|
|
83
|
+
await sleep((_g = this.options.pollMs) !== null && _g !== void 0 ? _g : POLL_MS, signal);
|
|
84
|
+
await ((_d = (_c = this.options).beforeLook) === null || _d === void 0 ? void 0 : _d.call(_c));
|
|
85
|
+
state = (_h = (await this.permissions())) !== null && _h !== void 0 ? _h : state;
|
|
86
|
+
}
|
|
87
|
+
if (state === null || state === void 0 ? void 0 : state.accessibility) {
|
|
88
|
+
return { ready: true, notes: state.screenRecording ? [] : [NO_SCREEN_RECORDING], known: true };
|
|
89
|
+
}
|
|
90
|
+
return { ready: false, reason: (signal === null || signal === void 0 ? void 0 : signal.aborted) ? 'Stopped while waiting for the permissions of macOS.' : NO_ACCESSIBILITY };
|
|
91
|
+
}
|
|
92
|
+
async checkDesktop() {
|
|
93
|
+
const result = await this.run(this.program, ['doctor', '--json'], DOCTOR_TIMEOUT_MS, this.options.env);
|
|
94
|
+
return CuaDriverAccess.parseDoctor(result, this.options.platform);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
(function (CuaDriverAccess) {
|
|
98
|
+
function parsePermissions(result) {
|
|
99
|
+
if (`${result.stdout}${result.stderr}`.includes('permissions_pending')) {
|
|
100
|
+
return { accessibility: false, screenRecording: false, pending: true };
|
|
101
|
+
}
|
|
102
|
+
try {
|
|
103
|
+
const answer = JSON.parse(result.stdout);
|
|
104
|
+
if (typeof answer.accessibility !== 'boolean') {
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
return { accessibility: answer.accessibility, screenRecording: answer.screen_recording === true, pending: false };
|
|
108
|
+
}
|
|
109
|
+
catch (_a) {
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
CuaDriverAccess.parsePermissions = parsePermissions;
|
|
114
|
+
/**
|
|
115
|
+
* Not ready when the program cannot start (a library is missing), when a probe fails, or when there is
|
|
116
|
+
* no desktop session; the other warnings about the desktop are notes. A report it cannot read does not stop Ta.
|
|
117
|
+
*/
|
|
118
|
+
function parseDoctor(result, platform) {
|
|
119
|
+
var _a;
|
|
120
|
+
var _b;
|
|
121
|
+
const library = (_a = MISSING_LIBRARY.exec(result.stderr)) === null || _a === void 0 ? void 0 : _a[1];
|
|
122
|
+
if (library) {
|
|
123
|
+
const install = platform === 'linux' ? ' On Debian or Ubuntu: `sudo apt install libx11-6 libxi6 libxkbcommon0 at-spi2-core`.' : '';
|
|
124
|
+
return { ready: false, reason: `Cua Driver cannot start: the system has no ${library}.${install}` };
|
|
125
|
+
}
|
|
126
|
+
let probes;
|
|
127
|
+
try {
|
|
128
|
+
probes = (_b = JSON.parse(result.stdout).probes) !== null && _b !== void 0 ? _b : [];
|
|
129
|
+
}
|
|
130
|
+
catch (_c) {
|
|
131
|
+
return { ready: true, notes: [], known: true };
|
|
132
|
+
}
|
|
133
|
+
const text = (probe) => [`${probe.label}: ${probe.message}`, probe.detail].filter(Boolean).join(' — ');
|
|
134
|
+
const failed = probes.filter(probe => { var _a; return probe.status === 'error' || (probe.status === 'warn' && /display server/i.test((_a = probe.label) !== null && _a !== void 0 ? _a : '')); });
|
|
135
|
+
if (failed.length > 0) {
|
|
136
|
+
return { ready: false, reason: `Cua Driver cannot use the desktop: ${failed.map(text).join('; ')}` };
|
|
137
|
+
}
|
|
138
|
+
const warned = probes.filter(probe => { var _a; return probe.status === 'warn' && DESKTOP_PROBES.test((_a = probe.label) !== null && _a !== void 0 ? _a : ''); });
|
|
139
|
+
return { ready: true, notes: warned.map(probe => `Cua Driver: ${text(probe)}`), known: true };
|
|
140
|
+
}
|
|
141
|
+
CuaDriverAccess.parseDoctor = parseDoctor;
|
|
142
|
+
})(CuaDriverAccess || (CuaDriverAccess = {}));
|
|
143
|
+
function runProgram(program, args, timeoutMs, env) {
|
|
144
|
+
return new Promise(resolve => {
|
|
145
|
+
execFile(program, args, { timeout: timeoutMs, maxBuffer: 4 * 1024 * 1024, env: { ...process.env, ...env } }, (error, stdout, stderr) => {
|
|
146
|
+
const code = error ? (typeof error.code === 'number' ? error.code : -1) : 0;
|
|
147
|
+
resolve({ code, stdout: String(stdout), stderr: String(stderr) });
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
function sleep(ms, signal) {
|
|
152
|
+
return new Promise(resolve => {
|
|
153
|
+
const timer = setTimeout(done, ms);
|
|
154
|
+
signal === null || signal === void 0 ? void 0 : signal.addEventListener('abort', done, { once: true });
|
|
155
|
+
function done() {
|
|
156
|
+
clearTimeout(timer);
|
|
157
|
+
signal === null || signal === void 0 ? void 0 : signal.removeEventListener('abort', done);
|
|
158
|
+
resolve();
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
//# sourceMappingURL=cua-driver-access.js.map
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/** Starts the daemon: `app` is the CuaDriver.app, `args` those of its program. */
|
|
2
|
+
export type CuaDriverLauncher = (app: string, args: string[], env: Readonly<Record<string, string>>) => Promise<void>;
|
|
3
|
+
export interface CuaDriverDaemonOptions {
|
|
4
|
+
dir?: string;
|
|
5
|
+
/** Where an app inside another app's bundle is copied to run, by version. */
|
|
6
|
+
stageDir?: string;
|
|
7
|
+
/** For the daemon's environment. */
|
|
8
|
+
env?: Readonly<Record<string, string>>;
|
|
9
|
+
launch?: CuaDriverLauncher;
|
|
10
|
+
idleMs?: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The daemon of the Cua Driver that comes with Ta Desktop, on macOS. There its MCP server only relays to a
|
|
14
|
+
* daemon, which macOS must start as CuaDriver.app, so that the permissions (Accessibility, Screen
|
|
15
|
+
* Recording) are the app's and not Ta's. Left alone, `cua-driver mcp` starts the CuaDriver.app in
|
|
16
|
+
* /Applications, which may be another version or missing; so Ta starts its own on a socket of its own and
|
|
17
|
+
* runs `cua-driver mcp --embedded --socket`, which only relays to it. Started on first use, it stops when
|
|
18
|
+
* unused for a while or when the process that started it exits; the relays exit with it, and pi starts
|
|
19
|
+
* them again on the next call.
|
|
20
|
+
*/
|
|
21
|
+
export declare class CuaDriverDaemon {
|
|
22
|
+
readonly source: string;
|
|
23
|
+
protected readonly options: CuaDriverDaemonOptions;
|
|
24
|
+
readonly socket: string;
|
|
25
|
+
readonly pidFile: string;
|
|
26
|
+
readonly dir: string;
|
|
27
|
+
/** The app that runs: `source`, or its copy outside the bundle that contains it. */
|
|
28
|
+
readonly app: string;
|
|
29
|
+
protected starting?: Promise<void>;
|
|
30
|
+
protected launched: boolean;
|
|
31
|
+
/** The daemon on the socket is this app's: checked once for one this process did not start. */
|
|
32
|
+
protected verified: boolean;
|
|
33
|
+
protected idle?: ReturnType<typeof setTimeout>;
|
|
34
|
+
protected readonly launch: CuaDriverLauncher;
|
|
35
|
+
constructor(source: string, options?: CuaDriverDaemonOptions);
|
|
36
|
+
/** Running, started if it is not; one left by an earlier run is used as it is. */
|
|
37
|
+
ensure(): Promise<void>;
|
|
38
|
+
/** Whether it answers on its socket: a connection, which costs well under a millisecond. */
|
|
39
|
+
alive(): Promise<boolean>;
|
|
40
|
+
/** Stops it if this process started it. Synchronous, for the `exit` event. */
|
|
41
|
+
stop(): void;
|
|
42
|
+
/**
|
|
43
|
+
* Stops other daemons of the same app, such as the one macOS starts again without Ta's arguments when
|
|
44
|
+
* the user lets it quit and reopen for Screen Recording. Only processes of this very program.
|
|
45
|
+
*/
|
|
46
|
+
reap(): Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* Whether the running daemon, left by another Ta process or an earlier run, is this app's; another
|
|
49
|
+
* copy's (another version, whose relay would not match) is stopped, to be replaced.
|
|
50
|
+
*/
|
|
51
|
+
protected adopt(): Promise<boolean>;
|
|
52
|
+
/** The program and process of the daemon on the socket, as it says; undefined when it does not say. */
|
|
53
|
+
protected identify(): Promise<{
|
|
54
|
+
executable: string;
|
|
55
|
+
pid: number;
|
|
56
|
+
} | undefined>;
|
|
57
|
+
protected get program(): string;
|
|
58
|
+
/** That the daemon is this app's program, not another copy LaunchServices chose; else it is stopped. */
|
|
59
|
+
protected verify(): Promise<void>;
|
|
60
|
+
protected commandOf(pid: number): Promise<string | undefined>;
|
|
61
|
+
protected touch(): void;
|
|
62
|
+
protected pid(): number | undefined;
|
|
63
|
+
protected get stageDir(): string;
|
|
64
|
+
/**
|
|
65
|
+
* macOS counts an app inside another app's bundle as part of that app: its permissions would be asked
|
|
66
|
+
* for, and kept, under Ta's name, and lost with each build of Ta. Its copy outside (a clone on APFS,
|
|
67
|
+
* which takes no space) is CuaDriver.app on its own, with the permissions the user gave Cua Driver.
|
|
68
|
+
*/
|
|
69
|
+
protected stage(): Promise<void>;
|
|
70
|
+
protected start(): Promise<void>;
|
|
71
|
+
}
|
|
72
|
+
export declare namespace CuaDriverDaemon {
|
|
73
|
+
/** Whether an app is inside another app's bundle. */
|
|
74
|
+
function nested(app: string): boolean;
|
|
75
|
+
/** CFBundleVersion of an app, which names its copy. */
|
|
76
|
+
function version(app: string): string;
|
|
77
|
+
}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync } from 'node:fs';
|
|
3
|
+
import { connect } from 'node:net';
|
|
4
|
+
import { homedir } from 'node:os';
|
|
5
|
+
import { dirname, join } from 'node:path';
|
|
6
|
+
const START_TIMEOUT_MS = 10000;
|
|
7
|
+
const POLL_MS = 20;
|
|
8
|
+
/** Unused this long, the daemon stops (about 60 MB); the next use starts it again in a few hundred ms. */
|
|
9
|
+
const IDLE_MS = 10 * 60 * 1000;
|
|
10
|
+
/**
|
|
11
|
+
* The daemon of the Cua Driver that comes with Ta Desktop, on macOS. There its MCP server only relays to a
|
|
12
|
+
* daemon, which macOS must start as CuaDriver.app, so that the permissions (Accessibility, Screen
|
|
13
|
+
* Recording) are the app's and not Ta's. Left alone, `cua-driver mcp` starts the CuaDriver.app in
|
|
14
|
+
* /Applications, which may be another version or missing; so Ta starts its own on a socket of its own and
|
|
15
|
+
* runs `cua-driver mcp --embedded --socket`, which only relays to it. Started on first use, it stops when
|
|
16
|
+
* unused for a while or when the process that started it exits; the relays exit with it, and pi starts
|
|
17
|
+
* them again on the next call.
|
|
18
|
+
*/
|
|
19
|
+
export class CuaDriverDaemon {
|
|
20
|
+
constructor(source, options = {}) {
|
|
21
|
+
var _a, _b;
|
|
22
|
+
this.source = source;
|
|
23
|
+
this.options = options;
|
|
24
|
+
this.launched = false;
|
|
25
|
+
/** The daemon on the socket is this app's: checked once for one this process did not start. */
|
|
26
|
+
this.verified = false;
|
|
27
|
+
this.dir = (_a = options.dir) !== null && _a !== void 0 ? _a : join(homedir(), 'Library', 'Caches', 'ta');
|
|
28
|
+
this.app = CuaDriverDaemon.nested(source) ? join(this.stageDir, CuaDriverDaemon.version(source), 'CuaDriver.app') : source;
|
|
29
|
+
this.socket = join(this.dir, 'cua-driver.sock');
|
|
30
|
+
this.pidFile = join(this.dir, 'cua-driver.pid');
|
|
31
|
+
this.launch = (_b = options.launch) !== null && _b !== void 0 ? _b : openApp;
|
|
32
|
+
}
|
|
33
|
+
/** Running, started if it is not; one left by an earlier run is used as it is. */
|
|
34
|
+
async ensure() {
|
|
35
|
+
var _a;
|
|
36
|
+
if ((await this.alive()) && (this.verified || (await this.adopt()))) {
|
|
37
|
+
this.touch();
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
// Unused is counted from when it answers: a start slower than the idle time is not stopped.
|
|
41
|
+
clearTimeout(this.idle);
|
|
42
|
+
(_a = this.starting) !== null && _a !== void 0 ? _a : (this.starting = this.start().finally(() => {
|
|
43
|
+
this.starting = undefined;
|
|
44
|
+
}));
|
|
45
|
+
await this.starting;
|
|
46
|
+
this.touch();
|
|
47
|
+
}
|
|
48
|
+
/** Whether it answers on its socket: a connection, which costs well under a millisecond. */
|
|
49
|
+
alive() {
|
|
50
|
+
if (!existsSync(this.socket)) {
|
|
51
|
+
return Promise.resolve(false);
|
|
52
|
+
}
|
|
53
|
+
return new Promise(resolve => {
|
|
54
|
+
const socket = connect(this.socket);
|
|
55
|
+
socket.once('connect', () => {
|
|
56
|
+
socket.destroy();
|
|
57
|
+
resolve(true);
|
|
58
|
+
});
|
|
59
|
+
socket.on('error', () => resolve(false));
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/** Stops it if this process started it. Synchronous, for the `exit` event. */
|
|
63
|
+
stop() {
|
|
64
|
+
clearTimeout(this.idle);
|
|
65
|
+
if (!this.launched) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
this.launched = false;
|
|
69
|
+
const pid = this.pid();
|
|
70
|
+
if (pid) {
|
|
71
|
+
try {
|
|
72
|
+
process.kill(pid, 'SIGTERM');
|
|
73
|
+
}
|
|
74
|
+
catch (_a) {
|
|
75
|
+
// Gone already.
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Stops other daemons of the same app, such as the one macOS starts again without Ta's arguments when
|
|
81
|
+
* the user lets it quit and reopen for Screen Recording. Only processes of this very program.
|
|
82
|
+
*/
|
|
83
|
+
async reap() {
|
|
84
|
+
var _a;
|
|
85
|
+
const program = this.program;
|
|
86
|
+
const own = this.pid();
|
|
87
|
+
const list = await new Promise(resolve => execFile('ps', ['-axo', 'pid=,command='], (_error, stdout) => resolve(String(stdout !== null && stdout !== void 0 ? stdout : ''))));
|
|
88
|
+
for (const line of list.split('\n')) {
|
|
89
|
+
const [, pid, command] = (_a = /^\s*(\d+)\s+(.*)$/.exec(line)) !== null && _a !== void 0 ? _a : [];
|
|
90
|
+
if (pid && Number(pid) !== own && command.startsWith(`${program} serve`) && !command.includes(this.socket)) {
|
|
91
|
+
try {
|
|
92
|
+
process.kill(Number(pid), 'SIGTERM');
|
|
93
|
+
}
|
|
94
|
+
catch (_b) {
|
|
95
|
+
// Gone already.
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Whether the running daemon, left by another Ta process or an earlier run, is this app's; another
|
|
102
|
+
* copy's (another version, whose relay would not match) is stopped, to be replaced.
|
|
103
|
+
*/
|
|
104
|
+
async adopt() {
|
|
105
|
+
const running = await this.identify();
|
|
106
|
+
if (!running || running.executable === this.program) {
|
|
107
|
+
this.verified = true;
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
process.kill(running.pid, 'SIGTERM');
|
|
112
|
+
}
|
|
113
|
+
catch (_a) {
|
|
114
|
+
// Gone already.
|
|
115
|
+
}
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
/** The program and process of the daemon on the socket, as it says; undefined when it does not say. */
|
|
119
|
+
identify() {
|
|
120
|
+
return new Promise(resolve => execFile(this.program, ['call', 'check_permissions', '{"prompt":false}', '--socket', this.socket], { timeout: 5000, env: { ...process.env, ...this.options.env } }, (_error, stdout) => {
|
|
121
|
+
try {
|
|
122
|
+
const { source } = JSON.parse(String(stdout));
|
|
123
|
+
resolve((source === null || source === void 0 ? void 0 : source.executable) && source.pid ? { executable: source.executable, pid: source.pid } : undefined);
|
|
124
|
+
}
|
|
125
|
+
catch (_a) {
|
|
126
|
+
resolve(undefined);
|
|
127
|
+
}
|
|
128
|
+
}));
|
|
129
|
+
}
|
|
130
|
+
get program() {
|
|
131
|
+
return join(this.app, 'Contents', 'MacOS', 'cua-driver');
|
|
132
|
+
}
|
|
133
|
+
/** That the daemon is this app's program, not another copy LaunchServices chose; else it is stopped. */
|
|
134
|
+
async verify() {
|
|
135
|
+
const pid = this.pid();
|
|
136
|
+
const command = pid ? await this.commandOf(pid) : undefined;
|
|
137
|
+
const program = this.program;
|
|
138
|
+
if (command === undefined || command.includes(`${program} serve`)) {
|
|
139
|
+
this.verified = true;
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
this.stop();
|
|
143
|
+
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.`);
|
|
144
|
+
}
|
|
145
|
+
commandOf(pid) {
|
|
146
|
+
return new Promise(resolve => execFile('ps', ['-o', 'command=', '-p', String(pid)], (error, stdout) => resolve(error ? undefined : String(stdout).trim())));
|
|
147
|
+
}
|
|
148
|
+
touch() {
|
|
149
|
+
var _a, _b;
|
|
150
|
+
var _c;
|
|
151
|
+
clearTimeout(this.idle);
|
|
152
|
+
this.idle = setTimeout(() => this.stop(), (_c = this.options.idleMs) !== null && _c !== void 0 ? _c : IDLE_MS);
|
|
153
|
+
(_b = (_a = this.idle).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
154
|
+
}
|
|
155
|
+
pid() {
|
|
156
|
+
try {
|
|
157
|
+
return Number(readFileSync(this.pidFile, 'utf8').trim()) || undefined;
|
|
158
|
+
}
|
|
159
|
+
catch (_a) {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
get stageDir() {
|
|
164
|
+
var _a;
|
|
165
|
+
return (_a = this.options.stageDir) !== null && _a !== void 0 ? _a : join(homedir(), 'Library', 'Application Support', 'Ta', 'cua-driver');
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* macOS counts an app inside another app's bundle as part of that app: its permissions would be asked
|
|
169
|
+
* for, and kept, under Ta's name, and lost with each build of Ta. Its copy outside (a clone on APFS,
|
|
170
|
+
* which takes no space) is CuaDriver.app on its own, with the permissions the user gave Cua Driver.
|
|
171
|
+
*/
|
|
172
|
+
async stage() {
|
|
173
|
+
if (this.app === this.source || existsSync(this.app)) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const versions = dirname(dirname(this.app));
|
|
177
|
+
const temp = `${dirname(this.app)}.part`;
|
|
178
|
+
rmSync(temp, { recursive: true, force: true });
|
|
179
|
+
mkdirSync(temp, { recursive: true });
|
|
180
|
+
await run('cp', ['-cR', this.source, temp]).catch(() => run('cp', ['-R', this.source, temp]));
|
|
181
|
+
rmSync(dirname(this.app), { recursive: true, force: true });
|
|
182
|
+
renameSync(temp, dirname(this.app));
|
|
183
|
+
// The copies of earlier versions, which Ta no longer comes with.
|
|
184
|
+
for (const name of readdirSync(versions)) {
|
|
185
|
+
if (join(versions, name) !== dirname(this.app)) {
|
|
186
|
+
rmSync(join(versions, name), { recursive: true, force: true });
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
async start() {
|
|
191
|
+
var _a;
|
|
192
|
+
await this.stage();
|
|
193
|
+
mkdirSync(this.dir, { recursive: true });
|
|
194
|
+
// A socket file left by a daemon that did not stop cleanly, or by the one adopt() stopped.
|
|
195
|
+
rmSync(this.socket, { force: true });
|
|
196
|
+
this.verified = false;
|
|
197
|
+
await this.launch(this.app, ['serve', '--socket', this.socket, '--pid-file', this.pidFile], (_a = this.options.env) !== null && _a !== void 0 ? _a : {});
|
|
198
|
+
if (!this.launched) {
|
|
199
|
+
this.launched = true;
|
|
200
|
+
process.once('exit', () => this.stop());
|
|
201
|
+
}
|
|
202
|
+
const deadline = Date.now() + START_TIMEOUT_MS;
|
|
203
|
+
while (Date.now() < deadline) {
|
|
204
|
+
if (await this.alive()) {
|
|
205
|
+
return this.verify();
|
|
206
|
+
}
|
|
207
|
+
await new Promise(resolve => setTimeout(resolve, POLL_MS));
|
|
208
|
+
}
|
|
209
|
+
throw new Error(`Cua Driver did not start in ${START_TIMEOUT_MS / 1000} s: run \`${join(this.app, 'Contents/MacOS/cua-driver')} doctor\`.`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
(function (CuaDriverDaemon) {
|
|
213
|
+
/** Whether an app is inside another app's bundle. */
|
|
214
|
+
function nested(app) {
|
|
215
|
+
return /\.app\//.test(dirname(app));
|
|
216
|
+
}
|
|
217
|
+
CuaDriverDaemon.nested = nested;
|
|
218
|
+
/** CFBundleVersion of an app, which names its copy. */
|
|
219
|
+
function version(app) {
|
|
220
|
+
var _a;
|
|
221
|
+
var _b;
|
|
222
|
+
try {
|
|
223
|
+
return (_b = (_a = /<key>CFBundleVersion<\/key>\s*<string>([^<]+)<\/string>/.exec(readFileSync(join(app, 'Contents', 'Info.plist'), 'utf8'))) === null || _a === void 0 ? void 0 : _a[1]) !== null && _b !== void 0 ? _b : 'current';
|
|
224
|
+
}
|
|
225
|
+
catch (_c) {
|
|
226
|
+
return 'current';
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
CuaDriverDaemon.version = version;
|
|
230
|
+
})(CuaDriverDaemon || (CuaDriverDaemon = {}));
|
|
231
|
+
const LSREGISTER = '/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister';
|
|
232
|
+
/**
|
|
233
|
+
* Through LaunchServices, in the background and as a new instance, so that it is the app that runs and
|
|
234
|
+
* macOS applies the app's permissions. LaunchServices picks a copy by bundle id, and other apps may ship
|
|
235
|
+
* their own CuaDriver.app (another version, whose relay would not match): registering this one first makes
|
|
236
|
+
* it launch this very copy.
|
|
237
|
+
*/
|
|
238
|
+
async function openApp(app, args, env) {
|
|
239
|
+
await run(LSREGISTER, ['-f', app]).catch(() => undefined);
|
|
240
|
+
const vars = Object.entries(env).flatMap(([name, value]) => ['--env', `${name}=${value}`]);
|
|
241
|
+
await run('open', ['-g', '-n', '-a', app, ...vars, '--args', ...args]);
|
|
242
|
+
}
|
|
243
|
+
function run(program, args) {
|
|
244
|
+
return new Promise((resolve, reject) => execFile(program, args, (error, stdout) => (error ? reject(error) : resolve(String(stdout)))));
|
|
245
|
+
}
|
|
246
|
+
//# sourceMappingURL=cua-driver-daemon.js.map
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** Where to look for Cua Driver: the parts of the system it depends on, so that tests can give their own. */
|
|
2
|
+
export interface CuaDriverHost {
|
|
3
|
+
platform: NodeJS.Platform;
|
|
4
|
+
/** The `PATH` environment variable. */
|
|
5
|
+
path?: string;
|
|
6
|
+
home: string;
|
|
7
|
+
/** {@link CuaDriver.BUNDLED_ENV}. */
|
|
8
|
+
bundled?: string;
|
|
9
|
+
isFile(path: string): boolean;
|
|
10
|
+
}
|
|
11
|
+
/** The program of Cua Driver that Ta uses. */
|
|
12
|
+
export interface CuaDriverProgram {
|
|
13
|
+
path: string;
|
|
14
|
+
/** The one that comes with Ta Desktop, used when the user has none of their own. */
|
|
15
|
+
bundled: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Cua Driver (https://cua.ai/cua-driver): a program that uses the desktop apps of this computer in the
|
|
19
|
+
* background, through each system's accessibility tree, as an MCP server (`cua-driver mcp`). Ta uses the
|
|
20
|
+
* one that comes with Ta Desktop, a version Ta is tested with, or else the one the user installed.
|
|
21
|
+
*/
|
|
22
|
+
export declare namespace CuaDriver {
|
|
23
|
+
/** The name of its MCP server, so its tools are `mcp__cua_driver__<tool>`. */
|
|
24
|
+
const SERVER = "cua-driver";
|
|
25
|
+
/** pi's namespace of the server's tools. */
|
|
26
|
+
const NAMESPACE = "mcp__cua_driver";
|
|
27
|
+
/** The section of the system prompt that tells the model about it. */
|
|
28
|
+
const SECTION = "computer_use";
|
|
29
|
+
/** The program that comes with Ta Desktop, which sets it for the `ta serve` it starts. */
|
|
30
|
+
const BUNDLED_ENV = "TA_CUA_DRIVER";
|
|
31
|
+
/** What the system prompt says, with what only one system needs. */
|
|
32
|
+
function prompt(platform: NodeJS.Platform): string;
|
|
33
|
+
/**
|
|
34
|
+
* For each run of the one of Ta Desktop: no product telemetry and no update check, which Ta's own
|
|
35
|
+
* updates take care of. The user's own install keeps the user's settings.
|
|
36
|
+
*/
|
|
37
|
+
const BUNDLED_ENV_VARS: Readonly<Record<string, string>>;
|
|
38
|
+
/** The description of the MCP server, which pi lists in the system prompt once it is registered. */
|
|
39
|
+
const DESCRIPTION = "Use the desktop apps of this computer in the background (Cua Driver)";
|
|
40
|
+
const INSTALL: Readonly<Record<'posix' | 'win32', string>>;
|
|
41
|
+
/**
|
|
42
|
+
* Its program: the one of Ta Desktop, else the user's own, on `PATH` or where its installer or its macOS
|
|
43
|
+
* app puts it. Only looks at files, it runs nothing.
|
|
44
|
+
*/
|
|
45
|
+
function locate(host?: CuaDriverHost): CuaDriverProgram | undefined;
|
|
46
|
+
/** The CuaDriver.app of a program inside one. */
|
|
47
|
+
function appOf(program: string): string | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* Whether a call may use its tools, given what the called tool says it reaches (pi's `reachesTools`):
|
|
50
|
+
* the words of a codemode script, or `all` for one that searches tools, which then has to name it.
|
|
51
|
+
*/
|
|
52
|
+
function reaches(reach: 'all' | readonly string[] | undefined, input: unknown): boolean;
|
|
53
|
+
/** What `/computer-use` says. */
|
|
54
|
+
function status(program: CuaDriverProgram | undefined, platform: NodeJS.Platform): string;
|
|
55
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { statSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
/**
|
|
5
|
+
* Cua Driver (https://cua.ai/cua-driver): a program that uses the desktop apps of this computer in the
|
|
6
|
+
* background, through each system's accessibility tree, as an MCP server (`cua-driver mcp`). Ta uses the
|
|
7
|
+
* one that comes with Ta Desktop, a version Ta is tested with, or else the one the user installed.
|
|
8
|
+
*/
|
|
9
|
+
export var CuaDriver;
|
|
10
|
+
(function (CuaDriver) {
|
|
11
|
+
/** The name of its MCP server, so its tools are `mcp__cua_driver__<tool>`. */
|
|
12
|
+
CuaDriver.SERVER = 'cua-driver';
|
|
13
|
+
/** pi's namespace of the server's tools. */
|
|
14
|
+
CuaDriver.NAMESPACE = 'mcp__cua_driver';
|
|
15
|
+
/** The section of the system prompt that tells the model about it. */
|
|
16
|
+
CuaDriver.SECTION = 'computer_use';
|
|
17
|
+
/** The program that comes with Ta Desktop, which sets it for the `ta serve` it starts. */
|
|
18
|
+
CuaDriver.BUNDLED_ENV = 'TA_CUA_DRIVER';
|
|
19
|
+
/** What the system prompt says, with what only one system needs. */
|
|
20
|
+
function prompt(platform) {
|
|
21
|
+
return [
|
|
22
|
+
"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.",
|
|
23
|
+
`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.`,
|
|
24
|
+
'Use it only when a task needs a desktop app, not for files or commands.',
|
|
25
|
+
...(PLATFORM_NOTES[platform] ? [PLATFORM_NOTES[platform]] : []),
|
|
26
|
+
].join('\n');
|
|
27
|
+
}
|
|
28
|
+
CuaDriver.prompt = prompt;
|
|
29
|
+
const PLATFORM_NOTES = {
|
|
30
|
+
darwin: 'If a call waits for the permissions of macOS, Ta has asked the user for them; do not ask again.',
|
|
31
|
+
win32: 'Windows does not let it control the windows of apps run as administrator unless Ta runs as administrator too.',
|
|
32
|
+
linux: 'It needs the desktop session of the user (X11, or Wayland with XWayland) and its accessibility bus (AT-SPI).',
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* For each run of the one of Ta Desktop: no product telemetry and no update check, which Ta's own
|
|
36
|
+
* updates take care of. The user's own install keeps the user's settings.
|
|
37
|
+
*/
|
|
38
|
+
CuaDriver.BUNDLED_ENV_VARS = { CUA_DRIVER_RS_TELEMETRY_ENABLED: '0', CUA_DRIVER_RS_UPDATE_CHECK: '0' };
|
|
39
|
+
/** The description of the MCP server, which pi lists in the system prompt once it is registered. */
|
|
40
|
+
CuaDriver.DESCRIPTION = 'Use the desktop apps of this computer in the background (Cua Driver)';
|
|
41
|
+
CuaDriver.INSTALL = {
|
|
42
|
+
posix: '/bin/bash -c "$(curl -fsSL https://cua.ai/driver/install.sh)"',
|
|
43
|
+
win32: 'irm https://cua.ai/driver/install.ps1 | iex',
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Its program: the one of Ta Desktop, else the user's own, on `PATH` or where its installer or its macOS
|
|
47
|
+
* app puts it. Only looks at files, it runs nothing.
|
|
48
|
+
*/
|
|
49
|
+
function locate(host = currentHost()) {
|
|
50
|
+
var _a;
|
|
51
|
+
if (host.bundled && host.isFile(host.bundled)) {
|
|
52
|
+
return { path: host.bundled, bundled: true };
|
|
53
|
+
}
|
|
54
|
+
const windows = host.platform === 'win32';
|
|
55
|
+
const name = windows ? 'cua-driver.exe' : 'cua-driver';
|
|
56
|
+
const dirs = ((_a = host.path) !== null && _a !== void 0 ? _a : '').split(windows ? ';' : ':').filter(Boolean);
|
|
57
|
+
const candidates = [...dirs.map(dir => join(dir, name)), join(host.home, '.local', 'bin', name)];
|
|
58
|
+
if (host.platform === 'darwin') {
|
|
59
|
+
const app = 'CuaDriver.app/Contents/MacOS/cua-driver';
|
|
60
|
+
candidates.push(join('/Applications', app), join(host.home, 'Applications', app));
|
|
61
|
+
}
|
|
62
|
+
const own = candidates.find(candidate => host.isFile(candidate));
|
|
63
|
+
return own ? { path: own, bundled: false } : undefined;
|
|
64
|
+
}
|
|
65
|
+
CuaDriver.locate = locate;
|
|
66
|
+
/** The CuaDriver.app of a program inside one. */
|
|
67
|
+
function appOf(program) {
|
|
68
|
+
const inside = '/Contents/MacOS/cua-driver';
|
|
69
|
+
const app = program.slice(0, -inside.length);
|
|
70
|
+
return program.endsWith(inside) && app.endsWith('.app') ? app : undefined;
|
|
71
|
+
}
|
|
72
|
+
CuaDriver.appOf = appOf;
|
|
73
|
+
/**
|
|
74
|
+
* Whether a call may use its tools, given what the called tool says it reaches (pi's `reachesTools`):
|
|
75
|
+
* the words of a codemode script, or `all` for one that searches tools, which then has to name it.
|
|
76
|
+
*/
|
|
77
|
+
function reaches(reach, input) {
|
|
78
|
+
if (reach === undefined) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
if (reach === 'all') {
|
|
82
|
+
return JSON.stringify(input !== null && input !== void 0 ? input : null).includes(CuaDriver.NAMESPACE);
|
|
83
|
+
}
|
|
84
|
+
return reach.some(word => word.includes(CuaDriver.NAMESPACE));
|
|
85
|
+
}
|
|
86
|
+
CuaDriver.reaches = reaches;
|
|
87
|
+
/** What `/computer-use` says. */
|
|
88
|
+
function status(program, platform) {
|
|
89
|
+
if (program) {
|
|
90
|
+
const which = program.bundled ? `the one that comes with Ta Desktop (${program.path})` : program.path;
|
|
91
|
+
return `Cua Driver: ${which}. Ta starts it the first time it uses a desktop app, and checks what the system needs then.`;
|
|
92
|
+
}
|
|
93
|
+
const install = platform === 'win32' ? CuaDriver.INSTALL.win32 : CuaDriver.INSTALL.posix;
|
|
94
|
+
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.`;
|
|
95
|
+
}
|
|
96
|
+
CuaDriver.status = status;
|
|
97
|
+
function currentHost() {
|
|
98
|
+
return { platform: process.platform, path: process.env.PATH, home: homedir(), bundled: process.env[CuaDriver.BUNDLED_ENV], isFile };
|
|
99
|
+
}
|
|
100
|
+
function isFile(path) {
|
|
101
|
+
try {
|
|
102
|
+
return statSync(path).isFile();
|
|
103
|
+
}
|
|
104
|
+
catch (_a) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
})(CuaDriver || (CuaDriver = {}));
|
|
109
|
+
//# sourceMappingURL=cua-driver.js.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type CuaDriverProgram } from './cua-driver.js';
|
|
2
|
+
import { CuaDriverAccess } from './cua-driver-access.js';
|
|
3
|
+
import { CuaDriverDaemon } from './cua-driver-daemon.js';
|
|
4
|
+
import type { ExtensionAPI } from './pi-types.js';
|
|
5
|
+
/** A program of Cua Driver and what goes with it, shared by the sessions of a process. */
|
|
6
|
+
export interface CuaDriverSetup {
|
|
7
|
+
program: CuaDriverProgram;
|
|
8
|
+
/** The daemon Ta runs, for the driver of Ta Desktop on macOS. */
|
|
9
|
+
daemon?: CuaDriverDaemon;
|
|
10
|
+
access: CuaDriverAccess;
|
|
11
|
+
/** The arguments and environment of its MCP server. */
|
|
12
|
+
args: string[];
|
|
13
|
+
env?: Record<string, string>;
|
|
14
|
+
/** Checked, once for the process. */
|
|
15
|
+
ready: boolean;
|
|
16
|
+
checking?: Promise<Awaited<ReturnType<CuaDriverAccess['check']>>>;
|
|
17
|
+
}
|
|
18
|
+
/** What the extension depends on, so that tests can give their own. */
|
|
19
|
+
export interface ComputerUseOptions {
|
|
20
|
+
locate(): CuaDriverProgram | undefined;
|
|
21
|
+
platform: NodeJS.Platform;
|
|
22
|
+
setup(program: CuaDriverProgram, platform: NodeJS.Platform): CuaDriverSetup;
|
|
23
|
+
}
|
|
24
|
+
/** One per program and process: the sessions of `ta serve` share its daemon and its check. */
|
|
25
|
+
export declare function setupOf(found: CuaDriverProgram, platform: NodeJS.Platform): CuaDriverSetup;
|
|
26
|
+
/**
|
|
27
|
+
* Lets the agent use the desktop apps of this computer through Cua Driver, at no cost until it does.
|
|
28
|
+
* Registered up front, an MCP server starts in every session: its process (about 10 MB) and pi's MCP
|
|
29
|
+
* client. So the system prompt only names it, and the server is registered when a call first names its
|
|
30
|
+
* tools; pi connects it before the call runs. Each prompt looks for the program (a few `stat`s, nothing
|
|
31
|
+
* is run) until it is found, so a driver installed meanwhile is used without a restart.
|
|
32
|
+
*
|
|
33
|
+
* The first call also checks what the system needs (CuaDriverAccess): on macOS it waits while the user
|
|
34
|
+
* gives the permissions it asked for, elsewhere it says what is missing, so that the agent's call goes on
|
|
35
|
+
* or fails with what to do, not with an error of the driver.
|
|
36
|
+
*/
|
|
37
|
+
export declare function activate(pi: ExtensionAPI, options?: ComputerUseOptions): void;
|
|
38
|
+
export default function computerUse(pi: ExtensionAPI): void;
|
package/esm/extension.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { realpathSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { CuaDriver } from './cua-driver.js';
|
|
5
|
+
import { CuaDriverAccess } from './cua-driver-access.js';
|
|
6
|
+
import { CuaDriverDaemon } from './cua-driver-daemon.js';
|
|
7
|
+
/** How long a call waits the first time for Cua Driver's tools; it connects in tens of milliseconds. */
|
|
8
|
+
const CONNECT_WAIT_MS = 5000;
|
|
9
|
+
const POLL_MS = 15;
|
|
10
|
+
const setups = new Map();
|
|
11
|
+
/** One per program and process: the sessions of `ta serve` share its daemon and its check. */
|
|
12
|
+
export function setupOf(found, platform) {
|
|
13
|
+
// LaunchServices finds an app by its exact path: one with `..` or a link in it is another app to it.
|
|
14
|
+
const program = { ...found, path: realPath(found.path) };
|
|
15
|
+
let setup = setups.get(program.path);
|
|
16
|
+
if (!setup) {
|
|
17
|
+
const env = program.bundled ? { ...CuaDriver.BUNDLED_ENV_VARS } : undefined;
|
|
18
|
+
const app = program.bundled && platform === 'darwin' ? CuaDriver.appOf(program.path) : undefined;
|
|
19
|
+
const daemon = app ? new CuaDriverDaemon(app, { env }) : undefined;
|
|
20
|
+
const access = new CuaDriverAccess(daemon ? join(daemon.app, 'Contents', 'MacOS', 'cua-driver') : program.path, {
|
|
21
|
+
platform,
|
|
22
|
+
socket: daemon === null || daemon === void 0 ? void 0 : daemon.socket,
|
|
23
|
+
env,
|
|
24
|
+
beforeLook: daemon ? () => daemon.ensure() : undefined,
|
|
25
|
+
openSettings: pane => execFile('open', [`x-apple.systempreferences:com.apple.preference.security?${pane}`], () => undefined),
|
|
26
|
+
});
|
|
27
|
+
const args = daemon ? ['mcp', '--embedded', '--socket', daemon.socket] : ['mcp'];
|
|
28
|
+
setup = { program, daemon, access, args, env, ready: false };
|
|
29
|
+
setups.set(program.path, setup);
|
|
30
|
+
}
|
|
31
|
+
return setup;
|
|
32
|
+
}
|
|
33
|
+
function realPath(path) {
|
|
34
|
+
try {
|
|
35
|
+
return realpathSync(path);
|
|
36
|
+
}
|
|
37
|
+
catch (_a) {
|
|
38
|
+
return path;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const DEFAULTS = { locate: () => CuaDriver.locate(), platform: process.platform, setup: setupOf };
|
|
42
|
+
/**
|
|
43
|
+
* Lets the agent use the desktop apps of this computer through Cua Driver, at no cost until it does.
|
|
44
|
+
* Registered up front, an MCP server starts in every session: its process (about 10 MB) and pi's MCP
|
|
45
|
+
* client. So the system prompt only names it, and the server is registered when a call first names its
|
|
46
|
+
* tools; pi connects it before the call runs. Each prompt looks for the program (a few `stat`s, nothing
|
|
47
|
+
* is run) until it is found, so a driver installed meanwhile is used without a restart.
|
|
48
|
+
*
|
|
49
|
+
* The first call also checks what the system needs (CuaDriverAccess): on macOS it waits while the user
|
|
50
|
+
* gives the permissions it asked for, elsewhere it says what is missing, so that the agent's call goes on
|
|
51
|
+
* or fails with what to do, not with an error of the driver.
|
|
52
|
+
*/
|
|
53
|
+
export function activate(pi, options = DEFAULTS) {
|
|
54
|
+
let setup;
|
|
55
|
+
/** Registered, and waited for once: later calls are pi's to wait for, as for any server it knows. */
|
|
56
|
+
let connected = false;
|
|
57
|
+
let connecting;
|
|
58
|
+
const prefix = `${CuaDriver.NAMESPACE}__`;
|
|
59
|
+
const ready = () => pi.getAllTools().some(tool => tool.name.startsWith(prefix));
|
|
60
|
+
const found = () => {
|
|
61
|
+
if (!setup) {
|
|
62
|
+
const program = options.locate();
|
|
63
|
+
setup = program ? options.setup(program, options.platform) : undefined;
|
|
64
|
+
}
|
|
65
|
+
return setup;
|
|
66
|
+
};
|
|
67
|
+
const connect = async (driver, signal) => {
|
|
68
|
+
if (!pi.getMcpServers().some(server => server.name === CuaDriver.SERVER)) {
|
|
69
|
+
pi.registerMcpServer(CuaDriver.SERVER, { command: driver.program.path, args: driver.args, env: driver.env, description: CuaDriver.DESCRIPTION });
|
|
70
|
+
}
|
|
71
|
+
// pi's MCP extension waits for a server it already knows; this one may come after this handler.
|
|
72
|
+
const deadline = Date.now() + CONNECT_WAIT_MS;
|
|
73
|
+
while (!ready() && Date.now() < deadline && !(signal === null || signal === void 0 ? void 0 : signal.aborted)) {
|
|
74
|
+
await new Promise(resolve => setTimeout(resolve, POLL_MS));
|
|
75
|
+
}
|
|
76
|
+
// A call stopped meanwhile leaves the wait to the next one.
|
|
77
|
+
connected = !(signal === null || signal === void 0 ? void 0 : signal.aborted);
|
|
78
|
+
};
|
|
79
|
+
/** Once for the process; the calls at the same time share it. */
|
|
80
|
+
const check = async (driver, notify, signal) => {
|
|
81
|
+
var _a;
|
|
82
|
+
var _b;
|
|
83
|
+
(_b = driver.checking) !== null && _b !== void 0 ? _b : (driver.checking = driver.access.check(notify, signal).finally(() => {
|
|
84
|
+
driver.checking = undefined;
|
|
85
|
+
}));
|
|
86
|
+
const result = await driver.checking;
|
|
87
|
+
if (!result.ready) {
|
|
88
|
+
return result.reason;
|
|
89
|
+
}
|
|
90
|
+
if (result.known && !driver.ready) {
|
|
91
|
+
driver.ready = true;
|
|
92
|
+
for (const note of result.notes) {
|
|
93
|
+
notify(note);
|
|
94
|
+
}
|
|
95
|
+
void ((_a = driver.daemon) === null || _a === void 0 ? void 0 : _a.reap());
|
|
96
|
+
}
|
|
97
|
+
return undefined;
|
|
98
|
+
};
|
|
99
|
+
pi.on('before_agent_start', event => {
|
|
100
|
+
const { sections } = event.systemPromptOptions;
|
|
101
|
+
if (found()) {
|
|
102
|
+
sections[CuaDriver.SECTION] = CuaDriver.prompt(options.platform);
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
delete sections[CuaDriver.SECTION];
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
pi.on('tool_call', async (event, ctx) => {
|
|
109
|
+
var _a, _b;
|
|
110
|
+
const driver = setup;
|
|
111
|
+
if (!driver) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
if (driver.ready && connected && !driver.daemon) {
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
const tool = pi.getAllTools().find(candidate => candidate.name === event.toolName);
|
|
118
|
+
if (!CuaDriver.reaches((_a = tool === null || tool === void 0 ? void 0 : tool.reachesTools) === null || _a === void 0 ? void 0 : _a.call(tool, event.input), event.input)) {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
const notify = (message) => ctx.ui.notify(message, 'warning');
|
|
122
|
+
try {
|
|
123
|
+
// Every call that uses it, in case the daemon stopped: a check costs one connection.
|
|
124
|
+
await ((_b = driver.daemon) === null || _b === void 0 ? void 0 : _b.ensure());
|
|
125
|
+
// The daemon of Ta Desktop answers before the server connects: its relay needs the permissions.
|
|
126
|
+
// The user's own install starts its daemon when it connects, so it is asked afterwards.
|
|
127
|
+
const reason = driver.daemon && !driver.ready ? await check(driver, notify, ctx.signal) : undefined;
|
|
128
|
+
if (reason) {
|
|
129
|
+
return { block: true, reason };
|
|
130
|
+
}
|
|
131
|
+
if (!connected) {
|
|
132
|
+
connecting !== null && connecting !== void 0 ? connecting : (connecting = connect(driver, ctx.signal).finally(() => {
|
|
133
|
+
connecting = undefined;
|
|
134
|
+
}));
|
|
135
|
+
await connecting;
|
|
136
|
+
}
|
|
137
|
+
const after = !driver.daemon && !driver.ready ? await check(driver, notify, ctx.signal) : undefined;
|
|
138
|
+
return after ? { block: true, reason: after } : undefined;
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
return { block: true, reason: error.message };
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
pi.registerCommand('computer-use', {
|
|
145
|
+
description: 'Whether Ta can use the desktop apps of this computer (Cua Driver), and what the system still needs',
|
|
146
|
+
handler: async (_args, ctx) => {
|
|
147
|
+
var _a;
|
|
148
|
+
const program = options.locate();
|
|
149
|
+
ctx.ui.notify(CuaDriver.status(program, options.platform), program ? 'info' : 'warning');
|
|
150
|
+
if (!program) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const driver = options.setup(program, options.platform);
|
|
154
|
+
await ((_a = driver.daemon) === null || _a === void 0 ? void 0 : _a.ensure());
|
|
155
|
+
const result = await driver.access.check(message => ctx.ui.notify(message, 'warning'), ctx.signal);
|
|
156
|
+
if (!result.ready) {
|
|
157
|
+
ctx.ui.notify(result.reason, 'warning');
|
|
158
|
+
}
|
|
159
|
+
else if (result.known) {
|
|
160
|
+
driver.ready = true;
|
|
161
|
+
ctx.ui.notify(['Cua Driver has what it needs.', ...result.notes].join('\n'), result.notes.length > 0 ? 'warning' : 'info');
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
export default function computerUse(pi) {
|
|
167
|
+
activate(pi);
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=extension.js.map
|
package/esm/package.json
ADDED
package/esm/pi-types.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/ta-computer-use",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.36.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"
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"!**/*.map"
|
|
21
21
|
],
|
|
22
22
|
"scripts": {
|
|
23
|
-
"build": "tsc -b && bun build src/extension.ts --format esm --target node --outfile extension/index.mjs",
|
|
23
|
+
"build": "tsc -b && bun ../../scripts/build-esm.ts && bun build src/extension.ts --format esm --target node --outfile extension/index.mjs",
|
|
24
24
|
"clean": "rm -rf lib extension *.tsbuildinfo",
|
|
25
25
|
"test": "bun test ./lib",
|
|
26
26
|
"watch": "tsc -b -w --preserveWatchOutput"
|