@namzu/computer-use 1.4.2 → 2.0.0

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.
Files changed (74) hide show
  1. package/CHANGELOG.md +65 -0
  2. package/README.md +96 -5
  3. package/dist/SubprocessComputerUseHost.d.ts +45 -5
  4. package/dist/SubprocessComputerUseHost.d.ts.map +1 -1
  5. package/dist/SubprocessComputerUseHost.js +94 -8
  6. package/dist/SubprocessComputerUseHost.js.map +1 -1
  7. package/dist/adapters/cua-driver/adapter.d.ts +100 -0
  8. package/dist/adapters/cua-driver/adapter.d.ts.map +1 -0
  9. package/dist/adapters/cua-driver/adapter.js +459 -0
  10. package/dist/adapters/cua-driver/adapter.js.map +1 -0
  11. package/dist/adapters/cua-driver/client.d.ts +108 -0
  12. package/dist/adapters/cua-driver/client.d.ts.map +1 -0
  13. package/dist/adapters/cua-driver/client.js +316 -0
  14. package/dist/adapters/cua-driver/client.js.map +1 -0
  15. package/dist/adapters/cua-driver/keys.d.ts +24 -0
  16. package/dist/adapters/cua-driver/keys.d.ts.map +1 -0
  17. package/dist/adapters/cua-driver/keys.js +106 -0
  18. package/dist/adapters/cua-driver/keys.js.map +1 -0
  19. package/dist/adapters/cua-driver/provision.d.ts +80 -0
  20. package/dist/adapters/cua-driver/provision.d.ts.map +1 -0
  21. package/dist/adapters/cua-driver/provision.js +147 -0
  22. package/dist/adapters/cua-driver/provision.js.map +1 -0
  23. package/dist/adapters/cua-driver/ui-tree.d.ts +42 -0
  24. package/dist/adapters/cua-driver/ui-tree.d.ts.map +1 -0
  25. package/dist/adapters/cua-driver/ui-tree.js +187 -0
  26. package/dist/adapters/cua-driver/ui-tree.js.map +1 -0
  27. package/dist/adapters/cua-driver/zip.d.ts +6 -0
  28. package/dist/adapters/cua-driver/zip.d.ts.map +1 -0
  29. package/dist/adapters/cua-driver/zip.js +86 -0
  30. package/dist/adapters/cua-driver/zip.js.map +1 -0
  31. package/dist/adapters/darwin.d.ts +23 -0
  32. package/dist/adapters/darwin.d.ts.map +1 -1
  33. package/dist/adapters/darwin.js +96 -31
  34. package/dist/adapters/darwin.js.map +1 -1
  35. package/dist/adapters/linux-wayland.d.ts.map +1 -1
  36. package/dist/adapters/linux-wayland.js +12 -0
  37. package/dist/adapters/linux-wayland.js.map +1 -1
  38. package/dist/adapters/linux-x11.d.ts.map +1 -1
  39. package/dist/adapters/linux-x11.js +12 -0
  40. package/dist/adapters/linux-x11.js.map +1 -1
  41. package/dist/adapters/types.d.ts +23 -5
  42. package/dist/adapters/types.d.ts.map +1 -1
  43. package/dist/adapters/types.js.map +1 -1
  44. package/dist/adapters/win32-powershell.d.ts +41 -0
  45. package/dist/adapters/win32-powershell.d.ts.map +1 -0
  46. package/dist/adapters/win32-powershell.js +407 -0
  47. package/dist/adapters/win32-powershell.js.map +1 -0
  48. package/dist/adapters/win32.d.ts +39 -11
  49. package/dist/adapters/win32.d.ts.map +1 -1
  50. package/dist/adapters/win32.js +72 -311
  51. package/dist/adapters/win32.js.map +1 -1
  52. package/dist/adapters/wsl.d.ts +46 -0
  53. package/dist/adapters/wsl.d.ts.map +1 -0
  54. package/dist/adapters/wsl.js +130 -0
  55. package/dist/adapters/wsl.js.map +1 -0
  56. package/dist/index.d.ts +2 -1
  57. package/dist/index.d.ts.map +1 -1
  58. package/dist/index.js.map +1 -1
  59. package/package.json +3 -3
  60. package/src/SubprocessComputerUseHost.ts +115 -8
  61. package/src/adapters/cua-driver/adapter.ts +571 -0
  62. package/src/adapters/cua-driver/client.ts +451 -0
  63. package/src/adapters/cua-driver/keys.ts +111 -0
  64. package/src/adapters/cua-driver/provision.ts +208 -0
  65. package/src/adapters/cua-driver/ui-tree.ts +234 -0
  66. package/src/adapters/cua-driver/zip.ts +90 -0
  67. package/src/adapters/darwin.ts +124 -38
  68. package/src/adapters/linux-wayland.ts +13 -0
  69. package/src/adapters/linux-x11.ts +13 -0
  70. package/src/adapters/types.ts +29 -4
  71. package/src/adapters/win32-powershell.ts +482 -0
  72. package/src/adapters/win32.ts +112 -356
  73. package/src/adapters/wsl.ts +150 -0
  74. package/src/index.ts +5 -1
@@ -7,7 +7,9 @@ import type {
7
7
  ComputerUseCapabilities,
8
8
  ComputerUseResult,
9
9
  DisplayGeometry,
10
+ DisplayInfo,
10
11
  Point,
12
+ ScreenshotResult,
11
13
  } from '@namzu/sdk'
12
14
  import { hasExecutable, runCommand, runCommandOrThrow } from '../util/spawn.js'
13
15
  import { ActionCapabilityError, type Adapter, AdapterUnavailableError } from './types.js'
@@ -123,9 +125,25 @@ async function probe(): Promise<ProbeResult> {
123
125
  // Adapter
124
126
  // ---------------------------------------------------------------------------
125
127
 
128
+ /**
129
+ * Units: the host contract is physical pixels. `screencapture` writes the
130
+ * backing store (2880x1800 on a 1440x900-point Retina panel), while
131
+ * `cliclick` and System Events work in points. The conversion happens here
132
+ * and only here: every point going in is divided by pixels-per-point and
133
+ * every point coming out is multiplied by it, once.
134
+ *
135
+ * Pixels-per-point is learned from a capture (its width over the display's
136
+ * width in points), so it is the backing store's own ratio whatever "Looks
137
+ * like" resolution is chosen; `system_profiler`'s panel pixels are not the
138
+ * backing store in a scaled mode. Before the first capture the adapter uses
139
+ * `system_profiler`'s ratio, read once.
140
+ */
126
141
  export class DarwinAdapter implements Adapter {
127
142
  readonly capabilities: ComputerUseCapabilities
128
143
  private readonly hasCliclick: boolean
144
+ private geometry: Promise<MainDisplay> | undefined
145
+ /** Captured pixels per point on the main display, from the latest capture. */
146
+ private pixelsPerPoint: number | undefined
129
147
 
130
148
  private constructor(probeResult: ProbeResult) {
131
149
  this.hasCliclick = probeResult.hasCliclick
@@ -166,27 +184,41 @@ export class DarwinAdapter implements Adapter {
166
184
  return new DarwinAdapter(probeResult)
167
185
  }
168
186
 
187
+ /** The main display in physical pixels (the capture's), with pixels per point. */
169
188
  async getDisplayGeometry(): Promise<DisplayGeometry> {
170
- const result = await runCommandOrThrow('system_profiler', ['-json', 'SPDisplaysDataType'])
171
- const parsed = JSON.parse(result.stdout.toString('utf8')) as {
172
- SPDisplaysDataType?: readonly {
173
- spdisplays_ndrvs?: readonly {
174
- _spdisplays_resolution?: string
175
- _spdisplays_pixels?: string
176
- }[]
177
- }[]
189
+ const { logical } = await this.readGeometry()
190
+ const scaleFactor = await this.scale()
191
+ return {
192
+ width: Math.round(logical.width * scaleFactor),
193
+ height: Math.round(logical.height * scaleFactor),
194
+ scaleFactor,
178
195
  }
179
- const primary = parsed.SPDisplaysDataType?.[0]?.spdisplays_ndrvs?.[0]
180
- if (!primary) {
181
- throw new Error('DarwinAdapter: could not parse display geometry from system_profiler')
182
- }
183
- const physical = parsePixelDims(primary._spdisplays_pixels ?? primary._spdisplays_resolution)
184
- const logical = parsePixelDims(primary._spdisplays_resolution)
185
- if (!physical || !logical) {
186
- throw new Error('DarwinAdapter: missing pixel/resolution fields in system_profiler output')
196
+ }
197
+
198
+ /** The main display in pixels and in points, read once; a failed read is retried next time. */
199
+ private readGeometry(): Promise<MainDisplay> {
200
+ if (!this.geometry) {
201
+ this.geometry = readMainDisplay().catch((error: unknown) => {
202
+ this.geometry = undefined
203
+ throw error
204
+ })
187
205
  }
188
- const scaleFactor = physical.width > 0 ? physical.width / logical.width : 1
189
- return { width: logical.width, height: logical.height, scaleFactor }
206
+ return this.geometry
207
+ }
208
+
209
+ private async scale(): Promise<number> {
210
+ if (this.pixelsPerPoint !== undefined) return this.pixelsPerPoint
211
+ return (await this.readGeometry()).scaleFactor
212
+ }
213
+
214
+ private async toPoints(pixels: Point): Promise<Point> {
215
+ const scale = await this.scale()
216
+ return { x: Math.round(pixels.x / scale), y: Math.round(pixels.y / scale) }
217
+ }
218
+
219
+ private async toPixels(points: Point): Promise<Point> {
220
+ const scale = await this.scale()
221
+ return { x: Math.round(points.x * scale), y: Math.round(points.y * scale) }
190
222
  }
191
223
 
192
224
  async execute(action: ComputerUseAction): Promise<ComputerUseResult> {
@@ -194,7 +226,7 @@ export class DarwinAdapter implements Adapter {
194
226
  case 'screenshot':
195
227
  return { type: 'screenshot', result: await this.screenshot() }
196
228
  case 'cursor_position':
197
- return { type: 'cursor_position', point: await this.cursorPosition() }
229
+ return { type: 'cursor_position', point: await this.toPixels(await this.cursorPosition()) }
198
230
  case 'mouse_move':
199
231
  await this.mouseMove(action.to)
200
232
  return { type: 'ok' }
@@ -227,25 +259,36 @@ export class DarwinAdapter implements Adapter {
227
259
 
228
260
  // --- screenshot ---------------------------------------------------------
229
261
 
230
- private async screenshot() {
262
+ private async screenshot(): Promise<ScreenshotResult> {
231
263
  const tmpPath = join(tmpdir(), `namzu-cu-${randomUUID()}.png`)
264
+ let data: Buffer
232
265
  try {
233
- await runCommandOrThrow('screencapture', ['-t', 'png', '-x', tmpPath])
234
- const data = await readFile(tmpPath)
235
- const dims = decodePngDims(data)
236
- return {
237
- data,
238
- mimeType: 'image/png' as const,
239
- width: dims.width,
240
- height: dims.height,
241
- }
266
+ // `-m`: the main display only, the one every input tool here addresses.
267
+ await runCommandOrThrow('screencapture', ['-t', 'png', '-x', '-m', tmpPath])
268
+ data = await readFile(tmpPath)
242
269
  } finally {
243
270
  await unlink(tmpPath).catch(() => undefined)
244
271
  }
272
+ const dims = decodePngDims(data)
273
+ const main = await this.readGeometry().catch(() => undefined)
274
+ const ratio = main && main.logical.width > 0 ? dims.width / main.logical.width : 1
275
+ const scaleFactor = Number.isFinite(ratio) && ratio > 0 ? ratio : 1
276
+ if (main) this.pixelsPerPoint = scaleFactor
277
+ const display: DisplayInfo = {
278
+ id: 'main',
279
+ x: 0,
280
+ y: 0,
281
+ width: dims.width,
282
+ height: dims.height,
283
+ scaleFactor,
284
+ primary: true,
285
+ }
286
+ return { data, mimeType: 'image/png', width: dims.width, height: dims.height, display }
245
287
  }
246
288
 
247
289
  // --- cursor position (requires cliclick) --------------------------------
248
290
 
291
+ /** The cursor in points, as cliclick reports it; {@link execute} converts it. */
249
292
  private async cursorPosition(): Promise<Point> {
250
293
  if (!this.hasCliclick) {
251
294
  throw new ActionCapabilityError(
@@ -269,8 +312,12 @@ export class DarwinAdapter implements Adapter {
269
312
 
270
313
  // --- mouse --------------------------------------------------------------
271
314
 
272
- private async mouseMove(to: Point) {
315
+ // Each takes physical pixels and converts to points after its refusals,
316
+ // so a refused gesture never costs a display read.
317
+
318
+ private async mouseMove(pixels: Point) {
273
319
  if (this.hasCliclick) {
320
+ const to = await this.toPoints(pixels)
274
321
  await runCommandOrThrow('cliclick', [`m:${to.x},${to.y}`])
275
322
  return
276
323
  }
@@ -281,30 +328,31 @@ export class DarwinAdapter implements Adapter {
281
328
  )
282
329
  }
283
330
 
284
- private async mouseClick(at: Point, button: 'left' | 'right' | 'middle') {
331
+ private async mouseClick(pixels: Point, button: 'left' | 'right' | 'middle') {
285
332
  if (button === 'middle')
286
333
  throw new ActionCapabilityError(
287
334
  'mouse_click',
288
335
  'mouse',
289
336
  'middle-click is not supported by this macOS adapter',
290
337
  )
291
- if (this.hasCliclick) {
292
- const prefix = button === 'right' ? 'rc' : 'c'
293
- await runCommandOrThrow('cliclick', [`${prefix}:${at.x},${at.y}`])
294
- return
295
- }
296
- if (button !== 'left') {
338
+ if (!this.hasCliclick && button !== 'left') {
297
339
  throw new ActionCapabilityError(
298
340
  'mouse_click',
299
341
  'mouse',
300
342
  `${button}-click requires \`cliclick\` on macOS (osascript only supports left-click)`,
301
343
  )
302
344
  }
345
+ const at = await this.toPoints(pixels)
346
+ if (this.hasCliclick) {
347
+ const prefix = button === 'right' ? 'rc' : 'c'
348
+ await runCommandOrThrow('cliclick', [`${prefix}:${at.x},${at.y}`])
349
+ return
350
+ }
303
351
  const script = `tell application "System Events" to click at {${at.x}, ${at.y}}`
304
352
  await runCommandOrThrow('osascript', ['-e', script])
305
353
  }
306
354
 
307
- private async mouseDrag(from: Point, to: Point) {
355
+ private async mouseDrag(fromPixels: Point, toPixels: Point) {
308
356
  if (!this.hasCliclick) {
309
357
  throw new ActionCapabilityError(
310
358
  'mouse_drag',
@@ -312,6 +360,8 @@ export class DarwinAdapter implements Adapter {
312
360
  'install `cliclick` (brew install cliclick) to enable drag on macOS',
313
361
  )
314
362
  }
363
+ const from = await this.toPoints(fromPixels)
364
+ const to = await this.toPoints(toPixels)
315
365
  await runCommandOrThrow('cliclick', [`dd:${from.x},${from.y}`, `du:${to.x},${to.y}`])
316
366
  }
317
367
 
@@ -347,6 +397,42 @@ export class DarwinAdapter implements Adapter {
347
397
  // Helpers
348
398
  // ---------------------------------------------------------------------------
349
399
 
400
+ interface Size {
401
+ readonly width: number
402
+ readonly height: number
403
+ }
404
+
405
+ /** The main display as `system_profiler` describes it: panel pixels, points, and their ratio. */
406
+ interface MainDisplay {
407
+ readonly physical: Size
408
+ readonly logical: Size
409
+ readonly scaleFactor: number
410
+ }
411
+
412
+ /** The main display from `system_profiler`: its pixels, its points, and their ratio. */
413
+ async function readMainDisplay(): Promise<MainDisplay> {
414
+ const result = await runCommandOrThrow('system_profiler', ['-json', 'SPDisplaysDataType'])
415
+ const parsed = JSON.parse(result.stdout.toString('utf8')) as {
416
+ SPDisplaysDataType?: readonly {
417
+ spdisplays_ndrvs?: readonly {
418
+ _spdisplays_resolution?: string
419
+ _spdisplays_pixels?: string
420
+ }[]
421
+ }[]
422
+ }
423
+ const primary = parsed.SPDisplaysDataType?.[0]?.spdisplays_ndrvs?.[0]
424
+ if (!primary) {
425
+ throw new Error('DarwinAdapter: could not parse display geometry from system_profiler')
426
+ }
427
+ const physical = parsePixelDims(primary._spdisplays_pixels ?? primary._spdisplays_resolution)
428
+ const logical = parsePixelDims(primary._spdisplays_resolution)
429
+ if (!physical || !logical) {
430
+ throw new Error('DarwinAdapter: missing pixel/resolution fields in system_profiler output')
431
+ }
432
+ const ratio = logical.width > 0 ? physical.width / logical.width : 1
433
+ return { physical, logical, scaleFactor: Number.isFinite(ratio) && ratio > 0 ? ratio : 1 }
434
+ }
435
+
350
436
  function parsePixelDims(value: string | undefined): { width: number; height: number } | null {
351
437
  if (!value) return null
352
438
  // system_profiler emits e.g. "2560 x 1600" or "2560 x 1600 @ 60.00Hz"
@@ -3,6 +3,7 @@ import type {
3
3
  ComputerUseCapabilities,
4
4
  ComputerUseResult,
5
5
  DisplayGeometry,
6
+ DisplayInfo,
6
7
  MouseButton,
7
8
  ScrollDirection,
8
9
  } from '@namzu/sdk'
@@ -206,11 +207,23 @@ export class LinuxWaylandAdapter implements Adapter {
206
207
  private async screenshot() {
207
208
  const result = await runCommandOrThrow('grim', ['-t', 'png', '-'])
208
209
  const dims = decodePngDims(result.stdout)
210
+ // grim composites every output into one image at the layout's origin.
211
+ // The compositor's per-output scale is not read here; 1 is reported.
212
+ const display: DisplayInfo = {
213
+ id: 'outputs',
214
+ x: 0,
215
+ y: 0,
216
+ width: dims.width,
217
+ height: dims.height,
218
+ scaleFactor: 1,
219
+ primary: true,
220
+ }
209
221
  return {
210
222
  data: result.stdout,
211
223
  mimeType: 'image/png' as const,
212
224
  width: dims.width,
213
225
  height: dims.height,
226
+ display,
214
227
  }
215
228
  }
216
229
 
@@ -3,6 +3,7 @@ import type {
3
3
  ComputerUseCapabilities,
4
4
  ComputerUseResult,
5
5
  DisplayGeometry,
6
+ DisplayInfo,
6
7
  MouseButton,
7
8
  Point,
8
9
  ScrollDirection,
@@ -133,11 +134,23 @@ export class LinuxX11Adapter implements Adapter {
133
134
  private async screenshot() {
134
135
  const result = await runCommandOrThrow('maim', ['-f', 'png'])
135
136
  const dims = decodePngDims(result.stdout)
137
+ // maim captures the whole X screen — every monitor — whose origin is
138
+ // (0, 0) and whose pixels are the ones xdotool moves through.
139
+ const display: DisplayInfo = {
140
+ id: 'screen',
141
+ x: 0,
142
+ y: 0,
143
+ width: dims.width,
144
+ height: dims.height,
145
+ scaleFactor: await this.detectScaleFactor(),
146
+ primary: true,
147
+ }
136
148
  return {
137
149
  data: result.stdout,
138
150
  mimeType: 'image/png' as const,
139
151
  width: dims.width,
140
152
  height: dims.height,
153
+ display,
141
154
  }
142
155
  }
143
156
 
@@ -3,21 +3,46 @@ import type {
3
3
  ComputerUseCapabilities,
4
4
  ComputerUseResult,
5
5
  DisplayGeometry,
6
+ FocusWindowResult,
7
+ Rect,
8
+ ScreenshotResult,
9
+ UiActResult,
10
+ UiElementAction,
11
+ UiSnapshot,
12
+ WindowInfo,
6
13
  } from '@namzu/sdk'
7
14
 
8
15
  /**
9
16
  * Platform-specific execution surface behind `SubprocessComputerUseHost`.
10
17
  * Each adapter owns its CLI invocations, capability probe, and error mapping.
11
18
  *
12
- * Implementations must be stateless between calls (the subprocess model has
13
- * no persistent resources to manage). `capabilities` is frozen at probe time
14
- * and reflects what actually works on the host, not what the action union
15
- * permits.
19
+ * `capabilities` is frozen at probe time and reflects what actually works on
20
+ * the host, not what the action union permits. An adapter that keeps a
21
+ * process between calls (the Windows cua-driver backend does) releases it in
22
+ * `dispose`.
23
+ *
24
+ * Units follow the SDK's host contract: physical pixels, action points
25
+ * relative to the display of the last capture.
16
26
  */
17
27
  export interface Adapter {
18
28
  readonly capabilities: ComputerUseCapabilities
29
+ /** What drives the desktop, for diagnosis: `cua-driver 0.28.2`, `powershell`. */
30
+ readonly backend?: string
31
+ /** Why the preferred backend is not the one in use, when the adapter fell back. */
32
+ readonly fallbackReason?: string
19
33
  getDisplayGeometry(): Promise<DisplayGeometry>
20
34
  execute(action: ComputerUseAction): Promise<ComputerUseResult>
35
+ /** With `capabilities.windows`. */
36
+ listWindows?(): Promise<readonly WindowInfo[]>
37
+ /** With `capabilities.windows`. */
38
+ focusWindow?(id: string): Promise<FocusWindowResult>
39
+ /** With `capabilities.regionCapture`. */
40
+ captureRegion?(rect: Rect): Promise<ScreenshotResult>
41
+ /** With `capabilities.uiTree`. */
42
+ uiSnapshot?(windowId?: string): Promise<UiSnapshot>
43
+ /** With `capabilities.uiTree`. */
44
+ uiAct?(ref: string, action: UiElementAction, value?: string): Promise<UiActResult>
45
+ dispose?(): Promise<void>
21
46
  }
22
47
 
23
48
  /**