@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.
- package/README.md +325 -288
- package/bin/cli.mjs +163 -17
- package/bin/lib/localPreview.mjs +437 -0
- package/bin/lib/openFile.mjs +235 -0
- package/bin/lib/previewCache.mjs +71 -0
- package/bin/lib/remote/atomicFile.mjs +32 -0
- package/bin/lib/remote/autoStart.mjs +2 -1
- package/bin/lib/remote/autoUpdate.mjs +12 -0
- package/bin/lib/remote/browserTunnel.mjs +47 -8
- package/bin/lib/remote/callLedger.mjs +94 -14
- package/bin/lib/remote/cdpPolicy.mjs +14 -2
- package/bin/lib/remote/client.mjs +1024 -808
- package/bin/lib/remote/cloudFiles.mjs +53 -0
- package/bin/lib/remote/codingAgents.mjs +2 -2
- package/bin/lib/remote/constants.mjs +261 -234
- package/bin/lib/remote/credentials.mjs +81 -4
- package/bin/lib/remote/daemonLock.mjs +57 -17
- package/bin/lib/remote/dailyBrowser.mjs +8 -2
- package/bin/lib/remote/desktop/castInput.mjs +98 -0
- package/bin/lib/remote/desktop/desktopCast.mjs +334 -0
- package/bin/lib/remote/desktop/desktopCastCaptureSource.mjs +598 -0
- package/bin/lib/remote/desktop/desktopCastSource.mjs +798 -0
- package/bin/lib/remote/desktop/desktopWebRtc.mjs +242 -0
- package/bin/lib/remote/desktop/fileAssociation.mjs +108 -0
- package/bin/lib/remote/desktop/index.mjs +58 -7
- package/bin/lib/remote/desktop/linux.mjs +1 -1
- package/bin/lib/remote/desktop/linuxTraySource.mjs +427 -427
- package/bin/lib/remote/desktop/mac.mjs +2 -2
- package/bin/lib/remote/desktop/macTraySource.mjs +293 -292
- package/bin/lib/remote/desktop/strings.mjs +28 -2
- package/bin/lib/remote/desktop/traySource.mjs +905 -527
- package/bin/lib/remote/desktop/webrtcRuntime.mjs +137 -0
- package/bin/lib/remote/desktop/webrtcWorker.mjs +181 -0
- package/bin/lib/remote/deviceProfile.mjs +1 -0
- package/bin/lib/remote/docText.mjs +85 -0
- package/bin/lib/remote/fileIndexer.mjs +773 -399
- package/bin/lib/remote/folderRules.mjs +143 -0
- package/bin/lib/remote/gitSafe.mjs +31 -0
- package/bin/lib/remote/gui/index.mjs +170 -19
- package/bin/lib/remote/gui/officeHostSource.mjs +635 -393
- package/bin/lib/remote/gui/windowsHost.mjs +12 -3
- package/bin/lib/remote/gui/windowsHostSource.mjs +461 -87
- package/bin/lib/remote/httpTransfer.mjs +64 -3
- package/bin/lib/remote/idempotency.mjs +49 -20
- package/bin/lib/remote/localPolicy.mjs +81 -9
- package/bin/lib/remote/mascot/ask.mjs +207 -42
- package/bin/lib/remote/mascot/askBlocksSource.mjs +286 -0
- package/bin/lib/remote/mascot/askHostSource.mjs +658 -512
- package/bin/lib/remote/mascot/blocks.mjs +201 -0
- package/bin/lib/remote/mascot/bot.mjs +54 -5
- package/bin/lib/remote/mascot/chartLayout.mjs +382 -0
- package/bin/lib/remote/mascot/diagramRender.mjs +154 -0
- package/bin/lib/remote/mascot/emojiSource.mjs +273 -0
- package/bin/lib/remote/mascot/fileChips.mjs +62 -0
- package/bin/lib/remote/mascot/hostSource.mjs +331 -52
- package/bin/lib/remote/mascot/hotkey.mjs +31 -7
- package/bin/lib/remote/mascot/images.mjs +111 -0
- package/bin/lib/remote/mascot/index.mjs +663 -493
- package/bin/lib/remote/mascot/lucideIcons.mjs +32 -0
- package/bin/lib/remote/mascot/mermaidBundle.mjs +28 -0
- package/bin/lib/remote/mascot/mood.mjs +57 -0
- package/bin/lib/remote/mascot/rigSource.mjs +648 -609
- package/bin/lib/remote/mascot/safeUrl.mjs +29 -0
- package/bin/lib/remote/mascot/strings.mjs +456 -375
- package/bin/lib/remote/mascot/ui.mjs +27 -7
- package/bin/lib/remote/multiUser.mjs +7 -2
- package/bin/lib/remote/notify.mjs +4 -1
- package/bin/lib/remote/office/index.mjs +12 -6
- package/bin/lib/remote/osSearch.mjs +107 -0
- package/bin/lib/remote/processes.mjs +5 -2
- package/bin/lib/remote/sandbox.mjs +26 -0
- package/bin/lib/remote/secretPaths.mjs +21 -0
- package/bin/lib/remote/sqliteDb.mjs +51 -0
- package/bin/lib/remote/tokenRotation.mjs +28 -0
- package/bin/lib/remote/toolHandlers.mjs +852 -741
- package/bin/lib/remote/watchBatcher.mjs +56 -0
- package/bin/lib/remoteControl.mjs +26 -26
- package/bin/lib/selfUpdate.mjs +12 -2
- package/bin/lib/update/binarySwap.mjs +15 -1
- package/bin/lib/update/state.mjs +10 -0
- package/docs/AIVIN_MASCOT.md +14 -0
- package/docs/CLI.md +1545 -1484
- package/docs/media/aivin-ecosystem.jpg +0 -0
- 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 +75 -71
- package/docs/media/aivin-mascot-states.mp4 +0 -0
|
@@ -1,427 +1,427 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Aivin trên màn hình Linux (29/10/2026) — biểu tượng khay qua AppIndicator (Ubuntu / KDE / phần lớn môi trường có khay; GNOME cần tiện
|
|
3
|
-
* ích AppIndicator) bằng python3 + PyGObject có sẵn trên đa số bản desktop. Không có AppIndicator / GTK / màn hình → vẫn chạy, chỉ giữ
|
|
4
|
-
* daemon chạy (không biểu tượng). Không có python3 → kịch bản sh khởi chạy tự lặp giữ daemon. Như Aivin.exe: mã 0 → chờ tới khi máy rảnh lại,
|
|
5
|
-
* 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 aivin-remote-run.sh). `@@HOME@@` = thư mục cấu hình (chuỗi JSON).
|
|
6
|
-
*/
|
|
7
|
-
export const LINUX_TRAY_PY = String.raw`#!/usr/bin/env python3
|
|
8
|
-
# Aivin tray (Linux): AppIndicator icon + keeps aivin-remote-run.sh running. Falls back to no icon when GTK / AppIndicator is missing.
|
|
9
|
-
import calendar
|
|
10
|
-
import json
|
|
11
|
-
import os
|
|
12
|
-
import shutil
|
|
13
|
-
import signal
|
|
14
|
-
import subprocess
|
|
15
|
-
import sys
|
|
16
|
-
import time
|
|
17
|
-
|
|
18
|
-
HOME = @@HOME@@
|
|
19
|
-
CREDS = os.path.join(HOME, 'device-credentials.json')
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
cfg['
|
|
44
|
-
elif k
|
|
45
|
-
cfg['
|
|
46
|
-
|
|
47
|
-
cfg['
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
return True
|
|
76
|
-
except
|
|
77
|
-
return
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
self.
|
|
85
|
-
self.
|
|
86
|
-
self.
|
|
87
|
-
self.
|
|
88
|
-
self.
|
|
89
|
-
self.
|
|
90
|
-
self.
|
|
91
|
-
self.
|
|
92
|
-
self.
|
|
93
|
-
self.
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
self.
|
|
130
|
-
self.
|
|
131
|
-
|
|
132
|
-
self.
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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
|
-
self.
|
|
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
|
-
return
|
|
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
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
state['
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
box.
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
if
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
quit_item.connect('activate', quit_app)
|
|
346
|
-
menu.show_all()
|
|
347
|
-
ind.set_menu(menu)
|
|
348
|
-
sup.on_open = open_aivin
|
|
349
|
-
sup.on_quit = quit_app
|
|
350
|
-
|
|
351
|
-
def on_tick():
|
|
352
|
-
sup.tick()
|
|
353
|
-
refresh()
|
|
354
|
-
return True
|
|
355
|
-
|
|
356
|
-
GLib.timeout_add_seconds(2, on_tick)
|
|
357
|
-
refresh()
|
|
358
|
-
|
|
359
|
-
class Ui:
|
|
360
|
-
open = staticmethod(open_aivin)
|
|
361
|
-
|
|
362
|
-
@staticmethod
|
|
363
|
-
def run():
|
|
364
|
-
Gtk.main()
|
|
365
|
-
return Ui
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
def main(argv):
|
|
369
|
-
import fcntl
|
|
370
|
-
os.makedirs(DESK, exist_ok=True)
|
|
371
|
-
lock = open(TRAY_LOCK, 'w')
|
|
372
|
-
try:
|
|
373
|
-
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
374
|
-
except OSError:
|
|
375
|
-
# Đã có một bản chạy: mở lần nữa = mở Aivin; --quit = bảo bản đó thoát.
|
|
376
|
-
if '--quit' in argv:
|
|
377
|
-
touch(QUIT_REQ)
|
|
378
|
-
elif '--autostart' not in argv and '--installed' not in argv:
|
|
379
|
-
touch(OPEN_REQ)
|
|
380
|
-
return 0
|
|
381
|
-
if '--quit' in argv:
|
|
382
|
-
return 0
|
|
383
|
-
sup = Supervisor()
|
|
384
|
-
signal.signal(signal.SIGTERM, lambda *_: (sup.kill(), sys.exit(0)))
|
|
385
|
-
ui = gtk_ui(sup)
|
|
386
|
-
sup.tick()
|
|
387
|
-
if ui is None:
|
|
388
|
-
stop = []
|
|
389
|
-
sup.on_quit = lambda: (sup.kill(), stop.append(True))
|
|
390
|
-
while not stop:
|
|
391
|
-
time.sleep(2)
|
|
392
|
-
sup.tick()
|
|
393
|
-
return 0
|
|
394
|
-
if '--installed' in argv:
|
|
395
|
-
notify(sup.t('firstRun' if sup.was_paired else 'connectHint'))
|
|
396
|
-
elif '--autostart' not in argv:
|
|
397
|
-
ui.open()
|
|
398
|
-
ui.run()
|
|
399
|
-
return 0
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
if __name__ == '__main__':
|
|
403
|
-
sys.exit(main(sys.argv[1:]))
|
|
404
|
-
`;
|
|
405
|
-
|
|
406
|
-
/**
|
|
407
|
-
* Kịch bản mở Aivin (menu ứng dụng, autostart). Có python3 → khay; không thì tự lặp giữ daemon (chỉ khi đã ghép máy — chưa ghép thì
|
|
408
|
-
* daemon không tự kết nối được, chạy lặp vô ích). `@@…@@` thay bằng đường dẫn đã bọc nháy đơn.
|
|
409
|
-
*/
|
|
410
|
-
export const LINUX_LAUNCHER_SH = `#!/bin/sh
|
|
411
|
-
# Aivin desktop app (Linux): tray icon + keeps \`aivin remote start\` running (tray.py); without python3, keeps it running without an icon.
|
|
412
|
-
exec >>@@LOG@@ 2>&1
|
|
413
|
-
if command -v python3 >/dev/null 2>&1; then exec python3 @@TRAY@@ "$@"; fi
|
|
414
|
-
case " $* " in *" --quit "*) exit 0 ;; esac
|
|
415
|
-
delay=10
|
|
416
|
-
while [ -f @@CREDS@@ ]; do
|
|
417
|
-
started=$(date +%s)
|
|
418
|
-
/bin/sh @@RUN@@
|
|
419
|
-
rc=$?
|
|
420
|
-
[ "$rc" -eq 0 ] && exit 0
|
|
421
|
-
if [ "$rc" -eq 75 ]; then sleep 1; continue; fi
|
|
422
|
-
[ $(( $(date +%s) - started )) -gt 300 ] && delay=10
|
|
423
|
-
sleep "$delay"
|
|
424
|
-
delay=$(( delay * 2 ))
|
|
425
|
-
[ "$delay" -gt 300 ] && delay=300
|
|
426
|
-
done
|
|
427
|
-
`;
|
|
1
|
+
/**
|
|
2
|
+
* Aivin trên màn hình Linux (29/10/2026) — biểu tượng khay qua AppIndicator (Ubuntu / KDE / phần lớn môi trường có khay; GNOME cần tiện
|
|
3
|
+
* ích AppIndicator) bằng python3 + PyGObject có sẵn trên đa số bản desktop. Không có AppIndicator / GTK / màn hình → vẫn chạy, chỉ giữ
|
|
4
|
+
* daemon chạy (không biểu tượng). Không có python3 → kịch bản sh khởi chạy tự lặp giữ daemon. Như Aivin.exe: mã 0 → chờ tới khi máy rảnh lại,
|
|
5
|
+
* 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 aivin-remote-run.sh). `@@HOME@@` = thư mục cấu hình (chuỗi JSON).
|
|
6
|
+
*/
|
|
7
|
+
export const LINUX_TRAY_PY = String.raw`#!/usr/bin/env python3
|
|
8
|
+
# Aivin tray (Linux): AppIndicator icon + keeps aivin-remote-run.sh running. Falls back to no icon when GTK / AppIndicator is missing.
|
|
9
|
+
import calendar
|
|
10
|
+
import json
|
|
11
|
+
import os
|
|
12
|
+
import shutil
|
|
13
|
+
import signal
|
|
14
|
+
import subprocess
|
|
15
|
+
import sys
|
|
16
|
+
import time
|
|
17
|
+
|
|
18
|
+
HOME = @@HOME@@
|
|
19
|
+
CREDS = os.path.join(HOME, 'device-credentials.json')
|
|
20
|
+
# 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.
|
|
21
|
+
FIRST_DELAY = 2
|
|
22
|
+
LOCK = os.path.join(HOME, 'remote-daemon.lock')
|
|
23
|
+
PAUSED = os.path.join(HOME, 'remote-paused')
|
|
24
|
+
DESK = os.path.join(HOME, 'desktop')
|
|
25
|
+
CFG = os.path.join(DESK, 'launch.cfg')
|
|
26
|
+
LOGO = os.path.join(DESK, 'logo.png')
|
|
27
|
+
LOGO_GREY = os.path.join(DESK, 'logo-grey.png')
|
|
28
|
+
TRAY_LOCK = os.path.join(DESK, 'tray.lock')
|
|
29
|
+
OPEN_REQ = os.path.join(DESK, 'open-request')
|
|
30
|
+
QUIT_REQ = os.path.join(DESK, 'quit-request')
|
|
31
|
+
RESTART_CODE = 75
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def load_cfg():
|
|
35
|
+
cfg = {'exe': None, 'args': [], 's': {}, 'extra': {}}
|
|
36
|
+
try:
|
|
37
|
+
with open(CFG, encoding='utf-8') as f:
|
|
38
|
+
for line in f.read().splitlines():
|
|
39
|
+
k, sep, v = line.partition('=')
|
|
40
|
+
if not sep or not k:
|
|
41
|
+
continue
|
|
42
|
+
if k == 'exe':
|
|
43
|
+
cfg['exe'] = v
|
|
44
|
+
elif k == 'arg':
|
|
45
|
+
cfg['args'].append(v)
|
|
46
|
+
elif k.startswith('s.'):
|
|
47
|
+
cfg['s'][k[2:]] = v
|
|
48
|
+
else:
|
|
49
|
+
cfg['extra'][k] = v
|
|
50
|
+
except OSError:
|
|
51
|
+
pass
|
|
52
|
+
return cfg
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def mtime(path):
|
|
56
|
+
try:
|
|
57
|
+
return os.stat(path).st_mtime
|
|
58
|
+
except OSError:
|
|
59
|
+
return 0
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def touch(path):
|
|
63
|
+
with open(path, 'w') as f:
|
|
64
|
+
f.write(str(time.time()))
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def notify(text):
|
|
68
|
+
if shutil.which('notify-send'):
|
|
69
|
+
subprocess.Popen(['notify-send', '-i', LOGO, 'AIVIN Bot', text], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def pid_alive(pid):
|
|
73
|
+
try:
|
|
74
|
+
os.kill(pid, 0)
|
|
75
|
+
return True
|
|
76
|
+
except PermissionError:
|
|
77
|
+
return True
|
|
78
|
+
except (OSError, ValueError):
|
|
79
|
+
return False
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class Supervisor:
|
|
83
|
+
def __init__(self):
|
|
84
|
+
self.cfg = load_cfg()
|
|
85
|
+
self.child = None
|
|
86
|
+
self.child_pid = 0
|
|
87
|
+
self.started = 0
|
|
88
|
+
self.next_start = 0
|
|
89
|
+
self.delay = FIRST_DELAY
|
|
90
|
+
self.pairing = False
|
|
91
|
+
self.was_paired = os.path.exists(CREDS)
|
|
92
|
+
self.seen_open = mtime(OPEN_REQ)
|
|
93
|
+
self.seen_quit = mtime(QUIT_REQ)
|
|
94
|
+
self.on_open = None
|
|
95
|
+
self.on_quit = None
|
|
96
|
+
|
|
97
|
+
def t(self, key):
|
|
98
|
+
return self.cfg['s'].get(key, key)
|
|
99
|
+
|
|
100
|
+
def lock_held(self):
|
|
101
|
+
try:
|
|
102
|
+
with open(LOCK, encoding='utf-8') as f:
|
|
103
|
+
d = json.load(f)
|
|
104
|
+
pid = int(d.get('pid') or 0)
|
|
105
|
+
if self.child is None and pid == self.child_pid:
|
|
106
|
+
return False
|
|
107
|
+
beat = str(d.get('beat_at') or d.get('started_at') or '')
|
|
108
|
+
if beat:
|
|
109
|
+
at = calendar.timegm(time.strptime(beat[:19], '%Y-%m-%dT%H:%M:%S'))
|
|
110
|
+
if time.time() - at > 90:
|
|
111
|
+
return False
|
|
112
|
+
return pid > 0 and pid_alive(pid)
|
|
113
|
+
except (OSError, ValueError):
|
|
114
|
+
return False
|
|
115
|
+
|
|
116
|
+
def running(self):
|
|
117
|
+
return (self.child is not None and self.child.poll() is None) or self.lock_held()
|
|
118
|
+
|
|
119
|
+
def backoff(self):
|
|
120
|
+
self.next_start = time.time() + self.delay
|
|
121
|
+
self.delay = min(self.delay * 2, 300)
|
|
122
|
+
|
|
123
|
+
def start(self, code=None):
|
|
124
|
+
self.cfg = load_cfg()
|
|
125
|
+
if not self.cfg['exe']:
|
|
126
|
+
return
|
|
127
|
+
args = [self.cfg['exe']] + self.cfg['args'] + (['--code', code] if code else [])
|
|
128
|
+
try:
|
|
129
|
+
self.child = subprocess.Popen(args, cwd=os.path.expanduser('~'), stdin=subprocess.DEVNULL, start_new_session=True)
|
|
130
|
+
self.child_pid = self.child.pid
|
|
131
|
+
self.started = time.time()
|
|
132
|
+
self.pairing = bool(code)
|
|
133
|
+
except OSError:
|
|
134
|
+
self.child = None
|
|
135
|
+
self.backoff()
|
|
136
|
+
|
|
137
|
+
def on_exit(self, rc):
|
|
138
|
+
secs = time.time() - self.started
|
|
139
|
+
self.child = None
|
|
140
|
+
was_pairing, self.pairing = self.pairing, False
|
|
141
|
+
if was_pairing and not os.path.exists(CREDS):
|
|
142
|
+
notify(self.t('pairFailed'))
|
|
143
|
+
return
|
|
144
|
+
if rc == 0:
|
|
145
|
+
# 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.
|
|
146
|
+
if secs < 30:
|
|
147
|
+
self.backoff()
|
|
148
|
+
else:
|
|
149
|
+
self.delay = FIRST_DELAY
|
|
150
|
+
self.next_start = time.time() + FIRST_DELAY
|
|
151
|
+
elif rc == RESTART_CODE:
|
|
152
|
+
self.next_start = time.time() + 1
|
|
153
|
+
else:
|
|
154
|
+
if secs > 300:
|
|
155
|
+
self.delay = FIRST_DELAY
|
|
156
|
+
self.backoff()
|
|
157
|
+
|
|
158
|
+
def kill(self):
|
|
159
|
+
c, self.child = self.child, None
|
|
160
|
+
if c is None or c.poll() is not None:
|
|
161
|
+
return
|
|
162
|
+
try:
|
|
163
|
+
os.killpg(c.pid, signal.SIGTERM)
|
|
164
|
+
c.wait(timeout=10)
|
|
165
|
+
except (OSError, subprocess.TimeoutExpired):
|
|
166
|
+
try:
|
|
167
|
+
os.killpg(c.pid, signal.SIGKILL)
|
|
168
|
+
except OSError:
|
|
169
|
+
pass
|
|
170
|
+
|
|
171
|
+
def start_now(self):
|
|
172
|
+
if self.child is None and not self.lock_held():
|
|
173
|
+
self.next_start = 0
|
|
174
|
+
self.delay = FIRST_DELAY
|
|
175
|
+
|
|
176
|
+
def tick(self):
|
|
177
|
+
if mtime(QUIT_REQ) > self.seen_quit:
|
|
178
|
+
self.seen_quit = mtime(QUIT_REQ)
|
|
179
|
+
if self.on_quit:
|
|
180
|
+
self.on_quit()
|
|
181
|
+
return
|
|
182
|
+
if self.child is not None:
|
|
183
|
+
rc = self.child.poll()
|
|
184
|
+
if rc is not None:
|
|
185
|
+
self.on_exit(rc)
|
|
186
|
+
paired = os.path.exists(CREDS)
|
|
187
|
+
if paired and not self.was_paired:
|
|
188
|
+
notify(self.t('connected'))
|
|
189
|
+
self.was_paired = paired
|
|
190
|
+
if self.child is None and paired and time.time() >= self.next_start and not self.lock_held():
|
|
191
|
+
self.start()
|
|
192
|
+
req = mtime(OPEN_REQ)
|
|
193
|
+
if req > self.seen_open:
|
|
194
|
+
self.seen_open = req
|
|
195
|
+
if self.on_open:
|
|
196
|
+
self.on_open()
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def startup_file(sup):
|
|
200
|
+
return sup.cfg['extra'].get('autostart')
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def startup_enabled(sup):
|
|
204
|
+
path = startup_file(sup)
|
|
205
|
+
try:
|
|
206
|
+
with open(path, encoding='utf-8') as f:
|
|
207
|
+
text = f.read()
|
|
208
|
+
except (OSError, TypeError):
|
|
209
|
+
return False
|
|
210
|
+
return 'Hidden=true' not in text and 'X-GNOME-Autostart-enabled=false' not in text
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def set_startup(sup, on):
|
|
214
|
+
path, copy = startup_file(sup), sup.cfg['extra'].get('autostart_copy')
|
|
215
|
+
if not path:
|
|
216
|
+
return
|
|
217
|
+
try:
|
|
218
|
+
if on:
|
|
219
|
+
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
220
|
+
shutil.copyfile(copy, path)
|
|
221
|
+
elif os.path.exists(path):
|
|
222
|
+
with open(path, encoding='utf-8') as f:
|
|
223
|
+
text = f.read()
|
|
224
|
+
text = text.replace('X-GNOME-Autostart-enabled=true', 'X-GNOME-Autostart-enabled=false').replace('Hidden=false', 'Hidden=true')
|
|
225
|
+
with open(path, 'w', encoding='utf-8') as f:
|
|
226
|
+
f.write(text)
|
|
227
|
+
except OSError:
|
|
228
|
+
pass
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def gtk_ui(sup):
|
|
232
|
+
"""Biểu tượng khay. None khi không có màn hình / GTK / AppIndicator (khi đó chạy không biểu tượng)."""
|
|
233
|
+
if not (os.environ.get('DISPLAY') or os.environ.get('WAYLAND_DISPLAY')):
|
|
234
|
+
return None
|
|
235
|
+
try:
|
|
236
|
+
import gi
|
|
237
|
+
gi.require_version('Gtk', '3.0')
|
|
238
|
+
gi.require_version('GdkPixbuf', '2.0')
|
|
239
|
+
try:
|
|
240
|
+
gi.require_version('AyatanaAppIndicator3', '0.1')
|
|
241
|
+
from gi.repository import AyatanaAppIndicator3 as AppIndicator
|
|
242
|
+
except (ValueError, ImportError):
|
|
243
|
+
gi.require_version('AppIndicator3', '0.1')
|
|
244
|
+
from gi.repository import AppIndicator3 as AppIndicator
|
|
245
|
+
from gi.repository import GdkPixbuf, GLib, Gtk
|
|
246
|
+
except (ImportError, ValueError):
|
|
247
|
+
return None
|
|
248
|
+
|
|
249
|
+
try:
|
|
250
|
+
pb = GdkPixbuf.Pixbuf.new_from_file(LOGO)
|
|
251
|
+
grey = pb.copy()
|
|
252
|
+
pb.saturate_and_pixelate(grey, 0.0, False)
|
|
253
|
+
grey.save(LOGO_GREY, 'png')
|
|
254
|
+
except Exception:
|
|
255
|
+
pass
|
|
256
|
+
|
|
257
|
+
t = sup.t
|
|
258
|
+
ind = AppIndicator.Indicator.new('aivin', LOGO, AppIndicator.IndicatorCategory.APPLICATION_STATUS)
|
|
259
|
+
ind.set_status(AppIndicator.IndicatorStatus.ACTIVE)
|
|
260
|
+
ind.set_title('AIVIN Bot')
|
|
261
|
+
menu = Gtk.Menu()
|
|
262
|
+
status = Gtk.MenuItem(label='AIVIN Bot')
|
|
263
|
+
status.set_sensitive(False)
|
|
264
|
+
connect = Gtk.MenuItem(label=t('connect'))
|
|
265
|
+
pause = Gtk.MenuItem(label=t('pause'))
|
|
266
|
+
startup = Gtk.CheckMenuItem(label=t('startup'))
|
|
267
|
+
quit_item = Gtk.MenuItem(label=t('quit'))
|
|
268
|
+
for item in (status, Gtk.SeparatorMenuItem(), connect, pause, startup, Gtk.SeparatorMenuItem(), quit_item):
|
|
269
|
+
menu.append(item)
|
|
270
|
+
state = {'icon': None, 'syncing': False, 'dialog': False}
|
|
271
|
+
|
|
272
|
+
def refresh():
|
|
273
|
+
paired, paused, up = os.path.exists(CREDS), os.path.exists(PAUSED), sup.running()
|
|
274
|
+
text = t('connecting' if sup.pairing else 'notPaired') if not paired else t('stopped') if not up else t('paused') if paused else t('running')
|
|
275
|
+
status.set_label(text)
|
|
276
|
+
icon = LOGO if paired and up and not paused else (LOGO_GREY if os.path.exists(LOGO_GREY) else LOGO)
|
|
277
|
+
if icon != state['icon']:
|
|
278
|
+
state['icon'] = icon
|
|
279
|
+
ind.set_icon_full(icon, 'Aivin - ' + text)
|
|
280
|
+
connect.set_visible(not paired)
|
|
281
|
+
pause.set_label(t('resume' if paused else 'pause'))
|
|
282
|
+
pause.set_visible(paired)
|
|
283
|
+
state['syncing'] = True
|
|
284
|
+
startup.set_active(startup_enabled(sup))
|
|
285
|
+
state['syncing'] = False
|
|
286
|
+
|
|
287
|
+
def ask_code(*_):
|
|
288
|
+
if state['dialog']:
|
|
289
|
+
return
|
|
290
|
+
state['dialog'] = True
|
|
291
|
+
d = Gtk.Dialog(title='AIVIN Bot')
|
|
292
|
+
d.add_buttons(t('pairCancel'), Gtk.ResponseType.CANCEL, t('pairOk'), Gtk.ResponseType.OK)
|
|
293
|
+
d.set_default_response(Gtk.ResponseType.OK)
|
|
294
|
+
d.set_keep_above(True)
|
|
295
|
+
box = d.get_content_area()
|
|
296
|
+
box.set_spacing(10)
|
|
297
|
+
box.set_border_width(14)
|
|
298
|
+
label = Gtk.Label(label=t('pairPrompt'))
|
|
299
|
+
label.set_line_wrap(True)
|
|
300
|
+
label.set_max_width_chars(48)
|
|
301
|
+
entry = Gtk.Entry()
|
|
302
|
+
entry.set_activates_default(True)
|
|
303
|
+
box.pack_start(label, False, False, 0)
|
|
304
|
+
box.pack_start(entry, False, False, 0)
|
|
305
|
+
d.show_all()
|
|
306
|
+
response = d.run()
|
|
307
|
+
code = entry.get_text().strip().upper()
|
|
308
|
+
d.destroy()
|
|
309
|
+
state['dialog'] = False
|
|
310
|
+
if response == Gtk.ResponseType.OK and code:
|
|
311
|
+
sup.kill()
|
|
312
|
+
sup.start(code)
|
|
313
|
+
refresh()
|
|
314
|
+
|
|
315
|
+
def toggle_pause(*_):
|
|
316
|
+
try:
|
|
317
|
+
if os.path.exists(PAUSED):
|
|
318
|
+
os.remove(PAUSED)
|
|
319
|
+
else:
|
|
320
|
+
with open(PAUSED, 'w') as f:
|
|
321
|
+
f.write(time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()))
|
|
322
|
+
except OSError:
|
|
323
|
+
pass
|
|
324
|
+
refresh()
|
|
325
|
+
|
|
326
|
+
def toggle_startup(item):
|
|
327
|
+
if not state['syncing']:
|
|
328
|
+
set_startup(sup, item.get_active())
|
|
329
|
+
refresh()
|
|
330
|
+
|
|
331
|
+
def quit_app(*_):
|
|
332
|
+
sup.kill()
|
|
333
|
+
Gtk.main_quit()
|
|
334
|
+
|
|
335
|
+
def open_aivin():
|
|
336
|
+
if not os.path.exists(CREDS):
|
|
337
|
+
ask_code()
|
|
338
|
+
else:
|
|
339
|
+
sup.start_now()
|
|
340
|
+
notify(t('running') if sup.running() else t('stopped'))
|
|
341
|
+
|
|
342
|
+
connect.connect('activate', ask_code)
|
|
343
|
+
pause.connect('activate', toggle_pause)
|
|
344
|
+
startup.connect('toggled', toggle_startup)
|
|
345
|
+
quit_item.connect('activate', quit_app)
|
|
346
|
+
menu.show_all()
|
|
347
|
+
ind.set_menu(menu)
|
|
348
|
+
sup.on_open = open_aivin
|
|
349
|
+
sup.on_quit = quit_app
|
|
350
|
+
|
|
351
|
+
def on_tick():
|
|
352
|
+
sup.tick()
|
|
353
|
+
refresh()
|
|
354
|
+
return True
|
|
355
|
+
|
|
356
|
+
GLib.timeout_add_seconds(2, on_tick)
|
|
357
|
+
refresh()
|
|
358
|
+
|
|
359
|
+
class Ui:
|
|
360
|
+
open = staticmethod(open_aivin)
|
|
361
|
+
|
|
362
|
+
@staticmethod
|
|
363
|
+
def run():
|
|
364
|
+
Gtk.main()
|
|
365
|
+
return Ui
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def main(argv):
|
|
369
|
+
import fcntl
|
|
370
|
+
os.makedirs(DESK, exist_ok=True)
|
|
371
|
+
lock = open(TRAY_LOCK, 'w')
|
|
372
|
+
try:
|
|
373
|
+
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
374
|
+
except OSError:
|
|
375
|
+
# Đã có một bản chạy: mở lần nữa = mở Aivin; --quit = bảo bản đó thoát.
|
|
376
|
+
if '--quit' in argv:
|
|
377
|
+
touch(QUIT_REQ)
|
|
378
|
+
elif '--autostart' not in argv and '--installed' not in argv:
|
|
379
|
+
touch(OPEN_REQ)
|
|
380
|
+
return 0
|
|
381
|
+
if '--quit' in argv:
|
|
382
|
+
return 0
|
|
383
|
+
sup = Supervisor()
|
|
384
|
+
signal.signal(signal.SIGTERM, lambda *_: (sup.kill(), sys.exit(0)))
|
|
385
|
+
ui = gtk_ui(sup)
|
|
386
|
+
sup.tick()
|
|
387
|
+
if ui is None:
|
|
388
|
+
stop = []
|
|
389
|
+
sup.on_quit = lambda: (sup.kill(), stop.append(True))
|
|
390
|
+
while not stop:
|
|
391
|
+
time.sleep(2)
|
|
392
|
+
sup.tick()
|
|
393
|
+
return 0
|
|
394
|
+
if '--installed' in argv:
|
|
395
|
+
notify(sup.t('firstRun' if sup.was_paired else 'connectHint'))
|
|
396
|
+
elif '--autostart' not in argv:
|
|
397
|
+
ui.open()
|
|
398
|
+
ui.run()
|
|
399
|
+
return 0
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
if __name__ == '__main__':
|
|
403
|
+
sys.exit(main(sys.argv[1:]))
|
|
404
|
+
`;
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Kịch bản mở Aivin (menu ứng dụng, autostart). Có python3 → khay; không thì tự lặp giữ daemon (chỉ khi đã ghép máy — chưa ghép thì
|
|
408
|
+
* daemon không tự kết nối được, chạy lặp vô ích). `@@…@@` thay bằng đường dẫn đã bọc nháy đơn.
|
|
409
|
+
*/
|
|
410
|
+
export const LINUX_LAUNCHER_SH = `#!/bin/sh
|
|
411
|
+
# Aivin desktop app (Linux): tray icon + keeps \`aivin remote start\` running (tray.py); without python3, keeps it running without an icon.
|
|
412
|
+
exec >>@@LOG@@ 2>&1
|
|
413
|
+
if command -v python3 >/dev/null 2>&1; then exec python3 @@TRAY@@ "$@"; fi
|
|
414
|
+
case " $* " in *" --quit "*) exit 0 ;; esac
|
|
415
|
+
delay=10
|
|
416
|
+
while [ -f @@CREDS@@ ]; do
|
|
417
|
+
started=$(date +%s)
|
|
418
|
+
/bin/sh @@RUN@@
|
|
419
|
+
rc=$?
|
|
420
|
+
[ "$rc" -eq 0 ] && exit 0
|
|
421
|
+
if [ "$rc" -eq 75 ]; then sleep 1; continue; fi
|
|
422
|
+
[ $(( $(date +%s) - started )) -gt 300 ] && delay=10
|
|
423
|
+
sleep "$delay"
|
|
424
|
+
delay=$(( delay * 2 ))
|
|
425
|
+
[ "$delay" -gt 300 ] && delay=300
|
|
426
|
+
done
|
|
427
|
+
`;
|