@aivin-labs/cli 1.0.11 → 1.0.21

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 (88) hide show
  1. package/README.md +325 -288
  2. package/bin/cli.mjs +163 -17
  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 +12 -0
  9. package/bin/lib/remote/browserTunnel.mjs +47 -8
  10. package/bin/lib/remote/callLedger.mjs +94 -14
  11. package/bin/lib/remote/cdpPolicy.mjs +14 -2
  12. package/bin/lib/remote/client.mjs +1024 -808
  13. package/bin/lib/remote/cloudFiles.mjs +53 -0
  14. package/bin/lib/remote/codingAgents.mjs +2 -2
  15. package/bin/lib/remote/constants.mjs +261 -234
  16. package/bin/lib/remote/credentials.mjs +81 -4
  17. package/bin/lib/remote/daemonLock.mjs +57 -17
  18. package/bin/lib/remote/dailyBrowser.mjs +8 -2
  19. package/bin/lib/remote/desktop/castInput.mjs +98 -0
  20. package/bin/lib/remote/desktop/desktopCast.mjs +334 -0
  21. package/bin/lib/remote/desktop/desktopCastCaptureSource.mjs +598 -0
  22. package/bin/lib/remote/desktop/desktopCastSource.mjs +798 -0
  23. package/bin/lib/remote/desktop/desktopWebRtc.mjs +242 -0
  24. package/bin/lib/remote/desktop/fileAssociation.mjs +108 -0
  25. package/bin/lib/remote/desktop/index.mjs +58 -7
  26. package/bin/lib/remote/desktop/linux.mjs +1 -1
  27. package/bin/lib/remote/desktop/linuxTraySource.mjs +427 -427
  28. package/bin/lib/remote/desktop/mac.mjs +2 -2
  29. package/bin/lib/remote/desktop/macTraySource.mjs +293 -292
  30. package/bin/lib/remote/desktop/strings.mjs +28 -2
  31. package/bin/lib/remote/desktop/traySource.mjs +905 -527
  32. package/bin/lib/remote/desktop/webrtcRuntime.mjs +137 -0
  33. package/bin/lib/remote/desktop/webrtcWorker.mjs +181 -0
  34. package/bin/lib/remote/deviceProfile.mjs +1 -0
  35. package/bin/lib/remote/docText.mjs +85 -0
  36. package/bin/lib/remote/fileIndexer.mjs +773 -399
  37. package/bin/lib/remote/folderRules.mjs +143 -0
  38. package/bin/lib/remote/gitSafe.mjs +31 -0
  39. package/bin/lib/remote/gui/index.mjs +170 -19
  40. package/bin/lib/remote/gui/officeHostSource.mjs +635 -393
  41. package/bin/lib/remote/gui/windowsHost.mjs +12 -3
  42. package/bin/lib/remote/gui/windowsHostSource.mjs +461 -87
  43. package/bin/lib/remote/httpTransfer.mjs +64 -3
  44. package/bin/lib/remote/idempotency.mjs +49 -20
  45. package/bin/lib/remote/localPolicy.mjs +81 -9
  46. package/bin/lib/remote/mascot/ask.mjs +207 -42
  47. package/bin/lib/remote/mascot/askBlocksSource.mjs +286 -0
  48. package/bin/lib/remote/mascot/askHostSource.mjs +658 -512
  49. package/bin/lib/remote/mascot/blocks.mjs +201 -0
  50. package/bin/lib/remote/mascot/bot.mjs +54 -5
  51. package/bin/lib/remote/mascot/chartLayout.mjs +382 -0
  52. package/bin/lib/remote/mascot/diagramRender.mjs +154 -0
  53. package/bin/lib/remote/mascot/emojiSource.mjs +273 -0
  54. package/bin/lib/remote/mascot/fileChips.mjs +62 -0
  55. package/bin/lib/remote/mascot/hostSource.mjs +331 -52
  56. package/bin/lib/remote/mascot/hotkey.mjs +31 -7
  57. package/bin/lib/remote/mascot/images.mjs +111 -0
  58. package/bin/lib/remote/mascot/index.mjs +663 -493
  59. package/bin/lib/remote/mascot/lucideIcons.mjs +32 -0
  60. package/bin/lib/remote/mascot/mermaidBundle.mjs +28 -0
  61. package/bin/lib/remote/mascot/mood.mjs +57 -0
  62. package/bin/lib/remote/mascot/rigSource.mjs +648 -609
  63. package/bin/lib/remote/mascot/safeUrl.mjs +29 -0
  64. package/bin/lib/remote/mascot/strings.mjs +456 -375
  65. package/bin/lib/remote/mascot/ui.mjs +27 -7
  66. package/bin/lib/remote/multiUser.mjs +7 -2
  67. package/bin/lib/remote/notify.mjs +4 -1
  68. package/bin/lib/remote/office/index.mjs +12 -6
  69. package/bin/lib/remote/osSearch.mjs +107 -0
  70. package/bin/lib/remote/processes.mjs +5 -2
  71. package/bin/lib/remote/sandbox.mjs +26 -0
  72. package/bin/lib/remote/secretPaths.mjs +21 -0
  73. package/bin/lib/remote/sqliteDb.mjs +51 -0
  74. package/bin/lib/remote/tokenRotation.mjs +28 -0
  75. package/bin/lib/remote/toolHandlers.mjs +852 -741
  76. package/bin/lib/remote/watchBatcher.mjs +56 -0
  77. package/bin/lib/remoteControl.mjs +26 -26
  78. package/bin/lib/selfUpdate.mjs +12 -2
  79. package/bin/lib/update/binarySwap.mjs +15 -1
  80. package/bin/lib/update/state.mjs +10 -0
  81. package/docs/AIVIN_MASCOT.md +14 -0
  82. package/docs/CLI.md +1545 -1484
  83. package/docs/media/aivin-ecosystem.jpg +0 -0
  84. package/docs/media/aivin-mascot-live.gif +0 -0
  85. package/docs/media/aivin-mascot-live.png +0 -0
  86. package/docs/media/aivin-remote-architecture.svg +102 -0
  87. package/package.json +75 -71
  88. package/docs/media/aivin-mascot-states.mp4 +0 -0
@@ -1,18 +1,26 @@
1
1
  import { spawnSync } from 'child_process';
2
2
 
3
3
  /**
4
- * Phím tắt gọi linh vật (29/10/2026): một tổ hợp toàn hệ thống (`RegisterHotKey`, không móc bàn phím) — bấm → mở ô hỏi + nghe luôn. Mặc định
5
- * thử lần lượt `DEFAULT_HOTKEYS`, lấy tổ hợp đầu tiên đăng ký được (mỗi máy cài app khác nhau: máy dev 29/10 có Ctrl+Alt+Space,
6
- * Ctrl+Win+Space, Win+Shift+Space đều đã bị giữ — lỗi 1409). Chủ máy đặt riêng: `aivin remote mascot hotkey <tổ hợp> | off` (mascot.json).
4
+ * Phím tắt gọi linh vật (29/10/2026): tổ hợp toàn hệ thống (`RegisterHotKey`, không móc bàn phím), một cho nói (mở ô hỏi + nghe luôn), một
5
+ * cho gõ. Mặc định thử lần lượt `DEFAULT_HOTKEYS` / `DEFAULT_CHAT_HOTKEYS`, lấy tổ hợp đầu tiên đăng ký được (mỗi máy cài app khác nhau: máy
6
+ * dev 29/10 có Ctrl+Alt+Space, Ctrl+Win+Space, Win+Shift+Space đều đã bị giữ — lỗi 1409). Chủ máy đặt riêng:
7
+ * `aivin remote mascot hotkey [--chat] <tổ hợp> | off` (mascot.json `hotkey` / `chat_hotkey`); daemon chuyển danh sách vào config.json của
8
+ * cửa sổ (`hostHotkeys`) — có hiệu lực từ lần cửa sổ mở kế tiếp.
7
9
  */
8
10
 
9
11
  export const HOTKEY_MODS = Object.freeze({ alt: 0x1, ctrl: 0x2, shift: 0x4, win: 0x8 });
10
12
  export const MOD_NOREPEAT = 0x4000;
11
- export const DEFAULT_HOTKEYS = Object.freeze(['Win+Alt+Space', 'Ctrl+Shift+Alt+Space', 'Ctrl+Shift+Alt+A']);
13
+ // Không dùng Ctrl+Alt+<chữ> (kể cả thêm Shift): trên bàn phím quốc tế Ctrl+Alt = AltGr, đăng ký toàn hệ thống là nuốt mất ký tự gõ
14
+ // (@, €, ć, ą…) của mọi ứng dụng (02/11/2026).
15
+ // Mặc định đầu tiên bấm được bằng một tay, thêm Shift = chuyển từ gõ sang nói; app khác đã giữ thì lùi về tổ hợp dài hơn. Alt+Space lấy mất
16
+ // phím mở menu cửa sổ của Windows — đánh đổi đã chấp nhận (02/11/2026).
17
+ export const DEFAULT_HOTKEYS = Object.freeze(['Alt+Shift+Space', 'Win+Alt+Space', 'Ctrl+Alt+Shift+Space', 'Win+Alt+A']);
18
+ /** Phím tắt mở ô hỏi để gõ (không nghe). */
19
+ export const DEFAULT_CHAT_HOTKEYS = Object.freeze(['Alt+Space', 'Win+Alt+C']);
12
20
 
13
21
  const MOD_ALIASES = { alt: 'alt', option: 'alt', ctrl: 'ctrl', control: 'ctrl', shift: 'shift', win: 'win', windows: 'win', super: 'win', meta: 'win', cmd: 'win' };
14
22
  const NAMED_KEYS = { space: 0x20, enter: 0x0d, tab: 0x09, esc: 0x1b, escape: 0x1b, backspace: 0x08, insert: 0x2d, delete: 0x2e, home: 0x24, end: 0x23, pageup: 0x21, pagedown: 0x22, '`': 0xc0, '/': 0xbf, '.': 0xbe, ',': 0xbc, ';': 0xba };
15
- const MOD_ORDER = ['win', 'ctrl', 'shift', 'alt'];
23
+ const MOD_ORDER = ['win', 'ctrl', 'alt', 'shift'];
16
24
 
17
25
  function keyCode(name) {
18
26
  const k = name.toLowerCase();
@@ -45,13 +53,29 @@ export function parseHotkey(text) {
45
53
  }
46
54
 
47
55
  /** Thứ tự thử: tổ hợp chủ máy đặt (nếu có) rồi mặc định; `off` → không có. */
48
- export function hotkeyCandidates(setting) {
56
+ export function hotkeyCandidates(setting, defaults = DEFAULT_HOTKEYS) {
49
57
  if (setting === 'off' || setting === false) return [];
50
- const list = setting ? [setting, ...DEFAULT_HOTKEYS] : [...DEFAULT_HOTKEYS];
58
+ const list = setting ? [setting, ...defaults] : [...defaults];
51
59
  const seen = new Set();
52
60
  return list.map((h) => { try { return parseHotkey(h); } catch { return null; } }).filter((h) => h && !seen.has(h.label) && seen.add(h.label));
53
61
  }
54
62
 
63
+ /**
64
+ * Danh sách cửa sổ linh vật đăng ký (config.json `hotkeys`, mascot/index.mjs ghi mỗi lần mở): mỗi mục `{ n: nhãn, m: mods | MOD_NOREPEAT,
65
+ * v: vk }`, cửa sổ lấy tổ hợp đầu tiên còn trống của mỗi danh sách. Mỗi danh sách = tổ hợp chủ máy đặt rồi mặc định, không trùng nhau: tổ
66
+ * hợp chủ máy đặt cho chat không nằm trong `voice`, `chat` bỏ mọi tổ hợp đã nằm trong `voice`. `setting` = `off` → cả hai rỗng; `chatSetting`
67
+ * = `off` → chỉ `chat` rỗng.
68
+ */
69
+ export function hostHotkeys(setting, chatSetting) {
70
+ const toHost = (h) => ({ n: h.label, m: h.mods | MOD_NOREPEAT, v: h.vk });
71
+ const chosenChat = chatSetting ? hotkeyCandidates(chatSetting, [])[0]?.label : null;
72
+ const voice = hotkeyCandidates(setting).filter((h) => h.label !== chosenChat);
73
+ if (!voice.length) return { voice: [], chat: [] };
74
+ const taken = new Set(voice.map((h) => h.label));
75
+ const chat = hotkeyCandidates(chatSetting, DEFAULT_CHAT_HOTKEYS).filter((h) => !taken.has(h.label));
76
+ return { voice: voice.map(toHost), chat: chat.map(toHost) };
77
+ }
78
+
55
79
  /**
56
80
  * Tổ hợp có đăng ký được trên máy này ngay lúc này không (đăng ký thử rồi trả lại). Chỉ Windows; nơi khác → null (không biết).
57
81
  * Lúc linh vật đang chạy thì tổ hợp nó đang giữ báo "bị giữ" — chính nó giữ.
@@ -0,0 +1,111 @@
1
+ import { createHash } from 'crypto';
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+ import axios from 'axios';
5
+ import { REMOTE_CONFIG_DIR } from '../constants.mjs';
6
+
7
+ /**
8
+ * Tải ảnh cho ô hỏi của linh vật (03/11/2026). Daemon tải thay cho cửa sổ vì URL ảnh đến từ câu trả lời của AI (có thể lấy từ trang web lạ), còn bộ giải
9
+ * mã ảnh của cửa sổ chạy ngay trên máy chủ máy, không có sandbox như trình duyệt:
10
+ * - chỉ `http(s)`; địa chỉ nội bộ (router, máy trong LAN, localhost) bị từ chối, trừ chính máy chủ Aivin, để nội dung độc hại không gọi được vào mạng của chủ máy;
11
+ * - theo redirect thủ công, mỗi bước kiểm lại địa chỉ;
12
+ * - giới hạn dung lượng + thời gian; định dạng lấy theo byte đầu tệp, không tin Content-Type.
13
+ * Ảnh hỏng / bị từ chối → null, nơi gọi đổi thành nút mở link.
14
+ */
15
+ export const MASCOT_IMAGE_DIR = path.join(REMOTE_CONFIG_DIR, 'mascot-images');
16
+ const MAX_BYTES = 6 * 1024 * 1024;
17
+ const TIMEOUT_MS = 8000;
18
+ const MAX_REDIRECTS = 3;
19
+ const KEEP_FILES = 60;
20
+
21
+ /** Đuôi tệp theo byte đầu; loại khác (SVG, HTML…) không đưa vào bộ giải mã. */
22
+ export function imageExtension(head) {
23
+ if (head.length < 12) return null;
24
+ if (head[0] === 0x89 && head.toString('latin1', 1, 4) === 'PNG') return 'png';
25
+ if (head[0] === 0xff && head[1] === 0xd8 && head[2] === 0xff) return 'jpg';
26
+ if (head.toString('latin1', 0, 4) === 'GIF8') return 'gif';
27
+ if (head.toString('latin1', 0, 4) === 'RIFF' && head.toString('latin1', 8, 12) === 'WEBP') return 'webp';
28
+ if (head.toString('latin1', 0, 2) === 'BM') return 'bmp';
29
+ return null;
30
+ }
31
+
32
+ /** Tên máy / IP nội bộ. Chỉ xét chữ trong URL (không phân giải DNS) — đủ cho link AI viết ra, không chống được DNS rebinding. */
33
+ export function isPrivateHost(host) {
34
+ const h = String(host || '').toLowerCase().replace(/^\[|\]$/g, '');
35
+ if (!h || h === 'localhost' || h.endsWith('.localhost') || h.endsWith('.local') || !h.includes('.') && !h.includes(':')) return true;
36
+ if (h === '::1' || /^(fc|fd|fe[89ab])/.test(h)) return true;
37
+ const ip = h.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/);
38
+ if (!ip) return false;
39
+ const [a, b] = [Number(ip[1]), Number(ip[2])];
40
+ return a === 0 || a === 10 || a === 127 || (a === 169 && b === 254) || (a === 172 && b >= 16 && b <= 31) || (a === 192 && b === 168);
41
+ }
42
+
43
+ function allowedUrl(url, serverHost) {
44
+ try {
45
+ const u = new URL(url);
46
+ if (u.protocol !== 'http:' && u.protocol !== 'https:') return null;
47
+ return u.hostname === serverHost || !isPrivateHost(u.hostname) ? u.href : null;
48
+ } catch {
49
+ return null;
50
+ }
51
+ }
52
+
53
+ async function readLimited(stream, signal) {
54
+ const chunks = [];
55
+ let total = 0;
56
+ for await (const chunk of stream) {
57
+ if (signal.aborted) throw new Error('timeout');
58
+ total += chunk.length;
59
+ if (total > MAX_BYTES) { stream.destroy(); throw new Error('too big'); }
60
+ chunks.push(chunk);
61
+ }
62
+ return Buffer.concat(chunks);
63
+ }
64
+
65
+ async function download(url, serverHost, http, signal) {
66
+ let current = allowedUrl(url, serverHost);
67
+ for (let hop = 0; current && hop <= MAX_REDIRECTS; hop++) {
68
+ const res = await http.get(current, { responseType: 'stream', maxRedirects: 0, validateStatus: (s) => s >= 200 && s < 400, signal, timeout: TIMEOUT_MS });
69
+ if (res.status < 300) return readLimited(res.data, signal);
70
+ res.data?.destroy?.();
71
+ const next = res.headers?.location ? new URL(res.headers.location, current).href : null;
72
+ current = next && allowedUrl(next, serverHost);
73
+ }
74
+ return null;
75
+ }
76
+
77
+ /** Xoá ảnh cũ, giữ `KEEP_FILES` tệp mới nhất. */
78
+ function prune(dir) {
79
+ const files = fs.readdirSync(dir).map((name) => {
80
+ const file = path.join(dir, name);
81
+ return { file, at: fs.statSync(file).mtimeMs };
82
+ }).sort((a, b) => b.at - a.at);
83
+ for (const { file } of files.slice(KEEP_FILES)) fs.rmSync(file, { force: true });
84
+ }
85
+
86
+ /** Ảnh tại `url` → đường dẫn tệp đã kiểm (dùng lại nếu đã tải), không được → null. Không ném lỗi. */
87
+ export async function fetchMascotImage(url, { dir = MASCOT_IMAGE_DIR, serverUrl = '', http = axios } = {}) {
88
+ const name = createHash('sha256').update(String(url)).digest('hex').slice(0, 32);
89
+ const controller = new AbortController();
90
+ const timer = setTimeout(() => controller.abort(), TIMEOUT_MS);
91
+ try {
92
+ fs.mkdirSync(dir, { recursive: true });
93
+ const cached = fs.readdirSync(dir).find((f) => f.startsWith(`${name}.`));
94
+ if (cached) return path.join(dir, cached);
95
+ let serverHost = '';
96
+ try { serverHost = new URL(serverUrl).hostname; } catch { /* không biết máy chủ thì mọi địa chỉ nội bộ đều bị từ chối */ }
97
+ const body = await download(url, serverHost, http, controller.signal);
98
+ const ext = body && imageExtension(body.subarray(0, 12));
99
+ if (!ext) return null;
100
+ const file = path.join(dir, `${name}.${ext}`);
101
+ const tmp = `${file}.tmp${process.pid}`;
102
+ fs.writeFileSync(tmp, body);
103
+ fs.renameSync(tmp, file);
104
+ prune(dir);
105
+ return file;
106
+ } catch {
107
+ return null;
108
+ } finally {
109
+ clearTimeout(timer);
110
+ }
111
+ }