@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/darwin.ts
CHANGED
|
@@ -8,10 +8,21 @@ import type {
|
|
|
8
8
|
ComputerUseResult,
|
|
9
9
|
DisplayGeometry,
|
|
10
10
|
DisplayInfo,
|
|
11
|
+
FocusWindowResult,
|
|
11
12
|
Point,
|
|
12
13
|
ScreenshotResult,
|
|
14
|
+
UiActResult,
|
|
15
|
+
UiElementAction,
|
|
16
|
+
UiSnapshot,
|
|
17
|
+
UiSnapshotOptions,
|
|
18
|
+
WindowInfo,
|
|
19
|
+
WindowInputAction,
|
|
20
|
+
WindowInputResult,
|
|
21
|
+
WindowScreenshotResult,
|
|
13
22
|
} from '@namzu/sdk'
|
|
23
|
+
import type { MacA11yHandle } from '../a11y/macos.js'
|
|
14
24
|
import { hasExecutable, runCommand, runCommandOrThrow } from '../util/spawn.js'
|
|
25
|
+
import { MacAxSurface, type PickFileResult } from './darwin-ax.js'
|
|
15
26
|
import { ActionCapabilityError, type Adapter, AdapterUnavailableError } from './types.js'
|
|
16
27
|
|
|
17
28
|
// ---------------------------------------------------------------------------
|
|
@@ -66,6 +77,57 @@ const MODIFIER_ALIASES: Readonly<Record<string, 'command' | 'control' | 'option'
|
|
|
66
77
|
shift: 'shift',
|
|
67
78
|
}
|
|
68
79
|
|
|
80
|
+
/** Virtual key codes of the printable keys on a US layout, for shortcuts (`cmd+a`). */
|
|
81
|
+
const ANSI_KEY_CODES: Readonly<Record<string, number>> = {
|
|
82
|
+
a: 0,
|
|
83
|
+
s: 1,
|
|
84
|
+
d: 2,
|
|
85
|
+
f: 3,
|
|
86
|
+
h: 4,
|
|
87
|
+
g: 5,
|
|
88
|
+
z: 6,
|
|
89
|
+
x: 7,
|
|
90
|
+
c: 8,
|
|
91
|
+
v: 9,
|
|
92
|
+
b: 11,
|
|
93
|
+
q: 12,
|
|
94
|
+
w: 13,
|
|
95
|
+
e: 14,
|
|
96
|
+
r: 15,
|
|
97
|
+
y: 16,
|
|
98
|
+
t: 17,
|
|
99
|
+
'1': 18,
|
|
100
|
+
'2': 19,
|
|
101
|
+
'3': 20,
|
|
102
|
+
'4': 21,
|
|
103
|
+
'6': 22,
|
|
104
|
+
'5': 23,
|
|
105
|
+
'=': 24,
|
|
106
|
+
'9': 25,
|
|
107
|
+
'7': 26,
|
|
108
|
+
'-': 27,
|
|
109
|
+
'8': 28,
|
|
110
|
+
'0': 29,
|
|
111
|
+
']': 30,
|
|
112
|
+
o: 31,
|
|
113
|
+
u: 32,
|
|
114
|
+
'[': 33,
|
|
115
|
+
i: 34,
|
|
116
|
+
p: 35,
|
|
117
|
+
l: 37,
|
|
118
|
+
j: 38,
|
|
119
|
+
"'": 39,
|
|
120
|
+
k: 40,
|
|
121
|
+
';': 41,
|
|
122
|
+
'\\': 42,
|
|
123
|
+
',': 43,
|
|
124
|
+
'/': 44,
|
|
125
|
+
n: 45,
|
|
126
|
+
m: 46,
|
|
127
|
+
'.': 47,
|
|
128
|
+
'`': 50,
|
|
129
|
+
}
|
|
130
|
+
|
|
69
131
|
interface ParsedKeyCombo {
|
|
70
132
|
readonly modifiers: readonly ('command' | 'control' | 'option' | 'shift')[]
|
|
71
133
|
readonly mainKey: string
|
|
@@ -89,6 +151,14 @@ export function parseKeyCombo(input: string): ParsedKeyCombo {
|
|
|
89
151
|
return { modifiers: mods, mainKey }
|
|
90
152
|
}
|
|
91
153
|
|
|
154
|
+
/** A key combination as a virtual key code and modifier names, for events posted to a window. */
|
|
155
|
+
export function keyEventOf(input: string): { code: number; flags: readonly string[] } {
|
|
156
|
+
const { modifiers, mainKey } = parseKeyCombo(input)
|
|
157
|
+
const code = KEY_CODES[mainKey.toLowerCase()] ?? ANSI_KEY_CODES[mainKey.toLowerCase()]
|
|
158
|
+
if (code === undefined) throw new Error(`DarwinAdapter: unknown key "${mainKey}"`)
|
|
159
|
+
return { code, flags: modifiers }
|
|
160
|
+
}
|
|
161
|
+
|
|
92
162
|
function modifiersClause(mods: readonly ('command' | 'control' | 'option' | 'shift')[]): string {
|
|
93
163
|
if (mods.length === 0) return ''
|
|
94
164
|
const items = mods.map((m) => `${m} down`)
|
|
@@ -125,6 +195,13 @@ async function probe(): Promise<ProbeResult> {
|
|
|
125
195
|
// Adapter
|
|
126
196
|
// ---------------------------------------------------------------------------
|
|
127
197
|
|
|
198
|
+
export interface DarwinAdapterOptions {
|
|
199
|
+
/** The accessibility helper; absent, windows, the tree and scroll are not offered. */
|
|
200
|
+
readonly ax?: MacA11yHandle
|
|
201
|
+
/** For tests: pixels per point instead of the display's. */
|
|
202
|
+
readonly scale?: () => Promise<number>
|
|
203
|
+
}
|
|
204
|
+
|
|
128
205
|
/**
|
|
129
206
|
* Units: the host contract is physical pixels. `screencapture` writes the
|
|
130
207
|
* backing store (2880x1800 on a 1440x900-point Retina panel), while
|
|
@@ -145,9 +222,25 @@ export class DarwinAdapter implements Adapter {
|
|
|
145
222
|
/** Captured pixels per point on the main display, from the latest capture. */
|
|
146
223
|
private pixelsPerPoint: number | undefined
|
|
147
224
|
|
|
148
|
-
private
|
|
225
|
+
private readonly ax: MacAxSurface | undefined
|
|
226
|
+
readonly backend: string
|
|
227
|
+
readonly fallbackReason: string | undefined
|
|
228
|
+
|
|
229
|
+
private constructor(probeResult: ProbeResult, ax?: MacAxSurface, note?: string) {
|
|
149
230
|
this.hasCliclick = probeResult.hasCliclick
|
|
231
|
+
this.ax = ax
|
|
232
|
+
this.backend = ax ? 'osascript + namzu-helper' : 'osascript'
|
|
233
|
+
this.fallbackReason = note
|
|
150
234
|
this.capabilities = Object.freeze({
|
|
235
|
+
...(ax
|
|
236
|
+
? {
|
|
237
|
+
windows: true,
|
|
238
|
+
windowCapture: true,
|
|
239
|
+
windowScroll: true,
|
|
240
|
+
uiTree: true,
|
|
241
|
+
maxClickCount: 3 as const,
|
|
242
|
+
}
|
|
243
|
+
: {}),
|
|
151
244
|
displayServer: 'darwin',
|
|
152
245
|
screenshot: probeResult.hasScreencapture,
|
|
153
246
|
mouse: probeResult.hasOsascript,
|
|
@@ -162,6 +255,7 @@ export class DarwinAdapter implements Adapter {
|
|
|
162
255
|
...(probeResult.hasCliclick
|
|
163
256
|
? ['cursor_position' as const, 'mouse_move' as const, 'mouse_drag' as const]
|
|
164
257
|
: []),
|
|
258
|
+
...(ax ? ['scroll' as const] : []),
|
|
165
259
|
]),
|
|
166
260
|
mouseClickButtons: Object.freeze(
|
|
167
261
|
probeResult.hasCliclick ? ['left' as const, 'right' as const] : ['left' as const],
|
|
@@ -170,7 +264,11 @@ export class DarwinAdapter implements Adapter {
|
|
|
170
264
|
})
|
|
171
265
|
}
|
|
172
266
|
|
|
173
|
-
|
|
267
|
+
/**
|
|
268
|
+
* `options.ax` is the accessibility helper's host (see `createMacA11yHost`); it is built and started
|
|
269
|
+
* here, and when it cannot be the adapter works as before, with `fallbackReason` saying why.
|
|
270
|
+
*/
|
|
271
|
+
static async create(options: DarwinAdapterOptions = {}): Promise<DarwinAdapter> {
|
|
174
272
|
const probeResult = await probe()
|
|
175
273
|
const missing: string[] = []
|
|
176
274
|
if (!probeResult.hasScreencapture) missing.push('screencapture')
|
|
@@ -181,7 +279,85 @@ export class DarwinAdapter implements Adapter {
|
|
|
181
279
|
missing,
|
|
182
280
|
)
|
|
183
281
|
}
|
|
184
|
-
|
|
282
|
+
let surface: MacAxSurface | undefined
|
|
283
|
+
let note: string | undefined
|
|
284
|
+
if (options.ax) {
|
|
285
|
+
const prepared = await options.ax.prepare()
|
|
286
|
+
if (prepared.ok) {
|
|
287
|
+
const adapterScale = options.scale
|
|
288
|
+
surface = new MacAxSurface({
|
|
289
|
+
host: options.ax.host,
|
|
290
|
+
scale: adapterScale ?? (async () => (await readMainDisplay()).scaleFactor),
|
|
291
|
+
keyEvent: keyEventOf,
|
|
292
|
+
})
|
|
293
|
+
const perms = await options.ax.host.permissions().catch(() => undefined)
|
|
294
|
+
if (perms && perms.steps.length > 0)
|
|
295
|
+
note = `${perms.app} still needs permission: ${perms.steps.join(' ')}`
|
|
296
|
+
} else {
|
|
297
|
+
note = `the macOS accessibility helper is not in use: ${prepared.reason}`
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
const adapter = new DarwinAdapter(probeResult, surface, note)
|
|
301
|
+
if (surface && !options.scale) surface.bindScale(() => adapter.scale())
|
|
302
|
+
return adapter
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
async dispose(): Promise<void> {
|
|
306
|
+
await this.ax?.close()
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
async release(): Promise<void> {
|
|
310
|
+
this.ax?.release()
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
async listWindows(): Promise<readonly WindowInfo[]> {
|
|
314
|
+
return this.requireAx().listWindows()
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
async focusWindow(id: string): Promise<FocusWindowResult> {
|
|
318
|
+
return this.requireAx().focusWindow(id)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
async captureWindow(id: string): Promise<WindowScreenshotResult> {
|
|
322
|
+
return this.requireAx().captureWindow(id)
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
async executeWindow(
|
|
326
|
+
captureId: string,
|
|
327
|
+
action: WindowInputAction,
|
|
328
|
+
): Promise<WindowInputResult | undefined> {
|
|
329
|
+
return this.requireAx().executeWindow(captureId, action)
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
async settleAfterInput(
|
|
333
|
+
request: { readonly windowId?: string; readonly budgetMs: number },
|
|
334
|
+
signal?: AbortSignal,
|
|
335
|
+
): Promise<{ readonly waitedMs: number; readonly changed: boolean } | undefined> {
|
|
336
|
+
return this.ax?.settleAfterInput(request, signal)
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
async uiSnapshot(windowId?: string | number, options?: UiSnapshotOptions): Promise<UiSnapshot> {
|
|
340
|
+
return this.requireAx().uiSnapshot(windowId, options)
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
async uiAct(ref: string, action: UiElementAction, value?: string): Promise<UiActResult> {
|
|
344
|
+
return this.requireAx().uiAct(ref, action, value)
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** Picks a file in the open or save panel in front, through its accessibility tree. macOS only. */
|
|
348
|
+
async uiPickFile(
|
|
349
|
+
path: string,
|
|
350
|
+
options?: { readonly windowId?: string | number; readonly confirm?: boolean },
|
|
351
|
+
): Promise<PickFileResult> {
|
|
352
|
+
return this.requireAx().pickFile(path, options)
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
private requireAx(): MacAxSurface {
|
|
356
|
+
if (!this.ax)
|
|
357
|
+
throw new AdapterUnavailableError(
|
|
358
|
+
this.fallbackReason ?? 'the macOS accessibility helper is not running',
|
|
359
|
+
)
|
|
360
|
+
return this.ax
|
|
185
361
|
}
|
|
186
362
|
|
|
187
363
|
/** The main display in physical pixels (the capture's), with pixels per point. */
|
|
@@ -243,10 +419,14 @@ export class DarwinAdapter implements Adapter {
|
|
|
243
419
|
await this.mouseDrag(action.from, action.to)
|
|
244
420
|
return { type: 'ok' }
|
|
245
421
|
case 'scroll':
|
|
422
|
+
if (this.ax) {
|
|
423
|
+
await this.ax.scrollAt(action.at, action.direction, action.amount)
|
|
424
|
+
return { type: 'ok' }
|
|
425
|
+
}
|
|
246
426
|
throw new ActionCapabilityError(
|
|
247
427
|
action.type,
|
|
248
428
|
'mouse',
|
|
249
|
-
'scroll is not supported on macOS without
|
|
429
|
+
'scroll is not supported on macOS without the Namzu helper (it builds with the Xcode Command Line Tools); use keyboard page navigation instead',
|
|
250
430
|
)
|
|
251
431
|
case 'type_text':
|
|
252
432
|
await this.typeText(action.text)
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** The pixel size in a PNG's header, or undefined when the bytes are not one. */
|
|
2
|
+
export function decodePngSize(data: Buffer): { width: number; height: number } | undefined {
|
|
3
|
+
if (data.length < 24 || data.readUInt32BE(0) !== 0x89504e47) return undefined
|
|
4
|
+
return { width: data.readUInt32BE(16), height: data.readUInt32BE(20) }
|
|
5
|
+
}
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import { mkdirSync, mkdtempSync, readFileSync, rmSync, unlinkSync } from 'node:fs'
|
|
2
|
+
import { tmpdir } from 'node:os'
|
|
3
|
+
import { join } from 'node:path'
|
|
4
|
+
import type {
|
|
5
|
+
ComputerUseAction,
|
|
6
|
+
ComputerUseCapabilities,
|
|
7
|
+
ComputerUseResult,
|
|
8
|
+
DisplayGeometry,
|
|
9
|
+
DisplayInfo,
|
|
10
|
+
Point,
|
|
11
|
+
Rect,
|
|
12
|
+
ScreenshotResult,
|
|
13
|
+
} from '@namzu/sdk'
|
|
14
|
+
import { type A11yDisplay, A11yError } from '../a11y/interface.js'
|
|
15
|
+
import type { NativeWinProcessOptions } from '../native/process.js'
|
|
16
|
+
import type { NativeCaptureTarget, NativeInputRequest } from '../native/protocol.js'
|
|
17
|
+
import { type NativeHelper, shareNativeProcess } from '../native/shared.js'
|
|
18
|
+
import { SpawnError } from '../util/spawn.js'
|
|
19
|
+
import { type Adapter, AdapterUnavailableError } from './types.js'
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The Windows display adapter when the native helper (`namzu-win`) is
|
|
23
|
+
* configured: screenshot, zoom region, click, drag, scroll, type, key,
|
|
24
|
+
* cursor and display geometry, each one request to a helper that is already
|
|
25
|
+
* running. It keeps the host contract the PowerShell adapter keeps (physical
|
|
26
|
+
* pixels, points relative to the display of the last capture) and replaces its
|
|
27
|
+
* process-per-action cost and its scripts with `SendInput` and
|
|
28
|
+
* Windows.Graphics.Capture calls made by a compiled program.
|
|
29
|
+
*
|
|
30
|
+
* There is no PowerShell here, and no fallback to it: when the helper cannot
|
|
31
|
+
* start the adapter does not exist, and the caller is told why.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
/** What the helper must report in `hello` for this adapter to use it. */
|
|
35
|
+
const REQUIRED_CAPS = ['capture', 'input', 'foreground'] as const
|
|
36
|
+
|
|
37
|
+
/** How long a helper may take over one display call, ms. */
|
|
38
|
+
const REQUEST_MS = 8_000
|
|
39
|
+
|
|
40
|
+
/** Errors after which the input may already have reached the desktop. */
|
|
41
|
+
const OUTCOME_UNKNOWN = new Set(['timeout', 'helper_exited', 'bad_reply', 'input_blocked'])
|
|
42
|
+
|
|
43
|
+
export interface NativeWin32AdapterOptions extends NativeWinProcessOptions {
|
|
44
|
+
/**
|
|
45
|
+
* A directory the helper writes pictures into, made when missing. Default:
|
|
46
|
+
* a new private directory under the system's temporary one, removed on dispose.
|
|
47
|
+
*/
|
|
48
|
+
readonly captureDir?: string
|
|
49
|
+
/**
|
|
50
|
+
* The path as the helper reads it: under WSL a Linux path goes through the
|
|
51
|
+
* distro's share. Default: the path as it is.
|
|
52
|
+
*/
|
|
53
|
+
readonly toHelperPath?: (path: string) => string
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function toDisplayInfo(d: A11yDisplay): DisplayInfo {
|
|
57
|
+
return {
|
|
58
|
+
id: d.id,
|
|
59
|
+
x: d.bounds.x,
|
|
60
|
+
y: d.bounds.y,
|
|
61
|
+
width: d.bounds.width,
|
|
62
|
+
height: d.bounds.height,
|
|
63
|
+
scaleFactor: d.scale,
|
|
64
|
+
primary: d.primary,
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The last part of a path written with either separator. */
|
|
69
|
+
function baseName(path: string): string {
|
|
70
|
+
return path.split(/[\\/]/).pop() ?? path
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export class NativeWin32Adapter implements Adapter {
|
|
74
|
+
readonly capabilities: ComputerUseCapabilities
|
|
75
|
+
readonly backend: string
|
|
76
|
+
private readonly ownsDir: boolean
|
|
77
|
+
/** The display points are relative to: the one of the last capture. */
|
|
78
|
+
private display: DisplayInfo | undefined
|
|
79
|
+
|
|
80
|
+
private constructor(
|
|
81
|
+
private readonly process: NativeHelper,
|
|
82
|
+
private readonly dir: string,
|
|
83
|
+
private readonly helperDir: string,
|
|
84
|
+
ownsDir: boolean,
|
|
85
|
+
private readonly executable: string,
|
|
86
|
+
) {
|
|
87
|
+
this.ownsDir = ownsDir
|
|
88
|
+
this.backend = `namzu-win ${process.hello?.version ?? ''}`.trim()
|
|
89
|
+
this.capabilities = Object.freeze({
|
|
90
|
+
displayServer: 'win32',
|
|
91
|
+
screenshot: true,
|
|
92
|
+
mouse: true,
|
|
93
|
+
keyboard: true,
|
|
94
|
+
cursorPosition: true,
|
|
95
|
+
clipboard: false,
|
|
96
|
+
supportedActions: Object.freeze([
|
|
97
|
+
'screenshot' as const,
|
|
98
|
+
'cursor_position' as const,
|
|
99
|
+
'mouse_move' as const,
|
|
100
|
+
'mouse_click' as const,
|
|
101
|
+
'mouse_drag' as const,
|
|
102
|
+
'scroll' as const,
|
|
103
|
+
'type_text' as const,
|
|
104
|
+
'key' as const,
|
|
105
|
+
]),
|
|
106
|
+
mouseClickButtons: Object.freeze(['left', 'right', 'middle'] as const),
|
|
107
|
+
mouseDragButtons: Object.freeze(['left', 'right', 'middle'] as const),
|
|
108
|
+
maxClickCount: 3,
|
|
109
|
+
regionCapture: true,
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Starts the helper and checks it can do what this adapter needs. Throws
|
|
115
|
+
* {@link AdapterUnavailableError} when it cannot.
|
|
116
|
+
*/
|
|
117
|
+
static async create(options: NativeWin32AdapterOptions): Promise<NativeWin32Adapter> {
|
|
118
|
+
const process = shareNativeProcess(options)
|
|
119
|
+
if (!(await process.ready())) {
|
|
120
|
+
throw new AdapterUnavailableError(
|
|
121
|
+
`Win32Adapter: the native helper did not start (${options.executable})`,
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
const caps = process.hello?.caps ?? []
|
|
125
|
+
const missing = REQUIRED_CAPS.filter((c) => !caps.includes(c))
|
|
126
|
+
if (missing.length > 0) {
|
|
127
|
+
await process.close()
|
|
128
|
+
throw new AdapterUnavailableError(
|
|
129
|
+
`Win32Adapter: the native helper cannot ${missing.join(', ')}; it is older than this program`,
|
|
130
|
+
[...missing],
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
const ownsDir = options.captureDir === undefined
|
|
134
|
+
const dir = options.captureDir ?? mkdtempSync(join(tmpdir(), 'namzu-win-'))
|
|
135
|
+
if (!ownsDir) mkdirSync(dir, { recursive: true })
|
|
136
|
+
const helperDir = (options.toHelperPath ?? ((p: string) => p))(dir)
|
|
137
|
+
return new NativeWin32Adapter(process, dir, helperDir, ownsDir, options.executable)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* One input. A helper that stopped answering after it was sent leaves the
|
|
142
|
+
* desktop in a state nobody knows, which the host reports as an unknown
|
|
143
|
+
* outcome (the same `SpawnError` the script path throws), never as a
|
|
144
|
+
* failure that is safe to repeat.
|
|
145
|
+
*/
|
|
146
|
+
private async sendInput(request: NativeInputRequest): Promise<void> {
|
|
147
|
+
try {
|
|
148
|
+
await this.process.request(request, REQUEST_MS)
|
|
149
|
+
} catch (error) {
|
|
150
|
+
if (error instanceof A11yError && OUTCOME_UNKNOWN.has(error.code)) {
|
|
151
|
+
throw new SpawnError(
|
|
152
|
+
`namzu-win input: ${error.message}`,
|
|
153
|
+
{
|
|
154
|
+
exitCode: -1,
|
|
155
|
+
stdout: Buffer.alloc(0),
|
|
156
|
+
stderr: error.message,
|
|
157
|
+
timedOut: error.code === 'timeout',
|
|
158
|
+
signal: null,
|
|
159
|
+
},
|
|
160
|
+
this.executable,
|
|
161
|
+
['input'],
|
|
162
|
+
)
|
|
163
|
+
}
|
|
164
|
+
if (error instanceof A11yError) throw new Error(`namzu-win input: ${error.message}`)
|
|
165
|
+
throw error
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private async displays(): Promise<readonly DisplayInfo[]> {
|
|
170
|
+
const reply = await this.process.request({ op: 'displays' }, REQUEST_MS)
|
|
171
|
+
return reply.displays.map(toDisplayInfo)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private async primary(): Promise<DisplayInfo> {
|
|
175
|
+
const all = await this.displays()
|
|
176
|
+
const found = all.find((d) => d.primary) ?? all[0]
|
|
177
|
+
if (!found) throw new Error('namzu-win: the helper lists no display')
|
|
178
|
+
return found
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** The display points are relative to; the primary one before any capture. */
|
|
182
|
+
private async origin(): Promise<DisplayInfo> {
|
|
183
|
+
this.display ??= await this.primary()
|
|
184
|
+
return this.display
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async getDisplayGeometry(): Promise<DisplayGeometry> {
|
|
188
|
+
const d = await this.primary()
|
|
189
|
+
this.display = d
|
|
190
|
+
return { width: d.width, height: d.height, scaleFactor: d.scaleFactor }
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Takes the picture the helper wrote and removes its file. */
|
|
194
|
+
private takePicture(path: string): Buffer {
|
|
195
|
+
const local = join(this.dir, baseName(path))
|
|
196
|
+
try {
|
|
197
|
+
return readFileSync(local)
|
|
198
|
+
} finally {
|
|
199
|
+
try {
|
|
200
|
+
unlinkSync(local)
|
|
201
|
+
} catch {
|
|
202
|
+
// Already gone.
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private async shot(target: NativeCaptureTarget, region?: Rect): Promise<ScreenshotResult> {
|
|
208
|
+
const picture = await this.process.request(
|
|
209
|
+
{ op: 'capture', target, into: this.helperDir },
|
|
210
|
+
REQUEST_MS,
|
|
211
|
+
)
|
|
212
|
+
if (picture.path === undefined) throw new Error('namzu-win capture: the helper sent no picture')
|
|
213
|
+
const data = this.takePicture(picture.path)
|
|
214
|
+
const all = await this.displays()
|
|
215
|
+
const shown = all.find((d) => d.id === picture.display) ?? all.find((d) => d.primary) ?? all[0]
|
|
216
|
+
if (!shown) throw new Error('namzu-win: the helper lists no display')
|
|
217
|
+
if (!region) this.display = shown
|
|
218
|
+
return {
|
|
219
|
+
data,
|
|
220
|
+
mimeType: 'image/png',
|
|
221
|
+
width: picture.width,
|
|
222
|
+
height: picture.height,
|
|
223
|
+
display: shown,
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
async captureRegion(rect: Rect): Promise<ScreenshotResult> {
|
|
228
|
+
// The rectangle is relative to the display of the last capture.
|
|
229
|
+
const origin = await this.origin()
|
|
230
|
+
return this.shot(
|
|
231
|
+
{
|
|
232
|
+
kind: 'region',
|
|
233
|
+
x: origin.x + rect.x,
|
|
234
|
+
y: origin.y + rect.y,
|
|
235
|
+
width: rect.width,
|
|
236
|
+
height: rect.height,
|
|
237
|
+
},
|
|
238
|
+
rect,
|
|
239
|
+
)
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
private async absolute(p: Point): Promise<Point> {
|
|
243
|
+
const origin = await this.origin()
|
|
244
|
+
return { x: p.x + origin.x, y: p.y + origin.y }
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async execute(action: ComputerUseAction): Promise<ComputerUseResult> {
|
|
248
|
+
switch (action.type) {
|
|
249
|
+
case 'screenshot': {
|
|
250
|
+
const primary = await this.primary()
|
|
251
|
+
const result = await this.shot({ kind: 'display', id: primary.id })
|
|
252
|
+
return { type: 'screenshot', result }
|
|
253
|
+
}
|
|
254
|
+
case 'cursor_position': {
|
|
255
|
+
const state = await this.process.request({ op: 'foreground.get' }, REQUEST_MS)
|
|
256
|
+
const origin = await this.origin()
|
|
257
|
+
return {
|
|
258
|
+
type: 'cursor_position',
|
|
259
|
+
point: { x: state.cursor.x - origin.x, y: state.cursor.y - origin.y },
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
case 'mouse_move': {
|
|
263
|
+
const to = await this.absolute(action.to)
|
|
264
|
+
await this.sendInput({ op: 'input', kind: 'move', x: to.x, y: to.y })
|
|
265
|
+
return { type: 'ok' }
|
|
266
|
+
}
|
|
267
|
+
case 'mouse_click': {
|
|
268
|
+
const at = await this.absolute(action.at)
|
|
269
|
+
await this.sendInput({
|
|
270
|
+
op: 'input',
|
|
271
|
+
kind: 'click',
|
|
272
|
+
x: at.x,
|
|
273
|
+
y: at.y,
|
|
274
|
+
button: action.button,
|
|
275
|
+
...(action.count !== undefined ? { count: action.count } : {}),
|
|
276
|
+
})
|
|
277
|
+
return { type: 'ok' }
|
|
278
|
+
}
|
|
279
|
+
case 'mouse_drag': {
|
|
280
|
+
const from = await this.absolute(action.from)
|
|
281
|
+
const to = await this.absolute(action.to)
|
|
282
|
+
await this.sendInput({
|
|
283
|
+
op: 'input',
|
|
284
|
+
kind: 'drag',
|
|
285
|
+
x: from.x,
|
|
286
|
+
y: from.y,
|
|
287
|
+
toX: to.x,
|
|
288
|
+
toY: to.y,
|
|
289
|
+
button: action.button,
|
|
290
|
+
})
|
|
291
|
+
return { type: 'ok' }
|
|
292
|
+
}
|
|
293
|
+
case 'scroll': {
|
|
294
|
+
const at = await this.absolute(action.at)
|
|
295
|
+
await this.sendInput({
|
|
296
|
+
op: 'input',
|
|
297
|
+
kind: 'scroll',
|
|
298
|
+
x: at.x,
|
|
299
|
+
y: at.y,
|
|
300
|
+
direction: action.direction,
|
|
301
|
+
amount: action.amount,
|
|
302
|
+
})
|
|
303
|
+
return { type: 'ok' }
|
|
304
|
+
}
|
|
305
|
+
case 'type_text':
|
|
306
|
+
await this.sendInput({ op: 'input', kind: 'type', text: action.text })
|
|
307
|
+
return { type: 'ok' }
|
|
308
|
+
case 'key':
|
|
309
|
+
await this.sendInput({ op: 'input', kind: 'key', keys: action.keys })
|
|
310
|
+
return { type: 'ok' }
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
async release(): Promise<void> {
|
|
315
|
+
this.process.release()
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
async dispose(): Promise<void> {
|
|
319
|
+
await this.process.close()
|
|
320
|
+
if (this.ownsDir) {
|
|
321
|
+
try {
|
|
322
|
+
rmSync(this.dir, { recursive: true, force: true })
|
|
323
|
+
} catch {
|
|
324
|
+
// Left for the system's temporary-file cleanup.
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|