@aivin-labs/cli 1.0.11 → 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.
- package/README.md +16 -0
- package/bin/cli.mjs +67 -0
- package/bin/lib/remote/browserTunnel.mjs +47 -8
- package/bin/lib/remote/callLedger.mjs +73 -14
- package/bin/lib/remote/client.mjs +89 -8
- package/bin/lib/remote/cloudFiles.mjs +53 -0
- package/bin/lib/remote/constants.mjs +17 -0
- package/bin/lib/remote/dailyBrowser.mjs +8 -2
- package/bin/lib/remote/desktop/desktopCast.mjs +198 -0
- package/bin/lib/remote/desktop/desktopCastSource.mjs +514 -0
- package/bin/lib/remote/desktop/desktopWebRtc.mjs +95 -0
- package/bin/lib/remote/desktop/index.mjs +44 -7
- package/bin/lib/remote/desktop/linux.mjs +1 -1
- package/bin/lib/remote/desktop/linuxTraySource.mjs +434 -427
- package/bin/lib/remote/desktop/mac.mjs +2 -2
- package/bin/lib/remote/desktop/macTraySource.mjs +295 -292
- package/bin/lib/remote/desktop/traySource.mjs +541 -527
- package/bin/lib/remote/deviceProfile.mjs +1 -0
- package/bin/lib/remote/docText.mjs +85 -0
- package/bin/lib/remote/fileIndexer.mjs +757 -399
- package/bin/lib/remote/folderRules.mjs +140 -0
- package/bin/lib/remote/gui/index.mjs +126 -17
- package/bin/lib/remote/gui/windowsHost.mjs +9 -2
- package/bin/lib/remote/gui/windowsHostSource.mjs +194 -51
- package/bin/lib/remote/idempotency.mjs +49 -20
- package/bin/lib/remote/localPolicy.mjs +8 -2
- package/bin/lib/remote/mascot/askHostSource.mjs +18 -0
- package/bin/lib/remote/mascot/bot.mjs +29 -4
- package/bin/lib/remote/mascot/hostSource.mjs +4 -4
- package/bin/lib/remote/mascot/rigSource.mjs +12 -7
- package/bin/lib/remote/osSearch.mjs +107 -0
- package/bin/lib/remote/sqliteDb.mjs +51 -0
- package/bin/lib/remote/toolHandlers.mjs +788 -741
- package/bin/lib/remoteControl.mjs +26 -26
- package/docs/AIVIN_MASCOT.md +14 -0
- package/docs/CLI.md +1504 -1484
- package/docs/media/aivin-mascot-live.gif +0 -0
- package/docs/media/aivin-mascot-live.png +0 -0
- package/docs/media/aivin-remote-architecture.svg +102 -0
- package/package.json +71 -71
- package/docs/media/aivin-mascot-states.mp4 +0 -0
|
@@ -47,9 +47,9 @@ export function macInfoPlist() {
|
|
|
47
47
|
<plist version="1.0">
|
|
48
48
|
<dict>
|
|
49
49
|
<key>CFBundleName</key>
|
|
50
|
-
<string>AIVIN Bot
|
|
50
|
+
<string>AIVIN Bot</string>
|
|
51
51
|
<key>CFBundleDisplayName</key>
|
|
52
|
-
<string>AIVIN Bot
|
|
52
|
+
<string>AIVIN Bot</string>
|
|
53
53
|
<key>CFBundleIdentifier</key>
|
|
54
54
|
<string>${MAC_BUNDLE_ID}</string>
|
|
55
55
|
<key>CFBundleExecutable</key>
|
|
@@ -1,292 +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
|
|
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
|
-
|
|
83
|
-
var
|
|
84
|
-
var
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
var
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
if (
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
t.
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
var
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
if (
|
|
133
|
-
|
|
134
|
-
backoff();
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if (
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
'
|
|
245
|
-
'
|
|
246
|
-
'
|
|
247
|
-
'
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
menu.
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
addItem(T('
|
|
268
|
-
|
|
269
|
-
addItem(T('
|
|
270
|
-
|
|
271
|
-
$.
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
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
|
+
`;
|