@aivin-labs/cli 1.0.5 → 1.0.11

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 (93) hide show
  1. package/README.md +288 -216
  2. package/bin/assets/aivin-mascot.png +0 -0
  3. package/bin/cli.mjs +551 -5
  4. package/bin/lib/browserAutomation.mjs +384 -614
  5. package/bin/lib/browserTui.mjs +276 -251
  6. package/bin/lib/cancellation.mjs +61 -0
  7. package/bin/lib/localBrowser.mjs +345 -0
  8. package/bin/lib/mcpProxy.mjs +869 -837
  9. package/bin/lib/missionStream.mjs +696 -691
  10. package/bin/lib/releaseKeys.mjs +9 -0
  11. package/bin/lib/remote/activityLog.mjs +68 -0
  12. package/bin/lib/remote/autoStart.mjs +617 -0
  13. package/bin/lib/remote/autoUpdate.mjs +129 -0
  14. package/bin/lib/remote/brand.mjs +60 -0
  15. package/bin/lib/remote/browserTunnel.mjs +403 -0
  16. package/bin/lib/remote/callLedger.mjs +134 -0
  17. package/bin/lib/remote/cdpPolicy.mjs +79 -0
  18. package/bin/lib/remote/client.mjs +808 -0
  19. package/bin/lib/remote/clipboard.mjs +138 -0
  20. package/bin/lib/remote/codingAgents.mjs +293 -0
  21. package/bin/lib/remote/constants.mjs +234 -0
  22. package/bin/lib/remote/credentials.mjs +208 -0
  23. package/bin/lib/remote/daemonLock.mjs +86 -0
  24. package/bin/lib/remote/dailyBrowser.mjs +371 -0
  25. package/bin/lib/remote/desktop/index.mjs +272 -0
  26. package/bin/lib/remote/desktop/linux.mjs +144 -0
  27. package/bin/lib/remote/desktop/linuxTraySource.mjs +427 -0
  28. package/bin/lib/remote/desktop/mac.mjs +187 -0
  29. package/bin/lib/remote/desktop/macTraySource.mjs +292 -0
  30. package/bin/lib/remote/desktop/strings.mjs +74 -0
  31. package/bin/lib/remote/desktop/traySource.mjs +527 -0
  32. package/bin/lib/remote/deviceMemory.mjs +246 -0
  33. package/bin/lib/remote/deviceProfile.mjs +142 -0
  34. package/bin/lib/remote/deviceWatches.mjs +138 -0
  35. package/bin/lib/remote/docText.mjs +117 -0
  36. package/bin/lib/remote/fileIndexer.mjs +399 -0
  37. package/bin/lib/remote/gui/index.mjs +326 -0
  38. package/bin/lib/remote/gui/officeHostSource.mjs +393 -0
  39. package/bin/lib/remote/gui/windowsHost.mjs +109 -0
  40. package/bin/lib/remote/gui/windowsHostSource.mjs +1156 -0
  41. package/bin/lib/remote/httpTransfer.mjs +89 -0
  42. package/bin/lib/remote/idempotency.mjs +135 -0
  43. package/bin/lib/remote/keepAwake.mjs +100 -0
  44. package/bin/lib/remote/localConfirm.mjs +268 -0
  45. package/bin/lib/remote/localPolicy.mjs +256 -0
  46. package/bin/lib/remote/mascot/adpcm.mjs +70 -0
  47. package/bin/lib/remote/mascot/ask.mjs +768 -0
  48. package/bin/lib/remote/mascot/askHostSource.mjs +512 -0
  49. package/bin/lib/remote/mascot/assets.mjs +827 -0
  50. package/bin/lib/remote/mascot/bot.mjs +372 -0
  51. package/bin/lib/remote/mascot/hostSource.mjs +759 -0
  52. package/bin/lib/remote/mascot/hotkey.mjs +66 -0
  53. package/bin/lib/remote/mascot/index.mjs +493 -0
  54. package/bin/lib/remote/mascot/labels.mjs +82 -0
  55. package/bin/lib/remote/mascot/rigSource.mjs +609 -0
  56. package/bin/lib/remote/mascot/strings.mjs +375 -0
  57. package/bin/lib/remote/mascot/ui.mjs +104 -0
  58. package/bin/lib/remote/mascot/voiceSource.mjs +584 -0
  59. package/bin/lib/remote/meeting/index.mjs +415 -0
  60. package/bin/lib/remote/meeting/recorder.mjs +109 -0
  61. package/bin/lib/remote/meeting/recorderSource.mjs +394 -0
  62. package/bin/lib/remote/meeting/transcribe.mjs +259 -0
  63. package/bin/lib/remote/memberFsPool.mjs +134 -0
  64. package/bin/lib/remote/memberFsWorker.mjs +60 -0
  65. package/bin/lib/remote/mission.mjs +156 -0
  66. package/bin/lib/remote/missionQuestion.mjs +63 -0
  67. package/bin/lib/remote/multiUser.mjs +344 -0
  68. package/bin/lib/remote/netProxy.mjs +145 -0
  69. package/bin/lib/remote/notify.mjs +58 -0
  70. package/bin/lib/remote/office/index.mjs +90 -0
  71. package/bin/lib/remote/policyCommand.mjs +93 -0
  72. package/bin/lib/remote/processes.mjs +217 -0
  73. package/bin/lib/remote/sandbox.mjs +221 -0
  74. package/bin/lib/remote/schedule.mjs +213 -0
  75. package/bin/lib/remote/scheduler.mjs +124 -0
  76. package/bin/lib/remote/tabGroups.mjs +209 -0
  77. package/bin/lib/remote/textIndex.mjs +131 -0
  78. package/bin/lib/remote/tokenStore.mjs +77 -0
  79. package/bin/lib/remote/toolHandlers.mjs +741 -0
  80. package/bin/lib/remote/undoStore.mjs +242 -0
  81. package/bin/lib/remoteControl.mjs +26 -0
  82. package/bin/lib/scaffold.mjs +654 -653
  83. package/bin/lib/selfUpdate.mjs +71 -0
  84. package/bin/lib/update/binarySwap.mjs +63 -0
  85. package/bin/lib/update/constants.mjs +56 -0
  86. package/bin/lib/update/download.mjs +145 -0
  87. package/bin/lib/update/signature.mjs +42 -0
  88. package/bin/lib/update/state.mjs +205 -0
  89. package/bin/lib/update/versions.mjs +60 -0
  90. package/docs/AIVIN_MASCOT.md +116 -0
  91. package/docs/CLI.md +1484 -1299
  92. package/docs/media/aivin-mascot-states.mp4 +0 -0
  93. package/package.json +71 -62
@@ -0,0 +1,134 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { REMOTE_OUTBOX_FILE } from './constants.mjs';
4
+
5
+ /** Giữ kết quả chưa gửi bấy nhiêu lâu — BE chờ một lời gọi tối đa 10 phút (+ thời gian ân hạn nối lại). */
6
+ const UNSENT_KEEP_MS = 15 * 60_000;
7
+ /** Kết quả đã gửi vẫn nhớ thêm chừng này (BE gửi lại đúng `call_id` vì ACK / kết quả lỡ mất → trả lại kết quả cũ, không chạy lần hai). */
8
+ const SENT_KEEP_MS = 3 * 60_000;
9
+ const MAX_UNSENT = 200;
10
+ /** Kết quả lớn hơn không ghi xuống đĩa (chỉ giữ trong bộ nhớ) — daemon chết thì BE nhận lỗi thay vì nội dung. */
11
+ const PERSIST_RESULT_BYTES = 4 * 1024 * 1024;
12
+
13
+ export const CallState = Object.freeze({ RUNNING: 'running', DONE: 'done' });
14
+
15
+ function payloadBytes(payload) {
16
+ try { return Buffer.byteLength(JSON.stringify(payload)); } catch { return Infinity; }
17
+ }
18
+
19
+ /**
20
+ * Sổ lời gọi của daemon (29/10/2026). Theo dõi từng `call_id` BE gửi xuống: đang chạy → xong (kết quả chờ gửi) → đã gửi.
21
+ * - Lời gọi trùng `call_id` (BE gửi lại sau khi máy rớt mạng) không chạy lần hai: đang chạy thì bỏ qua, đã xong thì gửi lại kết quả cũ.
22
+ * - Kết quả chưa gửi được (mất mạng, chưa có ack) ghi xuống `REMOTE_OUTBOX_FILE` — daemon khởi động lại vẫn gửi.
23
+ * - `heldIds()`: lời gọi máy đang giữ, gửi lên khi chào để BE đối chiếu (huỷ việc không còn ai chờ, khỏi gửi lại việc máy đang làm).
24
+ */
25
+ export function createCallLedger({ file = REMOTE_OUTBOX_FILE, now = () => Date.now() } = {}) {
26
+ const calls = new Map();
27
+
28
+ function persist() {
29
+ const unsent = [...calls.entries()].filter(([, e]) => e.state === CallState.DONE && !e.sent);
30
+ try {
31
+ if (!unsent.length) {
32
+ fs.rmSync(file, { force: true });
33
+ return;
34
+ }
35
+ const rows = unsent.map(([id, e]) => {
36
+ const payload = payloadBytes(e.payload) <= PERSIST_RESULT_BYTES ? e.payload
37
+ : { call_id: id, ok: false, error: 'Result was too large to keep while offline — run the step again', error_code: 'OUTPUT_TOO_LARGE', duration_ms: e.payload.duration_ms };
38
+ return { id, at: e.at, payload };
39
+ });
40
+ fs.mkdirSync(path.dirname(file), { recursive: true });
41
+ const tmp = `${file}.tmp`;
42
+ fs.writeFileSync(tmp, JSON.stringify(rows), { mode: 0o600 });
43
+ fs.renameSync(tmp, file);
44
+ } catch { /* không ghi được: vẫn giữ trong bộ nhớ */ }
45
+ }
46
+
47
+ function load() {
48
+ let rows;
49
+ try { rows = JSON.parse(fs.readFileSync(file, 'utf8')); } catch { return; }
50
+ const t = now();
51
+ for (const r of Array.isArray(rows) ? rows : []) {
52
+ if (typeof r?.id !== 'string' || !r.payload || t - Number(r.at) > UNSENT_KEEP_MS) continue;
53
+ calls.set(r.id, { state: CallState.DONE, at: Number(r.at), payload: r.payload, sent: false, onDisk: true });
54
+ }
55
+ }
56
+
57
+ function prune() {
58
+ const t = now();
59
+ let changed = false;
60
+ for (const [id, e] of calls) {
61
+ if (e.state !== CallState.DONE) continue;
62
+ const age = t - e.at;
63
+ if ((e.sent && age > SENT_KEEP_MS) || (!e.sent && age > UNSENT_KEEP_MS)) {
64
+ calls.delete(id);
65
+ changed ||= !e.sent;
66
+ }
67
+ }
68
+ const unsent = [...calls.entries()].filter(([, e]) => e.state === CallState.DONE && !e.sent);
69
+ for (const [id] of unsent.slice(0, Math.max(0, unsent.length - MAX_UNSENT))) {
70
+ calls.delete(id);
71
+ changed = true;
72
+ }
73
+ if (changed) persist();
74
+ }
75
+
76
+ load();
77
+
78
+ return {
79
+ /** Nhận một lời gọi: mới → ghi "đang chạy", trả `null`; trùng → trả mục đã có (`state`, `payload`). */
80
+ admit(call) {
81
+ const existing = calls.get(call.call_id);
82
+ if (existing) return existing;
83
+ calls.set(call.call_id, { state: CallState.RUNNING, at: now() });
84
+ return null;
85
+ },
86
+ /** Lời gọi xong — kết quả chờ gửi. `persistNow`: ghi xuống đĩa ngay (đang mất kết nối). */
87
+ finish(callId, payload, { persistNow = false } = {}) {
88
+ calls.set(callId, { state: CallState.DONE, at: now(), payload, sent: false });
89
+ if (persistNow) persist();
90
+ },
91
+ markSent(callId) {
92
+ const e = calls.get(callId);
93
+ if (!e || e.state !== CallState.DONE || e.sent) return;
94
+ const wasOnDisk = e.onDisk;
95
+ e.sent = true;
96
+ e.payload = { call_id: callId, ...pick(e.payload) };
97
+ if (wasOnDisk) persist();
98
+ },
99
+ /** Lời gọi đã xong bị BE gửi lại: đánh dấu chưa gửi, trả kết quả để gửi lần nữa. */
100
+ reopen(callId) {
101
+ const e = calls.get(callId);
102
+ if (e?.state !== CallState.DONE) return null;
103
+ e.sent = false;
104
+ return e.payload;
105
+ },
106
+ /** Ghi các kết quả chưa gửi xuống đĩa (gửi chưa được xác nhận / daemon sắp dừng). */
107
+ persist() {
108
+ for (const e of calls.values()) if (e.state === CallState.DONE && !e.sent) e.onDisk = true;
109
+ persist();
110
+ },
111
+ /** BE báo không còn ai chờ → bỏ kết quả chưa gửi của các lời gọi này. */
112
+ drop(callIds) {
113
+ let changed = false;
114
+ for (const id of callIds || []) {
115
+ const e = calls.get(id);
116
+ if (e?.state === CallState.DONE && !e.sent) {
117
+ calls.delete(id);
118
+ changed = true;
119
+ }
120
+ }
121
+ if (changed) persist();
122
+ },
123
+ unsent: () => [...calls.values()].filter((e) => e.state === CallState.DONE && !e.sent).map((e) => e.payload),
124
+ heldIds: () => [...calls.entries()].filter(([, e]) => e.state === CallState.RUNNING || (e.state === CallState.DONE && !e.sent)).map(([id]) => id),
125
+ runningIds: () => [...calls.entries()].filter(([, e]) => e.state === CallState.RUNNING).map(([id]) => id),
126
+ prune,
127
+ };
128
+ }
129
+
130
+ /** Kết quả đã gửi: chỉ giữ phần đủ để gửi lại cho lời gọi trùng (không giữ nội dung lớn quá lâu). */
131
+ function pick(payload) {
132
+ if (payloadBytes(payload) <= 256 * 1024) return payload;
133
+ return { ok: payload.ok, error: payload.error, error_code: payload.error_code, duration_ms: payload.duration_ms, result: payload.ok ? { note: 'Result already delivered' } : undefined };
134
+ }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Bộ lọc lệnh CDP máy chủ gửi xuống trình duyệt của máy (28/10/2026) — áp cho MỌI đường hầm (cửa sổ riêng của Aivin lẫn trình duyệt hằng ngày).
3
+ *
4
+ * Đường hầm trao cho máy chủ quyền điều khiển trình duyệt; không lọc thì máy chủ (hoặc prompt injection đẩy AI tới đó) vượt được giới hạn thư mục
5
+ * của Device Agent: mở `file://` đọc mọi tệp kể cả token máy trong `~/.aivin`, đặt thư mục tải về chỗ tuỳ ý (ghi tệp bất cứ đâu), chọn tệp trên đĩa
6
+ * đưa lên trang web, mở kênh CDP không qua bộ chia (`exposeDevToolsProtocol`, phiên không phẳng). Hàm thuần: nhận lệnh, trả lệnh (có thể đã sửa)
7
+ * hoặc lý do chặn.
8
+ */
9
+
10
+ /** Scheme không bao giờ mở từ xa: tệp trên đĩa, trang nội bộ / cài đặt / tiện ích của trình duyệt. `about:blank` vẫn được. */
11
+ const BLOCKED_URL_RE = /^\s*(file|chrome|edge|brave|opera|vivaldi|devtools|view-source|chrome-extension|edge-extension|chrome-untrusted|filesystem|javascript):/i;
12
+ const BLOCKED_ABOUT_RE = /^\s*about:(?!blank\b|srcdoc\b)/i;
13
+
14
+ /** Lệnh mở kênh CDP / đọc tài nguyên ngoài tầm lọc — không bao giờ cho qua. */
15
+ const ALWAYS_BLOCKED = new Set([
16
+ 'Target.exposeDevToolsProtocol', 'Target.sendMessageToTarget', 'Target.attachToBrowserTarget', 'Network.loadNetworkResource',
17
+ 'DOM.setFileInputFiles', 'Page.setInterceptFileChooserDialog', 'Browser.close', 'Browser.crash', 'Browser.crashGpuProcess',
18
+ ]);
19
+
20
+ /**
21
+ * Phiên gắn vào đích của tiện ích (`chrome-extension://…` — service worker / trang nền, gồm tiện ích gom nhóm tab của Aivin có quyền `debugger`):
22
+ * Puppeteer tự gắn mọi đích nên chỉ cho khởi động lại đích đang chờ và gỡ phiên — không chạy script trong tiện ích (29/10/2026).
23
+ */
24
+ export const EXTENSION_SESSION_ALLOWED = new Set([
25
+ 'Runtime.runIfWaitingForDebugger', 'Target.setAutoAttach', 'Target.detachFromTarget', 'Runtime.releaseObject', 'Runtime.releaseObjectGroup',
26
+ ]);
27
+
28
+ export function isExtensionUrl(url) {
29
+ return /^\s*(chrome|edge)-extension:/i.test(String(url ?? ''));
30
+ }
31
+
32
+ export function isBlockedUrl(url) {
33
+ const u = String(url ?? '');
34
+ return BLOCKED_URL_RE.test(u) || BLOCKED_ABOUT_RE.test(u);
35
+ }
36
+
37
+ /**
38
+ * `{ msg }` — lệnh được gửi (có thể đã sửa: thư mục tải ép về thư mục tải của Aivin); `{ error }` — lý do chặn. `downloadsDir` là thư mục tải
39
+ * của Aivin trên máy (`~/Downloads/Aivin`).
40
+ */
41
+ export function applyDevicePolicy(msg, { downloadsDir }) {
42
+ const method = String(msg?.method || '');
43
+ const params = msg?.params || {};
44
+ // `Extensions.*`: nạp / gỡ tiện ích, đọc kho của tiện ích — cửa sổ Aivin mở với `--enable-unsafe-extension-debugging` (để CLI nạp tiện ích gom nhóm tab).
45
+ if (ALWAYS_BLOCKED.has(method) || method.startsWith('Extensions.')) return { error: `${method} is not allowed on this computer` };
46
+ if ((method === 'Target.setAutoAttach' || method === 'Target.attachToTarget') && params.flatten === false) {
47
+ return { error: 'Only flattened sessions are allowed' };
48
+ }
49
+ if ((method === 'Target.createTarget' || method === 'Page.navigate') && isBlockedUrl(params.url)) {
50
+ return { error: `Opening ${String(params.url).split(':')[0]}: URLs is not allowed on this computer` };
51
+ }
52
+ if ((method === 'Page.setDownloadBehavior' || method === 'Browser.setDownloadBehavior') && params.downloadPath !== undefined) {
53
+ return { msg: { ...msg, params: { ...params, downloadPath: downloadsDir } } };
54
+ }
55
+ return { msg };
56
+ }
57
+
58
+ /** Tệp chạy được / cài đặt — AI tải trên máy người dùng thì huỷ ngay (máy chủ vốn không nhận chúng, nhưng tệp đã nằm lại trên đĩa). */
59
+ const EXECUTABLE_RE = /\.(exe|msi|msix|msixbundle|appx|bat|cmd|com|scr|pif|ps1|psm1|vbs|vbe|js|jse|wsf|wsh|hta|cpl|dll|lnk|reg|jar|dmg|pkg|app|command|sh|run|deb|rpm|apk|xpi|crx)$/i;
60
+
61
+ export function isExecutableName(name) {
62
+ return EXECUTABLE_RE.test(String(name || '').trim());
63
+ }
64
+
65
+ /**
66
+ * Sự kiện trình duyệt gửi lên: bắt đầu tải một tệp chạy được → `guid` của lượt tải cần huỷ, không thì null. So chuỗi trước khi parse (mọi khung
67
+ * CDP đi qua đây).
68
+ */
69
+ export function executableDownloadGuid(frame) {
70
+ const text = String(frame);
71
+ if (!text.includes('downloadWillBegin')) return null;
72
+ try {
73
+ const msg = JSON.parse(text);
74
+ if (msg.method !== 'Page.downloadWillBegin' && msg.method !== 'Browser.downloadWillBegin') return null;
75
+ return isExecutableName(msg.params?.suggestedFilename) ? msg.params.guid : null;
76
+ } catch {
77
+ return null;
78
+ }
79
+ }