@aivin-labs/cli 1.0.21 → 1.0.27
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/bin/cli.mjs +67 -2
- package/bin/lib/remote/autoStart.mjs +6 -1
- package/bin/lib/remote/autoUpdate.mjs +153 -141
- package/bin/lib/remote/client.mjs +77 -5
- package/bin/lib/remote/constants.mjs +2 -0
- package/bin/lib/remote/desktop/castFileDrop.mjs +227 -0
- package/bin/lib/remote/desktop/castInput.mjs +17 -4
- package/bin/lib/remote/desktop/desktopCast.mjs +3 -0
- package/bin/lib/remote/desktop/desktopCastSource.mjs +132 -20
- package/bin/lib/remote/desktop/desktopWebRtc.mjs +37 -3
- package/bin/lib/remote/desktop/fileAssociation.mjs +104 -0
- package/bin/lib/remote/desktop/index.mjs +54 -5
- package/bin/lib/remote/desktop/strings.mjs +8 -0
- package/bin/lib/remote/desktop/traySource.mjs +72 -1
- package/bin/lib/remote/desktop/uninstall.mjs +96 -0
- package/bin/lib/remote/desktop/webrtcWorker.mjs +191 -181
- package/bin/lib/remote/deviceProfile.mjs +2 -4
- package/bin/lib/remote/mascot/ask.mjs +983 -933
- package/bin/lib/remote/mascot/askHostSource.mjs +4 -2
- package/bin/lib/remote/mascot/fileChips.mjs +2 -2
- package/bin/lib/remote/mascot/index.mjs +69 -5
- package/bin/lib/remote/mascot/strings.mjs +18 -0
- package/bin/lib/remote/sandbox.mjs +3 -3
- package/bin/lib/remote/workFolder.mjs +100 -0
- package/docs/CLI.md +1576 -1545
- package/package.json +75 -75
|
@@ -443,6 +443,8 @@ function AttachChips($chips) {
|
|
|
443
443
|
if (-not $c -or -not $c.path) { continue }
|
|
444
444
|
$ink = [string]$c.ink; if (-not $ink) { $ink = '#64748B' }
|
|
445
445
|
$chip = New-Object Windows.Controls.Border; $chip.CornerRadius = '8'; $chip.Padding = '6,2,2,2'; $chip.Margin = '4,0,0,4'; $chip.ToolTip = [string]$c.path
|
|
446
|
+
# The working folder's chip stays after a question goes out and is not a waiting file (ask_files).
|
|
447
|
+
if ($c.pinned) { $chip.Tag = 'pinned' }
|
|
446
448
|
$chip.Background = Brush $(if ($c.bg) { [string]$c.bg } else { '#F8FAFC' })
|
|
447
449
|
# Hairline in the type's colour (ink at ~20 %): keeps the very light chip readable on the white box.
|
|
448
450
|
$chip.BorderThickness = '1'; $chip.BorderBrush = Brush ('#33' + $ink.TrimStart('#'))
|
|
@@ -451,7 +453,7 @@ function AttachChips($chips) {
|
|
|
451
453
|
$tb = New-Object Windows.Controls.TextBlock; $tb.Text = [string]$c.name; $tb.FontSize = 12; $tb.Foreground = Brush $ink; $tb.MaxWidth = 140
|
|
452
454
|
$tb.TextTrimming = 'CharacterEllipsis'; $tb.VerticalAlignment = 'Center'; [void]$row.Children.Add($tb)
|
|
453
455
|
$x = New-Object Windows.Controls.Button; $x.Style = $w.FindResource('IconBtn'); $x.Width = 18; $x.Height = 18; $x.Margin = '3,0,0,0'
|
|
454
|
-
$x.Background = Brush '#00000000'; $x.BorderThickness = '0'; $x.Tag = Brush '#1F000000'; $x.ToolTip = $T.ask_detach
|
|
456
|
+
$x.Background = Brush '#00000000'; $x.BorderThickness = '0'; $x.Tag = Brush '#1F000000'; $x.ToolTip = $(if ($c.detach_tip) { [string]$c.detach_tip } else { $T.ask_detach })
|
|
455
457
|
$x.Content = LucideIcon '${DETACH_ICON_PATH}' 10 $ink; $x.CommandParameter = [string]$c.path
|
|
456
458
|
$x.Add_Click({ param($s) Send @{ t = 'detach'; path = [string]$s.CommandParameter } })
|
|
457
459
|
[void]$row.Children.Add($x); $chip.Child = $row; [void]$bar.Children.Add($chip)
|
|
@@ -590,7 +592,7 @@ function Handle($m) {
|
|
|
590
592
|
'ask_status' { AskStatus $m.text; return }
|
|
591
593
|
'ask_choices' { ChoiceButtons $m.options $m.multiple; return }
|
|
592
594
|
# Files waiting for the next question: chips above the input; then on to the bot's tray (no return).
|
|
593
|
-
'ask_files' { $chips = @($m.files | Where-Object { $_ }); $more = $chips.Count -gt (E 'AttachBar').Children.Count; $script:files = [int]$m.n; AttachChips $chips; SyncAction; if ($more) { PeekAsk; HideAskLater ${ASK_IDLE_MS} } }
|
|
595
|
+
'ask_files' { $chips = @($m.files | Where-Object { $_ }); $more = @($chips | Where-Object { -not $_.pinned }).Count -gt @((E 'AttachBar').Children | Where-Object { $_.Tag -ne 'pinned' }).Count; $script:files = [int]$m.n; AttachChips $chips; SyncAction; if ($more) { PeekAsk; HideAskLater ${ASK_IDLE_MS} } }
|
|
594
596
|
'ask_pin' {
|
|
595
597
|
$script:askPinned = [bool]$m.on
|
|
596
598
|
if ($script:askPinned) { $script:userHidden = $false; ApplyVisibility; AskHide 'Panel'; PeekAsk; (E 'ChatScroll').ScrollToEnd() }
|
|
@@ -44,8 +44,8 @@ const isDir = (p) => {
|
|
|
44
44
|
try { return fs.statSync(p).isDirectory(); } catch { return false; }
|
|
45
45
|
};
|
|
46
46
|
|
|
47
|
-
/** Tên hiện trên chip: tên tệp / thư mục (bỏ dấu gạch cuối); ổ đĩa trống tên → cả đường dẫn. */
|
|
48
|
-
export const fileName = (p) => path.basename(String(p).replace(/[\\/]+$/, '')) || String(p);
|
|
47
|
+
/** Tên hiện trên chip: tên tệp / thư mục (bỏ dấu gạch cuối); ổ đĩa trống tên → cả đường dẫn. win32: tách cả `\` lẫn `/` trên mọi hệ điều hành. */
|
|
48
|
+
export const fileName = (p) => path.win32.basename(String(p).replace(/[\\/]+$/, '')) || String(p);
|
|
49
49
|
|
|
50
50
|
/** Loại của một đường dẫn: thư mục → folder; còn lại theo đuôi tệp (không biết → file). */
|
|
51
51
|
export function fileKind(p, { dir = isDir } = {}) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import readline from 'readline';
|
|
4
|
-
import { createHash } from 'crypto';
|
|
4
|
+
import { createHash, randomBytes } from 'crypto';
|
|
5
5
|
import { spawn } from 'child_process';
|
|
6
6
|
import { NotifyKind, REMOTE_CONFIG_DIR, ToolGroup } from '../constants.mjs';
|
|
7
7
|
import { AIVIN_LOGO_PNG_BASE64 } from '../brand.mjs';
|
|
@@ -43,9 +43,60 @@ export const MASCOT_HIDE_FILE = path.join(REMOTE_CONFIG_DIR, 'mascot-hide');
|
|
|
43
43
|
export const MASCOT_VOICE_FILE = path.join(REMOTE_CONFIG_DIR, 'mascot-voice');
|
|
44
44
|
/** Phím tắt cửa sổ linh vật đang giữ thật (`{ chat, voice }`, tổ hợp đầu tiên còn trống) — khay đọc để hiện cạnh mục menu; cửa sổ đóng thì xoá. */
|
|
45
45
|
export const MASCOT_HOTKEYS_FILE = path.join(REMOTE_CONFIG_DIR, 'mascot-hotkeys.json');
|
|
46
|
+
/**
|
|
47
|
+
* "Gửi cho AIVIN Bot" (chuột phải tệp / Send to, Aivin.exe --share; `aivin share`): mỗi yêu cầu một tệp `<mốc>-<pid>.txt`, mỗi dòng một đường
|
|
48
|
+
* dẫn — Explorer chạy một tiến trình cho mỗi tệp được chọn, nên không ghi chung một tệp. Daemon đọc xong xoá, đính vào ô hỏi như kéo thả.
|
|
49
|
+
*/
|
|
50
|
+
export const MASCOT_ATTACH_DIR = path.join(REMOTE_CONFIG_DIR, 'mascot-attach');
|
|
51
|
+
/** Yêu cầu cũ hơn chừng này (daemon không chạy lúc đó) thì bỏ — không bật ô hỏi với tệp gửi từ hôm qua. Đủ cho khay mở daemon từ đầu. */
|
|
52
|
+
const ATTACH_REQUEST_GRACE_MS = 2 * 60_000;
|
|
46
53
|
const SHOW_POLL_MS = 250;
|
|
47
54
|
const SHOW_REQUEST_GRACE_MS = 30_000;
|
|
48
55
|
|
|
56
|
+
let attachRequestSeq = 0;
|
|
57
|
+
|
|
58
|
+
/** Ghi yêu cầu đính tệp cho daemon đang chạy (hoặc sắp chạy). Số thứ tự giữ đúng thứ tự gửi khi trùng mili giây (nơi đọc xếp theo tên tệp). */
|
|
59
|
+
export function requestMascotAttach(paths, dir = MASCOT_ATTACH_DIR) {
|
|
60
|
+
const list = (paths || []).filter((p) => typeof p === 'string' && p).map((p) => path.resolve(p));
|
|
61
|
+
if (!list.length) return;
|
|
62
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
63
|
+
const seq = String(++attachRequestSeq).padStart(6, '0');
|
|
64
|
+
const file = path.join(dir, `${Date.now()}-${process.pid}-${seq}-${randomBytes(4).toString('hex')}.txt`);
|
|
65
|
+
fs.writeFileSync(`${file}.tmp`, list.join('\r\n'));
|
|
66
|
+
fs.renameSync(`${file}.tmp`, file);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Đọc + xoá mọi yêu cầu có đuôi `ext`; trả các dòng còn trong hạn (theo thứ tự gửi, không trùng). */
|
|
70
|
+
function takeRequests(dir, ext, now) {
|
|
71
|
+
let names;
|
|
72
|
+
try { names = fs.readdirSync(dir).filter((n) => n.endsWith(ext)).sort(); } catch { return []; }
|
|
73
|
+
const lines = [];
|
|
74
|
+
for (const name of names) {
|
|
75
|
+
const file = path.join(dir, name);
|
|
76
|
+
try {
|
|
77
|
+
const fresh = now - fs.statSync(file).mtimeMs <= ATTACH_REQUEST_GRACE_MS;
|
|
78
|
+
const text = fs.readFileSync(file, 'utf8').replace(/^\uFEFF/, '');
|
|
79
|
+
fs.rmSync(file, { force: true });
|
|
80
|
+
if (!fresh) continue;
|
|
81
|
+
for (const line of text.split(/\r?\n/)) {
|
|
82
|
+
const p = line.trim();
|
|
83
|
+
if (p && !lines.includes(p)) lines.push(p);
|
|
84
|
+
}
|
|
85
|
+
} catch { /* đang ghi dở / vừa bị xoá — lần quét sau */ }
|
|
86
|
+
}
|
|
87
|
+
return lines;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Các đường dẫn "Gửi cho AIVIN Bot" đang chờ. */
|
|
91
|
+
export function takeAttachRequests(dir = MASCOT_ATTACH_DIR, now = Date.now()) {
|
|
92
|
+
return takeRequests(dir, '.txt', now);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** "Làm việc ở đây với AIVIN Bot" (Aivin.exe --workdir, cùng thư mục yêu cầu, đuôi `.workdir`): chọn nhiều lần thì lần sau cùng thắng. */
|
|
96
|
+
export function takeWorkFolderRequest(dir = MASCOT_ATTACH_DIR, now = Date.now()) {
|
|
97
|
+
return takeRequests(dir, '.workdir', now).at(-1) || null;
|
|
98
|
+
}
|
|
99
|
+
|
|
49
100
|
/** Ghi yêu cầu hiện linh vật cho daemon đang chạy (hoặc sắp chạy). */
|
|
50
101
|
export function requestMascotShow(file = MASCOT_SHOW_FILE) {
|
|
51
102
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
@@ -89,9 +140,11 @@ function normalizeMascotSettings(data) {
|
|
|
89
140
|
const pos = Number.isFinite(data.x) && Number.isFinite(data.y) ? { x: data.x, y: data.y } : {};
|
|
90
141
|
const freq = Number.isInteger(data.notify_freq) && data.notify_freq >= 0 && data.notify_freq <= 2 ? { notify_freq: data.notify_freq } : {};
|
|
91
142
|
// `aivin remote mascot hotkey …` (cli.mjs): tổ hợp chủ máy đặt / 'off'; trước đây bị bỏ khi đọc → lượt lưu vị trí kế tiếp xoá mất.
|
|
143
|
+
// Thư mục làm việc của ô hỏi ("Làm việc ở đây với AIVIN Bot"): giữ qua các lần khởi động lại, tới khi chủ máy bấm × trên chip.
|
|
144
|
+
const workDir = typeof data.work_dir === 'string' && data.work_dir ? { work_dir: data.work_dir } : {};
|
|
92
145
|
const hotkey = typeof data.hotkey === 'string' && data.hotkey ? { hotkey: data.hotkey } : {};
|
|
93
146
|
const chatHotkey = typeof data.chat_hotkey === 'string' && data.chat_hotkey ? { chat_hotkey: data.chat_hotkey } : {};
|
|
94
|
-
return { enabled: data.enabled !== false, ...freq, ...hotkey, ...chatHotkey, ...pos };
|
|
147
|
+
return { enabled: data.enabled !== false, ...freq, ...workDir, ...hotkey, ...chatHotkey, ...pos };
|
|
95
148
|
}
|
|
96
149
|
|
|
97
150
|
export function loadMascotSettings(file = MASCOT_SETTINGS_FILE) {
|
|
@@ -172,8 +225,9 @@ export function userWindow(w) {
|
|
|
172
225
|
*/
|
|
173
226
|
export function createMascot({
|
|
174
227
|
enabled = true, platform = process.platform, env = process.env, lang: preferredLang, onPause = () => {}, onResume = () => {}, say = () => {},
|
|
175
|
-
onAsk = () => {}, onVoice = () => {}, onChoose = () => {}, onCancel = () => {}, onNewChat = () => {}, onOpenTool = () => {}, onDrop = () => {}, onDetach = () => {}, onNotifyFreq = () => {},
|
|
176
|
-
spawnImpl = spawn, dir = MASCOT_DIR, settingsFile = MASCOT_SETTINGS_FILE, showFile = MASCOT_SHOW_FILE, hideFile = MASCOT_HIDE_FILE, voiceFile = MASCOT_VOICE_FILE, hotkeysFile = MASCOT_HOTKEYS_FILE,
|
|
228
|
+
onAsk = () => {}, onVoice = () => {}, onChoose = () => {}, onCancel = () => {}, onNewChat = () => {}, onOpenTool = () => {}, onDrop = () => {}, onWorkFolder = () => {}, onDetach = () => {}, onNotifyFreq = () => {},
|
|
229
|
+
spawnImpl = spawn, dir = MASCOT_DIR, settingsFile = MASCOT_SETTINGS_FILE, showFile = MASCOT_SHOW_FILE, hideFile = MASCOT_HIDE_FILE, voiceFile = MASCOT_VOICE_FILE, hotkeysFile = MASCOT_HOTKEYS_FILE,
|
|
230
|
+
attachDir = MASCOT_ATTACH_DIR, now = Date.now, timers = { setTimeout, clearTimeout },
|
|
177
231
|
thinkMs = THINK_MS, resultMs = RESULT_MS, mainThrottleMs = MAIN_THROTTLE_MS, restartDelaysMs = RESTART_DELAYS_MS, awayWaveMs = AWAY_WAVE_MS,
|
|
178
232
|
} = {}) {
|
|
179
233
|
if (!enabled || platform !== 'win32') return NOOP;
|
|
@@ -254,7 +308,7 @@ export function createMascot({
|
|
|
254
308
|
if (lastChoices) write(lastChoices);
|
|
255
309
|
write(lastPin);
|
|
256
310
|
}
|
|
257
|
-
if (lastFiles?.
|
|
311
|
+
if (lastFiles?.files?.length) write(lastFiles);
|
|
258
312
|
if (commandOnReady) { write({ t: commandOnReady }); commandOnReady = null; }
|
|
259
313
|
};
|
|
260
314
|
|
|
@@ -382,6 +436,16 @@ export function createMascot({
|
|
|
382
436
|
fs.rmSync(voiceFile, { force: true });
|
|
383
437
|
if (voiceAt > voiceSeenAt) wake(HostCommand.VOICE_TOGGLE);
|
|
384
438
|
} catch {}
|
|
439
|
+
const shared = takeAttachRequests(attachDir, now());
|
|
440
|
+
if (shared.length) {
|
|
441
|
+
onDrop(shared);
|
|
442
|
+
wake();
|
|
443
|
+
}
|
|
444
|
+
const workFolder = takeWorkFolderRequest(attachDir, now());
|
|
445
|
+
if (workFolder) {
|
|
446
|
+
onWorkFolder(workFolder);
|
|
447
|
+
wake();
|
|
448
|
+
}
|
|
385
449
|
}, SHOW_POLL_MS);
|
|
386
450
|
showWatch.unref?.();
|
|
387
451
|
|
|
@@ -69,6 +69,7 @@ export const MascotText = Object.freeze({
|
|
|
69
69
|
ASK_TOO_SHORT: 'ask_too_short',
|
|
70
70
|
ASK_OFFLINE: 'ask_offline',
|
|
71
71
|
ASK_ERR_BUSY: 'ask_err_busy',
|
|
72
|
+
ASK_PARALLEL_SENT: 'ask_parallel_sent',
|
|
72
73
|
ASK_BUSY_VOICE: 'ask_busy_voice',
|
|
73
74
|
ASK_QUEUED: 'ask_queued',
|
|
74
75
|
ASK_ERR_TOO_LONG: 'ask_err_too_long',
|
|
@@ -80,6 +81,11 @@ export const MascotText = Object.freeze({
|
|
|
80
81
|
ASK_ERR_INVALID_ANSWER: 'ask_err_invalid_answer',
|
|
81
82
|
ASK_ATTACHED: 'ask_attached',
|
|
82
83
|
ASK_DETACH: 'ask_detach',
|
|
84
|
+
ASK_WORKDIR_CHIP: 'ask_workdir_chip',
|
|
85
|
+
ASK_WORKDIR_DETACH: 'ask_workdir_detach',
|
|
86
|
+
ASK_WORKDIR_SET: 'ask_workdir_set',
|
|
87
|
+
ASK_WORKDIR_CLEARED: 'ask_workdir_cleared',
|
|
88
|
+
ASK_WORKDIR_DENIED: 'ask_workdir_denied',
|
|
83
89
|
ASK_FILES_LABEL: 'ask_files_label',
|
|
84
90
|
ASK_FILES_DEFAULT: 'ask_files_default',
|
|
85
91
|
DROP_ONE: 'drop_one',
|
|
@@ -208,6 +214,7 @@ const STRINGS = {
|
|
|
208
214
|
[MascotText.ASK_TOO_SHORT]: "Đoạn nói quá ngắn — nói lâu hơn một chút",
|
|
209
215
|
[MascotText.ASK_OFFLINE]: "Chưa nối được Aivin, thử lại sau",
|
|
210
216
|
[MascotText.ASK_ERR_BUSY]: "Đang làm việc trước, chờ chút",
|
|
217
|
+
[MascotText.ASK_PARALLEL_SENT]: "Đã giao chạy song song — xong mình báo ngay",
|
|
211
218
|
[MascotText.ASK_BUSY_VOICE]: "Đang nói chuyện — kết thúc rồi hãy trả lời",
|
|
212
219
|
[MascotText.ASK_QUEUED]: "(còn {n} câu hỏi nữa)",
|
|
213
220
|
[MascotText.ASK_ERR_TOO_LONG]: "Dài quá, rút gọn nhé",
|
|
@@ -219,6 +226,11 @@ const STRINGS = {
|
|
|
219
226
|
[MascotText.ASK_ERR_INVALID_ANSWER]: "Chọn một nút hoặc trả lời đúng câu hỏi",
|
|
220
227
|
[MascotText.ASK_ATTACHED]: "Gõ việc cần làm với tệp…",
|
|
221
228
|
[MascotText.ASK_DETACH]: "Bỏ tệp này",
|
|
229
|
+
[MascotText.ASK_WORKDIR_CHIP]: "Làm việc ở: {name}",
|
|
230
|
+
[MascotText.ASK_WORKDIR_DETACH]: "Thôi làm việc ở thư mục này",
|
|
231
|
+
[MascotText.ASK_WORKDIR_SET]: "Từ giờ bot làm việc trong: {path}",
|
|
232
|
+
[MascotText.ASK_WORKDIR_CLEARED]: "Đã bỏ thư mục làm việc",
|
|
233
|
+
[MascotText.ASK_WORKDIR_DENIED]: "Không đặt được thư mục làm việc: {path}",
|
|
222
234
|
[MascotText.ASK_FILES_LABEL]: "Tệp trên máy này:",
|
|
223
235
|
[MascotText.ASK_FILES_DEFAULT]: "Xem giúp tôi các tệp này",
|
|
224
236
|
[MascotText.DROP_ONE]: "Thả vào để hỏi về {name}",
|
|
@@ -338,6 +350,7 @@ const STRINGS = {
|
|
|
338
350
|
[MascotText.ASK_TOO_SHORT]: "That was too short — talk a bit longer",
|
|
339
351
|
[MascotText.ASK_OFFLINE]: "Not connected to Aivin, try again soon",
|
|
340
352
|
[MascotText.ASK_ERR_BUSY]: "Still on the last one, hold on",
|
|
353
|
+
[MascotText.ASK_PARALLEL_SENT]: "Started alongside the current one — I'll tell you when it's done",
|
|
341
354
|
[MascotText.ASK_BUSY_VOICE]: "In a call — end it to answer",
|
|
342
355
|
[MascotText.ASK_QUEUED]: "({n} more waiting)",
|
|
343
356
|
[MascotText.ASK_ERR_TOO_LONG]: "Too long, please shorten it",
|
|
@@ -349,6 +362,11 @@ const STRINGS = {
|
|
|
349
362
|
[MascotText.ASK_ERR_INVALID_ANSWER]: "Pick a button or answer the question",
|
|
350
363
|
[MascotText.ASK_ATTACHED]: "Type what to do with the files…",
|
|
351
364
|
[MascotText.ASK_DETACH]: "Remove this file",
|
|
365
|
+
[MascotText.ASK_WORKDIR_CHIP]: "Working in: {name}",
|
|
366
|
+
[MascotText.ASK_WORKDIR_DETACH]: "Stop working in this folder",
|
|
367
|
+
[MascotText.ASK_WORKDIR_SET]: "The bot now works in: {path}",
|
|
368
|
+
[MascotText.ASK_WORKDIR_CLEARED]: "Working folder removed",
|
|
369
|
+
[MascotText.ASK_WORKDIR_DENIED]: "Could not set the working folder: {path}",
|
|
352
370
|
[MascotText.ASK_FILES_LABEL]: "Files on this computer:",
|
|
353
371
|
[MascotText.ASK_FILES_DEFAULT]: "Take a look at these files",
|
|
354
372
|
[MascotText.DROP_ONE]: "Drop to ask about {name}",
|
|
@@ -181,8 +181,8 @@ export function agentEnv(env = process.env) {
|
|
|
181
181
|
return { ...scrubEnv(env), [AGENT_CALL_ENV]: '1' };
|
|
182
182
|
}
|
|
183
183
|
|
|
184
|
-
const HUMAN_ONLY_REMOTE_COMMANDS = new Set(['start', 'pair', 'unpair', 'pause', 'resume', 'enable', 'disable', 'reset-id', 'do']);
|
|
185
|
-
const HUMAN_ONLY_COMMANDS = new Set(['do', 'login', 'logout']);
|
|
184
|
+
const HUMAN_ONLY_REMOTE_COMMANDS = new Set(['start', 'pair', 'unpair', 'pause', 'resume', 'enable', 'disable', 'reset-id', 'do', 'forget-folder']);
|
|
185
|
+
const HUMAN_ONLY_COMMANDS = new Set(['do', 'login', 'logout', 'uninstall']);
|
|
186
186
|
|
|
187
187
|
/** Lệnh chỉ người ngồi trước máy được chạy. `parent` / `name`: lệnh cha + lệnh con của CLI. */
|
|
188
188
|
export function assertHumanCommand(parent, name, env = process.env) {
|
|
@@ -211,7 +211,7 @@ const CATASTROPHIC_COMMANDS = [
|
|
|
211
211
|
const AIVIN_DIR_SEGMENT = /(?:^|[\\/\s'"`~=:(;,|&])\.aivin(?:-release)?(?=[\\/\s'"`);,|&]|$)/i;
|
|
212
212
|
|
|
213
213
|
/** Agent gọi chính CLI Aivin để đổi việc ghép máy / luật / giao việc mới. Khớp chuỗi — `assertHumanCommand` là chốt thứ hai trong tiến trình CLI. */
|
|
214
|
-
const AIVIN_SELF_COMMAND = /\baivin(\.exe)?["']?\s+(remote\s+(pair|start|unpair|pause|resume|enable|disable|reset-id|do)\b|do\b|login\b|logout\b)/i;
|
|
214
|
+
const AIVIN_SELF_COMMAND = /\baivin(\.exe)?["']?\s+(remote\s+(pair|start|unpair|pause|resume|enable|disable|reset-id|do|forget-folder)\b|do\b|login\b|logout\b|uninstall\b|desktop\s+uninstall\b)/i;
|
|
215
215
|
|
|
216
216
|
function mentionsProtectedDir(command) {
|
|
217
217
|
if (AIVIN_DIR_SEGMENT.test(command)) return true;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { lockHolder } from './daemonLock.mjs';
|
|
4
|
+
import { loadDeviceCredentials, saveDeviceCredentials } from './credentials.mjs';
|
|
5
|
+
import { isNetworkPath, resolveInRoots } from './sandbox.mjs';
|
|
6
|
+
import { DeviceErrorCode } from './constants.mjs';
|
|
7
|
+
import { MASCOT_ATTACH_DIR, MASCOT_SETTINGS_FILE, loadMascotSettings, saveMascotSettings } from './mascot/index.mjs';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Thư mục làm việc của ô hỏi (05/11/2026, "Làm việc ở đây với AIVIN Bot"). Yêu cầu tới từ một tệp trong thư mục cấu hình nên không đủ để mở
|
|
11
|
+
* quyền: thư mục nằm ngoài các thư mục được phép thì phải có người bấm đồng ý trên máy, kể cả khi yêu cầu do agent tự ghi qua lệnh shell.
|
|
12
|
+
* Thư mục đã đồng ý nằm ở `extra_roots` của device-credentials.json — `--dir` trong launch.cfg không ghi đè, `aivin remote forget-folder` thu lại.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export const WorkFolderStatus = Object.freeze({ OK: 'ok', NEEDS_ROOT: 'needs_root', DENIED: 'denied' });
|
|
16
|
+
export const WORK_FOLDER_CONFIRM_SEC = 120;
|
|
17
|
+
|
|
18
|
+
const CONFIRM_TEXT = Object.freeze({
|
|
19
|
+
vi: {
|
|
20
|
+
reason: 'Thư mục này nằm ngoài các thư mục Aivin được phép. Chỉ đồng ý nếu chính bạn vừa chọn nó.',
|
|
21
|
+
detail: 'Cho Aivin đọc, ghi và chạy lệnh trong thư mục: {path}',
|
|
22
|
+
},
|
|
23
|
+
en: {
|
|
24
|
+
reason: 'This folder is outside the folders Aivin may use. Only allow it if you just picked it yourself.',
|
|
25
|
+
detail: 'Let Aivin read, write and run commands in this folder: {path}',
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export function workFolderConfirmText(folder, lang = 'en') {
|
|
30
|
+
const text = CONFIRM_TEXT[lang] || CONFIRM_TEXT.en;
|
|
31
|
+
return { reason: text.reason, detail: text.detail.replace('{path}', folder) };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Danh sách thư mục khi chạy: thư mục gốc + các thư mục chủ máy đã đồng ý thêm, không trùng. */
|
|
35
|
+
export function withExtraRoots(base, extra) {
|
|
36
|
+
const list = [...base];
|
|
37
|
+
for (const dir of Array.isArray(extra) ? extra : []) {
|
|
38
|
+
if (typeof dir !== 'string' || !dir) continue;
|
|
39
|
+
if (!list.some((r) => path.resolve(r) === path.resolve(dir))) list.push(path.resolve(dir));
|
|
40
|
+
}
|
|
41
|
+
return list;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const isFolder = (dir) => { try { return fs.statSync(dir).isDirectory(); } catch { return false; } };
|
|
45
|
+
|
|
46
|
+
/** Thư mục còn tồn tại, trong quyền và không phải vùng cấm. */
|
|
47
|
+
export function isAllowedWorkFolder(dir, roots) {
|
|
48
|
+
if (!isFolder(dir)) return false;
|
|
49
|
+
try { resolveInRoots(roots, dir); return true; } catch { return false; }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* `confirm(path)` → có đồng ý mở thêm không; chỉ hỏi khi lý do từ chối đúng là "ngoài quyền". Ổ mạng và thư mục chứa khoá của Aivin thì đồng ý
|
|
54
|
+
* cũng không dùng được (resolveInRoots chặn cứng), nên từ chối luôn. Trả `{ status, path }`; NEEDS_ROOT = đã đồng ý, nơi gọi thêm vào danh sách.
|
|
55
|
+
*/
|
|
56
|
+
export async function resolveWorkFolder(dir, { roots, confirm }) {
|
|
57
|
+
const abs = path.resolve(String(dir));
|
|
58
|
+
if (isNetworkPath(String(dir)) || !isFolder(abs)) return { status: WorkFolderStatus.DENIED, path: abs };
|
|
59
|
+
try {
|
|
60
|
+
resolveInRoots(roots, abs);
|
|
61
|
+
return { status: WorkFolderStatus.OK, path: abs };
|
|
62
|
+
} catch (e) {
|
|
63
|
+
if (e?.code !== DeviceErrorCode.ROOT_DENIED) return { status: WorkFolderStatus.DENIED, path: abs };
|
|
64
|
+
}
|
|
65
|
+
try { resolveInRoots([abs], abs); } catch { return { status: WorkFolderStatus.DENIED, path: abs }; }
|
|
66
|
+
return { status: (await confirm(abs)) ? WorkFolderStatus.NEEDS_ROOT : WorkFolderStatus.DENIED, path: abs };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* `aivin desktop uninstall`: bỏ hết những gì các mục chuột phải để lại — yêu cầu còn chờ, thư mục làm việc đã lưu, quyền thư mục đã đồng ý. Thư mục
|
|
71
|
+
* cấu hình giữ nguyên (lệnh `aivin` và việc ghép máy vẫn còn), nên không dọn thì cài lại là quyền cũ tự có hiệu lực mà không ai hỏi lại.
|
|
72
|
+
*/
|
|
73
|
+
export function clearWorkFolderState({ attachDir = MASCOT_ATTACH_DIR, settingsFile = MASCOT_SETTINGS_FILE } = {}) {
|
|
74
|
+
fs.rmSync(attachDir, { recursive: true, force: true });
|
|
75
|
+
if (loadMascotSettings(settingsFile).work_dir) {
|
|
76
|
+
try { saveMascotSettings({ work_dir: null }, settingsFile); } catch { /* mascot.json hỏng: lần khởi động vẫn kiểm lại quyền trước khi khôi phục */ }
|
|
77
|
+
}
|
|
78
|
+
const creds = loadDeviceCredentials();
|
|
79
|
+
if (!creds?.extra_roots) return;
|
|
80
|
+
const granted = creds.extra_roots.map((p) => path.resolve(p));
|
|
81
|
+
delete creds.extra_roots;
|
|
82
|
+
creds.roots = (creds.roots || []).filter((p) => !granted.includes(path.resolve(p)));
|
|
83
|
+
saveDeviceCredentials(creds);
|
|
84
|
+
const running = lockHolder();
|
|
85
|
+
if (running) try { process.kill(running); } catch { /* vừa tự thoát */ }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** `aivin remote forget-folder`: thu lại thư mục đã đồng ý, dừng daemon để khay mở lại với danh sách mới. Trả `{ removed, stopped }`. */
|
|
89
|
+
export function forgetWorkFolder(dir) {
|
|
90
|
+
const creds = loadDeviceCredentials();
|
|
91
|
+
const abs = path.resolve(String(dir));
|
|
92
|
+
const same = (p) => path.resolve(p) === abs;
|
|
93
|
+
if (!creds?.extra_roots?.some(same)) return { removed: false, stopped: null };
|
|
94
|
+
creds.extra_roots = creds.extra_roots.filter((p) => !same(p));
|
|
95
|
+
creds.roots = (creds.roots || []).filter((p) => !same(p));
|
|
96
|
+
saveDeviceCredentials(creds);
|
|
97
|
+
const stopped = lockHolder();
|
|
98
|
+
if (stopped) try { process.kill(stopped); } catch { /* vừa tự thoát */ }
|
|
99
|
+
return { removed: true, stopped };
|
|
100
|
+
}
|