@aivin-labs/cli 1.0.5 → 1.0.11
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 +288 -216
- package/bin/assets/aivin-mascot.png +0 -0
- package/bin/cli.mjs +551 -5
- package/bin/lib/browserAutomation.mjs +384 -614
- package/bin/lib/browserTui.mjs +276 -251
- package/bin/lib/cancellation.mjs +61 -0
- package/bin/lib/localBrowser.mjs +345 -0
- package/bin/lib/mcpProxy.mjs +869 -837
- package/bin/lib/missionStream.mjs +696 -691
- package/bin/lib/releaseKeys.mjs +9 -0
- package/bin/lib/remote/activityLog.mjs +68 -0
- package/bin/lib/remote/autoStart.mjs +617 -0
- package/bin/lib/remote/autoUpdate.mjs +129 -0
- package/bin/lib/remote/brand.mjs +60 -0
- package/bin/lib/remote/browserTunnel.mjs +403 -0
- package/bin/lib/remote/callLedger.mjs +134 -0
- package/bin/lib/remote/cdpPolicy.mjs +79 -0
- package/bin/lib/remote/client.mjs +808 -0
- package/bin/lib/remote/clipboard.mjs +138 -0
- package/bin/lib/remote/codingAgents.mjs +293 -0
- package/bin/lib/remote/constants.mjs +234 -0
- package/bin/lib/remote/credentials.mjs +208 -0
- package/bin/lib/remote/daemonLock.mjs +86 -0
- package/bin/lib/remote/dailyBrowser.mjs +371 -0
- package/bin/lib/remote/desktop/index.mjs +272 -0
- package/bin/lib/remote/desktop/linux.mjs +144 -0
- package/bin/lib/remote/desktop/linuxTraySource.mjs +427 -0
- package/bin/lib/remote/desktop/mac.mjs +187 -0
- package/bin/lib/remote/desktop/macTraySource.mjs +292 -0
- package/bin/lib/remote/desktop/strings.mjs +74 -0
- package/bin/lib/remote/desktop/traySource.mjs +527 -0
- package/bin/lib/remote/deviceMemory.mjs +246 -0
- package/bin/lib/remote/deviceProfile.mjs +142 -0
- package/bin/lib/remote/deviceWatches.mjs +138 -0
- package/bin/lib/remote/docText.mjs +117 -0
- package/bin/lib/remote/fileIndexer.mjs +399 -0
- package/bin/lib/remote/gui/index.mjs +326 -0
- package/bin/lib/remote/gui/officeHostSource.mjs +393 -0
- package/bin/lib/remote/gui/windowsHost.mjs +109 -0
- package/bin/lib/remote/gui/windowsHostSource.mjs +1156 -0
- package/bin/lib/remote/httpTransfer.mjs +89 -0
- package/bin/lib/remote/idempotency.mjs +135 -0
- package/bin/lib/remote/keepAwake.mjs +100 -0
- package/bin/lib/remote/localConfirm.mjs +268 -0
- package/bin/lib/remote/localPolicy.mjs +256 -0
- package/bin/lib/remote/mascot/adpcm.mjs +70 -0
- package/bin/lib/remote/mascot/ask.mjs +768 -0
- package/bin/lib/remote/mascot/askHostSource.mjs +512 -0
- package/bin/lib/remote/mascot/assets.mjs +827 -0
- package/bin/lib/remote/mascot/bot.mjs +372 -0
- package/bin/lib/remote/mascot/hostSource.mjs +759 -0
- package/bin/lib/remote/mascot/hotkey.mjs +66 -0
- package/bin/lib/remote/mascot/index.mjs +493 -0
- package/bin/lib/remote/mascot/labels.mjs +82 -0
- package/bin/lib/remote/mascot/rigSource.mjs +609 -0
- package/bin/lib/remote/mascot/strings.mjs +375 -0
- package/bin/lib/remote/mascot/ui.mjs +104 -0
- package/bin/lib/remote/mascot/voiceSource.mjs +584 -0
- package/bin/lib/remote/meeting/index.mjs +415 -0
- package/bin/lib/remote/meeting/recorder.mjs +109 -0
- package/bin/lib/remote/meeting/recorderSource.mjs +394 -0
- package/bin/lib/remote/meeting/transcribe.mjs +259 -0
- package/bin/lib/remote/memberFsPool.mjs +134 -0
- package/bin/lib/remote/memberFsWorker.mjs +60 -0
- package/bin/lib/remote/mission.mjs +156 -0
- package/bin/lib/remote/missionQuestion.mjs +63 -0
- package/bin/lib/remote/multiUser.mjs +344 -0
- package/bin/lib/remote/netProxy.mjs +145 -0
- package/bin/lib/remote/notify.mjs +58 -0
- package/bin/lib/remote/office/index.mjs +90 -0
- package/bin/lib/remote/policyCommand.mjs +93 -0
- package/bin/lib/remote/processes.mjs +217 -0
- package/bin/lib/remote/sandbox.mjs +221 -0
- package/bin/lib/remote/schedule.mjs +213 -0
- package/bin/lib/remote/scheduler.mjs +124 -0
- package/bin/lib/remote/tabGroups.mjs +209 -0
- package/bin/lib/remote/textIndex.mjs +131 -0
- package/bin/lib/remote/tokenStore.mjs +77 -0
- package/bin/lib/remote/toolHandlers.mjs +741 -0
- package/bin/lib/remote/undoStore.mjs +242 -0
- package/bin/lib/remoteControl.mjs +26 -0
- package/bin/lib/scaffold.mjs +654 -653
- package/bin/lib/selfUpdate.mjs +71 -0
- package/bin/lib/update/binarySwap.mjs +63 -0
- package/bin/lib/update/constants.mjs +56 -0
- package/bin/lib/update/download.mjs +145 -0
- package/bin/lib/update/signature.mjs +42 -0
- package/bin/lib/update/state.mjs +205 -0
- package/bin/lib/update/versions.mjs +60 -0
- package/docs/AIVIN_MASCOT.md +116 -0
- package/docs/CLI.md +1484 -1299
- package/docs/media/aivin-mascot-states.mp4 +0 -0
- package/package.json +71 -62
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import os from 'os';
|
|
3
|
+
import { posix as path } from 'path';
|
|
4
|
+
import { execFileSync, spawn } from 'child_process';
|
|
5
|
+
import { REMOTE_CONFIG_DIR } from '../constants.mjs';
|
|
6
|
+
import { AIVIN_LOGO_PNG_BASE64 } from '../brand.mjs';
|
|
7
|
+
import { DESKTOP_DIR, LAUNCH_CONFIG_FILE, TrayLaunch, launchConfigText } from './index.mjs';
|
|
8
|
+
import { MAC_TRAY_JS } from './macTraySource.mjs';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Aivin trên macOS (29/10/2026): gói ~/Applications/Aivin.app (Spotlight / Launchpad tìm được "Aivin") chạy biểu tượng thanh menu
|
|
12
|
+
* (macTraySource.mjs); LaunchAgent com.aivin.remote mở app lúc đăng nhập — AssociatedBundleIdentifiers để System Settings → General →
|
|
13
|
+
* Login Items hiện "Aivin" kèm biểu tượng (không phải "sh") và bật / tắt được ở đó. Nhãn giữ com.aivin.remote như trước (detectSupervisor
|
|
14
|
+
* nhận ra tiến trình cũ theo nhãn), cài đè lên LaunchAgent cũ chạy thẳng aivin-remote-run.sh.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export const MAC_BUNDLE_ID = 'vn.aivin.desktop';
|
|
18
|
+
export const MAC_AGENT_LABEL = 'com.aivin.remote';
|
|
19
|
+
|
|
20
|
+
export const macAppPath = (home = os.homedir()) => path.join(home, 'Applications', 'Aivin.app');
|
|
21
|
+
export const macAppExecutable = (app = macAppPath()) => path.join(app, 'Contents', 'MacOS', 'Aivin');
|
|
22
|
+
export const macAgentPath = (home = os.homedir()) => path.join(home, 'Library', 'LaunchAgents', `${MAC_AGENT_LABEL}.plist`);
|
|
23
|
+
/** Bản sao LaunchAgent: menu "Mở khi đăng nhập" chép lại vào ~/Library/LaunchAgents khi bật lại. */
|
|
24
|
+
const AGENT_COPY_FILE = path.join(DESKTOP_DIR, `${MAC_AGENT_LABEL}.plist`);
|
|
25
|
+
|
|
26
|
+
const xml = (s) => String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
27
|
+
|
|
28
|
+
/** .icns chứa nguyên ảnh PNG 64×64 (icp6 = 64 px, ic11 = 32 px @2x) — macOS 10.8+. */
|
|
29
|
+
export function pngToIcns(png) {
|
|
30
|
+
const entries = ['icp6', 'ic11'].map((type) => {
|
|
31
|
+
const head = Buffer.alloc(8);
|
|
32
|
+
head.write(type, 0, 'ascii');
|
|
33
|
+
head.writeUInt32BE(png.length + 8, 4);
|
|
34
|
+
return Buffer.concat([head, png]);
|
|
35
|
+
});
|
|
36
|
+
const body = Buffer.concat(entries);
|
|
37
|
+
const head = Buffer.alloc(8);
|
|
38
|
+
head.write('icns', 0, 'ascii');
|
|
39
|
+
head.writeUInt32BE(body.length + 8, 4);
|
|
40
|
+
return Buffer.concat([head, body]);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** LSUIElement: không có biểu tượng trên Dock, chỉ ở thanh menu. */
|
|
44
|
+
export function macInfoPlist() {
|
|
45
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
46
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
47
|
+
<plist version="1.0">
|
|
48
|
+
<dict>
|
|
49
|
+
<key>CFBundleName</key>
|
|
50
|
+
<string>AIVIN Bot CLI</string>
|
|
51
|
+
<key>CFBundleDisplayName</key>
|
|
52
|
+
<string>AIVIN Bot CLI</string>
|
|
53
|
+
<key>CFBundleIdentifier</key>
|
|
54
|
+
<string>${MAC_BUNDLE_ID}</string>
|
|
55
|
+
<key>CFBundleExecutable</key>
|
|
56
|
+
<string>Aivin</string>
|
|
57
|
+
<key>CFBundleIconFile</key>
|
|
58
|
+
<string>aivin</string>
|
|
59
|
+
<key>CFBundlePackageType</key>
|
|
60
|
+
<string>APPL</string>
|
|
61
|
+
<key>CFBundleShortVersionString</key>
|
|
62
|
+
<string>1.0.0</string>
|
|
63
|
+
<key>CFBundleVersion</key>
|
|
64
|
+
<string>1</string>
|
|
65
|
+
<key>LSUIElement</key>
|
|
66
|
+
<true/>
|
|
67
|
+
<key>LSMinimumSystemVersion</key>
|
|
68
|
+
<string>10.13</string>
|
|
69
|
+
<key>NSHighResolutionCapable</key>
|
|
70
|
+
<true/>
|
|
71
|
+
</dict>
|
|
72
|
+
</plist>
|
|
73
|
+
`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export const MAC_APP_LAUNCHER = `#!/bin/sh
|
|
77
|
+
# Aivin menu bar app: the menu bar icon + keeps \`aivin remote start\` running (Resources/tray.js, JavaScript for Automation).
|
|
78
|
+
exec /usr/bin/osascript -l JavaScript "$(dirname "$0")/../Resources/tray.js" "$@"
|
|
79
|
+
`;
|
|
80
|
+
|
|
81
|
+
/** LaunchAgent: mở app lúc đăng nhập (chỉ phiên có màn hình), mở lại khi app sập (thoát khác 0); Thoát ở menu = mã 0 → không mở lại. */
|
|
82
|
+
export function macAgentPlist({ app = macAppPath(), logFile = path.join(REMOTE_CONFIG_DIR, 'remote-service.log') } = {}) {
|
|
83
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
84
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
85
|
+
<plist version="1.0">
|
|
86
|
+
<dict>
|
|
87
|
+
<key>Label</key>
|
|
88
|
+
<string>${MAC_AGENT_LABEL}</string>
|
|
89
|
+
<key>ProgramArguments</key>
|
|
90
|
+
<array>
|
|
91
|
+
<string>${xml(macAppExecutable(app))}</string>
|
|
92
|
+
<string>--autostart</string>
|
|
93
|
+
</array>
|
|
94
|
+
<key>AssociatedBundleIdentifiers</key>
|
|
95
|
+
<array>
|
|
96
|
+
<string>${MAC_BUNDLE_ID}</string>
|
|
97
|
+
</array>
|
|
98
|
+
<key>RunAtLoad</key>
|
|
99
|
+
<true/>
|
|
100
|
+
<key>KeepAlive</key>
|
|
101
|
+
<dict>
|
|
102
|
+
<key>SuccessfulExit</key>
|
|
103
|
+
<false/>
|
|
104
|
+
</dict>
|
|
105
|
+
<key>ThrottleInterval</key>
|
|
106
|
+
<integer>10</integer>
|
|
107
|
+
<key>LimitLoadToSessionType</key>
|
|
108
|
+
<string>Aqua</string>
|
|
109
|
+
<key>ProcessType</key>
|
|
110
|
+
<string>Interactive</string>
|
|
111
|
+
<key>StandardOutPath</key>
|
|
112
|
+
<string>${xml(logFile)}</string>
|
|
113
|
+
<key>StandardErrorPath</key>
|
|
114
|
+
<string>${xml(logFile)}</string>
|
|
115
|
+
</dict>
|
|
116
|
+
</plist>
|
|
117
|
+
`;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function macTraySource(home = REMOTE_CONFIG_DIR) {
|
|
121
|
+
return MAC_TRAY_JS.replace('@@HOME@@', JSON.stringify(home));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Dựng / làm mới Aivin.app (ghi đè cả gói: mã + biểu tượng đổi theo bản CLI), báo LaunchServices để Spotlight thấy ngay. */
|
|
125
|
+
export function writeMacApp({ app = macAppPath(), home = REMOTE_CONFIG_DIR } = {}) {
|
|
126
|
+
const contents = path.join(app, 'Contents');
|
|
127
|
+
fs.mkdirSync(path.join(contents, 'MacOS'), { recursive: true });
|
|
128
|
+
fs.mkdirSync(path.join(contents, 'Resources'), { recursive: true });
|
|
129
|
+
fs.writeFileSync(path.join(contents, 'Info.plist'), macInfoPlist());
|
|
130
|
+
fs.writeFileSync(path.join(contents, 'Resources', 'tray.js'), macTraySource(home));
|
|
131
|
+
fs.writeFileSync(path.join(contents, 'Resources', 'aivin.icns'), pngToIcns(Buffer.from(AIVIN_LOGO_PNG_BASE64, 'base64')));
|
|
132
|
+
const exe = macAppExecutable(app);
|
|
133
|
+
fs.writeFileSync(exe, MAC_APP_LAUNCHER, { mode: 0o755 });
|
|
134
|
+
fs.chmodSync(exe, 0o755);
|
|
135
|
+
const lsregister = '/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister';
|
|
136
|
+
try { execFileSync(lsregister, ['-f', app], { stdio: 'ignore', timeout: 15_000 }); } catch { /* Spotlight tự lập chỉ mục sau */ }
|
|
137
|
+
return app;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function macStartupState() {
|
|
141
|
+
const registered = fs.existsSync(macAgentPath());
|
|
142
|
+
return { registered, enabled: registered };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function setMacStartup(enabled) {
|
|
146
|
+
if (enabled) fs.copyFileSync(AGENT_COPY_FILE, macAgentPath());
|
|
147
|
+
else fs.rmSync(macAgentPath(), { force: true });
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** `open` qua LaunchServices: app độc lập với cửa sổ terminal; đang chạy thì macOS gửi "mở lại" → app mở Aivin. */
|
|
151
|
+
export function launchMacTray({ app = macAppPath(), mode = TrayLaunch.OPEN } = {}) {
|
|
152
|
+
const args = mode === TrayLaunch.OPEN ? [app] : [app, '--args', `--${mode}`];
|
|
153
|
+
spawn('/usr/bin/open', args, { detached: true, stdio: 'ignore' }).unref();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** `runScript` = aivin-remote-run.sh (autoStart.mjs ghi, dấu supervisor = tray) — app chạy nó, kèm `--code` khi kết nối bằng mã. */
|
|
157
|
+
export function installMacDesktop({ runScript, lang = 'en', autostart = true, launch = true, launchMode = TrayLaunch.INSTALLED }) {
|
|
158
|
+
fs.mkdirSync(DESKTOP_DIR, { recursive: true });
|
|
159
|
+
const agent = macAgentPath();
|
|
160
|
+
fs.writeFileSync(LAUNCH_CONFIG_FILE, launchConfigText({ argv: ['/bin/sh', runScript], lang, platform: 'darwin', extra: { agent, agent_copy: AGENT_COPY_FILE } }));
|
|
161
|
+
fs.writeFileSync(path.join(DESKTOP_DIR, 'logo.png'), Buffer.from(AIVIN_LOGO_PNG_BASE64, 'base64'));
|
|
162
|
+
const app = writeMacApp();
|
|
163
|
+
fs.writeFileSync(AGENT_COPY_FILE, macAgentPlist({ app }));
|
|
164
|
+
if (autostart) {
|
|
165
|
+
fs.mkdirSync(path.dirname(agent), { recursive: true });
|
|
166
|
+
setMacStartup(true);
|
|
167
|
+
}
|
|
168
|
+
if (launch) launchMacTray({ app, mode: launchMode });
|
|
169
|
+
return { exe: app, shortcut: app, built: true, autostart: macStartupState().enabled };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function isMacDesktopInstalled() {
|
|
173
|
+
return fs.existsSync(macAppExecutable()) && fs.existsSync(LAUNCH_CONFIG_FILE);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Gỡ: bảo app thoát (dừng daemon nó giữ), xoá LaunchAgent, Aivin.app, desktop\. */
|
|
177
|
+
export function uninstallMacDesktop({ wait = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms) } = {}) {
|
|
178
|
+
const exe = macAppExecutable();
|
|
179
|
+
if (fs.existsSync(exe)) {
|
|
180
|
+
try { execFileSync(exe, ['--quit'], { stdio: 'ignore', timeout: 15_000 }); } catch { /* không chạy */ }
|
|
181
|
+
wait(3000);
|
|
182
|
+
}
|
|
183
|
+
fs.rmSync(macAgentPath(), { force: true });
|
|
184
|
+
fs.rmSync(macAppPath(), { recursive: true, force: true });
|
|
185
|
+
fs.rmSync(DESKTOP_DIR, { recursive: true, force: true });
|
|
186
|
+
return { removed: !fs.existsSync(macAppPath()) };
|
|
187
|
+
}
|
|
@@ -0,0 +1,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 CLI' }); } 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
|
+
var child = null, childPid = 0, childStarted = 0, nextStart = 0, delay = 10, pairing = false, quitting = false;
|
|
83
|
+
var wasPaired = exists(CREDS);
|
|
84
|
+
var seenOpen = mtime(OPEN_REQ), seenQuit = mtime(QUIT_REQ);
|
|
85
|
+
|
|
86
|
+
/** 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á). */
|
|
87
|
+
function lockHeld() {
|
|
88
|
+
var s = read(LOCK);
|
|
89
|
+
if (!s) return false;
|
|
90
|
+
try {
|
|
91
|
+
var d = JSON.parse(s);
|
|
92
|
+
if (Number(d.pid) === childPid && !child) return false;
|
|
93
|
+
var at = Date.parse(d.beat_at || d.started_at || '');
|
|
94
|
+
return !isFinite(at) || Date.now() - at < 90000;
|
|
95
|
+
} catch (e) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function backoff() {
|
|
101
|
+
nextStart = Date.now() + delay * 1000;
|
|
102
|
+
delay = Math.min(delay * 2, 300);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function startChild(code) {
|
|
106
|
+
cfg = loadCfg();
|
|
107
|
+
if (!cfg.exe) return;
|
|
108
|
+
var args = cfg.args.slice();
|
|
109
|
+
if (code) args.push('--code', code);
|
|
110
|
+
try {
|
|
111
|
+
var t = task(cfg.exe, args);
|
|
112
|
+
t.currentDirectoryPath = $.NSHomeDirectory();
|
|
113
|
+
t.launch;
|
|
114
|
+
child = t;
|
|
115
|
+
childPid = t.processIdentifier;
|
|
116
|
+
childStarted = Date.now();
|
|
117
|
+
pairing = !!code;
|
|
118
|
+
} catch (e) {
|
|
119
|
+
child = null;
|
|
120
|
+
backoff();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function onChildExit() {
|
|
125
|
+
var rc = child.terminationStatus;
|
|
126
|
+
var secs = (Date.now() - childStarted) / 1000;
|
|
127
|
+
child = null;
|
|
128
|
+
var wasPairing = pairing;
|
|
129
|
+
pairing = false;
|
|
130
|
+
if (wasPairing && !exists(CREDS)) { notify(T('pairFailed')); return; }
|
|
131
|
+
if (rc === 0) { nextStart = Date.now() + 60000; return; }
|
|
132
|
+
if (rc === RESTART_CODE) { nextStart = Date.now() + 1000; return; }
|
|
133
|
+
if (secs > 300) delay = 10;
|
|
134
|
+
backoff();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function killChild() {
|
|
138
|
+
var c = child;
|
|
139
|
+
child = null;
|
|
140
|
+
if (!c || !c.isRunning) return;
|
|
141
|
+
// aivin-remote-run.sh chờ daemon (không exec) — dừng con của nó trước rồi tới nó.
|
|
142
|
+
runWait('/usr/bin/pkill', ['-TERM', '-P', String(c.processIdentifier)]);
|
|
143
|
+
try { c.terminate; } catch (e) {}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function running() { return (child && child.isRunning) || lockHeld(); }
|
|
147
|
+
|
|
148
|
+
function tick() {
|
|
149
|
+
if (quitting) return;
|
|
150
|
+
if (mtime(QUIT_REQ) > seenQuit) { quit(); return; }
|
|
151
|
+
if (child && !child.isRunning) onChildExit();
|
|
152
|
+
var paired = exists(CREDS);
|
|
153
|
+
if (paired && !wasPaired) notify(T('connected'));
|
|
154
|
+
wasPaired = paired;
|
|
155
|
+
if (!child && paired && Date.now() >= nextStart && !lockHeld()) startChild(null);
|
|
156
|
+
var req = mtime(OPEN_REQ);
|
|
157
|
+
if (req > seenOpen) { seenOpen = req; openAivin(); }
|
|
158
|
+
refresh();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// --- Menu --------------------------------------------------------------------------------------------------------------------------
|
|
162
|
+
|
|
163
|
+
var app = $.NSApplication.sharedApplication;
|
|
164
|
+
var item, menu, statusItem, openItem, pauseItem, startupItem, target;
|
|
165
|
+
|
|
166
|
+
function startupEnabled() { return !!cfg.extra.agent && exists(cfg.extra.agent); }
|
|
167
|
+
|
|
168
|
+
function setStartup(on) {
|
|
169
|
+
var agent = cfg.extra.agent, copy = cfg.extra.agent_copy;
|
|
170
|
+
if (!agent) return;
|
|
171
|
+
if (on) {
|
|
172
|
+
if (copy && exists(copy) && !exists(agent)) fm.copyItemAtPathToPathError(copy, agent, null);
|
|
173
|
+
} else {
|
|
174
|
+
remove(agent);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function refresh() {
|
|
179
|
+
var paired = exists(CREDS), paused = exists(PAUSED), up = running();
|
|
180
|
+
var state = !paired ? T(pairing ? 'connecting' : 'notPaired') : !up ? T('stopped') : paused ? T('paused') : T('running');
|
|
181
|
+
statusItem.title = state;
|
|
182
|
+
item.button.toolTip = 'Aivin - ' + state;
|
|
183
|
+
item.button.appearsDisabled = !(paired && up && !paused);
|
|
184
|
+
openItem.title = T(paired ? 'open' : 'connect');
|
|
185
|
+
openItem.hidden = paired;
|
|
186
|
+
pauseItem.title = T(paused ? 'resume' : 'pause');
|
|
187
|
+
pauseItem.hidden = !paired;
|
|
188
|
+
startupItem.state = startupEnabled() ? 1 : 0;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function askCode() {
|
|
192
|
+
app.activateIgnoringOtherApps(true);
|
|
193
|
+
var r;
|
|
194
|
+
try {
|
|
195
|
+
r = sa.displayDialog(T('pairPrompt'), {
|
|
196
|
+
defaultAnswer: '', buttons: [T('pairCancel'), T('pairOk')], defaultButton: T('pairOk'), cancelButton: T('pairCancel'), withTitle: 'AIVIN Bot CLI',
|
|
197
|
+
});
|
|
198
|
+
} catch (e) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
var code = String(r.textReturned || '').trim().toUpperCase();
|
|
202
|
+
if (!code || quitting) return;
|
|
203
|
+
killChild();
|
|
204
|
+
startChild(code);
|
|
205
|
+
refresh();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** 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. */
|
|
209
|
+
function openAivin() {
|
|
210
|
+
if (quitting) return;
|
|
211
|
+
if (!exists(CREDS)) { askCode(); return; }
|
|
212
|
+
if (!child && !lockHeld()) { nextStart = 0; delay = 10; tick(); }
|
|
213
|
+
try { item.button.performClick(null); } catch (e) {}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function togglePause() {
|
|
217
|
+
if (exists(PAUSED)) remove(PAUSED);
|
|
218
|
+
else write(PAUSED, new Date().toISOString());
|
|
219
|
+
refresh();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function quit() {
|
|
223
|
+
if (quitting) return;
|
|
224
|
+
quitting = true;
|
|
225
|
+
killChild();
|
|
226
|
+
remove(PIDFILE);
|
|
227
|
+
$.exit(0);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function addItem(title, action) {
|
|
231
|
+
var mi = $.NSMenuItem.alloc.initWithTitleActionKeyEquivalent(title, action, '');
|
|
232
|
+
if (action) mi.target = target;
|
|
233
|
+
menu.addItem(mi);
|
|
234
|
+
return mi;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function buildUi() {
|
|
238
|
+
ObjC.registerSubclass({
|
|
239
|
+
name: 'AivinTrayTarget',
|
|
240
|
+
methods: {
|
|
241
|
+
'onOpen:': { types: ['void', ['id']], implementation: function () { openAivin(); } },
|
|
242
|
+
'onPause:': { types: ['void', ['id']], implementation: function () { togglePause(); } },
|
|
243
|
+
'onStartup:': { types: ['void', ['id']], implementation: function () { setStartup(!startupEnabled()); refresh(); } },
|
|
244
|
+
'onFolder:': { types: ['void', ['id']], implementation: function () { task('/usr/bin/open', [HOME]).launch; } },
|
|
245
|
+
'onQuit:': { types: ['void', ['id']], implementation: function () { quit(); } },
|
|
246
|
+
'tick:': { types: ['void', ['id']], implementation: function () { try { tick(); } catch (e) { console.log('aivin tray: ' + e); } } },
|
|
247
|
+
'applicationShouldHandleReopen:hasVisibleWindows:': { types: ['bool', ['id', 'bool']], implementation: function () { openAivin(); return false; } },
|
|
248
|
+
},
|
|
249
|
+
});
|
|
250
|
+
target = $.AivinTrayTarget.alloc.init;
|
|
251
|
+
app.setActivationPolicy($.NSApplicationActivationPolicyAccessory);
|
|
252
|
+
app.delegate = target;
|
|
253
|
+
|
|
254
|
+
item = $.NSStatusBar.systemStatusBar.statusItemWithLength($.NSVariableStatusItemLength);
|
|
255
|
+
var img = $.NSImage.alloc.initWithContentsOfFile(LOGO);
|
|
256
|
+
if (img.isNil()) item.button.title = 'AIVIN Bot CLI';
|
|
257
|
+
else { img.size = $.NSMakeSize(18, 18); item.button.image = img; }
|
|
258
|
+
|
|
259
|
+
menu = $.NSMenu.alloc.init;
|
|
260
|
+
menu.autoenablesItems = false;
|
|
261
|
+
statusItem = addItem('AIVIN Bot CLI', null);
|
|
262
|
+
statusItem.enabled = false;
|
|
263
|
+
menu.addItem($.NSMenuItem.separatorItem);
|
|
264
|
+
openItem = addItem(T('connect'), 'onOpen:');
|
|
265
|
+
pauseItem = addItem(T('pause'), 'onPause:');
|
|
266
|
+
startupItem = addItem(T('startup'), 'onStartup:');
|
|
267
|
+
addItem(T('folder'), 'onFolder:');
|
|
268
|
+
menu.addItem($.NSMenuItem.separatorItem);
|
|
269
|
+
addItem(T('quit'), 'onQuit:');
|
|
270
|
+
item.menu = menu;
|
|
271
|
+
$.NSTimer.scheduledTimerWithTimeIntervalTargetSelectorUserInfoRepeats(2, target, 'tick:', $(), true);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function run(argv) {
|
|
275
|
+
var has = function (flag) { return argv.indexOf(flag) >= 0; };
|
|
276
|
+
var me = $.NSProcessInfo.processInfo.processIdentifier;
|
|
277
|
+
var other = parseInt(read(PIDFILE) || '0', 10);
|
|
278
|
+
// Đã 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).
|
|
279
|
+
if (other && other !== me && output('/bin/ps', ['-p', String(other), '-o', 'command=']).indexOf('tray.js') >= 0) {
|
|
280
|
+
if (has('--quit')) write(QUIT_REQ, String(Date.now()));
|
|
281
|
+
else if (!has('--autostart') && !has('--installed')) write(OPEN_REQ, String(Date.now()));
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (has('--quit')) return;
|
|
285
|
+
write(PIDFILE, String(me));
|
|
286
|
+
buildUi();
|
|
287
|
+
tick();
|
|
288
|
+
if (has('--installed')) notify(T(wasPaired ? 'firstRun' : 'connectHint'));
|
|
289
|
+
else if (!has('--autostart')) { if (wasPaired) notify(T('firstRun')); openAivin(); }
|
|
290
|
+
app.run;
|
|
291
|
+
}
|
|
292
|
+
`;
|
|
@@ -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
|
+
}
|