@aivin-labs/cli 1.0.11 → 1.0.13

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 +16 -0
  2. package/bin/cli.mjs +67 -0
  3. package/bin/lib/remote/browserTunnel.mjs +47 -8
  4. package/bin/lib/remote/callLedger.mjs +73 -14
  5. package/bin/lib/remote/client.mjs +89 -8
  6. package/bin/lib/remote/cloudFiles.mjs +53 -0
  7. package/bin/lib/remote/constants.mjs +17 -0
  8. package/bin/lib/remote/dailyBrowser.mjs +8 -2
  9. package/bin/lib/remote/desktop/desktopCast.mjs +198 -0
  10. package/bin/lib/remote/desktop/desktopCastSource.mjs +514 -0
  11. package/bin/lib/remote/desktop/desktopWebRtc.mjs +95 -0
  12. package/bin/lib/remote/desktop/index.mjs +44 -7
  13. package/bin/lib/remote/desktop/linux.mjs +1 -1
  14. package/bin/lib/remote/desktop/linuxTraySource.mjs +434 -427
  15. package/bin/lib/remote/desktop/mac.mjs +2 -2
  16. package/bin/lib/remote/desktop/macTraySource.mjs +295 -292
  17. package/bin/lib/remote/desktop/traySource.mjs +541 -527
  18. package/bin/lib/remote/deviceProfile.mjs +1 -0
  19. package/bin/lib/remote/docText.mjs +85 -0
  20. package/bin/lib/remote/fileIndexer.mjs +757 -399
  21. package/bin/lib/remote/folderRules.mjs +140 -0
  22. package/bin/lib/remote/gui/index.mjs +126 -17
  23. package/bin/lib/remote/gui/windowsHost.mjs +9 -2
  24. package/bin/lib/remote/gui/windowsHostSource.mjs +194 -51
  25. package/bin/lib/remote/idempotency.mjs +49 -20
  26. package/bin/lib/remote/localPolicy.mjs +8 -2
  27. package/bin/lib/remote/mascot/askHostSource.mjs +18 -0
  28. package/bin/lib/remote/mascot/bot.mjs +29 -4
  29. package/bin/lib/remote/mascot/hostSource.mjs +4 -4
  30. package/bin/lib/remote/mascot/rigSource.mjs +12 -7
  31. package/bin/lib/remote/osSearch.mjs +107 -0
  32. package/bin/lib/remote/sqliteDb.mjs +51 -0
  33. package/bin/lib/remote/toolHandlers.mjs +788 -741
  34. package/bin/lib/remoteControl.mjs +26 -26
  35. package/docs/AIVIN_MASCOT.md +14 -0
  36. package/docs/CLI.md +1504 -1484
  37. package/docs/media/aivin-mascot-live.gif +0 -0
  38. package/docs/media/aivin-mascot-live.png +0 -0
  39. package/docs/media/aivin-remote-architecture.svg +102 -0
  40. package/package.json +71 -71
  41. package/docs/media/aivin-mascot-states.mp4 +0 -0
@@ -256,7 +256,11 @@ export function createCdpMux({ wsUrl, downloadsDir, consentTimeoutMs = CONSENT_T
256
256
  sessions.set(params.sessionId, { client, targetId, type: params.targetInfo?.type, parent, aivin });
257
257
  if (aivin) markTabAivin(params.targetInfo?.type === 'tab' ? params.sessionId : parent);
258
258
  }
259
- if (msg.method === 'Target.detachedFromTarget' && params.sessionId) sessions.delete(params.sessionId);
259
+ if (msg.method === 'Target.detachedFromTarget' && params.sessionId) {
260
+ sessions.delete(params.sessionId);
261
+ for (const [sid, s] of [...sessions]) if (s.parent === params.sessionId) sessions.delete(sid);
262
+ for (const k of [...client.requests.keys()]) if (k.startsWith(`${params.sessionId}:`)) client.requests.delete(k);
263
+ }
260
264
  if (msg.method === 'Target.targetDestroyed' && params.targetId) aivinTargets.delete(params.targetId);
261
265
  };
262
266
 
@@ -332,7 +336,9 @@ export function createCdpMux({ wsUrl, downloadsDir, consentTimeoutMs = CONSENT_T
332
336
  if (msg.id !== undefined) toClient(JSON.stringify({ id: msg.id, ...(msg.sessionId ? { sessionId: msg.sessionId } : {}), error: { code: -32000, message: reason } }));
333
337
  return;
334
338
  }
335
- if (msg.id !== undefined) client.requests.set(`${sid}:${msg.id}`, { method: msg.method, targetId: params.targetId });
339
+ if (msg.id !== undefined && (msg.method === 'Target.createTarget' || msg.method === 'Target.attachToTarget')) {
340
+ client.requests.set(`${sid}:${msg.id}`, { method: msg.method, targetId: params.targetId });
341
+ }
336
342
  // Thư mục tải đặt qua một trang áp cho cả hồ sơ: nhớ để trả về mặc định khi đường hầm cuối cùng đóng (tệp người dùng tự tải về đúng chỗ cũ).
337
343
  if (msg.method === 'Page.setDownloadBehavior') client.setDownloads = true;
338
344
  // Tab mới mở ở nền: không giật tiêu điểm khỏi cửa sổ người dùng đang làm việc.
@@ -0,0 +1,198 @@
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
+ }