@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/dist/executor.js CHANGED
@@ -4,7 +4,9 @@ import { homedir, platform } from "node:os";
4
4
  import { dirname, resolve } from "node:path";
5
5
  import { actionError, isRecord } from "./actions.js";
6
6
  import { AUDIT_PUSH_BYTES } from "./audit.js";
7
- import { decodeMeta, encodeMeta, FRAME_TYPE, frameFits, MAX_FRAME_BYTES, } from "./frame.js";
7
+ import { computerUseAuditFields } from "./computer-use/audit-fields.js";
8
+ import { ComputerUseHost } from "./computer-use/host.js";
9
+ import { decodeMeta, encodeMeta, FRAME_TYPE, frameFits, isUuid, MAX_FRAME_BYTES, } from "./frame.js";
8
10
  import { parseTransferMessage, TRANSFER_VERSION, TransferHost } from "./transfer.js";
9
11
  function execInput(input) {
10
12
  if (!isRecord(input) ||
@@ -98,22 +100,28 @@ function outcomeOf(response, interrupted) {
98
100
  ? "exit_nonzero"
99
101
  : "ok";
100
102
  }
101
- export function createExecutor(name, audit, send, secrets = [], sendFrame = () => false) {
103
+ export function createExecutor(name, audit, send, secrets = [], sendFrame = () => false, computerUseHelperPath, helperOptions) {
104
+ const computer = new ComputerUseHost({ bundledAppPath: computerUseHelperPath, ...helperOptions });
102
105
  const children = new Set();
103
106
  const transfers = new TransferHost({ keepBytes: AUDIT_PUSH_BYTES });
104
107
  let generation = 0;
105
- const info = async () => ({
106
- type: "response",
107
- ok: true,
108
- result: {
109
- name,
110
- platform: platform() === "darwin" ? "macos" : platform() === "win32" ? "windows" : platform(),
111
- actions: ["system.info", "exec", "fs.list"],
112
- frameVersion: 1,
113
- transferVersion: TRANSFER_VERSION,
114
- audit: await audit.status(),
115
- },
116
- });
108
+ const info = async (peer) => {
109
+ const { capability, actions, usage } = await computer.info(peer);
110
+ return {
111
+ type: "response",
112
+ ok: true,
113
+ result: {
114
+ name,
115
+ platform: platform() === "darwin" ? "macos" : platform() === "win32" ? "windows" : platform(),
116
+ actions: [...Object.keys(handlers), ...actions],
117
+ ...(actions.length ? { usage } : {}),
118
+ frameVersion: 1,
119
+ transferVersion: TRANSFER_VERSION,
120
+ capabilities: [capability],
121
+ audit: await audit.status(),
122
+ },
123
+ };
124
+ };
117
125
  const refused = () => actionError("audit_unavailable", `This computer could not record the request in its audit log, so it did not run it. ${audit.hint}`);
118
126
  const replaced = () => actionError("connection_replaced", "The connection was replaced before the request ran.");
119
127
  const startRecord = (action, args, extra) => ({
@@ -187,34 +195,10 @@ export function createExecutor(name, audit, send, secrets = [], sendFrame = () =
187
195
  }
188
196
  const redactText = (data) => secrets.reduce((text, secret) => text.replaceAll(secret, "[redacted]"), data.toString("utf8"));
189
197
  const isOutput = (value) => !("type" in value);
190
- const handlers = {
191
- "system.info": () => info(),
192
- exec: async (input, notes) => {
193
- const parsed = execInput(input);
194
- if (!parsed)
195
- return invalidExec();
196
- const output = await runProgram(parsed, notes);
197
- if (!isOutput(output))
198
- return output;
199
- return {
200
- type: "response",
201
- ok: true,
202
- result: {
203
- exitCode: output.exitCode,
204
- signal: output.signal,
205
- stdout: redactText(output.stdout),
206
- stderr: redactText(output.stderr),
207
- truncated: { stdout: false, stderr: false },
208
- },
209
- };
210
- },
211
- "fs.list": listFolder,
212
- };
213
198
  const empty = new Uint8Array();
214
- const frameHandlers = {
215
- "system.info": async () => ({ response: await info(), body: empty }),
216
- "fs.list": async (input) => ({ response: await listFolder(input), body: empty }),
217
- exec: async (input, body, notes) => {
199
+ const handlers = {
200
+ "system.info": async (_args, _notes, peer) => ({ response: await info(peer), body: empty }),
201
+ exec: async (input, notes, _peer, body) => {
218
202
  const parsed = execInput(input);
219
203
  if (!parsed)
220
204
  return { response: invalidExec(), body: empty };
@@ -228,13 +212,15 @@ export function createExecutor(name, audit, send, secrets = [], sendFrame = () =
228
212
  result: {
229
213
  exitCode: output.exitCode,
230
214
  signal: output.signal,
215
+ ...(body === undefined ? { stdout: redactText(output.stdout) } : {}),
231
216
  stderr: redactText(output.stderr),
232
217
  truncated: { stdout: false, stderr: false },
233
218
  },
234
219
  },
235
- body: redactBytes(output.stdout, secrets),
220
+ body: body === undefined ? empty : redactBytes(output.stdout, secrets),
236
221
  };
237
222
  },
223
+ "fs.list": async (args) => ({ response: await listFolder(args), body: empty }),
238
224
  };
239
225
  let running = 0;
240
226
  const drained = [];
@@ -259,32 +245,7 @@ export function createExecutor(name, audit, send, secrets = [], sendFrame = () =
259
245
  const request = message.payload;
260
246
  if (!isRecord(request) || request.type !== "request" || typeof request.action !== "string")
261
247
  return;
262
- const current = generation;
263
- const action = request.action;
264
- const handler = Object.hasOwn(handlers, action) ? handlers[action] : undefined;
265
- const notes = { fields: {}, content: {} };
266
- const started = Date.now();
267
- const seq = action === "system.info"
268
- ? 0
269
- : await audit.start(action, startRecord(action, request.args, {
270
- requestId: message.id,
271
- from: message.from,
272
- transport: "json",
273
- generation: current,
274
- }), {}, secrets);
275
- const payload = seq === null
276
- ? refused()
277
- : current !== generation
278
- ? replaced()
279
- : handler
280
- ? await handler(request.args, notes).catch(() => actionError("exec_failed", "The action failed."))
281
- : actionError("unsupported_action", "The computer does not support this action.");
282
- const reply = { id: message.id, to: message.from, payload };
283
- const interrupted = current !== generation;
284
- if (seq)
285
- endRecord(action, seq, started, payload, interrupted, notes, interrupted ? 0 : Buffer.byteLength(JSON.stringify(reply)));
286
- if (!interrupted)
287
- send(reply);
248
+ await dispatch(request.action, request.args, message.id, message.from);
288
249
  }
289
250
  async function receiveFrame(frame) {
290
251
  if (frame.type !== FRAME_TYPE.request)
@@ -297,81 +258,110 @@ export function createExecutor(name, audit, send, secrets = [], sendFrame = () =
297
258
  }
298
259
  if (!isRecord(request) || request.type !== "request" || typeof request.action !== "string")
299
260
  return;
261
+ await dispatch(request.action, request.args, frame.id, frame.peer, frame.body);
262
+ }
263
+ async function dispatch(action, args, id, peer, body) {
300
264
  const current = generation;
301
- const action = request.action;
302
- const respond = (response) => current === generation &&
303
- sendFrame({
304
- type: FRAME_TYPE.response,
305
- id: frame.id,
306
- peer: frame.peer,
307
- meta: encodeMeta(response),
308
- body: empty,
309
- });
310
265
  const started = Date.now();
311
- const seq = action === "system.info"
312
- ? 0
313
- : await audit.start(action, startRecord(action, request.args, {
314
- requestId: frame.id,
315
- from: frame.peer,
316
- transport: "binary",
317
- generation: current,
318
- }), { stdin: frame.body }, secrets);
319
- if (seq === null) {
320
- respond(refused());
321
- return;
322
- }
323
- if (current !== generation) {
324
- endRecord(action, seq, started, replaced(), true, { fields: {}, content: {} }, 0);
325
- return;
326
- }
327
- if (action === "transfer.open") {
328
- const transfer = transfers.open(frame.id, frame.peer, request.args, (meta, body = empty) => {
329
- if (current !== generation)
330
- return false;
331
- return sendFrame({
266
+ const input = computer.prepare(action, peer, args, body);
267
+ const privateAction = action.startsWith("computer.");
268
+ const recordAction = privateAction && !input ? "computer.unsupported" : action;
269
+ const transport = body === undefined ? "json" : "binary";
270
+ const notes = { fields: {}, content: {} };
271
+ if (privateAction)
272
+ Object.assign(notes.fields, computerUseAuditFields(action, args));
273
+ const metadata = {
274
+ ...(!privateAction || isUuid(id) ? { requestId: id } : {}),
275
+ ...(!privateAction || isUuid(peer) ? { from: peer } : {}),
276
+ transport,
277
+ generation: current,
278
+ };
279
+ const respond = (response, output = empty, meta) => {
280
+ if (current !== generation)
281
+ return;
282
+ if (body === undefined)
283
+ send({ id, to: peer, payload: response });
284
+ else
285
+ sendFrame({
332
286
  type: FRAME_TYPE.response,
333
- id: frame.id,
334
- peer: frame.peer,
335
- meta: encodeMeta(meta),
336
- body,
287
+ id,
288
+ peer,
289
+ meta: meta ?? encodeMeta(response),
290
+ body: output,
337
291
  });
338
- });
339
- void track(() => transfer.then(({ code, file, ...fields }) => {
340
- const response = code
341
- ? actionError(code, "")
342
- : { type: "response", ok: true, result: null };
343
- endRecord(action, seq, started, response, current !== generation, { fields, content: file ? { file } : {} }, 0);
344
- }));
345
- return;
292
+ };
293
+ try {
294
+ const seq = action === "system.info" || action === "computer.status"
295
+ ? 0
296
+ : await audit
297
+ .start(recordAction, privateAction
298
+ ? { ...metadata, ...notes.fields }
299
+ : startRecord(action, args, metadata), privateAction || body === undefined ? {} : { stdin: body }, secrets)
300
+ .catch(() => null);
301
+ if (seq === null && !input) {
302
+ respond(refused());
303
+ return;
304
+ }
305
+ if (current !== generation) {
306
+ if (seq)
307
+ endRecord(recordAction, seq, started, replaced(), true, notes, 0);
308
+ return;
309
+ }
310
+ if (body !== undefined && action === "transfer.open" && seq) {
311
+ const transfer = transfers.open(id, peer, args, (meta, data) => current === generation &&
312
+ sendFrame({
313
+ type: FRAME_TYPE.response,
314
+ id,
315
+ peer,
316
+ meta: encodeMeta(meta),
317
+ body: data ?? empty,
318
+ }));
319
+ void track(() => transfer.then(({ code, file, ...fields }) => {
320
+ const response = code
321
+ ? actionError(code, "")
322
+ : { type: "response", ok: true, result: null };
323
+ endRecord(action, seq, started, response, current !== generation, { fields, content: file ? { file } : {} }, 0);
324
+ }));
325
+ return;
326
+ }
327
+ const handler = Object.hasOwn(handlers, action) ? handlers[action] : undefined;
328
+ const result = input
329
+ ? await input.run(seq === null ? refused() : undefined)
330
+ : handler
331
+ ? await handler(args, notes, peer, body).catch(() => ({
332
+ response: actionError("exec_failed", "The action failed."),
333
+ body: empty,
334
+ }))
335
+ : {
336
+ response: actionError("unsupported_action", "The computer does not support this action."),
337
+ body: empty,
338
+ };
339
+ let response = result.response;
340
+ let output = result.body;
341
+ let meta = body === undefined ? undefined : encodeMeta(response);
342
+ if (meta && !frameFits(meta.byteLength, output.byteLength)) {
343
+ response = actionError("output_too_large", `The output does not fit in one ${MAX_FRAME_BYTES / 1024 / 1024} MiB frame and was discarded. The program ran. Use rome-node cp to copy large files.`);
344
+ meta = encodeMeta(response);
345
+ output = empty;
346
+ }
347
+ if (action === "computer.cancel" && response.ok && isRecord(response.result)) {
348
+ notes.fields.stop = response.result.stop;
349
+ notes.fields.reason = response.result.reason;
350
+ }
351
+ const interrupted = current !== generation;
352
+ if (seq)
353
+ endRecord(recordAction, seq, started, response, interrupted, notes, meta
354
+ ? meta.byteLength + output.byteLength
355
+ : Buffer.byteLength(JSON.stringify({ id, to: peer, payload: response })));
356
+ respond(response, output, meta);
346
357
  }
347
- const handler = Object.hasOwn(frameHandlers, action) ? frameHandlers[action] : undefined;
348
- const notes = { fields: {}, content: {} };
349
- const result = handler
350
- ? await handler(request.args, frame.body, notes).catch(() => ({
351
- response: actionError("exec_failed", "The action failed."),
352
- body: empty,
353
- }))
354
- : {
355
- response: actionError("unsupported_action", "The computer does not support this action."),
356
- body: empty,
357
- };
358
- const interrupted = current !== generation;
359
- let response = result.response;
360
- let meta = encodeMeta(response);
361
- let body = result.body;
362
- if (!frameFits(meta.byteLength, body.byteLength)) {
363
- response = actionError("output_too_large", `The output does not fit in one ${MAX_FRAME_BYTES / 1024 / 1024} MiB frame and was discarded. The program ran. Use rome-node cp to copy large files.`);
364
- meta = encodeMeta(response);
365
- body = empty;
358
+ finally {
359
+ input?.release();
366
360
  }
367
- if (seq)
368
- endRecord(action, seq, started, response, interrupted, notes, meta.byteLength + body.byteLength);
369
- if (interrupted)
370
- return;
371
- sendFrame({ type: FRAME_TYPE.response, id: frame.id, peer: frame.peer, meta, body });
372
361
  }
373
362
  function disconnect() {
374
363
  generation++;
364
+ computer.disconnect();
375
365
  transfers.abortAll();
376
366
  for (const child of children) {
377
367
  child.kill("SIGTERM");
@@ -90,6 +90,8 @@ export async function runHostConnection(options, responder) {
90
90
  onFrame: (frame) => void executor.receiveFrame(frame),
91
91
  onNotification: options.onNotification,
92
92
  onStatus: (status) => {
93
+ if (status !== "online" && status !== "connecting")
94
+ executor.disconnect();
93
95
  options.audit?.host({
94
96
  event: "connection",
95
97
  status,
@@ -98,11 +100,9 @@ export async function runHostConnection(options, responder) {
98
100
  cloudOrigin: cloudUrl,
99
101
  }, [active.token]);
100
102
  options.onEvent?.({ type: "connection", status });
101
- if (status !== "online" && status !== "connecting")
102
- executor.disconnect();
103
103
  if (status === "revoked" || status === "superseded") {
104
104
  queueMicrotask(() => finish(new Error(status === "revoked"
105
- ? "Authorization revoked. Explicit authorization is required."
105
+ ? "This device was removed from your account. Connect it again to add it back."
106
106
  : "Another connection replaced this device. This process has stopped.")));
107
107
  }
108
108
  },
package/dist/host.d.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  export { connectHost, defaultDeviceName, type HostCredential, type HostEvent, type HostOptions, } from "./connect.js";
2
2
  export { connectLimitedHost, type LimitedHostOptions } from "./limited-host.js";
3
+ export type { EnsureComputerUseHelper } from "./computer-use/host.js";
3
4
  export { loginDevice, type DeviceSession } from "./login.js";
4
5
  export { type GatewayNotification } from "./protocol.js";
5
6
  export { loginDeviceCode, type DeviceAuthorizationPrompt } from "./device-authorization.js";
6
7
  export { createFileCredentialStore, type CredentialStore } from "./storage.js";
7
- export { AuditStore, type AuditItem, type AuditQuery, type AuditResult, type AuditStats, type AuditStatus, type AuditSummary, type ContentName, } from "./audit.js";
8
+ export { discoverHelper, findInstalledHelper, hasStagedHelperDirectory, stagedHelperPath, } from "./computer-use/discovery.js";
9
+ export { APP_NAME, RELEASE_SIGNING_REQUIREMENT, createHelperPermissionCache, launchPermissionSetup, probeHelperPermissions, type HelperPermissionCache, type PermissionSetupOptions, type PermissionSetupPane, } from "./computer-use/launch.js";
10
+ export { AuditStore, type AuditContentPreview, type AuditItem, type AuditQuery, type AuditResult, type AuditStats, type AuditStatus, type AuditSummary, type ContentName, } from "./audit.js";
package/dist/host.js CHANGED
@@ -3,4 +3,6 @@ export { connectLimitedHost } from "./limited-host.js";
3
3
  export { loginDevice } from "./login.js";
4
4
  export { loginDeviceCode } from "./device-authorization.js";
5
5
  export { createFileCredentialStore } from "./storage.js";
6
+ export { discoverHelper, findInstalledHelper, hasStagedHelperDirectory, stagedHelperPath, } from "./computer-use/discovery.js";
7
+ export { APP_NAME, RELEASE_SIGNING_REQUIREMENT, createHelperPermissionCache, launchPermissionSetup, probeHelperPermissions, } from "./computer-use/launch.js";
6
8
  export { AuditStore, } from "./audit.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rome-os/node-core",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "JavaScript APIs for Rome device hosts and shared caller daemons",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -10,6 +10,7 @@
10
10
  "files": [
11
11
  "bin",
12
12
  "dist",
13
+ "!dist/**/*.app/**",
13
14
  "README.md"
14
15
  ],
15
16
  "exports": {
@@ -49,6 +50,9 @@
49
50
  "dependencies": {
50
51
  "ws": "^8.18.0"
51
52
  },
53
+ "optionalDependencies": {
54
+ "@rome-os/node-core-darwin-arm64": "0.1.10"
55
+ },
52
56
  "devDependencies": {
53
57
  "@rstest/core": "^0.11.11",
54
58
  "@types/node": "25.2.3",