@namzu/computer-use 4.3.1 → 4.4.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 +12 -0
- package/dist/SubprocessComputerUseHost.d.ts +13 -0
- package/dist/SubprocessComputerUseHost.d.ts.map +1 -1
- package/dist/SubprocessComputerUseHost.js +52 -11
- package/dist/SubprocessComputerUseHost.js.map +1 -1
- package/dist/a11y/a11y.d.ts +1 -0
- package/dist/a11y/a11y.d.ts.map +1 -1
- package/dist/a11y/a11y.js +11 -0
- package/dist/a11y/a11y.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/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 +176 -3
- 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/types.d.ts +5 -0
- package/dist/adapters/types.d.ts.map +1 -1
- package/dist/adapters/types.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/presence/presence.d.ts +8 -0
- 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/package.json +2 -2
- package/src/SubprocessComputerUseHost.ts +54 -8
- package/src/a11y/a11y.ts +11 -0
- package/src/a11y/interface.ts +5 -0
- package/src/adapters/cua-driver/adapter.ts +190 -2
- package/src/adapters/cua-driver/client.ts +27 -0
- package/src/adapters/types.ts +5 -0
- package/src/foreground/guard.ts +1 -0
- package/src/presence/presence.ts +20 -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;
|
|
@@ -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
|
|
@@ -238,6 +240,8 @@ export class CuaDriverAdapter {
|
|
|
238
240
|
driverCursorOff;
|
|
239
241
|
/** cua-driver's screenshot resize registry is per PID, so only this capture may drive input. */
|
|
240
242
|
windowFrame;
|
|
243
|
+
/** The process count at the last idle release, for a screenshot taken from that process. */
|
|
244
|
+
releasedStarts;
|
|
241
245
|
/** Capture and scoped input must not race over cua-driver's per-PID resize ratio. */
|
|
242
246
|
windowOperation = Promise.resolve();
|
|
243
247
|
/** 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 +510,28 @@ export class CuaDriverAdapter {
|
|
|
506
510
|
async captureWindow(id) {
|
|
507
511
|
return this.serializeWindowOperation(() => this.captureWindowInternal(id));
|
|
508
512
|
}
|
|
513
|
+
/**
|
|
514
|
+
* Read the window again with a screenshot, so the driver can place pixels of `frame` on it.
|
|
515
|
+
* True when that read has the size of the screenshot the model is looking at.
|
|
516
|
+
*/
|
|
517
|
+
async restoreDriverFrame(frame) {
|
|
518
|
+
const hwnd = parseWindowId(frame.window.id);
|
|
519
|
+
if (hwnd === undefined)
|
|
520
|
+
return false;
|
|
521
|
+
try {
|
|
522
|
+
const result = await this.callTool('get_window_state', {
|
|
523
|
+
pid: frame.window.pid,
|
|
524
|
+
window_id: hwnd,
|
|
525
|
+
include_accessibility_tree: false,
|
|
526
|
+
include_screenshot: true,
|
|
527
|
+
});
|
|
528
|
+
const facts = structured(result, 'get_window_state');
|
|
529
|
+
return facts.screenshot_width === frame.width && facts.screenshot_height === frame.height;
|
|
530
|
+
}
|
|
531
|
+
catch {
|
|
532
|
+
return false;
|
|
533
|
+
}
|
|
534
|
+
}
|
|
509
535
|
async captureWindowInternal(id) {
|
|
510
536
|
const target = await this.resolveWindow(id);
|
|
511
537
|
// A failed or overlapping capture invalidates the old resize ratio even
|
|
@@ -639,6 +665,7 @@ export class CuaDriverAdapter {
|
|
|
639
665
|
return { ...again, detail: finalDetail };
|
|
640
666
|
}
|
|
641
667
|
async attemptWindowInput(captureId, action, route) {
|
|
668
|
+
await this.carryFrameOver(captureId);
|
|
642
669
|
const frame = this.windowFrame;
|
|
643
670
|
const watched = action.type === 'type_text' || action.type === 'key' || action.type === 'mouse_click';
|
|
644
671
|
const hwnd = frame && frame.id === captureId ? parseWindowId(frame.window.id) : undefined;
|
|
@@ -837,6 +864,7 @@ export class CuaDriverAdapter {
|
|
|
837
864
|
}
|
|
838
865
|
}
|
|
839
866
|
async deliverWindowInput(captureId, action, route = { foreground: false, onScreen: false }, listing) {
|
|
867
|
+
await this.carryFrameOver(captureId);
|
|
840
868
|
const frame = this.windowFrame;
|
|
841
869
|
if (!frame || frame.id !== captureId)
|
|
842
870
|
throw new Error('computer-use: this window screenshot is stale; capture the window again before acting.');
|
|
@@ -879,7 +907,19 @@ export class CuaDriverAdapter {
|
|
|
879
907
|
throw new Error('computer-use: the captured window changed or its driver restarted; take a new window screenshot.');
|
|
880
908
|
this.showPresence(frame, current.bounds, name, args);
|
|
881
909
|
await this.markForSettle(target.window_id);
|
|
882
|
-
|
|
910
|
+
const sendOnce = () => this.callTool(name, { ...target, ...args }, undefined, undefined, undefined, frame);
|
|
911
|
+
try {
|
|
912
|
+
return await sendOnce();
|
|
913
|
+
}
|
|
914
|
+
catch (error) {
|
|
915
|
+
// The driver keeps one latest read per window, and a read of the controls (made just before
|
|
916
|
+
// an input to judge it) replaces a screenshot with one that has none. It then refuses a
|
|
917
|
+
// pixel input and sends nothing. The window is unchanged (checked above), so the same
|
|
918
|
+
// screenshot is taken again for the driver and the input goes once more.
|
|
919
|
+
if (!isNoDriverFrame(error) || !(await this.restoreDriverFrame(frame)))
|
|
920
|
+
throw error;
|
|
921
|
+
return await sendOnce();
|
|
922
|
+
}
|
|
883
923
|
};
|
|
884
924
|
// The driver's hotkey finds a shortcut only through an accelerator the window names, and refuses (sending
|
|
885
925
|
// nothing) when there is none; the same combination as a key with modifiers is real input.
|
|
@@ -1332,9 +1372,10 @@ export class CuaDriverAdapter {
|
|
|
1332
1372
|
}));
|
|
1333
1373
|
try {
|
|
1334
1374
|
switch (action) {
|
|
1335
|
-
case 'invoke':
|
|
1336
1375
|
case 'expand':
|
|
1337
1376
|
case 'collapse':
|
|
1377
|
+
return await this.expandAndWatch(facts, uiRef, action);
|
|
1378
|
+
case 'invoke':
|
|
1338
1379
|
// cua-driver's click on an element token performs the control's
|
|
1339
1380
|
// own pattern in the background (Invoke, or a posted click at its
|
|
1340
1381
|
// centre), with no pointer move and no change of foreground.
|
|
@@ -1646,6 +1687,79 @@ export class CuaDriverAdapter {
|
|
|
1646
1687
|
...(seen.observed !== undefined ? { readBack: { observed: seen.observed } } : {}),
|
|
1647
1688
|
};
|
|
1648
1689
|
}
|
|
1690
|
+
/**
|
|
1691
|
+
* expand or collapse, which the driver sends as a click and cannot read back: the control reports no
|
|
1692
|
+
* expanded state. What counts is what the window shows afterwards, and it has to still be there a
|
|
1693
|
+
* moment later. A menu that opened and closed again is not an expanded menu.
|
|
1694
|
+
*/
|
|
1695
|
+
async expandAndWatch(facts, uiRef, want) {
|
|
1696
|
+
const target = { hwnd: facts.hwnd, pid: facts.pid };
|
|
1697
|
+
const before = await this.observe(target);
|
|
1698
|
+
const windows = await this.windowIdsOf(target.pid);
|
|
1699
|
+
// Each read of the window replaces the driver's handles: click through the refreshed one.
|
|
1700
|
+
const current = this.uiRefs.get(uiRef.ref) ?? facts;
|
|
1701
|
+
const sent = await this.clickRef(current, { ref: uiRef.ref, facts: current });
|
|
1702
|
+
if (!sent.ok || sent.delivery === 'not_delivered')
|
|
1703
|
+
return sent;
|
|
1704
|
+
const what = describeEvidence(current);
|
|
1705
|
+
const unverified = (detail) => ({
|
|
1706
|
+
ok: true,
|
|
1707
|
+
delivery: 'delivered_unverified',
|
|
1708
|
+
retrySafety: 'unsafe',
|
|
1709
|
+
detail,
|
|
1710
|
+
});
|
|
1711
|
+
if (!before)
|
|
1712
|
+
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.`);
|
|
1713
|
+
const readState = async () => {
|
|
1714
|
+
const newWindow = windows ? await this.newWindowOf(target.pid, windows) : undefined;
|
|
1715
|
+
const after = await this.observe(target);
|
|
1716
|
+
if (!after)
|
|
1717
|
+
return undefined;
|
|
1718
|
+
const change = changeBetween(before, after);
|
|
1719
|
+
const gone = windows ? await this.windowGone(target.pid, windows) : false;
|
|
1720
|
+
if (want === 'expand') {
|
|
1721
|
+
const shown = newWindow?.title.trim() || change.added[0];
|
|
1722
|
+
return { shown, held: newWindow !== undefined || change.added.length > 0 };
|
|
1723
|
+
}
|
|
1724
|
+
return { shown: change.removed[0], held: gone || change.removed.length > 0 };
|
|
1725
|
+
};
|
|
1726
|
+
await this.settle(EXPAND_WAITS_MS[0]);
|
|
1727
|
+
const first = await readState();
|
|
1728
|
+
if (!first)
|
|
1729
|
+
return unverified(`the click on ${what} was sent, but the window could not be read afterwards. Look at the window before repeating it.`);
|
|
1730
|
+
await this.settle(EXPAND_WAITS_MS[1]);
|
|
1731
|
+
const second = await readState();
|
|
1732
|
+
if (first.held && second?.held)
|
|
1733
|
+
return {
|
|
1734
|
+
ok: true,
|
|
1735
|
+
delivery: 'delivered_verified',
|
|
1736
|
+
retrySafety: 'unsafe',
|
|
1737
|
+
...(second.shown !== undefined ? { readBack: { observed: trimText(second.shown) } } : {}),
|
|
1738
|
+
detail: want === 'expand'
|
|
1739
|
+
? `${what} is open: the window shows what it opened, and still did a moment later.`
|
|
1740
|
+
: `${what} is closed: what it showed is gone, and stayed gone a moment later.`,
|
|
1741
|
+
};
|
|
1742
|
+
if (first.held && second && !second.held)
|
|
1743
|
+
return unverified(want === 'expand'
|
|
1744
|
+
? `${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.`
|
|
1745
|
+
: `${what} looked closed, but what it showed is back. Look at the window before repeating it.`);
|
|
1746
|
+
return unverified(want === 'expand'
|
|
1747
|
+
? `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.`
|
|
1748
|
+
: `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.`);
|
|
1749
|
+
}
|
|
1750
|
+
/** Whether a window the program had before (a popup that was open) is no longer listed. */
|
|
1751
|
+
async windowGone(pid, known) {
|
|
1752
|
+
try {
|
|
1753
|
+
const now = new Set((await this.listWindows()).filter((w) => w.pid === pid).map((w) => w.id));
|
|
1754
|
+
for (const id of known)
|
|
1755
|
+
if (!now.has(id))
|
|
1756
|
+
return true;
|
|
1757
|
+
return false;
|
|
1758
|
+
}
|
|
1759
|
+
catch {
|
|
1760
|
+
return false;
|
|
1761
|
+
}
|
|
1762
|
+
}
|
|
1649
1763
|
/**
|
|
1650
1764
|
* A click on a control by its driver token, in the background. Where the
|
|
1651
1765
|
* driver says the window cannot take background input (or is covered) the
|
|
@@ -1739,6 +1853,45 @@ export class CuaDriverAdapter {
|
|
|
1739
1853
|
async dispose() {
|
|
1740
1854
|
await this.client.dispose();
|
|
1741
1855
|
}
|
|
1856
|
+
/**
|
|
1857
|
+
* Let go of the driver process (and the agent cursor it draws) while nobody is using it. The next
|
|
1858
|
+
* call starts it again; every screenshot and ref taken from the old process is then stale, which
|
|
1859
|
+
* the process counter already says.
|
|
1860
|
+
*/
|
|
1861
|
+
async release(stillIdle = () => true) {
|
|
1862
|
+
const before = this.client.starts;
|
|
1863
|
+
const ended = await this.client.release();
|
|
1864
|
+
// Nothing below runs unless the process really ended and no call began while it wound down;
|
|
1865
|
+
// a call that did started its own process, and its frame and helpers are not ours to clear.
|
|
1866
|
+
if (!ended || this.client.starts !== before || !stillIdle())
|
|
1867
|
+
return;
|
|
1868
|
+
// The refs read from the old process are dead. The window screenshot is kept: the model may
|
|
1869
|
+
// still be looking at it, and the first input on it reads the window again for the new process.
|
|
1870
|
+
this.windowReads.clear();
|
|
1871
|
+
this.releasedStarts = before;
|
|
1872
|
+
// The helpers started beside it (the overlay, the accessibility host) are processes too; the
|
|
1873
|
+
// next window list starts them again.
|
|
1874
|
+
this.presence?.release?.();
|
|
1875
|
+
this.a11y?.release?.();
|
|
1876
|
+
}
|
|
1877
|
+
/**
|
|
1878
|
+
* After an idle release the screenshot the model took is still the one it acts on. Read the window
|
|
1879
|
+
* again for the new process (the same size is required) and carry the screenshot over to it.
|
|
1880
|
+
*/
|
|
1881
|
+
async carryFrameOver(captureId) {
|
|
1882
|
+
const frame = this.windowFrame;
|
|
1883
|
+
if (!frame ||
|
|
1884
|
+
frame.id !== captureId ||
|
|
1885
|
+
this.releasedStarts !== frame.processStarts ||
|
|
1886
|
+
frame.generation !== this.sessionGeneration)
|
|
1887
|
+
return;
|
|
1888
|
+
if (!(await this.restoreDriverFrame(frame)))
|
|
1889
|
+
return;
|
|
1890
|
+
if (this.windowFrame !== frame)
|
|
1891
|
+
return;
|
|
1892
|
+
this.windowFrame = { ...frame, processStarts: this.client.starts };
|
|
1893
|
+
this.releasedStarts = undefined;
|
|
1894
|
+
}
|
|
1742
1895
|
/** Tells the overlay where the agent is about to act in a window; never waits and never fails the action. */
|
|
1743
1896
|
showPresence(frame, bounds, tool, args) {
|
|
1744
1897
|
const presence = this.presence;
|
|
@@ -2029,6 +2182,8 @@ export function failureDelivery(error) {
|
|
|
2029
2182
|
const refusal = facts.refusal;
|
|
2030
2183
|
return verdict('not_delivered', 'safe', typeof refusal?.message === 'string' ? refusal.message : error.message);
|
|
2031
2184
|
}
|
|
2185
|
+
if (isNoDriverFrame(error))
|
|
2186
|
+
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
2187
|
if (/stale (window|screenshot)/i.test(error.message))
|
|
2033
2188
|
return verdict('not_delivered', 'safe', trimText(error.message, 300));
|
|
2034
2189
|
if (/stale/i.test(`${error.message} ${JSON.stringify(facts)}`))
|
|
@@ -2037,6 +2192,10 @@ export function failureDelivery(error) {
|
|
|
2037
2192
|
// foreground burst calls this too), so it never invites a blind resend.
|
|
2038
2193
|
return verdict('delivered_unverified', 'unsafe', `${trimText(error.message, 300)} The input may have been sent; look at the window before repeating it.`);
|
|
2039
2194
|
}
|
|
2195
|
+
/** The driver refused a pixel input because it holds no screenshot of the window (nothing was sent). */
|
|
2196
|
+
function isNoDriverFrame(error) {
|
|
2197
|
+
return error instanceof McpToolError && /No screenshot frame for/i.test(error.message);
|
|
2198
|
+
}
|
|
2040
2199
|
/** The driver refused a handle a newer read of its window replaced (nothing was sent). */
|
|
2041
2200
|
function isStaleHandle(error) {
|
|
2042
2201
|
if (/stale (window|screenshot)/i.test(error.message))
|
|
@@ -2175,6 +2334,20 @@ export function windowIdOf(hwnd) {
|
|
|
2175
2334
|
return `0x${hwnd.toString(16)}`;
|
|
2176
2335
|
}
|
|
2177
2336
|
export function parseWindowId(value) {
|
|
2337
|
+
// A handle as an object ({ hwnd }, { id }, { window_id } ...) is read by the id inside it.
|
|
2338
|
+
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
2339
|
+
const record = value;
|
|
2340
|
+
for (const key of ['window_id', 'windowId', 'hwnd', 'handle', 'id', 'value']) {
|
|
2341
|
+
if (record[key] === undefined || typeof record[key] === 'object')
|
|
2342
|
+
continue;
|
|
2343
|
+
const found = parseWindowId(record[key]);
|
|
2344
|
+
if (found !== undefined)
|
|
2345
|
+
return found;
|
|
2346
|
+
}
|
|
2347
|
+
return undefined;
|
|
2348
|
+
}
|
|
2349
|
+
if (typeof value === 'bigint')
|
|
2350
|
+
return value > 0n && value <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(value) : undefined;
|
|
2178
2351
|
if (typeof value === 'number')
|
|
2179
2352
|
return Number.isSafeInteger(value) && value > 0 ? value : undefined;
|
|
2180
2353
|
if (typeof value !== 'string')
|