@aivin-labs/cli 1.0.5 → 1.0.8
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 +263 -216
- package/bin/cli.mjs +1780 -1366
- package/bin/lib/browserAutomation.mjs +655 -648
- package/bin/lib/browserTui.mjs +276 -251
- package/bin/lib/cancellation.mjs +61 -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 +457 -0
- package/bin/lib/remote/autoUpdate.mjs +129 -0
- package/bin/lib/remote/brand.mjs +60 -0
- package/bin/lib/remote/browserTunnel.mjs +292 -0
- package/bin/lib/remote/callLedger.mjs +134 -0
- package/bin/lib/remote/cdpPolicy.mjs +66 -0
- package/bin/lib/remote/client.mjs +726 -0
- package/bin/lib/remote/constants.mjs +173 -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/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 +245 -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 +904 -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 +249 -0
- package/bin/lib/remote/mascot/assets.mjs +218 -0
- package/bin/lib/remote/mascot/hostSource.mjs +362 -0
- package/bin/lib/remote/mascot/index.mjs +322 -0
- package/bin/lib/remote/mascot/labels.mjs +83 -0
- package/bin/lib/remote/mascot/strings.mjs +189 -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/textIndex.mjs +131 -0
- package/bin/lib/remote/tokenStore.mjs +77 -0
- package/bin/lib/remote/toolHandlers.mjs +726 -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/CLI.md +1353 -1299
- package/package.json +71 -62
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import readline from 'readline';
|
|
4
|
+
import { createHash } from 'crypto';
|
|
5
|
+
import { spawn } from 'child_process';
|
|
6
|
+
import { REMOTE_CONFIG_DIR, ToolGroup } from '../constants.mjs';
|
|
7
|
+
import { AIVIN_LOGO_PNG_BASE64 } from '../brand.mjs';
|
|
8
|
+
import { MASCOT_PNG_BASE64 } from './assets.mjs';
|
|
9
|
+
import { MASCOT_GLYPHS, MASCOT_PALETTE, MASCOT_PS1, MASCOT_XAML } from './hostSource.mjs';
|
|
10
|
+
import { describeStep } from './labels.mjs';
|
|
11
|
+
import { MascotText, mascotLang, mascotText, windowStrings } from './strings.mjs';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Linh vật Aivin trên màn hình máy người dùng (29/10/2026): một nhân vật nhỏ dựng từ logo, nổi trên mọi cửa sổ khi `aivin remote start`
|
|
15
|
+
* chạy, bong bóng trên đầu hiện việc chính đang làm (mục đích bước / "Đọc baocao.xlsx"), kéo đi đâu cũng được, bấm vào ra bảng nhật ký chi
|
|
16
|
+
* tiết + nút tạm dừng AI. Đợt đầu Windows (WPF, hostSource.mjs); hệ điều hành khác / máy tính tổ chức → không có gì (mọi hàm là no-op).
|
|
17
|
+
*
|
|
18
|
+
* Trạng thái: working (có bước đang chạy) → thinking (vừa xong lô, BE đang lập bước kế — chưa coi là xong) → done / error (yên THINK_MS
|
|
19
|
+
* mà không có lô mới) → idle. waiting (chờ người bấm xác nhận trên máy), paused (công tắc ngắt), offline (mất kết nối) đè lên các trạng
|
|
20
|
+
* thái kia. Bong bóng đổi tối đa mỗi MAIN_THROTTLE_MS (lô nhiều bước chỉ hiện bước mới nhất, không nhấp nháy).
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export const MASCOT_DIR = path.join(REMOTE_CONFIG_DIR, 'mascot');
|
|
24
|
+
export const MASCOT_SETTINGS_FILE = path.join(REMOTE_CONFIG_DIR, 'mascot.json');
|
|
25
|
+
|
|
26
|
+
export const MascotState = Object.freeze({
|
|
27
|
+
IDLE: 'idle', WORKING: 'working', THINKING: 'thinking', WAITING: 'waiting', PAUSED: 'paused', OFFLINE: 'offline', DONE: 'done', ERROR: 'error',
|
|
28
|
+
});
|
|
29
|
+
export const LogStatus = Object.freeze({ RUN: 'run', OK: 'ok', FAIL: 'fail', INFO: 'info', WARN: 'warn' });
|
|
30
|
+
/** Lệnh gửi cửa sổ / sự kiện cửa sổ gửi về (một dòng JSON mỗi lệnh). */
|
|
31
|
+
export const HostCommand = Object.freeze({ STATE: 'state', MAIN: 'main', LOG: 'log', GUI: 'gui', SHOW: 'show', QUIT: 'quit' });
|
|
32
|
+
export const HostEvent = Object.freeze({ READY: 'ready', POS: 'pos', PAUSE: 'pause', RESUME: 'resume', HIDE: 'hide', OFF: 'off' });
|
|
33
|
+
|
|
34
|
+
const THINK_MS = 8000;
|
|
35
|
+
const RESULT_MS = 6000;
|
|
36
|
+
const MAIN_THROTTLE_MS = 600;
|
|
37
|
+
const RESTART_DELAYS_MS = [5000, 30_000, 120_000];
|
|
38
|
+
const QUEUE_LOGS = 40;
|
|
39
|
+
|
|
40
|
+
export function loadMascotSettings(file = MASCOT_SETTINGS_FILE) {
|
|
41
|
+
try {
|
|
42
|
+
const data = JSON.parse(fs.readFileSync(file, 'utf8')) || {};
|
|
43
|
+
const pos = Number.isFinite(data.x) && Number.isFinite(data.y) ? { x: data.x, y: data.y } : {};
|
|
44
|
+
return { enabled: data.enabled !== false, ...pos };
|
|
45
|
+
} catch {
|
|
46
|
+
return { enabled: true };
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function saveMascotSettings(patch, file = MASCOT_SETTINGS_FILE) {
|
|
51
|
+
const next = { ...loadMascotSettings(file), ...patch };
|
|
52
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
53
|
+
fs.writeFileSync(file, JSON.stringify(next, null, 2) + '\n');
|
|
54
|
+
return next;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Tệp của cửa sổ: mã + ảnh theo dấu vân tay (đổi bản CLI → bộ mới, dọn bộ cũ); config.json (vị trí, chữ, màu) ghi mỗi lần mở. */
|
|
58
|
+
function writeHostFiles({ dir, lang, settings }) {
|
|
59
|
+
const hash = createHash('sha256').update(MASCOT_PS1).update(MASCOT_XAML).update(MASCOT_PNG_BASE64).digest('hex').slice(0, 12);
|
|
60
|
+
const home = path.join(dir, hash);
|
|
61
|
+
fs.mkdirSync(home, { recursive: true });
|
|
62
|
+
const files = { 'host.ps1': MASCOT_PS1, 'mascot.xaml': MASCOT_XAML };
|
|
63
|
+
for (const [name, body] of Object.entries(files)) if (!fs.existsSync(path.join(home, name))) fs.writeFileSync(path.join(home, name), body);
|
|
64
|
+
if (!fs.existsSync(path.join(home, 'mascot.png'))) fs.writeFileSync(path.join(home, 'mascot.png'), Buffer.from(MASCOT_PNG_BASE64, 'base64'));
|
|
65
|
+
if (!fs.existsSync(path.join(home, 'logo.png'))) fs.writeFileSync(path.join(home, 'logo.png'), Buffer.from(AIVIN_LOGO_PNG_BASE64, 'base64'));
|
|
66
|
+
fs.writeFileSync(path.join(home, 'config.json'), JSON.stringify({
|
|
67
|
+
strings: windowStrings(lang), palette: MASCOT_PALETTE, glyphs: MASCOT_GLYPHS, x: settings.x ?? null, y: settings.y ?? null,
|
|
68
|
+
}));
|
|
69
|
+
for (const name of fs.readdirSync(dir)) {
|
|
70
|
+
if (name !== hash && /^[0-9a-f]{12}$/.test(name)) fs.rmSync(path.join(dir, name), { recursive: true, force: true });
|
|
71
|
+
}
|
|
72
|
+
return home;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const NOOP = Object.freeze({
|
|
76
|
+
connected() {}, disconnected() {}, paused() {}, callStarted() {}, callFinished() {}, waitingConfirm() {}, confirmDone() {}, close() {}, enabled: false,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const hhmmss = (t) => new Date(t).toTimeString().slice(0, 8);
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* `onPause` / `onResume`: nút trên bảng nhật ký bật / tắt công tắc ngắt (như `aivin remote pause|resume`). `spawnImpl`, `timers`, `now`,
|
|
83
|
+
* `dir`, `settingsFile`, các mốc `*Ms`: cho test.
|
|
84
|
+
*/
|
|
85
|
+
export function createMascot({
|
|
86
|
+
enabled = true, platform = process.platform, env = process.env, onPause = () => {}, onResume = () => {}, say = () => {},
|
|
87
|
+
spawnImpl = spawn, dir = MASCOT_DIR, settingsFile = MASCOT_SETTINGS_FILE, now = Date.now, timers = { setTimeout, clearTimeout },
|
|
88
|
+
thinkMs = THINK_MS, resultMs = RESULT_MS, mainThrottleMs = MAIN_THROTTLE_MS, restartDelaysMs = RESTART_DELAYS_MS,
|
|
89
|
+
} = {}) {
|
|
90
|
+
if (!enabled || platform !== 'win32' || !loadMascotSettings(settingsFile).enabled) return NOOP;
|
|
91
|
+
const lang = mascotLang(env);
|
|
92
|
+
const t = (key, vars) => mascotText(lang, key, vars);
|
|
93
|
+
|
|
94
|
+
let child = null;
|
|
95
|
+
let ready = false;
|
|
96
|
+
let closed = false;
|
|
97
|
+
let restarts = 0;
|
|
98
|
+
let restartTimer = null;
|
|
99
|
+
const pending = [];
|
|
100
|
+
let lastState = null;
|
|
101
|
+
let lastMain = null;
|
|
102
|
+
|
|
103
|
+
const write = (msg) => {
|
|
104
|
+
try { child?.stdin?.write(`${JSON.stringify(msg)}\n`); } catch { /* cửa sổ vừa chết — lần dựng lại gửi trạng thái mới nhất */ }
|
|
105
|
+
};
|
|
106
|
+
const send = (msg) => {
|
|
107
|
+
if (msg.t === HostCommand.STATE) lastState = msg;
|
|
108
|
+
if (msg.t === HostCommand.MAIN) lastMain = msg;
|
|
109
|
+
if (ready) return write(msg);
|
|
110
|
+
if (msg.t === HostCommand.LOG) {
|
|
111
|
+
pending.push(msg);
|
|
112
|
+
if (pending.length > QUEUE_LOGS) pending.shift();
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
const flush = () => {
|
|
116
|
+
for (const msg of pending.splice(0)) write(msg);
|
|
117
|
+
if (lastState) write(lastState);
|
|
118
|
+
if (lastMain) write({ ...lastMain, quiet: true });
|
|
119
|
+
if (guiActive > 0) write({ t: HostCommand.GUI, active: true });
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const onEvent = (msg) => {
|
|
123
|
+
if (msg.t === HostEvent.READY) { ready = true; restarts = 0; flush(); return; }
|
|
124
|
+
if (msg.t === HostEvent.POS && Number.isFinite(msg.x) && Number.isFinite(msg.y)) {
|
|
125
|
+
try { saveMascotSettings({ x: msg.x, y: msg.y }, settingsFile); } catch { /* không lưu được vị trí — lần sau về chỗ cũ */ }
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (msg.t === HostEvent.PAUSE) return onPause();
|
|
129
|
+
if (msg.t === HostEvent.RESUME) return onResume();
|
|
130
|
+
if (msg.t === HostEvent.HIDE) { hiddenUntilWork = true; return; }
|
|
131
|
+
if (msg.t === HostEvent.OFF) {
|
|
132
|
+
try { saveMascotSettings({ enabled: false }, settingsFile); } catch { /* tắt trong phiên này vẫn được */ }
|
|
133
|
+
say(t(MascotText.TURNED_OFF));
|
|
134
|
+
close();
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
function start() {
|
|
139
|
+
if (closed || child) return;
|
|
140
|
+
let home;
|
|
141
|
+
try {
|
|
142
|
+
home = writeHostFiles({ dir, lang, settings: loadMascotSettings(settingsFile) });
|
|
143
|
+
} catch {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
let c;
|
|
147
|
+
try {
|
|
148
|
+
c = spawnImpl('powershell.exe', ['-NoProfile', '-NonInteractive', '-STA', '-ExecutionPolicy', 'Bypass', '-File', path.join(home, 'host.ps1')], {
|
|
149
|
+
env: { ...env, AIVIN_MASCOT_DIR: home }, windowsHide: true, stdio: ['pipe', 'pipe', 'pipe'],
|
|
150
|
+
});
|
|
151
|
+
} catch {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
child = c;
|
|
155
|
+
ready = false;
|
|
156
|
+
let stderr = '';
|
|
157
|
+
c.stderr?.on('data', (d) => { stderr = (stderr + d).slice(-2000); });
|
|
158
|
+
c.stdin?.on('error', () => { /* cửa sổ đã đóng */ });
|
|
159
|
+
readline.createInterface({ input: c.stdout }).on('line', (line) => {
|
|
160
|
+
try { onEvent(JSON.parse(line)); } catch { /* dòng lạ */ }
|
|
161
|
+
});
|
|
162
|
+
c.on('error', () => { /* xử lý ở close */ });
|
|
163
|
+
c.on('close', () => {
|
|
164
|
+
if (child === c) { child = null; ready = false; }
|
|
165
|
+
if (closed) return;
|
|
166
|
+
// Không có màn hình (phiên dịch vụ), WPF hỏng… → thử lại giãn dần, quá số lần thì thôi (daemon vẫn chạy bình thường).
|
|
167
|
+
const delay = restartDelaysMs[restarts++];
|
|
168
|
+
if (delay === undefined) { say(`(mascot: ${stderr.trim().split(/\r?\n/).pop() || 'window could not start'})`); return; }
|
|
169
|
+
restartTimer = timers.setTimeout(start, delay);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function close() {
|
|
174
|
+
if (closed) return;
|
|
175
|
+
closed = true;
|
|
176
|
+
timers.clearTimeout(restartTimer);
|
|
177
|
+
timers.clearTimeout(thinkTimer);
|
|
178
|
+
timers.clearTimeout(resultTimer);
|
|
179
|
+
timers.clearTimeout(mainTimer);
|
|
180
|
+
const c = child;
|
|
181
|
+
if (!c) return;
|
|
182
|
+
write({ t: HostCommand.QUIT });
|
|
183
|
+
try { c.stdin?.end(); } catch { /* đã đóng */ }
|
|
184
|
+
const kill = timers.setTimeout(() => { try { c.kill(); } catch { /* đã thoát */ } }, 1500);
|
|
185
|
+
kill?.unref?.();
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// --- Trạng thái ---------------------------------------------------------------------------------------------------------------------
|
|
189
|
+
const active = new Map();
|
|
190
|
+
const waiting = new Set();
|
|
191
|
+
let guiActive = 0;
|
|
192
|
+
let paused = false;
|
|
193
|
+
let offline = false;
|
|
194
|
+
let phase = MascotState.IDLE;
|
|
195
|
+
let batchOk = 0;
|
|
196
|
+
let batchFailed = null;
|
|
197
|
+
let thinkTimer = null;
|
|
198
|
+
let resultTimer = null;
|
|
199
|
+
let hiddenUntilWork = false;
|
|
200
|
+
|
|
201
|
+
const current = () => {
|
|
202
|
+
if (paused) return MascotState.PAUSED;
|
|
203
|
+
if (offline) return MascotState.OFFLINE;
|
|
204
|
+
if (waiting.size) return MascotState.WAITING;
|
|
205
|
+
if (active.size) return MascotState.WORKING;
|
|
206
|
+
return phase;
|
|
207
|
+
};
|
|
208
|
+
let shownState = null;
|
|
209
|
+
const syncState = () => {
|
|
210
|
+
const s = current();
|
|
211
|
+
if (s === shownState) return;
|
|
212
|
+
shownState = s;
|
|
213
|
+
send({ t: HostCommand.STATE, state: s });
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
let mainTimer = null;
|
|
217
|
+
let mainNext = null;
|
|
218
|
+
let mainAt = 0;
|
|
219
|
+
const main = (text) => {
|
|
220
|
+
mainNext = text;
|
|
221
|
+
const wait = mainThrottleMs - (now() - mainAt);
|
|
222
|
+
if (wait > 0) {
|
|
223
|
+
if (!mainTimer) mainTimer = timers.setTimeout(() => { mainTimer = null; main(mainNext); }, wait);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
mainAt = now();
|
|
227
|
+
send({ t: HostCommand.MAIN, text });
|
|
228
|
+
};
|
|
229
|
+
const log = (entry) => send({ t: HostCommand.LOG, time: hhmmss(now()), ...entry });
|
|
230
|
+
|
|
231
|
+
const settle = () => {
|
|
232
|
+
timers.clearTimeout(thinkTimer);
|
|
233
|
+
timers.clearTimeout(resultTimer);
|
|
234
|
+
if (active.size || waiting.size) return;
|
|
235
|
+
// Hết lô bước: BE thường gửi lô kế trong vài giây (lượt Planner) — "đang nghĩ", chưa báo xong.
|
|
236
|
+
phase = MascotState.THINKING;
|
|
237
|
+
syncState();
|
|
238
|
+
main(t(MascotText.THINKING));
|
|
239
|
+
thinkTimer = timers.setTimeout(() => {
|
|
240
|
+
phase = batchFailed ? MascotState.ERROR : MascotState.DONE;
|
|
241
|
+
main(batchFailed ? t(MascotText.FAILED, { title: batchFailed }) : batchOk > 1 ? t(MascotText.DONE_MANY, { count: batchOk }) : t(MascotText.DONE_ONE));
|
|
242
|
+
batchOk = 0;
|
|
243
|
+
batchFailed = null;
|
|
244
|
+
syncState();
|
|
245
|
+
// Về chờ việc: mục "đang làm" của bảng nhật ký không giữ kết quả cũ (kết quả vẫn còn trong nhật ký); không bật bong bóng.
|
|
246
|
+
resultTimer = timers.setTimeout(() => {
|
|
247
|
+
phase = MascotState.IDLE;
|
|
248
|
+
syncState();
|
|
249
|
+
send({ t: HostCommand.MAIN, text: t(MascotText.READY), quiet: true });
|
|
250
|
+
}, resultMs);
|
|
251
|
+
}, thinkMs);
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
start();
|
|
255
|
+
|
|
256
|
+
let everConnected = false;
|
|
257
|
+
return {
|
|
258
|
+
enabled: true,
|
|
259
|
+
connected() {
|
|
260
|
+
offline = false;
|
|
261
|
+
syncState();
|
|
262
|
+
log({ status: LogStatus.INFO, title: t(MascotText.CONNECTED) });
|
|
263
|
+
if (!everConnected) main(t(MascotText.READY));
|
|
264
|
+
everConnected = true;
|
|
265
|
+
},
|
|
266
|
+
disconnected() {
|
|
267
|
+
if (offline) return;
|
|
268
|
+
offline = true;
|
|
269
|
+
syncState();
|
|
270
|
+
log({ status: LogStatus.WARN, title: t(MascotText.DISCONNECTED) });
|
|
271
|
+
main(t(MascotText.DISCONNECTED));
|
|
272
|
+
},
|
|
273
|
+
paused(on) {
|
|
274
|
+
if (paused === on) return;
|
|
275
|
+
paused = on;
|
|
276
|
+
syncState();
|
|
277
|
+
const text = t(on ? MascotText.PAUSED : MascotText.RESUMED);
|
|
278
|
+
log({ status: on ? LogStatus.WARN : LogStatus.INFO, title: text });
|
|
279
|
+
main(text);
|
|
280
|
+
},
|
|
281
|
+
callStarted(call, tool) {
|
|
282
|
+
if (!call?.call_id || tool?.internal) return;
|
|
283
|
+
const { title, detail } = describeStep({ call, tool, lang });
|
|
284
|
+
active.set(call.call_id, { title, detail, gui: tool?.group === ToolGroup.GUI, at: now() });
|
|
285
|
+
timers.clearTimeout(thinkTimer);
|
|
286
|
+
timers.clearTimeout(resultTimer);
|
|
287
|
+
phase = MascotState.WORKING;
|
|
288
|
+
// AI sắp bấm / chụp cửa sổ: linh vật tránh đường (không che, không hứng cú bấm).
|
|
289
|
+
if (tool?.group === ToolGroup.GUI && guiActive++ === 0) send({ t: HostCommand.GUI, active: true });
|
|
290
|
+
if (hiddenUntilWork) { hiddenUntilWork = false; send({ t: HostCommand.SHOW }); }
|
|
291
|
+
syncState();
|
|
292
|
+
main(title);
|
|
293
|
+
log({ id: call.call_id, status: LogStatus.RUN, title, sub: detail });
|
|
294
|
+
},
|
|
295
|
+
callFinished(call, tool, outcome) {
|
|
296
|
+
const entry = active.get(call?.call_id);
|
|
297
|
+
if (!entry) return;
|
|
298
|
+
active.delete(call.call_id);
|
|
299
|
+
if (entry.gui && --guiActive === 0) send({ t: HostCommand.GUI, active: false });
|
|
300
|
+
const ms = Number(outcome?.duration_ms) || now() - entry.at;
|
|
301
|
+
const took = ms >= 10_000 ? t(MascotText.SECONDS, { n: Math.round(ms / 1000) }) : t(MascotText.MS, { n: ms });
|
|
302
|
+
if (outcome?.ok) batchOk++;
|
|
303
|
+
else batchFailed = entry.title;
|
|
304
|
+
log({ id: call.call_id, status: outcome?.ok ? LogStatus.OK : LogStatus.FAIL, sub: outcome?.ok ? `${entry.detail} · ${took}` : String(outcome?.error || '').slice(0, 300) });
|
|
305
|
+
settle();
|
|
306
|
+
},
|
|
307
|
+
waitingConfirm(call, tool) {
|
|
308
|
+
if (!call?.call_id) return;
|
|
309
|
+
const { title } = describeStep({ call, tool, lang });
|
|
310
|
+
waiting.add(call.call_id);
|
|
311
|
+
syncState();
|
|
312
|
+
main(t(MascotText.WAITING_CONFIRM, { title }));
|
|
313
|
+
},
|
|
314
|
+
confirmDone(call, allowed) {
|
|
315
|
+
if (!waiting.delete(call?.call_id)) return;
|
|
316
|
+
log({ status: allowed ? LogStatus.INFO : LogStatus.WARN, title: t(allowed ? MascotText.CONFIRM_ALLOWED : MascotText.CONFIRM_REJECTED) });
|
|
317
|
+
syncState();
|
|
318
|
+
if (!allowed) settle();
|
|
319
|
+
},
|
|
320
|
+
close,
|
|
321
|
+
};
|
|
322
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { ToolGroup } from '../constants.mjs';
|
|
3
|
+
import { MascotVerb, mascotVerb } from './strings.mjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Nhãn cho từng bước hiện trên linh vật: `title` (bong bóng — "log chính") = mục đích Planner ghi cho bước có thay đổi, bước chỉ đọc không
|
|
7
|
+
* có mục đích → động từ theo công cụ + đích chính ("Đọc baocao.xlsx"); `detail` (bảng nhật ký) = tên công cụ + tham số rút gọn, không chép
|
|
8
|
+
* nội dung ghi tệp (chỉ ghi độ dài).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const TITLE_MAX = 140;
|
|
12
|
+
const DETAIL_MAX = 180;
|
|
13
|
+
const TARGET_MAX = 60;
|
|
14
|
+
|
|
15
|
+
/** Tên công cụ đúng → động từ; rồi tiền tố (kết thúc '.'); rồi nhóm công cụ. */
|
|
16
|
+
const VERB_BY_NAME = {
|
|
17
|
+
'fs.read': MascotVerb.READ, 'fs.read_many': MascotVerb.READ, 'fs.read_lines': MascotVerb.READ, 'fs.preview': MascotVerb.READ,
|
|
18
|
+
'fs.info': MascotVerb.READ, 'fs.stat': MascotVerb.READ,
|
|
19
|
+
'fs.list': MascotVerb.LIST, 'fs.tree': MascotVerb.LIST,
|
|
20
|
+
'fs.find': MascotVerb.FIND, 'fs.grep': MascotVerb.FIND, 'fs.search': MascotVerb.FIND,
|
|
21
|
+
'fs.move': MascotVerb.MOVE, 'fs.move_many': MascotVerb.MOVE, 'fs.rename': MascotVerb.MOVE,
|
|
22
|
+
'fs.copy': MascotVerb.COPY, 'fs.delete': MascotVerb.DELETE, 'fs.delete_many': MascotVerb.DELETE,
|
|
23
|
+
'fs.undo': MascotVerb.UNDO, 'fs.history': MascotVerb.UNDO, 'fs.measure': MascotVerb.MEASURE,
|
|
24
|
+
'fs.upload': MascotVerb.TRANSFER, 'fs.download': MascotVerb.TRANSFER, 'sys.schedule': MascotVerb.SCHEDULE, 'sys.schedules': MascotVerb.SCHEDULE,
|
|
25
|
+
};
|
|
26
|
+
const VERB_BY_PREFIX = [
|
|
27
|
+
['office.excel.', MascotVerb.EXCEL], ['office.word.', MascotVerb.WORD], ['office.outlook.', MascotVerb.OUTLOOK],
|
|
28
|
+
['shell.', MascotVerb.EXEC], ['proc.', MascotVerb.PROC], ['git.', MascotVerb.GIT], ['sys.', MascotVerb.SYSTEM], ['system.', MascotVerb.SYSTEM],
|
|
29
|
+
['gui.', MascotVerb.GUI], ['browser.', MascotVerb.BROWSER], ['memory.', MascotVerb.MEMORY], ['context.', MascotVerb.MEMORY],
|
|
30
|
+
['watch.', MascotVerb.WATCH], ['fs.', MascotVerb.WRITE],
|
|
31
|
+
];
|
|
32
|
+
const VERB_BY_GROUP = {
|
|
33
|
+
[ToolGroup.FS_READ]: MascotVerb.READ, [ToolGroup.FS_WRITE]: MascotVerb.WRITE, [ToolGroup.EXEC]: MascotVerb.EXEC, [ToolGroup.PROC]: MascotVerb.PROC,
|
|
34
|
+
[ToolGroup.GIT]: MascotVerb.GIT, [ToolGroup.SYSTEM]: MascotVerb.SYSTEM, [ToolGroup.GUI]: MascotVerb.GUI, [ToolGroup.BROWSER]: MascotVerb.BROWSER,
|
|
35
|
+
[ToolGroup.MEMORY]: MascotVerb.MEMORY,
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** Tham số mang nội dung (không hiện, chỉ ghi độ dài). */
|
|
39
|
+
const CONTENT_KEYS = new Set(['content', 'data', 'text', 'body', 'input', 'value', 'values', 'html', 'chunk']);
|
|
40
|
+
|
|
41
|
+
export function verbFor(toolName, group) {
|
|
42
|
+
if (VERB_BY_NAME[toolName]) return VERB_BY_NAME[toolName];
|
|
43
|
+
for (const [prefix, verb] of VERB_BY_PREFIX) if (toolName.startsWith(prefix)) return group === ToolGroup.FS_READ && verb === MascotVerb.WRITE ? MascotVerb.READ : verb;
|
|
44
|
+
return VERB_BY_GROUP[group] || MascotVerb.OTHER;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const clip = (s, max) => {
|
|
48
|
+
const t = String(s ?? '').replace(/\s+/g, ' ').trim();
|
|
49
|
+
return t.length > max ? `${t.slice(0, max - 1)}…` : t;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** Đích chính của bước: tên tệp / thư mục (không cả đường dẫn), lệnh, từ khoá tìm, ứng dụng. */
|
|
53
|
+
export function targetOf(args = {}) {
|
|
54
|
+
const file = args.path || args.file || args.src || args.from || args.source || (Array.isArray(args.paths) ? args.paths[0] : undefined) || args.dir || args.cwd;
|
|
55
|
+
if (typeof file === 'string' && file) return clip(path.basename(file.replace(/[\\/]+$/, '')) || file, TARGET_MAX);
|
|
56
|
+
const cmd = args.command || args.cmd;
|
|
57
|
+
if (typeof cmd === 'string' && cmd) return clip(cmd, TARGET_MAX);
|
|
58
|
+
const q = args.query || args.pattern || args.name;
|
|
59
|
+
if (typeof q === 'string' && q) return `"${clip(q, TARGET_MAX - 2)}"`;
|
|
60
|
+
const app = args.app || args.window || args.title;
|
|
61
|
+
return typeof app === 'string' ? clip(app, TARGET_MAX) : '';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Tham số rút gọn cho bảng nhật ký: nội dung dài → độ dài. */
|
|
65
|
+
export function argsPreview(args = {}) {
|
|
66
|
+
const shown = Object.fromEntries(Object.entries(args || {}).map(([k, v]) => {
|
|
67
|
+
if (CONTENT_KEYS.has(k) && typeof v === 'string') return [k, `‹${v.length}›`];
|
|
68
|
+
if (CONTENT_KEYS.has(k) && v && typeof v === 'object') return [k, '‹…›'];
|
|
69
|
+
return [k, v];
|
|
70
|
+
}));
|
|
71
|
+
let json = '';
|
|
72
|
+
try { json = JSON.stringify(shown); } catch { json = ''; }
|
|
73
|
+
return clip(json === '{}' ? '' : json, DETAIL_MAX);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function describeStep({ call, tool, lang }) {
|
|
77
|
+
const name = String(call?.tool || tool?.name || '');
|
|
78
|
+
const purpose = typeof call?.purpose === 'string' ? clip(call.purpose, TITLE_MAX) : '';
|
|
79
|
+
const target = targetOf(call?.args);
|
|
80
|
+
const title = purpose || clip(`${mascotVerb(lang, verbFor(name, tool?.group))}${target ? ` ${target}` : ''}`, TITLE_MAX);
|
|
81
|
+
const preview = argsPreview(call?.args);
|
|
82
|
+
return { title, detail: clip(`${name}${preview ? ` ${preview}` : ''}`, DETAIL_MAX) };
|
|
83
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chữ của linh vật theo ngôn ngữ (29/10/2026). Linh vật hiện trên máy người dùng, không qua máy chủ → chọn ngôn ngữ theo máy: AIVIN_LANG,
|
|
3
|
+
* rồi ngôn ngữ của hệ điều hành (Intl), không có bản dịch → tiếng Anh. Mục đích bước do BE gửi (`call.purpose`) vốn đã theo ngôn ngữ của
|
|
4
|
+
* người dùng nên hiện nguyên văn. Thêm ngôn ngữ: thêm một bảng cùng khoá vào STRINGS.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export const MascotText = Object.freeze({
|
|
8
|
+
WINDOW_TITLE: 'window_title',
|
|
9
|
+
TOOLTIP: 'tooltip',
|
|
10
|
+
PANEL_TITLE: 'panel_title',
|
|
11
|
+
NOW_LABEL: 'now_label',
|
|
12
|
+
LOG_LABEL: 'log_label',
|
|
13
|
+
NOTHING_YET: 'nothing_yet',
|
|
14
|
+
PAUSE: 'pause',
|
|
15
|
+
RESUME: 'resume',
|
|
16
|
+
HIDE: 'hide',
|
|
17
|
+
TURN_OFF: 'turn_off',
|
|
18
|
+
CLOSE: 'close',
|
|
19
|
+
STATE_IDLE: 'state_idle',
|
|
20
|
+
STATE_WORKING: 'state_working',
|
|
21
|
+
STATE_THINKING: 'state_thinking',
|
|
22
|
+
STATE_WAITING: 'state_waiting',
|
|
23
|
+
STATE_PAUSED: 'state_paused',
|
|
24
|
+
STATE_OFFLINE: 'state_offline',
|
|
25
|
+
STATE_DONE: 'state_done',
|
|
26
|
+
STATE_ERROR: 'state_error',
|
|
27
|
+
READY: 'ready',
|
|
28
|
+
CONNECTED: 'connected',
|
|
29
|
+
DISCONNECTED: 'disconnected',
|
|
30
|
+
PAUSED: 'paused',
|
|
31
|
+
RESUMED: 'resumed',
|
|
32
|
+
THINKING: 'thinking',
|
|
33
|
+
DONE_ONE: 'done_one',
|
|
34
|
+
DONE_MANY: 'done_many',
|
|
35
|
+
FAILED: 'failed',
|
|
36
|
+
WAITING_CONFIRM: 'waiting_confirm',
|
|
37
|
+
CONFIRM_ALLOWED: 'confirm_allowed',
|
|
38
|
+
CONFIRM_REJECTED: 'confirm_rejected',
|
|
39
|
+
TURNED_OFF: 'turned_off',
|
|
40
|
+
MS: 'ms',
|
|
41
|
+
SECONDS: 'seconds',
|
|
42
|
+
CMD_ON: 'cmd_on',
|
|
43
|
+
CMD_OFF: 'cmd_off',
|
|
44
|
+
CMD_STATUS_ON: 'cmd_status_on',
|
|
45
|
+
CMD_STATUS_OFF: 'cmd_status_off',
|
|
46
|
+
CMD_UNSUPPORTED: 'cmd_unsupported',
|
|
47
|
+
CMD_USAGE: 'cmd_usage',
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
/** Động từ cho bước không có mục đích (bước chỉ đọc — Planner không ghi `purpose`). Khoá = tên công cụ hoặc tiền tố kết thúc bằng '.'. */
|
|
51
|
+
export const MascotVerb = Object.freeze({
|
|
52
|
+
READ: 'read', LIST: 'list', FIND: 'find', WRITE: 'write', MOVE: 'move', COPY: 'copy', DELETE: 'delete', UNDO: 'undo', MEASURE: 'measure',
|
|
53
|
+
TRANSFER: 'transfer', EXEC: 'exec', PROC: 'proc', GIT: 'git', SYSTEM: 'system', SCHEDULE: 'schedule', GUI: 'gui', EXCEL: 'excel',
|
|
54
|
+
WORD: 'word', OUTLOOK: 'outlook', BROWSER: 'browser', MEMORY: 'memory', WATCH: 'watch', OTHER: 'other',
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const STRINGS = {
|
|
58
|
+
vi: {
|
|
59
|
+
[MascotText.WINDOW_TITLE]: 'Aivin trên máy này',
|
|
60
|
+
[MascotText.TOOLTIP]: 'Aivin — bấm để xem đang làm gì, kéo để di chuyển',
|
|
61
|
+
[MascotText.PANEL_TITLE]: 'Aivin trên máy này',
|
|
62
|
+
[MascotText.NOW_LABEL]: 'ĐANG LÀM',
|
|
63
|
+
[MascotText.LOG_LABEL]: 'NHẬT KÝ',
|
|
64
|
+
[MascotText.NOTHING_YET]: 'Chưa có việc nào — agent giao việc là thấy ở đây.',
|
|
65
|
+
[MascotText.PAUSE]: 'Tạm dừng AI',
|
|
66
|
+
[MascotText.RESUME]: 'Cho AI làm tiếp',
|
|
67
|
+
[MascotText.HIDE]: 'Ẩn tới khi có việc',
|
|
68
|
+
[MascotText.TURN_OFF]: 'Tắt linh vật',
|
|
69
|
+
[MascotText.CLOSE]: 'Thu gọn',
|
|
70
|
+
[MascotText.STATE_IDLE]: 'Đang chờ việc',
|
|
71
|
+
[MascotText.STATE_WORKING]: 'Đang làm',
|
|
72
|
+
[MascotText.STATE_THINKING]: 'Đang nghĩ',
|
|
73
|
+
[MascotText.STATE_WAITING]: 'Chờ bạn',
|
|
74
|
+
[MascotText.STATE_PAUSED]: 'Tạm dừng',
|
|
75
|
+
[MascotText.STATE_OFFLINE]: 'Mất kết nối',
|
|
76
|
+
[MascotText.STATE_DONE]: 'Xong',
|
|
77
|
+
[MascotText.STATE_ERROR]: 'Có lỗi',
|
|
78
|
+
[MascotText.READY]: 'Sẵn sàng nhận việc',
|
|
79
|
+
[MascotText.CONNECTED]: 'Đã kết nối máy chủ Aivin',
|
|
80
|
+
[MascotText.DISCONNECTED]: 'Mất kết nối — đang nối lại…',
|
|
81
|
+
[MascotText.PAUSED]: 'Đã tạm dừng — AI không làm gì trên máy này',
|
|
82
|
+
[MascotText.RESUMED]: 'AI làm việc tiếp trên máy này',
|
|
83
|
+
[MascotText.THINKING]: 'Đang nghĩ bước tiếp theo…',
|
|
84
|
+
[MascotText.DONE_ONE]: 'Xong',
|
|
85
|
+
[MascotText.DONE_MANY]: 'Xong {count} bước',
|
|
86
|
+
[MascotText.FAILED]: 'Chưa được: {title}',
|
|
87
|
+
[MascotText.WAITING_CONFIRM]: 'Chờ bạn bấm xác nhận: {title}',
|
|
88
|
+
[MascotText.CONFIRM_ALLOWED]: 'Bạn đã cho phép',
|
|
89
|
+
[MascotText.CONFIRM_REJECTED]: 'Không được cho phép',
|
|
90
|
+
[MascotText.TURNED_OFF]: 'Đã tắt linh vật — bật lại: aivin remote mascot on',
|
|
91
|
+
[MascotText.MS]: '{n} ms',
|
|
92
|
+
[MascotText.SECONDS]: '{n} s',
|
|
93
|
+
[MascotText.CMD_ON]: 'Đã bật linh vật — hiện khi chạy `aivin remote start` (cửa sổ đang chạy: chạy lại).',
|
|
94
|
+
[MascotText.CMD_OFF]: 'Đã tắt linh vật — áp dụng từ lần chạy `aivin remote start` kế tiếp.',
|
|
95
|
+
[MascotText.CMD_STATUS_ON]: 'Linh vật: bật',
|
|
96
|
+
[MascotText.CMD_STATUS_OFF]: 'Linh vật: tắt (bật: aivin remote mascot on)',
|
|
97
|
+
[MascotText.CMD_UNSUPPORTED]: 'Linh vật hiện mới có trên Windows.',
|
|
98
|
+
[MascotText.CMD_USAGE]: 'Dùng: aivin remote mascot [on | off]',
|
|
99
|
+
},
|
|
100
|
+
en: {
|
|
101
|
+
[MascotText.WINDOW_TITLE]: 'Aivin on this computer',
|
|
102
|
+
[MascotText.TOOLTIP]: 'Aivin — click to see what it is doing, drag to move',
|
|
103
|
+
[MascotText.PANEL_TITLE]: 'Aivin on this computer',
|
|
104
|
+
[MascotText.NOW_LABEL]: 'NOW',
|
|
105
|
+
[MascotText.LOG_LABEL]: 'ACTIVITY',
|
|
106
|
+
[MascotText.NOTHING_YET]: 'Nothing yet — work from your agents shows up here.',
|
|
107
|
+
[MascotText.PAUSE]: 'Pause AI',
|
|
108
|
+
[MascotText.RESUME]: 'Let AI continue',
|
|
109
|
+
[MascotText.HIDE]: 'Hide until work',
|
|
110
|
+
[MascotText.TURN_OFF]: 'Turn mascot off',
|
|
111
|
+
[MascotText.CLOSE]: 'Collapse',
|
|
112
|
+
[MascotText.STATE_IDLE]: 'Waiting for work',
|
|
113
|
+
[MascotText.STATE_WORKING]: 'Working',
|
|
114
|
+
[MascotText.STATE_THINKING]: 'Thinking',
|
|
115
|
+
[MascotText.STATE_WAITING]: 'Needs you',
|
|
116
|
+
[MascotText.STATE_PAUSED]: 'Paused',
|
|
117
|
+
[MascotText.STATE_OFFLINE]: 'Offline',
|
|
118
|
+
[MascotText.STATE_DONE]: 'Done',
|
|
119
|
+
[MascotText.STATE_ERROR]: 'Problem',
|
|
120
|
+
[MascotText.READY]: 'Ready for work',
|
|
121
|
+
[MascotText.CONNECTED]: 'Connected to Aivin',
|
|
122
|
+
[MascotText.DISCONNECTED]: 'Connection lost — reconnecting…',
|
|
123
|
+
[MascotText.PAUSED]: 'Paused — AI does nothing on this computer',
|
|
124
|
+
[MascotText.RESUMED]: 'AI continues on this computer',
|
|
125
|
+
[MascotText.THINKING]: 'Thinking about the next step…',
|
|
126
|
+
[MascotText.DONE_ONE]: 'Done',
|
|
127
|
+
[MascotText.DONE_MANY]: 'Done — {count} steps',
|
|
128
|
+
[MascotText.FAILED]: 'Did not work: {title}',
|
|
129
|
+
[MascotText.WAITING_CONFIRM]: 'Waiting for your confirmation: {title}',
|
|
130
|
+
[MascotText.CONFIRM_ALLOWED]: 'You allowed it',
|
|
131
|
+
[MascotText.CONFIRM_REJECTED]: 'Not allowed',
|
|
132
|
+
[MascotText.TURNED_OFF]: 'Mascot turned off — turn it back on: aivin remote mascot on',
|
|
133
|
+
[MascotText.MS]: '{n} ms',
|
|
134
|
+
[MascotText.SECONDS]: '{n} s',
|
|
135
|
+
[MascotText.CMD_ON]: 'Mascot on — it shows while `aivin remote start` runs (a window already running: restart it).',
|
|
136
|
+
[MascotText.CMD_OFF]: 'Mascot off — from the next `aivin remote start`.',
|
|
137
|
+
[MascotText.CMD_STATUS_ON]: 'Mascot: on',
|
|
138
|
+
[MascotText.CMD_STATUS_OFF]: 'Mascot: off (turn on: aivin remote mascot on)',
|
|
139
|
+
[MascotText.CMD_UNSUPPORTED]: 'The mascot is only available on Windows for now.',
|
|
140
|
+
[MascotText.CMD_USAGE]: 'Usage: aivin remote mascot [on | off]',
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const VERBS = {
|
|
145
|
+
vi: {
|
|
146
|
+
[MascotVerb.READ]: 'Đọc', [MascotVerb.LIST]: 'Xem thư mục', [MascotVerb.FIND]: 'Tìm', [MascotVerb.WRITE]: 'Ghi', [MascotVerb.MOVE]: 'Chuyển',
|
|
147
|
+
[MascotVerb.COPY]: 'Chép', [MascotVerb.DELETE]: 'Xoá', [MascotVerb.UNDO]: 'Hoàn tác', [MascotVerb.MEASURE]: 'Đo dung lượng',
|
|
148
|
+
[MascotVerb.TRANSFER]: 'Chuyển tệp với Aivin', [MascotVerb.EXEC]: 'Chạy lệnh', [MascotVerb.PROC]: 'Theo dõi chương trình chạy nền',
|
|
149
|
+
[MascotVerb.GIT]: 'Git', [MascotVerb.SYSTEM]: 'Xem thông tin máy', [MascotVerb.SCHEDULE]: 'Đặt lịch chạy', [MascotVerb.GUI]: 'Thao tác cửa sổ',
|
|
150
|
+
[MascotVerb.EXCEL]: 'Làm trên Excel', [MascotVerb.WORD]: 'Làm trên Word', [MascotVerb.OUTLOOK]: 'Làm trên Outlook',
|
|
151
|
+
[MascotVerb.BROWSER]: 'Dùng trình duyệt', [MascotVerb.MEMORY]: 'Ghi nhớ', [MascotVerb.WATCH]: 'Theo dõi', [MascotVerb.OTHER]: 'Làm',
|
|
152
|
+
},
|
|
153
|
+
en: {
|
|
154
|
+
[MascotVerb.READ]: 'Read', [MascotVerb.LIST]: 'Look in folder', [MascotVerb.FIND]: 'Find', [MascotVerb.WRITE]: 'Write', [MascotVerb.MOVE]: 'Move',
|
|
155
|
+
[MascotVerb.COPY]: 'Copy', [MascotVerb.DELETE]: 'Delete', [MascotVerb.UNDO]: 'Undo', [MascotVerb.MEASURE]: 'Measure',
|
|
156
|
+
[MascotVerb.TRANSFER]: 'Transfer file with Aivin', [MascotVerb.EXEC]: 'Run command', [MascotVerb.PROC]: 'Watch background program',
|
|
157
|
+
[MascotVerb.GIT]: 'Git', [MascotVerb.SYSTEM]: 'Check this computer', [MascotVerb.SCHEDULE]: 'Schedule a job', [MascotVerb.GUI]: 'Use an app window',
|
|
158
|
+
[MascotVerb.EXCEL]: 'Work in Excel', [MascotVerb.WORD]: 'Work in Word', [MascotVerb.OUTLOOK]: 'Work in Outlook',
|
|
159
|
+
[MascotVerb.BROWSER]: 'Use the browser', [MascotVerb.MEMORY]: 'Remember', [MascotVerb.WATCH]: 'Watch', [MascotVerb.OTHER]: 'Work',
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
export const SUPPORTED_LANGS = Object.keys(STRINGS);
|
|
164
|
+
|
|
165
|
+
/** Ngôn ngữ của máy: AIVIN_LANG → LANG / LC_ALL → Intl; không có bản dịch → en. */
|
|
166
|
+
export function mascotLang(env = process.env) {
|
|
167
|
+
const candidates = [env.AIVIN_LANG, env.LC_ALL, env.LANG];
|
|
168
|
+
try { candidates.push(Intl.DateTimeFormat().resolvedOptions().locale); } catch { /* không có Intl */ }
|
|
169
|
+
for (const c of candidates) {
|
|
170
|
+
const code = String(c || '').toLowerCase().split(/[-_.]/)[0];
|
|
171
|
+
if (STRINGS[code]) return code;
|
|
172
|
+
}
|
|
173
|
+
return 'en';
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const fill = (text, vars = {}) => String(text).replace(/\{(\w+)\}/g, (_, k) => (vars[k] ?? `{${k}}`));
|
|
177
|
+
|
|
178
|
+
export function mascotText(lang, key, vars) {
|
|
179
|
+
return fill((STRINGS[lang] || STRINGS.en)[key] ?? STRINGS.en[key] ?? key, vars);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function mascotVerb(lang, verb) {
|
|
183
|
+
return (VERBS[lang] || VERBS.en)[verb] ?? VERBS.en[MascotVerb.OTHER];
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Toàn bộ chữ tĩnh của cửa sổ (gửi cho tiến trình hiển thị một lần lúc mở). */
|
|
187
|
+
export function windowStrings(lang) {
|
|
188
|
+
return Object.fromEntries(Object.values(MascotText).map((k) => [k, mascotText(lang, k)]));
|
|
189
|
+
}
|