@namzu/computer-use 4.1.1 → 4.2.1
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 +12 -0
- package/dist/SubprocessComputerUseHost.d.ts +18 -1
- package/dist/SubprocessComputerUseHost.d.ts.map +1 -1
- package/dist/SubprocessComputerUseHost.js +28 -2
- package/dist/SubprocessComputerUseHost.js.map +1 -1
- package/dist/a11y/a11y-script.d.ts +47 -0
- package/dist/a11y/a11y-script.d.ts.map +1 -0
- package/dist/a11y/a11y-script.js +738 -0
- package/dist/a11y/a11y-script.js.map +1 -0
- package/dist/a11y/a11y.d.ts +60 -0
- package/dist/a11y/a11y.d.ts.map +1 -0
- package/dist/a11y/a11y.js +400 -0
- package/dist/a11y/a11y.js.map +1 -0
- package/dist/a11y/index.d.ts +8 -0
- package/dist/a11y/index.d.ts.map +1 -0
- package/dist/a11y/index.js +5 -0
- package/dist/a11y/index.js.map +1 -0
- package/dist/a11y/interface.d.ts +198 -0
- package/dist/a11y/interface.d.ts.map +1 -0
- package/dist/a11y/interface.js +22 -0
- package/dist/a11y/interface.js.map +1 -0
- package/dist/a11y/windows.d.ts +20 -0
- package/dist/a11y/windows.d.ts.map +1 -0
- package/dist/a11y/windows.js +40 -0
- package/dist/a11y/windows.js.map +1 -0
- package/dist/adapters/cua-driver/adapter.d.ts +47 -0
- package/dist/adapters/cua-driver/adapter.d.ts.map +1 -1
- package/dist/adapters/cua-driver/adapter.js +141 -3
- package/dist/adapters/cua-driver/adapter.js.map +1 -1
- package/dist/adapters/types.d.ts +11 -0
- package/dist/adapters/types.d.ts.map +1 -1
- package/dist/adapters/types.js.map +1 -1
- package/dist/adapters/win32.d.ts +3 -0
- package/dist/adapters/win32.d.ts.map +1 -1
- package/dist/adapters/win32.js +1 -0
- package/dist/adapters/win32.js.map +1 -1
- package/dist/foreground/guard.d.ts.map +1 -1
- package/dist/foreground/guard.js +1 -0
- package/dist/foreground/guard.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/SubprocessComputerUseHost.ts +48 -1
- package/src/a11y/a11y-script.ts +740 -0
- package/src/a11y/a11y.ts +499 -0
- package/src/a11y/index.ts +24 -0
- package/src/a11y/interface.ts +226 -0
- package/src/a11y/windows.ts +56 -0
- package/src/adapters/cua-driver/adapter.ts +154 -6
- package/src/adapters/types.ts +8 -0
- package/src/adapters/win32.ts +4 -0
- package/src/foreground/guard.ts +1 -0
- package/src/index.ts +19 -0
package/src/a11y/a11y.ts
ADDED
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import { createInterface } from 'node:readline'
|
|
3
|
+
import { A11Y_BOOTSTRAP, a11yScriptLine } from './a11y-script.js'
|
|
4
|
+
import {
|
|
5
|
+
type A11yAction,
|
|
6
|
+
type A11yChangeKind,
|
|
7
|
+
type A11yDisplay,
|
|
8
|
+
A11yError,
|
|
9
|
+
type A11yFingerprint,
|
|
10
|
+
type A11yIdentityRequest,
|
|
11
|
+
type A11yNode,
|
|
12
|
+
type A11yRect,
|
|
13
|
+
type A11yResolution,
|
|
14
|
+
type A11yResolveRequest,
|
|
15
|
+
type A11ySeq,
|
|
16
|
+
type A11ySnapshot,
|
|
17
|
+
type A11ySnapshotRequest,
|
|
18
|
+
type A11yWaitRequest,
|
|
19
|
+
type A11yWaitResult,
|
|
20
|
+
type A11yWindow,
|
|
21
|
+
type AccessibilityBackend,
|
|
22
|
+
} from './interface.js'
|
|
23
|
+
|
|
24
|
+
/** The ends of a started helper; the real one is a child process. */
|
|
25
|
+
export interface A11yProcess {
|
|
26
|
+
write(line: string): void
|
|
27
|
+
onLine(handler: (line: string) => void): void
|
|
28
|
+
onExit(handler: () => void): void
|
|
29
|
+
kill(): void
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface WindowsA11yOptions {
|
|
33
|
+
/** Windows PowerShell, as this process starts it. */
|
|
34
|
+
readonly powershell: string
|
|
35
|
+
readonly env?: NodeJS.ProcessEnv
|
|
36
|
+
/** For tests. */
|
|
37
|
+
readonly spawnHelper?: () => A11yProcess
|
|
38
|
+
/** Waiting for the helper to start (it compiles a small library), ms. Default 30 000. */
|
|
39
|
+
readonly startTimeoutMs?: number
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function spawnPowerShell(options: WindowsA11yOptions): A11yProcess {
|
|
43
|
+
const child = spawn(
|
|
44
|
+
options.powershell,
|
|
45
|
+
[
|
|
46
|
+
'-NoProfile',
|
|
47
|
+
'-NonInteractive',
|
|
48
|
+
'-ExecutionPolicy',
|
|
49
|
+
'Bypass',
|
|
50
|
+
'-EncodedCommand',
|
|
51
|
+
Buffer.from(A11Y_BOOTSTRAP, 'utf16le').toString('base64'),
|
|
52
|
+
],
|
|
53
|
+
{ env: options.env, stdio: ['pipe', 'pipe', 'ignore'], windowsHide: true },
|
|
54
|
+
)
|
|
55
|
+
child.stdin.on('error', () => undefined)
|
|
56
|
+
child.on('error', () => undefined)
|
|
57
|
+
return {
|
|
58
|
+
write: (line) => {
|
|
59
|
+
child.stdin.write(`${line}\n`)
|
|
60
|
+
},
|
|
61
|
+
onLine: (handler) => createInterface({ input: child.stdout }).on('line', handler),
|
|
62
|
+
onExit: (handler) => {
|
|
63
|
+
child.once('exit', handler)
|
|
64
|
+
child.once('error', handler)
|
|
65
|
+
},
|
|
66
|
+
kill: () => {
|
|
67
|
+
try {
|
|
68
|
+
// Closing the input ends the helper by itself.
|
|
69
|
+
child.stdin.end()
|
|
70
|
+
} catch {
|
|
71
|
+
// Already closed.
|
|
72
|
+
}
|
|
73
|
+
setTimeout(() => child.kill(), 2_000).unref()
|
|
74
|
+
},
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** How many times a helper that ended mid-session is started again. */
|
|
79
|
+
const MAX_RESTARTS = 1
|
|
80
|
+
/** The protocol revision this client speaks. */
|
|
81
|
+
const PROTOCOL_VERSION = 1
|
|
82
|
+
/** Tokens carry the helper run they came from, so one from before a restart is refused, not misread. */
|
|
83
|
+
const EPOCH_SPAN = 2 ** 40
|
|
84
|
+
/** The client gives up on a reply this long after the helper should have sent it, ms. */
|
|
85
|
+
const BACKSTOP_MS = 1_500
|
|
86
|
+
|
|
87
|
+
const DEFAULT_REQUEST_MS = 5_000
|
|
88
|
+
const DEFAULT_SNAPSHOT_MS = 8_000
|
|
89
|
+
const DEFAULT_WAIT_MS = 2_000
|
|
90
|
+
const ARM_MS = 9_000
|
|
91
|
+
|
|
92
|
+
interface Pending {
|
|
93
|
+
readonly resolve: (reply: Record<string, unknown>) => void
|
|
94
|
+
readonly reject: (error: A11yError) => void
|
|
95
|
+
readonly timer: NodeJS.Timeout
|
|
96
|
+
readonly cleanup: () => void
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** The helper's window id (an HWND) → the host's string, the same `0x…` form the adapter uses. */
|
|
100
|
+
const windowIdOf = (hwnd: number): string => `0x${hwnd.toString(16)}`
|
|
101
|
+
|
|
102
|
+
function hwndOf(windowId: string): number {
|
|
103
|
+
const text = windowId.trim().toLowerCase()
|
|
104
|
+
const value = /^0x[0-9a-f]+$/.test(text)
|
|
105
|
+
? Number.parseInt(text.slice(2), 16)
|
|
106
|
+
: /^\d+$/.test(text)
|
|
107
|
+
? Number.parseInt(text, 10)
|
|
108
|
+
: Number.NaN
|
|
109
|
+
if (!Number.isSafeInteger(value) || value <= 0)
|
|
110
|
+
throw new A11yError('bad_window_id', 'not a window id this backend handed out')
|
|
111
|
+
return value
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const num = (value: unknown, fallback = 0): number =>
|
|
115
|
+
typeof value === 'number' && Number.isFinite(value) ? value : fallback
|
|
116
|
+
const str = (value: unknown): string => (typeof value === 'string' ? value : '')
|
|
117
|
+
const records = (value: unknown): Record<string, unknown>[] =>
|
|
118
|
+
Array.isArray(value)
|
|
119
|
+
? value.filter((v): v is Record<string, unknown> => typeof v === 'object' && v !== null)
|
|
120
|
+
: []
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* {@link AccessibilityBackend} on a Windows desktop (WSL included), answered
|
|
124
|
+
* by one long-lived PowerShell helper that starts on first use and is
|
|
125
|
+
* reused: a question is one line written to it, not a process.
|
|
126
|
+
*
|
|
127
|
+
* It never throws from `ready()`. Everything else rejects with an
|
|
128
|
+
* {@link A11yError} when the helper cannot answer, and the caller is
|
|
129
|
+
* expected to carry on without it (the adapter falls back to its driver).
|
|
130
|
+
* A helper that ends mid-session is started once more by the next question;
|
|
131
|
+
* a second end, or a helper that never came up, leaves it off.
|
|
132
|
+
*/
|
|
133
|
+
export class WindowsA11yHost implements AccessibilityBackend {
|
|
134
|
+
readonly id = 'windows-uia'
|
|
135
|
+
|
|
136
|
+
private proc: A11yProcess | undefined
|
|
137
|
+
private starting: Promise<boolean> | undefined
|
|
138
|
+
private up = false
|
|
139
|
+
private dead = false
|
|
140
|
+
private restarts = 0
|
|
141
|
+
private epoch = 0
|
|
142
|
+
private closed = false
|
|
143
|
+
private nextId = 1
|
|
144
|
+
private readonly pending = new Map<number, Pending>()
|
|
145
|
+
|
|
146
|
+
constructor(private readonly options: WindowsA11yOptions) {}
|
|
147
|
+
|
|
148
|
+
get isReady(): boolean {
|
|
149
|
+
return this.up && !this.dead && !this.closed
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
warm(): void {
|
|
153
|
+
if (!this.dead && !this.closed) void this.start()
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
ready(): Promise<boolean> {
|
|
157
|
+
if (this.dead || this.closed) return Promise.resolve(false)
|
|
158
|
+
return this.start()
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async listWindows(
|
|
162
|
+
options: { readonly includeOwned?: boolean } = {},
|
|
163
|
+
signal?: AbortSignal,
|
|
164
|
+
): Promise<readonly A11yWindow[]> {
|
|
165
|
+
const reply = await this.request(
|
|
166
|
+
'windows',
|
|
167
|
+
options.includeOwned ? { includeOwned: true } : {},
|
|
168
|
+
DEFAULT_REQUEST_MS,
|
|
169
|
+
signal,
|
|
170
|
+
)
|
|
171
|
+
return records(reply.windows).map((w) => ({
|
|
172
|
+
windowId: windowIdOf(num(w.hwnd)),
|
|
173
|
+
processId: num(w.pid),
|
|
174
|
+
processName: str(w.process),
|
|
175
|
+
title: str(w.title),
|
|
176
|
+
bounds: { x: num(w.x), y: num(w.y), width: num(w.width), height: num(w.height) },
|
|
177
|
+
minimized: w.minimized === true,
|
|
178
|
+
focused: w.focused === true,
|
|
179
|
+
z: num(w.z),
|
|
180
|
+
}))
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async listDisplays(signal?: AbortSignal): Promise<readonly A11yDisplay[]> {
|
|
184
|
+
const reply = await this.request('displays', {}, DEFAULT_REQUEST_MS, signal)
|
|
185
|
+
return records(reply.displays).map((d) => ({
|
|
186
|
+
id: str(d.id),
|
|
187
|
+
bounds: { x: num(d.x), y: num(d.y), width: num(d.width), height: num(d.height) },
|
|
188
|
+
workArea: {
|
|
189
|
+
x: num(d.workX),
|
|
190
|
+
y: num(d.workY),
|
|
191
|
+
width: num(d.workWidth),
|
|
192
|
+
height: num(d.workHeight),
|
|
193
|
+
},
|
|
194
|
+
scale: num(d.scale, 1),
|
|
195
|
+
primary: d.primary === true,
|
|
196
|
+
}))
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async snapshot(request: A11ySnapshotRequest, signal?: AbortSignal): Promise<A11ySnapshot> {
|
|
200
|
+
const hwnd = hwndOf(request.windowId)
|
|
201
|
+
const timeoutMs = request.timeoutMs ?? DEFAULT_SNAPSHOT_MS
|
|
202
|
+
const reply = await this.request(
|
|
203
|
+
'snapshot',
|
|
204
|
+
{
|
|
205
|
+
hwnd,
|
|
206
|
+
timeoutMs,
|
|
207
|
+
...(request.maxNodes !== undefined ? { maxNodes: request.maxNodes } : {}),
|
|
208
|
+
...(request.maxDepth !== undefined ? { maxDepth: request.maxDepth } : {}),
|
|
209
|
+
...(request.skeleton ? { skeleton: true } : {}),
|
|
210
|
+
...(request.allowChromium ? { allowChromium: true } : {}),
|
|
211
|
+
},
|
|
212
|
+
timeoutMs,
|
|
213
|
+
signal,
|
|
214
|
+
)
|
|
215
|
+
return {
|
|
216
|
+
windowId: windowIdOf(hwnd),
|
|
217
|
+
revision: num(reply.rev),
|
|
218
|
+
nodes: records(reply.nodes).map(toNode),
|
|
219
|
+
total: num(reply.total),
|
|
220
|
+
truncated: reply.truncated === true,
|
|
221
|
+
skeleton: reply.skeleton === true,
|
|
222
|
+
readMs: num(reply.readMs),
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async seq(windowId?: string, signal?: AbortSignal): Promise<A11ySeq> {
|
|
227
|
+
const reply = await this.request(
|
|
228
|
+
'seq',
|
|
229
|
+
windowId === undefined ? {} : { hwnd: hwndOf(windowId) },
|
|
230
|
+
windowId === undefined ? DEFAULT_REQUEST_MS : ARM_MS,
|
|
231
|
+
signal,
|
|
232
|
+
)
|
|
233
|
+
return this.epoch * EPOCH_SPAN + num(reply.seq)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async waitForChange(request: A11yWaitRequest, signal?: AbortSignal): Promise<A11yWaitResult> {
|
|
237
|
+
const hwnd = hwndOf(request.windowId)
|
|
238
|
+
const timeoutMs = request.timeoutMs ?? DEFAULT_WAIT_MS
|
|
239
|
+
let since: number | undefined
|
|
240
|
+
if (request.since !== undefined) {
|
|
241
|
+
if (Math.floor(request.since / EPOCH_SPAN) !== this.epoch)
|
|
242
|
+
throw new A11yError('stale_seq', 'the helper restarted since this marker was taken')
|
|
243
|
+
since = request.since % EPOCH_SPAN
|
|
244
|
+
}
|
|
245
|
+
const reply = await this.request(
|
|
246
|
+
'wait_change',
|
|
247
|
+
{
|
|
248
|
+
hwnd,
|
|
249
|
+
timeoutMs,
|
|
250
|
+
...(request.quietMs !== undefined ? { quietMs: request.quietMs } : {}),
|
|
251
|
+
...(since !== undefined ? { since } : {}),
|
|
252
|
+
...(request.kinds ? { kinds: [...request.kinds] satisfies A11yChangeKind[] } : {}),
|
|
253
|
+
},
|
|
254
|
+
// The helper answers within the wait plus a few ms; the client's own limit is the backstop.
|
|
255
|
+
Math.min(timeoutMs, 10_000),
|
|
256
|
+
signal,
|
|
257
|
+
)
|
|
258
|
+
if (reply.changed === true)
|
|
259
|
+
return {
|
|
260
|
+
changed: true,
|
|
261
|
+
settled: reply.settled === true,
|
|
262
|
+
firstEventMs: num(reply.firstEventMs),
|
|
263
|
+
settledMs: num(reply.settledMs),
|
|
264
|
+
events: num(reply.events),
|
|
265
|
+
newWindows: (Array.isArray(reply.newWindows) ? reply.newWindows : [])
|
|
266
|
+
.filter((h): h is number => typeof h === 'number')
|
|
267
|
+
.map(windowIdOf),
|
|
268
|
+
}
|
|
269
|
+
return { changed: false, timedOut: true, settledMs: num(reply.settledMs) }
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
async identity(request: A11yIdentityRequest, signal?: AbortSignal): Promise<A11yFingerprint> {
|
|
273
|
+
const reply = await this.request(
|
|
274
|
+
'identity',
|
|
275
|
+
{
|
|
276
|
+
hwnd: hwndOf(request.windowId),
|
|
277
|
+
index: request.index,
|
|
278
|
+
...(request.revision !== undefined ? { rev: request.revision } : {}),
|
|
279
|
+
},
|
|
280
|
+
DEFAULT_SNAPSHOT_MS,
|
|
281
|
+
signal,
|
|
282
|
+
)
|
|
283
|
+
const f = (reply.fingerprint ?? {}) as Record<string, unknown>
|
|
284
|
+
return {
|
|
285
|
+
runtimeId: str(f.runtimeId),
|
|
286
|
+
automationId: str(f.automationId),
|
|
287
|
+
role: str(f.role),
|
|
288
|
+
nameHash: str(f.nameHash),
|
|
289
|
+
parentPath: str(f.parentPath),
|
|
290
|
+
ordinal: num(f.ordinal),
|
|
291
|
+
processStart: num(f.processStart),
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
async resolve(request: A11yResolveRequest, signal?: AbortSignal): Promise<A11yResolution> {
|
|
296
|
+
const reply = await this.request(
|
|
297
|
+
'resolve',
|
|
298
|
+
{ hwnd: hwndOf(request.windowId), fingerprint: request.fingerprint },
|
|
299
|
+
DEFAULT_SNAPSHOT_MS,
|
|
300
|
+
signal,
|
|
301
|
+
)
|
|
302
|
+
if (reply.found === true) {
|
|
303
|
+
const via = reply.via
|
|
304
|
+
return {
|
|
305
|
+
found: true,
|
|
306
|
+
index: num(reply.index),
|
|
307
|
+
via: via === 'runtimeId' || via === 'automationId' ? via : 'name',
|
|
308
|
+
revision: num(reply.rev),
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return {
|
|
312
|
+
found: false,
|
|
313
|
+
code: reply.code === 'ambiguous' ? 'ambiguous' : 'not_found',
|
|
314
|
+
...(typeof reply.candidates === 'number' ? { candidates: reply.candidates } : {}),
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
async close(): Promise<void> {
|
|
319
|
+
this.closed = true
|
|
320
|
+
const proc = this.proc
|
|
321
|
+
this.proc = undefined
|
|
322
|
+
this.up = false
|
|
323
|
+
this.failAll(new A11yError('closed', 'the accessibility host was closed'))
|
|
324
|
+
if (proc) {
|
|
325
|
+
try {
|
|
326
|
+
proc.write(JSON.stringify({ id: 0, cmd: 'quit' }))
|
|
327
|
+
} catch {
|
|
328
|
+
// Already gone.
|
|
329
|
+
}
|
|
330
|
+
proc.kill()
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
private failAll(error: A11yError): void {
|
|
335
|
+
for (const [id, entry] of [...this.pending]) {
|
|
336
|
+
this.pending.delete(id)
|
|
337
|
+
clearTimeout(entry.timer)
|
|
338
|
+
entry.cleanup()
|
|
339
|
+
entry.reject(error)
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
private async request(
|
|
344
|
+
cmd: string,
|
|
345
|
+
payload: Record<string, unknown>,
|
|
346
|
+
limitMs: number,
|
|
347
|
+
signal?: AbortSignal,
|
|
348
|
+
): Promise<Record<string, unknown>> {
|
|
349
|
+
if (signal?.aborted) throw new A11yError('aborted', 'cancelled')
|
|
350
|
+
if (!(await this.ready())) throw new A11yError('unavailable', 'the accessibility host is off')
|
|
351
|
+
const proc = this.proc
|
|
352
|
+
if (!proc || !this.up) throw new A11yError('helper_exited', 'the accessibility host ended')
|
|
353
|
+
const id = this.nextId++
|
|
354
|
+
return new Promise<Record<string, unknown>>((resolve, reject) => {
|
|
355
|
+
const onAbort = (): void => {
|
|
356
|
+
if (this.pending.delete(id)) {
|
|
357
|
+
clearTimeout(timer)
|
|
358
|
+
reject(new A11yError('aborted', 'cancelled'))
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
const timer = setTimeout(() => {
|
|
362
|
+
if (this.pending.delete(id)) {
|
|
363
|
+
signal?.removeEventListener('abort', onAbort)
|
|
364
|
+
reject(new A11yError('timeout', `the accessibility host did not answer ${cmd}`))
|
|
365
|
+
}
|
|
366
|
+
}, limitMs + BACKSTOP_MS)
|
|
367
|
+
timer.unref?.()
|
|
368
|
+
signal?.addEventListener('abort', onAbort, { once: true })
|
|
369
|
+
this.pending.set(id, {
|
|
370
|
+
resolve,
|
|
371
|
+
reject,
|
|
372
|
+
timer,
|
|
373
|
+
cleanup: () => signal?.removeEventListener('abort', onAbort),
|
|
374
|
+
})
|
|
375
|
+
proc.write(JSON.stringify({ id, cmd, ...payload }))
|
|
376
|
+
})
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
private onReply(line: string): void {
|
|
380
|
+
let reply: Record<string, unknown>
|
|
381
|
+
try {
|
|
382
|
+
reply = JSON.parse(line) as Record<string, unknown>
|
|
383
|
+
} catch {
|
|
384
|
+
return
|
|
385
|
+
}
|
|
386
|
+
const id = reply.id
|
|
387
|
+
if (typeof id !== 'number') return
|
|
388
|
+
const entry = this.pending.get(id)
|
|
389
|
+
if (!entry) return
|
|
390
|
+
this.pending.delete(id)
|
|
391
|
+
clearTimeout(entry.timer)
|
|
392
|
+
entry.cleanup()
|
|
393
|
+
if (reply.ok === true) entry.resolve(reply)
|
|
394
|
+
else
|
|
395
|
+
entry.reject(
|
|
396
|
+
new A11yError(
|
|
397
|
+
typeof reply.code === 'string' ? reply.code : 'error',
|
|
398
|
+
typeof reply.message === 'string' ? reply.message : 'the accessibility host failed',
|
|
399
|
+
),
|
|
400
|
+
)
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
private start(): Promise<boolean> {
|
|
404
|
+
this.starting ??= new Promise<boolean>((resolve) => {
|
|
405
|
+
let proc: A11yProcess
|
|
406
|
+
try {
|
|
407
|
+
proc = (this.options.spawnHelper ?? (() => spawnPowerShell(this.options)))()
|
|
408
|
+
} catch {
|
|
409
|
+
this.dead = true
|
|
410
|
+
resolve(false)
|
|
411
|
+
return
|
|
412
|
+
}
|
|
413
|
+
this.proc = proc
|
|
414
|
+
this.epoch += 1
|
|
415
|
+
let greeted = false
|
|
416
|
+
const fail = (): void => {
|
|
417
|
+
this.dead = true
|
|
418
|
+
this.up = false
|
|
419
|
+
resolve(false)
|
|
420
|
+
proc.kill()
|
|
421
|
+
}
|
|
422
|
+
const timer = setTimeout(fail, this.options.startTimeoutMs ?? 30_000)
|
|
423
|
+
timer.unref?.()
|
|
424
|
+
proc.onExit(() => {
|
|
425
|
+
clearTimeout(timer)
|
|
426
|
+
// Closed, or already replaced: nothing left to do for this one.
|
|
427
|
+
if (this.proc !== proc) {
|
|
428
|
+
resolve(false)
|
|
429
|
+
return
|
|
430
|
+
}
|
|
431
|
+
this.up = false
|
|
432
|
+
this.failAll(new A11yError('helper_exited', 'the accessibility host ended'))
|
|
433
|
+
// A helper that was running and ended is started again by the next question, once;
|
|
434
|
+
// one that never came up, or ends again, stays off.
|
|
435
|
+
if (greeted && !this.closed && this.restarts < MAX_RESTARTS) {
|
|
436
|
+
this.restarts++
|
|
437
|
+
this.proc = undefined
|
|
438
|
+
this.starting = undefined
|
|
439
|
+
} else this.dead = true
|
|
440
|
+
resolve(false)
|
|
441
|
+
})
|
|
442
|
+
proc.onLine((line) => {
|
|
443
|
+
if (greeted) {
|
|
444
|
+
this.onReply(line)
|
|
445
|
+
return
|
|
446
|
+
}
|
|
447
|
+
greeted = true
|
|
448
|
+
clearTimeout(timer)
|
|
449
|
+
let hello: { ok?: boolean; hello?: boolean; version?: number }
|
|
450
|
+
try {
|
|
451
|
+
hello = JSON.parse(line) as typeof hello
|
|
452
|
+
} catch {
|
|
453
|
+
fail()
|
|
454
|
+
return
|
|
455
|
+
}
|
|
456
|
+
if (hello.ok !== true || hello.hello !== true || hello.version !== PROTOCOL_VERSION) fail()
|
|
457
|
+
else {
|
|
458
|
+
this.up = true
|
|
459
|
+
resolve(true)
|
|
460
|
+
}
|
|
461
|
+
})
|
|
462
|
+
proc.write(a11yScriptLine())
|
|
463
|
+
})
|
|
464
|
+
return this.starting
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
function rect(value: unknown): A11yRect {
|
|
469
|
+
const r = Array.isArray(value) ? value : []
|
|
470
|
+
return { x: num(r[0]), y: num(r[1]), width: num(r[2]), height: num(r[3]) }
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function toNode(n: Record<string, unknown>): A11yNode {
|
|
474
|
+
const toggle = n.toggle
|
|
475
|
+
const expand = n.expand
|
|
476
|
+
return {
|
|
477
|
+
index: num(n.index),
|
|
478
|
+
parent: num(n.parent, -1),
|
|
479
|
+
depth: num(n.depth),
|
|
480
|
+
role: str(n.role),
|
|
481
|
+
...(typeof n.name === 'string' ? { name: n.name } : {}),
|
|
482
|
+
...(typeof n.automationId === 'string' ? { automationId: n.automationId } : {}),
|
|
483
|
+
...(typeof n.className === 'string' ? { className: n.className } : {}),
|
|
484
|
+
bounds: rect(n.rect),
|
|
485
|
+
enabled: n.enabled !== false,
|
|
486
|
+
offscreen: n.offscreen === true,
|
|
487
|
+
focused: n.focused === true,
|
|
488
|
+
actions: (Array.isArray(n.actions) ? n.actions : []).filter(
|
|
489
|
+
(a): a is A11yAction => typeof a === 'string',
|
|
490
|
+
),
|
|
491
|
+
...(typeof n.value === 'string' ? { value: n.value } : {}),
|
|
492
|
+
...(toggle === 'on' || toggle === 'off' || toggle === 'mixed' ? { toggle } : {}),
|
|
493
|
+
...(expand === 'expanded' || expand === 'collapsed' || expand === 'partial' || expand === 'leaf'
|
|
494
|
+
? { expand }
|
|
495
|
+
: {}),
|
|
496
|
+
...(typeof n.selected === 'boolean' ? { selected: n.selected } : {}),
|
|
497
|
+
...(typeof n.childCount === 'number' ? { childCount: n.childCount } : {}),
|
|
498
|
+
}
|
|
499
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export { WindowsA11yHost } from './a11y.js'
|
|
2
|
+
export type { A11yProcess, WindowsA11yOptions } from './a11y.js'
|
|
3
|
+
export { A11Y_BOOTSTRAP, A11Y_HELPER_SCRIPT, a11yScriptLine } from './a11y-script.js'
|
|
4
|
+
export { A11Y_ENV, a11yDisabledByEnv, createWindowsA11yHost } from './windows.js'
|
|
5
|
+
export type { WindowsA11yFactoryOptions } from './windows.js'
|
|
6
|
+
export { A11yError } from './interface.js'
|
|
7
|
+
export type {
|
|
8
|
+
A11yAction,
|
|
9
|
+
A11yChangeKind,
|
|
10
|
+
A11yDisplay,
|
|
11
|
+
A11yFingerprint,
|
|
12
|
+
A11yIdentityRequest,
|
|
13
|
+
A11yNode,
|
|
14
|
+
A11yRect,
|
|
15
|
+
A11yResolution,
|
|
16
|
+
A11yResolveRequest,
|
|
17
|
+
A11ySeq,
|
|
18
|
+
A11ySnapshot,
|
|
19
|
+
A11ySnapshotRequest,
|
|
20
|
+
A11yWaitRequest,
|
|
21
|
+
A11yWaitResult,
|
|
22
|
+
A11yWindow,
|
|
23
|
+
AccessibilityBackend,
|
|
24
|
+
} from './interface.js'
|