@namzu/computer-use 3.0.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 (122) hide show
  1. package/CHANGELOG.md +64 -1
  2. package/README.md +37 -6
  3. package/dist/SubprocessComputerUseHost.d.ts +58 -7
  4. package/dist/SubprocessComputerUseHost.d.ts.map +1 -1
  5. package/dist/SubprocessComputerUseHost.js +222 -35
  6. package/dist/SubprocessComputerUseHost.js.map +1 -1
  7. package/dist/adapters/cua-driver/adapter.d.ts +182 -13
  8. package/dist/adapters/cua-driver/adapter.d.ts.map +1 -1
  9. package/dist/adapters/cua-driver/adapter.js +1657 -121
  10. package/dist/adapters/cua-driver/adapter.js.map +1 -1
  11. package/dist/adapters/cua-driver/client.d.ts +16 -0
  12. package/dist/adapters/cua-driver/client.d.ts.map +1 -1
  13. package/dist/adapters/cua-driver/client.js +54 -5
  14. package/dist/adapters/cua-driver/client.js.map +1 -1
  15. package/dist/adapters/cua-driver/escalation.d.ts +67 -0
  16. package/dist/adapters/cua-driver/escalation.d.ts.map +1 -0
  17. package/dist/adapters/cua-driver/escalation.js +82 -0
  18. package/dist/adapters/cua-driver/escalation.js.map +1 -0
  19. package/dist/adapters/cua-driver/keys.d.ts +5 -0
  20. package/dist/adapters/cua-driver/keys.d.ts.map +1 -1
  21. package/dist/adapters/cua-driver/keys.js +23 -0
  22. package/dist/adapters/cua-driver/keys.js.map +1 -1
  23. package/dist/adapters/cua-driver/observe.d.ts +99 -0
  24. package/dist/adapters/cua-driver/observe.d.ts.map +1 -0
  25. package/dist/adapters/cua-driver/observe.js +227 -0
  26. package/dist/adapters/cua-driver/observe.js.map +1 -0
  27. package/dist/adapters/cua-driver/ui-tree.d.ts +75 -2
  28. package/dist/adapters/cua-driver/ui-tree.d.ts.map +1 -1
  29. package/dist/adapters/cua-driver/ui-tree.js +142 -14
  30. package/dist/adapters/cua-driver/ui-tree.js.map +1 -1
  31. package/dist/adapters/types.d.ts +6 -2
  32. package/dist/adapters/types.d.ts.map +1 -1
  33. package/dist/adapters/types.js.map +1 -1
  34. package/dist/adapters/win32.d.ts +10 -0
  35. package/dist/adapters/win32.d.ts.map +1 -1
  36. package/dist/adapters/win32.js +12 -2
  37. package/dist/adapters/win32.js.map +1 -1
  38. package/dist/errors.d.ts +19 -0
  39. package/dist/errors.d.ts.map +1 -1
  40. package/dist/errors.js +26 -0
  41. package/dist/errors.js.map +1 -1
  42. package/dist/foreground/guard.d.ts +31 -0
  43. package/dist/foreground/guard.d.ts.map +1 -0
  44. package/dist/foreground/guard.js +156 -0
  45. package/dist/foreground/guard.js.map +1 -0
  46. package/dist/foreground/index.d.ts +11 -0
  47. package/dist/foreground/index.d.ts.map +1 -0
  48. package/dist/foreground/index.js +7 -0
  49. package/dist/foreground/index.js.map +1 -0
  50. package/dist/foreground/ladder.d.ts +143 -0
  51. package/dist/foreground/ladder.d.ts.map +1 -0
  52. package/dist/foreground/ladder.js +505 -0
  53. package/dist/foreground/ladder.js.map +1 -0
  54. package/dist/foreground/lease.d.ts +69 -0
  55. package/dist/foreground/lease.d.ts.map +1 -0
  56. package/dist/foreground/lease.js +182 -0
  57. package/dist/foreground/lease.js.map +1 -0
  58. package/dist/foreground/types.d.ts +287 -0
  59. package/dist/foreground/types.d.ts.map +1 -0
  60. package/dist/foreground/types.js +32 -0
  61. package/dist/foreground/types.js.map +1 -0
  62. package/dist/foreground/windows-helper-script.d.ts +19 -0
  63. package/dist/foreground/windows-helper-script.d.ts.map +1 -0
  64. package/dist/foreground/windows-helper-script.js +472 -0
  65. package/dist/foreground/windows-helper-script.js.map +1 -0
  66. package/dist/foreground/windows-monitor.d.ts +75 -0
  67. package/dist/foreground/windows-monitor.d.ts.map +1 -0
  68. package/dist/foreground/windows-monitor.js +216 -0
  69. package/dist/foreground/windows-monitor.js.map +1 -0
  70. package/dist/foreground/windows.d.ts +21 -0
  71. package/dist/foreground/windows.d.ts.map +1 -0
  72. package/dist/foreground/windows.js +54 -0
  73. package/dist/foreground/windows.js.map +1 -0
  74. package/dist/index.d.ts +6 -1
  75. package/dist/index.d.ts.map +1 -1
  76. package/dist/index.js +3 -1
  77. package/dist/index.js.map +1 -1
  78. package/dist/presence/helper-script.d.ts +39 -0
  79. package/dist/presence/helper-script.d.ts.map +1 -0
  80. package/dist/presence/helper-script.js +320 -0
  81. package/dist/presence/helper-script.js.map +1 -0
  82. package/dist/presence/index.d.ts +6 -0
  83. package/dist/presence/index.d.ts.map +1 -0
  84. package/dist/presence/index.js +4 -0
  85. package/dist/presence/index.js.map +1 -0
  86. package/dist/presence/presence.d.ts +70 -0
  87. package/dist/presence/presence.d.ts.map +1 -0
  88. package/dist/presence/presence.js +147 -0
  89. package/dist/presence/presence.js.map +1 -0
  90. package/dist/presence/windows.d.ts +19 -0
  91. package/dist/presence/windows.d.ts.map +1 -0
  92. package/dist/presence/windows.js +39 -0
  93. package/dist/presence/windows.js.map +1 -0
  94. package/dist/util/call-scope.d.ts +34 -0
  95. package/dist/util/call-scope.d.ts.map +1 -0
  96. package/dist/util/call-scope.js +58 -0
  97. package/dist/util/call-scope.js.map +1 -0
  98. package/package.json +2 -2
  99. package/src/SubprocessComputerUseHost.ts +298 -35
  100. package/src/adapters/cua-driver/adapter.ts +1986 -137
  101. package/src/adapters/cua-driver/client.ts +59 -2
  102. package/src/adapters/cua-driver/escalation.ts +137 -0
  103. package/src/adapters/cua-driver/keys.ts +20 -0
  104. package/src/adapters/cua-driver/observe.ts +310 -0
  105. package/src/adapters/cua-driver/ui-tree.ts +219 -15
  106. package/src/adapters/types.ts +12 -1
  107. package/src/adapters/win32.ts +26 -3
  108. package/src/errors.ts +32 -0
  109. package/src/foreground/guard.ts +182 -0
  110. package/src/foreground/index.ts +45 -0
  111. package/src/foreground/ladder.ts +616 -0
  112. package/src/foreground/lease.ts +232 -0
  113. package/src/foreground/types.ts +318 -0
  114. package/src/foreground/windows-helper-script.ts +471 -0
  115. package/src/foreground/windows-monitor.ts +288 -0
  116. package/src/foreground/windows.ts +73 -0
  117. package/src/index.ts +15 -1
  118. package/src/presence/helper-script.ts +323 -0
  119. package/src/presence/index.ts +17 -0
  120. package/src/presence/presence.ts +195 -0
  121. package/src/presence/windows.ts +54 -0
  122. package/src/util/call-scope.ts +73 -0
@@ -0,0 +1,232 @@
1
+ import { randomUUID } from 'node:crypto'
2
+ import {
3
+ closeSync,
4
+ mkdirSync,
5
+ openSync,
6
+ readFileSync,
7
+ renameSync,
8
+ unlinkSync,
9
+ writeSync,
10
+ } from 'node:fs'
11
+ import { dirname } from 'node:path'
12
+
13
+ /**
14
+ * The cross-session screen lease: one file that says which session may use
15
+ * the user's screen right now, so two sessions never interleave foreground
16
+ * bursts.
17
+ *
18
+ * A lease is stale, and may be taken over, when its owner process is gone,
19
+ * when a process with that id exists but started at another time (the id was
20
+ * reused), or when nobody refreshed it within the time-to-live. The last rule
21
+ * covers owners on a machine this process cannot inspect.
22
+ */
23
+
24
+ export interface LeaseRecord {
25
+ readonly pid: number
26
+ /** The owner's process start time as the OS reports it; absent where unknown. */
27
+ readonly identity?: string
28
+ readonly session: string
29
+ readonly token: string
30
+ readonly acquiredAt: number
31
+ readonly heartbeatAt: number
32
+ }
33
+
34
+ /** The file operations the lease needs; replaced in tests. */
35
+ export interface LeaseFs {
36
+ /** Creates the file with `text`; false when it already exists. */
37
+ createExclusive(path: string, text: string): boolean
38
+ read(path: string): string | undefined
39
+ remove(path: string): void
40
+ rename(from: string, to: string): void
41
+ /** Replaces the text of an existing file. */
42
+ overwrite(path: string, text: string): void
43
+ }
44
+
45
+ export const realLeaseFs: LeaseFs = {
46
+ createExclusive(path, text) {
47
+ mkdirSync(dirname(path), { recursive: true })
48
+ let fd: number
49
+ try {
50
+ fd = openSync(path, 'wx')
51
+ } catch (error) {
52
+ if ((error as NodeJS.ErrnoException).code === 'EEXIST') return false
53
+ throw error
54
+ }
55
+ try {
56
+ writeSync(fd, text)
57
+ } finally {
58
+ closeSync(fd)
59
+ }
60
+ return true
61
+ },
62
+ read(path) {
63
+ try {
64
+ return readFileSync(path, 'utf8')
65
+ } catch {
66
+ return undefined
67
+ }
68
+ },
69
+ remove(path) {
70
+ try {
71
+ unlinkSync(path)
72
+ } catch {
73
+ // Already gone.
74
+ }
75
+ },
76
+ rename(from, to) {
77
+ renameSync(from, to)
78
+ },
79
+ overwrite(path, text) {
80
+ // Write beside it and rename over it: a contender reading meanwhile sees
81
+ // the old whole record or the new one, never an empty file.
82
+ const temp = `${path}.tmp-${process.pid}`
83
+ const fd = openSync(temp, 'w')
84
+ try {
85
+ writeSync(fd, text)
86
+ } finally {
87
+ closeSync(fd)
88
+ }
89
+ renameSync(temp, path)
90
+ },
91
+ }
92
+
93
+ /** `/proc/<pid>/stat` field 22 (start time in ticks) on Linux; undefined elsewhere. */
94
+ export function processIdentity(pid: number): string | undefined {
95
+ try {
96
+ const stat = readFileSync(`/proc/${pid}/stat`, 'utf8')
97
+ const tail = stat.slice(stat.lastIndexOf(')') + 2).split(' ')
98
+ return tail[19]
99
+ } catch {
100
+ return undefined
101
+ }
102
+ }
103
+
104
+ export function processAlive(pid: number): boolean {
105
+ try {
106
+ process.kill(pid, 0)
107
+ return true
108
+ } catch (error) {
109
+ return (error as NodeJS.ErrnoException).code === 'EPERM'
110
+ }
111
+ }
112
+
113
+ export interface ScreenLeaseOptions {
114
+ readonly path: string
115
+ readonly session: string
116
+ readonly now: () => number
117
+ /** Milliseconds after the last refresh at which a lease is stale. Default 30 000. */
118
+ readonly ttlMs?: number
119
+ readonly pid?: number
120
+ readonly fs?: LeaseFs
121
+ readonly identity?: (pid: number) => string | undefined
122
+ readonly alive?: (pid: number) => boolean
123
+ }
124
+
125
+ export type LeaseAttempt =
126
+ | { readonly held: true; readonly token: string }
127
+ | { readonly held: false; readonly owner: LeaseRecord | undefined }
128
+
129
+ export class ScreenLease {
130
+ readonly path: string
131
+ private readonly fs: LeaseFs
132
+ private readonly ttlMs: number
133
+ private readonly pid: number
134
+ private readonly identity: (pid: number) => string | undefined
135
+ private readonly alive: (pid: number) => boolean
136
+
137
+ constructor(private readonly options: ScreenLeaseOptions) {
138
+ this.path = options.path
139
+ this.fs = options.fs ?? realLeaseFs
140
+ this.ttlMs = options.ttlMs ?? 30_000
141
+ this.pid = options.pid ?? process.pid
142
+ this.identity = options.identity ?? processIdentity
143
+ this.alive = options.alive ?? processAlive
144
+ }
145
+
146
+ /** One attempt. A stale lease is taken over; a live one is reported. */
147
+ tryAcquire(): LeaseAttempt {
148
+ const token = randomUUID()
149
+ const now = this.options.now()
150
+ const record: LeaseRecord = {
151
+ pid: this.pid,
152
+ ...(this.identity(this.pid) !== undefined ? { identity: this.identity(this.pid) } : {}),
153
+ session: this.options.session,
154
+ token,
155
+ acquiredAt: now,
156
+ heartbeatAt: now,
157
+ }
158
+ for (let attempt = 0; attempt < 3; attempt++) {
159
+ if (this.fs.createExclusive(this.path, JSON.stringify(record))) return { held: true, token }
160
+ const owner = this.read()
161
+ if (owner && !this.stale(owner)) return { held: false, owner }
162
+ // Stale, or unreadable (half-written by a process that died): move it
163
+ // aside so exactly one contender removes it, then look again.
164
+ const aside = `${this.path}.stale-${token}`
165
+ try {
166
+ this.fs.rename(this.path, aside)
167
+ } catch {
168
+ continue
169
+ }
170
+ const taken = this.fs.read(aside)
171
+ this.fs.remove(aside)
172
+ if (taken !== undefined && owner === undefined) continue
173
+ const movedOwner = taken === undefined ? undefined : parse(taken)
174
+ if (movedOwner && owner && movedOwner.token !== owner.token) {
175
+ // What was moved aside was a fresh lease someone took after our read.
176
+ this.fs.createExclusive(this.path, taken as string)
177
+ return { held: false, owner: movedOwner }
178
+ }
179
+ }
180
+ return { held: false, owner: this.read() }
181
+ }
182
+
183
+ /** Marks the lease as alive. Returns false when it is no longer ours. */
184
+ heartbeat(token: string): boolean {
185
+ const owner = this.read()
186
+ if (!owner || owner.token !== token) return false
187
+ this.fs.overwrite(
188
+ this.path,
189
+ JSON.stringify({
190
+ ...owner,
191
+ heartbeatAt: this.options.now(),
192
+ } satisfies LeaseRecord),
193
+ )
194
+ return true
195
+ }
196
+
197
+ release(token: string): void {
198
+ const owner = this.read()
199
+ if (owner?.token === token) this.fs.remove(this.path)
200
+ }
201
+
202
+ read(): LeaseRecord | undefined {
203
+ const text = this.fs.read(this.path)
204
+ return text === undefined ? undefined : parse(text)
205
+ }
206
+
207
+ private stale(owner: LeaseRecord): boolean {
208
+ if (this.options.now() - owner.heartbeatAt > this.ttlMs) return true
209
+ if (owner.pid === this.pid) return false
210
+ if (!this.alive(owner.pid)) return true
211
+ if (owner.identity !== undefined) {
212
+ const now = this.identity(owner.pid)
213
+ if (now !== undefined && now !== owner.identity) return true
214
+ }
215
+ return false
216
+ }
217
+ }
218
+
219
+ function parse(text: string): LeaseRecord | undefined {
220
+ try {
221
+ const value = JSON.parse(text) as Partial<LeaseRecord>
222
+ if (
223
+ typeof value.pid === 'number' &&
224
+ typeof value.token === 'string' &&
225
+ typeof value.heartbeatAt === 'number'
226
+ )
227
+ return value as LeaseRecord
228
+ } catch {
229
+ // Falls through.
230
+ }
231
+ return undefined
232
+ }
@@ -0,0 +1,318 @@
1
+ /**
2
+ * The foreground ladder: what lets a computer-use action use the user's screen
3
+ * when it cannot be delivered in the background, without taking it from a
4
+ * person who is using it.
5
+ *
6
+ * The pieces are separate on purpose so each can be tested on its own:
7
+ * - {@link ForegroundMonitor}: a long-lived observer of the desktop (user
8
+ * input, the window in front, the cursor, the input desktop). On Windows it
9
+ * is a helper process; here it is an interface.
10
+ * - `ScreenLease`: one file under the Namzu home that says which session owns
11
+ * the screen.
12
+ * - `ForegroundLadder`: the policy that uses both.
13
+ */
14
+
15
+ /** The application a burst is for: what the user is asked about, and what a refusal names. */
16
+ export interface ForegroundSubject {
17
+ /** Display name, as `list_windows` shows it. */
18
+ readonly app: string
19
+ /** Executable file name, when known (`editor.exe`). */
20
+ readonly exe?: string
21
+ /** Process id of the target window, when the burst has one. */
22
+ readonly pid?: number
23
+ /** Window handle of the target, in hex. */
24
+ readonly hwnd?: string
25
+ }
26
+
27
+ /** What a monitor sees at one moment. */
28
+ export interface MonitorState {
29
+ /**
30
+ * Milliseconds since the last input a person made. Input the machine
31
+ * injected (this program's own bursts included) does not count once the
32
+ * monitor has seen it go by; before that it counts, which only makes the
33
+ * gate wait longer.
34
+ */
35
+ readonly userIdleMs: number
36
+ /** Count of real keyboard and mouse events since the monitor started. Only goes up. */
37
+ readonly userInputs: number
38
+ /** Count of real Escape key presses since the monitor started. */
39
+ readonly escapes: number
40
+ readonly foreground: {
41
+ readonly hwnd: string
42
+ readonly pid: number
43
+ readonly exe: string
44
+ /** Windows keeps this program's input from it: it runs above this program's rights, or cannot be inspected. */
45
+ readonly elevated: boolean
46
+ /** Its mandatory integrity level as a RID (0x1000 low, 0x2000 medium, 0x3000 high, 0x4000 system); -1 when unreadable. */
47
+ readonly integrity?: number
48
+ } | null
49
+ readonly cursor: { readonly x: number; readonly y: number }
50
+ /** The input desktop's name. `Default` is the normal one; anything else is a lock screen or a prompt. */
51
+ readonly desktop: string
52
+ /** This process runs with administrator rights. */
53
+ readonly selfElevated: boolean
54
+ /** This process's integrity level (RID). */
55
+ readonly selfIntegrity?: number
56
+ /** The system's last-input tick (`GetLastInputInfo`); it moves when any input, injected or real, is registered. */
57
+ readonly inputTick?: number
58
+ /** The target process, when the state was asked for one. */
59
+ readonly target?: {
60
+ readonly exe: string
61
+ readonly elevated: boolean
62
+ readonly integrity?: number
63
+ }
64
+ }
65
+
66
+ export interface MonitorBurstEnd {
67
+ /** Real input events that arrived between `begin` and `end`. */
68
+ readonly inputsDuring: number
69
+ /** Real Escape presses between `begin` and `end`. */
70
+ readonly escapesDuring: number
71
+ readonly restoredForeground: boolean
72
+ readonly restoredCursor: boolean
73
+ /** Why the window in front before the burst was not put back, when it was not. */
74
+ readonly restoreNote?: string
75
+ /**
76
+ * Whether the system registered any input between the end of `begin` and
77
+ * `end`. False after a burst that sent input means Windows dropped it (a
78
+ * window above this program's rights was in front). Absent where the
79
+ * monitor cannot tell.
80
+ */
81
+ readonly inputMoved?: boolean
82
+ /** The window in front at `end` was positively read to be above this program's rights (unreadable is not a yes). */
83
+ readonly frontElevated?: boolean
84
+ }
85
+
86
+ /** One operation on a classic edit control, by window message (never by focus or keys). */
87
+ export interface EditTextRequest {
88
+ /** The window that holds the control, in hex. */
89
+ readonly hwnd: string
90
+ /** `get` reads it, `set` replaces its text, `type` inserts at the caret. */
91
+ readonly op: 'get' | 'set' | 'type'
92
+ readonly text?: string
93
+ /** Which control: the one with focus in that window, or the one at a screen point. A lone edit control is taken either way. */
94
+ readonly pick: 'focus' | 'bounds'
95
+ readonly at?: { readonly x: number; readonly y: number }
96
+ }
97
+
98
+ /**
99
+ * What the control reads afterwards. `found` without `done` means the message
100
+ * went out (or was refused: see `reason`) and the control does not read as
101
+ * intended.
102
+ */
103
+ export interface EditTextResult {
104
+ readonly found: boolean
105
+ /**
106
+ * Why nothing was done or read: `gone`, `uipi` (the program has higher
107
+ * rights), `no_edit`, `ambiguous`, `read_only`, `hung`, `refused`,
108
+ * `unreadable`. Empty on success.
109
+ */
110
+ readonly reason: string
111
+ readonly hwnd?: string
112
+ readonly cls?: string
113
+ /** The control reads as asked (`get` always). */
114
+ readonly done: boolean
115
+ /** Characters in the control now. */
116
+ readonly len: number
117
+ /** For `get`: the text (the first 65 536 characters). */
118
+ readonly text: string
119
+ /** For `set` and `type`: the control's text around the change. */
120
+ readonly seen: string
121
+ }
122
+
123
+ /** Select one item of a list or tree through its selection pattern, which selects without opening it. */
124
+ export interface SelectItemRequest {
125
+ /** The window that holds the item, in hex. */
126
+ readonly hwnd: string
127
+ /** The item's role (`ListItem`, `TreeItem`, `DataItem`), name and, when it has one, automation id. */
128
+ readonly role: string
129
+ readonly name: string
130
+ readonly automationId?: string
131
+ /** Where the item was read, in screen pixels: tells apart same-named items. */
132
+ readonly bounds?: {
133
+ readonly x: number
134
+ readonly y: number
135
+ readonly width: number
136
+ readonly height: number
137
+ }
138
+ }
139
+
140
+ /**
141
+ * `found` is false with a `reason` (`gone`, `not_found`, `ambiguous`,
142
+ * `no_pattern`, `unreadable`, `refused`) when nothing was selected; `selected`
143
+ * is what the item reads as afterwards.
144
+ */
145
+ export interface SelectItemResult {
146
+ readonly found: boolean
147
+ readonly reason: string
148
+ readonly selected: boolean
149
+ /** `shell`: a folder window's own view (no focus, the window stays where it is); `pattern`: the accessibility selection pattern. */
150
+ readonly how?: 'shell' | 'pattern'
151
+ }
152
+
153
+ /**
154
+ * A keyboard shortcut whose effect is a command of the window's classic menu
155
+ * (the item names the combination after a tab, such as "Open... Ctrl+O").
156
+ */
157
+ export interface MenuCommandRequest {
158
+ /** The window, in hex. */
159
+ readonly hwnd: string
160
+ /** The combination as a menu names it: `Ctrl+O`, `Ctrl+Shift+S`. */
161
+ readonly accelerator: string
162
+ }
163
+
164
+ /**
165
+ * `found` is false with a `reason` (`gone`, `none`: no classic menu or no item
166
+ * with that accelerator, `disabled`) when nothing was sent.
167
+ */
168
+ export interface MenuCommandResult {
169
+ readonly found: boolean
170
+ readonly reason: string
171
+ /** The command's id and the item's text, when it was sent. */
172
+ readonly id?: number
173
+ readonly label?: string
174
+ }
175
+
176
+ /**
177
+ * Watches the desktop for a burst. Implementations must be cheap to poll (the
178
+ * Windows helper answers in under a millisecond) and must keep working when
179
+ * the program that started them dies: `begin` hands over what to put back.
180
+ */
181
+ export interface ForegroundMonitor {
182
+ /** Starts watching. Rejects when real input cannot be observed: the burst is then refused. */
183
+ start(): Promise<void>
184
+ state(query?: { readonly pid?: number }): Promise<MonitorState>
185
+ /**
186
+ * A burst is about to start. The monitor remembers the window in front, the
187
+ * cursor and the input count, and, should the program die before `end`,
188
+ * restores them and deletes the lease file (when `token` still matches).
189
+ */
190
+ begin(args: {
191
+ readonly leasePath: string
192
+ readonly token: string
193
+ /**
194
+ * Window handle (hex) to bring to the front first. A driver that is not
195
+ * itself allowed to take focus (Windows' foreground lock) then finds its
196
+ * target already in front. `raised` on the answer says whether it worked.
197
+ */
198
+ readonly raise?: string
199
+ /**
200
+ * The user must have been still this long (ms). Checked before anything
201
+ * is raised; when they were not, nothing is moved and `refused` is true.
202
+ */
203
+ readonly idleMs?: number
204
+ }): Promise<MonitorState & { readonly raised?: boolean; readonly refused?: boolean }>
205
+ /**
206
+ * The burst is over. With `restore`, the window that was in front is put
207
+ * back only if this burst raised a window and that window (or one of its
208
+ * program) is still in front; a burst that raised nothing restores no
209
+ * window, so a click that opened another window is not undone. The cursor
210
+ * is put back only when no person touched input meanwhile.
211
+ */
212
+ end(args: {
213
+ readonly restore: boolean
214
+ readonly keepCursor?: boolean
215
+ }): Promise<MonitorBurstEnd>
216
+ /** Deletes the lease file when its token still matches. */
217
+ release(args: {
218
+ readonly leasePath: string
219
+ readonly token: string
220
+ }): Promise<void>
221
+ /**
222
+ * Reads or changes a classic edit control by window message. Optional: a
223
+ * monitor that cannot reach controls leaves it out.
224
+ */
225
+ editText?(request: EditTextRequest): Promise<EditTextResult>
226
+ /** Selects a list or tree item by its selection pattern. Optional like `editText`. */
227
+ selectItem?(request: SelectItemRequest): Promise<SelectItemResult>
228
+ /** Sends the menu command a shortcut stands for, as a window message. Optional like `editText`. */
229
+ menuCommand?(request: MenuCommandRequest): Promise<MenuCommandResult>
230
+ dispose(): Promise<void>
231
+ }
232
+
233
+ /**
234
+ * Why a burst did not run. Nothing was sent to the target in any of these, so
235
+ * a retry is safe once the cause is gone.
236
+ */
237
+ export type ForegroundBlockCode =
238
+ | 'user_active'
239
+ | 'lease_busy'
240
+ | 'declined'
241
+ | 'stopped'
242
+ | 'monitor_unavailable'
243
+ | 'locked_desktop'
244
+ | 'secure_desktop'
245
+ | 'uac_prompt'
246
+ | 'elevated_target'
247
+
248
+ export interface ForegroundBlock {
249
+ readonly code: ForegroundBlockCode
250
+ /** In words the model can act on: what is blocking, and what to do next. */
251
+ readonly detail: string
252
+ }
253
+
254
+ export type ForegroundOutcome<T> =
255
+ | {
256
+ readonly kind: 'ran'
257
+ readonly value: T
258
+ /** A person used keyboard or mouse during the burst: the result is not proof either way. */
259
+ readonly interfered: boolean
260
+ /** Escape was pressed during the burst: the turn is stopped. */
261
+ readonly stopped: boolean
262
+ readonly restoredForeground: boolean
263
+ readonly restoredCursor: boolean
264
+ readonly restoreNote?: string
265
+ /**
266
+ * For a burst declared as sending input: whether the system registered
267
+ * it. False means it was dropped before it reached any window; `why`
268
+ * then says in plain words. Absent when the burst did not send input
269
+ * or the monitor cannot tell.
270
+ */
271
+ readonly inputLanded?: boolean
272
+ readonly inputDroppedWhy?: string
273
+ /** How long the screen was in use, from lease to release. */
274
+ readonly heldMs: number
275
+ }
276
+ | { readonly kind: 'blocked'; readonly block: ForegroundBlock }
277
+
278
+ /** What the user answers when the screen is needed while they are using it. */
279
+ export type ForegroundAnswer = 'allow' | 'deny' | 'unavailable'
280
+
281
+ /**
282
+ * Asks the person, once, whether an application may use the screen for a
283
+ * moment. `unavailable` means nobody can answer (a run with no one watching):
284
+ * the ladder then waits and retries within its bound instead.
285
+ */
286
+ export type ForegroundApprover = (
287
+ subject: ForegroundSubject,
288
+ signal?: AbortSignal,
289
+ ) => Promise<ForegroundAnswer>
290
+
291
+ /** What a burst function receives. */
292
+ export interface BurstContext {
293
+ /** Throws {@link BurstInterrupted} when a person has touched input since the burst began. */
294
+ checkpoint(): Promise<void>
295
+ }
296
+
297
+ /** Thrown by {@link BurstContext.checkpoint}: the next step was not sent. */
298
+ export class BurstInterrupted extends Error {
299
+ constructor(readonly reason: 'person' | 'administrator_window' = 'person') {
300
+ super(
301
+ reason === 'person'
302
+ ? 'a person used the keyboard or mouse during the burst'
303
+ : 'an administrator window came to the front during the burst',
304
+ )
305
+ this.name = 'BurstInterrupted'
306
+ }
307
+ }
308
+
309
+ /** The processes the ladder will not drive in the foreground. */
310
+ export const PROTECTED_PROCESSES: ReadonlyMap<string, 'locked_desktop' | 'uac_prompt'> = new Map<
311
+ string,
312
+ 'locked_desktop' | 'uac_prompt'
313
+ >([
314
+ ['lockapp.exe', 'locked_desktop'],
315
+ ['logonui.exe', 'locked_desktop'],
316
+ ['winlogon.exe', 'locked_desktop'],
317
+ ['consent.exe', 'uac_prompt'],
318
+ ])