@lobu/runtime-device 24.1.1 → 24.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/dist/component.json +1 -1
  2. package/dist/dependencies.bun.lock +26 -28
  3. package/npm-shrinkwrap.json +155 -172
  4. package/package.json +1 -1
  5. package/vendor/cli/dist/bundled-skills/lobu/SKILL.md +14 -5
  6. package/vendor/cli/dist/chrome-extension/accessibility-tree.js +834 -0
  7. package/vendor/cli/dist/chrome-extension/ambient-context.js +53 -0
  8. package/vendor/cli/dist/chrome-extension/auth-refresh.js +167 -0
  9. package/vendor/cli/dist/chrome-extension/automation-run.js +13 -0
  10. package/vendor/cli/dist/chrome-extension/background.js +1155 -0
  11. package/vendor/cli/dist/chrome-extension/bridge.js +140 -0
  12. package/vendor/cli/dist/chrome-extension/browser-origin-policy.js +62 -0
  13. package/vendor/cli/dist/chrome-extension/browser-recovery.js +260 -0
  14. package/vendor/cli/dist/chrome-extension/browser-url-sanitizer.js +284 -0
  15. package/vendor/cli/dist/chrome-extension/completion-response.js +12 -0
  16. package/vendor/cli/dist/chrome-extension/config.js +144 -0
  17. package/vendor/cli/dist/chrome-extension/connector-manifests/chrome.json +1516 -0
  18. package/vendor/cli/dist/chrome-extension/connector-manifests/chrome_bookmarks.json +84 -0
  19. package/vendor/cli/dist/chrome-extension/connector-manifests/chrome_downloads.json +86 -0
  20. package/vendor/cli/dist/chrome-extension/connector-manifests/chrome_history.json +78 -0
  21. package/vendor/cli/dist/chrome-extension/connector-manifests.js +1767 -0
  22. package/vendor/cli/dist/chrome-extension/console-capture.js +627 -0
  23. package/vendor/cli/dist/chrome-extension/control-activity.js +70 -0
  24. package/vendor/cli/dist/chrome-extension/debugger-lease.js +439 -0
  25. package/vendor/cli/dist/chrome-extension/feed-listener.js +488 -0
  26. package/vendor/cli/dist/chrome-extension/feed-transaction.js +48 -0
  27. package/vendor/cli/dist/chrome-extension/feeds.js +632 -0
  28. package/vendor/cli/dist/chrome-extension/focus-tab.js +25 -0
  29. package/vendor/cli/dist/chrome-extension/fonts/dm-mono-latin-500-normal.woff2 +0 -0
  30. package/vendor/cli/dist/chrome-extension/fonts/dm-sans-latin-400-normal.woff2 +0 -0
  31. package/vendor/cli/dist/chrome-extension/fonts/dm-sans-latin-600-normal.woff2 +0 -0
  32. package/vendor/cli/dist/chrome-extension/heartbeat.js +140 -0
  33. package/vendor/cli/dist/chrome-extension/icons/icon-128-rec.png +0 -0
  34. package/vendor/cli/dist/chrome-extension/icons/icon-128.png +0 -0
  35. package/vendor/cli/dist/chrome-extension/icons/icon-16-rec.png +0 -0
  36. package/vendor/cli/dist/chrome-extension/icons/icon-16.png +0 -0
  37. package/vendor/cli/dist/chrome-extension/icons/icon-32-rec.png +0 -0
  38. package/vendor/cli/dist/chrome-extension/icons/icon-32.png +0 -0
  39. package/vendor/cli/dist/chrome-extension/icons/icon-48-rec.png +0 -0
  40. package/vendor/cli/dist/chrome-extension/icons/icon-48.png +0 -0
  41. package/vendor/cli/dist/chrome-extension/lens-page.js +135 -0
  42. package/vendor/cli/dist/chrome-extension/lens-target.js +20 -0
  43. package/vendor/cli/dist/chrome-extension/manifest.json +76 -0
  44. package/vendor/cli/dist/chrome-extension/network-intercept.js +657 -0
  45. package/vendor/cli/dist/chrome-extension/page-activation.js +120 -0
  46. package/vendor/cli/dist/chrome-extension/page-dialogs.js +125 -0
  47. package/vendor/cli/dist/chrome-extension/page-url.js +11 -0
  48. package/vendor/cli/dist/chrome-extension/panel-session.js +184 -0
  49. package/vendor/cli/dist/chrome-extension/poll-auth.js +85 -0
  50. package/vendor/cli/dist/chrome-extension/poll-backoff.js +22 -0
  51. package/vendor/cli/dist/chrome-extension/run-fence.js +117 -0
  52. package/vendor/cli/dist/chrome-extension/setup-status.js +71 -0
  53. package/vendor/cli/dist/chrome-extension/side-panel-action-click.js +65 -0
  54. package/vendor/cli/dist/chrome-extension/sidepanel-context.js +50 -0
  55. package/vendor/cli/dist/chrome-extension/sidepanel.html +680 -0
  56. package/vendor/cli/dist/chrome-extension/sidepanel.js +1643 -0
  57. package/vendor/cli/dist/chrome-extension/tab-labels.js +233 -0
  58. package/vendor/cli/dist/chrome-extension/tools.js +3354 -0
  59. package/vendor/cli/dist/chrome-extension/watch-observer.js +354 -0
  60. package/vendor/cli/dist/chrome-extension/watch-redact.js +96 -0
  61. package/vendor/cli/dist/chrome-extension/watch.js +348 -0
  62. package/vendor/cli/dist/commands/_lib/apply/desired-state.d.ts +6 -4
  63. package/vendor/cli/dist/commands/_lib/apply/desired-state.js +5 -3
  64. package/vendor/cli/dist/commands/_lib/apply/diff.js +4 -3
  65. package/vendor/cli/dist/commands/_lib/chrome-for-testing.d.ts +13 -0
  66. package/vendor/cli/dist/commands/_lib/chrome-for-testing.js +226 -0
  67. package/vendor/cli/dist/commands/_lib/device-credential.d.ts +34 -0
  68. package/vendor/cli/dist/commands/_lib/device-credential.js +157 -0
  69. package/vendor/cli/dist/commands/_lib/device-wizard.d.ts +17 -0
  70. package/vendor/cli/dist/commands/_lib/device-wizard.js +11 -2
  71. package/vendor/cli/dist/commands/_lib/native-messaging.d.ts +7 -0
  72. package/vendor/cli/dist/commands/_lib/native-messaging.js +40 -0
  73. package/vendor/cli/dist/commands/browser.d.ts +23 -0
  74. package/vendor/cli/dist/commands/browser.js +492 -0
  75. package/vendor/cli/dist/commands/call.d.ts +12 -0
  76. package/vendor/cli/dist/commands/call.js +48 -26
  77. package/vendor/cli/dist/commands/context.d.ts +3 -1
  78. package/vendor/cli/dist/commands/context.js +13 -2
  79. package/vendor/cli/dist/commands/daemon.js +2 -152
  80. package/vendor/cli/dist/commands/memory/_lib/errors.d.ts +4 -1
  81. package/vendor/cli/dist/commands/memory/_lib/errors.js +9 -1
  82. package/vendor/cli/dist/commands/memory/_lib/mcp.d.ts +0 -9
  83. package/vendor/cli/dist/commands/memory/_lib/mcp.js +1 -23
  84. package/vendor/cli/dist/commands/memory/_lib/memory-auth.d.ts +20 -4
  85. package/vendor/cli/dist/commands/memory/_lib/memory-auth.js +21 -17
  86. package/vendor/cli/dist/commands/memory/_lib/seed-cmd.js +6 -17
  87. package/vendor/cli/dist/commands/memory/run.js +4 -25
  88. package/vendor/cli/dist/connectors/google_gmail.ts +70 -41
  89. package/vendor/cli/dist/connectors/google_gmail_references.ts +406 -0
  90. package/vendor/cli/dist/connectors/linkedin-inbox-adapter.js +107 -36
  91. package/vendor/cli/dist/connectors/linkedin-inbox.ts +13 -0
  92. package/vendor/cli/dist/connectors/linkedin.ts +1 -1
  93. package/vendor/cli/dist/connectors/postgres.ts +5 -3
  94. package/vendor/cli/dist/connectors/reddit.ts +171 -5
  95. package/vendor/cli/dist/connectors/whatsapp-web-adapter.js +147 -61
  96. package/vendor/cli/dist/connectors/whatsapp-web-helpers.ts +6 -2
  97. package/vendor/cli/dist/connectors/whatsapp_web.ts +48 -17
  98. package/vendor/cli/dist/connectors/x-dm-adapter.js +252 -35
  99. package/vendor/cli/dist/connectors/x.ts +189 -13
  100. package/vendor/cli/dist/index.js +46 -6
  101. package/vendor/cli/dist/internal/device-state.d.ts +2 -0
  102. package/vendor/cli/dist/internal/device-state.js +4 -0
  103. package/vendor/cli/dist/runtime-components.json +4 -4
  104. package/vendor/cli/dist/templates/AGENTS.md.tmpl +3 -1
  105. package/vendor/cli/dist/templates/TESTING.md.tmpl +8 -8
  106. package/vendor/cli/package.json +1 -1
  107. package/vendor/connector-sdk/dist/browser-requirement.js +1 -1
  108. package/vendor/connector-sdk/dist/connector-types.d.ts +5 -1
  109. package/vendor/connector-sdk/package.json +1 -1
  110. package/vendor/connector-worker/dist/agent-turn/gateway-tools.d.ts +9 -0
  111. package/vendor/connector-worker/dist/agent-turn/gateway-tools.js +23 -2
  112. package/vendor/connector-worker/dist/agent-turn/guest-entry.js +14 -6
  113. package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +62 -25
  114. package/vendor/connector-worker/dist/agent-turn/media-tools.d.ts +1 -0
  115. package/vendor/connector-worker/dist/agent-turn/media-tools.js +5 -2
  116. package/vendor/connector-worker/dist/bin.js +2 -0
  117. package/vendor/connector-worker/dist/daemon/agent-turn.js +52 -17
  118. package/vendor/connector-worker/dist/daemon/automation.js +2 -12
  119. package/vendor/connector-worker/dist/daemon/client.d.ts +8 -2
  120. package/vendor/connector-worker/dist/daemon/client.js +8 -1
  121. package/vendor/connector-worker/dist/daemon/interactive-session.js +29 -6
  122. package/vendor/connector-worker/dist/daemon/poll-loop.js +23 -1
  123. package/vendor/connector-worker/dist/daemon/terminal-delivery.d.ts +5 -10
  124. package/vendor/connector-worker/dist/daemon/terminal-delivery.js +55 -23
  125. package/vendor/connector-worker/dist/daemon/terminal-failure.js +2 -2
  126. package/vendor/connector-worker/dist/executor/isolate.js +3 -0
  127. package/vendor/connector-worker/package.json +1 -1
  128. package/vendor/core/dist/agent-tooling.d.ts +8 -0
  129. package/vendor/core/dist/agent-tooling.js +9 -1
  130. package/vendor/core/dist/capabilities.d.ts +1 -1
  131. package/vendor/core/dist/capabilities.js +5 -0
  132. package/vendor/core/dist/contracts/entity-reference.d.ts +3 -0
  133. package/vendor/core/dist/contracts/entity-reference.js +31 -0
  134. package/vendor/core/dist/contracts/tools/manage-automations.d.ts +3 -0
  135. package/vendor/core/dist/contracts/tools/manage-automations.js +3 -0
  136. package/vendor/core/dist/contracts/tools/manage-connections.js +1 -1
  137. package/vendor/core/dist/contracts/tools/manage-entity-schema.js +1 -1
  138. package/vendor/core/dist/contracts/tools/manage-entity.d.ts +113 -34
  139. package/vendor/core/dist/contracts/tools/manage-entity.js +41 -14
  140. package/vendor/core/dist/contracts/tools/manage-operations.d.ts +13 -4
  141. package/vendor/core/dist/contracts/tools/manage-operations.js +19 -7
  142. package/vendor/core/dist/contracts/tools/read-knowledge.d.ts +6 -1
  143. package/vendor/core/dist/contracts/tools/read-knowledge.js +20 -1
  144. package/vendor/core/dist/contracts/tools/view-attach.d.ts +1 -1
  145. package/vendor/core/dist/contracts/tools/view-runtime.d.ts +2 -0
  146. package/vendor/core/dist/contracts/tools/view-runtime.js +3 -1
  147. package/vendor/core/dist/contracts/worker/device-automation.d.ts +4 -9
  148. package/vendor/core/dist/contracts/worker/device-automation.js +10 -15
  149. package/vendor/core/dist/contracts/worker/protocol.d.ts +20 -0
  150. package/vendor/core/dist/contracts/worker/protocol.js +20 -0
  151. package/vendor/core/dist/reserved.d.ts +6 -3
  152. package/vendor/core/dist/reserved.js +6 -3
  153. package/vendor/core/dist/worker/auth.d.ts +3 -2
  154. package/vendor/core/package.json +6 -1
  155. package/vendor/postgres/cf/src/query.js +3 -1
  156. package/vendor/postgres/cjs/src/query.js +3 -1
  157. package/vendor/postgres/src/query.js +3 -1
  158. /package/vendor/postgres/{.bun-tag-2f292c6554a8475e → .bun-tag-22d644c2873790db} +0 -0
@@ -0,0 +1,834 @@
1
+ // Bundled content script that builds a custom accessibility snapshot of
2
+ // the page on demand. It is NOT the browser's real AX tree (chrome.debugger
3
+ // can ask for that via Accessibility.getFullAXTree, but that surface is
4
+ // noisy and lossy for our purposes). This walker computes roles + names
5
+ // from explicit ARIA attrs falling back to a tag→role table, filters by
6
+ // visibility/viewport/interactivity, and assigns each kept element a
7
+ // stable ref_id that the click_ref / type_ref tools dereference.
8
+ //
9
+ // Sensitive-field redaction is intentional: password/cc-*/one-time-code
10
+ // inputs surface their value as "[value redacted]" and are visible in the
11
+ // snapshot as elements (the agent needs to know the field exists to act
12
+ // on it), but the literal value never leaves the page.
13
+ //
14
+ // document_epoch starts with a random document-local seed, then increments
15
+ // on every full extraction. This makes cross-navigation ref revival negligibly
16
+ // likely. Refs from epoch N are valid only when the next extraction returns the
17
+ // same epoch. The
18
+ // extension's tool dispatcher passes the ref's document_epoch into the
19
+ // page when dereferencing and refuses stale refs.
20
+ //
21
+ // This script is injected by Runtime.evaluate on demand (not via manifest
22
+ // content_scripts), so the page bears no cost when the agent isn't
23
+ // observing it. Lives in a CDP isolated world.
24
+
25
+ (() => {
26
+ // State lives in the DOM (data attributes), NOT in a JS global on the page's
27
+ // window. This script is injected into a CDP **isolated world** (see
28
+ // tools.js injectAccessibilityScript), so page JS can neither observe nor
29
+ // forge our helpers — closing the old tampering vector where a hostile page
30
+ // could predefine `window.__lobu_controller` before our injection.
31
+ //
32
+ // Carrying the ref→element mapping as DOM attributes (rather than an
33
+ // in-memory Map in the world's closure) is what makes isolated worlds
34
+ // viable here: each tool run attaches/detaches the debugger independently,
35
+ // so its isolated world is torn down between runs. The DOM, however, is
36
+ // shared across worlds and survives across runs until navigation — so the
37
+ // snapshot the agent took in get_accessibility_tree is still resolvable when
38
+ // click_ref/type_ref run later in a fresh world. Re-injection just redefines
39
+ // the globalThis helpers below; harmless.
40
+ //
41
+ // Residual (lesser) vector: a page can read/remove the data-lobu-ref
42
+ // attributes (the DOM is shared). The epoch check rejects stale refs, and
43
+ // the connector's allowedOrigins gate remains the primary defense against
44
+ // navigating to unexpected sites.
45
+ const REF_ATTR = "data-lobu-ref";
46
+ const EPOCH_ATTR = "data-lobu-epoch";
47
+ // What the snapshot said the element was (tag, role, label). A framework can
48
+ // keep the same DOM node and rewrite it for a different item (a list row
49
+ // re-used for the next record, a button relabelled by a route change); the
50
+ // epoch cannot see that, so a ref whose element no longer matches is stale.
51
+ const IDENTITY_ATTR = "data-lobu-identity";
52
+
53
+ // ── helpers ───────────────────────────────────────────────────────────
54
+ function tagRoleFallback(el) {
55
+ const tag = el.tagName.toLowerCase();
56
+ const type = (el.getAttribute('type') || '').toLowerCase();
57
+ switch (tag) {
58
+ case 'a': return 'link';
59
+ case 'button': return 'button';
60
+ case 'input':
61
+ if (type === 'submit' || type === 'button') return 'button';
62
+ if (type === 'checkbox') return 'checkbox';
63
+ if (type === 'radio') return 'radio';
64
+ if (type === 'file') return 'button';
65
+ return 'textbox';
66
+ case 'select': return 'combobox';
67
+ case 'textarea': return 'textbox';
68
+ case 'h1': case 'h2': case 'h3':
69
+ case 'h4': case 'h5': case 'h6': return 'heading';
70
+ case 'img': return 'image';
71
+ case 'nav': return 'navigation';
72
+ case 'main': return 'main';
73
+ case 'header': return 'banner';
74
+ case 'footer': return 'contentinfo';
75
+ case 'section': return 'region';
76
+ case 'article': return 'article';
77
+ case 'aside': return 'complementary';
78
+ case 'form': return 'form';
79
+ case 'table': return 'table';
80
+ case 'ul': case 'ol': return 'list';
81
+ case 'li': return 'listitem';
82
+ case 'label': return 'label';
83
+ default: return 'generic';
84
+ }
85
+ }
86
+
87
+ function computeRole(el) {
88
+ const explicit = el.getAttribute('role');
89
+ if (explicit) return explicit;
90
+ return tagRoleFallback(el);
91
+ }
92
+
93
+ // Sensitive-field detection — kept conservative. Anything matching here
94
+ // surfaces in the tree as "[value redacted]" instead of the real value.
95
+ function isSensitive(el) {
96
+ const type = (el.getAttribute('type') || '').toLowerCase();
97
+ if (type === 'password' || type === 'hidden') return true;
98
+ const ac = (el.getAttribute('autocomplete') || '').toLowerCase();
99
+ const danger = [
100
+ 'current-password',
101
+ 'new-password',
102
+ 'one-time-code',
103
+ 'cc-number',
104
+ 'cc-csc',
105
+ 'cc-exp',
106
+ 'cc-exp-month',
107
+ 'cc-exp-year',
108
+ ];
109
+ for (const term of danger) if (ac.indexOf(term) !== -1) return true;
110
+ return false;
111
+ }
112
+
113
+ // Scripts, templates and field values cannot label surrounding elements.
114
+ const NAME_SKIPPED_TAGS = ['script', 'style', 'template', 'input', 'select', 'textarea'];
115
+
116
+ function hiddenFromName(el) {
117
+ if (el.getAttribute('aria-hidden') === 'true' || el.hasAttribute('hidden')) return true;
118
+ const style = window.getComputedStyle(el);
119
+ return style.display === 'none' || style.visibility === 'hidden' ||
120
+ style.visibility === 'collapse' || style.contentVisibility === 'hidden';
121
+ }
122
+
123
+ // The text a control or label shows, through any child elements: real pages
124
+ // wrap a button's label in a span, an icon beside it, a label's text in
125
+ // markup. Field values are excluded even when referenced directly.
126
+ function nestedText(root, includeHidden) {
127
+ if (NAME_SKIPPED_TAGS.includes(root.tagName.toLowerCase()) || holdsTypedText(root)) return '';
128
+ let out = '';
129
+ for (const child of root.childNodes) {
130
+ if (child.nodeType === Node.TEXT_NODE) {
131
+ out += child.textContent;
132
+ } else if (
133
+ child.nodeType === Node.ELEMENT_NODE &&
134
+ (includeHidden || !hiddenFromName(child))
135
+ ) {
136
+ out += nestedText(child, includeHidden);
137
+ }
138
+ }
139
+ return out;
140
+ }
141
+
142
+ function nameText(root) {
143
+ // Explicitly referenced hidden labels still supply their text. Keep the
144
+ // full name because ref identity must notice changes beyond its prefix.
145
+ return nestedText(root, hiddenFromName(root)).replace(/\s+/g, ' ').trim();
146
+ }
147
+
148
+ // Roles whose name comes from their content, as a button's does.
149
+ const NAME_FROM_CONTENT_ROLES = [
150
+ 'button', 'link', 'tab', 'menuitem', 'menuitemcheckbox', 'menuitemradio',
151
+ 'option', 'treeitem', 'switch', 'checkbox', 'radio',
152
+ ];
153
+
154
+ // `withValue` false leaves out what the user (or the agent) types or picks:
155
+ // a field's value and a select's chosen option. That variant is the
156
+ // element's identity, which typing into it must not change.
157
+ function computeName(el, withValue = true) {
158
+ const tag = el.tagName.toLowerCase();
159
+
160
+ const labelledBy = el.getAttribute('aria-labelledby');
161
+ if (labelledBy) {
162
+ const t = labelledBy
163
+ .split(/\s+/)
164
+ .map((id) => document.getElementById(id))
165
+ .filter(Boolean)
166
+ .map(nameText)
167
+ .filter(Boolean)
168
+ .join(' ');
169
+ if (t) return t;
170
+ }
171
+
172
+ const aria = el.getAttribute('aria-label');
173
+ if (aria && aria.trim()) return aria.trim();
174
+ const ph = el.getAttribute('placeholder');
175
+ if (ph && ph.trim()) return ph.trim();
176
+ const title = el.getAttribute('title');
177
+ if (title && title.trim()) return title.trim();
178
+ const alt = el.getAttribute('alt');
179
+ if (alt && alt.trim()) return alt.trim();
180
+
181
+ // A wrapping <label> as well as label[for]; its text without the
182
+ // control's own value.
183
+ const labels = el.labels && el.labels.length > 0
184
+ ? Array.from(el.labels)
185
+ : el.id
186
+ ? [document.querySelector(`label[for="${CSS.escape(el.id)}"]`)].filter(Boolean)
187
+ : [];
188
+ for (const lbl of labels) {
189
+ const t = nameText(lbl);
190
+ if (t) return t;
191
+ }
192
+
193
+ // What an editor or custom text box holds is its value, not its name: an
194
+ // unlabelled one has no stable name, so its identity is tag and role.
195
+ if (!withValue && holdsTypedText(el)) return '';
196
+
197
+ // A select is named by its label, like any field; the option it shows is
198
+ // its value, the name only when nothing labels it.
199
+ if (withValue && tag === 'select') {
200
+ if (isSensitive(el)) return '[value redacted]';
201
+ const opt = el.options[el.selectedIndex] || el.querySelector('option[selected]');
202
+ if (opt && opt.textContent) return opt.textContent.trim();
203
+ }
204
+
205
+ if (tag === 'input') {
206
+ const type = (el.getAttribute('type') || '').toLowerCase();
207
+ const val = el.getAttribute('value');
208
+ if (type === 'submit' && val && val.trim()) return val.trim();
209
+ if (!withValue) return '';
210
+ if (isSensitive(el)) return el.value ? '[value redacted]' : '';
211
+ if (el.value && el.value.length < 50 && el.value.trim()) return el.value.trim();
212
+ }
213
+ if (tag === 'textarea' && !withValue) return '';
214
+ if (tag === 'textarea' && isSensitive(el)) {
215
+ return el.value ? '[value redacted]' : '';
216
+ }
217
+
218
+ if (tag === 'button' || tag === 'a' || tag === 'summary' || NAME_FROM_CONTENT_ROLES.includes(el.getAttribute('role'))) {
219
+ const t = nameText(el);
220
+ if (t) return t;
221
+ }
222
+
223
+ if (/^h[1-6]$/.test(tag)) {
224
+ const t = (el.textContent || '').trim();
225
+ if (t) return t.substring(0, 100);
226
+ }
227
+ if (tag === 'img') return '';
228
+
229
+ let acc = '';
230
+ for (const child of el.childNodes) {
231
+ if (child.nodeType === Node.TEXT_NODE) acc += child.textContent;
232
+ }
233
+ const t = acc.trim();
234
+ if (t.length >= 3) return t.length > 100 ? t.substring(0, 100) + '…' : t;
235
+ return '';
236
+ }
237
+
238
+ const TEXT_ROLES = ['textbox', 'searchbox', 'combobox', 'spinbutton'];
239
+
240
+ function holdsTypedText(el) {
241
+ const editable = el.getAttribute('contenteditable');
242
+ if (editable != null && editable !== 'false') return true;
243
+ return TEXT_ROLES.includes(el.getAttribute('role'));
244
+ }
245
+
246
+ function isVisible(el) {
247
+ const style = window.getComputedStyle(el);
248
+ if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0') return false;
249
+ return el.offsetWidth > 0 && el.offsetHeight > 0;
250
+ }
251
+
252
+ function inViewport(el) {
253
+ const r = el.getBoundingClientRect();
254
+ return r.top < window.innerHeight && r.bottom > 0 && r.left < window.innerWidth && r.right > 0;
255
+ }
256
+
257
+ function isInteractive(el) {
258
+ const tag = el.tagName.toLowerCase();
259
+ if (['a', 'button', 'input', 'select', 'textarea', 'details', 'summary'].includes(tag)) return true;
260
+ if (el.getAttribute('onclick') != null) return true;
261
+ if (el.getAttribute('tabindex') != null) return true;
262
+ const role = el.getAttribute('role');
263
+ if (role === 'button' || role === 'link') return true;
264
+ // An editing host: contenteditable="", "true" or "plaintext-only" (not
265
+ // "false"). Message composers are usually one.
266
+ const editable = el.getAttribute('contenteditable');
267
+ if (editable != null && editable !== 'false') return true;
268
+ return false;
269
+ }
270
+
271
+ function isStructural(el) {
272
+ const tag = el.tagName.toLowerCase();
273
+ if (['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'nav', 'main', 'header', 'footer', 'section', 'article', 'aside'].includes(tag)) return true;
274
+ if (el.getAttribute('role') != null) return true;
275
+ return false;
276
+ }
277
+
278
+ function keepElement(el, filter) {
279
+ const tag = el.tagName.toLowerCase();
280
+ if (['script', 'style', 'meta', 'link', 'title', 'noscript'].includes(tag)) return false;
281
+ if (filter !== 'all' && el.getAttribute('aria-hidden') === 'true') return false;
282
+ if (filter !== 'all' && !isVisible(el)) return false;
283
+ if (filter !== 'all' && !inViewport(el)) return false;
284
+ if (filter === 'interactive') return isInteractive(el);
285
+ if (isInteractive(el)) return true;
286
+ if (isStructural(el)) return true;
287
+ if (computeName(el).length > 0) return true;
288
+ return false;
289
+ }
290
+
291
+ function identityOf(el) {
292
+ return [el.tagName.toLowerCase(), computeRole(el), computeName(el, false)].join('\u0000');
293
+ }
294
+
295
+ function rectOf(el) {
296
+ const r = el.getBoundingClientRect();
297
+ return {
298
+ x: Math.round(r.left),
299
+ y: Math.round(r.top),
300
+ w: Math.round(r.width),
301
+ h: Math.round(r.height),
302
+ };
303
+ }
304
+
305
+ // ── helpers (exposed on globalThis of the isolated world) ─────────────
306
+
307
+ function extract(opts) {
308
+ const filter = opts && opts.filter ? opts.filter : 'interactive';
309
+ const prevEpoch = Number(document.documentElement.getAttribute(EPOCH_ATTR));
310
+ if (prevEpoch >= Number.MAX_SAFE_INTEGER) throw new Error('Accessibility epoch exhausted; reload the page.');
311
+ // New documents must not all start at 1: an old (epoch, ref) pair could
312
+ // otherwise click a different element after navigation and a new snapshot.
313
+ const seed = crypto.getRandomValues(new Uint32Array(2));
314
+ const documentEpoch = Number.isSafeInteger(prevEpoch) && prevEpoch > 0
315
+ ? prevEpoch + 1
316
+ : seed[0] * 0x100000 + (seed[1] >>> 12) + 1;
317
+ // Refs and epoch live in the DOM so they survive debugger reattachment.
318
+ for (const el of document.querySelectorAll('[' + REF_ATTR + ']')) {
319
+ el.removeAttribute(REF_ATTR);
320
+ el.removeAttribute(IDENTITY_ATTR);
321
+ }
322
+ document.documentElement.setAttribute(EPOCH_ATTR, String(documentEpoch));
323
+
324
+ let refCounter = 0;
325
+ const tree = [];
326
+ const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_ELEMENT);
327
+ let node = walker.nextNode();
328
+ while (node) {
329
+ if (keepElement(node, filter)) {
330
+ const ref = ++refCounter;
331
+ node.setAttribute(REF_ATTR, String(ref));
332
+ node.setAttribute(IDENTITY_ATTR, identityOf(node));
333
+ const role = computeRole(node);
334
+ // Identity keeps the full name; the snapshot shows a readable prefix.
335
+ const fullName = computeName(node);
336
+ const name = fullName.length > 100 ? fullName.substring(0, 100) + '…' : fullName;
337
+ const item = {
338
+ ref_id: ref,
339
+ role,
340
+ name,
341
+ tag: node.tagName.toLowerCase(),
342
+ bbox: rectOf(node),
343
+ };
344
+ if (node.id) item.id = node.id;
345
+ const ariaCurrent = node.getAttribute('aria-current');
346
+ if (ariaCurrent) item.aria_current = ariaCurrent;
347
+ const ariaChecked = node.getAttribute('aria-checked');
348
+ if (ariaChecked != null) item.aria_checked = ariaChecked;
349
+ const ariaExpanded = node.getAttribute('aria-expanded');
350
+ if (ariaExpanded != null) item.aria_expanded = ariaExpanded;
351
+ const ariaSelected = node.getAttribute('aria-selected');
352
+ if (ariaSelected != null) item.aria_selected = ariaSelected;
353
+ if (node.tagName === 'A' && node.href) item.href = node.href;
354
+ tree.push(item);
355
+ }
356
+ node = walker.nextNode();
357
+ }
358
+
359
+ return {
360
+ document_epoch: documentEpoch,
361
+ current_url: location.href,
362
+ title: document.title,
363
+ tree,
364
+ };
365
+ }
366
+
367
+ // Resolve a ref → element via its DOM attribute. Returns null if the ref is
368
+ // unknown, the document epoch doesn't match (caller's snapshot is stale), or
369
+ // the element was rewritten into something else since the snapshot.
370
+ //
371
+ // The epoch lives on this document's root, so it also carries frame
372
+ // identity: a cross-document navigation (a new loader) starts a document
373
+ // without it, and nothing from the old snapshot resolves there.
374
+ function resolveEl(refId, expectedEpoch) {
375
+ const epoch = parseInt(document.documentElement.getAttribute(EPOCH_ATTR) || '0', 10) || 0;
376
+ if (epoch !== expectedEpoch) return null;
377
+ const id = parseInt(refId, 10);
378
+ if (!Number.isInteger(id) || id <= 0) return null;
379
+ const el = document.querySelector('[' + REF_ATTR + '="' + id + '"]');
380
+ if (!el || el.getAttribute(IDENTITY_ATTR) !== identityOf(el)) return null;
381
+ return el;
382
+ }
383
+
384
+ // Focus + return bbox of an element resolved by ref. Used by the tool
385
+ // dispatcher to translate ref-based actions into coordinate-based CDP
386
+ // Input dispatches.
387
+ function focusBbox(refId, expectedEpoch) {
388
+ const el = resolveEl(refId, expectedEpoch);
389
+ if (!el) return null;
390
+ if (typeof el.focus === 'function') el.focus();
391
+ return centerOf(el);
392
+ }
393
+
394
+ // Like focusBbox without moving focus: hovering must not focus.
395
+ function bbox(refId, expectedEpoch) {
396
+ const el = resolveEl(refId, expectedEpoch);
397
+ return el ? centerOf(el) : null;
398
+ }
399
+
400
+ function centerOf(el) {
401
+ el.scrollIntoView({ block: 'center', inline: 'center', behavior: 'instant' });
402
+ const r = el.getBoundingClientRect();
403
+ return {
404
+ x: Math.round(r.left + r.width / 2),
405
+ y: Math.round(r.top + r.height / 2),
406
+ w: Math.round(r.width),
407
+ h: Math.round(r.height),
408
+ };
409
+ }
410
+
411
+ // Headed Chrome drops synthesized input (mouse and key events) on a hidden
412
+ // page: the CDP call resolves and the page sees nothing. Agent tabs live in
413
+ // the background, so the dispatcher picks the route up front from
414
+ // document.visibilityState — never by replaying a click it failed to observe,
415
+ // which could activate twice.
416
+ //
417
+ // In-page click: one fully initialized pointer/mouse/click sequence per click.
418
+ // An untrusted `click` still runs the element's default action (checkbox
419
+ // toggle, link navigation, form submit); right/middle clicks have no such
420
+ // default action, so the dispatcher refuses them on a hidden page instead.
421
+ // What a user's click on el could not do: a disabled or inert control, or a
422
+ // point covered by something else (a modal, an overlay). Dispatching straight
423
+ // to the element would bypass both, so the in-page route refuses them.
424
+ function blockedReason(el) {
425
+ if (el.matches(':disabled') || el.closest('[inert]')) return 'disabled';
426
+ const r = el.getBoundingClientRect();
427
+ const x = r.left + r.width / 2;
428
+ const y = r.top + r.height / 2;
429
+ if (x < 0 || y < 0 || x >= window.innerWidth || y >= window.innerHeight) return 'outside the viewport';
430
+ const hit = document.elementFromPoint(x, y);
431
+ // The element's own <label> on top still activates it (label activation).
432
+ const onTarget = (node) => node === el || el.contains(node);
433
+ const ownLabel = (node) => [...(el.labels ?? [])].some((label) => label === node || label.contains(node));
434
+ if (!hit || (!onTarget(hit) && !ownLabel(hit))) {
435
+ const what = hit ? hit.tagName.toLowerCase() + (hit.id ? '#' + hit.id : '') : 'nothing';
436
+ return 'covered by ' + what;
437
+ }
438
+ return null;
439
+ }
440
+
441
+ // Trusted input lands on whatever is on top at the point, so before each
442
+ // press the dispatcher asks what a user's click there would hit. Returns
443
+ // null (the element itself), 'stale', or why the click was refused.
444
+ function hitTest(refId, expectedEpoch) {
445
+ const el = resolveEl(refId, expectedEpoch);
446
+ if (!el) return 'stale';
447
+ return blockedReason(el);
448
+ }
449
+
450
+ // Returns 'done', 'stale', or why the click was refused.
451
+ function clickEl(refId, expectedEpoch, clickCount) {
452
+ const el = resolveEl(refId, expectedEpoch);
453
+ if (!el) return 'stale';
454
+ const r = el.getBoundingClientRect();
455
+ const init = {
456
+ bubbles: true,
457
+ cancelable: true,
458
+ composed: true,
459
+ view: window,
460
+ clientX: r.left + r.width / 2,
461
+ clientY: r.top + r.height / 2,
462
+ button: 0,
463
+ };
464
+ if (typeof el.focus === 'function') el.focus();
465
+ for (let i = 1; i <= clickCount; i++) {
466
+ // Per click: the first click may have opened a modal over the second.
467
+ const blocked = blockedReason(el);
468
+ if (blocked) return blocked;
469
+ const down = { ...init, detail: i, buttons: 1 };
470
+ const up = { ...init, detail: i, buttons: 0 };
471
+ el.dispatchEvent(new PointerEvent('pointerdown', { ...down, pointerType: 'mouse', isPrimary: true }));
472
+ el.dispatchEvent(new MouseEvent('mousedown', down));
473
+ el.dispatchEvent(new PointerEvent('pointerup', { ...up, pointerType: 'mouse', isPrimary: true }));
474
+ el.dispatchEvent(new MouseEvent('mouseup', up));
475
+ el.dispatchEvent(new MouseEvent('click', up));
476
+ }
477
+ if (clickCount >= 2) {
478
+ el.dispatchEvent(new MouseEvent('dblclick', { ...init, detail: 2 }));
479
+ }
480
+ return 'done';
481
+ }
482
+
483
+ // Controls whose value is not editable text: insertText (and key events on a
484
+ // hidden page) cannot set them, so the value is assigned directly.
485
+ const VALUE_INPUT_TYPES = new Set(['date', 'time', 'datetime-local', 'month', 'week', 'color', 'range']);
486
+ // Input types the readonly attribute applies to (HTML ignores it on range,
487
+ // color, checkbox and the rest).
488
+ const READONLY_INPUT_TYPES = new Set([
489
+ 'text', 'search', 'url', 'tel', 'email', 'password', 'number',
490
+ 'date', 'month', 'week', 'time', 'datetime-local',
491
+ ]);
492
+
493
+ // Input types that hold text (insertText edits them) or a typed value.
494
+ const TEXT_INPUT_TYPES = new Set([
495
+ 'text', 'search', 'url', 'tel', 'email', 'password', 'number',
496
+ ]);
497
+
498
+ function takesText(el) {
499
+ if (!el) return false;
500
+ if (el instanceof HTMLTextAreaElement) return true;
501
+ if (el instanceof HTMLInputElement) {
502
+ return TEXT_INPUT_TYPES.has(el.type) || VALUE_INPUT_TYPES.has(el.type);
503
+ }
504
+ return el.isContentEditable === true;
505
+ }
506
+
507
+ function setNativeValue(el, value) {
508
+ const proto = el instanceof HTMLTextAreaElement ? HTMLTextAreaElement.prototype : HTMLInputElement.prototype;
509
+ // The prototype setter, not el.value =, so framework-controlled inputs
510
+ // (React tracks the instance property) see the change.
511
+ Object.getOwnPropertyDescriptor(proto, 'value').set.call(el, value);
512
+ el.dispatchEvent(new Event('input', { bubbles: true }));
513
+ el.dispatchEvent(new Event('change', { bubbles: true }));
514
+ }
515
+
516
+ // Prepare an element for type_ref. Returns:
517
+ // 'insert' — focused, and (with clear) its content selected, so the
518
+ // caller's Input.insertText replaces it;
519
+ // 'done' — the value was set here (non-text controls, or clearing to '');
520
+ // 'stale' — the ref no longer resolves;
521
+ // 'disabled' / 'readonly' — a user could not edit it either;
522
+ // 'not-editable:<tag>' — focus did not land on anything that takes text
523
+ // (a <select>, a checkbox, a button), so insertText would be a
524
+ // silent no-op;
525
+ // 'failed' — the value did not take.
526
+ // Key events (select-all, Delete) are dropped on a hidden page, which left the
527
+ // old text in place ("newold"); selection plus insertText is not.
528
+ function prepareText(refId, expectedEpoch, text, clear) {
529
+ const el = resolveEl(refId, expectedEpoch);
530
+ if (!el) return 'stale';
531
+ const field = el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement;
532
+ // :disabled covers a disabled <fieldset> ancestor too.
533
+ if (el.matches(':disabled') || el.closest('[inert]')) return 'disabled';
534
+ if (el.readOnly && (el instanceof HTMLTextAreaElement || READONLY_INPUT_TYPES.has(el.type))) return 'readonly';
535
+ if (typeof el.focus === 'function') el.focus();
536
+ // A wrapper may hand focus to an inner editor (a code editor's textarea);
537
+ // what matters is where focus landed.
538
+ const target = document.activeElement;
539
+ if (!takesText(target)) return 'not-editable:' + el.tagName.toLowerCase();
540
+ if (field && VALUE_INPUT_TYPES.has(el.type)) {
541
+ // A date or a colour cannot be appended to: the text is the value.
542
+ setNativeValue(el, text);
543
+ const ok =
544
+ el.value === text ||
545
+ (el.type === 'color' && el.value === text.toLowerCase()) ||
546
+ (el.type === 'range' && Number(el.value) === Number(text));
547
+ return ok ? 'done' : 'failed';
548
+ }
549
+ if (field && clear && text === '') {
550
+ setNativeValue(el, '');
551
+ return el.value === '' ? 'done' : 'failed';
552
+ }
553
+ if (!clear) return 'insert';
554
+ if (field) {
555
+ el.select();
556
+ } else if (el.isContentEditable) {
557
+ const range = document.createRange();
558
+ range.selectNodeContents(el);
559
+ const selection = window.getSelection();
560
+ selection.removeAllRanges();
561
+ selection.addRange(range);
562
+ if (text === '') {
563
+ document.execCommand('delete');
564
+ return el.textContent === '' ? 'done' : 'failed';
565
+ }
566
+ }
567
+ return 'insert';
568
+ }
569
+
570
+ // ── outcome observation ─────────────────────────────────────────────
571
+ //
572
+ // An action reports whether anything observably happened. watchStart is
573
+ // called just before the action is dispatched and watchResult after it
574
+ // settles: a DOM mutation, a URL change, a focus move or a scroll counts.
575
+ // It cannot tell the action's effect from the page's own timers; it says
576
+ // "the page changed", which is what an agent needs to decide whether to
577
+ // look again or retry.
578
+ const WATCH = { state: null };
579
+
580
+ // Form controls change state through properties (checked, value,
581
+ // selectedIndex) that no MutationObserver sees: a checkbox toggle would
582
+ // read as "nothing happened" and invite a retry that undoes it. The watch
583
+ // compares them directly. Values stay in this world; only the verdict
584
+ // leaves it. Every control is read: a cap would hide a toggle past it.
585
+ function controlStates() {
586
+ return Array.from(document.querySelectorAll('input, select, textarea'), (el) => [
587
+ el, el.checked, el.value, el.selectedIndex,
588
+ ]);
589
+ }
590
+
591
+ function controlsChanged(before) {
592
+ const after = controlStates();
593
+ if (after.length !== before.length) return true;
594
+ for (let i = 0; i < after.length; i++) {
595
+ for (let j = 0; j < after[i].length; j++) {
596
+ if (after[i][j] !== before[i][j]) return true;
597
+ }
598
+ }
599
+ return false;
600
+ }
601
+
602
+ function scrollPositions() {
603
+ const se = document.scrollingElement || document.documentElement;
604
+ return [se.scrollLeft, se.scrollTop];
605
+ }
606
+
607
+ function watchStart() {
608
+ if (WATCH.state) WATCH.state.observer.disconnect();
609
+ const state = {
610
+ mutations: 0,
611
+ href: location.href,
612
+ active: document.activeElement,
613
+ scroll: scrollPositions(),
614
+ controls: controlStates(),
615
+ observer: null,
616
+ };
617
+ state.observer = new MutationObserver((records) => {
618
+ state.mutations += records.length;
619
+ });
620
+ state.observer.observe(document, {
621
+ subtree: true,
622
+ childList: true,
623
+ attributes: true,
624
+ characterData: true,
625
+ });
626
+ WATCH.state = state;
627
+ return state.href;
628
+ }
629
+
630
+ function watchResult() {
631
+ const state = WATCH.state;
632
+ if (!state) return null;
633
+ state.mutations += state.observer.takeRecords().length;
634
+ state.observer.disconnect();
635
+ WATCH.state = null;
636
+ const scroll = scrollPositions();
637
+ return {
638
+ changed:
639
+ state.mutations > 0 ||
640
+ location.href !== state.href ||
641
+ document.activeElement !== state.active ||
642
+ scroll[0] !== state.scroll[0] ||
643
+ scroll[1] !== state.scroll[1] ||
644
+ controlsChanged(state.controls),
645
+ };
646
+ }
647
+
648
+ // The text an editable element holds, for type_ref's read-back. Stays in
649
+ // this world: fieldBefore keeps it here for typedOutcome, so the dispatcher
650
+ // only learns how the field compares, never its value (a password field's
651
+ // included). Both run in the same injected world within one action.
652
+ const FIELD = { before: null };
653
+ function textOf(el) {
654
+ if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) return el.value;
655
+ return el.textContent || '';
656
+ }
657
+
658
+ // Compare the field after typing with what it held before. Returns
659
+ // 'confirmed' (the text is there: equal when replacing, appended otherwise),
660
+ // 'no_change' (the field is as it was), 'unverifiable' (it changed into
661
+ // something else, e.g. a mask reformatted it), or 'stale'.
662
+ function typedOutcome(refId, expectedEpoch, text, clear) {
663
+ const before = FIELD.before;
664
+ FIELD.before = null;
665
+ const el = resolveEl(refId, expectedEpoch);
666
+ if (!el || before == null) return 'stale';
667
+ const target = takesText(document.activeElement) && el.contains(document.activeElement)
668
+ ? document.activeElement
669
+ : el;
670
+ const now = textOf(target);
671
+ if (clear ? now === text : now === before + text) return 'confirmed';
672
+ if (now === before) return 'no_change';
673
+ return 'unverifiable';
674
+ }
675
+
676
+ function fieldBefore(refId, expectedEpoch) {
677
+ const el = resolveEl(refId, expectedEpoch);
678
+ FIELD.before = null;
679
+ if (!el) return false;
680
+ const target = takesText(document.activeElement) && el.contains(document.activeElement)
681
+ ? document.activeElement
682
+ : el;
683
+ FIELD.before = textOf(target);
684
+ return true;
685
+ }
686
+
687
+ // Choose an option of a native <select> the way the user's pick does: the
688
+ // prototype's selectedIndex setter (framework-tracked instance properties
689
+ // see it), then input + change. Returns { status, value?, label? } where
690
+ // status is 'done', 'unchanged', 'stale', 'not-select', 'multiple',
691
+ // 'disabled', 'no-option' or 'option-disabled'.
692
+ function selectOption(refId, expectedEpoch, value, label) {
693
+ const el = resolveEl(refId, expectedEpoch);
694
+ if (!el) return { status: 'stale' };
695
+ if (!(el instanceof HTMLSelectElement)) return { status: 'not-select', tag: el.tagName.toLowerCase() };
696
+ if (el.multiple) return { status: 'multiple' };
697
+ if (el.matches(':disabled') || el.closest('[inert]')) return { status: 'disabled' };
698
+ const options = [...el.options];
699
+ const index = options.findIndex((option) =>
700
+ value != null ? option.value === value : option.label.trim() === label || option.text.trim() === label,
701
+ );
702
+ // A sensitive select (cc-exp-month, …) is redacted in snapshots; its
703
+ // options and the picked value stay redacted here too.
704
+ const redact = isSensitive(el);
705
+ if (index < 0) {
706
+ return { status: 'no-option', available: redact ? [] : options.map((o) => o.label.trim()).slice(0, 50) };
707
+ }
708
+ const option = options[index];
709
+ if (option.disabled || option.closest('optgroup[disabled]')) return { status: 'option-disabled' };
710
+ const picked = redact
711
+ ? { value: '[value redacted]', label: '[value redacted]' }
712
+ : { value: option.value, label: option.label.trim() };
713
+ if (el.selectedIndex === index) return { status: 'unchanged', ...picked };
714
+ if (typeof el.focus === 'function') el.focus();
715
+ Object.getOwnPropertyDescriptor(HTMLSelectElement.prototype, 'selectedIndex').set.call(el, index);
716
+ el.dispatchEvent(new Event('input', { bubbles: true }));
717
+ el.dispatchEvent(new Event('change', { bubbles: true }));
718
+ return { status: el.selectedIndex === index ? 'done' : 'failed', ...picked };
719
+ }
720
+
721
+ function canScroll(el, dx, dy) {
722
+ const style = getComputedStyle(el);
723
+ const scrollable = (overflow) => overflow === 'auto' || overflow === 'scroll' || overflow === 'overlay';
724
+ return (
725
+ (dy !== 0 && scrollable(style.overflowY) && el.scrollHeight > el.clientHeight) ||
726
+ (dx !== 0 && scrollable(style.overflowX) && el.scrollWidth > el.clientWidth)
727
+ );
728
+ }
729
+
730
+ // Scroll by a delta, or bring a ref into view. With a ref and a delta the
731
+ // ref's own scroll container moves (the ref itself when it scrolls), so a
732
+ // feed in an inner pane can be paged. Returns { moved, x, y, at_end } or
733
+ // { stale: true }.
734
+ function scrollBy(refId, expectedEpoch, dx, dy) {
735
+ const el = refId == null ? null : resolveEl(refId, expectedEpoch);
736
+ if (refId != null && !el) return { stale: true };
737
+ const page = document.scrollingElement || document.documentElement;
738
+ let container = page;
739
+ if (el && (dx !== 0 || dy !== 0)) {
740
+ for (let node = el; node && node !== page; node = node.parentElement) {
741
+ if (canScroll(node, dx, dy)) {
742
+ container = node;
743
+ break;
744
+ }
745
+ }
746
+ }
747
+ const beforeRect = el ? el.getBoundingClientRect() : null;
748
+ const before = [container.scrollLeft, container.scrollTop];
749
+ if (el && dx === 0 && dy === 0) {
750
+ el.scrollIntoView({ block: 'center', inline: 'nearest', behavior: 'instant' });
751
+ } else {
752
+ container.scrollBy({ left: dx, top: dy, behavior: 'instant' });
753
+ }
754
+ const after = [container.scrollLeft, container.scrollTop];
755
+ const afterRect = el ? el.getBoundingClientRect() : null;
756
+ const moved =
757
+ after[0] !== before[0] ||
758
+ after[1] !== before[1] ||
759
+ (beforeRect != null && (afterRect.top !== beforeRect.top || afterRect.left !== beforeRect.left));
760
+ return {
761
+ moved,
762
+ x: Math.round(after[0]),
763
+ y: Math.round(after[1]),
764
+ at_end: Math.ceil(container.scrollTop + container.clientHeight) >= container.scrollHeight,
765
+ };
766
+ }
767
+
768
+ // Trusted keydowns the page received since keysStart: press_key's proof
769
+ // that Chrome delivered the press at all (a background tab can drop it).
770
+ const KEYS = { count: 0, listener: null };
771
+
772
+ function keysStart() {
773
+ if (KEYS.listener) removeEventListener('keydown', KEYS.listener, true);
774
+ KEYS.count = 0;
775
+ KEYS.listener = (event) => {
776
+ if (event.isTrusted) KEYS.count += 1;
777
+ };
778
+ addEventListener('keydown', KEYS.listener, true);
779
+ return true;
780
+ }
781
+
782
+ function keysReceived() {
783
+ return KEYS.count;
784
+ }
785
+
786
+ // hover_ref's read-back: the element saw a trusted mouse event, or matches
787
+ // :hover. A background page delivers mouseover at once but applies :hover
788
+ // only on its next rendered frame, so the event is the reliable witness.
789
+ const POINTER = { el: null, seen: false, listener: null };
790
+
791
+ function pointerStart(refId, expectedEpoch) {
792
+ if (POINTER.listener) removeEventListener('mouseover', POINTER.listener, true);
793
+ const el = resolveEl(refId, expectedEpoch);
794
+ POINTER.el = el;
795
+ POINTER.seen = false;
796
+ POINTER.listener = (event) => {
797
+ if (event.isTrusted && el && el.contains(event.target)) POINTER.seen = true;
798
+ };
799
+ addEventListener('mouseover', POINTER.listener, true);
800
+ return Boolean(el);
801
+ }
802
+
803
+ function hovered(refId, expectedEpoch) {
804
+ const el = resolveEl(refId, expectedEpoch);
805
+ if (POINTER.listener) removeEventListener('mouseover', POINTER.listener, true);
806
+ const seen = POINTER.seen && POINTER.el === el;
807
+ POINTER.listener = null;
808
+ POINTER.el = null;
809
+ if (!el) return 'stale';
810
+ return seen || el.matches(':hover') ? 'confirmed' : 'no_change';
811
+ }
812
+
813
+ globalThis.__lobuExtract = extract;
814
+ globalThis.__lobuFocusBbox = focusBbox;
815
+ globalThis.__lobuBbox = bbox;
816
+ globalThis.__lobuWatchStart = watchStart;
817
+ globalThis.__lobuWatchResult = watchResult;
818
+ globalThis.__lobuFieldBefore = fieldBefore;
819
+ globalThis.__lobuTypedOutcome = typedOutcome;
820
+ globalThis.__lobuSelectOption = selectOption;
821
+ globalThis.__lobuScrollBy = scrollBy;
822
+ globalThis.__lobuHovered = hovered;
823
+ globalThis.__lobuPointerStart = pointerStart;
824
+ globalThis.__lobuKeysStart = keysStart;
825
+ globalThis.__lobuKeysReceived = keysReceived;
826
+ globalThis.__lobuPrepareText = prepareText;
827
+ globalThis.__lobuClickEl = clickEl;
828
+ globalThis.__lobuHitTest = hitTest;
829
+ // Resolve a ref → element (or null if stale/unknown). Unlike focusBbox this
830
+ // returns the element itself so the tool dispatcher can hand it to CDP by
831
+ // reference (Runtime.evaluate returnByValue:false → objectId) for commands
832
+ // that need a node target, e.g. DOM.setFileInputFiles for upload_file.
833
+ globalThis.__lobuResolveEl = resolveEl;
834
+ })();