@mgamadeus/ddd-agents 0.6.5 → 0.6.6

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/ui.js CHANGED
@@ -78,7 +78,7 @@ const PACKAGE_REQUEST_TYPES = ["READ_SCREEN", "LIST_TARGETS", "OPEN_TARGET", "HI
78
78
  const STEP_ASIDE_REQUEST_TYPES = ["OPEN_TARGET", "HIGHLIGHT", "READ_SCREEN"];
79
79
  const PICTURE_SIZES = ["small", "medium", "large"];
80
80
  const RENDER_MODES = ["colour", "greyscale", "machine"];
81
- const MAX_SCREEN_CHARS = 6e3;
81
+ const MAX_SCREEN_CHARS = MAX_ANSWER_TEXT_CHARS / 2;
82
82
  const REMEMBERED_REQUEST_IDS = 50;
83
83
  class ClientToolRequestRunner {
84
84
  constructor(adapters) {
@@ -256,12 +256,26 @@ class AgentUIClient {
256
256
  constructor(adapters, uiStateSource = null, clientId = createAgentUIClientId()) {
257
257
  this.acceptedClientToolRequestCount = 0;
258
258
  this.clientId = clientId;
259
+ this.adapters = adapters;
259
260
  this.runner = new ClientToolRequestRunner(adapters);
260
261
  this.uiStateSource = uiStateSource;
261
262
  }
263
+ /**
264
+ * The source's UI state with the agent window's mode (the window adapter) and the browser window's size, unless the
265
+ * source reports them itself: the agent learns whether its window covers the page and whether a panel fits beside it.
266
+ */
262
267
  readUIState() {
263
268
  try {
264
- return this.uiStateSource?.readUIState() ?? null;
269
+ const uiState = this.uiStateSource?.readUIState() ?? null;
270
+ if (uiState === null) return null;
271
+ const completedUIState = { ...uiState };
272
+ const agentWindowMode = uiState.agentWindow ?? this.adapters.agentWindow?.getMode?.() ?? null;
273
+ if (agentWindowMode !== null) completedUIState.agentWindow = agentWindowMode;
274
+ if (typeof window !== "undefined") {
275
+ completedUIState.viewportWidth = uiState.viewportWidth ?? Math.round(window.innerWidth);
276
+ completedUIState.viewportHeight = uiState.viewportHeight ?? Math.round(window.innerHeight);
277
+ }
278
+ return completedUIState;
265
279
  } catch {
266
280
  return null;
267
281
  }
@@ -310,7 +324,9 @@ const useAgentUIClient = (adapters, uiStateSource = null) => {
310
324
  }
311
325
  return clientRef.current;
312
326
  };
313
- const MAX_TABLE_ROWS = 25;
327
+ const MAX_TABLE_ROWS = 15;
328
+ const MAX_CELL_CHARS = 60;
329
+ const MAX_SECTION_TEXT_CHARS = 1200;
314
330
  const MAX_VALUE_CHARS = 200;
315
331
  const MAX_BUTTONS = 30;
316
332
  const HIDDEN_VALUE = "(hidden)";
@@ -343,10 +359,15 @@ const formatFieldLine = (field) => {
343
359
  const formatTable = (table, maxRows = MAX_TABLE_ROWS) => {
344
360
  const lines = [];
345
361
  const shownRows = table.rows.slice(0, maxRows);
362
+ const columnCount = Math.max(table.columns.length, ...shownRows.map((row) => row.cells.length));
363
+ const keptColumns = [];
364
+ for (let column = 0; column < columnCount; column += 1) {
365
+ if (shownRows.length === 0 || shownRows.some((row) => (row.cells[column] ?? "").trim() !== "")) keptColumns.push(column);
366
+ }
346
367
  lines.push(`Table${table.caption ? ` "${capValue(table.caption, 80)}"` : ""}: ${table.totalRows} rows on screen`);
347
- lines.push(`| ref | ${table.columns.map((column) => capValue(column, 40) || "-").join(" | ")} |`);
368
+ lines.push(`| ref | ${keptColumns.map((column) => capValue(table.columns[column] ?? "", 40) || "-").join(" | ")} |`);
348
369
  for (const row of shownRows) {
349
- const cells = row.cells.map((cell) => capValue(cell, 80).replace(/\|/g, "/"));
370
+ const cells = keptColumns.map((column) => capValue(row.cells[column] ?? "", MAX_CELL_CHARS).replace(/\|/g, "/"));
350
371
  lines.push(`| ${row.reference}${row.selected ? " (selected)" : ""} | ${cells.join(" | ")} |`);
351
372
  }
352
373
  if (table.totalRows > shownRows.length) lines.push(`… ${table.totalRows - shownRows.length} more rows on screen`);
@@ -374,12 +395,30 @@ const AGENT_ATTRIBUTES = {
374
395
  /** The element and everything in it is never read (the agent window itself) */
375
396
  skip: "data-agent-skip",
376
397
  /** The key of a table row (@mgamadeus/ddd-admin-ui) */
377
- rowKey: "data-row-key"
398
+ rowKey: "data-row-key",
399
+ /** The key of a main menu entry (@mgamadeus/ddd-admin-ui): `nav.<value>` */
400
+ navKey: "data-nav-key",
401
+ /** The key of a main menu group (@mgamadeus/ddd-admin-ui): `nav.<value>`; its entries sit inside it */
402
+ navGroup: "data-nav-group"
378
403
  };
379
404
  const SKIPPED_SELECTOR = `[${AGENT_ATTRIBUTES.skip}], script, style, noscript, template, svg, [aria-hidden="true"], [hidden]`;
380
405
  const SECRET_FIELD_SELECTOR = `[${AGENT_ATTRIBUTES.secret}] , input[type="password"], input[autocomplete="one-time-code"]`;
381
406
  const FIELD_SELECTOR = 'input:not([type="hidden"]):not([type="submit"]):not([type="button"]), textarea, select, [role="combobox"], [role="switch"], [role="checkbox"], [role="radiogroup"]';
382
407
  const BUTTON_SELECTOR = 'button, [role="button"], a[href]';
408
+ const TABLE_SELECTOR = 'table, [role="grid"], [role="table"]';
409
+ const ROW_CHECKBOX_SELECTOR = [TABLE_SELECTOR, `[${AGENT_ATTRIBUTES.rowKey}]`].flatMap((container) => container.split(", ").flatMap((part) => [`${part} input[type="checkbox"]`, `${part} [role="checkbox"]`])).join(", ");
410
+ const TEXT_SKIPPED_SELECTOR = [
411
+ SKIPPED_SELECTOR,
412
+ `[${AGENT_ATTRIBUTES.secret}]`,
413
+ "nav",
414
+ 'input, textarea, select, label, [role="combobox"], [role="switch"], [role="checkbox"], [role="radiogroup"]',
415
+ '[contenteditable="true"]',
416
+ `${TABLE_SELECTOR}, [${AGENT_ATTRIBUTES.rowKey}]`,
417
+ 'button, [role="button"], a[href], [role="tablist"], [role="tab"], [role="menu"], [role="listbox"]',
418
+ 'h1, h2, h3, h4, [role="heading"]',
419
+ '[role="dialog"], [role="alertdialog"], [role="alert"], [role="status"]'
420
+ ].join(", ");
421
+ const MAIN_MENU_HEADING = "## Main menu (highlightOnScreen marks an entry by its nav. reference)";
383
422
  const MAX_PAGE_TABS = 16;
384
423
  const ID_COLUMN_LABELS = /* @__PURE__ */ new Set(["id", "#", "nr", "no"]);
385
424
  class ScreenReferenceRegistry {
@@ -413,6 +452,7 @@ const isRendered = (element) => {
413
452
  if (typeof htmlElement.getClientRects === "function" && htmlElement.getClientRects().length === 0) return false;
414
453
  return true;
415
454
  };
455
+ const isChecked = (checkbox) => checkbox instanceof HTMLInputElement ? checkbox.checked : checkbox.getAttribute("aria-checked") === "true";
416
456
  const textOf = (element) => {
417
457
  if (!element) return "";
418
458
  const htmlElement = element;
@@ -470,6 +510,7 @@ const invalidMessageOf = (field) => {
470
510
  class DomScreenReader {
471
511
  constructor(options) {
472
512
  this.options = options;
513
+ this.textReadContainers = /* @__PURE__ */ new Set();
473
514
  }
474
515
  read({ focus, maxChars }) {
475
516
  const location = this.options.currentLocation();
@@ -490,9 +531,11 @@ class DomScreenReader {
490
531
  references[unique] = labelOfElement(element);
491
532
  return unique;
492
533
  };
534
+ this.textReadContainers = /* @__PURE__ */ new Set();
493
535
  const parts = [`# ${capValue(location.title, 120)}`, `Route: ${location.route}`];
494
536
  const unsaved = this.options.describeUnsavedChanges();
495
537
  parts.push(`Unsaved changes: ${unsaved}`);
538
+ parts.push(this.describeWindow());
496
539
  const root = this.options.findRoot();
497
540
  const dialogs = Array.from(document.querySelectorAll('[role="dialog"], [role="alertdialog"]')).filter(isRendered);
498
541
  for (const dialog of dialogs) {
@@ -522,8 +565,57 @@ class DomScreenReader {
522
565
  if (anchors.length > 0) {
523
566
  parts.push(`Anchors: ${anchors.map((anchor) => register(`a.${anchor.getAttribute(AGENT_ATTRIBUTES.anchor)}`, anchor)).join(" · ")}`);
524
567
  }
568
+ const menu = this.readNavigation(register).join("\n");
525
569
  this.options.registry.replace(elements);
526
- return { markdown: joinScreenParts(parts, maxChars), route: location.route, title: location.title, references };
570
+ const page = joinScreenParts(parts, Math.max(maxChars - (menu === "" ? 0 : menu.length + 1), Math.floor(maxChars / 2)));
571
+ return { markdown: menu === "" ? page : `${page}
572
+ ${menu}`, route: location.route, title: location.title, references };
573
+ }
574
+ /** `Agent window: … · browser 1440×900 px`, or `Browser window: 1440×900 px` without a description of the window. */
575
+ describeWindow() {
576
+ const size = typeof window === "undefined" ? "unknown" : `${Math.round(window.innerWidth)}×${Math.round(window.innerHeight)} px`;
577
+ const agentWindow = this.options.describeAgentWindow?.() ?? null;
578
+ return agentWindow ? `Agent window: ${oneLine(agentWindow)} · browser ${size}` : `Browser window: ${size}`;
579
+ }
580
+ /**
581
+ * The main menu: its entries (`data-nav-key`) and groups (`data-nav-group`) on the screen, as `nav.<key>`
582
+ * references (the menu of a compact layout shows icons only: an entry's name is its aria-label). A closed group
583
+ * shows only itself.
584
+ */
585
+ readNavigation(register) {
586
+ const navigation = this.options.findNavigation?.() ?? null;
587
+ if (!navigation) return [];
588
+ const groups = /* @__PURE__ */ new Map();
589
+ const readKeys = /* @__PURE__ */ new Set();
590
+ for (const mark of Array.from(navigation.querySelectorAll(`[${AGENT_ATTRIBUTES.navKey}], [${AGENT_ATTRIBUTES.navGroup}]`))) {
591
+ const groupKey = mark.getAttribute(AGENT_ATTRIBUTES.navGroup);
592
+ const key = mark.getAttribute(AGENT_ATTRIBUTES.navKey) ?? groupKey;
593
+ const element = mark.tagName === "LI" ? mark.firstElementChild ?? mark : mark;
594
+ if (key === null || readKeys.has(key) || !isRendered(element)) continue;
595
+ const label = labelOfElement(element);
596
+ if (label === "") continue;
597
+ readKeys.add(key);
598
+ const reference = register(`nav.${slugOf(key)}`, element);
599
+ const entry = `${reference} "${capValue(label, 40)}"${mark.getAttribute("aria-current") !== null ? " (open)" : ""}`;
600
+ if (groupKey !== null) {
601
+ const isExpanded = Array.from(mark.querySelectorAll(`[${AGENT_ATTRIBUTES.navKey}]`)).some(isRendered);
602
+ groups.set(groupKey, [isExpanded ? `${capValue(label, 40)} (${reference}):` : `${entry} (closed group)`]);
603
+ continue;
604
+ }
605
+ const owningGroup = mark.parentElement?.closest(`[${AGENT_ATTRIBUTES.navGroup}]`)?.getAttribute(AGENT_ATTRIBUTES.navGroup) ?? "";
606
+ groups.set(owningGroup, [...groups.get(owningGroup) ?? [], entry]);
607
+ }
608
+ if (readKeys.size === 0) return [];
609
+ const lines = [MAIN_MENU_HEADING];
610
+ for (const [groupKey, entries] of groups) {
611
+ if (groupKey === "") {
612
+ lines.push(`- ${entries.join(" · ")}`);
613
+ continue;
614
+ }
615
+ const [head, ...rest] = entries;
616
+ lines.push(rest.length > 0 ? `- ${head} ${rest.join(" · ")}` : `- ${head}`);
617
+ }
618
+ return lines;
527
619
  }
528
620
  readWhole(element) {
529
621
  if (element.matches(FIELD_SELECTOR)) return valueOfField(element).value;
@@ -546,8 +638,17 @@ class DomScreenReader {
546
638
  lines.push(...this.readElements(container, register, seen));
547
639
  return lines;
548
640
  }
641
+ /**
642
+ * The plain text, tables, cards, fields and buttons of a container. Tables and cards go before the fields: a page's
643
+ * filters and forms never push its list out of the read.
644
+ */
549
645
  readElements(container, register, seen) {
550
646
  const lines = [];
647
+ const text = this.readPlainText(container);
648
+ this.textReadContainers.add(container);
649
+ if (text !== "") lines.push(`Text: ${text}`);
650
+ for (const checkbox of Array.from(container.querySelectorAll(ROW_CHECKBOX_SELECTOR))) seen.add(checkbox);
651
+ lines.push(...this.readTablesAndCards(container, register, seen));
551
652
  for (const field of Array.from(container.querySelectorAll(FIELD_SELECTOR))) {
552
653
  if (seen.has(field) || !isRendered(field) || field.closest('[role="radiogroup"]') !== null && field.getAttribute("role") !== "radiogroup") continue;
553
654
  seen.add(field);
@@ -565,23 +666,6 @@ class DomScreenReader {
565
666
  };
566
667
  lines.push(formatFieldLine(facts));
567
668
  }
568
- for (const table of Array.from(container.querySelectorAll('table, [role="grid"], [role="table"]'))) {
569
- if (seen.has(table) || !isRendered(table)) continue;
570
- seen.add(table);
571
- lines.push(...formatTable(this.readTable(table, register)));
572
- }
573
- const cards = Array.from(container.querySelectorAll(`[${AGENT_ATTRIBUTES.rowKey}]`)).filter(
574
- (card) => !seen.has(card) && !card.closest('table, [role="grid"], [role="table"]') && isRendered(card)
575
- );
576
- if (cards.length > 0) {
577
- lines.push(`Cards: ${cards.length} on screen`);
578
- for (const card of cards.slice(0, MAX_TABLE_ROWS)) {
579
- seen.add(card);
580
- for (const inner of Array.from(card.querySelectorAll(BUTTON_SELECTOR))) seen.add(inner);
581
- lines.push(`- ${register(`row.${rowKeyOf(card, null, 0)}`, card)}: ${capValue(textOf(card), 160)}`);
582
- }
583
- if (cards.length > MAX_TABLE_ROWS) lines.push(`… ${cards.length - MAX_TABLE_ROWS} more cards on screen`);
584
- }
585
669
  const buttons = [];
586
670
  for (const button of Array.from(container.querySelectorAll(BUTTON_SELECTOR))) {
587
671
  if (seen.has(button) || !isRendered(button) || button.closest('table, [role="grid"], [role="tablist"]')) continue;
@@ -598,6 +682,66 @@ class DomScreenReader {
598
682
  if (buttonLine) lines.push(buttonLine);
599
683
  return lines;
600
684
  }
685
+ /** The tables of a container and the cards of a card grid (`EntityListGrid` sets `data-row-key` on each). */
686
+ readTablesAndCards(container, register, seen) {
687
+ const lines = [];
688
+ for (const table of Array.from(container.querySelectorAll(TABLE_SELECTOR))) {
689
+ if (seen.has(table) || !isRendered(table)) continue;
690
+ seen.add(table);
691
+ lines.push(...formatTable(this.readTable(table, register)));
692
+ }
693
+ const cards = Array.from(container.querySelectorAll(`[${AGENT_ATTRIBUTES.rowKey}]`)).filter(
694
+ (card) => !seen.has(card) && !card.closest(TABLE_SELECTOR) && isRendered(card)
695
+ );
696
+ if (cards.length > 0) {
697
+ lines.push(`Cards: ${cards.length} on screen`);
698
+ for (const card of cards) seen.add(card);
699
+ for (const card of cards.slice(0, MAX_TABLE_ROWS)) {
700
+ for (const inner of Array.from(card.querySelectorAll(BUTTON_SELECTOR))) seen.add(inner);
701
+ const checkbox = card.querySelector('input[type="checkbox"], [role="checkbox"]');
702
+ const selected = checkbox !== null && isChecked(checkbox) ? " (selected)" : "";
703
+ lines.push(`- ${register(`row.${rowKeyOf(card, null, 0)}`, card)}${selected}: ${capValue(textOf(card), 160)}`);
704
+ }
705
+ if (cards.length > MAX_TABLE_ROWS) lines.push(`… ${cards.length - MAX_TABLE_ROWS} more cards on screen`);
706
+ }
707
+ return lines;
708
+ }
709
+ /**
710
+ * The visible text of a container that no field, table, card or button line carries, its pieces joined with " · "
711
+ * (pieces of one block with a space), without the text of a section read before: a message, a record's facts or a
712
+ * dashboard figure the user reads.
713
+ */
714
+ readPlainText(container) {
715
+ if (this.textReadContainers.has(container)) return "";
716
+ let text = "";
717
+ let previousBlock = null;
718
+ const walker = document.createTreeWalker(container, NodeFilter.SHOW_TEXT);
719
+ for (let node = walker.nextNode(); node !== null && text.length < MAX_SECTION_TEXT_CHARS; node = walker.nextNode()) {
720
+ const piece = oneLine(node.textContent ?? "");
721
+ const parent = node.parentElement;
722
+ if (piece === "" || parent === null) continue;
723
+ const skipped = parent.closest(TEXT_SKIPPED_SELECTOR);
724
+ if (skipped !== null && skipped !== container && container.contains(skipped)) continue;
725
+ if (!isRendered(parent) || this.isInsideReadSection(parent, container)) continue;
726
+ const block = this.blockOf(parent, container);
727
+ text = text === "" ? piece : `${text}${block === previousBlock ? " " : " · "}${piece}`;
728
+ previousBlock = block;
729
+ }
730
+ return text.length <= MAX_SECTION_TEXT_CHARS ? text : `${text.slice(0, MAX_SECTION_TEXT_CHARS - 1)}…`;
731
+ }
732
+ /** The nearest ancestor that is not laid out inline (the container at the latest). */
733
+ blockOf(element, container) {
734
+ for (let ancestor = element; ancestor !== null && ancestor !== container; ancestor = ancestor.parentElement) {
735
+ if (!window.getComputedStyle(ancestor).display.startsWith("inline")) return ancestor;
736
+ }
737
+ return container;
738
+ }
739
+ isInsideReadSection(element, container) {
740
+ for (let ancestor = element; ancestor !== null && ancestor !== container; ancestor = ancestor.parentElement) {
741
+ if (this.textReadContainers.has(ancestor)) return true;
742
+ }
743
+ return false;
744
+ }
601
745
  readTable(table, register) {
602
746
  const headerCells = Array.from(table.querySelectorAll('thead th, [role="columnheader"]'));
603
747
  const columns = headerCells.map((cell) => textOf(cell));
@@ -611,10 +755,15 @@ class DomScreenReader {
611
755
  const secret = cell.closest(`[${AGENT_ATTRIBUTES.secret}]`) !== null || cell.querySelector(`[${AGENT_ATTRIBUTES.secret}]`) !== null;
612
756
  return secret ? "(hidden)" : textOf(cell);
613
757
  }),
614
- selected: row.getAttribute("aria-selected") === "true" || row.getAttribute("data-state") === "selected"
758
+ selected: row.getAttribute("aria-selected") === "true" || row.getAttribute("data-state") === "selected" || this.isRowChecked(row)
615
759
  }));
616
760
  return { caption: textOf(table.querySelector("caption")) || table.getAttribute("aria-label"), columns, rows, totalRows: bodyRows.length };
617
761
  }
762
+ /** Whether the row's selection checkbox is checked. */
763
+ isRowChecked(row) {
764
+ const checkbox = row.querySelector('input[type="checkbox"], [role="checkbox"]');
765
+ return checkbox !== null && isChecked(checkbox);
766
+ }
618
767
  }
619
768
  const MARK_VISIBLE_MS = 8e3;
620
769
  const MAX_NOTE_CHARS = 120;
@@ -942,7 +1091,7 @@ const PICTURE_LIMITATIONS = [
942
1091
  "content of other origins (embedded frames, some images and fonts) can be blank or replaced",
943
1092
  "video, canvas and WebGL content can be blank",
944
1093
  "password fields show dots; fields the application marks secret or skipped are left out",
945
- "scrolled-away parts of a scroll container are shown expanded, so the picture can be longer than the window"
1094
+ "only what is in the browser window is shown; parts scrolled out of view are left out (the text read has them)"
946
1095
  ];
947
1096
  const SCREEN_PICTURE_PRESETS = {
948
1097
  small: { maxLongEdge: 1024, maxPixels: 786432, maxBytes: 15e4 },
@@ -988,14 +1137,85 @@ const convertPixelsToGrey = (pixels, greyLevels = null) => {
988
1137
  }
989
1138
  };
990
1139
  const isScrollContainer = (element, style) => element.scrollHeight > element.clientHeight + 1 && (style.overflowY === "auto" || style.overflowY === "scroll");
991
- const copyComputedStyle = (style, clone) => {
1140
+ const defaultStylesByTag = /* @__PURE__ */ new Map();
1141
+ const createFrameDefaultStyleSource = (documentOfRoot) => {
1142
+ let frame = null;
1143
+ const frameDocument = () => {
1144
+ if (frame === null) {
1145
+ frame = documentOfRoot.createElement("iframe");
1146
+ frame.setAttribute("aria-hidden", "true");
1147
+ frame.setAttribute(AGENT_SKIP_ATTRIBUTE, "");
1148
+ frame.style.cssText = "position:fixed;left:-100000px;top:0;width:10px;height:10px;visibility:hidden;border:0;";
1149
+ documentOfRoot.body.appendChild(frame);
1150
+ }
1151
+ return frame.contentDocument;
1152
+ };
1153
+ return {
1154
+ valueOf: (tagName, namespaceUri, property) => {
1155
+ const cacheKey = `${namespaceUri ?? ""} ${tagName}`;
1156
+ let defaults = defaultStylesByTag.get(cacheKey);
1157
+ if (defaults === void 0) {
1158
+ defaults = /* @__PURE__ */ new Map();
1159
+ const emptyDocument = frameDocument();
1160
+ const view = emptyDocument?.defaultView;
1161
+ if (emptyDocument?.body && view) {
1162
+ const element = namespaceUri ? emptyDocument.createElementNS(namespaceUri, tagName) : emptyDocument.createElement(tagName);
1163
+ emptyDocument.body.appendChild(element);
1164
+ const style = view.getComputedStyle(element);
1165
+ for (let index = 0; index < style.length; index += 1) {
1166
+ const name = style.item(index);
1167
+ defaults.set(name, style.getPropertyValue(name));
1168
+ }
1169
+ element.remove();
1170
+ defaultStylesByTag.set(cacheKey, defaults);
1171
+ }
1172
+ }
1173
+ return defaults.get(property) ?? "";
1174
+ },
1175
+ dispose: () => {
1176
+ frame?.remove();
1177
+ frame = null;
1178
+ }
1179
+ };
1180
+ };
1181
+ const TABLE_LAYOUT_DISPLAYS = /* @__PURE__ */ new Set(["table", "inline-table", "table-row-group", "table-header-group", "table-footer-group", "table-row"]);
1182
+ const ALWAYS_COPIED_PROPERTY = /^(border-.*-width|outline-width|column-rule-width)$/;
1183
+ const TABLE_LAYOUT_HEIGHT_PROPERTIES = /* @__PURE__ */ new Set(["height", "block-size"]);
1184
+ const copyComputedStyle = (style, clone, original, parentStyle, defaults) => {
992
1185
  const declarations = [];
1186
+ const isTablePart = TABLE_LAYOUT_DISPLAYS.has(style.display);
1187
+ const namespaceUri = original.namespaceURI === "http://www.w3.org/1999/xhtml" ? null : original.namespaceURI;
993
1188
  for (let index = 0; index < style.length; index += 1) {
994
1189
  const property = style.item(index);
995
- declarations.push(`${property}:${style.getPropertyValue(property)}`);
1190
+ if (property.startsWith("--") || isTablePart && TABLE_LAYOUT_HEIGHT_PROPERTIES.has(property)) continue;
1191
+ const value = style.getPropertyValue(property);
1192
+ const isDefault = defaults !== null && value === defaults.valueOf(original.localName, namespaceUri, property);
1193
+ if (isDefault && !ALWAYS_COPIED_PROPERTY.test(property) && (parentStyle === null || parentStyle.getPropertyValue(property) === value)) continue;
1194
+ declarations.push(`${property}:${value}`);
996
1195
  }
997
1196
  clone.setAttribute("style", declarations.join(";"));
998
1197
  };
1198
+ const intersectBoxes = (box, clip) => {
1199
+ const top = Math.max(box.top, clip.top);
1200
+ const left = Math.max(box.left, clip.left);
1201
+ const bottom = Math.min(box.bottom, clip.bottom);
1202
+ const right = Math.min(box.right, clip.right);
1203
+ return bottom > top && right > left ? { top, left, bottom, right } : null;
1204
+ };
1205
+ const isOutOfView = (element, visibleBox) => {
1206
+ const rect = element.getBoundingClientRect();
1207
+ if (rect.width === 0 && rect.height === 0) return false;
1208
+ return intersectBoxes({ top: rect.top, left: rect.left, bottom: rect.bottom, right: rect.right }, visibleBox) === null;
1209
+ };
1210
+ const clipBoxOf = (element, style, visibleBox) => {
1211
+ if (style === null) return visibleBox;
1212
+ if (style.position === "fixed") return windowBox();
1213
+ if (style.overflowX === "visible" && style.overflowY === "visible") return visibleBox;
1214
+ const rect = element.getBoundingClientRect();
1215
+ if (rect.width === 0 && rect.height === 0) return visibleBox;
1216
+ return intersectBoxes({ top: rect.top, left: rect.left, bottom: rect.bottom, right: rect.right }, visibleBox) ?? { top: 0, left: 0, bottom: 0, right: 0 };
1217
+ };
1218
+ const windowBox = () => ({ top: 0, left: 0, bottom: window.innerHeight, right: window.innerWidth });
999
1219
  const copyFieldValue = (original, clone) => {
1000
1220
  if (original instanceof HTMLInputElement && clone instanceof HTMLInputElement) {
1001
1221
  if (original.type === "password") {
@@ -1017,20 +1237,32 @@ const copyFieldValue = (original, clone) => {
1017
1237
  }
1018
1238
  };
1019
1239
  const LEFT_OUT_TAGS = /* @__PURE__ */ new Set(["SCRIPT", "NOSCRIPT", "TEMPLATE", "LINK", "META"]);
1020
- const prepareScreenCloneForPicture = (root, copyStyles = true) => {
1240
+ const prepareScreenCloneForPicture = (root, cloneOptions = true) => {
1241
+ const options = typeof cloneOptions === "boolean" ? { copyStyles: cloneOptions } : cloneOptions;
1242
+ const copyStyles = options.copyStyles ?? true;
1243
+ const visibleBox = options.visibleBox === void 0 ? typeof window === "undefined" ? null : windowBox() : options.visibleBox;
1244
+ const ownsDefaults = copyStyles && options.defaultStyles === void 0;
1245
+ const defaults = ownsDefaults ? createFrameDefaultStyleSource(root.ownerDocument) : options.defaultStyles ?? null;
1246
+ try {
1247
+ return copyScreenClone(root, copyStyles, visibleBox, defaults);
1248
+ } finally {
1249
+ if (ownsDefaults) defaults?.dispose();
1250
+ }
1251
+ };
1252
+ const copyScreenClone = (root, copyStyles, rootVisibleBox, defaults) => {
1021
1253
  const clone = root.cloneNode(false);
1022
1254
  let copiedElements = 0;
1023
- const copyInto = (original, target) => {
1255
+ const copyInto = (original, target, parentStyle, visibleBox) => {
1024
1256
  const view = original.ownerDocument.defaultView;
1025
- const style = copyStyles && view ? view.getComputedStyle(original) : null;
1026
- if (style && (target instanceof HTMLElement || target instanceof SVGElement)) {
1027
- copyComputedStyle(style, target);
1028
- if (target instanceof HTMLElement && isScrollContainer(original, style)) {
1257
+ const style = view && (copyStyles || visibleBox !== null) ? view.getComputedStyle(original) : null;
1258
+ if (copyStyles && style && (target instanceof HTMLElement || target instanceof SVGElement)) {
1259
+ copyComputedStyle(style, target, original, parentStyle, defaults);
1260
+ if (visibleBox === null && target instanceof HTMLElement && isScrollContainer(original, style)) {
1029
1261
  target.style.overflowY = "visible";
1030
1262
  target.style.height = "auto";
1031
1263
  target.style.maxHeight = "none";
1032
1264
  }
1033
- } else if (!copyStyles && target instanceof HTMLElement && original.scrollHeight > original.clientHeight + 1) {
1265
+ } else if (visibleBox === null && !copyStyles && target instanceof HTMLElement && original.scrollHeight > original.clientHeight + 1) {
1034
1266
  target.style.overflowY = "visible";
1035
1267
  target.style.height = "auto";
1036
1268
  target.style.maxHeight = "none";
@@ -1042,6 +1274,7 @@ const prepareScreenCloneForPicture = (root, copyStyles = true) => {
1042
1274
  return;
1043
1275
  }
1044
1276
  if (original instanceof HTMLTextAreaElement) return;
1277
+ const childVisibleBox = visibleBox === null ? null : clipBoxOf(original, style, visibleBox);
1045
1278
  for (const childNode of Array.from(original.childNodes)) {
1046
1279
  if (childNode.nodeType === Node.TEXT_NODE) {
1047
1280
  target.appendChild(childNode.cloneNode(false));
@@ -1049,14 +1282,15 @@ const prepareScreenCloneForPicture = (root, copyStyles = true) => {
1049
1282
  }
1050
1283
  if (!(childNode instanceof Element)) continue;
1051
1284
  if (LEFT_OUT_TAGS.has(childNode.tagName) || childNode.hasAttribute(AGENT_SKIP_ATTRIBUTE)) continue;
1285
+ if (childVisibleBox !== null && isOutOfView(childNode, childVisibleBox)) continue;
1052
1286
  if (copiedElements >= MAX_PICTURE_ELEMENTS) return;
1053
1287
  copiedElements += 1;
1054
1288
  const childClone = childNode.cloneNode(false);
1055
1289
  target.appendChild(childClone);
1056
- copyInto(childNode, childClone);
1290
+ copyInto(childNode, childClone, copyStyles ? style : null, childVisibleBox);
1057
1291
  }
1058
1292
  };
1059
- copyInto(root, clone);
1293
+ copyInto(root, clone, null, rootVisibleBox);
1060
1294
  return clone;
1061
1295
  };
1062
1296
  const measureCloneHeight = (clone, width, documentOfRoot) => {
@@ -1102,7 +1336,7 @@ const createDomScreenPictureCapturer = (options = {}) => ({
1102
1336
  try {
1103
1337
  const sourceWidth = Math.max(1, Math.ceil(window.innerWidth || root.clientWidth));
1104
1338
  const clone = prepareScreenCloneForPicture(root);
1105
- const sourceHeight = Math.max(1, Math.min(measureCloneHeight(clone, sourceWidth, document), sourceWidth * 8));
1339
+ const sourceHeight = Math.max(1, Math.min(measureCloneHeight(clone, sourceWidth, document), Math.ceil(window.innerHeight || sourceWidth * 8)));
1106
1340
  const serialized = new XMLSerializer().serializeToString(clone);
1107
1341
  const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${sourceWidth}" height="${sourceHeight}"><foreignObject x="0" y="0" width="100%" height="100%">${serialized}</foreignObject></svg>`;
1108
1342
  const image = await loadImage(`data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`);
@@ -1169,7 +1403,8 @@ const createAgentWindowAdapter = (options) => {
1169
1403
  await options.applyMode(mode);
1170
1404
  return true;
1171
1405
  },
1172
- shouldStepAsideBefore: (requestType) => stepAsideFor.includes(requestType)
1406
+ shouldStepAsideBefore: (requestType) => stepAsideFor.includes(requestType),
1407
+ getMode: () => options.getMode()
1173
1408
  };
1174
1409
  };
1175
1410
  const ACCOUNT_REALTIME_POLL_INTERVAL_MS = 5e3;
@@ -1323,12 +1558,15 @@ export {
1323
1558
  MACHINE_MODE_BYTE_FACTOR,
1324
1559
  MACHINE_MODE_GREY_LEVELS,
1325
1560
  MACHINE_MODE_SIZE_FACTOR,
1561
+ MAIN_MENU_HEADING,
1326
1562
  MARK_VISIBLE_MS,
1327
1563
  MAX_ANSWER_TEXT_CHARS,
1328
1564
  MAX_BUTTONS,
1565
+ MAX_CELL_CHARS,
1329
1566
  MAX_PICTURE_ELEMENTS,
1330
1567
  MAX_PICTURE_ENCODE_ATTEMPTS,
1331
1568
  MAX_SCREEN_CHARS,
1569
+ MAX_SECTION_TEXT_CHARS,
1332
1570
  MAX_TABLE_ROWS,
1333
1571
  MAX_TARGET_LIST_CHARS,
1334
1572
  MAX_VALUE_CHARS,
@@ -1355,6 +1593,7 @@ export {
1355
1593
  createDomScreenPathGuide,
1356
1594
  createDomScreenPictureCapturer,
1357
1595
  createDomScreenTargetSource,
1596
+ createFrameDefaultStyleSource,
1358
1597
  createGuideCounter,
1359
1598
  fieldReferenceNameOf,
1360
1599
  formatButtons,