@reticlehq/computer 0.0.0-stage → 3.11.1
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/LICENSE +202 -0
- package/NOTICE +12 -0
- package/README.md +237 -2
- package/bin/attach.mjs +333 -0
- package/dist/attach.d.ts +105 -0
- package/dist/attach.js +234 -0
- package/dist/channels.d.ts +42 -0
- package/dist/channels.js +46 -0
- package/dist/computer-realm.d.ts +54 -0
- package/dist/computer-realm.js +487 -0
- package/dist/doctor.d.ts +32 -0
- package/dist/doctor.js +42 -0
- package/dist/driver.d.ts +227 -0
- package/dist/driver.js +73 -0
- package/dist/effect.d.ts +60 -0
- package/dist/effect.js +158 -0
- package/dist/files.d.ts +16 -0
- package/dist/files.js +40 -0
- package/dist/hud/port.d.ts +44 -0
- package/dist/hud/port.js +6 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +22 -0
- package/dist/lines.d.ts +34 -0
- package/dist/lines.js +80 -0
- package/dist/mac/from-ax.d.ts +27 -0
- package/dist/mac/from-ax.js +275 -0
- package/dist/mac/mac-driver.d.ts +42 -0
- package/dist/mac/mac-driver.js +355 -0
- package/dist/mac/mac-hud.d.ts +10 -0
- package/dist/mac/mac-hud.js +142 -0
- package/dist/peer/act-effect.d.ts +9 -0
- package/dist/peer/act-effect.js +13 -0
- package/dist/peer/commands.d.ts +87 -0
- package/dist/peer/commands.js +811 -0
- package/dist/peer/form.d.ts +62 -0
- package/dist/peer/form.js +103 -0
- package/dist/peer/link-switch.d.ts +12 -0
- package/dist/peer/link-switch.js +38 -0
- package/dist/peer/peer.d.ts +43 -0
- package/dist/peer/peer.js +216 -0
- package/dist/peer/pointer.d.ts +97 -0
- package/dist/peer/pointer.js +289 -0
- package/dist/peer/scroll.d.ts +12 -0
- package/dist/peer/scroll.js +157 -0
- package/dist/peer/seen.d.ts +18 -0
- package/dist/peer/seen.js +61 -0
- package/dist/peer/select.d.ts +27 -0
- package/dist/peer/select.js +78 -0
- package/dist/peer/window.d.ts +25 -0
- package/dist/peer/window.js +111 -0
- package/dist/ui-node.d.ts +93 -0
- package/dist/ui-node.js +192 -0
- package/dist/win/helper-launch.d.ts +16 -0
- package/dist/win/helper-launch.js +57 -0
- package/native/linux/Dockerfile +22 -0
- package/native/linux/reticle-atspi.py +933 -0
- package/native/linux/sandbox.mjs +127 -0
- package/native/mac/build/reticle-ax +0 -0
- package/native/mac/build/reticle-hud +0 -0
- package/native/mac/hud.swift +749 -0
- package/native/mac/reticle-ax.swift +1786 -0
- package/native/win/reticle-uia.ps1 +620 -0
- package/package.json +65 -3
|
@@ -0,0 +1,811 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Answers the bridge's commands for an application, so the tools an agent already has for a web
|
|
3
|
+
* page (look, act, act and wait, assert) work on a desktop application.
|
|
4
|
+
*
|
|
5
|
+
* It is strict where the browser is generous: a name matches only when it is equal, and a state
|
|
6
|
+
* this realm cannot read is an error, not a guess. A check that cannot be decided must come back
|
|
7
|
+
* as "not known". A wrong `yes` costs more than a missing one.
|
|
8
|
+
*/
|
|
9
|
+
import { basename } from 'node:path';
|
|
10
|
+
import { ChangeKind as FileKind, diffSnapshots } from '@reticlehq/terminal';
|
|
11
|
+
import { ActionType, DRAG_TO_REF_ARG, ElementState, EventType, HumanControlKind, QueryBy, ReticleCommand, } from '@reticlehq/core';
|
|
12
|
+
import { ActKind, ActRefusal, Delivery, PIXELS_CHANGED_SIGNAL, Reach } from '../driver.js';
|
|
13
|
+
import { pixelEffect, sawNothing } from './act-effect.js';
|
|
14
|
+
import { ChangeKind, effectOf } from '../effect.js';
|
|
15
|
+
import { REWRITTEN, rewrittenSince, writeTimeOnDisk, writeTimesOf } from '../files.js';
|
|
16
|
+
import { HudEffect } from '../hud/port.js';
|
|
17
|
+
import { RefTable, writeWindows } from '../lines.js';
|
|
18
|
+
import { BOX_ROLES, DELETE, FOREGROUND_ONLY, PEER_ACTIONS, SELECT_ALL, SettleReason, TEXT_ROLES, VALUE_OF, WANTS_CHECKED, heldBy, movedTo, isChecked, testidOf, } from './form.js';
|
|
19
|
+
import { keyTarget, READ_STATE, see } from './seen.js';
|
|
20
|
+
import { arrangeWindow } from './window.js';
|
|
21
|
+
import { refusalOf } from './link-switch.js';
|
|
22
|
+
import { NO_SCREEN_RECORDING, handOf, wordsOfPicture, wantsPixels, pointerPath, contentMoved, kindFor, needsForeground, placesOf, windowFramesOf, windowMoved, dragValue, pointFor, usesPicture, } from './pointer.js';
|
|
23
|
+
import { WHEEL_AT, scrollBy } from './scroll.js';
|
|
24
|
+
import { selectChoice } from './select.js';
|
|
25
|
+
import { UiRole, isAddressable, place, quoted } from '../ui-node.js';
|
|
26
|
+
/** An application has one screen for the server: it has no address a caller can go to. */
|
|
27
|
+
const ROUTE = '/';
|
|
28
|
+
/** Room for an effect that comes up to a second late, and then the quiet time after it. */
|
|
29
|
+
const DEFAULT_SETTLE_MS = 1500;
|
|
30
|
+
/**
|
|
31
|
+
* The quiet time for an action through the peer. It is shorter than the driver's own 500 ms,
|
|
32
|
+
* because the peer goes on looking: a change that comes after the action is sent when it comes.
|
|
33
|
+
*/
|
|
34
|
+
const PEER_QUIET_MS = 150;
|
|
35
|
+
/** The looks after an action that teach nothing: a late effect of the action can be in them. */
|
|
36
|
+
const LOOKS_AFTER_ACT = 2;
|
|
37
|
+
/** The looks in which a place must change, with no action near, before it "changes by itself". */
|
|
38
|
+
const RESTLESS_LOOKS = 2;
|
|
39
|
+
/** The places in one parent that must change by themselves before the whole parent does. */
|
|
40
|
+
const RESTLESS_PLACES = 3;
|
|
41
|
+
/**
|
|
42
|
+
* Said with a press after which the realm saw no effect of any kind. "Dispatched" means that the
|
|
43
|
+
* application took the call, and an application can take it and do nothing with it.
|
|
44
|
+
*/
|
|
45
|
+
const NOTHING_SEEN_NOTE = 'The press was sent, and nothing changed: not on the screen, not on disk, not in a setting, not on the clipboard. The control may do nothing here, or the application did not act on the press. Do not take the press for done. Read the window again, and give `until` with the consequence you expect.';
|
|
46
|
+
/**
|
|
47
|
+
* The element that a ref named, when its key is gone: an application can change the groups round
|
|
48
|
+
* a control and leave the control where it was. The answer is the one element with the same role,
|
|
49
|
+
* name and identifier at the same place. With no name, two such elements, or another place, there
|
|
50
|
+
* is no answer: the OK of a new dialog is not the OK of the dialog before it.
|
|
51
|
+
*/
|
|
52
|
+
function sameAtItsPlace(all, who) {
|
|
53
|
+
const was = who?.frame;
|
|
54
|
+
if (who === undefined || was === undefined || 0 === who.name.length) {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
const same = all.filter((s) => {
|
|
58
|
+
const is = s.node.frame;
|
|
59
|
+
return (s.node.role === who.role &&
|
|
60
|
+
s.node.name === who.name &&
|
|
61
|
+
s.node.id === who.id &&
|
|
62
|
+
is !== undefined &&
|
|
63
|
+
was.every((value, at) => Math.abs(value - (is[at] ?? Number.NaN)) <= 1));
|
|
64
|
+
});
|
|
65
|
+
return 1 === same.length ? same[0] : undefined;
|
|
66
|
+
}
|
|
67
|
+
/** Each check is one question to the operating system. A search this wide is not a check. */
|
|
68
|
+
const MAX_REACH_CHECKS = 8;
|
|
69
|
+
const Field = { VALUE: 'value', STATES: 'states' };
|
|
70
|
+
/**
|
|
71
|
+
* One signal for each file an action changed. The name is `file.` and the kind of change:
|
|
72
|
+
* `file.written`, `file.modified`, `file.deleted`, `file.mode-changed`. A file that was written
|
|
73
|
+
* again with the same bytes is `file.written` too, with `kind: "rewritten"`. The data is the file name,
|
|
74
|
+
* its path and its size.
|
|
75
|
+
*/
|
|
76
|
+
/** What the agent reads when the person presses "Done" on the HUD at the end of their turn. */
|
|
77
|
+
export const HUMAN_DONE = 'Done: the person finished what you asked for on the HUD. Read the screen again.';
|
|
78
|
+
export const FILE_SIGNAL_PREFIX = 'file.';
|
|
79
|
+
/**
|
|
80
|
+
* The signal for an action in which the application's process wrote bytes to disk, in any folder.
|
|
81
|
+
* The operating system counts the bytes. No folder has to be watched for it.
|
|
82
|
+
*/
|
|
83
|
+
export const DISK_WROTE_SIGNAL = 'disk.wrote';
|
|
84
|
+
/** The signal for one setting of the application that an action changed: its key and its values. */
|
|
85
|
+
export const SETTINGS_CHANGED_SIGNAL = 'settings.changed';
|
|
86
|
+
/** The signal for an action that put something on the clipboard. It does not carry the content. */
|
|
87
|
+
export const CLIPBOARD_CHANGED_SIGNAL = 'clipboard.changed';
|
|
88
|
+
/** The commands this realm answers. The peer declares exactly these to the bridge. */
|
|
89
|
+
export const PEER_COMMANDS = [
|
|
90
|
+
ReticleCommand.SNAPSHOT,
|
|
91
|
+
ReticleCommand.QUERY,
|
|
92
|
+
ReticleCommand.MATCH,
|
|
93
|
+
ReticleCommand.INSPECT,
|
|
94
|
+
ReticleCommand.ACT,
|
|
95
|
+
ReticleCommand.ACT_SEQUENCE,
|
|
96
|
+
ReticleCommand.SCROLL,
|
|
97
|
+
ReticleCommand.CAPABILITIES,
|
|
98
|
+
ReticleCommand.CAPTURE,
|
|
99
|
+
ReticleCommand.NARRATE,
|
|
100
|
+
];
|
|
101
|
+
function text(value) {
|
|
102
|
+
return 'string' === typeof value ? value : undefined;
|
|
103
|
+
}
|
|
104
|
+
function record(value) {
|
|
105
|
+
return 'object' === typeof value && value !== null ? value : {};
|
|
106
|
+
}
|
|
107
|
+
export { PEER_ACTIONS };
|
|
108
|
+
export class PeerCommands {
|
|
109
|
+
#deps;
|
|
110
|
+
#refs = new RefTable();
|
|
111
|
+
/** The watched folders at the last look. Absent before the first one. */
|
|
112
|
+
#files;
|
|
113
|
+
/** When each watched file was last written, at the last look. */
|
|
114
|
+
#fileTimes;
|
|
115
|
+
/** The tree at the last look, to say what changed since. */
|
|
116
|
+
#tree;
|
|
117
|
+
/** Actions in progress. An action says its own changes, so the look between actions waits. */
|
|
118
|
+
#acting = 0;
|
|
119
|
+
#looking = false;
|
|
120
|
+
/** Where the last picture was taken, and its pixels for each point of the screen. */
|
|
121
|
+
#picture;
|
|
122
|
+
/** Looks since the last action. The first ones can hold a late effect of that action. */
|
|
123
|
+
#looksSinceAct = 0;
|
|
124
|
+
/**
|
|
125
|
+
* How many looks, with no action near, saw each place change. A place that changed in two of
|
|
126
|
+
* them changes by itself: a clock, a tip that rotates. A change there during an action is not
|
|
127
|
+
* counted as the effect of the action. A place is one element: its parent, its role and its
|
|
128
|
+
* corner. It was the whole parent, and a clock that lies in the window made every effect in
|
|
129
|
+
* that window "changed by itself".
|
|
130
|
+
*/
|
|
131
|
+
#restless = new Map();
|
|
132
|
+
/**
|
|
133
|
+
* The places in each parent that changed with no action near. A parent with several of them
|
|
134
|
+
* changes by itself as a whole: a list that gets new rows, each at a new place.
|
|
135
|
+
*/
|
|
136
|
+
#restlessIn = new Map();
|
|
137
|
+
constructor(deps) {
|
|
138
|
+
this.#deps = deps;
|
|
139
|
+
deps.hud?.app(deps.driver.subject().app);
|
|
140
|
+
// The person ends their turn on the HUD: the agent gets it as their message.
|
|
141
|
+
deps.hud?.onDone?.(() => {
|
|
142
|
+
deps.emit(EventType.HUMAN_CONTROL, { kind: HumanControlKind.MESSAGE, text: HUMAN_DONE });
|
|
143
|
+
});
|
|
144
|
+
// The first look. A file that is there now is not a change.
|
|
145
|
+
this.sendFileChanges();
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Sends what changed in the tree since the last look, when no action is in progress.
|
|
149
|
+
*
|
|
150
|
+
* An action sends its own changes before it answers. An application also changes after that: a
|
|
151
|
+
* second step of an effect, a save that ends late. With no look between actions those changes
|
|
152
|
+
* were sent by nobody, and a caller that waited for them had only its own questions to go on.
|
|
153
|
+
* The peer calls this on a timer.
|
|
154
|
+
*/
|
|
155
|
+
async sendTreeChanges() {
|
|
156
|
+
if (this.#acting > 0 || this.#looking) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
this.#looking = true;
|
|
160
|
+
try {
|
|
161
|
+
const read = await this.#deps.driver.read();
|
|
162
|
+
// An action that began while the read was out says its own changes from its own reads.
|
|
163
|
+
if (0 === this.#acting) {
|
|
164
|
+
if (this.#tree !== undefined) {
|
|
165
|
+
const { changes } = effectOf(this.#tree, read.root);
|
|
166
|
+
for (const change of changes) {
|
|
167
|
+
this.#send(change);
|
|
168
|
+
}
|
|
169
|
+
this.#looksSinceAct += 1;
|
|
170
|
+
if (this.#looksSinceAct > LOOKS_AFTER_ACT) {
|
|
171
|
+
for (const change of changes) {
|
|
172
|
+
this.#restlessIn.set(change.within, (this.#restlessIn.get(change.within) ?? new Set()).add(change.at));
|
|
173
|
+
}
|
|
174
|
+
for (const place of new Set(changes.map((change) => change.at))) {
|
|
175
|
+
this.#restless.set(place, (this.#restless.get(place) ?? 0) + 1);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
this.#tree = read.root;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
finally {
|
|
183
|
+
this.#looking = false;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Sends one signal for each file in the watched folders that changed since the last look.
|
|
188
|
+
*
|
|
189
|
+
* It is called after each action, and the peer calls it on a timer too. An application can
|
|
190
|
+
* write a file a moment after the action is finished. A signal that is sent only inside the
|
|
191
|
+
* action misses that file, and a claim that waits for it then fails for a file that is there.
|
|
192
|
+
*/
|
|
193
|
+
sendFileChanges() {
|
|
194
|
+
const workspace = this.#deps.workspace;
|
|
195
|
+
if (workspace === undefined) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const timeOf = this.#deps.writeTimeOf ?? writeTimeOnDisk;
|
|
199
|
+
const now = workspace.snapshot();
|
|
200
|
+
const changes = diffSnapshots(this.#files ?? now, now);
|
|
201
|
+
for (const file of changes) {
|
|
202
|
+
this.#deps.emit(EventType.SIGNAL, {
|
|
203
|
+
name: `${FILE_SIGNAL_PREFIX}${file.kind}`,
|
|
204
|
+
data: { file: basename(file.path), path: file.path, size: file.after?.size ?? 0 },
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
// The same bytes, written again: the snapshot shows no change, and the write time does.
|
|
208
|
+
const changed = new Set(changes.map((file) => file.path));
|
|
209
|
+
for (const file of rewrittenSince(this.#fileTimes ?? new Map(), now, changed, timeOf)) {
|
|
210
|
+
this.#deps.emit(EventType.SIGNAL, {
|
|
211
|
+
// The same name as a first write: a claim that the file was written holds for it. The
|
|
212
|
+
// data says that the bytes are the ones that were there.
|
|
213
|
+
name: `${FILE_SIGNAL_PREFIX}${FileKind.WRITTEN}`,
|
|
214
|
+
data: { file: basename(file.path), path: file.path, size: file.size, kind: REWRITTEN },
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
this.#files = now;
|
|
218
|
+
this.#fileTimes = writeTimesOf(now, timeOf);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* The answer to one command. It throws when the command fails; the peer sends the words on.
|
|
222
|
+
*
|
|
223
|
+
* `outward` comes with a command the Harness sent: the outward actions (send, delete, pay) its
|
|
224
|
+
* journey asks for. No other outward control is pressed. Absent: an agent or a saved flow sent
|
|
225
|
+
* the command, and there is no limit here.
|
|
226
|
+
*/
|
|
227
|
+
async answer(name, args, outward) {
|
|
228
|
+
switch (name) {
|
|
229
|
+
case ReticleCommand.SNAPSHOT:
|
|
230
|
+
return this.#snapshot();
|
|
231
|
+
case ReticleCommand.QUERY:
|
|
232
|
+
return this.#query(args, undefined);
|
|
233
|
+
case ReticleCommand.MATCH:
|
|
234
|
+
return this.#match(args);
|
|
235
|
+
case ReticleCommand.INSPECT:
|
|
236
|
+
return this.#inspect(text(args['ref']) ?? '');
|
|
237
|
+
case ReticleCommand.ACT:
|
|
238
|
+
return this.#act(args, outward);
|
|
239
|
+
case ReticleCommand.ACT_SEQUENCE:
|
|
240
|
+
return this.#sequence(Array.isArray(args['steps']) ? args['steps'] : [], outward);
|
|
241
|
+
case ReticleCommand.SCROLL:
|
|
242
|
+
return this.#scroll(args);
|
|
243
|
+
case ReticleCommand.CAPABILITIES:
|
|
244
|
+
return { testids: [], signals: [], stores: [], flows: [] };
|
|
245
|
+
case ReticleCommand.CAPTURE:
|
|
246
|
+
return this.#capture(args);
|
|
247
|
+
case ReticleCommand.PRESENTER:
|
|
248
|
+
return this.#presenter(args);
|
|
249
|
+
case ReticleCommand.NARRATE:
|
|
250
|
+
// What the agent did in a page, or what it is about to do here, in the log the person reads
|
|
251
|
+
// while computer use is live. It is declared in COMMANDS: the bridge refused it before.
|
|
252
|
+
this.#deps.hud?.act(text(args['text']) ?? '');
|
|
253
|
+
return { ok: true, shown: this.#deps.hud !== undefined };
|
|
254
|
+
case ReticleCommand.IMPACT:
|
|
255
|
+
case ReticleCommand.FLOW_PROGRESS:
|
|
256
|
+
case ReticleCommand.PLAN:
|
|
257
|
+
// Reports that a page HUD draws. The desktop HUD has no place for them.
|
|
258
|
+
return { ok: true };
|
|
259
|
+
default:
|
|
260
|
+
throw new Error(`the computer realm does not answer ${name}`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
async #snapshot() {
|
|
264
|
+
const read = await this.#deps.driver.read();
|
|
265
|
+
const written = writeWindows(read.root, this.#refs);
|
|
266
|
+
return {
|
|
267
|
+
tree: written.tree,
|
|
268
|
+
status: { route: ROUTE, title: this.#deps.driver.subject().app },
|
|
269
|
+
nodes: written.tree.split('\n').length,
|
|
270
|
+
truncated: false,
|
|
271
|
+
// Elements with no place on the screen (closed menus) are left out, here and in a search.
|
|
272
|
+
omitted: written.omitted,
|
|
273
|
+
// Zero: the window is closed, or it is on another Space, which this realm cannot read.
|
|
274
|
+
windows: see(read.root).filter((s) => s.node.role === UiRole.WINDOW).length,
|
|
275
|
+
...(read.windowsNotOnScreen === undefined || 0 === read.windowsNotOnScreen
|
|
276
|
+
? {}
|
|
277
|
+
: {
|
|
278
|
+
// Not `next`: the server says a `next` line once in a session, and this is a state.
|
|
279
|
+
readable: false,
|
|
280
|
+
why: `${String(read.windowsNotOnScreen)} window(s) of this application are on another Space or made small. They cannot be read from here. Ask the person to bring the application to this Space.`,
|
|
281
|
+
}),
|
|
282
|
+
unread: read.unread,
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
#describe(seen) {
|
|
286
|
+
const states = [...seen.node.states];
|
|
287
|
+
states.push(seen.visible ? ElementState.VISIBLE : ElementState.HIDDEN);
|
|
288
|
+
if (!seen.node.states.includes(ElementState.DISABLED)) {
|
|
289
|
+
states.push(ElementState.ENABLED);
|
|
290
|
+
}
|
|
291
|
+
return {
|
|
292
|
+
ref: this.#refs.refFor(seen.key, seen.node),
|
|
293
|
+
role: seen.node.role,
|
|
294
|
+
name: seen.node.name,
|
|
295
|
+
...(seen.node.value === undefined ? {} : { value: seen.node.value }),
|
|
296
|
+
...(seen.node.id === undefined ? {} : { testid: seen.node.id }),
|
|
297
|
+
states,
|
|
298
|
+
visible: seen.visible,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
async #find(query) {
|
|
302
|
+
const by = text(query['by']);
|
|
303
|
+
const value = text(query['value']);
|
|
304
|
+
const role = by === QueryBy.ROLE ? value : text(query['role']);
|
|
305
|
+
const name = by === QueryBy.LABEL ? value : (text(query['name']) ?? text(query['label']));
|
|
306
|
+
const wanted = by === QueryBy.TEXT ? value : text(query['text']);
|
|
307
|
+
const id = by === QueryBy.TESTID ? value : text(query['testid']);
|
|
308
|
+
if (role === undefined && name === undefined && wanted === undefined && id === undefined) {
|
|
309
|
+
throw new Error('the computer realm finds an element by role, name, text or test id. This query has none of them.');
|
|
310
|
+
}
|
|
311
|
+
const exact = true === query['matchCase'];
|
|
312
|
+
const fold = (s) => (exact ? s : s.toLowerCase());
|
|
313
|
+
const read = await this.#deps.driver.read();
|
|
314
|
+
return see(read.root).filter((s) => isAddressable(s.node) &&
|
|
315
|
+
(role === undefined || role === s.node.role) &&
|
|
316
|
+
(name === undefined || name === s.node.name) &&
|
|
317
|
+
(id === undefined || id === s.node.id) &&
|
|
318
|
+
(wanted === undefined ||
|
|
319
|
+
fold(`${s.node.name} ${s.node.value ?? ''}`).includes(fold(wanted))));
|
|
320
|
+
}
|
|
321
|
+
async #query(args, state) {
|
|
322
|
+
let found = await this.#find(args);
|
|
323
|
+
if (state !== undefined) {
|
|
324
|
+
const holds = READ_STATE[state];
|
|
325
|
+
if (holds === undefined) {
|
|
326
|
+
throw new Error(`the state ${state} is not read by the computer realm`);
|
|
327
|
+
}
|
|
328
|
+
if (state === ElementState.HIDDEN) {
|
|
329
|
+
// Hidden is the other side of visible, by the same two facts: no place in its window, or
|
|
330
|
+
// a place that something covers. A check and a click must not disagree about an element.
|
|
331
|
+
const placed = found.filter((s) => s.visible);
|
|
332
|
+
const seen = new Set(await this.#uncovered(placed));
|
|
333
|
+
found = found.filter((s) => !seen.has(s));
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
336
|
+
found = found.filter(holds);
|
|
337
|
+
if (state === ElementState.VISIBLE) {
|
|
338
|
+
found = await this.#uncovered(found);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
const limit = 'number' === typeof args['limit'] ? args['limit'] : found.length;
|
|
343
|
+
return { elements: found.slice(0, limit).map((s) => this.#describe(s)), count: found.length };
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Of the elements that have a size and are in their window, those that nothing is on top of.
|
|
347
|
+
*
|
|
348
|
+
* "Visible" means a person can see it. A window of another application over this one hides an
|
|
349
|
+
* element as well as a closed dialog does, and the tree does not say so: the window server does.
|
|
350
|
+
* When the operating system cannot say, the check fails with words, and the caller gets "not
|
|
351
|
+
* known". It never gets a `yes` for an element nobody could see.
|
|
352
|
+
*/
|
|
353
|
+
async #uncovered(found) {
|
|
354
|
+
if (found.length > MAX_REACH_CHECKS) {
|
|
355
|
+
throw new Error(`${String(found.length)} elements match. The computer realm checks each one for a window on top, and it checks ${String(MAX_REACH_CHECKS)} at most. Name one element.`);
|
|
356
|
+
}
|
|
357
|
+
const seen = [];
|
|
358
|
+
for (const candidate of found) {
|
|
359
|
+
const answer = await this.#deps.driver.reach(candidate);
|
|
360
|
+
if (answer.reach === Reach.UNKNOWN) {
|
|
361
|
+
throw new Error(`could not tell if a person can see ${candidate.node.role} "${candidate.node.name}": ${answer.detail ?? 'the operating system did not answer'}`);
|
|
362
|
+
}
|
|
363
|
+
if (answer.reach === Reach.YES) {
|
|
364
|
+
seen.push(candidate);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return seen;
|
|
368
|
+
}
|
|
369
|
+
async #match(args) {
|
|
370
|
+
const found = await this.#query(record(args['query']), text(args['state']));
|
|
371
|
+
return {
|
|
372
|
+
matched: found.count > 0,
|
|
373
|
+
...found,
|
|
374
|
+
// The whole name and value are compared, so a text check here is always on the full text.
|
|
375
|
+
...(true === args['fullText'] ? { fullText: true } : {}),
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
/** The element a ref was given for, in a new read. */
|
|
379
|
+
async #resolve(ref) {
|
|
380
|
+
const key = this.#refs.keyOf(ref);
|
|
381
|
+
const read = await this.#deps.driver.read();
|
|
382
|
+
const all = see(read.root);
|
|
383
|
+
const seen = (key === undefined ? undefined : all.find((s) => s.key === key)) ??
|
|
384
|
+
sameAtItsPlace(all, this.#refs.whoOf(ref));
|
|
385
|
+
if (seen === undefined) {
|
|
386
|
+
throw new Error(`ref '${ref}' no longer resolves to an element`);
|
|
387
|
+
}
|
|
388
|
+
if (seen.key !== key) {
|
|
389
|
+
// The application changed the tree round the element, and the element stayed.
|
|
390
|
+
this.#refs.move(ref, seen.key);
|
|
391
|
+
}
|
|
392
|
+
return seen;
|
|
393
|
+
}
|
|
394
|
+
async #inspect(ref) {
|
|
395
|
+
const seen = await this.#resolve(ref);
|
|
396
|
+
const frame = seen.node.frame;
|
|
397
|
+
return {
|
|
398
|
+
...this.#describe(seen),
|
|
399
|
+
// What a hover gives a person. The realm has no hover, and it does not need one for this.
|
|
400
|
+
...(seen.node.help === undefined ? {} : { help: seen.node.help }),
|
|
401
|
+
...(frame === undefined
|
|
402
|
+
? {}
|
|
403
|
+
: { box: { x: frame[0], y: frame[1], width: frame[2], height: frame[3] } }),
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* A picture of the window, for an application that gives few elements. The place and the scale
|
|
408
|
+
* of the picture are kept: a click at a point of the picture needs them.
|
|
409
|
+
*/
|
|
410
|
+
/** The agent's `yield`: the HUD shows whose turn it is, and the question when it asks. */
|
|
411
|
+
#presenter(args) {
|
|
412
|
+
const state = text(args['state']);
|
|
413
|
+
if (state !== undefined) {
|
|
414
|
+
const tone = text(args['tone']);
|
|
415
|
+
const words = text(args['text']);
|
|
416
|
+
this.#deps.hud?.turn?.({
|
|
417
|
+
state,
|
|
418
|
+
...(tone === undefined ? {} : { tone }),
|
|
419
|
+
...(words === undefined ? {} : { text: words }),
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
return Promise.resolve({ ok: true });
|
|
423
|
+
}
|
|
424
|
+
async #capture(args) {
|
|
425
|
+
const screen = true === args['screen'];
|
|
426
|
+
const taken = await this.#deps.driver.capture?.(screen ? { screen } : undefined);
|
|
427
|
+
if (taken === undefined) {
|
|
428
|
+
return { ok: false, reason: 'this realm takes no picture on this system' };
|
|
429
|
+
}
|
|
430
|
+
if (!taken.ok) {
|
|
431
|
+
return {
|
|
432
|
+
ok: false,
|
|
433
|
+
reason: NO_SCREEN_RECORDING === taken.reason
|
|
434
|
+
? 'The terminal has no Screen Recording permission. Open System Settings, Privacy & Security, Screen Recording, and switch on the terminal that runs reticle-computer.'
|
|
435
|
+
: taken.reason,
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
const shown = await this.#deps.driver.read();
|
|
439
|
+
this.#picture = {
|
|
440
|
+
window: taken.window,
|
|
441
|
+
scale: taken.scale,
|
|
442
|
+
appWindow: taken.appWindow ?? taken.window,
|
|
443
|
+
places: placesOf(shown.root),
|
|
444
|
+
// A picture of the whole screen holds every window: a point can be in any of them.
|
|
445
|
+
...(screen ? { windows: windowFramesOf(shown.root) } : {}),
|
|
446
|
+
};
|
|
447
|
+
const words = wordsOfPicture(await this.#deps.driver.texts?.(taken.path));
|
|
448
|
+
return { ok: true, path: taken.path, ...(screen ? { screen } : {}), ...words };
|
|
449
|
+
}
|
|
450
|
+
#send(change) {
|
|
451
|
+
const who = { role: change.role, name: change.name };
|
|
452
|
+
if (change.kind === ChangeKind.NEW) {
|
|
453
|
+
this.#deps.emit(EventType.DOM_ADDED, who);
|
|
454
|
+
}
|
|
455
|
+
else if (change.kind === ChangeKind.GONE) {
|
|
456
|
+
this.#deps.emit(EventType.DOM_REMOVED, who);
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
for (const [attr, pair] of [
|
|
460
|
+
[Field.VALUE, change.value],
|
|
461
|
+
[Field.STATES, change.states],
|
|
462
|
+
]) {
|
|
463
|
+
if (pair !== undefined) {
|
|
464
|
+
this.#deps.emit(EventType.DOM_ATTR, { ...who, attr, old: pair[0], value: pair[1] });
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Does one action, and sends what it changed before the caller gets an answer: each change in
|
|
471
|
+
* the tree as an event, and each file written in a watched folder as a signal.
|
|
472
|
+
*/
|
|
473
|
+
async #drive(target, ref, kind, value, how) {
|
|
474
|
+
const workspace = this.#deps.workspace;
|
|
475
|
+
const hud = this.#deps.hud;
|
|
476
|
+
if (true === hud?.stopped()) {
|
|
477
|
+
throw new Error('the person at the machine stopped Reticle in its window. The realm does no more actions on this application. Tell them what you were about to do.');
|
|
478
|
+
}
|
|
479
|
+
if (true === hud?.paused()) {
|
|
480
|
+
throw new Error('the person at the machine paused Reticle in its window. No action is done while it is paused. Tell them what you were about to do, and wait.');
|
|
481
|
+
}
|
|
482
|
+
// The pointer goes where the action goes, and for a drag on to where it ends. See pointerPath.
|
|
483
|
+
const path = pointerPath(kind, value, target.node.frame);
|
|
484
|
+
if (path.start !== undefined) {
|
|
485
|
+
await hud?.move(...path.start);
|
|
486
|
+
}
|
|
487
|
+
if (target.windowFrame !== undefined) {
|
|
488
|
+
hud?.glow(target.windowFrame);
|
|
489
|
+
}
|
|
490
|
+
hud?.act(`${kind} ${target.node.role} "${target.node.name}"`);
|
|
491
|
+
// What changed before this action is not its effect: say it first, on its own.
|
|
492
|
+
this.sendFileChanges();
|
|
493
|
+
this.#acting += 1;
|
|
494
|
+
let outcome;
|
|
495
|
+
try {
|
|
496
|
+
outcome = await this.#deps.driver.act({
|
|
497
|
+
target,
|
|
498
|
+
kind,
|
|
499
|
+
...(value === undefined ? {} : { value }),
|
|
500
|
+
...how,
|
|
501
|
+
quietMs: this.#deps.quietMs ?? PEER_QUIET_MS,
|
|
502
|
+
watch: workspace?.roots ?? [],
|
|
503
|
+
});
|
|
504
|
+
if (outcome.refused === undefined) {
|
|
505
|
+
this.#tree = outcome.after.root;
|
|
506
|
+
}
|
|
507
|
+
this.#looksSinceAct = 0;
|
|
508
|
+
if (outcome.refused !== undefined || !outcome.delivered) {
|
|
509
|
+
hud?.effect(`refused: ${outcome.refused ?? 'not taken'}`, HudEffect.REFUSED);
|
|
510
|
+
}
|
|
511
|
+
else if (path.end !== undefined) {
|
|
512
|
+
await hud?.move(...path.end);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
finally {
|
|
516
|
+
this.#acting -= 1;
|
|
517
|
+
}
|
|
518
|
+
if (outcome.refused === ActRefusal.STALE) {
|
|
519
|
+
throw new Error(`ref '${ref}' no longer resolves to an element`);
|
|
520
|
+
}
|
|
521
|
+
if (outcome.refused === ActRefusal.UNREACHABLE) {
|
|
522
|
+
throw new Error(`a person cannot reach ${target.node.role} "${target.node.name}": ${outcome.detail ?? 'another view or window'} is over it.`);
|
|
523
|
+
}
|
|
524
|
+
if (true === outcome.appGone) {
|
|
525
|
+
throw new Error(`${this.#deps.driver.subject().app} ended while the action on ${target.node.role} "${target.node.name}" ran: it quit, or it crashed. The action can be the cause. Do not send it again before you know.`);
|
|
526
|
+
}
|
|
527
|
+
if (outcome.refused === ActRefusal.NO_KEYBOARD) {
|
|
528
|
+
throw new Error(`${this.#deps.driver.subject().app} did not take the keyboard, so no real key was sent: a real key goes to whatever application has the keyboard. Send the keys without the foreground, or bring the application to the front first with the action ${ActionType.FOCUS}.`);
|
|
529
|
+
}
|
|
530
|
+
if (outcome.refused === ActRefusal.PERSON_ACTIVE) {
|
|
531
|
+
throw new Error(`the person at the machine is using the keyboard or the pointer, so the realm did not take the pointer for ${target.node.role} "${target.node.name}". It waited five seconds. Try again when they are not typing, or do the step without the foreground.`);
|
|
532
|
+
}
|
|
533
|
+
if (outcome.refused === ActRefusal.NO_REAL_INPUT) {
|
|
534
|
+
throw new Error(`no real pointer or key was sent: ${outcome.detail ?? 'the desktop takes none from a program'}. Do the step without args.foreground, through the accessibility interface, or log in to an X session.`);
|
|
535
|
+
}
|
|
536
|
+
if (outcome.refused === ActRefusal.NEEDS_FOREGROUND) {
|
|
537
|
+
throw new Error(needsForeground(kind, this.#deps.driver.subject().app));
|
|
538
|
+
}
|
|
539
|
+
if (outcome.refused === ActRefusal.REACH_UNKNOWN) {
|
|
540
|
+
throw new Error(`could not tell if a person can reach ${target.node.role} "${target.node.name}": ${outcome.detail ?? 'the operating system did not answer'}. The menu bar of an application is on the screen only when the application is in front. Bring ${this.#deps.driver.subject().app} to the front, or use a control in its window.`);
|
|
541
|
+
}
|
|
542
|
+
// The application has no menu to give by name for this element. The real button can try.
|
|
543
|
+
if (kind === ActKind.MENU && outcome.refused === undefined && !outcome.delivered) {
|
|
544
|
+
throw new Error(needsForeground('a right-click here', this.#deps.driver.subject().app));
|
|
545
|
+
}
|
|
546
|
+
if (outcome.refused !== undefined || !outcome.delivered) {
|
|
547
|
+
throw new Error(`${target.node.role} "${target.node.name}" did not take the action: ${outcome.refused ?? 'the operating system refused it'}.`);
|
|
548
|
+
}
|
|
549
|
+
const restless = new Set([
|
|
550
|
+
...[...this.#restless].filter(([, looks]) => looks >= RESTLESS_LOOKS).map(([at]) => at),
|
|
551
|
+
...[...this.#restlessIn]
|
|
552
|
+
.filter(([, places]) => places.size >= RESTLESS_PLACES)
|
|
553
|
+
.map(([parent]) => parent),
|
|
554
|
+
]);
|
|
555
|
+
const effect = effectOf(outcome.before.root, outcome.after.root, restless);
|
|
556
|
+
const all = effect.changes;
|
|
557
|
+
// Every change is sent: it did happen. Only the count of what the action did leaves out the
|
|
558
|
+
// places that change by themselves.
|
|
559
|
+
for (const change of all) {
|
|
560
|
+
this.#send(change);
|
|
561
|
+
}
|
|
562
|
+
const changes = all.filter((change) => change.ambient === undefined);
|
|
563
|
+
hud?.effect(0 === changes.length ? 'no change' : `${String(changes.length)} change(s)`, 0 === changes.length ? HudEffect.NONE : HudEffect.OK);
|
|
564
|
+
// The bytes that the process wrote to disk, when the driver counts them. A signal only when
|
|
565
|
+
// there were some: a claim with `count: 0` then says "the application wrote nothing".
|
|
566
|
+
// A setting that the action changed. It is often not on the screen and in no watched file.
|
|
567
|
+
for (const setting of outcome.settingsChanged ?? []) {
|
|
568
|
+
this.#deps.emit(EventType.SIGNAL, {
|
|
569
|
+
name: SETTINGS_CHANGED_SIGNAL,
|
|
570
|
+
data: {
|
|
571
|
+
key: setting.key,
|
|
572
|
+
old: setting.old,
|
|
573
|
+
value: setting.new,
|
|
574
|
+
...(true === setting.cut ? { cut: true } : {}),
|
|
575
|
+
},
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
if (true === outcome.clipboardChanged) {
|
|
579
|
+
this.#deps.emit(EventType.SIGNAL, { name: CLIPBOARD_CHANGED_SIGNAL, data: {} });
|
|
580
|
+
}
|
|
581
|
+
if ((outcome.pixels?.changed ?? 0) > 0) {
|
|
582
|
+
this.#deps.emit(EventType.SIGNAL, { name: PIXELS_CHANGED_SIGNAL, data: outcome.pixels });
|
|
583
|
+
}
|
|
584
|
+
if (outcome.bytesWritten !== undefined && outcome.bytesWritten > 0) {
|
|
585
|
+
this.#deps.emit(EventType.SIGNAL, {
|
|
586
|
+
name: DISK_WROTE_SIGNAL,
|
|
587
|
+
data: { bytes: outcome.bytesWritten },
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
this.sendFileChanges();
|
|
591
|
+
return { outcome, changes, byItself: all.length - changes.length, offScreen: effect.offScreen };
|
|
592
|
+
}
|
|
593
|
+
#scroll(args) {
|
|
594
|
+
return scrollBy({
|
|
595
|
+
driver: this.#deps.driver,
|
|
596
|
+
refs: this.#refs,
|
|
597
|
+
drive: (target, at, kind, value, how) => this.#drive(target, at, kind, value, how),
|
|
598
|
+
}, args);
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* Several actions in one command, so a model turn drives more than one step. It stops at the
|
|
602
|
+
* first action that fails, and the failure names the step.
|
|
603
|
+
*/
|
|
604
|
+
async #sequence(steps, outward) {
|
|
605
|
+
const done = [];
|
|
606
|
+
for (const [index, step] of steps.entries()) {
|
|
607
|
+
try {
|
|
608
|
+
done.push(await this.#act(record(step), outward));
|
|
609
|
+
}
|
|
610
|
+
catch (error) {
|
|
611
|
+
const words = error instanceof Error ? error.message : String(error);
|
|
612
|
+
throw new Error(`step ${String(index + 1)} of ${String(steps.length)}: ${words}`, {
|
|
613
|
+
cause: error,
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
return {
|
|
618
|
+
ok: true,
|
|
619
|
+
count: done.length,
|
|
620
|
+
effects: done.map((result) => result.effect),
|
|
621
|
+
steps: done.map(({ effect: _effect, ok: _ok, ...step }) => step),
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* The point a drag ends at, as "x,y" on the screen: the middle of the element with the ref
|
|
626
|
+
* `to.ref`, or the point `to.x`, `to.y`.
|
|
627
|
+
*/
|
|
628
|
+
/** The frame of the element a drag goes onto (`args.toRef`), or absent when none is named. */
|
|
629
|
+
async #ontoFrame(more) {
|
|
630
|
+
const onto = text(more[DRAG_TO_REF_ARG]);
|
|
631
|
+
if (onto === undefined) {
|
|
632
|
+
return undefined;
|
|
633
|
+
}
|
|
634
|
+
const frame = (await this.#resolve(onto)).node.frame;
|
|
635
|
+
if (frame === undefined) {
|
|
636
|
+
throw new Error(`the element ${onto} has no place on the screen to drag to.`);
|
|
637
|
+
}
|
|
638
|
+
return frame;
|
|
639
|
+
}
|
|
640
|
+
async #act(args, allowed) {
|
|
641
|
+
const ref = text(args['ref']) ?? '';
|
|
642
|
+
const action = text(args['action']) ?? '';
|
|
643
|
+
if (action === ActionType.SCROLL) {
|
|
644
|
+
// The same scroll as the `scroll` command, with the answer in the shape of an action.
|
|
645
|
+
const target = await this.#resolve(ref);
|
|
646
|
+
const given = record(args['args']);
|
|
647
|
+
const at = pointFor(this.#picture, given, ActKind.WHEEL, undefined);
|
|
648
|
+
const moved = await this.#scroll({
|
|
649
|
+
...given,
|
|
650
|
+
ref,
|
|
651
|
+
...(at === undefined ? {} : { [WHEEL_AT]: at }),
|
|
652
|
+
});
|
|
653
|
+
return {
|
|
654
|
+
ok: true,
|
|
655
|
+
ref,
|
|
656
|
+
action,
|
|
657
|
+
dispatched: true,
|
|
658
|
+
settled: true,
|
|
659
|
+
settleReason: null,
|
|
660
|
+
effect: { dispatched: true, targetMatched: true, ...moved },
|
|
661
|
+
role: target.node.role,
|
|
662
|
+
name: target.node.name,
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
if (action === ActionType.WINDOW) {
|
|
666
|
+
return arrangeWindow({ drive: (target, at, kind, value, how) => this.#drive(target, at, kind, value, how) }, await this.#resolve(ref), ref, record(args['args']));
|
|
667
|
+
}
|
|
668
|
+
if (action === ActionType.SELECT) {
|
|
669
|
+
return selectChoice({
|
|
670
|
+
driver: this.#deps.driver,
|
|
671
|
+
refs: this.#refs,
|
|
672
|
+
drive: (target, at, kind, value, how) => this.#drive(target, at, kind, value, how),
|
|
673
|
+
act: (step, limit) => this.#act(step, limit),
|
|
674
|
+
}, ref, record(args['args']), allowed);
|
|
675
|
+
}
|
|
676
|
+
const more = record(args['args']);
|
|
677
|
+
const foreground = true === more['foreground'];
|
|
678
|
+
const kind = kindFor(action, more, foreground);
|
|
679
|
+
if (kind === undefined) {
|
|
680
|
+
throw new Error(`the computer realm does not have the action ${action}. It has: ${PEER_ACTIONS.join(', ')}.`);
|
|
681
|
+
}
|
|
682
|
+
const keyed = '' === ref && kind === ActKind.KEY;
|
|
683
|
+
const typed = keyed ? keyTarget(see((await this.#deps.driver.read()).root)) : undefined;
|
|
684
|
+
let target = typed ?? (await this.#resolve(ref));
|
|
685
|
+
const refused = refusalOf(target, action, more, allowed);
|
|
686
|
+
if (refused !== undefined) {
|
|
687
|
+
throw new Error(refused);
|
|
688
|
+
}
|
|
689
|
+
const modifiers = Array.isArray(more['modifiers']) ? more['modifiers'].map(String) : [];
|
|
690
|
+
const key = text(more['text']);
|
|
691
|
+
if (kind === ActKind.KEY && key === undefined && VALUE_OF[action] === undefined) {
|
|
692
|
+
throw new Error(`${action} needs the key in args.text, as the page realm takes it: { text: "Escape" }, or { text: "r", modifiers: ["Alt", "Shift"] }.`);
|
|
693
|
+
}
|
|
694
|
+
// `fill` gives `value`. `type` gives `text`. `press` gives the key in `text`, with modifiers.
|
|
695
|
+
const given = kind === ActKind.KEY && key !== undefined
|
|
696
|
+
? [...modifiers, key].join('+')
|
|
697
|
+
: (text(more['value']) ?? key);
|
|
698
|
+
if (FOREGROUND_ONLY.has(kind) && !foreground) {
|
|
699
|
+
throw new Error(needsForeground(action, this.#deps.driver.subject().app));
|
|
700
|
+
}
|
|
701
|
+
if (foreground && false === this.#deps.foreground) {
|
|
702
|
+
throw new Error(`this session was joined with --background-only: the realm does not take the pointer or bring ${this.#deps.driver.subject().app} to the front.`);
|
|
703
|
+
}
|
|
704
|
+
const at = kind === ActKind.DRAG ? undefined : pointFor(this.#picture, more, kind, target.node.frame);
|
|
705
|
+
const picture = usesPicture(more);
|
|
706
|
+
if (picture && windowMoved(this.#picture, target.windowFrame)) {
|
|
707
|
+
throw new Error(`the window of ${this.#deps.driver.subject().app} moved or changed its size after the picture was taken, so the point of the picture is now on something else. Take a new picture with reticle_screenshot, and give the point of that one.`);
|
|
708
|
+
}
|
|
709
|
+
if (picture) {
|
|
710
|
+
const now = placesOf((await this.#deps.driver.read()).root);
|
|
711
|
+
if (contentMoved(this.#picture?.places, now)) {
|
|
712
|
+
throw new Error(`the content of the window of ${this.#deps.driver.subject().app} moved after the picture was taken (it scrolled, or a part came in above), so the point of the picture is now on something else. Take a new picture with reticle_screenshot, and give the point of that one.`);
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
const base = kind === ActKind.DRAG
|
|
716
|
+
? dragValue(this.#picture, more, target.node.frame, await this.#ontoFrame(more))
|
|
717
|
+
: (at ?? VALUE_OF[action] ?? given);
|
|
718
|
+
const hand = handOf(kind, more, wantsPixels(target.node.role, more));
|
|
719
|
+
const value = '' === hand ? base : `${base ?? ''}${hand}`;
|
|
720
|
+
const wanted = WANTS_CHECKED[action];
|
|
721
|
+
if (wanted !== undefined) {
|
|
722
|
+
if (!BOX_ROLES.has(target.node.role)) {
|
|
723
|
+
throw new Error(`${action} is for a checkbox or a radio button. ${target.node.role} "${target.node.name}" is not one. Use ${ActionType.CLICK}.`);
|
|
724
|
+
}
|
|
725
|
+
// Nothing is sent to a box that is in the state already: a press would turn it over.
|
|
726
|
+
if (isChecked(target.node) === wanted) {
|
|
727
|
+
return {
|
|
728
|
+
ok: true,
|
|
729
|
+
ref,
|
|
730
|
+
action,
|
|
731
|
+
dispatched: false,
|
|
732
|
+
settled: true,
|
|
733
|
+
settleReason: null,
|
|
734
|
+
effect: { alreadyAtValue: true, dispatched: false, targetMatched: true },
|
|
735
|
+
role: target.node.role,
|
|
736
|
+
name: target.node.name,
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
const how = {
|
|
741
|
+
delivery: foreground ? Delivery.FOREGROUND : Delivery.USER_PATH,
|
|
742
|
+
budgetMs: 'number' === typeof more['settleMs'] ? more['settleMs'] : DEFAULT_SETTLE_MS,
|
|
743
|
+
};
|
|
744
|
+
// True when the field took a new value and the application moved it in the tree.
|
|
745
|
+
let valueMoved = false;
|
|
746
|
+
let driven = await this.#drive(target, ref, kind, value, how);
|
|
747
|
+
if (kind === ActKind.SET && value !== undefined && TEXT_ROLES.has(target.node.role)) {
|
|
748
|
+
// A field in a web view answers "done" to a value set from outside, and keeps its old
|
|
749
|
+
// value. The field then gets the keys, as from a person: all of its text selected first,
|
|
750
|
+
// so that the new text replaces it.
|
|
751
|
+
// The application can move the field in its tree as it takes the value. The ref then goes
|
|
752
|
+
// with the field, and the value is read where the field is now.
|
|
753
|
+
const moved = movedTo(driven.outcome, target, value);
|
|
754
|
+
if (moved !== undefined) {
|
|
755
|
+
valueMoved = moved.node.value !== target.node.value;
|
|
756
|
+
this.#refs.move(ref, moved.key);
|
|
757
|
+
target = { ...target, ...moved };
|
|
758
|
+
}
|
|
759
|
+
const first = heldBy(driven.outcome, target.key);
|
|
760
|
+
if (first !== value) {
|
|
761
|
+
if ('' !== first) {
|
|
762
|
+
await this.#drive(target, ref, ActKind.KEY, SELECT_ALL, how);
|
|
763
|
+
}
|
|
764
|
+
driven =
|
|
765
|
+
'' === value
|
|
766
|
+
? await this.#drive(target, ref, ActKind.KEY, DELETE, how)
|
|
767
|
+
: await this.#drive(target, ref, ActKind.TYPE, value, how);
|
|
768
|
+
}
|
|
769
|
+
const held = heldBy(driven.outcome, target.key);
|
|
770
|
+
if (held !== value) {
|
|
771
|
+
throw new Error(`${target.node.role} "${target.node.name}" did not take the value: it holds ${quoted(held)}, not ${quoted(value)}. The realm set the value, then typed it.`);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
const { outcome, changes, byItself, offScreen } = driven;
|
|
775
|
+
if (wanted !== undefined) {
|
|
776
|
+
const now = place(outcome.after.root).find((p) => p.key === target.key)?.node;
|
|
777
|
+
if (now === undefined || isChecked(now) !== wanted) {
|
|
778
|
+
throw new Error(`${target.node.role} "${target.node.name}" is still ${wanted ? 'not checked' : 'checked'} after the press.`);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
return {
|
|
782
|
+
ok: true,
|
|
783
|
+
ref,
|
|
784
|
+
action,
|
|
785
|
+
dispatched: true,
|
|
786
|
+
settled: outcome.settled,
|
|
787
|
+
settleReason: outcome.settled ? null : SettleReason.TIMEOUT,
|
|
788
|
+
effect: {
|
|
789
|
+
dispatched: true,
|
|
790
|
+
targetMatched: true,
|
|
791
|
+
visible: target.visible,
|
|
792
|
+
enabled: true,
|
|
793
|
+
valueChanged: valueMoved || changes.some((c) => c.value !== undefined),
|
|
794
|
+
// Every change in the application, not only those inside the target.
|
|
795
|
+
domMutatedWithin: changes.length,
|
|
796
|
+
// Changes in a place that changes with no action. The action may not be their cause.
|
|
797
|
+
...(byItself > 0 ? { changedByItself: byItself } : {}),
|
|
798
|
+
// Changes to elements that are not on the screen. They are not in the count above.
|
|
799
|
+
...(offScreen > 0 ? { changedOffScreen: offScreen } : {}),
|
|
800
|
+
...pixelEffect(outcome),
|
|
801
|
+
...(kind === ActKind.PRESS && sawNothing(outcome, changes.length + byItself + offScreen)
|
|
802
|
+
? { note: this.#deps.noEffectNote ?? NOTHING_SEEN_NOTE }
|
|
803
|
+
: {}),
|
|
804
|
+
occluded: !outcome.reachable,
|
|
805
|
+
},
|
|
806
|
+
role: target.node.role,
|
|
807
|
+
name: target.node.name,
|
|
808
|
+
...testidOf(target, outcome.before.root),
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
}
|