@aivin-labs/cli 1.0.13 → 1.0.24

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 (73) hide show
  1. package/README.md +325 -304
  2. package/bin/cli.mjs +97 -18
  3. package/bin/lib/localPreview.mjs +437 -0
  4. package/bin/lib/openFile.mjs +235 -0
  5. package/bin/lib/previewCache.mjs +71 -0
  6. package/bin/lib/remote/atomicFile.mjs +32 -0
  7. package/bin/lib/remote/autoStart.mjs +2 -1
  8. package/bin/lib/remote/autoUpdate.mjs +153 -129
  9. package/bin/lib/remote/callLedger.mjs +21 -0
  10. package/bin/lib/remote/cdpPolicy.mjs +14 -2
  11. package/bin/lib/remote/client.mjs +1025 -889
  12. package/bin/lib/remote/codingAgents.mjs +2 -2
  13. package/bin/lib/remote/constants.mjs +263 -251
  14. package/bin/lib/remote/credentials.mjs +81 -4
  15. package/bin/lib/remote/daemonLock.mjs +57 -17
  16. package/bin/lib/remote/desktop/castInput.mjs +98 -0
  17. package/bin/lib/remote/desktop/desktopCast.mjs +337 -198
  18. package/bin/lib/remote/desktop/desktopCastCaptureSource.mjs +598 -0
  19. package/bin/lib/remote/desktop/desktopCastSource.mjs +880 -514
  20. package/bin/lib/remote/desktop/desktopWebRtc.mjs +251 -95
  21. package/bin/lib/remote/desktop/fileAssociation.mjs +108 -0
  22. package/bin/lib/remote/desktop/index.mjs +21 -7
  23. package/bin/lib/remote/desktop/linuxTraySource.mjs +1 -8
  24. package/bin/lib/remote/desktop/macTraySource.mjs +0 -2
  25. package/bin/lib/remote/desktop/strings.mjs +28 -2
  26. package/bin/lib/remote/desktop/traySource.mjs +380 -16
  27. package/bin/lib/remote/desktop/webrtcRuntime.mjs +137 -0
  28. package/bin/lib/remote/desktop/webrtcWorker.mjs +183 -0
  29. package/bin/lib/remote/deviceProfile.mjs +2 -4
  30. package/bin/lib/remote/fileIndexer.mjs +30 -14
  31. package/bin/lib/remote/folderRules.mjs +3 -0
  32. package/bin/lib/remote/gitSafe.mjs +31 -0
  33. package/bin/lib/remote/gui/index.mjs +66 -24
  34. package/bin/lib/remote/gui/officeHostSource.mjs +635 -393
  35. package/bin/lib/remote/gui/windowsHost.mjs +3 -1
  36. package/bin/lib/remote/gui/windowsHostSource.mjs +291 -60
  37. package/bin/lib/remote/httpTransfer.mjs +64 -3
  38. package/bin/lib/remote/localPolicy.mjs +73 -7
  39. package/bin/lib/remote/mascot/ask.mjs +956 -768
  40. package/bin/lib/remote/mascot/askBlocksSource.mjs +286 -0
  41. package/bin/lib/remote/mascot/askHostSource.mjs +658 -530
  42. package/bin/lib/remote/mascot/blocks.mjs +201 -0
  43. package/bin/lib/remote/mascot/bot.mjs +26 -2
  44. package/bin/lib/remote/mascot/chartLayout.mjs +382 -0
  45. package/bin/lib/remote/mascot/diagramRender.mjs +154 -0
  46. package/bin/lib/remote/mascot/emojiSource.mjs +273 -0
  47. package/bin/lib/remote/mascot/fileChips.mjs +62 -0
  48. package/bin/lib/remote/mascot/hostSource.mjs +328 -49
  49. package/bin/lib/remote/mascot/hotkey.mjs +31 -7
  50. package/bin/lib/remote/mascot/images.mjs +111 -0
  51. package/bin/lib/remote/mascot/index.mjs +663 -493
  52. package/bin/lib/remote/mascot/lucideIcons.mjs +32 -0
  53. package/bin/lib/remote/mascot/mermaidBundle.mjs +28 -0
  54. package/bin/lib/remote/mascot/mood.mjs +57 -0
  55. package/bin/lib/remote/mascot/rigSource.mjs +643 -609
  56. package/bin/lib/remote/mascot/safeUrl.mjs +29 -0
  57. package/bin/lib/remote/mascot/strings.mjs +459 -375
  58. package/bin/lib/remote/mascot/ui.mjs +27 -7
  59. package/bin/lib/remote/multiUser.mjs +7 -2
  60. package/bin/lib/remote/notify.mjs +4 -1
  61. package/bin/lib/remote/office/index.mjs +12 -6
  62. package/bin/lib/remote/processes.mjs +5 -2
  63. package/bin/lib/remote/sandbox.mjs +26 -0
  64. package/bin/lib/remote/secretPaths.mjs +21 -0
  65. package/bin/lib/remote/tokenRotation.mjs +28 -0
  66. package/bin/lib/remote/toolHandlers.mjs +79 -15
  67. package/bin/lib/remote/watchBatcher.mjs +56 -0
  68. package/bin/lib/selfUpdate.mjs +12 -2
  69. package/bin/lib/update/binarySwap.mjs +15 -1
  70. package/bin/lib/update/state.mjs +10 -0
  71. package/docs/CLI.md +1545 -1504
  72. package/docs/media/aivin-ecosystem.jpg +0 -0
  73. package/package.json +75 -71
@@ -1,198 +1,337 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import readline from 'readline';
4
- import { createHash } from 'crypto';
5
- import { spawn } from 'child_process';
6
- import { REMOTE_CONFIG_DIR } from '../constants.mjs';
7
- import { DESKTOP_CAST_CSHARP, DESKTOP_CAST_PS1 } from './desktopCastSource.mjs';
8
-
9
- const CAST_DIR = path.join(REMOTE_CONFIG_DIR, 'desktop-cast');
10
-
11
- function hostFiles(dir = CAST_DIR) {
12
- const hash = createHash('sha256').update(DESKTOP_CAST_CSHARP).update(DESKTOP_CAST_PS1).digest('hex').slice(0, 16);
13
- fs.mkdirSync(dir, { recursive: true });
14
- const src = path.join(dir, `cast-${hash}.cs`);
15
- const ps1 = path.join(dir, `cast-${hash}.ps1`);
16
- if (!fs.existsSync(src)) fs.writeFileSync(src, DESKTOP_CAST_CSHARP);
17
- if (!fs.existsSync(ps1)) fs.writeFileSync(ps1, DESKTOP_CAST_PS1);
18
- for (const name of fs.readdirSync(dir)) {
19
- if (name.startsWith('cast-') && !name.startsWith(`cast-${hash}`)) fs.rmSync(path.join(dir, name), { force: true });
20
- }
21
- return { src, ps1, dll: path.join(dir, `cast-${hash}.dll`) };
22
- }
23
-
24
- export function isDesktopCastSupported(platform = process.platform) {
25
- return platform === 'win32';
26
- }
27
-
28
- /**
29
- * Quản lý phiên phát hình ảnh và điều khiển máy tính từ xa.
30
- */
31
- export function createDesktopCastManager({ dir = CAST_DIR, spawnImpl = spawn } = {}) {
32
- let activeSession = null;
33
-
34
- function start({
35
- sessionId,
36
- maxFps = 15,
37
- maxWidth = 1366,
38
- quality = 65,
39
- monitor = -1,
40
- onFrame,
41
- onReady,
42
- onError,
43
- onStop,
44
- } = {}) {
45
- if (activeSession) {
46
- stop();
47
- }
48
-
49
- if (!isDesktopCastSupported()) {
50
- onError?.(new Error('Desktop cast is currently supported on Windows only'));
51
- return null;
52
- }
53
-
54
- const files = hostFiles(dir);
55
- const argsJson = JSON.stringify({
56
- max_fps: maxFps,
57
- max_width: maxWidth,
58
- quality,
59
- monitor,
60
- });
61
-
62
- const child = spawnImpl('powershell.exe', [
63
- '-NoProfile',
64
- '-NonInteractive',
65
- '-ExecutionPolicy',
66
- 'Bypass',
67
- '-File',
68
- files.ps1,
69
- ], {
70
- env: {
71
- ...process.env,
72
- AIVIN_CAST_SRC: files.src,
73
- AIVIN_CAST_DLL: files.dll,
74
- AIVIN_CAST_ARGS: argsJson,
75
- },
76
- windowsHide: true,
77
- });
78
-
79
- let rl = readline.createInterface({ input: child.stdout });
80
-
81
- rl.on('line', (line) => {
82
- let msg;
83
- try {
84
- msg = JSON.parse(line);
85
- } catch {
86
- return;
87
- }
88
-
89
- if (msg.t === 'ready') {
90
- onReady?.(msg);
91
- } else if (msg.t === 'frame') {
92
- onFrame?.({
93
- sessionId,
94
- image: msg.image,
95
- width: msg.width,
96
- height: msg.height,
97
- screenWidth: msg.screenWidth,
98
- screenHeight: msg.screenHeight,
99
- monitor: msg.monitor,
100
- screens: msg.screens,
101
- ts: msg.ts,
102
- });
103
- } else if (msg.t === 'error') {
104
- onError?.(new Error(msg.error || 'Desktop cast error'));
105
- }
106
- });
107
-
108
- child.on('error', (err) => {
109
- onError?.(err);
110
- activeSession = null;
111
- });
112
-
113
- child.on('close', () => {
114
- activeSession = null;
115
- onStop?.();
116
- });
117
-
118
- function sendInput(inputCmd) {
119
- if (!child.stdin || child.stdin.destroyed) return;
120
- try {
121
- const payload = JSON.stringify({ op: 'input', ...inputCmd });
122
- child.stdin.write(payload + '\n');
123
- } catch {
124
- // bỏ qua lỗi ghi stdin nếu tiến trình đang đóng
125
- }
126
- }
127
-
128
- function config(configCmd) {
129
- if (!child.stdin || child.stdin.destroyed) return;
130
- try {
131
- const payload = JSON.stringify({ op: 'config', ...configCmd });
132
- child.stdin.write(payload + '\n');
133
- } catch {
134
- // bỏ qua
135
- }
136
- }
137
-
138
- function stop() {
139
- if (!activeSession) return;
140
- activeSession = null;
141
- try {
142
- if (child.stdin && !child.stdin.destroyed) {
143
- child.stdin.write('stop\n');
144
- }
145
- } catch {
146
- // bỏ qua
147
- }
148
- setTimeout(() => {
149
- try {
150
- child.kill();
151
- } catch {
152
- // bỏ qua
153
- }
154
- }, 500);
155
- }
156
-
157
- activeSession = {
158
- sessionId,
159
- child,
160
- sendInput,
161
- config,
162
- stop,
163
- };
164
-
165
- return activeSession;
166
- }
167
-
168
- function sendInput(inputCmd) {
169
- if (activeSession) {
170
- activeSession.sendInput(inputCmd);
171
- }
172
- }
173
-
174
- function config(configCmd) {
175
- if (activeSession) {
176
- activeSession.config(configCmd);
177
- }
178
- }
179
-
180
- function stop() {
181
- if (activeSession) {
182
- activeSession.stop();
183
- activeSession = null;
184
- }
185
- }
186
-
187
- function isActive() {
188
- return activeSession !== null;
189
- }
190
-
191
- return {
192
- start,
193
- sendInput,
194
- config,
195
- stop,
196
- isActive,
197
- };
198
- }
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import readline from 'readline';
4
+ import { createHash, randomBytes } from 'crypto';
5
+ import { spawn } from 'child_process';
6
+ import { REMOTE_CONFIG_DIR } from '../constants.mjs';
7
+ import { DESKTOP_CAST_CSHARP, DESKTOP_CAST_PS1 } from './desktopCastSource.mjs';
8
+
9
+ const CAST_DIR = path.join(REMOTE_CONFIG_DIR, 'desktop-cast');
10
+
11
+ function hostFiles(dir = CAST_DIR) {
12
+ const hash = createHash('sha256').update(DESKTOP_CAST_CSHARP).update(DESKTOP_CAST_PS1).digest('hex').slice(0, 16);
13
+ fs.mkdirSync(dir, { recursive: true });
14
+ const src = path.join(dir, `cast-${hash}.cs`);
15
+ const ps1 = path.join(dir, `cast-${hash}.ps1`);
16
+ if (!fs.existsSync(src)) fs.writeFileSync(src, DESKTOP_CAST_CSHARP);
17
+ if (!fs.existsSync(ps1)) fs.writeFileSync(ps1, DESKTOP_CAST_PS1);
18
+ for (const name of fs.readdirSync(dir)) {
19
+ if (name.startsWith('cast-') && !name.startsWith(`cast-${hash}`)) fs.rmSync(path.join(dir, name), { force: true });
20
+ }
21
+ return { src, ps1, dll: path.join(dir, `cast-${hash}.dll`) };
22
+ }
23
+
24
+ const RAW_HEADER_BYTES = 12;
25
+ const MAX_RAW_FRAME_BYTES = 64 * 1024 * 1024;
26
+
27
+ /**
28
+ * Tách luồng khung thô từ named pipe: mỗi khung = [rộng, cao, độ dài] int32 LE + dữ liệu I420. Trả hàm nhận từng khúc dữ liệu.
29
+ * Độ dài vô lý → `onCorrupt` (đóng pipe, không cố đồng bộ lại).
30
+ */
31
+ export function createRawFrameReader(onFrame, onCorrupt = () => {}) {
32
+ const header = Buffer.alloc(RAW_HEADER_BYTES);
33
+ let headerFilled = 0;
34
+ let frame = null;
35
+ let filled = 0;
36
+ return (chunk) => {
37
+ let pos = 0;
38
+ while (pos < chunk.length) {
39
+ if (!frame) {
40
+ const n = chunk.copy(header, headerFilled, pos, pos + RAW_HEADER_BYTES - headerFilled);
41
+ headerFilled += n;
42
+ pos += n;
43
+ if (headerFilled < RAW_HEADER_BYTES) return;
44
+ headerFilled = 0;
45
+ const width = header.readInt32LE(0);
46
+ const height = header.readInt32LE(4);
47
+ const length = header.readInt32LE(8);
48
+ if (width <= 0 || height <= 0 || length !== width * height * 1.5 || length > MAX_RAW_FRAME_BYTES) {
49
+ onCorrupt();
50
+ return;
51
+ }
52
+ // Vùng nhớ riêng cho từng khung (bộ mã hoá giữ tham chiếu), chép thẳng từng khúc: khúc pipe 64 KB, ghép dần cả khung ăn luồng chính ~9 ms.
53
+ frame = { width, height, data: new Uint8ClampedArray(length) };
54
+ filled = 0;
55
+ }
56
+ const piece = chunk.subarray(pos, pos + frame.data.length - filled);
57
+ frame.data.set(piece, filled);
58
+ filled += piece.length;
59
+ pos += piece.length;
60
+ if (filled === frame.data.length) {
61
+ const done = frame;
62
+ frame = null;
63
+ onFrame(done);
64
+ }
65
+ }
66
+ };
67
+ }
68
+
69
+ export function isDesktopCastSupported(platform = process.platform) {
70
+ return platform === 'win32';
71
+ }
72
+
73
+ /**
74
+ * Quản lý phiên phát hình ảnh và điều khiển máy tính từ xa.
75
+ */
76
+ export function createDesktopCastManager({ dir = CAST_DIR, spawnImpl = spawn } = {}) {
77
+ let activeSession = null;
78
+
79
+ function start({
80
+ sessionId,
81
+ maxFps = 15,
82
+ maxWidth = 1366,
83
+ quality = 65,
84
+ monitor = -1,
85
+ onFrame,
86
+ onMeta,
87
+ onCursor,
88
+ onCursorShape,
89
+ onInputFocus,
90
+ onCapture,
91
+ rawPipe = false,
92
+ onReady,
93
+ onError,
94
+ onStop,
95
+ } = {}) {
96
+ if (activeSession) {
97
+ stop();
98
+ }
99
+
100
+ if (!isDesktopCastSupported()) {
101
+ onError?.(new Error('Desktop cast is currently supported on Windows only'));
102
+ return null;
103
+ }
104
+
105
+ const files = hostFiles(dir);
106
+ // Có nơi nhận khung thô (nguồn video WebRTC ở tiến trình con webrtcWorker.mjs) → tiến trình quay mở named pipe gửi I420; không thì chỉ JPEG.
107
+ const pipeName = rawPipe ? `aivin-cast-${process.pid}-${randomBytes(6).toString('hex')}` : '';
108
+ const argsJson = JSON.stringify({
109
+ max_fps: maxFps,
110
+ max_width: maxWidth,
111
+ quality,
112
+ monitor,
113
+ ...(pipeName ? { pipe: pipeName } : {}),
114
+ });
115
+
116
+ const child = spawnImpl('powershell.exe', [
117
+ '-NoProfile',
118
+ '-NonInteractive',
119
+ '-ExecutionPolicy',
120
+ 'Bypass',
121
+ '-File',
122
+ files.ps1,
123
+ ], {
124
+ env: {
125
+ ...process.env,
126
+ AIVIN_CAST_SRC: files.src,
127
+ AIVIN_CAST_DLL: files.dll,
128
+ AIVIN_CAST_ARGS: argsJson,
129
+ },
130
+ windowsHide: true,
131
+ });
132
+
133
+ let rl = readline.createInterface({ input: child.stdout });
134
+
135
+ rl.on('line', (line) => {
136
+ let msg;
137
+ try {
138
+ msg = JSON.parse(line);
139
+ } catch {
140
+ return;
141
+ }
142
+
143
+ if (msg.t === 'ready') {
144
+ onReady?.(msg);
145
+ } else if (msg.t === 'frame') {
146
+ onFrame?.({
147
+ sessionId: session.sessionId,
148
+ image: msg.image,
149
+ width: msg.width,
150
+ height: msg.height,
151
+ screenWidth: msg.screenWidth,
152
+ screenHeight: msg.screenHeight,
153
+ screenX: msg.screenX,
154
+ screenY: msg.screenY,
155
+ monitor: msg.monitor,
156
+ screens: msg.screens,
157
+ ts: msg.ts,
158
+ stats: msg.stats,
159
+ });
160
+ } else if (msg.t === 'meta') {
161
+ onMeta?.({
162
+ sessionId: session.sessionId,
163
+ width: msg.width,
164
+ height: msg.height,
165
+ screenWidth: msg.screenWidth,
166
+ screenHeight: msg.screenHeight,
167
+ screenX: msg.screenX,
168
+ screenY: msg.screenY,
169
+ monitor: msg.monitor,
170
+ screens: msg.screens,
171
+ ts: msg.ts,
172
+ stats: msg.stats,
173
+ });
174
+ } else if (msg.t === 'cursor') {
175
+ onCursor?.({ x: msg.x, y: msg.y, visible: msg.visible, shape: msg.shape });
176
+ } else if (msg.t === 'cursor_shape') {
177
+ onCursorShape?.({ id: msg.id, png: msg.png, hotX: msg.hotX, hotY: msg.hotY });
178
+ } else if (msg.t === 'input_focus') {
179
+ onInputFocus?.({ editable: msg.editable === true });
180
+ } else if (msg.t === 'capture') {
181
+ onCapture?.({ mode: msg.mode, reason: msg.reason });
182
+ } else if (msg.t === 'error') {
183
+ onError?.(new Error(msg.error || 'Desktop cast error'));
184
+ }
185
+ });
186
+
187
+ child.on('error', (err) => {
188
+ onError?.(err);
189
+ if (activeSession === session) activeSession = null;
190
+ });
191
+
192
+ child.on('close', () => {
193
+ if (activeSession === session) activeSession = null;
194
+ onStop?.();
195
+ });
196
+
197
+ function sendInput(inputCmd) {
198
+ if (!child.stdin || child.stdin.destroyed) return;
199
+ try {
200
+ const payload = JSON.stringify({ ...inputCmd, op: 'input' });
201
+ child.stdin.write(payload + '\n');
202
+ } catch {
203
+ // bỏ qua lỗi ghi stdin nếu tiến trình đang đóng
204
+ }
205
+ }
206
+
207
+ function config(configCmd) {
208
+ if (!child.stdin || child.stdin.destroyed) return;
209
+ try {
210
+ const payload = JSON.stringify({ ...configCmd, op: 'config' });
211
+ child.stdin.write(payload + '\n');
212
+ } catch {
213
+ // bỏ qua
214
+ }
215
+ }
216
+
217
+ /** Bật / tắt nén JPEG (chỉ cần khi còn người xem chưa có video WebRTC). */
218
+ let jpegOn = true;
219
+ function setJpeg(on) {
220
+ if (!pipeName || jpegOn === on) return;
221
+ jpegOn = on;
222
+ config({ jpeg: on });
223
+ }
224
+
225
+ /** Con trỏ lồng vào khung hình (mặc định) hay gửi riêng để FE tự vẽ. */
226
+ let cursorInFrame = true;
227
+ function setCursorInFrame(on) {
228
+ if (cursorInFrame === on) return;
229
+ cursorInFrame = on;
230
+ config({ cursor: on });
231
+ }
232
+
233
+ /** Gửi ngay một khung (người xem mới nối, màn hình đang đứng yên). */
234
+ function refresh() {
235
+ if (!child.stdin || child.stdin.destroyed) return;
236
+ try { child.stdin.write(JSON.stringify({ op: 'refresh' }) + '\n'); } catch { /* đang đóng */ }
237
+ }
238
+
239
+ function stop() {
240
+ if (activeSession === session) activeSession = null;
241
+ try {
242
+ if (child.stdin && !child.stdin.destroyed) {
243
+ child.stdin.write('stop\n');
244
+ }
245
+ } catch {
246
+ // bỏ qua
247
+ }
248
+ setTimeout(() => {
249
+ try {
250
+ child.kill();
251
+ } catch {
252
+ // bỏ qua
253
+ }
254
+ }, 500);
255
+ }
256
+
257
+ const session = {
258
+ sessionId,
259
+ child,
260
+ sendInput,
261
+ config,
262
+ setJpeg,
263
+ setCursorInFrame,
264
+ refresh,
265
+ stop,
266
+ pipeName,
267
+ };
268
+ activeSession = session;
269
+
270
+ return activeSession;
271
+ }
272
+
273
+ function setSessionId(sessionId) {
274
+ if (activeSession && sessionId) {
275
+ activeSession.sessionId = sessionId;
276
+ }
277
+ }
278
+
279
+ function sendInput(inputCmd) {
280
+ if (activeSession) {
281
+ activeSession.sendInput(inputCmd);
282
+ }
283
+ }
284
+
285
+ function config(configCmd) {
286
+ if (activeSession) {
287
+ activeSession.config(configCmd);
288
+ }
289
+ }
290
+
291
+ function stop() {
292
+ if (activeSession) {
293
+ activeSession.stop();
294
+ activeSession = null;
295
+ }
296
+ }
297
+
298
+ function setJpeg(on) {
299
+ activeSession?.setJpeg(on);
300
+ }
301
+
302
+ function setCursorInFrame(on) {
303
+ activeSession?.setCursorInFrame(on);
304
+ }
305
+
306
+ function refresh() {
307
+ activeSession?.refresh();
308
+ }
309
+
310
+ /** Tên named pipe khung thô của phiên đang phát (rỗng khi không bật `rawPipe`). */
311
+ function pipeName() {
312
+ return activeSession?.pipeName || '';
313
+ }
314
+
315
+ function isActive() {
316
+ return activeSession !== null;
317
+ }
318
+
319
+ /** Mã phiên đang phát (BE đặt) — lệnh / thao tác mang mã khác bị bỏ. */
320
+ function sessionId() {
321
+ return activeSession?.sessionId || null;
322
+ }
323
+
324
+ return {
325
+ start,
326
+ setSessionId,
327
+ sessionId,
328
+ sendInput,
329
+ config,
330
+ setJpeg,
331
+ setCursorInFrame,
332
+ refresh,
333
+ pipeName,
334
+ stop,
335
+ isActive,
336
+ };
337
+ }