@burdenoff/ui-engine 2026.1127.3 → 2026.1127.4

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 (40) hide show
  1. package/README.md +45 -5
  2. package/dist/dom-runtime.iife.js +1 -1
  3. package/dist/dom-runtime.js +27 -532
  4. package/dist/drivers/dom-driver.d.ts +44 -0
  5. package/dist/drivers/index.d.ts +7 -0
  6. package/dist/drivers/snapshot-driver.d.ts +59 -0
  7. package/dist/drivers.d.ts +2 -0
  8. package/dist/drivers.js +170 -0
  9. package/dist/index-3f1j8386.js +334 -0
  10. package/dist/index-3jymbm5t.js +315 -0
  11. package/dist/{index-q1b0j9yr.js → index-5k26keah.js} +58 -592
  12. package/dist/index-77pcz6n3.js +303 -0
  13. package/dist/index-895kscx3.js +741 -0
  14. package/dist/index-ct2xctrq.js +229 -0
  15. package/dist/index-h88y5qgv.js +400 -0
  16. package/dist/{index-zwrxw3zp.js → index-pm13mvvc.js} +24 -1009
  17. package/dist/index-vg8r40y7.js +220 -0
  18. package/dist/index-ycs2bz1a.js +589 -0
  19. package/dist/index-zzhysbkj.js +1731 -0
  20. package/dist/index.d.ts +1 -0
  21. package/dist/index.js +123 -37
  22. package/dist/interpret/compile.d.ts +58 -0
  23. package/dist/interpret/driver.d.ts +171 -0
  24. package/dist/interpret/errors.d.ts +194 -0
  25. package/dist/interpret/events.d.ts +73 -0
  26. package/dist/interpret/expr.d.ts +35 -0
  27. package/dist/interpret/index.d.ts +13 -0
  28. package/dist/interpret/run.d.ts +77 -0
  29. package/dist/interpret.d.ts +2 -0
  30. package/dist/interpret.js +84 -0
  31. package/dist/script/adapters/browser-action-contract.d.ts +5 -2
  32. package/dist/script/limits.d.ts +2 -0
  33. package/dist/script.js +44 -38
  34. package/dist/target/index.d.ts +1 -1
  35. package/dist/target/resolve.d.ts +18 -1
  36. package/dist/target.js +23 -19
  37. package/dist/testing/fake-web-driver.d.ts +28 -0
  38. package/dist/testing/index.d.ts +3 -2
  39. package/dist/testing.js +55 -0
  40. package/package.json +11 -3
@@ -0,0 +1,229 @@
1
+ import {
2
+ READ_ACTIONS2,
3
+ POINTER_ACTIONS2,
4
+ performDomAction2
5
+ } from "./index-77pcz6n3.js";
6
+ import {
7
+ resolveWebTarget2,
8
+ serializeTreeSnapshot2
9
+ } from "./index-5k26keah.js";
10
+
11
+ // src/dom-runtime/runtime.ts
12
+ var DOM_RUNTIME_VERSION2 = 1;
13
+ var DOM_RUNTIME_GLOBAL2 = "__burdenoffUiRuntime";
14
+ var MAX_HIGHLIGHT_MS2 = 60000;
15
+ var HIGHLIGHT_HOST_TAG2 = "fg-ui-highlight";
16
+ var METHOD_KINDS = new Set(["role", "css", "testId", "xpath", "fuzzy", "anchor", "text"]);
17
+ function isRuntimeTarget2(value) {
18
+ if (!value || typeof value !== "object")
19
+ return false;
20
+ const target = value;
21
+ if (!Array.isArray(target.methods) || target.methods.length === 0 || target.methods.length > 8)
22
+ return false;
23
+ if (target.frame !== undefined && !(Array.isArray(target.frame) && target.frame.every((item) => typeof item === "string"))) {
24
+ return false;
25
+ }
26
+ return target.methods.every((method) => !!method && typeof method === "object" && METHOD_KINDS.has(String(method.kind)));
27
+ }
28
+ function invalidTarget() {
29
+ return { status: "failed", code: "ELEMENT_NOT_FOUND", candidates: [], candidateCount: 0, attempts: [] };
30
+ }
31
+ function withoutElement(found) {
32
+ return {
33
+ status: found.status,
34
+ method: found.method,
35
+ methodIndex: found.methodIndex,
36
+ fallback: found.fallback,
37
+ candidate: found.candidate,
38
+ attempts: found.attempts
39
+ };
40
+ }
41
+ function frameElementOf(document) {
42
+ try {
43
+ return document.defaultView?.frameElement ?? null;
44
+ } catch {
45
+ return null;
46
+ }
47
+ }
48
+ function coveredAt(current, x, y, labels) {
49
+ const root = current.getRootNode();
50
+ if (typeof root.elementFromPoint !== "function")
51
+ return null;
52
+ const top = root.elementFromPoint(x, y);
53
+ if (!top)
54
+ return "offscreen";
55
+ if (top === current || current.contains(top))
56
+ return null;
57
+ return labels.some((label) => label.contains(top)) ? null : "covered";
58
+ }
59
+ function coverage(element, rect) {
60
+ let x = rect.x + rect.width / 2;
61
+ let y = rect.y + rect.height / 2;
62
+ const labels = Array.from(element.labels ?? []);
63
+ let current = element;
64
+ for (let hop = 0;hop < 32; hop += 1) {
65
+ const blocked = coveredAt(current, x, y, hop === 0 ? labels : []);
66
+ if (blocked)
67
+ return blocked;
68
+ const root = current.getRootNode();
69
+ if (root && "host" in root) {
70
+ current = root.host;
71
+ continue;
72
+ }
73
+ const frame = frameElementOf(current.ownerDocument);
74
+ if (!frame)
75
+ return null;
76
+ const box = frame.getBoundingClientRect();
77
+ x += box.x + frame.clientLeft;
78
+ y += box.y + frame.clientTop;
79
+ current = frame;
80
+ }
81
+ return null;
82
+ }
83
+ function outsideViewport(element, rect) {
84
+ const view = element.ownerDocument.defaultView;
85
+ if (!view)
86
+ return false;
87
+ const x = rect.x + rect.width / 2;
88
+ const y = rect.y + rect.height / 2;
89
+ return x < 0 || y < 0 || x >= view.innerWidth || y >= view.innerHeight;
90
+ }
91
+ function pointerBlock(element) {
92
+ const view = element.ownerDocument.defaultView;
93
+ if (view && view.getComputedStyle(element).pointerEvents === "none")
94
+ return "noPointerEvents";
95
+ let rect = element.getBoundingClientRect();
96
+ if (rect.width <= 0 || rect.height <= 0)
97
+ return null;
98
+ if (outsideViewport(element, rect) && typeof element.scrollIntoView === "function") {
99
+ element.scrollIntoView({ block: "center", inline: "center" });
100
+ rect = element.getBoundingClientRect();
101
+ }
102
+ return coverage(element, rect);
103
+ }
104
+ function createDomRuntime2(document, options = {}) {
105
+ const root = options.root ?? document;
106
+ const probe = {
107
+ ...options.rectOf ? { rectOf: options.rectOf } : {},
108
+ ...options.isVisible ? { isVisible: options.isVisible } : {}
109
+ };
110
+ let overlay = null;
111
+ let clearTimer = null;
112
+ const clearHighlight = () => {
113
+ if (clearTimer !== null) {
114
+ clearTimeout(clearTimer);
115
+ clearTimer = null;
116
+ }
117
+ overlay?.remove();
118
+ overlay = null;
119
+ };
120
+ const resolveRaw = (target, state) => {
121
+ clearHighlight();
122
+ return resolveWebTarget2(root, target, { ...probe, ...state ? { state } : {} });
123
+ };
124
+ const resolve = (target, resolveOptions = {}) => {
125
+ if (!isRuntimeTarget2(target))
126
+ return invalidTarget();
127
+ const result = resolveRaw(target, resolveOptions.state);
128
+ return result.status === "found" ? withoutElement(result) : result;
129
+ };
130
+ const act = (target, action, actOptions = {}) => {
131
+ if (!action || typeof action !== "object" || typeof action.kind !== "string") {
132
+ return { ok: false, code: "INVALID_ACTION" };
133
+ }
134
+ if (!isRuntimeTarget2(target)) {
135
+ return { ok: false, code: "ELEMENT_NOT_FOUND", resolution: invalidTarget() };
136
+ }
137
+ const state = actOptions.state ?? (READ_ACTIONS2.has(action.kind) ? "visible" : "interactable");
138
+ const result = resolveRaw(target, state);
139
+ if (result.status !== "found") {
140
+ if (action.kind === "exists" && result.code === "ELEMENT_NOT_FOUND") {
141
+ return { ok: true, value: false, resolution: result };
142
+ }
143
+ return { ok: false, code: result.code, resolution: result };
144
+ }
145
+ const resolution = withoutElement(result);
146
+ const blocked = POINTER_ACTIONS2.has(action.kind) ? pointerBlock(result.element) : null;
147
+ if (blocked) {
148
+ return {
149
+ ok: false,
150
+ code: "ELEMENT_NOT_INTERACTABLE",
151
+ reason: blocked,
152
+ resolution: {
153
+ status: "failed",
154
+ code: "ELEMENT_NOT_INTERACTABLE",
155
+ candidates: [result.candidate],
156
+ candidateCount: 1,
157
+ attempts: result.attempts
158
+ }
159
+ };
160
+ }
161
+ const outcome = performDomAction2(result.element, action);
162
+ if (!outcome.ok)
163
+ return { ok: false, code: outcome.problem, resolution };
164
+ return outcome.value === undefined ? { ok: true, resolution } : { ok: true, resolution, value: outcome.value };
165
+ };
166
+ const drawBoxes = (boxes, label, numbered) => {
167
+ const host = document.createElement(HIGHLIGHT_HOST_TAG2);
168
+ host.setAttribute("aria-hidden", "true");
169
+ host.setAttribute("style", "all: initial; position: fixed; inset: 0; pointer-events: none; z-index: 2147483647;");
170
+ const shadow = host.attachShadow({ mode: "closed" });
171
+ boxes.forEach((box, index) => {
172
+ const frame = document.createElement("div");
173
+ frame.setAttribute("style", `position: fixed; left: ${box.bounds.x - 3}px; top: ${box.bounds.y - 3}px; width: ${box.bounds.width + 6}px; height: ${box.bounds.height + 6}px; outline: 3px solid Highlight; border-radius: 4px; box-sizing: border-box;`);
174
+ const caption = numbered ? `${index + 1}${label ? ` ${label}` : ""}` : label;
175
+ if (caption) {
176
+ const tag = document.createElement("span");
177
+ tag.textContent = caption.slice(0, 120);
178
+ tag.setAttribute("style", "position: absolute; left: 0; top: 100%; margin-top: 4px; padding: 2px 6px; font: 600 11px system-ui, sans-serif; background: Highlight; color: HighlightText; border-radius: 3px; white-space: nowrap;");
179
+ frame.appendChild(tag);
180
+ }
181
+ shadow.appendChild(frame);
182
+ });
183
+ document.documentElement.appendChild(host);
184
+ overlay = host;
185
+ };
186
+ const highlight = (target, highlightOptions = {}) => {
187
+ if (!isRuntimeTarget2(target))
188
+ return invalidTarget();
189
+ const result = resolveRaw(target, highlightOptions.state ?? "visible");
190
+ if (result.status === "found")
191
+ drawBoxes([result.candidate], highlightOptions.label, false);
192
+ else if (result.candidates.length > 0)
193
+ drawBoxes(result.candidates, highlightOptions.label, true);
194
+ const duration = Math.min(MAX_HIGHLIGHT_MS2, Math.max(0, Number(highlightOptions.durationMs ?? 0) || 0));
195
+ if (overlay && duration > 0)
196
+ clearTimer = setTimeout(clearHighlight, duration);
197
+ return result.status === "found" ? withoutElement(result) : result;
198
+ };
199
+ return {
200
+ version: DOM_RUNTIME_VERSION2,
201
+ resolve,
202
+ act,
203
+ highlight,
204
+ clearHighlight,
205
+ snapshot: (snapshotOptions = {}) => {
206
+ clearHighlight();
207
+ return serializeTreeSnapshot2(document, {
208
+ ...probe,
209
+ ...snapshotOptions.maxNodes ? { maxNodes: snapshotOptions.maxNodes } : {}
210
+ });
211
+ }
212
+ };
213
+ }
214
+ function installDomRuntime2(scope, document) {
215
+ const existing = Reflect.get(scope, DOM_RUNTIME_GLOBAL2);
216
+ if (existing && typeof existing === "object" && typeof existing.version === "number" && existing.version >= DOM_RUNTIME_VERSION2) {
217
+ return existing;
218
+ }
219
+ const runtime = Object.freeze(createDomRuntime2(document));
220
+ Object.defineProperty(scope, DOM_RUNTIME_GLOBAL2, {
221
+ value: runtime,
222
+ configurable: true,
223
+ enumerable: false,
224
+ writable: false
225
+ });
226
+ return runtime;
227
+ }
228
+
229
+ export { DOM_RUNTIME_VERSION2, DOM_RUNTIME_GLOBAL2, MAX_HIGHLIGHT_MS2, HIGHLIGHT_HOST_TAG2, isRuntimeTarget2, createDomRuntime2, installDomRuntime2 };
@@ -0,0 +1,400 @@
1
+ import {
2
+ sha256Hex2
3
+ } from "./index-cs6acyjn.js";
4
+ import {
5
+ NAME_PATTERN2,
6
+ STEP_ID_PATTERN2,
7
+ ELEMENT_KEY_PATTERN2,
8
+ uiTargetSchema2,
9
+ withFingerprint2,
10
+ UiScriptError2,
11
+ parseUiScript2
12
+ } from "./index-895kscx3.js";
13
+ import {
14
+ UiRunError2
15
+ } from "./index-vg8r40y7.js";
16
+ import {
17
+ walkSteps2,
18
+ requiredCapabilities2,
19
+ missingCapabilities2
20
+ } from "./index-3jymbm5t.js";
21
+ // src/interpret/compile.ts
22
+ var MAX_CALL_DEPTH2 = 4;
23
+ var COMPILED_NAME_PREFIX2 = "__c";
24
+ function invalid(message, detail) {
25
+ throw new UiRunError2("INVALID_SCRIPT", message, detail);
26
+ }
27
+ function parsedTarget(value, key) {
28
+ const parsed = uiTargetSchema2.safeParse(value);
29
+ if (!parsed.success)
30
+ invalid(`The resolved target of element "${key}" is not a valid UI target`, { key });
31
+ return parsed.data;
32
+ }
33
+ function applyPins2(script, resolved) {
34
+ if (!script.elements)
35
+ return script;
36
+ const byKey = new Map(resolved.map((element) => [element.key, element]));
37
+ const elements = {};
38
+ for (const [key, pin] of Object.entries(script.elements)) {
39
+ const found = byKey.get(key);
40
+ if (!found)
41
+ invalid(`The element "${key}" was not resolved`, { key });
42
+ if (found.elementId !== pin.elementId || found.version !== pin.version) {
43
+ invalid(`The element "${key}" was resolved to a different element or version`, { key });
44
+ }
45
+ elements[key] = {
46
+ ...pin,
47
+ ...pin.name === undefined && found.name ? { name: found.name.slice(0, 512) } : {},
48
+ target: parsedTarget(found.target, key)
49
+ };
50
+ }
51
+ return { ...script, elements };
52
+ }
53
+ function stripBreakpoints2(script) {
54
+ const strip = (steps) => steps.map((step) => {
55
+ const copy = { ...step };
56
+ delete copy.breakpoint;
57
+ return mapBlocks(copy, strip);
58
+ });
59
+ return { ...script, steps: strip(script.steps), ...script.cleanup ? { cleanup: strip(script.cleanup) } : {} };
60
+ }
61
+ function mapBlocks(step, map) {
62
+ switch (step.action) {
63
+ case "if":
64
+ return { ...step, then: map(step.then), ...step.else ? { else: map(step.else) } : {} };
65
+ case "forEach":
66
+ case "while":
67
+ case "retryScope":
68
+ return { ...step, steps: map(step.steps) };
69
+ default:
70
+ return step;
71
+ }
72
+ }
73
+ function prefixed(prefix, name) {
74
+ const full = `${prefix}${name}`;
75
+ return NAME_PATTERN2.test(full) ? full : `${prefix}${sha256Hex2(name).slice(0, 12)}`;
76
+ }
77
+ function prefixedKey(prefix, key) {
78
+ const full = `${prefix}${key}`;
79
+ return ELEMENT_KEY_PATTERN2.test(full) ? full : `${prefix}${sha256Hex2(key).slice(0, 12)}`;
80
+ }
81
+ function renameExpression(expression, renames) {
82
+ if (expression === null || typeof expression !== "object")
83
+ return expression;
84
+ if ("input" in expression) {
85
+ return { var: renames.argument(expression.input), ...expression.path ? { path: expression.path } : {} };
86
+ }
87
+ if ("var" in expression) {
88
+ return { var: renames.variable(expression.var), ...expression.path ? { path: expression.path } : {} };
89
+ }
90
+ if ("list" in expression)
91
+ return { list: expression.list.map((item) => renameExpression(item, renames)) };
92
+ if ("object" in expression) {
93
+ return {
94
+ object: Object.fromEntries(Object.entries(expression.object).map(([key, item]) => [key, renameExpression(item, renames)]))
95
+ };
96
+ }
97
+ return { op: expression.op, args: expression.args.map((arg) => renameExpression(arg, renames)) };
98
+ }
99
+ function renameRef(ref, renames) {
100
+ if (ref && "element" in ref)
101
+ return { element: renames.element(ref.element) };
102
+ return ref;
103
+ }
104
+ function renameVerify(verify, renames) {
105
+ if (!verify)
106
+ return;
107
+ switch (verify.kind) {
108
+ case "elementVisible":
109
+ case "elementGone":
110
+ return { ...verify, target: renameRef(verify.target, renames) };
111
+ case "valueEquals":
112
+ return {
113
+ ...verify,
114
+ target: renameRef(verify.target, renames),
115
+ value: renameExpression(verify.value, renames)
116
+ };
117
+ case "urlContains":
118
+ return { ...verify, value: renameExpression(verify.value, renames) };
119
+ case "textPresent":
120
+ return { ...verify, text: renameExpression(verify.text, renames) };
121
+ }
122
+ }
123
+ var expr = (value, renames) => value === undefined ? undefined : renameExpression(value, renames);
124
+ function renameStep(step, renames) {
125
+ const common = {
126
+ id: renames.stepId(step.id),
127
+ ...step.saveAs === undefined ? {} : { saveAs: renames.variable(step.saveAs) },
128
+ ...step.verify ? { verify: renameVerify(step.verify, renames) } : {}
129
+ };
130
+ const children = (steps) => steps.map((child) => renameStep(child, renames));
131
+ const fields = (list) => list.map((field) => ({ ...field, target: renameRef(field.target, renames) }));
132
+ let renamed;
133
+ switch (step.action) {
134
+ case "navigate":
135
+ renamed = { ...step, ...common, url: renameExpression(step.url, renames) };
136
+ break;
137
+ case "type":
138
+ renamed = {
139
+ ...step,
140
+ ...common,
141
+ ...step.target ? { target: renameRef(step.target, renames) } : {},
142
+ ...step.text === undefined ? {} : { text: renameExpression(step.text, renames) }
143
+ };
144
+ break;
145
+ case "select":
146
+ renamed = {
147
+ ...step,
148
+ ...common,
149
+ target: renameRef(step.target, renames),
150
+ values: step.values.map((value) => renameExpression(value, renames))
151
+ };
152
+ break;
153
+ case "waitFor":
154
+ renamed = {
155
+ ...step,
156
+ ...common,
157
+ ...step.target ? { target: renameRef(step.target, renames) } : {},
158
+ ...step.text === undefined ? {} : { text: expr(step.text, renames) },
159
+ ...step.urlContains === undefined ? {} : { urlContains: expr(step.urlContains, renames) }
160
+ };
161
+ break;
162
+ case "extract":
163
+ renamed = { ...step, ...common, fields: fields(step.fields) };
164
+ break;
165
+ case "extractList":
166
+ renamed = {
167
+ ...step,
168
+ ...common,
169
+ item: renameRef(step.item, renames),
170
+ fields: fields(step.fields),
171
+ ...step.next ? { next: renameRef(step.next, renames) } : {}
172
+ };
173
+ break;
174
+ case "login":
175
+ renamed = {
176
+ ...step,
177
+ ...common,
178
+ ...step.username ? { username: renameRef(step.username, renames) } : {},
179
+ ...step.password ? { password: renameRef(step.password, renames) } : {},
180
+ ...step.submit ? { submit: renameRef(step.submit, renames) } : {},
181
+ ...step.success ? { success: renameRef(step.success, renames) } : {}
182
+ };
183
+ break;
184
+ case "if":
185
+ renamed = {
186
+ ...step,
187
+ ...common,
188
+ ...step.condition === undefined ? {} : { condition: expr(step.condition, renames) },
189
+ ...step.elementExists ? { elementExists: renameRef(step.elementExists, renames) } : {},
190
+ then: children(step.then),
191
+ ...step.else ? { else: children(step.else) } : {}
192
+ };
193
+ break;
194
+ case "while":
195
+ renamed = {
196
+ ...step,
197
+ ...common,
198
+ ...step.condition === undefined ? {} : { condition: expr(step.condition, renames) },
199
+ ...step.elementExists ? { elementExists: renameRef(step.elementExists, renames) } : {},
200
+ steps: children(step.steps)
201
+ };
202
+ break;
203
+ case "forEach":
204
+ renamed = {
205
+ ...step,
206
+ ...common,
207
+ ...step.in === undefined ? {} : { in: expr(step.in, renames) },
208
+ ...step.elements ? { elements: renameRef(step.elements, renames) } : {},
209
+ as: renames.variable(step.as),
210
+ ...step.index === undefined ? {} : { index: renames.variable(step.index) },
211
+ steps: children(step.steps)
212
+ };
213
+ break;
214
+ case "retryScope":
215
+ renamed = { ...step, ...common, steps: children(step.steps) };
216
+ break;
217
+ case "set":
218
+ renamed = { ...step, ...common, name: renames.variable(step.name), value: renameExpression(step.value, renames) };
219
+ break;
220
+ case "log":
221
+ renamed = { ...step, ...common, message: renameExpression(step.message, renames) };
222
+ break;
223
+ case "call":
224
+ renamed = {
225
+ ...step,
226
+ ...common,
227
+ ...step.args ? {
228
+ args: Object.fromEntries(Object.entries(step.args).map(([name, value]) => [name, renameExpression(value, renames)]))
229
+ } : {}
230
+ };
231
+ break;
232
+ default:
233
+ renamed = "target" in step && step.target ? { ...step, ...common, target: renameRef(step.target, renames) } : { ...step, ...common };
234
+ break;
235
+ }
236
+ return step.fingerprint === undefined ? renamed : withFingerprint2(renamed);
237
+ }
238
+ function assertNoReservedNames(script) {
239
+ const reserved = (name) => name !== undefined && name.startsWith(COMPILED_NAME_PREFIX2);
240
+ const names = [
241
+ ...(script.inputs ?? []).map((input) => input.name),
242
+ ...(script.variables ?? []).map((variable) => variable.name),
243
+ ...Object.keys(script.elements ?? {})
244
+ ];
245
+ for (const { step } of walkSteps2(script)) {
246
+ names.push(step.saveAs ?? "");
247
+ if (step.action === "set")
248
+ names.push(step.name);
249
+ if (step.action === "forEach")
250
+ names.push(step.as, step.index ?? "");
251
+ }
252
+ const clash = names.find(reserved);
253
+ if (clash !== undefined) {
254
+ invalid(`The name "${clash}" starts with "${COMPILED_NAME_PREFIX2}", which is reserved for inlined shared sequences`);
255
+ }
256
+ }
257
+ function inlineCalls2(script, sequences) {
258
+ const hasCall = Array.from(walkSteps2(script)).some(({ step }) => step.action === "call");
259
+ if (!hasCall)
260
+ return { script, calls: [] };
261
+ assertNoReservedNames(script);
262
+ const bySequence = new Map(sequences.map((sequence) => [`${sequence.sequenceId}@${sequence.version}`, sequence]));
263
+ const usedIds = new Set(Array.from(walkSteps2(script)).map(({ step }) => step.id));
264
+ const elements = { ...script.elements };
265
+ const calls = [];
266
+ let counter = 0;
267
+ const uniqueId = (wanted) => {
268
+ let id = STEP_ID_PATTERN2.test(wanted) ? wanted : `s-${sha256Hex2(wanted).slice(0, 12)}`;
269
+ for (let n = 2;usedIds.has(id); n += 1)
270
+ id = `s-${sha256Hex2(`${wanted}#${n}`).slice(0, 12)}`;
271
+ usedIds.add(id);
272
+ return id;
273
+ };
274
+ const inlineList = (steps, stack) => steps.map((step) => step.action === "call" ? inlineOne(step, stack) : mapBlocks(step, (children) => inlineList(children, stack)));
275
+ const inlineOne = (call, stack) => {
276
+ const ref = `${call.sequenceId}@${call.version}`;
277
+ if (stack.includes(ref))
278
+ invalid(`The shared sequence ${ref} calls itself`, { sequenceId: call.sequenceId });
279
+ if (stack.length >= MAX_CALL_DEPTH2) {
280
+ invalid(`Shared sequences call each other at most ${MAX_CALL_DEPTH2} deep`, { sequenceId: call.sequenceId });
281
+ }
282
+ const sequence = bySequence.get(ref);
283
+ if (!sequence)
284
+ invalid(`The shared sequence ${ref} was not resolved`, { sequenceId: call.sequenceId });
285
+ counter += 1;
286
+ const prefix = `${COMPILED_NAME_PREFIX2}${counter}_`;
287
+ const elementKeys = new Map;
288
+ for (const element of sequence.elements) {
289
+ const key = prefixedKey(prefix, element.key);
290
+ elementKeys.set(element.key, key);
291
+ elements[key] = {
292
+ elementId: element.elementId,
293
+ version: element.version,
294
+ ...element.name ? { name: element.name.slice(0, 512) } : {},
295
+ target: parsedTarget(element.target, element.key)
296
+ };
297
+ }
298
+ const declared = new Map(sequence.arguments.map((argument) => [argument.name, argument]));
299
+ const renames = {
300
+ variable: (name) => prefixed(`${prefix}v_`, name),
301
+ argument: (name) => {
302
+ if (!declared.has(name))
303
+ invalid(`The shared sequence ${ref} reads an argument "${name}" it does not declare`);
304
+ return prefixed(`${prefix}a_`, name);
305
+ },
306
+ element: (key) => {
307
+ const mapped = elementKeys.get(key);
308
+ if (mapped === undefined)
309
+ invalid(`The shared sequence ${ref} uses an element "${key}" it does not pin`, { key });
310
+ return mapped;
311
+ },
312
+ stepId: (id) => uniqueId(`${call.id}-${id}`)
313
+ };
314
+ for (const name of Object.keys(call.args ?? {})) {
315
+ if (!declared.has(name))
316
+ invalid(`Step ${call.id} passes an argument "${name}" the sequence does not declare`);
317
+ }
318
+ const argumentSteps = sequence.arguments.map((argument) => {
319
+ const given = call.args?.[argument.name];
320
+ if (given === undefined && argument.default === undefined && argument.required === true) {
321
+ invalid(`Step ${call.id} does not pass the required argument "${argument.name}"`);
322
+ }
323
+ const value = given !== undefined ? given : argument.default !== undefined ? renameExpression(argument.default, renames) : null;
324
+ return {
325
+ id: uniqueId(`${call.id}-arg-${argument.name}`),
326
+ action: "set",
327
+ name: renames.argument(argument.name),
328
+ value
329
+ };
330
+ });
331
+ const body = inlineList(sequence.steps.map((step) => renameStep(step, renames)), [...stack, ref]);
332
+ const outputSteps = call.saveAs === undefined ? [] : [
333
+ {
334
+ id: uniqueId(`${call.id}-out`),
335
+ action: "set",
336
+ name: call.saveAs,
337
+ value: {
338
+ object: Object.fromEntries(sequence.outputs.map((output) => [output.name, renameExpression(output.value, renames)]))
339
+ }
340
+ }
341
+ ];
342
+ calls.push({ stepId: call.id, sequenceId: call.sequenceId, version: call.version });
343
+ return {
344
+ id: call.id,
345
+ action: "retryScope",
346
+ attempts: 1,
347
+ ...call.intent === undefined ? {} : { intent: call.intent },
348
+ ...call.note === undefined ? {} : { note: call.note },
349
+ ...call.disabled === undefined ? {} : { disabled: call.disabled },
350
+ ...call.breakpoint === undefined ? {} : { breakpoint: call.breakpoint },
351
+ ...call.retry === undefined ? {} : { retry: call.retry },
352
+ ...call.onError === undefined ? {} : { onError: call.onError },
353
+ ...call.safeToRepeat === undefined ? {} : { safeToRepeat: call.safeToRepeat },
354
+ ...call.saveAs === undefined ? {} : { saveAs: call.saveAs },
355
+ steps: [...argumentSteps, ...body, ...outputSteps]
356
+ };
357
+ };
358
+ const steps = inlineList(script.steps, []);
359
+ const cleanup = script.cleanup ? inlineList(script.cleanup, []) : undefined;
360
+ return {
361
+ script: {
362
+ ...script,
363
+ ...Object.keys(elements).length > 0 ? { elements } : {},
364
+ steps,
365
+ ...cleanup ? { cleanup } : {}
366
+ },
367
+ calls
368
+ };
369
+ }
370
+ function parseOrThrow(value, resolved) {
371
+ try {
372
+ return parseUiScript2(value, { resolved });
373
+ } catch (error) {
374
+ if (error instanceof UiScriptError2) {
375
+ throw new UiRunError2("INVALID_SCRIPT", error.message, {
376
+ engineCode: error.code,
377
+ ...error.path === undefined ? {} : { path: error.path }
378
+ });
379
+ }
380
+ throw error;
381
+ }
382
+ }
383
+ function compileUiScript2(value, options = {}) {
384
+ const parsed = parseOrThrow(value, false);
385
+ const pinned = applyPins2(parsed, options.elements ?? []);
386
+ const { script: inlined, calls } = inlineCalls2(pinned, options.sequences ?? []);
387
+ const stripped = options.mode === "test" ? inlined : stripBreakpoints2(inlined);
388
+ const script = parseOrThrow(stripped, true);
389
+ const required = requiredCapabilities2(script);
390
+ if (options.capabilities) {
391
+ const missing = missingCapabilities2(script, options.capabilities);
392
+ if (missing.length > 0) {
393
+ throw new UiRunError2("UNSUPPORTED_ON_DESK", `The desk cannot run this script: it lacks ${missing.join(", ")}`, {
394
+ missing
395
+ });
396
+ }
397
+ }
398
+ return { script, requiredCapabilities: required, calls };
399
+ }
400
+ export { MAX_CALL_DEPTH2, COMPILED_NAME_PREFIX2, applyPins2, stripBreakpoints2, inlineCalls2, compileUiScript2 };