@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.
- 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/client.d.ts +2 -1
- package/dist/client.js +12 -3
- 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 +10 -24
- package/dist/connect.js +8 -112
- package/dist/daemon-client.js +1 -1
- package/dist/daemon.js +15 -3
- package/dist/devices-status.d.ts +2 -1
- package/dist/devices-status.js +16 -10
- package/dist/executor.d.ts +2 -1
- package/dist/executor.js +124 -134
- package/dist/host-connection.d.ts +37 -0
- package/dist/host-connection.js +114 -0
- package/dist/host.d.ts +6 -1
- package/dist/host.js +3 -0
- package/dist/limited-host.d.ts +3 -0
- package/dist/limited-host.js +5 -0
- package/dist/limited.d.ts +10 -0
- package/dist/limited.js +43 -0
- package/dist/protocol.d.ts +13 -0
- package/dist/protocol.js +29 -0
- package/package.json +5 -1
package/dist/devices-status.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isRecord } from "./actions.js";
|
|
2
|
+
const REFRESH_AFTER_MS = 10_000;
|
|
2
3
|
export class DeviceStatusReader {
|
|
3
4
|
connector;
|
|
4
5
|
cached;
|
|
@@ -10,23 +11,31 @@ export class DeviceStatusReader {
|
|
|
10
11
|
invalidate() {
|
|
11
12
|
this.generation++;
|
|
12
13
|
this.cached = undefined;
|
|
14
|
+
this.pending = undefined;
|
|
13
15
|
}
|
|
14
16
|
async read() {
|
|
15
|
-
if (this.cached
|
|
16
|
-
return this.
|
|
17
|
+
if (!this.cached)
|
|
18
|
+
return this.refresh();
|
|
19
|
+
if (Date.now() - Date.parse(this.cached.checkedAt) >= REFRESH_AFTER_MS)
|
|
20
|
+
this.refresh().catch(() => undefined);
|
|
21
|
+
return this.cached;
|
|
22
|
+
}
|
|
23
|
+
refresh() {
|
|
17
24
|
if (this.pending)
|
|
18
25
|
return this.pending;
|
|
19
26
|
const generation = this.generation;
|
|
20
|
-
|
|
27
|
+
const pending = this.check()
|
|
21
28
|
.then((snapshot) => {
|
|
22
29
|
if (generation === this.generation)
|
|
23
30
|
this.cached = snapshot;
|
|
24
31
|
return snapshot;
|
|
25
32
|
})
|
|
26
33
|
.finally(() => {
|
|
27
|
-
this.pending
|
|
34
|
+
if (this.pending === pending)
|
|
35
|
+
this.pending = undefined;
|
|
28
36
|
});
|
|
29
|
-
|
|
37
|
+
this.pending = pending;
|
|
38
|
+
return pending;
|
|
30
39
|
}
|
|
31
40
|
async check() {
|
|
32
41
|
const list = await this.connector.list();
|
|
@@ -39,7 +48,7 @@ export class DeviceStatusReader {
|
|
|
39
48
|
id: item.id,
|
|
40
49
|
name: typeof item.device_name === "string" ? item.device_name : item.id,
|
|
41
50
|
platform: typeof item.platform === "string" ? item.platform : null,
|
|
42
|
-
status:
|
|
51
|
+
status: "unknown",
|
|
43
52
|
};
|
|
44
53
|
});
|
|
45
54
|
const deadline = Date.now() + 15_000;
|
|
@@ -47,8 +56,6 @@ export class DeviceStatusReader {
|
|
|
47
56
|
await Promise.all(Array.from({ length: Math.min(4, devices.length) }, async () => {
|
|
48
57
|
while (next < devices.length && Date.now() < deadline) {
|
|
49
58
|
const device = devices[next++];
|
|
50
|
-
if (device.status === "revoked")
|
|
51
|
-
continue;
|
|
52
59
|
const response = await this.connector.run(device.id, "system.info", {}, 3000);
|
|
53
60
|
device.status = response.ok
|
|
54
61
|
? "connected"
|
|
@@ -67,8 +74,7 @@ export class DeviceStatusReader {
|
|
|
67
74
|
const connection = this.connector.getStatus();
|
|
68
75
|
if (connection !== "online") {
|
|
69
76
|
for (const device of devices)
|
|
70
|
-
|
|
71
|
-
device.status = "unknown";
|
|
77
|
+
device.status = "unknown";
|
|
72
78
|
}
|
|
73
79
|
return { connection, checkedAt: new Date().toISOString(), devices };
|
|
74
80
|
}
|
package/dist/executor.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { type AuditStore } from "./audit.js";
|
|
2
|
+
import { type HelperOptions } from "./computer-use/host.js";
|
|
2
3
|
import { type Frame } from "./frame.js";
|
|
3
4
|
import type { InboundEnvelope, OutboundEnvelope } from "./protocol.js";
|
|
4
5
|
export declare const FS_LIST_LIMIT = 1000;
|
|
5
6
|
export declare function redactBytes(data: Buffer, secrets: string[]): Buffer;
|
|
6
7
|
export type HostAudit = Pick<AuditStore, "start" | "end" | "host" | "status" | "hint">;
|
|
7
|
-
export declare function createExecutor(name: string, audit: HostAudit, send: (message: OutboundEnvelope) => boolean, secrets?: string[], sendFrame?: (frame: Frame) => boolean): {
|
|
8
|
+
export declare function createExecutor(name: string, audit: HostAudit, send: (message: OutboundEnvelope) => boolean, secrets?: string[], sendFrame?: (frame: Frame) => boolean, computerUseHelperPath?: string, helperOptions?: HelperOptions): {
|
|
8
9
|
receive: (message: InboundEnvelope) => Promise<void>;
|
|
9
10
|
receiveFrame: (frame: Frame) => Promise<void>;
|
|
10
11
|
idle: () => Promise<void>;
|
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 {
|
|
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
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
|
215
|
-
"system.info": async () => ({ response: await info(), body: empty }),
|
|
216
|
-
|
|
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
|
-
|
|
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
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
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
|
|
334
|
-
peer
|
|
335
|
-
meta: encodeMeta(
|
|
336
|
-
body,
|
|
287
|
+
id,
|
|
288
|
+
peer,
|
|
289
|
+
meta: meta ?? encodeMeta(response),
|
|
290
|
+
body: output,
|
|
337
291
|
});
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
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
|
-
|
|
348
|
-
|
|
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");
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { DeviceSession } from "./login.js";
|
|
2
|
+
import type { CredentialStore } from "./storage.js";
|
|
3
|
+
import type { Frame } from "./frame.js";
|
|
4
|
+
import type { HostAudit } from "./executor.js";
|
|
5
|
+
import { type GatewayNotification, type InboundEnvelope, type OutboundEnvelope } from "./protocol.js";
|
|
6
|
+
export type HostCredential = DeviceSession & {
|
|
7
|
+
name: string;
|
|
8
|
+
};
|
|
9
|
+
export type HostEvent = {
|
|
10
|
+
type: "authorization_required";
|
|
11
|
+
} | {
|
|
12
|
+
type: "retrying";
|
|
13
|
+
} | {
|
|
14
|
+
type: "device";
|
|
15
|
+
name: string;
|
|
16
|
+
deviceId: string;
|
|
17
|
+
} | {
|
|
18
|
+
type: "connection";
|
|
19
|
+
status: import("./client.js").ConnectionStatus;
|
|
20
|
+
};
|
|
21
|
+
export interface HostConnectionOptions {
|
|
22
|
+
cloudUrl: string;
|
|
23
|
+
name: string;
|
|
24
|
+
signal: AbortSignal;
|
|
25
|
+
credentials: CredentialStore<HostCredential>;
|
|
26
|
+
authorize(cloudUrl: string, name: string, signal: AbortSignal): Promise<DeviceSession>;
|
|
27
|
+
onEvent?(event: HostEvent): void;
|
|
28
|
+
onNotification?(notification: GatewayNotification): void;
|
|
29
|
+
audit?: Pick<HostAudit, "host">;
|
|
30
|
+
}
|
|
31
|
+
export interface HostResponder {
|
|
32
|
+
receive(message: InboundEnvelope): Promise<void> | void;
|
|
33
|
+
receiveFrame(frame: Frame): Promise<void> | void;
|
|
34
|
+
disconnect(): void;
|
|
35
|
+
idle(): Promise<void>;
|
|
36
|
+
}
|
|
37
|
+
export declare function runHostConnection(options: HostConnectionOptions, responder: (session: HostCredential, send: (message: OutboundEnvelope) => boolean, sendFrame: (frame: Frame) => boolean, secrets: string[]) => HostResponder): Promise<void>;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
2
|
+
import { isRecord } from "./actions.js";
|
|
3
|
+
import { cloudOrigin, cloudRequest, CloudError, gatewayConfig } from "./cloud.js";
|
|
4
|
+
import { connectGateway } from "./client.js";
|
|
5
|
+
import { createNodeSocket } from "./socket.js";
|
|
6
|
+
import { validId, } from "./protocol.js";
|
|
7
|
+
export async function runHostConnection(options, responder) {
|
|
8
|
+
const { name, signal, credentials } = options;
|
|
9
|
+
if (signal.aborted)
|
|
10
|
+
return;
|
|
11
|
+
const cloudUrl = cloudOrigin(options.cloudUrl);
|
|
12
|
+
const stored = await credentials.load();
|
|
13
|
+
let session = isRecord(stored) &&
|
|
14
|
+
stored.cloudUrl === cloudUrl &&
|
|
15
|
+
typeof stored.token === "string" &&
|
|
16
|
+
/^(?:romedev|romemob)_[A-Za-z0-9_-]{43}$/.test(stored.token) &&
|
|
17
|
+
validId(stored.deviceId) &&
|
|
18
|
+
typeof stored.name === "string"
|
|
19
|
+
? { cloudUrl, token: stored.token, deviceId: stored.deviceId, name: stored.name }
|
|
20
|
+
: null;
|
|
21
|
+
let gatewayUrl = "";
|
|
22
|
+
let attempt = 0;
|
|
23
|
+
while (!signal.aborted) {
|
|
24
|
+
if (!session) {
|
|
25
|
+
options.onEvent?.({ type: "authorization_required" });
|
|
26
|
+
session = { ...(await options.authorize(cloudUrl, name, signal)), name };
|
|
27
|
+
await credentials.save(session);
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
gatewayUrl = await gatewayConfig(cloudUrl, session.token);
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
if (error instanceof CloudError && error.code === "invalid_device_session") {
|
|
35
|
+
await credentials.clear();
|
|
36
|
+
session = null;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
options.onEvent?.({ type: "retrying" });
|
|
40
|
+
await delay(Math.min(60_000, 1000 * 2 ** Math.min(attempt++, 6)), undefined, {
|
|
41
|
+
signal,
|
|
42
|
+
}).catch(() => { });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (signal.aborted || !session)
|
|
46
|
+
return;
|
|
47
|
+
const devices = await cloudRequest(cloudUrl, "/api/account/devices", session.token).catch(() => null);
|
|
48
|
+
if (isRecord(devices) && Array.isArray(devices.items)) {
|
|
49
|
+
const device = devices.items.find((item) => isRecord(item) && item.id === session?.deviceId);
|
|
50
|
+
if (isRecord(device) && typeof device.device_name === "string") {
|
|
51
|
+
session.name = device.device_name;
|
|
52
|
+
await credentials.save(session);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (signal.aborted)
|
|
56
|
+
return;
|
|
57
|
+
const active = session;
|
|
58
|
+
options.onEvent?.({ type: "device", name: active.name, deviceId: active.deviceId });
|
|
59
|
+
await new Promise((resolve, reject) => {
|
|
60
|
+
const executor = responder(active, (message) => connection.send(message), (frame) => connection.sendFrame(frame), [active.token]);
|
|
61
|
+
let finished = false;
|
|
62
|
+
const finish = (error) => {
|
|
63
|
+
if (finished)
|
|
64
|
+
return;
|
|
65
|
+
finished = true;
|
|
66
|
+
signal.removeEventListener("abort", abort);
|
|
67
|
+
executor.disconnect();
|
|
68
|
+
connection.stop();
|
|
69
|
+
void Promise.race([executor.idle(), delay(3000, undefined, { ref: false })]).then(() => error ? reject(error) : resolve());
|
|
70
|
+
};
|
|
71
|
+
const abort = () => finish();
|
|
72
|
+
const connection = connectGateway({
|
|
73
|
+
gatewayUrl,
|
|
74
|
+
getToken: () => active.token,
|
|
75
|
+
createSocket: createNodeSocket,
|
|
76
|
+
beforeConnect: async () => {
|
|
77
|
+
try {
|
|
78
|
+
return await gatewayConfig(cloudUrl, active.token);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
if (error instanceof CloudError && error.code === "invalid_device_session")
|
|
82
|
+
return null;
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
onMessage: (message) => {
|
|
87
|
+
if ("from" in message)
|
|
88
|
+
void executor.receive(message);
|
|
89
|
+
},
|
|
90
|
+
onFrame: (frame) => void executor.receiveFrame(frame),
|
|
91
|
+
onNotification: options.onNotification,
|
|
92
|
+
onStatus: (status) => {
|
|
93
|
+
if (status !== "online" && status !== "connecting")
|
|
94
|
+
executor.disconnect();
|
|
95
|
+
options.audit?.host({
|
|
96
|
+
event: "connection",
|
|
97
|
+
status,
|
|
98
|
+
deviceId: active.deviceId,
|
|
99
|
+
name: active.name,
|
|
100
|
+
cloudOrigin: cloudUrl,
|
|
101
|
+
}, [active.token]);
|
|
102
|
+
options.onEvent?.({ type: "connection", status });
|
|
103
|
+
if (status === "revoked" || status === "superseded") {
|
|
104
|
+
queueMicrotask(() => finish(new Error(status === "revoked"
|
|
105
|
+
? "This device was removed from your account. Connect it again to add it back."
|
|
106
|
+
: "Another connection replaced this device. This process has stopped.")));
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
111
|
+
if (signal.aborted)
|
|
112
|
+
abort();
|
|
113
|
+
});
|
|
114
|
+
}
|
package/dist/host.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
export { connectHost, defaultDeviceName, type HostCredential, type HostEvent, type HostOptions, } from "./connect.js";
|
|
2
|
+
export { connectLimitedHost, type LimitedHostOptions } from "./limited-host.js";
|
|
3
|
+
export type { EnsureComputerUseHelper } from "./computer-use/host.js";
|
|
2
4
|
export { loginDevice, type DeviceSession } from "./login.js";
|
|
5
|
+
export { type GatewayNotification } from "./protocol.js";
|
|
3
6
|
export { loginDeviceCode, type DeviceAuthorizationPrompt } from "./device-authorization.js";
|
|
4
7
|
export { createFileCredentialStore, type CredentialStore } from "./storage.js";
|
|
5
|
-
export {
|
|
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
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
export { connectHost, defaultDeviceName, } from "./connect.js";
|
|
2
|
+
export { connectLimitedHost } from "./limited-host.js";
|
|
2
3
|
export { loginDevice } from "./login.js";
|
|
3
4
|
export { loginDeviceCode } from "./device-authorization.js";
|
|
4
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";
|
|
5
8
|
export { AuditStore, } from "./audit.js";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { runHostConnection } from "./host-connection.js";
|
|
2
|
+
import { createLimitedResponder } from "./limited.js";
|
|
3
|
+
export function connectLimitedHost(options) {
|
|
4
|
+
return runHostConnection(options, (session, send, sendFrame) => createLimitedResponder(session.name, send, sendFrame));
|
|
5
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type ActionResponse } from "./actions.js";
|
|
2
|
+
import { type Frame } from "./frame.js";
|
|
3
|
+
import type { InboundEnvelope, OutboundEnvelope } from "./protocol.js";
|
|
4
|
+
export declare const COMPUTER_USE_OFF: ActionResponse;
|
|
5
|
+
export declare function createLimitedResponder(name: string, send: (message: OutboundEnvelope) => boolean, sendFrame?: (frame: Frame) => boolean): {
|
|
6
|
+
receive(message: InboundEnvelope): void;
|
|
7
|
+
receiveFrame(frame: Frame): void;
|
|
8
|
+
idle: () => Promise<void>;
|
|
9
|
+
disconnect(): void;
|
|
10
|
+
};
|
package/dist/limited.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { platform } from "node:os";
|
|
2
|
+
import { actionError, isRecord } from "./actions.js";
|
|
3
|
+
import { decodeMeta, encodeMeta, FRAME_TYPE } from "./frame.js";
|
|
4
|
+
export const COMPUTER_USE_OFF = actionError("computer_use_off", "Computer use is turned off on this Mac, so it did not run the request.");
|
|
5
|
+
const empty = new Uint8Array();
|
|
6
|
+
export function createLimitedResponder(name, send, sendFrame = () => false) {
|
|
7
|
+
const info = () => ({
|
|
8
|
+
type: "response",
|
|
9
|
+
ok: true,
|
|
10
|
+
result: {
|
|
11
|
+
name,
|
|
12
|
+
platform: platform() === "darwin" ? "macos" : platform() === "win32" ? "windows" : platform(),
|
|
13
|
+
actions: [],
|
|
14
|
+
computerUse: false,
|
|
15
|
+
frameVersion: 1,
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
const answer = (action) => action === "system.info" ? info() : COMPUTER_USE_OFF;
|
|
19
|
+
return {
|
|
20
|
+
receive(message) {
|
|
21
|
+
const request = message.payload;
|
|
22
|
+
if (!isRecord(request) || request.type !== "request" || typeof request.action !== "string")
|
|
23
|
+
return;
|
|
24
|
+
send({ id: message.id, to: message.from, payload: answer(request.action) });
|
|
25
|
+
},
|
|
26
|
+
receiveFrame(frame) {
|
|
27
|
+
if (frame.type !== FRAME_TYPE.request)
|
|
28
|
+
return;
|
|
29
|
+
const request = decodeMeta(frame.meta);
|
|
30
|
+
if (!isRecord(request) || request.type !== "request" || typeof request.action !== "string")
|
|
31
|
+
return;
|
|
32
|
+
sendFrame({
|
|
33
|
+
type: FRAME_TYPE.response,
|
|
34
|
+
id: frame.id,
|
|
35
|
+
peer: frame.peer,
|
|
36
|
+
meta: encodeMeta(answer(request.action)),
|
|
37
|
+
body: empty,
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
idle: () => Promise.resolve(),
|
|
41
|
+
disconnect() { },
|
|
42
|
+
};
|
|
43
|
+
}
|