@rome-os/node-core 0.1.7 → 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.
Files changed (41) hide show
  1. package/README.md +4 -5
  2. package/dist/actions.d.ts +2 -1
  3. package/dist/actions.js +3 -3
  4. package/dist/audit.d.ts +6 -0
  5. package/dist/audit.js +11 -0
  6. package/dist/client.d.ts +2 -1
  7. package/dist/client.js +12 -3
  8. package/dist/cloud.js +1 -2
  9. package/dist/computer-use/audit-fields.d.ts +5 -0
  10. package/dist/computer-use/audit-fields.js +17 -0
  11. package/dist/computer-use/bridge.d.ts +53 -0
  12. package/dist/computer-use/bridge.js +361 -0
  13. package/dist/computer-use/discovery.d.ts +14 -0
  14. package/dist/computer-use/discovery.js +117 -0
  15. package/dist/computer-use/frames.d.ts +9 -0
  16. package/dist/computer-use/frames.js +100 -0
  17. package/dist/computer-use/host.d.ts +90 -0
  18. package/dist/computer-use/host.js +765 -0
  19. package/dist/computer-use/launch.d.ts +55 -0
  20. package/dist/computer-use/launch.js +375 -0
  21. package/dist/computer-use/protocol.d.ts +238 -0
  22. package/dist/computer-use/protocol.js +441 -0
  23. package/dist/connect.d.ts +10 -24
  24. package/dist/connect.js +8 -112
  25. package/dist/daemon-client.js +1 -1
  26. package/dist/daemon.js +15 -3
  27. package/dist/devices-status.d.ts +2 -1
  28. package/dist/devices-status.js +16 -10
  29. package/dist/executor.d.ts +2 -1
  30. package/dist/executor.js +124 -134
  31. package/dist/host-connection.d.ts +37 -0
  32. package/dist/host-connection.js +114 -0
  33. package/dist/host.d.ts +6 -1
  34. package/dist/host.js +3 -0
  35. package/dist/limited-host.d.ts +3 -0
  36. package/dist/limited-host.js +5 -0
  37. package/dist/limited.d.ts +10 -0
  38. package/dist/limited.js +43 -0
  39. package/dist/protocol.d.ts +13 -0
  40. package/dist/protocol.js +29 -0
  41. package/package.json +5 -1
@@ -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
+ }
@@ -0,0 +1,9 @@
1
+ import { type JsonObject } from "./protocol.js";
2
+ export declare function encodeFrame(message: JsonObject, binary?: Buffer): Buffer;
3
+ export declare class FrameDecoder {
4
+ #private;
5
+ private readonly receiveMetadata;
6
+ constructor(receiveMetadata: (message: JsonObject) => (binary: Buffer) => void);
7
+ push(chunk: Buffer): void;
8
+ finish(): void;
9
+ }
@@ -0,0 +1,100 @@
1
+ import { checkPng, ComputerUseError, isObject, JSON_LIMIT, PNG_LIMIT, screenshotFor, } from "./protocol.js";
2
+ const HEADER_BYTES = 8;
3
+ const EMPTY = Buffer.alloc(0);
4
+ const utf8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
5
+ function checkLengths(json, binary) {
6
+ if (json === 0 || json > JSON_LIMIT || binary > PNG_LIMIT) {
7
+ throw new ComputerUseError("frame_limit");
8
+ }
9
+ }
10
+ export function encodeFrame(message, binary = EMPTY) {
11
+ if (!isObject(message))
12
+ throw new ComputerUseError("invalid_frame");
13
+ const json = Buffer.from(JSON.stringify(message));
14
+ checkLengths(json.length, binary.length);
15
+ const screenshot = screenshotFor(message, binary.length);
16
+ if (screenshot)
17
+ checkPng(binary, screenshot);
18
+ const header = Buffer.allocUnsafe(HEADER_BYTES);
19
+ header.writeUInt32BE(json.length, 0);
20
+ header.writeUInt32BE(binary.length, 4);
21
+ return Buffer.concat([header, json, binary]);
22
+ }
23
+ function decodeJson(bytes) {
24
+ let message;
25
+ try {
26
+ message = JSON.parse(utf8.decode(bytes));
27
+ }
28
+ catch (cause) {
29
+ throw new ComputerUseError("invalid_frame", { cause });
30
+ }
31
+ if (!isObject(message))
32
+ throw new ComputerUseError("invalid_frame");
33
+ return message;
34
+ }
35
+ export class FrameDecoder {
36
+ receiveMetadata;
37
+ #header = Buffer.allocUnsafe(HEADER_BYTES);
38
+ #part = this.#header;
39
+ #offset = 0;
40
+ #read = this.#readHeader;
41
+ #failure;
42
+ constructor(receiveMetadata) {
43
+ this.receiveMetadata = receiveMetadata;
44
+ }
45
+ push(chunk) {
46
+ if (this.#failure)
47
+ throw this.#failure;
48
+ try {
49
+ let offset = 0;
50
+ while (offset < chunk.length) {
51
+ const size = Math.min(this.#part.length - this.#offset, chunk.length - offset);
52
+ chunk.copy(this.#part, this.#offset, offset, offset + size);
53
+ offset += size;
54
+ this.#offset += size;
55
+ if (this.#offset === this.#part.length)
56
+ this.#read(this.#part);
57
+ }
58
+ }
59
+ catch (cause) {
60
+ this.#failure =
61
+ cause instanceof ComputerUseError
62
+ ? cause
63
+ : new ComputerUseError("invalid_frame", { cause });
64
+ throw this.#failure;
65
+ }
66
+ }
67
+ finish() {
68
+ if (this.#failure)
69
+ throw this.#failure;
70
+ if (this.#part !== this.#header || this.#offset !== 0) {
71
+ this.#failure = new ComputerUseError("invalid_frame");
72
+ throw this.#failure;
73
+ }
74
+ }
75
+ #next(part, read) {
76
+ this.#part = part;
77
+ this.#offset = 0;
78
+ this.#read = read;
79
+ }
80
+ #readHeader(header) {
81
+ const jsonLength = header.readUInt32BE(0);
82
+ const binaryLength = header.readUInt32BE(4);
83
+ checkLengths(jsonLength, binaryLength);
84
+ this.#next(Buffer.allocUnsafe(jsonLength), (json) => {
85
+ const message = decodeJson(json);
86
+ const screenshot = screenshotFor(message, binaryLength);
87
+ const receive = this.receiveMetadata(message);
88
+ if (!screenshot) {
89
+ this.#next(this.#header, this.#readHeader);
90
+ receive(EMPTY);
91
+ return;
92
+ }
93
+ this.#next(Buffer.allocUnsafe(binaryLength), (binary) => {
94
+ checkPng(binary, screenshot);
95
+ this.#next(this.#header, this.#readHeader);
96
+ receive(binary);
97
+ });
98
+ });
99
+ }
100
+ }
@@ -0,0 +1,90 @@
1
+ import { type ActionResponse, type BinaryActionResult } from "../actions.js";
2
+ import { ComputerUseBridge } from "./bridge.js";
3
+ import { discoverHelper } from "./discovery.js";
4
+ import { type HelperPermissionCache, probeHelperPermissions } from "./launch.js";
5
+ type Bridge = Pick<ComputerUseBridge, "initialAccess" | "accessStatus" | "openPermissionGuide" | "instanceId" | "helperPid" | "closed" | "windows" | "bind" | "unbind" | "observe" | "act" | "cancel" | "status" | "close" | "onDisconnect" | "onCancelRequested">;
6
+ export type EnsureComputerUseHelper = (options?: {
7
+ force?: boolean;
8
+ }) => Promise<boolean>;
9
+ export interface HelperOptions {
10
+ computerUseLocale?: () => string | undefined;
11
+ ensureHelper?: EnsureComputerUseHelper;
12
+ strictBundledAppPath?: boolean;
13
+ permissionCache?: HelperPermissionCache;
14
+ }
15
+ interface HostDeps extends HelperOptions {
16
+ bundledAppPath?: string;
17
+ discover: typeof discoverHelper;
18
+ probePermissions: typeof probeHelperPermissions;
19
+ launch: (options: Parameters<typeof ComputerUseBridge.launch>[0]) => Promise<Bridge>;
20
+ consentTtlMs: number;
21
+ permissionTtlMs: number;
22
+ terminalTtlMs: number;
23
+ sessionTtlMs: number;
24
+ now: () => number;
25
+ isProcessAlive: (pid: number) => boolean;
26
+ stopPollMs: number;
27
+ }
28
+ interface PreparedOperation {
29
+ run(refusal?: ActionResponse): Promise<BinaryActionResult>;
30
+ release(): void;
31
+ }
32
+ export declare class ComputerUseHost {
33
+ private readonly sessions;
34
+ private readonly history;
35
+ private readonly windows;
36
+ private readonly deps;
37
+ private readonly permissionCache;
38
+ private discovering?;
39
+ private ensuring?;
40
+ constructor(deps?: Partial<HostDeps>);
41
+ private readonly operations;
42
+ info(peer: string): Promise<{
43
+ capability: {
44
+ id: string;
45
+ prefix: string;
46
+ title: string;
47
+ state: string;
48
+ personal: boolean;
49
+ actions: string[];
50
+ };
51
+ actions: string[];
52
+ usage: {
53
+ [k: string]: string;
54
+ };
55
+ }>;
56
+ prepare(action: string, peer: string, args: unknown, body?: Uint8Array): PreparedOperation | undefined;
57
+ disconnect(): void;
58
+ private reserve;
59
+ private findHelper;
60
+ private ensureReady;
61
+ private resolveHelper;
62
+ private requireSession;
63
+ private check;
64
+ private work;
65
+ private snapshot;
66
+ private refusal;
67
+ private deadline;
68
+ private expiry;
69
+ private expire;
70
+ private start;
71
+ private current;
72
+ private updateAccess;
73
+ private failed;
74
+ private bind;
75
+ private bound;
76
+ private changeBinding;
77
+ private cancel;
78
+ private seal;
79
+ private scheduleStopCleanup;
80
+ private stopHelper;
81
+ private confirmStop;
82
+ private finishStop;
83
+ private watchHelperExit;
84
+ private releaseBinding;
85
+ private releaseWindows;
86
+ private outcome;
87
+ private record;
88
+ private remove;
89
+ }
90
+ export {};