@agent-om/devtools 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +124 -36
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -119,6 +119,8 @@ function jsonRow(doc, row, onToggle) {
119
119
  }
120
120
  return el;
121
121
  }
122
+ // src/list.ts
123
+ const ROW = 20;
122
124
  /** Rows drawn beyond those in sight, each way. */
123
125
  const OVERSCAN = 12;
124
126
  var VirtualList = class {
@@ -171,16 +173,16 @@ var VirtualList = class {
171
173
  this.hooks = hooks;
172
174
  this.selected = selected;
173
175
  if (this.active >= count) this.active = -1;
174
- this.rows.style.height = `${count * 20}px`;
176
+ this.rows.style.height = `${count * ROW}px`;
175
177
  this.paint();
176
178
  }
177
179
  /** Scroll a row into sight: one step for the keys, to the middle for a row out of sight (`middle`). */
178
180
  reveal(i, middle = false) {
179
181
  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
+ const top = i * ROW;
183
+ if ((top < this.el.scrollTop || top + ROW > this.el.scrollTop + this.el.clientHeight) && middle) this.el.scrollTop = Math.max(0, top - (this.el.clientHeight - ROW) / 2);
182
184
  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;
185
+ else if (top + ROW > this.el.scrollTop + this.el.clientHeight) this.el.scrollTop = top + ROW - this.el.clientHeight;
184
186
  this.top = this.el.scrollTop;
185
187
  this.paint();
186
188
  }
@@ -197,8 +199,8 @@ var VirtualList = class {
197
199
  paint() {
198
200
  const top = this.el.isConnected ? this.el.scrollTop : this.top;
199
201
  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 first = Math.max(0, Math.floor(top / ROW) - OVERSCAN);
203
+ const last = Math.min(this.count, Math.ceil((top + height) / ROW) + OVERSCAN);
202
204
  const out = [];
203
205
  for (let i = first; i < last; i++) {
204
206
  const r = this.hooks.row(i);
@@ -209,7 +211,7 @@ var VirtualList = class {
209
211
  r.id = `${this.id}-${i}`;
210
212
  r.setAttribute("role", "option");
211
213
  r.setAttribute("aria-selected", String(i === this.selected));
212
- r.style.top = `${i * 20}px`;
214
+ r.style.top = `${i * ROW}px`;
213
215
  out.push(r);
214
216
  }
215
217
  this.rows.replaceChildren(...out);
@@ -218,7 +220,7 @@ var VirtualList = class {
218
220
  }
219
221
  key(e) {
220
222
  if (!this.count) return;
221
- const page = Math.max(1, Math.floor(this.el.clientHeight / 20) - 1);
223
+ const page = Math.max(1, Math.floor(this.el.clientHeight / ROW) - 1);
222
224
  const from = this.active >= 0 ? this.active : this.selected;
223
225
  const to = {
224
226
  ArrowDown: from + 1,
@@ -266,10 +268,12 @@ const refIn = (text) => /\[(e\d+)\]/.exec(text)?.[1];
266
268
  /** Most ref labels drawn at once: a page with thousands in sight shows the first ones. */
267
269
  const LABELS_MAX = 600;
268
270
  var Overlay = class {
271
+ view;
269
272
  hoverBox;
270
273
  label;
271
274
  selBox;
272
275
  refs;
276
+ moved;
273
277
  hovered = null;
274
278
  hoverText = "";
275
279
  selected = null;
@@ -278,7 +282,9 @@ var Overlay = class {
278
282
  refsOn = false;
279
283
  read;
280
284
  refsDirty = false;
285
+ destroyed = false;
281
286
  constructor(doc, layer) {
287
+ this.view = doc.defaultView ?? window;
282
288
  this.hoverBox = h(doc, "div", {
283
289
  class: "box",
284
290
  hidden: true
@@ -293,16 +299,33 @@ var Overlay = class {
293
299
  });
294
300
  this.refs = h(doc, "div");
295
301
  layer.append(this.refs, this.selBox, this.hoverBox, this.label);
296
- const view = doc.defaultView;
297
- const moved = () => {
302
+ this.moved = () => {
298
303
  this.refsDirty = true;
299
304
  this.loop();
300
305
  };
301
- view.addEventListener("scroll", moved, {
306
+ this.view.addEventListener("scroll", this.moved, {
302
307
  capture: true,
303
308
  passive: true
304
309
  });
305
- view.addEventListener("resize", moved, { passive: true });
310
+ this.view.addEventListener("resize", this.moved, { passive: true });
311
+ }
312
+ /** Remove window listeners, cancel any pending frame, and release references. */
313
+ destroy() {
314
+ if (this.destroyed) return;
315
+ this.destroyed = true;
316
+ this.view.removeEventListener("scroll", this.moved, { capture: true });
317
+ this.view.removeEventListener("resize", this.moved);
318
+ if (this.frame) {
319
+ this.view.cancelAnimationFrame(this.frame);
320
+ this.frame = 0;
321
+ }
322
+ this.hovered = null;
323
+ this.hoverText = "";
324
+ this.selected = null;
325
+ this.read = void 0;
326
+ this.refsOn = false;
327
+ this.refsDirty = false;
328
+ this.refs.replaceChildren();
306
329
  }
307
330
  /** Outline what the pointer or the keys rest on, labelled. */
308
331
  hover(n, text = "") {
@@ -323,9 +346,10 @@ var Overlay = class {
323
346
  this.loop();
324
347
  }
325
348
  loop() {
326
- if (this.frame) return;
327
- this.frame = requestAnimationFrame(() => {
349
+ if (this.destroyed || this.frame) return;
350
+ this.frame = this.view.requestAnimationFrame(() => {
328
351
  this.frame = 0;
352
+ if (this.destroyed) return;
329
353
  this.draw();
330
354
  if (this.hovered || this.selected) this.loop();
331
355
  });
@@ -354,27 +378,29 @@ var Overlay = class {
354
378
  this.label.hidden = false;
355
379
  const height = this.label.offsetHeight;
356
380
  const width = this.label.offsetWidth;
357
- const vh = innerHeight;
381
+ const vh = this.view.innerHeight;
358
382
  let top = r.bottom + 4;
359
383
  if (top + height > vh) top = r.top - height - 4;
360
384
  if (top < 0) top = Math.max(0, vh - height);
361
385
  this.label.style.top = `${top}px`;
362
- this.label.style.left = `${Math.max(4, Math.min(r.left, innerWidth - width - 8))}px`;
386
+ this.label.style.left = `${Math.max(4, Math.min(r.left, this.view.innerWidth - width - 8))}px`;
363
387
  }
364
388
  /** Each ref of the read whose element is in sight, as a small label at its top right corner. */
365
389
  drawRefs(read) {
366
390
  const labels = [];
367
391
  const doc = this.refs.ownerDocument;
392
+ const vw = this.view.innerWidth;
393
+ const vh = this.view.innerHeight;
368
394
  for (const [ref, node] of read.nodes) {
369
395
  if (labels.length >= LABELS_MAX) break;
370
396
  if (!CONTROLS.has(node.role) && !node.clickable && !node.focusable && !node.editable) continue;
371
397
  const el = nodeById(Number(ref.slice(1)));
372
398
  if (!el || el.nodeType !== Node.ELEMENT_NODE) continue;
373
399
  const r = el.getBoundingClientRect();
374
- if (!r.width || !r.height || r.bottom < 0 || r.top > innerHeight || r.right < 0 || r.left > innerWidth) continue;
400
+ if (!r.width || !r.height || r.bottom < 0 || r.top > vh || r.right < 0 || r.left > vw) continue;
375
401
  const label = h(doc, "span", { class: node.clickable || node.focusable || !node.name && !!node.hint ? "ref unnamed" : "ref" }, ref);
376
402
  label.style.top = `${Math.max(0, r.top - 6)}px`;
377
- label.style.left = `${Math.min(innerWidth - 40, r.right - 18)}px`;
403
+ label.style.left = `${Math.min(vw - 40, r.right - 18)}px`;
378
404
  labels.push(label);
379
405
  }
380
406
  this.refs.replaceChildren(...labels);
@@ -401,6 +427,7 @@ function place(box, r) {
401
427
  */
402
428
  var Picker = class {
403
429
  on = false;
430
+ releaseTimer = 0;
404
431
  win;
405
432
  host;
406
433
  onHover;
@@ -417,21 +444,25 @@ var Picker = class {
417
444
  }
418
445
  start() {
419
446
  if (this.on) return;
447
+ this.win.clearTimeout(this.releaseTimer);
448
+ this.releaseTimer = 0;
420
449
  this.on = true;
421
450
  for (const type of SWALLOWED) this.win.addEventListener(type, this.swallow, true);
422
451
  this.win.addEventListener("pointermove", this.move, true);
423
452
  this.win.addEventListener("keydown", this.key, true);
424
453
  }
425
454
  stop() {
426
- if (!this.on) return;
455
+ const wasOn = this.on;
427
456
  this.on = false;
428
457
  this.release();
429
458
  this.win.removeEventListener("pointermove", this.move, true);
430
459
  this.win.removeEventListener("keydown", this.key, true);
431
- this.onHover(null, false);
460
+ if (wasOn) this.onHover(null, false);
432
461
  }
433
462
  /** Give the page its pointer events back. */
434
463
  release() {
464
+ this.win.clearTimeout(this.releaseTimer);
465
+ this.releaseTimer = 0;
435
466
  if (this.on) return;
436
467
  for (const type of SWALLOWED) this.win.removeEventListener(type, this.swallow, true);
437
468
  }
@@ -452,7 +483,8 @@ var Picker = class {
452
483
  this.win.removeEventListener("keydown", this.key, true);
453
484
  this.onHover(null, false);
454
485
  this.onPick(el, e.altKey);
455
- this.win.setTimeout(() => this.release(), 600);
486
+ this.win.clearTimeout(this.releaseTimer);
487
+ this.releaseTimer = this.win.setTimeout(() => this.release(), 600);
456
488
  } else if (e.type === "click" && !this.on) this.release();
457
489
  };
458
490
  move = (e) => {
@@ -728,11 +760,13 @@ const SHOWN = /^(id|class|role|type|name|for|href|alt|title|tabindex|contentedit
728
760
  const VALUE_MAX = 60;
729
761
  /** An element's opening tag, with the attributes that say what it is, secrets taken out. */
730
762
  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}"`;
763
+ const attrs = [...el.attributes].filter((a) => SHOWN.test(a.name)).map((a) => {
764
+ if (a.value === "") return a.name;
765
+ const clean = withoutSecrets(a.value, secrets);
766
+ const v = clean.length > VALUE_MAX ? `${clean.slice(0, VALUE_MAX)}…` : clean;
767
+ return `${a.name}="${v}"`;
734
768
  });
735
- return withoutSecrets(`<${el.localName}${attrs.length ? " " + attrs.join(" ") : ""}>`, secrets);
769
+ return `<${el.localName}${attrs.length ? " " + attrs.join(" ") : ""}>`;
736
770
  }
737
771
  /** The element around this one, across a shadow root's edge. */
738
772
  const up = (el) => el.parentElement ?? (el.getRootNode() instanceof ShadowRoot ? el.getRootNode().host : null);
@@ -741,7 +775,7 @@ function leftOutBecause(el, secrets) {
741
775
  for (let e = el; e; e = up(e)) {
742
776
  const at = e === el ? "" : ` on ${tagOf(e, secrets)}`;
743
777
  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}`;
778
+ if (e.hasAttribute("data-agentom-redact") && !e.querySelector("input, textarea, select, [contenteditable]:not([contenteditable=\"false\" i])")) return `kept out by the app: data-agentom-redact${at}`;
745
779
  if (e.getAttribute("aria-hidden") === "true") return `aria-hidden="true"${at}`;
746
780
  if (e.hasAttribute("inert")) return `inert${at}`;
747
781
  }
@@ -750,7 +784,7 @@ function leftOutBecause(el, secrets) {
750
784
  visibilityProperty: true
751
785
  })) return "not drawn: display: none, visibility or content-visibility on it or around it";
752
786
  const modal = el.ownerDocument.querySelector("dialog:modal");
753
- if (modal && !modal.contains(el)) return "outside the open modal dialog";
787
+ if (modal && modal !== el && !within(modal, el)) return "outside the open modal dialog";
754
788
  return "the walk keeps no node for it: a script, a style, a field’s inside, whitespace";
755
789
  }
756
790
  /** The nearest element around this one that the projection has a node for. */
@@ -918,11 +952,39 @@ const TABS = [
918
952
  ["projection", "Projection"],
919
953
  ["tree", "Tree"]
920
954
  ];
955
+ const TAB_SET = /* @__PURE__ */ new Set([
956
+ "view",
957
+ "changes",
958
+ "projection",
959
+ "tree"
960
+ ]);
961
+ const DOCK_SET = /* @__PURE__ */ new Set(["bottom", "right"]);
962
+ const THEME_SET = /* @__PURE__ */ new Set([
963
+ "system",
964
+ "light",
965
+ "dark"
966
+ ]);
921
967
  function load(win) {
922
968
  try {
969
+ const raw = JSON.parse(win.localStorage.getItem(KEY) ?? "{}");
970
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return { ...DEFAULTS };
971
+ const s = raw;
972
+ const num = (v, fallback) => typeof v === "number" && Number.isFinite(v) ? v : fallback;
973
+ const bool = (v, fallback) => typeof v === "boolean" ? v : fallback;
923
974
  return {
924
- ...DEFAULTS,
925
- ...JSON.parse(win.localStorage.getItem(KEY) ?? "{}")
975
+ open: bool(s.open, DEFAULTS.open),
976
+ tab: typeof s.tab === "string" && TAB_SET.has(s.tab) ? s.tab : DEFAULTS.tab,
977
+ dock: typeof s.dock === "string" && DOCK_SET.has(s.dock) ? s.dock : DEFAULTS.dock,
978
+ height: num(s.height, DEFAULTS.height),
979
+ width: num(s.width, DEFAULTS.width),
980
+ side: num(s.side, DEFAULTS.side),
981
+ sideHeight: num(s.sideHeight, DEFAULTS.sideHeight),
982
+ steps: num(s.steps, DEFAULTS.steps),
983
+ interactive: bool(s.interactive, DEFAULTS.interactive),
984
+ match: typeof s.match === "string" ? s.match : DEFAULTS.match,
985
+ hideIgnored: bool(s.hideIgnored, DEFAULTS.hideIgnored),
986
+ refs: bool(s.refs, DEFAULTS.refs),
987
+ theme: typeof s.theme === "string" && THEME_SET.has(s.theme) ? s.theme : DEFAULTS.theme
926
988
  };
927
989
  } catch {
928
990
  return { ...DEFAULTS };
@@ -977,6 +1039,8 @@ var Panel = class {
977
1039
  revealRow = false;
978
1040
  jsonPath;
979
1041
  timer = 0;
1042
+ cancelIdle;
1043
+ unmounted = false;
980
1044
  observer;
981
1045
  cleanups = [];
982
1046
  constructor(doc, options = {}, mode = "closed") {
@@ -1180,21 +1244,30 @@ var Panel = class {
1180
1244
  this.makeRoom();
1181
1245
  }
1182
1246
  unmount() {
1247
+ if (this.unmounted) return;
1248
+ this.unmounted = true;
1183
1249
  this.picker.stop();
1250
+ this.overlay.destroy();
1184
1251
  this.observer.disconnect();
1185
1252
  this.win.clearTimeout(this.timer);
1186
- for (const c of this.cleanups) c();
1253
+ this.timer = 0;
1254
+ this.cancelIdle?.();
1255
+ this.cancelIdle = void 0;
1256
+ for (const c of this.cleanups.splice(0)) c();
1187
1257
  this.spacer.remove();
1188
1258
  this.host.remove();
1189
1259
  }
1190
1260
  /** Read the page now and show it. */
1191
1261
  refresh() {
1262
+ if (this.unmounted) return;
1192
1263
  this.win.clearTimeout(this.timer);
1264
+ this.timer = 0;
1193
1265
  const read = this.reader.read();
1194
1266
  this.read = read;
1195
1267
  const ms = read.ms.read + read.ms.merge;
1196
1268
  if (ms > SLOW && this.live && this.settings.open) {
1197
1269
  this.live = false;
1270
+ this.tools.live.setAttribute("aria-pressed", "false");
1198
1271
  this.slow = `Reads take ${Math.round(ms)} ms on this page, so the panel reads when you press ↻.`;
1199
1272
  }
1200
1273
  if (this.selected && !this.selected.isConnected) this.selected = null;
@@ -1202,15 +1275,26 @@ var Panel = class {
1202
1275
  }
1203
1276
  /** Read again once the page has been still for a moment (open and live), or later and only if cheap (closed). */
1204
1277
  schedule() {
1278
+ if (this.unmounted) return;
1205
1279
  this.win.clearTimeout(this.timer);
1206
1280
  if (this.settings.open) {
1207
1281
  if (this.live) this.timer = this.win.setTimeout(() => this.refresh(), SETTLE);
1208
1282
  } else if (!this.read || this.read.ms.read + this.read.ms.merge < CHEAP) this.timer = this.win.setTimeout(() => this.idle(() => this.refresh()), 1e3);
1209
1283
  }
1210
1284
  idle(fn) {
1285
+ this.cancelIdle?.();
1211
1286
  const w = this.win;
1212
- if (w.requestIdleCallback) w.requestIdleCallback(fn, { timeout: 1e3 });
1213
- else this.win.setTimeout(fn, 50);
1287
+ const run = () => {
1288
+ this.cancelIdle = void 0;
1289
+ if (!this.unmounted) fn();
1290
+ };
1291
+ if (w.requestIdleCallback) {
1292
+ const id = w.requestIdleCallback(run, { timeout: 1e3 });
1293
+ this.cancelIdle = () => w.cancelIdleCallback?.(id);
1294
+ } else {
1295
+ const id = this.win.setTimeout(run, 50);
1296
+ this.cancelIdle = () => this.win.clearTimeout(id);
1297
+ }
1214
1298
  }
1215
1299
  mutated(records) {
1216
1300
  const mine = (n) => n === this.host || n === this.spacer;
@@ -1405,7 +1489,7 @@ var Panel = class {
1405
1489
  const row = viewLine(doc, l);
1406
1490
  if (/^\+ /.test(l)) row.classList.add("add");
1407
1491
  else if (/^- gone/.test(l)) row.classList.add("del");
1408
- else if (/^\s+(was|text was):/.test(l)) row.classList.add("was");
1492
+ else if (/^\s+was:/.test(l)) row.classList.add("was");
1409
1493
  return row;
1410
1494
  },
1411
1495
  hover: (i) => this.hoverRef(i < 0 ? void 0 : refIn(lines[i]), i < 0 ? "" : lines[i].trim()),
@@ -1853,13 +1937,17 @@ var Panel = class {
1853
1937
  e.preventDefault();
1854
1938
  grip.setPointerCapture(e.pointerId);
1855
1939
  const move = (m) => resize(measure(m));
1856
- const up = () => {
1940
+ const end = () => {
1857
1941
  grip.removeEventListener("pointermove", move);
1858
- grip.removeEventListener("pointerup", up);
1942
+ grip.removeEventListener("pointerup", end);
1943
+ grip.removeEventListener("pointercancel", end);
1944
+ grip.removeEventListener("lostpointercapture", end);
1859
1945
  this.save();
1860
1946
  };
1861
1947
  grip.addEventListener("pointermove", move);
1862
- grip.addEventListener("pointerup", up);
1948
+ grip.addEventListener("pointerup", end);
1949
+ grip.addEventListener("pointercancel", end);
1950
+ grip.addEventListener("lostpointercapture", end);
1863
1951
  });
1864
1952
  grip.addEventListener("keydown", (e) => {
1865
1953
  const [grow, shrink] = keys();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agent-om/devtools",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "A panel for development builds that shows what an AI agent reads from the page: the text sent to the model, the projection behind it, the accessibility tree, and what changed after each action.",
5
5
  "keywords": [
6
6
  "devtools",
@@ -38,10 +38,10 @@
38
38
  "dist"
39
39
  ],
40
40
  "peerDependencies": {
41
- "@agent-om/core": "^0.1.0"
41
+ "@agent-om/core": "^0.1.1"
42
42
  },
43
43
  "devDependencies": {
44
- "@agent-om/core": "0.1.0",
44
+ "@agent-om/core": "0.1.1",
45
45
  "tsdown": "0.23.1"
46
46
  },
47
47
  "publishConfig": {