@burdenoff/ui-engine 2026.1204.1 → 2026.1204.2

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 (63) hide show
  1. package/README.md +20 -2
  2. package/dist/analyze/analyze.d.ts +10 -0
  3. package/dist/analyze/context.d.ts +25 -9
  4. package/dist/analyze/index.d.ts +3 -2
  5. package/dist/analyze/messages.d.ts +2 -0
  6. package/dist/analyze/rules/run-command-from-variable.d.ts +2 -0
  7. package/dist/analyze/rules/single-method-image-target.d.ts +7 -0
  8. package/dist/analyze/sensitive.d.ts +2 -2
  9. package/dist/analyze/types.d.ts +6 -2
  10. package/dist/analyze.js +4 -2
  11. package/dist/desk.d.ts +13 -0
  12. package/dist/desk.js +29 -0
  13. package/dist/dom-runtime.iife.js +1 -1
  14. package/dist/dom-runtime.js +2 -2
  15. package/dist/drivers/desk-backend.d.ts +40 -0
  16. package/dist/drivers/desk-driver.d.ts +41 -0
  17. package/dist/drivers/desk-errors.d.ts +19 -0
  18. package/dist/drivers/desk-tree.d.ts +28 -0
  19. package/dist/drivers/desk-wire.d.ts +237 -0
  20. package/dist/drivers/index.d.ts +3 -1
  21. package/dist/drivers.js +10 -7
  22. package/dist/heal.js +5 -5
  23. package/dist/index-0ps53dhe.js +85 -0
  24. package/dist/{index-ptrmf779.js → index-2q0hh6we.js} +3 -3
  25. package/dist/index-552x2bnm.js +56 -0
  26. package/dist/index-6m0chbhq.js +64 -0
  27. package/dist/{index-dwp10g62.js → index-7gfzv5n8.js} +96 -53
  28. package/dist/{index-gvavb597.js → index-93hfbk5c.js} +10 -0
  29. package/dist/index-a8s39bse.js +920 -0
  30. package/dist/index-b5a306v7.js +43 -0
  31. package/dist/index-bx4bsvg0.js +458 -0
  32. package/dist/{index-9xgz8dxt.js → index-cc49ykn5.js} +4 -4
  33. package/dist/{index-vpm1gnmy.js → index-e557vzdd.js} +219 -34
  34. package/dist/{index-vnv9tp11.js → index-e7pgqd60.js} +2 -2
  35. package/dist/index-hdq3y007.js +246 -0
  36. package/dist/{index-zagmp5ya.js → index-n3nfhjrj.js} +2 -2
  37. package/dist/{index-wnch00m6.js → index-nmsg2wze.js} +2 -2
  38. package/dist/{index-zhbwhdya.js → index-q2jbva7c.js} +5 -3
  39. package/dist/{index-n7ah1jn7.js → index-rbem2yms.js} +16 -243
  40. package/dist/{index-mhv6a02p.js → index-reef7e8m.js} +1 -1
  41. package/dist/{index-3r2y0mnc.js → index-wdbn4xjc.js} +5 -5
  42. package/dist/{index-8eb4xd1j.js → index-x8fc4r4h.js} +101 -426
  43. package/dist/{index-0tfzfxx9.js → index-yhzykhdj.js} +1 -43
  44. package/dist/{index-t55wvhvh.js → index-z2v8prer.js} +1 -61
  45. package/dist/{index-5pv673wd.js → index-zj3zhbac.js} +2 -1
  46. package/dist/index.d.ts +1 -0
  47. package/dist/index.js +79 -31
  48. package/dist/interpret/driver.d.ts +109 -18
  49. package/dist/interpret/events.d.ts +5 -2
  50. package/dist/interpret/index.d.ts +4 -3
  51. package/dist/interpret/run.d.ts +28 -1
  52. package/dist/interpret.js +16 -9
  53. package/dist/script/adapters/recording-desktop.d.ts +52 -0
  54. package/dist/script/describe.d.ts +7 -2
  55. package/dist/script/desk-roles.d.ts +33 -0
  56. package/dist/script/desktop-capabilities.d.ts +7 -2
  57. package/dist/script/desktop-schema.d.ts +2 -0
  58. package/dist/script/index.d.ts +3 -1
  59. package/dist/script.js +35 -19
  60. package/dist/target/types.d.ts +10 -4
  61. package/dist/target.js +20 -16
  62. package/dist/testing.js +5 -3
  63. package/package.json +7 -3
@@ -0,0 +1,920 @@
1
+ import {
2
+ MAX_DESK_TEXT_TARGET2,
3
+ MAX_DESK_SET_VALUE2,
4
+ isWorkspaceRelativePath2
5
+ } from "./index-zj3zhbac.js";
6
+ import {
7
+ readsLikeSecret
8
+ } from "./index-552x2bnm.js";
9
+ import {
10
+ UiDriverError2
11
+ } from "./index-yhzykhdj.js";
12
+ import {
13
+ rolesMatch2
14
+ } from "./index-0ps53dhe.js";
15
+ import {
16
+ deskFeaturesOfMethod2,
17
+ deskHasFeature2
18
+ } from "./index-bx4bsvg0.js";
19
+ import {
20
+ MAX_CANDIDATES2,
21
+ MAX_CANDIDATE_NAME2,
22
+ DEFAULT_FUZZY_THRESHOLD2,
23
+ FUZZY_MARGIN2,
24
+ ANCHOR_MARGIN_PX2,
25
+ relationDistance2
26
+ } from "./index-hdq3y007.js";
27
+ import {
28
+ sameText2,
29
+ containsText2,
30
+ textSimilarity2,
31
+ clip2
32
+ } from "./index-z2v8prer.js";
33
+
34
+ // src/drivers/desk-errors.ts
35
+ function engineCodeOfDeskError2(error) {
36
+ switch (error.code) {
37
+ case "TARGET_NOT_FOUND": {
38
+ const matches = error.detail?.matches;
39
+ return typeof matches === "number" && matches > 1 ? "ELEMENT_AMBIGUOUS" : "ELEMENT_NOT_FOUND";
40
+ }
41
+ case "UNEXPECTED_WINDOW":
42
+ return "UNEXPECTED_WINDOW";
43
+ case "UNSUPPORTED_ON_DESK":
44
+ return "UNSUPPORTED_ON_DESK";
45
+ case "SHELL_NOT_ALLOWED":
46
+ return "SHELL_NOT_ALLOWED";
47
+ case "URL_BLOCKED":
48
+ return "URL_NOT_ALLOWED";
49
+ case "ORIGIN_MISMATCH":
50
+ return "SECRET_UNAVAILABLE";
51
+ case "OUTSIDE_WORKSPACE_FOLDER":
52
+ return "ACTION_FAILED";
53
+ case "TIMEOUT":
54
+ return "APPLICATION_NOT_RESPONDING";
55
+ case "STALE_FENCING_TOKEN":
56
+ case "LEASE_PREEMPTED":
57
+ case "KILL_SWITCH":
58
+ case "LOCAL_CONSENT_OFF":
59
+ case "CONSENT_REQUIRED":
60
+ case "DESK_OFFLINE":
61
+ case "DESK_UNAVAILABLE":
62
+ return "DESK_UNAVAILABLE";
63
+ default:
64
+ return "DRIVER_ERROR";
65
+ }
66
+ }
67
+ var PASSED_DETAIL = ["matches", "reason", "busyJobId"];
68
+ function engineDetailOfDeskError2(error) {
69
+ const detail = { deskCode: error.code.slice(0, 64) };
70
+ for (const name of PASSED_DETAIL) {
71
+ const value = error.detail?.[name];
72
+ if (typeof value === "number" || typeof value === "string" && value.length <= 200)
73
+ detail[name] = value;
74
+ }
75
+ if (error.code === "OUTSIDE_WORKSPACE_FOLDER")
76
+ detail.problem = "OUTSIDE_WORKSPACE_FOLDER";
77
+ return detail;
78
+ }
79
+
80
+ // src/drivers/desk-tree.ts
81
+ function webRectOf(bounds) {
82
+ return { x: bounds.x, y: bounds.y, width: bounds.w, height: bounds.h };
83
+ }
84
+ function deskNodeInState2(node, state) {
85
+ if (state === "attached")
86
+ return true;
87
+ if (state === "visible")
88
+ return node.visible;
89
+ return node.visible && node.enabled;
90
+ }
91
+ function windowMatches(method, snapshot) {
92
+ const window = snapshot.window;
93
+ if (method.app !== undefined && (!window || !sameText2(window.app, method.app)))
94
+ return false;
95
+ if (method.window !== undefined && (!window || !sameText2(window.title, method.window)))
96
+ return false;
97
+ return true;
98
+ }
99
+ function pickList(all, state, truncated, nth) {
100
+ const kept = all.filter((node) => deskNodeInState2(node, state));
101
+ if (kept.length === 0) {
102
+ return all.length > 0 ? { outcome: "notInteractable", nodes: all, matches: 0 } : { outcome: "none", matches: 0 };
103
+ }
104
+ if (nth !== undefined) {
105
+ const node = kept[nth];
106
+ return node ? { outcome: "found", node, matches: kept.length } : { outcome: "none", matches: 0 };
107
+ }
108
+ const [only] = kept;
109
+ return kept.length === 1 && only && !truncated ? { outcome: "found", node: only, matches: 1 } : { outcome: "ambiguous", nodes: kept, matches: kept.length };
110
+ }
111
+ function pickRanked(all, state, margin, truncated) {
112
+ const kept = all.filter((item) => deskNodeInState2(item.node, state)).sort((a, b) => b.score - a.score);
113
+ const [best, runnerUp] = kept;
114
+ if (!best) {
115
+ return all.length > 0 ? { outcome: "notInteractable", nodes: all.map((item) => item.node), matches: 0 } : { outcome: "none", matches: 0 };
116
+ }
117
+ if (!runnerUp || best.score - runnerUp.score >= margin) {
118
+ return truncated ? { outcome: "ambiguous", nodes: [best.node], matches: kept.length } : { outcome: "found", node: best.node, matches: kept.length };
119
+ }
120
+ const tied = kept.filter((item) => best.score - item.score < margin).map((item) => item.node);
121
+ return { outcome: "ambiguous", nodes: tied, matches: tied.length };
122
+ }
123
+ function anchorNodes(anchor, nodes) {
124
+ if (anchor.kind === "role") {
125
+ return nodes.filter((node) => rolesMatch2(anchor.role, node.role) && sameText2(node.name, anchor.name));
126
+ }
127
+ const exact = nodes.filter((node) => sameText2(node.name, anchor.text));
128
+ return exact.length > 0 ? exact : nodes.filter((node) => containsText2(node.name, anchor.text));
129
+ }
130
+ function isInside(node, ancestor, byRef) {
131
+ let current = node.parentRef === undefined ? undefined : byRef.get(node.parentRef);
132
+ for (let steps = 0;current && steps <= byRef.size; steps += 1) {
133
+ if (current.ref === ancestor.ref)
134
+ return true;
135
+ current = current.parentRef === undefined ? undefined : byRef.get(current.parentRef);
136
+ }
137
+ return false;
138
+ }
139
+ function hasArea(rect) {
140
+ return rect.width > 0 && rect.height > 0;
141
+ }
142
+ function matchAnchor(method, snapshot, state) {
143
+ const anchors = anchorNodes(method.anchor, snapshot.nodes);
144
+ if (anchors.length === 0)
145
+ return { outcome: "none", matches: 0 };
146
+ const candidates = snapshot.nodes.filter((node) => !anchors.includes(node) && (method.role === undefined || rolesMatch2(method.role, node.role)));
147
+ if (method.relation === "inside") {
148
+ const byRef = new Map(snapshot.nodes.map((node) => [node.ref, node]));
149
+ return pickList(candidates.filter((node) => anchors.some((anchor) => isInside(node, anchor, byRef))), state, snapshot.truncated);
150
+ }
151
+ const anchorRects = anchors.map((anchor) => webRectOf(anchor.bounds)).filter(hasArea);
152
+ const scored = [];
153
+ for (const node of candidates) {
154
+ const rect = webRectOf(node.bounds);
155
+ if (!hasArea(rect))
156
+ continue;
157
+ let best = null;
158
+ for (const anchorRect of anchorRects) {
159
+ const distance = relationDistance2(method.relation, anchorRect, rect);
160
+ if (distance !== null && (best === null || distance < best))
161
+ best = distance;
162
+ }
163
+ if (best !== null)
164
+ scored.push({ node, score: -best });
165
+ }
166
+ return pickRanked(scored, state, ANCHOR_MARGIN_PX2, snapshot.truncated);
167
+ }
168
+ function matchDeskTreeMethod2(method, snapshot, state) {
169
+ switch (method.kind) {
170
+ case "role": {
171
+ if (!windowMatches(method, snapshot))
172
+ return { outcome: "none", matches: 0 };
173
+ const all = snapshot.nodes.filter((node) => rolesMatch2(method.role, node.role) && sameText2(node.name, method.name));
174
+ return pickList(all, state, snapshot.truncated, method.nth);
175
+ }
176
+ case "fuzzy": {
177
+ const threshold = method.threshold ?? DEFAULT_FUZZY_THRESHOLD2;
178
+ const scored = [];
179
+ for (const node of snapshot.nodes) {
180
+ if (method.role !== undefined && !rolesMatch2(method.role, node.role))
181
+ continue;
182
+ const score = textSimilarity2(node.name, method.name);
183
+ if (score >= threshold)
184
+ scored.push({ node, score });
185
+ }
186
+ return pickRanked(scored, state, FUZZY_MARGIN2, snapshot.truncated);
187
+ }
188
+ case "anchor":
189
+ return matchAnchor(method, snapshot, state);
190
+ }
191
+ }
192
+
193
+ // src/drivers/desk-wire.ts
194
+ function isDeskWireTreeSnapshot2(value) {
195
+ if (value === null || typeof value !== "object" || !("format" in value) || !("nodes" in value))
196
+ return false;
197
+ return value.format === "desk-tree-v1" && Array.isArray(value.nodes);
198
+ }
199
+
200
+ // src/drivers/desk-driver.ts
201
+ var DESK_FIND_MAX2 = 10;
202
+ var DESK_FIND_ALL_MAX2 = 50;
203
+ var DEFAULT_DESK_SNAPSHOT_NODES2 = 2000;
204
+ var MAX_DESK_PAGE_TEXT = 1e6;
205
+ var TREE_METHODS = new Set(["role", "fuzzy", "anchor"]);
206
+ function isTreeMethod(method) {
207
+ return TREE_METHODS.has(method.kind);
208
+ }
209
+ function candidateOf(match, method, index) {
210
+ return {
211
+ role: clip2(match.role, MAX_CANDIDATE_NAME2),
212
+ name: clip2(match.name ?? "", MAX_CANDIDATE_NAME2),
213
+ bounds: webRectOf(match.bounds),
214
+ method: method.kind,
215
+ methodIndex: index
216
+ };
217
+ }
218
+ function roleForCss(target) {
219
+ const role = target.methods.find((method) => method.kind === "role" && method.enabled !== false);
220
+ return role?.role;
221
+ }
222
+ function deskTargetOfMethod2(method, target) {
223
+ switch (method.kind) {
224
+ case "uia":
225
+ case "ax":
226
+ case "atspi":
227
+ return {
228
+ by: "selector",
229
+ platform: method.kind,
230
+ ...method.app === undefined ? {} : { app: method.app },
231
+ ...method.window === undefined ? {} : { window: { ...method.window } },
232
+ path: method.path.map((node) => ({ ...node }))
233
+ };
234
+ case "css": {
235
+ const role = roleForCss(target);
236
+ return role === undefined ? undefined : { by: "selector", platform: "css", path: [{ role, css: method.selector }] };
237
+ }
238
+ case "testId": {
239
+ const role = roleForCss(target);
240
+ const attribute = method.attribute ?? "data-testid";
241
+ if (role === undefined || attribute !== "data-testid" && attribute !== "id")
242
+ return;
243
+ return { by: "selector", platform: "css", path: [{ role, automationId: method.value }] };
244
+ }
245
+ case "image":
246
+ return {
247
+ by: "image",
248
+ pngBase64: method.pngBase64,
249
+ ...method.threshold === undefined ? {} : { threshold: method.threshold },
250
+ ...method.region === undefined ? {} : { region: { ...method.region } },
251
+ ...method.recordedScale === undefined ? {} : { recordedScale: method.recordedScale }
252
+ };
253
+ case "text":
254
+ return {
255
+ by: "text",
256
+ text: method.text,
257
+ source: "accessibility",
258
+ match: method.exact === true ? "exact" : "contains",
259
+ ...method.nth === undefined ? {} : { occurrence: method.nth }
260
+ };
261
+ case "ocr":
262
+ return {
263
+ by: "text",
264
+ text: method.text,
265
+ source: "ocr",
266
+ match: method.match ?? "exact",
267
+ ...method.occurrence === undefined ? {} : { occurrence: method.occurrence },
268
+ ...method.region === undefined ? {} : { region: { ...method.region } }
269
+ };
270
+ default:
271
+ return;
272
+ }
273
+ }
274
+ function pickMatches(all, state, truncated) {
275
+ const kept = all.filter((match) => deskNodeInState2(match, state));
276
+ if (kept.length === 0)
277
+ return all.length > 0 ? { outcome: "notInteractable", matches: [...all] } : { outcome: "none" };
278
+ const [only] = kept;
279
+ if (kept.length === 1 && only && !truncated)
280
+ return { outcome: "found", match: only, matches: 1 };
281
+ return {
282
+ outcome: "ambiguous",
283
+ matches: kept,
284
+ count: truncated ? Math.max(kept.length, DESK_FIND_MAX2 + 1) : kept.length
285
+ };
286
+ }
287
+ function windowOf(window) {
288
+ return {
289
+ app: window.app,
290
+ title: window.title,
291
+ ...window.processName === undefined ? {} : { processName: window.processName },
292
+ state: window.state,
293
+ focused: window.focused,
294
+ bounds: { ...window.bounds }
295
+ };
296
+ }
297
+ function deskError(result, what) {
298
+ const error = result.error ?? { code: "DESK_OFFLINE", message: "The desk gave no answer" };
299
+ return new UiDriverError2(engineCodeOfDeskError2(error), `The desk refused ${what}: ${clip2(error.message, 300)}`, engineDetailOfDeskError2(error));
300
+ }
301
+ function outsideFolder(field) {
302
+ return new UiDriverError2("ACTION_FAILED", `The ${field} must be a path inside the workspace folder`, {
303
+ problem: "OUTSIDE_WORKSPACE_FOLDER"
304
+ });
305
+ }
306
+ function unsupported(what) {
307
+ return new UiDriverError2("UNSUPPORTED_ON_DESK", `A desk cannot ${what}`);
308
+ }
309
+ function readValueOf(value) {
310
+ if (value === undefined || value === null)
311
+ return null;
312
+ if (typeof value === "string" || typeof value === "boolean" || typeof value === "number")
313
+ return value;
314
+ return { ...value };
315
+ }
316
+ function textValueOf(value) {
317
+ if (value === undefined || value === null)
318
+ return null;
319
+ if (typeof value === "string" || typeof value === "boolean")
320
+ return value;
321
+ if (typeof value === "number")
322
+ return String(value);
323
+ return null;
324
+ }
325
+ function attributeOf(value, name) {
326
+ if (value === null || value === undefined || typeof value !== "object")
327
+ return null;
328
+ const isBounds = ["x", "y", "w", "h"].every((key) => typeof Reflect.get(value, key) === "number");
329
+ if (isBounds)
330
+ return null;
331
+ const attribute = Reflect.get(value, name);
332
+ return typeof attribute === "string" ? attribute : null;
333
+ }
334
+ var SECURE_ROLES = new Set(["AXSecureTextField", "password text"]);
335
+ function isSensitiveDeskMatch(match) {
336
+ if (SECURE_ROLES.has(match.role))
337
+ return true;
338
+ return rolesMatch2("textbox", match.role) && readsLikeSecret(match.name ?? "");
339
+ }
340
+ function sensitiveRead() {
341
+ return { ok: false, code: "SENSITIVE_ELEMENT" };
342
+ }
343
+ function createDeskDriver2(backend, options = {}) {
344
+ const requestedNodes = options.maxSnapshotNodes;
345
+ const maxNodes = Math.min(5000, Math.max(1, Math.floor(requestedNodes !== undefined && Number.isFinite(requestedNodes) ? requestedNodes : DEFAULT_DESK_SNAPSHOT_NODES2)));
346
+ const callContext = (context) => ({
347
+ stepId: context.stepId,
348
+ ...context.signal ? { signal: context.signal } : {},
349
+ ...context.timeoutMs === undefined ? {} : { timeoutMs: context.timeoutMs }
350
+ });
351
+ const has = (feature) => deskHasFeature2(backend.capabilities(), feature);
352
+ const run = async (action, context, what) => {
353
+ const result = await backend.act(action, callContext(context));
354
+ if (!result.ok)
355
+ throw deskError(result, what);
356
+ return result;
357
+ };
358
+ const runOn = async (action, context, what) => {
359
+ const result = await backend.act(action, callContext(context));
360
+ if (!result.ok && result.error?.code !== "TARGET_NOT_FOUND")
361
+ throw deskError(result, what);
362
+ return result;
363
+ };
364
+ const runOnWindow = async (action, context, what) => {
365
+ const result = await backend.act(action, callContext(context));
366
+ if (!result.ok && result.error?.code === "TARGET_NOT_FOUND") {
367
+ throw new UiDriverError2("WINDOW_NOT_FOUND", `The desk found no such application or window (${what})`, {
368
+ deskCode: "TARGET_NOT_FOUND"
369
+ });
370
+ }
371
+ if (!result.ok)
372
+ throw deskError(result, what);
373
+ return result;
374
+ };
375
+ const treeSnapshot = async (context) => {
376
+ const result = await backend.snapshot({ maxNodes }, callContext(context));
377
+ if (!result.ok) {
378
+ if (result.error?.code === "UNSUPPORTED_ON_DESK")
379
+ return;
380
+ throw deskError(result, "a snapshot");
381
+ }
382
+ return isDeskWireTreeSnapshot2(result.snapshot) ? result.snapshot : undefined;
383
+ };
384
+ const pickMethod = async (method, target, state, context, tree) => {
385
+ const features = deskFeaturesOfMethod2(method, target);
386
+ if (!features || !features.every(has))
387
+ return { outcome: "unsupported" };
388
+ if (isTreeMethod(method)) {
389
+ const snapshot = await tree();
390
+ if (!snapshot)
391
+ return { outcome: "unsupported" };
392
+ const picked = matchDeskTreeMethod2(method, snapshot, state);
393
+ switch (picked.outcome) {
394
+ case "found":
395
+ return { outcome: "found", match: picked.node, matches: picked.matches };
396
+ case "ambiguous":
397
+ return { outcome: "ambiguous", matches: picked.nodes, count: picked.matches };
398
+ case "notInteractable":
399
+ return { outcome: "notInteractable", matches: picked.nodes };
400
+ default:
401
+ return { outcome: "none" };
402
+ }
403
+ }
404
+ const deskTarget = deskTargetOfMethod2(method, target);
405
+ if (!deskTarget)
406
+ return { outcome: "unsupported" };
407
+ const result = await backend.find(deskTarget, callContext(context), DESK_FIND_MAX2);
408
+ if (!result.ok) {
409
+ const code = result.error?.code;
410
+ if (code === "TARGET_NOT_FOUND")
411
+ return { outcome: "none" };
412
+ if (code === "UNSUPPORTED_ON_DESK")
413
+ return { outcome: "unsupported" };
414
+ throw deskError(result, "a find");
415
+ }
416
+ return pickMatches(result.matches ?? [], state, result.truncated === true);
417
+ };
418
+ const resolveOnDesk = async (target, state, context) => {
419
+ const attempts = [];
420
+ const firstEnabled = target.methods.findIndex((method) => method.enabled !== false);
421
+ let snapshot;
422
+ const tree = () => snapshot ??= treeSnapshot(context);
423
+ let found;
424
+ let ambiguous;
425
+ let blocked;
426
+ for (const [index, method] of target.methods.entries()) {
427
+ const attempt = (outcome, matches = 0) => {
428
+ attempts.push({ index, method: method.kind, outcome, matches });
429
+ };
430
+ if (found) {
431
+ attempt("notTried");
432
+ continue;
433
+ }
434
+ if (method.enabled === false) {
435
+ attempt("disabled");
436
+ continue;
437
+ }
438
+ if (target.frame !== undefined) {
439
+ attempt("unsupported");
440
+ continue;
441
+ }
442
+ const picked = await pickMethod(method, target, state, context, tree);
443
+ switch (picked.outcome) {
444
+ case "found":
445
+ attempt("found", picked.matches);
446
+ found = { match: picked.match, method, index };
447
+ break;
448
+ case "ambiguous":
449
+ attempt("ambiguous", picked.count);
450
+ ambiguous ??= { matches: picked.matches, method, index, count: picked.count };
451
+ break;
452
+ case "notInteractable":
453
+ attempt("notInteractable");
454
+ blocked ??= { matches: picked.matches, method, index };
455
+ break;
456
+ default:
457
+ attempt(picked.outcome);
458
+ }
459
+ }
460
+ if (found) {
461
+ return {
462
+ match: found.match,
463
+ resolution: {
464
+ status: "found",
465
+ method: found.method.kind,
466
+ methodIndex: found.index,
467
+ fallback: found.index !== firstEnabled,
468
+ candidate: candidateOf(found.match, found.method, found.index),
469
+ attempts
470
+ }
471
+ };
472
+ }
473
+ const listed = (report) => report.matches.slice(0, MAX_CANDIDATES2).map((match) => candidateOf(match, report.method, report.index));
474
+ if (ambiguous) {
475
+ return {
476
+ resolution: {
477
+ status: "failed",
478
+ code: "ELEMENT_AMBIGUOUS",
479
+ candidates: listed(ambiguous),
480
+ candidateCount: ambiguous.count,
481
+ attempts
482
+ }
483
+ };
484
+ }
485
+ if (blocked) {
486
+ return {
487
+ resolution: {
488
+ status: "failed",
489
+ code: "ELEMENT_NOT_INTERACTABLE",
490
+ candidates: listed(blocked),
491
+ candidateCount: blocked.matches.length,
492
+ attempts
493
+ }
494
+ };
495
+ }
496
+ return { resolution: { status: "failed", code: "ELEMENT_NOT_FOUND", candidates: [], candidateCount: 0, attempts } };
497
+ };
498
+ const notResolved = (resolved) => ({
499
+ ok: false,
500
+ code: resolved.resolution.status === "failed" ? resolved.resolution.code : "ELEMENT_NOT_FOUND",
501
+ resolution: resolved.resolution
502
+ });
503
+ const onElement = async (target, state, context, what, action, allowed) => {
504
+ const resolved = await resolveOnDesk(target, state, context);
505
+ if (!resolved.match)
506
+ return notResolved(resolved);
507
+ if (allowed && !allowed(resolved.match))
508
+ return { ...sensitiveRead(), resolution: resolved.resolution };
509
+ const result = await runOn(action({ by: "ref", ref: resolved.match.ref }), context, what);
510
+ if (!result.ok)
511
+ return { ok: false, code: "ELEMENT_NOT_FOUND", resolution: resolved.resolution };
512
+ return { ok: true, result, resolution: resolved.resolution };
513
+ };
514
+ const usePattern = (context, patternAction) => {
515
+ const method = context.inputMethod ?? "auto";
516
+ if (method === "direct")
517
+ return true;
518
+ if (method === "hardware")
519
+ return false;
520
+ return has(`action:${patternAction}`);
521
+ };
522
+ const getOn = async (target, what, context, attributes) => {
523
+ const reveals = what === "text" || what === "value" || what === "attributes";
524
+ const done = await onElement(target, "visible", context, "a read", (ref) => ({
525
+ type: "get",
526
+ target: ref,
527
+ what,
528
+ ...attributes === undefined ? {} : { attributes: [...attributes] }
529
+ }), reveals ? (match) => !isSensitiveDeskMatch(match) : undefined);
530
+ return done.ok ? { ok: true, value: done.result.value, resolution: done.resolution } : done;
531
+ };
532
+ const readOn = async (target, read, context) => {
533
+ switch (read.kind) {
534
+ case "html":
535
+ throw unsupported("read HTML");
536
+ case "attribute": {
537
+ const got = await getOn(target, "attributes", context, [read.name]);
538
+ if (!got.ok)
539
+ return got;
540
+ return { ok: true, value: attributeOf(got.value, read.name), resolution: got.resolution };
541
+ }
542
+ case "auto": {
543
+ let got;
544
+ try {
545
+ got = await getOn(target, "value", context);
546
+ } catch (error) {
547
+ if (!(error instanceof UiDriverError2) || error.code !== "UNSUPPORTED_ON_DESK")
548
+ throw error;
549
+ }
550
+ if (got && !got.ok)
551
+ return got;
552
+ const value = got ? textValueOf(got.value) : null;
553
+ if (got && value !== null && value !== "")
554
+ return { ok: true, value, resolution: got.resolution };
555
+ const text = await getOn(target, "text", context);
556
+ if (!text.ok)
557
+ return text;
558
+ const read = textValueOf(text.value);
559
+ return { ok: true, value: read ?? value, resolution: text.resolution };
560
+ }
561
+ default: {
562
+ const got = await getOn(target, read.kind, context);
563
+ return got.ok ? { ok: true, value: textValueOf(got.value), resolution: got.resolution } : got;
564
+ }
565
+ }
566
+ };
567
+ const allMatches = async (target, context) => {
568
+ let snapshot;
569
+ const tree = () => snapshot ??= treeSnapshot(context);
570
+ for (const [index, method] of target.methods.entries()) {
571
+ if (method.enabled === false || target.frame !== undefined)
572
+ continue;
573
+ const features = deskFeaturesOfMethod2(method, target);
574
+ if (!features || !features.every(has))
575
+ continue;
576
+ let matches = [];
577
+ if (isTreeMethod(method)) {
578
+ const tree_ = await tree();
579
+ if (!tree_)
580
+ continue;
581
+ const picked = matchDeskTreeMethod2(method, tree_, "visible");
582
+ if (picked.outcome === "found")
583
+ matches = [picked.node];
584
+ else if (picked.outcome === "ambiguous")
585
+ matches = picked.nodes;
586
+ } else {
587
+ const deskTarget = deskTargetOfMethod2(method, target);
588
+ if (!deskTarget)
589
+ continue;
590
+ const result = await backend.find(deskTarget, callContext(context), DESK_FIND_ALL_MAX2);
591
+ if (!result.ok) {
592
+ if (result.error?.code === "TARGET_NOT_FOUND" || result.error?.code === "UNSUPPORTED_ON_DESK")
593
+ continue;
594
+ throw deskError(result, "a find");
595
+ }
596
+ matches = (result.matches ?? []).filter((match) => match.visible);
597
+ }
598
+ const [first] = matches;
599
+ if (!first)
600
+ continue;
601
+ return {
602
+ ok: true,
603
+ matches,
604
+ resolution: {
605
+ status: "found",
606
+ method: method.kind,
607
+ methodIndex: index,
608
+ fallback: index !== target.methods.findIndex((item) => item.enabled !== false),
609
+ candidate: candidateOf(first, method, index),
610
+ attempts: []
611
+ }
612
+ };
613
+ }
614
+ return { ok: false, code: "ELEMENT_NOT_FOUND" };
615
+ };
616
+ const windows = (result) => (result.windows ?? []).map(windowOf);
617
+ const pathChecked = (path, field) => {
618
+ if (!isWorkspaceRelativePath2(path))
619
+ throw outsideFolder(field);
620
+ return path;
621
+ };
622
+ const driver = {
623
+ desk: true,
624
+ capabilities: [],
625
+ ...options.clock ? { clock: options.clock } : {},
626
+ deskCapabilities: () => backend.capabilities(),
627
+ resolve: async (target, resolveOptions, context) => (await resolveOnDesk(target, resolveOptions.state, context)).resolution,
628
+ act: async (target, action, actOptions, context) => {
629
+ const state = actOptions.state ?? "interactable";
630
+ if (target === null) {
631
+ switch (action.kind) {
632
+ case "type":
633
+ await run({ type: "type", text: action.text }, context, "typing");
634
+ return { ok: true };
635
+ case "pressKey":
636
+ await run({ type: "key", keys: [action.key] }, context, "a key press");
637
+ return { ok: true };
638
+ default:
639
+ throw unsupported(`run ${action.kind} without a target`);
640
+ }
641
+ }
642
+ const element = async (what, build) => {
643
+ const done = await onElement(target, state, context, what, build);
644
+ return done.ok ? { ok: true, resolution: done.resolution } : done;
645
+ };
646
+ switch (action.kind) {
647
+ case "click": {
648
+ const button = action.button ?? "left";
649
+ if (button === "middle")
650
+ throw unsupported("click with the middle button");
651
+ if (button === "left" && usePattern(context, "invoke")) {
652
+ try {
653
+ return await element("a click", (ref) => ({ type: "invoke", target: ref }));
654
+ } catch (error) {
655
+ const auto = (context.inputMethod ?? "auto") === "auto";
656
+ if (!auto || !(error instanceof UiDriverError2) || error.code !== "UNSUPPORTED_ON_DESK" || !has("action:click")) {
657
+ throw error;
658
+ }
659
+ }
660
+ }
661
+ return element("a click", (ref) => ({
662
+ type: "click",
663
+ target: ref,
664
+ ...button === "right" ? { button } : {}
665
+ }));
666
+ }
667
+ case "doubleClick":
668
+ return element("a double click", (ref) => ({ type: "click", target: ref, count: 2 }));
669
+ case "type": {
670
+ if (action.append !== true && usePattern(context, "set_value")) {
671
+ if (action.text.length > MAX_DESK_SET_VALUE2) {
672
+ throw new UiDriverError2("ACTION_FAILED", `A desk sets at most ${MAX_DESK_SET_VALUE2} characters at once; type longer text with hardware input`, { problem: "INVALID_ACTION" });
673
+ }
674
+ return element("setting a value", (ref) => ({ type: "set_value", target: ref, value: action.text }));
675
+ }
676
+ if (action.append !== true) {
677
+ if (!has("action:set_value"))
678
+ throw unsupported("empty a field before typing without set_value");
679
+ const cleared = await element("clearing a field", (ref) => ({ type: "set_value", target: ref, value: "" }));
680
+ if (!cleared.ok)
681
+ return cleared;
682
+ }
683
+ return element("typing", (ref) => ({ type: "type", target: ref, text: action.text }));
684
+ }
685
+ case "clear":
686
+ return element("clearing a field", (ref) => ({ type: "set_value", target: ref, value: "" }));
687
+ case "check":
688
+ return element("a check", (ref) => ({ type: "toggle", target: ref, state: action.checked ? "on" : "off" }));
689
+ case "select": {
690
+ const item = action.value ?? action.label;
691
+ if (item === undefined)
692
+ throw unsupported("select an option by its position");
693
+ const done = await onElement(target, state, context, "a selection", (ref) => ({
694
+ type: "select",
695
+ target: ref,
696
+ item
697
+ }));
698
+ if (!done.ok)
699
+ return done.resolution?.status === "found" ? { ...done, code: "OPTION_NOT_FOUND" } : done;
700
+ return { ok: true, resolution: done.resolution };
701
+ }
702
+ case "pressKey": {
703
+ const clicked = await element("a click", (ref) => ({ type: "click", target: ref }));
704
+ if (!clicked.ok)
705
+ return clicked;
706
+ await run({ type: "key", keys: [action.key] }, context, "a key press");
707
+ return clicked;
708
+ }
709
+ case "scroll":
710
+ return element("a scroll", (ref) => ({
711
+ type: "scroll",
712
+ target: ref,
713
+ dx: action.dx ?? 0,
714
+ dy: action.dy ?? 0
715
+ }));
716
+ case "scrollIntoView":
717
+ return element("a scroll", (ref) => ({ type: "scroll", target: ref, dx: 0, dy: 0 }));
718
+ case "getText":
719
+ return readOn(target, { kind: "text" }, context);
720
+ case "getValue":
721
+ return readOn(target, { kind: "value" }, context);
722
+ case "getAttribute":
723
+ return readOn(target, { kind: "attribute", name: action.name }, context);
724
+ case "exists": {
725
+ const resolved = await resolveOnDesk(target, "visible", context);
726
+ return { ok: true, value: resolved.match !== undefined, resolution: resolved.resolution };
727
+ }
728
+ default:
729
+ throw unsupported(`run ${action.kind}`);
730
+ }
731
+ },
732
+ read: (target, read, context) => readOn(target, read, context),
733
+ readAll: async (target, read, context) => {
734
+ const found = await allMatches(target, context);
735
+ if (!found.ok)
736
+ return found;
737
+ if (read.kind === "html")
738
+ throw unsupported("read HTML");
739
+ const getRef = async (ref, what) => {
740
+ const result = await runOn({
741
+ type: "get",
742
+ target: { by: "ref", ref },
743
+ what,
744
+ ...read.kind === "attribute" ? { attributes: [read.name] } : {}
745
+ }, context, "a read");
746
+ return result.ok ? result.value : null;
747
+ };
748
+ const items = [];
749
+ for (const match of found.matches) {
750
+ if (isSensitiveDeskMatch(match))
751
+ items.push(null);
752
+ else if (read.kind === "attribute")
753
+ items.push(attributeOf(await getRef(match.ref, "attributes"), read.name));
754
+ else if (read.kind !== "auto")
755
+ items.push(textValueOf(await getRef(match.ref, read.kind)));
756
+ else {
757
+ const value = textValueOf(await getRef(match.ref, "value"));
758
+ items.push(value !== null && value !== "" ? value : textValueOf(await getRef(match.ref, "text")) ?? value);
759
+ }
760
+ }
761
+ return { ok: true, items, ...found.resolution ? { resolution: found.resolution } : {} };
762
+ },
763
+ listItems: async () => {
764
+ throw unsupported("read repeated items inside a list");
765
+ },
766
+ pressKeys: async (target, keys, context) => {
767
+ if (target === null) {
768
+ await run({ type: "key", keys: [...keys] }, context, "a key press");
769
+ return { ok: true };
770
+ }
771
+ const done = await onElement(target, "interactable", context, "a click", (ref) => ({
772
+ type: "click",
773
+ target: ref
774
+ }));
775
+ if (!done.ok)
776
+ return done;
777
+ await run({ type: "key", keys: [...keys] }, context, "a key press");
778
+ return { ok: true, resolution: done.resolution };
779
+ },
780
+ navigate: async (url, _navigateOptions, context) => {
781
+ await run({ type: "navigate", url }, context, "a navigation");
782
+ },
783
+ goBack: async () => {
784
+ throw unsupported("go back");
785
+ },
786
+ reload: async () => {
787
+ throw unsupported("reload a page");
788
+ },
789
+ closeTab: async () => {
790
+ throw unsupported("close a tab");
791
+ },
792
+ currentUrl: async () => {
793
+ throw unsupported("report the page address");
794
+ },
795
+ pageHtml: async () => {
796
+ throw unsupported("return page HTML");
797
+ },
798
+ pageText: async (context) => {
799
+ const snapshot = await treeSnapshot(context);
800
+ if (!snapshot)
801
+ throw unsupported("read the screen text");
802
+ const lines = [];
803
+ let length = 0;
804
+ for (const node of snapshot.nodes) {
805
+ if (!node.visible)
806
+ continue;
807
+ for (const text of [node.name, isSensitiveDeskMatch(node) ? undefined : node.value]) {
808
+ if (text === undefined || text.length === 0 || length >= MAX_DESK_PAGE_TEXT)
809
+ continue;
810
+ lines.push(text);
811
+ length += text.length + 1;
812
+ }
813
+ }
814
+ return lines.join(`
815
+ `).slice(0, MAX_DESK_PAGE_TEXT);
816
+ },
817
+ containsText: async (text, context) => {
818
+ if (text.length > MAX_DESK_TEXT_TARGET2)
819
+ return containsText2(await driver.pageText(context), text);
820
+ const result = await backend.find({ by: "text", text, match: "contains" }, callContext(context), DESK_FIND_MAX2);
821
+ if (!result.ok) {
822
+ if (result.error?.code === "TARGET_NOT_FOUND")
823
+ return false;
824
+ throw deskError(result, "a text search");
825
+ }
826
+ return (result.matches ?? []).some((match) => match.visible);
827
+ },
828
+ scrollPage: async (scrollOptions, context) => {
829
+ if (scrollOptions.to !== undefined)
830
+ throw unsupported("scroll to the top or the end");
831
+ await run({ type: "scroll", dx: scrollOptions.dx ?? 0, dy: scrollOptions.dy ?? 0 }, context, "a scroll");
832
+ },
833
+ typeSecret: async (target, secret, context) => {
834
+ const secretRef = options.secretRefFor ? options.secretRefFor(context.stepId) : secret.secretRef;
835
+ if (secretRef === undefined || secretRef.length === 0) {
836
+ throw new UiDriverError2("SECRET_UNAVAILABLE", "The desk has no secret reference for this step");
837
+ }
838
+ const done = await onElement(target, "interactable", context, "typing a secret", (ref) => ({
839
+ type: "secret_fill",
840
+ target: ref,
841
+ secretRef,
842
+ expectedOrigin: secret.expectedOrigin ?? ""
843
+ }));
844
+ return done.ok ? { ok: true, resolution: done.resolution } : done;
845
+ },
846
+ screenshot: async (screenshotOptions, context) => {
847
+ if (screenshotOptions.target !== undefined || screenshotOptions.fullPage === true) {
848
+ throw unsupported("take an element or full-page screenshot");
849
+ }
850
+ if (!options.storeScreenshot)
851
+ return null;
852
+ const result = await run({ type: "screenshot" }, context, "a screenshot");
853
+ return result.screenshotPng ? options.storeScreenshot(result.screenshotPng, context) : null;
854
+ },
855
+ focusWindow: async (app, title, context) => {
856
+ await runOnWindow({ type: "focus_window", app, ...title === undefined ? {} : { title } }, context, "focusing a window");
857
+ },
858
+ openApp: async (app, openOptions, context) => windows(await runOnWindow({
859
+ type: "open_app",
860
+ app,
861
+ ...openOptions.args === undefined ? {} : { args: [...openOptions.args] },
862
+ ...openOptions.waitForTitle === undefined ? {} : { waitForTitle: openOptions.waitForTitle }
863
+ }, context, "opening an application")),
864
+ closeApp: async (app, title, context) => {
865
+ await runOnWindow({ type: "close_app", app, ...title === undefined ? {} : { title } }, context, "closing an application");
866
+ },
867
+ windowState: async (app, title, state, context) => windows(await runOnWindow({ type: "window_state", app, state, ...title === undefined ? {} : { title } }, context, "changing a window")),
868
+ listWindows: async (app, context) => windows(await run({ type: "list_windows", ...app === undefined ? {} : { app } }, context, "listing windows")),
869
+ pattern: async (target, action, context) => {
870
+ if (action.kind === "get") {
871
+ const got = await getOn(target, action.what, context, action.attributes);
872
+ return got.ok ? { ok: true, value: readValueOf(got.value), resolution: got.resolution } : got;
873
+ }
874
+ const done = await onElement(target, "interactable", context, `the ${action.kind} action`, (ref) => {
875
+ switch (action.kind) {
876
+ case "invoke":
877
+ return { type: "invoke", target: ref };
878
+ case "setValue":
879
+ return { type: "set_value", target: ref, value: action.value };
880
+ case "toggle":
881
+ return { type: "toggle", target: ref, ...action.state === undefined ? {} : { state: action.state } };
882
+ default:
883
+ return { type: "expand", target: ref, ...action.state === undefined ? {} : { state: action.state } };
884
+ }
885
+ });
886
+ if (!done.ok)
887
+ return done;
888
+ return {
889
+ ok: true,
890
+ resolution: done.resolution,
891
+ ...action.kind === "toggle" && done.result.value !== undefined ? { value: readValueOf(done.result.value) } : {}
892
+ };
893
+ },
894
+ runCommand: async (command, runOptions, context) => {
895
+ const cwd = runOptions.cwd === undefined ? undefined : pathChecked(runOptions.cwd, "working folder");
896
+ const result = await run({ type: "shell", command, timeoutMs: runOptions.timeoutMs, ...cwd === undefined ? {} : { cwd } }, context, "running a command");
897
+ return result.text ?? "";
898
+ },
899
+ readFile: async (path, maxBytes, context) => {
900
+ const result = await run({ type: "read_file", path: pathChecked(path, "path"), maxBytes }, context, "reading a file");
901
+ return result.text ?? "";
902
+ },
903
+ writeFile: async (path, content, context) => {
904
+ await run({ type: "write_file", path: pathChecked(path, "path"), content }, context, "writing a file");
905
+ }
906
+ };
907
+ return driver;
908
+ }
909
+ // src/drivers/desk-backend.ts
910
+ function deskBackendFromAct2(options) {
911
+ const act = options.act;
912
+ return {
913
+ capabilities: () => options.capabilities(),
914
+ act: (action, context) => act(action, context),
915
+ find: (target, context, max) => act({ type: "find", target, ...max === undefined ? {} : { max } }, context),
916
+ snapshot: (snapshotOptions, context) => act({ type: "snapshot", ...snapshotOptions }, context),
917
+ get: (target, what, context, attributes) => act({ type: "get", target, what, ...attributes === undefined ? {} : { attributes: [...attributes] } }, context)
918
+ };
919
+ }
920
+ export { engineCodeOfDeskError2, engineDetailOfDeskError2, deskNodeInState2, matchDeskTreeMethod2, isDeskWireTreeSnapshot2, DESK_FIND_MAX2, DESK_FIND_ALL_MAX2, DEFAULT_DESK_SNAPSHOT_NODES2, deskTargetOfMethod2, createDeskDriver2, deskBackendFromAct2 };