@copylee/dsh-computer-use 0.1.8 → 0.1.10

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 CHANGED
@@ -12,7 +12,7 @@
12
12
  | 按应用授权 | 先 `request_access` 列出要操控的应用,经 DSH 原生审批后才能操作;也可改为“全部允许” |
13
13
  | 不会操作自己 | 永远拒绝向 DeepSeek Harness 自身窗口输入;常见密码管理器默认禁止,可再自定义禁用列表 |
14
14
  | 正在控制的提示 | 橙色光晕 + 胶囊提示条(显示当前动作),**不会出现在截图里** |
15
- | 悬浮卡片 | 操控期间 DeepSeek Harness 窗口缩成右下角置顶卡片,结束后自动恢复原尺寸、位置和最大化 / 最小化状态(也可选最小化或不动)。卡片只给你看:AI 的截图里看到的是卡片下面的内容(截图那一瞬间卡片会透明几十毫秒),点击和滚动也直接穿透卡片,卡片不会挪位置 |
15
+ | 悬浮卡片 | 操控期间 DeepSeek Harness 窗口缩成右下角置顶卡片,结束后自动恢复原尺寸、位置和最大化 / 最小化状态(也可选最小化或不动)。卡片只给你看:AI 的截图里看到的是卡片下面的内容(通过 Windows 放大镜接口截图时把卡片排除在外,不闪烁;该接口不可用时退回为截图瞬间让卡片透明约 20 毫秒),点击和滚动也直接穿透卡片,卡片不会挪位置 |
16
16
  | 暂停与停止 | 你亲手按 **Esc** 暂停(AI 停在下一步之前),再按 Esc 或点“继续”恢复,对话不会被中断;点“停止”才结束本轮。插件自己发出的 Esc 不会误触发 |
17
17
  | 你随时可以插手 | 你用键盘打字时 AI 自动暂停,停手 1.5 秒后继续(单按 Shift / Ctrl 等修饰键不算);你切换或打开了别的窗口,AI 下一步不会盲目执行,而是先看新截图再决定。你只是点进 DSH 卡片看对话时不算切换,AI 的按键会自动回到它正在操作的窗口。鼠标移动不会触发,避免误触 |
18
18
  | 中文输入 | Unicode 直接输入,不受输入法状态影响;长文本走剪贴板粘贴,剪贴板文本统一为 CRLF,粘贴到 Obsidian 等应用不丢换行 |
@@ -136,6 +136,11 @@ namespace DshComputerUse
136
136
  object v; if (!d.TryGetValue(k, out v) || v == null) return def;
137
137
  return (int)Math.Round(Convert.ToDouble(v));
138
138
  }
139
+ public static double Double(Dictionary<string, object> d, string k, double def)
140
+ {
141
+ object v;
142
+ return d.TryGetValue(k, out v) && v != null ? Convert.ToDouble(v, System.Globalization.CultureInfo.InvariantCulture) : def;
143
+ }
139
144
  public static string Str(Dictionary<string, object> d, string k, string def)
140
145
  {
141
146
  object v; if (!d.TryGetValue(k, out v) || v == null) return def;
@@ -253,8 +258,6 @@ namespace DshComputerUse
253
258
  case "displays": return Screen2.Displays();
254
259
  case "screenshot": return Screen2.Capture(r);
255
260
  case "settle": return Screen2.Settle(r);
256
- case "shot_mark": Screen2.Mark(r); return null;
257
- case "shot_diff": return Screen2.Diff(r);
258
261
  case "cursor": { Native.POINT p; Native.GetCursorPos(out p); return Pt(p.X, p.Y); }
259
262
  case "move": using (new PassThrough()) { Overlay.Dodge(Args.Int(r, "x", 0), Args.Int(r, "y", 0)); Input.Move(Args.Int(r, "x", 0), Args.Int(r, "y", 0)); } return null;
260
263
  case "click": using (new PassThrough()) return Input.Click(r);
@@ -318,12 +321,8 @@ namespace DshComputerUse
318
321
  }
319
322
 
320
323
  /** Sample every 8th pixel of a screen rectangle (BGR bytes). */
321
- /**
322
- * composite: paint what is under the DSH card (exact, slower: used once
323
- * per action). Otherwise the card area is blanked so its own updates
324
- * (streaming text, timers) never look like screen activity.
325
- */
326
- static byte[] Sample(int x, int y, int w, int h, bool composite)
324
+ /** Sample the screen for settle detection; the card area is blanked (no flicker). */
325
+ static byte[] Sample(int x, int y, int w, int h)
327
326
  {
328
327
  using (var bmp = new Bitmap(w, h, PixelFormat.Format24bppRgb))
329
328
  {
@@ -331,53 +330,41 @@ namespace DshComputerUse
331
330
  {
332
331
  IntPtr dst = g.GetHdc();
333
332
  IntPtr src = Native.GetDC(IntPtr.Zero);
334
- using (composite ? CardLayer.Hide() : null)
335
- {
336
- try { Native.BitBlt(dst, 0, 0, w, h, src, x, y, 0x00CC0020 | 0x40000000); }
337
- finally { Native.ReleaseDC(IntPtr.Zero, src); g.ReleaseHdc(dst); }
338
- }
339
- if (!composite) CardLayer.Blank(g, x, y);
333
+ try { Native.BitBlt(dst, 0, 0, w, h, src, x, y, 0x00CC0020 | 0x40000000); }
334
+ finally { Native.ReleaseDC(IntPtr.Zero, src); g.ReleaseHdc(dst); }
335
+ CardLayer.Blank(g, x, y);
340
336
  }
341
- var data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
342
- try
337
+ return SampleBitmap(bmp);
338
+ }
339
+ }
340
+
341
+ /** Every 8th pixel of a 24-bit bitmap (BGR bytes). */
342
+ static byte[] SampleBitmap(Bitmap bmp)
343
+ {
344
+ int w = bmp.Width, h = bmp.Height;
345
+ var data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
346
+ try
347
+ {
348
+ int sx = (w + 7) / 8, sy = (h + 7) / 8;
349
+ var row = new byte[data.Stride];
350
+ var outp = new byte[sx * sy * 3];
351
+ int k = 0;
352
+ for (int j = 0; j < sy; j++)
343
353
  {
344
- int sx = (w + 7) / 8, sy = (h + 7) / 8;
345
- var row = new byte[data.Stride];
346
- var outp = new byte[sx * sy * 3];
347
- int k = 0;
348
- for (int j = 0; j < sy; j++)
349
- {
350
- Marshal.Copy(new IntPtr(data.Scan0.ToInt64() + (long)j * 8 * data.Stride), row, 0, data.Stride);
351
- for (int i = 0; i < sx; i++) { int o = i * 8 * 3; outp[k++] = row[o]; outp[k++] = row[o + 1]; outp[k++] = row[o + 2]; }
352
- }
353
- return outp;
354
+ Marshal.Copy(new IntPtr(data.Scan0.ToInt64() + (long)j * 8 * data.Stride), row, 0, data.Stride);
355
+ for (int i = 0; i < sx; i++) { int o = i * 8 * 3; outp[k++] = row[o]; outp[k++] = row[o + 1]; outp[k++] = row[o + 2]; }
354
356
  }
355
- finally { bmp.UnlockBits(data); }
357
+ return outp;
356
358
  }
359
+ finally { bmp.UnlockBits(data); }
357
360
  }
358
361
 
362
+ /** What the screen looked like in the last screenshot the model was shown. */
359
363
  static byte[] marked;
360
364
  static string markedRect;
361
365
 
362
366
  static string RectKey(int x, int y, int w, int h) { return x + "," + y + "," + w + "," + h; }
363
367
 
364
- /** Remember what the screen looked like when the model was last shown it. */
365
- public static void Mark(Dictionary<string, object> r)
366
- {
367
- int x = Args.Int(r, "x", 0), y = Args.Int(r, "y", 0), w = Args.Int(r, "width", 0), h = Args.Int(r, "height", 0);
368
- marked = Sample(x, y, w, h, true);
369
- markedRect = RectKey(x, y, w, h);
370
- }
371
-
372
- /** Fraction of sampled pixels changed since Mark (-1 when there is nothing comparable). */
373
- public static Dictionary<string, object> Diff(Dictionary<string, object> r)
374
- {
375
- int x = Args.Int(r, "x", 0), y = Args.Int(r, "y", 0), w = Args.Int(r, "width", 0), h = Args.Int(r, "height", 0);
376
- var d = new Dictionary<string, object>();
377
- d["diff"] = marked == null || markedRect != RectKey(x, y, w, h) ? -1.0 : Changed(marked, Sample(x, y, w, h, true));
378
- return d;
379
- }
380
-
381
368
  static double Changed(byte[] a, byte[] b)
382
369
  {
383
370
  int n = a.Length / 3, diff = 0;
@@ -397,13 +384,13 @@ namespace DshComputerUse
397
384
  int minMs = Args.Int(r, "minMs", 300), maxMs = Args.Int(r, "maxMs", 2500), interval = Args.Int(r, "intervalMs", 120), quietMs = Args.Int(r, "quietMs", 240);
398
385
  if (w <= 0 || h <= 0) throw new Exception("invalid settle rect");
399
386
  var watch = System.Diagnostics.Stopwatch.StartNew();
400
- byte[] prev = Sample(x, y, w, h, false);
387
+ byte[] prev = Sample(x, y, w, h);
401
388
  long quietSince = -1;
402
389
  bool stable = false;
403
390
  while (true)
404
391
  {
405
392
  Thread.Sleep(interval);
406
- byte[] cur = Sample(x, y, w, h, false);
393
+ byte[] cur = Sample(x, y, w, h);
407
394
  long now = watch.ElapsedMilliseconds;
408
395
  if (Changed(prev, cur) < 0.002) { if (quietSince < 0) quietSince = now - interval; }
409
396
  else quietSince = -1;
@@ -425,6 +412,7 @@ namespace DshComputerUse
425
412
  int ow = Args.Int(r, "outWidth", w), oh = Args.Int(r, "outHeight", h);
426
413
  int quality = Args.Int(r, "quality", 80);
427
414
  if (w <= 0 || h <= 0 || ow <= 0 || oh <= 0) throw new Exception("invalid capture size");
415
+ string via = "blt";
428
416
  using (var bmp = new Bitmap(w, h, PixelFormat.Format24bppRgb))
429
417
  {
430
418
  using (var g = Graphics.FromImage(bmp))
@@ -432,11 +420,45 @@ namespace DshComputerUse
432
420
  IntPtr dst = g.GetHdc();
433
421
  IntPtr src = Native.GetDC(IntPtr.Zero);
434
422
  // The DSH card is for the user only: capture what is under it.
435
- using (CardLayer.Hide())
423
+ // Preferred: the Magnification API renders the screen without
424
+ // the card (no flicker). Fallback: hide the card for one BitBlt.
425
+ bool done = false;
426
+ var area = new Rectangle(x, y, w, h);
427
+ via = "blt";
428
+ if (CardLayer.Covers(area))
436
429
  {
437
- try { Native.BitBlt(dst, 0, 0, w, h, src, x, y, 0x00CC0020 | 0x40000000); }
438
- finally { Native.ReleaseDC(IntPtr.Zero, src); g.ReleaseHdc(dst); }
430
+ g.ReleaseHdc(dst);
431
+ dst = IntPtr.Zero;
432
+ using (var mag = MagCapture.Capture(area, CardLayer.Hwnd))
433
+ {
434
+ if (mag != null) { g.DrawImageUnscaled(mag, 0, 0); done = true; via = "mag"; }
435
+ }
436
+ if (!done) dst = g.GetHdc();
439
437
  }
438
+ if (!done)
439
+ {
440
+ using (CardLayer.Hide(area))
441
+ {
442
+ try { Native.BitBlt(dst, 0, 0, w, h, src, x, y, 0x00CC0020 | 0x40000000); }
443
+ finally { g.ReleaseHdc(dst); }
444
+ }
445
+ }
446
+ Native.ReleaseDC(IntPtr.Zero, src);
447
+ }
448
+ // compare: report "unchanged" instead of an identical image; mark: remember for next time.
449
+ bool compare = Args.Bool(r, "compare", false), mark = Args.Bool(r, "mark", false) || compare;
450
+ if (mark)
451
+ {
452
+ string key = RectKey(x, y, w, h);
453
+ byte[] sample = SampleBitmap(bmp);
454
+ if (compare && marked != null && markedRect == key && Changed(marked, sample) < Args.Double(r, "threshold", 0.0005))
455
+ {
456
+ var same = new Dictionary<string, object>();
457
+ same["unchanged"] = true;
458
+ return same;
459
+ }
460
+ marked = sample;
461
+ markedRect = key;
440
462
  }
441
463
  Bitmap output = bmp;
442
464
  Bitmap scaled = null;
@@ -465,6 +487,7 @@ namespace DshComputerUse
465
487
  output.Save(ms, jpegCodec, ep);
466
488
  var d = new Dictionary<string, object>();
467
489
  d["data"] = Convert.ToBase64String(ms.ToArray());
490
+ d["via"] = via == "blt" && MagCapture.Why.Length > 0 ? "blt (" + MagCapture.Why + ")" : via;
468
491
  d["width"] = ow; d["height"] = oh;
469
492
  return d;
470
493
  }
@@ -842,10 +865,22 @@ namespace DshComputerUse
842
865
  g.FillRectangle(Brushes.Black, r.X - ox, r.Y - oy, r.Width, r.Height);
843
866
  }
844
867
 
845
- /** Make the card invisible while a capture runs (dispose to show it again). */
846
- public static IDisposable Hide()
868
+ /** Is the card visible over (any part of) this screen rectangle? */
869
+ public static bool Covers(Rectangle area)
870
+ {
871
+ if (!Live()) return false;
872
+ var r = Frame(Hwnd);
873
+ r.Inflate(32, 32);
874
+ return r.IntersectsWith(area);
875
+ }
876
+
877
+ /** Make the card invisible while a capture of `area` runs (dispose to show it again). */
878
+ public static IDisposable Hide(Rectangle area)
847
879
  {
848
- return new Hidden(Live() ? Hwnd : IntPtr.Zero);
880
+ if (!Live()) return new Hidden(IntPtr.Zero);
881
+ var r = Frame(Hwnd);
882
+ r.Inflate(32, 32);
883
+ return new Hidden(r.IntersectsWith(area) ? Hwnd : IntPtr.Zero);
849
884
  }
850
885
 
851
886
  sealed class Hidden : IDisposable
@@ -863,6 +898,102 @@ namespace DshComputerUse
863
898
  }
864
899
  }
865
900
 
901
+ /**
902
+ * Screen capture through the Windows Magnification API, which can leave
903
+ * out windows of other processes (MagSetWindowFilterList). The magnifier
904
+ * control lives in a never-shown host window; MagSetWindowSource renders
905
+ * synchronously into the scaling callback. Any failure disables it for the
906
+ * rest of the run and callers fall back to BitBlt.
907
+ */
908
+ internal static class MagCapture
909
+ {
910
+ [StructLayout(LayoutKind.Sequential)]
911
+ public struct MAGIMAGEHEADER { public uint width; public uint height; public Guid format; public uint stride; public uint offset; public UIntPtr cbSize; }
912
+
913
+ delegate bool ScalingCallback(IntPtr hwnd, IntPtr srcdata, MAGIMAGEHEADER srcheader, IntPtr destdata, MAGIMAGEHEADER destheader, Native.RECT unclipped, Native.RECT clipped, IntPtr dirty);
914
+
915
+ [DllImport("Magnification.dll")] static extern bool MagInitialize();
916
+ [DllImport("Magnification.dll")] static extern bool MagSetWindowSource(IntPtr hwnd, Native.RECT rect);
917
+ [DllImport("Magnification.dll")] static extern bool MagSetWindowFilterList(IntPtr hwnd, int mode, int count, IntPtr[] list);
918
+ [DllImport("Magnification.dll")] static extern bool MagSetImageScalingCallback(IntPtr hwnd, ScalingCallback cb);
919
+ [DllImport("user32.dll", CharSet = CharSet.Unicode)]
920
+ static extern IntPtr CreateWindowEx(int ex, string cls, string name, int style, int x, int y, int w, int h, IntPtr parent, IntPtr menu, IntPtr inst, IntPtr param);
921
+
922
+ static bool broken;
923
+ public static string Why = "";
924
+ static Form host;
925
+ static IntPtr magnifier = IntPtr.Zero;
926
+ static ScalingCallback callback;
927
+ static Bitmap frame;
928
+
929
+ static bool Ready()
930
+ {
931
+ if (broken) return false;
932
+ if (magnifier != IntPtr.Zero) return true;
933
+ try
934
+ {
935
+ if (!MagInitialize()) { broken = true; Why = "init"; return false; }
936
+ host = new Form { FormBorderStyle = FormBorderStyle.None, ShowInTaskbar = false, StartPosition = FormStartPosition.Manual, Location = new Point(0, 0), Size = new Size(16, 16) };
937
+ IntPtr parent = host.Handle; // created, never shown
938
+ magnifier = CreateWindowEx(0, "Magnifier", "cu-magnifier", 0x40000000 | 0x10000000, 0, 0, 16, 16, parent, IntPtr.Zero, Native.GetModuleHandle(null), IntPtr.Zero);
939
+ callback = OnImage;
940
+ if (magnifier == IntPtr.Zero || !MagSetImageScalingCallback(magnifier, callback)) { broken = true; Why = "callback"; return false; }
941
+ return true;
942
+ }
943
+ catch (Exception e) { broken = true; Why = "ready: " + e.Message; return false; }
944
+ }
945
+
946
+ /** The screen rectangle without the `exclude` window, or null when unavailable. */
947
+ public static Bitmap Capture(Rectangle area, IntPtr exclude)
948
+ {
949
+ if (!Ready()) return null;
950
+ try
951
+ {
952
+ // The never-shown host must be as large as the capture, or the control is clipped.
953
+ Native.SetWindowPos(host.Handle, IntPtr.Zero, 0, 0, area.Width, area.Height, 0x0004 | 0x0010);
954
+ Native.SetWindowPos(magnifier, IntPtr.Zero, 0, 0, area.Width, area.Height, 0x0004 | 0x0010); // NOZORDER | NOACTIVATE
955
+ if (!MagSetWindowFilterList(magnifier, 0 /* MW_FILTERMODE_EXCLUDE */, 1, new[] { exclude })) { broken = true; Why = "filter"; return null; }
956
+ frame = null;
957
+ var rc = new Native.RECT { Left = area.Left, Top = area.Top, Right = area.Right, Bottom = area.Bottom };
958
+ if (!MagSetWindowSource(magnifier, rc)) { broken = true; Why = "source"; return null; }
959
+ var result = frame;
960
+ frame = null;
961
+ if (result == null || result.Width != area.Width || result.Height != area.Height)
962
+ {
963
+ Why = result == null ? "no frame" : "size " + result.Width + "x" + result.Height;
964
+ if (result != null) result.Dispose();
965
+ broken = true;
966
+ return null;
967
+ }
968
+ return result;
969
+ }
970
+ catch (Exception e) { broken = true; Why = "capture: " + e.Message; return null; }
971
+ }
972
+
973
+ static bool OnImage(IntPtr hwnd, IntPtr srcdata, MAGIMAGEHEADER h, IntPtr destdata, MAGIMAGEHEADER dh, Native.RECT unclipped, Native.RECT clipped, IntPtr dirty)
974
+ {
975
+ try
976
+ {
977
+ var bmp = new Bitmap((int)h.width, (int)h.height, PixelFormat.Format32bppRgb);
978
+ var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
979
+ try
980
+ {
981
+ int rowBytes = (int)h.width * 4;
982
+ var row = new byte[rowBytes];
983
+ for (int y = 0; y < h.height; y++)
984
+ {
985
+ Marshal.Copy(new IntPtr(srcdata.ToInt64() + h.offset + (long)y * h.stride), row, 0, rowBytes);
986
+ Marshal.Copy(row, 0, new IntPtr(data.Scan0.ToInt64() + (long)y * data.Stride), rowBytes);
987
+ }
988
+ }
989
+ finally { bmp.UnlockBits(data); }
990
+ frame = bmp;
991
+ }
992
+ catch (Exception) { frame = null; }
993
+ return true;
994
+ }
995
+ }
996
+
866
997
  /** While the agent moves, clicks or scrolls, the card lets the mouse through. */
867
998
  internal sealed class PassThrough : IDisposable
868
999
  {
package/lib/index.js CHANGED
@@ -559,32 +559,24 @@ var Computer = class Computer {
559
559
  await this.refreshDisplays();
560
560
  const display = await this.display();
561
561
  const size = this.shotSize(display);
562
- const shot = await this.capture(display, size);
563
- await this.helper.call("shot_mark", this.rect(display)).catch(() => {});
562
+ const shot = await this.capture(display, size, { mark: true });
564
563
  await this.remember();
565
564
  return shot;
566
565
  }
567
- rect(display) {
568
- return {
569
- x: display.x,
570
- y: display.y,
571
- width: display.width,
572
- height: display.height
573
- };
574
- }
575
566
  /**
576
567
  * A fresh screenshot, or 'unchanged' when the screen looks the same as in
577
568
  * the last one the model saw (saves an image and tells it the action had no
578
569
  * visible effect).
579
570
  */
580
571
  async freshShot() {
572
+ await this.refreshDisplays();
581
573
  const display = await this.display();
582
- const result = await this.helper.call("shot_diff", this.rect(display)).catch(() => null);
583
- if (result && result.diff >= 0 && result.diff < UNCHANGED_DIFF) {
584
- await this.remember();
585
- return "unchanged";
586
- }
587
- return this.screenshot();
574
+ const shot = await this.capture(display, this.shotSize(display), {
575
+ compare: true,
576
+ threshold: UNCHANGED_DIFF
577
+ });
578
+ await this.remember();
579
+ return shot ?? "unchanged";
588
580
  }
589
581
  /** The foreground window as of the model's latest look at the screen. */
590
582
  seen;
@@ -607,7 +599,7 @@ var Computer = class Computer {
607
599
  const now = await this.foreground().catch(() => void 0);
608
600
  return now && now.hwnd !== seen.hwnd ? now : void 0;
609
601
  }
610
- async capture(rect, out) {
602
+ async capture(rect, out, options = {}) {
611
603
  const result = await this.helper.call("screenshot", {
612
604
  x: rect.x,
613
605
  y: rect.y,
@@ -615,8 +607,10 @@ var Computer = class Computer {
615
607
  height: rect.height,
616
608
  outWidth: out.width,
617
609
  outHeight: out.height,
618
- quality: this.settings().jpegQuality
610
+ quality: this.settings().jpegQuality,
611
+ ...options
619
612
  }, 3e4);
613
+ if (result.unchanged || result.data === void 0) return void 0;
620
614
  return {
621
615
  data: Buffer.from(result.data, "base64"),
622
616
  width: result.width,
@@ -632,7 +626,7 @@ var Computer = class Computer {
632
626
  maxLongEdge: s.maxLongEdge,
633
627
  maxPixels: s.maxPixels
634
628
  });
635
- return this.capture(rect, out);
629
+ return await this.capture(rect, out);
636
630
  }
637
631
  async foreground() {
638
632
  const win = await this.helper.call("foreground");
@@ -89,7 +89,6 @@ export declare class Computer {
89
89
  offscreen: boolean;
90
90
  }>;
91
91
  screenshot(): Promise<Shot>;
92
- private rect;
93
92
  /**
94
93
  * A fresh screenshot, or 'unchanged' when the screen looks the same as in
95
94
  * the last one the model saw (saves an image and tells it the action had no
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@copylee/dsh-computer-use",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "DeepSeek Harness Computer Use 插件(Windows):让 AI 看屏幕、点鼠标、敲键盘操控电脑;控制时屏幕泛起橙色光晕并显示“DeepSeek Harness 正在操控你的电脑”,按 Esc 暂停 / 继续。",
5
5
  "type": "module",
6
6
  "author": "copylee",