@namzu/computer-use 4.4.0 → 4.6.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 (139) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/dist/SubprocessComputerUseHost.d.ts +8 -1
  3. package/dist/SubprocessComputerUseHost.d.ts.map +1 -1
  4. package/dist/SubprocessComputerUseHost.js +15 -4
  5. package/dist/SubprocessComputerUseHost.js.map +1 -1
  6. package/dist/a11y/a11y.d.ts +15 -7
  7. package/dist/a11y/a11y.d.ts.map +1 -1
  8. package/dist/a11y/a11y.js +12 -5
  9. package/dist/a11y/a11y.js.map +1 -1
  10. package/dist/a11y/index.d.ts +7 -1
  11. package/dist/a11y/index.d.ts.map +1 -1
  12. package/dist/a11y/index.js +4 -1
  13. package/dist/a11y/index.js.map +1 -1
  14. package/dist/a11y/macos-helper.d.ts +80 -0
  15. package/dist/a11y/macos-helper.d.ts.map +1 -0
  16. package/dist/a11y/macos-helper.js +436 -0
  17. package/dist/a11y/macos-helper.js.map +1 -0
  18. package/dist/a11y/macos-signing.d.ts +56 -0
  19. package/dist/a11y/macos-signing.d.ts.map +1 -0
  20. package/dist/a11y/macos-signing.js +228 -0
  21. package/dist/a11y/macos-signing.js.map +1 -0
  22. package/dist/a11y/macos.d.ts +173 -0
  23. package/dist/a11y/macos.d.ts.map +1 -0
  24. package/dist/a11y/macos.js +271 -0
  25. package/dist/a11y/macos.js.map +1 -0
  26. package/dist/a11y/windows.d.ts +14 -2
  27. package/dist/a11y/windows.d.ts.map +1 -1
  28. package/dist/a11y/windows.js +31 -6
  29. package/dist/a11y/windows.js.map +1 -1
  30. package/dist/adapters/cua-driver/adapter.d.ts.map +1 -1
  31. package/dist/adapters/cua-driver/adapter.js +48 -10
  32. package/dist/adapters/cua-driver/adapter.js.map +1 -1
  33. package/dist/adapters/cua-driver/ui-tree.d.ts +1 -0
  34. package/dist/adapters/cua-driver/ui-tree.d.ts.map +1 -1
  35. package/dist/adapters/cua-driver/ui-tree.js +2 -0
  36. package/dist/adapters/cua-driver/ui-tree.js.map +1 -1
  37. package/dist/adapters/darwin-ax.d.ts +94 -0
  38. package/dist/adapters/darwin-ax.d.ts.map +1 -0
  39. package/dist/adapters/darwin-ax.js +754 -0
  40. package/dist/adapters/darwin-ax.js.map +1 -0
  41. package/dist/adapters/darwin.d.ts +43 -2
  42. package/dist/adapters/darwin.d.ts.map +1 -1
  43. package/dist/adapters/darwin.js +145 -7
  44. package/dist/adapters/darwin.js.map +1 -1
  45. package/dist/adapters/png.d.ts +6 -0
  46. package/dist/adapters/png.d.ts.map +1 -0
  47. package/dist/adapters/png.js +7 -0
  48. package/dist/adapters/png.js.map +1 -0
  49. package/dist/adapters/win32-native.d.ts +53 -0
  50. package/dist/adapters/win32-native.d.ts.map +1 -0
  51. package/dist/adapters/win32-native.js +279 -0
  52. package/dist/adapters/win32-native.js.map +1 -0
  53. package/dist/adapters/win32.d.ts +19 -1
  54. package/dist/adapters/win32.d.ts.map +1 -1
  55. package/dist/adapters/win32.js +35 -8
  56. package/dist/adapters/win32.js.map +1 -1
  57. package/dist/foreground/ladder.d.ts.map +1 -1
  58. package/dist/foreground/ladder.js +5 -1
  59. package/dist/foreground/ladder.js.map +1 -1
  60. package/dist/foreground/types.d.ts +6 -0
  61. package/dist/foreground/types.d.ts.map +1 -1
  62. package/dist/foreground/types.js.map +1 -1
  63. package/dist/foreground/windows.d.ts +2 -0
  64. package/dist/foreground/windows.d.ts.map +1 -1
  65. package/dist/foreground/windows.js +24 -11
  66. package/dist/foreground/windows.js.map +1 -1
  67. package/dist/index.d.ts +9 -0
  68. package/dist/index.d.ts.map +1 -1
  69. package/dist/index.js +7 -0
  70. package/dist/index.js.map +1 -1
  71. package/dist/native/a11y.d.ts +61 -0
  72. package/dist/native/a11y.d.ts.map +1 -0
  73. package/dist/native/a11y.js +197 -0
  74. package/dist/native/a11y.js.map +1 -0
  75. package/dist/native/env.d.ts +19 -0
  76. package/dist/native/env.d.ts.map +1 -0
  77. package/dist/native/env.js +29 -0
  78. package/dist/native/env.js.map +1 -0
  79. package/dist/native/foreground-monitor.d.ts +51 -0
  80. package/dist/native/foreground-monitor.d.ts.map +1 -0
  81. package/dist/native/foreground-monitor.js +120 -0
  82. package/dist/native/foreground-monitor.js.map +1 -0
  83. package/dist/native/presence.d.ts +24 -0
  84. package/dist/native/presence.d.ts.map +1 -0
  85. package/dist/native/presence.js +47 -0
  86. package/dist/native/presence.js.map +1 -0
  87. package/dist/native/process.d.ts +73 -0
  88. package/dist/native/process.d.ts.map +1 -0
  89. package/dist/native/process.js +302 -0
  90. package/dist/native/process.js.map +1 -0
  91. package/dist/native/protocol.d.ts +476 -0
  92. package/dist/native/protocol.d.ts.map +1 -0
  93. package/dist/native/protocol.js +601 -0
  94. package/dist/native/protocol.js.map +1 -0
  95. package/dist/native/shared.d.ts +16 -0
  96. package/dist/native/shared.d.ts.map +1 -0
  97. package/dist/native/shared.js +62 -0
  98. package/dist/native/shared.js.map +1 -0
  99. package/dist/native/watch.d.ts +35 -0
  100. package/dist/native/watch.d.ts.map +1 -0
  101. package/dist/native/watch.js +45 -0
  102. package/dist/native/watch.js.map +1 -0
  103. package/dist/presence/presence.d.ts +3 -2
  104. package/dist/presence/presence.d.ts.map +1 -1
  105. package/dist/presence/presence.js.map +1 -1
  106. package/dist/presence/windows.d.ts +2 -0
  107. package/dist/presence/windows.d.ts.map +1 -1
  108. package/dist/presence/windows.js +15 -0
  109. package/dist/presence/windows.js.map +1 -1
  110. package/helper-macos/main.swift +1756 -0
  111. package/package.json +3 -2
  112. package/src/SubprocessComputerUseHost.ts +26 -2
  113. package/src/a11y/a11y.ts +25 -11
  114. package/src/a11y/index.ts +28 -1
  115. package/src/a11y/macos-helper.ts +538 -0
  116. package/src/a11y/macos-signing.ts +280 -0
  117. package/src/a11y/macos.ts +496 -0
  118. package/src/a11y/windows.ts +31 -4
  119. package/src/adapters/cua-driver/adapter.ts +55 -15
  120. package/src/adapters/cua-driver/ui-tree.ts +2 -0
  121. package/src/adapters/darwin-ax.ts +920 -0
  122. package/src/adapters/darwin.ts +184 -4
  123. package/src/adapters/png.ts +5 -0
  124. package/src/adapters/win32-native.ts +329 -0
  125. package/src/adapters/win32.ts +53 -5
  126. package/src/foreground/ladder.ts +5 -1
  127. package/src/foreground/types.ts +6 -0
  128. package/src/foreground/windows.ts +25 -11
  129. package/src/index.ts +14 -0
  130. package/src/native/a11y.ts +267 -0
  131. package/src/native/env.ts +33 -0
  132. package/src/native/foreground-monitor.ts +165 -0
  133. package/src/native/presence.ts +58 -0
  134. package/src/native/process.ts +347 -0
  135. package/src/native/protocol.ts +1103 -0
  136. package/src/native/shared.ts +75 -0
  137. package/src/native/watch.ts +78 -0
  138. package/src/presence/presence.ts +3 -2
  139. package/src/presence/windows.ts +15 -0
@@ -1,6 +1,9 @@
1
1
  import type { AccessibilityBackend } from '../a11y/interface.js'
2
2
  import { guardForeground } from '../foreground/guard.js'
3
3
  import type { ForegroundLadder } from '../foreground/ladder.js'
4
+ import { windowsPathFor } from '../foreground/windows.js'
5
+ import { resolveNativeHelper } from '../native/env.js'
6
+ import type { NativeWinProcessOptions } from '../native/process.js'
4
7
  import type { Presence } from '../presence/presence.js'
5
8
  import { CuaDriverAdapter } from './cua-driver/adapter.js'
6
9
  import {
@@ -9,6 +12,7 @@ import {
9
12
  resolveCuaDriver,
10
13
  } from './cua-driver/provision.js'
11
14
  import { type Adapter, AdapterUnavailableError } from './types.js'
15
+ import { NativeWin32Adapter } from './win32-native.js'
12
16
  import { Win32PowerShellAdapter } from './win32-powershell.js'
13
17
  import { type WslProbes, isWsl, realWslProbes, wslChildEnv } from './wsl.js'
14
18
 
@@ -17,9 +21,14 @@ import { type WslProbes, isWsl, realWslProbes, wslChildEnv } from './wsl.js'
17
21
  *
18
22
  * - `auto` (the default): cua-driver — the pinned build, downloaded and
19
23
  * checked on first use — and, when it cannot be had or does not answer,
20
- * the per-action PowerShell path, with the reason on `fallbackReason`.
24
+ * the per-action path, with the reason on `fallbackReason`.
21
25
  * - `cua-driver`: cua-driver or nothing; its failure is the adapter's.
22
26
  * - `powershell`: never cua-driver.
27
+ *
28
+ * The per-action path is the native helper (`namzu-native`) when one is
29
+ * configured (`NAMZU_NATIVE_HELPER`, or the `helper` option) and PowerShell
30
+ * otherwise. With a helper configured there is no PowerShell fallback: a
31
+ * helper that does not start is an error, not a reason to run scripts.
23
32
  */
24
33
  export type Win32Backend = 'auto' | 'cua-driver' | 'powershell'
25
34
 
@@ -33,6 +42,14 @@ export interface Win32AdapterOptions {
33
42
  readonly download?: boolean
34
43
  /** Told once, in plain words, before the pinned build is downloaded. */
35
44
  readonly onNotice?: (notice: string) => void
45
+ /**
46
+ * The path of the native helper, `namzu-native.exe`. Default: the
47
+ * `NAMZU_NATIVE_HELPER` variable. Set, the helper is the per-action path and
48
+ * PowerShell is never started for display input or capture.
49
+ */
50
+ readonly helper?: string
51
+ /** Where the helper writes its pictures. Default: a private temporary directory. */
52
+ readonly captureDir?: string
36
53
  readonly env?: NodeJS.ProcessEnv
37
54
  readonly platform?: NodeJS.Platform
38
55
  /**
@@ -49,6 +66,8 @@ export interface Win32AdapterOptions {
49
66
  readonly wslProbes?: WslProbes
50
67
  /** For tests. */
51
68
  readonly resolve?: typeof resolveCuaDriver
69
+ /** For tests: starts the native helper's process. */
70
+ readonly spawnHelper?: NativeWinProcessOptions['spawnHelper']
52
71
  /** For tests. */
53
72
  readonly createCuaDriverAdapter?: (
54
73
  options: ConstructorParameters<typeof CuaDriverAdapter>[0],
@@ -84,6 +103,19 @@ export function cuaDriverEnvironment(
84
103
  return wsl ? wslChildEnv(base, Object.keys(CUA_DRIVER_SETTINGS), probes) : base
85
104
  }
86
105
 
106
+ /** The environment the native helper runs with: the caller's minus anything named like a secret; under WSL, with a working `WSL_INTEROP`. */
107
+ export function nativeHelperEnvironment(
108
+ env: NodeJS.ProcessEnv,
109
+ wsl: boolean,
110
+ probes: WslProbes = realWslProbes,
111
+ ): NodeJS.ProcessEnv {
112
+ const base: NodeJS.ProcessEnv = {}
113
+ for (const [name, value] of Object.entries(env)) {
114
+ if (!SECRET_NAME.test(name)) base[name] = value
115
+ }
116
+ return wsl ? wslChildEnv(base, [], probes) : base
117
+ }
118
+
87
119
  /**
88
120
  * `NAMZU_CUA_DRIVER`: `off` keeps cua-driver out (the PowerShell path);
89
121
  * a path names the `cua-driver.exe` to run instead of the pinned build.
@@ -106,8 +138,23 @@ export class Win32Adapter {
106
138
  const ladder = options.foreground
107
139
  const guard = (adapter: Adapter): Adapter =>
108
140
  ladder ? guardForeground(adapter, ladder) : adapter
109
- const powershell = async (fallbackReason?: string) =>
110
- guard(
141
+ const helper = resolveNativeHelper(options.helper, env)
142
+ // The per-action path: the native helper when one is named, else PowerShell.
143
+ // A helper that does not start throws; it never turns into PowerShell.
144
+ const perAction = async (fallbackReason?: string) => {
145
+ if (helper) {
146
+ const wsl = isWsl(env, platform, probes)
147
+ return guard(
148
+ await NativeWin32Adapter.create({
149
+ executable: helper,
150
+ env: nativeHelperEnvironment(env, wsl, probes),
151
+ ...(options.captureDir ? { captureDir: options.captureDir } : {}),
152
+ ...(wsl ? { toHelperPath: (path: string) => windowsPathFor(path, env) } : {}),
153
+ ...(options.spawnHelper ? { spawnHelper: options.spawnHelper } : {}),
154
+ }),
155
+ )
156
+ }
157
+ return guard(
111
158
  await Win32PowerShellAdapter.create({
112
159
  env,
113
160
  platform,
@@ -115,7 +162,8 @@ export class Win32Adapter {
115
162
  fallbackReason,
116
163
  }),
117
164
  )
118
- if (backend === 'powershell') return powershell()
165
+ }
166
+ if (backend === 'powershell') return perAction()
119
167
 
120
168
  const cuaDriverPath = options.cuaDriverPath ?? configured.path
121
169
  let adapter: Adapter | undefined
@@ -149,7 +197,7 @@ export class Win32Adapter {
149
197
  if (backend === 'cua-driver') {
150
198
  throw new AdapterUnavailableError(`Win32Adapter: cua-driver is not usable: ${reason}`)
151
199
  }
152
- return powershell(reason)
200
+ return perAction(reason)
153
201
  }
154
202
  }
155
203
  }
@@ -411,7 +411,11 @@ export class ForegroundLadder {
411
411
  // Checked by the monitor before it raises anything, so a person who
412
412
  // started typing after the last look is not displaced.
413
413
  idleMs: state.needed,
414
- ...(subject.hwnd !== undefined ? { raise: subject.hwnd } : {}),
414
+ ...(subject.hwnd !== undefined
415
+ ? { raise: subject.hwnd }
416
+ : state.state.foreground
417
+ ? { watch: state.state.foreground.hwnd }
418
+ : {}),
415
419
  })
416
420
  } catch (error) {
417
421
  if (error instanceof CallAbortedError) throw error
@@ -205,6 +205,12 @@ export interface ForegroundMonitor {
205
205
  * target already in front. `raised` on the answer says whether it worked.
206
206
  */
207
207
  readonly raise?: string
208
+ /**
209
+ * Window handle (hex) the burst is for, nothing raised: a monitor that
210
+ * can refuse input checks that a window of this window's program is in
211
+ * front for every input it is sent. Ignored when `raise` is given.
212
+ */
213
+ readonly watch?: string
208
214
  /**
209
215
  * The user must have been still this long (ms). Checked before anything
210
216
  * is raised; when they were not, nothing is moved and `refused` is true.
@@ -7,6 +7,8 @@ import {
7
7
  wslChildEnv,
8
8
  wslPowerShellPath,
9
9
  } from '../adapters/wsl.js'
10
+ import { resolveNativeHelper } from '../native/env.js'
11
+ import { NativeForegroundMonitor } from '../native/foreground-monitor.js'
10
12
  import { ForegroundLadder, type LadderTiming } from './ladder.js'
11
13
  import type { ForegroundApprover } from './types.js'
12
14
  import { WindowsInputMonitor } from './windows-monitor.js'
@@ -21,6 +23,8 @@ export interface WindowsLadderOptions {
21
23
  readonly approve?: ForegroundApprover
22
24
  readonly session?: string
23
25
  readonly timing?: Partial<LadderTiming>
26
+ /** The native helper (`namzu-native.exe`); default `NAMZU_NATIVE_HELPER`. Named, the monitor is the helper and no PowerShell runs. */
27
+ readonly helper?: string
24
28
  }
25
29
 
26
30
  /** `<NAMZU_HOME>/computer-use/screen.lease`, `NAMZU_HOME` defaulting to `~/.namzu`. */
@@ -48,12 +52,16 @@ export function createWindowsForegroundLadder(
48
52
  const platform = options.platform ?? process.platform
49
53
  const probes = options.wslProbes ?? realWslProbes
50
54
  const wsl = isWsl(env, platform, probes)
51
- const powershell = wsl
52
- ? wslPowerShellPath(probes)
53
- : platform === 'win32'
54
- ? 'powershell.exe'
55
- : undefined
56
- if (!powershell) return undefined
55
+ const native = resolveNativeHelper(options.helper, env)
56
+ const powershell = native
57
+ ? undefined
58
+ : wsl
59
+ ? wslPowerShellPath(probes)
60
+ : platform === 'win32'
61
+ ? 'powershell.exe'
62
+ : undefined
63
+ if (!native && !powershell) return undefined
64
+ if (native && !wsl && platform !== 'win32') return undefined
57
65
  const clean: NodeJS.ProcessEnv = {}
58
66
  for (const [name, value] of Object.entries(env)) if (!SECRET_NAME.test(name)) clean[name] = value
59
67
  const helperEnv = wsl ? wslChildEnv(clean, [], probes) : clean
@@ -61,11 +69,17 @@ export function createWindowsForegroundLadder(
61
69
  return new ForegroundLadder({
62
70
  leasePath,
63
71
  createMonitor: () =>
64
- new WindowsInputMonitor({
65
- powershell,
66
- env: helperEnv,
67
- toWindowsPath: (path) => windowsPathFor(path, env),
68
- }),
72
+ native
73
+ ? new NativeForegroundMonitor({
74
+ executable: native,
75
+ env: helperEnv,
76
+ toHelperPath: (path) => windowsPathFor(path, env),
77
+ })
78
+ : new WindowsInputMonitor({
79
+ powershell: powershell as string,
80
+ env: helperEnv,
81
+ toWindowsPath: (path) => windowsPathFor(path, env),
82
+ }),
69
83
  ...(options.approve ? { approve: options.approve } : {}),
70
84
  ...(options.session ? { session: options.session } : {}),
71
85
  ...(options.timing ? { timing: options.timing } : {}),
package/src/index.ts CHANGED
@@ -58,3 +58,17 @@ export type {
58
58
  A11yWindow,
59
59
  AccessibilityBackend,
60
60
  } from './a11y/index.js'
61
+
62
+ /**
63
+ * The controls a person uses, watched through the native Windows helper's read-only UI Automation
64
+ * subscriptions. There is no key and no mouse button in what it reports, and a password field says
65
+ * `secure` and never its text.
66
+ */
67
+ export { NativeWatch } from './native/watch.js'
68
+ export type { NativeWatchOptions, NativeWatchStart, NativeWatching } from './native/watch.js'
69
+ export { NATIVE_WATCH_KINDS } from './native/protocol.js'
70
+ export type {
71
+ NativeWatchEvent,
72
+ NativeWatchKind,
73
+ NativeWatchPollReply,
74
+ } from './native/protocol.js'
@@ -0,0 +1,267 @@
1
+ /**
2
+ * {@link AccessibilityBackend} answered by the native Windows helper.
3
+ *
4
+ * It says what {@link WindowsA11yHost} says (the same interface, the same
5
+ * limits, the same change-marker contract) but asks a compiled program that
6
+ * calls UI Automation directly, instead of a PowerShell process that
7
+ * compiles C# at its first use. Only the process behind it changes; a caller
8
+ * that held the PowerShell host holds this one the same way.
9
+ *
10
+ * Window ids: the host's callers know a window as `0x<handle>` (the form the
11
+ * adapter reports); the helper names one `w:<handle>:<process>`. This class
12
+ * translates both ways, remembering the process of every window it has been
13
+ * told about so a later id carries it.
14
+ */
15
+
16
+ import {
17
+ type A11yDisplay,
18
+ A11yError,
19
+ type A11yFingerprint,
20
+ type A11yIdentityRequest,
21
+ type A11yNode,
22
+ type A11yResolution,
23
+ type A11yResolveRequest,
24
+ type A11ySeq,
25
+ type A11ySnapshot,
26
+ type A11ySnapshotRequest,
27
+ type A11yWaitRequest,
28
+ type A11yWaitResult,
29
+ type A11yWindow,
30
+ type AccessibilityBackend,
31
+ } from '../a11y/interface.js'
32
+ import { NativeWinProcess, type NativeWinProcessOptions } from './process.js'
33
+ import type { NativeActReply, NativeActRequest, NativeNode } from './protocol.js'
34
+
35
+ /** Tokens carry the helper run they came from, so one from before a restart is refused, not misread. */
36
+ const EPOCH_SPAN = 2 ** 40
37
+
38
+ const DEFAULT_REQUEST_MS = 5_000
39
+ const DEFAULT_SNAPSHOT_MS = 8_000
40
+ const DEFAULT_WAIT_MS = 2_000
41
+ const ARM_MS = 9_000
42
+
43
+ /** The handle in a helper window id, or in the host's `0x…` form. */
44
+ function handleOf(windowId: string): number {
45
+ const text = windowId.trim().toLowerCase()
46
+ const helper = /^w:([0-9a-f]{1,16})(?::\d+)?$/.exec(text)
47
+ const value = helper
48
+ ? Number.parseInt(helper[1] as string, 16)
49
+ : /^0x[0-9a-f]+$/.test(text)
50
+ ? Number.parseInt(text.slice(2), 16)
51
+ : /^\d+$/.test(text)
52
+ ? Number.parseInt(text, 10)
53
+ : Number.NaN
54
+ if (!Number.isSafeInteger(value) || value <= 0)
55
+ throw new A11yError('bad_window_id', 'not a window id this backend handed out')
56
+ return value
57
+ }
58
+
59
+ /** The id callers hold: `0x…`, the form the adapter uses. */
60
+ const hostIdOf = (handle: number): string => `0x${handle.toString(16)}`
61
+
62
+ const num = (value: number | undefined, fallback = 0): number =>
63
+ typeof value === 'number' && Number.isFinite(value) ? value : fallback
64
+
65
+ function toNode(n: NativeNode): A11yNode {
66
+ const { ref: _ref, ...node } = n
67
+ return node as A11yNode
68
+ }
69
+
70
+ /** What `act` takes: a window by the id callers hold, and the control by ref or by fingerprint. */
71
+ export type NativeActInput = Omit<NativeActRequest, 'op'>
72
+
73
+ /**
74
+ * The typed failure a ref that could not be acted on stands for, from the
75
+ * helper's own error code: the control is gone or the ref is out of date
76
+ * (`STALE_REF`), or several controls fit (`AMBIGUOUS_TARGET`). Anything else
77
+ * is not one of the two, and nothing is said about it here.
78
+ */
79
+ export function actFailureCode(error: unknown): 'STALE_REF' | 'AMBIGUOUS_TARGET' | undefined {
80
+ if (!(error instanceof A11yError)) return undefined
81
+ if (error.code === 'not_found' || error.code === 'stale_snapshot') return 'STALE_REF'
82
+ if (error.code === 'ambiguous') return 'AMBIGUOUS_TARGET'
83
+ return undefined
84
+ }
85
+
86
+ export class NativeA11yBackend implements AccessibilityBackend {
87
+ readonly id = 'native-uia'
88
+
89
+ private readonly process: NativeWinProcess
90
+ /** Window handle to the process that owned it when last listed or named. */
91
+ private readonly processes = new Map<number, number>()
92
+
93
+ constructor(options: NativeWinProcessOptions) {
94
+ this.process = new NativeWinProcess(options)
95
+ }
96
+
97
+ get isReady(): boolean {
98
+ return this.process.isUp
99
+ }
100
+
101
+ warm(): void {
102
+ this.process.warm()
103
+ }
104
+
105
+ release(): void {
106
+ this.process.release()
107
+ }
108
+
109
+ ready(): Promise<boolean> {
110
+ return this.process.ready()
111
+ }
112
+
113
+ async listWindows(
114
+ options: { readonly includeOwned?: boolean } = {},
115
+ signal?: AbortSignal,
116
+ ): Promise<readonly A11yWindow[]> {
117
+ const reply = await this.process.request(
118
+ { op: 'windows', ...(options.includeOwned ? { includeOwned: true } : {}) },
119
+ DEFAULT_REQUEST_MS,
120
+ signal,
121
+ )
122
+ return reply.windows.map((w) => {
123
+ const handle = handleOf(w.windowId)
124
+ this.processes.set(handle, w.processId)
125
+ return { ...w, windowId: hostIdOf(handle) }
126
+ })
127
+ }
128
+
129
+ async listDisplays(signal?: AbortSignal): Promise<readonly A11yDisplay[]> {
130
+ const reply = await this.process.request({ op: 'displays' }, DEFAULT_REQUEST_MS, signal)
131
+ return reply.displays
132
+ }
133
+
134
+ async snapshot(request: A11ySnapshotRequest, signal?: AbortSignal): Promise<A11ySnapshot> {
135
+ const handle = handleOf(request.windowId)
136
+ const timeoutMs = request.timeoutMs ?? DEFAULT_SNAPSHOT_MS
137
+ const reply = await this.process.request(
138
+ {
139
+ op: 'snapshot',
140
+ windowId: this.helperIdOf(handle),
141
+ timeoutMs,
142
+ ...(request.maxNodes !== undefined ? { maxNodes: request.maxNodes } : {}),
143
+ ...(request.maxDepth !== undefined ? { maxDepth: request.maxDepth } : {}),
144
+ ...(request.skeleton ? { skeleton: true } : {}),
145
+ ...(request.allowChromium ? { allowChromium: true } : {}),
146
+ },
147
+ timeoutMs,
148
+ signal,
149
+ )
150
+ return {
151
+ windowId: hostIdOf(handle),
152
+ revision: reply.revision,
153
+ nodes: reply.nodes.map(toNode),
154
+ total: reply.total,
155
+ truncated: reply.truncated,
156
+ skeleton: reply.skeleton,
157
+ readMs: reply.readMs,
158
+ }
159
+ }
160
+
161
+ async seq(windowId?: string, signal?: AbortSignal): Promise<A11ySeq> {
162
+ const reply = await this.process.request(
163
+ windowId === undefined
164
+ ? { op: 'seq' }
165
+ : { op: 'seq', windowId: this.helperIdOf(handleOf(windowId)) },
166
+ windowId === undefined ? DEFAULT_REQUEST_MS : ARM_MS,
167
+ signal,
168
+ )
169
+ return this.process.epoch * EPOCH_SPAN + reply.seq
170
+ }
171
+
172
+ async waitForChange(request: A11yWaitRequest, signal?: AbortSignal): Promise<A11yWaitResult> {
173
+ const handle = handleOf(request.windowId)
174
+ const timeoutMs = request.timeoutMs ?? DEFAULT_WAIT_MS
175
+ let since: number | undefined
176
+ if (request.since !== undefined) {
177
+ if (Math.floor(request.since / EPOCH_SPAN) !== this.process.epoch)
178
+ throw new A11yError('stale_seq', 'the helper restarted since this marker was taken')
179
+ since = request.since % EPOCH_SPAN
180
+ }
181
+ const reply = await this.process.request(
182
+ {
183
+ op: 'wait_change',
184
+ windowId: this.helperIdOf(handle),
185
+ timeoutMs,
186
+ ...(request.quietMs !== undefined ? { quietMs: request.quietMs } : {}),
187
+ ...(since !== undefined ? { since } : {}),
188
+ ...(request.kinds ? { kinds: [...request.kinds] } : {}),
189
+ },
190
+ // The helper answers within the wait plus a few ms; the client's own limit is the backstop.
191
+ Math.min(timeoutMs, 10_000),
192
+ signal,
193
+ )
194
+ if (!reply.changed) return { changed: false, timedOut: true, settledMs: num(reply.settledMs) }
195
+ return {
196
+ changed: true,
197
+ settled: reply.settled,
198
+ firstEventMs: reply.firstEventMs,
199
+ settledMs: reply.settledMs,
200
+ events: reply.events,
201
+ newWindows: reply.newWindows.map((id) => hostIdOf(this.remember(id))),
202
+ }
203
+ }
204
+
205
+ async identity(request: A11yIdentityRequest, signal?: AbortSignal): Promise<A11yFingerprint> {
206
+ const reply = await this.process.request(
207
+ {
208
+ op: 'identity',
209
+ windowId: this.helperIdOf(handleOf(request.windowId)),
210
+ index: request.index,
211
+ ...(request.revision !== undefined ? { revision: request.revision } : {}),
212
+ },
213
+ DEFAULT_SNAPSHOT_MS,
214
+ signal,
215
+ )
216
+ return reply.fingerprint
217
+ }
218
+
219
+ async resolve(request: A11yResolveRequest, signal?: AbortSignal): Promise<A11yResolution> {
220
+ return this.process.request(
221
+ {
222
+ op: 'resolve',
223
+ windowId: this.helperIdOf(handleOf(request.windowId)),
224
+ fingerprint: request.fingerprint,
225
+ },
226
+ DEFAULT_SNAPSHOT_MS,
227
+ signal,
228
+ )
229
+ }
230
+
231
+ /**
232
+ * Does one thing to one control through the pattern it offers (see the
233
+ * helper's `act`): nothing is clicked and no key is sent. A control that
234
+ * offers no pattern for the action is refused (`background_unavailable`), as
235
+ * is one that is turned off (`disabled`); a ref or fingerprint that no longer
236
+ * names one control fails with {@link actFailureCode}'s codes. In every
237
+ * refusal nothing was sent.
238
+ */
239
+ async act(request: NativeActInput, signal?: AbortSignal): Promise<NativeActReply> {
240
+ const handle = handleOf(request.windowId)
241
+ const { windowId: _windowId, ...rest } = request
242
+ return this.process.request(
243
+ { op: 'act', windowId: this.helperIdOf(handle), ...rest },
244
+ request.timeoutMs ?? DEFAULT_SNAPSHOT_MS,
245
+ signal,
246
+ )
247
+ }
248
+
249
+ close(): Promise<void> {
250
+ return this.process.close()
251
+ }
252
+
253
+ /** The helper's id for a window handle: with its process when this backend knows it. */
254
+ private helperIdOf(handle: number): string {
255
+ const hex = handle.toString(16).padStart(16, '0')
256
+ const pid = this.processes.get(handle)
257
+ return pid === undefined ? `w:${hex}` : `w:${hex}:${pid}`
258
+ }
259
+
260
+ /** Reads a helper id, keeps its process and returns its handle. */
261
+ private remember(helperId: string): number {
262
+ const handle = handleOf(helperId)
263
+ const pid = /:(\d+)$/.exec(helperId)?.[1]
264
+ if (pid !== undefined) this.processes.set(handle, Number.parseInt(pid, 10))
265
+ return handle
266
+ }
267
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Which environment variable names the native helper.
3
+ *
4
+ * `NAMZU_NATIVE_HELPER` is the name. `NAMZU_WIN_HELPER` is what the variable
5
+ * was called while the helper was `namzu-win`; it is still read when the new
6
+ * one is absent or blank.
7
+ *
8
+ * Deprecated: the next major release of `@namzu/computer-use` stops reading
9
+ * `NAMZU_WIN_HELPER` and accepting the `namzu-win/1` protocol name.
10
+ */
11
+
12
+ /** The name of the variable that holds the path of the helper. */
13
+ export const NATIVE_HELPER_ENV = 'NAMZU_NATIVE_HELPER'
14
+
15
+ /** @deprecated The old name of {@link NATIVE_HELPER_ENV}; read only as a fallback. */
16
+ export const LEGACY_NATIVE_HELPER_ENV = 'NAMZU_WIN_HELPER'
17
+
18
+ /** The helper path the environment names, or undefined when it names none. */
19
+ export function nativeHelperFromEnv(env: NodeJS.ProcessEnv): string | undefined {
20
+ const named = env[NATIVE_HELPER_ENV]?.trim()
21
+ if (named) return named
22
+ const legacy = env[LEGACY_NATIVE_HELPER_ENV]?.trim()
23
+ return legacy || undefined
24
+ }
25
+
26
+ /** The helper path: the `helper` option when given (blank means none), else the environment's. */
27
+ export function resolveNativeHelper(
28
+ option: string | undefined,
29
+ env: NodeJS.ProcessEnv,
30
+ ): string | undefined {
31
+ if (option !== undefined) return option.trim() || undefined
32
+ return nativeHelperFromEnv(env)
33
+ }