dsh-codex-style-computer-use 2.3.2 → 2.3.3

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.
@@ -2,6 +2,6 @@
2
2
  "format": 1,
3
3
  "platform": "win32",
4
4
  "architecture": "x64",
5
- "sourceSha256": "ebbf272345b97a9f1131222da00bab2a2b657a2907da7dea406fe895b98d6057",
6
- "binarySha256": "4d849ce169734ab03aee4ee3ee7b5991cb49c34545274bb4a00105b6bed3a153"
5
+ "sourceSha256": "c1c90d8a4ed05bbaae388723ec3b8a50f6087250552d9c8c1b460e09f32e9b59",
6
+ "binarySha256": "924452ec3269323128d9d02c65bc40d75df2c0d9fe7d50272e928cbf0deb3984"
7
7
  }
@@ -4517,7 +4517,7 @@ namespace DshComputerUse.Worker
4517
4517
  if (GetB(a, "clamp", true))
4518
4518
  {
4519
4519
  int x2 = Math.Max(x, vx), y2 = Math.Max(y, vy);
4520
- int x3 = Math.Min(x + w, vx + vw), y3 = Math.Min(y + h, vy + vh);
4520
+ int x3 = (int)Math.Min((long)x + w, (long)vx + vw), y3 = (int)Math.Min((long)y + h, (long)vy + vh);
4521
4521
  if (x3 <= x2 || y3 <= y2) throw new Exception("region outside virtual desktop");
4522
4522
  x = x2; y = y2; w = x3 - x2; h = y3 - y2;
4523
4523
  }
@@ -4549,7 +4549,7 @@ namespace DshComputerUse.Worker
4549
4549
  if (mem != IntPtr.Zero) Native.DeleteDC(mem);
4550
4550
  if (screen != IntPtr.Zero) Native.ReleaseDC(IntPtr.Zero, screen);
4551
4551
  }
4552
- return RecognizePixels(bgra, x, y, w, h);
4552
+ return RecognizePixels(bgra, x, y, w, h, GetI(a, "scale", 0));
4553
4553
  }
4554
4554
 
4555
4555
  static Dictionary<string, object> WindowOcr(Dictionary<string, object> args, IntPtr window)
@@ -4561,8 +4561,13 @@ namespace DshComputerUse.Worker
4561
4561
  RECT r = frames.Bounds;
4562
4562
  int x = GetI(args, "x", r.Left), y = GetI(args, "y", r.Top);
4563
4563
  int w = GetI(args, "width", r.Right - r.Left), h = GetI(args, "height", r.Bottom - r.Top);
4564
- if (w <= 0 || h <= 0 || x < r.Left || y < r.Top || (long)x+w > r.Right || (long)y+h > r.Bottom ||
4565
- (long)w*h > 16L*1024*1024) throw new Exception("OCR region must fit inside the window surface and within 16M pixels");
4564
+ int[] requested = { x, y, w, h };
4565
+ int left = Math.Max(x, r.Left), top = Math.Max(y, r.Top);
4566
+ long right = Math.Min((long)x + w, r.Right), bottom = Math.Min((long)y + h, r.Bottom);
4567
+ if (w <= 0 || h <= 0 || right <= left || bottom <= top)
4568
+ throw new Exception("OCR region outside window surface " + String.Join(",", new int[] { r.Left, r.Top, r.Right-r.Left, r.Bottom-r.Top }));
4569
+ x = left; y = top; w = (int)(right-left); h = (int)(bottom-top);
4570
+ if ((long)w*h > 16L*1024*1024) throw new Exception("OCR region exceeds 16M pixels; crop tighter");
4566
4571
  frames.Sample();
4567
4572
  byte[] pixels = frames.Pixels;
4568
4573
  if (x != r.Left || y != r.Top || w != r.Right-r.Left || h != r.Bottom-r.Top) {
@@ -4570,19 +4575,48 @@ namespace DshComputerUse.Worker
4570
4575
  for (int i = 0; i < h; i++) Buffer.BlockCopy(frames.Pixels,
4571
4576
  ((y-r.Top+i)*(r.Right-r.Left)+x-r.Left)*4, pixels, i*w*4, w*4);
4572
4577
  }
4573
- var result = RecognizePixels(pixels, x, y, w, h);
4578
+ var result = RecognizePixels(pixels, x, y, w, h, GetI(args, "scale", 0));
4574
4579
  ValidateCaptureTarget(args, window);
4575
4580
  if (ScreenshotGeometry(window, out bounds) != geometry) throw new Exception("OCR_TARGET_CHANGED: observe this window again");
4581
+ if (requested[0] != x || requested[1] != y || requested[2] != w || requested[3] != h)
4582
+ result["requestedRegion"] = requested;
4576
4583
  result["hwnd"] = window.ToInt64(); result["captureBackend"] = "windows-graphics-capture";
4577
4584
  return result;
4578
4585
  }
4579
4586
  }
4580
4587
 
4581
- static Dictionary<string, object> RecognizePixels(byte[] bgra, int x, int y, int w, int h)
4588
+ static byte[] EnlargeOcrPixels(byte[] pixels, int width, int height)
4582
4589
  {
4590
+ // Preserve GDI screen captures whose unused alpha byte may be zero.
4591
+ var pin = GCHandle.Alloc(pixels, GCHandleType.Pinned);
4592
+ try {
4593
+ using (var source = new Bitmap(width, height, width*4, PixelFormat.Format32bppRgb, pin.AddrOfPinnedObject()))
4594
+ using (var target = new Bitmap(width*2, height*2, PixelFormat.Format32bppArgb)) {
4595
+ using (var g = Graphics.FromImage(target)) {
4596
+ g.InterpolationMode = InterpolationMode.HighQualityBicubic;
4597
+ g.PixelOffsetMode = PixelOffsetMode.Half;
4598
+ g.DrawImage(source, new Rectangle(0, 0, target.Width, target.Height));
4599
+ }
4600
+ var bits = target.LockBits(new Rectangle(0, 0, target.Width, target.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
4601
+ try {
4602
+ byte[] data = new byte[target.Width*target.Height*4];
4603
+ Marshal.Copy(bits.Scan0, data, 0, data.Length);
4604
+ return data;
4605
+ } finally { target.UnlockBits(bits); }
4606
+ }
4607
+ } finally { pin.Free(); }
4608
+ }
4609
+
4610
+ static Dictionary<string, object> RecognizePixels(byte[] bgra, int x, int y, int w, int h, int requestedScale)
4611
+ {
4612
+ bool fits = (long)w*h <= 1024*1024 && Math.Max(w,h)*2L <= OcrEngine.MaxImageDimension;
4613
+ if (requestedScale < 0 || requestedScale > 2 || (requestedScale == 2 && !fits))
4614
+ throw new Exception("OCR scale 2 requires a crop within 1M pixels and the Windows OCR dimension limit");
4615
+ int scale = requestedScale == 0 ? (fits ? 2 : 1) : requestedScale;
4616
+ if (scale == 2) bgra = EnlargeOcrPixels(bgra, w, h);
4583
4617
  OcrResult result;
4584
4618
  using (SoftwareBitmap bitmap = SoftwareBitmap.CreateCopyFromBuffer(
4585
- WindowsRuntimeBufferExtensions.AsBuffer(bgra), BitmapPixelFormat.Bgra8, w, h))
4619
+ WindowsRuntimeBufferExtensions.AsBuffer(bgra), BitmapPixelFormat.Bgra8, w*scale, h*scale))
4586
4620
  result = _ocrEngine.RecognizeAsync(bitmap).AsTask().GetAwaiter().GetResult();
4587
4621
  List<object> lines = new List<object>();
4588
4622
  StringBuilder joined = new StringBuilder();
@@ -4591,15 +4625,17 @@ namespace DshComputerUse.Worker
4591
4625
  List<object> words = new List<object>();
4592
4626
  foreach (OcrWord word in line.Words)
4593
4627
  {
4594
- words.Add(Dict("text", word.Text,
4595
- "x", x + (int)word.BoundingRect.X, "y", y + (int)word.BoundingRect.Y,
4596
- "w", (int)word.BoundingRect.Width, "h", (int)word.BoundingRect.Height));
4628
+ var box = word.BoundingRect;
4629
+ int left = (int)(box.X/scale), top = (int)(box.Y/scale);
4630
+ words.Add(Dict("text", word.Text, "x", x+left, "y", y+top,
4631
+ "w", (int)Math.Ceiling((box.X+box.Width)/scale)-left,
4632
+ "h", (int)Math.Ceiling((box.Y+box.Height)/scale)-top));
4597
4633
  }
4598
4634
  lines.Add(Dict("text", line.Text, "words", words));
4599
4635
  if (joined.Length > 0) joined.Append('\n');
4600
4636
  joined.Append(line.Text);
4601
4637
  }
4602
- return Dict("lines", lines, "text", joined.ToString(), "language", _ocrLanguage,
4638
+ return Dict("lines", lines, "text", joined.ToString(), "language", _ocrLanguage, "scale", scale,
4603
4639
  "region", new int[] { x, y, w, h });
4604
4640
  }
4605
4641
 
@@ -4839,7 +4875,7 @@ namespace DshComputerUse.Worker
4839
4875
  if (shotWindow == IntPtr.Zero && GetB(a, "clamp", true))
4840
4876
  {
4841
4877
  int x2 = Math.Max(x, vx), y2 = Math.Max(y, vy);
4842
- int x3 = Math.Min(x + w, vx + vw), y3 = Math.Min(y + h, vy + vh);
4878
+ int x3 = (int)Math.Min((long)x + w, (long)vx + vw), y3 = (int)Math.Min((long)y + h, (long)vy + vh);
4843
4879
  if (x3 <= x2 || y3 <= y2) throw new Exception("region outside virtual desktop");
4844
4880
  x = x2; y = y2; w = x3 - x2; h = y3 - y2;
4845
4881
  }
package/lib/tools.js CHANGED
@@ -70,6 +70,43 @@ const rect = (v) => {
70
70
  return null
71
71
  }
72
72
 
73
+ // Keep the recognizer's own text within each fragment (including RTL and CJK).
74
+ // Render separately located fragments together when they share a visual baseline.
75
+ function renderOcr(value) {
76
+ if (!value?.lines) return 'no text'
77
+ const head = `ocr ${value.language}${value.hwnd ? ' hwnd=' + value.hwnd : ''} region=${value.region || ''}${value.requestedRegion ? ' clippedFrom=' + value.requestedRegion : ''} scale=${value.scale || 1} @center(x,y)`
78
+ const parts = [], unplaced = []
79
+ for (const line of value.lines) {
80
+ const words = line.words || []
81
+ if (!words.length) { unplaced.push(line.text); continue }
82
+ let group = []
83
+ function flush() {
84
+ const left = Math.min(...group.map(w => w.x)), right = Math.max(...group.map(w => w.x + w.w))
85
+ const top = Math.min(...group.map(w => w.y)), bottom = Math.max(...group.map(w => w.y + w.h))
86
+ parts.push({ x: Math.floor((left+right)/2), y: Math.floor((top+bottom)/2), h: bottom-top,
87
+ text: group.length === words.length ? line.text : group.map(w => w.text).join(' ') })
88
+ group = []
89
+ }
90
+ for (const w of words) {
91
+ const prev = group.at(-1)
92
+ if (prev && Math.max(w.x-prev.x-prev.w, prev.x-w.x-w.w) > 2*Math.max(w.h,prev.h)) flush()
93
+ group.push(w)
94
+ }
95
+ flush()
96
+ }
97
+ const rows = []
98
+ for (const p of parts.sort((a,b) => a.y-b.y || a.x-b.x)) {
99
+ let row = rows.at(-1)
100
+ if (!row || p.y-row.y > Math.max(2, Math.min(row.h,p.h)*0.45)) {
101
+ row = { y:p.y, h:p.h, parts:[] }
102
+ rows.push(row)
103
+ }
104
+ row.parts.push(p)
105
+ }
106
+ const body = rows.map(row => row.parts.sort((a,b) => a.x-b.x).map(p => `@${p.x},${p.y} ${p.text}`).join(' | '))
107
+ return [head, ...body, ...unplaced].join('\n')
108
+ }
109
+
73
110
  export function registerTools ({ ctx, cu, config, dataDir, log, snapshotPath, defineTool, hooks, ensureCycle, register }) {
74
111
  const observations = new ObservationStore({ archiveDir: typeof dataDir?.root === 'string' ? join(dataDir.root, 'observation-cache') : undefined })
75
112
  let observationEpoch
@@ -263,9 +300,11 @@ export function registerTools ({ ctx, cu, config, dataDir, log, snapshotPath, de
263
300
  const readOcr = async args => {
264
301
  const hwnd = checkedWindow(args.hwnd)
265
302
  if (args.region !== undefined && String(args.region).trim().split(/[,\s]+/).length !== 4) throw Error('region must be "x,y,w,h"')
266
- const region = args.region === undefined ? undefined : rect(args.region)
303
+ const region = args.region === undefined ? undefined : rect(String(args.region).trim())
267
304
  if (args.region !== undefined && (!region || region.width <= 0 || region.height <= 0)) throw Error('region must be "x,y,w,h"')
268
- return cu.ocr(hwnd ? { ...region, hwnd } : region)
305
+ if (region && Object.values(region).some(n => !Number.isInteger(n) || n < -2147483648 || n > 2147483647)) throw Error('OCR region requires 32-bit integer screen coordinates and sizes')
306
+ if (args.scale !== undefined && ![1, 2].includes(args.scale)) throw Error('OCR scale must be 1 or 2')
307
+ return cu.ocr({ ...region, ...(hwnd ? { hwnd } : {}), ...(args.scale ? { scale: args.scale } : {}) })
269
308
  }
270
309
  const observeState = async (args, exec) => {
271
310
  if (args.appQuery !== undefined) {
@@ -559,20 +598,13 @@ export function registerTools ({ ctx, cu, config, dataDir, log, snapshotPath, de
559
598
 
560
599
  reg({
561
600
  name: 'computer_ocr',
562
- description: "Read TEXT with coordinates from a screen region (inbox Windows OCR). Returns lines/words with absolute-pixel boxes — locate controls BY NAME in zero-UIA apps instead of spending a screenshot; words' x,y are click-ready centers offset by w/2,h/2. Crop tight (16M px cap); zh/en mixed supported.",
601
+ description: "Read text as spatial rows with absolute screen centers. Start with hwnd alone; use a targeted crop only for ambiguous text. Partially overlapping window crops are clipped to its captured surface. No images.",
563
602
  parameters: {
564
603
  hwnd: windowParam,
565
- region: P('string', 'crop "x,y,w,h" (default: whole screen)'),
604
+ region: P('string', 'absolute x,y,w,h (default: target window or desktop)'),
605
+ scale: P('number', 'optional OCR magnification: 1 or 2; default doubles small regions'),
566
606
  },
567
- output: out((a, v) => {
568
- if (!v || !v.lines) return [{ type: 'text', text: 'no text' }]
569
- const head = `ocr ${v.language}${v.hwnd ? ' hwnd=' + v.hwnd : ''} region=${v.region ? v.region.join(',') : ''} lines=${v.lines.length}`
570
- const body = v.lines.map(l => {
571
- const w0 = l.words[0]
572
- return `${w0 ? `@${w0.x},${w0.y} ` : ''}${l.text}`
573
- }).join('\n')
574
- return [{ type: 'text', text: head + '\n' + body }]
575
- }),
607
+ output: out((_args, value) => [{ type: 'text', text: renderOcr(value) }]),
576
608
  isConcurrencySafe: () => false,
577
609
  timeoutMs: 30_000,
578
610
  execute: readOcr,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-codex-style-computer-use",
3
- "version": "2.3.2",
3
+ "version": "2.3.3",
4
4
  "description": "Codex-style Computer Use for DeepSeek Harness: Windows desktop control, screenshots, UI Automation, human input handoff, approval gates and prebuilt native workers. Install and update from dsh-market without PowerShell 7 or build steps.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -42,7 +42,7 @@ The plugin also has a deterministic backstop: after a non-desktop top-level tool
42
42
 
43
43
  On a multi-display desktop, keep the task in its existing work window. Inspect `computer_state {hwnd}` for the work area, owned popups and nine sampled pixel receivers. If a required popup opens on another screen, identify its owner and use `computer_window {op:"move", hwnd:popup, workHwnd:parent}`. Check `contained:true`, then activate the popup and observe it again. The same call leaves an already contained window in place. For a covered window, query its controls by hwnd. Pixel samples describe only the sampled positions.
44
44
 
45
- For a screenshot-bound click, the worker rechecks a known accessible control or the local pixels before pressing. On STALE_SCREENSHOT, observe again and select the current target. Window OCR accepts `hwnd` with an optional absolute crop inside the surface. Window-scoped pixel waits and OCR read the same surface as the screenshot, including behind other windows. For a targeted action, automatic feedback follows its window and active owned popup. Inspect the returned title and controls before choosing the next action.
45
+ For a screenshot-bound click, the worker rechecks a known accessible control or the local pixels before pressing. On STALE_SCREENSHOT, observe again and select the current target. Window OCR accepts `hwnd` alone or an absolute screen crop. A partially overlapping crop returns the intersection and identifies the requested region. Read the spatial rows and their absolute centers first. For ambiguous text, crop the relevant area or change `scale` between 1 and 2. Window-scoped pixel waits and OCR read the same surface as the screenshot, including behind other windows. For a targeted action, automatic feedback follows its window and active owned popup. Inspect the returned title and controls before choosing the next action.
46
46
 
47
47
  On Windows, `computer_shot {hwnd}` reads the selected window surface even under an opaque window. A background capture can be inspected. For input, bring the intended window forward and take a fresh shot with a screenshotId. Minimized or unavailable surfaces can return a capture error. Default screenshots wait for the observed area to go quiet. Repeated static views return sooner. If `settled:false` is returned, observe again before acting.
48
48