@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
@@ -0,0 +1,295 @@
1
+ /**
2
+ * Biểu tượng Aivin trên thanh menu macOS (29/10/2026) — JavaScript for Automation (`osascript -l JavaScript`, cầu nối ObjC tới Cocoa), có sẵn
3
+ * trên mọi máy Mac nên không phải biên dịch / ký. Chạy trong gói ~/Applications/Aivin.app (mac.mjs), LaunchAgent com.aivin.remote mở nó
4
+ * lúc đăng nhập. Như Aivin.exe trên Windows: biểu tượng + menu (trạng thái, kết nối bằng mã, tạm dừng, mở khi đăng nhập, thư mục, thoát) và
5
+ * giữ `aivin-remote-run.sh` chạy (mã 0 → chờ tới khi máy rảnh lại, 75 → chạy lại ngay, khác → giãn dần tới 5 phút; tự lùi bản nằm trong
6
+ * kịch bản đó). `@@HOME@@` = thư mục cấu hình (chuỗi JSON). Không dùng template literal ở đây (mã nằm trong String.raw).
7
+ */
8
+ export const MAC_TRAY_JS = String.raw`ObjC.import('Cocoa');
9
+ ObjC.import('stdlib');
10
+
11
+ var HOME = @@HOME@@;
12
+ var CREDS = HOME + '/device-credentials.json';
13
+ var LOCK = HOME + '/remote-daemon.lock';
14
+ var PAUSED = HOME + '/remote-paused';
15
+ var DESK = HOME + '/desktop';
16
+ var CFG = DESK + '/launch.cfg';
17
+ var LOGO = DESK + '/logo.png';
18
+ var PIDFILE = DESK + '/tray.pid';
19
+ var OPEN_REQ = DESK + '/open-request';
20
+ var QUIT_REQ = DESK + '/quit-request';
21
+ var RESTART_CODE = 75;
22
+
23
+ var fm = $.NSFileManager.defaultManager;
24
+ var sa = Application.currentApplication();
25
+ sa.includeStandardAdditions = true;
26
+
27
+ function exists(p) { return fm.fileExistsAtPath(p); }
28
+ function read(p) {
29
+ var s = $.NSString.stringWithContentsOfFileEncodingError(p, $.NSUTF8StringEncoding, null);
30
+ return s.isNil() ? null : ObjC.unwrap(s);
31
+ }
32
+ function write(p, text) { $(text).writeToFileAtomicallyEncodingError(p, true, $.NSUTF8StringEncoding, null); }
33
+ function remove(p) { if (exists(p)) fm.removeItemAtPathError(p, null); }
34
+ function mtime(p) {
35
+ var a = fm.attributesOfItemAtPathError(p, null);
36
+ return a.isNil() ? 0 : a.objectForKey($.NSFileModificationDate).timeIntervalSince1970;
37
+ }
38
+ function task(path, args) {
39
+ var t = $.NSTask.alloc.init;
40
+ t.launchPath = path;
41
+ t.arguments = $(args);
42
+ return t;
43
+ }
44
+ function runWait(path, args) {
45
+ try { var t = task(path, args); t.launch; t.waitUntilExit; return t.terminationStatus; } catch (e) { return -1; }
46
+ }
47
+ function output(path, args) {
48
+ try {
49
+ var t = task(path, args);
50
+ var pipe = $.NSPipe.pipe;
51
+ t.standardOutput = pipe;
52
+ t.standardError = $.NSFileHandle.fileHandleWithNullDevice;
53
+ t.launch;
54
+ var data = pipe.fileHandleForReading.readDataToEndOfFile;
55
+ t.waitUntilExit;
56
+ return ObjC.unwrap($.NSString.alloc.initWithDataEncoding(data, $.NSUTF8StringEncoding)) || '';
57
+ } catch (e) {
58
+ return '';
59
+ }
60
+ }
61
+ function notify(text) { try { sa.displayNotification(text, { withTitle: 'AIVIN Bot' }); } catch (e) {} }
62
+
63
+ function loadCfg() {
64
+ var c = { exe: null, args: [], s: {}, extra: {} };
65
+ var lines = (read(CFG) || '').split(/\r?\n/);
66
+ for (var i = 0; i < lines.length; i++) {
67
+ var at = lines[i].indexOf('=');
68
+ if (at <= 0) continue;
69
+ var k = lines[i].slice(0, at), v = lines[i].slice(at + 1);
70
+ if (k === 'exe') c.exe = v;
71
+ else if (k === 'arg') c.args.push(v);
72
+ else if (k.indexOf('s.') === 0) c.s[k.slice(2)] = v;
73
+ else c.extra[k] = v;
74
+ }
75
+ return c;
76
+ }
77
+ var cfg = loadCfg();
78
+ function T(k) { return cfg.s[k] || k; }
79
+
80
+ // --- Trình giữ chạy ----------------------------------------------------------------------------------------------------------------
81
+
82
+ // Listener sập: mở lại sau 2 s, rồi gấp đôi (tối đa 5 phút) nếu sập liên tục.
83
+ var FIRST_DELAY = 2;
84
+ var child = null, childPid = 0, childStarted = 0, nextStart = 0, delay = FIRST_DELAY, pairing = false, quitting = false;
85
+ var wasPaired = exists(CREDS);
86
+ var seenOpen = mtime(OPEN_REQ), seenQuit = mtime(QUIT_REQ);
87
+
88
+ /** Khoá daemon còn được làm mới (< 90 s) bởi tiến trình khác tiến trình con vừa thoát của mình (nó có thể sập mà không xoá khoá). */
89
+ function lockHeld() {
90
+ var s = read(LOCK);
91
+ if (!s) return false;
92
+ try {
93
+ var d = JSON.parse(s);
94
+ if (Number(d.pid) === childPid && !child) return false;
95
+ var at = Date.parse(d.beat_at || d.started_at || '');
96
+ return !isFinite(at) || Date.now() - at < 90000;
97
+ } catch (e) {
98
+ return false;
99
+ }
100
+ }
101
+
102
+ function backoff() {
103
+ nextStart = Date.now() + delay * 1000;
104
+ delay = Math.min(delay * 2, 300);
105
+ }
106
+
107
+ function startChild(code) {
108
+ cfg = loadCfg();
109
+ if (!cfg.exe) return;
110
+ var args = cfg.args.slice();
111
+ if (code) args.push('--code', code);
112
+ try {
113
+ var t = task(cfg.exe, args);
114
+ t.currentDirectoryPath = $.NSHomeDirectory();
115
+ t.launch;
116
+ child = t;
117
+ childPid = t.processIdentifier;
118
+ childStarted = Date.now();
119
+ pairing = !!code;
120
+ } catch (e) {
121
+ child = null;
122
+ backoff();
123
+ }
124
+ }
125
+
126
+ function onChildExit() {
127
+ var rc = child.terminationStatus;
128
+ var secs = (Date.now() - childStarted) / 1000;
129
+ child = null;
130
+ var wasPairing = pairing;
131
+ pairing = false;
132
+ if (wasPairing && !exists(CREDS)) { notify(T('pairFailed')); return; }
133
+ // 0 = dừng có chủ đích (thu hồi, ghép lại, cửa sổ khác nhận việc): chờ ngắn; thoát 0 liền nhau (hai máy chung token) thì giãn dần.
134
+ if (rc === 0) { if (secs < 30) { backoff(); } else { delay = FIRST_DELAY; nextStart = Date.now() + FIRST_DELAY * 1000; } return; }
135
+ if (rc === RESTART_CODE) { nextStart = Date.now() + 1000; return; }
136
+ if (secs > 300) delay = FIRST_DELAY;
137
+ backoff();
138
+ }
139
+
140
+ function killChild() {
141
+ var c = child;
142
+ child = null;
143
+ if (!c || !c.isRunning) return;
144
+ // aivin-remote-run.sh chờ daemon (không exec) — dừng con của nó trước rồi tới nó.
145
+ runWait('/usr/bin/pkill', ['-TERM', '-P', String(c.processIdentifier)]);
146
+ try { c.terminate; } catch (e) {}
147
+ }
148
+
149
+ function running() { return (child && child.isRunning) || lockHeld(); }
150
+
151
+ function tick() {
152
+ if (quitting) return;
153
+ if (mtime(QUIT_REQ) > seenQuit) { quit(); return; }
154
+ if (child && !child.isRunning) onChildExit();
155
+ var paired = exists(CREDS);
156
+ if (paired && !wasPaired) notify(T('connected'));
157
+ wasPaired = paired;
158
+ if (!child && paired && Date.now() >= nextStart && !lockHeld()) startChild(null);
159
+ var req = mtime(OPEN_REQ);
160
+ if (req > seenOpen) { seenOpen = req; openAivin(); }
161
+ refresh();
162
+ }
163
+
164
+ // --- Menu --------------------------------------------------------------------------------------------------------------------------
165
+
166
+ var app = $.NSApplication.sharedApplication;
167
+ var item, menu, statusItem, openItem, pauseItem, startupItem, target;
168
+
169
+ function startupEnabled() { return !!cfg.extra.agent && exists(cfg.extra.agent); }
170
+
171
+ function setStartup(on) {
172
+ var agent = cfg.extra.agent, copy = cfg.extra.agent_copy;
173
+ if (!agent) return;
174
+ if (on) {
175
+ if (copy && exists(copy) && !exists(agent)) fm.copyItemAtPathToPathError(copy, agent, null);
176
+ } else {
177
+ remove(agent);
178
+ }
179
+ }
180
+
181
+ function refresh() {
182
+ var paired = exists(CREDS), paused = exists(PAUSED), up = running();
183
+ var state = !paired ? T(pairing ? 'connecting' : 'notPaired') : !up ? T('stopped') : paused ? T('paused') : T('running');
184
+ statusItem.title = state;
185
+ item.button.toolTip = 'Aivin - ' + state;
186
+ item.button.appearsDisabled = !(paired && up && !paused);
187
+ openItem.title = T(paired ? 'open' : 'connect');
188
+ openItem.hidden = paired;
189
+ pauseItem.title = T(paused ? 'resume' : 'pause');
190
+ pauseItem.hidden = !paired;
191
+ startupItem.state = startupEnabled() ? 1 : 0;
192
+ }
193
+
194
+ function askCode() {
195
+ app.activateIgnoringOtherApps(true);
196
+ var r;
197
+ try {
198
+ r = sa.displayDialog(T('pairPrompt'), {
199
+ defaultAnswer: '', buttons: [T('pairCancel'), T('pairOk')], defaultButton: T('pairOk'), cancelButton: T('pairCancel'), withTitle: 'AIVIN Bot',
200
+ });
201
+ } catch (e) {
202
+ return;
203
+ }
204
+ var code = String(r.textReturned || '').trim().toUpperCase();
205
+ if (!code || quitting) return;
206
+ killChild();
207
+ startChild(code);
208
+ refresh();
209
+ }
210
+
211
+ /** Mở lại Aivin (Spotlight / Launchpad khi đang chạy): chưa ghép máy → hỏi mã; có rồi → mở menu, chạy daemon ngay nếu đang không chạy. */
212
+ function openAivin() {
213
+ if (quitting) return;
214
+ if (!exists(CREDS)) { askCode(); return; }
215
+ if (!child && !lockHeld()) { nextStart = 0; delay = FIRST_DELAY; tick(); }
216
+ try { item.button.performClick(null); } catch (e) {}
217
+ }
218
+
219
+ function togglePause() {
220
+ if (exists(PAUSED)) remove(PAUSED);
221
+ else write(PAUSED, new Date().toISOString());
222
+ refresh();
223
+ }
224
+
225
+ function quit() {
226
+ if (quitting) return;
227
+ quitting = true;
228
+ killChild();
229
+ remove(PIDFILE);
230
+ $.exit(0);
231
+ }
232
+
233
+ function addItem(title, action) {
234
+ var mi = $.NSMenuItem.alloc.initWithTitleActionKeyEquivalent(title, action, '');
235
+ if (action) mi.target = target;
236
+ menu.addItem(mi);
237
+ return mi;
238
+ }
239
+
240
+ function buildUi() {
241
+ ObjC.registerSubclass({
242
+ name: 'AivinTrayTarget',
243
+ methods: {
244
+ 'onOpen:': { types: ['void', ['id']], implementation: function () { openAivin(); } },
245
+ 'onPause:': { types: ['void', ['id']], implementation: function () { togglePause(); } },
246
+ 'onStartup:': { types: ['void', ['id']], implementation: function () { setStartup(!startupEnabled()); refresh(); } },
247
+ 'onFolder:': { types: ['void', ['id']], implementation: function () { task('/usr/bin/open', [HOME]).launch; } },
248
+ 'onQuit:': { types: ['void', ['id']], implementation: function () { quit(); } },
249
+ 'tick:': { types: ['void', ['id']], implementation: function () { try { tick(); } catch (e) { console.log('aivin tray: ' + e); } } },
250
+ 'applicationShouldHandleReopen:hasVisibleWindows:': { types: ['bool', ['id', 'bool']], implementation: function () { openAivin(); return false; } },
251
+ },
252
+ });
253
+ target = $.AivinTrayTarget.alloc.init;
254
+ app.setActivationPolicy($.NSApplicationActivationPolicyAccessory);
255
+ app.delegate = target;
256
+
257
+ item = $.NSStatusBar.systemStatusBar.statusItemWithLength($.NSVariableStatusItemLength);
258
+ var img = $.NSImage.alloc.initWithContentsOfFile(LOGO);
259
+ if (img.isNil()) item.button.title = 'AIVIN Bot';
260
+ else { img.size = $.NSMakeSize(18, 18); item.button.image = img; }
261
+
262
+ menu = $.NSMenu.alloc.init;
263
+ menu.autoenablesItems = false;
264
+ statusItem = addItem('AIVIN Bot', null);
265
+ statusItem.enabled = false;
266
+ menu.addItem($.NSMenuItem.separatorItem);
267
+ openItem = addItem(T('connect'), 'onOpen:');
268
+ pauseItem = addItem(T('pause'), 'onPause:');
269
+ startupItem = addItem(T('startup'), 'onStartup:');
270
+ addItem(T('folder'), 'onFolder:');
271
+ menu.addItem($.NSMenuItem.separatorItem);
272
+ addItem(T('quit'), 'onQuit:');
273
+ item.menu = menu;
274
+ $.NSTimer.scheduledTimerWithTimeIntervalTargetSelectorUserInfoRepeats(2, target, 'tick:', $(), true);
275
+ }
276
+
277
+ function run(argv) {
278
+ var has = function (flag) { return argv.indexOf(flag) >= 0; };
279
+ var me = $.NSProcessInfo.processInfo.processIdentifier;
280
+ var other = parseInt(read(PIDFILE) || '0', 10);
281
+ // Đã có một bản chạy: mở lần nữa = mở Aivin; --quit = bảo bản đó thoát (qua tệp yêu cầu, bản đang chạy đọc mỗi 2 s).
282
+ if (other && other !== me && output('/bin/ps', ['-p', String(other), '-o', 'command=']).indexOf('tray.js') >= 0) {
283
+ if (has('--quit')) write(QUIT_REQ, String(Date.now()));
284
+ else if (!has('--autostart') && !has('--installed')) write(OPEN_REQ, String(Date.now()));
285
+ return;
286
+ }
287
+ if (has('--quit')) return;
288
+ write(PIDFILE, String(me));
289
+ buildUi();
290
+ tick();
291
+ if (has('--installed')) notify(T(wasPaired ? 'firstRun' : 'connectHint'));
292
+ else if (!has('--autostart')) { if (wasPaired) notify(T('firstRun')); openAivin(); }
293
+ app.run;
294
+ }
295
+ `;
@@ -0,0 +1,74 @@
1
+ /** Chữ trên menu khay hệ thống (Aivin.exe) — ghi vào launch.cfg theo ngôn ngữ máy (mascotLang). */
2
+ export const TRAY_STRINGS = Object.freeze({
3
+ vi: {
4
+ running: 'Đang chạy — sẵn sàng nhận việc',
5
+ paused: 'Đang tạm dừng — từ chối mọi việc từ xa',
6
+ stopped: 'Đang không chạy',
7
+ notPaired: 'Chưa kết nối với Aivin',
8
+ connecting: 'Đang kết nối…',
9
+ open: 'Mở Aivin',
10
+ connect: 'Kết nối máy này…',
11
+ pause: 'Tạm dừng nhận việc',
12
+ resume: 'Tiếp tục nhận việc',
13
+ startup: 'Khởi động cùng Windows',
14
+ folder: 'Mở thư mục Aivin',
15
+ quit: 'Thoát Aivin',
16
+ firstRun: 'Aivin đang chạy ở khay hệ thống — bấm biểu tượng để mở.',
17
+ connectHint: 'Aivin đã ở khay hệ thống — bấm biểu tượng rồi nhập mã từ Aivin để kết nối máy này.',
18
+ connected: 'Đã kết nối máy này với Aivin.',
19
+ pairFailed: 'Không kết nối được — mã sai hoặc đã hết hạn. Tạo mã mới trong Aivin rồi thử lại.',
20
+ pairPrompt: 'Nhập mã kết nối từ Aivin (khung chat → Browser ▾ → Thêm máy tính này):',
21
+ pairOk: 'Kết nối',
22
+ pairCancel: 'Huỷ',
23
+ shortcut: 'Aivin — cho các agent Aivin làm việc trên máy tính này',
24
+ },
25
+ en: {
26
+ running: 'Running — ready for work',
27
+ paused: 'Paused — refusing all remote work',
28
+ stopped: 'Not running',
29
+ notPaired: 'Not connected to Aivin',
30
+ connecting: 'Connecting…',
31
+ open: 'Open Aivin',
32
+ connect: 'Connect this computer…',
33
+ pause: 'Pause work',
34
+ resume: 'Resume work',
35
+ startup: 'Start with Windows',
36
+ folder: 'Open Aivin folder',
37
+ quit: 'Quit Aivin',
38
+ firstRun: 'Aivin is running in the system tray — click the icon to open it.',
39
+ connectHint: 'Aivin is in the system tray — click the icon and enter the code from Aivin to connect this computer.',
40
+ connected: 'This computer is now connected to Aivin.',
41
+ pairFailed: 'Could not connect — the code is wrong or expired. Create a new code in Aivin and try again.',
42
+ pairPrompt: 'Enter the connect code from Aivin (chat box → Browser ▾ → Add this computer):',
43
+ pairOk: 'Connect',
44
+ pairCancel: 'Cancel',
45
+ shortcut: 'Aivin — lets your Aivin agents work on this computer',
46
+ },
47
+ });
48
+
49
+ /** Chữ khác đi theo hệ điều hành (macOS: thanh menu; macOS / Linux: khởi động cùng máy thay vì cùng Windows). */
50
+ const PLATFORM_STRINGS = Object.freeze({
51
+ darwin: {
52
+ vi: {
53
+ startup: 'Mở khi đăng nhập',
54
+ firstRun: 'Aivin đang chạy trên thanh menu — bấm biểu tượng để xem.',
55
+ connectHint: 'Aivin đã ở thanh menu — bấm biểu tượng rồi chọn "Kết nối máy này…" để nhập mã từ Aivin.',
56
+ folder: 'Mở thư mục Aivin trong Finder',
57
+ },
58
+ en: {
59
+ startup: 'Open at login',
60
+ firstRun: 'Aivin is running in the menu bar — click its icon to see it.',
61
+ connectHint: 'Aivin is in the menu bar — click its icon, then "Connect this computer…" to enter the code from Aivin.',
62
+ folder: 'Show Aivin folder in Finder',
63
+ },
64
+ },
65
+ linux: {
66
+ vi: { startup: 'Khởi động cùng máy' },
67
+ en: { startup: 'Start at login' },
68
+ },
69
+ });
70
+
71
+ export function trayStrings(lang, platform = 'win32') {
72
+ const code = TRAY_STRINGS[lang] ? lang : 'en';
73
+ return { ...TRAY_STRINGS[code], ...(PLATFORM_STRINGS[platform]?.[code] || {}) };
74
+ }