@luaward/mcp 0.2.0 → 0.2.1
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 +1 -1
- package/dist/cli.js +699 -223
- package/dist/cli.js.map +4 -4
- package/dist/index.js +690 -222
- package/dist/index.js.map +3 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { existsSync as existsSync3, readFileSync as readFileSync2 } from "node:fs";
|
|
3
3
|
import { homedir as homedir3 } from "node:os";
|
|
4
4
|
import { join as join3, resolve as resolve2 } from "node:path";
|
|
5
|
-
import { createInterface } from "node:readline";
|
|
5
|
+
import { createInterface as createInterface2 } from "node:readline";
|
|
6
6
|
|
|
7
7
|
// src/potassium.ts
|
|
8
8
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
@@ -10,10 +10,53 @@ import { homedir } from "node:os";
|
|
|
10
10
|
import { dirname, join, resolve } from "node:path";
|
|
11
11
|
var DEFAULT_POTASSIUM_URL = "http://127.0.0.1:8225/mcp";
|
|
12
12
|
var SCRATCH_TAB_NAME = "luaward_scratch.luau";
|
|
13
|
-
function
|
|
13
|
+
function stripBearerPrefix(raw) {
|
|
14
|
+
const trimmed = raw.trim();
|
|
15
|
+
return /^bearer\s+/i.test(trimmed) ? trimmed.replace(/^bearer\s+/i, "").trim() : trimmed;
|
|
16
|
+
}
|
|
17
|
+
function extractPotassiumTokenFromMcpConfig(parsed) {
|
|
18
|
+
const containers = [];
|
|
19
|
+
if (parsed.mcpServers && typeof parsed.mcpServers === "object" && !Array.isArray(parsed.mcpServers)) {
|
|
20
|
+
containers.push(parsed.mcpServers);
|
|
21
|
+
}
|
|
22
|
+
if (parsed.projects && typeof parsed.projects === "object" && !Array.isArray(parsed.projects)) {
|
|
23
|
+
for (const proj of Object.values(parsed.projects)) {
|
|
24
|
+
if (proj && typeof proj === "object" && !Array.isArray(proj)) {
|
|
25
|
+
const ms = proj.mcpServers;
|
|
26
|
+
if (ms && typeof ms === "object" && !Array.isArray(ms)) {
|
|
27
|
+
containers.push(ms);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
for (const servers of containers) {
|
|
33
|
+
for (const [serverName, cfg] of Object.entries(servers)) {
|
|
34
|
+
if (!cfg || typeof cfg !== "object" || Array.isArray(cfg)) continue;
|
|
35
|
+
const entry = cfg;
|
|
36
|
+
const urlStr = String(entry.url ?? entry.serverUrl ?? entry.endpoint ?? "").toLowerCase();
|
|
37
|
+
const isPotassium = serverName.toLowerCase().includes("potassium") || urlStr.includes("127.0.0.1:8225") || urlStr.includes("localhost:8225");
|
|
38
|
+
if (!isPotassium) continue;
|
|
39
|
+
const headers = entry.headers;
|
|
40
|
+
if (headers && typeof headers === "object" && !Array.isArray(headers)) {
|
|
41
|
+
for (const [hk, hv] of Object.entries(headers)) {
|
|
42
|
+
if (hk.toLowerCase() === "authorization" && typeof hv === "string" && hv.trim()) {
|
|
43
|
+
const clean = stripBearerPrefix(hv);
|
|
44
|
+
if (clean) return clean;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
for (const directKey of ["token", "bearerToken", "apiKey"]) {
|
|
49
|
+
const dv = entry[directKey];
|
|
50
|
+
if (typeof dv === "string" && dv.trim()) return stripBearerPrefix(dv);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
function discoverPotassiumToken(env = process.env, homeOverride) {
|
|
14
57
|
const fromEnv = (env.POTASSIUM_MCP_TOKEN || env.POTASSIUM_TOKEN || "").trim();
|
|
15
|
-
if (fromEnv) return fromEnv;
|
|
16
|
-
const home = homedir();
|
|
58
|
+
if (fromEnv) return stripBearerPrefix(fromEnv);
|
|
59
|
+
const home = homeOverride || homedir();
|
|
17
60
|
const appData = env.APPDATA || join(home, "AppData", "Roaming");
|
|
18
61
|
const localAppData = env.LOCALAPPDATA || join(home, "AppData", "Local");
|
|
19
62
|
const candidates = [
|
|
@@ -31,16 +74,31 @@ function discoverPotassiumToken(env = process.env) {
|
|
|
31
74
|
const parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
32
75
|
for (const key of ["mcpToken", "mcp_token", "bearerToken", "token", "apiKey"]) {
|
|
33
76
|
const val = parsed[key];
|
|
34
|
-
if (typeof val === "string" && val.trim()) return val
|
|
77
|
+
if (typeof val === "string" && val.trim()) return stripBearerPrefix(val);
|
|
35
78
|
}
|
|
36
79
|
const mcp = parsed.mcp;
|
|
37
80
|
if (mcp && typeof mcp === "object") {
|
|
38
81
|
const t = mcp.token ?? mcp.bearerToken;
|
|
39
|
-
if (typeof t === "string" && t.trim()) return t
|
|
82
|
+
if (typeof t === "string" && t.trim()) return stripBearerPrefix(t);
|
|
40
83
|
}
|
|
41
84
|
} catch {
|
|
42
85
|
}
|
|
43
86
|
}
|
|
87
|
+
const mcpConfigCandidates = [
|
|
88
|
+
join(home, ".gemini", "config", "mcp_config.json"),
|
|
89
|
+
join(home, ".claude.json"),
|
|
90
|
+
join(appData, "Claude", "claude_desktop_config.json"),
|
|
91
|
+
join(home, ".cursor", "mcp.json")
|
|
92
|
+
];
|
|
93
|
+
for (const cfgPath of mcpConfigCandidates) {
|
|
94
|
+
if (!existsSync(cfgPath)) continue;
|
|
95
|
+
try {
|
|
96
|
+
const parsed = JSON.parse(readFileSync(cfgPath, "utf8"));
|
|
97
|
+
const found = extractPotassiumTokenFromMcpConfig(parsed);
|
|
98
|
+
if (found) return found;
|
|
99
|
+
} catch {
|
|
100
|
+
}
|
|
101
|
+
}
|
|
44
102
|
return null;
|
|
45
103
|
}
|
|
46
104
|
function discoverPotassiumAutoexecDir(customDir, env = process.env) {
|
|
@@ -173,7 +231,7 @@ var PotassiumClient = class {
|
|
|
173
231
|
params: {
|
|
174
232
|
protocolVersion: "2025-03-26",
|
|
175
233
|
capabilities: {},
|
|
176
|
-
clientInfo: { name: "@luaward/mcp", version: "0.2.
|
|
234
|
+
clientInfo: { name: "@luaward/mcp", version: "0.2.1" }
|
|
177
235
|
}
|
|
178
236
|
})
|
|
179
237
|
});
|
|
@@ -181,6 +239,18 @@ var PotassiumClient = class {
|
|
|
181
239
|
if (sid) this.sessionId = sid;
|
|
182
240
|
if (!res.ok) return false;
|
|
183
241
|
this.initialized = true;
|
|
242
|
+
try {
|
|
243
|
+
await this.fetchFn(this.url, {
|
|
244
|
+
method: "POST",
|
|
245
|
+
headers: this.buildHeaders(),
|
|
246
|
+
body: JSON.stringify({
|
|
247
|
+
jsonrpc: "2.0",
|
|
248
|
+
method: "notifications/initialized",
|
|
249
|
+
params: {}
|
|
250
|
+
})
|
|
251
|
+
});
|
|
252
|
+
} catch {
|
|
253
|
+
}
|
|
184
254
|
return true;
|
|
185
255
|
} catch {
|
|
186
256
|
return false;
|
|
@@ -248,12 +318,11 @@ var PotassiumClient = class {
|
|
|
248
318
|
return { ok: true, clients, rawText: res.text };
|
|
249
319
|
}
|
|
250
320
|
async executeScript(code, pids) {
|
|
251
|
-
const
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
321
|
+
const cleanPids = (pids ?? []).map((p) => String(p).trim()).filter((p) => p.length > 0);
|
|
322
|
+
const args = {
|
|
323
|
+
source: code,
|
|
324
|
+
pid: cleanPids.length === 1 ? cleanPids[0] : cleanPids.length > 1 ? "all" : null
|
|
325
|
+
};
|
|
257
326
|
return this.callTool("execute_script", args);
|
|
258
327
|
}
|
|
259
328
|
async listTabs() {
|
|
@@ -300,12 +369,14 @@ var PotassiumClient = class {
|
|
|
300
369
|
if (!res.ok) return { ok: false, content: res.text, tabId: targetId };
|
|
301
370
|
if (res.structured && typeof res.structured === "object") {
|
|
302
371
|
const obj = res.structured;
|
|
303
|
-
|
|
372
|
+
const tabObj = obj.tab && typeof obj.tab === "object" && !Array.isArray(obj.tab) ? obj.tab : obj;
|
|
373
|
+
const contentStr = typeof obj.content === "string" ? obj.content : typeof tabObj.content === "string" ? tabObj.content : void 0;
|
|
374
|
+
if (contentStr !== void 0) {
|
|
304
375
|
return {
|
|
305
376
|
ok: true,
|
|
306
|
-
content:
|
|
307
|
-
tabId: String(obj.id ?? obj.tab_id ?? targetId ?? ""),
|
|
308
|
-
name: typeof
|
|
377
|
+
content: contentStr,
|
|
378
|
+
tabId: String(tabObj.id ?? tabObj.tab_id ?? tabObj.tabId ?? obj.id ?? obj.tab_id ?? targetId ?? ""),
|
|
379
|
+
name: typeof tabObj.title === "string" ? tabObj.title : typeof tabObj.name === "string" ? tabObj.name : typeof obj.title === "string" ? obj.title : typeof obj.name === "string" ? obj.name : targetName
|
|
309
380
|
};
|
|
310
381
|
}
|
|
311
382
|
}
|
|
@@ -318,6 +389,16 @@ var PotassiumClient = class {
|
|
|
318
389
|
async openOrReuseTab(opts) {
|
|
319
390
|
const desiredName = (opts.name || SCRATCH_TAB_NAME).trim();
|
|
320
391
|
const reuseScratch = opts.reuseScratch !== false;
|
|
392
|
+
const extractNestedTab = (structured, fallbackId, fallbackName) => {
|
|
393
|
+
if (!structured || typeof structured !== "object" || Array.isArray(structured)) {
|
|
394
|
+
return { id: fallbackId, name: fallbackName };
|
|
395
|
+
}
|
|
396
|
+
const obj = structured;
|
|
397
|
+
const tab = obj.tab && typeof obj.tab === "object" && !Array.isArray(obj.tab) ? obj.tab : obj;
|
|
398
|
+
const id = String(tab.id ?? tab.tab_id ?? tab.tabId ?? obj.id ?? obj.tab_id ?? fallbackId);
|
|
399
|
+
const name = String(tab.title ?? tab.name ?? obj.title ?? obj.name ?? fallbackName);
|
|
400
|
+
return { id, name };
|
|
401
|
+
};
|
|
321
402
|
if (opts.tabId) {
|
|
322
403
|
const w = await this.callTool("tabs", {
|
|
323
404
|
action: "write",
|
|
@@ -326,12 +407,13 @@ var PotassiumClient = class {
|
|
|
326
407
|
content: opts.content
|
|
327
408
|
});
|
|
328
409
|
await this.callTool("tabs", { action: "activate", tab_id: opts.tabId, id: opts.tabId });
|
|
410
|
+
const meta2 = extractNestedTab(w.structured, opts.tabId, desiredName);
|
|
329
411
|
return {
|
|
330
412
|
ok: w.ok,
|
|
331
|
-
tabId:
|
|
332
|
-
name:
|
|
413
|
+
tabId: meta2.id,
|
|
414
|
+
name: meta2.name,
|
|
333
415
|
reused: true,
|
|
334
|
-
text: w.ok ? `updated tab ${
|
|
416
|
+
text: w.ok ? `updated tab ${meta2.id} (${meta2.name})` : w.text
|
|
335
417
|
};
|
|
336
418
|
}
|
|
337
419
|
const listed = await this.listTabs();
|
|
@@ -349,12 +431,13 @@ var PotassiumClient = class {
|
|
|
349
431
|
content: opts.content
|
|
350
432
|
});
|
|
351
433
|
await this.callTool("tabs", { action: "activate", tab_id: target.id, id: target.id });
|
|
434
|
+
const meta2 = extractNestedTab(w.structured, target.id, target.name);
|
|
352
435
|
return {
|
|
353
436
|
ok: w.ok,
|
|
354
|
-
tabId:
|
|
355
|
-
name:
|
|
437
|
+
tabId: meta2.id,
|
|
438
|
+
name: meta2.name,
|
|
356
439
|
reused: true,
|
|
357
|
-
text: w.ok ? `reused tab "${
|
|
440
|
+
text: w.ok ? `reused tab "${meta2.name}" (id ${meta2.id}, ${opts.content.length} chars)` : w.text
|
|
358
441
|
};
|
|
359
442
|
}
|
|
360
443
|
}
|
|
@@ -364,17 +447,13 @@ var PotassiumClient = class {
|
|
|
364
447
|
title: desiredName,
|
|
365
448
|
content: opts.content
|
|
366
449
|
});
|
|
367
|
-
|
|
368
|
-
if (opened.structured && typeof opened.structured === "object") {
|
|
369
|
-
const obj = opened.structured;
|
|
370
|
-
if (obj.id ?? obj.tab_id) newId = String(obj.id ?? obj.tab_id);
|
|
371
|
-
}
|
|
450
|
+
const meta = extractNestedTab(opened.structured, desiredName, desiredName);
|
|
372
451
|
return {
|
|
373
452
|
ok: opened.ok,
|
|
374
|
-
tabId:
|
|
375
|
-
name:
|
|
453
|
+
tabId: meta.id,
|
|
454
|
+
name: meta.name,
|
|
376
455
|
reused: false,
|
|
377
|
-
text: opened.ok ? `opened tab "${
|
|
456
|
+
text: opened.ok ? `opened tab "${meta.name}" (id ${meta.id}, ${opts.content.length} chars)` : opened.text
|
|
378
457
|
};
|
|
379
458
|
}
|
|
380
459
|
installAutoexecBootstrapper(pairCommand, customDir) {
|
|
@@ -413,12 +492,16 @@ var PotassiumClient = class {
|
|
|
413
492
|
};
|
|
414
493
|
|
|
415
494
|
// src/windows.ts
|
|
416
|
-
import { execFile } from "node:child_process";
|
|
495
|
+
import { execFile, spawn } from "node:child_process";
|
|
417
496
|
import { existsSync as existsSync2, readdirSync, statSync } from "node:fs";
|
|
418
497
|
import { homedir as homedir2 } from "node:os";
|
|
419
498
|
import { join as join2 } from "node:path";
|
|
499
|
+
import { createInterface } from "node:readline";
|
|
420
500
|
import { promisify } from "node:util";
|
|
421
501
|
var execFileAsync = promisify(execFile);
|
|
502
|
+
function encodePowerShellCommand(script) {
|
|
503
|
+
return Buffer.from(script, "utf16le").toString("base64");
|
|
504
|
+
}
|
|
422
505
|
function buildRobloxDeepLink(params) {
|
|
423
506
|
const place = String(params.placeId).trim();
|
|
424
507
|
if (!/^\d+$/.test(place)) {
|
|
@@ -626,7 +709,9 @@ namespace Luaward {
|
|
|
626
709
|
[DllImport("user32.dll")] public static extern int GetSystemMetrics(int nIndex);
|
|
627
710
|
[DllImport("user32.dll")] public static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
|
|
628
711
|
[DllImport("user32.dll")] public static extern uint MapVirtualKey(uint uCode, uint uMapType);
|
|
712
|
+
[DllImport("user32.dll")] public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, uint nFlags);
|
|
629
713
|
|
|
714
|
+
public const uint PW_RENDERFULLCONTENT = 0x00000002;
|
|
630
715
|
public const uint INPUT_MOUSE = 0;
|
|
631
716
|
public const uint INPUT_KEYBOARD = 1;
|
|
632
717
|
public const uint MOUSEEVENTF_MOVE = 0x0001;
|
|
@@ -644,7 +729,7 @@ namespace Luaward {
|
|
|
644
729
|
public const uint KEYEVENTF_UNICODE = 0x0004;
|
|
645
730
|
public const uint KEYEVENTF_SCANCODE = 0x0008;
|
|
646
731
|
|
|
647
|
-
public static
|
|
732
|
+
public static uint SendMouseMoveAbs(int screenX, int screenY) {
|
|
648
733
|
SetCursorPos(screenX, screenY);
|
|
649
734
|
int vx = GetSystemMetrics(76); // SM_XVIRTUALSCREEN
|
|
650
735
|
int vy = GetSystemMetrics(77); // SM_YVIRTUALSCREEN
|
|
@@ -655,27 +740,27 @@ namespace Luaward {
|
|
|
655
740
|
inp[0].u.mi.dx = (int)Math.Round(((screenX - vx) * 65535.0) / vw);
|
|
656
741
|
inp[0].u.mi.dy = (int)Math.Round(((screenY - vy) * 65535.0) / vh);
|
|
657
742
|
inp[0].u.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_VIRTUALDESK;
|
|
658
|
-
SendInput(1, inp, Marshal.SizeOf(typeof(INPUT)));
|
|
743
|
+
return SendInput(1, inp, Marshal.SizeOf(typeof(INPUT)));
|
|
659
744
|
}
|
|
660
745
|
|
|
661
|
-
public static
|
|
746
|
+
public static uint SendMouseMoveRel(int dx, int dy) {
|
|
662
747
|
INPUT[] inp = new INPUT[1];
|
|
663
748
|
inp[0].type = INPUT_MOUSE;
|
|
664
749
|
inp[0].u.mi.dx = dx;
|
|
665
750
|
inp[0].u.mi.dy = dy;
|
|
666
751
|
inp[0].u.mi.dwFlags = MOUSEEVENTF_MOVE;
|
|
667
|
-
SendInput(1, inp, Marshal.SizeOf(typeof(INPUT)));
|
|
752
|
+
return SendInput(1, inp, Marshal.SizeOf(typeof(INPUT)));
|
|
668
753
|
}
|
|
669
754
|
|
|
670
|
-
public static
|
|
755
|
+
public static uint SendMouseButton(uint flag, uint mouseData = 0) {
|
|
671
756
|
INPUT[] inp = new INPUT[1];
|
|
672
757
|
inp[0].type = INPUT_MOUSE;
|
|
673
758
|
inp[0].u.mi.dwFlags = flag;
|
|
674
759
|
inp[0].u.mi.mouseData = mouseData;
|
|
675
|
-
SendInput(1, inp, Marshal.SizeOf(typeof(INPUT)));
|
|
760
|
+
return SendInput(1, inp, Marshal.SizeOf(typeof(INPUT)));
|
|
676
761
|
}
|
|
677
762
|
|
|
678
|
-
public static
|
|
763
|
+
public static uint SendKeyVk(ushort vk, bool keyUp) {
|
|
679
764
|
ushort scan = (ushort)MapVirtualKey(vk, 0);
|
|
680
765
|
uint flags = KEYEVENTF_SCANCODE | (keyUp ? KEYEVENTF_KEYUP : 0u);
|
|
681
766
|
if ((vk >= 0x21 && vk <= 0x2e) || vk == 0xa3 || vk == 0xa5) flags |= KEYEVENTF_EXTENDEDKEY;
|
|
@@ -684,10 +769,11 @@ namespace Luaward {
|
|
|
684
769
|
inp[0].u.ki.wVk = vk;
|
|
685
770
|
inp[0].u.ki.wScan = scan;
|
|
686
771
|
inp[0].u.ki.dwFlags = flags;
|
|
687
|
-
SendInput(1, inp, Marshal.SizeOf(typeof(INPUT)));
|
|
772
|
+
return SendInput(1, inp, Marshal.SizeOf(typeof(INPUT)));
|
|
688
773
|
}
|
|
689
774
|
|
|
690
|
-
public static
|
|
775
|
+
public static uint SendUnicodeString(string text) {
|
|
776
|
+
uint totalSent = 0;
|
|
691
777
|
foreach (char c in text) {
|
|
692
778
|
INPUT[] inp = new INPUT[2];
|
|
693
779
|
inp[0].type = INPUT_KEYBOARD;
|
|
@@ -696,8 +782,9 @@ namespace Luaward {
|
|
|
696
782
|
inp[1].type = INPUT_KEYBOARD;
|
|
697
783
|
inp[1].u.ki.wScan = (ushort)c;
|
|
698
784
|
inp[1].u.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
|
|
699
|
-
SendInput(2, inp, Marshal.SizeOf(typeof(INPUT)));
|
|
785
|
+
totalSent += SendInput(2, inp, Marshal.SizeOf(typeof(INPUT)));
|
|
700
786
|
}
|
|
787
|
+
return totalSent;
|
|
701
788
|
}
|
|
702
789
|
}
|
|
703
790
|
}
|
|
@@ -705,71 +792,217 @@ namespace Luaward {
|
|
|
705
792
|
[void][Luaward.Win32Desktop]::SetProcessDPIAware()
|
|
706
793
|
}
|
|
707
794
|
`;
|
|
795
|
+
var PS_WORKER_BOOTSTRAP = `
|
|
796
|
+
[Console]::InputEncoding = [System.Text.UTF8Encoding]::new($false)
|
|
797
|
+
[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false)
|
|
798
|
+
$ErrorActionPreference = 'Stop'
|
|
799
|
+
${WIN32_CSHARP_PRELUDE}
|
|
800
|
+
function Get-ForegroundProcessName {
|
|
801
|
+
try {
|
|
802
|
+
$fg = [Luaward.Win32Desktop]::GetForegroundWindow()
|
|
803
|
+
if ($fg -eq [IntPtr]::Zero) { return "" }
|
|
804
|
+
$pidVal = [uint32]0
|
|
805
|
+
[void][Luaward.Win32Desktop]::GetWindowThreadProcessId($fg, [ref]$pidVal)
|
|
806
|
+
$p = Get-Process -Id $pidVal -ErrorAction SilentlyContinue
|
|
807
|
+
if ($p) { return [string]$p.ProcessName }
|
|
808
|
+
} catch {}
|
|
809
|
+
return ""
|
|
810
|
+
}
|
|
811
|
+
[Console]::Out.WriteLine("__LW_PS_READY__")
|
|
812
|
+
[Console]::Out.Flush()
|
|
813
|
+
while ($true) {
|
|
814
|
+
$line = [Console]::In.ReadLine()
|
|
815
|
+
if ($null -eq $line) { break }
|
|
816
|
+
$sep = $line.IndexOf(' ')
|
|
817
|
+
if ($sep -lt 1) { continue }
|
|
818
|
+
$jobId = $line.Substring(0, $sep)
|
|
819
|
+
$b64 = $line.Substring($sep + 1)
|
|
820
|
+
try {
|
|
821
|
+
$code = [System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($b64))
|
|
822
|
+
$sb = [ScriptBlock]::Create($code)
|
|
823
|
+
$out = (& $sb) | Out-String
|
|
824
|
+
$outB64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($out))
|
|
825
|
+
[Console]::Out.WriteLine("__LW_PS_OK__ $jobId $outB64")
|
|
826
|
+
} catch {
|
|
827
|
+
$errMsg = $_.Exception.Message
|
|
828
|
+
if (-not $errMsg) { $errMsg = [string]$_ }
|
|
829
|
+
$errB64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($errMsg))
|
|
830
|
+
[Console]::Out.WriteLine("__LW_PS_ERR__ $jobId $errB64")
|
|
831
|
+
}
|
|
832
|
+
[Console]::Out.Flush()
|
|
833
|
+
}
|
|
834
|
+
`;
|
|
835
|
+
function buildUipiWarning(fgProcess) {
|
|
836
|
+
const targetDesc = fgProcess ? ` while targeting foreground process "${fgProcess}"` : "";
|
|
837
|
+
return `Windows UIPI blocked SendInput (returned 0)${targetDesc}. The target window is likely running elevated (Administrator, e.g. Potassium.exe); run your MCP client as Administrator or use Potassium local MCP tools directly.`;
|
|
838
|
+
}
|
|
708
839
|
var PowerShellWindowsDriver = class {
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
840
|
+
worker = null;
|
|
841
|
+
workerReady = null;
|
|
842
|
+
pendingJobs = /* @__PURE__ */ new Map();
|
|
843
|
+
queue = Promise.resolve();
|
|
844
|
+
jobSeq = 0;
|
|
845
|
+
exitHookInstalled = false;
|
|
846
|
+
dispose() {
|
|
847
|
+
const w = this.worker;
|
|
848
|
+
this.worker = null;
|
|
849
|
+
this.workerReady = null;
|
|
850
|
+
for (const [id, job] of this.pendingJobs) {
|
|
851
|
+
clearTimeout(job.timer);
|
|
852
|
+
job.reject(new Error("PowerShell worker disposed"));
|
|
853
|
+
this.pendingJobs.delete(id);
|
|
854
|
+
}
|
|
855
|
+
if (w && !w.killed) {
|
|
856
|
+
try {
|
|
857
|
+
w.stdin.end();
|
|
858
|
+
w.kill();
|
|
859
|
+
} catch {
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
ensureWorker() {
|
|
864
|
+
if (this.worker && !this.worker.killed && this.workerReady) {
|
|
865
|
+
return this.workerReady;
|
|
714
866
|
}
|
|
867
|
+
const encodedBootstrap = encodePowerShellCommand(PS_WORKER_BOOTSTRAP);
|
|
868
|
+
const child = spawn(
|
|
869
|
+
"powershell.exe",
|
|
870
|
+
["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-EncodedCommand", encodedBootstrap],
|
|
871
|
+
{ stdio: ["pipe", "pipe", "pipe"], windowsHide: true }
|
|
872
|
+
);
|
|
873
|
+
child.unref();
|
|
874
|
+
this.worker = child;
|
|
875
|
+
if (!this.exitHookInstalled) {
|
|
876
|
+
this.exitHookInstalled = true;
|
|
877
|
+
process.once("exit", () => this.dispose());
|
|
878
|
+
}
|
|
879
|
+
let stderrBuf = "";
|
|
880
|
+
child.stderr.on("data", (chunk) => {
|
|
881
|
+
stderrBuf = (stderrBuf + String(chunk)).slice(-2e3);
|
|
882
|
+
});
|
|
883
|
+
const rl = createInterface({ input: child.stdout, crlfDelay: Number.POSITIVE_INFINITY });
|
|
884
|
+
this.workerReady = new Promise((resolveReady, rejectReady) => {
|
|
885
|
+
let isReady = false;
|
|
886
|
+
const readyTimer = setTimeout(() => {
|
|
887
|
+
if (!isReady) {
|
|
888
|
+
this.dispose();
|
|
889
|
+
rejectReady(
|
|
890
|
+
new Error(
|
|
891
|
+
`PowerShell worker failed to initialize in time${stderrBuf ? `: ${stderrBuf.trim()}` : ""}`
|
|
892
|
+
)
|
|
893
|
+
);
|
|
894
|
+
}
|
|
895
|
+
}, 25e3);
|
|
896
|
+
rl.on("line", (line) => {
|
|
897
|
+
if (line === "__LW_PS_READY__") {
|
|
898
|
+
isReady = true;
|
|
899
|
+
clearTimeout(readyTimer);
|
|
900
|
+
resolveReady();
|
|
901
|
+
return;
|
|
902
|
+
}
|
|
903
|
+
if (line.startsWith("__LW_PS_OK__ ") || line.startsWith("__LW_PS_ERR__ ")) {
|
|
904
|
+
const isOk = line.startsWith("__LW_PS_OK__ ");
|
|
905
|
+
const rest = line.slice(isOk ? "__LW_PS_OK__ ".length : "__LW_PS_ERR__ ".length);
|
|
906
|
+
const sp = rest.indexOf(" ");
|
|
907
|
+
if (sp < 1) return;
|
|
908
|
+
const jobId = rest.slice(0, sp);
|
|
909
|
+
const payload = Buffer.from(rest.slice(sp + 1), "base64").toString("utf8");
|
|
910
|
+
const pending = this.pendingJobs.get(jobId);
|
|
911
|
+
if (!pending) return;
|
|
912
|
+
this.pendingJobs.delete(jobId);
|
|
913
|
+
clearTimeout(pending.timer);
|
|
914
|
+
if (isOk) pending.resolve(payload);
|
|
915
|
+
else pending.reject(new Error(payload.trim() || "PowerShell worker command failed"));
|
|
916
|
+
}
|
|
917
|
+
});
|
|
918
|
+
child.once("exit", (code) => {
|
|
919
|
+
clearTimeout(readyTimer);
|
|
920
|
+
if (this.worker === child) {
|
|
921
|
+
this.worker = null;
|
|
922
|
+
this.workerReady = null;
|
|
923
|
+
}
|
|
924
|
+
const err = new Error(
|
|
925
|
+
`PowerShell worker exited (code ${code ?? "null"})${stderrBuf ? `: ${stderrBuf.trim()}` : ""}`
|
|
926
|
+
);
|
|
927
|
+
if (!isReady) rejectReady(err);
|
|
928
|
+
for (const [id, job] of this.pendingJobs) {
|
|
929
|
+
clearTimeout(job.timer);
|
|
930
|
+
job.reject(err);
|
|
931
|
+
this.pendingJobs.delete(id);
|
|
932
|
+
}
|
|
933
|
+
});
|
|
934
|
+
child.once("error", (err) => {
|
|
935
|
+
clearTimeout(readyTimer);
|
|
936
|
+
if (this.worker === child) {
|
|
937
|
+
this.worker = null;
|
|
938
|
+
this.workerReady = null;
|
|
939
|
+
}
|
|
940
|
+
if (!isReady) rejectReady(err);
|
|
941
|
+
});
|
|
942
|
+
});
|
|
943
|
+
return this.workerReady;
|
|
944
|
+
}
|
|
945
|
+
async runPsOneShot(scriptBody, timeoutMs) {
|
|
715
946
|
const full = `${WIN32_CSHARP_PRELUDE}
|
|
947
|
+
function Get-ForegroundProcessName { return "" }
|
|
716
948
|
$ErrorActionPreference = 'Stop'
|
|
717
949
|
${scriptBody}`;
|
|
950
|
+
const encoded = encodePowerShellCommand(full);
|
|
718
951
|
const { stdout } = await execFileAsync(
|
|
719
952
|
"powershell.exe",
|
|
720
|
-
["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-
|
|
721
|
-
{ timeout: timeoutMs, maxBuffer: 25 * 1024 * 1024 }
|
|
953
|
+
["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-EncodedCommand", encoded],
|
|
954
|
+
{ timeout: timeoutMs, maxBuffer: 25 * 1024 * 1024, windowsHide: true }
|
|
955
|
+
);
|
|
956
|
+
return stdout;
|
|
957
|
+
}
|
|
958
|
+
async runPsRaw(scriptBody, timeoutMs = 2e4) {
|
|
959
|
+
if (process.platform !== "win32") {
|
|
960
|
+
throw new Error(
|
|
961
|
+
"Windows Computer Use tools require Windows OS (win32) or a configured WindowsDriver mock."
|
|
962
|
+
);
|
|
963
|
+
}
|
|
964
|
+
const execute = async () => {
|
|
965
|
+
try {
|
|
966
|
+
await this.ensureWorker();
|
|
967
|
+
const child = this.worker;
|
|
968
|
+
if (!child || child.killed) {
|
|
969
|
+
return await this.runPsOneShot(scriptBody, timeoutMs);
|
|
970
|
+
}
|
|
971
|
+
const jobId = `j${++this.jobSeq}`;
|
|
972
|
+
const b64Code = Buffer.from(scriptBody, "utf8").toString("base64");
|
|
973
|
+
return await new Promise((resolveJob, rejectJob) => {
|
|
974
|
+
const timer = setTimeout(() => {
|
|
975
|
+
this.pendingJobs.delete(jobId);
|
|
976
|
+
this.dispose();
|
|
977
|
+
rejectJob(new Error(`PowerShell command timed out after ${timeoutMs}ms`));
|
|
978
|
+
}, timeoutMs);
|
|
979
|
+
this.pendingJobs.set(jobId, { resolve: resolveJob, reject: rejectJob, timer });
|
|
980
|
+
child.stdin.write(`${jobId} ${b64Code}
|
|
981
|
+
`);
|
|
982
|
+
});
|
|
983
|
+
} catch (err) {
|
|
984
|
+
if (!this.worker) {
|
|
985
|
+
return await this.runPsOneShot(scriptBody, timeoutMs);
|
|
986
|
+
}
|
|
987
|
+
throw err;
|
|
988
|
+
}
|
|
989
|
+
};
|
|
990
|
+
const runPromise = this.queue.then(execute, execute);
|
|
991
|
+
this.queue = runPromise.then(
|
|
992
|
+
() => void 0,
|
|
993
|
+
() => void 0
|
|
722
994
|
);
|
|
995
|
+
return runPromise;
|
|
996
|
+
}
|
|
997
|
+
async runPsJson(scriptBody, timeoutMs = 2e4) {
|
|
998
|
+
const stdout = await this.runPsRaw(scriptBody, timeoutMs);
|
|
723
999
|
const trimmed = stdout.trim();
|
|
724
1000
|
const jsonLine = trimmed.split(/\r?\n/).reverse().find((l) => l.trim().startsWith("{") || l.trim().startsWith("["));
|
|
725
1001
|
return JSON.parse(jsonLine || trimmed);
|
|
726
1002
|
}
|
|
727
1003
|
async listWindows(targetFilter) {
|
|
728
1004
|
const filterStr = (targetFilter || "").trim().toLowerCase();
|
|
729
|
-
const ps =
|
|
730
|
-
$fg = [Luaward.Win32Desktop]::GetForegroundWindow()
|
|
731
|
-
$list = New-Object System.Collections.Generic.List[object]
|
|
732
|
-
$cb = [Luaward.Win32Desktop+EnumWindowsProc]{
|
|
733
|
-
param($hWnd, $lParam)
|
|
734
|
-
if (-not [Luaward.Win32Desktop]::IsWindowVisible($hWnd)) { return $true }
|
|
735
|
-
$sb = New-Object System.Text.StringBuilder 256
|
|
736
|
-
[void][Luaward.Win32Desktop]::GetWindowText($hWnd, $sb, $sb.Capacity)
|
|
737
|
-
$title = $sb.ToString()
|
|
738
|
-
if ([string]::IsNullOrWhiteSpace($title)) { return $true }
|
|
739
|
-
$csb = New-Object System.Text.StringBuilder 128
|
|
740
|
-
[void][Luaward.Win32Desktop]::GetClassName($hWnd, $csb, $csb.Capacity)
|
|
741
|
-
$cls = $csb.ToString()
|
|
742
|
-
$pidVal = [uint32]0
|
|
743
|
-
[void][Luaward.Win32Desktop]::GetWindowThreadProcessId($hWnd, [ref]$pidVal)
|
|
744
|
-
$procName = ""
|
|
745
|
-
$resp = $true
|
|
746
|
-
try {
|
|
747
|
-
$p = Get-Process -Id $pidVal -ErrorAction SilentlyContinue
|
|
748
|
-
if ($p) { $procName = $p.ProcessName; $resp = [bool]$p.Responding }
|
|
749
|
-
} catch {}
|
|
750
|
-
$wr = New-Object Luaward.RECT
|
|
751
|
-
[void][Luaward.Win32Desktop]::GetWindowRect($hWnd, [ref]$wr)
|
|
752
|
-
$cr = New-Object Luaward.RECT
|
|
753
|
-
[void][Luaward.Win32Desktop]::GetClientRect($hWnd, [ref]$cr)
|
|
754
|
-
$pt = New-Object Luaward.POINT
|
|
755
|
-
[void][Luaward.Win32Desktop]::ClientToScreen($hWnd, [ref]$pt)
|
|
756
|
-
$list.Add(@{
|
|
757
|
-
hwnd = "0x" + $hWnd.ToString("X")
|
|
758
|
-
pid = [int]$pidVal
|
|
759
|
-
processName = $procName
|
|
760
|
-
title = $title
|
|
761
|
-
className = $cls
|
|
762
|
-
responding = $resp
|
|
763
|
-
isMinimized = [Luaward.Win32Desktop]::IsIconic($hWnd)
|
|
764
|
-
isForeground = ($hWnd -eq $fg)
|
|
765
|
-
rect = @{ left = $wr.Left; top = $wr.Top; width = ($wr.Right - $wr.Left); height = ($wr.Bottom - $wr.Top) }
|
|
766
|
-
clientRect = @{ screenLeft = $pt.X; screenTop = $pt.Y; width = ($cr.Right - $cr.Left); height = ($cr.Bottom - $cr.Top) }
|
|
767
|
-
})
|
|
768
|
-
return $true
|
|
769
|
-
}
|
|
770
|
-
[void][Luaward.Win32Desktop]::EnumWindows($cb, [IntPtr]::Zero)
|
|
771
|
-
ConvertTo-Json -InputObject @($list) -Depth 5 -Compress
|
|
772
|
-
`;
|
|
1005
|
+
const ps = buildListWindowsPsScript();
|
|
773
1006
|
const all = await this.runPsJson(ps);
|
|
774
1007
|
if (!filterStr) return all;
|
|
775
1008
|
return all.filter((w) => matchesWindowTarget(w, filterStr));
|
|
@@ -828,6 +1061,7 @@ $h = [IntPtr]::new([Convert]::ToInt64("${win.hwnd}", 16))
|
|
|
828
1061
|
}
|
|
829
1062
|
async captureScreenshot(opts) {
|
|
830
1063
|
const target = (opts.target || "roblox").trim();
|
|
1064
|
+
const method = opts.method || "auto";
|
|
831
1065
|
let win;
|
|
832
1066
|
if (target.toLowerCase() !== "screen") {
|
|
833
1067
|
if (opts.focusFirst) {
|
|
@@ -854,41 +1088,27 @@ $h = [IntPtr]::new([Convert]::ToInt64("${win.hwnd}", 16))
|
|
|
854
1088
|
capW = Math.max(10, Math.round(opts.crop.width));
|
|
855
1089
|
capH = Math.max(10, Math.round(opts.crop.height));
|
|
856
1090
|
}
|
|
857
|
-
const res = await this.runPsJson(
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
try {
|
|
872
|
-
$g.CopyFromScreen($left, $top, 0, 0, (New-Object System.Drawing.Size($w, $h)))
|
|
873
|
-
$ms = New-Object System.IO.MemoryStream
|
|
874
|
-
$bmp.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png)
|
|
875
|
-
@{
|
|
876
|
-
ok = $true
|
|
877
|
-
width = $w
|
|
878
|
-
height = $h
|
|
879
|
-
data = [Convert]::ToBase64String($ms.ToArray())
|
|
880
|
-
} | ConvertTo-Json -Compress
|
|
881
|
-
} finally {
|
|
882
|
-
$g.Dispose()
|
|
883
|
-
$bmp.Dispose()
|
|
884
|
-
}
|
|
885
|
-
`);
|
|
1091
|
+
const res = await this.runPsJson(
|
|
1092
|
+
buildScreenshotPsScript({
|
|
1093
|
+
hwnd: win?.hwnd ?? "",
|
|
1094
|
+
winLeft: win?.rect.left ?? 0,
|
|
1095
|
+
winTop: win?.rect.top ?? 0,
|
|
1096
|
+
winWidth: win?.rect.width ?? 0,
|
|
1097
|
+
winHeight: win?.rect.height ?? 0,
|
|
1098
|
+
capLeft,
|
|
1099
|
+
capTop,
|
|
1100
|
+
capW,
|
|
1101
|
+
capH,
|
|
1102
|
+
method
|
|
1103
|
+
})
|
|
1104
|
+
);
|
|
886
1105
|
return {
|
|
887
1106
|
ok: res.ok,
|
|
888
1107
|
mimeType: "image/png",
|
|
889
1108
|
data: res.data,
|
|
890
1109
|
width: res.width,
|
|
891
1110
|
height: res.height,
|
|
1111
|
+
captureMethod: res.captureMethod,
|
|
892
1112
|
windowRect: win?.rect,
|
|
893
1113
|
clientRect: win?.clientRect,
|
|
894
1114
|
err: res.err
|
|
@@ -902,50 +1122,14 @@ try {
|
|
|
902
1122
|
const maxDepth = Math.max(1, Math.min(12, Math.floor(opts.maxDepth ?? 5)));
|
|
903
1123
|
const maxElements = Math.max(1, Math.min(500, Math.floor(opts.maxElements ?? 150)));
|
|
904
1124
|
const interactiveOnly = opts.interactiveOnly !== false;
|
|
905
|
-
const nodes = await this.runPsJson(
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
function Walk-Ui($el, [int]$depth) {
|
|
914
|
-
if ($null -eq $el -or $depth -gt ${maxDepth} -or $nodes.Count -ge ${maxElements}) { return }
|
|
915
|
-
$child = $walker.GetFirstChild($el)
|
|
916
|
-
while ($null -ne $child -and $nodes.Count -lt ${maxElements}) {
|
|
917
|
-
try {
|
|
918
|
-
$cur = $child.Current
|
|
919
|
-
$ct = ($cur.ControlType.ProgrammaticName -replace '^ControlType\\.', '')
|
|
920
|
-
$r = $cur.BoundingRectangle
|
|
921
|
-
$hasBounds = (-not [double]::IsInfinity($r.Width)) -and $r.Width -gt 1 -and $r.Height -gt 1
|
|
922
|
-
$keep = $hasBounds -and ((-not $${interactiveOnly}) -or ($interactiveTypes -contains $ct) -or (-not [string]::IsNullOrWhiteSpace($cur.Name)))
|
|
923
|
-
if ($keep) {
|
|
924
|
-
$script:seq++
|
|
925
|
-
$lx = [int][Math]::Round($r.Left)
|
|
926
|
-
$ly = [int][Math]::Round($r.Top)
|
|
927
|
-
$lw = [int][Math]::Round($r.Width)
|
|
928
|
-
$lh = [int][Math]::Round($r.Height)
|
|
929
|
-
$nodes.Add(@{
|
|
930
|
-
id = $script:seq
|
|
931
|
-
depth = $depth
|
|
932
|
-
controlType = $ct
|
|
933
|
-
name = [string]$cur.Name
|
|
934
|
-
automationId = [string]$cur.AutomationId
|
|
935
|
-
className = [string]$cur.ClassName
|
|
936
|
-
isEnabled = [bool]$cur.IsEnabled
|
|
937
|
-
rect = @{ left = $lx; top = $ly; width = $lw; height = $lh }
|
|
938
|
-
center = @{ x = [int]($lx + $lw / 2); y = [int]($ly + $lh / 2) }
|
|
939
|
-
})
|
|
940
|
-
}
|
|
941
|
-
Walk-Ui $child ($depth + 1)
|
|
942
|
-
} catch {}
|
|
943
|
-
$child = $walker.GetNextSibling($child)
|
|
944
|
-
}
|
|
945
|
-
}
|
|
946
|
-
Walk-Ui $root 1
|
|
947
|
-
ConvertTo-Json -InputObject @($nodes) -Depth 5 -Compress
|
|
948
|
-
`);
|
|
1125
|
+
const nodes = await this.runPsJson(
|
|
1126
|
+
buildUiTreePsScript({
|
|
1127
|
+
hwnd: win.hwnd,
|
|
1128
|
+
maxDepth,
|
|
1129
|
+
maxElements,
|
|
1130
|
+
interactiveOnly
|
|
1131
|
+
})
|
|
1132
|
+
);
|
|
949
1133
|
return { ok: true, windowTitle: win.title, nodes };
|
|
950
1134
|
}
|
|
951
1135
|
async mouse(params) {
|
|
@@ -970,20 +1154,24 @@ ConvertTo-Json -InputObject @($nodes) -Depth 5 -Compress
|
|
|
970
1154
|
if (mode === "camera" || params.action === "camera_rotate") {
|
|
971
1155
|
const totalDx = Math.round(params.dx ?? 0);
|
|
972
1156
|
const totalDy = Math.round(params.dy ?? 0);
|
|
973
|
-
await this.runPsJson(`
|
|
1157
|
+
const res2 = await this.runPsJson(`
|
|
974
1158
|
$steps = ${steps}
|
|
975
1159
|
$remX = ${totalDx}
|
|
976
1160
|
$remY = ${totalDy}
|
|
1161
|
+
$sent = 0
|
|
977
1162
|
for ($i = 1; $i -le $steps; $i++) {
|
|
978
1163
|
$sx = [int][Math]::Round($remX / ($steps - $i + 1))
|
|
979
1164
|
$sy = [int][Math]::Round($remY / ($steps - $i + 1))
|
|
980
|
-
[Luaward.Win32Desktop]::SendMouseMoveRel($sx, $sy)
|
|
1165
|
+
$sent += [int][Luaward.Win32Desktop]::SendMouseMoveRel($sx, $sy)
|
|
981
1166
|
$remX -= $sx
|
|
982
1167
|
$remY -= $sy
|
|
983
1168
|
Start-Sleep -Milliseconds 8
|
|
984
1169
|
}
|
|
985
|
-
@{ ok = $
|
|
1170
|
+
@{ ok = ($sent -gt 0); sent = $sent; fgProcess = (Get-ForegroundProcessName) } | ConvertTo-Json -Compress
|
|
986
1171
|
`);
|
|
1172
|
+
if (!res2.ok) {
|
|
1173
|
+
return { ok: false, uipiBlocked: true, text: buildUipiWarning(res2.fgProcess) };
|
|
1174
|
+
}
|
|
987
1175
|
return {
|
|
988
1176
|
ok: true,
|
|
989
1177
|
text: `rotated camera via relative Raw Input (dx=${totalDx}, dy=${totalDy}, steps=${steps})`
|
|
@@ -992,25 +1180,35 @@ for ($i = 1; $i -le $steps; $i++) {
|
|
|
992
1180
|
if (params.action === "drag_and_drop") {
|
|
993
1181
|
const toX = Math.round((params.toX ?? screenX) + baseX);
|
|
994
1182
|
const toY = Math.round((params.toY ?? screenY) + baseY);
|
|
995
|
-
await this.runPsJson(`
|
|
996
|
-
|
|
1183
|
+
const res2 = await this.runPsJson(`
|
|
1184
|
+
$sent = 0
|
|
1185
|
+
$sent += [int][Luaward.Win32Desktop]::SendMouseMoveAbs(${screenX}, ${screenY})
|
|
997
1186
|
Start-Sleep -Milliseconds 15
|
|
998
|
-
[Luaward.Win32Desktop]::SendMouseMoveRel(1, 0)
|
|
1187
|
+
$sent += [int][Luaward.Win32Desktop]::SendMouseMoveRel(1, 0)
|
|
999
1188
|
Start-Sleep -Milliseconds 15
|
|
1000
|
-
[Luaward.Win32Desktop]::SendMouseButton([Luaward.Win32Desktop]::MOUSEEVENTF_LEFTDOWN, 0)
|
|
1189
|
+
$sent += [int][Luaward.Win32Desktop]::SendMouseButton([Luaward.Win32Desktop]::MOUSEEVENTF_LEFTDOWN, 0)
|
|
1001
1190
|
Start-Sleep -Milliseconds 25
|
|
1002
1191
|
$steps = ${steps}
|
|
1003
1192
|
for ($i = 1; $i -le $steps; $i++) {
|
|
1004
1193
|
$cx = [int][Math]::Round(${screenX} + ((${toX} - ${screenX}) * $i) / $steps)
|
|
1005
1194
|
$cy = [int][Math]::Round(${screenY} + ((${toY} - ${screenY}) * $i) / $steps)
|
|
1006
|
-
[Luaward.Win32Desktop]::SendMouseMoveAbs($cx, $cy)
|
|
1007
|
-
[Luaward.Win32Desktop]::SendMouseMoveRel(1, 0)
|
|
1195
|
+
$sent += [int][Luaward.Win32Desktop]::SendMouseMoveAbs($cx, $cy)
|
|
1196
|
+
$sent += [int][Luaward.Win32Desktop]::SendMouseMoveRel(1, 0)
|
|
1008
1197
|
Start-Sleep -Milliseconds 12
|
|
1009
1198
|
}
|
|
1010
1199
|
Start-Sleep -Milliseconds ${holdMs}
|
|
1011
|
-
[Luaward.Win32Desktop]::SendMouseButton([Luaward.Win32Desktop]::MOUSEEVENTF_LEFTUP, 0)
|
|
1012
|
-
@{ ok = $
|
|
1200
|
+
$sent += [int][Luaward.Win32Desktop]::SendMouseButton([Luaward.Win32Desktop]::MOUSEEVENTF_LEFTUP, 0)
|
|
1201
|
+
@{ ok = ($sent -gt 0); sent = $sent; fgProcess = (Get-ForegroundProcessName) } | ConvertTo-Json -Compress
|
|
1013
1202
|
`);
|
|
1203
|
+
if (!res2.ok) {
|
|
1204
|
+
return {
|
|
1205
|
+
ok: false,
|
|
1206
|
+
screenX: toX,
|
|
1207
|
+
screenY: toY,
|
|
1208
|
+
uipiBlocked: true,
|
|
1209
|
+
text: buildUipiWarning(res2.fgProcess)
|
|
1210
|
+
};
|
|
1211
|
+
}
|
|
1014
1212
|
return {
|
|
1015
1213
|
ok: true,
|
|
1016
1214
|
screenX: toX,
|
|
@@ -1020,24 +1218,31 @@ Start-Sleep -Milliseconds ${holdMs}
|
|
|
1020
1218
|
}
|
|
1021
1219
|
if (params.action === "scroll") {
|
|
1022
1220
|
const delta = Math.round(params.scrollDelta ?? -120);
|
|
1023
|
-
await this.runPsJson(`
|
|
1221
|
+
const res2 = await this.runPsJson(`
|
|
1222
|
+
$sent = 0
|
|
1024
1223
|
if (${params.x !== void 0 ? "$true" : "$false"}) {
|
|
1025
|
-
[Luaward.Win32Desktop]::SendMouseMoveAbs(${screenX}, ${screenY})
|
|
1224
|
+
$sent += [int][Luaward.Win32Desktop]::SendMouseMoveAbs(${screenX}, ${screenY})
|
|
1026
1225
|
Start-Sleep -Milliseconds 10
|
|
1027
1226
|
}
|
|
1028
1227
|
$uDelta = [BitConverter]::ToUInt32([BitConverter]::GetBytes([int]${delta}), 0)
|
|
1029
|
-
[Luaward.Win32Desktop]::SendMouseButton([Luaward.Win32Desktop]::MOUSEEVENTF_WHEEL, $uDelta)
|
|
1030
|
-
@{ ok = $
|
|
1228
|
+
$sent += [int][Luaward.Win32Desktop]::SendMouseButton([Luaward.Win32Desktop]::MOUSEEVENTF_WHEEL, $uDelta)
|
|
1229
|
+
@{ ok = ($sent -gt 0); sent = $sent; fgProcess = (Get-ForegroundProcessName) } | ConvertTo-Json -Compress
|
|
1031
1230
|
`);
|
|
1231
|
+
if (!res2.ok) {
|
|
1232
|
+
return { ok: false, screenX, screenY, uipiBlocked: true, text: buildUipiWarning(res2.fgProcess) };
|
|
1233
|
+
}
|
|
1032
1234
|
return { ok: true, screenX, screenY, text: `scrolled wheel delta=${delta}` };
|
|
1033
1235
|
}
|
|
1034
1236
|
if (params.action === "move") {
|
|
1035
|
-
await this.runPsJson(`
|
|
1036
|
-
[Luaward.Win32Desktop]::SendMouseMoveAbs(${screenX}, ${screenY})
|
|
1237
|
+
const res2 = await this.runPsJson(`
|
|
1238
|
+
$sent = [int][Luaward.Win32Desktop]::SendMouseMoveAbs(${screenX}, ${screenY})
|
|
1037
1239
|
Start-Sleep -Milliseconds 12
|
|
1038
|
-
[Luaward.Win32Desktop]::SendMouseMoveRel(1, 0)
|
|
1039
|
-
@{ ok = $
|
|
1240
|
+
$sent += [int][Luaward.Win32Desktop]::SendMouseMoveRel(1, 0)
|
|
1241
|
+
@{ ok = ($sent -gt 0); sent = $sent; fgProcess = (Get-ForegroundProcessName) } | ConvertTo-Json -Compress
|
|
1040
1242
|
`);
|
|
1243
|
+
if (!res2.ok) {
|
|
1244
|
+
return { ok: false, screenX, screenY, uipiBlocked: true, text: buildUipiWarning(res2.fgProcess) };
|
|
1245
|
+
}
|
|
1041
1246
|
return {
|
|
1042
1247
|
ok: true,
|
|
1043
1248
|
screenX,
|
|
@@ -1048,19 +1253,23 @@ Start-Sleep -Milliseconds 12
|
|
|
1048
1253
|
const downFlag = params.action === "right_click" ? "MOUSEEVENTF_RIGHTDOWN" : params.action === "middle_click" ? "MOUSEEVENTF_MIDDLEDOWN" : "MOUSEEVENTF_LEFTDOWN";
|
|
1049
1254
|
const upFlag = params.action === "right_click" ? "MOUSEEVENTF_RIGHTUP" : params.action === "middle_click" ? "MOUSEEVENTF_MIDDLEUP" : "MOUSEEVENTF_LEFTUP";
|
|
1050
1255
|
const clicks = params.action === "double_click" ? 2 : 1;
|
|
1051
|
-
await this.runPsJson(`
|
|
1052
|
-
|
|
1256
|
+
const res = await this.runPsJson(`
|
|
1257
|
+
$sent = 0
|
|
1258
|
+
$sent += [int][Luaward.Win32Desktop]::SendMouseMoveAbs(${screenX}, ${screenY})
|
|
1053
1259
|
Start-Sleep -Milliseconds 15
|
|
1054
|
-
[Luaward.Win32Desktop]::SendMouseMoveRel(1, 0)
|
|
1260
|
+
$sent += [int][Luaward.Win32Desktop]::SendMouseMoveRel(1, 0)
|
|
1055
1261
|
Start-Sleep -Milliseconds 15
|
|
1056
1262
|
for ($c = 1; $c -le ${clicks}; $c++) {
|
|
1057
|
-
[Luaward.Win32Desktop]::SendMouseButton([Luaward.Win32Desktop]::${downFlag}, 0)
|
|
1263
|
+
$sent += [int][Luaward.Win32Desktop]::SendMouseButton([Luaward.Win32Desktop]::${downFlag}, 0)
|
|
1058
1264
|
Start-Sleep -Milliseconds ${holdMs}
|
|
1059
|
-
[Luaward.Win32Desktop]::SendMouseButton([Luaward.Win32Desktop]::${upFlag}, 0)
|
|
1265
|
+
$sent += [int][Luaward.Win32Desktop]::SendMouseButton([Luaward.Win32Desktop]::${upFlag}, 0)
|
|
1060
1266
|
if ($c -lt ${clicks}) { Start-Sleep -Milliseconds 40 }
|
|
1061
1267
|
}
|
|
1062
|
-
@{ ok = $
|
|
1268
|
+
@{ ok = ($sent -gt 0); sent = $sent; fgProcess = (Get-ForegroundProcessName) } | ConvertTo-Json -Compress
|
|
1063
1269
|
`);
|
|
1270
|
+
if (!res.ok) {
|
|
1271
|
+
return { ok: false, screenX, screenY, uipiBlocked: true, text: buildUipiWarning(res.fgProcess) };
|
|
1272
|
+
}
|
|
1064
1273
|
return {
|
|
1065
1274
|
ok: true,
|
|
1066
1275
|
screenX,
|
|
@@ -1075,49 +1284,62 @@ for ($c = 1; $c -le ${clicks}; $c++) {
|
|
|
1075
1284
|
const holdMs = Math.max(15, Math.min(5e3, Math.floor(params.holdMs ?? 50)));
|
|
1076
1285
|
if (params.action === "type") {
|
|
1077
1286
|
const text = String(params.text ?? "");
|
|
1287
|
+
if (text.length === 0) {
|
|
1288
|
+
return { ok: true, text: "typed 0 characters" };
|
|
1289
|
+
}
|
|
1078
1290
|
const b64 = Buffer.from(text, "utf8").toString("base64");
|
|
1079
|
-
await this.runPsJson(`
|
|
1291
|
+
const res2 = await this.runPsJson(`
|
|
1080
1292
|
$str = [System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String("${b64}"))
|
|
1081
|
-
[Luaward.Win32Desktop]::SendUnicodeString($str)
|
|
1082
|
-
@{ ok = $
|
|
1293
|
+
$sent = [int][Luaward.Win32Desktop]::SendUnicodeString($str)
|
|
1294
|
+
@{ ok = ($sent -gt 0); sent = $sent; fgProcess = (Get-ForegroundProcessName) } | ConvertTo-Json -Compress
|
|
1083
1295
|
`);
|
|
1296
|
+
if (!res2.ok) return { ok: false, uipiBlocked: true, text: buildUipiWarning(res2.fgProcess) };
|
|
1084
1297
|
return { ok: true, text: `typed ${text.length} characters via Win32 Unicode SendInput` };
|
|
1085
1298
|
}
|
|
1086
1299
|
if (params.action === "hotkey") {
|
|
1087
1300
|
const keys = Array.isArray(params.keys) && params.keys.length > 0 ? params.keys : [];
|
|
1088
1301
|
if (keys.length === 0) throw new Error("keys array is required for action=hotkey");
|
|
1089
1302
|
const vks = keys.map((k) => resolveVirtualKeyCode(k));
|
|
1090
|
-
const downCalls = vks.map(
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1303
|
+
const downCalls = vks.map(
|
|
1304
|
+
(vk2) => `$sent += [int][Luaward.Win32Desktop]::SendKeyVk(${vk2}, $false); Start-Sleep -Milliseconds 15`
|
|
1305
|
+
).join("\n");
|
|
1306
|
+
const upCalls = [...vks].reverse().map(
|
|
1307
|
+
(vk2) => `$sent += [int][Luaward.Win32Desktop]::SendKeyVk(${vk2}, $true); Start-Sleep -Milliseconds 15`
|
|
1308
|
+
).join("\n");
|
|
1309
|
+
const res2 = await this.runPsJson(
|
|
1310
|
+
`$sent = 0
|
|
1311
|
+
${downCalls}
|
|
1094
1312
|
Start-Sleep -Milliseconds ${holdMs}
|
|
1095
1313
|
${upCalls}
|
|
1096
|
-
@{ ok = $
|
|
1314
|
+
@{ ok = ($sent -gt 0); sent = $sent; fgProcess = (Get-ForegroundProcessName) } | ConvertTo-Json -Compress`
|
|
1097
1315
|
);
|
|
1316
|
+
if (!res2.ok) return { ok: false, uipiBlocked: true, text: buildUipiWarning(res2.fgProcess) };
|
|
1098
1317
|
return { ok: true, text: `pressed hotkey ${keys.join("+")} via ScanCode SendInput` };
|
|
1099
1318
|
}
|
|
1100
1319
|
const vk = resolveVirtualKeyCode(String(params.key ?? ""));
|
|
1101
1320
|
if (params.action === "key_down") {
|
|
1102
|
-
await this.runPsJson(
|
|
1103
|
-
|
|
1104
|
-
@{ ok = $
|
|
1321
|
+
const res2 = await this.runPsJson(
|
|
1322
|
+
`$sent = [int][Luaward.Win32Desktop]::SendKeyVk(${vk}, $false)
|
|
1323
|
+
@{ ok = ($sent -gt 0); sent = $sent; fgProcess = (Get-ForegroundProcessName) } | ConvertTo-Json -Compress`
|
|
1105
1324
|
);
|
|
1325
|
+
if (!res2.ok) return { ok: false, uipiBlocked: true, text: buildUipiWarning(res2.fgProcess) };
|
|
1106
1326
|
return { ok: true, text: `held key_down ${params.key} (vk=0x${vk.toString(16)})` };
|
|
1107
1327
|
}
|
|
1108
1328
|
if (params.action === "key_up") {
|
|
1109
|
-
await this.runPsJson(
|
|
1110
|
-
|
|
1111
|
-
@{ ok = $
|
|
1329
|
+
const res2 = await this.runPsJson(
|
|
1330
|
+
`$sent = [int][Luaward.Win32Desktop]::SendKeyVk(${vk}, $true)
|
|
1331
|
+
@{ ok = ($sent -gt 0); sent = $sent; fgProcess = (Get-ForegroundProcessName) } | ConvertTo-Json -Compress`
|
|
1112
1332
|
);
|
|
1333
|
+
if (!res2.ok) return { ok: false, uipiBlocked: true, text: buildUipiWarning(res2.fgProcess) };
|
|
1113
1334
|
return { ok: true, text: `released key_up ${params.key} (vk=0x${vk.toString(16)})` };
|
|
1114
1335
|
}
|
|
1115
|
-
await this.runPsJson(`
|
|
1116
|
-
[Luaward.Win32Desktop]::SendKeyVk(${vk}, $false)
|
|
1336
|
+
const res = await this.runPsJson(`
|
|
1337
|
+
$sent = [int][Luaward.Win32Desktop]::SendKeyVk(${vk}, $false)
|
|
1117
1338
|
Start-Sleep -Milliseconds ${holdMs}
|
|
1118
|
-
[Luaward.Win32Desktop]::SendKeyVk(${vk}, $true)
|
|
1119
|
-
@{ ok = $
|
|
1339
|
+
$sent += [int][Luaward.Win32Desktop]::SendKeyVk(${vk}, $true)
|
|
1340
|
+
@{ ok = ($sent -gt 0); sent = $sent; fgProcess = (Get-ForegroundProcessName) } | ConvertTo-Json -Compress
|
|
1120
1341
|
`);
|
|
1342
|
+
if (!res.ok) return { ok: false, uipiBlocked: true, text: buildUipiWarning(res.fgProcess) };
|
|
1121
1343
|
return { ok: true, text: `pressed key ${params.key} for ${holdMs}ms via ScanCode SendInput` };
|
|
1122
1344
|
}
|
|
1123
1345
|
async launchRoblox(params) {
|
|
@@ -1192,6 +1414,193 @@ if ($cnt -gt 0) {
|
|
|
1192
1414
|
};
|
|
1193
1415
|
}
|
|
1194
1416
|
};
|
|
1417
|
+
function buildListWindowsPsScript() {
|
|
1418
|
+
return `
|
|
1419
|
+
$fg = [Luaward.Win32Desktop]::GetForegroundWindow()
|
|
1420
|
+
$list = New-Object System.Collections.Generic.List[object]
|
|
1421
|
+
$cb = [Luaward.Win32Desktop+EnumWindowsProc]{
|
|
1422
|
+
param($hWnd, $lParam)
|
|
1423
|
+
if (-not [Luaward.Win32Desktop]::IsWindowVisible($hWnd)) { return $true }
|
|
1424
|
+
$sb = New-Object System.Text.StringBuilder 256
|
|
1425
|
+
[void][Luaward.Win32Desktop]::GetWindowText($hWnd, $sb, $sb.Capacity)
|
|
1426
|
+
$title = $sb.ToString()
|
|
1427
|
+
if ([string]::IsNullOrWhiteSpace($title)) { return $true }
|
|
1428
|
+
$csb = New-Object System.Text.StringBuilder 128
|
|
1429
|
+
[void][Luaward.Win32Desktop]::GetClassName($hWnd, $csb, $csb.Capacity)
|
|
1430
|
+
$cls = $csb.ToString()
|
|
1431
|
+
$pidVal = [uint32]0
|
|
1432
|
+
[void][Luaward.Win32Desktop]::GetWindowThreadProcessId($hWnd, [ref]$pidVal)
|
|
1433
|
+
$procName = ""
|
|
1434
|
+
$resp = $true
|
|
1435
|
+
try {
|
|
1436
|
+
$p = Get-Process -Id $pidVal -ErrorAction SilentlyContinue
|
|
1437
|
+
if ($p) { $procName = $p.ProcessName; $resp = [bool]$p.Responding }
|
|
1438
|
+
} catch {}
|
|
1439
|
+
$wr = New-Object Luaward.RECT
|
|
1440
|
+
[void][Luaward.Win32Desktop]::GetWindowRect($hWnd, [ref]$wr)
|
|
1441
|
+
$cr = New-Object Luaward.RECT
|
|
1442
|
+
[void][Luaward.Win32Desktop]::GetClientRect($hWnd, [ref]$cr)
|
|
1443
|
+
$pt = New-Object Luaward.POINT
|
|
1444
|
+
[void][Luaward.Win32Desktop]::ClientToScreen($hWnd, [ref]$pt)
|
|
1445
|
+
$list.Add(@{
|
|
1446
|
+
hwnd = "0x" + $hWnd.ToString("X")
|
|
1447
|
+
pid = [int]$pidVal
|
|
1448
|
+
processName = $procName
|
|
1449
|
+
title = $title
|
|
1450
|
+
className = $cls
|
|
1451
|
+
responding = $resp
|
|
1452
|
+
isMinimized = [Luaward.Win32Desktop]::IsIconic($hWnd)
|
|
1453
|
+
isForeground = ($hWnd -eq $fg)
|
|
1454
|
+
rect = @{ left = $wr.Left; top = $wr.Top; width = ($wr.Right - $wr.Left); height = ($wr.Bottom - $wr.Top) }
|
|
1455
|
+
clientRect = @{ screenLeft = $pt.X; screenTop = $pt.Y; width = ($cr.Right - $cr.Left); height = ($cr.Bottom - $cr.Top) }
|
|
1456
|
+
})
|
|
1457
|
+
return $true
|
|
1458
|
+
}
|
|
1459
|
+
[void][Luaward.Win32Desktop]::EnumWindows($cb, [IntPtr]::Zero)
|
|
1460
|
+
if ($list.Count -eq 0) { "[]" } else { ConvertTo-Json -InputObject $list.ToArray() -Depth 5 -Compress }
|
|
1461
|
+
`;
|
|
1462
|
+
}
|
|
1463
|
+
function buildUiTreePsScript(opts) {
|
|
1464
|
+
return `
|
|
1465
|
+
$h = [IntPtr]::new([Convert]::ToInt64("${opts.hwnd}", 16))
|
|
1466
|
+
$root = [System.Windows.Automation.AutomationElement]::FromHandle($h)
|
|
1467
|
+
$walker = [System.Windows.Automation.TreeWalker]::ControlViewWalker
|
|
1468
|
+
$nodes = New-Object System.Collections.Generic.List[object]
|
|
1469
|
+
$script:seq = 0
|
|
1470
|
+
$interactiveTypes = @("Button", "Edit", "TabItem", "ListItem", "CheckBox", "RadioButton", "ComboBox", "MenuItem", "Hyperlink", "Slider", "Document", "Text", "Window")
|
|
1471
|
+
|
|
1472
|
+
function Walk-Ui($el, [int]$depth) {
|
|
1473
|
+
if ($null -eq $el -or $depth -gt ${opts.maxDepth} -or $nodes.Count -ge ${opts.maxElements}) { return }
|
|
1474
|
+
$child = $walker.GetFirstChild($el)
|
|
1475
|
+
while ($null -ne $child -and $nodes.Count -lt ${opts.maxElements}) {
|
|
1476
|
+
try {
|
|
1477
|
+
$cur = $child.Current
|
|
1478
|
+
$ct = ($cur.ControlType.ProgrammaticName -replace '^ControlType\\.', '')
|
|
1479
|
+
$r = $cur.BoundingRectangle
|
|
1480
|
+
$hasBounds = (-not [double]::IsInfinity($r.Width)) -and $r.Width -gt 1 -and $r.Height -gt 1
|
|
1481
|
+
$keep = $hasBounds -and ((-not $${opts.interactiveOnly}) -or ($interactiveTypes -contains $ct) -or (-not [string]::IsNullOrWhiteSpace($cur.Name)))
|
|
1482
|
+
if ($keep) {
|
|
1483
|
+
$script:seq++
|
|
1484
|
+
$lx = [int][Math]::Round($r.Left)
|
|
1485
|
+
$ly = [int][Math]::Round($r.Top)
|
|
1486
|
+
$lw = [int][Math]::Round($r.Width)
|
|
1487
|
+
$lh = [int][Math]::Round($r.Height)
|
|
1488
|
+
$nodes.Add(@{
|
|
1489
|
+
id = $script:seq
|
|
1490
|
+
depth = $depth
|
|
1491
|
+
controlType = $ct
|
|
1492
|
+
name = [string]$cur.Name
|
|
1493
|
+
automationId = [string]$cur.AutomationId
|
|
1494
|
+
className = [string]$cur.ClassName
|
|
1495
|
+
isEnabled = [bool]$cur.IsEnabled
|
|
1496
|
+
rect = @{ left = $lx; top = $ly; width = $lw; height = $lh }
|
|
1497
|
+
center = @{ x = [int]($lx + $lw / 2); y = [int]($ly + $lh / 2) }
|
|
1498
|
+
})
|
|
1499
|
+
}
|
|
1500
|
+
Walk-Ui $child ($depth + 1)
|
|
1501
|
+
} catch {}
|
|
1502
|
+
$child = $walker.GetNextSibling($child)
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
Walk-Ui $root 1
|
|
1506
|
+
if ($nodes.Count -eq 0) { "[]" } else { ConvertTo-Json -InputObject $nodes.ToArray() -Depth 5 -Compress }
|
|
1507
|
+
`;
|
|
1508
|
+
}
|
|
1509
|
+
function buildScreenshotPsScript(opts) {
|
|
1510
|
+
const hasHwnd = Boolean(opts.hwnd?.startsWith("0x"));
|
|
1511
|
+
return `
|
|
1512
|
+
$left = ${opts.capLeft}
|
|
1513
|
+
$top = ${opts.capTop}
|
|
1514
|
+
$w = ${opts.capW}
|
|
1515
|
+
$h = ${opts.capH}
|
|
1516
|
+
$method = "${opts.method}"
|
|
1517
|
+
$hasHwnd = ${hasHwnd ? "$true" : "$false"}
|
|
1518
|
+
if ($w -le 0 -or $h -le 0) {
|
|
1519
|
+
$bounds = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
|
|
1520
|
+
$left = $bounds.Left
|
|
1521
|
+
$top = $bounds.Top
|
|
1522
|
+
$w = $bounds.Width
|
|
1523
|
+
$h = $bounds.Height
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
function Capture-ViaPrintWindow {
|
|
1527
|
+
$hwndPtr = [IntPtr]::new([Convert]::ToInt64("${opts.hwnd || "0x0"}", 16))
|
|
1528
|
+
$ww = [Math]::Max(1, ${opts.winWidth || opts.capW || 1})
|
|
1529
|
+
$wh = [Math]::Max(1, ${opts.winHeight || opts.capH || 1})
|
|
1530
|
+
$fullBmp = New-Object System.Drawing.Bitmap $ww, $wh
|
|
1531
|
+
$fg = [System.Drawing.Graphics]::FromImage($fullBmp)
|
|
1532
|
+
$hdc = [IntPtr]::Zero
|
|
1533
|
+
try {
|
|
1534
|
+
$hdc = $fg.GetHdc()
|
|
1535
|
+
$okPw = [Luaward.Win32Desktop]::PrintWindow($hwndPtr, $hdc, [Luaward.Win32Desktop]::PW_RENDERFULLCONTENT)
|
|
1536
|
+
$fg.ReleaseHdc($hdc)
|
|
1537
|
+
$hdc = [IntPtr]::Zero
|
|
1538
|
+
if (-not $okPw) { throw "PrintWindow returned false" }
|
|
1539
|
+
$cropX = [Math]::Max(0, $left - ${opts.winLeft})
|
|
1540
|
+
$cropY = [Math]::Max(0, $top - ${opts.winTop})
|
|
1541
|
+
$cropW = [Math]::Min($w, $ww - $cropX)
|
|
1542
|
+
$cropH = [Math]::Min($h, $wh - $cropY)
|
|
1543
|
+
if ($cropW -le 0 -or $cropH -le 0) {
|
|
1544
|
+
$cropX = 0; $cropY = 0; $cropW = $ww; $cropH = $wh
|
|
1545
|
+
}
|
|
1546
|
+
$rect = New-Object System.Drawing.Rectangle($cropX, $cropY, $cropW, $cropH)
|
|
1547
|
+
$outBmp = $fullBmp.Clone($rect, $fullBmp.PixelFormat)
|
|
1548
|
+
try {
|
|
1549
|
+
$ms = New-Object System.IO.MemoryStream
|
|
1550
|
+
$outBmp.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png)
|
|
1551
|
+
return @{
|
|
1552
|
+
ok = $true
|
|
1553
|
+
width = $cropW
|
|
1554
|
+
height = $cropH
|
|
1555
|
+
captureMethod = "printwindow"
|
|
1556
|
+
data = [Convert]::ToBase64String($ms.ToArray())
|
|
1557
|
+
}
|
|
1558
|
+
} finally {
|
|
1559
|
+
$outBmp.Dispose()
|
|
1560
|
+
}
|
|
1561
|
+
} finally {
|
|
1562
|
+
if ($hdc -ne [IntPtr]::Zero) { try { $fg.ReleaseHdc($hdc) } catch {} }
|
|
1563
|
+
$fg.Dispose()
|
|
1564
|
+
$fullBmp.Dispose()
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
$result = $null
|
|
1569
|
+
if ($method -eq "printwindow" -and $hasHwnd) {
|
|
1570
|
+
$result = Capture-ViaPrintWindow
|
|
1571
|
+
} else {
|
|
1572
|
+
$bmp = New-Object System.Drawing.Bitmap $w, $h
|
|
1573
|
+
$g = [System.Drawing.Graphics]::FromImage($bmp)
|
|
1574
|
+
try {
|
|
1575
|
+
$g.CopyFromScreen($left, $top, 0, 0, (New-Object System.Drawing.Size($w, $h)))
|
|
1576
|
+
$ms = New-Object System.IO.MemoryStream
|
|
1577
|
+
$bmp.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png)
|
|
1578
|
+
$result = @{
|
|
1579
|
+
ok = $true
|
|
1580
|
+
width = $w
|
|
1581
|
+
height = $h
|
|
1582
|
+
captureMethod = "copy"
|
|
1583
|
+
data = [Convert]::ToBase64String($ms.ToArray())
|
|
1584
|
+
}
|
|
1585
|
+
} catch {
|
|
1586
|
+
$copyErr = $_.Exception.Message
|
|
1587
|
+
if ($method -eq "auto" -and $hasHwnd) {
|
|
1588
|
+
try {
|
|
1589
|
+
$result = Capture-ViaPrintWindow
|
|
1590
|
+
} catch {
|
|
1591
|
+
$result = @{ ok = $false; err = "CopyFromScreen failed ($copyErr); PrintWindow fallback failed ($($_.Exception.Message))" }
|
|
1592
|
+
}
|
|
1593
|
+
} else {
|
|
1594
|
+
$result = @{ ok = $false; err = "CopyFromScreen failed: $copyErr" }
|
|
1595
|
+
}
|
|
1596
|
+
} finally {
|
|
1597
|
+
$g.Dispose()
|
|
1598
|
+
$bmp.Dispose()
|
|
1599
|
+
}
|
|
1600
|
+
}
|
|
1601
|
+
$result | ConvertTo-Json -Compress
|
|
1602
|
+
`;
|
|
1603
|
+
}
|
|
1195
1604
|
function matchesWindowTarget(w, filter) {
|
|
1196
1605
|
const f = filter.trim().toLowerCase();
|
|
1197
1606
|
if (!f || f === "all") return true;
|
|
@@ -1206,7 +1615,7 @@ function matchesWindowTarget(w, filter) {
|
|
|
1206
1615
|
}
|
|
1207
1616
|
|
|
1208
1617
|
// src/index.ts
|
|
1209
|
-
var MCP_BRIDGE_VERSION = "0.2.
|
|
1618
|
+
var MCP_BRIDGE_VERSION = "0.2.1";
|
|
1210
1619
|
var DEFAULT_MCP_URL = "https://luaward.com/mcp";
|
|
1211
1620
|
var TOKEN_SHAPE = /^lwm_[0-9a-f]{48}$/;
|
|
1212
1621
|
function resolveStoredToken(configDir) {
|
|
@@ -1426,6 +1835,12 @@ var LOCAL_HYBRID_TOOLS = [
|
|
|
1426
1835
|
auto_inject: {
|
|
1427
1836
|
type: "boolean",
|
|
1428
1837
|
description: "Auto-inject Luaward agent via Potassium once client attaches (default true)"
|
|
1838
|
+
},
|
|
1839
|
+
wait_for_client_ms: {
|
|
1840
|
+
type: "integer",
|
|
1841
|
+
minimum: 0,
|
|
1842
|
+
maximum: 12e4,
|
|
1843
|
+
description: "How long (ms) to poll Potassium list_clients waiting for Roblox to attach before auto-injecting (default 30000)"
|
|
1429
1844
|
}
|
|
1430
1845
|
},
|
|
1431
1846
|
required: ["action"],
|
|
@@ -1493,6 +1908,11 @@ var LOCAL_HYBRID_TOOLS = [
|
|
|
1493
1908
|
client_only: {
|
|
1494
1909
|
type: "boolean",
|
|
1495
1910
|
description: "Capture only the inner client viewport excluding window title bar/borders (default true)"
|
|
1911
|
+
},
|
|
1912
|
+
method: {
|
|
1913
|
+
type: "string",
|
|
1914
|
+
enum: ["auto", "copy", "printwindow"],
|
|
1915
|
+
description: '"auto" (CopyFromScreen with PrintWindow PW_RENDERFULLCONTENT fallback, default), "copy", or "printwindow" (captures occluded windows)'
|
|
1496
1916
|
}
|
|
1497
1917
|
},
|
|
1498
1918
|
additionalProperties: false
|
|
@@ -1796,10 +2216,26 @@ var HybridMcpBridge = class {
|
|
|
1796
2216
|
}
|
|
1797
2217
|
return null;
|
|
1798
2218
|
}
|
|
1799
|
-
async tryZeroTouchAutoInject() {
|
|
1800
|
-
const
|
|
1801
|
-
|
|
1802
|
-
|
|
2219
|
+
async tryZeroTouchAutoInject(opts) {
|
|
2220
|
+
const waitMs = Math.max(0, opts?.waitForClientMs ?? 0);
|
|
2221
|
+
const clientPollMs = Math.max(0, opts?.pollClientMs ?? Math.min(1e3, this.autoInjectPollMs || 500));
|
|
2222
|
+
const deadline = Date.now() + waitMs;
|
|
2223
|
+
let clientsRes = await this.potassium.listClients();
|
|
2224
|
+
while ((!clientsRes.ok || clientsRes.clients.length === 0) && Date.now() < deadline) {
|
|
2225
|
+
if (clientPollMs > 0) await sleep(clientPollMs);
|
|
2226
|
+
clientsRes = await this.potassium.listClients();
|
|
2227
|
+
}
|
|
2228
|
+
if (!clientsRes.ok) {
|
|
2229
|
+
return {
|
|
2230
|
+
injected: false,
|
|
2231
|
+
text: `Potassium local MCP error: ${clientsRes.rawText}`
|
|
2232
|
+
};
|
|
2233
|
+
}
|
|
2234
|
+
if (clientsRes.clients.length === 0) {
|
|
2235
|
+
return {
|
|
2236
|
+
injected: false,
|
|
2237
|
+
text: waitMs > 0 ? `no attached Roblox client appeared in Potassium local MCP within ${Math.round(waitMs / 1e3)}s` : "no attached Roblox client found in Potassium local MCP"
|
|
2238
|
+
};
|
|
1803
2239
|
}
|
|
1804
2240
|
const pairCmd = await this.fetchWorkspacePairCommand();
|
|
1805
2241
|
if (!pairCmd) {
|
|
@@ -1897,12 +2333,12 @@ ${res.content}` }] };
|
|
|
1897
2333
|
return { content: [{ type: "text", text: "tab_id is required for action=close" }], isError: true };
|
|
1898
2334
|
}
|
|
1899
2335
|
const res = await this.potassium.callTool("tabs", { action: "close", tab_id: tabId, id: tabId });
|
|
1900
|
-
if (!res.ok && res.text
|
|
2336
|
+
if (!res.ok && /dirty|unsaved/i.test(res.text)) {
|
|
1901
2337
|
return {
|
|
1902
2338
|
content: [
|
|
1903
2339
|
{
|
|
1904
2340
|
type: "text",
|
|
1905
|
-
text: `${res.text} (Note: Potassium MCP refuses to close tabs
|
|
2341
|
+
text: `${res.text} (Note: Potassium MCP refuses to close tabs with unsaved changes; reuse "${SCRATCH_TAB_NAME}" via editor_tabs write/open with reuse_scratch=true instead.)`
|
|
1906
2342
|
}
|
|
1907
2343
|
],
|
|
1908
2344
|
isError: true
|
|
@@ -1934,6 +2370,12 @@ ${res.content}` }] };
|
|
|
1934
2370
|
...args.readonly === true ? { readonly: true } : {},
|
|
1935
2371
|
...args.guard === true ? { guard: true } : {}
|
|
1936
2372
|
});
|
|
2373
|
+
} else {
|
|
2374
|
+
execRes = {
|
|
2375
|
+
...execRes,
|
|
2376
|
+
text: `${execRes.text}
|
|
2377
|
+
[Zero-Touch Auto-Inject failed: ${inj.text}]`
|
|
2378
|
+
};
|
|
1937
2379
|
}
|
|
1938
2380
|
}
|
|
1939
2381
|
return {
|
|
@@ -2030,8 +2472,12 @@ ${luauBody}`,
|
|
|
2030
2472
|
const action = String(args.action ?? "");
|
|
2031
2473
|
switch (action) {
|
|
2032
2474
|
case "status": {
|
|
2475
|
+
let winErr = null;
|
|
2033
2476
|
const [wins, kClients] = await Promise.all([
|
|
2034
|
-
this.windows.listWindows().catch(() =>
|
|
2477
|
+
this.windows.listWindows().catch((e) => {
|
|
2478
|
+
winErr = e instanceof Error ? e.message : String(e);
|
|
2479
|
+
return [];
|
|
2480
|
+
}),
|
|
2035
2481
|
this.potassium.listClients()
|
|
2036
2482
|
]);
|
|
2037
2483
|
const rbxWins = wins.filter(
|
|
@@ -2039,6 +2485,7 @@ ${luauBody}`,
|
|
|
2039
2485
|
);
|
|
2040
2486
|
const potWins = wins.filter((w) => w.processName.toLowerCase().includes("potassium"));
|
|
2041
2487
|
const lines = [
|
|
2488
|
+
...winErr ? [`Desktop window query warning: ${winErr}`] : [],
|
|
2042
2489
|
`Roblox windows: ${rbxWins.length}${rbxWins.length ? ` (${rbxWins.map((w) => `PID ${w.pid}, ${w.responding ? "responding" : "NOT RESPONDING"}, client ${w.clientRect.width}x${w.clientRect.height}`).join("; ")})` : ""}`,
|
|
2043
2490
|
`Potassium windows: ${potWins.length}${potWins.length ? ` (PID ${potWins.map((w) => w.pid).join(", ")})` : ""}`,
|
|
2044
2491
|
`Potassium MCP (${this.potassium.url}): ${kClients.ok ? `online (${kClients.clients.length} attached client(s))` : `offline (${kClients.rawText})`}`
|
|
@@ -2087,8 +2534,9 @@ ${luauBody}`,
|
|
|
2087
2534
|
const launched = await this.windows.launchRoblox(launchCfg);
|
|
2088
2535
|
notes.push(launched.text);
|
|
2089
2536
|
if (args.auto_inject !== false && this.autoInject) {
|
|
2090
|
-
const
|
|
2091
|
-
|
|
2537
|
+
const waitForClientMs = typeof args.wait_for_client_ms === "number" ? args.wait_for_client_ms : 3e4;
|
|
2538
|
+
const inj = await this.tryZeroTouchAutoInject({ waitForClientMs });
|
|
2539
|
+
notes.push(inj.injected ? inj.text : `[Zero-Touch Auto-Inject failed: ${inj.text}]`);
|
|
2092
2540
|
}
|
|
2093
2541
|
return { content: [{ type: "text", text: notes.join("\n") }], isError: !launched.ok };
|
|
2094
2542
|
}
|
|
@@ -2167,10 +2615,12 @@ ${luauBody}`,
|
|
|
2167
2615
|
}
|
|
2168
2616
|
}
|
|
2169
2617
|
async handleDesktopScreenshot(args) {
|
|
2618
|
+
const method = args.method === "copy" || args.method === "printwindow" || args.method === "auto" ? args.method : "auto";
|
|
2170
2619
|
const shot = await this.windows.captureScreenshot({
|
|
2171
2620
|
target: typeof args.target === "string" ? args.target : "roblox",
|
|
2172
2621
|
focusFirst: args.focus_first === true,
|
|
2173
|
-
clientOnly: args.client_only !== false
|
|
2622
|
+
clientOnly: args.client_only !== false,
|
|
2623
|
+
method
|
|
2174
2624
|
});
|
|
2175
2625
|
if (!shot.ok || !shot.data) {
|
|
2176
2626
|
return {
|
|
@@ -2178,7 +2628,7 @@ ${luauBody}`,
|
|
|
2178
2628
|
isError: true
|
|
2179
2629
|
};
|
|
2180
2630
|
}
|
|
2181
|
-
const meta = `desktop_screenshot (${shot.width ?? "?"}x${shot.height ?? "?"}${shot.clientRect ? `, client origin screen=(${shot.clientRect.screenLeft},${shot.clientRect.screenTop})` : ""})`;
|
|
2631
|
+
const meta = `desktop_screenshot (${shot.width ?? "?"}x${shot.height ?? "?"}${shot.captureMethod ? `, method=${shot.captureMethod}` : ""}${shot.clientRect ? `, client origin screen=(${shot.clientRect.screenLeft},${shot.clientRect.screenTop})` : ""})`;
|
|
2182
2632
|
return {
|
|
2183
2633
|
content: [
|
|
2184
2634
|
{ type: "text", text: meta },
|
|
@@ -2242,6 +2692,12 @@ ${lines.join("\n")}` }]
|
|
|
2242
2692
|
readonly: true,
|
|
2243
2693
|
...typeof args.session === "string" ? { session: args.session } : {}
|
|
2244
2694
|
});
|
|
2695
|
+
} else {
|
|
2696
|
+
queryRes = {
|
|
2697
|
+
...queryRes,
|
|
2698
|
+
text: `${queryRes.text}
|
|
2699
|
+
[Zero-Touch Auto-Inject failed: ${inj.text}]`
|
|
2700
|
+
};
|
|
2245
2701
|
}
|
|
2246
2702
|
}
|
|
2247
2703
|
if (!queryRes.ok) {
|
|
@@ -2486,6 +2942,12 @@ ${retryContent.text}`;
|
|
|
2486
2942
|
}
|
|
2487
2943
|
return JSON.stringify(retryRes.body);
|
|
2488
2944
|
}
|
|
2945
|
+
} else {
|
|
2946
|
+
const firstContent = resultObj.content?.find((c) => c.type === "text");
|
|
2947
|
+
if (firstContent && typeof firstContent.text === "string") {
|
|
2948
|
+
firstContent.text = `${firstContent.text}
|
|
2949
|
+
[Zero-Touch Auto-Inject failed: ${inj.text}]`;
|
|
2950
|
+
}
|
|
2489
2951
|
}
|
|
2490
2952
|
}
|
|
2491
2953
|
}
|
|
@@ -2524,7 +2986,7 @@ function startStdioBridge(options = {}) {
|
|
|
2524
2986
|
const stdin = options.stdin ?? process.stdin;
|
|
2525
2987
|
const stdout = options.stdout ?? process.stdout;
|
|
2526
2988
|
const bridge = new HybridMcpBridge(options);
|
|
2527
|
-
const rl =
|
|
2989
|
+
const rl = createInterface2({ input: stdin, crlfDelay: Number.POSITIVE_INFINITY });
|
|
2528
2990
|
const pending = /* @__PURE__ */ new Set();
|
|
2529
2991
|
rl.on("line", (line) => {
|
|
2530
2992
|
const task = (async () => {
|
|
@@ -2540,6 +3002,7 @@ function startStdioBridge(options = {}) {
|
|
|
2540
3002
|
const done = new Promise((resolveDone) => {
|
|
2541
3003
|
rl.on("close", async () => {
|
|
2542
3004
|
await Promise.allSettled([...pending]);
|
|
3005
|
+
bridge.windows.dispose?.();
|
|
2543
3006
|
resolveDone();
|
|
2544
3007
|
});
|
|
2545
3008
|
});
|
|
@@ -2559,9 +3022,14 @@ export {
|
|
|
2559
3022
|
PowerShellWindowsDriver,
|
|
2560
3023
|
SCRATCH_TAB_NAME,
|
|
2561
3024
|
TOKEN_SHAPE,
|
|
3025
|
+
buildListWindowsPsScript,
|
|
2562
3026
|
buildRobloxDeepLink,
|
|
3027
|
+
buildScreenshotPsScript,
|
|
3028
|
+
buildUiTreePsScript,
|
|
3029
|
+
buildUipiWarning,
|
|
2563
3030
|
discoverPotassiumAutoexecDir,
|
|
2564
3031
|
discoverPotassiumToken,
|
|
3032
|
+
encodePowerShellCommand,
|
|
2565
3033
|
findPotassiumBinary,
|
|
2566
3034
|
findRobloxPlayerBinary,
|
|
2567
3035
|
forwardJsonRpcLine,
|