@aivin-labs/cli 1.0.42 → 1.0.46
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 +19 -2
- package/bin/lib/openFile.mjs +15 -4
- package/bin/lib/remote/client.mjs +97 -45
- package/bin/lib/remote/desktop/castAuth.mjs +244 -0
- package/bin/lib/remote/desktop/castFileDrop.mjs +12 -1
- package/bin/lib/remote/desktop/castInput.mjs +129 -111
- package/bin/lib/remote/desktop/desktopCast.mjs +417 -337
- package/bin/lib/remote/desktop/desktopCastCaptureSource.mjs +362 -0
- package/bin/lib/remote/desktop/desktopCastSource.mjs +1191 -960
- package/bin/lib/remote/desktop/desktopWebRtc.mjs +365 -276
- package/bin/lib/remote/desktop/fileAssociation.mjs +1 -1
- package/bin/lib/remote/desktop/strings.mjs +4 -0
- package/bin/lib/remote/desktop/traySource.mjs +66 -5
- package/bin/lib/remote/desktop/webrtcWorker.mjs +223 -191
- package/bin/lib/remote/mascot/index.mjs +25 -9
- package/package.json +1 -1
|
@@ -201,7 +201,18 @@ export function createCastFileDrop({
|
|
|
201
201
|
// Ổ không phải NTFS không có luồng phụ: bỏ qua, tệp vẫn nhận xong.
|
|
202
202
|
if (process.platform === 'win32') await fs.promises.writeFile(`${dest}:Zone.Identifier`, ZONE_IDENTIFIER).catch(() => undefined);
|
|
203
203
|
reply(viewerId, { t: 'saved', id: active.id, name: path.basename(dest), path: dest });
|
|
204
|
-
onSaved({
|
|
204
|
+
onSaved({
|
|
205
|
+
viewerId,
|
|
206
|
+
path: dest,
|
|
207
|
+
name: path.basename(dest),
|
|
208
|
+
bytes: active.size,
|
|
209
|
+
reveal: msg.reveal === true && !msg.paste,
|
|
210
|
+
paste: msg.paste === true,
|
|
211
|
+
x: msg.x,
|
|
212
|
+
y: msg.y,
|
|
213
|
+
rx: msg.rx,
|
|
214
|
+
ry: msg.ry,
|
|
215
|
+
});
|
|
205
216
|
}
|
|
206
217
|
|
|
207
218
|
function message(viewerId, msg) {
|
|
@@ -1,111 +1,129 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Cổng thao tác chuột / phím từ xa (Desktop Scenecast) — lớp chặn cuối trên máy, không tin BE / trình duyệt: chỉ nhận action đã biết,
|
|
3
|
-
* chỉ chép trường đã biết (trường lạ như `op` không bao giờ xuống tiến trình điều khiển), bỏ khi chủ máy đã tạm dừng, khi AI đang thao tác
|
|
4
|
-
* giao diện, hoặc gửi quá dồn. Thao tác qua kênh WebRTC cũng đi qua đây.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
export const CAST_INPUT_ACTIONS = Object.freeze([
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
if (
|
|
43
|
-
const
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if (
|
|
56
|
-
const
|
|
57
|
-
if (
|
|
58
|
-
if (
|
|
59
|
-
|
|
60
|
-
if (
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Cổng thao tác chuột / phím từ xa (Desktop Scenecast) — lớp chặn cuối trên máy, không tin BE / trình duyệt: chỉ nhận action đã biết,
|
|
3
|
+
* chỉ chép trường đã biết (trường lạ như `op` không bao giờ xuống tiến trình điều khiển), bỏ khi chủ máy đã tạm dừng, khi AI đang thao tác
|
|
4
|
+
* giao diện, hoặc gửi quá dồn. Thao tác qua kênh WebRTC cũng đi qua đây.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export const CAST_INPUT_ACTIONS = Object.freeze([
|
|
8
|
+
'move', 'click', 'dblclick', 'rightclick', 'mousedown', 'mouseup', 'scroll', 'type', 'paste', 'clipboard', 'key', 'hotkey',
|
|
9
|
+
'paste_image', 'paste_file'
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
export const CastInputRejection = Object.freeze({
|
|
13
|
+
INVALID: 'invalid',
|
|
14
|
+
PAUSED: 'paused',
|
|
15
|
+
AI_ACTIVE: 'ai_active',
|
|
16
|
+
RATE_LIMITED: 'rate_limited',
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const ACTIONS = new Set(CAST_INPUT_ACTIONS);
|
|
20
|
+
const MAX_COORD = 100_000;
|
|
21
|
+
const MAX_SCROLL = 10_000;
|
|
22
|
+
/** Toạ độ theo tỷ lệ (rx, ry): 0 = mép trái / trên, 65535 = mép phải / dưới. */
|
|
23
|
+
const RATIO_SCALE = 65_535;
|
|
24
|
+
const MAX_KEY_CHARS = 24;
|
|
25
|
+
const MAX_PASTE_CHARS = 65_536;
|
|
26
|
+
|
|
27
|
+
function finiteIn(value, limit) {
|
|
28
|
+
const n = Number(value);
|
|
29
|
+
return Number.isFinite(n) && Math.abs(n) <= limit ? n : undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function shortStrings(value, maxCount) {
|
|
33
|
+
if (!Array.isArray(value)) return undefined;
|
|
34
|
+
const out = value.filter((k) => typeof k === 'string' && k && k.length <= MAX_KEY_CHARS).slice(0, maxCount);
|
|
35
|
+
return out.length ? out : undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Lọc một thao tác thành lệnh hợp lệ (cùng luật với BE `sanitizeCastInput`); không hợp lệ → null. */
|
|
39
|
+
export function sanitizeCastInput(raw, maxTypeChars = 2000) {
|
|
40
|
+
if (!raw || typeof raw !== 'object') return null;
|
|
41
|
+
const action = String(raw.action || '');
|
|
42
|
+
if (!ACTIONS.has(action)) return null;
|
|
43
|
+
const cmd = { action };
|
|
44
|
+
const x = finiteIn(raw.x, MAX_COORD);
|
|
45
|
+
const y = finiteIn(raw.y, MAX_COORD);
|
|
46
|
+
if (x !== undefined && y !== undefined) { cmd.x = Math.round(x); cmd.y = Math.round(y); }
|
|
47
|
+
const rx = finiteIn(raw.rx, RATIO_SCALE);
|
|
48
|
+
const ry = finiteIn(raw.ry, RATIO_SCALE);
|
|
49
|
+
if (rx >= 0 && ry >= 0) { cmd.rx = Math.round(rx); cmd.ry = Math.round(ry); }
|
|
50
|
+
const button = Number(raw.button);
|
|
51
|
+
if (button === 0 || button === 1 || button === 2) cmd.button = button;
|
|
52
|
+
const deltaX = finiteIn(raw.deltaX, MAX_SCROLL);
|
|
53
|
+
const deltaY = finiteIn(raw.deltaY, MAX_SCROLL);
|
|
54
|
+
if (deltaX !== undefined) cmd.deltaX = Math.round(deltaX);
|
|
55
|
+
if (deltaY !== undefined) cmd.deltaY = Math.round(deltaY);
|
|
56
|
+
const charLimit = action === 'paste' || action === 'clipboard' ? MAX_PASTE_CHARS : maxTypeChars;
|
|
57
|
+
if (typeof raw.text === 'string' && raw.text) cmd.text = raw.text.slice(0, charLimit);
|
|
58
|
+
if (typeof raw.key === 'string' && raw.key && raw.key.length <= MAX_KEY_CHARS) cmd.key = raw.key;
|
|
59
|
+
const keys = shortStrings(raw.keys, 6);
|
|
60
|
+
if (keys) cmd.keys = keys;
|
|
61
|
+
const modifiers = shortStrings(raw.modifiers, 4);
|
|
62
|
+
if (modifiers) cmd.modifiers = modifiers;
|
|
63
|
+
if (action === 'paste_image') {
|
|
64
|
+
if (typeof raw.data !== 'string' || !raw.data) return null;
|
|
65
|
+
cmd.data = raw.data.slice(0, 25_000_000); // Tối đa ~18MB ảnh base64
|
|
66
|
+
if (typeof raw.mime === 'string') cmd.mime = raw.mime.slice(0, 64);
|
|
67
|
+
if (raw.click) cmd.click = true;
|
|
68
|
+
return cmd;
|
|
69
|
+
}
|
|
70
|
+
if (action === 'paste_file') {
|
|
71
|
+
if (typeof raw.path !== 'string' || !raw.path) return null;
|
|
72
|
+
cmd.path = raw.path;
|
|
73
|
+
if (raw.click) cmd.click = true;
|
|
74
|
+
return cmd;
|
|
75
|
+
}
|
|
76
|
+
if ((action === 'type' || action === 'paste' || action === 'clipboard') && !cmd.text) return null;
|
|
77
|
+
if (action === 'key' && !cmd.key) return null;
|
|
78
|
+
if (action === 'hotkey' && !cmd.keys) return null;
|
|
79
|
+
return cmd;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* `isPaused()`: công tắc ngắt của chủ máy. `isAiBusy()`: AI đang chạy công cụ GUI ghi. `accept(raw)` → `{ ok, cmd }` hoặc `{ ok: false, reason }`.
|
|
84
|
+
* `waitHumanIdle()`: công cụ GUI của AI chờ người thôi thao tác (tối đa `maxWaitMs`) trước khi bấm — hai bên không giành chuột cùng lúc.
|
|
85
|
+
*/
|
|
86
|
+
export function createCastInputGate({ isPaused = () => false, isAiBusy = () => false, maxPerSec = 120, now = Date.now, sleep } = {}) {
|
|
87
|
+
let lastHumanAt = null;
|
|
88
|
+
let accepted = 0;
|
|
89
|
+
let window = { start: 0, count: 0 };
|
|
90
|
+
/** Nút chuột người xem đang giữ trên máy: gói nhả của chúng luôn được qua cổng, không thì nút kẹt xuống tới hết phiên. */
|
|
91
|
+
const heldButtons = new Set();
|
|
92
|
+
const wait = sleep || ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
93
|
+
|
|
94
|
+
function accept(raw) {
|
|
95
|
+
const releasing = raw?.action === 'mouseup' && heldButtons.has(Number(raw.button) || 0);
|
|
96
|
+
const blocked = isPaused() ? CastInputRejection.PAUSED : isAiBusy() ? CastInputRejection.AI_ACTIVE : null;
|
|
97
|
+
if (blocked && !releasing) return { ok: false, reason: blocked };
|
|
98
|
+
const t = now();
|
|
99
|
+
if (t - window.start >= 1000) window = { start: t, count: 0 };
|
|
100
|
+
if (++window.count > maxPerSec && !releasing) return { ok: false, reason: CastInputRejection.RATE_LIMITED };
|
|
101
|
+
const cmd = sanitizeCastInput(raw);
|
|
102
|
+
if (!cmd) return { ok: false, reason: CastInputRejection.INVALID };
|
|
103
|
+
if (cmd.action === 'mousedown') heldButtons.add(cmd.button ?? 0);
|
|
104
|
+
if (cmd.action === 'mouseup') heldButtons.delete(cmd.button ?? 0);
|
|
105
|
+
if (blocked) {
|
|
106
|
+
// Chỉ nhả nút, không kéo con trỏ khỏi chỗ AI đang bấm; không tính là người đang thao tác.
|
|
107
|
+
const { x: _x, y: _y, rx: _rx, ry: _ry, ...release } = cmd;
|
|
108
|
+
return { ok: true, cmd: release };
|
|
109
|
+
}
|
|
110
|
+
lastHumanAt = t;
|
|
111
|
+
accepted++;
|
|
112
|
+
return { ok: true, cmd };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function waitHumanIdle({ idleMs = 1500, maxWaitMs = 3000 } = {}) {
|
|
116
|
+
const deadline = now() + maxWaitMs;
|
|
117
|
+
while (lastHumanAt !== null && now() - lastHumanAt < idleMs && now() < deadline) {
|
|
118
|
+
await wait(Math.min(250, Math.max(10, idleMs - (now() - lastHumanAt))));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Số thao tác đã nhận từ lần gọi trước (nhật ký phiên trên máy) — đọc xong đặt lại 0. */
|
|
123
|
+
function takeAcceptedCount() { const n = accepted; accepted = 0; return n; }
|
|
124
|
+
|
|
125
|
+
/** Máy vừa nhả hết nút (`release_all`): AI sắp bấm hoặc người xem đã rời. Trả có đang giữ nút nào không để nơi gọi biết cần gửi lệnh nhả. */
|
|
126
|
+
function takeHeldInput() { const had = heldButtons.size > 0; heldButtons.clear(); return had; }
|
|
127
|
+
|
|
128
|
+
return { accept, waitHumanIdle, takeAcceptedCount, takeHeldInput, lastHumanInputAt: () => lastHumanAt };
|
|
129
|
+
}
|