@aivin-labs/cli 1.0.64 → 1.0.66
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/lib/remote/client.mjs +27 -0
- package/bin/lib/remote/constants.mjs +16 -0
- package/bin/lib/remote/desktop/cscExe.mjs +72 -0
- package/bin/lib/remote/desktop/desktopWebRtc.mjs +479 -436
- package/bin/lib/remote/desktop/humanIdle.mjs +57 -0
- package/bin/lib/remote/desktop/identifyOverlay.mjs +100 -0
- package/bin/lib/remote/desktop/windowProbe.mjs +68 -0
- package/bin/lib/remote/deviceProfile.mjs +4 -1
- package/bin/lib/remote/localLlm.mjs +533 -514
- package/bin/lib/remote/mascot/hostSource.mjs +7 -4
- package/package.json +1 -1
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { execFile } from 'child_process';
|
|
2
|
+
import { createCscExe, runFile } from './cscExe.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Máy rảnh tay bao lâu (16/11/2026): số ms từ lần chuột / phím cuối cùng trên màn hình của máy (Windows `GetLastInputInfo`).
|
|
6
|
+
*
|
|
7
|
+
* Dùng để máy chủ phân biệt "trình duyệt đang mở ngay trên máy này" với "máy khác cùng mạng": hai PC chung một mạng có cùng IP công cộng,
|
|
8
|
+
* nên trùng IP không chứng minh được gì. Người dùng vừa bấm trong trình duyệt cách đây X ms; nếu trình duyệt chạy trên máy này thì cú bấm
|
|
9
|
+
* đó cũng là thao tác trên máy này → máy không thể rảnh lâu hơn X. Rảnh lâu hơn hẳn ⇒ chắc chắn là máy khác. Menu chọn máy trên web cũng
|
|
10
|
+
* dùng con số này để ghi "đang có người dùng" / "rảnh 2 giờ".
|
|
11
|
+
*
|
|
12
|
+
* Đọc bằng exe nhỏ biên dịch một lần (cscExe.mjs), ~30 ms mỗi lần. Không dựng được → `null` (máy chủ coi như không đo được, không chặn).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const SOURCE = `using System; using System.Runtime.InteropServices;
|
|
16
|
+
class AivinIdle {
|
|
17
|
+
[StructLayout(LayoutKind.Sequential)] struct LASTINPUTINFO { public uint cbSize; public uint dwTime; }
|
|
18
|
+
[DllImport("user32.dll")] static extern bool GetLastInputInfo(ref LASTINPUTINFO p);
|
|
19
|
+
[DllImport("kernel32.dll")] static extern uint GetTickCount();
|
|
20
|
+
static int Main() {
|
|
21
|
+
var info = new LASTINPUTINFO(); info.cbSize = (uint)Marshal.SizeOf(info);
|
|
22
|
+
if (!GetLastInputInfo(ref info)) return 1;
|
|
23
|
+
Console.Out.Write(unchecked(GetTickCount() - info.dwTime));
|
|
24
|
+
return 0;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
`;
|
|
28
|
+
|
|
29
|
+
const RUN_TIMEOUT_MS = 3_000;
|
|
30
|
+
const exe = createCscExe({ name: 'aivin-idle', source: SOURCE });
|
|
31
|
+
export const HUMAN_IDLE_HELPER_PATH = exe.path;
|
|
32
|
+
|
|
33
|
+
/** Máy này đọc được thời gian rảnh tay không (chỉ Windows — Desktop Scenecast cũng chỉ có trên Windows). */
|
|
34
|
+
export function humanIdleSupported(platform = process.platform) {
|
|
35
|
+
return platform === 'win32' && process.env.AIVIN_HUMAN_IDLE !== '0';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Dựng sẵn exe (gọi sớm lúc CLI nối để lần hỏi đầu không phải chờ csc). */
|
|
39
|
+
export function prepareHumanIdle({ platform = process.platform } = {}) {
|
|
40
|
+
return humanIdleSupported(platform) ? exe.prepare({ platform }) : Promise.resolve(null);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Số ms rảnh tay, hoặc `null` khi không đọc được (không phải Windows, chưa dựng được exe, exe lỗi / quá hạn). */
|
|
44
|
+
export async function readHumanIdleMs({ platform = process.platform, prepare = prepareHumanIdle, execFileImpl = execFile } = {}) {
|
|
45
|
+
const file = await prepare({ platform });
|
|
46
|
+
if (!file) return null;
|
|
47
|
+
try {
|
|
48
|
+
const out = (await runFile(file, [], { timeout: RUN_TIMEOUT_MS, execFileImpl })).trim();
|
|
49
|
+
const ms = Number(out);
|
|
50
|
+
return /^\d+$/.test(out) && Number.isFinite(ms) ? ms : null;
|
|
51
|
+
} catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Chỉ cho test: quên trạng thái dựng. */
|
|
57
|
+
export function resetHumanIdleForTest() { exe.reset(); }
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { spawn } from 'child_process';
|
|
2
|
+
import { CscTarget, createCscExe } from './cscExe.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* "Máy này đây" (16/11/2026): người dùng rê chuột lên dòng máy trong menu chọn máy trên web → máy hiện tên to giữa mọi màn hình + viền sáng
|
|
6
|
+
* quanh mép màn hình trong vài giây. Hai máy để cạnh nhau, màn hình nào sáng lên là máy đó — không phải đọc tên Windows na ná nhau.
|
|
7
|
+
*
|
|
8
|
+
* Cửa sổ WinForms nổi trên cùng, KHÔNG lấy tiêu điểm (`WS_EX_NOACTIVATE`), chuột bấm xuyên qua (`WS_EX_TRANSPARENT`), không hiện trên thanh
|
|
9
|
+
* tác vụ — người đang làm việc ở máy đó không bị gián đoạn. Exe biên dịch một lần (cscExe.mjs). Chỉ Windows.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const SOURCE = String.raw`using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Text; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices;
|
|
13
|
+
class AivinIdentify : Form {
|
|
14
|
+
[DllImport("user32.dll")] static extern bool SetProcessDPIAware();
|
|
15
|
+
static readonly Color Key = Color.FromArgb(255, 1, 2, 3);
|
|
16
|
+
static readonly Color Accent = Color.FromArgb(255, 59, 130, 246);
|
|
17
|
+
readonly string title; readonly string subtitle;
|
|
18
|
+
AivinIdentify(Rectangle bounds, string title, string subtitle) {
|
|
19
|
+
this.title = title; this.subtitle = subtitle;
|
|
20
|
+
FormBorderStyle = FormBorderStyle.None; ShowInTaskbar = false; TopMost = true; StartPosition = FormStartPosition.Manual;
|
|
21
|
+
Bounds = bounds; BackColor = Key; TransparencyKey = Key; DoubleBuffered = true;
|
|
22
|
+
}
|
|
23
|
+
protected override bool ShowWithoutActivation { get { return true; } }
|
|
24
|
+
protected override CreateParams CreateParams { get { var cp = base.CreateParams; cp.ExStyle |= 0x08000000 | 0x00000080 | 0x00000020 | 0x00080000 | 0x00000008; return cp; } }
|
|
25
|
+
protected override void OnPaint(PaintEventArgs e) {
|
|
26
|
+
var g = e.Graphics; float dpi = g.DpiX / 96f; int edge = (int)(10 * dpi);
|
|
27
|
+
using (var pen = new SolidBrush(Accent)) {
|
|
28
|
+
g.FillRectangle(pen, 0, 0, Width, edge); g.FillRectangle(pen, 0, Height - edge, Width, edge);
|
|
29
|
+
g.FillRectangle(pen, 0, 0, edge, Height); g.FillRectangle(pen, Width - edge, 0, edge, Height);
|
|
30
|
+
}
|
|
31
|
+
using (var big = new Font("Segoe UI", 44, FontStyle.Bold)) using (var small = new Font("Segoe UI", 15)) {
|
|
32
|
+
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
|
|
33
|
+
SizeF t = g.MeasureString(title, big); SizeF s = g.MeasureString(subtitle, small);
|
|
34
|
+
float padX = 56 * dpi, padY = 34 * dpi, gap = 6 * dpi;
|
|
35
|
+
float w = Math.Max(t.Width, s.Width) + padX * 2, h = t.Height + s.Height + gap + padY * 2;
|
|
36
|
+
w = Math.Min(w, Width - edge * 4);
|
|
37
|
+
var card = new RectangleF((Width - w) / 2, (Height - h) / 2, w, h);
|
|
38
|
+
g.SmoothingMode = SmoothingMode.None;
|
|
39
|
+
using (var path = Rounded(card, 22 * dpi)) using (var fill = new SolidBrush(Color.FromArgb(255, 17, 24, 39))) g.FillPath(fill, path);
|
|
40
|
+
using (var bar = new SolidBrush(Accent)) g.FillRectangle(bar, card.X + 22 * dpi, card.Bottom - 5 * dpi, card.Width - 44 * dpi, 5 * dpi);
|
|
41
|
+
var fmt = new StringFormat { Alignment = StringAlignment.Center, Trimming = StringTrimming.EllipsisCharacter, FormatFlags = StringFormatFlags.NoWrap };
|
|
42
|
+
using (var white = new SolidBrush(Color.White)) g.DrawString(title, big, white, new RectangleF(card.X + padX / 2, card.Y + padY, card.Width - padX, t.Height), fmt);
|
|
43
|
+
using (var grey = new SolidBrush(Color.FromArgb(255, 203, 213, 225))) g.DrawString(subtitle, small, grey, new RectangleF(card.X + padX / 2, card.Y + padY + t.Height + gap, card.Width - padX, s.Height), fmt);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
static GraphicsPath Rounded(RectangleF r, float rad) {
|
|
47
|
+
var p = new GraphicsPath(); float d = rad * 2;
|
|
48
|
+
p.AddArc(r.X, r.Y, d, d, 180, 90); p.AddArc(r.Right - d, r.Y, d, d, 270, 90);
|
|
49
|
+
p.AddArc(r.Right - d, r.Bottom - d, d, d, 0, 90); p.AddArc(r.X, r.Bottom - d, d, d, 90, 90); p.CloseFigure(); return p;
|
|
50
|
+
}
|
|
51
|
+
static string Arg(string[] a, int i) { try { return Encoding.UTF8.GetString(Convert.FromBase64String(a[i])); } catch { return ""; } }
|
|
52
|
+
[STAThread] static int Main(string[] a) {
|
|
53
|
+
if (a.Length < 3) return 2;
|
|
54
|
+
try { SetProcessDPIAware(); } catch { }
|
|
55
|
+
string title = Arg(a, 0), subtitle = Arg(a, 1); int ms; if (!int.TryParse(a[2], out ms)) ms = 2500;
|
|
56
|
+
ms = Math.Max(500, Math.Min(ms, 10000));
|
|
57
|
+
Application.EnableVisualStyles();
|
|
58
|
+
foreach (var screen in Screen.AllScreens) new AivinIdentify(screen.Bounds, title, subtitle).Show();
|
|
59
|
+
var timer = new Timer { Interval = ms }; timer.Tick += delegate { timer.Stop(); Application.Exit(); }; timer.Start();
|
|
60
|
+
Application.Run();
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
`;
|
|
65
|
+
|
|
66
|
+
const SUBTITLE = Object.freeze({
|
|
67
|
+
vi: 'Máy tính này trên AIVIN',
|
|
68
|
+
en: 'This computer on AIVIN',
|
|
69
|
+
});
|
|
70
|
+
/** Trần chờ tiến trình tự thoát; quá thì giết (cửa sổ không được kẹt trên màn hình người dùng). */
|
|
71
|
+
const KILL_GRACE_MS = 3_000;
|
|
72
|
+
|
|
73
|
+
const exe = createCscExe({ name: 'aivin-identify', source: SOURCE, refs: ['System.Windows.Forms.dll', 'System.Drawing.dll'], target: CscTarget.WINDOWS });
|
|
74
|
+
let current = null;
|
|
75
|
+
|
|
76
|
+
export function identifySupported(platform = process.platform) {
|
|
77
|
+
return platform === 'win32' && process.env.AIVIN_IDENTIFY !== '0';
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function prepareIdentify({ platform = process.platform } = {}) {
|
|
81
|
+
return identifySupported(platform) ? exe.prepare({ platform }) : Promise.resolve(null);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const b64 = (s) => Buffer.from(String(s ?? ''), 'utf8').toString('base64');
|
|
85
|
+
|
|
86
|
+
/** Hiện tên máy trên mọi màn hình `durationMs` ms. Đang hiện dở thì thay bằng lần mới. Trả `true` khi đã mở được cửa sổ. */
|
|
87
|
+
export async function showIdentify({ name, durationMs = 2500, lang = 'en', platform = process.platform, prepare = prepareIdentify, spawnImpl = spawn } = {}) {
|
|
88
|
+
const file = await prepare({ platform });
|
|
89
|
+
const title = String(name || '').replace(/\s+/g, ' ').trim().slice(0, 80);
|
|
90
|
+
if (!file || !title) return false;
|
|
91
|
+
const ms = Math.max(500, Math.min(Number(durationMs) || 2500, 10_000));
|
|
92
|
+
try { current?.kill(); } catch { /* đã thoát */ }
|
|
93
|
+
const child = spawnImpl(file, [b64(title), b64(SUBTITLE[lang] || SUBTITLE.en), String(ms)], { windowsHide: false, stdio: 'ignore' });
|
|
94
|
+
current = child;
|
|
95
|
+
const killer = setTimeout(() => { try { child.kill(); } catch { /* đã thoát */ } }, ms + KILL_GRACE_MS);
|
|
96
|
+
killer.unref?.();
|
|
97
|
+
child.on?.('exit', () => { clearTimeout(killer); if (current === child) current = null; });
|
|
98
|
+
child.on?.('error', () => { clearTimeout(killer); if (current === child) current = null; });
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { execFile } from 'child_process';
|
|
2
|
+
import { createCscExe, runFile } from './cscExe.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Trình duyệt xem màn hình có đang mở NGAY TRÊN máy này không (16/11/2026) — cách chắc nhất, không phụ thuộc mạng: trang web gắn tạm một mã
|
|
6
|
+
* ngắn vào tiêu đề tab (`AIVIN · #k3f9x2`), máy chủ hỏi máy "có cửa sổ nào mang mã này không". Cửa sổ trình duyệt lấy tiêu đề theo tab đang
|
|
7
|
+
* mở → có cửa sổ đó thì người xem đang ngồi trước chính máy này (mở xem màn hình sẽ thành hình lặp vô tận). Chỉ trả có / không, không lộ tiêu
|
|
8
|
+
* đề cửa sổ nào khác. Trình duyệt nhúng trong app khác (tiêu đề theo app) không bắt được — lưới sau là kiểm địa chỉ WebRTC (desktopWebRtc).
|
|
9
|
+
*
|
|
10
|
+
* Đọc bằng exe nhỏ biên dịch một lần (cscExe.mjs), ~30 ms mỗi lần. Không dựng được → `null` (máy chủ coi như không đo được).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const SOURCE = `using System; using System.Text; using System.Runtime.InteropServices;
|
|
14
|
+
class AivinWindowProbe {
|
|
15
|
+
delegate bool EnumProc(IntPtr hWnd, IntPtr lParam);
|
|
16
|
+
[DllImport("user32.dll")] static extern bool EnumWindows(EnumProc cb, IntPtr lParam);
|
|
17
|
+
[DllImport("user32.dll")] static extern bool IsWindowVisible(IntPtr hWnd);
|
|
18
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern int GetWindowTextLengthW(IntPtr hWnd);
|
|
19
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern int GetWindowTextW(IntPtr hWnd, StringBuilder s, int max);
|
|
20
|
+
static int Main(string[] args) {
|
|
21
|
+
if (args.Length != 1 || args[0].Length < 6) return 2;
|
|
22
|
+
string needle = args[0];
|
|
23
|
+
bool found = false;
|
|
24
|
+
EnumWindows((h, l) => {
|
|
25
|
+
if (!IsWindowVisible(h)) return true;
|
|
26
|
+
int n = GetWindowTextLengthW(h);
|
|
27
|
+
if (n <= 0) return true;
|
|
28
|
+
var sb = new StringBuilder(n + 1);
|
|
29
|
+
GetWindowTextW(h, sb, sb.Capacity);
|
|
30
|
+
if (sb.ToString().IndexOf(needle, StringComparison.Ordinal) >= 0) { found = true; return false; }
|
|
31
|
+
return true;
|
|
32
|
+
}, IntPtr.Zero);
|
|
33
|
+
Console.Out.Write(found ? "1" : "0");
|
|
34
|
+
return 0;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
`;
|
|
38
|
+
|
|
39
|
+
const RUN_TIMEOUT_MS = 3_000;
|
|
40
|
+
/** Mã trang gắn vào tiêu đề: chữ thường + số, 6–16 ký tự (không để trang đưa chuỗi tuỳ ý vào dòng lệnh). */
|
|
41
|
+
export const WINDOW_PROBE_NONCE_RE = /^[a-z0-9]{6,16}$/;
|
|
42
|
+
|
|
43
|
+
const exe = createCscExe({ name: 'aivin-winprobe', source: SOURCE });
|
|
44
|
+
|
|
45
|
+
export function windowProbeSupported(platform = process.platform) {
|
|
46
|
+
return platform === 'win32' && process.env.AIVIN_WINDOW_PROBE !== '0';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Dựng sẵn exe (gọi lúc CLI nối để lần hỏi đầu không phải chờ csc). */
|
|
50
|
+
export function prepareWindowProbe({ platform = process.platform } = {}) {
|
|
51
|
+
return windowProbeSupported(platform) ? exe.prepare({ platform }) : Promise.resolve(null);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Có cửa sổ hiện trên màn hình mang `nonce` trong tiêu đề không — `true` / `false`, hoặc `null` khi không đọc được. */
|
|
55
|
+
export async function findWindowTitle(nonce, { platform = process.platform, prepare = prepareWindowProbe, execFileImpl = execFile } = {}) {
|
|
56
|
+
if (!WINDOW_PROBE_NONCE_RE.test(String(nonce || ''))) return null;
|
|
57
|
+
const file = await prepare({ platform });
|
|
58
|
+
if (!file) return null;
|
|
59
|
+
try {
|
|
60
|
+
const out = (await runFile(file, [String(nonce)], { timeout: RUN_TIMEOUT_MS, execFileImpl })).trim();
|
|
61
|
+
return out === '1' ? true : out === '0' ? false : null;
|
|
62
|
+
} catch {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Chỉ cho test: quên trạng thái dựng. */
|
|
68
|
+
export function resetWindowProbeForTest() { exe.reset(); }
|
|
@@ -5,6 +5,9 @@ import { createHash } from 'crypto';
|
|
|
5
5
|
import { currentCliVersion } from '../update/versions.mjs';
|
|
6
6
|
import { IGNORED_DIRS } from './constants.mjs';
|
|
7
7
|
import { isProtectedPath } from './sandbox.mjs';
|
|
8
|
+
import { humanIdleSupported } from './desktop/humanIdle.mjs';
|
|
9
|
+
import { identifySupported } from './desktop/identifyOverlay.mjs';
|
|
10
|
+
import { windowProbeSupported } from './desktop/windowProbe.mjs';
|
|
8
11
|
|
|
9
12
|
export { REMOTE_CONFIG_DIR } from './constants.mjs';
|
|
10
13
|
|
|
@@ -123,7 +126,7 @@ export function buildDeviceProfile(roots) {
|
|
|
123
126
|
projects: detectProjects(roots),
|
|
124
127
|
desktop_cast: process.platform === 'win32',
|
|
125
128
|
// Giao diện chỉ gửi lệnh mà bản CLI này hiểu; CLI cũ không khai thì giao diện đi đường cũ.
|
|
126
|
-
cast_features: ['paste_html', 'drop_files'],
|
|
129
|
+
cast_features: ['paste_html', 'drop_files', ...(humanIdleSupported() ? ['human_idle'] : []), ...(identifySupported() ? ['identify'] : []), ...(windowProbeSupported() ? ['window_probe'] : []), 'local_viewer_report'],
|
|
127
130
|
};
|
|
128
131
|
}
|
|
129
132
|
|