@agent-om/devtools 0.0.0-stage → 0.1.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.
package/dist/index.js ADDED
@@ -0,0 +1,1964 @@
1
+ import { idOf, isSecretField, merge, nodeById, readSelf, shownTree } from "@agent-om/core";
2
+ import { CONTROLS, changes, line, view, withoutSecrets } from "@agent-om/core/view";
3
+ // src/dom.ts
4
+ /** An element of the panel's, made in the page's document: attributes (true for one with no value), listeners, children. */
5
+ function h(doc, tag, props, ...children) {
6
+ const el = doc.createElement(tag);
7
+ for (const [k, v] of Object.entries(props ?? {})) if (k === "on") for (const [type, fn] of Object.entries(v)) el.addEventListener(type, fn);
8
+ else if (v === true) el.setAttribute(k, "");
9
+ else if (v !== false && v !== void 0 && v !== null) el.setAttribute(k, String(v));
10
+ append(el, children);
11
+ return el;
12
+ }
13
+ function append(el, children) {
14
+ for (const c of children) if (c !== null && c !== void 0 && c !== false) el.append(typeof c === "number" ? String(c) : c);
15
+ }
16
+ /** A number as people write it. */
17
+ const count = (n) => n.toLocaleString("en");
18
+ /** Bytes as KB or MB. */
19
+ function bytes(n) {
20
+ if (n < 1024) return `${n} B`;
21
+ if (n < 1048576) return `${(n / 1024).toFixed(1)} KB`;
22
+ return `${(n / 1024 / 1024).toFixed(1)} MB`;
23
+ }
24
+ /** The element a node is, or the element a text node is in. */
25
+ const elementOf = (n) => !n ? null : n.nodeType === Node.ELEMENT_NODE ? n : n.parentElement;
26
+ /** Where a node's box is: an element's, or a text node's text. */
27
+ function rectOf(n) {
28
+ if (!n.isConnected) return null;
29
+ if (n.nodeType === Node.ELEMENT_NODE) return n.getBoundingClientRect();
30
+ const range = n.ownerDocument.createRange();
31
+ range.selectNodeContents(n);
32
+ return range.getBoundingClientRect();
33
+ }
34
+ // src/json.ts
35
+ /** Items of an array shown at once: more are grouped in runs of this many. */
36
+ const BUCKET = 100;
37
+ /** Characters of a string shown before it's cut. */
38
+ const STRING_MAX = 200;
39
+ var JsonTree = class {
40
+ opened = /* @__PURE__ */ new Set();
41
+ seeded = false;
42
+ /** The rows of a value, as opened now. The first time, a small value opens a few levels deep, a large one less. */
43
+ rows(value) {
44
+ if (!this.seeded) {
45
+ this.seeded = true;
46
+ const depth = JSON.stringify(value).length < 1e5 ? 5 : 3;
47
+ this.openTo(value, "$", depth);
48
+ }
49
+ const out = [];
50
+ this.add(out, value, "$", 0);
51
+ return out;
52
+ }
53
+ /** Open these rows (the ones around a row to show). */
54
+ open(paths) {
55
+ for (const p of paths) this.opened.add(p);
56
+ }
57
+ toggle(path) {
58
+ if (!this.opened.delete(path)) this.opened.add(path);
59
+ }
60
+ openTo(value, path, depth) {
61
+ if (depth <= 0 || !value || typeof value !== "object") return;
62
+ this.opened.add(path);
63
+ if (Array.isArray(value)) value.slice(0, BUCKET).forEach((v, i) => this.openTo(v, `${path}[${i}]`, depth - 1));
64
+ else for (const [k, v] of Object.entries(value)) this.openTo(v, `${path}.${k}`, depth - 1);
65
+ }
66
+ add(out, value, path, depth, key) {
67
+ const isArray = Array.isArray(value);
68
+ const kind = value && typeof value === "object" ? isArray ? "array" : "object" : "leaf";
69
+ const open = kind !== "leaf" && this.opened.has(path);
70
+ out.push({
71
+ path,
72
+ depth,
73
+ key,
74
+ value,
75
+ kind,
76
+ open
77
+ });
78
+ if (!open) return;
79
+ if (isArray) {
80
+ const items = value;
81
+ if (items.length <= BUCKET) items.forEach((v, i) => this.add(out, v, `${path}[${i}]`, depth + 1, String(i)));
82
+ else for (let from = 0; from < items.length; from += BUCKET) {
83
+ const to = Math.min(items.length, from + BUCKET) - 1;
84
+ const run = `${path}[${from}…${to}]`;
85
+ const runOpen = this.opened.has(run);
86
+ out.push({
87
+ path: run,
88
+ depth: depth + 1,
89
+ key: `[${from}…${to}]`,
90
+ value: items.slice(from, to + 1),
91
+ kind: "run",
92
+ open: runOpen
93
+ });
94
+ if (runOpen) for (let i = from; i <= to; i++) this.add(out, items[i], `${path}[${i}]`, depth + 2, String(i));
95
+ }
96
+ } else for (const [k, v] of Object.entries(value)) this.add(out, v, `${path}.${k}`, depth + 1, k);
97
+ }
98
+ };
99
+ /** A row of the tree, drawn: its twisty, its key, and its value or what it holds. */
100
+ function jsonRow(doc, row, onToggle) {
101
+ const el = doc.createElement("div");
102
+ el.append(" ".repeat(row.depth));
103
+ const twisty = h(doc, "span", { class: "tw" }, row.kind === "leaf" ? "" : row.open ? "▾" : "▸");
104
+ if (row.kind !== "leaf") twisty.addEventListener("click", () => onToggle(row.path));
105
+ el.append(twisty);
106
+ if (row.key !== void 0) el.append(h(doc, "span", { class: "a" }, row.key), ": ");
107
+ const v = row.value;
108
+ if (row.kind === "leaf") {
109
+ if (typeof v === "string") el.append(h(doc, "span", { class: "n" }, JSON.stringify(v.length > STRING_MAX ? `${v.slice(0, STRING_MAX)}…` : v) + (v.length > STRING_MAX ? ` (${v.length - STRING_MAX} more)` : "")));
110
+ else el.append(h(doc, "span", { class: "e" }, String(v)));
111
+ } else if (row.kind === "array" || row.kind === "run") el.append(h(doc, "span", { class: "why" }, `${v.length} items`));
112
+ else {
113
+ const n = v;
114
+ if (typeof n.role === "string") {
115
+ el.append(h(doc, "span", { class: "r" }, n.role));
116
+ if (n.name) el.append(" ", h(doc, "span", { class: "n" }, JSON.stringify(n.name.length > 80 ? `${n.name.slice(0, 80)}…` : n.name)));
117
+ if (n.ref) el.append(" ", h(doc, "span", { class: "e" }, n.ref));
118
+ } else el.append(h(doc, "span", { class: "why" }, `{${Object.keys(v).length}}`));
119
+ }
120
+ return el;
121
+ }
122
+ /** Rows drawn beyond those in sight, each way. */
123
+ const OVERSCAN = 12;
124
+ var VirtualList = class {
125
+ el;
126
+ rows;
127
+ doc;
128
+ count = 0;
129
+ hooks;
130
+ active = -1;
131
+ selected = -1;
132
+ keying = false;
133
+ /** where the list is scrolled to, kept apart from the element: one taken out and put back is at the top again */
134
+ top = 0;
135
+ id;
136
+ constructor(doc, label, id) {
137
+ this.doc = doc;
138
+ this.id = id;
139
+ this.hooks = { row: () => this.doc.createElement("div") };
140
+ this.rows = h(doc, "div", { class: "rows" });
141
+ this.el = h(doc, "div", {
142
+ class: "list",
143
+ role: "listbox",
144
+ tabindex: 0,
145
+ "aria-label": label
146
+ }, this.rows);
147
+ this.el.addEventListener("scroll", () => {
148
+ if (!this.el.isConnected) return;
149
+ this.top = this.el.scrollTop;
150
+ this.paint();
151
+ });
152
+ this.el.addEventListener("keydown", (e) => this.key(e));
153
+ this.el.addEventListener("pointermove", () => this.keying = false);
154
+ this.el.addEventListener("mouseover", (e) => {
155
+ if (this.keying) return;
156
+ const r = e.target.closest(".row");
157
+ this.hooks.hover?.(r ? Number(r.dataset.i) : -1);
158
+ });
159
+ this.el.addEventListener("mouseleave", () => !this.keying && this.hooks.hover?.(-1));
160
+ this.el.addEventListener("click", (e) => {
161
+ const root = this.el.getRootNode();
162
+ const selection = "getSelection" in root && root.getSelection ? root.getSelection() : this.doc.getSelection();
163
+ if (selection && !selection.isCollapsed && selection.toString().trim()) return;
164
+ const r = e.target.closest(".row");
165
+ if (r && !e.target.closest(".tw")) this.hooks.pick?.(Number(r.dataset.i));
166
+ });
167
+ }
168
+ /** Show these rows, keeping the scroll where it was. */
169
+ set(count, hooks, selected = -1) {
170
+ this.count = count;
171
+ this.hooks = hooks;
172
+ this.selected = selected;
173
+ if (this.active >= count) this.active = -1;
174
+ this.rows.style.height = `${count * 20}px`;
175
+ this.paint();
176
+ }
177
+ /** Scroll a row into sight: one step for the keys, to the middle for a row out of sight (`middle`). */
178
+ reveal(i, middle = false) {
179
+ if (i < 0) return;
180
+ const top = i * 20;
181
+ if ((top < this.el.scrollTop || top + 20 > this.el.scrollTop + this.el.clientHeight) && middle) this.el.scrollTop = Math.max(0, top - (this.el.clientHeight - 20) / 2);
182
+ else if (top < this.el.scrollTop) this.el.scrollTop = top;
183
+ else if (top + 20 > this.el.scrollTop + this.el.clientHeight) this.el.scrollTop = top + 20 - this.el.clientHeight;
184
+ this.top = this.el.scrollTop;
185
+ this.paint();
186
+ }
187
+ /** Put the list back where it was scrolled to, after it was put (back) on the page, and draw what's in sight there. */
188
+ placed() {
189
+ if (this.el.scrollTop !== this.top) this.el.scrollTop = this.top;
190
+ this.top = this.el.scrollTop;
191
+ this.paint();
192
+ }
193
+ /** Scroll the selected row into sight, to the middle. */
194
+ revealSelected() {
195
+ this.reveal(this.selected, true);
196
+ }
197
+ paint() {
198
+ const top = this.el.isConnected ? this.el.scrollTop : this.top;
199
+ const height = this.el.clientHeight || 400;
200
+ const first = Math.max(0, Math.floor(top / 20) - OVERSCAN);
201
+ const last = Math.min(this.count, Math.ceil((top + height) / 20) + OVERSCAN);
202
+ const out = [];
203
+ for (let i = first; i < last; i++) {
204
+ const r = this.hooks.row(i);
205
+ r.classList.add("row");
206
+ if (i === this.selected) r.classList.add("sel");
207
+ if (i === this.active) r.classList.add("active");
208
+ r.dataset.i = String(i);
209
+ r.id = `${this.id}-${i}`;
210
+ r.setAttribute("role", "option");
211
+ r.setAttribute("aria-selected", String(i === this.selected));
212
+ r.style.top = `${i * 20}px`;
213
+ out.push(r);
214
+ }
215
+ this.rows.replaceChildren(...out);
216
+ if (this.active >= 0) this.el.setAttribute("aria-activedescendant", `${this.id}-${this.active}`);
217
+ else this.el.removeAttribute("aria-activedescendant");
218
+ }
219
+ key(e) {
220
+ if (!this.count) return;
221
+ const page = Math.max(1, Math.floor(this.el.clientHeight / 20) - 1);
222
+ const from = this.active >= 0 ? this.active : this.selected;
223
+ const to = {
224
+ ArrowDown: from + 1,
225
+ ArrowUp: from - 1,
226
+ PageDown: from + page,
227
+ PageUp: from - page,
228
+ Home: 0,
229
+ End: this.count - 1
230
+ };
231
+ if (e.key in to) {
232
+ e.preventDefault();
233
+ this.keying = true;
234
+ this.active = Math.max(0, Math.min(this.count - 1, to[e.key]));
235
+ this.reveal(this.active);
236
+ this.hooks.hover?.(this.active);
237
+ } else if (e.key === "Enter" && this.active >= 0) {
238
+ e.preventDefault();
239
+ this.hooks.pick?.(this.active);
240
+ }
241
+ }
242
+ };
243
+ /** A line of the view as coloured spans: its role, quoted names, refs, the facts after them. */
244
+ function viewLine(doc, text) {
245
+ const row = doc.createElement("div");
246
+ const m = /^(\s*)(- |\+ |~ )?([A-Za-z][\w-]*)?(.*)$/s.exec(text);
247
+ if (!m) {
248
+ row.textContent = text;
249
+ return row;
250
+ }
251
+ const [, indent, dash, role, rest] = m;
252
+ row.append(indent + (dash ?? ""));
253
+ if (role) row.append(h(doc, "span", { class: "r" }, role));
254
+ for (const part of rest.split(/(\[e\d+\]|"(?:[^"\\]|\\.)*"|\b[a-z][\w-]*=)/)) {
255
+ if (!part) continue;
256
+ if (/^\[e\d+\]$/.test(part)) row.append(h(doc, "span", { class: "e" }, part));
257
+ else if (part.startsWith("\"")) row.append(h(doc, "span", { class: "n" }, part));
258
+ else if (/^[a-z][\w-]*=$/.test(part)) row.append(h(doc, "span", { class: "a" }, part));
259
+ else row.append(part);
260
+ }
261
+ return row;
262
+ }
263
+ /** The first ref a line names. */
264
+ const refIn = (text) => /\[(e\d+)\]/.exec(text)?.[1];
265
+ // src/overlay.ts
266
+ /** Most ref labels drawn at once: a page with thousands in sight shows the first ones. */
267
+ const LABELS_MAX = 600;
268
+ var Overlay = class {
269
+ hoverBox;
270
+ label;
271
+ selBox;
272
+ refs;
273
+ hovered = null;
274
+ hoverText = "";
275
+ selected = null;
276
+ frame = 0;
277
+ last = "";
278
+ refsOn = false;
279
+ read;
280
+ refsDirty = false;
281
+ constructor(doc, layer) {
282
+ this.hoverBox = h(doc, "div", {
283
+ class: "box",
284
+ hidden: true
285
+ });
286
+ this.selBox = h(doc, "div", {
287
+ class: "box sel",
288
+ hidden: true
289
+ });
290
+ this.label = h(doc, "div", {
291
+ class: "label",
292
+ hidden: true
293
+ });
294
+ this.refs = h(doc, "div");
295
+ layer.append(this.refs, this.selBox, this.hoverBox, this.label);
296
+ const view = doc.defaultView;
297
+ const moved = () => {
298
+ this.refsDirty = true;
299
+ this.loop();
300
+ };
301
+ view.addEventListener("scroll", moved, {
302
+ capture: true,
303
+ passive: true
304
+ });
305
+ view.addEventListener("resize", moved, { passive: true });
306
+ }
307
+ /** Outline what the pointer or the keys rest on, labelled. */
308
+ hover(n, text = "") {
309
+ this.hovered = n;
310
+ this.hoverText = text;
311
+ this.loop();
312
+ }
313
+ select(n) {
314
+ this.selected = n;
315
+ this.loop();
316
+ }
317
+ /** Label each ref of this read that can be acted on, on its element (or stop). */
318
+ showRefs(on, read) {
319
+ this.refsOn = on;
320
+ if (read) this.read = read;
321
+ this.refsDirty = true;
322
+ if (!on) this.refs.replaceChildren();
323
+ this.loop();
324
+ }
325
+ loop() {
326
+ if (this.frame) return;
327
+ this.frame = requestAnimationFrame(() => {
328
+ this.frame = 0;
329
+ this.draw();
330
+ if (this.hovered || this.selected) this.loop();
331
+ });
332
+ }
333
+ draw() {
334
+ const hr = this.hovered && rectOf(this.hovered);
335
+ const sr = this.selected && rectOf(this.selected);
336
+ const key = `${rect(hr)}|${rect(sr)}|${this.hoverText}`;
337
+ if (key !== this.last) {
338
+ this.last = key;
339
+ place(this.hoverBox, hr);
340
+ place(this.selBox, sr);
341
+ this.placeLabel(hr);
342
+ }
343
+ if (this.refsDirty && this.refsOn && this.read) {
344
+ this.refsDirty = false;
345
+ this.drawRefs(this.read);
346
+ }
347
+ }
348
+ placeLabel(r) {
349
+ if (!r || !this.hoverText) {
350
+ this.label.hidden = true;
351
+ return;
352
+ }
353
+ this.label.textContent = this.hoverText;
354
+ this.label.hidden = false;
355
+ const height = this.label.offsetHeight;
356
+ const width = this.label.offsetWidth;
357
+ const vh = innerHeight;
358
+ let top = r.bottom + 4;
359
+ if (top + height > vh) top = r.top - height - 4;
360
+ if (top < 0) top = Math.max(0, vh - height);
361
+ this.label.style.top = `${top}px`;
362
+ this.label.style.left = `${Math.max(4, Math.min(r.left, innerWidth - width - 8))}px`;
363
+ }
364
+ /** Each ref of the read whose element is in sight, as a small label at its top right corner. */
365
+ drawRefs(read) {
366
+ const labels = [];
367
+ const doc = this.refs.ownerDocument;
368
+ for (const [ref, node] of read.nodes) {
369
+ if (labels.length >= LABELS_MAX) break;
370
+ if (!CONTROLS.has(node.role) && !node.clickable && !node.focusable && !node.editable) continue;
371
+ const el = nodeById(Number(ref.slice(1)));
372
+ if (!el || el.nodeType !== Node.ELEMENT_NODE) continue;
373
+ const r = el.getBoundingClientRect();
374
+ if (!r.width || !r.height || r.bottom < 0 || r.top > innerHeight || r.right < 0 || r.left > innerWidth) continue;
375
+ const label = h(doc, "span", { class: node.clickable || node.focusable || !node.name && !!node.hint ? "ref unnamed" : "ref" }, ref);
376
+ label.style.top = `${Math.max(0, r.top - 6)}px`;
377
+ label.style.left = `${Math.min(innerWidth - 40, r.right - 18)}px`;
378
+ labels.push(label);
379
+ }
380
+ this.refs.replaceChildren(...labels);
381
+ }
382
+ };
383
+ const rect = (r) => r ? `${r.x},${r.y},${r.width},${r.height}` : "";
384
+ function place(box, r) {
385
+ if (!r || !r.width && !r.height) {
386
+ box.hidden = true;
387
+ return;
388
+ }
389
+ box.hidden = false;
390
+ Object.assign(box.style, {
391
+ top: `${r.top}px`,
392
+ left: `${r.left}px`,
393
+ width: `${r.width}px`,
394
+ height: `${r.height}px`
395
+ });
396
+ }
397
+ /**
398
+ * Picking an element on the page: while on, the page gets no pointer event (the panel takes them all, in the capture
399
+ * phase, before the app's own listeners), the element under the pointer is outlined, and a press picks it. Escape
400
+ * stops. Elements in open shadow roots are reached through the event's path.
401
+ */
402
+ var Picker = class {
403
+ on = false;
404
+ win;
405
+ host;
406
+ onHover;
407
+ onPick;
408
+ /** `exact`: the element itself was asked for (Alt held), not the nearest the model has a line for */
409
+ constructor(win, host, onHover, onPick) {
410
+ this.win = win;
411
+ this.host = host;
412
+ this.onHover = onHover;
413
+ this.onPick = onPick;
414
+ }
415
+ get active() {
416
+ return this.on;
417
+ }
418
+ start() {
419
+ if (this.on) return;
420
+ this.on = true;
421
+ for (const type of SWALLOWED) this.win.addEventListener(type, this.swallow, true);
422
+ this.win.addEventListener("pointermove", this.move, true);
423
+ this.win.addEventListener("keydown", this.key, true);
424
+ }
425
+ stop() {
426
+ if (!this.on) return;
427
+ this.on = false;
428
+ this.release();
429
+ this.win.removeEventListener("pointermove", this.move, true);
430
+ this.win.removeEventListener("keydown", this.key, true);
431
+ this.onHover(null, false);
432
+ }
433
+ /** Give the page its pointer events back. */
434
+ release() {
435
+ if (this.on) return;
436
+ for (const type of SWALLOWED) this.win.removeEventListener(type, this.swallow, true);
437
+ }
438
+ target = (e) => {
439
+ const path = e.composedPath();
440
+ if (path.includes(this.host)) return null;
441
+ const first = path[0];
442
+ return first instanceof Element ? first : first instanceof Node ? first.parentElement : null;
443
+ };
444
+ swallow = (e) => {
445
+ if (e.composedPath().includes(this.host)) return;
446
+ e.preventDefault();
447
+ e.stopImmediatePropagation();
448
+ if (e.type === "pointerdown" && this.on) {
449
+ const el = this.target(e);
450
+ this.on = false;
451
+ this.win.removeEventListener("pointermove", this.move, true);
452
+ this.win.removeEventListener("keydown", this.key, true);
453
+ this.onHover(null, false);
454
+ this.onPick(el, e.altKey);
455
+ this.win.setTimeout(() => this.release(), 600);
456
+ } else if (e.type === "click" && !this.on) this.release();
457
+ };
458
+ move = (e) => {
459
+ const el = this.target(e);
460
+ if (el) e.stopImmediatePropagation();
461
+ this.onHover(el, e.altKey);
462
+ };
463
+ key = (e) => {
464
+ if (e.key !== "Escape") return;
465
+ e.preventDefault();
466
+ e.stopImmediatePropagation();
467
+ this.stop();
468
+ this.onPick(null, false);
469
+ };
470
+ };
471
+ /** The pointer events a pick keeps from the page: a press, its release and the click it makes. */
472
+ const SWALLOWED = [
473
+ "pointerdown",
474
+ "pointerup",
475
+ "mousedown",
476
+ "mouseup",
477
+ "click",
478
+ "dblclick",
479
+ "contextmenu",
480
+ "auxclick"
481
+ ];
482
+ // src/reader.ts
483
+ /** How many steps are kept: the oldest go. */
484
+ const STEPS = 100;
485
+ var Reader = class {
486
+ doc;
487
+ /** every value seen in a secret field, kept for the page's life */
488
+ filled = /* @__PURE__ */ new Set();
489
+ last;
490
+ steps = [];
491
+ /** what the developer did last, before the next read: described by the read before it */
492
+ action;
493
+ constructor(doc) {
494
+ this.doc = doc;
495
+ }
496
+ read() {
497
+ const t0 = performance.now();
498
+ const raw = readSelf(this.doc);
499
+ const t1 = performance.now();
500
+ this.remember(raw);
501
+ const filled = [...this.filled];
502
+ const projection = merge(raw, filled);
503
+ const t2 = performance.now();
504
+ const nodes = /* @__PURE__ */ new Map();
505
+ const index = (n) => {
506
+ if (n.ref) nodes.set(n.ref, n);
507
+ n.children?.forEach(index);
508
+ };
509
+ index(projection.root);
510
+ let tree;
511
+ const read = {
512
+ raw,
513
+ projection,
514
+ text: view(projection),
515
+ nodes,
516
+ ms: {
517
+ read: t1 - t0,
518
+ merge: t2 - t1
519
+ },
520
+ elements: raw.dom.documents[0]?.nodes.nodeType.filter((t) => t === 1).length ?? 0,
521
+ secrets: filled,
522
+ tree: () => {
523
+ if (!tree) {
524
+ const { root } = shownTree(raw, filled);
525
+ const byNode = /* @__PURE__ */ new Map();
526
+ const add = (n) => {
527
+ if (n.node !== void 0) byNode.set(n.node, n);
528
+ n.children.forEach(add);
529
+ };
530
+ add(root);
531
+ tree = {
532
+ root,
533
+ byNode
534
+ };
535
+ }
536
+ return tree;
537
+ }
538
+ };
539
+ this.record(this.last, read);
540
+ this.last = read;
541
+ return read;
542
+ }
543
+ /** What was just done on the page: the next read that finds a change records it as a step. */
544
+ did(kind, target, key) {
545
+ this.action = {
546
+ kind,
547
+ target,
548
+ key
549
+ };
550
+ }
551
+ /** A secret field's value, wherever the read found one (open shadow roots too), kept for the page's life. */
552
+ remember(raw) {
553
+ const doc = raw.dom.documents[0];
554
+ if (!doc) return;
555
+ const s = raw.dom.strings;
556
+ for (const rare of [doc.nodes.inputValue, doc.nodes.textValue]) rare?.index.forEach((i, k) => {
557
+ const value = s[rare.value[k]];
558
+ if (!value || value.length < 3 || this.filled.has(value)) return;
559
+ const el = nodeById(doc.nodes.backendNodeId[i]);
560
+ if (el instanceof Element && isSecretField(el)) this.filled.add(value);
561
+ });
562
+ }
563
+ record(before, after) {
564
+ const action = this.action;
565
+ this.action = void 0;
566
+ if (!before) {
567
+ this.push({
568
+ at: Date.now(),
569
+ what: `page read · ${pathOf(after.projection.url)}`,
570
+ text: after.text,
571
+ whole: true
572
+ });
573
+ return;
574
+ }
575
+ const c = changes(before.projection, after.projection);
576
+ if (!c.whole && c.none) return;
577
+ const what = action ? `${action.kind}${action.key ? ` ${action.key}` : ""} · ${describe(action.target, before, after)}` : "the page changed by itself";
578
+ this.push({
579
+ at: Date.now(),
580
+ what: withoutSecrets(what, after.secrets),
581
+ text: c.whole ? after.text : c.text,
582
+ whole: c.whole
583
+ });
584
+ }
585
+ push(step) {
586
+ this.steps.unshift(step);
587
+ if (this.steps.length > STEPS) this.steps.length = STEPS;
588
+ }
589
+ };
590
+ /**
591
+ * An element as the read before an action saw it, or the read after where the one before didn't hold it (it came
592
+ * after that read, in a menu that had just opened): the line of it, or of the nearest element around it with one.
593
+ */
594
+ function describe(el, ...reads) {
595
+ for (const read of reads) for (let e = el; e; e = e.parentElement ?? e.getRootNode().host ?? null) {
596
+ const node = read.nodes.get(`e${idOf(e)}`);
597
+ if (node && (e === el || read === reads.at(-1))) return line(node, read.projection.url);
598
+ }
599
+ return `<${el.localName}>`;
600
+ }
601
+ const pathOf = (url) => {
602
+ try {
603
+ return new URL(url).pathname;
604
+ } catch {
605
+ return url;
606
+ }
607
+ };
608
+ // src/styles.ts
609
+ const CSS = `
610
+ :host { all: initial; }
611
+ .root {
612
+ --bg: #fbfcfd; --bar: #eef2f6; --fg: #1b2530; --dim: #6b7685; --line: #d9dfe6; --sel: #dde9fb;
613
+ --accent: #1f5fbf; --hover: #c2620a; --ok: #2f7d4f; --stop: #b4381f;
614
+ --role: #6a3fb5; --name: #12704a; --attr: #8a5a00; --ink: #ffffff;
615
+ --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
616
+ --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
617
+ font-family: var(--sans); font-size: 12.5px; line-height: 1.4; color: var(--fg); color-scheme: light;
618
+ }
619
+ @media (prefers-color-scheme: dark) {
620
+ .root:not([data-theme="light"]) {
621
+ --bg: #141a21; --bar: #1b232c; --fg: #dbe2ea; --dim: #7d8a99; --line: #2a3440; --sel: #22344d;
622
+ --accent: #7fb0ff; --hover: #f59e42; --ok: #6cc497; --stop: #f08a70;
623
+ --role: #c4a3ff; --name: #7fd4a8; --attr: #e3b45c; --ink: #141a21; color-scheme: dark;
624
+ }
625
+ }
626
+ .root[data-theme="dark"] {
627
+ --bg: #141a21; --bar: #1b232c; --fg: #dbe2ea; --dim: #7d8a99; --line: #2a3440; --sel: #22344d;
628
+ --accent: #7fb0ff; --hover: #f59e42; --ok: #6cc497; --stop: #f08a70;
629
+ --role: #c4a3ff; --name: #7fd4a8; --attr: #e3b45c; --ink: #141a21; color-scheme: dark;
630
+ }
631
+ * { box-sizing: border-box; }
632
+ button, input, select { font: inherit; color: inherit; }
633
+ [hidden] { display: none !important; }
634
+
635
+ /* the boxes drawn over the page: never in the way of a click */
636
+ .layer { position: fixed; inset: 0; pointer-events: none; z-index: 1; }
637
+ .box { position: fixed; border-radius: 3px; outline: 2px dashed var(--hover); outline-offset: 1px; background: color-mix(in srgb, var(--hover) 10%, transparent); }
638
+ .box.sel { outline: 2px solid var(--accent); background: color-mix(in srgb, var(--accent) 7%, transparent); }
639
+ .label { position: fixed; max-width: min(640px, calc(100vw - 16px)); overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
640
+ font: 11.5px/1.5 var(--mono); background: var(--fg); color: var(--bg); padding: 1px 7px; border-radius: 4px; }
641
+ .ref { position: fixed; font: 10px/1 var(--mono); background: var(--accent); color: var(--ink); padding: 2px 3px; border-radius: 3px; white-space: nowrap; }
642
+ .ref.unnamed { background: var(--hover); }
643
+
644
+ .trigger { position: fixed; z-index: 2; width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line);
645
+ background: var(--fg); color: var(--bg); font-weight: 600; font-size: 13px; cursor: pointer; box-shadow: 0 4px 14px rgb(0 0 0 / .22); }
646
+ .trigger:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
647
+ .badge { position: absolute; top: -3px; right: -3px; min-width: 16px; background: var(--hover); color: #fff; font: 10px/16px var(--mono); border-radius: 999px; padding: 0 4px; }
648
+
649
+ .dock { position: fixed; z-index: 2; background: var(--bg); color: var(--fg); display: grid; grid-template-rows: auto auto 1fr; box-shadow: 0 -2px 12px rgb(0 0 0 / .12); }
650
+ .dock.bottom { left: 0; right: 0; bottom: 0; border-top: 1px solid var(--line); }
651
+ .dock.right { top: 0; right: 0; bottom: 0; border-left: 1px solid var(--line); }
652
+ .grip { position: absolute; background: transparent; z-index: 1; }
653
+ .dock.bottom .grip { left: 0; right: 0; top: -3px; height: 6px; cursor: ns-resize; }
654
+ .dock.right .grip { top: 0; bottom: 0; left: -3px; width: 6px; cursor: ew-resize; }
655
+ .grip:focus-visible { background: var(--accent); }
656
+ .bar { display: flex; align-items: center; gap: 4px; background: var(--bar); padding: 0 6px; min-height: 34px; border-bottom: 1px solid var(--line); overflow-x: auto; }
657
+ .dock.right .bar { flex-wrap: wrap; padding-block: 2px; }
658
+ .brand { font-weight: 650; padding: 0 8px 0 4px; white-space: nowrap; }
659
+ .tab { border: 0; background: none; color: var(--dim); padding: 0 9px; height: 34px; border-bottom: 2px solid transparent; cursor: pointer; white-space: nowrap; }
660
+ .tab[aria-selected="true"] { color: var(--fg); border-bottom-color: var(--accent); }
661
+ .grow { flex: 1; }
662
+ .tool { border: 1px solid var(--line); background: var(--bg); border-radius: 5px; padding: 2px 8px; cursor: pointer; white-space: nowrap; display: inline-flex; align-items: center; gap: 5px; }
663
+ .tool[aria-pressed="true"] { background: var(--sel); border-color: var(--accent); }
664
+ .tab:focus-visible, .tool:focus-visible, .list:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
665
+ .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--dim); }
666
+ .tool[aria-pressed="true"] .dot { background: var(--ok); }
667
+ .sizes { display: flex; flex-wrap: wrap; gap: 4px 14px; align-items: center; padding: 4px 10px; font: 11.5px var(--mono); color: var(--dim); border-bottom: 1px solid var(--line); }
668
+ .sizes b { color: var(--fg); font-weight: 500; }
669
+ .sizes .note { font-family: var(--sans); color: var(--hover); }
670
+ .opts { margin-left: auto; display: flex; gap: 10px; align-items: center; font-family: var(--sans); color: var(--fg); }
671
+ .opts label { display: inline-flex; gap: 4px; align-items: center; white-space: nowrap; }
672
+ .opts input[type=text] { border: 1px solid var(--line); background: var(--bg); border-radius: 4px; padding: 1px 6px; width: 170px; }
673
+ .chip { font: 11px var(--mono); background: var(--sel); border-radius: 4px; padding: 0 2px 0 6px; display: inline-flex; gap: 2px; align-items: center; }
674
+ .chip button { border: 0; background: none; cursor: pointer; padding: 0 4px; }
675
+ .body { display: grid; grid-template-columns: minmax(0, 1fr) 5px var(--side, 300px); min-height: 0; }
676
+ .dock.right .body { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) 5px var(--side, 260px); }
677
+ /* between the list and the side: dragged, or moved with the arrow keys */
678
+ .split-grip { cursor: col-resize; border-left: 1px solid var(--line); }
679
+ .dock.right .body > .split-grip { cursor: row-resize; border-left: 0; border-top: 1px solid var(--line); }
680
+ .split-grip:hover, .split-grip:focus-visible { background: color-mix(in srgb, var(--accent) 35%, transparent); outline: none; }
681
+ .main { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
682
+ .side { overflow: auto; padding: 8px 10px 12px; min-height: 0; }
683
+
684
+ /* the lists: fixed rows, only those in sight drawn */
685
+ .list { position: relative; flex: 1; min-height: 0; overflow: auto; font: 12px/20px var(--mono); }
686
+ .rows { position: relative; }
687
+ .row { position: absolute; left: 0; min-width: 100%; height: 20px; padding: 0 10px; white-space: pre; cursor: default; }
688
+ .row:hover, .row.active { background: color-mix(in srgb, var(--hover) 10%, transparent); }
689
+ .row.sel { background: var(--sel); }
690
+ .row.dim { color: var(--dim); }
691
+ .empty { padding: 14px; color: var(--dim); font-family: var(--sans); }
692
+ .r { color: var(--role); }
693
+ .n { color: var(--name); }
694
+ .e { color: var(--accent); }
695
+ .a { color: var(--attr); }
696
+ .why { color: var(--dim); font-style: italic; }
697
+ .add { color: var(--ok); }
698
+ .del { color: var(--stop); }
699
+ .was { color: var(--dim); }
700
+ .tw { display: inline-block; width: 14px; color: var(--dim); cursor: pointer; }
701
+
702
+ .split { display: grid; grid-template-columns: var(--steps, 240px) 5px minmax(0, 1fr); min-height: 0; flex: 1; }
703
+ .steps { overflow: auto; }
704
+ .step { display: block; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--line); background: none; padding: 5px 10px; cursor: pointer; }
705
+ .step[aria-current="true"] { background: var(--sel); }
706
+ .step small { display: block; color: var(--dim); font: 10.5px var(--mono); }
707
+ .tabbar { display: flex; gap: 6px; align-items: center; padding: 4px 10px; border-bottom: 1px solid var(--line); font: 11.5px var(--mono); color: var(--dim); min-height: 29px; }
708
+ .tabbar .path { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
709
+
710
+ .side h3 { margin: 6px 0 5px; font-size: 10.5px; letter-spacing: .07em; text-transform: uppercase; color: var(--dim); font-weight: 600; }
711
+ .side .line { font: 11.5px/1.45 var(--mono); background: var(--bar); border-radius: 4px; padding: 4px 6px; white-space: pre-wrap; word-break: break-word; }
712
+ .trail { list-style: none; margin: 0; padding: 0; }
713
+ .trail li { display: grid; grid-template-columns: 16px 74px minmax(0, 1fr); gap: 4px; padding: 3px 0; border-bottom: 1px dashed var(--line); }
714
+ .trail .yes { color: var(--ok); font-weight: 700; }
715
+ .trail .no { color: var(--stop); font-weight: 700; }
716
+ .trail .stage { color: var(--dim); }
717
+ .trail .d { font: 11px/1.45 var(--mono); word-break: break-word; }
718
+ .side .note { font-size: 12px; background: color-mix(in srgb, var(--hover) 12%, transparent); border-radius: 4px; padding: 5px 7px; margin-top: 8px; }
719
+ .side dl { margin: 0; display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 2px 8px; font: 11px var(--mono); }
720
+ .side dt { color: var(--attr); }
721
+ .side dd { margin: 0; word-break: break-word; }
722
+ .side .actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
723
+ `;
724
+ // src/where.ts
725
+ /** Attributes worth showing of an element's markup; never a field's value. */
726
+ const SHOWN = /^(id|class|role|type|name|for|href|alt|title|tabindex|contenteditable|disabled|hidden|inert|popover|open|aria-.*|data-agentom-.*)$/;
727
+ /** How much of an attribute's value is shown. */
728
+ const VALUE_MAX = 60;
729
+ /** An element's opening tag, with the attributes that say what it is, secrets taken out. */
730
+ function tagOf(el, secrets) {
731
+ const attrs = [...el.attributes].filter((a) => SHOWN.test(a.name) && !(a.name === "value")).map((a) => {
732
+ const v = a.value.length > VALUE_MAX ? `${a.value.slice(0, VALUE_MAX)}…` : a.value;
733
+ return a.value === "" ? a.name : `${a.name}="${v}"`;
734
+ });
735
+ return withoutSecrets(`<${el.localName}${attrs.length ? " " + attrs.join(" ") : ""}>`, secrets);
736
+ }
737
+ /** The element around this one, across a shadow root's edge. */
738
+ const up = (el) => el.parentElement ?? (el.getRootNode() instanceof ShadowRoot ? el.getRootNode().host : null);
739
+ /** Why the tree may hold nothing of an element: the first fact, from the element out, that the walk leaves things out for. */
740
+ function leftOutBecause(el, secrets) {
741
+ for (let e = el; e; e = up(e)) {
742
+ const at = e === el ? "" : ` on ${tagOf(e, secrets)}`;
743
+ if (e.hasAttribute("data-agentom-ignore")) return `kept out by the app: data-agentom-ignore${at}`;
744
+ if (e.hasAttribute("data-agentom-redact") && !e.querySelector("input, textarea, select, [contenteditable]")) return `kept out by the app: data-agentom-redact${at}`;
745
+ if (e.getAttribute("aria-hidden") === "true") return `aria-hidden="true"${at}`;
746
+ if (e.hasAttribute("inert")) return `inert${at}`;
747
+ }
748
+ if (!el.checkVisibility({
749
+ contentVisibilityAuto: false,
750
+ visibilityProperty: true
751
+ })) return "not drawn: display: none, visibility or content-visibility on it or around it";
752
+ const modal = el.ownerDocument.querySelector("dialog:modal");
753
+ if (modal && !modal.contains(el)) return "outside the open modal dialog";
754
+ return "the walk keeps no node for it: a script, a style, a field’s inside, whitespace";
755
+ }
756
+ /** The nearest element around this one that the projection has a node for. */
757
+ function holder(el, read) {
758
+ for (let e = up(el); e; e = up(e)) {
759
+ const n = read.nodes.get(`e${idOf(e)}`);
760
+ if (n) return n;
761
+ }
762
+ }
763
+ /** Whether an element is inside another, across shadow roots' edges. */
764
+ function within(outer, el) {
765
+ for (let e = up(el); e; e = up(e)) if (e === outer) return true;
766
+ return false;
767
+ }
768
+ /** The view's lines for what's inside an element: those whose ref is an element inside it, in order. */
769
+ function linesInside(el, lines) {
770
+ const out = [];
771
+ lines.forEach((l, i) => {
772
+ const m = /\[e(\d+)\]/.exec(l);
773
+ const e = m ? elementOf(nodeById(Number(m[1]))) : null;
774
+ if (e && within(el, e)) out.push(i);
775
+ });
776
+ return out;
777
+ }
778
+ /** A projection node's own fields, without what it holds: what the panel shows of it. */
779
+ function fields(n) {
780
+ const { children, ...own } = n;
781
+ return JSON.stringify(own) + (children?.length ? ` + ${children.length} inside` : "");
782
+ }
783
+ /** Where an element went, by the read given. `shown`: the view as the panel shows it now (with its options). */
784
+ function trail(el, read, shown) {
785
+ const secrets = read.secrets;
786
+ const ref = `e${idOf(el)}`;
787
+ const base = read.projection.url;
788
+ const stages = [{
789
+ stage: "DOM",
790
+ reached: true,
791
+ detail: tagOf(el, secrets)
792
+ }];
793
+ const attributes = [...el.attributes].filter((a) => a.name.startsWith("data-agentom-")).map((a) => [a.name, withoutSecrets(a.value, secrets)]);
794
+ const warnings = read.projection.warnings?.filter((w) => w.target === ref) ?? [];
795
+ const done = (more, own) => ({
796
+ ref: own ? ref : void 0,
797
+ line: own ? line(own, base) : void 0,
798
+ stages: [...stages, ...more],
799
+ attributes,
800
+ warnings
801
+ });
802
+ const ax = read.tree().byNode.get(idOf(el));
803
+ if (!ax) return done([
804
+ {
805
+ stage: "Tree",
806
+ reached: false,
807
+ detail: leftOutBecause(el, secrets)
808
+ },
809
+ {
810
+ stage: "Projection",
811
+ reached: void 0,
812
+ detail: ""
813
+ },
814
+ {
815
+ stage: "View",
816
+ reached: void 0,
817
+ detail: ""
818
+ }
819
+ ]);
820
+ const what = `${ax.role}${ax.name ? ` "${ax.name}"` : ""}`;
821
+ stages.push({
822
+ stage: "Tree",
823
+ reached: true,
824
+ detail: ax.ignored ? `${what}, ignored: ${ax.ignored.join(", ") || "no reason given"}` : what
825
+ });
826
+ const own = read.nodes.get(ref);
827
+ if (!own) {
828
+ const h = holder(el, read);
829
+ const all = read.text.split("\n");
830
+ const inside = linesInside(el, all);
831
+ const where = h ? `what it says, if anything, is in ${h.ref}: ${line(h, base)}` : inside.length ? "what’s inside it is read on its own" : "nothing holds it";
832
+ const view = inside.length ? `what’s inside it: lines ${inside[0] + 1}–${inside[inside.length - 1] + 1} of ${all.length}` : "";
833
+ return done([{
834
+ stage: "Projection",
835
+ reached: ax.ignored ? false : void 0,
836
+ detail: `no node of its own (${ax.ignored ? "the tree ignores it" : "a wrapper with nothing to say of itself is folded away"}); ${where}`
837
+ }, {
838
+ stage: "View",
839
+ reached: void 0,
840
+ detail: view
841
+ }]);
842
+ }
843
+ stages.push({
844
+ stage: "Projection",
845
+ reached: true,
846
+ detail: fields(own)
847
+ });
848
+ const lines = read.text.split("\n");
849
+ const at = lines.findIndex((l) => l.includes(`[${ref}]`));
850
+ if (at < 0) {
851
+ for (let e = up(el); e; e = up(e)) {
852
+ const row = lines.findIndex((l) => l.trimStart().startsWith(`- row [e${idOf(e)}]`));
853
+ if (row >= 0) return done([{
854
+ stage: "View",
855
+ reached: true,
856
+ detail: `a value on its row’s line, e${idOf(e)} (line ${row + 1} of ${lines.length})`
857
+ }], own);
858
+ }
859
+ return done([{
860
+ stage: "View",
861
+ reached: false,
862
+ detail: "no line of its own: it says nothing a line would, or its parent’s line already says it"
863
+ }], own);
864
+ }
865
+ const filtered = shown !== read.text && !shown.includes(`[${ref}]`);
866
+ return done([{
867
+ stage: "View",
868
+ reached: true,
869
+ detail: `line ${at + 1} of ${lines.length}${filtered ? " (not in the view as filtered now)" : ""}`
870
+ }], own);
871
+ }
872
+ // src/panel.ts
873
+ const KEY = "agent-om-devtools:v1";
874
+ const DEFAULTS = {
875
+ open: false,
876
+ tab: "view",
877
+ dock: "bottom",
878
+ height: 320,
879
+ width: 520,
880
+ side: 300,
881
+ sideHeight: 260,
882
+ steps: 240,
883
+ interactive: false,
884
+ match: "",
885
+ hideIgnored: false,
886
+ refs: false,
887
+ theme: "system"
888
+ };
889
+ /** The grip between the list and the side, px (as the stylesheet draws it). */
890
+ const GRIP = 5;
891
+ /** How long after the last change on the page it's read again, ms. */
892
+ const SETTLE = 200;
893
+ /** A read slower than this, ms, and the panel stops reading as the page changes: it reads when asked. */
894
+ const SLOW = 250;
895
+ /** While closed, the page is read again (for the button's count of warnings) only if a read takes less than this, ms. */
896
+ const CHEAP = 50;
897
+ /** The events the panel handles that stop at it: the app's listeners never see a click or a key meant for the panel. */
898
+ const STOPPED = [
899
+ "pointerdown",
900
+ "pointerup",
901
+ "mousedown",
902
+ "mouseup",
903
+ "click",
904
+ "dblclick",
905
+ "contextmenu",
906
+ "keydown",
907
+ "keyup",
908
+ "keypress",
909
+ "focusin",
910
+ "focusout",
911
+ "input",
912
+ "change",
913
+ "beforeinput"
914
+ ];
915
+ const TABS = [
916
+ ["view", "Sent to the model"],
917
+ ["changes", "Changes"],
918
+ ["projection", "Projection"],
919
+ ["tree", "Tree"]
920
+ ];
921
+ function load(win) {
922
+ try {
923
+ return {
924
+ ...DEFAULTS,
925
+ ...JSON.parse(win.localStorage.getItem(KEY) ?? "{}")
926
+ };
927
+ } catch {
928
+ return { ...DEFAULTS };
929
+ }
930
+ }
931
+ var Panel = class {
932
+ host;
933
+ doc;
934
+ win;
935
+ shadow;
936
+ root;
937
+ settings;
938
+ reader;
939
+ overlay;
940
+ picker;
941
+ trigger;
942
+ badge;
943
+ dock;
944
+ grip;
945
+ splitGrip;
946
+ body;
947
+ /** the Changes tab, kept on the page between reads so its columns keep their scroll: steps, a grip, what changed */
948
+ changes;
949
+ tabs = /* @__PURE__ */ new Map();
950
+ sizes;
951
+ /** said while the focus is in the panel, kept in place so drawing the bar again doesn't move the focus */
952
+ focusNote;
953
+ main;
954
+ side;
955
+ tools;
956
+ lists;
957
+ json = new JsonTree();
958
+ collapsed = /* @__PURE__ */ new Set();
959
+ read;
960
+ /** reading as the page changes (off by the developer, or when reads are slow) */
961
+ live = true;
962
+ slow;
963
+ selected = null;
964
+ scope;
965
+ step;
966
+ /**
967
+ * Room at the end of the page as tall as the panel docked at the bottom, so the page's last content can be scrolled
968
+ * out from under it (as a browser's own devtools take their room from the window). Kept out of every reader of the
969
+ * page, as the panel is.
970
+ */
971
+ spacer;
972
+ /** what was picked, when the pick took the element around it that the model has a line for */
973
+ pickedInside;
974
+ /** why the selected element couldn't be scrolled out from under the panel, when it couldn't */
975
+ hidden;
976
+ /** the tab drawn next scrolls its list to the selected element's row (after a selection, or on changing tab) */
977
+ revealRow = false;
978
+ jsonPath;
979
+ timer = 0;
980
+ observer;
981
+ cleanups = [];
982
+ constructor(doc, options = {}, mode = "closed") {
983
+ this.doc = doc;
984
+ this.win = doc.defaultView;
985
+ this.settings = load(this.win);
986
+ if (options.open !== void 0) this.settings.open = options.open;
987
+ this.reader = new Reader(doc);
988
+ this.spacer = h(doc, "div", {
989
+ "data-agentom-ignore": true,
990
+ "aria-hidden": "true"
991
+ });
992
+ this.host = doc.createElement("agent-om-devtools");
993
+ this.host.setAttribute("data-agentom-ignore", "");
994
+ this.host.setAttribute("popover", "manual");
995
+ for (const [k, v] of Object.entries({
996
+ position: "fixed",
997
+ inset: "auto",
998
+ top: "0",
999
+ left: "0",
1000
+ width: "0",
1001
+ height: "0",
1002
+ margin: "0",
1003
+ padding: "0",
1004
+ border: "0",
1005
+ overflow: "visible",
1006
+ background: "transparent",
1007
+ display: "block",
1008
+ "z-index": "2147483647"
1009
+ })) this.host.style.setProperty(k, v, "important");
1010
+ this.shadow = this.host.attachShadow({ mode });
1011
+ const sheet = new this.win.CSSStyleSheet();
1012
+ sheet.replaceSync(CSS);
1013
+ this.shadow.adoptedStyleSheets = [sheet];
1014
+ const layer = h(doc, "div", {
1015
+ class: "layer",
1016
+ "aria-hidden": "true"
1017
+ });
1018
+ this.overlay = new Overlay(doc, layer);
1019
+ this.badge = h(doc, "span", {
1020
+ class: "badge",
1021
+ hidden: true
1022
+ });
1023
+ this.trigger = h(doc, "button", {
1024
+ class: "trigger",
1025
+ type: "button",
1026
+ "aria-label": "Open Agent OM devtools (Alt+Shift+A)",
1027
+ title: "Agent OM devtools (Alt+Shift+A)"
1028
+ }, "AO", this.badge);
1029
+ this.trigger.addEventListener("click", () => this.open());
1030
+ const [v, hz] = (options.corner ?? "bottom-right").split("-");
1031
+ this.trigger.style.setProperty(v, `calc(16px + env(safe-area-inset-${v}, 0px))`);
1032
+ this.trigger.style.setProperty(hz, `calc(16px + env(safe-area-inset-${hz}, 0px))`);
1033
+ const tool = (label, title, on, pressed) => h(doc, "button", {
1034
+ class: "tool",
1035
+ type: "button",
1036
+ title,
1037
+ "aria-label": title,
1038
+ ...pressed === void 0 ? {} : { "aria-pressed": String(pressed) },
1039
+ on: { click: on }
1040
+ }, label);
1041
+ this.tools = {
1042
+ pick: tool("⌖ Pick", "Pick an element on the page: the nearest the model has a line for, or with Alt held exactly what is under the pointer (Esc stops)", () => this.togglePick(), false),
1043
+ refs: tool("[e] Refs", "Label each ref on the page", () => this.toggleRefs(), this.settings.refs),
1044
+ live: tool("Live", "Read the page as it changes", () => this.toggleLive(), true),
1045
+ reread: tool("↻", "Read the page now", () => this.refresh()),
1046
+ copy: tool("Copy", "Copy what this tab shows, all of it", () => this.copy()),
1047
+ theme: tool("◐", "Theme: system, light or dark", () => this.cycleTheme()),
1048
+ dock: tool("▭", "Dock at the bottom or on the right", () => this.toggleDock()),
1049
+ close: tool("✕", "Close (Esc)", () => this.close())
1050
+ };
1051
+ this.tools.live.prepend(h(doc, "span", { class: "dot" }));
1052
+ const bar = h(doc, "div", {
1053
+ class: "bar",
1054
+ role: "tablist",
1055
+ "aria-label": "Agent OM devtools"
1056
+ }, h(doc, "span", { class: "brand" }, "Agent OM"));
1057
+ for (const [tab, label] of TABS) {
1058
+ const b = h(doc, "button", {
1059
+ class: "tab",
1060
+ type: "button",
1061
+ role: "tab",
1062
+ "aria-selected": String(tab === this.settings.tab),
1063
+ on: { click: () => this.show(tab) }
1064
+ }, label);
1065
+ this.tabs.set(tab, b);
1066
+ bar.append(b);
1067
+ }
1068
+ bar.addEventListener("keydown", (e) => this.tabKeys(e));
1069
+ bar.append(h(doc, "span", { class: "grow" }), ...Object.values(this.tools));
1070
+ this.sizes = h(doc, "div", {
1071
+ class: "sizes",
1072
+ "aria-live": "off"
1073
+ });
1074
+ this.focusNote = h(doc, "span", {
1075
+ class: "note",
1076
+ hidden: true
1077
+ }, "The focus is in the panel: the page reads as having nothing focused.");
1078
+ this.main = h(doc, "div", { class: "main" });
1079
+ this.side = h(doc, "aside", {
1080
+ class: "side",
1081
+ "aria-label": "The selected element"
1082
+ });
1083
+ this.grip = h(doc, "div", {
1084
+ class: "grip",
1085
+ role: "separator",
1086
+ tabindex: 0,
1087
+ "aria-label": "Resize the panel"
1088
+ });
1089
+ this.splitGrip = h(doc, "div", {
1090
+ class: "split-grip",
1091
+ role: "separator",
1092
+ tabindex: 0,
1093
+ "aria-label": "Resize the side with the selected element"
1094
+ });
1095
+ this.body = h(doc, "div", { class: "body" }, this.main, this.splitGrip, this.side);
1096
+ const steps = h(doc, "div", {
1097
+ class: "steps",
1098
+ role: "list",
1099
+ "aria-label": "What was done on the page"
1100
+ });
1101
+ const stepsGrip = h(doc, "div", {
1102
+ class: "split-grip",
1103
+ role: "separator",
1104
+ tabindex: 0,
1105
+ "aria-orientation": "vertical",
1106
+ "aria-label": "Resize the list of steps"
1107
+ });
1108
+ const text = h(doc, "div", { class: "main" });
1109
+ this.changes = {
1110
+ split: h(doc, "div", { class: "split" }, steps, stepsGrip, text),
1111
+ steps,
1112
+ grip: stepsGrip,
1113
+ text
1114
+ };
1115
+ this.dock = h(doc, "section", {
1116
+ class: `dock ${this.settings.dock}`,
1117
+ "aria-label": "Agent OM devtools",
1118
+ hidden: true
1119
+ }, this.grip, bar, this.sizes, this.body);
1120
+ this.lists = {
1121
+ view: new VirtualList(doc, "The text sent to the model", "v"),
1122
+ changes: new VirtualList(doc, "What an agent would be told changed", "c"),
1123
+ projection: new VirtualList(doc, "The projection", "p"),
1124
+ tree: new VirtualList(doc, "The accessibility tree", "t")
1125
+ };
1126
+ this.root = h(doc, "div", { class: "root" }, layer, this.trigger, this.dock);
1127
+ if (this.settings.theme !== "system") this.root.dataset.theme = this.settings.theme;
1128
+ this.shadow.append(this.root);
1129
+ this.resizable();
1130
+ this.root.addEventListener("mousedown", (e) => {
1131
+ const t = e.target;
1132
+ if (t.closest("button, .tw, .grip, .split-grip") && !t.closest("input")) e.preventDefault();
1133
+ });
1134
+ this.root.addEventListener("focusin", () => this.focusNote.hidden = false);
1135
+ this.root.addEventListener("focusout", () => this.win.setTimeout(() => this.focusNote.hidden = !this.shadow.activeElement));
1136
+ for (const type of STOPPED) this.host.addEventListener(type, (e) => e.stopPropagation());
1137
+ this.picker = new Picker(this.win, this.host, (el, exact) => {
1138
+ const shown = el && (exact ? el : this.withLine(el));
1139
+ this.overlay.hover(shown, shown ? this.lineFor(shown) : "");
1140
+ }, (el, exact) => {
1141
+ this.tools.pick.setAttribute("aria-pressed", "false");
1142
+ if (!el) return;
1143
+ const shown = exact ? el : this.withLine(el);
1144
+ this.select(shown);
1145
+ this.pickedInside = shown !== el ? el : void 0;
1146
+ if (this.pickedInside) this.renderSide();
1147
+ });
1148
+ doc.documentElement.append(this.host);
1149
+ this.raise();
1150
+ this.observer = new MutationObserver((records) => this.mutated(records));
1151
+ this.observer.observe(doc, {
1152
+ subtree: true,
1153
+ childList: true,
1154
+ attributes: true,
1155
+ characterData: true
1156
+ });
1157
+ this.listen();
1158
+ if (this.settings.open) this.open();
1159
+ else this.idle(() => this.refresh());
1160
+ }
1161
+ open() {
1162
+ this.settings.open = true;
1163
+ this.save();
1164
+ this.trigger.hidden = true;
1165
+ this.dock.hidden = false;
1166
+ this.applySize();
1167
+ this.makeRoom();
1168
+ this.raise();
1169
+ this.refresh();
1170
+ }
1171
+ close() {
1172
+ this.settings.open = false;
1173
+ this.save();
1174
+ this.picker.stop();
1175
+ this.dock.hidden = true;
1176
+ this.trigger.hidden = false;
1177
+ this.overlay.hover(null);
1178
+ this.overlay.select(null);
1179
+ this.overlay.showRefs(false);
1180
+ this.makeRoom();
1181
+ }
1182
+ unmount() {
1183
+ this.picker.stop();
1184
+ this.observer.disconnect();
1185
+ this.win.clearTimeout(this.timer);
1186
+ for (const c of this.cleanups) c();
1187
+ this.spacer.remove();
1188
+ this.host.remove();
1189
+ }
1190
+ /** Read the page now and show it. */
1191
+ refresh() {
1192
+ this.win.clearTimeout(this.timer);
1193
+ const read = this.reader.read();
1194
+ this.read = read;
1195
+ const ms = read.ms.read + read.ms.merge;
1196
+ if (ms > SLOW && this.live && this.settings.open) {
1197
+ this.live = false;
1198
+ this.slow = `Reads take ${Math.round(ms)} ms on this page, so the panel reads when you press ↻.`;
1199
+ }
1200
+ if (this.selected && !this.selected.isConnected) this.selected = null;
1201
+ this.render();
1202
+ }
1203
+ /** Read again once the page has been still for a moment (open and live), or later and only if cheap (closed). */
1204
+ schedule() {
1205
+ this.win.clearTimeout(this.timer);
1206
+ if (this.settings.open) {
1207
+ if (this.live) this.timer = this.win.setTimeout(() => this.refresh(), SETTLE);
1208
+ } else if (!this.read || this.read.ms.read + this.read.ms.merge < CHEAP) this.timer = this.win.setTimeout(() => this.idle(() => this.refresh()), 1e3);
1209
+ }
1210
+ idle(fn) {
1211
+ const w = this.win;
1212
+ if (w.requestIdleCallback) w.requestIdleCallback(fn, { timeout: 1e3 });
1213
+ else this.win.setTimeout(fn, 50);
1214
+ }
1215
+ mutated(records) {
1216
+ const mine = (n) => n === this.host || n === this.spacer;
1217
+ const page = records.filter((r) => !mine(r.target) && !this.host.contains(r.target) && !(r.type === "childList" && [...r.addedNodes, ...r.removedNodes].every(mine)));
1218
+ if (!page.length) return;
1219
+ if (page.some((r) => r.type === "attributes" && (r.attributeName === "open" || r.attributeName === "popover"))) this.raise();
1220
+ this.schedule();
1221
+ }
1222
+ /** The events on the page that change what's read without a change to the DOM, and what the developer did. */
1223
+ listen() {
1224
+ const on = (target, type, fn, capture = true) => {
1225
+ target.addEventListener(type, fn, capture);
1226
+ this.cleanups.push(() => target.removeEventListener(type, fn, capture));
1227
+ };
1228
+ const mine = (e) => e.composedPath().includes(this.host);
1229
+ const target = (e) => elementOf(e.composedPath()[0]);
1230
+ for (const type of [
1231
+ "input",
1232
+ "change",
1233
+ "focusin",
1234
+ "focusout",
1235
+ "toggle"
1236
+ ]) on(this.doc, type, (e) => {
1237
+ if (mine(e)) return;
1238
+ if (type === "toggle" && e.newState === "open") this.raise();
1239
+ if (type === "input" || type === "change") {
1240
+ const t = target(e);
1241
+ if (t) this.reader.did(type === "input" ? "type into" : "change", t);
1242
+ }
1243
+ this.schedule();
1244
+ });
1245
+ on(this.doc, "click", (e) => {
1246
+ const t = target(e);
1247
+ if (!mine(e) && t) this.reader.did("click", t);
1248
+ });
1249
+ on(this.doc, "keydown", (e) => {
1250
+ const k = e;
1251
+ const t = target(e);
1252
+ if (!mine(e) && t && (k.key.length > 1 || k.ctrlKey || k.metaKey) && ![
1253
+ "Shift",
1254
+ "Control",
1255
+ "Alt",
1256
+ "Meta"
1257
+ ].includes(k.key)) this.reader.did("press", t, [
1258
+ k.ctrlKey && "Control",
1259
+ k.metaKey && "Meta",
1260
+ k.altKey && "Alt",
1261
+ k.key
1262
+ ].filter(Boolean).join("+"));
1263
+ });
1264
+ on(this.win, "keydown", (e) => {
1265
+ const k = e;
1266
+ if (k.altKey && k.shiftKey && k.code === "KeyA") {
1267
+ k.preventDefault();
1268
+ k.stopPropagation();
1269
+ if (this.settings.open) this.close();
1270
+ else this.open();
1271
+ } else if (k.key === "Escape" && this.settings.open && mine(e) && !this.picker.active) this.close();
1272
+ });
1273
+ }
1274
+ /** Put the panel back on top of the top layer (an app's popover or dialog opened after it would be above it). */
1275
+ raise() {
1276
+ const host = this.host;
1277
+ if (!host.showPopover || !host.isConnected) return;
1278
+ try {
1279
+ if (host.matches(":popover-open")) host.hidePopover();
1280
+ host.showPopover();
1281
+ } catch {}
1282
+ }
1283
+ render() {
1284
+ const warnings = this.read?.projection.warnings?.length ?? 0;
1285
+ this.badge.hidden = !warnings;
1286
+ this.badge.textContent = String(warnings);
1287
+ this.trigger.setAttribute("aria-label", `Open Agent OM devtools (Alt+Shift+A)${warnings ? `: ${warnings} warnings about the page's metadata` : ""}`);
1288
+ if (!this.settings.open || !this.read) return;
1289
+ this.renderTab();
1290
+ this.renderSizes();
1291
+ this.renderSide();
1292
+ this.overlay.select(this.selected);
1293
+ this.overlay.showRefs(this.settings.refs, this.read);
1294
+ }
1295
+ options() {
1296
+ const match = this.settings.match.split(",").map((w) => w.trim()).filter(Boolean);
1297
+ return {
1298
+ ...this.settings.interactive ? { interactive: true } : {},
1299
+ ...match.length ? { match } : {},
1300
+ ...this.scope ? { scope: this.scope } : {}
1301
+ };
1302
+ }
1303
+ /** The view as the panel shows it now: the whole view, or with the options set. */
1304
+ shown() {
1305
+ const o = this.options();
1306
+ return Object.keys(o).length ? view(this.read.projection, o) : this.read.text;
1307
+ }
1308
+ renderSizes() {
1309
+ const read = this.read;
1310
+ if (!read || !this.settings.open) return;
1311
+ const doc = this.doc;
1312
+ const text = this.settings.tab === "view" ? this.shown() : read.text;
1313
+ const items = [
1314
+ h(doc, "span", null, "page ", h(doc, "b", null, count(read.elements)), " elements"),
1315
+ h(doc, "span", null, "tree ", h(doc, "b", null, count(read.raw.ax.length)), " nodes"),
1316
+ h(doc, "span", null, "projection ", h(doc, "b", null, bytes(JSON.stringify(read.projection).length))),
1317
+ h(doc, "span", null, "view ", h(doc, "b", null, count(text.split("\n").length)), " lines · ", h(doc, "b", null, count(text.length)), " chars"),
1318
+ h(doc, "span", { title: "reading the page, then merging it into the projection" }, "read ", h(doc, "b", null, `${Math.round(read.ms.read)} ms`), " + ", h(doc, "b", null, `${Math.round(read.ms.merge)} ms`))
1319
+ ];
1320
+ items.push(this.focusNote);
1321
+ if (this.slow) items.push(h(doc, "span", { class: "note" }, this.slow));
1322
+ const opts = h(doc, "span", { class: "opts" });
1323
+ if (this.settings.tab === "view") {
1324
+ const only = h(doc, "input", {
1325
+ type: "checkbox",
1326
+ ...this.settings.interactive ? { checked: true } : {}
1327
+ });
1328
+ only.addEventListener("change", () => this.set({ interactive: only.checked }));
1329
+ const match = h(doc, "input", {
1330
+ type: "text",
1331
+ value: this.settings.match,
1332
+ placeholder: "match: words, comma-separated",
1333
+ "aria-label": "Only lines with these words"
1334
+ });
1335
+ match.addEventListener("input", () => this.set({ match: match.value }, false));
1336
+ opts.append(h(doc, "label", null, only, "Only what can be acted on"), match);
1337
+ if (this.scope) opts.append(h(doc, "span", { class: "chip" }, `scope ${this.scope}`, h(doc, "button", {
1338
+ type: "button",
1339
+ "aria-label": "Show the whole page",
1340
+ on: { click: () => this.setScope(void 0) }
1341
+ }, "✕")));
1342
+ } else if (this.settings.tab === "tree") {
1343
+ const hide = h(doc, "input", {
1344
+ type: "checkbox",
1345
+ ...this.settings.hideIgnored ? { checked: true } : {}
1346
+ });
1347
+ hide.addEventListener("change", () => this.set({ hideIgnored: hide.checked }));
1348
+ opts.append(h(doc, "label", null, hide, "Hide ignored nodes"));
1349
+ }
1350
+ const focused = this.shadow.activeElement;
1351
+ if (focused instanceof HTMLInputElement && focused.type === "text" && this.sizes.contains(focused)) {
1352
+ this.sizes.replaceChildren(...items, focused.closest(".opts"));
1353
+ return;
1354
+ }
1355
+ this.sizes.replaceChildren(...items, opts);
1356
+ }
1357
+ renderTab() {
1358
+ for (const [tab, b] of this.tabs) b.setAttribute("aria-selected", String(tab === this.settings.tab));
1359
+ const read = this.read;
1360
+ const tab = this.settings.tab;
1361
+ if (tab === "view") this.renderView();
1362
+ else if (tab === "changes") this.renderChanges();
1363
+ else if (tab === "projection") this.renderProjection(read);
1364
+ else this.renderTree(read);
1365
+ }
1366
+ renderView() {
1367
+ const lines = this.shown().split("\n");
1368
+ const list = this.lists.view;
1369
+ list.set(lines.length, {
1370
+ row: (i) => viewLine(this.doc, lines[i]),
1371
+ hover: (i) => this.hoverRef(i < 0 ? void 0 : refIn(lines[i]), i < 0 ? "" : lines[i].trim()),
1372
+ pick: (i) => {
1373
+ const ref = refIn(lines[i]);
1374
+ const el = ref ? nodeById(Number(ref.slice(1))) : void 0;
1375
+ if (el) this.select(elementOf(el), true);
1376
+ }
1377
+ }, this.lineOfSelected(lines));
1378
+ this.main.replaceChildren(list.el);
1379
+ list.placed();
1380
+ if (this.revealRow) list.revealSelected();
1381
+ }
1382
+ renderChanges() {
1383
+ const doc = this.doc;
1384
+ const steps = this.reader.steps;
1385
+ if (!this.step || !steps.includes(this.step)) this.step = steps[0];
1386
+ const time = (t) => new Date(t).toLocaleTimeString([], {
1387
+ hour: "2-digit",
1388
+ minute: "2-digit",
1389
+ second: "2-digit"
1390
+ });
1391
+ this.changes.steps.replaceChildren(...steps.map((s) => h(doc, "button", {
1392
+ class: "step",
1393
+ type: "button",
1394
+ "aria-current": String(s === this.step),
1395
+ on: { click: () => {
1396
+ this.step = s;
1397
+ this.renderChanges();
1398
+ } }
1399
+ }, h(doc, "small", null, `${time(s.at)} · ${s.whole ? "whole page" : `${s.text.split("\n").length - 1} lines`}`), s.what)));
1400
+ const lines = this.step ? this.step.text.split("\n") : ["Nothing read yet."];
1401
+ const list = this.lists.changes;
1402
+ list.set(lines.length, {
1403
+ row: (i) => {
1404
+ const l = lines[i];
1405
+ const row = viewLine(doc, l);
1406
+ if (/^\+ /.test(l)) row.classList.add("add");
1407
+ else if (/^- gone/.test(l)) row.classList.add("del");
1408
+ else if (/^\s+(was|text was):/.test(l)) row.classList.add("was");
1409
+ return row;
1410
+ },
1411
+ hover: (i) => this.hoverRef(i < 0 ? void 0 : refIn(lines[i]), i < 0 ? "" : lines[i].trim()),
1412
+ pick: (i) => {
1413
+ const ref = refIn(lines[i]);
1414
+ const el = ref ? nodeById(Number(ref.slice(1))) : void 0;
1415
+ if (el) this.select(elementOf(el), true);
1416
+ }
1417
+ }, this.lineOfSelected(lines));
1418
+ const note = this.step?.whole && this.step !== steps.at(-1) ? h(doc, "div", { class: "tabbar" }, "An agent gets the whole page after this: too much of it changed for a list of changes to say less.") : null;
1419
+ if (this.main.firstChild !== this.changes.split || this.main.childNodes.length !== 1) this.main.replaceChildren(this.changes.split);
1420
+ this.changes.text.replaceChildren(...note ? [note] : [], list.el);
1421
+ this.applySteps();
1422
+ list.placed();
1423
+ if (this.revealRow) list.revealSelected();
1424
+ }
1425
+ renderProjection(read) {
1426
+ const doc = this.doc;
1427
+ if (this.revealRow) this.openToSelected(read);
1428
+ const rows = this.json.rows(read.projection);
1429
+ const at = rows.findIndex((r) => r.path === this.jsonPath);
1430
+ const chosen = rows[at];
1431
+ const copy = (text) => void this.win.navigator.clipboard?.writeText(text).catch(() => void 0);
1432
+ const bar = h(doc, "div", { class: "tabbar" }, h(doc, "span", { class: "path" }, chosen ? chosen.path : "Select a row to copy its value or its path."), h(doc, "span", { class: "grow" }), chosen ? h(doc, "button", {
1433
+ class: "tool",
1434
+ type: "button",
1435
+ on: { click: () => copy(JSON.stringify(chosen.value, null, 2)) }
1436
+ }, "Copy value") : null, chosen ? h(doc, "button", {
1437
+ class: "tool",
1438
+ type: "button",
1439
+ on: { click: () => copy(chosen.path) }
1440
+ }, "Copy path") : null, h(doc, "button", {
1441
+ class: "tool",
1442
+ type: "button",
1443
+ on: { click: () => copy(JSON.stringify(read.projection, null, 2)) }
1444
+ }, "Copy all"));
1445
+ const toggle = (path) => {
1446
+ this.json.toggle(path);
1447
+ this.renderProjection(read);
1448
+ };
1449
+ this.lists.projection.set(rows.length, {
1450
+ row: (i) => jsonRow(doc, rows[i], toggle),
1451
+ hover: (i) => this.hoverRef(i < 0 ? void 0 : refOfValue(rows[i].value), ""),
1452
+ pick: (i) => {
1453
+ this.jsonPath = rows[i].path;
1454
+ const ref = refOfValue(rows[i].value);
1455
+ const el = ref ? nodeById(Number(ref.slice(1))) : void 0;
1456
+ if (el) this.select(elementOf(el), true);
1457
+ else this.renderProjection(read);
1458
+ }
1459
+ }, at);
1460
+ this.main.replaceChildren(h(doc, "div", { class: "main" }, bar, this.lists.projection.el));
1461
+ this.lists.projection.placed();
1462
+ if (this.revealRow) this.lists.projection.revealSelected();
1463
+ }
1464
+ renderTree(read) {
1465
+ const doc = this.doc;
1466
+ const rows = [];
1467
+ const hide = this.settings.hideIgnored;
1468
+ const add = (n, depth, key) => {
1469
+ const skip = hide && !!n.ignored;
1470
+ if (!skip) rows.push({
1471
+ n,
1472
+ depth,
1473
+ key,
1474
+ kids: n.children.length > 0
1475
+ });
1476
+ if (!skip && this.collapsed.has(key)) return;
1477
+ n.children.forEach((c, i) => add(c, skip ? depth : depth + 1, `${key}.${c.node ?? `#${i}`}`));
1478
+ };
1479
+ const { root, byNode } = read.tree();
1480
+ let selected = -1;
1481
+ for (const e of around(this.selected)) if (byNode.has(idOf(e))) {
1482
+ selected = idOf(e);
1483
+ break;
1484
+ }
1485
+ if (this.revealRow && selected >= 0) {
1486
+ const open = (n, key) => {
1487
+ if (n.node === selected) return true;
1488
+ const found = n.children.some((c, i) => open(c, `${key}.${c.node ?? `#${i}`}`));
1489
+ if (found) this.collapsed.delete(key);
1490
+ return found;
1491
+ };
1492
+ open(root, String(root.node ?? "root"));
1493
+ }
1494
+ add(root, 0, String(root.node ?? "root"));
1495
+ this.lists.tree.set(rows.length, {
1496
+ row: (i) => {
1497
+ const { n, depth, key, kids } = rows[i];
1498
+ const el = doc.createElement("div");
1499
+ if (n.ignored) el.classList.add("dim");
1500
+ el.append(" ".repeat(depth));
1501
+ const tw = h(doc, "span", { class: "tw" }, kids ? this.collapsed.has(key) ? "▸" : "▾" : "");
1502
+ if (kids) tw.addEventListener("click", () => {
1503
+ if (!this.collapsed.delete(key)) this.collapsed.add(key);
1504
+ this.renderTree(read);
1505
+ });
1506
+ el.append(tw, n.ignored ? n.role : h(doc, "span", { class: "r" }, n.role));
1507
+ if (n.name) el.append(" ", h(doc, "span", { class: "n" }, JSON.stringify(n.name.length > 120 ? `${n.name.slice(0, 120)}…` : n.name)));
1508
+ if (n.value !== void 0) el.append(" ", h(doc, "span", { class: "a" }, "value="), JSON.stringify(n.value));
1509
+ for (const [k, v] of Object.entries(n.properties ?? {})) if (v !== false) el.append(" ", h(doc, "span", { class: "a" }, `${k}=`), String(v));
1510
+ if (n.node !== void 0) el.append(" ", h(doc, "span", { class: "e" }, `e${n.node}`));
1511
+ if (n.ignored) el.append(" ", h(doc, "span", { class: "why" }, `ignored: ${n.ignored.join(", ") || "no reason given"}`));
1512
+ return el;
1513
+ },
1514
+ hover: (i) => {
1515
+ const n = i < 0 ? void 0 : rows[i].n;
1516
+ const node = n?.node !== void 0 ? nodeById(n.node) : void 0;
1517
+ this.overlay.hover(node ?? null, n ? `${n.role}${n.name ? ` "${n.name}"` : ""}` : "");
1518
+ },
1519
+ pick: (i) => {
1520
+ const node = rows[i].n.node !== void 0 ? nodeById(rows[i].n.node) : void 0;
1521
+ const el = elementOf(node);
1522
+ if (el) this.select(el, true);
1523
+ }
1524
+ }, rows.findIndex((r) => r.n.node === selected));
1525
+ this.main.replaceChildren(this.lists.tree.el);
1526
+ this.lists.tree.placed();
1527
+ if (this.revealRow) this.lists.tree.revealSelected();
1528
+ }
1529
+ renderSide() {
1530
+ const doc = this.doc;
1531
+ const read = this.read;
1532
+ if (!read) return;
1533
+ const el = this.selected;
1534
+ if (!el) {
1535
+ this.side.replaceChildren(h(doc, "h3", null, "Selected"), h(doc, "p", { class: "why" }, "Select an element: click a line or a row, or pick one on the page (⌖ Pick). This side then shows where it went on its way to the model."));
1536
+ return;
1537
+ }
1538
+ const t = trail(el, read, this.settings.tab === "view" ? this.shown() : read.text);
1539
+ const mark = (r) => h(doc, "span", { class: r === true ? "yes" : r === false ? "no" : "stage" }, r === true ? "✓" : r === false ? "✕" : "–");
1540
+ const list = h(doc, "ul", { class: "trail" });
1541
+ for (const s of t.stages) list.append(h(doc, "li", null, mark(s.reached), h(doc, "span", { class: "stage" }, s.stage), h(doc, "span", { class: "d" }, s.detail)));
1542
+ const inside = this.pickedInside?.isConnected ? h(doc, "p", { class: "why" }, `Picked inside it: ${tagOf(this.pickedInside, read.secrets)}, which has no line of its own. Hold Alt while picking to select exactly what's under the pointer.`) : null;
1543
+ const parts = [
1544
+ h(doc, "h3", null, `Selected${t.ref ? ` · ${t.ref}` : ""}`),
1545
+ t.line ? h(doc, "div", { class: "line" }, t.line) : null,
1546
+ inside,
1547
+ h(doc, "h3", null, "Where it went"),
1548
+ list
1549
+ ];
1550
+ if (t.attributes.length) {
1551
+ const dl = h(doc, "dl");
1552
+ for (const [k, v] of t.attributes) dl.append(h(doc, "dt", null, k.replace("data-agentom-", "")), h(doc, "dd", null, v));
1553
+ parts.push(h(doc, "h3", null, "What the app says of it"), dl);
1554
+ }
1555
+ if (this.hidden) parts.push(h(doc, "div", { class: "note" }, this.hidden));
1556
+ for (const w of t.warnings) parts.push(h(doc, "div", { class: "note" }, `${w.code}: ${w.message}`));
1557
+ const actions = h(doc, "div", { class: "actions" });
1558
+ if (t.ref) actions.append(h(doc, "button", {
1559
+ class: "tool",
1560
+ type: "button",
1561
+ on: { click: () => this.setScope(t.ref) }
1562
+ }, "Only this in the view"));
1563
+ actions.append(h(doc, "button", {
1564
+ class: "tool",
1565
+ type: "button",
1566
+ on: { click: () => el.scrollIntoView({
1567
+ block: "center",
1568
+ behavior: "smooth"
1569
+ }) }
1570
+ }, "Scroll to it"));
1571
+ actions.append(h(doc, "button", {
1572
+ class: "tool",
1573
+ type: "button",
1574
+ on: { click: () => this.select(null) }
1575
+ }, "Clear"));
1576
+ parts.push(actions);
1577
+ this.side.replaceChildren(...parts.filter((p) => !!p));
1578
+ }
1579
+ /** Select an element; from the panel's lists, scroll it into sight when it's out of it (or behind the panel). */
1580
+ select(el, reveal = false) {
1581
+ this.selected = el;
1582
+ this.hidden = void 0;
1583
+ this.pickedInside = void 0;
1584
+ if (el && reveal) this.reveal(el);
1585
+ if (!this.read) return;
1586
+ this.revealRow = true;
1587
+ this.renderTab();
1588
+ this.revealRow = false;
1589
+ this.renderSide();
1590
+ this.overlay.select(el);
1591
+ }
1592
+ /** The row of the view's lines that is the selected element's, or the nearest element's around it that has one. */
1593
+ lineOfSelected(lines) {
1594
+ for (const e of around(this.selected)) {
1595
+ const ref = `[e${idOf(e)}]`;
1596
+ const i = lines.findIndex((l) => l.includes(ref));
1597
+ if (i >= 0) return i;
1598
+ }
1599
+ return this.selected ? linesInside(this.selected, lines)[0] ?? -1 : -1;
1600
+ }
1601
+ /** Open the projection's rows down to the selected element's node (or the nearest one around it), and select that row. */
1602
+ openToSelected(read) {
1603
+ let target;
1604
+ for (const e of around(this.selected)) if (read.nodes.has(`e${idOf(e)}`)) {
1605
+ target = `e${idOf(e)}`;
1606
+ break;
1607
+ }
1608
+ if (!target && this.selected) {
1609
+ const lines = read.text.split("\n");
1610
+ const i = linesInside(this.selected, lines)[0];
1611
+ if (i !== void 0) target = refIn(lines[i]);
1612
+ }
1613
+ if (!target) return;
1614
+ const opens = ["$", "$.root"];
1615
+ const find = (n, path) => {
1616
+ if (n.ref === target) return path;
1617
+ const kids = n.children ?? [];
1618
+ const list = `${path}.children`;
1619
+ for (let i = 0; i < kids.length; i++) {
1620
+ const found = find(kids[i], `${list}[${i}]`);
1621
+ if (!found) continue;
1622
+ const from = Math.floor(i / 100) * 100;
1623
+ opens.push(path, list, ...kids.length > 100 ? [`${list}[${from}…${Math.min(kids.length, from + 100) - 1}]`] : []);
1624
+ return found;
1625
+ }
1626
+ };
1627
+ const path = find(read.projection.root, "$.root");
1628
+ if (!path) return;
1629
+ this.json.open(opens);
1630
+ this.jsonPath = path;
1631
+ }
1632
+ /** Scroll an element into the part of the window the panel leaves free, unless it's all there already. */
1633
+ reveal(el) {
1634
+ this.hidden = void 0;
1635
+ const free = {
1636
+ top: 0,
1637
+ left: 0,
1638
+ bottom: this.win.innerHeight,
1639
+ right: this.win.innerWidth
1640
+ };
1641
+ if (!this.dock.hidden) {
1642
+ const d = this.dock.getBoundingClientRect();
1643
+ if (this.settings.dock === "bottom") free.bottom = d.top;
1644
+ else free.right = d.left;
1645
+ }
1646
+ const seen = (r) => r.top >= free.top - 1 && (r.bottom <= free.bottom + 1 || r.height > free.bottom - free.top && r.top <= free.bottom - 40) && r.left >= free.left - 1 && (r.right <= free.right + 1 || r.width > free.right - free.left && r.left <= free.right - 40);
1647
+ if (!el.getClientRects().length) return;
1648
+ if (seen(el.getBoundingClientRect())) return;
1649
+ const was = {
1650
+ x: this.win.scrollX,
1651
+ y: this.win.scrollY
1652
+ };
1653
+ el.scrollIntoView({
1654
+ block: "center",
1655
+ inline: "nearest",
1656
+ behavior: "instant"
1657
+ });
1658
+ const r = el.getBoundingClientRect();
1659
+ const middle = (from, to, size) => size >= to - from ? from + 8 : from + (to - from - size) / 2;
1660
+ const dy = this.settings.dock === "bottom" ? r.top - middle(free.top, free.bottom, r.height) : 0;
1661
+ const dx = this.settings.dock === "right" && r.right > free.right ? r.left - middle(free.left, free.right, r.width) : 0;
1662
+ if (dy || dx) {
1663
+ const box = scrollerOf(el, dy ? "y" : "x");
1664
+ box.scrollBy({
1665
+ top: dy,
1666
+ left: dx,
1667
+ behavior: "instant"
1668
+ });
1669
+ const page = this.doc.scrollingElement ?? this.doc.documentElement;
1670
+ if (box !== page && !seen(el.getBoundingClientRect())) {
1671
+ const after = el.getBoundingClientRect();
1672
+ page.scrollBy({
1673
+ top: dy ? after.top - middle(free.top, free.bottom, after.height) : 0,
1674
+ left: dx ? after.left - middle(free.left, free.right, after.width) : 0,
1675
+ behavior: "instant"
1676
+ });
1677
+ }
1678
+ }
1679
+ if (!seen(el.getBoundingClientRect())) {
1680
+ this.win.scrollTo({
1681
+ left: was.x,
1682
+ top: was.y,
1683
+ behavior: "instant"
1684
+ });
1685
+ this.hidden = `It's under the panel, and the page can't scroll it out (it's fixed in place, or its box scrolls no further). Make the panel shorter, or dock it on the right.`;
1686
+ }
1687
+ }
1688
+ /** Outline a ref's element, labelled with its line. */
1689
+ hoverRef(ref, label) {
1690
+ const node = ref ? nodeById(Number(ref.slice(1))) : void 0;
1691
+ this.overlay.hover(node ?? null, node ? label : "");
1692
+ }
1693
+ /** What an element is to the model, for the picker's label: its line, or why it has none. */
1694
+ /** The element, or the nearest around it, that has a line of its own in the view: what the model reads of a spot. */
1695
+ withLine(el) {
1696
+ const read = this.read;
1697
+ if (!read) return el;
1698
+ for (const e of around(el)) if (read.text.includes(`[e${idOf(e)}]`)) return e;
1699
+ return el;
1700
+ }
1701
+ lineFor(el) {
1702
+ const read = this.read;
1703
+ if (!read) return `<${el.localName}>`;
1704
+ const t = trail(el, read, read.text);
1705
+ if (t.line) return t.line;
1706
+ const stop = t.stages.find((s) => s.reached === false) ?? t.stages.find((s) => s.stage === "View" && s.detail);
1707
+ return `<${el.localName}>: ${stop ? stop.detail : "no line of its own"}`;
1708
+ }
1709
+ show(tab) {
1710
+ this.revealRow = true;
1711
+ this.set({ tab });
1712
+ this.revealRow = false;
1713
+ }
1714
+ set(change, render = true) {
1715
+ Object.assign(this.settings, change);
1716
+ this.save();
1717
+ if (!render || !this.read) {
1718
+ if (!render && this.read && this.settings.tab === "view") this.renderView();
1719
+ return;
1720
+ }
1721
+ this.renderTab();
1722
+ this.renderSizes();
1723
+ this.renderSide();
1724
+ }
1725
+ setScope(ref) {
1726
+ this.scope = ref;
1727
+ this.set({ tab: "view" });
1728
+ }
1729
+ save() {
1730
+ try {
1731
+ this.win.localStorage.setItem(KEY, JSON.stringify(this.settings));
1732
+ } catch {}
1733
+ }
1734
+ /** What the tab shows, all of it (a list draws only the rows in sight), to the clipboard. */
1735
+ copy() {
1736
+ const read = this.read;
1737
+ if (!read) return;
1738
+ const tab = this.settings.tab;
1739
+ const text = tab === "view" ? this.shown() : tab === "changes" ? this.step?.text ?? "" : tab === "projection" ? JSON.stringify(read.projection, null, 2) : treeText(read.tree().root);
1740
+ const done = (said) => {
1741
+ this.tools.copy.textContent = said;
1742
+ this.win.setTimeout(() => this.tools.copy.textContent = "Copy", 1500);
1743
+ };
1744
+ this.win.navigator.clipboard?.writeText(text).then(() => done("Copied"), () => done("Not allowed"));
1745
+ }
1746
+ togglePick() {
1747
+ if (this.picker.active) {
1748
+ this.picker.stop();
1749
+ this.tools.pick.setAttribute("aria-pressed", "false");
1750
+ } else {
1751
+ this.picker.start();
1752
+ this.tools.pick.setAttribute("aria-pressed", "true");
1753
+ }
1754
+ }
1755
+ toggleRefs() {
1756
+ const on = !this.settings.refs;
1757
+ this.tools.refs.setAttribute("aria-pressed", String(on));
1758
+ this.set({ refs: on }, false);
1759
+ this.overlay.showRefs(on, this.read);
1760
+ }
1761
+ toggleLive() {
1762
+ this.live = !this.live;
1763
+ this.slow = void 0;
1764
+ this.tools.live.setAttribute("aria-pressed", String(this.live));
1765
+ if (this.live) this.refresh();
1766
+ else this.renderSizes();
1767
+ }
1768
+ cycleTheme() {
1769
+ const theme = {
1770
+ system: "light",
1771
+ light: "dark",
1772
+ dark: "system"
1773
+ }[this.settings.theme];
1774
+ if (theme === "system") delete this.root.dataset.theme;
1775
+ else this.root.dataset.theme = theme;
1776
+ this.set({ theme }, false);
1777
+ }
1778
+ toggleDock() {
1779
+ const dock = this.settings.dock === "bottom" ? "right" : "bottom";
1780
+ this.dock.className = `dock ${dock}`;
1781
+ this.set({ dock }, false);
1782
+ this.applySize();
1783
+ }
1784
+ applySize() {
1785
+ const bottom = this.settings.dock === "bottom";
1786
+ const size = this.clamp(bottom ? this.settings.height : this.settings.width);
1787
+ this.dock.style.height = bottom ? `${size}px` : "";
1788
+ this.dock.style.width = bottom ? "" : `${size}px`;
1789
+ this.grip.setAttribute("aria-orientation", bottom ? "horizontal" : "vertical");
1790
+ this.grip.setAttribute("aria-valuenow", String(size));
1791
+ this.applySide();
1792
+ if (this.changes.split.isConnected) this.applySteps();
1793
+ this.makeRoom();
1794
+ }
1795
+ /** The room at the end of the page: as tall as the panel while it's open at the bottom, else none. */
1796
+ makeRoom() {
1797
+ if (!this.settings.open || this.settings.dock !== "bottom") {
1798
+ this.spacer.remove();
1799
+ return;
1800
+ }
1801
+ const height = `${this.clamp(this.settings.height)}px`;
1802
+ for (const [k, v] of Object.entries({
1803
+ display: "block",
1804
+ height,
1805
+ margin: "0",
1806
+ padding: "0",
1807
+ border: "0",
1808
+ "pointer-events": "none"
1809
+ })) this.spacer.style.setProperty(k, v, "important");
1810
+ if (this.spacer.parentNode !== this.doc.documentElement || this.spacer.nextSibling) this.doc.documentElement.append(this.spacer);
1811
+ }
1812
+ clamp(size) {
1813
+ const bottom = this.settings.dock === "bottom";
1814
+ const max = Math.round((bottom ? this.win.innerHeight : this.win.innerWidth) * .9);
1815
+ return Math.max(bottom ? 120 : 280, Math.min(max, Math.round(size)));
1816
+ }
1817
+ /**
1818
+ * The grips: the panel's edge sizes the panel, the one between the list and the side sizes the side. Each is
1819
+ * dragged, or moved with the arrow keys, ten pixels a press; both grow toward the page's middle or the list.
1820
+ */
1821
+ resizable() {
1822
+ const bottom = () => this.settings.dock === "bottom";
1823
+ this.grippable(this.grip, (m) => bottom() ? this.win.innerHeight - m.clientY : this.win.innerWidth - m.clientX, () => bottom() ? this.settings.height : this.settings.width, (size) => {
1824
+ if (bottom()) this.settings.height = this.clamp(size);
1825
+ else this.settings.width = this.clamp(size);
1826
+ this.applySize();
1827
+ }, () => bottom() ? ["ArrowUp", "ArrowDown"] : ["ArrowLeft", "ArrowRight"]);
1828
+ this.grippable(this.splitGrip, (m) => {
1829
+ const b = this.body.getBoundingClientRect();
1830
+ return bottom() ? b.right - m.clientX : b.bottom - m.clientY;
1831
+ }, () => bottom() ? this.settings.side : this.settings.sideHeight, (size) => {
1832
+ if (bottom()) this.settings.side = size;
1833
+ else this.settings.sideHeight = size;
1834
+ this.applySide();
1835
+ }, () => bottom() ? ["ArrowLeft", "ArrowRight"] : ["ArrowUp", "ArrowDown"]);
1836
+ this.grippable(this.changes.grip, (m) => m.clientX - this.changes.split.getBoundingClientRect().left, () => this.settings.steps, (size) => {
1837
+ this.settings.steps = size;
1838
+ this.applySteps();
1839
+ }, () => ["ArrowRight", "ArrowLeft"]);
1840
+ }
1841
+ /** The Changes tab's column of steps, its width kept inside the tab: room for what changed beside it. */
1842
+ applySteps() {
1843
+ const room = this.changes.split.getBoundingClientRect().width;
1844
+ const size = room ? Math.max(140, Math.min(room - 240 - GRIP, Math.round(this.settings.steps))) : Math.round(this.settings.steps);
1845
+ this.settings.steps = size;
1846
+ this.changes.split.style.setProperty("--steps", `${size}px`);
1847
+ this.changes.grip.setAttribute("aria-valuenow", String(size));
1848
+ this.lists.changes.paint();
1849
+ }
1850
+ /** A grip: `measure` gives the size a pointer at a point asks for; `keys` the keys that grow and shrink it. */
1851
+ grippable(grip, measure, size, resize, keys) {
1852
+ grip.addEventListener("pointerdown", (e) => {
1853
+ e.preventDefault();
1854
+ grip.setPointerCapture(e.pointerId);
1855
+ const move = (m) => resize(measure(m));
1856
+ const up = () => {
1857
+ grip.removeEventListener("pointermove", move);
1858
+ grip.removeEventListener("pointerup", up);
1859
+ this.save();
1860
+ };
1861
+ grip.addEventListener("pointermove", move);
1862
+ grip.addEventListener("pointerup", up);
1863
+ });
1864
+ grip.addEventListener("keydown", (e) => {
1865
+ const [grow, shrink] = keys();
1866
+ if (e.key !== grow && e.key !== shrink) return;
1867
+ e.preventDefault();
1868
+ resize(size() + (e.key === grow ? 10 : -10));
1869
+ this.save();
1870
+ });
1871
+ }
1872
+ /** The side's size, kept inside what the panel holds: room for the list beside it, and for the side itself. */
1873
+ applySide() {
1874
+ const bottom = this.settings.dock === "bottom";
1875
+ const b = this.body.getBoundingClientRect();
1876
+ const room = bottom ? b.width : b.height;
1877
+ const [min, list] = bottom ? [200, 240] : [100, 120];
1878
+ const wanted = bottom ? this.settings.side : this.settings.sideHeight;
1879
+ const size = room ? Math.max(min, Math.min(room - list - GRIP, Math.round(wanted))) : Math.round(wanted);
1880
+ if (bottom) this.settings.side = size;
1881
+ else this.settings.sideHeight = size;
1882
+ this.body.style.setProperty("--side", `${size}px`);
1883
+ this.splitGrip.setAttribute("aria-orientation", bottom ? "vertical" : "horizontal");
1884
+ this.splitGrip.setAttribute("aria-valuenow", String(size));
1885
+ for (const list of Object.values(this.lists)) list.paint();
1886
+ }
1887
+ /** Left and right move between the tabs. */
1888
+ tabKeys(e) {
1889
+ if (!e.target.classList.contains("tab") || e.key !== "ArrowLeft" && e.key !== "ArrowRight") return;
1890
+ const order = TABS.map(([t]) => t);
1891
+ const next = order[(order.indexOf(this.settings.tab) + (e.key === "ArrowRight" ? 1 : order.length - 1)) % order.length];
1892
+ this.show(next);
1893
+ this.tabs.get(next).focus();
1894
+ }
1895
+ };
1896
+ /** The ref a projection node holds, when a row is one. */
1897
+ function refOfValue(v) {
1898
+ const ref = v && typeof v === "object" && !Array.isArray(v) ? v.ref : void 0;
1899
+ return typeof ref === "string" && /^e\d+$/.test(ref) ? ref : void 0;
1900
+ }
1901
+ /** An element and the elements around it, out to the document, across shadow roots' edges. */
1902
+ function* around(el) {
1903
+ for (let e = el; e; e = e.parentElement ?? (e.getRootNode() instanceof ShadowRoot ? e.getRootNode().host : null)) yield e;
1904
+ }
1905
+ /** The box that scrolls an element along an axis: the nearest around it that can, else the page. */
1906
+ function scrollerOf(el, axis) {
1907
+ for (let e = el.parentElement ?? (el.getRootNode() instanceof ShadowRoot ? el.getRootNode().host : null); e; e = e.parentElement ?? (e.getRootNode() instanceof ShadowRoot ? e.getRootNode().host : null)) {
1908
+ if (e === e.ownerDocument.body || e === e.ownerDocument.documentElement) break;
1909
+ const style = getComputedStyle(e);
1910
+ const overflow = axis === "y" ? style.overflowY : style.overflowX;
1911
+ if ((axis === "y" ? e.scrollHeight > e.clientHeight : e.scrollWidth > e.clientWidth) && /auto|scroll|overlay/.test(overflow)) return e;
1912
+ }
1913
+ return el.ownerDocument.scrollingElement ?? el.ownerDocument.documentElement;
1914
+ }
1915
+ /** The accessibility tree as text, as the Tree tab shows it: a line a node, indented, ignored nodes with their reasons. */
1916
+ function treeText(root) {
1917
+ const out = [];
1918
+ const walk = (n, depth) => {
1919
+ const parts = [
1920
+ n.role,
1921
+ n.name ? JSON.stringify(n.name) : "",
1922
+ n.value !== void 0 ? `value=${JSON.stringify(n.value)}` : ""
1923
+ ];
1924
+ for (const [k, v] of Object.entries(n.properties ?? {})) if (v !== false) parts.push(`${k}=${v}`);
1925
+ if (n.node !== void 0) parts.push(`e${n.node}`);
1926
+ if (n.ignored) parts.push(`(ignored: ${n.ignored.join(", ") || "no reason given"})`);
1927
+ out.push(`${" ".repeat(depth)}${parts.filter(Boolean).join(" ")}`);
1928
+ for (const c of n.children) walk(c, depth + 1);
1929
+ };
1930
+ walk(root, 0);
1931
+ return out.join("\n");
1932
+ }
1933
+ // src/index.ts
1934
+ const NOTHING = {
1935
+ open() {},
1936
+ close() {},
1937
+ unmount() {}
1938
+ };
1939
+ const mounted = /* @__PURE__ */ new WeakMap();
1940
+ /**
1941
+ * Put the panel on the page: a button in a corner that opens what an agent reads from the page. Call it in a
1942
+ * development build only (the package gives a production build a `mount` that does nothing). Under a browser
1943
+ * automation (navigator.webdriver) it does nothing unless asked (`inAutomation`), and a second call on the same page
1944
+ * returns the panel already there.
1945
+ */
1946
+ function mount(options = {}) {
1947
+ const doc = options.document ?? globalThis.document;
1948
+ const win = doc?.defaultView;
1949
+ if (!doc || !win || win.navigator.webdriver && !options.inAutomation) return NOTHING;
1950
+ const there = mounted.get(doc);
1951
+ if (there) return there;
1952
+ const panel = new Panel(doc, options);
1953
+ const devtools = {
1954
+ open: () => panel.open(),
1955
+ close: () => panel.close(),
1956
+ unmount: () => {
1957
+ panel.unmount();
1958
+ mounted.delete(doc);
1959
+ }
1960
+ };
1961
+ mounted.set(doc, devtools);
1962
+ return devtools;
1963
+ }
1964
+ export { mount };