@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
|
@@ -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
|
+
}
|