@namzu/computer-use 4.5.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 (51) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/a11y/windows.d.ts +3 -2
  3. package/dist/a11y/windows.d.ts.map +1 -1
  4. package/dist/a11y/windows.js +6 -4
  5. package/dist/a11y/windows.js.map +1 -1
  6. package/dist/adapters/win32-native.d.ts.map +1 -1
  7. package/dist/adapters/win32-native.js +8 -8
  8. package/dist/adapters/win32-native.js.map +1 -1
  9. package/dist/adapters/win32.d.ts +4 -4
  10. package/dist/adapters/win32.d.ts.map +1 -1
  11. package/dist/adapters/win32.js +2 -1
  12. package/dist/adapters/win32.js.map +1 -1
  13. package/dist/foreground/windows.d.ts +1 -1
  14. package/dist/foreground/windows.d.ts.map +1 -1
  15. package/dist/foreground/windows.js +2 -1
  16. package/dist/foreground/windows.js.map +1 -1
  17. package/dist/index.d.ts +9 -0
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +7 -0
  20. package/dist/index.js.map +1 -1
  21. package/dist/native/env.d.ts +19 -0
  22. package/dist/native/env.d.ts.map +1 -0
  23. package/dist/native/env.js +29 -0
  24. package/dist/native/env.js.map +1 -0
  25. package/dist/native/process.d.ts +2 -2
  26. package/dist/native/process.d.ts.map +1 -1
  27. package/dist/native/process.js +19 -9
  28. package/dist/native/process.js.map +1 -1
  29. package/dist/native/protocol.d.ts +74 -6
  30. package/dist/native/protocol.d.ts.map +1 -1
  31. package/dist/native/protocol.js +79 -5
  32. package/dist/native/protocol.js.map +1 -1
  33. package/dist/native/watch.d.ts +35 -0
  34. package/dist/native/watch.d.ts.map +1 -0
  35. package/dist/native/watch.js +45 -0
  36. package/dist/native/watch.js.map +1 -0
  37. package/dist/presence/windows.d.ts +1 -1
  38. package/dist/presence/windows.d.ts.map +1 -1
  39. package/dist/presence/windows.js +2 -1
  40. package/dist/presence/windows.js.map +1 -1
  41. package/package.json +2 -2
  42. package/src/a11y/windows.ts +6 -4
  43. package/src/adapters/win32-native.ts +9 -8
  44. package/src/adapters/win32.ts +6 -5
  45. package/src/foreground/windows.ts +3 -2
  46. package/src/index.ts +14 -0
  47. package/src/native/env.ts +33 -0
  48. package/src/native/process.ts +23 -11
  49. package/src/native/protocol.ts +145 -5
  50. package/src/native/watch.ts +78 -0
  51. package/src/presence/windows.ts +3 -2
@@ -19,7 +19,7 @@ import { SpawnError } from '../util/spawn.js'
19
19
  import { type Adapter, AdapterUnavailableError } from './types.js'
20
20
 
21
21
  /**
22
- * The Windows display adapter when the native helper (`namzu-win`) is
22
+ * The Windows display adapter when the native helper (`namzu-native`) is
23
23
  * configured: screenshot, zoom region, click, drag, scroll, type, key,
24
24
  * cursor and display geometry, each one request to a helper that is already
25
25
  * running. It keeps the host contract the PowerShell adapter keeps (physical
@@ -85,7 +85,7 @@ export class NativeWin32Adapter implements Adapter {
85
85
  private readonly executable: string,
86
86
  ) {
87
87
  this.ownsDir = ownsDir
88
- this.backend = `namzu-win ${process.hello?.version ?? ''}`.trim()
88
+ this.backend = `namzu-native ${process.hello?.version ?? ''}`.trim()
89
89
  this.capabilities = Object.freeze({
90
90
  displayServer: 'win32',
91
91
  screenshot: true,
@@ -131,7 +131,7 @@ export class NativeWin32Adapter implements Adapter {
131
131
  )
132
132
  }
133
133
  const ownsDir = options.captureDir === undefined
134
- const dir = options.captureDir ?? mkdtempSync(join(tmpdir(), 'namzu-win-'))
134
+ const dir = options.captureDir ?? mkdtempSync(join(tmpdir(), 'namzu-native-'))
135
135
  if (!ownsDir) mkdirSync(dir, { recursive: true })
136
136
  const helperDir = (options.toHelperPath ?? ((p: string) => p))(dir)
137
137
  return new NativeWin32Adapter(process, dir, helperDir, ownsDir, options.executable)
@@ -149,7 +149,7 @@ export class NativeWin32Adapter implements Adapter {
149
149
  } catch (error) {
150
150
  if (error instanceof A11yError && OUTCOME_UNKNOWN.has(error.code)) {
151
151
  throw new SpawnError(
152
- `namzu-win input: ${error.message}`,
152
+ `namzu-native input: ${error.message}`,
153
153
  {
154
154
  exitCode: -1,
155
155
  stdout: Buffer.alloc(0),
@@ -161,7 +161,7 @@ export class NativeWin32Adapter implements Adapter {
161
161
  ['input'],
162
162
  )
163
163
  }
164
- if (error instanceof A11yError) throw new Error(`namzu-win input: ${error.message}`)
164
+ if (error instanceof A11yError) throw new Error(`namzu-native input: ${error.message}`)
165
165
  throw error
166
166
  }
167
167
  }
@@ -174,7 +174,7 @@ export class NativeWin32Adapter implements Adapter {
174
174
  private async primary(): Promise<DisplayInfo> {
175
175
  const all = await this.displays()
176
176
  const found = all.find((d) => d.primary) ?? all[0]
177
- if (!found) throw new Error('namzu-win: the helper lists no display')
177
+ if (!found) throw new Error('namzu-native: the helper lists no display')
178
178
  return found
179
179
  }
180
180
 
@@ -209,11 +209,12 @@ export class NativeWin32Adapter implements Adapter {
209
209
  { op: 'capture', target, into: this.helperDir },
210
210
  REQUEST_MS,
211
211
  )
212
- if (picture.path === undefined) throw new Error('namzu-win capture: the helper sent no picture')
212
+ if (picture.path === undefined)
213
+ throw new Error('namzu-native capture: the helper sent no picture')
213
214
  const data = this.takePicture(picture.path)
214
215
  const all = await this.displays()
215
216
  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 (!shown) throw new Error('namzu-native: the helper lists no display')
217
218
  if (!region) this.display = shown
218
219
  return {
219
220
  data,
@@ -2,6 +2,7 @@ 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
4
  import { windowsPathFor } from '../foreground/windows.js'
5
+ import { resolveNativeHelper } from '../native/env.js'
5
6
  import type { NativeWinProcessOptions } from '../native/process.js'
6
7
  import type { Presence } from '../presence/presence.js'
7
8
  import { CuaDriverAdapter } from './cua-driver/adapter.js'
@@ -24,8 +25,8 @@ import { type WslProbes, isWsl, realWslProbes, wslChildEnv } from './wsl.js'
24
25
  * - `cua-driver`: cua-driver or nothing; its failure is the adapter's.
25
26
  * - `powershell`: never cua-driver.
26
27
  *
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
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
29
30
  * otherwise. With a helper configured there is no PowerShell fallback: a
30
31
  * helper that does not start is an error, not a reason to run scripts.
31
32
  */
@@ -42,8 +43,8 @@ export interface Win32AdapterOptions {
42
43
  /** Told once, in plain words, before the pinned build is downloaded. */
43
44
  readonly onNotice?: (notice: string) => void
44
45
  /**
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
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
47
48
  * PowerShell is never started for display input or capture.
48
49
  */
49
50
  readonly helper?: string
@@ -137,7 +138,7 @@ export class Win32Adapter {
137
138
  const ladder = options.foreground
138
139
  const guard = (adapter: Adapter): Adapter =>
139
140
  ladder ? guardForeground(adapter, ladder) : adapter
140
- const helper = (options.helper ?? env.NAMZU_WIN_HELPER)?.trim()
141
+ const helper = resolveNativeHelper(options.helper, env)
141
142
  // The per-action path: the native helper when one is named, else PowerShell.
142
143
  // A helper that does not start throws; it never turns into PowerShell.
143
144
  const perAction = async (fallbackReason?: string) => {
@@ -7,6 +7,7 @@ import {
7
7
  wslChildEnv,
8
8
  wslPowerShellPath,
9
9
  } from '../adapters/wsl.js'
10
+ import { resolveNativeHelper } from '../native/env.js'
10
11
  import { NativeForegroundMonitor } from '../native/foreground-monitor.js'
11
12
  import { ForegroundLadder, type LadderTiming } from './ladder.js'
12
13
  import type { ForegroundApprover } from './types.js'
@@ -22,7 +23,7 @@ export interface WindowsLadderOptions {
22
23
  readonly approve?: ForegroundApprover
23
24
  readonly session?: string
24
25
  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
+ /** The native helper (`namzu-native.exe`); default `NAMZU_NATIVE_HELPER`. Named, the monitor is the helper and no PowerShell runs. */
26
27
  readonly helper?: string
27
28
  }
28
29
 
@@ -51,7 +52,7 @@ export function createWindowsForegroundLadder(
51
52
  const platform = options.platform ?? process.platform
52
53
  const probes = options.wslProbes ?? realWslProbes
53
54
  const wsl = isWsl(env, platform, probes)
54
- const native = (options.helper ?? env.NAMZU_WIN_HELPER)?.trim()
55
+ const native = resolveNativeHelper(options.helper, env)
55
56
  const powershell = native
56
57
  ? undefined
57
58
  : wsl
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,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
+ }
@@ -2,7 +2,7 @@
2
2
  * The helper as a process: start it, greet it, send it requests as lines and
3
3
  * match the replies to them by id.
4
4
  *
5
- * The helper is the compiled `namzu-win` executable. It is started directly,
5
+ * The helper is the compiled `namzu-native` executable. It is started directly,
6
6
  * with no shell and no script engine, and answers JSON lines on its standard
7
7
  * output. Everything the TypeScript side asks of it goes through
8
8
  * {@link NativeWinProcess.request}, which gives a reply body or throws an
@@ -13,6 +13,7 @@ import { spawn } from 'node:child_process'
13
13
  import { createInterface } from 'node:readline'
14
14
  import { A11yError } from '../a11y/interface.js'
15
15
  import {
16
+ LEGACY_NATIVE_PROTOCOL,
16
17
  NATIVE_PROTOCOL,
17
18
  type NativeHello,
18
19
  type NativeOp,
@@ -32,7 +33,7 @@ export interface NativeChannel {
32
33
  }
33
34
 
34
35
  export interface NativeWinProcessOptions {
35
- /** The path of `namzu-win.exe`. */
36
+ /** The path of `namzu-native.exe`. */
36
37
  readonly executable: string
37
38
  /** Sent in `hello`, so the helper's log can say who asked. */
38
39
  readonly cliVersion?: string
@@ -271,6 +272,15 @@ export class NativeWinProcess {
271
272
  this.runs += 1
272
273
  let greeted = false
273
274
  let greetedAt = 0
275
+ let triedLegacy = false
276
+ const hello = (protocol: string): void =>
277
+ channel.write(
278
+ encodeRequest(this.nextId++, {
279
+ op: 'hello',
280
+ protocol,
281
+ cliVersion: this.options.cliVersion ?? '0.0.0',
282
+ }),
283
+ )
274
284
  const fail = (): void => {
275
285
  this.dead = true
276
286
  this.up = false
@@ -305,10 +315,16 @@ export class NativeWinProcess {
305
315
  this.onReply(line)
306
316
  return
307
317
  }
308
- greeted = true
309
- clearTimeout(timer)
310
318
  try {
311
319
  const reply = decodeReply('hello', line)
320
+ // A helper built before the rename knows only the old protocol name.
321
+ if (!reply.ok && reply.code === 'protocol_mismatch' && !triedLegacy) {
322
+ triedLegacy = true
323
+ hello(LEGACY_NATIVE_PROTOCOL)
324
+ return
325
+ }
326
+ greeted = true
327
+ clearTimeout(timer)
312
328
  if (!reply.ok) {
313
329
  fail()
314
330
  return
@@ -318,17 +334,13 @@ export class NativeWinProcess {
318
334
  greetedAt = Date.now()
319
335
  resolve(true)
320
336
  } catch {
337
+ greeted = true
338
+ clearTimeout(timer)
321
339
  fail()
322
340
  }
323
341
  })
324
342
  // The helper ids start at 1 and the greeting is the first of them.
325
- channel.write(
326
- encodeRequest(this.nextId++, {
327
- op: 'hello',
328
- protocol: NATIVE_PROTOCOL,
329
- cliVersion: this.options.cliVersion ?? '0.0.0',
330
- }),
331
- )
343
+ hello(NATIVE_PROTOCOL)
332
344
  })
333
345
  return this.starting
334
346
  }
@@ -1,11 +1,11 @@
1
1
  /**
2
- * The `namzu-win/1` line codec: how this package talks to the native Windows
3
- * helper (`crates/namzu-win`), one JSON object per line over stdio.
2
+ * The `namzu-native/1` line codec: how this package talks to the native Windows
3
+ * helper (`crates/namzu-native`), one JSON object per line over stdio.
4
4
  *
5
5
  * Only the codec lives here: a request is written with {@link encodeRequest},
6
6
  * a reply is read with {@link decodeReply}, which refuses a line that is not
7
- * the shape the op promises. The Rust side (`crates/namzu-win-core`) reads and
8
- * writes the same lines; `conformance/namzu-win/` is the list both are held to.
7
+ * the shape the op promises. The Rust side (`crates/namzu-native-core`) reads and
8
+ * writes the same lines; `conformance/namzu-native/` is the list both are held to.
9
9
  */
10
10
 
11
11
  import type {
@@ -18,7 +18,16 @@ import type {
18
18
  A11yWindow,
19
19
  } from '../a11y/interface.js'
20
20
 
21
- export const NATIVE_PROTOCOL = 'namzu-win/1'
21
+ export const NATIVE_PROTOCOL = 'namzu-native/1'
22
+
23
+ /**
24
+ * What `NATIVE_PROTOCOL` was called while the helper was `namzu-win`. The
25
+ * lines are identical. A helper built before the rename answers `hello` with
26
+ * `protocol_mismatch` to the new name, and the client then says the old one.
27
+ *
28
+ * Deprecated: the next major release of `@namzu/computer-use` drops it.
29
+ */
30
+ export const LEGACY_NATIVE_PROTOCOL = 'namzu-win/1'
22
31
 
23
32
  /** The largest id or revision either side writes: a JavaScript number holds it exactly. */
24
33
  export const MAX_SAFE_ID = Number.MAX_SAFE_INTEGER
@@ -42,6 +51,9 @@ export type NativeOp =
42
51
  | 'monitor.start'
43
52
  | 'monitor.stop'
44
53
  | 'monitor.poll'
54
+ | 'watch.start'
55
+ | 'watch.stop'
56
+ | 'watch.poll'
45
57
  | 'presence.act'
46
58
  | 'presence.hide'
47
59
  | 'cancel'
@@ -93,6 +105,9 @@ export type NativeRequest =
93
105
  | NativeMonitorStartRequest
94
106
  | { readonly op: 'monitor.stop' }
95
107
  | { readonly op: 'monitor.poll'; readonly since?: number }
108
+ | NativeWatchStartRequest
109
+ | { readonly op: 'watch.stop' }
110
+ | { readonly op: 'watch.poll' }
96
111
  | NativePresenceActRequest
97
112
  | { readonly op: 'presence.hide' }
98
113
  | { readonly op: 'cancel'; readonly target: number }
@@ -187,6 +202,34 @@ export interface NativeMonitorStartRequest {
187
202
  readonly hooks?: readonly NativeMonitorHook[]
188
203
  }
189
204
 
205
+ /** What a watch reports. Nothing here is about keys or the mouse: there is no such kind. */
206
+ export type NativeWatchKind = 'invoke' | 'focus' | 'selection' | 'menu' | 'window' | 'value' | 'app'
207
+
208
+ export const NATIVE_WATCH_KINDS: readonly NativeWatchKind[] = [
209
+ 'invoke',
210
+ 'focus',
211
+ 'selection',
212
+ 'menu',
213
+ 'window',
214
+ 'value',
215
+ 'app',
216
+ ]
217
+
218
+ /** What to watch and how much to keep. Every field is optional; the helper clamps each. */
219
+ export interface NativeWatchStartRequest {
220
+ readonly op: 'watch.start'
221
+ /** Default every kind. */
222
+ readonly kinds?: readonly NativeWatchKind[]
223
+ /** Repeated value changes of one control within this many ms are one event (default 250, at most 5000; 0 keeps all). */
224
+ readonly coalesceMs?: number
225
+ /** Events kept between polls (default and most 2000). */
226
+ readonly maxQueue?: number
227
+ /** Characters of a value kept (default and most 200). */
228
+ readonly maxValue?: number
229
+ /** Characters of a control name kept (default and most 80). */
230
+ readonly maxName?: number
231
+ }
232
+
190
233
  export type NativePresenceKind = 'click' | 'type' | 'move'
191
234
 
192
235
  export interface NativePresenceActRequest {
@@ -389,6 +432,45 @@ export interface NativeMonitorPollReply {
389
432
  readonly dropped: boolean
390
433
  }
391
434
 
435
+ export interface NativeWatchStartReply {
436
+ /** UI Automation subscriptions are in place; false: the controls of programs are not watched. */
437
+ readonly controls: boolean
438
+ /** Foreground changes are hooked. */
439
+ readonly foreground: boolean
440
+ /** The kinds being watched. */
441
+ readonly kinds: readonly NativeWatchKind[]
442
+ }
443
+
444
+ /**
445
+ * One thing a person did to a control, or a window that opened or closed.
446
+ * There is no key, no character typed and no mouse button or position in it;
447
+ * `value` is an ordinary edit control's text and is never present for a
448
+ * password field, which says `secure: true` instead.
449
+ */
450
+ export interface NativeWatchEvent {
451
+ readonly kind: NativeWatchKind
452
+ /** Monotonic ms since the watch started. */
453
+ readonly tMs: number
454
+ readonly role?: string
455
+ readonly name?: string
456
+ readonly value?: string
457
+ readonly secure?: boolean
458
+ /** For a menu or a window: which end. */
459
+ readonly phase?: 'open' | 'close'
460
+ /** The title of the window the control is in. */
461
+ readonly window?: string
462
+ /** The program's file name. */
463
+ readonly process?: string
464
+ readonly windowId?: string
465
+ }
466
+
467
+ export interface NativeWatchPollReply {
468
+ /** Events since the last poll, oldest first. */
469
+ readonly events: readonly NativeWatchEvent[]
470
+ /** Events not kept since the last poll because the queue was full. */
471
+ readonly dropped: number
472
+ }
473
+
392
474
  export interface NativePresenceActReply {
393
475
  readonly cursorWindow: number
394
476
  readonly frameWindow: number
@@ -413,6 +495,9 @@ export interface NativeReplyBodies {
413
495
  readonly 'monitor.start': NativeMonitorStartReply
414
496
  readonly 'monitor.stop': Record<string, never>
415
497
  readonly 'monitor.poll': NativeMonitorPollReply
498
+ readonly 'watch.start': NativeWatchStartReply
499
+ readonly 'watch.stop': Record<string, never>
500
+ readonly 'watch.poll': NativeWatchPollReply
416
501
  readonly 'presence.act': NativePresenceActReply
417
502
  readonly 'presence.hide': Record<string, never>
418
503
  readonly cancel: Record<string, never>
@@ -848,6 +933,41 @@ function monitorPoll(o: Obj): NativeMonitorPollReply {
848
933
  return out
849
934
  }
850
935
 
936
+ function watchKind(v: unknown, path: string): NativeWatchKind {
937
+ return oneOf(v, NATIVE_WATCH_KINDS, path)
938
+ }
939
+
940
+ function watchEvent(v: unknown, path: string): NativeWatchEvent {
941
+ const o = obj(v, path)
942
+ const out: { -readonly [K in keyof NativeWatchEvent]: NativeWatchEvent[K] } = {
943
+ kind: watchKind(o.kind, `${path}.kind`),
944
+ tMs: int(o, 'tMs', path),
945
+ }
946
+ for (const key of ['role', 'name', 'value', 'window', 'process', 'windowId'] as const) {
947
+ const text = optStr(o, key, path)
948
+ if (text !== undefined) out[key] = text
949
+ }
950
+ const secure = optBool(o, 'secure', path)
951
+ if (secure !== undefined) out.secure = secure
952
+ if (o.phase !== undefined) out.phase = oneOf(o.phase, ['open', 'close'] as const, `${path}.phase`)
953
+ const cap = (key: 'name' | 'value' | 'window' | 'process', max: number) => {
954
+ const t = out[key]
955
+ if (t !== undefined && [...t].length > max) bad(`${path}.${key}`, `at most ${max} characters`)
956
+ }
957
+ cap('name', 80)
958
+ cap('value', 200)
959
+ cap('window', 120)
960
+ cap('process', 80)
961
+ if (out.value !== undefined && out.kind !== 'value')
962
+ bad(`${path}.value`, 'present only on value events')
963
+ if (out.phase !== undefined && out.kind !== 'menu' && out.kind !== 'window')
964
+ bad(`${path}.phase`, 'present only on menu and window events')
965
+ // A password field never carries its text, whatever a helper sent.
966
+ if (out.secure === true && out.value !== undefined)
967
+ bad(`${path}.value`, 'absent for a secure field')
968
+ return out
969
+ }
970
+
851
971
  function body(op: NativeOp, o: Obj): NativeReplyBodies[NativeOp] {
852
972
  switch (op) {
853
973
  case 'hello':
@@ -889,12 +1009,32 @@ function body(op: NativeOp, o: Obj): NativeReplyBodies[NativeOp] {
889
1009
  }
890
1010
  case 'monitor.poll':
891
1011
  return monitorPoll(o)
1012
+ case 'watch.start': {
1013
+ const kinds = arr(o, 'kinds', 'watch.start').map((k, i) =>
1014
+ watchKind(k, `watch.start.kinds[${i}]`),
1015
+ )
1016
+ if (kinds.length === 0) bad('watch.start.kinds', 'not empty')
1017
+ return {
1018
+ controls: bool(o, 'controls', 'watch.start'),
1019
+ foreground: bool(o, 'foreground', 'watch.start'),
1020
+ kinds,
1021
+ }
1022
+ }
1023
+ case 'watch.poll': {
1024
+ const events = arr(o, 'events', 'watch.poll')
1025
+ if (events.length > 2000) bad('watch.poll.events', 'at most 2000 events')
1026
+ return {
1027
+ events: events.map((e, i) => watchEvent(e, `watch.poll.events[${i}]`)),
1028
+ dropped: int(o, 'dropped', 'watch.poll'),
1029
+ }
1030
+ }
892
1031
  case 'presence.act':
893
1032
  return {
894
1033
  cursorWindow: int(o, 'cursorWindow', 'presence.act'),
895
1034
  frameWindow: int(o, 'frameWindow', 'presence.act'),
896
1035
  }
897
1036
  case 'monitor.stop':
1037
+ case 'watch.stop':
898
1038
  case 'presence.hide':
899
1039
  case 'cancel':
900
1040
  case 'quit':
@@ -0,0 +1,78 @@
1
+ import type { NativeWinProcessOptions } from './process.js'
2
+ import type {
3
+ NativeWatchEvent,
4
+ NativeWatchKind,
5
+ NativeWatchPollReply,
6
+ NativeWatchStartRequest,
7
+ } from './protocol.js'
8
+ import { type NativeHelper, shareNativeProcess } from './shared.js'
9
+
10
+ /**
11
+ * The controls a person uses, watched through the native helper without any
12
+ * input hook: which control was invoked, focused or selected, which menu or
13
+ * window opened or closed, what an ordinary edit field says, and which
14
+ * program came to the front.
15
+ *
16
+ * The helper reads these through read-only UI Automation subscriptions. It
17
+ * never registers for the keyboard or the mouse, so nothing here is a key or a
18
+ * button; a password field reports `secure` and never its text.
19
+ */
20
+ export type NativeWatchOptions = NativeWinProcessOptions
21
+
22
+ export type NativeWatchStart = Omit<NativeWatchStartRequest, 'op'>
23
+
24
+ export interface NativeWatching {
25
+ /** UI Automation subscriptions are in place; false: only the foreground may be watched. */
26
+ readonly controls: boolean
27
+ readonly foreground: boolean
28
+ readonly kinds: readonly NativeWatchKind[]
29
+ }
30
+
31
+ const REQUEST_MS = 5_000
32
+
33
+ export class NativeWatch {
34
+ private readonly process: NativeHelper
35
+ private started = false
36
+
37
+ constructor(options: NativeWatchOptions) {
38
+ this.process = shareNativeProcess(options)
39
+ }
40
+
41
+ get running(): boolean {
42
+ return this.started && this.process.isUp
43
+ }
44
+
45
+ /** Starts watching. A helper that has no watch (an older one) is an error that says so. */
46
+ async start(options: NativeWatchStart = {}): Promise<NativeWatching> {
47
+ if (!(await this.process.ready())) throw new Error('the native Windows helper did not start')
48
+ const caps = this.process.hello?.caps ?? []
49
+ if (!caps.includes('watch')) {
50
+ throw new Error('the native Windows helper in use cannot watch the controls a person uses')
51
+ }
52
+ const reply = await this.process.request({ op: 'watch.start', ...options }, REQUEST_MS)
53
+ this.started = true
54
+ return { controls: reply.controls, foreground: reply.foreground, kinds: reply.kinds }
55
+ }
56
+
57
+ /** What happened since the last poll, and how many events were not kept. */
58
+ async poll(): Promise<NativeWatchPollReply> {
59
+ return this.process.request({ op: 'watch.poll' }, REQUEST_MS)
60
+ }
61
+
62
+ /** Takes every subscription down. Safe to call twice. */
63
+ async stop(): Promise<void> {
64
+ if (!this.started) return
65
+ this.started = false
66
+ if (this.process.isUp) await this.process.request({ op: 'watch.stop' }, REQUEST_MS)
67
+ }
68
+
69
+ async dispose(): Promise<void> {
70
+ try {
71
+ await this.stop()
72
+ } finally {
73
+ await this.process.close()
74
+ }
75
+ }
76
+ }
77
+
78
+ export type { NativeWatchEvent }
@@ -5,6 +5,7 @@ import {
5
5
  wslChildEnv,
6
6
  wslPowerShellPath,
7
7
  } from '../adapters/wsl.js'
8
+ import { resolveNativeHelper } from '../native/env.js'
8
9
  import { NativePresence } from '../native/presence.js'
9
10
  import { type Presence, WindowsPresence } from './presence.js'
10
11
 
@@ -20,7 +21,7 @@ export interface WindowsPresenceFactoryOptions {
20
21
  readonly wslProbes?: WslProbes
21
22
  /** Default true. `false` shows nothing, whatever the environment says. */
22
23
  readonly enabled?: boolean
23
- /** The native helper (`namzu-win.exe`); default `NAMZU_WIN_HELPER`. Named, the overlay is drawn by the helper and no PowerShell runs. */
24
+ /** The native helper (`namzu-native.exe`); default `NAMZU_NATIVE_HELPER`. Named, the overlay is drawn by the helper and no PowerShell runs. */
24
25
  readonly helper?: string
25
26
  }
26
27
 
@@ -42,7 +43,7 @@ export function createWindowsPresence(
42
43
  const platform = options.platform ?? process.platform
43
44
  const probes = options.wslProbes ?? realWslProbes
44
45
  const wsl = isWsl(env, platform, probes)
45
- const native = (options.helper ?? env.NAMZU_WIN_HELPER)?.trim()
46
+ const native = resolveNativeHelper(options.helper, env)
46
47
  if (native) {
47
48
  if (!wsl && platform !== 'win32') return undefined
48
49
  const clean: NodeJS.ProcessEnv = {}