@aivin-labs/cli 1.0.8 → 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 (63) hide show
  1. package/README.md +41 -0
  2. package/bin/assets/aivin-mascot.png +0 -0
  3. package/bin/cli.mjs +1979 -1780
  4. package/bin/lib/browserAutomation.mjs +232 -469
  5. package/bin/lib/localBrowser.mjs +345 -0
  6. package/bin/lib/remote/activityLog.mjs +1 -1
  7. package/bin/lib/remote/autoStart.mjs +174 -14
  8. package/bin/lib/remote/browserTunnel.mjs +187 -37
  9. package/bin/lib/remote/callLedger.mjs +74 -15
  10. package/bin/lib/remote/cdpPolicy.mjs +14 -1
  11. package/bin/lib/remote/client.mjs +174 -11
  12. package/bin/lib/remote/clipboard.mjs +138 -0
  13. package/bin/lib/remote/cloudFiles.mjs +53 -0
  14. package/bin/lib/remote/codingAgents.mjs +293 -0
  15. package/bin/lib/remote/constants.mjs +78 -0
  16. package/bin/lib/remote/dailyBrowser.mjs +9 -3
  17. package/bin/lib/remote/desktop/desktopCast.mjs +198 -0
  18. package/bin/lib/remote/desktop/desktopCastSource.mjs +514 -0
  19. package/bin/lib/remote/desktop/desktopWebRtc.mjs +95 -0
  20. package/bin/lib/remote/desktop/index.mjs +309 -0
  21. package/bin/lib/remote/desktop/linux.mjs +144 -0
  22. package/bin/lib/remote/desktop/linuxTraySource.mjs +434 -0
  23. package/bin/lib/remote/desktop/mac.mjs +187 -0
  24. package/bin/lib/remote/desktop/macTraySource.mjs +295 -0
  25. package/bin/lib/remote/desktop/strings.mjs +74 -0
  26. package/bin/lib/remote/desktop/traySource.mjs +541 -0
  27. package/bin/lib/remote/deviceProfile.mjs +1 -0
  28. package/bin/lib/remote/docText.mjs +86 -1
  29. package/bin/lib/remote/fileIndexer.mjs +757 -399
  30. package/bin/lib/remote/folderRules.mjs +140 -0
  31. package/bin/lib/remote/gui/index.mjs +208 -18
  32. package/bin/lib/remote/gui/windowsHost.mjs +9 -2
  33. package/bin/lib/remote/gui/windowsHostSource.mjs +444 -49
  34. package/bin/lib/remote/idempotency.mjs +50 -21
  35. package/bin/lib/remote/localPolicy.mjs +17 -4
  36. package/bin/lib/remote/mascot/adpcm.mjs +70 -0
  37. package/bin/lib/remote/mascot/ask.mjs +768 -0
  38. package/bin/lib/remote/mascot/askHostSource.mjs +530 -0
  39. package/bin/lib/remote/mascot/assets.mjs +825 -216
  40. package/bin/lib/remote/mascot/bot.mjs +397 -0
  41. package/bin/lib/remote/mascot/hostSource.mjs +500 -103
  42. package/bin/lib/remote/mascot/hotkey.mjs +66 -0
  43. package/bin/lib/remote/mascot/index.mjs +199 -28
  44. package/bin/lib/remote/mascot/labels.mjs +2 -3
  45. package/bin/lib/remote/mascot/rigSource.mjs +614 -0
  46. package/bin/lib/remote/mascot/strings.mjs +204 -18
  47. package/bin/lib/remote/mascot/ui.mjs +104 -0
  48. package/bin/lib/remote/mascot/voiceSource.mjs +584 -0
  49. package/bin/lib/remote/meeting/index.mjs +415 -0
  50. package/bin/lib/remote/meeting/recorder.mjs +109 -0
  51. package/bin/lib/remote/meeting/recorderSource.mjs +394 -0
  52. package/bin/lib/remote/meeting/transcribe.mjs +259 -0
  53. package/bin/lib/remote/osSearch.mjs +107 -0
  54. package/bin/lib/remote/sqliteDb.mjs +51 -0
  55. package/bin/lib/remote/tabGroups.mjs +209 -0
  56. package/bin/lib/remote/toolHandlers.mjs +788 -726
  57. package/bin/lib/remoteControl.mjs +26 -26
  58. package/docs/AIVIN_MASCOT.md +130 -0
  59. package/docs/CLI.md +1504 -1353
  60. package/docs/media/aivin-mascot-live.gif +0 -0
  61. package/docs/media/aivin-mascot-live.png +0 -0
  62. package/docs/media/aivin-remote-architecture.svg +102 -0
  63. package/package.json +71 -71
@@ -39,17 +39,11 @@ const interruptedOutcome = () => ({
39
39
  export function createIdemLog(file, { now = () => Date.now() } = {}) {
40
40
  const entries = new Map();
41
41
  let lines = 0;
42
+ const pendingLines = [];
43
+ let writeChain = Promise.resolve();
44
+ let dirEnsured = false;
42
45
 
43
- const write = (record) => {
44
- try {
45
- fs.mkdirSync(path.dirname(file), { recursive: true });
46
- fs.appendFileSync(file, JSON.stringify(record) + '\n');
47
- lines++;
48
- if (lines > COMPACT_LINES) compact();
49
- } catch { /* ổ đầy / không ghi được: chống trùng vẫn chạy trong bộ nhớ */ }
50
- };
51
-
52
- function compact() {
46
+ function compactSync() {
53
47
  const keep = [...entries.entries()].slice(-PERSIST_MAX);
54
48
  entries.clear();
55
49
  keep.forEach(([k, v]) => entries.set(k, v));
@@ -59,9 +53,38 @@ export function createIdemLog(file, { now = () => Date.now() } = {}) {
59
53
  lines = keep.length;
60
54
  }
61
55
 
56
+ async function compactAsync() {
57
+ const keep = [...entries.entries()].slice(-PERSIST_MAX);
58
+ entries.clear();
59
+ keep.forEach(([k, v]) => entries.set(k, v));
60
+ const tmp = `${file}.tmp`;
61
+ const body = keep.map(([k, v]) => JSON.stringify({ k, s: v.state, at: v.at, ...(v.outcome ? { o: v.outcome } : {}) }) + '\n').join('');
62
+ await fs.promises.writeFile(tmp, body);
63
+ await fs.promises.rename(tmp, file);
64
+ lines = keep.length;
65
+ }
66
+
67
+ const write = (record) => {
68
+ pendingLines.push(JSON.stringify(record) + '\n');
69
+ writeChain = writeChain.then(async () => {
70
+ if (!pendingLines.length) return;
71
+ const batch = pendingLines.splice(0, pendingLines.length);
72
+ try {
73
+ if (!dirEnsured) {
74
+ await fs.promises.mkdir(path.dirname(file), { recursive: true });
75
+ dirEnsured = true;
76
+ }
77
+ await fs.promises.appendFile(file, batch.join(''));
78
+ lines += batch.length;
79
+ if (lines > COMPACT_LINES) await compactAsync();
80
+ } catch { /* ổ đầy / không ghi được: chống trùng vẫn chạy trong bộ nhớ */ }
81
+ });
82
+ return writeChain;
83
+ };
84
+
62
85
  /** Đọc sổ cũ: trả các khoá còn hạn (đã xong / bị ngắt), viết gọn tệp. */
63
86
  function load() {
64
- let raw = '';
87
+ let raw;
65
88
  try { raw = fs.readFileSync(file, 'utf8'); } catch { return entries; }
66
89
  const t = now();
67
90
  for (const line of raw.split('\n')) {
@@ -73,19 +96,20 @@ export function createIdemLog(file, { now = () => Date.now() } = {}) {
73
96
  if (r.s === IdemState.FAIL) continue;
74
97
  entries.set(r.k, { state: r.s === IdemState.START ? IdemState.INTERRUPTED : r.s, at: Number(r.at), ...(r.o ? { outcome: r.o } : {}) });
75
98
  }
76
- try { compact(); } catch { /* giữ tệp cũ */ }
99
+ try { compactSync(); dirEnsured = true; } catch { /* giữ tệp cũ */ }
77
100
  return entries;
78
101
  }
79
102
 
80
103
  return {
81
104
  load,
82
- started: (k) => { entries.set(k, { state: IdemState.START, at: now() }); write({ k, s: IdemState.START, at: now() }); },
105
+ flush: () => writeChain,
106
+ started: (k) => { entries.set(k, { state: IdemState.START, at: now() }); return write({ k, s: IdemState.START, at: now() }); },
83
107
  done: (k, outcome) => {
84
108
  const o = keptOutcome(outcome);
85
109
  entries.set(k, { state: IdemState.DONE, at: now(), outcome: o });
86
- write({ k, s: IdemState.DONE, at: now(), o });
110
+ return write({ k, s: IdemState.DONE, at: now(), o });
87
111
  },
88
- failed: (k) => { entries.delete(k); write({ k, s: IdemState.FAIL, at: now() }); },
112
+ failed: (k) => { entries.delete(k); return write({ k, s: IdemState.FAIL, at: now() }); },
89
113
  };
90
114
  }
91
115
 
@@ -122,14 +146,19 @@ export function createIdempotentRunner(run, isReadOnly, { onDuplicate, now = ()
122
146
  return hit.promise.then(markDeduplicated);
123
147
  }
124
148
  let marked = false;
125
- const started = () => { if (!marked) { marked = true; log?.started(key); } };
149
+ const started = () => { if (!marked) { marked = true; void log?.started(key); } };
126
150
  if (!deferStart) started();
127
- const promise = run(call, ...rest, ...(deferStart ? [{ started }] : []));
151
+ const rawPromise = run(call, ...rest, ...(deferStart ? [{ started }] : []));
152
+ const promise = rawPromise.then(async (r) => {
153
+ if (r.outcome.ok) await log?.done(key, r.outcome);
154
+ else { seen.delete(key); await log?.failed(key); }
155
+ return r;
156
+ }, async (err) => {
157
+ seen.delete(key);
158
+ await log?.failed(key);
159
+ throw err;
160
+ });
128
161
  seen.set(key, { promise, at: t });
129
- promise.then((r) => {
130
- if (r.outcome.ok) log?.done(key, r.outcome);
131
- else { seen.delete(key); log?.failed(key); }
132
- }, () => { seen.delete(key); log?.failed(key); });
133
162
  return promise;
134
163
  };
135
164
  }
@@ -39,10 +39,19 @@ const POLICY_KEY_ACCOUNT = 'remote-policy-key';
39
39
  * Công cụ mang lệnh để chạy → tham số chứa lệnh. `gui.act`: chữ gõ vào cửa sổ (hộp Run, terminal đang mở) cũng là một cách chạy lệnh —
40
40
  * xét cùng mẫu lệnh rủi ro / luật chặn của chủ máy.
41
41
  */
42
- const COMMAND_ARG = Object.freeze({ 'shell.exec': 'command', 'proc.start': 'command', 'proc.write': 'text', 'sys.schedule': 'command', 'gui.act': 'text' });
42
+ const COMMAND_ARG = Object.freeze({
43
+ 'shell.exec': 'command', 'proc.start': 'command', 'proc.write': 'text', 'sys.schedule': 'command',
44
+ 'gui.act': 'text', 'gui.do': 'value',
45
+ });
43
46
 
44
47
  /** Công cụ luôn xét là rủi ro: gửi ra ngoài thay người dùng, cài việc chạy định kỳ (cách bám trụ kinh điển của mã độc). */
45
- const ALWAYS_RISKY_TOOLS = new Set(['office.outlook.send', 'sys.schedule']);
48
+ // meeting.start: ghi âm micro + âm thanh máy — AI không tự bật ghi âm khi không ai ngồi trước máy biết (29/10/2026).
49
+ const ALWAYS_RISKY_TOOLS = new Set(['office.outlook.send', 'sys.schedule', 'meeting.start']);
50
+ const ALWAYS_RISKY_REASON = {
51
+ 'office.outlook.send': 'Gửi thư thay bạn',
52
+ 'sys.schedule': 'Đặt việc chạy định kỳ trên máy',
53
+ 'meeting.start': 'Ghi âm cuộc họp (micro + âm thanh máy) và chụp màn hình',
54
+ };
46
55
 
47
56
  /**
48
57
  * Lệnh có hậu quả khó lấy lại / chạm bảo mật — khớp chuỗi nên lách được (ghép chuỗi, mã hoá), nhưng chính các cách lách thông dụng
@@ -212,7 +221,7 @@ const commandOf = (toolName, args) => (COMMAND_ARG[toolName] ? String(args?.[COM
212
221
 
213
222
  /** Vì sao một lời gọi rủi ro (chuỗi ngắn để hiện cho người dùng) — `null` = không rủi ro. */
214
223
  export function riskReason(tool, args = {}) {
215
- if (ALWAYS_RISKY_TOOLS.has(tool.name)) return tool.name === 'office.outlook.send' ? 'Gửi thư thay bạn' : 'Đặt việc chạy định kỳ trên máy';
224
+ if (ALWAYS_RISKY_TOOLS.has(tool.name)) return ALWAYS_RISKY_REASON[tool.name];
216
225
  if (tool.destructive && args.permanent === true) return 'Xoá hẳn, không lấy lại được';
217
226
  const command = commandOf(tool.name, args);
218
227
  if (command && RISKY_COMMAND_PATTERNS.some((re) => re.test(command))) return 'Lệnh có thể thay đổi hệ thống hoặc cài đặt bảo mật';
@@ -224,7 +233,10 @@ export function riskReason(tool, args = {}) {
224
233
  * Lời gọi nội bộ (BE tự dùng: đọc khúc tệp, ghi nhớ…) và chỉ đọc không bao giờ cần xác nhận.
225
234
  */
226
235
  export function evaluateLocalPolicy(policy, tool, args = {}) {
227
- if (policy.deny_tools.includes(tool.name)) {
236
+ // gui.do is only a compact route into the same GUI actuator. A device owner who disabled the
237
+ // lower-level actuator must not get it back merely because a newer CLI advertised the fast path.
238
+ const deniedByAlias = tool.name === 'gui.do' && policy.deny_tools.includes('gui.act');
239
+ if (policy.deny_tools.includes(tool.name) || deniedByAlias) {
228
240
  throw new DeviceToolError(DeviceErrorCode.LOCAL_DENIED, `The owner of this computer turned off ${tool.name} here`);
229
241
  }
230
242
  const command = commandOf(tool.name, args);
@@ -243,6 +255,7 @@ export function describeCall(tool, args = {}) {
243
255
  const command = commandOf(tool.name, args);
244
256
  if (command) return cut(command);
245
257
  if (tool.name === 'office.outlook.send') return cut(`Tới: ${args.to || '?'} — ${args.subject || ''}`, 300);
258
+ if (tool.name === 'meeting.start') return cut(`${args.title || 'Cuộc họp'}${args.then ? ` — sau đó: ${args.then}` : ''}`, 300);
246
259
  if (args.path) return cut(args.path, 300);
247
260
  if (Array.isArray(args.paths)) return cut(args.paths.join(', '), 300);
248
261
  return tool.name;
@@ -0,0 +1,70 @@
1
+ /**
2
+ * IMA ADPCM cho âm thanh nói chuyện giữa máy và Aivin (30/10/2026): PCM16 mono → 4 bit / mẫu (nhỏ đi 4 lần; gửi nhị phân thay base64 thêm
3
+ * ~1/3) — không cần thư viện native như Opus. Mỗi đoạn tự giải được (đầu đoạn mang trạng thái bộ mã hoá), mất một đoạn không làm hỏng đoạn
4
+ * sau. Cùng thuật toán với BE (`device-agent/helper/AdpcmHelper.ts`).
5
+ *
6
+ * Một đoạn: [int16 LE dự đoán][uint8 chỉ số bước][uint8 cờ: bit 0 = số mẫu lẻ] + 2 mẫu / byte (nửa thấp trước).
7
+ */
8
+ export const VOICE_CODEC = 'ima-adpcm';
9
+
10
+ const INDEX_TABLE = [-1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8];
11
+ const STEP_TABLE = [
12
+ 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173,
13
+ 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
14
+ 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 15289, 16818,
15
+ 18500, 20350, 22385, 24623, 27086, 29794, 32767,
16
+ ];
17
+ const HEADER = 4;
18
+ const clamp = (v, lo, hi) => (v < lo ? lo : v > hi ? hi : v);
19
+
20
+ /** Bộ mã hoá giữ trạng thái giữa các đoạn của một luồng (âm thanh liền mạch). */
21
+ export function createAdpcmEncoder() {
22
+ let pred = 0;
23
+ let index = 0;
24
+ return (pcm) => {
25
+ const n = Math.floor(pcm.length / 2);
26
+ const out = Buffer.alloc(HEADER + Math.ceil(n / 2));
27
+ out.writeInt16LE(pred, 0);
28
+ out[2] = index;
29
+ out[3] = n % 2;
30
+ for (let i = 0; i < n; i++) {
31
+ let step = STEP_TABLE[index];
32
+ let diff = pcm.readInt16LE(i * 2) - pred;
33
+ let nib = 0;
34
+ if (diff < 0) { nib = 8; diff = -diff; }
35
+ let delta = step >> 3;
36
+ if (diff >= step) { nib |= 4; diff -= step; delta += step; }
37
+ step >>= 1;
38
+ if (diff >= step) { nib |= 2; diff -= step; delta += step; }
39
+ step >>= 1;
40
+ if (diff >= step) { nib |= 1; delta += step; }
41
+ pred = clamp(nib & 8 ? pred - delta : pred + delta, -32768, 32767);
42
+ index = clamp(index + INDEX_TABLE[nib], 0, 88);
43
+ out[HEADER + (i >> 1)] |= i & 1 ? nib << 4 : nib;
44
+ }
45
+ return out;
46
+ };
47
+ }
48
+
49
+ /** Giải một đoạn → PCM16 mono. Đoạn hỏng / rỗng → Buffer rỗng. */
50
+ export function decodeAdpcm(data) {
51
+ const buf = Buffer.isBuffer(data) ? data : data instanceof ArrayBuffer ? Buffer.from(data) : ArrayBuffer.isView(data) ? Buffer.from(data.buffer, data.byteOffset, data.byteLength) : null;
52
+ if (!buf || buf.length < HEADER) return Buffer.alloc(0);
53
+ let pred = buf.readInt16LE(0);
54
+ let index = clamp(buf[2], 0, 88);
55
+ const n = (buf.length - HEADER) * 2 - (buf[3] & 1);
56
+ const out = Buffer.alloc(n * 2);
57
+ for (let i = 0; i < n; i++) {
58
+ const byte = buf[HEADER + (i >> 1)];
59
+ const nib = i & 1 ? byte >> 4 : byte & 15;
60
+ const step = STEP_TABLE[index];
61
+ let delta = step >> 3;
62
+ if (nib & 4) delta += step;
63
+ if (nib & 2) delta += step >> 1;
64
+ if (nib & 1) delta += step >> 2;
65
+ pred = clamp(nib & 8 ? pred - delta : pred + delta, -32768, 32767);
66
+ index = clamp(index + INDEX_TABLE[nib], 0, 88);
67
+ out.writeInt16LE(pred, i * 2);
68
+ }
69
+ return out;
70
+ }