@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
package/README.md
CHANGED
|
@@ -53,9 +53,8 @@ devices without asserting that they are online.
|
|
|
53
53
|
`renameDevice(deviceId, name)` and `removeDevice(deviceId)` change the account's
|
|
54
54
|
device list in Cloud and resolve with no value. A failure throws
|
|
55
55
|
`DeviceActionError` with code `device_not_found`, `invalid_device_name`,
|
|
56
|
-
`invalid_device_session`, or `cloud_unavailable`. `removeDevice`
|
|
57
|
-
device
|
|
58
|
-
the revocation. The device stays listed as revoked until a later call deletes it.
|
|
56
|
+
`invalid_device_session`, or `cloud_unavailable`. `removeDevice` ends the
|
|
57
|
+
device's token and connection and deletes it from the account in one call.
|
|
59
58
|
|
|
60
59
|
`runBinary(deviceId, action, args?, body?)` resolves to `{ response, body }`.
|
|
61
60
|
It sends `body` as input bytes in a binary frame. For `exec`, the input is the
|
|
@@ -81,8 +80,8 @@ See [copying files](../../docs/rome-node.md#copying-files).
|
|
|
81
80
|
`getDevicesStatus()` checks device reachability through read-only `system.info`
|
|
82
81
|
requests through an existing daemon and returns `{ connection, checkedAt, devices }`.
|
|
83
82
|
It returns `null` when no daemon runs and never starts or restarts one. Each device has
|
|
84
|
-
`id`, `name`, `platform`, and status `connected`, `not_connected`, `unknown
|
|
85
|
-
|
|
83
|
+
`id`, `name`, `platform`, and status `connected`, `not_connected`, or `unknown`.
|
|
84
|
+
The daemon shares concurrent checks and caches snapshots for ten
|
|
86
85
|
seconds. A timeout is unknown, not proof that a device is offline. This call
|
|
87
86
|
opens the shared Gateway connection when there are active devices to check.
|
|
88
87
|
|
package/dist/actions.d.ts
CHANGED
|
@@ -13,12 +13,13 @@ export type ActionResponse = {
|
|
|
13
13
|
error: {
|
|
14
14
|
code: string;
|
|
15
15
|
message: string;
|
|
16
|
+
details?: Record<string, unknown>;
|
|
16
17
|
};
|
|
17
18
|
};
|
|
18
19
|
export interface BinaryActionResult {
|
|
19
20
|
response: ActionResponse;
|
|
20
21
|
body: Uint8Array;
|
|
21
22
|
}
|
|
22
|
-
export declare function actionError(code: string, message: string): ActionResponse;
|
|
23
|
+
export declare function actionError(code: string, message: string, details?: Record<string, unknown>): ActionResponse;
|
|
23
24
|
export declare function isRecord(value: unknown): value is Record<string, unknown>;
|
|
24
25
|
export declare function parseResponse(value: unknown): ActionResponse | null;
|
package/dist/actions.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export function actionError(code, message) {
|
|
2
|
-
return { type: "response", ok: false, error: { code, message } };
|
|
1
|
+
export function actionError(code, message, details) {
|
|
2
|
+
return { type: "response", ok: false, error: { code, message, ...(details && { details }) } };
|
|
3
3
|
}
|
|
4
4
|
export function isRecord(value) {
|
|
5
5
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
@@ -13,6 +13,6 @@ export function parseResponse(value) {
|
|
|
13
13
|
isRecord(value.error) &&
|
|
14
14
|
typeof value.error.code === "string" &&
|
|
15
15
|
typeof value.error.message === "string")
|
|
16
|
-
return actionError(value.error.code, value.error.message);
|
|
16
|
+
return actionError(value.error.code, value.error.message, isRecord(value.error.details) ? value.error.details : undefined);
|
|
17
17
|
return null;
|
|
18
18
|
}
|
package/dist/audit.d.ts
CHANGED
|
@@ -11,6 +11,11 @@ export type AuditStatus = {
|
|
|
11
11
|
code: AuditUnavailableCode;
|
|
12
12
|
message: string;
|
|
13
13
|
};
|
|
14
|
+
export interface AuditContentPreview {
|
|
15
|
+
size: number;
|
|
16
|
+
preview: Buffer;
|
|
17
|
+
truncated: boolean;
|
|
18
|
+
}
|
|
14
19
|
export interface AuditEvent {
|
|
15
20
|
seq: number;
|
|
16
21
|
at: number;
|
|
@@ -108,5 +113,6 @@ export declare class AuditStore {
|
|
|
108
113
|
stats(since: number, until: number, bucketMs: number): AuditStats;
|
|
109
114
|
get(seq: number): AuditItem | null;
|
|
110
115
|
content(seq: number, name: ContentName): Buffer | null;
|
|
116
|
+
contentPreview(seq: number, name: ContentName, maxBytes: number): AuditContentPreview | null;
|
|
111
117
|
summary(): AuditSummary;
|
|
112
118
|
}
|
package/dist/audit.js
CHANGED
|
@@ -472,6 +472,17 @@ COMMIT;`);
|
|
|
472
472
|
.get(seq, name);
|
|
473
473
|
return row ? Buffer.from(row.data) : null;
|
|
474
474
|
}
|
|
475
|
+
contentPreview(seq, name, maxBytes) {
|
|
476
|
+
const limit = Math.max(0, Math.floor(maxBytes));
|
|
477
|
+
const row = this.reader()
|
|
478
|
+
.prepare("SELECT length(data) AS size, substr(data, 1, ?) AS preview FROM content WHERE seq = ? AND name = ?")
|
|
479
|
+
.get(limit, seq, name);
|
|
480
|
+
if (!row)
|
|
481
|
+
return null;
|
|
482
|
+
const size = Number(row.size);
|
|
483
|
+
const preview = Buffer.from(row.preview);
|
|
484
|
+
return { size, preview, truncated: size > preview.byteLength };
|
|
485
|
+
}
|
|
475
486
|
summary() {
|
|
476
487
|
const retention = {
|
|
477
488
|
maxAgeDays: (this.options.maxAgeMs ?? AUDIT_MAX_AGE_MS) / 86_400_000,
|
package/dist/cloud.js
CHANGED
|
@@ -4,9 +4,8 @@ const messages = {
|
|
|
4
4
|
invalid_device_session: "Device authorization is invalid.",
|
|
5
5
|
device_not_found: "No device with this ID exists in the account.",
|
|
6
6
|
invalid_device_name: "Use a name of 1 to 80 characters without control characters.",
|
|
7
|
-
revocation_pending: "The device is revoked. Rome Cloud deletes it after the Gateway confirms the revocation. Remove it again later.",
|
|
8
7
|
};
|
|
9
|
-
const deviceErrors = new Set(["device_not_found", "invalid_device_name"
|
|
8
|
+
const deviceErrors = new Set(["device_not_found", "invalid_device_name"]);
|
|
10
9
|
export class CloudError extends Error {
|
|
11
10
|
code;
|
|
12
11
|
constructor(code) {
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { isRecord } from "../actions.js";
|
|
2
|
+
import { isUuid } from "../frame.js";
|
|
3
|
+
import { requestParams } from "./protocol.js";
|
|
4
|
+
export function computerUseAuditFields(action, args) {
|
|
5
|
+
if (action !== "computer.act" || !isRecord(args))
|
|
6
|
+
return {};
|
|
7
|
+
const { actionId, ...input } = args;
|
|
8
|
+
if (!isUuid(actionId))
|
|
9
|
+
return {};
|
|
10
|
+
const id = actionId.toLowerCase();
|
|
11
|
+
try {
|
|
12
|
+
return { actionId: id, operation: requestParams("act", input).action };
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return { actionId: id };
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type LaunchOptions } from "./launch.js";
|
|
2
|
+
import { type ActParams, ComputerUseError, type ConsentMode, type ControllerClaim, type HelperAccess, type Results, type ScreenshotDescriptor } from "./protocol.js";
|
|
3
|
+
export interface RequestOptions {
|
|
4
|
+
timeoutMs?: number;
|
|
5
|
+
signal?: AbortSignal;
|
|
6
|
+
}
|
|
7
|
+
interface ObserveOptions extends RequestOptions {
|
|
8
|
+
element?: string;
|
|
9
|
+
screenshot?: boolean;
|
|
10
|
+
}
|
|
11
|
+
type ObserveResult = Omit<Results["observe"], "screenshot"> & {
|
|
12
|
+
screenshot?: ScreenshotDescriptor & {
|
|
13
|
+
png: Buffer;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
interface BridgeOptions extends LaunchOptions {
|
|
17
|
+
handshakeTimeoutMs?: number;
|
|
18
|
+
consentTimeoutMs?: number;
|
|
19
|
+
consentDialogTimeoutMs?: number;
|
|
20
|
+
includeAccess?: boolean;
|
|
21
|
+
requestTimeoutMs?: number;
|
|
22
|
+
locale?: string;
|
|
23
|
+
consentMode?: ConsentMode;
|
|
24
|
+
controller?: ControllerClaim & {
|
|
25
|
+
sign(payload: Buffer, signal: AbortSignal): Uint8Array | Promise<Uint8Array>;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export declare class ComputerUseBridge {
|
|
29
|
+
#private;
|
|
30
|
+
private readonly socket;
|
|
31
|
+
private readonly cleanup;
|
|
32
|
+
private readonly timeoutMs;
|
|
33
|
+
private constructor();
|
|
34
|
+
static launch(options: BridgeOptions): Promise<ComputerUseBridge>;
|
|
35
|
+
get initialAccess(): HelperAccess | undefined;
|
|
36
|
+
get instanceId(): string;
|
|
37
|
+
get helperPid(): number;
|
|
38
|
+
get closed(): boolean;
|
|
39
|
+
status(options?: RequestOptions): Promise<Results["status"]>;
|
|
40
|
+
accessStatus(afterRevision?: number, options?: RequestOptions): Promise<Results["access_status"]>;
|
|
41
|
+
openPermissionGuide(): Promise<Results["open_permission_guide"]>;
|
|
42
|
+
windows(application?: string, options?: RequestOptions): Promise<Results["windows"]>;
|
|
43
|
+
bind(window: number, options?: RequestOptions): Promise<Results["bind"]>;
|
|
44
|
+
unbind(binding: string, options?: RequestOptions): Promise<Results["unbind"]>;
|
|
45
|
+
observe(binding: string, options?: ObserveOptions): Promise<ObserveResult>;
|
|
46
|
+
act(params: ActParams, options?: RequestOptions): Promise<Results["act"]>;
|
|
47
|
+
cancel(options?: RequestOptions): Promise<Results["cancel"]>;
|
|
48
|
+
stop(options?: RequestOptions): Promise<Results["stop"]>;
|
|
49
|
+
close(): void;
|
|
50
|
+
onDisconnect(listener: (error: ComputerUseError) => void): () => void;
|
|
51
|
+
onCancelRequested(listener: () => void): void;
|
|
52
|
+
}
|
|
53
|
+
export {};
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { addAbortListener } from "node:events";
|
|
3
|
+
import { encodeFrame, FrameDecoder } from "./frames.js";
|
|
4
|
+
import { launchHelper, untilAbort } from "./launch.js";
|
|
5
|
+
import { authorizationPayload, ComputerUseError, controllerFingerprint, createProof, makeRequest, parseResponse, verifyProof, } from "./protocol.js";
|
|
6
|
+
const MAX_UNANSWERED_REQUESTS = 256;
|
|
7
|
+
function validTimeout(value) {
|
|
8
|
+
return Number.isInteger(value) && value > 0 && value <= 0x7fffffff;
|
|
9
|
+
}
|
|
10
|
+
function connectionError(cause) {
|
|
11
|
+
return cause instanceof ComputerUseError
|
|
12
|
+
? cause
|
|
13
|
+
: new ComputerUseError("connection_closed", { cause });
|
|
14
|
+
}
|
|
15
|
+
function closesConnection(error) {
|
|
16
|
+
return (error.code === "invalid_frame" ||
|
|
17
|
+
error.code === "frame_limit" ||
|
|
18
|
+
error.code === "connection_closed" ||
|
|
19
|
+
error.code === "authentication_failed" ||
|
|
20
|
+
error.code === "auth_failed" ||
|
|
21
|
+
error.code === "consent_denied" ||
|
|
22
|
+
error.code === "key_revoked");
|
|
23
|
+
}
|
|
24
|
+
export class ComputerUseBridge {
|
|
25
|
+
socket;
|
|
26
|
+
cleanup;
|
|
27
|
+
timeoutMs;
|
|
28
|
+
#instanceId = "";
|
|
29
|
+
#initialAccess;
|
|
30
|
+
#helperPid = 0;
|
|
31
|
+
#failure;
|
|
32
|
+
#lastId = 0n;
|
|
33
|
+
#requests = new Map();
|
|
34
|
+
#listeners = new Set();
|
|
35
|
+
#cancelRequested = false;
|
|
36
|
+
#onCancelRequested;
|
|
37
|
+
constructor(socket, cleanup, timeoutMs) {
|
|
38
|
+
this.socket = socket;
|
|
39
|
+
this.cleanup = cleanup;
|
|
40
|
+
this.timeoutMs = timeoutMs;
|
|
41
|
+
const decoder = new FrameDecoder((message) => this.#receiveMetadata(message));
|
|
42
|
+
const onData = (chunk) => {
|
|
43
|
+
try {
|
|
44
|
+
decoder.push(chunk);
|
|
45
|
+
}
|
|
46
|
+
catch (cause) {
|
|
47
|
+
this.#disconnect(connectionError(cause));
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
socket.on("data", onData);
|
|
51
|
+
socket.on("error", (cause) => this.#disconnect(connectionError(cause)));
|
|
52
|
+
socket.once("end", () => {
|
|
53
|
+
try {
|
|
54
|
+
decoder.finish();
|
|
55
|
+
}
|
|
56
|
+
catch (cause) {
|
|
57
|
+
this.#disconnect(connectionError(cause));
|
|
58
|
+
}
|
|
59
|
+
this.close();
|
|
60
|
+
});
|
|
61
|
+
socket.once("close", () => {
|
|
62
|
+
socket.off("data", onData);
|
|
63
|
+
this.close();
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
static async launch(options) {
|
|
67
|
+
const start = performance.now();
|
|
68
|
+
const requestTimeout = options.requestTimeoutMs ?? 10_000;
|
|
69
|
+
const handshakeTimeout = options.handshakeTimeoutMs ?? 4_000;
|
|
70
|
+
const consentTimeout = options.consentTimeoutMs ?? 195_000;
|
|
71
|
+
const { controller, locale, consentMode } = options;
|
|
72
|
+
if (!validTimeout(requestTimeout) ||
|
|
73
|
+
!validTimeout(handshakeTimeout) ||
|
|
74
|
+
!validTimeout(consentTimeout) ||
|
|
75
|
+
(options.consentDialogTimeoutMs !== undefined &&
|
|
76
|
+
(!validTimeout(options.consentDialogTimeoutMs) ||
|
|
77
|
+
options.consentDialogTimeoutMs > 180_000)) ||
|
|
78
|
+
(controller !== undefined && typeof controller?.sign !== "function") ||
|
|
79
|
+
(options.startupTimeoutMs !== undefined && !validTimeout(options.startupTimeoutMs))) {
|
|
80
|
+
throw new ComputerUseError("invalid_request");
|
|
81
|
+
}
|
|
82
|
+
const claim = controller && {
|
|
83
|
+
publicKey: controller.publicKey,
|
|
84
|
+
label: controller.label,
|
|
85
|
+
scope: controller.scope,
|
|
86
|
+
};
|
|
87
|
+
const { socket, secret, cleanup } = await launchHelper(options);
|
|
88
|
+
const bridge = new ComputerUseBridge(socket, cleanup, requestTimeout);
|
|
89
|
+
const abort = options.signal &&
|
|
90
|
+
addAbortListener(options.signal, () => {
|
|
91
|
+
bridge.#disconnect(new ComputerUseError("request_aborted"));
|
|
92
|
+
});
|
|
93
|
+
const nonce = randomBytes(32);
|
|
94
|
+
const context = { secret, nonce, hostPid: process.pid };
|
|
95
|
+
try {
|
|
96
|
+
if (options.signal?.aborted)
|
|
97
|
+
throw new ComputerUseError("request_aborted");
|
|
98
|
+
const { result } = await bridge.#request("hello", {
|
|
99
|
+
nonce: nonce.toString("base64"),
|
|
100
|
+
proof: createProof(context, "host"),
|
|
101
|
+
locale,
|
|
102
|
+
consentMode,
|
|
103
|
+
includeAccess: options.includeAccess,
|
|
104
|
+
consentTimeoutMs: options.consentDialogTimeoutMs === undefined
|
|
105
|
+
? undefined
|
|
106
|
+
: Math.max(1, Math.ceil(start + options.consentDialogTimeoutMs - performance.now())),
|
|
107
|
+
controller: claim,
|
|
108
|
+
}, { timeoutMs: controller ? handshakeTimeout : consentTimeout }, ({ instance, helperPid, helperProof }) => {
|
|
109
|
+
if (!verifyProof(helperProof, context, "helper", helperPid, instance)) {
|
|
110
|
+
throw new ComputerUseError("authentication_failed");
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
secret.fill(0);
|
|
114
|
+
if ("challenge" in result) {
|
|
115
|
+
if (!claim ||
|
|
116
|
+
!controller ||
|
|
117
|
+
result.fingerprint !== controllerFingerprint(claim.publicKey)) {
|
|
118
|
+
throw new ComputerUseError("auth_failed");
|
|
119
|
+
}
|
|
120
|
+
const payload = authorizationPayload(result, context.hostPid, claim, consentMode);
|
|
121
|
+
const disconnected = new AbortController();
|
|
122
|
+
const unsubscribe = bridge.onDisconnect((error) => disconnected.abort(error));
|
|
123
|
+
let signature;
|
|
124
|
+
try {
|
|
125
|
+
const signal = AbortSignal.any([disconnected.signal, AbortSignal.timeout(60_000)]);
|
|
126
|
+
signature = await untilAbort(() => controller.sign(payload, signal), signal);
|
|
127
|
+
if (!(signature instanceof Uint8Array) || signature.byteLength !== 64) {
|
|
128
|
+
throw new ComputerUseError("auth_failed");
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
finally {
|
|
132
|
+
unsubscribe();
|
|
133
|
+
}
|
|
134
|
+
await bridge.#request("authorize", { signature: Buffer.from(signature).toString("base64") }, { timeoutMs: consentTimeout }, (grant) => {
|
|
135
|
+
bridge.#initialAccess = grant.access;
|
|
136
|
+
if (grant.fingerprint !== result.fingerprint ||
|
|
137
|
+
grant.scope !== claim.scope ||
|
|
138
|
+
grant.locale !== result.locale ||
|
|
139
|
+
(consentMode === "persistent_only" && grant.auth !== "persistent") ||
|
|
140
|
+
((consentMode ?? "session_only") === "session_only" && grant.enrolled)) {
|
|
141
|
+
throw new ComputerUseError("auth_failed");
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
else if (claim) {
|
|
146
|
+
throw new ComputerUseError("auth_failed");
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
bridge.#initialAccess = result.access;
|
|
150
|
+
}
|
|
151
|
+
if (options.includeAccess && !bridge.#initialAccess)
|
|
152
|
+
throw new ComputerUseError("method_unavailable");
|
|
153
|
+
if (bridge.#failure)
|
|
154
|
+
throw bridge.#failure;
|
|
155
|
+
bridge.#instanceId = result.instance;
|
|
156
|
+
bridge.#helperPid = result.helperPid;
|
|
157
|
+
return bridge;
|
|
158
|
+
}
|
|
159
|
+
catch (cause) {
|
|
160
|
+
bridge.#disconnect(connectionError(cause));
|
|
161
|
+
throw cause;
|
|
162
|
+
}
|
|
163
|
+
finally {
|
|
164
|
+
abort?.[Symbol.dispose]();
|
|
165
|
+
secret.fill(0);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
get initialAccess() {
|
|
169
|
+
return this.#initialAccess;
|
|
170
|
+
}
|
|
171
|
+
get instanceId() {
|
|
172
|
+
return this.#instanceId;
|
|
173
|
+
}
|
|
174
|
+
get helperPid() {
|
|
175
|
+
return this.#helperPid;
|
|
176
|
+
}
|
|
177
|
+
get closed() {
|
|
178
|
+
return this.#failure !== undefined;
|
|
179
|
+
}
|
|
180
|
+
async status(options) {
|
|
181
|
+
const { result } = await this.#request("status", undefined, options, ({ instance }) => {
|
|
182
|
+
if (instance !== this.instanceId)
|
|
183
|
+
throw new ComputerUseError("invalid_frame");
|
|
184
|
+
});
|
|
185
|
+
return result;
|
|
186
|
+
}
|
|
187
|
+
async accessStatus(afterRevision, options) {
|
|
188
|
+
const { result } = await this.#request("access_status", afterRevision === undefined ? undefined : { afterRevision }, options);
|
|
189
|
+
return result;
|
|
190
|
+
}
|
|
191
|
+
async openPermissionGuide() {
|
|
192
|
+
const { result } = await this.#request("open_permission_guide", undefined);
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
195
|
+
async windows(application, options) {
|
|
196
|
+
const { result } = await this.#request("windows", application === undefined ? undefined : { application }, options);
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
async bind(window, options) {
|
|
200
|
+
const { result } = await this.#request("bind", { window }, options, (bound) => {
|
|
201
|
+
if (bound.window.window !== window)
|
|
202
|
+
throw new ComputerUseError("invalid_frame");
|
|
203
|
+
});
|
|
204
|
+
return result;
|
|
205
|
+
}
|
|
206
|
+
async unbind(binding, options) {
|
|
207
|
+
const { result } = await this.#request("unbind", { binding }, options, (released) => {
|
|
208
|
+
if (released.binding !== binding)
|
|
209
|
+
throw new ComputerUseError("invalid_frame");
|
|
210
|
+
});
|
|
211
|
+
return result;
|
|
212
|
+
}
|
|
213
|
+
async observe(binding, options) {
|
|
214
|
+
const { element, screenshot } = options ?? {};
|
|
215
|
+
const { result, binary } = await this.#request("observe", { binding, element, screenshot }, options, (observed) => {
|
|
216
|
+
if (observed.binding !== binding ||
|
|
217
|
+
(element !== undefined && observed.tree.ref !== element) ||
|
|
218
|
+
Boolean(observed.screenshot) !== (screenshot === true)) {
|
|
219
|
+
throw new ComputerUseError("invalid_frame");
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
const { screenshot: descriptor, ...observation } = result;
|
|
223
|
+
return descriptor
|
|
224
|
+
? { ...observation, screenshot: { ...descriptor, png: binary } }
|
|
225
|
+
: observation;
|
|
226
|
+
}
|
|
227
|
+
async act(params, options) {
|
|
228
|
+
const binding = params?.binding;
|
|
229
|
+
const { result } = await this.#request("act", params, options, (acted) => {
|
|
230
|
+
if (acted.binding !== binding)
|
|
231
|
+
throw new ComputerUseError("invalid_frame");
|
|
232
|
+
});
|
|
233
|
+
return result;
|
|
234
|
+
}
|
|
235
|
+
async cancel(options) {
|
|
236
|
+
const { result } = await this.#request("cancel", undefined, options);
|
|
237
|
+
return result;
|
|
238
|
+
}
|
|
239
|
+
async stop(options) {
|
|
240
|
+
const { result } = await this.#request("stop", undefined, options);
|
|
241
|
+
return result;
|
|
242
|
+
}
|
|
243
|
+
close() {
|
|
244
|
+
this.#disconnect(new ComputerUseError("connection_closed"));
|
|
245
|
+
}
|
|
246
|
+
onDisconnect(listener) {
|
|
247
|
+
if (this.#failure)
|
|
248
|
+
listener(this.#failure);
|
|
249
|
+
else
|
|
250
|
+
this.#listeners.add(listener);
|
|
251
|
+
return () => {
|
|
252
|
+
this.#listeners.delete(listener);
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
onCancelRequested(listener) {
|
|
256
|
+
if (this.#cancelRequested)
|
|
257
|
+
listener();
|
|
258
|
+
else
|
|
259
|
+
this.#onCancelRequested = listener;
|
|
260
|
+
}
|
|
261
|
+
#disconnect(error) {
|
|
262
|
+
if (this.#failure)
|
|
263
|
+
return;
|
|
264
|
+
this.#failure = error;
|
|
265
|
+
this.socket.destroy();
|
|
266
|
+
for (const request of this.#requests.values())
|
|
267
|
+
request.fail?.(error);
|
|
268
|
+
this.#requests.clear();
|
|
269
|
+
const listeners = [...this.#listeners];
|
|
270
|
+
this.#listeners.clear();
|
|
271
|
+
try {
|
|
272
|
+
this.cleanup();
|
|
273
|
+
}
|
|
274
|
+
catch {
|
|
275
|
+
}
|
|
276
|
+
for (const listener of listeners)
|
|
277
|
+
listener(error);
|
|
278
|
+
}
|
|
279
|
+
#receiveMetadata(message) {
|
|
280
|
+
if (this.#failure)
|
|
281
|
+
throw this.#failure;
|
|
282
|
+
if (message.id === null) {
|
|
283
|
+
const error = parseResponse(message);
|
|
284
|
+
if (!error.ok && error.error.code === "cancel_requested") {
|
|
285
|
+
return () => {
|
|
286
|
+
this.#cancelRequested = true;
|
|
287
|
+
this.#onCancelRequested?.();
|
|
288
|
+
this.#onCancelRequested = undefined;
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
if (!error.ok)
|
|
292
|
+
throw new ComputerUseError(error.error.code);
|
|
293
|
+
}
|
|
294
|
+
const pending = typeof message.id === "string" ? this.#requests.get(message.id) : undefined;
|
|
295
|
+
if (!pending)
|
|
296
|
+
throw new ComputerUseError("invalid_frame");
|
|
297
|
+
return pending.receiveMetadata(message);
|
|
298
|
+
}
|
|
299
|
+
async #request(method, params, options, validate) {
|
|
300
|
+
if (this.#failure)
|
|
301
|
+
throw this.#failure;
|
|
302
|
+
const { signal, timeoutMs = this.timeoutMs } = options ?? {};
|
|
303
|
+
if (signal?.aborted)
|
|
304
|
+
throw new ComputerUseError("request_aborted");
|
|
305
|
+
if (!validTimeout(timeoutMs))
|
|
306
|
+
throw new ComputerUseError("invalid_request");
|
|
307
|
+
if (this.#requests.size >= MAX_UNANSWERED_REQUESTS) {
|
|
308
|
+
const error = new ComputerUseError("connection_closed", {
|
|
309
|
+
cause: new ComputerUseError("request_timeout"),
|
|
310
|
+
});
|
|
311
|
+
this.#disconnect(error);
|
|
312
|
+
throw error;
|
|
313
|
+
}
|
|
314
|
+
const id = String(++this.#lastId);
|
|
315
|
+
const frame = encodeFrame(makeRequest(id, method, params));
|
|
316
|
+
return new Promise((resolve, reject) => {
|
|
317
|
+
const pending = {
|
|
318
|
+
receiveMetadata: (message) => {
|
|
319
|
+
const response = parseResponse(message, method);
|
|
320
|
+
if (response.ok) {
|
|
321
|
+
validate?.(response.result);
|
|
322
|
+
return (binary) => {
|
|
323
|
+
this.#requests.delete(id);
|
|
324
|
+
pending.finish?.({ result: response.result, binary });
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
const error = new ComputerUseError(response.error.code);
|
|
328
|
+
if (closesConnection(error))
|
|
329
|
+
throw error;
|
|
330
|
+
return () => {
|
|
331
|
+
this.#requests.delete(id);
|
|
332
|
+
pending.finish?.(error);
|
|
333
|
+
};
|
|
334
|
+
},
|
|
335
|
+
fail: (error) => pending.finish?.(method === "act"
|
|
336
|
+
? new ComputerUseError("action_outcome_unknown", { cause: error })
|
|
337
|
+
: error),
|
|
338
|
+
};
|
|
339
|
+
const timer = setTimeout(() => pending.fail?.(new ComputerUseError("request_timeout")), timeoutMs);
|
|
340
|
+
const abort = signal &&
|
|
341
|
+
addAbortListener(signal, () => pending.fail?.(new ComputerUseError("request_aborted")));
|
|
342
|
+
pending.finish = (reply) => {
|
|
343
|
+
clearTimeout(timer);
|
|
344
|
+
abort?.[Symbol.dispose]();
|
|
345
|
+
pending.finish = undefined;
|
|
346
|
+
pending.fail = undefined;
|
|
347
|
+
if (reply instanceof ComputerUseError)
|
|
348
|
+
reject(reply);
|
|
349
|
+
else
|
|
350
|
+
resolve(reply);
|
|
351
|
+
};
|
|
352
|
+
this.#requests.set(id, pending);
|
|
353
|
+
try {
|
|
354
|
+
this.socket.write(frame);
|
|
355
|
+
}
|
|
356
|
+
catch (cause) {
|
|
357
|
+
this.#disconnect(connectionError(cause));
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { stagedHelperPath } from "./launch.js";
|
|
2
|
+
interface DiscoveryOptions {
|
|
3
|
+
platform: NodeJS.Platform;
|
|
4
|
+
applications: string[];
|
|
5
|
+
envPath?: string;
|
|
6
|
+
bundledAppPath?: string;
|
|
7
|
+
strictBundledAppPath?: boolean;
|
|
8
|
+
stagedAppPath?: string | null;
|
|
9
|
+
platformApp?: () => string | undefined;
|
|
10
|
+
}
|
|
11
|
+
export { stagedHelperPath };
|
|
12
|
+
export declare function hasStagedHelperDirectory(path: string): boolean;
|
|
13
|
+
export declare function findInstalledHelper({ platform, applications, envPath, bundledAppPath, stagedAppPath, platformApp, }?: Partial<Omit<DiscoveryOptions, "strictBundledAppPath">>): string;
|
|
14
|
+
export declare function discoverHelper(options?: Partial<DiscoveryOptions>): Promise<string>;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { accessSync, constants, lstatSync, statSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { dirname, isAbsolute, join } from "node:path";
|
|
5
|
+
import { APP_NAME, stagedHelperPath, verifyHelperBundle } from "./launch.js";
|
|
6
|
+
import { ComputerUseError } from "./protocol.js";
|
|
7
|
+
export { stagedHelperPath };
|
|
8
|
+
export function hasStagedHelperDirectory(path) {
|
|
9
|
+
if (!isAbsolute(path))
|
|
10
|
+
return false;
|
|
11
|
+
const parent = dirname(path);
|
|
12
|
+
const uid = process.getuid?.();
|
|
13
|
+
try {
|
|
14
|
+
return [parent, path].every((dir) => {
|
|
15
|
+
const info = lstatSync(dir, { throwIfNoEntry: false });
|
|
16
|
+
return info?.isDirectory() && (uid === undefined || info.uid === uid);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function hasUsableStagedHelper(path) {
|
|
24
|
+
if (!hasStagedHelperDirectory(path))
|
|
25
|
+
return false;
|
|
26
|
+
try {
|
|
27
|
+
const contents = lstatSync(join(path, "Contents"), { throwIfNoEntry: false });
|
|
28
|
+
const macOS = lstatSync(join(path, "Contents", "MacOS"), { throwIfNoEntry: false });
|
|
29
|
+
const executable = join(path, "Contents", "MacOS", "RomeComputerUse");
|
|
30
|
+
const binary = lstatSync(executable, { throwIfNoEntry: false });
|
|
31
|
+
const plist = lstatSync(join(path, "Contents", "Info.plist"), { throwIfNoEntry: false });
|
|
32
|
+
if (!contents?.isDirectory() ||
|
|
33
|
+
!macOS?.isDirectory() ||
|
|
34
|
+
!binary?.isFile() ||
|
|
35
|
+
!plist?.isFile()) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
accessSync(executable, constants.X_OK);
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function resolvePlatformApp() {
|
|
46
|
+
if (typeof import.meta.url !== "string")
|
|
47
|
+
return;
|
|
48
|
+
try {
|
|
49
|
+
const manifest = createRequire(import.meta.url).resolve("@rome-os/node-core-darwin-arm64/package.json");
|
|
50
|
+
return join(dirname(manifest), "dist", "native", APP_NAME);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
if (error.code !== "MODULE_NOT_FOUND")
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export function findInstalledHelper({ platform = process.platform, applications = ["/Applications", join(homedir(), "Applications")], envPath = process.env.ROME_NODE_COMPUTER_USE_HELPER, bundledAppPath, stagedAppPath = stagedHelperPath(), platformApp = resolvePlatformApp, } = {}) {
|
|
58
|
+
if (platform !== "darwin")
|
|
59
|
+
throw new ComputerUseError("computer_use_unsupported_platform");
|
|
60
|
+
function installed(path) {
|
|
61
|
+
if (!isAbsolute(path) || path.includes("\0")) {
|
|
62
|
+
throw new ComputerUseError("invalid_helper_bundle");
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
if (statSync(path).isDirectory())
|
|
66
|
+
return path;
|
|
67
|
+
}
|
|
68
|
+
catch (cause) {
|
|
69
|
+
if (cause.code === "ENOENT")
|
|
70
|
+
return;
|
|
71
|
+
throw new ComputerUseError("invalid_helper_bundle", { cause });
|
|
72
|
+
}
|
|
73
|
+
throw new ComputerUseError("invalid_helper_bundle");
|
|
74
|
+
}
|
|
75
|
+
if (envPath !== undefined) {
|
|
76
|
+
const app = installed(envPath);
|
|
77
|
+
if (app)
|
|
78
|
+
return app;
|
|
79
|
+
throw new ComputerUseError("computer_use_helper_missing");
|
|
80
|
+
}
|
|
81
|
+
if (bundledAppPath !== undefined) {
|
|
82
|
+
const app = installed(bundledAppPath);
|
|
83
|
+
if (app)
|
|
84
|
+
return app;
|
|
85
|
+
}
|
|
86
|
+
const optionalApp = platformApp();
|
|
87
|
+
if (optionalApp !== undefined) {
|
|
88
|
+
const app = installed(optionalApp);
|
|
89
|
+
if (app)
|
|
90
|
+
return app;
|
|
91
|
+
}
|
|
92
|
+
if (stagedAppPath !== null && hasUsableStagedHelper(stagedAppPath))
|
|
93
|
+
return stagedAppPath;
|
|
94
|
+
for (const directory of applications) {
|
|
95
|
+
const app = installed(join(directory, APP_NAME));
|
|
96
|
+
if (app)
|
|
97
|
+
return app;
|
|
98
|
+
}
|
|
99
|
+
throw new ComputerUseError("computer_use_helper_missing");
|
|
100
|
+
}
|
|
101
|
+
export async function discoverHelper(options = {}) {
|
|
102
|
+
if (!options.strictBundledAppPath)
|
|
103
|
+
return findInstalledHelper(options);
|
|
104
|
+
if ((options.platform ?? process.platform) !== "darwin")
|
|
105
|
+
throw new ComputerUseError("computer_use_unsupported_platform");
|
|
106
|
+
const { bundledAppPath } = options;
|
|
107
|
+
if (!bundledAppPath || !hasStagedHelperDirectory(bundledAppPath)) {
|
|
108
|
+
throw new ComputerUseError("computer_use_helper_missing");
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
await verifyHelperBundle(bundledAppPath, AbortSignal.timeout(5000));
|
|
112
|
+
return bundledAppPath;
|
|
113
|
+
}
|
|
114
|
+
catch (cause) {
|
|
115
|
+
throw new ComputerUseError("computer_use_helper_missing", { cause });
|
|
116
|
+
}
|
|
117
|
+
}
|