@aivin-labs/cli 1.0.21 → 1.0.27
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/bin/cli.mjs +67 -2
- package/bin/lib/remote/autoStart.mjs +6 -1
- package/bin/lib/remote/autoUpdate.mjs +153 -141
- package/bin/lib/remote/client.mjs +77 -5
- package/bin/lib/remote/constants.mjs +2 -0
- package/bin/lib/remote/desktop/castFileDrop.mjs +227 -0
- package/bin/lib/remote/desktop/castInput.mjs +17 -4
- package/bin/lib/remote/desktop/desktopCast.mjs +3 -0
- package/bin/lib/remote/desktop/desktopCastSource.mjs +132 -20
- package/bin/lib/remote/desktop/desktopWebRtc.mjs +37 -3
- package/bin/lib/remote/desktop/fileAssociation.mjs +104 -0
- package/bin/lib/remote/desktop/index.mjs +54 -5
- package/bin/lib/remote/desktop/strings.mjs +8 -0
- package/bin/lib/remote/desktop/traySource.mjs +72 -1
- package/bin/lib/remote/desktop/uninstall.mjs +96 -0
- package/bin/lib/remote/desktop/webrtcWorker.mjs +191 -181
- package/bin/lib/remote/deviceProfile.mjs +2 -4
- package/bin/lib/remote/mascot/ask.mjs +983 -933
- package/bin/lib/remote/mascot/askHostSource.mjs +4 -2
- package/bin/lib/remote/mascot/fileChips.mjs +2 -2
- package/bin/lib/remote/mascot/index.mjs +69 -5
- package/bin/lib/remote/mascot/strings.mjs +18 -0
- package/bin/lib/remote/sandbox.mjs +3 -3
- package/bin/lib/remote/workFolder.mjs +100 -0
- package/docs/CLI.md +1576 -1545
- package/package.json +75 -75
|
@@ -1,181 +1,191 @@
|
|
|
1
|
-
import net from 'net';
|
|
2
|
-
import readline from 'readline';
|
|
3
|
-
import { spawn } from 'child_process';
|
|
4
|
-
import { createDesktopWebRtc } from './desktopWebRtc.mjs';
|
|
5
|
-
import { createRawFrameReader } from './desktopCast.mjs';
|
|
6
|
-
import { createWebRtcRuntimeLoader } from './webrtcRuntime.mjs';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* WebRTC của Desktop Scenecast chạy trong TIẾN TRÌNH CON riêng (`aivin __desktop-webrtc-worker`): thư viện native `@roamhq/wrtc` làm
|
|
10
|
-
* tiến trình sập lúc thoát (Bun: mọi cách thoát; Node: thoát tự nhiên — đo 02/11/2026). Ở tiến trình con thì sập không ảnh hưởng mã
|
|
11
|
-
* thoát của daemon (trình giữ chạy / tự lùi bản dựa vào mã đó), và lỗi native giữa phiên chỉ làm mất WebRTC (lùi JPEG), không mất daemon.
|
|
12
|
-
*
|
|
13
|
-
* Giao thức: mỗi dòng stdin / stdout một JSON.
|
|
14
|
-
* daemon → con: `start {sessionId, viewerId, iceServers, videoBitrate}` ·`signal {msg}` · `push {frame}` · `pipe {name}` · `close {viewerId}` · `closeAll`
|
|
15
|
-
* daemon → con (con trỏ): `cursor {cursor}` · `cursorShape {shape}`
|
|
16
|
-
*
|
|
17
|
-
* `
|
|
18
|
-
*
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
let
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
sock.
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
else if (m.t === '
|
|
121
|
-
else if (m.t === '
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if (
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
waiting.
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
}
|
|
1
|
+
import net from 'net';
|
|
2
|
+
import readline from 'readline';
|
|
3
|
+
import { spawn } from 'child_process';
|
|
4
|
+
import { createDesktopWebRtc } from './desktopWebRtc.mjs';
|
|
5
|
+
import { createRawFrameReader } from './desktopCast.mjs';
|
|
6
|
+
import { createWebRtcRuntimeLoader } from './webrtcRuntime.mjs';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* WebRTC của Desktop Scenecast chạy trong TIẾN TRÌNH CON riêng (`aivin __desktop-webrtc-worker`): thư viện native `@roamhq/wrtc` làm
|
|
10
|
+
* tiến trình sập lúc thoát (Bun: mọi cách thoát; Node: thoát tự nhiên — đo 02/11/2026). Ở tiến trình con thì sập không ảnh hưởng mã
|
|
11
|
+
* thoát của daemon (trình giữ chạy / tự lùi bản dựa vào mã đó), và lỗi native giữa phiên chỉ làm mất WebRTC (lùi JPEG), không mất daemon.
|
|
12
|
+
*
|
|
13
|
+
* Giao thức: mỗi dòng stdin / stdout một JSON.
|
|
14
|
+
* daemon → con: `start {sessionId, viewerId, iceServers, videoBitrate}` ·`signal {msg}` · `push {frame}` · `pipe {name}` · `close {viewerId}` · `closeAll`
|
|
15
|
+
* daemon → con (con trỏ): `cursor {cursor}` · `cursorShape {shape}`
|
|
16
|
+
* daemon → con (tệp thả vào): `fileReply {viewerId, msg}`
|
|
17
|
+
* con → daemon: `started {viewerId, ok}` · `signal {payload}` · `status {payload}` · `input {raw, viewerId}` · `peerReady {viewerId}` ·
|
|
18
|
+
* `file {viewerId, msg, sessionId}` · `fileChunk {viewerId, data}` (base64; người gửi chờ `ack` nên ống không dồn quá cỡ cửa sổ gửi) ·
|
|
19
|
+
* `state {needsFallback, needsJpeg, hasVideo, videoLive, localCursor}` · `log {text}`
|
|
20
|
+
* Khung thô I420 không đi qua daemon: con tự nối named pipe của tiến trình quay.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export const DESKTOP_WEBRTC_WORKER_ARG = '__desktop-webrtc-worker';
|
|
24
|
+
const VIDEO_LIVE_MS = 3000;
|
|
25
|
+
|
|
26
|
+
export async function runDesktopWebRtcWorker({ input = process.stdin, output = process.stdout } = {}) {
|
|
27
|
+
const send = (msg) => { try { output.write(JSON.stringify(msg) + '\n'); } catch { /* daemon đã đóng */ } };
|
|
28
|
+
let lastRawAt = 0;
|
|
29
|
+
let lastState = '';
|
|
30
|
+
let rtc;
|
|
31
|
+
const sendState = () => {
|
|
32
|
+
const state = {
|
|
33
|
+
needsFallback: rtc.needsFallback(), needsJpeg: rtc.needsJpeg(), hasVideo: rtc.hasVideo(), videoLive: Date.now() - lastRawAt < VIDEO_LIVE_MS, localCursor: rtc.localCursor(),
|
|
34
|
+
};
|
|
35
|
+
const key = JSON.stringify(state);
|
|
36
|
+
if (key !== lastState) { lastState = key; send({ t: 'state', ...state }); }
|
|
37
|
+
};
|
|
38
|
+
rtc = createDesktopWebRtc({
|
|
39
|
+
emitSignal: (payload) => send({ t: 'signal', payload }),
|
|
40
|
+
emitStatus: (payload) => { send({ t: 'status', payload }); sendState(); },
|
|
41
|
+
onInput: (raw, viewerId) => send({ t: 'input', raw, viewerId }),
|
|
42
|
+
onFile: (viewerId, msg, sessionId) => send({ t: 'file', viewerId, msg, sessionId }),
|
|
43
|
+
onFileChunk: (viewerId, bytes) => send({ t: 'fileChunk', viewerId, data: bytes.toString('base64') }),
|
|
44
|
+
onPeerReady: (viewerId) => { send({ t: 'peerReady', viewerId }); sendState(); },
|
|
45
|
+
onCursorMode: () => sendState(),
|
|
46
|
+
loadRuntime: createWebRtcRuntimeLoader({ log: (text) => send({ t: 'log', text }) }),
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
let pipeSocket = null;
|
|
50
|
+
let pipeName = '';
|
|
51
|
+
const connectPipe = (name, attempt = 0) => {
|
|
52
|
+
if (attempt === 0 && name === pipeName && pipeSocket) return;
|
|
53
|
+
pipeName = name;
|
|
54
|
+
pipeSocket?.destroy();
|
|
55
|
+
const sock = net.connect(`\\\\.\\pipe\\${name}`);
|
|
56
|
+
const read = createRawFrameReader((frame) => { if (rtc.pushRaw(frame)) lastRawAt = Date.now(); }, () => sock.destroy());
|
|
57
|
+
sock.on('data', read);
|
|
58
|
+
sock.on('error', () => {});
|
|
59
|
+
sock.on('close', () => {
|
|
60
|
+
if (pipeSocket !== sock) return;
|
|
61
|
+
pipeSocket = null;
|
|
62
|
+
if (attempt < 20) setTimeout(() => { if (!pipeSocket) connectPipe(name, attempt + 1); }, Math.min(2000, 100 * 2 ** attempt));
|
|
63
|
+
});
|
|
64
|
+
pipeSocket = sock;
|
|
65
|
+
};
|
|
66
|
+
const stateTimer = setInterval(sendState, 1000);
|
|
67
|
+
|
|
68
|
+
const handlers = {
|
|
69
|
+
start: async (m) => {
|
|
70
|
+
const ok = await rtc.start({ sessionId: m.sessionId, viewerId: m.viewerId, iceServers: m.iceServers || [], videoBitrate: m.videoBitrate || null }).catch(() => false);
|
|
71
|
+
send({ t: 'started', viewerId: m.viewerId, ok });
|
|
72
|
+
sendState();
|
|
73
|
+
},
|
|
74
|
+
signal: (m) => rtc.signal(m.msg).catch(() => false),
|
|
75
|
+
push: (m) => rtc.push(m.frame),
|
|
76
|
+
cursor: (m) => rtc.pushCursor(m.cursor),
|
|
77
|
+
cursorShape: (m) => rtc.pushCursorShape(m.shape),
|
|
78
|
+
inputFocus: (m) => rtc.pushInputFocus(m.focus),
|
|
79
|
+
fileReply: (m) => rtc.replyFile(m.viewerId, m.msg),
|
|
80
|
+
pipe: (m) => { if (m.name) connectPipe(String(m.name)); },
|
|
81
|
+
close: (m) => { rtc.close(m.viewerId); sendState(); },
|
|
82
|
+
closeAll: () => { rtc.closeAll(); const sock = pipeSocket; pipeSocket = null; pipeName = ''; sock?.destroy(); sendState(); },
|
|
83
|
+
};
|
|
84
|
+
const rl = readline.createInterface({ input });
|
|
85
|
+
rl.on('line', (line) => {
|
|
86
|
+
let msg;
|
|
87
|
+
try { msg = JSON.parse(line); } catch { return; }
|
|
88
|
+
void Promise.resolve(handlers[msg?.t]?.(msg)).catch(() => undefined);
|
|
89
|
+
});
|
|
90
|
+
// Daemon thoát / đóng ống → con thoát theo (Windows không tự giết con khi cha chết).
|
|
91
|
+
await new Promise((resolve) => rl.on('close', resolve));
|
|
92
|
+
clearInterval(stateTimer);
|
|
93
|
+
rtc.closeAll();
|
|
94
|
+
process.exit(0);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Phía daemon: cùng API với createDesktopWebRtc (start / signal / push / needsFallback / needsJpeg / hasVideo / close / closeAll) + `attachPipe`
|
|
99
|
+
* và `videoLive`. Tiến trình con dựng khi phiên đầu cần, tắt khi `closeAll`; con chết giữa phiên → coi như không ai có WebRTC (lùi JPEG).
|
|
100
|
+
*/
|
|
101
|
+
export function createDesktopWebRtcWorker({
|
|
102
|
+
command, emitSignal = () => {}, emitStatus = () => {}, onInput = () => {}, onFile = () => {}, onFileChunk = () => {}, onPeerReady = () => {}, onState = () => {}, log = () => {}, spawnImpl = spawn,
|
|
103
|
+
} = {}) {
|
|
104
|
+
let child = null;
|
|
105
|
+
const NO_PEER_STATE = { needsFallback: true, needsJpeg: true, hasVideo: false, videoLive: false, localCursor: false };
|
|
106
|
+
let state = NO_PEER_STATE;
|
|
107
|
+
const waiting = new Map();
|
|
108
|
+
const peers = new Set();
|
|
109
|
+
|
|
110
|
+
const ensure = () => {
|
|
111
|
+
if (child) return child;
|
|
112
|
+
const [exe, ...args] = command;
|
|
113
|
+
const proc = spawnImpl(exe, [...args, DESKTOP_WEBRTC_WORKER_ARG], { stdio: ['pipe', 'pipe', 'ignore'], windowsHide: true });
|
|
114
|
+
child = proc;
|
|
115
|
+
readline.createInterface({ input: proc.stdout }).on('line', (line) => {
|
|
116
|
+
let m;
|
|
117
|
+
try { m = JSON.parse(line); } catch { return; }
|
|
118
|
+
if (m.t === 'signal') emitSignal(m.payload);
|
|
119
|
+
else if (m.t === 'status') emitStatus(m.payload);
|
|
120
|
+
else if (m.t === 'input') onInput(m.raw, m.viewerId);
|
|
121
|
+
else if (m.t === 'file') onFile(m.viewerId, m.msg, m.sessionId);
|
|
122
|
+
else if (m.t === 'fileChunk') onFileChunk(m.viewerId, Buffer.from(String(m.data || ''), 'base64'));
|
|
123
|
+
else if (m.t === 'peerReady') onPeerReady(m.viewerId);
|
|
124
|
+
else if (m.t === 'state') {
|
|
125
|
+
state = { needsFallback: !!m.needsFallback, needsJpeg: !!m.needsJpeg, hasVideo: !!m.hasVideo, videoLive: !!m.videoLive, localCursor: !!m.localCursor };
|
|
126
|
+
onState();
|
|
127
|
+
}
|
|
128
|
+
else if (m.t === 'log') log(String(m.text || ''));
|
|
129
|
+
else if (m.t === 'started') { waiting.get(m.viewerId)?.(!!m.ok); waiting.delete(m.viewerId); }
|
|
130
|
+
});
|
|
131
|
+
const gone = () => {
|
|
132
|
+
if (child !== proc) return;
|
|
133
|
+
child = null;
|
|
134
|
+
state = NO_PEER_STATE;
|
|
135
|
+
onState();
|
|
136
|
+
for (const done of waiting.values()) done(false);
|
|
137
|
+
waiting.clear();
|
|
138
|
+
peers.clear();
|
|
139
|
+
};
|
|
140
|
+
proc.on('exit', gone);
|
|
141
|
+
proc.on('error', gone);
|
|
142
|
+
proc.stdin.on('error', () => {});
|
|
143
|
+
return proc;
|
|
144
|
+
};
|
|
145
|
+
const send = (msg) => {
|
|
146
|
+
if (!child?.stdin?.writable) return;
|
|
147
|
+
try { child.stdin.write(JSON.stringify(msg) + '\n'); } catch { /* con vừa chết */ }
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
function start({ sessionId, viewerId, iceServers = [], videoBitrate = null }) {
|
|
151
|
+
ensure();
|
|
152
|
+
peers.add(viewerId);
|
|
153
|
+
return new Promise((resolve) => {
|
|
154
|
+
const timer = setTimeout(() => { waiting.delete(viewerId); resolve(false); }, 30_000);
|
|
155
|
+
timer.unref?.();
|
|
156
|
+
waiting.set(viewerId, (ok) => { clearTimeout(timer); resolve(ok); });
|
|
157
|
+
send({ t: 'start', sessionId, viewerId, iceServers, videoBitrate });
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function closeAll() {
|
|
162
|
+
peers.clear();
|
|
163
|
+
if (!child) return;
|
|
164
|
+
const proc = child;
|
|
165
|
+
send({ t: 'closeAll' });
|
|
166
|
+
try { proc.stdin.end(); } catch { /* đã đóng */ }
|
|
167
|
+
// Con tự thoát khi stdin đóng; kẹt thì giết.
|
|
168
|
+
setTimeout(() => { try { proc.kill(); } catch { /* đã thoát */ } }, 3000).unref?.();
|
|
169
|
+
child = null;
|
|
170
|
+
state = NO_PEER_STATE;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return {
|
|
174
|
+
start,
|
|
175
|
+
signal: async (msg) => { send({ t: 'signal', msg }); return true; },
|
|
176
|
+
push: (frame) => { if (child && peers.size) send({ t: 'push', frame }); },
|
|
177
|
+
pushCursor: (cursor) => { if (child && peers.size) send({ t: 'cursor', cursor }); },
|
|
178
|
+
// Hình gửi cả khi chưa có người xem: bộ quay chỉ gửi mỗi hình một lần, tiến trình con giữ lại cho người xem sau.
|
|
179
|
+
pushCursorShape: (shape) => { if (child) send({ t: 'cursorShape', shape }); },
|
|
180
|
+
pushInputFocus: (focus) => { if (child && peers.size) send({ t: 'inputFocus', focus }); },
|
|
181
|
+
replyFile: (viewerId, msg) => send({ t: 'fileReply', viewerId, msg }),
|
|
182
|
+
attachPipe: (name) => { if (name) send({ t: 'pipe', name }); },
|
|
183
|
+
close: (viewerId) => { peers.delete(viewerId); send({ t: 'close', viewerId }); },
|
|
184
|
+
closeAll,
|
|
185
|
+
needsFallback: () => state.needsFallback,
|
|
186
|
+
needsJpeg: () => state.needsJpeg,
|
|
187
|
+
hasVideo: () => state.hasVideo,
|
|
188
|
+
videoLive: () => state.videoLive,
|
|
189
|
+
localCursor: () => state.localCursor,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
@@ -2,14 +2,12 @@ import fs from 'fs';
|
|
|
2
2
|
import os from 'os';
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import { createHash } from 'crypto';
|
|
5
|
-
import {
|
|
5
|
+
import { currentCliVersion } from '../update/versions.mjs';
|
|
6
6
|
import { IGNORED_DIRS } from './constants.mjs';
|
|
7
7
|
import { isProtectedPath } from './sandbox.mjs';
|
|
8
8
|
|
|
9
9
|
export { REMOTE_CONFIG_DIR } from './constants.mjs';
|
|
10
10
|
|
|
11
|
-
const require = createRequire(import.meta.url);
|
|
12
|
-
const CLI_VERSION = (() => { try { return require('../../../package.json').version; } catch { return 'unknown'; } })();
|
|
13
11
|
|
|
14
12
|
/** Chương trình đáng biết trên máy — có thì Planner dùng được, và gói công cụ tương ứng (git…) được bật. */
|
|
15
13
|
const KNOWN_BINARIES = [
|
|
@@ -119,7 +117,7 @@ export function buildDeviceProfile(roots) {
|
|
|
119
117
|
shell: detectShell(),
|
|
120
118
|
os: detectOs(),
|
|
121
119
|
node_version: process.version,
|
|
122
|
-
cli_version:
|
|
120
|
+
cli_version: currentCliVersion(),
|
|
123
121
|
roots: roots.map((r) => path.resolve(r)),
|
|
124
122
|
binaries: detectBinaries(),
|
|
125
123
|
projects: detectProjects(roots),
|