@namzu/computer-use 4.3.1 → 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.
Files changed (143) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/SubprocessComputerUseHost.d.ts +21 -1
  3. package/dist/SubprocessComputerUseHost.d.ts.map +1 -1
  4. package/dist/SubprocessComputerUseHost.js +67 -15
  5. package/dist/SubprocessComputerUseHost.js.map +1 -1
  6. package/dist/a11y/a11y.d.ts +16 -7
  7. package/dist/a11y/a11y.d.ts.map +1 -1
  8. package/dist/a11y/a11y.js +23 -5
  9. package/dist/a11y/a11y.js.map +1 -1
  10. package/dist/a11y/index.d.ts +7 -1
  11. package/dist/a11y/index.d.ts.map +1 -1
  12. package/dist/a11y/index.js +4 -1
  13. package/dist/a11y/index.js.map +1 -1
  14. package/dist/a11y/interface.d.ts +5 -0
  15. package/dist/a11y/interface.d.ts.map +1 -1
  16. package/dist/a11y/interface.js.map +1 -1
  17. package/dist/a11y/macos-helper.d.ts +80 -0
  18. package/dist/a11y/macos-helper.d.ts.map +1 -0
  19. package/dist/a11y/macos-helper.js +436 -0
  20. package/dist/a11y/macos-helper.js.map +1 -0
  21. package/dist/a11y/macos-signing.d.ts +56 -0
  22. package/dist/a11y/macos-signing.d.ts.map +1 -0
  23. package/dist/a11y/macos-signing.js +228 -0
  24. package/dist/a11y/macos-signing.js.map +1 -0
  25. package/dist/a11y/macos.d.ts +173 -0
  26. package/dist/a11y/macos.d.ts.map +1 -0
  27. package/dist/a11y/macos.js +271 -0
  28. package/dist/a11y/macos.js.map +1 -0
  29. package/dist/a11y/windows.d.ts +13 -2
  30. package/dist/a11y/windows.d.ts.map +1 -1
  31. package/dist/a11y/windows.js +29 -6
  32. package/dist/a11y/windows.js.map +1 -1
  33. package/dist/adapters/cua-driver/adapter.d.ts +26 -0
  34. package/dist/adapters/cua-driver/adapter.d.ts.map +1 -1
  35. package/dist/adapters/cua-driver/adapter.js +224 -13
  36. package/dist/adapters/cua-driver/adapter.js.map +1 -1
  37. package/dist/adapters/cua-driver/client.d.ts +5 -0
  38. package/dist/adapters/cua-driver/client.d.ts.map +1 -1
  39. package/dist/adapters/cua-driver/client.js +31 -0
  40. package/dist/adapters/cua-driver/client.js.map +1 -1
  41. package/dist/adapters/cua-driver/ui-tree.d.ts +1 -0
  42. package/dist/adapters/cua-driver/ui-tree.d.ts.map +1 -1
  43. package/dist/adapters/cua-driver/ui-tree.js +2 -0
  44. package/dist/adapters/cua-driver/ui-tree.js.map +1 -1
  45. package/dist/adapters/darwin-ax.d.ts +94 -0
  46. package/dist/adapters/darwin-ax.d.ts.map +1 -0
  47. package/dist/adapters/darwin-ax.js +754 -0
  48. package/dist/adapters/darwin-ax.js.map +1 -0
  49. package/dist/adapters/darwin.d.ts +43 -2
  50. package/dist/adapters/darwin.d.ts.map +1 -1
  51. package/dist/adapters/darwin.js +145 -7
  52. package/dist/adapters/darwin.js.map +1 -1
  53. package/dist/adapters/png.d.ts +6 -0
  54. package/dist/adapters/png.d.ts.map +1 -0
  55. package/dist/adapters/png.js +7 -0
  56. package/dist/adapters/png.js.map +1 -0
  57. package/dist/adapters/types.d.ts +5 -0
  58. package/dist/adapters/types.d.ts.map +1 -1
  59. package/dist/adapters/types.js.map +1 -1
  60. package/dist/adapters/win32-native.d.ts +53 -0
  61. package/dist/adapters/win32-native.d.ts.map +1 -0
  62. package/dist/adapters/win32-native.js +279 -0
  63. package/dist/adapters/win32-native.js.map +1 -0
  64. package/dist/adapters/win32.d.ts +19 -1
  65. package/dist/adapters/win32.d.ts.map +1 -1
  66. package/dist/adapters/win32.js +34 -8
  67. package/dist/adapters/win32.js.map +1 -1
  68. package/dist/foreground/guard.d.ts.map +1 -1
  69. package/dist/foreground/guard.js +2 -0
  70. package/dist/foreground/guard.js.map +1 -1
  71. package/dist/foreground/ladder.d.ts.map +1 -1
  72. package/dist/foreground/ladder.js +5 -1
  73. package/dist/foreground/ladder.js.map +1 -1
  74. package/dist/foreground/types.d.ts +6 -0
  75. package/dist/foreground/types.d.ts.map +1 -1
  76. package/dist/foreground/types.js.map +1 -1
  77. package/dist/foreground/windows.d.ts +2 -0
  78. package/dist/foreground/windows.d.ts.map +1 -1
  79. package/dist/foreground/windows.js +23 -11
  80. package/dist/foreground/windows.js.map +1 -1
  81. package/dist/native/a11y.d.ts +61 -0
  82. package/dist/native/a11y.d.ts.map +1 -0
  83. package/dist/native/a11y.js +197 -0
  84. package/dist/native/a11y.js.map +1 -0
  85. package/dist/native/foreground-monitor.d.ts +51 -0
  86. package/dist/native/foreground-monitor.d.ts.map +1 -0
  87. package/dist/native/foreground-monitor.js +120 -0
  88. package/dist/native/foreground-monitor.js.map +1 -0
  89. package/dist/native/presence.d.ts +24 -0
  90. package/dist/native/presence.d.ts.map +1 -0
  91. package/dist/native/presence.js +47 -0
  92. package/dist/native/presence.js.map +1 -0
  93. package/dist/native/process.d.ts +73 -0
  94. package/dist/native/process.d.ts.map +1 -0
  95. package/dist/native/process.js +292 -0
  96. package/dist/native/process.js.map +1 -0
  97. package/dist/native/protocol.d.ts +408 -0
  98. package/dist/native/protocol.d.ts.map +1 -0
  99. package/dist/native/protocol.js +527 -0
  100. package/dist/native/protocol.js.map +1 -0
  101. package/dist/native/shared.d.ts +16 -0
  102. package/dist/native/shared.d.ts.map +1 -0
  103. package/dist/native/shared.js +62 -0
  104. package/dist/native/shared.js.map +1 -0
  105. package/dist/presence/presence.d.ts +11 -2
  106. package/dist/presence/presence.d.ts.map +1 -1
  107. package/dist/presence/presence.js +15 -0
  108. package/dist/presence/presence.js.map +1 -1
  109. package/dist/presence/windows.d.ts +2 -0
  110. package/dist/presence/windows.d.ts.map +1 -1
  111. package/dist/presence/windows.js +14 -0
  112. package/dist/presence/windows.js.map +1 -1
  113. package/helper-macos/main.swift +1756 -0
  114. package/package.json +3 -2
  115. package/src/SubprocessComputerUseHost.ts +80 -10
  116. package/src/a11y/a11y.ts +36 -11
  117. package/src/a11y/index.ts +28 -1
  118. package/src/a11y/interface.ts +5 -0
  119. package/src/a11y/macos-helper.ts +538 -0
  120. package/src/a11y/macos-signing.ts +280 -0
  121. package/src/a11y/macos.ts +496 -0
  122. package/src/a11y/windows.ts +29 -4
  123. package/src/adapters/cua-driver/adapter.ts +245 -17
  124. package/src/adapters/cua-driver/client.ts +27 -0
  125. package/src/adapters/cua-driver/ui-tree.ts +2 -0
  126. package/src/adapters/darwin-ax.ts +920 -0
  127. package/src/adapters/darwin.ts +184 -4
  128. package/src/adapters/png.ts +5 -0
  129. package/src/adapters/types.ts +5 -0
  130. package/src/adapters/win32-native.ts +328 -0
  131. package/src/adapters/win32.ts +52 -5
  132. package/src/foreground/guard.ts +1 -0
  133. package/src/foreground/ladder.ts +5 -1
  134. package/src/foreground/types.ts +6 -0
  135. package/src/foreground/windows.ts +24 -11
  136. package/src/native/a11y.ts +267 -0
  137. package/src/native/foreground-monitor.ts +165 -0
  138. package/src/native/presence.ts +58 -0
  139. package/src/native/process.ts +335 -0
  140. package/src/native/protocol.ts +963 -0
  141. package/src/native/shared.ts +75 -0
  142. package/src/presence/presence.ts +23 -2
  143. package/src/presence/windows.ts +14 -0
@@ -0,0 +1,58 @@
1
+ import type { Presence, PresenceMark } from '../presence/presence.js'
2
+ import { NativeWinProcess, type NativeWinProcessOptions } from './process.js'
3
+
4
+ /**
5
+ * The presence overlay drawn by the native helper: the agent's cursor and a
6
+ * frame round the operated window, in two layered, click-through windows the
7
+ * helper owns. It takes no input and never changes the foreground window.
8
+ *
9
+ * The overlay is not excluded from screen capture. A screenshot may show it,
10
+ * and that is honest: the picture shows where the agent pointed. (The
11
+ * PowerShell overlay this replaces hid itself from capture, which together
12
+ * with a topmost transparent window reads as evasion.)
13
+ *
14
+ * A failure to show it never fails an action.
15
+ */
16
+ export class NativePresence implements Presence {
17
+ private readonly process: NativeWinProcess
18
+
19
+ constructor(options: NativeWinProcessOptions) {
20
+ this.process = new NativeWinProcess(options)
21
+ }
22
+
23
+ mark(mark: PresenceMark): void {
24
+ void this.process
25
+ .request(
26
+ {
27
+ op: 'presence.act',
28
+ windowId: windowIdOf(mark.hwnd),
29
+ kind: mark.kind,
30
+ ...(mark.at ? { at: { x: Math.round(mark.at.x), y: Math.round(mark.at.y) } } : {}),
31
+ },
32
+ 4_000,
33
+ )
34
+ .catch(() => undefined)
35
+ }
36
+
37
+ warm(): void {
38
+ this.process.warm()
39
+ }
40
+
41
+ hide(): void {
42
+ // Only a helper that is running has anything to hide.
43
+ if (this.process.isUp)
44
+ void this.process.request({ op: 'presence.hide' }, 4_000).catch(() => undefined)
45
+ }
46
+
47
+ release(): void {
48
+ this.process.release()
49
+ }
50
+
51
+ async dispose(): Promise<void> {
52
+ await this.process.close()
53
+ }
54
+ }
55
+
56
+ function windowIdOf(hwnd: number): string {
57
+ return `w:${hwnd.toString(16).padStart(16, '0')}`
58
+ }
@@ -0,0 +1,335 @@
1
+ /**
2
+ * The helper as a process: start it, greet it, send it requests as lines and
3
+ * match the replies to them by id.
4
+ *
5
+ * The helper is the compiled `namzu-win` executable. It is started directly,
6
+ * with no shell and no script engine, and answers JSON lines on its standard
7
+ * output. Everything the TypeScript side asks of it goes through
8
+ * {@link NativeWinProcess.request}, which gives a reply body or throws an
9
+ * {@link A11yError} carrying the helper's own error code.
10
+ */
11
+
12
+ import { spawn } from 'node:child_process'
13
+ import { createInterface } from 'node:readline'
14
+ import { A11yError } from '../a11y/interface.js'
15
+ import {
16
+ NATIVE_PROTOCOL,
17
+ type NativeHello,
18
+ type NativeOp,
19
+ NativeProtocolError,
20
+ type NativeReplyBodies,
21
+ type NativeRequest,
22
+ decodeReply,
23
+ encodeRequest,
24
+ } from './protocol.js'
25
+
26
+ /** The ends of a started helper; the real one is a child process. */
27
+ export interface NativeChannel {
28
+ write(line: string): void
29
+ onLine(handler: (line: string) => void): void
30
+ onExit(handler: () => void): void
31
+ kill(): void
32
+ }
33
+
34
+ export interface NativeWinProcessOptions {
35
+ /** The path of `namzu-win.exe`. */
36
+ readonly executable: string
37
+ /** Sent in `hello`, so the helper's log can say who asked. */
38
+ readonly cliVersion?: string
39
+ readonly env?: NodeJS.ProcessEnv
40
+ /** For tests. */
41
+ readonly spawnHelper?: () => NativeChannel
42
+ /** Waiting for the greeting, ms. Default 10 000. */
43
+ readonly startTimeoutMs?: number
44
+ /** How many times a helper that ended mid-session is started again. Default 3. */
45
+ readonly maxRestarts?: number
46
+ /** A helper that ran this long before ending was healthy: the restart count starts again. Default 60 000. */
47
+ readonly stableAfterMs?: number
48
+ }
49
+
50
+ /** The client gives up on a reply this long after the helper should have sent it, ms. */
51
+ const BACKSTOP_MS = 1_500
52
+
53
+ function spawnHelperProcess(options: NativeWinProcessOptions): NativeChannel {
54
+ // The executable alone: no shell, no arguments, no window.
55
+ const child = spawn(options.executable, [], {
56
+ env: options.env,
57
+ stdio: ['pipe', 'pipe', 'ignore'],
58
+ windowsHide: true,
59
+ shell: false,
60
+ })
61
+ child.stdin.on('error', () => undefined)
62
+ child.on('error', () => undefined)
63
+ return {
64
+ write: (line) => {
65
+ child.stdin.write(`${line}\n`)
66
+ },
67
+ onLine: (handler) => createInterface({ input: child.stdout }).on('line', handler),
68
+ onExit: (handler) => {
69
+ child.once('exit', handler)
70
+ child.once('error', handler)
71
+ },
72
+ kill: () => {
73
+ try {
74
+ // Closing the input ends the helper by itself.
75
+ child.stdin.end()
76
+ } catch {
77
+ // Already closed.
78
+ }
79
+ setTimeout(() => child.kill(), 2_000).unref()
80
+ },
81
+ }
82
+ }
83
+
84
+ interface Pending {
85
+ readonly op: NativeOp
86
+ readonly resolve: (body: unknown) => void
87
+ readonly reject: (error: A11yError) => void
88
+ readonly timer: NodeJS.Timeout
89
+ readonly cleanup: () => void
90
+ }
91
+
92
+ export class NativeWinProcess {
93
+ private channel: NativeChannel | undefined
94
+ private starting: Promise<boolean> | undefined
95
+ private up = false
96
+ private dead = false
97
+ private closed = false
98
+ private restarts = 0
99
+ private runs = 0
100
+ private nextId = 1
101
+ private greeting: NativeHello | undefined
102
+ private readonly pending = new Map<number, Pending>()
103
+
104
+ constructor(private readonly options: NativeWinProcessOptions) {}
105
+
106
+ /** True once the helper greeted and has not ended since. */
107
+ get isUp(): boolean {
108
+ return this.up && !this.dead && !this.closed
109
+ }
110
+
111
+ /** Counts the helper's starts: a marker taken before a restart is stale. */
112
+ get epoch(): number {
113
+ return this.runs
114
+ }
115
+
116
+ /** What the helper said in `hello`, once it has. */
117
+ get hello(): NativeHello | undefined {
118
+ return this.greeting
119
+ }
120
+
121
+ warm(): void {
122
+ if (!this.dead && !this.closed) void this.start()
123
+ }
124
+
125
+ /** Ends the helper while nobody uses it; the next question starts it again. */
126
+ release(): void {
127
+ const channel = this.channel
128
+ if (!channel) return
129
+ // Replaced, not ended: its exit is not a crash (see the exit handler).
130
+ this.channel = undefined
131
+ this.starting = undefined
132
+ this.up = false
133
+ this.failAll(new A11yError('helper_exited', 'the native helper was released'))
134
+ this.sendQuit(channel)
135
+ }
136
+
137
+ ready(): Promise<boolean> {
138
+ if (this.dead || this.closed) return Promise.resolve(false)
139
+ return this.start()
140
+ }
141
+
142
+ async close(): Promise<void> {
143
+ this.closed = true
144
+ const channel = this.channel
145
+ this.channel = undefined
146
+ this.up = false
147
+ this.failAll(new A11yError('closed', 'the native helper was closed'))
148
+ if (channel) this.sendQuit(channel)
149
+ }
150
+
151
+ /**
152
+ * One request, one reply. `limitMs` is how long the helper has to answer
153
+ * (it is also what the request's own timeout says); a little more is
154
+ * allowed before the client gives up on a helper that says nothing.
155
+ */
156
+ async request<K extends NativeOp>(
157
+ request: NativeRequest & { readonly op: K },
158
+ limitMs: number,
159
+ signal?: AbortSignal,
160
+ ): Promise<NativeReplyBodies[K]> {
161
+ if (signal?.aborted) throw new A11yError('aborted', 'cancelled')
162
+ if (!(await this.ready())) throw new A11yError('unavailable', 'the native helper is off')
163
+ const channel = this.channel
164
+ if (!channel || !this.up) throw new A11yError('helper_exited', 'the native helper ended')
165
+ const id = this.nextId++
166
+ return new Promise<NativeReplyBodies[K]>((resolve, reject) => {
167
+ const settle = (): void => {
168
+ signal?.removeEventListener('abort', onAbort)
169
+ }
170
+ const onAbort = (): void => {
171
+ if (!this.pending.delete(id)) return
172
+ clearTimeout(timer)
173
+ this.cancelAtHelper(id)
174
+ reject(new A11yError('aborted', 'cancelled'))
175
+ }
176
+ const timer = setTimeout(() => {
177
+ if (!this.pending.delete(id)) return
178
+ settle()
179
+ this.cancelAtHelper(id)
180
+ reject(new A11yError('timeout', `the native helper did not answer ${request.op}`))
181
+ }, limitMs + BACKSTOP_MS)
182
+ timer.unref?.()
183
+ signal?.addEventListener('abort', onAbort, { once: true })
184
+ this.pending.set(id, {
185
+ op: request.op,
186
+ resolve: resolve as (body: unknown) => void,
187
+ reject,
188
+ timer,
189
+ cleanup: settle,
190
+ })
191
+ try {
192
+ channel.write(encodeRequest(id, request))
193
+ } catch {
194
+ this.pending.delete(id)
195
+ clearTimeout(timer)
196
+ settle()
197
+ reject(new A11yError('helper_exited', 'the native helper ended'))
198
+ }
199
+ })
200
+ }
201
+
202
+ private sendQuit(channel: NativeChannel): void {
203
+ try {
204
+ channel.write(encodeRequest(this.nextId++, { op: 'quit' }))
205
+ } catch {
206
+ // Already gone.
207
+ }
208
+ channel.kill()
209
+ }
210
+
211
+ /** Tells the helper to stop work nobody waits for any more. Never throws. */
212
+ private cancelAtHelper(target: number): void {
213
+ try {
214
+ this.channel?.write(encodeRequest(this.nextId++, { op: 'cancel', target }))
215
+ } catch {
216
+ // The helper is gone; there is nothing to cancel.
217
+ }
218
+ }
219
+
220
+ private failAll(error: A11yError): void {
221
+ for (const [id, entry] of [...this.pending]) {
222
+ this.pending.delete(id)
223
+ clearTimeout(entry.timer)
224
+ entry.cleanup()
225
+ entry.reject(error)
226
+ }
227
+ }
228
+
229
+ private onReply(line: string): void {
230
+ let id: number
231
+ try {
232
+ const parsed = JSON.parse(line) as { id?: unknown }
233
+ if (typeof parsed.id !== 'number') return
234
+ id = parsed.id
235
+ } catch {
236
+ return
237
+ }
238
+ const entry = this.pending.get(id)
239
+ // An answer to a request nobody awaits (a cancel, a quit, one that timed out).
240
+ if (!entry) return
241
+ this.pending.delete(id)
242
+ clearTimeout(entry.timer)
243
+ entry.cleanup()
244
+ try {
245
+ const reply = decodeReply(entry.op, line)
246
+ if (reply.ok) entry.resolve(reply.body)
247
+ else entry.reject(new A11yError(reply.code, reply.message))
248
+ } catch (error) {
249
+ entry.reject(
250
+ new A11yError(
251
+ 'bad_reply',
252
+ error instanceof NativeProtocolError
253
+ ? error.message
254
+ : 'the native helper sent a bad line',
255
+ ),
256
+ )
257
+ }
258
+ }
259
+
260
+ private start(): Promise<boolean> {
261
+ this.starting ??= new Promise<boolean>((resolve) => {
262
+ let channel: NativeChannel
263
+ try {
264
+ channel = (this.options.spawnHelper ?? (() => spawnHelperProcess(this.options)))()
265
+ } catch {
266
+ this.dead = true
267
+ resolve(false)
268
+ return
269
+ }
270
+ this.channel = channel
271
+ this.runs += 1
272
+ let greeted = false
273
+ let greetedAt = 0
274
+ const fail = (): void => {
275
+ this.dead = true
276
+ this.up = false
277
+ resolve(false)
278
+ channel.kill()
279
+ }
280
+ const timer = setTimeout(fail, this.options.startTimeoutMs ?? 10_000)
281
+ timer.unref?.()
282
+ channel.onExit(() => {
283
+ clearTimeout(timer)
284
+ // Closed, or already replaced: nothing left to do for this one.
285
+ if (this.channel !== channel) {
286
+ resolve(false)
287
+ return
288
+ }
289
+ this.up = false
290
+ this.failAll(new A11yError('helper_exited', 'the native helper ended'))
291
+ // A helper that was running and ended is started again by the next question,
292
+ // a few times; one that never came up stays off.
293
+ // A long, healthy run does not count against the few restarts a session gets.
294
+ if (greeted && Date.now() - greetedAt >= (this.options.stableAfterMs ?? 60_000))
295
+ this.restarts = 0
296
+ if (greeted && !this.closed && this.restarts < (this.options.maxRestarts ?? 3)) {
297
+ this.restarts++
298
+ this.channel = undefined
299
+ this.starting = undefined
300
+ } else this.dead = true
301
+ resolve(false)
302
+ })
303
+ channel.onLine((line) => {
304
+ if (greeted) {
305
+ this.onReply(line)
306
+ return
307
+ }
308
+ greeted = true
309
+ clearTimeout(timer)
310
+ try {
311
+ const reply = decodeReply('hello', line)
312
+ if (!reply.ok) {
313
+ fail()
314
+ return
315
+ }
316
+ this.greeting = reply.body
317
+ this.up = true
318
+ greetedAt = Date.now()
319
+ resolve(true)
320
+ } catch {
321
+ fail()
322
+ }
323
+ })
324
+ // 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
+ )
332
+ })
333
+ return this.starting
334
+ }
335
+ }