@namzu/computer-use 4.4.0 → 4.5.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.
- package/CHANGELOG.md +16 -0
- package/dist/SubprocessComputerUseHost.d.ts +8 -1
- package/dist/SubprocessComputerUseHost.d.ts.map +1 -1
- package/dist/SubprocessComputerUseHost.js +15 -4
- package/dist/SubprocessComputerUseHost.js.map +1 -1
- package/dist/a11y/a11y.d.ts +15 -7
- package/dist/a11y/a11y.d.ts.map +1 -1
- package/dist/a11y/a11y.js +12 -5
- package/dist/a11y/a11y.js.map +1 -1
- package/dist/a11y/index.d.ts +7 -1
- package/dist/a11y/index.d.ts.map +1 -1
- package/dist/a11y/index.js +4 -1
- package/dist/a11y/index.js.map +1 -1
- package/dist/a11y/macos-helper.d.ts +80 -0
- package/dist/a11y/macos-helper.d.ts.map +1 -0
- package/dist/a11y/macos-helper.js +436 -0
- package/dist/a11y/macos-helper.js.map +1 -0
- package/dist/a11y/macos-signing.d.ts +56 -0
- package/dist/a11y/macos-signing.d.ts.map +1 -0
- package/dist/a11y/macos-signing.js +228 -0
- package/dist/a11y/macos-signing.js.map +1 -0
- package/dist/a11y/macos.d.ts +173 -0
- package/dist/a11y/macos.d.ts.map +1 -0
- package/dist/a11y/macos.js +271 -0
- package/dist/a11y/macos.js.map +1 -0
- package/dist/a11y/windows.d.ts +13 -2
- package/dist/a11y/windows.d.ts.map +1 -1
- package/dist/a11y/windows.js +29 -6
- package/dist/a11y/windows.js.map +1 -1
- package/dist/adapters/cua-driver/adapter.d.ts.map +1 -1
- package/dist/adapters/cua-driver/adapter.js +48 -10
- package/dist/adapters/cua-driver/adapter.js.map +1 -1
- package/dist/adapters/cua-driver/ui-tree.d.ts +1 -0
- package/dist/adapters/cua-driver/ui-tree.d.ts.map +1 -1
- package/dist/adapters/cua-driver/ui-tree.js +2 -0
- package/dist/adapters/cua-driver/ui-tree.js.map +1 -1
- package/dist/adapters/darwin-ax.d.ts +94 -0
- package/dist/adapters/darwin-ax.d.ts.map +1 -0
- package/dist/adapters/darwin-ax.js +754 -0
- package/dist/adapters/darwin-ax.js.map +1 -0
- package/dist/adapters/darwin.d.ts +43 -2
- package/dist/adapters/darwin.d.ts.map +1 -1
- package/dist/adapters/darwin.js +145 -7
- package/dist/adapters/darwin.js.map +1 -1
- package/dist/adapters/png.d.ts +6 -0
- package/dist/adapters/png.d.ts.map +1 -0
- package/dist/adapters/png.js +7 -0
- package/dist/adapters/png.js.map +1 -0
- package/dist/adapters/win32-native.d.ts +53 -0
- package/dist/adapters/win32-native.d.ts.map +1 -0
- package/dist/adapters/win32-native.js +279 -0
- package/dist/adapters/win32-native.js.map +1 -0
- package/dist/adapters/win32.d.ts +19 -1
- package/dist/adapters/win32.d.ts.map +1 -1
- package/dist/adapters/win32.js +34 -8
- package/dist/adapters/win32.js.map +1 -1
- package/dist/foreground/ladder.d.ts.map +1 -1
- package/dist/foreground/ladder.js +5 -1
- package/dist/foreground/ladder.js.map +1 -1
- package/dist/foreground/types.d.ts +6 -0
- package/dist/foreground/types.d.ts.map +1 -1
- package/dist/foreground/types.js.map +1 -1
- package/dist/foreground/windows.d.ts +2 -0
- package/dist/foreground/windows.d.ts.map +1 -1
- package/dist/foreground/windows.js +23 -11
- package/dist/foreground/windows.js.map +1 -1
- package/dist/native/a11y.d.ts +61 -0
- package/dist/native/a11y.d.ts.map +1 -0
- package/dist/native/a11y.js +197 -0
- package/dist/native/a11y.js.map +1 -0
- package/dist/native/foreground-monitor.d.ts +51 -0
- package/dist/native/foreground-monitor.d.ts.map +1 -0
- package/dist/native/foreground-monitor.js +120 -0
- package/dist/native/foreground-monitor.js.map +1 -0
- package/dist/native/presence.d.ts +24 -0
- package/dist/native/presence.d.ts.map +1 -0
- package/dist/native/presence.js +47 -0
- package/dist/native/presence.js.map +1 -0
- package/dist/native/process.d.ts +73 -0
- package/dist/native/process.d.ts.map +1 -0
- package/dist/native/process.js +292 -0
- package/dist/native/process.js.map +1 -0
- package/dist/native/protocol.d.ts +408 -0
- package/dist/native/protocol.d.ts.map +1 -0
- package/dist/native/protocol.js +527 -0
- package/dist/native/protocol.js.map +1 -0
- package/dist/native/shared.d.ts +16 -0
- package/dist/native/shared.d.ts.map +1 -0
- package/dist/native/shared.js +62 -0
- package/dist/native/shared.js.map +1 -0
- package/dist/presence/presence.d.ts +3 -2
- package/dist/presence/presence.d.ts.map +1 -1
- package/dist/presence/presence.js.map +1 -1
- package/dist/presence/windows.d.ts +2 -0
- package/dist/presence/windows.d.ts.map +1 -1
- package/dist/presence/windows.js +14 -0
- package/dist/presence/windows.js.map +1 -1
- package/helper-macos/main.swift +1756 -0
- package/package.json +3 -2
- package/src/SubprocessComputerUseHost.ts +26 -2
- package/src/a11y/a11y.ts +25 -11
- package/src/a11y/index.ts +28 -1
- package/src/a11y/macos-helper.ts +538 -0
- package/src/a11y/macos-signing.ts +280 -0
- package/src/a11y/macos.ts +496 -0
- package/src/a11y/windows.ts +29 -4
- package/src/adapters/cua-driver/adapter.ts +55 -15
- package/src/adapters/cua-driver/ui-tree.ts +2 -0
- package/src/adapters/darwin-ax.ts +920 -0
- package/src/adapters/darwin.ts +184 -4
- package/src/adapters/png.ts +5 -0
- package/src/adapters/win32-native.ts +328 -0
- package/src/adapters/win32.ts +52 -5
- package/src/foreground/ladder.ts +5 -1
- package/src/foreground/types.ts +6 -0
- package/src/foreground/windows.ts +24 -11
- package/src/native/a11y.ts +267 -0
- package/src/native/foreground-monitor.ts +165 -0
- package/src/native/presence.ts +58 -0
- package/src/native/process.ts +335 -0
- package/src/native/protocol.ts +963 -0
- package/src/native/shared.ts +75 -0
- package/src/presence/presence.ts +3 -2
- package/src/presence/windows.ts +14 -0
package/src/adapters/win32.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
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 type { NativeWinProcessOptions } from '../native/process.js'
|
|
4
6
|
import type { Presence } from '../presence/presence.js'
|
|
5
7
|
import { CuaDriverAdapter } from './cua-driver/adapter.js'
|
|
6
8
|
import {
|
|
@@ -9,6 +11,7 @@ import {
|
|
|
9
11
|
resolveCuaDriver,
|
|
10
12
|
} from './cua-driver/provision.js'
|
|
11
13
|
import { type Adapter, AdapterUnavailableError } from './types.js'
|
|
14
|
+
import { NativeWin32Adapter } from './win32-native.js'
|
|
12
15
|
import { Win32PowerShellAdapter } from './win32-powershell.js'
|
|
13
16
|
import { type WslProbes, isWsl, realWslProbes, wslChildEnv } from './wsl.js'
|
|
14
17
|
|
|
@@ -17,9 +20,14 @@ import { type WslProbes, isWsl, realWslProbes, wslChildEnv } from './wsl.js'
|
|
|
17
20
|
*
|
|
18
21
|
* - `auto` (the default): cua-driver — the pinned build, downloaded and
|
|
19
22
|
* checked on first use — and, when it cannot be had or does not answer,
|
|
20
|
-
* the per-action
|
|
23
|
+
* the per-action path, with the reason on `fallbackReason`.
|
|
21
24
|
* - `cua-driver`: cua-driver or nothing; its failure is the adapter's.
|
|
22
25
|
* - `powershell`: never cua-driver.
|
|
26
|
+
*
|
|
27
|
+
* The per-action path is the native helper (`namzu-win`) when one is
|
|
28
|
+
* configured (`NAMZU_WIN_HELPER`, or the `helper` option) and PowerShell
|
|
29
|
+
* otherwise. With a helper configured there is no PowerShell fallback: a
|
|
30
|
+
* helper that does not start is an error, not a reason to run scripts.
|
|
23
31
|
*/
|
|
24
32
|
export type Win32Backend = 'auto' | 'cua-driver' | 'powershell'
|
|
25
33
|
|
|
@@ -33,6 +41,14 @@ export interface Win32AdapterOptions {
|
|
|
33
41
|
readonly download?: boolean
|
|
34
42
|
/** Told once, in plain words, before the pinned build is downloaded. */
|
|
35
43
|
readonly onNotice?: (notice: string) => void
|
|
44
|
+
/**
|
|
45
|
+
* The path of the native helper, `namzu-win.exe`. Default: the
|
|
46
|
+
* `NAMZU_WIN_HELPER` variable. Set, the helper is the per-action path and
|
|
47
|
+
* PowerShell is never started for display input or capture.
|
|
48
|
+
*/
|
|
49
|
+
readonly helper?: string
|
|
50
|
+
/** Where the helper writes its pictures. Default: a private temporary directory. */
|
|
51
|
+
readonly captureDir?: string
|
|
36
52
|
readonly env?: NodeJS.ProcessEnv
|
|
37
53
|
readonly platform?: NodeJS.Platform
|
|
38
54
|
/**
|
|
@@ -49,6 +65,8 @@ export interface Win32AdapterOptions {
|
|
|
49
65
|
readonly wslProbes?: WslProbes
|
|
50
66
|
/** For tests. */
|
|
51
67
|
readonly resolve?: typeof resolveCuaDriver
|
|
68
|
+
/** For tests: starts the native helper's process. */
|
|
69
|
+
readonly spawnHelper?: NativeWinProcessOptions['spawnHelper']
|
|
52
70
|
/** For tests. */
|
|
53
71
|
readonly createCuaDriverAdapter?: (
|
|
54
72
|
options: ConstructorParameters<typeof CuaDriverAdapter>[0],
|
|
@@ -84,6 +102,19 @@ export function cuaDriverEnvironment(
|
|
|
84
102
|
return wsl ? wslChildEnv(base, Object.keys(CUA_DRIVER_SETTINGS), probes) : base
|
|
85
103
|
}
|
|
86
104
|
|
|
105
|
+
/** The environment the native helper runs with: the caller's minus anything named like a secret; under WSL, with a working `WSL_INTEROP`. */
|
|
106
|
+
export function nativeHelperEnvironment(
|
|
107
|
+
env: NodeJS.ProcessEnv,
|
|
108
|
+
wsl: boolean,
|
|
109
|
+
probes: WslProbes = realWslProbes,
|
|
110
|
+
): NodeJS.ProcessEnv {
|
|
111
|
+
const base: NodeJS.ProcessEnv = {}
|
|
112
|
+
for (const [name, value] of Object.entries(env)) {
|
|
113
|
+
if (!SECRET_NAME.test(name)) base[name] = value
|
|
114
|
+
}
|
|
115
|
+
return wsl ? wslChildEnv(base, [], probes) : base
|
|
116
|
+
}
|
|
117
|
+
|
|
87
118
|
/**
|
|
88
119
|
* `NAMZU_CUA_DRIVER`: `off` keeps cua-driver out (the PowerShell path);
|
|
89
120
|
* a path names the `cua-driver.exe` to run instead of the pinned build.
|
|
@@ -106,8 +137,23 @@ export class Win32Adapter {
|
|
|
106
137
|
const ladder = options.foreground
|
|
107
138
|
const guard = (adapter: Adapter): Adapter =>
|
|
108
139
|
ladder ? guardForeground(adapter, ladder) : adapter
|
|
109
|
-
const
|
|
110
|
-
|
|
140
|
+
const helper = (options.helper ?? env.NAMZU_WIN_HELPER)?.trim()
|
|
141
|
+
// The per-action path: the native helper when one is named, else PowerShell.
|
|
142
|
+
// A helper that does not start throws; it never turns into PowerShell.
|
|
143
|
+
const perAction = async (fallbackReason?: string) => {
|
|
144
|
+
if (helper) {
|
|
145
|
+
const wsl = isWsl(env, platform, probes)
|
|
146
|
+
return guard(
|
|
147
|
+
await NativeWin32Adapter.create({
|
|
148
|
+
executable: helper,
|
|
149
|
+
env: nativeHelperEnvironment(env, wsl, probes),
|
|
150
|
+
...(options.captureDir ? { captureDir: options.captureDir } : {}),
|
|
151
|
+
...(wsl ? { toHelperPath: (path: string) => windowsPathFor(path, env) } : {}),
|
|
152
|
+
...(options.spawnHelper ? { spawnHelper: options.spawnHelper } : {}),
|
|
153
|
+
}),
|
|
154
|
+
)
|
|
155
|
+
}
|
|
156
|
+
return guard(
|
|
111
157
|
await Win32PowerShellAdapter.create({
|
|
112
158
|
env,
|
|
113
159
|
platform,
|
|
@@ -115,7 +161,8 @@ export class Win32Adapter {
|
|
|
115
161
|
fallbackReason,
|
|
116
162
|
}),
|
|
117
163
|
)
|
|
118
|
-
|
|
164
|
+
}
|
|
165
|
+
if (backend === 'powershell') return perAction()
|
|
119
166
|
|
|
120
167
|
const cuaDriverPath = options.cuaDriverPath ?? configured.path
|
|
121
168
|
let adapter: Adapter | undefined
|
|
@@ -149,7 +196,7 @@ export class Win32Adapter {
|
|
|
149
196
|
if (backend === 'cua-driver') {
|
|
150
197
|
throw new AdapterUnavailableError(`Win32Adapter: cua-driver is not usable: ${reason}`)
|
|
151
198
|
}
|
|
152
|
-
return
|
|
199
|
+
return perAction(reason)
|
|
153
200
|
}
|
|
154
201
|
}
|
|
155
202
|
}
|
package/src/foreground/ladder.ts
CHANGED
|
@@ -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
|
|
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
|
package/src/foreground/types.ts
CHANGED
|
@@ -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,7 @@ import {
|
|
|
7
7
|
wslChildEnv,
|
|
8
8
|
wslPowerShellPath,
|
|
9
9
|
} from '../adapters/wsl.js'
|
|
10
|
+
import { NativeForegroundMonitor } from '../native/foreground-monitor.js'
|
|
10
11
|
import { ForegroundLadder, type LadderTiming } from './ladder.js'
|
|
11
12
|
import type { ForegroundApprover } from './types.js'
|
|
12
13
|
import { WindowsInputMonitor } from './windows-monitor.js'
|
|
@@ -21,6 +22,8 @@ export interface WindowsLadderOptions {
|
|
|
21
22
|
readonly approve?: ForegroundApprover
|
|
22
23
|
readonly session?: string
|
|
23
24
|
readonly timing?: Partial<LadderTiming>
|
|
25
|
+
/** The native helper (`namzu-win.exe`); default `NAMZU_WIN_HELPER`. Named, the monitor is the helper and no PowerShell runs. */
|
|
26
|
+
readonly helper?: string
|
|
24
27
|
}
|
|
25
28
|
|
|
26
29
|
/** `<NAMZU_HOME>/computer-use/screen.lease`, `NAMZU_HOME` defaulting to `~/.namzu`. */
|
|
@@ -48,12 +51,16 @@ export function createWindowsForegroundLadder(
|
|
|
48
51
|
const platform = options.platform ?? process.platform
|
|
49
52
|
const probes = options.wslProbes ?? realWslProbes
|
|
50
53
|
const wsl = isWsl(env, platform, probes)
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
const native = (options.helper ?? env.NAMZU_WIN_HELPER)?.trim()
|
|
55
|
+
const powershell = native
|
|
56
|
+
? undefined
|
|
57
|
+
: wsl
|
|
58
|
+
? wslPowerShellPath(probes)
|
|
59
|
+
: platform === 'win32'
|
|
60
|
+
? 'powershell.exe'
|
|
61
|
+
: undefined
|
|
62
|
+
if (!native && !powershell) return undefined
|
|
63
|
+
if (native && !wsl && platform !== 'win32') return undefined
|
|
57
64
|
const clean: NodeJS.ProcessEnv = {}
|
|
58
65
|
for (const [name, value] of Object.entries(env)) if (!SECRET_NAME.test(name)) clean[name] = value
|
|
59
66
|
const helperEnv = wsl ? wslChildEnv(clean, [], probes) : clean
|
|
@@ -61,11 +68,17 @@ export function createWindowsForegroundLadder(
|
|
|
61
68
|
return new ForegroundLadder({
|
|
62
69
|
leasePath,
|
|
63
70
|
createMonitor: () =>
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
71
|
+
native
|
|
72
|
+
? new NativeForegroundMonitor({
|
|
73
|
+
executable: native,
|
|
74
|
+
env: helperEnv,
|
|
75
|
+
toHelperPath: (path) => windowsPathFor(path, env),
|
|
76
|
+
})
|
|
77
|
+
: new WindowsInputMonitor({
|
|
78
|
+
powershell: powershell as string,
|
|
79
|
+
env: helperEnv,
|
|
80
|
+
toWindowsPath: (path) => windowsPathFor(path, env),
|
|
81
|
+
}),
|
|
69
82
|
...(options.approve ? { approve: options.approve } : {}),
|
|
70
83
|
...(options.session ? { session: options.session } : {}),
|
|
71
84
|
...(options.timing ? { timing: options.timing } : {}),
|
|
@@ -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,165 @@
|
|
|
1
|
+
import { readFileSync, unlinkSync } from 'node:fs'
|
|
2
|
+
import type { ForegroundMonitor, MonitorBurstEnd, MonitorState } from '../foreground/types.js'
|
|
3
|
+
import type { NativeWinProcessOptions } from './process.js'
|
|
4
|
+
import type { NativeForegroundState, NativeMonitorPollReply } from './protocol.js'
|
|
5
|
+
import { type NativeHelper, shareNativeProcess } from './shared.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The foreground monitor on the native helper: it watches for a person's use
|
|
9
|
+
* of keyboard and mouse and for the window in front, without a global hook.
|
|
10
|
+
*
|
|
11
|
+
* The helper learns of input through Raw Input headers, which say that a
|
|
12
|
+
* device class sent something and when, never which key or button; what comes
|
|
13
|
+
* back here is a count. `escapes` is therefore always 0: the helper does not
|
|
14
|
+
* read keys, so it cannot tell Escape from any other key. A person who wants
|
|
15
|
+
* to stop a burst by using the machine does so by input, which the count sees.
|
|
16
|
+
*
|
|
17
|
+
* It replaces the PowerShell monitor (`WindowsInputMonitor`), which installed
|
|
18
|
+
* system-wide low-level keyboard and mouse hooks.
|
|
19
|
+
*/
|
|
20
|
+
export interface NativeForegroundMonitorOptions extends NativeWinProcessOptions {
|
|
21
|
+
/** The path as the helper reads it (under WSL a Linux path goes through the distro's share). Default: unchanged. */
|
|
22
|
+
readonly toHelperPath?: (path: string) => string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const REQUEST_MS = 5_000
|
|
26
|
+
|
|
27
|
+
/** `0x1a2b` and `w:000000000000_1a2b` name the same window. */
|
|
28
|
+
function windowIdOf(hwnd: string): string {
|
|
29
|
+
const hex = hwnd.replace(/^0x/i, '').toLowerCase()
|
|
30
|
+
return `w:${hex.padStart(16, '0')}`
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function hwndOf(windowId: string): string {
|
|
34
|
+
const m = /^w:([0-9a-f]+)/i.exec(windowId)
|
|
35
|
+
return `0x${(m?.[1] ?? '0').replace(/^0+(?=.)/, '').toUpperCase()}`
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export class NativeForegroundMonitor implements ForegroundMonitor {
|
|
39
|
+
private readonly process: NativeHelper
|
|
40
|
+
private started = false
|
|
41
|
+
|
|
42
|
+
constructor(private readonly options: NativeForegroundMonitorOptions) {
|
|
43
|
+
this.process = shareNativeProcess(options)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get running(): boolean {
|
|
47
|
+
return this.started && this.process.isUp
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async start(): Promise<void> {
|
|
51
|
+
if (this.started && this.process.isUp) return
|
|
52
|
+
if (!(await this.process.ready())) throw new Error('the native Windows helper did not start')
|
|
53
|
+
const caps = this.process.hello?.caps ?? []
|
|
54
|
+
if (!caps.includes('monitor')) {
|
|
55
|
+
throw new Error('the native Windows helper in use cannot watch for a person at the keyboard')
|
|
56
|
+
}
|
|
57
|
+
const watching = await this.process.request({ op: 'monitor.start' }, REQUEST_MS)
|
|
58
|
+
if (!watching.input) {
|
|
59
|
+
throw new Error('the native Windows helper could not register for keyboard and mouse use')
|
|
60
|
+
}
|
|
61
|
+
this.started = true
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async state(query: { readonly pid?: number } = {}): Promise<MonitorState> {
|
|
65
|
+
const [fg, poll] = await Promise.all([
|
|
66
|
+
this.process.request(
|
|
67
|
+
{ op: 'foreground.get', ...(query.pid !== undefined ? { processId: query.pid } : {}) },
|
|
68
|
+
REQUEST_MS,
|
|
69
|
+
),
|
|
70
|
+
this.process.request({ op: 'monitor.poll' }, REQUEST_MS),
|
|
71
|
+
])
|
|
72
|
+
return stateOf(fg, poll)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async begin(args: {
|
|
76
|
+
readonly leasePath: string
|
|
77
|
+
readonly token: string
|
|
78
|
+
readonly raise?: string
|
|
79
|
+
readonly watch?: string
|
|
80
|
+
readonly idleMs?: number
|
|
81
|
+
}): Promise<MonitorState & { readonly raised?: boolean; readonly refused?: boolean }> {
|
|
82
|
+
const to = this.options.toHelperPath ?? ((p: string) => p)
|
|
83
|
+
const reply = await this.process.request(
|
|
84
|
+
{
|
|
85
|
+
op: 'lease.begin',
|
|
86
|
+
...(args.raise !== undefined
|
|
87
|
+
? { windowId: windowIdOf(args.raise), raise: true }
|
|
88
|
+
: args.watch !== undefined
|
|
89
|
+
? { windowId: windowIdOf(args.watch) }
|
|
90
|
+
: {}),
|
|
91
|
+
...(args.idleMs !== undefined ? { idleMs: args.idleMs } : {}),
|
|
92
|
+
leasePath: to(args.leasePath),
|
|
93
|
+
token: args.token,
|
|
94
|
+
},
|
|
95
|
+
REQUEST_MS,
|
|
96
|
+
)
|
|
97
|
+
const poll = await this.process.request({ op: 'monitor.poll' }, REQUEST_MS)
|
|
98
|
+
return {
|
|
99
|
+
...stateOf(reply, poll),
|
|
100
|
+
refused: reply.refused,
|
|
101
|
+
...(reply.raised !== undefined ? { raised: reply.raised } : {}),
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async end(args: {
|
|
106
|
+
readonly restore: boolean
|
|
107
|
+
readonly keepCursor?: boolean
|
|
108
|
+
}): Promise<MonitorBurstEnd> {
|
|
109
|
+
const reply = await this.process.request(
|
|
110
|
+
{
|
|
111
|
+
op: 'lease.end',
|
|
112
|
+
restore: args.restore,
|
|
113
|
+
...(args.keepCursor ? { keepCursor: true } : {}),
|
|
114
|
+
},
|
|
115
|
+
REQUEST_MS,
|
|
116
|
+
)
|
|
117
|
+
return {
|
|
118
|
+
inputsDuring: reply.inputsDuring ?? 0,
|
|
119
|
+
escapesDuring: 0,
|
|
120
|
+
restoredForeground: reply.restoredForeground,
|
|
121
|
+
restoredCursor: reply.restoredCursor,
|
|
122
|
+
...(reply.restoreNote !== undefined ? { restoreNote: reply.restoreNote } : {}),
|
|
123
|
+
inputMoved: reply.inputMoved,
|
|
124
|
+
frontElevated: reply.frontElevated,
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Deletes the lease file when its token still matches. */
|
|
129
|
+
async release(args: { readonly leasePath: string; readonly token: string }): Promise<void> {
|
|
130
|
+
try {
|
|
131
|
+
const record = JSON.parse(readFileSync(args.leasePath, 'utf8')) as { token?: unknown }
|
|
132
|
+
if (record.token === args.token) unlinkSync(args.leasePath)
|
|
133
|
+
} catch {
|
|
134
|
+
// No file, or not ours: nothing to release.
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async dispose(): Promise<void> {
|
|
139
|
+
this.started = false
|
|
140
|
+
await this.process.close()
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function stateOf(fg: NativeForegroundState, poll: NativeMonitorPollReply): MonitorState {
|
|
145
|
+
return {
|
|
146
|
+
userIdleMs: fg.userIdleMs,
|
|
147
|
+
userInputs: poll.inputs,
|
|
148
|
+
escapes: 0,
|
|
149
|
+
foreground: fg.foreground
|
|
150
|
+
? {
|
|
151
|
+
hwnd: hwndOf(fg.foreground.windowId),
|
|
152
|
+
pid: fg.foreground.processId,
|
|
153
|
+
exe: fg.foreground.exe,
|
|
154
|
+
elevated: fg.foreground.elevated,
|
|
155
|
+
integrity: fg.foreground.integrity,
|
|
156
|
+
}
|
|
157
|
+
: null,
|
|
158
|
+
cursor: fg.cursor,
|
|
159
|
+
desktop: fg.desktop,
|
|
160
|
+
selfElevated: fg.selfElevated,
|
|
161
|
+
selfIntegrity: fg.selfIntegrity,
|
|
162
|
+
inputTick: fg.inputTick,
|
|
163
|
+
...(fg.target ? { target: fg.target } : {}),
|
|
164
|
+
}
|
|
165
|
+
}
|