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