@aivin-labs/cli 1.0.13 → 1.0.21
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/README.md +325 -304
- package/bin/cli.mjs +97 -18
- package/bin/lib/localPreview.mjs +437 -0
- package/bin/lib/openFile.mjs +235 -0
- package/bin/lib/previewCache.mjs +71 -0
- package/bin/lib/remote/atomicFile.mjs +32 -0
- package/bin/lib/remote/autoStart.mjs +2 -1
- package/bin/lib/remote/autoUpdate.mjs +12 -0
- package/bin/lib/remote/callLedger.mjs +21 -0
- package/bin/lib/remote/cdpPolicy.mjs +14 -2
- package/bin/lib/remote/client.mjs +1024 -889
- package/bin/lib/remote/codingAgents.mjs +2 -2
- package/bin/lib/remote/constants.mjs +261 -251
- package/bin/lib/remote/credentials.mjs +81 -4
- package/bin/lib/remote/daemonLock.mjs +57 -17
- package/bin/lib/remote/desktop/castInput.mjs +98 -0
- package/bin/lib/remote/desktop/desktopCast.mjs +334 -198
- package/bin/lib/remote/desktop/desktopCastCaptureSource.mjs +598 -0
- package/bin/lib/remote/desktop/desktopCastSource.mjs +798 -514
- package/bin/lib/remote/desktop/desktopWebRtc.mjs +242 -95
- package/bin/lib/remote/desktop/fileAssociation.mjs +108 -0
- package/bin/lib/remote/desktop/index.mjs +21 -7
- package/bin/lib/remote/desktop/linuxTraySource.mjs +1 -8
- package/bin/lib/remote/desktop/macTraySource.mjs +0 -2
- package/bin/lib/remote/desktop/strings.mjs +28 -2
- package/bin/lib/remote/desktop/traySource.mjs +380 -16
- package/bin/lib/remote/desktop/webrtcRuntime.mjs +137 -0
- package/bin/lib/remote/desktop/webrtcWorker.mjs +181 -0
- package/bin/lib/remote/fileIndexer.mjs +30 -14
- package/bin/lib/remote/folderRules.mjs +3 -0
- package/bin/lib/remote/gitSafe.mjs +31 -0
- package/bin/lib/remote/gui/index.mjs +66 -24
- package/bin/lib/remote/gui/officeHostSource.mjs +635 -393
- package/bin/lib/remote/gui/windowsHost.mjs +3 -1
- package/bin/lib/remote/gui/windowsHostSource.mjs +291 -60
- package/bin/lib/remote/httpTransfer.mjs +64 -3
- package/bin/lib/remote/localPolicy.mjs +73 -7
- package/bin/lib/remote/mascot/ask.mjs +207 -42
- package/bin/lib/remote/mascot/askBlocksSource.mjs +286 -0
- package/bin/lib/remote/mascot/askHostSource.mjs +658 -530
- package/bin/lib/remote/mascot/blocks.mjs +201 -0
- package/bin/lib/remote/mascot/bot.mjs +26 -2
- package/bin/lib/remote/mascot/chartLayout.mjs +382 -0
- package/bin/lib/remote/mascot/diagramRender.mjs +154 -0
- package/bin/lib/remote/mascot/emojiSource.mjs +273 -0
- package/bin/lib/remote/mascot/fileChips.mjs +62 -0
- package/bin/lib/remote/mascot/hostSource.mjs +328 -49
- package/bin/lib/remote/mascot/hotkey.mjs +31 -7
- package/bin/lib/remote/mascot/images.mjs +111 -0
- package/bin/lib/remote/mascot/index.mjs +663 -493
- package/bin/lib/remote/mascot/lucideIcons.mjs +32 -0
- package/bin/lib/remote/mascot/mermaidBundle.mjs +28 -0
- package/bin/lib/remote/mascot/mood.mjs +57 -0
- package/bin/lib/remote/mascot/rigSource.mjs +643 -609
- package/bin/lib/remote/mascot/safeUrl.mjs +29 -0
- package/bin/lib/remote/mascot/strings.mjs +456 -375
- package/bin/lib/remote/mascot/ui.mjs +27 -7
- package/bin/lib/remote/multiUser.mjs +7 -2
- package/bin/lib/remote/notify.mjs +4 -1
- package/bin/lib/remote/office/index.mjs +12 -6
- package/bin/lib/remote/processes.mjs +5 -2
- package/bin/lib/remote/sandbox.mjs +26 -0
- package/bin/lib/remote/secretPaths.mjs +21 -0
- package/bin/lib/remote/tokenRotation.mjs +28 -0
- package/bin/lib/remote/toolHandlers.mjs +79 -15
- package/bin/lib/remote/watchBatcher.mjs +56 -0
- package/bin/lib/selfUpdate.mjs +12 -2
- package/bin/lib/update/binarySwap.mjs +15 -1
- package/bin/lib/update/state.mjs +10 -0
- package/docs/CLI.md +42 -1
- package/docs/media/aivin-ecosystem.jpg +0 -0
- package/package.json +7 -3
|
@@ -15,6 +15,8 @@ import { WINDOWS_HOST_CSHARP, WINDOWS_HOST_PS1 } from './windowsHostSource.mjs';
|
|
|
15
15
|
|
|
16
16
|
const GUI_DIR = path.join(REMOTE_CONFIG_DIR, 'gui');
|
|
17
17
|
const READY_TIMEOUT_MS = 60_000;
|
|
18
|
+
/** PID các bản Excel / Word ẩn host đang giữ (officeHostSource.mjs) — host bị giết giữa chừng thì lần khởi động sau dọn (02/11/2026). */
|
|
19
|
+
export const OFFICE_ORPHANS_FILE = path.join(GUI_DIR, 'office-orphans.txt');
|
|
18
20
|
|
|
19
21
|
function hostFiles() {
|
|
20
22
|
const hash = createHash('sha256').update(WINDOWS_HOST_CSHARP).update(WINDOWS_HOST_PS1).digest('hex').slice(0, 16);
|
|
@@ -54,7 +56,7 @@ export function createWindowsHost({ spawnImpl = spawn } = {}) {
|
|
|
54
56
|
starting = new Promise((resolve, reject) => {
|
|
55
57
|
const files = hostFiles();
|
|
56
58
|
const c = spawnImpl('powershell.exe', ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-File', files.ps1], {
|
|
57
|
-
env: { ...process.env, AIVIN_GUI_SRC: files.src, AIVIN_GUI_DLL: files.dll },
|
|
59
|
+
env: { ...process.env, AIVIN_GUI_SRC: files.src, AIVIN_GUI_DLL: files.dll, AIVIN_GUI_ORPHANS: OFFICE_ORPHANS_FILE },
|
|
58
60
|
windowsHide: true,
|
|
59
61
|
});
|
|
60
62
|
child = c;
|
|
@@ -73,6 +73,8 @@ namespace AivinGui {
|
|
|
73
73
|
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint msg, IntPtr wParam, string lParam, uint flags, uint timeout, out IntPtr result);
|
|
74
74
|
[DllImport("user32.dll")] public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool attach);
|
|
75
75
|
[DllImport("dwmapi.dll")] public static extern int DwmGetWindowAttribute(IntPtr hWnd, int attr, out int value, int size);
|
|
76
|
+
[DllImport("user32.dll")] public static extern IntPtr GetAncestor(IntPtr hWnd, uint flags);
|
|
77
|
+
[DllImport("user32.dll")] public static extern IntPtr WindowFromPoint(POINT p);
|
|
76
78
|
}
|
|
77
79
|
|
|
78
80
|
public class Host {
|
|
@@ -86,7 +88,7 @@ namespace AivinGui {
|
|
|
86
88
|
static readonly JavaScriptSerializer Json = new JavaScriptSerializer { MaxJsonLength = int.MaxValue, RecursionLimit = 200 };
|
|
87
89
|
static readonly object OutputLock = new object();
|
|
88
90
|
static readonly object StateLock = new object();
|
|
89
|
-
static readonly HashSet<string> CancelledIds = new HashSet<string>();
|
|
91
|
+
static readonly HashSet<string> CancelledIds = new HashSet<string>();
|
|
90
92
|
static readonly HashSet<string> ActiveWaits = new HashSet<string>();
|
|
91
93
|
/** Ảnh chụp của lần observe: node_id = "<snap>.<idx>" → phần tử. Giữ vài lần gần nhất, cũ hơn → GUI_STALE_NODE. */
|
|
92
94
|
static readonly Dictionary<int, Dictionary<int, AutomationElement>> Snapshots = new Dictionary<int, Dictionary<int, AutomationElement>>();
|
|
@@ -103,7 +105,14 @@ namespace AivinGui {
|
|
|
103
105
|
* 28/10/2026: người dùng đang gõ ở cửa sổ khác, mọi set_value bị dừng oan 12 lượt).
|
|
104
106
|
*/
|
|
105
107
|
static bool pendingInputCheck = false;
|
|
108
|
+
/** Thao tác đang chạy có kiểm người dùng không (giữ suốt thao tác — pendingInputCheck chỉ cho lần kiểm đầu). */
|
|
109
|
+
static bool userInputCheck = false;
|
|
106
110
|
static long inputGraceMs = 1500;
|
|
111
|
+
/** Giữa các lô gõ: input mới hơn lô AI vừa phát quá ngưỡng này (ms) = của người dùng (lô của AI xử lý xong trong vài ms). */
|
|
112
|
+
const int BATCH_USER_INPUT_MARGIN_MS = 60;
|
|
113
|
+
const int TYPE_BATCH_PAUSE_MS = 80;
|
|
114
|
+
/** Điểm màn hình thật của lần bấm / cuộn / kéo chuột vừa rồi (tính SAU khi đưa cửa sổ lên) — cho screen_point. */
|
|
115
|
+
static Dictionary<string, object> lastMousePoint = null;
|
|
107
116
|
|
|
108
117
|
static void GuardInput() {
|
|
109
118
|
if (!pendingInputCheck) return;
|
|
@@ -112,10 +121,24 @@ namespace AivinGui {
|
|
|
112
121
|
if (idle < inputGraceMs) throw new GuiError("GUI_USER_ACTIVE", "The user is using the mouse/keyboard right now (" + idle + " ms ago) — this action needs the real mouse/keyboard");
|
|
113
122
|
}
|
|
114
123
|
|
|
124
|
+
/**
|
|
125
|
+
* Gõ dài nhiều lô (02/11/2026): người dùng chen vào giữa chừng → dừng. Kiểm con trỏ dời chỗ (gõ phím không di chuột) và input mới hơn lô
|
|
126
|
+
* cuối của AI quá BATCH_USER_INPUT_MARGIN_MS (UserIdleMs coi mọi input trong 150 ms sau lô AI là của AI → không thấy người gõ chen).
|
|
127
|
+
*/
|
|
128
|
+
static bool UserTouchedSince(Native.POINT cursorAtStart) {
|
|
129
|
+
Native.POINT now;
|
|
130
|
+
if (Native.GetCursorPos(out now) && (now.X != cursorAtStart.X || now.Y != cursorAtStart.Y)) return true;
|
|
131
|
+
var info = new Native.LASTINPUTINFO(); info.cbSize = (uint)Marshal.SizeOf(info);
|
|
132
|
+
if (!Native.GetLastInputInfo(ref info) || lastInjectedTick == 0) return false;
|
|
133
|
+
return (int)(info.dwTime - lastInjectedTick) > BATCH_USER_INPUT_MARGIN_MS;
|
|
134
|
+
}
|
|
135
|
+
|
|
115
136
|
static void MoveCursor(int x, int y) { GuardInput(); Native.SetCursorPos(x, y); }
|
|
116
137
|
|
|
117
138
|
public static void Run() {
|
|
118
139
|
try { Native.SetProcessDpiAwarenessContext(new IntPtr(-4)); } catch { }
|
|
140
|
+
// Host trước bị giết giữa lúc mở tệp → Excel / Word ẩn nó tạo còn sống, giữ khoá tệp: dọn (chỉ bản chắc chắn do host tạo).
|
|
141
|
+
try { Office.CleanupOrphans(); } catch { }
|
|
119
142
|
var t = new Thread(Loop);
|
|
120
143
|
t.SetApartmentState(ApartmentState.MTA);
|
|
121
144
|
t.Start();
|
|
@@ -174,8 +197,8 @@ namespace AivinGui {
|
|
|
174
197
|
object reqId = id;
|
|
175
198
|
string reqOp = op;
|
|
176
199
|
var reqArgs = args;
|
|
177
|
-
string reqIdStr = id == null ? null : Convert.ToString(id);
|
|
178
|
-
if (op == "wait_for" && !string.IsNullOrEmpty(reqIdStr)) { lock (CancelledIds) { ActiveWaits.Add(reqIdStr); } }
|
|
200
|
+
string reqIdStr = id == null ? null : Convert.ToString(id);
|
|
201
|
+
if (op == "wait_for" && !string.IsNullOrEmpty(reqIdStr)) { lock (CancelledIds) { ActiveWaits.Add(reqIdStr); } }
|
|
179
202
|
var worker = new Thread(delegate () { try { ExecuteAndWrite(output, reqId, reqOp, reqArgs); } finally { if (reqOp == "wait_for" && !string.IsNullOrEmpty(reqIdStr)) { lock (CancelledIds) { ActiveWaits.Remove(reqIdStr); CancelledIds.Remove(reqIdStr); } } } });
|
|
180
203
|
worker.IsBackground = true;
|
|
181
204
|
worker.SetApartmentState(ApartmentState.MTA);
|
|
@@ -306,8 +329,14 @@ namespace AivinGui {
|
|
|
306
329
|
} catch { return IntPtr.Zero; }
|
|
307
330
|
}
|
|
308
331
|
|
|
309
|
-
|
|
310
|
-
|
|
332
|
+
static IntPtr ResolveWindow(Dictionary<string, object> a) { return ResolveWindow(a, false); }
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Cửa sổ theo handle, hoặc tìm theo tiêu đề / tên tiến trình (chứa, không phân biệt hoa thường); không có → cửa sổ đang focus (chỉ cho
|
|
336
|
+
* thao tác đọc). forInput (act không node_id, 02/11/2026): không nêu cửa sổ thì KHÔNG lấy cửa sổ đang focus — người dùng có thể vừa
|
|
337
|
+
* chuyển sang cửa sổ khác, phím / chuột rơi nhầm chỗ. Ngoại lệ: hỏi từ linh vật → cửa sổ chủ máy dùng ngay trước đó ("fallback").
|
|
338
|
+
*/
|
|
339
|
+
static IntPtr ResolveWindow(Dictionary<string, object> a, bool forInput) {
|
|
311
340
|
string w = Str(a, "window");
|
|
312
341
|
IntPtr h = ParseWindow(w);
|
|
313
342
|
if (h != IntPtr.Zero) {
|
|
@@ -330,9 +359,12 @@ namespace AivinGui {
|
|
|
330
359
|
uint fgPid; Native.GetWindowThreadProcessId(fg, out fgPid);
|
|
331
360
|
if (fgPid == avoid) {
|
|
332
361
|
IntPtr back = ParseWindow(Str(a, "fallback"));
|
|
333
|
-
|
|
362
|
+
if (back != IntPtr.Zero && Native.IsWindow(back)) return back;
|
|
363
|
+
if (forInput) throw new GuiError("INVALID_ARGS", "Name the window for this action (window: handle 0x… or a title/process)");
|
|
364
|
+
return IntPtr.Zero;
|
|
334
365
|
}
|
|
335
366
|
}
|
|
367
|
+
if (forInput) throw new GuiError("INVALID_ARGS", "Name the window for this action (window: handle 0x… or a title/process) — input is never sent to whatever window happens to be focused");
|
|
336
368
|
return fg;
|
|
337
369
|
}
|
|
338
370
|
|
|
@@ -354,15 +386,94 @@ namespace AivinGui {
|
|
|
354
386
|
return Native.GetForegroundWindow() == prev;
|
|
355
387
|
}
|
|
356
388
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
389
|
+
const uint GA_ROOT = 2, GA_ROOTOWNER = 3;
|
|
390
|
+
const int GWL_STYLE = -16;
|
|
391
|
+
const int WS_POPUP = unchecked((int)0x80000000), WS_CAPTION = 0x00C00000;
|
|
392
|
+
|
|
393
|
+
static IntPtr RootOf(IntPtr h) {
|
|
394
|
+
if (h == IntPtr.Zero) return IntPtr.Zero;
|
|
395
|
+
IntPtr r = Native.GetAncestor(h, GA_ROOT);
|
|
396
|
+
return r == IntPtr.Zero ? h : r;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
static IntPtr RootOwnerOf(IntPtr h) {
|
|
400
|
+
if (h == IntPtr.Zero) return IntPtr.Zero;
|
|
401
|
+
IntPtr r = Native.GetAncestor(h, GA_ROOTOWNER);
|
|
402
|
+
return r == IntPtr.Zero ? RootOf(h) : r;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/** w thuộc cửa sổ đích: cùng cửa sổ gốc, hoặc hộp thoại / popup cùng cây sở hữu (hộp thoại modal của đích lên trên cùng là đúng). */
|
|
406
|
+
static bool BelongsTo(IntPtr w, IntPtr target) {
|
|
407
|
+
if (w == IntPtr.Zero || target == IntPtr.Zero) return false;
|
|
408
|
+
IntPtr tr = RootOf(target), wr = RootOf(w);
|
|
409
|
+
if (wr == tr) return true;
|
|
410
|
+
IntPtr two = RootOwnerOf(target), wo = RootOwnerOf(w);
|
|
411
|
+
return wo == tr || wo == two || wr == two;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** Điểm màn hình (x, y) hiện đang thuộc cửa sổ đích — không bị cửa sổ khác che. Menu Win32 / popup không khung cùng tiến trình cũng tính. */
|
|
415
|
+
static bool PointOnWindow(int x, int y, IntPtr target) {
|
|
416
|
+
var p = new Native.POINT { X = x, Y = y };
|
|
417
|
+
IntPtr hit = Native.WindowFromPoint(p);
|
|
418
|
+
if (BelongsTo(hit, target)) return true;
|
|
419
|
+
IntPtr root = RootOf(hit);
|
|
420
|
+
if (root == IntPtr.Zero) return false;
|
|
421
|
+
uint hp, tp;
|
|
422
|
+
Native.GetWindowThreadProcessId(root, out hp);
|
|
423
|
+
Native.GetWindowThreadProcessId(target, out tp);
|
|
424
|
+
if (hp != tp) return false;
|
|
425
|
+
int style = Native.GetWindowLong(root, GWL_STYLE);
|
|
426
|
+
return ClassOf(root) == "#32768" || ((style & WS_POPUP) != 0 && (style & WS_CAPTION) != WS_CAPTION);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** Bấm chuột thật: điểm bị cửa sổ khác che → không bấm (bấm sẽ rơi vào cửa sổ đang che, có thể là việc riêng của người dùng). */
|
|
430
|
+
static void AssertPointOnWindow(int x, int y, IntPtr target) {
|
|
431
|
+
if (PointOnWindow(x, y, target)) return;
|
|
432
|
+
IntPtr hit = Native.WindowFromPoint(new Native.POINT { X = x, Y = y });
|
|
433
|
+
uint pid = 0;
|
|
434
|
+
if (hit != IntPtr.Zero) Native.GetWindowThreadProcessId(hit, out pid);
|
|
435
|
+
string who = pid == 0 ? "nothing" : ProcessName(pid);
|
|
436
|
+
throw new GuiError("GUI_FOCUS_LOST", "Another window (" + who + ") is on top of the target point — nothing was clicked. Observe again, or use an accessibility action (click by node_id / set_value)");
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Đưa cửa sổ lên trên cùng (khôi phục nếu thu nhỏ). Trả true khi cửa sổ trên cùng đúng là đích (hoặc hộp thoại của nó) — Windows có thể từ
|
|
441
|
+
* chối SetForegroundWindow (tiến trình khác vừa có input, khoá đổi cửa sổ) mà không báo lỗi (02/11/2026: trước đây vẫn gõ / bấm tiếp).
|
|
442
|
+
*/
|
|
443
|
+
static bool ForceForeground(IntPtr h) {
|
|
444
|
+
if (h == IntPtr.Zero) return false;
|
|
445
|
+
if (Native.IsIconic(h)) { Native.ShowWindow(h, 9); Thread.Sleep(120); }
|
|
446
|
+
if (Native.GetForegroundWindow() == h) return true;
|
|
360
447
|
GuardInput();
|
|
361
448
|
// Windows chỉ cho đổi cửa sổ trước khi tiến trình vừa có input → nhấn nhả Alt (không tạo ký tự) rồi đặt.
|
|
362
449
|
KeyTap(0x12);
|
|
363
450
|
Native.SetForegroundWindow(h);
|
|
364
451
|
Native.BringWindowToTop(h);
|
|
365
452
|
Thread.Sleep(80);
|
|
453
|
+
if (BelongsTo(Native.GetForegroundWindow(), h)) return true;
|
|
454
|
+
// Lần 2: gắn tạm luồng input với cửa sổ đang ở trên cùng rồi đặt lại.
|
|
455
|
+
IntPtr cur = Native.GetForegroundWindow();
|
|
456
|
+
uint pid;
|
|
457
|
+
uint curThread = cur == IntPtr.Zero ? 0 : Native.GetWindowThreadProcessId(cur, out pid);
|
|
458
|
+
uint me = Native.GetCurrentThreadId();
|
|
459
|
+
bool attached = curThread != 0 && curThread != me && Native.AttachThreadInput(me, curThread, true);
|
|
460
|
+
try { Native.SetForegroundWindow(h); Native.BringWindowToTop(h); } finally { if (attached) Native.AttachThreadInput(me, curThread, false); }
|
|
461
|
+
Thread.Sleep(80);
|
|
462
|
+
return BelongsTo(Native.GetForegroundWindow(), h);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/** Chuột / phím thật cần cửa sổ đích ở trên cùng; không đưa lên được → dừng, KHÔNG phát input vào cửa sổ đang ở trên. */
|
|
466
|
+
static void RequireForeground(IntPtr h) {
|
|
467
|
+
if (h == IntPtr.Zero) throw new GuiError("INVALID_ARGS", "Name the window for real mouse/keyboard input (window: handle 0x… or a title/process)");
|
|
468
|
+
if (!ForceForeground(h)) {
|
|
469
|
+
throw new GuiError("GUI_FOCUS_LOST", "Could not bring the target window to the front (Windows refused, or another window took focus) — nothing was clicked or typed. Prefer an accessibility action (set_value / click by node_id), or ask the user to switch to that window");
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/** Giữa lúc gõ / bấm phím: cửa sổ trên cùng vẫn là đích, không thì dừng (phím sẽ rơi vào cửa sổ khác). */
|
|
474
|
+
static void AssertStillForeground(IntPtr h, string done) {
|
|
475
|
+
if (h == IntPtr.Zero || BelongsTo(Native.GetForegroundWindow(), h)) return;
|
|
476
|
+
throw new GuiError("GUI_FOCUS_LOST", "Another window took focus — stopped" + (string.IsNullOrEmpty(done) ? "" : " after " + done));
|
|
366
477
|
}
|
|
367
478
|
|
|
368
479
|
static object FocusWindowOp(Dictionary<string, object> a) {
|
|
@@ -466,7 +577,16 @@ namespace AivinGui {
|
|
|
466
577
|
int maxNodes = Math.Max(20, Math.Min(Int(a, "max_nodes", 300), 1500));
|
|
467
578
|
int maxDepth = Math.Max(2, Math.Min(Int(a, "max_depth", 25), 60));
|
|
468
579
|
var sw = Stopwatch.StartNew();
|
|
469
|
-
AssertWindowResponsive(h);
|
|
580
|
+
AssertWindowResponsive(h);
|
|
581
|
+
if (Native.IsIconic(h)) {
|
|
582
|
+
IntPtr fgBefore = Native.GetForegroundWindow();
|
|
583
|
+
Native.ShowWindow(h, 4);
|
|
584
|
+
if (fgBefore != IntPtr.Zero && fgBefore != h) {
|
|
585
|
+
RestoreForeground(fgBefore, h);
|
|
586
|
+
SendToBack(h);
|
|
587
|
+
}
|
|
588
|
+
Thread.Sleep(60);
|
|
589
|
+
}
|
|
470
590
|
AutomationElement root = AutomationElement.FromHandle(h);
|
|
471
591
|
var cache = BuildCache();
|
|
472
592
|
var nodes = new List<object>();
|
|
@@ -524,12 +644,19 @@ namespace AivinGui {
|
|
|
524
644
|
bool wantOcr = Bool(a, "ocr", quality == "poor");
|
|
525
645
|
if (wantImage || wantOcr) {
|
|
526
646
|
var sw2 = Stopwatch.StartNew();
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
647
|
+
string imageError;
|
|
648
|
+
var bmp = GrabWindow(h, out imageError);
|
|
649
|
+
if (bmp == null) {
|
|
650
|
+
res["image_error"] = imageError;
|
|
651
|
+
if (wantOcr) res["ocr"] = new Dictionary<string, object> { { "error", "no usable screenshot: " + imageError } };
|
|
652
|
+
} else {
|
|
653
|
+
using (bmp) {
|
|
654
|
+
if (wantImage) {
|
|
655
|
+
res["image"] = EncodeImage(bmp, Int(a, "image_max_width", 1366), Int(a, "image_quality", 70));
|
|
656
|
+
res["image_ms"] = sw2.ElapsedMilliseconds;
|
|
657
|
+
}
|
|
658
|
+
if (wantOcr) AddOcrNodes(res, nodes, snap, bmp, Str(a, "ocr_lang"), Int(a, "ocr_max_nodes", 200));
|
|
531
659
|
}
|
|
532
|
-
if (wantOcr) AddOcrNodes(res, nodes, snap, bmp, Str(a, "ocr_lang"), Int(a, "ocr_max_nodes", 200));
|
|
533
660
|
}
|
|
534
661
|
}
|
|
535
662
|
return res;
|
|
@@ -537,20 +664,44 @@ namespace AivinGui {
|
|
|
537
664
|
|
|
538
665
|
static uint SafeDpi(IntPtr h) { try { return Native.GetDpiForWindow(h); } catch { return 96; } }
|
|
539
666
|
|
|
540
|
-
|
|
667
|
+
/**
|
|
668
|
+
* Ảnh cửa sổ bằng PrintWindow (chụp đúng cửa sổ kể cả khi bị che). PrintWindow hỏng / ảnh đen (vài ứng dụng GPU) → chụp vùng màn hình,
|
|
669
|
+
* NHƯNG chỉ khi cửa sổ đích đang trên cùng và không bị che (02/11/2026): chụp vùng màn hình lấy cả cửa sổ khác đang đè lên (lộ việc riêng
|
|
670
|
+
* của người dùng, VLM chọn toạ độ trên cửa sổ khác). Không an toàn → null + lý do (image_error), không trả ảnh sai.
|
|
671
|
+
*/
|
|
672
|
+
static Bitmap GrabWindow(IntPtr h, out string error) {
|
|
673
|
+
error = null;
|
|
541
674
|
Native.RECT r; Native.GetWindowRect(h, out r);
|
|
542
675
|
int w = Math.Max(1, r.Right - r.Left), ht = Math.Max(1, r.Bottom - r.Top);
|
|
543
676
|
var bmp = new Bitmap(w, ht, PixelFormat.Format32bppArgb);
|
|
544
677
|
bool ok = false;
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
678
|
+
try {
|
|
679
|
+
using (var g = Graphics.FromImage(bmp)) {
|
|
680
|
+
IntPtr hdc = g.GetHdc();
|
|
681
|
+
try { ok = Native.PrintWindow(h, hdc, 2); } finally { g.ReleaseHdc(hdc); }
|
|
682
|
+
if (!ok || IsMostlyBlack(bmp)) {
|
|
683
|
+
error = ScreenCopyBlocker(h, r);
|
|
684
|
+
if (error == null) g.CopyFromScreen(r.Left, r.Top, 0, 0, new Size(w, ht));
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
} catch { bmp.Dispose(); throw; }
|
|
688
|
+
if (error != null) { bmp.Dispose(); return null; }
|
|
551
689
|
return bmp;
|
|
552
690
|
}
|
|
553
691
|
|
|
692
|
+
/** null = chụp vùng màn hình của cửa sổ an toàn (đích trên cùng, tâm + lưới 3×3 điểm đều thuộc đích); không thì lý do. */
|
|
693
|
+
static string ScreenCopyBlocker(IntPtr h, Native.RECT r) {
|
|
694
|
+
if (Native.IsIconic(h)) return "the window is minimized and cannot be captured";
|
|
695
|
+
if (!BelongsTo(Native.GetForegroundWindow(), h)) return "this app cannot be captured in the background and the window is not in front — bring it to the front (gui.focus) to get a screenshot";
|
|
696
|
+
int inset = 8;
|
|
697
|
+
int[] xs = { r.Left + inset, (r.Left + r.Right) / 2, r.Right - inset };
|
|
698
|
+
int[] ys = { r.Top + inset, (r.Top + r.Bottom) / 2, r.Bottom - inset };
|
|
699
|
+
foreach (int x in xs) foreach (int y in ys) {
|
|
700
|
+
if (!PointOnWindow(x, y, h)) return "another window covers part of this window, so a screen capture would show it — bring the window to the front (gui.focus) to get a screenshot";
|
|
701
|
+
}
|
|
702
|
+
return null;
|
|
703
|
+
}
|
|
704
|
+
|
|
554
705
|
static Dictionary<string, object> EncodeImage(Bitmap bmp, int maxWidth, int quality) {
|
|
555
706
|
int w = bmp.Width, ht = bmp.Height;
|
|
556
707
|
double scale = w > maxWidth ? (double)maxWidth / w : 1.0;
|
|
@@ -597,12 +748,30 @@ namespace AivinGui {
|
|
|
597
748
|
|
|
598
749
|
static bool IsMostlyBlack(Bitmap b) {
|
|
599
750
|
int dark = 0, total = 0;
|
|
600
|
-
|
|
601
|
-
|
|
751
|
+
int w = b.Width, h = b.Height;
|
|
752
|
+
if (w <= 0 || h <= 0) return true;
|
|
753
|
+
var data = b.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
|
|
754
|
+
try {
|
|
755
|
+
int stride = Math.Abs(data.Stride);
|
|
756
|
+
byte[] bytes = new byte[stride * h];
|
|
757
|
+
Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
|
|
758
|
+
int stepX = Math.Max(1, w / 16), stepY = Math.Max(1, h / 16);
|
|
759
|
+
for (int y = 0; y < h; y += stepY) {
|
|
760
|
+
int row = y * stride;
|
|
761
|
+
for (int x = 0; x < w; x += stepX) {
|
|
762
|
+
int idx = row + x * 4;
|
|
763
|
+
total++;
|
|
764
|
+
if (bytes[idx + 2] < 8 && bytes[idx + 1] < 8 && bytes[idx] < 8) dark++;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
} finally { b.UnlockBits(data); }
|
|
768
|
+
// Đã thay GetPixel bằng LockBits + Marshal.Copy để đọc mảng byte trực tiếp trên bộ nhớ.
|
|
769
|
+
/*
|
|
602
770
|
var c = b.GetPixel(x, y); total++;
|
|
603
771
|
if (c.R < 8 && c.G < 8 && c.B < 8) dark++;
|
|
604
772
|
}
|
|
605
773
|
}
|
|
774
|
+
*/
|
|
606
775
|
return total > 0 && dark * 10 >= total * 9;
|
|
607
776
|
}
|
|
608
777
|
|
|
@@ -663,12 +832,16 @@ namespace AivinGui {
|
|
|
663
832
|
string method = Str(a, "method") ?? "auto";
|
|
664
833
|
inputGraceMs = Int(a, "user_input_grace_ms", 1500);
|
|
665
834
|
pendingInputCheck = Bool(a, "check_user_input", true);
|
|
835
|
+
userInputCheck = pendingInputCheck;
|
|
836
|
+
lastMousePoint = null;
|
|
666
837
|
if (!InteractiveDesktop()) throw new GuiError("GUI_LOCKED", "The screen is locked or a secure prompt is showing");
|
|
667
838
|
AutomationElement el = null;
|
|
668
839
|
IntPtr window = IntPtr.Zero;
|
|
669
840
|
ocrPoint = null;
|
|
841
|
+
ocrNodeId = null;
|
|
670
842
|
if (!string.IsNullOrEmpty(nodeId) && nodeId.Contains(".t")) {
|
|
671
843
|
ocrPoint = OcrPointOf(nodeId, out window);
|
|
844
|
+
ocrNodeId = nodeId;
|
|
672
845
|
if (action != "click" && action != "double_click" && action != "right_click" && action != "set_value" && action != "type") {
|
|
673
846
|
throw new GuiError("INVALID_ARGS", "Text read from the screenshot (OCR) supports click / double_click / right_click / set_value / type only");
|
|
674
847
|
}
|
|
@@ -680,6 +853,7 @@ namespace AivinGui {
|
|
|
680
853
|
if (nearby != null) {
|
|
681
854
|
el = nearby;
|
|
682
855
|
ocrPoint = null;
|
|
856
|
+
ocrNodeId = null;
|
|
683
857
|
} else {
|
|
684
858
|
MouseAt(null, window, a, MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_LEFTUP, 1);
|
|
685
859
|
method = "mouse";
|
|
@@ -687,14 +861,19 @@ namespace AivinGui {
|
|
|
687
861
|
}
|
|
688
862
|
}
|
|
689
863
|
else if (!string.IsNullOrEmpty(nodeId)) el = NodeOf(nodeId, out window);
|
|
690
|
-
else window = ResolveWindow(a);
|
|
864
|
+
else window = ResolveWindow(a, true);
|
|
691
865
|
string beforeTitle = window == IntPtr.Zero ? "" : WindowText(window);
|
|
692
866
|
if (el != null) {
|
|
693
867
|
bool isPassword = false;
|
|
694
868
|
try { isPassword = el.Current.IsPassword; } catch (ElementNotAvailableException) { throw new GuiError("GUI_STALE_NODE", "Node " + nodeId + " is gone — observe again"); }
|
|
695
869
|
if (isPassword && (action == "type" || action == "set_value")) throw new GuiError("GUI_PASSWORD_FIELD", "Typing into a password field is not allowed — ask the user to type it");
|
|
696
870
|
}
|
|
697
|
-
AssertWindowResponsive(window);
|
|
871
|
+
AssertWindowResponsive(window);
|
|
872
|
+
Dictionary<string, object> screenPoint = null;
|
|
873
|
+
try {
|
|
874
|
+
var sp = PointFor(el, window, a);
|
|
875
|
+
screenPoint = new Dictionary<string, object> { { "x", (int)Math.Round(sp.X) }, { "y", (int)Math.Round(sp.Y) } };
|
|
876
|
+
} catch { }
|
|
698
877
|
IntPtr fgBefore = Native.GetForegroundWindow();
|
|
699
878
|
string used = DoAction(action, method, el, window, a);
|
|
700
879
|
// Thao tác theo pattern không cần cửa sổ đích ở trên cùng, nhưng nhiều ứng dụng (WinForms qua MSAA) tự lấy focus khi bị SetValue /
|
|
@@ -708,6 +887,8 @@ namespace AivinGui {
|
|
|
708
887
|
int defaultSettle = used == "mouse" || used == "keyboard" ? 250 : 30;
|
|
709
888
|
Thread.Sleep(Math.Max(0, Math.Min(Int(a, "settle_ms", defaultSettle), 3000)));
|
|
710
889
|
var res = new Dictionary<string, object> { { "action", action }, { "method", used } };
|
|
890
|
+
if (lastMousePoint != null) screenPoint = lastMousePoint;
|
|
891
|
+
if (screenPoint != null) res["screen_point"] = screenPoint;
|
|
711
892
|
if (restored) res["focus_returned_to_user"] = true;
|
|
712
893
|
if (el != null) res["target"] = Live(el);
|
|
713
894
|
IntPtr fg = Native.GetForegroundWindow();
|
|
@@ -762,16 +943,16 @@ namespace AivinGui {
|
|
|
762
943
|
if (TryBackgroundSetText(el, text)) return "background";
|
|
763
944
|
}
|
|
764
945
|
FocusTarget(el, window);
|
|
765
|
-
KeyCombo("ctrl+a");
|
|
766
|
-
TypeText(text);
|
|
946
|
+
KeyCombo("ctrl+a", window);
|
|
947
|
+
TypeText(text, window);
|
|
767
948
|
return "keyboard";
|
|
768
949
|
}
|
|
769
950
|
case "type": {
|
|
770
951
|
string text = Str(a, "text") ?? "";
|
|
771
952
|
if (allowPattern && TryBackgroundType(el, text)) return "background";
|
|
772
|
-
FocusTarget(el, window); TypeText(text); return "keyboard";
|
|
953
|
+
FocusTarget(el, window); TypeText(text, window); return "keyboard";
|
|
773
954
|
}
|
|
774
|
-
case "key": FocusTarget(el, window); KeyCombo(Str(a, "keys") ?? ""); return "keyboard";
|
|
955
|
+
case "key": FocusTarget(el, window); KeyCombo(Str(a, "keys") ?? "", window); return "keyboard";
|
|
775
956
|
case "scroll": {
|
|
776
957
|
string dir = (Str(a, "direction") ?? "down").ToLowerInvariant();
|
|
777
958
|
int amount = Math.Max(1, Math.Min(Int(a, "amount", 3), 30));
|
|
@@ -787,9 +968,11 @@ namespace AivinGui {
|
|
|
787
968
|
return "scroll";
|
|
788
969
|
}
|
|
789
970
|
}
|
|
971
|
+
RequireForeground(window);
|
|
790
972
|
var p = PointFor(el, window, a);
|
|
791
|
-
|
|
973
|
+
AssertPointOnWindow((int)p.X, (int)p.Y, window);
|
|
792
974
|
MoveCursor((int)p.X, (int)p.Y);
|
|
975
|
+
lastMousePoint = new Dictionary<string, object> { { "x", (int)p.X }, { "y", (int)p.Y } };
|
|
793
976
|
int delta = (dir == "up" ? 120 : -120) * amount;
|
|
794
977
|
var inp = MouseInput(MOUSEEVENTF_WHEEL, 0, 0, delta);
|
|
795
978
|
Send(new[] { inp });
|
|
@@ -838,11 +1021,14 @@ namespace AivinGui {
|
|
|
838
1021
|
return "expand";
|
|
839
1022
|
}
|
|
840
1023
|
case "drag": {
|
|
841
|
-
var start = PointFor(el, window, a);
|
|
842
1024
|
var toD = Get(a, "to") as Dictionary<string, object>;
|
|
843
1025
|
if (toD == null) throw new GuiError("INVALID_ARGS", "drag needs to {x,y}");
|
|
1026
|
+
RequireForeground(window);
|
|
1027
|
+
var start = PointFor(el, window, a);
|
|
844
1028
|
var to = WindowPoint(window, a, toD);
|
|
845
|
-
|
|
1029
|
+
AssertPointOnWindow((int)start.X, (int)start.Y, window);
|
|
1030
|
+
AssertPointOnWindow((int)to.X, (int)to.Y, window);
|
|
1031
|
+
lastMousePoint = new Dictionary<string, object> { { "x", (int)start.X }, { "y", (int)start.Y } };
|
|
846
1032
|
MoveCursor((int)start.X, (int)start.Y);
|
|
847
1033
|
Send(new[] { MouseInput(MOUSEEVENTF_LEFTDOWN, 0, 0, 0) });
|
|
848
1034
|
for (int i = 1; i <= 10; i++) {
|
|
@@ -864,22 +1050,22 @@ namespace AivinGui {
|
|
|
864
1050
|
return AutomationElement.RootElement.FindFirst(TreeScope.Descendants, new AndCondition(cond, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem)));
|
|
865
1051
|
}
|
|
866
1052
|
|
|
867
|
-
const uint WM_SETTEXT = 0x000C, WM_CHAR = 0x0102, SMTO_ABORTIFHUNG = 0x0002;
|
|
868
|
-
|
|
869
|
-
/** Kiểm tra nhanh nhịp phản hồi của cửa sổ bằng WM_NULL + SMTO_ABORTIFHUNG để tránh UI Automation treo cứng khi ứng dụng đích bị đơ. */
|
|
870
|
-
static bool IsWindowResponsive(IntPtr h, uint timeoutMs) {
|
|
871
|
-
if (h == IntPtr.Zero || !Native.IsWindow(h)) return false;
|
|
872
|
-
IntPtr r;
|
|
873
|
-
return Native.SendMessageTimeout(h, 0, IntPtr.Zero, null, SMTO_ABORTIFHUNG, timeoutMs, out r) != IntPtr.Zero;
|
|
874
|
-
}
|
|
875
|
-
|
|
876
|
-
static void AssertWindowResponsive(IntPtr h) {
|
|
877
|
-
if (h == IntPtr.Zero) return;
|
|
878
|
-
if (!Native.IsWindow(h)) throw new GuiError("GUI_WINDOW_GONE", "The target window has closed");
|
|
879
|
-
if (!IsWindowResponsive(h, 500)) {
|
|
880
|
-
if (!Native.IsWindow(h)) throw new GuiError("GUI_WINDOW_GONE", "The target window has closed");
|
|
881
|
-
throw new GuiError("GUI_WINDOW_HUNG", "The target window is not responding (frozen UI thread)");
|
|
882
|
-
}
|
|
1053
|
+
const uint WM_SETTEXT = 0x000C, WM_CHAR = 0x0102, SMTO_ABORTIFHUNG = 0x0002;
|
|
1054
|
+
|
|
1055
|
+
/** Kiểm tra nhanh nhịp phản hồi của cửa sổ bằng WM_NULL + SMTO_ABORTIFHUNG để tránh UI Automation treo cứng khi ứng dụng đích bị đơ. */
|
|
1056
|
+
static bool IsWindowResponsive(IntPtr h, uint timeoutMs) {
|
|
1057
|
+
if (h == IntPtr.Zero || !Native.IsWindow(h)) return false;
|
|
1058
|
+
IntPtr r;
|
|
1059
|
+
return Native.SendMessageTimeout(h, 0, IntPtr.Zero, null, SMTO_ABORTIFHUNG, timeoutMs, out r) != IntPtr.Zero;
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
static void AssertWindowResponsive(IntPtr h) {
|
|
1063
|
+
if (h == IntPtr.Zero) return;
|
|
1064
|
+
if (!Native.IsWindow(h)) throw new GuiError("GUI_WINDOW_GONE", "The target window has closed");
|
|
1065
|
+
if (!IsWindowResponsive(h, 500)) {
|
|
1066
|
+
if (!Native.IsWindow(h)) throw new GuiError("GUI_WINDOW_GONE", "The target window has closed");
|
|
1067
|
+
throw new GuiError("GUI_WINDOW_HUNG", "The target window is not responding (frozen UI thread)");
|
|
1068
|
+
}
|
|
883
1069
|
}
|
|
884
1070
|
|
|
885
1071
|
/** Handle Win32 riêng của phần tử (ô nhập / nút Win32, WinForms có; WPF / UWP / Chromium thì không). */
|
|
@@ -911,9 +1097,11 @@ namespace AivinGui {
|
|
|
911
1097
|
return true;
|
|
912
1098
|
}
|
|
913
1099
|
|
|
1100
|
+
/** Phím thật: cửa sổ đích phải lên được trên cùng (không thì GUI_FOCUS_LOST, không gõ), rồi focus phần tử. */
|
|
914
1101
|
static void FocusTarget(AutomationElement el, IntPtr window) {
|
|
915
|
-
|
|
1102
|
+
RequireForeground(window);
|
|
916
1103
|
if (el != null) { try { el.SetFocus(); } catch { } Thread.Sleep(50); }
|
|
1104
|
+
AssertStillForeground(window, null);
|
|
917
1105
|
}
|
|
918
1106
|
|
|
919
1107
|
/** Toạ độ theo ảnh của lần observe ({x,y} trong khung ảnh đã thu nhỏ, kèm scale) → toạ độ màn hình. */
|
|
@@ -978,7 +1166,7 @@ namespace AivinGui {
|
|
|
978
1166
|
}
|
|
979
1167
|
|
|
980
1168
|
/** Phần tử đầu tiên khớp (AutomationId trùng hẳn, hoặc tên chứa chữ — không phân biệt hoa thường), có lọc vai trò. */
|
|
981
|
-
static Dictionary<string, object> FindElement(IntPtr h, string name, string aid, string role) {
|
|
1169
|
+
static Dictionary<string, object> FindElement(IntPtr h, string name, string aid, string role) {
|
|
982
1170
|
if (!IsWindowResponsive(h, 300)) return null;
|
|
983
1171
|
AutomationElement root;
|
|
984
1172
|
try { root = AutomationElement.FromHandle(h); } catch { return null; }
|
|
@@ -1006,6 +1194,8 @@ namespace AivinGui {
|
|
|
1006
1194
|
|
|
1007
1195
|
/** Điểm màn hình của node chữ OCR của thao tác đang chạy (null = không phải node OCR). */
|
|
1008
1196
|
static System.Windows.Point? ocrPoint = null;
|
|
1197
|
+
/** node_id OCR của thao tác đang chạy — tính lại điểm theo vị trí cửa sổ lúc bấm (sau khi khôi phục cửa sổ thu nhỏ). */
|
|
1198
|
+
static string ocrNodeId = null;
|
|
1009
1199
|
|
|
1010
1200
|
static System.Windows.Point OcrPointOf(string nodeId, out IntPtr window) {
|
|
1011
1201
|
window = IntPtr.Zero;
|
|
@@ -1082,17 +1272,29 @@ namespace AivinGui {
|
|
|
1082
1272
|
|
|
1083
1273
|
static System.Windows.Point PointFor(AutomationElement el, IntPtr window, Dictionary<string, object> a) {
|
|
1084
1274
|
if (el != null) return CenterOf(el);
|
|
1085
|
-
if (ocrPoint.HasValue)
|
|
1275
|
+
if (ocrPoint.HasValue) {
|
|
1276
|
+
if (ocrNodeId != null) { IntPtr w; return OcrPointOf(ocrNodeId, out w); }
|
|
1277
|
+
return ocrPoint.Value;
|
|
1278
|
+
}
|
|
1086
1279
|
var pt = Get(a, "point") as Dictionary<string, object>;
|
|
1087
1280
|
if (pt == null) throw new GuiError("INVALID_ARGS", "Give node_id or point {x,y}");
|
|
1088
1281
|
return WindowPoint(window, a, pt);
|
|
1089
1282
|
}
|
|
1090
1283
|
|
|
1284
|
+
/**
|
|
1285
|
+
* Bấm chuột thật (02/11/2026): đưa cửa sổ lên (khôi phục nếu thu nhỏ) TRƯỚC khi tính toạ độ — cửa sổ thu nhỏ có toạ độ ảo; không lên được
|
|
1286
|
+
* trên cùng, hoặc điểm bấm đang bị cửa sổ khác che → GUI_FOCUS_LOST, không bấm.
|
|
1287
|
+
*/
|
|
1091
1288
|
static void MouseAt(AutomationElement el, IntPtr window, Dictionary<string, object> a, uint down, uint up, int times) {
|
|
1289
|
+
RequireForeground(window);
|
|
1092
1290
|
var p = PointFor(el, window, a);
|
|
1093
|
-
|
|
1094
|
-
|
|
1291
|
+
int x = (int)p.X, y = (int)p.Y;
|
|
1292
|
+
AssertPointOnWindow(x, y, window);
|
|
1293
|
+
MoveCursor(x, y);
|
|
1095
1294
|
Thread.Sleep(30);
|
|
1295
|
+
// Trong lúc di chuột có cửa sổ khác nhảy lên (thông báo, hộp thoại của ứng dụng khác) → không bấm.
|
|
1296
|
+
AssertPointOnWindow(x, y, window);
|
|
1297
|
+
lastMousePoint = new Dictionary<string, object> { { "x", x }, { "y", y } };
|
|
1096
1298
|
var list = new List<Native.INPUT>();
|
|
1097
1299
|
for (int i = 0; i < times; i++) { list.Add(MouseInput(down, 0, 0, 0)); list.Add(MouseInput(up, 0, 0, 0)); }
|
|
1098
1300
|
Send(list.ToArray());
|
|
@@ -1120,15 +1322,40 @@ namespace AivinGui {
|
|
|
1120
1322
|
|
|
1121
1323
|
static void KeyTap(ushort vk) { Send(new[] { KeyInput(vk, 0, 0), KeyInput(vk, 0, KEYEVENTF_KEYUP) }); }
|
|
1122
1324
|
|
|
1123
|
-
|
|
1325
|
+
/**
|
|
1326
|
+
* Gõ chữ theo lô 200 input. Trước MỖI lô (02/11/2026): cửa sổ trên cùng vẫn là đích, và (khi kiểm người dùng) người dùng không vừa đụng
|
|
1327
|
+
* chuột / phím từ lô trước — lệch thì dừng, báo đã gõ bao nhiêu ký tự (chữ còn lại không rơi vào cửa sổ khác).
|
|
1328
|
+
*/
|
|
1329
|
+
static void TypeText(string text, IntPtr window) {
|
|
1124
1330
|
var list = new List<Native.INPUT>();
|
|
1331
|
+
Native.POINT cursor;
|
|
1332
|
+
bool haveCursor = Native.GetCursorPos(out cursor);
|
|
1333
|
+
int typed = 0, inBatch = 0, total = text.Replace("\r", "").Length;
|
|
1334
|
+
bool first = true;
|
|
1125
1335
|
foreach (char ch in text) {
|
|
1126
|
-
if (ch == '\n') { list.Add(KeyInput(0x0D, 0, 0)); list.Add(KeyInput(0x0D, 0, KEYEVENTF_KEYUP)); continue; }
|
|
1127
1336
|
if (ch == '\r') continue;
|
|
1128
|
-
list.Add(KeyInput(0,
|
|
1129
|
-
|
|
1130
|
-
|
|
1337
|
+
if (ch == '\n') { list.Add(KeyInput(0x0D, 0, 0)); list.Add(KeyInput(0x0D, 0, KEYEVENTF_KEYUP)); }
|
|
1338
|
+
else {
|
|
1339
|
+
list.Add(KeyInput(0, ch, KEYEVENTF_UNICODE));
|
|
1340
|
+
list.Add(KeyInput(0, ch, KEYEVENTF_UNICODE | KEYEVENTF_KEYUP));
|
|
1341
|
+
}
|
|
1342
|
+
inBatch++;
|
|
1343
|
+
if (list.Count >= 200) {
|
|
1344
|
+
SendTextBatch(list, window, first, haveCursor, cursor, typed, total);
|
|
1345
|
+
typed += inBatch; inBatch = 0; first = false; list.Clear();
|
|
1346
|
+
Thread.Sleep(TYPE_BATCH_PAUSE_MS);
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
SendTextBatch(list, window, first, haveCursor, cursor, typed, total);
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1352
|
+
static void SendTextBatch(List<Native.INPUT> list, IntPtr window, bool first, bool haveCursor, Native.POINT cursor, int typed, int total) {
|
|
1353
|
+
if (list.Count == 0) return;
|
|
1354
|
+
string done = typed + " of " + total + " characters";
|
|
1355
|
+
if (!first && userInputCheck && haveCursor && UserTouchedSince(cursor)) {
|
|
1356
|
+
throw new GuiError("GUI_USER_ACTIVE", "The user started using the mouse/keyboard while the AI was typing — stopped after " + done);
|
|
1131
1357
|
}
|
|
1358
|
+
AssertStillForeground(window, done);
|
|
1132
1359
|
Send(list.ToArray());
|
|
1133
1360
|
}
|
|
1134
1361
|
|
|
@@ -1143,8 +1370,12 @@ namespace AivinGui {
|
|
|
1143
1370
|
static readonly HashSet<ushort> Extended = new HashSet<ushort> { 0x2E, 0x2D, 0x24, 0x23, 0x21, 0x22, 0x26, 0x28, 0x25, 0x27, 0x5B, 0x5D };
|
|
1144
1371
|
|
|
1145
1372
|
/** "ctrl+s", "alt+f4", "enter", "ctrl+shift+n" — nhiều tổ hợp cách nhau bằng dấu cách: "ctrl+a delete". */
|
|
1146
|
-
static void KeyCombo(string spec) {
|
|
1373
|
+
static void KeyCombo(string spec, IntPtr window) {
|
|
1374
|
+
int n = 0;
|
|
1147
1375
|
foreach (string combo in spec.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)) {
|
|
1376
|
+
// Mỗi tổ hợp: cửa sổ trên cùng vẫn là đích (tổ hợp trước có thể đã mở / đóng cửa sổ).
|
|
1377
|
+
AssertStillForeground(window, n == 0 ? null : n + " key combination(s)");
|
|
1378
|
+
n++;
|
|
1148
1379
|
var vks = new List<ushort>();
|
|
1149
1380
|
foreach (string raw in combo.ToLowerInvariant().Split('+')) {
|
|
1150
1381
|
string k = raw.Trim();
|