@namzu/computer-use 3.1.0 → 4.0.1
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 +64 -1
- package/README.md +3 -3
- package/dist/SubprocessComputerUseHost.d.ts +58 -10
- package/dist/SubprocessComputerUseHost.d.ts.map +1 -1
- package/dist/SubprocessComputerUseHost.js +214 -47
- package/dist/SubprocessComputerUseHost.js.map +1 -1
- package/dist/adapters/cua-driver/adapter.d.ts +171 -10
- package/dist/adapters/cua-driver/adapter.d.ts.map +1 -1
- package/dist/adapters/cua-driver/adapter.js +1479 -161
- package/dist/adapters/cua-driver/adapter.js.map +1 -1
- package/dist/adapters/cua-driver/client.d.ts +16 -0
- package/dist/adapters/cua-driver/client.d.ts.map +1 -1
- package/dist/adapters/cua-driver/client.js +54 -5
- package/dist/adapters/cua-driver/client.js.map +1 -1
- package/dist/adapters/cua-driver/escalation.d.ts +67 -0
- package/dist/adapters/cua-driver/escalation.d.ts.map +1 -0
- package/dist/adapters/cua-driver/escalation.js +82 -0
- package/dist/adapters/cua-driver/escalation.js.map +1 -0
- package/dist/adapters/cua-driver/keys.d.ts +5 -0
- package/dist/adapters/cua-driver/keys.d.ts.map +1 -1
- package/dist/adapters/cua-driver/keys.js +23 -0
- package/dist/adapters/cua-driver/keys.js.map +1 -1
- package/dist/adapters/cua-driver/observe.d.ts +99 -0
- package/dist/adapters/cua-driver/observe.d.ts.map +1 -0
- package/dist/adapters/cua-driver/observe.js +227 -0
- package/dist/adapters/cua-driver/observe.js.map +1 -0
- package/dist/adapters/cua-driver/provision.d.ts +13 -13
- package/dist/adapters/cua-driver/provision.js +13 -13
- package/dist/adapters/cua-driver/ui-tree.d.ts +75 -2
- package/dist/adapters/cua-driver/ui-tree.d.ts.map +1 -1
- package/dist/adapters/cua-driver/ui-tree.js +142 -14
- package/dist/adapters/cua-driver/ui-tree.js.map +1 -1
- package/dist/adapters/types.d.ts +4 -4
- package/dist/adapters/types.d.ts.map +1 -1
- package/dist/adapters/types.js.map +1 -1
- package/dist/adapters/win32.d.ts +10 -0
- package/dist/adapters/win32.d.ts.map +1 -1
- package/dist/adapters/win32.js +12 -2
- package/dist/adapters/win32.js.map +1 -1
- package/dist/errors.d.ts +19 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +26 -0
- package/dist/errors.js.map +1 -1
- package/dist/foreground/guard.d.ts +31 -0
- package/dist/foreground/guard.d.ts.map +1 -0
- package/dist/foreground/guard.js +156 -0
- package/dist/foreground/guard.js.map +1 -0
- package/dist/foreground/index.d.ts +11 -0
- package/dist/foreground/index.d.ts.map +1 -0
- package/dist/foreground/index.js +7 -0
- package/dist/foreground/index.js.map +1 -0
- package/dist/foreground/ladder.d.ts +143 -0
- package/dist/foreground/ladder.d.ts.map +1 -0
- package/dist/foreground/ladder.js +505 -0
- package/dist/foreground/ladder.js.map +1 -0
- package/dist/foreground/lease.d.ts +69 -0
- package/dist/foreground/lease.d.ts.map +1 -0
- package/dist/foreground/lease.js +182 -0
- package/dist/foreground/lease.js.map +1 -0
- package/dist/foreground/types.d.ts +287 -0
- package/dist/foreground/types.d.ts.map +1 -0
- package/dist/foreground/types.js +32 -0
- package/dist/foreground/types.js.map +1 -0
- package/dist/foreground/windows-helper-script.d.ts +19 -0
- package/dist/foreground/windows-helper-script.d.ts.map +1 -0
- package/dist/foreground/windows-helper-script.js +472 -0
- package/dist/foreground/windows-helper-script.js.map +1 -0
- package/dist/foreground/windows-monitor.d.ts +75 -0
- package/dist/foreground/windows-monitor.d.ts.map +1 -0
- package/dist/foreground/windows-monitor.js +216 -0
- package/dist/foreground/windows-monitor.js.map +1 -0
- package/dist/foreground/windows.d.ts +21 -0
- package/dist/foreground/windows.d.ts.map +1 -0
- package/dist/foreground/windows.js +54 -0
- package/dist/foreground/windows.js.map +1 -0
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/presence/helper-script.d.ts +39 -0
- package/dist/presence/helper-script.d.ts.map +1 -0
- package/dist/presence/helper-script.js +320 -0
- package/dist/presence/helper-script.js.map +1 -0
- package/dist/presence/index.d.ts +6 -0
- package/dist/presence/index.d.ts.map +1 -0
- package/dist/presence/index.js +4 -0
- package/dist/presence/index.js.map +1 -0
- package/dist/presence/presence.d.ts +70 -0
- package/dist/presence/presence.d.ts.map +1 -0
- package/dist/presence/presence.js +147 -0
- package/dist/presence/presence.js.map +1 -0
- package/dist/presence/windows.d.ts +19 -0
- package/dist/presence/windows.d.ts.map +1 -0
- package/dist/presence/windows.js +39 -0
- package/dist/presence/windows.js.map +1 -0
- package/dist/util/call-scope.d.ts +34 -0
- package/dist/util/call-scope.d.ts.map +1 -0
- package/dist/util/call-scope.js +58 -0
- package/dist/util/call-scope.js.map +1 -0
- package/package.json +2 -2
- package/src/SubprocessComputerUseHost.ts +288 -46
- package/src/adapters/cua-driver/adapter.ts +1754 -175
- package/src/adapters/cua-driver/client.ts +59 -2
- package/src/adapters/cua-driver/escalation.ts +137 -0
- package/src/adapters/cua-driver/keys.ts +20 -0
- package/src/adapters/cua-driver/observe.ts +310 -0
- package/src/adapters/cua-driver/provision.ts +13 -13
- package/src/adapters/cua-driver/ui-tree.ts +219 -15
- package/src/adapters/types.ts +8 -3
- package/src/adapters/win32.ts +26 -3
- package/src/errors.ts +32 -0
- package/src/foreground/guard.ts +182 -0
- package/src/foreground/index.ts +45 -0
- package/src/foreground/ladder.ts +616 -0
- package/src/foreground/lease.ts +232 -0
- package/src/foreground/types.ts +318 -0
- package/src/foreground/windows-helper-script.ts +471 -0
- package/src/foreground/windows-monitor.ts +288 -0
- package/src/foreground/windows.ts +73 -0
- package/src/index.ts +15 -1
- package/src/presence/helper-script.ts +323 -0
- package/src/presence/index.ts +17 -0
- package/src/presence/presence.ts +195 -0
- package/src/presence/windows.ts +54 -0
- package/src/util/call-scope.ts +73 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type ChildProcessWithoutNullStreams, spawn } from 'node:child_process'
|
|
2
|
+
import { CallAbortedError } from '../../util/call-scope.js'
|
|
2
3
|
import { SpawnError } from '../../util/spawn.js'
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -67,6 +68,13 @@ export interface McpProcessCallOptions {
|
|
|
67
68
|
readonly expectedStarts?: number
|
|
68
69
|
/** Record the process that actually received this call. */
|
|
69
70
|
readonly onProcess?: (starts: number) => void
|
|
71
|
+
/**
|
|
72
|
+
* Stops waiting for the answer when aborted: the call rejects with
|
|
73
|
+
* `CallAbortedError` at once. The server is not stopped, and the
|
|
74
|
+
* request counts as in flight (see {@link McpStdioClient.settled}) until it
|
|
75
|
+
* answers or its timeout kills the server.
|
|
76
|
+
*/
|
|
77
|
+
readonly signal?: AbortSignal
|
|
70
78
|
}
|
|
71
79
|
|
|
72
80
|
export interface McpStdioClientOptions {
|
|
@@ -97,6 +105,10 @@ interface Pending {
|
|
|
97
105
|
readonly resolve: (value: unknown) => void
|
|
98
106
|
readonly reject: (error: Error) => void
|
|
99
107
|
readonly timer: NodeJS.Timeout
|
|
108
|
+
/** The waiter gave up (abort); the answer, if one comes, is dropped. */
|
|
109
|
+
abandoned?: boolean
|
|
110
|
+
/** Called when the server answers, errors or is lost: the request is no longer in flight. */
|
|
111
|
+
readonly done?: () => void
|
|
100
112
|
}
|
|
101
113
|
|
|
102
114
|
interface Running {
|
|
@@ -118,9 +130,26 @@ export class McpStdioClient {
|
|
|
118
130
|
private nextId = 1
|
|
119
131
|
private disposed = false
|
|
120
132
|
private startCount = 0
|
|
133
|
+
private inFlight = 0
|
|
134
|
+
private idleWaiters: Array<() => void> = []
|
|
121
135
|
|
|
122
136
|
constructor(private readonly options: McpStdioClientOptions) {}
|
|
123
137
|
|
|
138
|
+
/**
|
|
139
|
+
* Resolves once no request is in flight: every request written has been
|
|
140
|
+
* answered, or lost to a timeout, an exit or {@link dispose}. Requests the
|
|
141
|
+
* callers abandoned count. Bounded by the per-request timeout.
|
|
142
|
+
*/
|
|
143
|
+
settled(): Promise<void> {
|
|
144
|
+
if (this.inFlight === 0) return Promise.resolve()
|
|
145
|
+
return new Promise((resolve) => this.idleWaiters.push(resolve))
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private requestDone(): void {
|
|
149
|
+
this.inFlight = Math.max(0, this.inFlight - 1)
|
|
150
|
+
if (this.inFlight === 0) for (const wake of this.idleWaiters.splice(0)) wake()
|
|
151
|
+
}
|
|
152
|
+
|
|
124
153
|
/** How many processes this client has started; a restart counts again. */
|
|
125
154
|
get starts(): number {
|
|
126
155
|
return this.startCount
|
|
@@ -143,11 +172,13 @@ export class McpStdioClient {
|
|
|
143
172
|
timeoutMs?: number,
|
|
144
173
|
process?: McpProcessCallOptions,
|
|
145
174
|
): Promise<McpToolResult> {
|
|
175
|
+
if (process?.signal?.aborted) throw new CallAbortedError(name, false)
|
|
146
176
|
const running = await this.ensureStarted()
|
|
147
177
|
if (process?.expectedStarts !== undefined && process.expectedStarts !== running.startOrdinal)
|
|
148
178
|
throw new McpToolError(name, 'stale UI snapshot; take a new one')
|
|
179
|
+
if (process?.signal?.aborted) throw new CallAbortedError(name, false)
|
|
149
180
|
process?.onProcess?.(running.startOrdinal)
|
|
150
|
-
return this.callOn(running, name, args, timeoutMs)
|
|
181
|
+
return this.callOn(running, name, args, timeoutMs, process?.signal)
|
|
151
182
|
}
|
|
152
183
|
|
|
153
184
|
async dispose(): Promise<void> {
|
|
@@ -228,6 +259,7 @@ export class McpStdioClient {
|
|
|
228
259
|
for (const [id, pending] of running.pending) {
|
|
229
260
|
clearTimeout(pending.timer)
|
|
230
261
|
running.pending.delete(id)
|
|
262
|
+
pending.done?.()
|
|
231
263
|
pending.reject(
|
|
232
264
|
this.lostRequest(running, pending.method, exitCode, signal, timedOut, spawnError),
|
|
233
265
|
)
|
|
@@ -281,12 +313,14 @@ export class McpStdioClient {
|
|
|
281
313
|
name: string,
|
|
282
314
|
args: Record<string, unknown>,
|
|
283
315
|
timeoutMs?: number,
|
|
316
|
+
signal?: AbortSignal,
|
|
284
317
|
): Promise<McpToolResult> {
|
|
285
318
|
const raw = (await this.request(
|
|
286
319
|
running,
|
|
287
320
|
'tools/call',
|
|
288
321
|
{ name, arguments: args },
|
|
289
322
|
timeoutMs ?? this.options.requestTimeoutMs ?? 15_000,
|
|
323
|
+
signal,
|
|
290
324
|
)) as {
|
|
291
325
|
content?: unknown
|
|
292
326
|
structuredContent?: unknown
|
|
@@ -313,6 +347,7 @@ export class McpStdioClient {
|
|
|
313
347
|
method: string,
|
|
314
348
|
params: unknown,
|
|
315
349
|
timeoutMs: number,
|
|
350
|
+
signal?: AbortSignal,
|
|
316
351
|
): Promise<unknown> {
|
|
317
352
|
if (running.exited) {
|
|
318
353
|
return Promise.reject(new Error(`${this.options.command} is not running.`))
|
|
@@ -320,6 +355,7 @@ export class McpStdioClient {
|
|
|
320
355
|
if (this.disposed && running.killedFor === 'dispose') {
|
|
321
356
|
return Promise.reject(new Error(`${this.options.command}: the client was disposed.`))
|
|
322
357
|
}
|
|
358
|
+
if (signal?.aborted) return Promise.reject(new CallAbortedError(method, false))
|
|
323
359
|
const id = this.nextId++
|
|
324
360
|
return new Promise((resolve, reject) => {
|
|
325
361
|
const timer = setTimeout(() => {
|
|
@@ -329,7 +365,26 @@ export class McpStdioClient {
|
|
|
329
365
|
running.killedFor = 'timeout'
|
|
330
366
|
running.child.kill('SIGKILL')
|
|
331
367
|
}, timeoutMs)
|
|
332
|
-
|
|
368
|
+
const entry: Pending = {
|
|
369
|
+
method,
|
|
370
|
+
resolve,
|
|
371
|
+
reject,
|
|
372
|
+
timer,
|
|
373
|
+
done: () => {
|
|
374
|
+
signal?.removeEventListener('abort', onAbort)
|
|
375
|
+
this.requestDone()
|
|
376
|
+
},
|
|
377
|
+
}
|
|
378
|
+
const onAbort = (): void => {
|
|
379
|
+
if (!running.pending.has(id) || entry.abandoned) return
|
|
380
|
+
// Stop waiting; the request stays pending so its answer (or the exit that
|
|
381
|
+
// ends it) still closes it out of the in-flight count.
|
|
382
|
+
entry.abandoned = true
|
|
383
|
+
reject(new CallAbortedError(method, true))
|
|
384
|
+
}
|
|
385
|
+
signal?.addEventListener('abort', onAbort, { once: true })
|
|
386
|
+
running.pending.set(id, entry)
|
|
387
|
+
this.inFlight++
|
|
333
388
|
running.child.stdin.write(`${JSON.stringify({ jsonrpc: '2.0', id, method, params })}\n`)
|
|
334
389
|
})
|
|
335
390
|
}
|
|
@@ -379,6 +434,8 @@ export class McpStdioClient {
|
|
|
379
434
|
if (!pending) return
|
|
380
435
|
running.pending.delete(message.id)
|
|
381
436
|
clearTimeout(pending.timer)
|
|
437
|
+
pending.done?.()
|
|
438
|
+
if (pending.abandoned) return
|
|
382
439
|
if (message.error) {
|
|
383
440
|
pending.reject(
|
|
384
441
|
new McpProtocolError(
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import type { ActionDelivery, ActionRetrySafety } from '@namzu/sdk'
|
|
2
|
+
import { type Observation, type ObservedVerdict, isCut, sameObservation } from './observe.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* When an input that could not be confirmed is tried again, and when it must not be.
|
|
6
|
+
*
|
|
7
|
+
* A background input the driver cannot confirm is judged by reading the window
|
|
8
|
+
* afterwards. There are three honest outcomes besides "the effect was seen":
|
|
9
|
+
*
|
|
10
|
+
* - no effect: the window read the same before and after, on every read over
|
|
11
|
+
* the wait the effect would take, no window of the program opened, the
|
|
12
|
+
* control the text went to (where there is one) holds the same text, and the
|
|
13
|
+
* window was not changing by itself. Nothing the input did is left to undo,
|
|
14
|
+
* so the same input may be sent again, on the screen;
|
|
15
|
+
* - unexplained: the window changed, but not in a way that shows this input
|
|
16
|
+
* did it. Sending it again could do it twice, so it is not retried;
|
|
17
|
+
* - unknown: the window could not be read well enough to say either.
|
|
18
|
+
*
|
|
19
|
+
* The retry is made once. After it the answer is final, whatever it is.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** What the control that took the text read before and after (a classic edit control), when one was read. */
|
|
23
|
+
export type EditStill = 'unchanged' | 'changed' | 'unknown'
|
|
24
|
+
|
|
25
|
+
export interface StillnessReads {
|
|
26
|
+
readonly before: Observation
|
|
27
|
+
/** A read taken just before `before`: unequal reads mean the window was changing by itself. */
|
|
28
|
+
readonly earlier?: Observation
|
|
29
|
+
/** Every read after the input, in order. */
|
|
30
|
+
readonly afters: readonly Observation[]
|
|
31
|
+
/** A new window of the same program appeared. */
|
|
32
|
+
readonly opened: boolean
|
|
33
|
+
readonly edit?: EditStill
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The fewest reads that can show a window holding still. */
|
|
37
|
+
export const MIN_STILL_READS = 2
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* What the reads show when no effect of the input was recognised in them: that
|
|
41
|
+
* the input provably did nothing, or that the window changed unexplained.
|
|
42
|
+
* Undefined when the reads cannot tell (too few, nothing to read, a window that
|
|
43
|
+
* changes by itself, a text control that could not be read).
|
|
44
|
+
*/
|
|
45
|
+
export function judgeStillness(reads: StillnessReads): ObservedVerdict | undefined {
|
|
46
|
+
if (reads.opened || reads.afters.length < MIN_STILL_READS) return undefined
|
|
47
|
+
if (reads.before.size === 0) return undefined
|
|
48
|
+
if (reads.earlier !== undefined && !sameObservation(reads.earlier, reads.before)) return undefined
|
|
49
|
+
if (reads.edit === 'unknown') return undefined
|
|
50
|
+
// A read that left something out (a cut text, a cut walk) cannot show that nothing changed.
|
|
51
|
+
if (isCut(reads.before) || reads.afters.some(isCut)) return undefined
|
|
52
|
+
const moved =
|
|
53
|
+
reads.edit === 'changed' || reads.afters.some((read) => !sameObservation(reads.before, read))
|
|
54
|
+
if (moved)
|
|
55
|
+
return {
|
|
56
|
+
delivery: 'delivered_unverified',
|
|
57
|
+
retrySafety: 'unsafe',
|
|
58
|
+
outcome: 'unexplained',
|
|
59
|
+
detail:
|
|
60
|
+
'The window changed after the input, but not in a way that shows this input did it, so sending it again could do it twice. Look at the window before repeating it.',
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
delivery: 'delivered_unverified',
|
|
64
|
+
retrySafety: 'safe',
|
|
65
|
+
outcome: 'no_effect',
|
|
66
|
+
detail: 'The window did not change after the input: nothing in it took the input.',
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Keys whose effect (where the focus or caret is, how far a view scrolled) the controls of a window do not show. */
|
|
71
|
+
const UNOBSERVABLE_KEYS = /^(tab|up|down|left|right|home|end|pageup|pagedown|shift|ctrl|alt|win)$/i
|
|
72
|
+
|
|
73
|
+
/** The input kinds whose absence of effect a read of the window can show. */
|
|
74
|
+
export function canBeJudgedByStillness(action: {
|
|
75
|
+
readonly type: string
|
|
76
|
+
readonly keys?: string
|
|
77
|
+
}): boolean {
|
|
78
|
+
if (action.type === 'type_text') return true
|
|
79
|
+
if (action.type !== 'key' || action.keys === undefined) return false
|
|
80
|
+
const main = action.keys.trim().split('+').pop()?.trim() ?? ''
|
|
81
|
+
if (UNOBSERVABLE_KEYS.test(main)) return false
|
|
82
|
+
// Enter, Backspace and Delete, and the undo, redo, cut and paste chords, change text a tree read may not show.
|
|
83
|
+
const chord =
|
|
84
|
+
action.keys.toLowerCase().includes('ctrl') || action.keys.toLowerCase().includes('cmd')
|
|
85
|
+
if (/^(enter|return|backspace|delete|del|space)$/i.test(main)) return false
|
|
86
|
+
if (chord && /^(z|y|x|v)$/i.test(main)) return false
|
|
87
|
+
return true
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** One attempt as the retry decision sees it. */
|
|
91
|
+
export interface AttemptSummary {
|
|
92
|
+
readonly delivery: ActionDelivery
|
|
93
|
+
readonly retrySafety: ActionRetrySafety
|
|
94
|
+
readonly detail?: string
|
|
95
|
+
readonly outcome?: 'no_effect' | 'unexplained'
|
|
96
|
+
/** The attempt used the screen (the foreground ladder). */
|
|
97
|
+
readonly onScreen: boolean
|
|
98
|
+
/** The ladder did not run it: the code says why (nothing was sent). */
|
|
99
|
+
readonly blocked?: string
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Whether a first attempt that left no effect is tried again on the screen. */
|
|
103
|
+
export function shouldRetryOnScreen(
|
|
104
|
+
first: AttemptSummary,
|
|
105
|
+
options: {
|
|
106
|
+
readonly hasScreen: boolean
|
|
107
|
+
readonly explicitMode: 'background' | 'foreground' | undefined
|
|
108
|
+
},
|
|
109
|
+
): boolean {
|
|
110
|
+
return (
|
|
111
|
+
options.hasScreen &&
|
|
112
|
+
options.explicitMode === undefined &&
|
|
113
|
+
!first.onScreen &&
|
|
114
|
+
first.outcome === 'no_effect' &&
|
|
115
|
+
first.retrySafety === 'safe'
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const WAITING = new Set(['user_active', 'lease_busy'])
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The detail of the final result once a background attempt had no effect and
|
|
123
|
+
* the retry on the screen was made (or could not be).
|
|
124
|
+
*/
|
|
125
|
+
export function escalationDetail(second: AttemptSummary): string {
|
|
126
|
+
const base = 'The first attempt, in the background, had no effect: the window did not change.'
|
|
127
|
+
if (second.blocked !== undefined) {
|
|
128
|
+
if (WAITING.has(second.blocked))
|
|
129
|
+
return `Waiting for you to be idle: ${base} Sending it again needs the screen, and it is in use, so nothing more was sent. It is safe to send it again once the keyboard and mouse are still. ${second.detail ?? ''}`.trim()
|
|
130
|
+
return `${base} Sending it again on the screen was not possible, so nothing more was sent. ${second.detail ?? ''}`.trim()
|
|
131
|
+
}
|
|
132
|
+
if (second.outcome === 'no_effect')
|
|
133
|
+
return `${base} It was sent again on the screen and the window still did not change, so nothing in the window takes this input (for text, no field has the focus: click the field first).`
|
|
134
|
+
if (second.outcome === 'unexplained')
|
|
135
|
+
return `${base} It was sent again on the screen and the window then changed in a way that cannot be tied to the input, so it is not safe to send it again. Look at the window first.`
|
|
136
|
+
return `${base} It was sent again on the screen. ${second.detail ?? ''}`.trim()
|
|
137
|
+
}
|
|
@@ -109,3 +109,23 @@ export function translateKeyForCuaDriver(combo: string): CuaKeyPlan {
|
|
|
109
109
|
const finalKey = key ?? ([...main].length === 1 ? main.toLowerCase() : lower)
|
|
110
110
|
return { tool: 'hotkey', keys: [...new Set(modifiers), finalKey] }
|
|
111
111
|
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* A combination as a classic menu names it (`Ctrl+O`), or undefined for a key
|
|
115
|
+
* that is not a combination (no modifier) or whose menu name cannot be known.
|
|
116
|
+
*/
|
|
117
|
+
export function menuAcceleratorOf(combo: string): string | undefined {
|
|
118
|
+
let plan: CuaKeyPlan
|
|
119
|
+
try {
|
|
120
|
+
plan = translateKeyForCuaDriver(combo)
|
|
121
|
+
} catch {
|
|
122
|
+
return undefined
|
|
123
|
+
}
|
|
124
|
+
if (plan.tool !== 'hotkey' || plan.keys.length < 2) return undefined
|
|
125
|
+
const main = plan.keys[plan.keys.length - 1] as string
|
|
126
|
+
if (!/^([a-z0-9]|f([1-9]|1[0-2]))$/i.test(main)) return undefined
|
|
127
|
+
const names: Record<string, string> = { ctrl: 'Ctrl', alt: 'Alt', shift: 'Shift' }
|
|
128
|
+
const modifiers = plan.keys.slice(0, -1).map((key) => names[key])
|
|
129
|
+
if (modifiers.some((name) => name === undefined)) return undefined
|
|
130
|
+
return [...modifiers, main.toUpperCase()].join('+')
|
|
131
|
+
}
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import type { ActionDelivery, ActionRetrySafety } from '@namzu/sdk'
|
|
2
|
+
import type { ParsedUiTree } from './ui-tree.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Verifying an input by what it changed.
|
|
6
|
+
*
|
|
7
|
+
* The driver cannot say whether a background input arrived, so the adapter
|
|
8
|
+
* reads the target window before and after and compares the two. What the
|
|
9
|
+
* window shows is the only evidence: the text it holds, as controls name it
|
|
10
|
+
* or give it as their value.
|
|
11
|
+
*
|
|
12
|
+
* An input is verified only by a change that belongs to it:
|
|
13
|
+
* - typed text is verified when that text is now shown where it was not;
|
|
14
|
+
* - any other input (a press, a key) is verified when the window changed and
|
|
15
|
+
* then held still, and nothing was changing it before: a window that
|
|
16
|
+
* changes by itself (a clock, a spinner) proves nothing;
|
|
17
|
+
* - a new window of the same program is verified by its title.
|
|
18
|
+
* Otherwise the input stays unconfirmed. A change of unrelated text is never
|
|
19
|
+
* reported as the effect.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** Every text a window shows, with how many times it shows it. */
|
|
23
|
+
export type Observation = ReadonlyMap<string, number>
|
|
24
|
+
|
|
25
|
+
const MAX_TEXT = 240
|
|
26
|
+
const SEP = '\u0000'
|
|
27
|
+
/** Keys that carry a control's state (selected, disabled), not text: they never count as text shown. */
|
|
28
|
+
const STATE = '\u0001'
|
|
29
|
+
/** Present in a read that could not show everything: a text cut short, or a walk cut at its cap. */
|
|
30
|
+
export const CUT = '\u0002cut'
|
|
31
|
+
|
|
32
|
+
/** Whether a read left something out, so that an unchanged read proves nothing. */
|
|
33
|
+
export const isCut = (obs: Observation): boolean => obs.has(CUT)
|
|
34
|
+
|
|
35
|
+
/** The texts of one read of a window: each control's name and each value. */
|
|
36
|
+
export function observationOf(
|
|
37
|
+
tree: ParsedUiTree,
|
|
38
|
+
options: { readonly walkCut?: boolean } = {},
|
|
39
|
+
): Observation {
|
|
40
|
+
const out = new Map<string, number>()
|
|
41
|
+
if (options.walkCut === true) out.set(CUT, 1)
|
|
42
|
+
const add = (key: string): void => {
|
|
43
|
+
out.set(key, (out.get(key) ?? 0) + 1)
|
|
44
|
+
}
|
|
45
|
+
for (const node of tree.nodes) {
|
|
46
|
+
const name = node.name.trim()
|
|
47
|
+
const value = node.element.value?.trim()
|
|
48
|
+
if (name.length > MAX_TEXT || (value?.length ?? 0) > MAX_TEXT) out.set(CUT, 1)
|
|
49
|
+
if (name !== '') add(`${node.role}${SEP}${name.slice(0, MAX_TEXT)}`)
|
|
50
|
+
if (value !== undefined && value !== '') add(`${node.role}${SEP}=${value.slice(0, MAX_TEXT)}`)
|
|
51
|
+
for (const state of node.element.states ?? [])
|
|
52
|
+
add(`${STATE}${node.role}${SEP}${name.slice(0, MAX_TEXT)}${SEP}${state}`)
|
|
53
|
+
}
|
|
54
|
+
return out
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface ObservedChange {
|
|
58
|
+
/** Texts shown now that were not (or fewer times) before. */
|
|
59
|
+
readonly added: readonly string[]
|
|
60
|
+
/** Texts shown before that are gone. */
|
|
61
|
+
readonly removed: readonly string[]
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const isMeta = (key: string): boolean => key.startsWith(STATE) || key === CUT
|
|
65
|
+
|
|
66
|
+
const textOf = (key: string): string => {
|
|
67
|
+
const at = key.indexOf(SEP)
|
|
68
|
+
const text = at < 0 ? key : key.slice(at + 1)
|
|
69
|
+
return text.startsWith('=') ? text.slice(1) : text
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function changeBetween(before: Observation, after: Observation): ObservedChange {
|
|
73
|
+
const added: string[] = []
|
|
74
|
+
const removed: string[] = []
|
|
75
|
+
for (const [key, count] of after)
|
|
76
|
+
if (!isMeta(key) && count > (before.get(key) ?? 0)) added.push(textOf(key))
|
|
77
|
+
for (const [key, count] of before)
|
|
78
|
+
if (!isMeta(key) && count > (after.get(key) ?? 0)) removed.push(textOf(key))
|
|
79
|
+
return { added, removed }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Whether every control's state (selected, disabled) is the same in two reads. */
|
|
83
|
+
export const sameState = (a: Observation, b: Observation): boolean => {
|
|
84
|
+
for (const [key, count] of a) if (key.startsWith(STATE) && b.get(key) !== count) return false
|
|
85
|
+
for (const [key, count] of b) if (key.startsWith(STATE) && a.get(key) !== count) return false
|
|
86
|
+
return true
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export const sameObservation = (a: Observation, b: Observation): boolean => {
|
|
90
|
+
const { added, removed } = changeBetween(a, b)
|
|
91
|
+
return added.length === 0 && removed.length === 0 && sameState(a, b)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Text as a person types it against how a window may show it: case, spaces and operator glyphs differ. */
|
|
95
|
+
export function foldText(value: string): string {
|
|
96
|
+
return value.toLowerCase().replace(/\s+/g, '').replace(/\*/g, '×').replace(/\//g, '÷')
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const quote = (value: string, max = 80): string =>
|
|
100
|
+
JSON.stringify(value.length > max ? `${value.slice(0, max - 1)}…` : value)
|
|
101
|
+
|
|
102
|
+
/** How many times a folded text occurs in everything a window shows. */
|
|
103
|
+
function occurrences(obs: Observation, want: string): number {
|
|
104
|
+
let total = 0
|
|
105
|
+
for (const [key, count] of obs) {
|
|
106
|
+
if (isMeta(key)) continue
|
|
107
|
+
const text = foldText(textOf(key))
|
|
108
|
+
let at = text.indexOf(want)
|
|
109
|
+
while (at >= 0) {
|
|
110
|
+
total += count
|
|
111
|
+
at = text.indexOf(want, at + want.length)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return total
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const countOf = (text: string, want: string): number => {
|
|
118
|
+
if (want === '') return 0
|
|
119
|
+
let n = 0
|
|
120
|
+
for (let at = text.indexOf(want); at >= 0; at = text.indexOf(want, at + want.length)) n++
|
|
121
|
+
return n
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export interface ObservedVerdict {
|
|
125
|
+
readonly delivery: ActionDelivery
|
|
126
|
+
readonly retrySafety: ActionRetrySafety
|
|
127
|
+
/** What the window reads now: the evidence, shown to the model as `reads "…"`. */
|
|
128
|
+
readonly observed?: string
|
|
129
|
+
readonly detail: string
|
|
130
|
+
/**
|
|
131
|
+
* Set when no effect was seen: `no_effect` means the window provably did not
|
|
132
|
+
* change (a retry is safe); `unexplained` means it changed in a way that cannot
|
|
133
|
+
* be tied to the input (a retry is not safe).
|
|
134
|
+
*/
|
|
135
|
+
readonly outcome?: 'no_effect' | 'unexplained'
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Whether the window shows the effect of an input the driver could not
|
|
140
|
+
* confirm. `later` is a read taken a moment after `after`: equal reads mean
|
|
141
|
+
* the window had settled. Undefined means nothing observable followed.
|
|
142
|
+
*/
|
|
143
|
+
export function judgeObserved(
|
|
144
|
+
input: { readonly typed?: string },
|
|
145
|
+
reads: {
|
|
146
|
+
readonly before: Observation
|
|
147
|
+
readonly after: Observation
|
|
148
|
+
/** A read just before `before`: unequal reads mean the window was changing by itself. */
|
|
149
|
+
readonly earlier?: Observation
|
|
150
|
+
readonly later?: Observation
|
|
151
|
+
},
|
|
152
|
+
): ObservedVerdict | undefined {
|
|
153
|
+
const change = changeBetween(reads.before, reads.after)
|
|
154
|
+
if (input.typed !== undefined) {
|
|
155
|
+
const want = foldText(input.typed)
|
|
156
|
+
if (want !== '') {
|
|
157
|
+
// Typed text counts only when the window shows it more often than before; a
|
|
158
|
+
// short text (a letter, a digit) is no evidence inside other text, so it
|
|
159
|
+
// must appear as a text of its own.
|
|
160
|
+
const short = want.length < 3
|
|
161
|
+
const shown =
|
|
162
|
+
occurrences(reads.after, want) > occurrences(reads.before, want)
|
|
163
|
+
? change.added.find((text) =>
|
|
164
|
+
short ? foldText(text) === want : foldText(text).includes(want),
|
|
165
|
+
)
|
|
166
|
+
: undefined
|
|
167
|
+
if (shown !== undefined)
|
|
168
|
+
return {
|
|
169
|
+
delivery: 'delivered_verified',
|
|
170
|
+
retrySafety: 'unsafe',
|
|
171
|
+
observed: shown,
|
|
172
|
+
detail: 'The window shows the text typed.',
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return undefined
|
|
176
|
+
}
|
|
177
|
+
const stateOnly = change.added.length === 0 && change.removed.length === 0
|
|
178
|
+
if (stateOnly && sameState(reads.before, reads.after)) return undefined
|
|
179
|
+
// A window still changing a moment later, or one that changed before the input, says nothing.
|
|
180
|
+
if (reads.later !== undefined && !sameObservation(reads.after, reads.later)) return undefined
|
|
181
|
+
if (reads.earlier !== undefined && !sameObservation(reads.earlier, reads.before)) return undefined
|
|
182
|
+
if (stateOnly)
|
|
183
|
+
return {
|
|
184
|
+
delivery: 'delivered_verified',
|
|
185
|
+
retrySafety: 'unsafe',
|
|
186
|
+
detail:
|
|
187
|
+
'A control of the window changed state (selected or enabled) after the input and then held still.',
|
|
188
|
+
}
|
|
189
|
+
const now = change.added[0]
|
|
190
|
+
const was = change.removed[0]
|
|
191
|
+
const said =
|
|
192
|
+
was !== undefined
|
|
193
|
+
? now !== undefined
|
|
194
|
+
? `It read ${quote(was)} before the input.`
|
|
195
|
+
: `The window no longer shows ${quote(was)}.`
|
|
196
|
+
: 'The window showed nothing like it before the input.'
|
|
197
|
+
return {
|
|
198
|
+
delivery: 'delivered_verified',
|
|
199
|
+
retrySafety: 'unsafe',
|
|
200
|
+
...(now !== undefined ? { observed: now } : {}),
|
|
201
|
+
detail: `The window changed after the input and then held still. ${said}`,
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** What a message to a classic edit control did, as the control reads afterwards. */
|
|
206
|
+
export interface EditReading {
|
|
207
|
+
readonly found: boolean
|
|
208
|
+
readonly reason: string
|
|
209
|
+
readonly done: boolean
|
|
210
|
+
readonly seen: string
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export interface EditVerdict {
|
|
214
|
+
readonly ok: boolean
|
|
215
|
+
readonly delivery: ActionDelivery
|
|
216
|
+
readonly retrySafety: ActionRetrySafety
|
|
217
|
+
readonly detail?: string
|
|
218
|
+
readonly readBack?: { readonly expected?: string; readonly observed?: string }
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const UIPI_TEXT =
|
|
222
|
+
'this window belongs to a program running with higher rights, so text cannot be sent to it. Ask the user; do not try to get around it.'
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* `set` or `type` on a classic edit control, judged by the text the control
|
|
226
|
+
* reads back: the control is the evidence, not the message that returned.
|
|
227
|
+
*/
|
|
228
|
+
export function judgeEdit(op: 'set' | 'type', text: string, reading: EditReading): EditVerdict {
|
|
229
|
+
const expected = trimOf(text)
|
|
230
|
+
if (reading.reason === 'uipi')
|
|
231
|
+
return { ok: false, delivery: 'not_delivered', retrySafety: 'safe', detail: UIPI_TEXT }
|
|
232
|
+
if (reading.reason === 'read_only')
|
|
233
|
+
return {
|
|
234
|
+
ok: false,
|
|
235
|
+
delivery: 'not_delivered',
|
|
236
|
+
retrySafety: 'safe',
|
|
237
|
+
detail: 'this text control is read-only, so nothing was sent.',
|
|
238
|
+
}
|
|
239
|
+
if (reading.reason === 'hung')
|
|
240
|
+
return {
|
|
241
|
+
ok: true,
|
|
242
|
+
delivery: 'delivered_unverified',
|
|
243
|
+
retrySafety: 'unsafe',
|
|
244
|
+
detail:
|
|
245
|
+
'the program did not answer the text in time. It may have arrived; look at the window before sending it again.',
|
|
246
|
+
}
|
|
247
|
+
if (reading.reason !== '')
|
|
248
|
+
return {
|
|
249
|
+
ok: true,
|
|
250
|
+
delivery: 'delivered_unverified',
|
|
251
|
+
retrySafety: 'unsafe',
|
|
252
|
+
detail:
|
|
253
|
+
'the text was sent, but the control could not be read back. Look at the window before sending it again.',
|
|
254
|
+
}
|
|
255
|
+
if (reading.done)
|
|
256
|
+
return {
|
|
257
|
+
ok: true,
|
|
258
|
+
delivery: 'delivered_verified',
|
|
259
|
+
retrySafety: op === 'set' ? 'safe' : 'unsafe',
|
|
260
|
+
readBack: { expected, observed: trimOf(reading.seen) },
|
|
261
|
+
}
|
|
262
|
+
return {
|
|
263
|
+
ok: true,
|
|
264
|
+
delivery: 'delivered_unverified',
|
|
265
|
+
retrySafety: 'unsafe',
|
|
266
|
+
readBack: { expected, observed: trimOf(reading.seen) },
|
|
267
|
+
detail:
|
|
268
|
+
op === 'set'
|
|
269
|
+
? `the control changed, but it reads ${quote(reading.seen)} instead of the text sent; it may have reformatted it. Check before setting it again.`
|
|
270
|
+
: `the control does not read as having received the text (around the caret it reads ${quote(reading.seen)}). Look at the window before typing it again.`,
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const trimOf = (value: string, max = 80): string =>
|
|
275
|
+
value.length > max ? `${value.slice(0, max - 1)}…` : value
|
|
276
|
+
|
|
277
|
+
/** A before and after read of an edit control, for a keyboard delivery the driver could not confirm. */
|
|
278
|
+
export function judgeEditBetween(
|
|
279
|
+
typed: string,
|
|
280
|
+
before: { readonly text: string; readonly len: number },
|
|
281
|
+
after: { readonly text: string; readonly len: number },
|
|
282
|
+
): ObservedVerdict | undefined {
|
|
283
|
+
const norm = (value: string): string => value.replace(/\r\n?/g, '\n')
|
|
284
|
+
const a = norm(after.text)
|
|
285
|
+
const want = norm(typed)
|
|
286
|
+
if (after.len === before.len && a === norm(before.text)) return undefined
|
|
287
|
+
// The text must occur more often than before: a change elsewhere in a control
|
|
288
|
+
// that already held it proves nothing, and a longer text alone is not evidence.
|
|
289
|
+
if (want === '') return undefined
|
|
290
|
+
// A read keeps only the start of a very long text: when both reads were cut, the
|
|
291
|
+
// length is all that is left to compare, and it must have grown by exactly the typed text.
|
|
292
|
+
const cut = after.len > after.text.length && before.len > before.text.length
|
|
293
|
+
if (cut && after.len - before.len === want.length)
|
|
294
|
+
return {
|
|
295
|
+
delivery: 'delivered_verified',
|
|
296
|
+
retrySafety: 'unsafe',
|
|
297
|
+
detail: 'The text control grew by the length of the text typed.',
|
|
298
|
+
}
|
|
299
|
+
const at = a.indexOf(want)
|
|
300
|
+
if (at < 0) return undefined
|
|
301
|
+
if (countOf(a, want) <= countOf(norm(before.text), want)) return undefined
|
|
302
|
+
const from = Math.max(0, at - 40)
|
|
303
|
+
const seen = a.slice(from, from + want.length + 80)
|
|
304
|
+
return {
|
|
305
|
+
delivery: 'delivered_verified',
|
|
306
|
+
retrySafety: 'unsafe',
|
|
307
|
+
observed: seen,
|
|
308
|
+
detail: 'The text control now holds the text typed.',
|
|
309
|
+
}
|
|
310
|
+
}
|
|
@@ -10,28 +10,28 @@ import { extractZipEntry } from './zip.js'
|
|
|
10
10
|
*
|
|
11
11
|
* cua-driver (github.com/trycua/cua, MIT) publishes every build as a GitHub
|
|
12
12
|
* prerelease with a `checksums.txt`; the archive hashes below are the ones it
|
|
13
|
-
* lists for `cua-driver-rs-v0.
|
|
13
|
+
* lists for `cua-driver-rs-v0.38.0`, and the executable hashes were taken
|
|
14
14
|
* from those archives. Moving to another build means changing all four and
|
|
15
15
|
* re-running the Windows verification in `docs/sdk/computer-actions.md`.
|
|
16
16
|
*/
|
|
17
17
|
export const CUA_DRIVER_RELEASE = {
|
|
18
|
-
version: '0.
|
|
19
|
-
tag: 'cua-driver-rs-v0.
|
|
18
|
+
version: '0.38.0',
|
|
19
|
+
tag: 'cua-driver-rs-v0.38.0',
|
|
20
20
|
baseUrl: 'https://github.com/trycua/cua/releases/download',
|
|
21
21
|
windows: {
|
|
22
22
|
x64: {
|
|
23
|
-
archive: 'cua-driver-rs-0.
|
|
24
|
-
archiveSize:
|
|
25
|
-
archiveSha256: '
|
|
26
|
-
exeSize:
|
|
27
|
-
exeSha256: '
|
|
23
|
+
archive: 'cua-driver-rs-0.38.0-windows-x86_64-binary.zip',
|
|
24
|
+
archiveSize: 35_890_999,
|
|
25
|
+
archiveSha256: '5edaa74d0afa1b55a9668dbc4f32736507bd2bd4d75b18ca1dc8881bb12402b5',
|
|
26
|
+
exeSize: 38_673_232,
|
|
27
|
+
exeSha256: 'ffa58d88779ee219304b0c307f4cfa970fe822f830c6ecf9254eda5c29e22bf2',
|
|
28
28
|
},
|
|
29
29
|
arm64: {
|
|
30
|
-
archive: 'cua-driver-rs-0.
|
|
31
|
-
archiveSize:
|
|
32
|
-
archiveSha256: '
|
|
33
|
-
exeSize:
|
|
34
|
-
exeSha256: '
|
|
30
|
+
archive: 'cua-driver-rs-0.38.0-windows-arm64-binary.zip',
|
|
31
|
+
archiveSize: 33_682_556,
|
|
32
|
+
archiveSha256: 'd25c8ebfffb34e4c095ea170baf47b87c35dc0d1116f42e46b39df2bcbff8700',
|
|
33
|
+
exeSize: 32_621_904,
|
|
34
|
+
exeSha256: '2d6a0822be887a8a51678f57682d7362d484d4049b769ef964f0c2dffda29de1',
|
|
35
35
|
},
|
|
36
36
|
},
|
|
37
37
|
} as const
|