@rome-os/node-core 0.1.8 → 0.1.10
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.md +4 -5
- package/dist/actions.d.ts +2 -1
- package/dist/actions.js +3 -3
- package/dist/audit.d.ts +6 -0
- package/dist/audit.js +11 -0
- package/dist/cloud.js +1 -2
- package/dist/computer-use/audit-fields.d.ts +5 -0
- package/dist/computer-use/audit-fields.js +17 -0
- package/dist/computer-use/bridge.d.ts +53 -0
- package/dist/computer-use/bridge.js +361 -0
- package/dist/computer-use/discovery.d.ts +14 -0
- package/dist/computer-use/discovery.js +117 -0
- package/dist/computer-use/frames.d.ts +9 -0
- package/dist/computer-use/frames.js +100 -0
- package/dist/computer-use/host.d.ts +90 -0
- package/dist/computer-use/host.js +765 -0
- package/dist/computer-use/launch.d.ts +55 -0
- package/dist/computer-use/launch.js +375 -0
- package/dist/computer-use/protocol.d.ts +238 -0
- package/dist/computer-use/protocol.js +441 -0
- package/dist/connect.d.ts +7 -0
- package/dist/connect.js +6 -1
- package/dist/daemon-client.js +1 -1
- package/dist/daemon.js +15 -3
- package/dist/devices-status.d.ts +1 -1
- package/dist/devices-status.js +2 -5
- package/dist/executor.d.ts +2 -1
- package/dist/executor.js +124 -134
- package/dist/host-connection.js +3 -3
- package/dist/host.d.ts +4 -1
- package/dist/host.js +2 -0
- package/package.json +5 -1
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { type Socket } from "node:net";
|
|
2
|
+
export declare const APP_NAME = "Rome Computer Use.app";
|
|
3
|
+
export declare const RELEASE_SIGNING_REQUIREMENT = "anchor apple generic and identifier \"com.rome.computer-use\" and certificate 1[field.1.2.840.113635.100.6.2.6] and certificate leaf[field.1.2.840.113635.100.6.1.13] and certificate leaf[subject.OU] = \"4YWXB26F39\"";
|
|
4
|
+
export declare function stagedHelperPath(home?: string): string;
|
|
5
|
+
interface LaunchContext {
|
|
6
|
+
appPath?: string;
|
|
7
|
+
stateDir: string;
|
|
8
|
+
signal: AbortSignal;
|
|
9
|
+
}
|
|
10
|
+
export interface LaunchOptions {
|
|
11
|
+
appPath?: string;
|
|
12
|
+
signal?: AbortSignal;
|
|
13
|
+
stateRoot?: string;
|
|
14
|
+
startupTimeoutMs?: number;
|
|
15
|
+
verifyBundle?: (appPath: string, signal: AbortSignal) => void | Promise<void>;
|
|
16
|
+
launcher?: (context: LaunchContext) => void | Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
export type PermissionSetupPane = "accessibility" | "screen-recording" | "all";
|
|
19
|
+
export interface PermissionSetupOptions {
|
|
20
|
+
appPath: string;
|
|
21
|
+
pane?: PermissionSetupPane;
|
|
22
|
+
locale?: string;
|
|
23
|
+
signal?: AbortSignal;
|
|
24
|
+
startupTimeoutMs?: number;
|
|
25
|
+
verifyBundle?: LaunchOptions["verifyBundle"];
|
|
26
|
+
launcher?: (args: string[], signal: AbortSignal) => void | Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
export declare function untilAbort<T>(runOperation: () => T | Promise<T>, signal: AbortSignal): Promise<T>;
|
|
29
|
+
export declare function verifyHelperBundle(path: string, signal: AbortSignal, verify?: NonNullable<LaunchOptions["verifyBundle"]>): Promise<string>;
|
|
30
|
+
export declare function launchPermissionSetup(options: PermissionSetupOptions): Promise<void>;
|
|
31
|
+
export declare function probeHelperPermissions(path: string): Promise<boolean | undefined>;
|
|
32
|
+
interface HelperPermissionCheck {
|
|
33
|
+
result: boolean | undefined;
|
|
34
|
+
checkedAt: number;
|
|
35
|
+
}
|
|
36
|
+
export interface HelperPermissionCache {
|
|
37
|
+
peek(path: string): HelperPermissionCheck | undefined;
|
|
38
|
+
check(path: string, options?: {
|
|
39
|
+
ttlMs?: number;
|
|
40
|
+
force?: boolean;
|
|
41
|
+
forceCooldownMs?: number;
|
|
42
|
+
}): Promise<boolean | undefined>;
|
|
43
|
+
invalidate(path?: string): void;
|
|
44
|
+
onChange(listener: () => void): () => void;
|
|
45
|
+
}
|
|
46
|
+
export declare function createHelperPermissionCache({ probe, now, }?: {
|
|
47
|
+
probe?: typeof probeHelperPermissions;
|
|
48
|
+
now?: () => number;
|
|
49
|
+
}): HelperPermissionCache;
|
|
50
|
+
export declare function launchHelper(options: LaunchOptions): Promise<{
|
|
51
|
+
socket: Socket;
|
|
52
|
+
secret: NonSharedBuffer;
|
|
53
|
+
cleanup: () => void;
|
|
54
|
+
}>;
|
|
55
|
+
export {};
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { randomBytes } from "node:crypto";
|
|
3
|
+
import { addAbortListener, once } from "node:events";
|
|
4
|
+
import { accessSync, chmodSync, closeSync, constants, fchmodSync, lstatSync, mkdtempSync, openSync, realpathSync, rmSync, writeFileSync, } from "node:fs";
|
|
5
|
+
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
|
6
|
+
import { createConnection } from "node:net";
|
|
7
|
+
import { homedir, tmpdir } from "node:os";
|
|
8
|
+
import { basename, join } from "node:path";
|
|
9
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
10
|
+
import { promisify } from "node:util";
|
|
11
|
+
import { isRecord } from "../actions.js";
|
|
12
|
+
import { ComputerUseError } from "./protocol.js";
|
|
13
|
+
export const APP_NAME = "Rome Computer Use.app";
|
|
14
|
+
const EXECUTABLE = "Contents/MacOS/RomeComputerUse";
|
|
15
|
+
export const RELEASE_SIGNING_REQUIREMENT = 'anchor apple generic and identifier "com.rome.computer-use" and certificate 1[field.1.2.840.113635.100.6.2.6] and certificate leaf[field.1.2.840.113635.100.6.1.13] and certificate leaf[subject.OU] = "4YWXB26F39"';
|
|
16
|
+
const SOCKET_NAME = "helper.sock";
|
|
17
|
+
const STATE_PREFIX = "rome-cu-";
|
|
18
|
+
const SOCKET_PATH_LIMIT = 103;
|
|
19
|
+
const POLL_INTERVAL_MS = 25;
|
|
20
|
+
const run = promisify(execFile);
|
|
21
|
+
export function stagedHelperPath(home = homedir()) {
|
|
22
|
+
return join(home, ".rome-computer-use", APP_NAME);
|
|
23
|
+
}
|
|
24
|
+
const exitCleanups = new Set();
|
|
25
|
+
function cleanOnExit() {
|
|
26
|
+
for (const cleanup of exitCleanups) {
|
|
27
|
+
try {
|
|
28
|
+
cleanup();
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function stat(path) {
|
|
35
|
+
return lstatSync(path, { bigint: true, throwIfNoEntry: false });
|
|
36
|
+
}
|
|
37
|
+
function sameInode(before, after) {
|
|
38
|
+
return before.dev === after?.dev && before.ino === after.ino;
|
|
39
|
+
}
|
|
40
|
+
function privateMode(info, uid, mode) {
|
|
41
|
+
return info.uid === BigInt(uid) && (info.mode & 4095n) === mode;
|
|
42
|
+
}
|
|
43
|
+
function prepareState(root, uid, secret) {
|
|
44
|
+
const prefix = join(realpathSync(root), STATE_PREFIX);
|
|
45
|
+
if (Buffer.byteLength(join(`${prefix}XXXXXX`, SOCKET_NAME)) > SOCKET_PATH_LIMIT) {
|
|
46
|
+
throw new ComputerUseError("socket_path_limit");
|
|
47
|
+
}
|
|
48
|
+
const stateDir = mkdtempSync(prefix);
|
|
49
|
+
const identity = lstatSync(stateDir, { bigint: true });
|
|
50
|
+
const socketPath = join(stateDir, SOCKET_NAME);
|
|
51
|
+
const cleanup = () => {
|
|
52
|
+
exitCleanups.delete(cleanup);
|
|
53
|
+
if (exitCleanups.size === 0)
|
|
54
|
+
process.off("exit", cleanOnExit);
|
|
55
|
+
if (sameInode(identity, stat(stateDir))) {
|
|
56
|
+
rmSync(stateDir, { force: true, recursive: true, maxRetries: 2, retryDelay: 10 });
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
if (exitCleanups.size === 0)
|
|
60
|
+
process.on("exit", cleanOnExit);
|
|
61
|
+
exitCleanups.add(cleanup);
|
|
62
|
+
try {
|
|
63
|
+
chmodSync(stateDir, 0o700);
|
|
64
|
+
const bootstrap = JSON.stringify({
|
|
65
|
+
secret: secret.toString("hex"),
|
|
66
|
+
hostPid: process.pid,
|
|
67
|
+
hostExecutable: realpathSync(process.execPath),
|
|
68
|
+
});
|
|
69
|
+
const fd = openSync(join(stateDir, "bootstrap.json"), constants.O_CREAT | constants.O_EXCL | constants.O_WRONLY, 0o600);
|
|
70
|
+
try {
|
|
71
|
+
fchmodSync(fd, 0o600);
|
|
72
|
+
writeFileSync(fd, bootstrap);
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
closeSync(fd);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch (cause) {
|
|
79
|
+
cleanup();
|
|
80
|
+
throw cause;
|
|
81
|
+
}
|
|
82
|
+
const inspectSocket = () => {
|
|
83
|
+
const dir = stat(stateDir);
|
|
84
|
+
if (!dir?.isDirectory() ||
|
|
85
|
+
!privateMode(dir, uid, 448n) ||
|
|
86
|
+
!sameInode(identity, dir) ||
|
|
87
|
+
realpathSync(stateDir) !== stateDir) {
|
|
88
|
+
throw new ComputerUseError("unsafe_state_directory");
|
|
89
|
+
}
|
|
90
|
+
const info = stat(socketPath);
|
|
91
|
+
if (info && (!info.isSocket() || !privateMode(info, uid, 384n))) {
|
|
92
|
+
throw new ComputerUseError("socket_unavailable");
|
|
93
|
+
}
|
|
94
|
+
return info;
|
|
95
|
+
};
|
|
96
|
+
return { stateDir, socketPath, cleanup, inspectSocket };
|
|
97
|
+
}
|
|
98
|
+
export async function untilAbort(runOperation, signal) {
|
|
99
|
+
signal.throwIfAborted();
|
|
100
|
+
let abort;
|
|
101
|
+
const stopped = new Promise((_, reject) => {
|
|
102
|
+
abort = () => reject(signal.reason);
|
|
103
|
+
});
|
|
104
|
+
const listener = addAbortListener(signal, abort);
|
|
105
|
+
try {
|
|
106
|
+
return await Promise.race([
|
|
107
|
+
Promise.resolve().then(() => {
|
|
108
|
+
signal.throwIfAborted();
|
|
109
|
+
return runOperation();
|
|
110
|
+
}),
|
|
111
|
+
stopped,
|
|
112
|
+
]);
|
|
113
|
+
}
|
|
114
|
+
finally {
|
|
115
|
+
listener[Symbol.dispose]();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
async function verifySignedBundle(appPath, signal) {
|
|
119
|
+
await run("/usr/bin/codesign", ["--verify", "--deep", "--strict", `-R=${RELEASE_SIGNING_REQUIREMENT}`, appPath], { signal });
|
|
120
|
+
}
|
|
121
|
+
export async function verifyHelperBundle(path, signal, verify = verifySignedBundle) {
|
|
122
|
+
try {
|
|
123
|
+
const appPath = realpathSync(path);
|
|
124
|
+
const executable = join(appPath, EXECUTABLE);
|
|
125
|
+
if ((path === stagedHelperPath() && appPath !== stagedHelperPath(realpathSync(homedir()))) ||
|
|
126
|
+
basename(appPath) !== APP_NAME ||
|
|
127
|
+
!stat(appPath)?.isDirectory() ||
|
|
128
|
+
!stat(executable)?.isFile() ||
|
|
129
|
+
realpathSync(executable) !== executable) {
|
|
130
|
+
throw new Error("The helper app or its executable is missing");
|
|
131
|
+
}
|
|
132
|
+
accessSync(executable, constants.X_OK);
|
|
133
|
+
await untilAbort(() => verify(appPath, signal), signal);
|
|
134
|
+
return appPath;
|
|
135
|
+
}
|
|
136
|
+
catch (cause) {
|
|
137
|
+
throw new ComputerUseError("invalid_helper_bundle", { cause });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
async function readOptionalFile(path, signal) {
|
|
141
|
+
try {
|
|
142
|
+
return await readFile(path, { encoding: "utf8", signal });
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT")
|
|
146
|
+
return "";
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const SETUP_FAILURE_CODES = new Set([
|
|
151
|
+
"invalid_request",
|
|
152
|
+
"desktop_disabled",
|
|
153
|
+
"console_unavailable",
|
|
154
|
+
"permission_setup_unavailable",
|
|
155
|
+
]);
|
|
156
|
+
function helperSetupFailure(output) {
|
|
157
|
+
for (const line of output.split(/\r?\n/)) {
|
|
158
|
+
const code = line.trim();
|
|
159
|
+
if (!SETUP_FAILURE_CODES.has(code))
|
|
160
|
+
continue;
|
|
161
|
+
if (code === "invalid_request" ||
|
|
162
|
+
code === "desktop_disabled" ||
|
|
163
|
+
code === "console_unavailable") {
|
|
164
|
+
return new ComputerUseError(code);
|
|
165
|
+
}
|
|
166
|
+
return new ComputerUseError("internal_error", { cause: new Error(code) });
|
|
167
|
+
}
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
export async function launchPermissionSetup(options) {
|
|
171
|
+
if (!options.launcher && process.platform !== "darwin") {
|
|
172
|
+
throw new ComputerUseError("computer_use_unsupported_platform");
|
|
173
|
+
}
|
|
174
|
+
const waitMs = options.startupTimeoutMs ?? 2500;
|
|
175
|
+
const timeout = AbortSignal.timeout(Math.max(5000, waitMs + 2500));
|
|
176
|
+
const signal = options.signal ? AbortSignal.any([options.signal, timeout]) : timeout;
|
|
177
|
+
const app = await untilAbort(() => verifyHelperBundle(options.appPath, signal, options.verifyBundle), signal);
|
|
178
|
+
const directory = await mkdtemp(join(tmpdir(), "rome-cu-setup-"));
|
|
179
|
+
const stdoutPath = join(directory, "stdout.txt");
|
|
180
|
+
const stderrPath = join(directory, "stderr.txt");
|
|
181
|
+
const args = [
|
|
182
|
+
"-n",
|
|
183
|
+
"--stdout",
|
|
184
|
+
stdoutPath,
|
|
185
|
+
"--stderr",
|
|
186
|
+
stderrPath,
|
|
187
|
+
"-a",
|
|
188
|
+
app,
|
|
189
|
+
"--args",
|
|
190
|
+
"--setup-permissions",
|
|
191
|
+
];
|
|
192
|
+
if (options.pane !== undefined)
|
|
193
|
+
args.push(options.pane);
|
|
194
|
+
if (options.locale !== undefined)
|
|
195
|
+
args.push("--locale", options.locale);
|
|
196
|
+
const launch = options.launcher ??
|
|
197
|
+
(async (launchArgs, launchSignal) => {
|
|
198
|
+
await run("/usr/bin/open", launchArgs, { signal: launchSignal });
|
|
199
|
+
});
|
|
200
|
+
try {
|
|
201
|
+
await untilAbort(() => launch(args, signal), signal);
|
|
202
|
+
const deadline = Date.now() + waitMs;
|
|
203
|
+
while (true) {
|
|
204
|
+
signal.throwIfAborted();
|
|
205
|
+
const stdout = await readOptionalFile(stdoutPath, signal);
|
|
206
|
+
if (stdout.split(/\r?\n/).includes("ready"))
|
|
207
|
+
return;
|
|
208
|
+
const stderr = await readOptionalFile(stderrPath, signal);
|
|
209
|
+
const failure = helperSetupFailure(stderr);
|
|
210
|
+
if (failure)
|
|
211
|
+
throw failure;
|
|
212
|
+
const remaining = deadline - Date.now();
|
|
213
|
+
if (remaining <= 0) {
|
|
214
|
+
if (stderr.trim()) {
|
|
215
|
+
throw new ComputerUseError("internal_error", { cause: new Error(stderr.trim()) });
|
|
216
|
+
}
|
|
217
|
+
throw new ComputerUseError("startup_timeout");
|
|
218
|
+
}
|
|
219
|
+
await delay(Math.min(POLL_INTERVAL_MS, remaining), undefined, { signal });
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
finally {
|
|
223
|
+
await rm(directory, { force: true, recursive: true });
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
export async function probeHelperPermissions(path) {
|
|
227
|
+
const signal = AbortSignal.timeout(2500);
|
|
228
|
+
const app = await verifyHelperBundle(path, signal);
|
|
229
|
+
const directory = await mkdtemp("/private/tmp/rome-cu-permissions-");
|
|
230
|
+
const output = join(directory, "permissions.json");
|
|
231
|
+
try {
|
|
232
|
+
const args = ["-n", "-g", "-j", "-W", "--stdout", output, "--stderr", "/dev/null"];
|
|
233
|
+
args.push("-a", app, "--args", "--check-permissions");
|
|
234
|
+
await run("/usr/bin/open", args, { signal }).catch(() => {
|
|
235
|
+
});
|
|
236
|
+
const result = JSON.parse(await readFile(output, { encoding: "utf8", signal }));
|
|
237
|
+
if (isRecord(result) &&
|
|
238
|
+
typeof result.accessibility === "boolean" &&
|
|
239
|
+
typeof result.screenRecording === "boolean") {
|
|
240
|
+
return result.accessibility && result.screenRecording;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
finally {
|
|
244
|
+
await rm(directory, { force: true, recursive: true });
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
export function createHelperPermissionCache({ probe = probeHelperPermissions, now = Date.now, } = {}) {
|
|
248
|
+
const entries = new Map();
|
|
249
|
+
const listeners = new Set();
|
|
250
|
+
const notify = () => {
|
|
251
|
+
for (const listener of listeners)
|
|
252
|
+
listener();
|
|
253
|
+
};
|
|
254
|
+
return {
|
|
255
|
+
peek: (path) => entries.get(path)?.checked,
|
|
256
|
+
check(path, { ttlMs = 30_000, force = false, forceCooldownMs = 2_000 } = {}) {
|
|
257
|
+
const entry = entries.get(path) ?? {};
|
|
258
|
+
if (entry.pending)
|
|
259
|
+
return entry.pending;
|
|
260
|
+
const { checked } = entry;
|
|
261
|
+
if (checked && now() - checked.checkedAt < (force ? forceCooldownMs : ttlMs)) {
|
|
262
|
+
return Promise.resolve(checked.result);
|
|
263
|
+
}
|
|
264
|
+
entries.set(path, entry);
|
|
265
|
+
entry.pending = Promise.resolve()
|
|
266
|
+
.then(() => (entries.get(path) === entry ? probe(path) : undefined))
|
|
267
|
+
.catch(() => undefined)
|
|
268
|
+
.then((result) => {
|
|
269
|
+
if (entries.get(path) === entry) {
|
|
270
|
+
entry.checked = { result, checkedAt: now() };
|
|
271
|
+
entry.pending = undefined;
|
|
272
|
+
notify();
|
|
273
|
+
}
|
|
274
|
+
return result;
|
|
275
|
+
});
|
|
276
|
+
return entry.pending;
|
|
277
|
+
},
|
|
278
|
+
invalidate(path) {
|
|
279
|
+
if (path !== undefined) {
|
|
280
|
+
if (!entries.delete(path))
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
if (!entries.size)
|
|
285
|
+
return;
|
|
286
|
+
entries.clear();
|
|
287
|
+
}
|
|
288
|
+
notify();
|
|
289
|
+
},
|
|
290
|
+
onChange(listener) {
|
|
291
|
+
listeners.add(listener);
|
|
292
|
+
return () => {
|
|
293
|
+
listeners.delete(listener);
|
|
294
|
+
};
|
|
295
|
+
},
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
async function connect(socketPath, signal) {
|
|
299
|
+
signal.throwIfAborted();
|
|
300
|
+
const socket = createConnection(socketPath);
|
|
301
|
+
try {
|
|
302
|
+
await once(socket, "connect", { signal });
|
|
303
|
+
return socket;
|
|
304
|
+
}
|
|
305
|
+
catch (error) {
|
|
306
|
+
socket.destroy();
|
|
307
|
+
if (error instanceof Error &&
|
|
308
|
+
"code" in error &&
|
|
309
|
+
(error.code === "ENOENT" || error.code === "ECONNREFUSED"))
|
|
310
|
+
return;
|
|
311
|
+
throw error;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
export async function launchHelper(options) {
|
|
315
|
+
const uid = process.getuid?.();
|
|
316
|
+
if (uid === undefined || (!options.launcher && process.platform !== "darwin")) {
|
|
317
|
+
throw new ComputerUseError("socket_unavailable");
|
|
318
|
+
}
|
|
319
|
+
const secret = randomBytes(32);
|
|
320
|
+
const deadline = new AbortController();
|
|
321
|
+
const timer = setTimeout(() => deadline.abort(), options.startupTimeoutMs ?? 10_000);
|
|
322
|
+
const signal = options.signal
|
|
323
|
+
? AbortSignal.any([options.signal, deadline.signal])
|
|
324
|
+
: deadline.signal;
|
|
325
|
+
let state;
|
|
326
|
+
let socket;
|
|
327
|
+
try {
|
|
328
|
+
signal.throwIfAborted();
|
|
329
|
+
const appPath = options.appPath
|
|
330
|
+
? await verifyHelperBundle(options.appPath, signal, options.verifyBundle)
|
|
331
|
+
: undefined;
|
|
332
|
+
if (!appPath && !options.launcher)
|
|
333
|
+
throw new ComputerUseError("invalid_helper_bundle");
|
|
334
|
+
signal.throwIfAborted();
|
|
335
|
+
state = prepareState(options.stateRoot ?? (process.platform === "darwin" ? "/private/tmp" : "/tmp"), uid, secret);
|
|
336
|
+
const { stateDir, socketPath, inspectSocket, cleanup } = state;
|
|
337
|
+
if (options.launcher) {
|
|
338
|
+
const launch = options.launcher;
|
|
339
|
+
await untilAbort(() => launch({ appPath, stateDir, signal }), signal);
|
|
340
|
+
}
|
|
341
|
+
else {
|
|
342
|
+
await run("/usr/bin/open", ["-n", "-a", appPath, "--args", "--serve", stateDir], {
|
|
343
|
+
signal,
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
while (!signal.aborted) {
|
|
347
|
+
const before = inspectSocket();
|
|
348
|
+
if (before) {
|
|
349
|
+
socket = await connect(socketPath, signal);
|
|
350
|
+
if (socket) {
|
|
351
|
+
if (!sameInode(before, inspectSocket()))
|
|
352
|
+
throw new ComputerUseError("socket_unavailable");
|
|
353
|
+
return { socket, secret, cleanup };
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
await delay(POLL_INTERVAL_MS, undefined, { signal });
|
|
357
|
+
}
|
|
358
|
+
throw signal.reason;
|
|
359
|
+
}
|
|
360
|
+
catch (cause) {
|
|
361
|
+
socket?.destroy();
|
|
362
|
+
secret.fill(0);
|
|
363
|
+
state?.cleanup();
|
|
364
|
+
if (options.signal?.aborted)
|
|
365
|
+
throw new ComputerUseError("request_aborted");
|
|
366
|
+
if (deadline.signal.aborted)
|
|
367
|
+
throw new ComputerUseError("startup_timeout");
|
|
368
|
+
if (cause instanceof ComputerUseError)
|
|
369
|
+
throw cause;
|
|
370
|
+
throw new ComputerUseError("socket_unavailable", { cause });
|
|
371
|
+
}
|
|
372
|
+
finally {
|
|
373
|
+
clearTimeout(timer);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
export declare const JSON_LIMIT: number;
|
|
2
|
+
export declare const PNG_LIMIT: number;
|
|
3
|
+
declare const wireErrorCodes: readonly ["invalid_frame", "frame_limit", "invalid_request", "unsupported_version", "method_unavailable", "authentication_failed", "auth_required", "auth_failed", "consent_denied", "consent_expired", "cancelled", "needs_system_permission", "permissions_expired", "key_store_unavailable", "trusted_key_limit", "key_revoked", "connection_closed", "desktop_disabled", "console_unavailable", "accessibility_permission_required", "screen_recording_permission_required", "session_stopped", "cancel_requested", "busy", "window_unavailable", "window_already_bound", "window_coordinate_out_of_bounds", "window_focus_unavailable", "window_occluded", "process_identity_changed", "binding_unavailable", "binding_not_found", "element_unavailable", "key_unavailable", "action_unavailable", "invalid_value", "action_rejected", "action_outcome_unknown", "capture_failed", "capture_limit", "internal_error"];
|
|
4
|
+
type WireErrorCode = (typeof wireErrorCodes)[number];
|
|
5
|
+
type ErrorCode = "consent_required" | "pending_consent" | WireErrorCode | "computer_use_unsupported_platform" | "computer_use_helper_missing" | "invalid_helper_bundle" | "socket_path_limit" | "unsafe_state_directory" | "socket_unavailable" | "startup_timeout" | "request_timeout" | "request_aborted";
|
|
6
|
+
export declare class ComputerUseError extends Error {
|
|
7
|
+
readonly code: ErrorCode;
|
|
8
|
+
constructor(code: ErrorCode, options?: ErrorOptions);
|
|
9
|
+
get retryable(): boolean;
|
|
10
|
+
get actionOutcomeUnknown(): boolean;
|
|
11
|
+
}
|
|
12
|
+
declare const actions: readonly ["press", "set_value", "increment", "decrement"];
|
|
13
|
+
type Action = (typeof actions)[number];
|
|
14
|
+
type Value = string | boolean | number;
|
|
15
|
+
type AXAction = {
|
|
16
|
+
element: string;
|
|
17
|
+
} & ({
|
|
18
|
+
action: "set_value";
|
|
19
|
+
value: Value;
|
|
20
|
+
} | {
|
|
21
|
+
action: Exclude<Action, "set_value">;
|
|
22
|
+
value?: never;
|
|
23
|
+
});
|
|
24
|
+
type Point = {
|
|
25
|
+
x: number;
|
|
26
|
+
y: number;
|
|
27
|
+
};
|
|
28
|
+
type Button = "left" | "right" | "middle";
|
|
29
|
+
type Modifiers = ("command" | "control" | "option" | "shift")[];
|
|
30
|
+
type InputAction = ({
|
|
31
|
+
modifiers?: Modifiers;
|
|
32
|
+
} & (({
|
|
33
|
+
action: "click";
|
|
34
|
+
button?: Button;
|
|
35
|
+
clickCount?: number;
|
|
36
|
+
} & Point) | ({
|
|
37
|
+
action: "move";
|
|
38
|
+
} & Point) | ({
|
|
39
|
+
action: "drag";
|
|
40
|
+
toX: number;
|
|
41
|
+
toY: number;
|
|
42
|
+
button?: Button;
|
|
43
|
+
steps?: number;
|
|
44
|
+
} & Point) | ({
|
|
45
|
+
action: "scroll";
|
|
46
|
+
deltaX: number;
|
|
47
|
+
deltaY: number;
|
|
48
|
+
} & Point) | {
|
|
49
|
+
action: "key_press";
|
|
50
|
+
key: string;
|
|
51
|
+
})) | {
|
|
52
|
+
action: "type_text";
|
|
53
|
+
text: string;
|
|
54
|
+
};
|
|
55
|
+
export type ActParams = {
|
|
56
|
+
binding: string;
|
|
57
|
+
} & (AXAction | InputAction);
|
|
58
|
+
export type ConsentMode = "session_only" | "persistent_only" | "session_or_persistent";
|
|
59
|
+
type Locale = "en" | "ja" | "zh-Hans" | "zh-Hant";
|
|
60
|
+
export interface ControllerClaim {
|
|
61
|
+
publicKey: string;
|
|
62
|
+
label: string;
|
|
63
|
+
scope: "remote" | "local";
|
|
64
|
+
}
|
|
65
|
+
interface HelperIdentity {
|
|
66
|
+
instance: string;
|
|
67
|
+
helperPid: number;
|
|
68
|
+
helperProof: string;
|
|
69
|
+
}
|
|
70
|
+
interface ConsentChallenge {
|
|
71
|
+
status: "challenge_required";
|
|
72
|
+
challenge: string;
|
|
73
|
+
fingerprint: string;
|
|
74
|
+
locale: Locale;
|
|
75
|
+
}
|
|
76
|
+
interface SessionGrant {
|
|
77
|
+
access?: HelperAccess;
|
|
78
|
+
auth: "session";
|
|
79
|
+
locale: Locale;
|
|
80
|
+
}
|
|
81
|
+
interface ControllerGrant {
|
|
82
|
+
access?: HelperAccess;
|
|
83
|
+
auth: "session" | "persistent";
|
|
84
|
+
locale: Locale;
|
|
85
|
+
fingerprint: string;
|
|
86
|
+
enrolled: boolean;
|
|
87
|
+
label: string;
|
|
88
|
+
scope: ControllerClaim["scope"];
|
|
89
|
+
}
|
|
90
|
+
export interface ScreenshotDescriptor {
|
|
91
|
+
mimeType: "image/png";
|
|
92
|
+
width: number;
|
|
93
|
+
height: number;
|
|
94
|
+
bytes: number;
|
|
95
|
+
}
|
|
96
|
+
interface WindowRecord {
|
|
97
|
+
window: number;
|
|
98
|
+
appName: string;
|
|
99
|
+
bundleName: string;
|
|
100
|
+
bundleId: string;
|
|
101
|
+
title: string;
|
|
102
|
+
bounds: [number, number, number, number];
|
|
103
|
+
}
|
|
104
|
+
interface TreeNode {
|
|
105
|
+
ref: string;
|
|
106
|
+
role: string;
|
|
107
|
+
actions: Action[];
|
|
108
|
+
children: TreeNode[];
|
|
109
|
+
title?: string;
|
|
110
|
+
description?: string;
|
|
111
|
+
value?: Value;
|
|
112
|
+
valueTruncated?: true;
|
|
113
|
+
}
|
|
114
|
+
type StopResult = {
|
|
115
|
+
stop: "cancel_requested" | "stop_confirmed";
|
|
116
|
+
reason: WireErrorCode;
|
|
117
|
+
};
|
|
118
|
+
type StopStatus = {
|
|
119
|
+
stop: "running";
|
|
120
|
+
reason?: never;
|
|
121
|
+
} | StopResult;
|
|
122
|
+
export type MissingPermission = "accessibility" | "screen_recording";
|
|
123
|
+
export type HelperAccess = {
|
|
124
|
+
revision: number;
|
|
125
|
+
} & ({
|
|
126
|
+
status: "authorized";
|
|
127
|
+
} | {
|
|
128
|
+
status: "needs_system_permission";
|
|
129
|
+
missing: MissingPermission[];
|
|
130
|
+
guideOpened: boolean;
|
|
131
|
+
});
|
|
132
|
+
export interface RequestParams {
|
|
133
|
+
hello: {
|
|
134
|
+
nonce: string;
|
|
135
|
+
proof: string;
|
|
136
|
+
locale?: string;
|
|
137
|
+
consentMode?: ConsentMode;
|
|
138
|
+
consentTimeoutMs?: number;
|
|
139
|
+
includeAccess?: boolean;
|
|
140
|
+
controller?: ControllerClaim;
|
|
141
|
+
};
|
|
142
|
+
authorize: {
|
|
143
|
+
signature: string;
|
|
144
|
+
};
|
|
145
|
+
access_status: {
|
|
146
|
+
afterRevision: number;
|
|
147
|
+
} | undefined;
|
|
148
|
+
open_permission_guide: undefined;
|
|
149
|
+
status: undefined;
|
|
150
|
+
windows: {
|
|
151
|
+
application?: string;
|
|
152
|
+
} | undefined;
|
|
153
|
+
bind: {
|
|
154
|
+
window: number;
|
|
155
|
+
};
|
|
156
|
+
unbind: {
|
|
157
|
+
binding: string;
|
|
158
|
+
};
|
|
159
|
+
observe: {
|
|
160
|
+
binding: string;
|
|
161
|
+
element?: string;
|
|
162
|
+
screenshot?: boolean;
|
|
163
|
+
};
|
|
164
|
+
act: ActParams;
|
|
165
|
+
cancel: undefined;
|
|
166
|
+
stop: undefined;
|
|
167
|
+
}
|
|
168
|
+
export interface Results {
|
|
169
|
+
hello: HelperIdentity & (SessionGrant | ConsentChallenge);
|
|
170
|
+
authorize: ControllerGrant;
|
|
171
|
+
access_status: HelperAccess;
|
|
172
|
+
open_permission_guide: HelperAccess;
|
|
173
|
+
status: {
|
|
174
|
+
instance: string;
|
|
175
|
+
} & StopStatus;
|
|
176
|
+
windows: {
|
|
177
|
+
windows: WindowRecord[];
|
|
178
|
+
};
|
|
179
|
+
bind: {
|
|
180
|
+
binding: string;
|
|
181
|
+
window: WindowRecord;
|
|
182
|
+
};
|
|
183
|
+
unbind: {
|
|
184
|
+
binding: string;
|
|
185
|
+
released: true;
|
|
186
|
+
};
|
|
187
|
+
observe: {
|
|
188
|
+
binding: string;
|
|
189
|
+
tree: TreeNode;
|
|
190
|
+
truncated: boolean;
|
|
191
|
+
refTTLms: number;
|
|
192
|
+
screenshot?: ScreenshotDescriptor;
|
|
193
|
+
};
|
|
194
|
+
act: {
|
|
195
|
+
binding: string;
|
|
196
|
+
dispatched: true;
|
|
197
|
+
};
|
|
198
|
+
cancel: StopResult;
|
|
199
|
+
stop: StopResult;
|
|
200
|
+
}
|
|
201
|
+
export type Method = keyof RequestParams;
|
|
202
|
+
type Request<M extends Method> = {
|
|
203
|
+
v: 1;
|
|
204
|
+
id: string;
|
|
205
|
+
method: M;
|
|
206
|
+
params?: RequestParams[M];
|
|
207
|
+
};
|
|
208
|
+
type Response<M extends Method> = {
|
|
209
|
+
v: 1;
|
|
210
|
+
id: string;
|
|
211
|
+
ok: true;
|
|
212
|
+
result: Results[M];
|
|
213
|
+
} | {
|
|
214
|
+
v: 1;
|
|
215
|
+
id: string | null;
|
|
216
|
+
ok: false;
|
|
217
|
+
error: {
|
|
218
|
+
code: WireErrorCode;
|
|
219
|
+
};
|
|
220
|
+
};
|
|
221
|
+
export type JsonObject = Record<string, unknown>;
|
|
222
|
+
export declare function isObject(value: unknown): value is JsonObject;
|
|
223
|
+
export declare function requestParams<M extends Method>(method: M, params: unknown): RequestParams[M];
|
|
224
|
+
export declare function makeRequest<M extends Method>(id: string, method: M, params: RequestParams[M]): Request<M>;
|
|
225
|
+
export declare function parseResponse<M extends Method>(message: JsonObject, method?: M): Response<M>;
|
|
226
|
+
export declare function screenshotFor(message: JsonObject, binaryLength: number): ScreenshotDescriptor | undefined;
|
|
227
|
+
export declare function checkPng(bytes: Buffer, { width, height }: ScreenshotDescriptor): void;
|
|
228
|
+
interface HandshakeContext {
|
|
229
|
+
secret: Buffer;
|
|
230
|
+
nonce: Buffer;
|
|
231
|
+
hostPid: number;
|
|
232
|
+
}
|
|
233
|
+
type ProofRole = ["host"] | ["helper", helperPid: number, instance: string];
|
|
234
|
+
export declare function createProof(context: HandshakeContext, ...role: ProofRole): string;
|
|
235
|
+
export declare function verifyProof(proof: string, context: HandshakeContext, ...role: ProofRole): boolean;
|
|
236
|
+
export declare function controllerFingerprint(publicKey: string): string;
|
|
237
|
+
export declare function authorizationPayload(hello: HelperIdentity & ConsentChallenge, hostPid: number, controller: ControllerClaim, mode?: ConsentMode): Buffer;
|
|
238
|
+
export {};
|