@aivin-labs/cli 1.0.51 → 1.0.60

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.
Files changed (32) hide show
  1. package/bin/cli.mjs +2215 -2211
  2. package/bin/lib/auth.mjs +119 -88
  3. package/bin/lib/context.mjs +58 -5
  4. package/bin/lib/loginHandoff.mjs +87 -0
  5. package/bin/lib/openFile.mjs +29 -4
  6. package/bin/lib/remote/agentContract.mjs +370 -0
  7. package/bin/lib/remote/agentEvidence.mjs +409 -0
  8. package/bin/lib/remote/autoStart.mjs +640 -623
  9. package/bin/lib/remote/client.mjs +55 -17
  10. package/bin/lib/remote/codingAgents.mjs +594 -293
  11. package/bin/lib/remote/constants.mjs +1 -1
  12. package/bin/lib/remote/credentials.mjs +131 -125
  13. package/bin/lib/remote/desktop/castAuth.mjs +44 -19
  14. package/bin/lib/remote/desktop/castConsent.mjs +79 -14
  15. package/bin/lib/remote/desktop/castInput.mjs +3 -0
  16. package/bin/lib/remote/desktop/castTrust.mjs +221 -0
  17. package/bin/lib/remote/desktop/desktopCast.mjs +483 -443
  18. package/bin/lib/remote/desktop/desktopCastCaptureSource.mjs +1 -1
  19. package/bin/lib/remote/desktop/desktopCastSource.mjs +324 -40
  20. package/bin/lib/remote/desktop/desktopWebRtc.mjs +59 -4
  21. package/bin/lib/remote/desktop/strings.mjs +26 -0
  22. package/bin/lib/remote/desktop/traySource.mjs +1512 -1385
  23. package/bin/lib/remote/desktop/webrtcWorker.mjs +4 -0
  24. package/bin/lib/remote/dpapiHelper.mjs +91 -0
  25. package/bin/lib/remote/gui/index.mjs +422 -68
  26. package/bin/lib/remote/gui/windowsHostSource.mjs +2418 -1530
  27. package/bin/lib/remote/humanInput.mjs +7 -0
  28. package/bin/lib/remote/localConfirm.mjs +2 -2
  29. package/bin/lib/remote/tokenStore.mjs +19 -2
  30. package/bin/lib/remote/toolHandlers.mjs +861 -859
  31. package/docs/CLI.md +1606 -1602
  32. package/package.json +1 -1
@@ -80,6 +80,12 @@ public static class AivinHumanGuard {
80
80
  keyHook = SetWindowsHookEx(13, keyProc, mod, 0);
81
81
  return mouseHook != IntPtr.Zero && keyHook != IntPtr.Zero;
82
82
  }
83
+ /** Gỡ ngay khi hộp đóng: hook mức thấp làm mọi lần di chuột / gõ phím của cả máy chờ luồng này, tiến trình chưa thoát mà luồng thôi bơm thông điệp là chuột khựng (12/11/2026). */
84
+ public static void Uninstall() {
85
+ if (mouseHook != IntPtr.Zero) { UnhookWindowsHookEx(mouseHook); mouseHook = IntPtr.Zero; }
86
+ if (keyHook != IntPtr.Zero) { UnhookWindowsHookEx(keyHook); keyHook = IntPtr.Zero; }
87
+ window = IntPtr.Zero;
88
+ }
83
89
  public static bool Recent(int ms) { return lastTrusted >= 0 && clock.ElapsedMilliseconds - lastTrusted <= ms; }
84
90
  }
85
91
  `;
@@ -113,5 +119,6 @@ export const HUMAN_GUARD_PS = [
113
119
  "try { Add-Type -TypeDefinition (D 'AIVIN_GUARD_CS') -ErrorAction Stop; $script:guard = $true } catch { }",
114
120
  'function Install-Guard($win) { if (-not $script:guard) { return }; try { $h = (New-Object System.Windows.Interop.WindowInteropHelper($win)).Handle; $tg = [long]0; [void][long]::TryParse([string]$env:AIVIN_GUARD_TAG, [ref]$tg); if (-not [AivinHumanGuard]::Install($h, $tg)) { $script:guard = $false } } catch { $script:guard = $false } }',
115
121
  `function Test-Human { if (-not $script:guard) { return $true }; return [AivinHumanGuard]::Recent(${HUMAN_RECENT_MS}) }`,
122
+ 'function Remove-Guard { try { [AivinHumanGuard]::Uninstall() } catch { } }',
116
123
  "$script:hintLeft = 0; $script:hint = D 'AIVIN_GUARD_HINT'",
117
124
  ];
@@ -140,14 +140,14 @@ const WINDOWS_SCRIPT = [
140
140
  "$script:result = 'No'; $script:closing = $false; $card = $w.FindName('Card')",
141
141
  '$tm = New-Object Windows.Threading.DispatcherTimer; $tm.Interval = [TimeSpan]::FromSeconds(1)',
142
142
  // Đóng có hiệu ứng: chạy storyboard "Out" rồi mới đóng cửa sổ; bấm liên tiếp chỉ tính lần đầu.
143
- "function Finish($r) { if ($script:closing) { return }; $script:closing = $true; $script:result = $r; $tm.Stop(); $sb = $w.FindResource('Out').Clone(); $sb.Add_Completed({ $w.Close() }); $sb.Begin($card) }",
143
+ "function Finish($r) { if ($script:closing) { return }; $script:closing = $true; $script:result = $r; $tm.Stop(); Remove-Guard; $sb = $w.FindResource('Out').Clone(); $sb.Add_Completed({ $w.Close() }); $sb.Begin($card) }",
144
144
  "$w.FindName('Allow').Add_Click({ if (Test-Human) { Finish 'Yes' } else { $script:hintLeft = 3; $cd.Text = $script:hint } })",
145
145
  "$w.FindName('Reject').Add_Click({ Finish 'No' }); $w.FindName('Close').Add_Click({ Finish 'No' })",
146
146
  "$w.FindName('Head').Add_MouseLeftButtonDown({ $w.DragMove() })",
147
147
  "$script:left = [int]$env:AIVIN_C_SECONDS; $fmt = D 'AIVIN_C_COUNTDOWN'; $cd = $w.FindName('Countdown'); $cd.Text = $fmt -f $script:left",
148
148
  "$tm.Add_Tick({ $script:left--; if ($script:hintLeft -gt 0) { $script:hintLeft--; $cd.Text = $script:hint } else { $cd.Text = $fmt -f $script:left }; if ($script:left -le 0) { Finish 'Timeout' } })",
149
149
  "$w.Add_ContentRendered({ Install-Guard $w; $w.FindResource('In').Begin($card); $w.Activate(); [void]$w.FindName('Reject').Focus(); $tm.Start() })",
150
- '[void]$w.ShowDialog(); $tm.Stop(); $script:result',
150
+ '[void]$w.ShowDialog(); Remove-Guard; $tm.Stop(); $script:result',
151
151
  ].join('\n');
152
152
 
153
153
  /** Nội dung chữ thường (macOS / Linux / terminal): lý do, mục đích agent ghi, lệnh. */
@@ -1,5 +1,6 @@
1
1
  import { execFileSync } from 'child_process';
2
2
  import fs from 'fs';
3
+ import { DpapiMode, dpapiHelper, runDpapiHelper } from './dpapiHelper.mjs';
3
4
 
4
5
  /**
5
6
  * Token thiết bị cất trong kho bí mật của hệ điều hành (29/10/2026). Trước đây `~/.aivin/device-credentials.json` giữ token dạng chữ thường
@@ -23,6 +24,22 @@ const DPAPI_UNPROTECT = `Add-Type -AssemblyName System.Security; $e = [Text.Enco
23
24
 
24
25
  const run = (cmd, args, input) => execFileSync(cmd, args, { input, encoding: 'utf8', timeout: 20_000, windowsHide: true, stdio: ['pipe', 'pipe', 'pipe'] }).trim();
25
26
 
27
+ /**
28
+ * DPAPI: ưu tiên exe biên dịch sẵn (dpapiHelper.mjs, ~0,1 s), không có / không chạy được thì PowerShell (~1–4 s). Exe chạy được mà báo lỗi
29
+ * (bản mã của máy / tài khoản khác) thì ném luôn — PowerShell cũng sẽ hỏng y vậy, chỉ tốn thêm vài giây.
30
+ */
31
+ function dpapi(mode, input) {
32
+ const exe = dpapiHelper();
33
+ if (exe) {
34
+ try {
35
+ return runDpapiHelper(exe, mode, input, DPAPI_ENTROPY);
36
+ } catch (e) {
37
+ if (typeof e?.status === 'number') throw e;
38
+ }
39
+ }
40
+ return run('powershell.exe', [...PS, mode === DpapiMode.PROTECT ? DPAPI_PROTECT : DPAPI_UNPROTECT], input);
41
+ }
42
+
26
43
  /** Kho dùng trên máy này. `AIVIN_TOKEN_STORE=file` ép tệp thường (máy không có kho / gỡ lỗi). */
27
44
  export function preferredTokenStore({ platform = process.platform, env = process.env, exists = fs.existsSync } = {}) {
28
45
  const forced = String(env.AIVIN_TOKEN_STORE || '').toLowerCase();
@@ -41,7 +58,7 @@ const quote = (s) => `"${String(s).replace(/["\\]/g, '\\$&')}"`;
41
58
  */
42
59
  export function sealToken(token, { account, store = preferredTokenStore() } = {}) {
43
60
  try {
44
- if (store === TokenStoreKind.DPAPI) return { token_store: store, token_enc: run('powershell.exe', [...PS, DPAPI_PROTECT], token) };
61
+ if (store === TokenStoreKind.DPAPI) return { token_store: store, token_enc: dpapi(DpapiMode.PROTECT, token) };
45
62
  if (store === TokenStoreKind.KEYCHAIN) {
46
63
  run('/usr/bin/security', ['-i'], `add-generic-password -U -a ${quote(account)} -s ${SERVICE} -l "Aivin device token" -w ${quote(token)}\n`);
47
64
  return { token_store: store };
@@ -61,7 +78,7 @@ export function openToken(record) {
61
78
  if (!record) return null;
62
79
  if (record.device_token) return record.device_token;
63
80
  try {
64
- if (record.token_store === TokenStoreKind.DPAPI && record.token_enc) return run('powershell.exe', [...PS, DPAPI_UNPROTECT], record.token_enc) || null;
81
+ if (record.token_store === TokenStoreKind.DPAPI && record.token_enc) return dpapi(DpapiMode.UNPROTECT, record.token_enc) || null;
65
82
  if (record.token_store === TokenStoreKind.KEYCHAIN) return run('/usr/bin/security', ['find-generic-password', '-a', record.token_account || record.device_id, '-s', SERVICE, '-w']) || null;
66
83
  if (record.token_store === TokenStoreKind.LIBSECRET) return run('secret-tool', ['lookup', 'service', SERVICE, 'account', record.token_account || record.device_id]) || null;
67
84
  } catch { /* không giải được */ }