@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.
Files changed (121) hide show
  1. package/CHANGELOG.md +58 -1
  2. package/dist/SubprocessComputerUseHost.d.ts +57 -9
  3. package/dist/SubprocessComputerUseHost.d.ts.map +1 -1
  4. package/dist/SubprocessComputerUseHost.js +213 -46
  5. package/dist/SubprocessComputerUseHost.js.map +1 -1
  6. package/dist/adapters/cua-driver/adapter.d.ts +171 -10
  7. package/dist/adapters/cua-driver/adapter.d.ts.map +1 -1
  8. package/dist/adapters/cua-driver/adapter.js +1476 -161
  9. package/dist/adapters/cua-driver/adapter.js.map +1 -1
  10. package/dist/adapters/cua-driver/client.d.ts +16 -0
  11. package/dist/adapters/cua-driver/client.d.ts.map +1 -1
  12. package/dist/adapters/cua-driver/client.js +54 -5
  13. package/dist/adapters/cua-driver/client.js.map +1 -1
  14. package/dist/adapters/cua-driver/escalation.d.ts +67 -0
  15. package/dist/adapters/cua-driver/escalation.d.ts.map +1 -0
  16. package/dist/adapters/cua-driver/escalation.js +82 -0
  17. package/dist/adapters/cua-driver/escalation.js.map +1 -0
  18. package/dist/adapters/cua-driver/keys.d.ts +5 -0
  19. package/dist/adapters/cua-driver/keys.d.ts.map +1 -1
  20. package/dist/adapters/cua-driver/keys.js +23 -0
  21. package/dist/adapters/cua-driver/keys.js.map +1 -1
  22. package/dist/adapters/cua-driver/observe.d.ts +99 -0
  23. package/dist/adapters/cua-driver/observe.d.ts.map +1 -0
  24. package/dist/adapters/cua-driver/observe.js +227 -0
  25. package/dist/adapters/cua-driver/observe.js.map +1 -0
  26. package/dist/adapters/cua-driver/ui-tree.d.ts +75 -2
  27. package/dist/adapters/cua-driver/ui-tree.d.ts.map +1 -1
  28. package/dist/adapters/cua-driver/ui-tree.js +142 -14
  29. package/dist/adapters/cua-driver/ui-tree.js.map +1 -1
  30. package/dist/adapters/types.d.ts +3 -3
  31. package/dist/adapters/types.d.ts.map +1 -1
  32. package/dist/adapters/types.js.map +1 -1
  33. package/dist/adapters/win32.d.ts +10 -0
  34. package/dist/adapters/win32.d.ts.map +1 -1
  35. package/dist/adapters/win32.js +12 -2
  36. package/dist/adapters/win32.js.map +1 -1
  37. package/dist/errors.d.ts +19 -0
  38. package/dist/errors.d.ts.map +1 -1
  39. package/dist/errors.js +26 -0
  40. package/dist/errors.js.map +1 -1
  41. package/dist/foreground/guard.d.ts +31 -0
  42. package/dist/foreground/guard.d.ts.map +1 -0
  43. package/dist/foreground/guard.js +156 -0
  44. package/dist/foreground/guard.js.map +1 -0
  45. package/dist/foreground/index.d.ts +11 -0
  46. package/dist/foreground/index.d.ts.map +1 -0
  47. package/dist/foreground/index.js +7 -0
  48. package/dist/foreground/index.js.map +1 -0
  49. package/dist/foreground/ladder.d.ts +143 -0
  50. package/dist/foreground/ladder.d.ts.map +1 -0
  51. package/dist/foreground/ladder.js +505 -0
  52. package/dist/foreground/ladder.js.map +1 -0
  53. package/dist/foreground/lease.d.ts +69 -0
  54. package/dist/foreground/lease.d.ts.map +1 -0
  55. package/dist/foreground/lease.js +182 -0
  56. package/dist/foreground/lease.js.map +1 -0
  57. package/dist/foreground/types.d.ts +287 -0
  58. package/dist/foreground/types.d.ts.map +1 -0
  59. package/dist/foreground/types.js +32 -0
  60. package/dist/foreground/types.js.map +1 -0
  61. package/dist/foreground/windows-helper-script.d.ts +19 -0
  62. package/dist/foreground/windows-helper-script.d.ts.map +1 -0
  63. package/dist/foreground/windows-helper-script.js +472 -0
  64. package/dist/foreground/windows-helper-script.js.map +1 -0
  65. package/dist/foreground/windows-monitor.d.ts +75 -0
  66. package/dist/foreground/windows-monitor.d.ts.map +1 -0
  67. package/dist/foreground/windows-monitor.js +216 -0
  68. package/dist/foreground/windows-monitor.js.map +1 -0
  69. package/dist/foreground/windows.d.ts +21 -0
  70. package/dist/foreground/windows.d.ts.map +1 -0
  71. package/dist/foreground/windows.js +54 -0
  72. package/dist/foreground/windows.js.map +1 -0
  73. package/dist/index.d.ts +6 -1
  74. package/dist/index.d.ts.map +1 -1
  75. package/dist/index.js +3 -1
  76. package/dist/index.js.map +1 -1
  77. package/dist/presence/helper-script.d.ts +39 -0
  78. package/dist/presence/helper-script.d.ts.map +1 -0
  79. package/dist/presence/helper-script.js +320 -0
  80. package/dist/presence/helper-script.js.map +1 -0
  81. package/dist/presence/index.d.ts +6 -0
  82. package/dist/presence/index.d.ts.map +1 -0
  83. package/dist/presence/index.js +4 -0
  84. package/dist/presence/index.js.map +1 -0
  85. package/dist/presence/presence.d.ts +70 -0
  86. package/dist/presence/presence.d.ts.map +1 -0
  87. package/dist/presence/presence.js +147 -0
  88. package/dist/presence/presence.js.map +1 -0
  89. package/dist/presence/windows.d.ts +19 -0
  90. package/dist/presence/windows.d.ts.map +1 -0
  91. package/dist/presence/windows.js +39 -0
  92. package/dist/presence/windows.js.map +1 -0
  93. package/dist/util/call-scope.d.ts +34 -0
  94. package/dist/util/call-scope.d.ts.map +1 -0
  95. package/dist/util/call-scope.js +58 -0
  96. package/dist/util/call-scope.js.map +1 -0
  97. package/package.json +2 -2
  98. package/src/SubprocessComputerUseHost.ts +287 -45
  99. package/src/adapters/cua-driver/adapter.ts +1751 -175
  100. package/src/adapters/cua-driver/client.ts +59 -2
  101. package/src/adapters/cua-driver/escalation.ts +137 -0
  102. package/src/adapters/cua-driver/keys.ts +20 -0
  103. package/src/adapters/cua-driver/observe.ts +310 -0
  104. package/src/adapters/cua-driver/ui-tree.ts +219 -15
  105. package/src/adapters/types.ts +7 -2
  106. package/src/adapters/win32.ts +26 -3
  107. package/src/errors.ts +32 -0
  108. package/src/foreground/guard.ts +182 -0
  109. package/src/foreground/index.ts +45 -0
  110. package/src/foreground/ladder.ts +616 -0
  111. package/src/foreground/lease.ts +232 -0
  112. package/src/foreground/types.ts +318 -0
  113. package/src/foreground/windows-helper-script.ts +471 -0
  114. package/src/foreground/windows-monitor.ts +288 -0
  115. package/src/foreground/windows.ts +73 -0
  116. package/src/index.ts +15 -1
  117. package/src/presence/helper-script.ts +323 -0
  118. package/src/presence/index.ts +17 -0
  119. package/src/presence/presence.ts +195 -0
  120. package/src/presence/windows.ts +54 -0
  121. package/src/util/call-scope.ts +73 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"windows.js","sourceRoot":"","sources":["../../src/presence/windows.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,EACL,aAAa,EACb,WAAW,EACX,iBAAiB,GACjB,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAiB,eAAe,EAAE,MAAM,eAAe,CAAA;AAE9D,uDAAuD;AACvD,MAAM,WAAW,GAAG,qDAAqD,CAAA;AAEzE,gGAAgG;AAChG,MAAM,CAAC,MAAM,YAAY,GAAG,6BAA6B,CAAA;AAUzD,8DAA8D;AAC9D,MAAM,UAAU,qBAAqB,CAAC,GAAsB;IAC3D,MAAM,KAAK,GAAG,GAAG,CAAC,YAAY,CAAC,CAAA;IAC/B,OAAO,KAAK,KAAK,SAAS,IAAI,0BAA0B,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAA;AAC5E,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,qBAAqB,CACpC,UAAyC,EAAE;IAE3C,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,CAAA;IACtC,IAAI,OAAO,CAAC,OAAO,KAAK,KAAK,IAAI,qBAAqB,CAAC,GAAG,CAAC;QAAE,OAAO,SAAS,CAAA;IAC7E,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,OAAO,CAAC,QAAQ,CAAA;IACrD,MAAM,MAAM,GAAG,OAAO,CAAC,SAAS,IAAI,aAAa,CAAA;IACjD,MAAM,GAAG,GAAG,KAAK,CAAC,GAAG,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAA;IACxC,MAAM,UAAU,GAAG,GAAG;QACrB,CAAC,CAAC,iBAAiB,CAAC,MAAM,CAAC;QAC3B,CAAC,CAAC,QAAQ,KAAK,OAAO;YACrB,CAAC,CAAC,gBAAgB;YAClB,CAAC,CAAC,SAAS,CAAA;IACb,IAAI,CAAC,UAAU;QAAE,OAAO,SAAS,CAAA;IACjC,MAAM,KAAK,GAAsB,EAAE,CAAA;IACnC,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC;QAAE,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC;YAAE,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,CAAA;IACjG,OAAO,IAAI,eAAe,CAAC;QAC1B,UAAU;QACV,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,WAAW,CAAC,KAAK,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK;KACjD,CAAC,CAAA;AACH,CAAC"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * A request was cancelled by its caller. `sent` tells whether it had been
3
+ * written to the desktop driver: when it had, the driver may still act on it,
4
+ * and the caller must not assume it did not.
5
+ */
6
+ export declare class CallAbortedError extends Error {
7
+ readonly method: string;
8
+ readonly sent: boolean;
9
+ readonly name = "CallAbortedError";
10
+ constructor(method: string, sent: boolean);
11
+ }
12
+ /** What the tool call being served shares with every driver call made for it. */
13
+ export interface CallScope {
14
+ readonly signal: AbortSignal | undefined;
15
+ /** Something that changes the desktop was sent for this call (or may have been). */
16
+ sent: boolean;
17
+ }
18
+ /**
19
+ * Runs `run` as one tool call. Its `signal` reaches every driver call and
20
+ * foreground burst made on its behalf without each internal helper taking a
21
+ * parameter, and the scope records whether anything was sent.
22
+ */
23
+ export declare function inCallScope<T>(signal: AbortSignal | undefined, run: (scope: CallScope) => Promise<T>): Promise<T>;
24
+ /** The signal of the call being served, when it has one. */
25
+ export declare function currentCallSignal(): AbortSignal | undefined;
26
+ /** Records that the call being served sent (or may have sent) something that changes the desktop. */
27
+ export declare function markSent(): void;
28
+ /**
29
+ * Waits for `work`, but no longer than the call being served lives: when its
30
+ * signal aborts first, rejects with `CallAbortedError` (nothing was sent). The
31
+ * work itself is left to finish on its own.
32
+ */
33
+ export declare function abortable<T>(name: string, work: Promise<T>): Promise<T>;
34
+ //# sourceMappingURL=call-scope.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"call-scope.d.ts","sourceRoot":"","sources":["../../src/util/call-scope.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;IAIzC,QAAQ,CAAC,MAAM,EAAE,MAAM;IACvB,QAAQ,CAAC,IAAI,EAAE,OAAO;IAJvB,SAAkB,IAAI,sBAAqB;gBAGjC,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,OAAO;CAQvB;AAED,iFAAiF;AACjF,MAAM,WAAW,SAAS;IACzB,QAAQ,CAAC,MAAM,EAAE,WAAW,GAAG,SAAS,CAAA;IACxC,oFAAoF;IACpF,IAAI,EAAE,OAAO,CAAA;CACb;AAID;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAC5B,MAAM,EAAE,WAAW,GAAG,SAAS,EAC/B,GAAG,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,OAAO,CAAC,CAAC,CAAC,GACnC,OAAO,CAAC,CAAC,CAAC,CAGZ;AAED,4DAA4D;AAC5D,wBAAgB,iBAAiB,IAAI,WAAW,GAAG,SAAS,CAE3D;AAED,qGAAqG;AACrG,wBAAgB,QAAQ,IAAI,IAAI,CAG/B;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAYvE"}
@@ -0,0 +1,58 @@
1
+ import { AsyncLocalStorage } from 'node:async_hooks';
2
+ /**
3
+ * A request was cancelled by its caller. `sent` tells whether it had been
4
+ * written to the desktop driver: when it had, the driver may still act on it,
5
+ * and the caller must not assume it did not.
6
+ */
7
+ export class CallAbortedError extends Error {
8
+ method;
9
+ sent;
10
+ name = 'CallAbortedError';
11
+ constructor(method, sent) {
12
+ super(sent
13
+ ? `${method} was cancelled after it was sent; the driver may still have acted on it.`
14
+ : `${method} was cancelled before it was sent.`);
15
+ this.method = method;
16
+ this.sent = sent;
17
+ }
18
+ }
19
+ const scope = new AsyncLocalStorage();
20
+ /**
21
+ * Runs `run` as one tool call. Its `signal` reaches every driver call and
22
+ * foreground burst made on its behalf without each internal helper taking a
23
+ * parameter, and the scope records whether anything was sent.
24
+ */
25
+ export function inCallScope(signal, run) {
26
+ const own = { signal, sent: false };
27
+ return scope.run(own, () => run(own));
28
+ }
29
+ /** The signal of the call being served, when it has one. */
30
+ export function currentCallSignal() {
31
+ return scope.getStore()?.signal;
32
+ }
33
+ /** Records that the call being served sent (or may have sent) something that changes the desktop. */
34
+ export function markSent() {
35
+ const own = scope.getStore();
36
+ if (own)
37
+ own.sent = true;
38
+ }
39
+ /**
40
+ * Waits for `work`, but no longer than the call being served lives: when its
41
+ * signal aborts first, rejects with `CallAbortedError` (nothing was sent). The
42
+ * work itself is left to finish on its own.
43
+ */
44
+ export function abortable(name, work) {
45
+ const signal = currentCallSignal();
46
+ if (!signal)
47
+ return work;
48
+ if (signal.aborted) {
49
+ work.catch(() => undefined);
50
+ return Promise.reject(new CallAbortedError(name, false));
51
+ }
52
+ return new Promise((resolve, reject) => {
53
+ const onAbort = () => reject(new CallAbortedError(name, false));
54
+ signal.addEventListener('abort', onAbort, { once: true });
55
+ work.then(resolve, reject).finally(() => signal.removeEventListener('abort', onAbort));
56
+ });
57
+ }
58
+ //# sourceMappingURL=call-scope.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"call-scope.js","sourceRoot":"","sources":["../../src/util/call-scope.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAEpD;;;;GAIG;AACH,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAIhC;IACA;IAJQ,IAAI,GAAG,kBAAkB,CAAA;IAE3C,YACU,MAAc,EACd,IAAa;QAEtB,KAAK,CACJ,IAAI;YACH,CAAC,CAAC,GAAG,MAAM,0EAA0E;YACrF,CAAC,CAAC,GAAG,MAAM,oCAAoC,CAChD,CAAA;QAPQ,WAAM,GAAN,MAAM,CAAQ;QACd,SAAI,GAAJ,IAAI,CAAS;IAOvB,CAAC;CACD;AASD,MAAM,KAAK,GAAG,IAAI,iBAAiB,EAAa,CAAA;AAEhD;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAC1B,MAA+B,EAC/B,GAAqC;IAErC,MAAM,GAAG,GAAc,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,CAAA;IAC9C,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAA;AACtC,CAAC;AAED,4DAA4D;AAC5D,MAAM,UAAU,iBAAiB;IAChC,OAAO,KAAK,CAAC,QAAQ,EAAE,EAAE,MAAM,CAAA;AAChC,CAAC;AAED,qGAAqG;AACrG,MAAM,UAAU,QAAQ;IACvB,MAAM,GAAG,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAA;IAC5B,IAAI,GAAG;QAAE,GAAG,CAAC,IAAI,GAAG,IAAI,CAAA;AACzB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAAI,IAAY,EAAE,IAAgB;IAC1D,MAAM,MAAM,GAAG,iBAAiB,EAAE,CAAA;IAClC,IAAI,CAAC,MAAM;QAAE,OAAO,IAAI,CAAA;IACxB,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;QACpB,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAA;QAC3B,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,gBAAgB,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAA;IACzD,CAAC;IACD,OAAO,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACzC,MAAM,OAAO,GAAG,GAAS,EAAE,CAAC,MAAM,CAAC,IAAI,gBAAgB,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAA;QACrE,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAA;QACzD,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAA;IACvF,CAAC,CAAC,CAAA;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@namzu/computer-use",
3
- "version": "3.1.0",
3
+ "version": "4.0.0",
4
4
  "description": "Subprocess-based computer-use host for @namzu/sdk. Screenshot, mouse, and keyboard control via platform-native CLIs (screencapture/osascript on darwin, xdotool/maim on Linux X11, grim/wtype/ydotool on Wayland, PowerShell on Windows).",
5
5
  "license": "FSL-1.1-MIT",
6
6
  "engines": {
@@ -53,7 +53,7 @@
53
53
  "@types/node": "^22.19.17",
54
54
  "typescript": "^5.5.0",
55
55
  "vitest": "^4.1.11",
56
- "@namzu/sdk": "^48.3.0"
56
+ "@namzu/sdk": "^60.0.0"
57
57
  },
58
58
  "scripts": {
59
59
  "build": "tsc --build",
@@ -10,14 +10,29 @@ import type {
10
10
  UiActResult,
11
11
  UiElementAction,
12
12
  UiSnapshot,
13
+ UiSnapshotOptions,
13
14
  WindowInfo,
14
15
  WindowInputAction,
16
+ WindowInputResult,
15
17
  WindowScreenshotResult,
16
18
  } from '@namzu/sdk'
17
19
  import { type Adapter, AdapterUnavailableError } from './adapters/types.js'
18
20
  import type { Win32AdapterOptions } from './adapters/win32.js'
19
21
  import { detectDisplayServer } from './detect/index.js'
20
- import { ComputerUseOutcomeUnknownError } from './errors.js'
22
+ import {
23
+ ComputerUseLockedError,
24
+ ComputerUseOutcomeUnknownError,
25
+ ComputerUseStoppedError,
26
+ LOCKED_TEXT,
27
+ } from './errors.js'
28
+ import {
29
+ type ForegroundApprover,
30
+ type ForegroundLadder,
31
+ type LadderTiming,
32
+ createWindowsForegroundLadder,
33
+ } from './foreground/index.js'
34
+ import { type Presence, createWindowsPresence } from './presence/index.js'
35
+ import { CallAbortedError, inCallScope } from './util/call-scope.js'
21
36
  import { SpawnError } from './util/spawn.js'
22
37
 
23
38
  const UNINITIALISED_CAPABILITIES: ComputerUseCapabilities = {
@@ -51,6 +66,33 @@ export interface SubprocessComputerUseHostOptions {
51
66
  * first use, with the PowerShell path as its fallback. See the README.
52
67
  */
53
68
  readonly windows?: SubprocessComputerUseHostWindowsOptions
69
+ /**
70
+ * The foreground ladder: how an action that can only be delivered in the
71
+ * foreground uses the user's screen. On Windows it is on by default.
72
+ */
73
+ readonly foreground?: SubprocessComputerUseHostForegroundOptions
74
+ /**
75
+ * The agent cursor and the border around the window being operated. On by
76
+ * default on Windows; `false` turns them off, as does the environment
77
+ * variable `NAMZU_COMPUTER_USE_PRESENCE=off`. A ready overlay replaces the
78
+ * one built for this machine (for tests).
79
+ */
80
+ readonly presence?: boolean | Presence
81
+ }
82
+
83
+ export interface SubprocessComputerUseHostForegroundOptions {
84
+ /**
85
+ * Asks the person, once, whether an application may use the screen for a
86
+ * moment while they are busy. Absent (or answering `unavailable`), a run
87
+ * with nobody to ask waits and retries within a bound instead.
88
+ */
89
+ readonly approve?: ForegroundApprover
90
+ /** Names this session in the screen lease. */
91
+ readonly session?: string
92
+ /** Overrides for the thresholds and bounds; see `DEFAULT_TIMING`. */
93
+ readonly timing?: Partial<LadderTiming>
94
+ /** A ready ladder, instead of the one built for this machine. For tests. */
95
+ readonly ladder?: ForegroundLadder
54
96
  }
55
97
 
56
98
  export type SubprocessComputerUseHostWindowsOptions = Pick<
@@ -75,6 +117,8 @@ export class SubprocessComputerUseHost implements ComputerUseHost {
75
117
  readonly id = 'subprocess-computer-use-host'
76
118
  private _capabilities: ComputerUseCapabilities
77
119
  private adapter: Adapter | null = null
120
+ private ladder: ForegroundLadder | undefined
121
+ private presence: Presence | undefined
78
122
 
79
123
  constructor(private readonly options: SubprocessComputerUseHostOptions = {}) {
80
124
  const displayServer = detectDisplayServer(options.env, options.platform)
@@ -82,6 +126,7 @@ export class SubprocessComputerUseHost implements ComputerUseHost {
82
126
  ...UNINITIALISED_CAPABILITIES,
83
127
  displayServer,
84
128
  })
129
+ this.ladder = options.foreground?.ladder
85
130
  if (options.adapter) {
86
131
  this.adapter = options.adapter
87
132
  this._capabilities = options.adapter.capabilities
@@ -109,7 +154,27 @@ export class SubprocessComputerUseHost implements ComputerUseHost {
109
154
  async initialize(): Promise<void> {
110
155
  if (this.adapter) return
111
156
  const displayServer = this._capabilities.displayServer
112
- const adapter = await loadAdapter(displayServer, this.options)
157
+ if (!this.ladder && displayServer === 'win32') {
158
+ this.ladder = createWindowsForegroundLadder({
159
+ ...(this.options.env !== undefined ? { env: this.options.env } : {}),
160
+ ...(this.options.platform !== undefined ? { platform: this.options.platform } : {}),
161
+ ...(this.options.foreground?.approve ? { approve: this.options.foreground.approve } : {}),
162
+ ...(this.options.foreground?.session ? { session: this.options.foreground.session } : {}),
163
+ ...(this.options.foreground?.timing ? { timing: this.options.foreground.timing } : {}),
164
+ })
165
+ }
166
+ if (!this.presence && displayServer === 'win32') {
167
+ const option = this.options.presence
168
+ this.presence =
169
+ typeof option === 'object'
170
+ ? option
171
+ : createWindowsPresence({
172
+ ...(this.options.env !== undefined ? { env: this.options.env } : {}),
173
+ ...(this.options.platform !== undefined ? { platform: this.options.platform } : {}),
174
+ ...(option === false ? { enabled: false } : {}),
175
+ })
176
+ }
177
+ const adapter = await loadAdapter(displayServer, this.options, this.ladder, this.presence)
113
178
  // Loading an adapter proves its tools exist on PATH, not that a desktop
114
179
  // answers. A WSL process finds PowerShell and still may have no
115
180
  // interactive Windows session to capture; an ssh session finds xdotool
@@ -138,62 +203,135 @@ export class SubprocessComputerUseHost implements ComputerUseHost {
138
203
  return adapter.getDisplayGeometry()
139
204
  }
140
205
 
141
- async execute(action: ComputerUseAction): Promise<ComputerUseResult> {
206
+ async execute(action: ComputerUseAction, signal?: AbortSignal): Promise<ComputerUseResult> {
142
207
  const adapter = this.requireAdapter()
143
- try {
144
- return await adapter.execute(action)
145
- } catch (error) {
146
- // SpawnError is produced only after the subprocess started and then
147
- // timed out or closed non-zero. An unsafe action may have taken effect
148
- // before that terminal status (a multi-command drag is the clearest
149
- // example), so replaying it could duplicate or compound the change.
150
- // A raw spawn error means the process never established that boundary;
151
- // safe reads and idempotent mouse_move keep their ordinary diagnosis.
152
- if (error instanceof SpawnError && UNSAFE_TO_REPLAY_AFTER_START.has(action.type)) {
153
- throw new ComputerUseOutcomeUnknownError(action.type, error)
154
- }
155
- throw error
156
- }
208
+ const read = action.type === 'screenshot' || action.type === 'cursor_position'
209
+ return this.scoped(
210
+ signal,
211
+ async () => {
212
+ if (await this.locked()) {
213
+ if (read) throw new ComputerUseLockedError()
214
+ return {
215
+ type: 'ok',
216
+ delivery: 'not_delivered',
217
+ retrySafety: 'safe',
218
+ detail: LOCKED_TEXT,
219
+ }
220
+ }
221
+ try {
222
+ const result = await adapter.execute(action)
223
+ if (action.type === 'screenshot') await this.notCapturedLocked()
224
+ return result
225
+ } catch (error) {
226
+ // SpawnError is produced only after the subprocess started and then
227
+ // timed out or closed non-zero. An unsafe action may have taken effect
228
+ // before that terminal status (a multi-command drag is the clearest
229
+ // example), so replaying it could duplicate or compound the change.
230
+ // A raw spawn error means the process never established that boundary;
231
+ // safe reads and idempotent mouse_move keep their ordinary diagnosis.
232
+ if (error instanceof SpawnError && UNSAFE_TO_REPLAY_AFTER_START.has(action.type)) {
233
+ throw new ComputerUseOutcomeUnknownError(action.type, error)
234
+ }
235
+ throw error
236
+ }
237
+ },
238
+ read
239
+ ? undefined
240
+ : (sent): ComputerUseResult => ({
241
+ type: 'ok',
242
+ ...abortedDelivery(sent),
243
+ }),
244
+ )
157
245
  }
158
246
 
159
247
  /** Offered to the model only when `capabilities.windows` is true. */
160
- async listWindows(): Promise<readonly WindowInfo[]> {
248
+ async listWindows(signal?: AbortSignal): Promise<readonly WindowInfo[]> {
161
249
  const adapter = this.requireAdapter()
162
250
  if (!adapter.listWindows) throw unsupported('listWindows', this._capabilities.displayServer)
163
- return adapter.listWindows()
251
+ return this.scoped(signal, async () => {
252
+ await this.refuseWhenLocked()
253
+ return (adapter.listWindows as NonNullable<Adapter['listWindows']>)()
254
+ })
164
255
  }
165
256
 
166
257
  /** Offered to the model only when `capabilities.windows` is true. */
167
- async focusWindow(id: string): Promise<FocusWindowResult> {
258
+ async focusWindow(id: string, signal?: AbortSignal): Promise<FocusWindowResult> {
168
259
  const adapter = this.requireAdapter()
169
260
  if (!adapter.focusWindow) throw unsupported('focusWindow', this._capabilities.displayServer)
170
- return adapter.focusWindow(id)
261
+ return this.scoped(
262
+ signal,
263
+ async () => {
264
+ if (await this.locked())
265
+ return {
266
+ ok: false,
267
+ focusedId: null,
268
+ delivery: 'not_delivered',
269
+ retrySafety: 'safe',
270
+ detail: LOCKED_TEXT,
271
+ }
272
+ return (adapter.focusWindow as NonNullable<Adapter['focusWindow']>)(id)
273
+ },
274
+ (sent): FocusWindowResult => ({
275
+ ok: false,
276
+ focusedId: null,
277
+ ...abortedDelivery(sent),
278
+ }),
279
+ )
171
280
  }
172
281
 
173
282
  /** Window-local PNG and input, only when the selected adapter supports both. */
174
- async captureWindow(id: string): Promise<WindowScreenshotResult> {
283
+ async captureWindow(id: string, signal?: AbortSignal): Promise<WindowScreenshotResult> {
175
284
  const adapter = this.requireAdapter()
176
285
  if (!adapter.captureWindow) throw unsupported('captureWindow', this._capabilities.displayServer)
177
- return adapter.captureWindow(id)
286
+ return this.scoped(signal, async () => {
287
+ await this.refuseWhenLocked()
288
+ const shot = await (adapter.captureWindow as NonNullable<Adapter['captureWindow']>)(id)
289
+ await this.notCapturedLocked()
290
+ return shot
291
+ })
178
292
  }
179
293
 
180
- async executeWindow(captureId: string, action: WindowInputAction): Promise<void> {
294
+ async executeWindow(
295
+ captureId: string,
296
+ action: WindowInputAction,
297
+ signal?: AbortSignal,
298
+ ): Promise<WindowInputResult | undefined> {
181
299
  const adapter = this.requireAdapter()
182
300
  if (!adapter.executeWindow) throw unsupported('executeWindow', this._capabilities.displayServer)
183
- try {
184
- await adapter.executeWindow(captureId, action)
185
- } catch (error) {
186
- if (error instanceof SpawnError && UNSAFE_TO_REPLAY_AFTER_START.has(action.type))
187
- throw new ComputerUseOutcomeUnknownError(action.type, error)
188
- throw error
189
- }
301
+ return this.scoped(
302
+ signal,
303
+ async () => {
304
+ if (await this.locked())
305
+ return {
306
+ delivery: 'not_delivered',
307
+ retrySafety: 'safe',
308
+ detail: LOCKED_TEXT,
309
+ }
310
+ try {
311
+ return await (adapter.executeWindow as NonNullable<Adapter['executeWindow']>)(
312
+ captureId,
313
+ action,
314
+ )
315
+ } catch (error) {
316
+ if (error instanceof SpawnError && UNSAFE_TO_REPLAY_AFTER_START.has(action.type))
317
+ throw new ComputerUseOutcomeUnknownError(action.type, error)
318
+ throw error
319
+ }
320
+ },
321
+ (sent): WindowInputResult => abortedDelivery(sent),
322
+ )
190
323
  }
191
324
 
192
325
  /** Offered to the model only when `capabilities.regionCapture` is true. */
193
- async captureRegion(rect: Rect): Promise<ScreenshotResult> {
326
+ async captureRegion(rect: Rect, signal?: AbortSignal): Promise<ScreenshotResult> {
194
327
  const adapter = this.requireAdapter()
195
328
  if (!adapter.captureRegion) throw unsupported('captureRegion', this._capabilities.displayServer)
196
- return adapter.captureRegion(rect)
329
+ return this.scoped(signal, async () => {
330
+ await this.refuseWhenLocked()
331
+ const shot = await (adapter.captureRegion as NonNullable<Adapter['captureRegion']>)(rect)
332
+ await this.notCapturedLocked()
333
+ return shot
334
+ })
197
335
  }
198
336
 
199
337
  /**
@@ -202,10 +340,17 @@ export class SubprocessComputerUseHost implements ComputerUseHost {
202
340
  *
203
341
  * @experimental Follows the SDK's UI-tree surface.
204
342
  */
205
- async uiSnapshot(windowId?: string): Promise<UiSnapshot> {
343
+ async uiSnapshot(
344
+ windowId?: string,
345
+ options?: UiSnapshotOptions,
346
+ signal?: AbortSignal,
347
+ ): Promise<UiSnapshot> {
206
348
  const adapter = this.requireAdapter()
207
349
  if (!adapter.uiSnapshot) throw unsupported('uiSnapshot', this._capabilities.displayServer)
208
- return adapter.uiSnapshot(windowId)
350
+ return this.scoped(signal, async () => {
351
+ await this.refuseWhenLocked()
352
+ return (adapter.uiSnapshot as NonNullable<Adapter['uiSnapshot']>)(windowId, options)
353
+ })
209
354
  }
210
355
 
211
356
  /**
@@ -215,19 +360,39 @@ export class SubprocessComputerUseHost implements ComputerUseHost {
215
360
  *
216
361
  * @experimental Follows the SDK's UI-tree surface.
217
362
  */
218
- async uiAct(ref: string, action: UiElementAction, value?: string): Promise<UiActResult> {
363
+ async uiAct(
364
+ ref: string,
365
+ action: UiElementAction,
366
+ value?: string,
367
+ signal?: AbortSignal,
368
+ ): Promise<UiActResult> {
219
369
  const adapter = this.requireAdapter()
220
370
  if (!adapter.uiAct) throw unsupported('uiAct', this._capabilities.displayServer)
221
- try {
222
- return await adapter.uiAct(ref, action, value)
223
- } catch (error) {
224
- if (error instanceof SpawnError)
225
- return {
226
- ok: false,
227
- detail: `the desktop driver stopped before it answered, so ${action} may or may not have happened; look at the screen (ui_snapshot or screenshot) before trying again. ${error.message}`,
371
+ return this.scoped(
372
+ signal,
373
+ async () => {
374
+ if (await this.locked())
375
+ return {
376
+ ok: false,
377
+ delivery: 'not_delivered',
378
+ retrySafety: 'safe',
379
+ detail: LOCKED_TEXT,
380
+ }
381
+ try {
382
+ return await (adapter.uiAct as NonNullable<Adapter['uiAct']>)(ref, action, value)
383
+ } catch (error) {
384
+ if (error instanceof SpawnError)
385
+ return {
386
+ ok: false,
387
+ delivery: 'delivered_unverified',
388
+ retrySafety: 'unsafe',
389
+ detail: `the desktop driver stopped before it answered, so ${action} may or may not have happened; look at the screen (ui_snapshot or screenshot) before trying again. ${error.message}`,
390
+ }
391
+ throw error
228
392
  }
229
- throw error
230
- }
393
+ },
394
+ (sent): UiActResult => ({ ok: false, ...abortedDelivery(sent) }),
395
+ )
231
396
  }
232
397
 
233
398
  /** Stops whatever the adapter keeps running (the Windows cua-driver process). */
@@ -235,9 +400,66 @@ export class SubprocessComputerUseHost implements ComputerUseHost {
235
400
  const adapter = this.adapter
236
401
  this.adapter = null
237
402
  await adapter?.dispose?.()
403
+ await this.ladder?.dispose()
404
+ await this.presence?.dispose()
405
+ }
406
+
407
+ /**
408
+ * The model's turn is making a tool call. A new turn lifts an Escape stop.
409
+ */
410
+ beginTurn(turnId: string): void {
411
+ this.ladder?.beginTurn(turnId)
412
+ }
413
+
414
+ /** The turn ended or was interrupted: the input monitor and any lease are released. */
415
+ async endTurn(): Promise<void> {
416
+ await this.ladder?.endTurn()
417
+ this.presence?.hide()
418
+ }
419
+
420
+ /**
421
+ * One tool call: its cancellation reaches every driver call and foreground
422
+ * burst made for it. When cancelled, a call that changes the desktop answers
423
+ * at once with what is known (`onAbort`, told whether anything may have been
424
+ * sent); a read has no answer to give and throws.
425
+ */
426
+ private async scoped<T>(
427
+ signal: AbortSignal | undefined,
428
+ run: () => Promise<T>,
429
+ onAbort?: (sent: boolean) => T,
430
+ ): Promise<T> {
431
+ return inCallScope(signal, async (scope) => {
432
+ try {
433
+ return await run()
434
+ } catch (error) {
435
+ if (!(error instanceof CallAbortedError)) throw error
436
+ if (!onAbort) throw error
437
+ return onAbort(scope.sent || error.sent)
438
+ }
439
+ })
440
+ }
441
+
442
+ /** True when the computer is known to be locked; false when it is not or cannot be told. */
443
+ private async locked(): Promise<boolean> {
444
+ if (!this.ladder) return false
445
+ const state = await this.ladder.lockState().catch((error: unknown) => {
446
+ if (error instanceof CallAbortedError) throw error
447
+ return null
448
+ })
449
+ return state === 'locked'
450
+ }
451
+
452
+ private async refuseWhenLocked(): Promise<void> {
453
+ if (await this.locked()) throw new ComputerUseLockedError()
454
+ }
455
+
456
+ /** A capture taken while the screen was locked shows no current state: it is dropped. */
457
+ private async notCapturedLocked(): Promise<void> {
458
+ if (await this.locked()) throw new ComputerUseLockedError()
238
459
  }
239
460
 
240
461
  private requireAdapter(): Adapter {
462
+ if (this.ladder?.stopped) throw new ComputerUseStoppedError()
241
463
  if (!this.adapter) {
242
464
  throw new Error(
243
465
  'SubprocessComputerUseHost: adapter not initialised — call `await host.initialize()` first',
@@ -247,6 +469,22 @@ export class SubprocessComputerUseHost implements ComputerUseHost {
247
469
  }
248
470
  }
249
471
 
472
+ /** What a cancelled call that changes the desktop tells the model. */
473
+ function abortedDelivery(sent: boolean): WindowInputResult {
474
+ return sent
475
+ ? {
476
+ delivery: 'delivered_unverified',
477
+ retrySafety: 'unsafe',
478
+ detail:
479
+ 'The turn was cancelled while this was being sent, so it may have gone through. Look at the window before repeating it.',
480
+ }
481
+ : {
482
+ delivery: 'not_delivered',
483
+ retrySafety: 'safe',
484
+ detail: 'The turn was cancelled before this was sent, so nothing happened.',
485
+ }
486
+ }
487
+
250
488
  function unsupported(method: string, displayServer: string): Error {
251
489
  return new Error(
252
490
  `SubprocessComputerUseHost: ${method} is not supported by the ${displayServer} adapter in use.`,
@@ -256,6 +494,8 @@ function unsupported(method: string, displayServer: string): Error {
256
494
  async function loadAdapter(
257
495
  displayServer: ComputerUseCapabilities['displayServer'],
258
496
  options: SubprocessComputerUseHostOptions,
497
+ ladder: ForegroundLadder | undefined,
498
+ presence: Presence | undefined,
259
499
  ): Promise<Adapter> {
260
500
  switch (displayServer) {
261
501
  case 'darwin': {
@@ -276,6 +516,8 @@ async function loadAdapter(
276
516
  ...options.windows,
277
517
  ...(options.env !== undefined ? { env: options.env } : {}),
278
518
  ...(options.platform !== undefined ? { platform: options.platform } : {}),
519
+ ...(ladder ? { foreground: ladder } : {}),
520
+ ...(presence ? { presence } : {}),
279
521
  })
280
522
  }
281
523
  case 'unknown':