@namzu/computer-use 4.3.1 → 4.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -0
- package/dist/SubprocessComputerUseHost.d.ts +21 -1
- package/dist/SubprocessComputerUseHost.d.ts.map +1 -1
- package/dist/SubprocessComputerUseHost.js +67 -15
- package/dist/SubprocessComputerUseHost.js.map +1 -1
- package/dist/a11y/a11y.d.ts +16 -7
- package/dist/a11y/a11y.d.ts.map +1 -1
- package/dist/a11y/a11y.js +23 -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/interface.d.ts +5 -0
- package/dist/a11y/interface.d.ts.map +1 -1
- package/dist/a11y/interface.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 +26 -0
- package/dist/adapters/cua-driver/adapter.d.ts.map +1 -1
- package/dist/adapters/cua-driver/adapter.js +224 -13
- package/dist/adapters/cua-driver/adapter.js.map +1 -1
- package/dist/adapters/cua-driver/client.d.ts +5 -0
- package/dist/adapters/cua-driver/client.d.ts.map +1 -1
- package/dist/adapters/cua-driver/client.js +31 -0
- package/dist/adapters/cua-driver/client.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/types.d.ts +5 -0
- package/dist/adapters/types.d.ts.map +1 -1
- package/dist/adapters/types.js.map +1 -1
- 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/guard.d.ts.map +1 -1
- package/dist/foreground/guard.js +2 -0
- package/dist/foreground/guard.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 +11 -2
- package/dist/presence/presence.d.ts.map +1 -1
- package/dist/presence/presence.js +15 -0
- 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 +80 -10
- package/src/a11y/a11y.ts +36 -11
- package/src/a11y/index.ts +28 -1
- package/src/a11y/interface.ts +5 -0
- 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 +245 -17
- package/src/adapters/cua-driver/client.ts +27 -0
- 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/types.ts +5 -0
- package/src/adapters/win32-native.ts +328 -0
- package/src/adapters/win32.ts +52 -5
- package/src/foreground/guard.ts +1 -0
- 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 +23 -2
- package/src/presence/windows.ts +14 -0
|
@@ -6,7 +6,7 @@ import { CallAbortedError, currentCallSignal, markSent } from '../../util/call-s
|
|
|
6
6
|
import { McpStdioClient, McpToolError, } from './client.js';
|
|
7
7
|
import { canBeJudgedByStillness, escalationDetail, judgeStillness, shouldRetryOnScreen, } from './escalation.js';
|
|
8
8
|
import { menuAcceleratorOf, translateKeyForCuaDriver } from './keys.js';
|
|
9
|
-
import { judgeEdit, judgeEditBetween, judgeObserved, observationOf, sameObservation, } from './observe.js';
|
|
9
|
+
import { changeBetween, judgeEdit, judgeEditBetween, judgeObserved, observationOf, sameObservation, } from './observe.js';
|
|
10
10
|
import { locate, toUiTree, } from './ui-tree.js';
|
|
11
11
|
const CAPTURE_TIMEOUT_MS = 30_000;
|
|
12
12
|
const LIST_TIMEOUT_MS = 20_000;
|
|
@@ -20,11 +20,11 @@ const UI_MAX_ELEMENTS = 5_000;
|
|
|
20
20
|
*/
|
|
21
21
|
const UI_WALK_CUT_AT = Math.floor(UI_MAX_ELEMENTS * 0.95);
|
|
22
22
|
/** A window with more shown controls than this is returned as an outline. */
|
|
23
|
-
const OUTLINE_THRESHOLD =
|
|
23
|
+
const OUTLINE_THRESHOLD = 400;
|
|
24
24
|
/** The most controls an outline shows; the rest are counted, not listed. */
|
|
25
|
-
const OUTLINE_BUDGET =
|
|
25
|
+
const OUTLINE_BUDGET = 300;
|
|
26
26
|
/** The most children of one element an outline lists; a long list is opened as a region. */
|
|
27
|
-
const OUTLINE_PER_PARENT =
|
|
27
|
+
const OUTLINE_PER_PARENT = 20;
|
|
28
28
|
/** The children of a region opened by itself that one page lists. */
|
|
29
29
|
const OUTLINE_PAGE = 60;
|
|
30
30
|
/** A browser tree this small is the window frame alone: the page has not been built yet. */
|
|
@@ -33,6 +33,8 @@ const SPARSE_TREE = 12;
|
|
|
33
33
|
const CHOICE_ROLES = /^(ComboBox|List|ListBox)$/i;
|
|
34
34
|
/** Waits (ms) before each read that looks for an input's effect: at once, then a little later. */
|
|
35
35
|
const OBSERVE_WAITS_MS = [0, 80, 200, 450];
|
|
36
|
+
/** expand and collapse: a first read after the click, and a second one this much later that must agree. */
|
|
37
|
+
const EXPAND_WAITS_MS = [150, 450];
|
|
36
38
|
/**
|
|
37
39
|
* How old (ms) the window list read just before an input may be and still
|
|
38
40
|
* stand for the check that the window has not moved. A list takes over a
|
|
@@ -193,6 +195,33 @@ function outlineOf(root, basePath, budget, page) {
|
|
|
193
195
|
};
|
|
194
196
|
return { root: build(root, basePath), regions, next };
|
|
195
197
|
}
|
|
198
|
+
/**
|
|
199
|
+
* The controls whose role is one of `roles`, with every element above them.
|
|
200
|
+
* Kept children carry their index in the full tree, as an outline's do, so
|
|
201
|
+
* refs bind to the right places.
|
|
202
|
+
*/
|
|
203
|
+
function pruneByRole(root, roles) {
|
|
204
|
+
const walk = (element) => {
|
|
205
|
+
const kept = [];
|
|
206
|
+
(element.children ?? []).forEach((child, index) => {
|
|
207
|
+
const built = walk(child);
|
|
208
|
+
if (!built)
|
|
209
|
+
return;
|
|
210
|
+
ORIGINAL_INDEX.set(built, index);
|
|
211
|
+
kept.push(built);
|
|
212
|
+
});
|
|
213
|
+
const matches = roles.has(element.role.toLowerCase());
|
|
214
|
+
if (!matches && kept.length === 0)
|
|
215
|
+
return undefined;
|
|
216
|
+
const { children: _children, ...rest } = element;
|
|
217
|
+
return Object.freeze({
|
|
218
|
+
...rest,
|
|
219
|
+
...(kept.length > 0 ? { children: Object.freeze(kept) } : {}),
|
|
220
|
+
});
|
|
221
|
+
};
|
|
222
|
+
const { children: _children, ...bare } = root;
|
|
223
|
+
return walk(root) ?? Object.freeze(bare);
|
|
224
|
+
}
|
|
196
225
|
/** The paths of the elements a view contains. */
|
|
197
226
|
function collectShown(element, path, into) {
|
|
198
227
|
into.add(pathKey(path));
|
|
@@ -238,6 +267,8 @@ export class CuaDriverAdapter {
|
|
|
238
267
|
driverCursorOff;
|
|
239
268
|
/** cua-driver's screenshot resize registry is per PID, so only this capture may drive input. */
|
|
240
269
|
windowFrame;
|
|
270
|
+
/** The process count at the last idle release, for a screenshot taken from that process. */
|
|
271
|
+
releasedStarts;
|
|
241
272
|
/** Capture and scoped input must not race over cua-driver's per-PID resize ratio. */
|
|
242
273
|
windowOperation = Promise.resolve();
|
|
243
274
|
/** Refs the model holds, by the adapter's ref. A handle is usable only in its window's latest read; otherwise the ref is found again. */
|
|
@@ -506,6 +537,28 @@ export class CuaDriverAdapter {
|
|
|
506
537
|
async captureWindow(id) {
|
|
507
538
|
return this.serializeWindowOperation(() => this.captureWindowInternal(id));
|
|
508
539
|
}
|
|
540
|
+
/**
|
|
541
|
+
* Read the window again with a screenshot, so the driver can place pixels of `frame` on it.
|
|
542
|
+
* True when that read has the size of the screenshot the model is looking at.
|
|
543
|
+
*/
|
|
544
|
+
async restoreDriverFrame(frame) {
|
|
545
|
+
const hwnd = parseWindowId(frame.window.id);
|
|
546
|
+
if (hwnd === undefined)
|
|
547
|
+
return false;
|
|
548
|
+
try {
|
|
549
|
+
const result = await this.callTool('get_window_state', {
|
|
550
|
+
pid: frame.window.pid,
|
|
551
|
+
window_id: hwnd,
|
|
552
|
+
include_accessibility_tree: false,
|
|
553
|
+
include_screenshot: true,
|
|
554
|
+
});
|
|
555
|
+
const facts = structured(result, 'get_window_state');
|
|
556
|
+
return facts.screenshot_width === frame.width && facts.screenshot_height === frame.height;
|
|
557
|
+
}
|
|
558
|
+
catch {
|
|
559
|
+
return false;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
509
562
|
async captureWindowInternal(id) {
|
|
510
563
|
const target = await this.resolveWindow(id);
|
|
511
564
|
// A failed or overlapping capture invalidates the old resize ratio even
|
|
@@ -639,6 +692,7 @@ export class CuaDriverAdapter {
|
|
|
639
692
|
return { ...again, detail: finalDetail };
|
|
640
693
|
}
|
|
641
694
|
async attemptWindowInput(captureId, action, route) {
|
|
695
|
+
await this.carryFrameOver(captureId);
|
|
642
696
|
const frame = this.windowFrame;
|
|
643
697
|
const watched = action.type === 'type_text' || action.type === 'key' || action.type === 'mouse_click';
|
|
644
698
|
const hwnd = frame && frame.id === captureId ? parseWindowId(frame.window.id) : undefined;
|
|
@@ -837,6 +891,7 @@ export class CuaDriverAdapter {
|
|
|
837
891
|
}
|
|
838
892
|
}
|
|
839
893
|
async deliverWindowInput(captureId, action, route = { foreground: false, onScreen: false }, listing) {
|
|
894
|
+
await this.carryFrameOver(captureId);
|
|
840
895
|
const frame = this.windowFrame;
|
|
841
896
|
if (!frame || frame.id !== captureId)
|
|
842
897
|
throw new Error('computer-use: this window screenshot is stale; capture the window again before acting.');
|
|
@@ -879,7 +934,19 @@ export class CuaDriverAdapter {
|
|
|
879
934
|
throw new Error('computer-use: the captured window changed or its driver restarted; take a new window screenshot.');
|
|
880
935
|
this.showPresence(frame, current.bounds, name, args);
|
|
881
936
|
await this.markForSettle(target.window_id);
|
|
882
|
-
|
|
937
|
+
const sendOnce = () => this.callTool(name, { ...target, ...args }, undefined, undefined, undefined, frame);
|
|
938
|
+
try {
|
|
939
|
+
return await sendOnce();
|
|
940
|
+
}
|
|
941
|
+
catch (error) {
|
|
942
|
+
// The driver keeps one latest read per window, and a read of the controls (made just before
|
|
943
|
+
// an input to judge it) replaces a screenshot with one that has none. It then refuses a
|
|
944
|
+
// pixel input and sends nothing. The window is unchanged (checked above), so the same
|
|
945
|
+
// screenshot is taken again for the driver and the input goes once more.
|
|
946
|
+
if (!isNoDriverFrame(error) || !(await this.restoreDriverFrame(frame)))
|
|
947
|
+
throw error;
|
|
948
|
+
return await sendOnce();
|
|
949
|
+
}
|
|
883
950
|
};
|
|
884
951
|
// The driver's hotkey finds a shortcut only through an accelerator the window names, and refuses (sending
|
|
885
952
|
// nothing) when there is none; the same combination as a key with modifiers is real input.
|
|
@@ -1099,9 +1166,19 @@ export class CuaDriverAdapter {
|
|
|
1099
1166
|
}
|
|
1100
1167
|
const viewRoot = located?.element ?? tree.root;
|
|
1101
1168
|
const basePath = located?.path ?? [];
|
|
1102
|
-
const
|
|
1103
|
-
|
|
1104
|
-
|
|
1169
|
+
const roles = new Set((options.role ?? '')
|
|
1170
|
+
.split(',')
|
|
1171
|
+
.map((role) => role.trim().toLowerCase())
|
|
1172
|
+
.filter((role) => role.length > 0));
|
|
1173
|
+
const view = roles.size > 0
|
|
1174
|
+
? {
|
|
1175
|
+
root: pruneByRole(viewRoot, roles),
|
|
1176
|
+
regions: new Set(),
|
|
1177
|
+
next: undefined,
|
|
1178
|
+
}
|
|
1179
|
+
: !query && visibleCount(viewRoot) > OUTLINE_THRESHOLD
|
|
1180
|
+
? outlineOf(viewRoot, basePath, OUTLINE_BUDGET, located ? { offset: options.offset ?? 0 } : undefined)
|
|
1181
|
+
: { root: viewRoot, regions: new Set(), next: undefined };
|
|
1105
1182
|
const shownKeys = new Set();
|
|
1106
1183
|
collectShown(view.root, basePath, shownKeys);
|
|
1107
1184
|
const refs = this.bindRead(target, read, {
|
|
@@ -1113,8 +1190,12 @@ export class CuaDriverAdapter {
|
|
|
1113
1190
|
const root = withRefs(view.root, basePath, refAt);
|
|
1114
1191
|
const title = typeof state.window_title === 'string' ? state.window_title : undefined;
|
|
1115
1192
|
const app = typeof state.app_name === 'string' ? state.app_name.replace(/\.exe$/i, '') : undefined;
|
|
1193
|
+
const selectedText = typeof state.selected_text === 'string' && state.selected_text.trim().length > 0
|
|
1194
|
+
? state.selected_text.slice(0, 2_000)
|
|
1195
|
+
: undefined;
|
|
1116
1196
|
return {
|
|
1117
1197
|
windowId: windowIdOf(target.hwnd),
|
|
1198
|
+
...(selectedText !== undefined ? { selectedText } : {}),
|
|
1118
1199
|
...(title !== undefined ? { title } : {}),
|
|
1119
1200
|
...(app !== undefined ? { app } : {}),
|
|
1120
1201
|
root,
|
|
@@ -1332,9 +1413,10 @@ export class CuaDriverAdapter {
|
|
|
1332
1413
|
}));
|
|
1333
1414
|
try {
|
|
1334
1415
|
switch (action) {
|
|
1335
|
-
case 'invoke':
|
|
1336
1416
|
case 'expand':
|
|
1337
1417
|
case 'collapse':
|
|
1418
|
+
return await this.expandAndWatch(facts, uiRef, action);
|
|
1419
|
+
case 'invoke':
|
|
1338
1420
|
// cua-driver's click on an element token performs the control's
|
|
1339
1421
|
// own pattern in the background (Invoke, or a posted click at its
|
|
1340
1422
|
// centre), with no pointer move and no change of foreground.
|
|
@@ -1624,10 +1706,7 @@ export class CuaDriverAdapter {
|
|
|
1624
1706
|
const windows = await this.windowIdsOf(target.pid);
|
|
1625
1707
|
// Each read of the window replaces the driver's handles: click through the refreshed one.
|
|
1626
1708
|
const current = this.uiRefs.get(uiRef.ref) ?? facts;
|
|
1627
|
-
const sent = await this.clickRef(current, {
|
|
1628
|
-
ref: uiRef.ref,
|
|
1629
|
-
facts: current,
|
|
1630
|
-
});
|
|
1709
|
+
const sent = await this.clickRef(current, { ref: uiRef.ref, facts: current });
|
|
1631
1710
|
if (!sent.ok || sent.delivery !== 'delivered_unverified' || !before)
|
|
1632
1711
|
return sent;
|
|
1633
1712
|
const seen = await this.verifyByReading(target, {
|
|
@@ -1646,6 +1725,79 @@ export class CuaDriverAdapter {
|
|
|
1646
1725
|
...(seen.observed !== undefined ? { readBack: { observed: seen.observed } } : {}),
|
|
1647
1726
|
};
|
|
1648
1727
|
}
|
|
1728
|
+
/**
|
|
1729
|
+
* expand or collapse, which the driver sends as a click and cannot read back: the control reports no
|
|
1730
|
+
* expanded state. What counts is what the window shows afterwards, and it has to still be there a
|
|
1731
|
+
* moment later. A menu that opened and closed again is not an expanded menu.
|
|
1732
|
+
*/
|
|
1733
|
+
async expandAndWatch(facts, uiRef, want) {
|
|
1734
|
+
const target = { hwnd: facts.hwnd, pid: facts.pid };
|
|
1735
|
+
const before = await this.observe(target);
|
|
1736
|
+
const windows = await this.windowIdsOf(target.pid);
|
|
1737
|
+
// Each read of the window replaces the driver's handles: click through the refreshed one.
|
|
1738
|
+
const current = this.uiRefs.get(uiRef.ref) ?? facts;
|
|
1739
|
+
const sent = await this.clickRef(current, { ref: uiRef.ref, facts: current });
|
|
1740
|
+
if (!sent.ok || sent.delivery === 'not_delivered')
|
|
1741
|
+
return sent;
|
|
1742
|
+
const what = describeEvidence(current);
|
|
1743
|
+
const unverified = (detail) => ({
|
|
1744
|
+
ok: true,
|
|
1745
|
+
delivery: 'delivered_unverified',
|
|
1746
|
+
retrySafety: 'unsafe',
|
|
1747
|
+
detail,
|
|
1748
|
+
});
|
|
1749
|
+
if (!before)
|
|
1750
|
+
return unverified(`the click on ${what} was sent, but the window could not be read to see whether it ${want === 'expand' ? 'opened' : 'closed'}. Look at the window before repeating it.`);
|
|
1751
|
+
const readState = async () => {
|
|
1752
|
+
const newWindow = windows ? await this.newWindowOf(target.pid, windows) : undefined;
|
|
1753
|
+
const after = await this.observe(target);
|
|
1754
|
+
if (!after)
|
|
1755
|
+
return undefined;
|
|
1756
|
+
const change = changeBetween(before, after);
|
|
1757
|
+
const gone = windows ? await this.windowGone(target.pid, windows) : false;
|
|
1758
|
+
if (want === 'expand') {
|
|
1759
|
+
const shown = newWindow?.title.trim() || change.added[0];
|
|
1760
|
+
return { shown, held: newWindow !== undefined || change.added.length > 0 };
|
|
1761
|
+
}
|
|
1762
|
+
return { shown: change.removed[0], held: gone || change.removed.length > 0 };
|
|
1763
|
+
};
|
|
1764
|
+
await this.settle(EXPAND_WAITS_MS[0]);
|
|
1765
|
+
const first = await readState();
|
|
1766
|
+
if (!first)
|
|
1767
|
+
return unverified(`the click on ${what} was sent, but the window could not be read afterwards. Look at the window before repeating it.`);
|
|
1768
|
+
await this.settle(EXPAND_WAITS_MS[1]);
|
|
1769
|
+
const second = await readState();
|
|
1770
|
+
if (first.held && second?.held)
|
|
1771
|
+
return {
|
|
1772
|
+
ok: true,
|
|
1773
|
+
delivery: 'delivered_verified',
|
|
1774
|
+
retrySafety: 'unsafe',
|
|
1775
|
+
...(second.shown !== undefined ? { readBack: { observed: trimText(second.shown) } } : {}),
|
|
1776
|
+
detail: want === 'expand'
|
|
1777
|
+
? `${what} is open: the window shows what it opened, and still did a moment later.`
|
|
1778
|
+
: `${what} is closed: what it showed is gone, and stayed gone a moment later.`,
|
|
1779
|
+
};
|
|
1780
|
+
if (first.held && second && !second.held)
|
|
1781
|
+
return unverified(want === 'expand'
|
|
1782
|
+
? `${what} opened and closed again, so it is not open now. Look at the window; a menu that closes on its own may need the keyboard (alt+letter) or a click in the foreground.`
|
|
1783
|
+
: `${what} looked closed, but what it showed is back. Look at the window before repeating it.`);
|
|
1784
|
+
return unverified(want === 'expand'
|
|
1785
|
+
? `the click on ${what} was sent, but no menu or new control showed in the window afterwards, so it may not be open. Look at the window (a window screenshot) before repeating it.`
|
|
1786
|
+
: `the click on ${what} was sent, but nothing in the window changed, so it may still be open. Look at the window before repeating it.`);
|
|
1787
|
+
}
|
|
1788
|
+
/** Whether a window the program had before (a popup that was open) is no longer listed. */
|
|
1789
|
+
async windowGone(pid, known) {
|
|
1790
|
+
try {
|
|
1791
|
+
const now = new Set((await this.listWindows()).filter((w) => w.pid === pid).map((w) => w.id));
|
|
1792
|
+
for (const id of known)
|
|
1793
|
+
if (!now.has(id))
|
|
1794
|
+
return true;
|
|
1795
|
+
return false;
|
|
1796
|
+
}
|
|
1797
|
+
catch {
|
|
1798
|
+
return false;
|
|
1799
|
+
}
|
|
1800
|
+
}
|
|
1649
1801
|
/**
|
|
1650
1802
|
* A click on a control by its driver token, in the background. Where the
|
|
1651
1803
|
* driver says the window cannot take background input (or is covered) the
|
|
@@ -1739,6 +1891,45 @@ export class CuaDriverAdapter {
|
|
|
1739
1891
|
async dispose() {
|
|
1740
1892
|
await this.client.dispose();
|
|
1741
1893
|
}
|
|
1894
|
+
/**
|
|
1895
|
+
* Let go of the driver process (and the agent cursor it draws) while nobody is using it. The next
|
|
1896
|
+
* call starts it again; every screenshot and ref taken from the old process is then stale, which
|
|
1897
|
+
* the process counter already says.
|
|
1898
|
+
*/
|
|
1899
|
+
async release(stillIdle = () => true) {
|
|
1900
|
+
const before = this.client.starts;
|
|
1901
|
+
const ended = await this.client.release();
|
|
1902
|
+
// Nothing below runs unless the process really ended and no call began while it wound down;
|
|
1903
|
+
// a call that did started its own process, and its frame and helpers are not ours to clear.
|
|
1904
|
+
if (!ended || this.client.starts !== before || !stillIdle())
|
|
1905
|
+
return;
|
|
1906
|
+
// The refs read from the old process are dead. The window screenshot is kept: the model may
|
|
1907
|
+
// still be looking at it, and the first input on it reads the window again for the new process.
|
|
1908
|
+
this.windowReads.clear();
|
|
1909
|
+
this.releasedStarts = before;
|
|
1910
|
+
// The helpers started beside it (the overlay, the accessibility host) are processes too; the
|
|
1911
|
+
// next window list starts them again.
|
|
1912
|
+
this.presence?.release?.();
|
|
1913
|
+
this.a11y?.release?.();
|
|
1914
|
+
}
|
|
1915
|
+
/**
|
|
1916
|
+
* After an idle release the screenshot the model took is still the one it acts on. Read the window
|
|
1917
|
+
* again for the new process (the same size is required) and carry the screenshot over to it.
|
|
1918
|
+
*/
|
|
1919
|
+
async carryFrameOver(captureId) {
|
|
1920
|
+
const frame = this.windowFrame;
|
|
1921
|
+
if (!frame ||
|
|
1922
|
+
frame.id !== captureId ||
|
|
1923
|
+
this.releasedStarts !== frame.processStarts ||
|
|
1924
|
+
frame.generation !== this.sessionGeneration)
|
|
1925
|
+
return;
|
|
1926
|
+
if (!(await this.restoreDriverFrame(frame)))
|
|
1927
|
+
return;
|
|
1928
|
+
if (this.windowFrame !== frame)
|
|
1929
|
+
return;
|
|
1930
|
+
this.windowFrame = { ...frame, processStarts: this.client.starts };
|
|
1931
|
+
this.releasedStarts = undefined;
|
|
1932
|
+
}
|
|
1742
1933
|
/** Tells the overlay where the agent is about to act in a window; never waits and never fails the action. */
|
|
1743
1934
|
showPresence(frame, bounds, tool, args) {
|
|
1744
1935
|
const presence = this.presence;
|
|
@@ -2029,6 +2220,8 @@ export function failureDelivery(error) {
|
|
|
2029
2220
|
const refusal = facts.refusal;
|
|
2030
2221
|
return verdict('not_delivered', 'safe', typeof refusal?.message === 'string' ? refusal.message : error.message);
|
|
2031
2222
|
}
|
|
2223
|
+
if (isNoDriverFrame(error))
|
|
2224
|
+
return verdict('not_delivered', 'safe', 'the driver had no screenshot of this window to place the pixels on, so nothing was sent. Take a new window screenshot and click again.');
|
|
2032
2225
|
if (/stale (window|screenshot)/i.test(error.message))
|
|
2033
2226
|
return verdict('not_delivered', 'safe', trimText(error.message, 300));
|
|
2034
2227
|
if (/stale/i.test(`${error.message} ${JSON.stringify(facts)}`))
|
|
@@ -2037,6 +2230,10 @@ export function failureDelivery(error) {
|
|
|
2037
2230
|
// foreground burst calls this too), so it never invites a blind resend.
|
|
2038
2231
|
return verdict('delivered_unverified', 'unsafe', `${trimText(error.message, 300)} The input may have been sent; look at the window before repeating it.`);
|
|
2039
2232
|
}
|
|
2233
|
+
/** The driver refused a pixel input because it holds no screenshot of the window (nothing was sent). */
|
|
2234
|
+
function isNoDriverFrame(error) {
|
|
2235
|
+
return error instanceof McpToolError && /No screenshot frame for/i.test(error.message);
|
|
2236
|
+
}
|
|
2040
2237
|
/** The driver refused a handle a newer read of its window replaced (nothing was sent). */
|
|
2041
2238
|
function isStaleHandle(error) {
|
|
2042
2239
|
if (/stale (window|screenshot)/i.test(error.message))
|
|
@@ -2175,6 +2372,20 @@ export function windowIdOf(hwnd) {
|
|
|
2175
2372
|
return `0x${hwnd.toString(16)}`;
|
|
2176
2373
|
}
|
|
2177
2374
|
export function parseWindowId(value) {
|
|
2375
|
+
// A handle as an object ({ hwnd }, { id }, { window_id } ...) is read by the id inside it.
|
|
2376
|
+
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
2377
|
+
const record = value;
|
|
2378
|
+
for (const key of ['window_id', 'windowId', 'hwnd', 'handle', 'id', 'value']) {
|
|
2379
|
+
if (record[key] === undefined || typeof record[key] === 'object')
|
|
2380
|
+
continue;
|
|
2381
|
+
const found = parseWindowId(record[key]);
|
|
2382
|
+
if (found !== undefined)
|
|
2383
|
+
return found;
|
|
2384
|
+
}
|
|
2385
|
+
return undefined;
|
|
2386
|
+
}
|
|
2387
|
+
if (typeof value === 'bigint')
|
|
2388
|
+
return value > 0n && value <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(value) : undefined;
|
|
2178
2389
|
if (typeof value === 'number')
|
|
2179
2390
|
return Number.isSafeInteger(value) && value > 0 ? value : undefined;
|
|
2180
2391
|
if (typeof value !== 'string')
|