@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
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Gom sự kiện của bộ theo dõi tệp thành lô (02/11/2026). Trước đây chỉ debounce: tệp bị ghi liên tục (log, bản build, đồng bộ đám mây) đẩy
3
+ * hẹn giờ lùi mãi → lô không bao giờ chạy, còn tập `pending` phình không giới hạn trong RAM.
4
+ * - `debounceMs`: chờ yên chừng này rồi chạy;
5
+ * - `maxWaitMs`: kể từ sự kiện đầu của lô, chạy chậm nhất sau chừng này dù sự kiện còn dồn tới;
6
+ * - `maxPending`: quá chừng này đường dẫn khác nhau → bỏ danh sách, chạy `onOverflow` (quét lại tăng dần cả cây) thay vì `onBatch`.
7
+ * `onBatch(paths)` / `onOverflow()` có thể async; lô chạy tuần tự (lô sau chờ lô trước xong).
8
+ */
9
+ export function createWatchBatcher({ debounceMs = 800, maxWaitMs = 5_000, maxPending = 5_000, onBatch, onOverflow, now = Date.now } = {}) {
10
+ const pending = new Set();
11
+ let overflow = false;
12
+ let firstAt = 0;
13
+ let timer = null;
14
+ let chain = Promise.resolve();
15
+
16
+ const run = () => {
17
+ timer = null;
18
+ firstAt = 0;
19
+ const batch = [...pending];
20
+ const full = overflow;
21
+ pending.clear();
22
+ overflow = false;
23
+ chain = chain.then(() => (full ? onOverflow?.() : batch.length ? onBatch?.(batch) : undefined)).catch(() => undefined);
24
+ return chain;
25
+ };
26
+
27
+ return {
28
+ add(abs) {
29
+ if (!overflow) {
30
+ pending.add(abs);
31
+ if (pending.size > maxPending) {
32
+ pending.clear();
33
+ overflow = true;
34
+ }
35
+ }
36
+ const t = now();
37
+ if (!firstAt) firstAt = t;
38
+ if (timer) clearTimeout(timer);
39
+ const wait = Math.max(0, Math.min(debounceMs, firstAt + maxWaitMs - t));
40
+ timer = setTimeout(run, wait);
41
+ timer.unref?.();
42
+ },
43
+ /** Chạy ngay lô đang chờ (test / đóng). */
44
+ flushNow() {
45
+ if (timer) clearTimeout(timer);
46
+ return run();
47
+ },
48
+ size: () => pending.size,
49
+ overflowed: () => overflow,
50
+ close() {
51
+ if (timer) clearTimeout(timer);
52
+ timer = null;
53
+ pending.clear();
54
+ },
55
+ };
56
+ }
@@ -1,26 +1,26 @@
1
- /**
2
- * Điều khiển máy từ xa (Device Agent phía máy) — các mô-đun trong ./remote/:
3
- * - client.mjs : daemon `aivin remote start` — nối namespace /device bằng token thiết bị, nhận lô lời gọi, trả kết quả/luồng.
4
- * - credentials.mjs : mã máy, ghép máy (token thiết bị riêng), danh sách / thu hồi máy.
5
- * - deviceProfile.mjs : tự phát hiện năng lực máy (chương trình trên PATH, dự án) + danh mục công cụ có dấu vân tay.
6
- * - toolHandlers.mjs : các gói công cụ (fs, shell, system, git, memory).
7
- * - scheduler.mjs : song song theo nhóm công cụ, khoá theo đường dẫn khi ghi, huỷ / hết giờ.
8
- * - sandbox.mjs : sàn cứng — thư mục gốc (realpath), lọc biến môi trường, chặn lệnh thảm hoạ, công tắc ngắt.
9
- * - deviceMemory.mjs : bộ nhớ trên máy (YAML người đọc/sửa được + vector), tìm kết hợp BM25 + cosine.
10
- * - fileIndexer.mjs : chỉ mục tệp trong RAM, quét tăng dần + theo dõi thay đổi.
11
- * - mission.mjs : `aivin remote do` — giao thẳng việc cho Device Agent.
12
- * - activityLog.mjs / autoStart.mjs : nhật ký hoạt động, tự khởi động cùng máy.
13
- */
14
-
15
- export * from './remote/deviceProfile.mjs';
16
- export * from './remote/toolHandlers.mjs';
17
- export * from './remote/deviceMemory.mjs';
18
- export * from './remote/activityLog.mjs';
19
- export * from './remote/autoStart.mjs';
20
- export * from './remote/client.mjs';
21
- export * from './remote/fileIndexer.mjs';
22
- export * from './remote/credentials.mjs';
23
- export * from './remote/scheduler.mjs';
24
- export * from './remote/sandbox.mjs';
25
- export * from './remote/mission.mjs';
26
- export { DEVICE_AGENT_PLUGIN_ID, DeviceEvent, DeviceErrorCode, MemoryKind } from './remote/constants.mjs';
1
+ /**
2
+ * Điều khiển máy từ xa (Device Agent phía máy) — các mô-đun trong ./remote/:
3
+ * - client.mjs : daemon `aivin remote start` — nối namespace /device bằng token thiết bị, nhận lô lời gọi, trả kết quả/luồng.
4
+ * - credentials.mjs : mã máy, ghép máy (token thiết bị riêng), danh sách / thu hồi máy.
5
+ * - deviceProfile.mjs : tự phát hiện năng lực máy (chương trình trên PATH, dự án) + danh mục công cụ có dấu vân tay.
6
+ * - toolHandlers.mjs : các gói công cụ (fs, shell, system, git, memory).
7
+ * - scheduler.mjs : song song theo nhóm công cụ, khoá theo đường dẫn khi ghi, huỷ / hết giờ.
8
+ * - sandbox.mjs : sàn cứng — thư mục gốc (realpath), lọc biến môi trường, chặn lệnh thảm hoạ, công tắc ngắt.
9
+ * - deviceMemory.mjs : bộ nhớ trên máy (YAML người đọc/sửa được + vector), tìm kết hợp BM25 + cosine.
10
+ * - fileIndexer.mjs : chỉ mục tệp (SQLite trên đĩa, FTS5 + vector), quét tăng dần + theo dõi thay đổi.
11
+ * - mission.mjs : `aivin remote do` — giao thẳng việc cho Device Agent.
12
+ * - activityLog.mjs / autoStart.mjs : nhật ký hoạt động, tự khởi động cùng máy.
13
+ */
14
+
15
+ export * from './remote/deviceProfile.mjs';
16
+ export * from './remote/toolHandlers.mjs';
17
+ export * from './remote/deviceMemory.mjs';
18
+ export * from './remote/activityLog.mjs';
19
+ export * from './remote/autoStart.mjs';
20
+ export * from './remote/client.mjs';
21
+ export * from './remote/fileIndexer.mjs';
22
+ export * from './remote/credentials.mjs';
23
+ export * from './remote/scheduler.mjs';
24
+ export * from './remote/sandbox.mjs';
25
+ export * from './remote/mission.mjs';
26
+ export { DEVICE_AGENT_PLUGIN_ID, DeviceEvent, DeviceErrorCode, MemoryKind } from './remote/constants.mjs';
@@ -7,7 +7,7 @@ import {
7
7
  } from './update/constants.mjs';
8
8
  import { replaceBinary } from './update/binarySwap.mjs';
9
9
  import { downloadRelease, downloadRoot, fetchLatestVersion, fetchRollout, verifyStarts } from './update/download.mjs';
10
- import { loadUpdateSettings, recordInstalled, withUpdateLock } from './update/state.mjs';
10
+ import { diskAheadOfRunning, loadUpdateSettings, recordInstalled, withUpdateLock } from './update/state.mjs';
11
11
  import { compareVersions, currentCliVersion, isStandaloneBinary, parseVersion, releaseAsset, rolloutBucket, tagOf, versionList } from './update/versions.mjs';
12
12
 
13
13
  /**
@@ -44,6 +44,12 @@ export async function installUpdate({
44
44
  const result = { current: tagOf(current), latest };
45
45
  if (!force && compareVersions(latest, current) <= 0) return { ...result, status: UpdateStatus.UP_TO_DATE };
46
46
  if (!binary) return { ...result, status: UpdateStatus.NOT_BINARY };
47
+ // Đĩa đã có đúng bản này (hoặc mới hơn) chờ khởi động lại → không tải / cài lại (02/11/2026: cài lại từng đè `.prev` bằng bản chưa chạy).
48
+ const alreadyOnDisk = () => {
49
+ const onDisk = diskAheadOfRunning({ target, current, file: markerFile });
50
+ return !force && onDisk && compareVersions(onDisk, latest) >= 0 ? { ...result, status: UpdateStatus.INSTALLED, already: true } : null;
51
+ };
52
+ if (alreadyOnDisk()) return alreadyOnDisk();
47
53
  if (!version && loadUpdateSettings(settingsFile).skip.includes(latest)) return { ...result, status: UpdateStatus.SKIPPED };
48
54
  if (!version && versionList(holdVersions).includes(latest)) return { ...result, status: UpdateStatus.HELD };
49
55
  if (check) return { ...result, status: UpdateStatus.AVAILABLE };
@@ -52,13 +58,17 @@ export async function installUpdate({
52
58
  if (rollout && rollout.version === latest && rolloutBucket(deviceId, latest) >= rollout.percent) return { ...result, status: UpdateStatus.DEFERRED };
53
59
  }
54
60
  return withUpdateLock(async () => {
61
+ // Cửa sổ khác vừa cài xong trong lúc chờ khoá.
62
+ const done = alreadyOnDisk();
63
+ if (done) return done;
55
64
  const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'aivin-update-'));
56
65
  try {
57
66
  const file = await downloadRelease({ root, version: latest, asset: releaseAsset(), destDir: tmp, signal, keys });
58
67
  await verify(file, latest);
59
68
  let backup;
60
69
  try {
61
- backup = replaceBinary({ newFile: file, target });
70
+ // Tệp trên đĩa là bản chưa từng chạy → `.prev` (bản đang chạy) giữ nguyên làm chỗ tự lùi.
71
+ backup = replaceBinary({ newFile: file, target, keepBackup: !!diskAheadOfRunning({ target, current, file: markerFile }) });
62
72
  } catch (e) {
63
73
  throw new Error(`Không thay được ${target} (${e?.code || e?.message}) — cài lại bằng bộ cài: https://get.aivin.vn`, { cause: e });
64
74
  }
@@ -16,8 +16,22 @@ export function retire(file) {
16
16
  * Thay tệp chạy, giữ bản cũ ở `<tệp>.prev`. Windows: đổi tên bản đang chạy thành `.prev` rồi chép bản mới (chép hỏng thì trả tên cũ lại).
17
17
  * macOS / Linux: chép bản cũ ra `.prev`, chép bản mới cạnh bên rồi đổi tên nguyên khối. Trả đường dẫn bản cũ (null nếu chưa có).
18
18
  */
19
- export function replaceBinary({ newFile, target, platform = process.platform }) {
19
+ export function replaceBinary({ newFile, target, platform = process.platform, keepBackup = false }) {
20
20
  const backup = `${target}.prev`;
21
+ // Tệp trên đĩa là bản mới CHƯA từng chạy (đã cài, chờ khởi động lại) → `.prev` đang là bản đang chạy / đã chạy ổn: giữ nguyên, chỉ thay
22
+ // tệp chạy (02/11/2026 — trước đây cài lần hai đè `.prev` bằng bản chưa kiểm, Windows còn dời chính tệp đang chạy vào `.trash`).
23
+ if (keepBackup && fs.existsSync(backup)) {
24
+ const staged = `${target}.new`;
25
+ fs.copyFileSync(newFile, staged);
26
+ if (platform !== 'win32') fs.chmodSync(staged, 0o755);
27
+ try {
28
+ fs.renameSync(staged, target);
29
+ } catch (e) {
30
+ fs.rmSync(staged, { force: true });
31
+ throw e;
32
+ }
33
+ return backup;
34
+ }
21
35
  if (fs.existsSync(backup)) retire(backup);
22
36
  const had = fs.existsSync(target);
23
37
  if (platform === 'win32') {
@@ -81,6 +81,16 @@ export function installedNewerThanRunning({ target = process.execPath, current =
81
81
  return loadUpdateSettings(settingsFile).skip.includes(tagOf(marker.version)) ? null : tagOf(marker.version);
82
82
  }
83
83
 
84
+ /**
85
+ * Đĩa đang giữ một bản ĐÃ CÀI mà chưa chạy (mới hơn bản đang chạy, đúng tệp chạy này) — kể cả bản nằm trong danh sách bỏ qua. Trả phiên
86
+ * bản hoặc null. Dùng để không cài chồng / không đè `.prev` (02/11/2026).
87
+ */
88
+ export function diskAheadOfRunning({ target = process.execPath, current = currentCliVersion(), file = UPDATE_INSTALLED_FILE } = {}) {
89
+ const marker = readInstalledMarker(file);
90
+ if (!marker || !samePath(marker.path, target) || compareVersions(marker.version, current) <= 0) return null;
91
+ return tagOf(marker.version);
92
+ }
93
+
84
94
  /** Tiến trình vừa khởi động chính là tệp đang nằm trên đĩa → dấu "đã cài bản mới" cho tệp này đã cũ (cài lại bằng bộ cài, đã lùi bản…). */
85
95
  export function clearStaleInstalledMarker({ target = process.execPath, file = UPDATE_INSTALLED_FILE } = {}) {
86
96
  const marker = readInstalledMarker(file);
@@ -1,7 +1,21 @@
1
1
  # Aivin mascot
2
2
 
3
+ <p align="center">
4
+ <img src="media/aivin-mascot-live.gif" alt="The current live Aivin mascot showing chat, file drag and attached-file states" width="480" />
5
+ </p>
6
+
3
7
  The Aivin mascot is the floating Windows interface for Aivin Remote. It shows what the agent is doing, accepts typed or spoken questions, and gives access to recent activity without opening the web app.
4
8
 
9
+ The animation above is rendered from the current WPF visual tree. It demonstrates the real chat box, the file-drag target and the tray badge after two files have been attached.
10
+
11
+ ## How it connects to Aivin
12
+
13
+ ![Aivin Remote architecture: the platform routes work to listeners on selected computers, which enforce local policy and return results](media/aivin-remote-architecture.svg)
14
+
15
+ The mascot is a local interface to the Aivin listener running on the same computer. It sends typed questions, voice events and user choices to that listener; the listener maintains the authenticated outbound connection to Aivin Platform. Work coming back from the platform passes through local policy and approval checks before any tool or app is used. Status, questions and results then travel back over the same connection.
16
+
17
+ This design supports multiple paired computers without exposing an inbound remote-control port. A chat or mission targets a selected computer, and each computer keeps its own identity, capabilities, policy and local execution boundary.
18
+
5
19
  ## Requirements
6
20
 
7
21
  - Windows.