@namzu/computer-use 3.1.0 → 4.0.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 +58 -1
- package/dist/SubprocessComputerUseHost.d.ts +57 -9
- package/dist/SubprocessComputerUseHost.d.ts.map +1 -1
- package/dist/SubprocessComputerUseHost.js +213 -46
- 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 +1476 -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/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 +3 -3
- 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 +287 -45
- package/src/adapters/cua-driver/adapter.ts +1751 -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/ui-tree.ts +219 -15
- package/src/adapters/types.ts +7 -2
- 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,3 +1,4 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
1
2
|
import type { Rect, UiElement, UiElementAction } from '@namzu/sdk'
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -30,18 +31,69 @@ export interface CuaUiElement {
|
|
|
30
31
|
readonly parent_index?: unknown
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
/**
|
|
34
|
+
/**
|
|
35
|
+
* What `uiAct` knows about a ref: the element's identity evidence (role, name,
|
|
36
|
+
* automation id), which finds it again in a later tree, and the state it had
|
|
37
|
+
* when it was read, which a read-back compares against.
|
|
38
|
+
*/
|
|
34
39
|
export interface UiRefFacts {
|
|
40
|
+
readonly role: string
|
|
41
|
+
readonly name: string
|
|
42
|
+
readonly automationId?: string
|
|
35
43
|
readonly value?: string
|
|
44
|
+
readonly selected: boolean
|
|
45
|
+
readonly actions: readonly UiElementAction[]
|
|
46
|
+
/** Child indexes from the root to the element when it was read: its place in the tree. */
|
|
47
|
+
readonly path: readonly number[]
|
|
48
|
+
/**
|
|
49
|
+
* Another control with this role and name (and no id of its own to tell them
|
|
50
|
+
* apart) was in the window when this one was read. Such a control is found
|
|
51
|
+
* again by its place only while the tree has not changed shape.
|
|
52
|
+
*/
|
|
53
|
+
readonly twin?: boolean
|
|
54
|
+
/** How many children the node had; tells a nameless container from another one of its role. */
|
|
55
|
+
readonly childCount?: number
|
|
56
|
+
/** The shape of the whole tree this was read from; absent for a partial (query) read. */
|
|
57
|
+
readonly treeSig?: string
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** One node of a read tree, whether or not it can be acted on. */
|
|
61
|
+
export interface UiNode {
|
|
62
|
+
readonly path: readonly number[]
|
|
63
|
+
readonly role: string
|
|
64
|
+
readonly name: string
|
|
65
|
+
readonly automationId?: string
|
|
66
|
+
/** The driver's handle for the node; empty for one the host cannot act on. */
|
|
67
|
+
readonly token: string
|
|
68
|
+
/** The node with its subtree, driver tokens still in `ref`. */
|
|
69
|
+
readonly element: UiElement
|
|
70
|
+
/** Present when `token` is. */
|
|
71
|
+
readonly facts?: UiRefFacts
|
|
36
72
|
}
|
|
37
73
|
|
|
38
74
|
export interface ParsedUiTree {
|
|
39
75
|
readonly root: UiElement
|
|
40
76
|
/** Every ref in the tree, with what `uiAct` needs about it. */
|
|
41
77
|
readonly refs: ReadonlyMap<string, UiRefFacts>
|
|
78
|
+
/** Every node, in tree order, with its path. */
|
|
79
|
+
readonly nodes: readonly UiNode[]
|
|
42
80
|
readonly count: number
|
|
81
|
+
/** A digest of every node's place, role, name and id: equal for a tree that kept its shape. */
|
|
82
|
+
readonly signature: string
|
|
43
83
|
}
|
|
44
84
|
|
|
85
|
+
/** Controls that hold no other controls. */
|
|
86
|
+
const LEAF_ROLES: ReadonlySet<string> = new Set([
|
|
87
|
+
'Button',
|
|
88
|
+
'Hyperlink',
|
|
89
|
+
'CheckBox',
|
|
90
|
+
'RadioButton',
|
|
91
|
+
'Edit',
|
|
92
|
+
'Slider',
|
|
93
|
+
'ScrollBar',
|
|
94
|
+
'Image',
|
|
95
|
+
])
|
|
96
|
+
|
|
45
97
|
/** cua-driver's pattern names → the actions this host performs for them. */
|
|
46
98
|
const PATTERN_ACTIONS: Readonly<Record<string, readonly UiElementAction[]>> = {
|
|
47
99
|
invoke: ['invoke'],
|
|
@@ -146,22 +198,157 @@ function automationIdOf(attributes: string): string | undefined {
|
|
|
146
198
|
return /\[(?:[^\]]*\s)?id=([A-Za-z0-9_.:-]+)/.exec(attributes)?.[1]
|
|
147
199
|
}
|
|
148
200
|
|
|
149
|
-
function
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
function collectRefs(element: MutableElement, into: Map<string, UiRefFacts>): number {
|
|
201
|
+
function collectRefs(
|
|
202
|
+
element: MutableElement,
|
|
203
|
+
path: readonly number[],
|
|
204
|
+
into: Map<string, UiRefFacts>,
|
|
205
|
+
nodes: Map<MutableElement, { path: readonly number[]; facts?: UiRefFacts }>,
|
|
206
|
+
): number {
|
|
158
207
|
let count = 1
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
208
|
+
let facts: UiRefFacts | undefined
|
|
209
|
+
if (element.ref) {
|
|
210
|
+
facts = {
|
|
211
|
+
role: element.role,
|
|
212
|
+
name: element.name,
|
|
213
|
+
...(element.automationId !== undefined ? { automationId: element.automationId } : {}),
|
|
214
|
+
...(element.value !== undefined ? { value: element.value } : {}),
|
|
215
|
+
selected: element.states?.includes('selected') === true,
|
|
216
|
+
actions: element.actions ?? [],
|
|
217
|
+
path,
|
|
218
|
+
}
|
|
219
|
+
into.set(element.ref, facts)
|
|
220
|
+
}
|
|
221
|
+
nodes.set(element, { path, ...(facts ? { facts } : {}) })
|
|
222
|
+
element.children.forEach((child, index) => {
|
|
223
|
+
count += collectRefs(child, [...path, index], into, nodes)
|
|
224
|
+
})
|
|
162
225
|
return count
|
|
163
226
|
}
|
|
164
227
|
|
|
228
|
+
/** The frozen form of a mutable tree, also listing every node in order. */
|
|
229
|
+
function freezeWithNodes(
|
|
230
|
+
root: MutableElement,
|
|
231
|
+
places: Map<MutableElement, { path: readonly number[]; facts?: UiRefFacts }>,
|
|
232
|
+
): { root: UiElement; nodes: UiNode[] } {
|
|
233
|
+
const nodes: UiNode[] = []
|
|
234
|
+
const build = (element: MutableElement): UiElement => {
|
|
235
|
+
const { children, ...rest } = element
|
|
236
|
+
// Reserve this node's place so the list stays in tree order.
|
|
237
|
+
const index = nodes.length
|
|
238
|
+
nodes.push(undefined as unknown as UiNode)
|
|
239
|
+
const frozenChildren = children.map(build)
|
|
240
|
+
const frozen = Object.freeze({
|
|
241
|
+
...rest,
|
|
242
|
+
...(frozenChildren.length > 0 ? { children: Object.freeze(frozenChildren) } : {}),
|
|
243
|
+
}) as UiElement
|
|
244
|
+
const place = places.get(element)
|
|
245
|
+
nodes[index] = {
|
|
246
|
+
path: place?.path ?? [],
|
|
247
|
+
role: element.role,
|
|
248
|
+
name: element.name,
|
|
249
|
+
...(element.automationId !== undefined ? { automationId: element.automationId } : {}),
|
|
250
|
+
token: element.ref,
|
|
251
|
+
element: frozen,
|
|
252
|
+
...(place?.facts ? { facts: place.facts } : {}),
|
|
253
|
+
}
|
|
254
|
+
return frozen
|
|
255
|
+
}
|
|
256
|
+
return { root: build(root), nodes }
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** What finds a control again: the evidence a ref carries. */
|
|
260
|
+
export interface UiEvidence {
|
|
261
|
+
readonly role: string
|
|
262
|
+
readonly name: string
|
|
263
|
+
readonly automationId?: string
|
|
264
|
+
readonly path: readonly number[]
|
|
265
|
+
readonly twin?: boolean
|
|
266
|
+
readonly childCount?: number
|
|
267
|
+
readonly treeSig?: string
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export type Located =
|
|
271
|
+
| { readonly kind: 'found'; readonly node: UiNode }
|
|
272
|
+
| { readonly kind: 'ambiguous'; readonly count: number }
|
|
273
|
+
| { readonly kind: 'gone' }
|
|
274
|
+
|
|
275
|
+
const sameText = (a: string, b: string): boolean => a.trim() === b.trim()
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* The control `evidence` described, found in a newer read of its window.
|
|
279
|
+
*
|
|
280
|
+
* A native id with the same role and name is the control when it is unique;
|
|
281
|
+
* the same id under another name is not (it may be another action), so a
|
|
282
|
+
* control whose name carries a changing value is found again only by taking a
|
|
283
|
+
* new snapshot. Without an id, the stored path is walked and the
|
|
284
|
+
* node there is accepted if it is the same role and name; failing that the
|
|
285
|
+
* whole tree is searched for that role and name. One match is the control;
|
|
286
|
+
* several are AMBIGUOUS (never a guess); none is gone. `actionable` asks for a
|
|
287
|
+
* node that has a driver handle, since a control that lost its patterns cannot
|
|
288
|
+
* be acted on.
|
|
289
|
+
*/
|
|
290
|
+
export function locate(
|
|
291
|
+
evidence: UiEvidence,
|
|
292
|
+
tree: ParsedUiTree,
|
|
293
|
+
options: { readonly actionable: boolean },
|
|
294
|
+
): Located {
|
|
295
|
+
const usable = (node: UiNode): boolean => !options.actionable || node.token !== ''
|
|
296
|
+
const samePath = (node: UiNode): boolean =>
|
|
297
|
+
node.path.length === evidence.path.length && node.path.every((i, n) => i === evidence.path[n])
|
|
298
|
+
// A tree that kept its exact shape puts every control where it was. A
|
|
299
|
+
// changed one may have shifted identical controls into each other's places,
|
|
300
|
+
// so the place decides only for a nameless container that holds as many
|
|
301
|
+
// children as before. Evidence from before this was recorded has no twin
|
|
302
|
+
// flag and keeps the place as it was.
|
|
303
|
+
const sameTree = evidence.treeSig !== undefined && evidence.treeSig === tree.signature
|
|
304
|
+
const placeCounts = (node: UiNode): boolean =>
|
|
305
|
+
evidence.twin === undefined ||
|
|
306
|
+
sameTree ||
|
|
307
|
+
(evidence.name.trim() === '' &&
|
|
308
|
+
evidence.childCount !== undefined &&
|
|
309
|
+
(node.element.children?.length ?? 0) === evidence.childCount)
|
|
310
|
+
const settle = (candidates: readonly UiNode[]): Located | undefined => {
|
|
311
|
+
const usableOnes = candidates.filter(usable)
|
|
312
|
+
if (usableOnes.length === 1 && usableOnes[0]) {
|
|
313
|
+
// The one left of several identical controls may be another one.
|
|
314
|
+
if (evidence.twin === true && !sameTree && evidence.name.trim() !== '')
|
|
315
|
+
return { kind: 'ambiguous', count: 1 }
|
|
316
|
+
return { kind: 'found', node: usableOnes[0] }
|
|
317
|
+
}
|
|
318
|
+
if (usableOnes.length > 1) {
|
|
319
|
+
const atPath = usableOnes.filter(samePath).filter(placeCounts)
|
|
320
|
+
if (atPath.length === 1 && atPath[0]) return { kind: 'found', node: atPath[0] }
|
|
321
|
+
return { kind: 'ambiguous', count: usableOnes.length }
|
|
322
|
+
}
|
|
323
|
+
return undefined
|
|
324
|
+
}
|
|
325
|
+
if (evidence.automationId !== undefined) {
|
|
326
|
+
// The id and the name must both still say the same thing: an id that now
|
|
327
|
+
// carries another label may be another action.
|
|
328
|
+
const byId = tree.nodes.filter(
|
|
329
|
+
(node) =>
|
|
330
|
+
node.automationId === evidence.automationId &&
|
|
331
|
+
node.role === evidence.role &&
|
|
332
|
+
sameText(node.name, evidence.name),
|
|
333
|
+
)
|
|
334
|
+
return settle(byId) ?? { kind: 'gone' }
|
|
335
|
+
}
|
|
336
|
+
const same = tree.nodes.filter(
|
|
337
|
+
(node) => node.role === evidence.role && sameText(node.name, evidence.name),
|
|
338
|
+
)
|
|
339
|
+
const atPath = same.filter(samePath).filter(usable).filter(placeCounts)
|
|
340
|
+
if (atPath.length === 1 && atPath[0]) return { kind: 'found', node: atPath[0] }
|
|
341
|
+
return settle(same) ?? { kind: 'gone' }
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** A digest of the tree's shape: place, role, name and id of every node, never values. */
|
|
345
|
+
function signatureOf(nodes: readonly UiNode[]): string {
|
|
346
|
+
const hash = createHash('sha1')
|
|
347
|
+
for (const node of nodes)
|
|
348
|
+
hash.update(`${node.path.join('.')}|${node.role}|${node.name}|${node.automationId ?? ''}\n`)
|
|
349
|
+
return hash.digest('hex')
|
|
350
|
+
}
|
|
351
|
+
|
|
165
352
|
/** The whole tree of one `get_window_state` answer. */
|
|
166
353
|
export function toUiTree(state: Record<string, unknown>): ParsedUiTree {
|
|
167
354
|
const records = Array.isArray(state.elements) ? (state.elements as CuaUiElement[]) : []
|
|
@@ -200,6 +387,15 @@ export function toUiTree(state: Record<string, unknown>): ParsedUiTree {
|
|
|
200
387
|
}
|
|
201
388
|
if (!node) continue
|
|
202
389
|
while (stack.length > 0 && (stack[stack.length - 1]?.depth ?? 0) >= depth) stack.pop()
|
|
390
|
+
// The driver leaves out the containers it does not report (a list, a
|
|
391
|
+
// group), which shows as a gap of more than one level. A control that
|
|
392
|
+
// holds nothing cannot be the parent across such a gap: the rows belong
|
|
393
|
+
// to its own parent.
|
|
394
|
+
for (;;) {
|
|
395
|
+
const top = stack[stack.length - 1]
|
|
396
|
+
if (!top || depth - top.depth < 2 || !LEAF_ROLES.has(top.node.role)) break
|
|
397
|
+
stack.pop()
|
|
398
|
+
}
|
|
203
399
|
const parent = stack[stack.length - 1]
|
|
204
400
|
if (parent) parent.node.children.push(node)
|
|
205
401
|
else top.push(node)
|
|
@@ -229,6 +425,14 @@ export function toUiTree(state: Record<string, unknown>): ParsedUiTree {
|
|
|
229
425
|
const root: MutableElement =
|
|
230
426
|
top.length === 1 && top[0] ? top[0] : { ref: '', role: 'Window', name: title, children: top }
|
|
231
427
|
const refs = new Map<string, UiRefFacts>()
|
|
232
|
-
const
|
|
233
|
-
|
|
428
|
+
const places = new Map<MutableElement, { path: readonly number[]; facts?: UiRefFacts }>()
|
|
429
|
+
const count = collectRefs(root, [], refs, places)
|
|
430
|
+
const frozen = freezeWithNodes(root, places)
|
|
431
|
+
return {
|
|
432
|
+
root: frozen.root,
|
|
433
|
+
refs,
|
|
434
|
+
nodes: frozen.nodes,
|
|
435
|
+
count,
|
|
436
|
+
signature: signatureOf(frozen.nodes),
|
|
437
|
+
}
|
|
234
438
|
}
|
package/src/adapters/types.ts
CHANGED
|
@@ -9,8 +9,10 @@ import type {
|
|
|
9
9
|
UiActResult,
|
|
10
10
|
UiElementAction,
|
|
11
11
|
UiSnapshot,
|
|
12
|
+
UiSnapshotOptions,
|
|
12
13
|
WindowInfo,
|
|
13
14
|
WindowInputAction,
|
|
15
|
+
WindowInputResult,
|
|
14
16
|
WindowScreenshotResult,
|
|
15
17
|
} from '@namzu/sdk'
|
|
16
18
|
|
|
@@ -41,11 +43,14 @@ export interface Adapter {
|
|
|
41
43
|
/** With `capabilities.windowCapture`. */
|
|
42
44
|
captureWindow?(id: string): Promise<WindowScreenshotResult>
|
|
43
45
|
/** With `capabilities.windowCapture`. */
|
|
44
|
-
executeWindow?(
|
|
46
|
+
executeWindow?(
|
|
47
|
+
captureId: string,
|
|
48
|
+
action: WindowInputAction,
|
|
49
|
+
): Promise<WindowInputResult | undefined>
|
|
45
50
|
/** With `capabilities.regionCapture`. */
|
|
46
51
|
captureRegion?(rect: Rect): Promise<ScreenshotResult>
|
|
47
52
|
/** With `capabilities.uiTree`. */
|
|
48
|
-
uiSnapshot?(windowId?: string): Promise<UiSnapshot>
|
|
53
|
+
uiSnapshot?(windowId?: string, options?: UiSnapshotOptions): Promise<UiSnapshot>
|
|
49
54
|
/** With `capabilities.uiTree`. */
|
|
50
55
|
uiAct?(ref: string, action: UiElementAction, value?: string): Promise<UiActResult>
|
|
51
56
|
dispose?(): Promise<void>
|
package/src/adapters/win32.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { guardForeground } from '../foreground/guard.js'
|
|
2
|
+
import type { ForegroundLadder } from '../foreground/ladder.js'
|
|
3
|
+
import type { Presence } from '../presence/presence.js'
|
|
1
4
|
import { CuaDriverAdapter } from './cua-driver/adapter.js'
|
|
2
5
|
import {
|
|
3
6
|
CUA_DRIVER_RELEASE,
|
|
@@ -29,6 +32,14 @@ export interface Win32AdapterOptions {
|
|
|
29
32
|
readonly download?: boolean
|
|
30
33
|
readonly env?: NodeJS.ProcessEnv
|
|
31
34
|
readonly platform?: NodeJS.Platform
|
|
35
|
+
/**
|
|
36
|
+
* Where input that can only be delivered in the foreground goes: through
|
|
37
|
+
* the idle gate, the screen lease and a restore. Absent, display input and
|
|
38
|
+
* `focusWindow` act at once, as before.
|
|
39
|
+
*/
|
|
40
|
+
readonly foreground?: ForegroundLadder
|
|
41
|
+
/** The agent cursor and window border shown while a window is operated. Absent, none. */
|
|
42
|
+
readonly presence?: Presence
|
|
32
43
|
/** For tests. */
|
|
33
44
|
readonly wslProbes?: WslProbes
|
|
34
45
|
/** For tests. */
|
|
@@ -87,8 +98,18 @@ export class Win32Adapter {
|
|
|
87
98
|
const probes = options.wslProbes ?? realWslProbes
|
|
88
99
|
const configured = fromEnvironment(env)
|
|
89
100
|
const backend: Win32Backend = options.backend ?? (configured.off ? 'powershell' : 'auto')
|
|
90
|
-
const
|
|
91
|
-
|
|
101
|
+
const ladder = options.foreground
|
|
102
|
+
const guard = (adapter: Adapter): Adapter =>
|
|
103
|
+
ladder ? guardForeground(adapter, ladder) : adapter
|
|
104
|
+
const powershell = async (fallbackReason?: string) =>
|
|
105
|
+
guard(
|
|
106
|
+
await Win32PowerShellAdapter.create({
|
|
107
|
+
env,
|
|
108
|
+
platform,
|
|
109
|
+
wslProbes: probes,
|
|
110
|
+
fallbackReason,
|
|
111
|
+
}),
|
|
112
|
+
)
|
|
92
113
|
if (backend === 'powershell') return powershell()
|
|
93
114
|
|
|
94
115
|
const cuaDriverPath = options.cuaDriverPath ?? configured.path
|
|
@@ -106,13 +127,15 @@ export class Win32Adapter {
|
|
|
106
127
|
executable: resolved.path,
|
|
107
128
|
env: cuaDriverEnvironment(env, wsl, probes),
|
|
108
129
|
...(resolved.source === 'configured' ? {} : { version: CUA_DRIVER_RELEASE.version }),
|
|
130
|
+
...(ladder ? { foreground: ladder } : {}),
|
|
131
|
+
...(options.presence ? { presence: options.presence } : {}),
|
|
109
132
|
}
|
|
110
133
|
adapter = options.createCuaDriverAdapter
|
|
111
134
|
? options.createCuaDriverAdapter(adapterOptions)
|
|
112
135
|
: new CuaDriverAdapter(adapterOptions)
|
|
113
136
|
// Starts the driver and proves it reaches the desktop.
|
|
114
137
|
await adapter.getDisplayGeometry()
|
|
115
|
-
return adapter
|
|
138
|
+
return guard(adapter)
|
|
116
139
|
} catch (error) {
|
|
117
140
|
await adapter?.dispose?.().catch(() => undefined)
|
|
118
141
|
const reason = error instanceof Error ? error.message : String(error)
|
package/src/errors.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ComputerUseAction, ComputerUseOutcomeUnknown } from '@namzu/sdk'
|
|
2
|
+
import { STOPPED_TEXT } from './foreground/ladder.js'
|
|
2
3
|
import type { SpawnError } from './util/spawn.js'
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -25,3 +26,34 @@ export class ComputerUseOutcomeUnknownError extends Error implements ComputerUse
|
|
|
25
26
|
this.exitCode = failure.result.exitCode
|
|
26
27
|
}
|
|
27
28
|
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The user pressed Escape while the screen was in use. Every computer-use call
|
|
32
|
+
* for the rest of that turn fails with this fixed message, below the model, so
|
|
33
|
+
* it cannot be talked around.
|
|
34
|
+
*/
|
|
35
|
+
export class ComputerUseStoppedError extends Error {
|
|
36
|
+
readonly code = 'computer_use_stopped' as const
|
|
37
|
+
|
|
38
|
+
constructor() {
|
|
39
|
+
super(STOPPED_TEXT)
|
|
40
|
+
this.name = 'ComputerUseStoppedError'
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** What the model reads when the computer is locked. Fixed on purpose. */
|
|
45
|
+
export const LOCKED_TEXT =
|
|
46
|
+
'The computer is locked (or a system prompt only the user can answer has the screen), so nothing was done or read. Ask the user to unlock it, then continue. Do not try to work around it.'
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The computer is locked: a read or a capture would show nothing current, and
|
|
50
|
+
* an action would go nowhere. Nothing was sent.
|
|
51
|
+
*/
|
|
52
|
+
export class ComputerUseLockedError extends Error {
|
|
53
|
+
readonly code = 'computer_locked' as const
|
|
54
|
+
|
|
55
|
+
constructor() {
|
|
56
|
+
super(LOCKED_TEXT)
|
|
57
|
+
this.name = 'ComputerUseLockedError'
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ComputerUseAction,
|
|
3
|
+
ComputerUseResult,
|
|
4
|
+
FocusWindowResult,
|
|
5
|
+
WindowInfo,
|
|
6
|
+
} from '@namzu/sdk'
|
|
7
|
+
import type { Adapter } from '../adapters/types.js'
|
|
8
|
+
import { currentCallSignal } from '../util/call-scope.js'
|
|
9
|
+
import type { ForegroundLadder } from './ladder.js'
|
|
10
|
+
import { STOPPED_TEXT } from './ladder.js'
|
|
11
|
+
import type { ForegroundOutcome, ForegroundSubject } from './types.js'
|
|
12
|
+
|
|
13
|
+
/** Display actions that put input on the user's screen. */
|
|
14
|
+
const SCREEN_ACTIONS = new Set<ComputerUseAction['type']>([
|
|
15
|
+
'mouse_move',
|
|
16
|
+
'mouse_click',
|
|
17
|
+
'mouse_drag',
|
|
18
|
+
'scroll',
|
|
19
|
+
'type_text',
|
|
20
|
+
'key',
|
|
21
|
+
])
|
|
22
|
+
|
|
23
|
+
const heldText = (ms: number): string => `${(Math.max(ms, 100) / 1000).toFixed(1)} s`
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* What an interrupted or stopped burst tells the model. Neither is proof that
|
|
27
|
+
* the input arrived or did not, so both leave the action unconfirmed.
|
|
28
|
+
*/
|
|
29
|
+
export function interferenceNote(outcome: {
|
|
30
|
+
readonly interfered: boolean
|
|
31
|
+
readonly stopped: boolean
|
|
32
|
+
}): string | undefined {
|
|
33
|
+
if (outcome.stopped) return STOPPED_TEXT
|
|
34
|
+
if (outcome.interfered)
|
|
35
|
+
return 'The user used the keyboard or mouse while the screen was in use, so the input may have landed somewhere else or only in part. Look at the window before repeating it.'
|
|
36
|
+
return undefined
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Set when a burst that sent input was not registered by the system at all:
|
|
41
|
+
* Windows dropped it, so it reached no window and sending it again is safe
|
|
42
|
+
* once the cause (an administrator window in front) is gone.
|
|
43
|
+
*/
|
|
44
|
+
export function droppedInputNote(
|
|
45
|
+
outcome: Extract<ForegroundOutcome<unknown>, { kind: 'ran' }>,
|
|
46
|
+
): string | undefined {
|
|
47
|
+
if (outcome.inputLanded !== false) return undefined
|
|
48
|
+
return `${outcome.inputDroppedWhy ?? 'Windows dropped the input before it reached any window.'} Nothing was sent to the window; try again once that window is closed or the user has acted in it.`
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The sentence that tells the model a background action ran in the foreground. */
|
|
52
|
+
export function foregroundNote(
|
|
53
|
+
outcome: Extract<ForegroundOutcome<unknown>, { kind: 'ran' }>,
|
|
54
|
+
): string {
|
|
55
|
+
const put =
|
|
56
|
+
outcome.restoredForeground || outcome.restoredCursor
|
|
57
|
+
? ' The window and pointer that were there before were put back.'
|
|
58
|
+
: outcome.restoreNote &&
|
|
59
|
+
outcome.restoreNote !== 'the burst had no window to put back' &&
|
|
60
|
+
!outcome.restoreNote.startsWith('the window that was in front is in front')
|
|
61
|
+
? ` The window that was in front was not put back: ${outcome.restoreNote}.`
|
|
62
|
+
: ''
|
|
63
|
+
return `Background input was not possible here, so this used the screen for ${heldText(outcome.heldMs)}.${put}`
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Display input and `focusWindow` through the foreground ladder, for an
|
|
68
|
+
* adapter that has no ladder of its own (the PowerShell path) and for the
|
|
69
|
+
* display-scope calls of one that has. Window-scoped input is the wrapped
|
|
70
|
+
* adapter's business: it decides when background is impossible.
|
|
71
|
+
*/
|
|
72
|
+
export function guardForeground(adapter: Adapter, ladder: ForegroundLadder): Adapter {
|
|
73
|
+
const apps = new Map<string, WindowInfo>()
|
|
74
|
+
const guarded: Adapter = {
|
|
75
|
+
get capabilities() {
|
|
76
|
+
return adapter.capabilities
|
|
77
|
+
},
|
|
78
|
+
get backend() {
|
|
79
|
+
return adapter.backend
|
|
80
|
+
},
|
|
81
|
+
get fallbackReason() {
|
|
82
|
+
return adapter.fallbackReason
|
|
83
|
+
},
|
|
84
|
+
getDisplayGeometry: () => adapter.getDisplayGeometry(),
|
|
85
|
+
async execute(action) {
|
|
86
|
+
if (!SCREEN_ACTIONS.has(action.type)) return adapter.execute(action)
|
|
87
|
+
const outcome = await ladder.run(
|
|
88
|
+
{
|
|
89
|
+
subject: { app: 'the desktop' },
|
|
90
|
+
keepCursor: action.type === 'mouse_move',
|
|
91
|
+
sendsInput: action.type !== 'mouse_move',
|
|
92
|
+
...(currentCallSignal() ? { signal: currentCallSignal() as AbortSignal } : {}),
|
|
93
|
+
},
|
|
94
|
+
async () => adapter.execute(action),
|
|
95
|
+
)
|
|
96
|
+
if (outcome.kind === 'blocked')
|
|
97
|
+
return {
|
|
98
|
+
type: 'ok',
|
|
99
|
+
delivery: 'not_delivered',
|
|
100
|
+
retrySafety: 'safe',
|
|
101
|
+
detail: outcome.block.detail,
|
|
102
|
+
}
|
|
103
|
+
const dropped = droppedInputNote(outcome)
|
|
104
|
+
if (dropped)
|
|
105
|
+
return {
|
|
106
|
+
type: 'ok',
|
|
107
|
+
delivery: 'not_delivered',
|
|
108
|
+
retrySafety: 'safe',
|
|
109
|
+
detail: dropped,
|
|
110
|
+
}
|
|
111
|
+
const note = interferenceNote(outcome)
|
|
112
|
+
if (!note || outcome.value.type !== 'ok') return outcome.value
|
|
113
|
+
return mergeNote(outcome.value, note)
|
|
114
|
+
},
|
|
115
|
+
async dispose() {
|
|
116
|
+
await ladder.dispose()
|
|
117
|
+
await adapter.dispose?.()
|
|
118
|
+
},
|
|
119
|
+
}
|
|
120
|
+
if (adapter.listWindows) {
|
|
121
|
+
const list = adapter.listWindows.bind(adapter)
|
|
122
|
+
guarded.listWindows = async () => {
|
|
123
|
+
const windows = await list()
|
|
124
|
+
apps.clear()
|
|
125
|
+
for (const window of windows) apps.set(window.id, window)
|
|
126
|
+
return windows
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (adapter.focusWindow) {
|
|
130
|
+
const focus = adapter.focusWindow.bind(adapter)
|
|
131
|
+
guarded.focusWindow = async (id): Promise<FocusWindowResult> => {
|
|
132
|
+
let window = apps.get(id)
|
|
133
|
+
if (!window && guarded.listWindows) {
|
|
134
|
+
await guarded.listWindows().catch(() => undefined)
|
|
135
|
+
window = apps.get(id)
|
|
136
|
+
}
|
|
137
|
+
const subject: ForegroundSubject = window
|
|
138
|
+
? { app: window.app, pid: window.pid, hwnd: window.id }
|
|
139
|
+
: { app: 'the window you named', hwnd: id }
|
|
140
|
+
const outcome = await ladder.run(
|
|
141
|
+
{
|
|
142
|
+
subject,
|
|
143
|
+
...(currentCallSignal() ? { signal: currentCallSignal() as AbortSignal } : {}),
|
|
144
|
+
},
|
|
145
|
+
() => focus(id),
|
|
146
|
+
)
|
|
147
|
+
if (outcome.kind === 'blocked')
|
|
148
|
+
return {
|
|
149
|
+
ok: false,
|
|
150
|
+
focusedId: null,
|
|
151
|
+
delivery: 'not_delivered',
|
|
152
|
+
retrySafety: 'safe',
|
|
153
|
+
detail: outcome.block.detail,
|
|
154
|
+
}
|
|
155
|
+
const note = interferenceNote(outcome)
|
|
156
|
+
return note ? { ...outcome.value, detail: note } : outcome.value
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
for (const name of [
|
|
160
|
+
'captureWindow',
|
|
161
|
+
'executeWindow',
|
|
162
|
+
'captureRegion',
|
|
163
|
+
'uiSnapshot',
|
|
164
|
+
'uiAct',
|
|
165
|
+
] as const) {
|
|
166
|
+
const method = adapter[name]
|
|
167
|
+
if (method) (guarded as unknown as Record<string, unknown>)[name] = method.bind(adapter)
|
|
168
|
+
}
|
|
169
|
+
return guarded
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function mergeNote(
|
|
173
|
+
result: Extract<ComputerUseResult, { type: 'ok' }>,
|
|
174
|
+
note: string,
|
|
175
|
+
): ComputerUseResult {
|
|
176
|
+
return {
|
|
177
|
+
type: 'ok',
|
|
178
|
+
delivery: result.delivery === 'not_delivered' ? 'not_delivered' : 'delivered_unverified',
|
|
179
|
+
retrySafety: result.delivery === 'not_delivered' ? (result.retrySafety ?? 'safe') : 'unsafe',
|
|
180
|
+
detail: [result.detail, note].filter(Boolean).join(' '),
|
|
181
|
+
}
|
|
182
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export {
|
|
2
|
+
DEFAULT_TIMING,
|
|
3
|
+
ForegroundLadder,
|
|
4
|
+
STOPPED_TEXT,
|
|
5
|
+
realClock,
|
|
6
|
+
} from './ladder.js'
|
|
7
|
+
export type {
|
|
8
|
+
ForegroundLadderOptions,
|
|
9
|
+
LadderClock,
|
|
10
|
+
LadderTiming,
|
|
11
|
+
} from './ladder.js'
|
|
12
|
+
export { ScreenLease } from './lease.js'
|
|
13
|
+
export type { LeaseRecord } from './lease.js'
|
|
14
|
+
export { foregroundNote, guardForeground, interferenceNote } from './guard.js'
|
|
15
|
+
export {
|
|
16
|
+
HELPER_BOOTSTRAP,
|
|
17
|
+
WindowsInputMonitor,
|
|
18
|
+
encodeHelperCommand,
|
|
19
|
+
helperScriptLine,
|
|
20
|
+
} from './windows-monitor.js'
|
|
21
|
+
export type {
|
|
22
|
+
BurstContext,
|
|
23
|
+
EditTextRequest,
|
|
24
|
+
EditTextResult,
|
|
25
|
+
ForegroundAnswer,
|
|
26
|
+
ForegroundApprover,
|
|
27
|
+
ForegroundBlock,
|
|
28
|
+
ForegroundBlockCode,
|
|
29
|
+
ForegroundMonitor,
|
|
30
|
+
ForegroundOutcome,
|
|
31
|
+
ForegroundSubject,
|
|
32
|
+
MonitorBurstEnd,
|
|
33
|
+
MonitorState,
|
|
34
|
+
MenuCommandRequest,
|
|
35
|
+
MenuCommandResult,
|
|
36
|
+
SelectItemRequest,
|
|
37
|
+
SelectItemResult,
|
|
38
|
+
} from './types.js'
|
|
39
|
+
export { BurstInterrupted, PROTECTED_PROCESSES } from './types.js'
|
|
40
|
+
export {
|
|
41
|
+
createWindowsForegroundLadder,
|
|
42
|
+
screenLeasePath,
|
|
43
|
+
windowsPathFor,
|
|
44
|
+
} from './windows.js'
|
|
45
|
+
export type { WindowsLadderOptions } from './windows.js'
|