@copylee/dsh-computer-use 0.8.3 → 0.8.5

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.
@@ -82,6 +82,10 @@ namespace DshComputerUse
82
82
  [DllImport("user32.dll")] public static extern IntPtr GetWindow(IntPtr hwnd, uint cmd);
83
83
  [DllImport("user32.dll")] public static extern int GetWindowLong(IntPtr hwnd, int index);
84
84
  [DllImport("user32.dll")] public static extern int SetWindowLong(IntPtr hwnd, int index, int value);
85
+ [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool SetProp(IntPtr hwnd, string name, IntPtr value);
86
+ [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr GetProp(IntPtr hwnd, string name);
87
+ [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr RemoveProp(IntPtr hwnd, string name);
88
+ [DllImport("user32.dll")] public static extern bool GetLayeredWindowAttributes(IntPtr hwnd, out uint key, out byte alpha, out uint flags);
85
89
  [DllImport("user32.dll")] public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint key, byte alpha, uint flags);
86
90
  [DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hwnd, out uint pid);
87
91
  [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
@@ -205,11 +209,13 @@ namespace DshComputerUse
205
209
  var parser = new JavaScriptSerializer();
206
210
  parser.MaxJsonLength = int.MaxValue;
207
211
  Apps.Warm();
212
+ Windows.RecoverCards(false);
208
213
 
209
214
  var hello = new Dictionary<string, object>();
210
215
  hello["event"] = "ready"; hello["version"] = Version; hello["pid"] = Process.GetCurrentProcess().Id;
211
216
  Emit(hello);
212
217
 
218
+ try {
213
219
  string line;
214
220
  while ((line = input.ReadLine()) != null)
215
221
  {
@@ -232,7 +238,8 @@ namespace DshComputerUse
232
238
  }
233
239
  Emit(reply);
234
240
  }
235
- Overlay.Shutdown();
241
+ }
242
+ finally { Windows.RecoverCards(true); Overlay.Shutdown(); }
236
243
  return 0;
237
244
  }
238
245
 
@@ -283,6 +290,7 @@ namespace DshComputerUse
283
290
  case "clipboard_release": Clip.Release(); return null;
284
291
  case "paste": return Input.Paste(Args.Str(r, "text", ""), Args.Bool(r, "restore", true), Args.Int(r, "waitMs", 250));
285
292
  case "windows": return Windows.List();
293
+ case "window_recover": Windows.RecoverCards(false); return null;
286
294
  case "background": return Windows.Background(Args.Bool(r, "tray", true));
287
295
  case "tray_restore": return Tray.Restore(Args.Str(r, "exe", ""));
288
296
  case "card_opacity": CardLayer.SetOpacity(Args.Int(r, "opacity", 100)); return null;
@@ -960,7 +968,9 @@ namespace DshComputerUse
960
968
  {
961
969
  var hwnd = Hwnd;
962
970
  Hwnd = IntPtr.Zero;
963
- if (hwnd != IntPtr.Zero && Native.IsWindow(hwnd)) Native.SetWindowLong(hwnd, -20, originalEx);
971
+ lock (Gate) {
972
+ if (hwnd != IntPtr.Zero && Native.IsWindow(hwnd)) Native.SetWindowLong(hwnd, -20, originalEx);
973
+ }
964
974
  }
965
975
 
966
976
  public static Rectangle Frame(IntPtr hwnd)
@@ -1380,20 +1390,60 @@ namespace DshComputerUse
1380
1390
  return d;
1381
1391
  }
1382
1392
 
1383
- static readonly Dictionary<long, Native.WINDOWPLACEMENT> cards = new Dictionary<long, Native.WINDOWPLACEMENT>();
1393
+ // Values live on the target HWND, not in helper memory. A replacement
1394
+ // helper can restore them after a timeout/kill, without stale HWND files.
1395
+ const string CardPrefix = "DSH.ComputerUse.Card.v1.";
1396
+ static readonly string[] CardFields = { "flags", "show", "minX", "minY", "maxX", "maxY", "left", "top", "right", "bottom", "ex", "key", "alpha", "layerFlags" };
1397
+
1398
+ static void SaveCard(IntPtr hwnd)
1399
+ {
1400
+ if (Native.GetProp(hwnd, CardPrefix + "owner") != IntPtr.Zero) return;
1401
+ var wp = new Native.WINDOWPLACEMENT(); wp.length = Marshal.SizeOf(wp);
1402
+ if (!Native.GetWindowPlacement(hwnd, ref wp)) throw new Exception("Cannot save host window placement");
1403
+ uint key, flags; byte alpha;
1404
+ Native.GetLayeredWindowAttributes(hwnd, out key, out alpha, out flags);
1405
+ int[] values = { wp.flags, wp.showCmd, wp.ptMinPosition.X, wp.ptMinPosition.Y, wp.ptMaxPosition.X, wp.ptMaxPosition.Y,
1406
+ wp.rcNormalPosition.Left, wp.rcNormalPosition.Top, wp.rcNormalPosition.Right, wp.rcNormalPosition.Bottom,
1407
+ Native.GetWindowLong(hwnd, -20), unchecked((int)key), alpha, unchecked((int)flags) };
1408
+ try {
1409
+ for (int i = 0; i < values.Length; i++)
1410
+ if (!Native.SetProp(hwnd, CardPrefix + CardFields[i], new IntPtr(values[i]))) throw new Exception("Cannot save host window state");
1411
+ // Commit marker is written last, before any window mutation.
1412
+ if (!Native.SetProp(hwnd, CardPrefix + "owner", new IntPtr(Process.GetCurrentProcess().Id))) throw new Exception("Cannot save host window owner");
1413
+ }
1414
+ catch { ClearCard(hwnd); throw; }
1415
+ }
1416
+
1417
+ static void ClearCard(IntPtr hwnd)
1418
+ {
1419
+ Native.RemoveProp(hwnd, CardPrefix + "owner");
1420
+ foreach (string field in CardFields) Native.RemoveProp(hwnd, CardPrefix + field);
1421
+ }
1422
+
1423
+ public static void RecoverCards(bool own)
1424
+ {
1425
+ int self = Process.GetCurrentProcess().Id;
1426
+ Native.EnumWindowsProc cb = delegate(IntPtr hwnd, IntPtr data) {
1427
+ int owner = Native.GetProp(hwnd, CardPrefix + "owner").ToInt32();
1428
+ if (owner == 0) return true;
1429
+ bool restore = own && owner == self;
1430
+ if (!own && owner != self) {
1431
+ try { using (var p = Process.GetProcessById(owner)) restore = p.HasExited; }
1432
+ catch (ArgumentException) { restore = true; }
1433
+ catch (Exception) { return true; } // do not touch a live/inaccessible helper
1434
+ }
1435
+ if (restore) { try { Uncard(hwnd); } catch (Exception) { } }
1436
+ return true;
1437
+ };
1438
+ Native.EnumWindows(cb, IntPtr.Zero); GC.KeepAlive(cb);
1439
+ }
1384
1440
 
1385
1441
  /// Shrink a window into an always-on-top card; Uncard restores it exactly.
1386
1442
  public static object Card(IntPtr hwnd, int x, int y, int w, int h)
1387
1443
  {
1388
1444
  if (!Native.IsWindow(hwnd)) throw new Exception("window no longer exists");
1445
+ SaveCard(hwnd);
1389
1446
  CardLayer.Attach(hwnd);
1390
- if (!cards.ContainsKey(hwnd.ToInt64()))
1391
- {
1392
- var wp = new Native.WINDOWPLACEMENT();
1393
- wp.length = Marshal.SizeOf(typeof(Native.WINDOWPLACEMENT));
1394
- Native.GetWindowPlacement(hwnd, ref wp);
1395
- cards[hwnd.ToInt64()] = wp;
1396
- }
1397
1447
  // A minimized window restores to its previous state, which may be maximized: restore until normal.
1398
1448
  for (int i = 0; i < 2 && (Native.IsIconic(hwnd) || Native.IsZoomed(hwnd)); i++) { Native.ShowWindow(hwnd, 9); Thread.Sleep(150); }
1399
1449
  Native.SetWindowPos(hwnd, new IntPtr(-1), x, y, w, h, 0x0010 | 0x0040); // TOPMOST, NOACTIVATE | SHOWWINDOW
@@ -1410,19 +1460,25 @@ namespace DshComputerUse
1410
1460
  public static object Uncard(IntPtr hwnd)
1411
1461
  {
1412
1462
  if (CardLayer.Hwnd == hwnd) CardLayer.Detach();
1413
- Native.WINDOWPLACEMENT wp;
1414
- bool known = cards.TryGetValue(hwnd.ToInt64(), out wp);
1415
- cards.Remove(hwnd.ToInt64());
1416
1463
  if (!Native.IsWindow(hwnd)) return null;
1417
- Native.SetWindowPos(hwnd, new IntPtr(-2), 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0010); // NOTOPMOST
1418
- if (known)
1419
- {
1464
+ if (Native.GetProp(hwnd, CardPrefix + "owner") != IntPtr.Zero)
1465
+ {
1466
+ int[] v = new int[CardFields.Length];
1467
+ for (int i = 0; i < v.Length; i++) v[i] = Native.GetProp(hwnd, CardPrefix + CardFields[i]).ToInt32();
1468
+ var wp = new Native.WINDOWPLACEMENT(); wp.length = Marshal.SizeOf(wp);
1469
+ wp.flags = v[0]; wp.showCmd = v[1];
1470
+ wp.ptMinPosition = new Native.POINT(v[2], v[3]); wp.ptMaxPosition = new Native.POINT(v[4], v[5]);
1471
+ wp.rcNormalPosition = new Native.RECT { Left = v[6], Top = v[7], Right = v[8], Bottom = v[9] };
1472
+ Native.SetWindowLong(hwnd, -20, v[10]);
1473
+ if ((v[10] & 0x80000) != 0 && v[13] != 0) Native.SetLayeredWindowAttributes(hwnd, unchecked((uint)v[11]), (byte)v[12], unchecked((uint)v[13]));
1474
+ if (!Native.SetWindowPos(hwnd, new IntPtr((v[10] & 8) != 0 ? -1 : -2), 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0010)) throw new Exception("Cannot restore host window style");
1420
1475
  // Restore the normal rectangle first, then the original show state (maximized etc.).
1421
1476
  int show = wp.showCmd;
1422
1477
  wp.showCmd = 1; // SW_SHOWNORMAL
1423
- Native.SetWindowPlacement(hwnd, ref wp);
1478
+ if (!Native.SetWindowPlacement(hwnd, ref wp)) throw new Exception("Cannot restore host window placement");
1424
1479
  if (show == 3) Native.ShowWindow(hwnd, 3);
1425
1480
  else if (show == 2) Native.ShowWindow(hwnd, 6);
1481
+ ClearCard(hwnd);
1426
1482
  }
1427
1483
  return Describe(hwnd);
1428
1484
  }
package/lib/client.js CHANGED
@@ -231,7 +231,9 @@ window.__ModuleLoader__.load({
231
231
  const MARKER = "data-dsh-computer-use-settings-nav";
232
232
  const POINTER_SVG = "%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4.037 4.688a.495.495 0 0 1 .651-.651l16 6.5a.5.5 0 0 1-.063.947l-6.124 1.58a2 2 0 0 0-1.438 1.435l-1.579 6.126a.5.5 0 0 1-.947.063z'/%3E%3C/svg%3E";
233
233
  const navIconCss = `
234
- [${MARKER}] > svg:first-child { display: none; }
234
+ /* Older shells render the SVG directly; newer shells wrap it in a decorative span. */
235
+ [${MARKER}] > svg:first-child,
236
+ [${MARKER}] > span[aria-hidden="true"]:first-child { display: none; }
235
237
  [${MARKER}]::before {
236
238
  content: '';
237
239
  flex: none;
package/lib/index.js CHANGED
@@ -1618,12 +1618,13 @@ function powershellPath() {
1618
1618
  const windir = process.env.SystemRoot ?? process.env.windir ?? "C:\\Windows";
1619
1619
  return join(windir, "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
1620
1620
  }
1621
- var HelperClient = class {
1621
+ var HelperClient = class HelperClient {
1622
1622
  log;
1623
1623
  child;
1624
1624
  starting;
1625
1625
  pending = /* @__PURE__ */ new Map();
1626
1626
  listeners = /* @__PURE__ */ new Set();
1627
+ cardChildren = /* @__PURE__ */ new WeakSet();
1627
1628
  nextId = 0;
1628
1629
  disposed = false;
1629
1630
  constructor(log = () => {}) {
@@ -1638,6 +1639,7 @@ var HelperClient = class {
1638
1639
  async call(cmd, args = {}, timeoutMs = 2e4) {
1639
1640
  if (this.disposed) throw new Error("computer-use helper is shut down");
1640
1641
  const child = await this.ensure();
1642
+ if (this.disposed) throw new Error("computer-use helper is shut down");
1641
1643
  const id = ++this.nextId;
1642
1644
  return new Promise((resolve, reject) => {
1643
1645
  const timer = setTimeout(() => {
@@ -1648,8 +1650,10 @@ var HelperClient = class {
1648
1650
  this.pending.set(id, {
1649
1651
  resolve,
1650
1652
  reject,
1651
- timer
1653
+ timer,
1654
+ child
1652
1655
  });
1656
+ if (cmd === "window_card") this.cardChildren.add(child);
1653
1657
  child.stdin.write(`${JSON.stringify({
1654
1658
  ...args,
1655
1659
  id,
@@ -1735,16 +1739,31 @@ var HelperClient = class {
1735
1739
  child.stderr.setEncoding("utf8");
1736
1740
  child.stderr.on("data", (chunk) => this.log(`helper stderr: ${chunk.trim()}`));
1737
1741
  child.on("exit", (code) => {
1738
- if (this.child === child) this.child = void 0;
1739
- this.starting = void 0;
1742
+ if (this.child === child) {
1743
+ this.child = void 0;
1744
+ this.starting = void 0;
1745
+ }
1740
1746
  for (const [id, entry] of this.pending) {
1747
+ if (entry.child !== child) continue;
1741
1748
  clearTimeout(entry.timer);
1742
1749
  entry.reject(/* @__PURE__ */ new Error(`computer-use helper exited (code ${String(code)})`));
1743
1750
  this.pending.delete(id);
1744
1751
  }
1752
+ if (this.cardChildren.has(child) && (!this.disposed || code !== 0)) {
1753
+ const recovery = this.disposed ? new HelperClient(this.log) : this;
1754
+ recovery.call("window_recover").catch((error) => {
1755
+ if (!recovery.disposed) this.log(`recover host window: ${String(error)}`);
1756
+ }).finally(() => {
1757
+ if (recovery !== this) recovery.dispose();
1758
+ });
1759
+ }
1745
1760
  });
1746
1761
  await readyPromise;
1747
1762
  this.child = child;
1763
+ if (this.disposed) {
1764
+ this.kill();
1765
+ throw new Error("computer-use helper is shut down");
1766
+ }
1748
1767
  return child;
1749
1768
  }
1750
1769
  handle(line, onReady) {
@@ -7,6 +7,6 @@
7
7
  * our own row by its label and let CSS swap the glyph (Lucide mouse-pointer-2,
8
8
  * drawn as a currentColor mask so hover / active colors still apply).
9
9
  */
10
- export declare const navIconCss = "\n[data-dsh-computer-use-settings-nav] > svg:first-child { display: none; }\n[data-dsh-computer-use-settings-nav]::before {\n content: '';\n flex: none;\n width: 16px;\n height: 16px;\n background: currentColor;\n -webkit-mask: url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4.037 4.688a.495.495 0 0 1 .651-.651l16 6.5a.5.5 0 0 1-.063.947l-6.124 1.58a2 2 0 0 0-1.438 1.435l-1.579 6.126a.5.5 0 0 1-.947.063z'/%3E%3C/svg%3E\") center / contain no-repeat;\n mask: url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4.037 4.688a.495.495 0 0 1 .651-.651l16 6.5a.5.5 0 0 1-.063.947l-6.124 1.58a2 2 0 0 0-1.438 1.435l-1.579 6.126a.5.5 0 0 1-.947.063z'/%3E%3C/svg%3E\") center / contain no-repeat;\n}\n";
10
+ export declare const navIconCss = "\n/* Older shells render the SVG directly; newer shells wrap it in a decorative span. */\n[data-dsh-computer-use-settings-nav] > svg:first-child,\n[data-dsh-computer-use-settings-nav] > span[aria-hidden=\"true\"]:first-child { display: none; }\n[data-dsh-computer-use-settings-nav]::before {\n content: '';\n flex: none;\n width: 16px;\n height: 16px;\n background: currentColor;\n -webkit-mask: url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4.037 4.688a.495.495 0 0 1 .651-.651l16 6.5a.5.5 0 0 1-.063.947l-6.124 1.58a2 2 0 0 0-1.438 1.435l-1.579 6.126a.5.5 0 0 1-.947.063z'/%3E%3C/svg%3E\") center / contain no-repeat;\n mask: url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4.037 4.688a.495.495 0 0 1 .651-.651l16 6.5a.5.5 0 0 1-.063.947l-6.124 1.58a2 2 0 0 0-1.438 1.435l-1.579 6.126a.5.5 0 0 1-.947.063z'/%3E%3C/svg%3E\") center / contain no-repeat;\n}\n";
11
11
  /** Keep the marker on the settings-nav button labelled `label`; returns a disposer. */
12
12
  export declare function registerNavIcon(label: string): () => void;
@@ -20,6 +20,7 @@ export declare class HelperClient implements HelperLike {
20
20
  private starting;
21
21
  private readonly pending;
22
22
  private readonly listeners;
23
+ private readonly cardChildren;
23
24
  private nextId;
24
25
  private disposed;
25
26
  constructor(log?: (message: string) => void);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@copylee/dsh-computer-use",
3
- "version": "0.8.3",
3
+ "version": "0.8.5",
4
4
  "description": "DeepSeek Harness Computer Use 插件(Windows):让 AI 看屏幕、点鼠标、敲键盘操控电脑;控制时屏幕泛起橙色光晕并显示“DeepSeek Harness 正在操控你的电脑”,按 Esc 暂停 / 继续。",
5
5
  "type": "module",
6
6
  "author": "copylee",