@namzu/computer-use 1.4.3 → 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 +36 -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 +2 -2
  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
package/CHANGELOG.md CHANGED
@@ -1,5 +1,41 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - 82769f1: On Windows and under WSL, `SubprocessComputerUseHost` now drives the desktop through cua-driver (MIT, github.com/trycua/cua) instead of starting `powershell.exe` for every action, and every adapter now takes and returns physical pixels. Both are changes a caller can see; read on before upgrading.
8
+
9
+ **What changes by default on Windows / WSL.** The first `initialize()` downloads cua-driver 0.28.2 for the machine's architecture (a 27–29 MB archive from the project's GitHub releases), checks the SHA-256 of the archive and of `cua-driver.exe` against values pinned in this package, and keeps only the executable in `<NAMZU_HOME>/computer-use/cua-driver/0.28.2/` (`~/.namzu` unless `NAMZU_HOME` is set). One `cua-driver.exe` process then runs until `host.dispose()`; call `dispose()` when you are done. It runs with its telemetry and release check off and without environment variables whose names look like secrets. If it cannot be downloaded, verified or started, the host falls back to PowerShell and says why in `host.fallbackReason`.
10
+
11
+ To keep the old behaviour — no download, no long-lived process — set `NAMZU_CUA_DRIVER=off` or pass `new SubprocessComputerUseHost({ windows: { backend: 'powershell' } })`. To use a cua-driver you installed yourself, set `NAMZU_CUA_DRIVER=<path to cua-driver.exe>` or `windows: { cuaDriverPath }`. `windows: { download: false }` uses only a cached or configured build.
12
+
13
+ **Physical pixels everywhere.** Points you pass to `execute()` and sizes you get back are pixels of the captured bitmap, as the SDK's host contract now states. This changes results only on scaled displays:
14
+
15
+ - macOS Retina: `getDisplayGeometry()` returns the capture's resolution (2880x1800, not 1440x900; 3360x2100 in a "Looks like 1680x1050" mode), `cursor_position` is in pixels, and click/move/drag points are pixels; the adapter divides by the capture's pixels per point for `cliclick` and System Events, once. Before, a click aimed at the middle of a Retina screenshot landed near the bottom-right corner. Screenshots show the main display only, the one the input tools address. If you converted screenshot pixels to points yourself, stop.
16
+ - Windows above 100 % scaling: both backends are DPI aware, so the capture is the whole display (it was the top-left part) and points are physical (they were DPI-virtualised).
17
+ - Every capture now carries `result.display` (`id`, origin, size, `scaleFactor`).
18
+
19
+ **Also new.** With the cua-driver backend, `capabilities.windows` is `true` and `host.listWindows()` / `host.focusWindow(id)` work (focus restores a minimised window and reports what is actually in front); `host.captureRegion()` exists and throws where the adapter has no region capture. `host.backend` names the backend in use. Measured on a 3440x1440 display: a click went from 0.68–0.83 s to 0.13 s, a screenshot from 0.40 s to 0.08–0.13 s.
20
+
21
+ **Smaller differences on the cua-driver backend.** `capabilities.clipboard` is `false` (no clipboard action was ever offered); `win`/`super` in a key chord press the Windows key (PowerShell's SendKeys pressed Ctrl); a single punctuation key such as `/` is typed as text so the keyboard layout cannot change it. Only the primary display is captured, as before. The PowerShell fallback now types text through Unicode key events instead of SendKeys, so `+ ^ % ~ ( ) { }` and non-Latin text arrive as written.
22
+
23
+ ### Minor Changes
24
+
25
+ - 82769f1: `computer_use` can read a window's controls and act on them by reference, on a host that declares `uiTree` — which the Windows backend of `@namzu/computer-use` now does.
26
+
27
+ **What changes for a caller of the tool**
28
+
29
+ - Two new actions when the host declares `uiTree` and implements `uiSnapshot` and `uiAct`: `ui_snapshot { window_id? }` returns the window's accessibility tree as text, each control that can be acted on with a ref such as `e12`; `ui_act { ref, action, value? }` performs `invoke`, `set_value`, `toggle`, `select`, `expand`, `collapse`, `focus` or `scroll_into_view` on it and returns a screenshot afterwards. `ui_act` may be in a batch; `ui_snapshot` may not. Refs count up across snapshots and only the latest snapshot's are accepted, so a stale ref is refused rather than acting on another control. `ActionInput` gains both, so code that switches exhaustively over `input.type` needs two more cases — that is why this is a major release for `@namzu/sdk`.
30
+ - `ui_snapshot` is read-only; `ui_act` is destructive.
31
+ - `list_windows` now returns its window lines inside an untrusted-content frame (`<namzu-untrusted kind="desktop-windows">`), since titles are whatever each application shows. The lines themselves are unchanged; code that parsed the whole output text needs to skip the frame's three header lines.
32
+ - `createComputerUseTool` returns a `ComputerUseTool`, a `ToolDefinition<ActionInput>` with `describeUiRef(ref)` (experimental), which names the control a ref points at for a review screen.
33
+ - `UiSnapshot` gains optional `title` and `app`; `UiElement.ref` is empty for an element nothing can be done with. Both types stay experimental.
34
+
35
+ **`@namzu/computer-use`**
36
+
37
+ On Windows and WSL with the cua-driver backend, `capabilities.uiTree` is `true` and `SubprocessComputerUseHost` offers `uiSnapshot(windowId?)` and `uiAct(ref, action, value?)`, read from cua-driver's UI Automation walk. A control is invoked in the background, without moving the pointer or bringing its window to the front; a field without a settable value is typed into when it is empty. An action whose driver died mid-request comes back as not done with an unknown outcome, never retried. The PowerShell fallback and the other platforms do not declare `uiTree`, so nothing changes there.
38
+
3
39
  ## 1.4.3
4
40
 
5
41
  ### Patch Changes
package/README.md CHANGED
@@ -34,7 +34,8 @@ Adapters publish an exact `supportedActions` subset. Optional `mouseClickButtons
34
34
  and `mouseDragButtons` distinguish gesture support: on macOS scrolling is
35
35
  unavailable, move/drag require cliclick, and drag supports only the left button.
36
36
  The SDK tool filters its advertised actions and rejects unsupported gestures
37
- before desktop execution. See [computer action capabilities](../../docs/sdk/computer-actions.md).
37
+ before desktop execution. See [the computer_use tool](../../docs/sdk/computer-actions.md)
38
+ and [the host contract](../../docs/sdk/computer-use-host.md).
38
39
 
39
40
  ## Install
40
41
 
@@ -80,10 +81,100 @@ report a clean completion, the host throws
80
81
  SDK returns that state to the model with `retrySafety: 'unsafe'` instead of
81
82
  inviting an automatic replay.
82
83
 
83
- When Namzu runs inside WSL, the host selects the paired Windows desktop and
84
- uses `powershell.exe` through WSL interop. This takes precedence over WSLg's
85
- `DISPLAY`/`WAYLAND_DISPLAY`, which describe Linux GUI applications rather than
86
- the Windows desktop containing the terminal.
84
+ Every coordinate and size crossing the host is in **physical pixels**: the
85
+ pixels of the captured bitmap, never points or DPI-scaled units. A capture
86
+ carries `display` (origin, size, `scaleFactor`) and action points are
87
+ relative to that display. On a Retina Mac the adapter converts to points for
88
+ `cliclick` itself, so a click at the pixel you saw lands there; on Windows
89
+ every backend is DPI aware.
90
+
91
+ `host.dispose()` stops whatever the adapter keeps running. Call it when the
92
+ session ends.
93
+
94
+ ## Windows and WSL
95
+
96
+ When Namzu runs inside WSL, the host selects the paired Windows desktop. This
97
+ takes precedence over WSLg's `DISPLAY`/`WAYLAND_DISPLAY`, which describe Linux
98
+ GUI applications rather than the Windows desktop containing the terminal.
99
+
100
+ The desktop is driven by [cua-driver](https://github.com/trycua/cua) (MIT),
101
+ one `cua-driver.exe mcp` process for the host's lifetime, spoken to over its
102
+ standard streams:
103
+
104
+ - **Pinned and checked.** The first `initialize()` downloads cua-driver
105
+ 0.28.2 for this architecture (x64 or arm64, a 27–29 MB archive from the
106
+ project's GitHub releases), checks the SHA-256 of the archive and of the
107
+ executable in it against values in this package, and keeps
108
+ `cua-driver.exe` in `<NAMZU_HOME>/computer-use/cua-driver/0.28.2/`
109
+ (`~/.namzu` by default). Nothing else in the archive is written. Later
110
+ sessions start the cached copy after checking its hash again.
111
+ - **Quiet.** It runs with its telemetry and its release check switched off
112
+ (`CUA_DRIVER_RS_TELEMETRY_ENABLED=0`, `CUA_DRIVER_RS_UPDATE_CHECK=0`,
113
+ forwarded through `WSLENV`), without any environment variable whose name
114
+ looks like a secret, and with its animated agent cursor off. Measured: a
115
+ whole session left nothing in the Windows user profile.
116
+ - **Windows.** `capabilities.windows` is `true`: `listWindows()` and
117
+ `focusWindow(id)` (which restores a minimized window, gets past the
118
+ foreground lock and reports what is actually in front afterwards).
119
+ - **Controls (experimental).** `capabilities.uiTree` is `true`:
120
+ `uiSnapshot(windowId?)` reads a window's UI Automation tree (the window in
121
+ front without an id) and `uiAct(ref, action, value?)` acts on a control of
122
+ the latest snapshot — `invoke`, `toggle`, `select`, `expand`, `collapse`
123
+ through UI Automation in the background, `set_value` through the control's
124
+ value, or by typing into an empty field that has none. The SDK tool offers
125
+ these as `ui_snapshot` and `ui_act`. Pressing six Calculator buttons this
126
+ way took about 90 ms, without bringing the window to the front.
127
+ - **Fallback.** When cua-driver cannot be downloaded, verified or started,
128
+ or does not reach the desktop, the host uses PowerShell instead — one
129
+ `powershell.exe` per action, DPI aware, Unicode text, no window list.
130
+ `host.backend` says which is in use (`cua-driver 0.28.2`, `powershell`),
131
+ `host.fallbackReason` why cua-driver is not.
132
+
133
+ `NAMZU_CUA_DRIVER=off` keeps cua-driver out (no download); a path in it runs
134
+ that `cua-driver.exe` instead of the pinned build. The same choices in code:
135
+
136
+ ```ts
137
+ import { SubprocessComputerUseHost } from '@namzu/computer-use'
138
+
139
+ const host = new SubprocessComputerUseHost({
140
+ windows: {
141
+ backend: 'auto', // or 'cua-driver' (no fallback) or 'powershell'
142
+ download: true, // false: use a cached or configured build only
143
+ },
144
+ })
145
+ await host.initialize()
146
+ console.log(host.backend, host.fallbackReason)
147
+ if (host.capabilities.windows) {
148
+ const windows = await host.listWindows()
149
+ const notepad = windows.find((window) => window.app === 'notepad')
150
+ if (notepad) await host.focusWindow(notepad.id)
151
+ }
152
+ await host.dispose()
153
+ ```
154
+
155
+ Measured on Windows 10 22H2 from WSL2, one 3440x1440 display at 100 %:
156
+
157
+ | Call | PowerShell per action (before) | cua-driver |
158
+ | --- | --- | --- |
159
+ | `initialize()` | 0.3 s | 0.5–0.7 s (2.2 s the first time, with the download) |
160
+ | screenshot (3440x1440 PNG) | 0.40–0.42 s | 0.08–0.13 s |
161
+ | cursor position | 0.31 s | 1–6 ms |
162
+ | move | 0.54–0.60 s | 1–5 ms |
163
+ | click | 0.68–0.83 s | 0.13–0.14 s |
164
+ | key | not measured | 0.04–0.05 s |
165
+ | type 21 characters | not measured | 0.09 s |
166
+ | focus a window | not offered | 4–22 ms |
167
+ | list windows | not offered | 1.2–1.9 s |
168
+
169
+ What cua-driver does not do, and so neither does this host on Windows: it
170
+ captures the primary display only; it has no region capture (the SDK crops a
171
+ full capture instead); its window list includes windows Windows keeps
172
+ cloaked (a suspended Settings app, the text-input host), and it does not say
173
+ which window has focus — `focused` is the front-most window that is not
174
+ minimized. A single punctuation key (`/`, `+`) is typed as text, because
175
+ cua-driver resolves it to a key without its shift state and on a Turkish
176
+ layout `/` came out as `7`; a chord such as `ctrl+/` still goes through
177
+ cua-driver's key mapping.
87
178
 
88
179
  ## Documentation
89
180
 
@@ -1,5 +1,6 @@
1
- import type { ComputerUseAction, ComputerUseCapabilities, ComputerUseHost, ComputerUseResult, DisplayGeometry } from '@namzu/sdk';
1
+ import type { ComputerUseAction, ComputerUseCapabilities, ComputerUseHost, ComputerUseResult, DisplayGeometry, FocusWindowResult, Rect, ScreenshotResult, UiActResult, UiElementAction, UiSnapshot, WindowInfo } from '@namzu/sdk';
2
2
  import { type Adapter } from './adapters/types.js';
3
+ import type { Win32AdapterOptions } from './adapters/win32.js';
3
4
  export interface SubprocessComputerUseHostOptions {
4
5
  readonly env?: NodeJS.ProcessEnv;
5
6
  readonly platform?: NodeJS.Platform;
@@ -9,27 +10,66 @@ export interface SubprocessComputerUseHostOptions {
9
10
  * correct adapter automatically.
10
11
  */
11
12
  readonly adapter?: Adapter;
13
+ /**
14
+ * The Windows backend (WSL included): cua-driver, pinned and downloaded on
15
+ * first use, with the PowerShell path as its fallback. See the README.
16
+ */
17
+ readonly windows?: SubprocessComputerUseHostWindowsOptions;
12
18
  }
19
+ export type SubprocessComputerUseHostWindowsOptions = Pick<Win32AdapterOptions, 'backend' | 'cuaDriverPath' | 'cacheDir' | 'download'>;
13
20
  /**
14
21
  * Subprocess-backed ComputerUseHost. Delegates platform-specific work to
15
22
  * adapters that spawn system CLIs (`screencapture`, `osascript`, `xdotool`,
16
- * `maim`, `grim`, PowerShell). The child-process model side-steps the macOS
17
- * CFRunLoop pump problem that blocks in-process native addons under
18
- * Node/libuv — each subprocess owns its own main thread.
23
+ * `maim`, `grim`, PowerShell) or, on Windows, keep one cua-driver process.
24
+ * The child-process model side-steps the macOS CFRunLoop pump problem that
25
+ * blocks in-process native addons under Node/libuv — each subprocess owns
26
+ * its own main thread.
19
27
  *
20
28
  * Lifecycle: `initialize()` probes the environment and selects the adapter.
21
29
  * Until then, `capabilities.displayServer` reflects detection but every
22
- * feature flag is `false`, and `execute` throws.
30
+ * feature flag is `false`, and `execute` throws. `dispose()` stops what the
31
+ * adapter keeps running.
23
32
  */
24
33
  export declare class SubprocessComputerUseHost implements ComputerUseHost {
34
+ private readonly options;
25
35
  readonly id = "subprocess-computer-use-host";
26
36
  private _capabilities;
27
37
  private adapter;
28
38
  constructor(options?: SubprocessComputerUseHostOptions);
29
39
  get capabilities(): ComputerUseCapabilities;
40
+ /**
41
+ * What drives the desktop once initialised — `cua-driver 0.28.2`,
42
+ * `powershell`, … — for logs and diagnosis. Undefined before
43
+ * `initialize()` and for adapters that do not say.
44
+ */
45
+ get backend(): string | undefined;
46
+ /** Why the preferred backend is not the one in use, when the adapter fell back. */
47
+ get fallbackReason(): string | undefined;
30
48
  initialize(): Promise<void>;
31
49
  getDisplayGeometry(): Promise<DisplayGeometry>;
32
50
  execute(action: ComputerUseAction): Promise<ComputerUseResult>;
51
+ /** Offered to the model only when `capabilities.windows` is true. */
52
+ listWindows(): Promise<readonly WindowInfo[]>;
53
+ /** Offered to the model only when `capabilities.windows` is true. */
54
+ focusWindow(id: string): Promise<FocusWindowResult>;
55
+ /** Offered to the model only when `capabilities.regionCapture` is true. */
56
+ captureRegion(rect: Rect): Promise<ScreenshotResult>;
57
+ /**
58
+ * A window's controls. Offered to the model only when
59
+ * `capabilities.uiTree` is true.
60
+ *
61
+ * @experimental Follows the SDK's UI-tree surface.
62
+ */
63
+ uiSnapshot(windowId?: string): Promise<UiSnapshot>;
64
+ /**
65
+ * Act on a control of the latest {@link uiSnapshot}. A request lost after
66
+ * it was sent (the driver died or stopped answering) may have acted, so
67
+ * it is reported as not done with an unknown outcome, never retried.
68
+ *
69
+ * @experimental Follows the SDK's UI-tree surface.
70
+ */
71
+ uiAct(ref: string, action: UiElementAction, value?: string): Promise<UiActResult>;
72
+ /** Stops whatever the adapter keeps running (the Windows cua-driver process). */
33
73
  dispose(): Promise<void>;
34
74
  private requireAdapter;
35
75
  }
@@ -1 +1 @@
1
- {"version":3,"file":"SubprocessComputerUseHost.d.ts","sourceRoot":"","sources":["../src/SubprocessComputerUseHost.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,iBAAiB,EACjB,uBAAuB,EACvB,eAAe,EACf,iBAAiB,EACjB,eAAe,EACf,MAAM,YAAY,CAAA;AACnB,OAAO,EAAE,KAAK,OAAO,EAA2B,MAAM,qBAAqB,CAAA;AAsB3E,MAAM,WAAW,gCAAgC;IAChD,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,CAAA;IAChC,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,QAAQ,CAAA;IACnC;;;;OAIG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAA;CAC1B;AAED;;;;;;;;;;GAUG;AACH,qBAAa,yBAA0B,YAAW,eAAe;IAChE,QAAQ,CAAC,EAAE,kCAAiC;IAC5C,OAAO,CAAC,aAAa,CAAyB;IAC9C,OAAO,CAAC,OAAO,CAAuB;gBAE1B,OAAO,GAAE,gCAAqC;IAY1D,IAAI,YAAY,IAAI,uBAAuB,CAE1C;IAEK,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAuB3B,kBAAkB,IAAI,OAAO,CAAC,eAAe,CAAC;IAK9C,OAAO,CAAC,MAAM,EAAE,iBAAiB,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAkB9D,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAI9B,OAAO,CAAC,cAAc;CAQtB"}
1
+ {"version":3,"file":"SubprocessComputerUseHost.d.ts","sourceRoot":"","sources":["../src/SubprocessComputerUseHost.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,iBAAiB,EACjB,uBAAuB,EACvB,eAAe,EACf,iBAAiB,EACjB,eAAe,EACf,iBAAiB,EACjB,IAAI,EACJ,gBAAgB,EAChB,WAAW,EACX,eAAe,EACf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AM,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAMzD,2EAA2E;IACrE,aAAa,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAM1D;;;;;OAKG;IACG,UAAU,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IAMxD;;;;;;OAMG;IACG,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;IAevF,iFAAiF;IAC3E,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAM9B,OAAO,CAAC,cAAc;CAQtB"}
@@ -20,19 +20,23 @@ const UNSAFE_TO_REPLAY_AFTER_START = new Set([
20
20
  /**
21
21
  * Subprocess-backed ComputerUseHost. Delegates platform-specific work to
22
22
  * adapters that spawn system CLIs (`screencapture`, `osascript`, `xdotool`,
23
- * `maim`, `grim`, PowerShell). The child-process model side-steps the macOS
24
- * CFRunLoop pump problem that blocks in-process native addons under
25
- * Node/libuv — each subprocess owns its own main thread.
23
+ * `maim`, `grim`, PowerShell) or, on Windows, keep one cua-driver process.
24
+ * The child-process model side-steps the macOS CFRunLoop pump problem that
25
+ * blocks in-process native addons under Node/libuv — each subprocess owns
26
+ * its own main thread.
26
27
  *
27
28
  * Lifecycle: `initialize()` probes the environment and selects the adapter.
28
29
  * Until then, `capabilities.displayServer` reflects detection but every
29
- * feature flag is `false`, and `execute` throws.
30
+ * feature flag is `false`, and `execute` throws. `dispose()` stops what the
31
+ * adapter keeps running.
30
32
  */
31
33
  export class SubprocessComputerUseHost {
34
+ options;
32
35
  id = 'subprocess-computer-use-host';
33
36
  _capabilities;
34
37
  adapter = null;
35
38
  constructor(options = {}) {
39
+ this.options = options;
36
40
  const displayServer = detectDisplayServer(options.env, options.platform);
37
41
  this._capabilities = Object.freeze({
38
42
  ...UNINITIALISED_CAPABILITIES,
@@ -46,11 +50,23 @@ export class SubprocessComputerUseHost {
46
50
  get capabilities() {
47
51
  return this._capabilities;
48
52
  }
53
+ /**
54
+ * What drives the desktop once initialised — `cua-driver 0.28.2`,
55
+ * `powershell`, … — for logs and diagnosis. Undefined before
56
+ * `initialize()` and for adapters that do not say.
57
+ */
58
+ get backend() {
59
+ return this.adapter?.backend;
60
+ }
61
+ /** Why the preferred backend is not the one in use, when the adapter fell back. */
62
+ get fallbackReason() {
63
+ return this.adapter?.fallbackReason;
64
+ }
49
65
  async initialize() {
50
66
  if (this.adapter)
51
67
  return;
52
68
  const displayServer = this._capabilities.displayServer;
53
- const adapter = await loadAdapter(displayServer);
69
+ const adapter = await loadAdapter(displayServer, this.options);
54
70
  // Loading an adapter proves its tools exist on PATH, not that a desktop
55
71
  // answers. A WSL process finds PowerShell and still may have no
56
72
  // interactive Windows session to capture; an ssh session finds xdotool
@@ -62,8 +78,12 @@ export class SubprocessComputerUseHost {
62
78
  await adapter.getDisplayGeometry();
63
79
  }
64
80
  catch (error) {
81
+ await adapter.dispose?.().catch(() => undefined);
65
82
  const reason = error instanceof Error ? error.message : String(error);
66
- throw new AdapterUnavailableError(`SubprocessComputerUseHost: the ${displayServer} adapter loaded but the desktop did not answer: ${reason}`);
83
+ const fallback = adapter.fallbackReason
84
+ ? ` (cua-driver was not used: ${adapter.fallbackReason})`
85
+ : '';
86
+ throw new AdapterUnavailableError(`SubprocessComputerUseHost: the ${displayServer} adapter loaded but the desktop did not answer: ${reason}${fallback}`);
67
87
  }
68
88
  this.adapter = adapter;
69
89
  this._capabilities = adapter.capabilities;
@@ -90,8 +110,67 @@ export class SubprocessComputerUseHost {
90
110
  throw error;
91
111
  }
92
112
  }
113
+ /** Offered to the model only when `capabilities.windows` is true. */
114
+ async listWindows() {
115
+ const adapter = this.requireAdapter();
116
+ if (!adapter.listWindows)
117
+ throw unsupported('listWindows', this._capabilities.displayServer);
118
+ return adapter.listWindows();
119
+ }
120
+ /** Offered to the model only when `capabilities.windows` is true. */
121
+ async focusWindow(id) {
122
+ const adapter = this.requireAdapter();
123
+ if (!adapter.focusWindow)
124
+ throw unsupported('focusWindow', this._capabilities.displayServer);
125
+ return adapter.focusWindow(id);
126
+ }
127
+ /** Offered to the model only when `capabilities.regionCapture` is true. */
128
+ async captureRegion(rect) {
129
+ const adapter = this.requireAdapter();
130
+ if (!adapter.captureRegion)
131
+ throw unsupported('captureRegion', this._capabilities.displayServer);
132
+ return adapter.captureRegion(rect);
133
+ }
134
+ /**
135
+ * A window's controls. Offered to the model only when
136
+ * `capabilities.uiTree` is true.
137
+ *
138
+ * @experimental Follows the SDK's UI-tree surface.
139
+ */
140
+ async uiSnapshot(windowId) {
141
+ const adapter = this.requireAdapter();
142
+ if (!adapter.uiSnapshot)
143
+ throw unsupported('uiSnapshot', this._capabilities.displayServer);
144
+ return adapter.uiSnapshot(windowId);
145
+ }
146
+ /**
147
+ * Act on a control of the latest {@link uiSnapshot}. A request lost after
148
+ * it was sent (the driver died or stopped answering) may have acted, so
149
+ * it is reported as not done with an unknown outcome, never retried.
150
+ *
151
+ * @experimental Follows the SDK's UI-tree surface.
152
+ */
153
+ async uiAct(ref, action, value) {
154
+ const adapter = this.requireAdapter();
155
+ if (!adapter.uiAct)
156
+ throw unsupported('uiAct', this._capabilities.displayServer);
157
+ try {
158
+ return await adapter.uiAct(ref, action, value);
159
+ }
160
+ catch (error) {
161
+ if (error instanceof SpawnError)
162
+ return {
163
+ ok: false,
164
+ detail: `the desktop driver stopped before it answered, so ${action} may or may not have happened; look at the screen (ui_snapshot or screenshot) before trying again. ${error.message}`,
165
+ };
166
+ throw error;
167
+ }
168
+ }
169
+ /** Stops whatever the adapter keeps running (the Windows cua-driver process). */
93
170
  async dispose() {
171
+ const adapter = this.adapter;
94
172
  this.adapter = null;
173
+ await adapter?.dispose?.();
95
174
  }
96
175
  requireAdapter() {
97
176
  if (!this.adapter) {
@@ -100,7 +179,10 @@ export class SubprocessComputerUseHost {
100
179
  return this.adapter;
101
180
  }
102
181
  }
103
- async function loadAdapter(displayServer) {
182
+ function unsupported(method, displayServer) {
183
+ return new Error(`SubprocessComputerUseHost: ${method} is not supported by the ${displayServer} adapter in use.`);
184
+ }
185
+ async function loadAdapter(displayServer, options) {
104
186
  switch (displayServer) {
105
187
  case 'darwin': {
106
188
  const { DarwinAdapter } = await import('./adapters/darwin.js');
@@ -116,7 +198,11 @@ async function loadAdapter(displayServer) {
116
198
  }
117
199
  case 'win32': {
118
200
  const { Win32Adapter } = await import('./adapters/win32.js');
119
- return Win32Adapter.create();
201
+ return Win32Adapter.create({
202
+ ...options.windows,
203
+ ...(options.env !== undefined ? { env: options.env } : {}),
204
+ ...(options.platform !== undefined ? { platform: options.platform } : {}),
205
+ });
120
206
  }
121
207
  case 'unknown':
122
208
  throw new Error(`SubprocessComputerUseHost: no adapter available for displayServer="${displayServer}" yet`);
@@ -1 +1 @@
1
- {"version":3,"file":"SubprocessComputerUseHost.js","sourceRoot":"","sources":["../src/SubprocessComputerUseHost.ts"],"names":[],"mappings":"AAOA,OAAO,EAAgB,uBAAuB,EAAE,MAAM,qBAAqB,CAAA;AAC3E,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AACvD,OAAO,EAAE,8BAA8B,EAAE,MAAM,aAAa,CAAA;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAE5C,MAAM,0BAA0B,GAA4B;IAC3D,aAAa,EAAE,SAAS;IACxB,UAAU,EAAE,KAAK;IACjB,KAAK,EAAE,KAAK;IACZ,QAAQ,EAAE,KAAK;IACf,cAAc,EAAE,KAAK;IACrB,SAAS,EAAE,KAAK;CAChB,CAAA;AAED,MAAM,4BAA4B,GAAG,IAAI,GAAG,CAA4B;IACvE,aAAa;IACb,YAAY;IACZ,QAAQ;IACR,WAAW;IACX,KAAK;CACL,CAAC,CAAA;AAaF;;;;;;;;;;GAUG;AACH,MAAM,OAAO,yBAAyB;IAC5B,EAAE,GAAG,8BAA8B,CAAA;IAC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CAAC,CAAA;YACnE,OAAO,eAAe,CAAC,MAAM,EAAE,CAAA;QAChC,CAAC;QACD,KAAK,SAAS,CAAC,CAAC,CAAC;YAChB,MAAM,EAAE,mBAAmB,EAAE,GAAG,MAAM,MAAM,CAAC,6BAA6B,CAAC,CAAA;YAC3E,OAAO,mBAAmB,CAAC,MAAM,EAAE,CAAA;QACpC,CAAC;QACD,KAAK,OAAO,CAAC,CAAC,CAAC;YACd,MAAM,EAAE,YAAY,EAAE,GAAG,MAAM,MAAM,CAAC,qBAAqB,CAAC,CAAA;YAC5D,OAAO,YAAY,CAAC,MAAM,CAAC;gBAC1B,GAAG,OAAO,CAAC,OAAO;gBAClB,GAAG,CAAC,OAAO,CAAC,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBAC1D,GAAG,CAAC,OAAO,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aACzE,CAAC,CAAA;QACH,CAAC;QACD,KAAK,SAAS;YACb,MAAM,IAAI,KAAK,CACd,sEAAsE,aAAa,OAAO,CAC1F,CAAA;IACH,CAAC;AACF,CAAC"}
@@ -0,0 +1,100 @@
1
+ import type { ComputerUseAction, ComputerUseCapabilities, ComputerUseResult, DisplayGeometry, FocusWindowResult, UiActResult, UiElementAction, UiSnapshot, WindowInfo } from '@namzu/sdk';
2
+ import type { Adapter } from '../types.js';
3
+ import { type McpStdioClientOptions, type McpToolResult } from './client.js';
4
+ /**
5
+ * The Windows desktop through cua-driver (github.com/trycua/cua, MIT): one
6
+ * `cua-driver.exe mcp` process for the adapter's lifetime, spoken to as an
7
+ * MCP server over its standard streams.
8
+ *
9
+ * Every input goes to cua-driver's `desktop` scope: real `SendInput` at
10
+ * screen coordinates, which is this package's contract. Its default
11
+ * `window` scope posts messages to a process in the background instead, a
12
+ * different model this adapter does not expose.
13
+ *
14
+ * What the adapter adds on top:
15
+ * - the animated agent cursor is switched off for its session: with it, a
16
+ * click took 0.9–3.8 s (the cursor glides to the target) instead of about
17
+ * 0.12 s, and a full-screen overlay window joined the window list;
18
+ * - a single printable key is typed as text (see `./keys.ts`);
19
+ * - a pointer action whose only failure is cua-driver's after-the-fact
20
+ * foreground check is reported as done (see {@link isDeliveredPointerAction});
21
+ * - the window list drops cua-driver's own overlay, the shell's desktop and
22
+ * 1-pixel windows, and orders it front to back;
23
+ * - a window's UI Automation tree (`get_window_state`) becomes a `UiSnapshot`
24
+ * whose refs are cua-driver's element tokens, and `uiAct` drives a control
25
+ * by token: UIA Invoke for a button, ValuePattern for a field, falling back
26
+ * to typing into a field that is empty and has no settable value (classic
27
+ * Notepad's editor is one).
28
+ */
29
+ export interface CuaDriverAdapterOptions {
30
+ /** `cua-driver.exe`, as this process starts it (a Linux path under WSL). */
31
+ readonly executable: string;
32
+ /** The environment for it; see `cuaDriverEnvironment` in `../win32.ts`. */
33
+ readonly env: NodeJS.ProcessEnv;
34
+ readonly cwd?: string;
35
+ /** For the backend label; the pinned version when it is the pinned build. */
36
+ readonly version?: string;
37
+ /** For tests. */
38
+ readonly spawnProcess?: McpStdioClientOptions['spawnProcess'];
39
+ readonly requestTimeoutMs?: number;
40
+ readonly startTimeoutMs?: number;
41
+ }
42
+ export declare class CuaDriverAdapter implements Adapter {
43
+ readonly capabilities: ComputerUseCapabilities;
44
+ readonly backend: string;
45
+ private readonly client;
46
+ /** Window id → owning pid, from the latest list; `bring_to_front` needs both. */
47
+ private readonly windowPids;
48
+ /** The refs of the latest UI snapshot: element token → its window's pid and what it held. */
49
+ private uiRefs;
50
+ constructor(options: CuaDriverAdapterOptions);
51
+ /** How many driver processes this adapter has started (a restart after a crash counts). */
52
+ get processStarts(): number;
53
+ getDisplayGeometry(): Promise<DisplayGeometry>;
54
+ execute(action: ComputerUseAction): Promise<ComputerUseResult>;
55
+ listWindows(): Promise<readonly WindowInfo[]>;
56
+ focusWindow(id: string): Promise<FocusWindowResult>;
57
+ /**
58
+ * One window's controls. Without an id, the window in front — which,
59
+ * for an agent run from a terminal, is usually that terminal.
60
+ */
61
+ uiSnapshot(windowId?: string): Promise<UiSnapshot>;
62
+ uiAct(ref: string, action: UiElementAction, value?: string): Promise<UiActResult>;
63
+ dispose(): Promise<void>;
64
+ /** A window id (or the window in front) → its handle and owning process. */
65
+ private resolveWindow;
66
+ private pointer;
67
+ private capture;
68
+ }
69
+ /**
70
+ * cua-driver activates the window under a desktop click, clicks, and then
71
+ * checks that the window — or another of its process — is still in front. A
72
+ * click that closes its own window (a dialog's "Don't Save", a close button)
73
+ * or starts another program leaves someone else in front, and the check
74
+ * fails although the click happened: "…was not foreground after the click".
75
+ * Reporting that as a failure would invite the model to click again, so it
76
+ * counts as done. The refusals that come before any input say "no input was
77
+ * sent" / "no mouse input was sent" and stay failures.
78
+ */
79
+ export declare function isDeliveredPointerAction(error: unknown): boolean;
80
+ /**
81
+ * cua-driver's action result → the host's. `confirmed`, `partial` and
82
+ * `unverifiable` count as done: a background UIA Invoke is `unverifiable` by
83
+ * design (the control does not report back), and the screenshot the tool
84
+ * takes afterwards is where the model sees the effect.
85
+ */
86
+ export declare function actResult(result: McpToolResult): UiActResult;
87
+ /** `0x…` hex, the form `bring_to_front` reports and `WindowInfo.id` carries. */
88
+ export declare function windowIdOf(hwnd: number): string;
89
+ export declare function parseWindowId(value: unknown): number | undefined;
90
+ /**
91
+ * cua-driver's `list_windows` records → the host's `WindowInfo`, front to back.
92
+ *
93
+ * Dropped: untitled windows, cua-driver's own (its cursor overlay covers the
94
+ * whole screen), the shell's `Program Manager` desktop, and windows a pixel
95
+ * wide or tall. `focused` is the front-most window that is not minimized —
96
+ * cua-driver does not report the foreground window itself, and the
97
+ * foreground window is the front of the non-topmost z-order.
98
+ */
99
+ export declare function toWindowInfos(raw: unknown): WindowInfo[];
100
+ //# sourceMappingURL=adap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