@aivin-labs/cli 1.0.63 → 1.0.65

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.
@@ -107,6 +107,22 @@ export const DeviceEvent = Object.freeze({
107
107
  DESKTOP_CAST_INPUT_FOCUS: 'device:desktop_cast_input_focus',
108
108
  /** BE → CLI: người xem tạm cầm lái khi AI đang chạy / trả lại — `{ session_id, active, idle_ms, max_wait_ms }`. */
109
109
  DESKTOP_CAST_TAKEOVER: 'device:desktop_cast_takeover',
110
+ /**
111
+ * BE → CLI (16/11/2026): máy rảnh tay bao lâu — ack `{ ok, idle_ms }`. Máy chủ dùng để biết trình duyệt có đang mở ngay trên máy này không
112
+ * (hai PC cùng mạng trùng IP công cộng). Chỉ máy khai `cast_features: ['human_idle']` mới được hỏi.
113
+ */
114
+ HUMAN_IDLE: 'device:human_idle',
115
+ /**
116
+ * BE → CLI (16/11/2026): trình duyệt xem màn hình có đang mở ngay trên máy này không — `{ nonce?, viewer_ips? }`, ack `{ ok, found, own_ip }`:
117
+ * có cửa sổ mang mã trang gắn vào tiêu đề tab / IP trình duyệt đang gắn trên card mạng của máy. Có ⇒ xem sẽ thành hình lặp. Chỉ máy khai
118
+ * `cast_features: ['window_probe']` mới được hỏi.
119
+ */
120
+ WINDOW_PROBE: 'device:window_probe',
121
+ /**
122
+ * BE → CLI (16/11/2026): hiện tên máy to giữa mọi màn hình `{ name, duration_ms }` — người dùng rê chuột lên dòng máy này trong menu chọn
123
+ * máy trên web. Chỉ máy khai `cast_features: ['identify']` mới nhận.
124
+ */
125
+ IDENTIFY: 'device:identify',
110
126
  });
111
127
 
112
128
  /** Trạng thái phiên xem màn hình gửi BE (`device:desktop_cast_status`) — khớp BE `DesktopCastState`. */
@@ -0,0 +1,72 @@
1
+ import { createHash } from 'crypto';
2
+ import { execFile } from 'child_process';
3
+ import fs from 'fs';
4
+ import path from 'path';
5
+ import { REMOTE_CONFIG_DIR } from '../constants.mjs';
6
+
7
+ /**
8
+ * Tệp exe nhỏ biên dịch MỘT lần bằng csc của .NET Framework có sẵn trong Windows (16/11/2026, cùng cách dpapiHelper): chạy ~30 ms thay vì
9
+ * 1–2 s biên dịch Add-Type của PowerShell mỗi lần. Đặt trong `~/.aivin/bin` (thư mục bị chặn với mọi công cụ từ xa), tên mang dấu vân tay mã
10
+ * nguồn nên đổi mã là dựng bản mới. Không dựng được (không phải Windows, máy chặn csc, diệt virus xoá tệp) → `null`, không thử lại trong 24 h.
11
+ */
12
+
13
+ const BIN_DIR = path.join(REMOTE_CONFIG_DIR, 'bin');
14
+ const RETRY_AFTER_FAIL_MS = 24 * 3600_000;
15
+ const BUILD_TIMEOUT_MS = 60_000;
16
+
17
+ export const CscTarget = Object.freeze({ CONSOLE: 'exe', WINDOWS: 'winexe' });
18
+
19
+ export function runFile(file, args, { timeout, execFileImpl = execFile } = {}) {
20
+ return new Promise((resolve, reject) => {
21
+ execFileImpl(file, args, { timeout, windowsHide: true, encoding: 'utf8' }, (error, stdout) => (error ? reject(error) : resolve(String(stdout || ''))));
22
+ });
23
+ }
24
+
25
+ function cscPath(env = process.env) {
26
+ const windir = env.WINDIR || env.SystemRoot || 'C:\\Windows';
27
+ return ['Framework64', 'Framework'].map((d) => path.join(windir, 'Microsoft.NET', d, 'v4.0.30319', 'csc.exe')).find((p) => fs.existsSync(p)) || null;
28
+ }
29
+
30
+ /** @param {{ name: string, source: string, refs?: string[], target?: string }} spec */
31
+ export function createCscExe({ name, source, refs = [], target = CscTarget.CONSOLE }) {
32
+ const version = createHash('sha256').update(source).update(refs.join(',')).update(target).digest('hex').slice(0, 10);
33
+ const exePath = path.join(BIN_DIR, `${name}-${version}.exe`);
34
+ const failedMark = path.join(BIN_DIR, `${name}-${version}.failed`);
35
+ let building = null;
36
+
37
+ const recentlyFailed = () => {
38
+ try { return Date.now() - fs.statSync(failedMark).mtimeMs < RETRY_AFTER_FAIL_MS; } catch { return false; }
39
+ };
40
+
41
+ async function build() {
42
+ const csc = cscPath();
43
+ if (!csc) return null;
44
+ fs.mkdirSync(BIN_DIR, { recursive: true });
45
+ const src = path.join(BIN_DIR, `${name}-${version}.cs`);
46
+ const tmp = `${exePath}.${process.pid}.tmp`;
47
+ try {
48
+ fs.writeFileSync(src, source, 'utf8');
49
+ await runFile(csc, ['-nologo', `-target:${target}`, '-optimize+', ...refs.map((r) => `-r:${r}`), `-out:${tmp}`, src], { timeout: BUILD_TIMEOUT_MS });
50
+ fs.renameSync(tmp, exePath);
51
+ return exePath;
52
+ } catch {
53
+ try { fs.rmSync(tmp, { force: true }); } catch { /* không có thì thôi */ }
54
+ try { fs.writeFileSync(failedMark, new Date().toISOString()); } catch { /* chỉ là dấu */ }
55
+ return fs.existsSync(exePath) ? exePath : null;
56
+ } finally {
57
+ try { fs.rmSync(src, { force: true }); } catch { /* không có thì thôi */ }
58
+ }
59
+ }
60
+
61
+ /** Đường dẫn exe, dựng lần đầu nếu cần (không chặn vòng sự kiện); `null` khi không dựng được. */
62
+ function prepare({ platform = process.platform } = {}) {
63
+ if (platform !== 'win32') return Promise.resolve(null);
64
+ if (building) return building;
65
+ if (fs.existsSync(exePath)) return (building = Promise.resolve(exePath));
66
+ if (recentlyFailed()) return (building = Promise.resolve(null));
67
+ building = build().catch(() => null);
68
+ return building;
69
+ }
70
+
71
+ return { path: exePath, prepare, reset: () => { building = null; } };
72
+ }