@namzu/computer-use 4.5.0 → 4.7.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 (60) hide show
  1. package/CHANGELOG.md +22 -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/ladder.d.ts +2 -0
  14. package/dist/foreground/ladder.d.ts.map +1 -1
  15. package/dist/foreground/ladder.js +16 -1
  16. package/dist/foreground/ladder.js.map +1 -1
  17. package/dist/foreground/types.d.ts +8 -3
  18. package/dist/foreground/types.d.ts.map +1 -1
  19. package/dist/foreground/types.js.map +1 -1
  20. package/dist/foreground/windows.d.ts +1 -1
  21. package/dist/foreground/windows.d.ts.map +1 -1
  22. package/dist/foreground/windows.js +2 -1
  23. package/dist/foreground/windows.js.map +1 -1
  24. package/dist/index.d.ts +9 -0
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +7 -0
  27. package/dist/index.js.map +1 -1
  28. package/dist/native/env.d.ts +19 -0
  29. package/dist/native/env.d.ts.map +1 -0
  30. package/dist/native/env.js +29 -0
  31. package/dist/native/env.js.map +1 -0
  32. package/dist/native/process.d.ts +2 -2
  33. package/dist/native/process.d.ts.map +1 -1
  34. package/dist/native/process.js +19 -9
  35. package/dist/native/process.js.map +1 -1
  36. package/dist/native/protocol.d.ts +74 -6
  37. package/dist/native/protocol.d.ts.map +1 -1
  38. package/dist/native/protocol.js +79 -5
  39. package/dist/native/protocol.js.map +1 -1
  40. package/dist/native/watch.d.ts +35 -0
  41. package/dist/native/watch.d.ts.map +1 -0
  42. package/dist/native/watch.js +45 -0
  43. package/dist/native/watch.js.map +1 -0
  44. package/dist/presence/windows.d.ts +1 -1
  45. package/dist/presence/windows.d.ts.map +1 -1
  46. package/dist/presence/windows.js +2 -1
  47. package/dist/presence/windows.js.map +1 -1
  48. package/package.json +2 -2
  49. package/src/a11y/windows.ts +6 -4
  50. package/src/adapters/win32-native.ts +9 -8
  51. package/src/adapters/win32.ts +6 -5
  52. package/src/foreground/ladder.ts +15 -1
  53. package/src/foreground/types.ts +9 -3
  54. package/src/foreground/windows.ts +3 -2
  55. package/src/index.ts +14 -0
  56. package/src/native/env.ts +33 -0
  57. package/src/native/process.ts +23 -11
  58. package/src/native/protocol.ts +145 -5
  59. package/src/native/watch.ts +78 -0
  60. package/src/presence/windows.ts +3 -2
@@ -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 = {}