dp-worker 0.1.6 → 0.1.8

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 (125) hide show
  1. package/builtin-skills/x.md +36 -28
  2. package/config.example.json +2 -0
  3. package/dist/agent/agent.js +1 -1
  4. package/dist/agent/agent.js.map +1 -1
  5. package/dist/agent/chat-engine.js +36 -13
  6. package/dist/agent/chat-engine.js.map +1 -1
  7. package/dist/agent/context.d.ts +2 -0
  8. package/dist/agent/context.js +4 -2
  9. package/dist/agent/context.js.map +1 -1
  10. package/dist/agent/task-runner.js +17 -16
  11. package/dist/agent/task-runner.js.map +1 -1
  12. package/dist/agent/tool-loop-detection.js +7 -3
  13. package/dist/agent/tool-loop-detection.js.map +1 -1
  14. package/dist/agent/tool-runner.js +2 -2
  15. package/dist/browser/actions.d.ts +26 -1
  16. package/dist/browser/actions.js +493 -242
  17. package/dist/browser/actions.js.map +1 -1
  18. package/dist/browser/chrome-process.d.ts +25 -3
  19. package/dist/browser/chrome-process.js +67 -13
  20. package/dist/browser/chrome-process.js.map +1 -1
  21. package/dist/browser/cloakbrowser-backend.d.ts +39 -0
  22. package/dist/browser/cloakbrowser-backend.js +243 -0
  23. package/dist/browser/cloakbrowser-backend.js.map +1 -0
  24. package/dist/browser/dispatch.js +32 -8
  25. package/dist/browser/dispatch.js.map +1 -1
  26. package/dist/browser/expect.d.ts +6 -1
  27. package/dist/browser/expect.js +32 -10
  28. package/dist/browser/expect.js.map +1 -1
  29. package/dist/browser/extension-relay.d.ts +9 -0
  30. package/dist/browser/extension-relay.js +579 -0
  31. package/dist/browser/extension-relay.js.map +1 -0
  32. package/dist/browser/humanize.d.ts +3 -0
  33. package/dist/browser/humanize.js +52 -7
  34. package/dist/browser/humanize.js.map +1 -1
  35. package/dist/browser/manager.d.ts +50 -0
  36. package/dist/browser/manager.js +210 -33
  37. package/dist/browser/manager.js.map +1 -1
  38. package/dist/browser/page-outline.d.ts +36 -0
  39. package/dist/browser/page-outline.js +703 -0
  40. package/dist/browser/page-outline.js.map +1 -0
  41. package/dist/browser/script-runner.js +4 -2
  42. package/dist/browser/script-runner.js.map +1 -1
  43. package/dist/browser/selectors.js +10 -3
  44. package/dist/browser/selectors.js.map +1 -1
  45. package/dist/browser/snapshot.d.ts +19 -1
  46. package/dist/browser/snapshot.js +145 -24
  47. package/dist/browser/snapshot.js.map +1 -1
  48. package/dist/chrome-extension/omni-flow/background.js +304 -21
  49. package/dist/chrome-extension/omni-flow/connect.html +81 -0
  50. package/dist/chrome-extension/omni-flow/connect.js +43 -0
  51. package/dist/chrome-extension/omni-flow/content.js +13 -21
  52. package/dist/chrome-extension/omni-flow/inject.js +77 -56
  53. package/dist/chrome-extension/omni-flow/manifest.json +6 -3
  54. package/dist/config/center-config.js +5 -0
  55. package/dist/config/center-config.js.map +1 -1
  56. package/dist/config/types.d.ts +21 -2
  57. package/dist/config/types.js +35 -1
  58. package/dist/config/types.js.map +1 -1
  59. package/dist/index.js +15 -1
  60. package/dist/index.js.map +1 -1
  61. package/dist/llm/live-tool-result-guard.d.ts +16 -0
  62. package/dist/llm/live-tool-result-guard.js +57 -0
  63. package/dist/llm/live-tool-result-guard.js.map +1 -0
  64. package/dist/llm/message-optimizer.d.ts +10 -0
  65. package/dist/llm/message-optimizer.js +12 -0
  66. package/dist/llm/message-optimizer.js.map +1 -1
  67. package/dist/llm/tools.js +37 -11
  68. package/dist/llm/tools.js.map +1 -1
  69. package/dist/render/ffmpeg-compose.d.ts +25 -0
  70. package/dist/render/ffmpeg-compose.js +141 -0
  71. package/dist/render/ffmpeg-compose.js.map +1 -0
  72. package/dist/render/ffmpeg-merge.d.ts +5 -0
  73. package/dist/render/ffmpeg-merge.js +61 -0
  74. package/dist/render/ffmpeg-merge.js.map +1 -0
  75. package/dist/render/font.d.ts +1 -0
  76. package/dist/render/font.js +61 -0
  77. package/dist/render/font.js.map +1 -0
  78. package/dist/render/image-gen.d.ts +11 -0
  79. package/dist/render/image-gen.js +42 -0
  80. package/dist/render/image-gen.js.map +1 -0
  81. package/dist/render/render-frontend.d.ts +46 -0
  82. package/dist/render/render-frontend.js +156 -0
  83. package/dist/render/render-frontend.js.map +1 -0
  84. package/dist/render/render-runner.d.ts +30 -0
  85. package/dist/render/render-runner.js +115 -0
  86. package/dist/render/render-runner.js.map +1 -0
  87. package/dist/render/tts.d.ts +15 -0
  88. package/dist/render/tts.js +35 -0
  89. package/dist/render/tts.js.map +1 -0
  90. package/node_modules/dp-shared/dist/db-rows.d.ts +4 -0
  91. package/node_modules/dp-shared/dist/db-rows.d.ts.map +1 -1
  92. package/node_modules/dp-shared/dist/db-rows.js +6 -0
  93. package/node_modules/dp-shared/dist/db-rows.js.map +1 -1
  94. package/node_modules/dp-shared/dist/fingerprint.d.ts +14 -0
  95. package/node_modules/dp-shared/dist/fingerprint.d.ts.map +1 -0
  96. package/node_modules/dp-shared/dist/fingerprint.js +22 -0
  97. package/node_modules/dp-shared/dist/fingerprint.js.map +1 -0
  98. package/node_modules/dp-shared/dist/index.d.ts +1 -0
  99. package/node_modules/dp-shared/dist/index.d.ts.map +1 -1
  100. package/node_modules/dp-shared/dist/index.js +1 -0
  101. package/node_modules/dp-shared/dist/index.js.map +1 -1
  102. package/node_modules/dp-shared/dist/rest-contracts.d.ts +4 -0
  103. package/node_modules/dp-shared/dist/rest-contracts.d.ts.map +1 -1
  104. package/node_modules/dp-shared/dist/rest-contracts.js +8 -0
  105. package/node_modules/dp-shared/dist/rest-contracts.js.map +1 -1
  106. package/node_modules/dp-shared/dist/worker-detail-contracts.d.ts +8 -0
  107. package/node_modules/dp-shared/dist/worker-detail-contracts.d.ts.map +1 -1
  108. package/node_modules/dp-shared/dist/worker-detail-contracts.js +2 -0
  109. package/node_modules/dp-shared/dist/worker-detail-contracts.js.map +1 -1
  110. package/node_modules/dp-shared/src/admin-broadcast.ts +38 -0
  111. package/node_modules/dp-shared/src/audit-contracts.ts +33 -0
  112. package/node_modules/dp-shared/src/configs-contracts.ts +211 -0
  113. package/node_modules/dp-shared/src/db-rows.ts +286 -0
  114. package/node_modules/dp-shared/src/deploy-contracts.ts +38 -0
  115. package/node_modules/dp-shared/src/domain.ts +124 -0
  116. package/node_modules/dp-shared/src/fingerprint.ts +21 -0
  117. package/node_modules/dp-shared/src/identities.ts +38 -0
  118. package/node_modules/dp-shared/src/index.ts +26 -0
  119. package/node_modules/dp-shared/src/omni-flow-session.ts +79 -0
  120. package/node_modules/dp-shared/src/rest-contracts.ts +417 -0
  121. package/node_modules/dp-shared/src/task-history-contracts.ts +118 -0
  122. package/node_modules/dp-shared/src/worker-detail-contracts.ts +232 -0
  123. package/node_modules/dp-shared/src/ws-protocol.ts +435 -0
  124. package/package.json +8 -2
  125. package/scripts/install-cloakbrowser.mjs +46 -0
@@ -0,0 +1,703 @@
1
+ import { safeSliceHead } from "../utils/safe-slice.js";
2
+ import { normalizeSelector } from "./selectors.js";
3
+ export const DEFAULT_OUTLINE_MAX_CHARS = 6_000;
4
+ export const OUTLINE_PREVIEW_CHARS = 120;
5
+ export const OUTLINE_MAX_LANDMARKS = 12;
6
+ export const OUTLINE_MAX_HEADINGS = 24;
7
+ export const OUTLINE_MIN_GROUP_SIZE = 4;
8
+ export const OUTLINE_MAX_GROUPS = 8;
9
+ export const OUTLINE_GROUP_SAMPLE_SIZE = 3;
10
+ export const OUTLINE_MAX_CONTROLS = 24;
11
+ export const OUTLINE_SELECTOR_PATH_MAX_DEPTH = 5;
12
+ const ERROR_VALUE_MAX_CHARS = 80;
13
+ const ERROR_VALUE_SAMPLE_CHARS = ERROR_VALUE_MAX_CHARS * 4;
14
+ export async function collectPageOutline(page, opts = {}) {
15
+ let scopeSelector;
16
+ if (opts.selector) {
17
+ const norm = normalizeSelector(opts.selector);
18
+ if (!norm.ok) {
19
+ return capOutlineText(`Error: selector ${summarizeErrorValue(norm.error ?? "invalid selector")}. Original: ${quoteErrorValue(norm.original)}.`, opts.maxChars);
20
+ }
21
+ scopeSelector = norm.selector;
22
+ }
23
+ const raw = await page.evaluate(collectOutlineInPage, {
24
+ scopeSelector,
25
+ previewChars: OUTLINE_PREVIEW_CHARS,
26
+ maxLandmarks: OUTLINE_MAX_LANDMARKS,
27
+ maxHeadings: OUTLINE_MAX_HEADINGS,
28
+ minGroupSize: OUTLINE_MIN_GROUP_SIZE,
29
+ maxGroups: OUTLINE_MAX_GROUPS,
30
+ groupSampleSize: OUTLINE_GROUP_SAMPLE_SIZE,
31
+ maxControls: OUTLINE_MAX_CONTROLS,
32
+ selectorPathMaxDepth: OUTLINE_SELECTOR_PATH_MAX_DEPTH,
33
+ });
34
+ if (raw == null) {
35
+ return capOutlineText(`Error: selector ${quoteErrorValue(scopeSelector ?? "")} not found on page.`, opts.maxChars);
36
+ }
37
+ return formatPageOutline(raw, {
38
+ selector: scopeSelector,
39
+ maxChars: opts.maxChars,
40
+ });
41
+ }
42
+ function collectOutlineInPage(args) {
43
+ const landmarkSelector = [
44
+ "main",
45
+ "nav",
46
+ "header",
47
+ "footer",
48
+ "aside",
49
+ '[role="main"]',
50
+ '[role="navigation"]',
51
+ '[role="banner"]',
52
+ '[role="contentinfo"]',
53
+ '[role="complementary"]',
54
+ '[role="dialog"]',
55
+ ].join(", ");
56
+ const headingSelector = 'h1, h2, h3, [role="heading"]';
57
+ const controlSelector = [
58
+ "a[href]",
59
+ "button",
60
+ "input",
61
+ "textarea",
62
+ "select",
63
+ '[role="button"]',
64
+ '[role="link"]',
65
+ '[role="textbox"]',
66
+ '[role="searchbox"]',
67
+ '[role="tab"]',
68
+ '[role="menuitem"]',
69
+ ].join(", ");
70
+ let scope;
71
+ if (args.scopeSelector) {
72
+ try {
73
+ scope = document.querySelector(args.scopeSelector);
74
+ }
75
+ catch {
76
+ scope = null;
77
+ }
78
+ }
79
+ else {
80
+ scope = document.body;
81
+ }
82
+ if (!scope)
83
+ return null;
84
+ return {
85
+ landmarks: collectItems(scope, landmarkSelector, args.maxLandmarks, (el) => {
86
+ const selector = getBestSelector(el, args);
87
+ return {
88
+ kind: landmarkKind(el),
89
+ label: getElementName(el) || landmarkKind(el),
90
+ selector: selector.selector,
91
+ preview: getPreview(el),
92
+ stable: selector.stable,
93
+ };
94
+ }),
95
+ headings: collectItems(scope, headingSelector, args.maxHeadings, (el) => {
96
+ const label = getElementName(el);
97
+ if (!label)
98
+ return undefined;
99
+ const selector = getBestSelector(el, args);
100
+ return {
101
+ kind: headingKind(el),
102
+ label,
103
+ selector: selector.selector,
104
+ preview: getPreview(el),
105
+ stable: selector.stable,
106
+ };
107
+ }),
108
+ groups: collectGroups(scope),
109
+ controls: collectItems(scope, controlSelector, args.maxControls, (el) => {
110
+ const selector = getBestSelector(el, args);
111
+ const label = getElementName(el) || controlKind(el);
112
+ return {
113
+ kind: controlKind(el),
114
+ label,
115
+ selector: selector.selector,
116
+ preview: getControlPreview(el),
117
+ stable: selector.stable,
118
+ };
119
+ }),
120
+ };
121
+ function collectItems(root, selector, maxItems, buildItem) {
122
+ const items = [];
123
+ const seen = new Set();
124
+ for (const el of matchingElements(root, selector)) {
125
+ if (seen.has(el) || !isVisibleElement(el))
126
+ continue;
127
+ seen.add(el);
128
+ const item = buildItem(el);
129
+ if (!item)
130
+ continue;
131
+ items.push(item);
132
+ if (items.length >= maxItems)
133
+ break;
134
+ }
135
+ return items;
136
+ }
137
+ function matchingElements(root, selector) {
138
+ const matches = [];
139
+ try {
140
+ if (root.matches(selector))
141
+ matches.push(root);
142
+ matches.push(...Array.from(root.querySelectorAll(selector)));
143
+ }
144
+ catch {
145
+ return [];
146
+ }
147
+ return matches;
148
+ }
149
+ function collectGroups(root) {
150
+ const bySelector = new Map();
151
+ const containers = [root, ...Array.from(root.querySelectorAll("*"))];
152
+ for (const container of containers) {
153
+ if (!isContainerVisible(container))
154
+ continue;
155
+ const buckets = new Map();
156
+ for (const child of Array.from(container.children)) {
157
+ if (!isVisibleElement(child))
158
+ continue;
159
+ const signature = repeatedSignature(child);
160
+ if (!signature)
161
+ continue;
162
+ const bucket = buckets.get(signature.key);
163
+ if (bucket) {
164
+ bucket.children.push(child);
165
+ }
166
+ else {
167
+ buckets.set(signature.key, { signature, children: [child] });
168
+ }
169
+ }
170
+ for (const { signature, children } of buckets.values()) {
171
+ if (children.length < args.minGroupSize)
172
+ continue;
173
+ const existing = bySelector.get(signature.selector);
174
+ if (existing && existing.count >= children.length)
175
+ continue;
176
+ const sampleElement = children[0];
177
+ const sampleSelector = sampleElement
178
+ ? getPermalinkSelector(sampleElement, signature.selector)
179
+ : undefined;
180
+ const samples = children
181
+ .slice(0, args.groupSampleSize)
182
+ .map((child) => getPreview(child))
183
+ .filter((sample) => sample.length > 0);
184
+ bySelector.set(signature.selector, {
185
+ kind: signature.kind,
186
+ count: children.length,
187
+ selector: signature.selector,
188
+ sampleSelector,
189
+ samples,
190
+ priority: signature.priority,
191
+ });
192
+ }
193
+ }
194
+ return Array.from(bySelector.values())
195
+ .sort((a, b) => a.priority - b.priority || b.count - a.count)
196
+ .slice(0, args.maxGroups)
197
+ .map(({ priority: _priority, ...group }) => group);
198
+ }
199
+ function repeatedSignature(el) {
200
+ const testId = attr(el, "data-testid");
201
+ if (testId) {
202
+ return {
203
+ key: `testid:${testId}`,
204
+ kind: `${humanize(testId)} items`,
205
+ selector: attrSelector("data-testid", testId),
206
+ priority: 0,
207
+ };
208
+ }
209
+ const tag = tagName(el);
210
+ if (tag.includes("-")) {
211
+ return {
212
+ key: `custom:${tag}`,
213
+ kind: `${humanize(tag)} items`,
214
+ selector: tag,
215
+ priority: 1,
216
+ };
217
+ }
218
+ const role = roleName(el);
219
+ if (tag === "article" && role === "article") {
220
+ return {
221
+ key: "article:article",
222
+ kind: "article cards",
223
+ selector: 'article[role="article"]',
224
+ priority: 2,
225
+ };
226
+ }
227
+ return {
228
+ key: `tag:${tag}:role:${role}`,
229
+ kind: role ? `${role} ${tag} items` : `${tag} items`,
230
+ selector: role ? `${tag}${attrSelector("role", role)}` : tag,
231
+ priority: role ? 3 : 4,
232
+ };
233
+ }
234
+ function isContainerVisible(el) {
235
+ return el === document.body || isVisibleElement(el);
236
+ }
237
+ function isVisibleElement(el) {
238
+ if (!(el instanceof HTMLElement) && !(el instanceof SVGElement)) {
239
+ return false;
240
+ }
241
+ for (let current = el; current; current = current.parentElement) {
242
+ if (current instanceof HTMLElement && current.hidden)
243
+ return false;
244
+ if (attr(current, "aria-hidden") === "true")
245
+ return false;
246
+ const style = getComputedStyle(current);
247
+ if (style.display === "none" ||
248
+ style.visibility === "hidden" ||
249
+ style.visibility === "collapse" ||
250
+ style.opacity === "0") {
251
+ return false;
252
+ }
253
+ }
254
+ const rects = Array.from(el.getClientRects());
255
+ return rects.some((rect) => rect.width > 0 && rect.height > 0);
256
+ }
257
+ function getBestSelector(el, collectArgs, permalinkBase) {
258
+ const testId = attr(el, "data-testid");
259
+ if (testId) {
260
+ const selector = attrSelector("data-testid", testId);
261
+ if (isUniqueSelector(selector))
262
+ return { selector, stable: true };
263
+ }
264
+ const id = attr(el, "id");
265
+ if (id) {
266
+ const selector = `#${cssEscape(id)}`;
267
+ if (isUniqueSelector(selector))
268
+ return { selector, stable: true };
269
+ }
270
+ const name = attr(el, "name");
271
+ if (name) {
272
+ const selector = attrSelector("name", name);
273
+ if (isUniqueSelector(selector))
274
+ return { selector, stable: true };
275
+ }
276
+ const ariaLabel = attr(el, "aria-label");
277
+ if (ariaLabel) {
278
+ const selector = attrSelector("aria-label", ariaLabel);
279
+ if (isUniqueSelector(selector))
280
+ return { selector, stable: true };
281
+ }
282
+ const permalinkSelector = getPermalinkSelector(el, permalinkBase);
283
+ if (permalinkSelector) {
284
+ return { selector: permalinkSelector, stable: true };
285
+ }
286
+ return { selector: cssPath(el, collectArgs.selectorPathMaxDepth), stable: false };
287
+ }
288
+ function getPermalinkSelector(el, preferredBase) {
289
+ const bases = [preferredBase, simpleElementSelector(el)].filter((base) => Boolean(base));
290
+ const suffixes = Array.from(el.querySelectorAll("a[href]"))
291
+ .map((anchor) => permalinkSuffix(anchor))
292
+ .filter((suffix) => Boolean(suffix))
293
+ .sort((a, b) => permalinkPriority(a) - permalinkPriority(b));
294
+ for (const suffix of suffixes) {
295
+ for (const base of bases) {
296
+ const selector = `${base}:has(${attrSelector("href", suffix, "$=", "a")})`;
297
+ if (selectorCount(selector) === 1)
298
+ return selector;
299
+ }
300
+ }
301
+ return undefined;
302
+ }
303
+ function permalinkPriority(suffix) {
304
+ const lower = suffix.toLowerCase();
305
+ if (lower.includes("/status/"))
306
+ return 0;
307
+ if (lower.includes("/watch") || lower.includes("watch?"))
308
+ return 1;
309
+ return 2;
310
+ }
311
+ function simpleElementSelector(el) {
312
+ const testId = attr(el, "data-testid");
313
+ if (testId)
314
+ return attrSelector("data-testid", testId);
315
+ const tag = tagName(el);
316
+ const role = roleName(el);
317
+ if (tag === "article" && role === "article")
318
+ return 'article[role="article"]';
319
+ if (role)
320
+ return `${tag}${attrSelector("role", role)}`;
321
+ return tag;
322
+ }
323
+ function permalinkSuffix(anchor) {
324
+ const raw = attr(anchor, "href");
325
+ if (!raw || raw === "#" || raw.toLowerCase().startsWith("javascript:")) {
326
+ return undefined;
327
+ }
328
+ try {
329
+ const url = new URL(anchor.href, document.baseURI);
330
+ const suffix = `${url.pathname}${url.search}${url.hash}`;
331
+ if (suffix && suffix !== "/" && suffix !== "#") {
332
+ return clipText(suffix, 180);
333
+ }
334
+ }
335
+ catch {
336
+ if (raw.startsWith("/") && raw.length > 1)
337
+ return clipText(raw, 180);
338
+ }
339
+ return undefined;
340
+ }
341
+ function cssPath(el, maxDepth) {
342
+ const segments = [];
343
+ let current = el;
344
+ while (current &&
345
+ current !== document.documentElement &&
346
+ segments.length < maxDepth) {
347
+ segments.unshift(cssPathSegment(current));
348
+ current = current.parentElement;
349
+ }
350
+ return segments.join(" > ") || tagName(el);
351
+ }
352
+ function cssPathSegment(el) {
353
+ const tag = tagName(el);
354
+ const parent = el.parentElement;
355
+ if (!parent)
356
+ return tag;
357
+ const sameTagSiblings = Array.from(parent.children).filter((child) => tagName(child) === tag);
358
+ const index = sameTagSiblings.indexOf(el) + 1;
359
+ return `${tag}:nth-of-type(${Math.max(index, 1)})`;
360
+ }
361
+ function selectorCount(selector) {
362
+ try {
363
+ return document.querySelectorAll(selector).length;
364
+ }
365
+ catch {
366
+ return 0;
367
+ }
368
+ }
369
+ function isUniqueSelector(selector) {
370
+ return selectorCount(selector) === 1;
371
+ }
372
+ function landmarkKind(el) {
373
+ const role = roleName(el);
374
+ if (role === "main")
375
+ return "main";
376
+ if (role === "navigation")
377
+ return "navigation";
378
+ if (role === "banner")
379
+ return "banner";
380
+ if (role === "contentinfo")
381
+ return "contentinfo";
382
+ if (role === "complementary")
383
+ return "complementary";
384
+ if (role === "dialog")
385
+ return "dialog";
386
+ const tag = tagName(el);
387
+ if (tag === "nav")
388
+ return "navigation";
389
+ if (tag === "header")
390
+ return "banner";
391
+ if (tag === "footer")
392
+ return "contentinfo";
393
+ if (tag === "aside")
394
+ return "complementary";
395
+ return tag;
396
+ }
397
+ function headingKind(el) {
398
+ const tag = tagName(el);
399
+ if (tag === "h1" || tag === "h2" || tag === "h3")
400
+ return tag;
401
+ const level = attr(el, "aria-level");
402
+ if (level && /^[1-6]$/.test(level))
403
+ return `h${level}`;
404
+ return "heading";
405
+ }
406
+ function controlKind(el) {
407
+ const role = roleName(el);
408
+ if (role === "button" ||
409
+ role === "link" ||
410
+ role === "textbox" ||
411
+ role === "searchbox" ||
412
+ role === "tab" ||
413
+ role === "menuitem") {
414
+ return role;
415
+ }
416
+ const tag = tagName(el);
417
+ if (tag === "a")
418
+ return "link";
419
+ if (tag === "button")
420
+ return "button";
421
+ if (tag === "textarea")
422
+ return "textbox";
423
+ if (tag === "select")
424
+ return "combobox";
425
+ if (tag === "input") {
426
+ const type = (attr(el, "type") || "text").toLowerCase();
427
+ if (type === "search")
428
+ return "searchbox";
429
+ if (type === "button" || type === "submit" || type === "reset") {
430
+ return "button";
431
+ }
432
+ if (type === "checkbox" || type === "radio")
433
+ return type;
434
+ return "textbox";
435
+ }
436
+ return tag;
437
+ }
438
+ function getElementName(el) {
439
+ const labelledBy = attr(el, "aria-labelledby");
440
+ if (labelledBy) {
441
+ const label = labelledBy
442
+ .split(/\s+/)
443
+ .map((id) => document.getElementById(id)?.textContent ?? "")
444
+ .join(" ");
445
+ const compacted = compactText(label);
446
+ if (compacted)
447
+ return compacted;
448
+ }
449
+ for (const nameAttr of ["aria-label", "title", "alt", "placeholder"]) {
450
+ const value = attr(el, nameAttr);
451
+ if (value)
452
+ return compactText(value);
453
+ }
454
+ const labelText = associatedLabelText(el);
455
+ if (labelText)
456
+ return labelText;
457
+ const name = attr(el, "name");
458
+ if (name)
459
+ return compactText(name);
460
+ const text = compactText(el instanceof HTMLElement ? el.innerText : el.textContent ?? "");
461
+ if (text)
462
+ return text;
463
+ if (el instanceof HTMLInputElement && el.value) {
464
+ return compactText(el.value);
465
+ }
466
+ if (tagName(el) === "a") {
467
+ const suffix = permalinkSuffix(el);
468
+ if (suffix)
469
+ return suffix;
470
+ }
471
+ return "";
472
+ }
473
+ function associatedLabelText(el) {
474
+ const labeled = el;
475
+ if (!("labels" in labeled) || !labeled.labels)
476
+ return "";
477
+ return compactText(Array.from(labeled.labels)
478
+ .map((label) => label.textContent ?? "")
479
+ .join(" "));
480
+ }
481
+ function getPreview(el) {
482
+ return compactText(el instanceof HTMLElement ? el.innerText : el.textContent ?? "");
483
+ }
484
+ function getControlPreview(el) {
485
+ const value = el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement
486
+ ? el.value
487
+ : "";
488
+ const preview = compactText(value || getPreview(el));
489
+ const label = getElementName(el);
490
+ return preview && preview !== label ? preview : undefined;
491
+ }
492
+ function compactText(value) {
493
+ return clipText(value.replace(/\s+/g, " ").trim(), args.previewChars);
494
+ }
495
+ function clipText(value, maxChars) {
496
+ return Array.from(value).slice(0, maxChars).join("");
497
+ }
498
+ function humanize(value) {
499
+ return (value
500
+ .replace(/([a-z0-9])([A-Z])/g, "$1 $2")
501
+ .replace(/[_-]+/g, " ")
502
+ .replace(/\s+/g, " ")
503
+ .trim()
504
+ .toLowerCase() || "repeated");
505
+ }
506
+ function roleName(el) {
507
+ return (attr(el, "role") || "").toLowerCase().trim().split(/\s+/)[0] ?? "";
508
+ }
509
+ function tagName(el) {
510
+ return el.tagName.toLowerCase();
511
+ }
512
+ function attr(el, name) {
513
+ return el.getAttribute(name)?.trim() ?? "";
514
+ }
515
+ function cssEscape(value) {
516
+ if (typeof CSS !== "undefined" && CSS.escape) {
517
+ return CSS.escape(value);
518
+ }
519
+ return value.replace(/[^a-zA-Z0-9_-]/g, (char) => `\\${char}`);
520
+ }
521
+ function attrSelector(name, value, operator = "=", element = "") {
522
+ return `${element}[${name}${operator}${quoteAttrValue(value)}]`;
523
+ }
524
+ function quoteAttrValue(value) {
525
+ if (!value.includes('"')) {
526
+ return `"${escapeAttrValue(value, '"')}"`;
527
+ }
528
+ if (!value.includes("'")) {
529
+ return `'${escapeAttrValue(value, "'")}'`;
530
+ }
531
+ return `"${escapeAttrValue(value, '"')}"`;
532
+ }
533
+ function escapeAttrValue(value, quote) {
534
+ return value
535
+ .replace(/\\/g, "\\\\")
536
+ .replace(new RegExp(quote, "g"), `\\${quote}`)
537
+ .replace(/\n/g, "\\A ");
538
+ }
539
+ }
540
+ export function formatPageOutline(raw, opts = {}) {
541
+ const maxChars = normalizeMaxChars(opts.maxChars);
542
+ if (maxChars <= 0)
543
+ return "";
544
+ const detailSelector = pickDetailSelector(raw, opts);
545
+ const lines = [
546
+ `Page outline (mode=outline, maxChars=${maxChars}${formatScope(opts)})`,
547
+ "",
548
+ ...formatItemSection("Landmarks", raw.landmarks, OUTLINE_MAX_LANDMARKS),
549
+ "",
550
+ ...formatItemSection("Headings", raw.headings, OUTLINE_MAX_HEADINGS),
551
+ "",
552
+ ...formatGroupSection(raw.groups),
553
+ "",
554
+ ...formatItemSection("Controls", raw.controls, OUTLINE_MAX_CONTROLS),
555
+ "",
556
+ "Next:",
557
+ `- For content, call snapshot({selector:${quote(detailSelector)}, mode:"full", maxChars:10000}) or extractVisibleText({selector:${quote(detailSelector)}}).`,
558
+ '- For actions, use selector from this outline or call snapshot({selector:"...", mode:"interactive"}).',
559
+ ];
560
+ return capWithTruncationHint(lines.join("\n"), maxChars, detailSelector);
561
+ }
562
+ function normalizeMaxChars(maxChars) {
563
+ if (maxChars === undefined || !Number.isFinite(maxChars)) {
564
+ return DEFAULT_OUTLINE_MAX_CHARS;
565
+ }
566
+ return Math.max(0, Math.min(Math.floor(maxChars), DEFAULT_OUTLINE_MAX_CHARS));
567
+ }
568
+ function capOutlineText(text, maxChars) {
569
+ const limit = normalizeMaxChars(maxChars);
570
+ if (limit <= 0)
571
+ return "";
572
+ if (text.length <= limit)
573
+ return text;
574
+ const suffix = "\n[...TRUNCATED]";
575
+ if (suffix.length >= limit) {
576
+ return safeSliceHead(suffix.trimStart(), limit);
577
+ }
578
+ return safeSliceHead(text, limit - suffix.length) + suffix;
579
+ }
580
+ function quoteErrorValue(value) {
581
+ return quote(summarizeErrorValue(value));
582
+ }
583
+ function summarizeErrorValue(value) {
584
+ const sample = safeSliceHead(value, ERROR_VALUE_SAMPLE_CHARS);
585
+ const compactedSample = sample.replace(/\s+/g, " ").trim();
586
+ if (value.length <= sample.length && compactedSample.length <= ERROR_VALUE_MAX_CHARS) {
587
+ return compactedSample;
588
+ }
589
+ return `${safeSliceHead(compactedSample, ERROR_VALUE_MAX_CHARS)}... [TRUNCATED ${value.length} chars]`;
590
+ }
591
+ function formatScope(opts) {
592
+ return opts.selector ? `, selector=${quote(opts.selector)}` : "";
593
+ }
594
+ function formatItemSection(title, items, maxItems) {
595
+ const boundedItems = items.slice(0, maxItems);
596
+ const lines = [`${title}:`];
597
+ if (boundedItems.length === 0) {
598
+ lines.push("- none");
599
+ return lines;
600
+ }
601
+ lines.push(...boundedItems.map(formatOutlineItem));
602
+ if (items.length > maxItems) {
603
+ lines.push(`- ... ${items.length - maxItems} more omitted`);
604
+ }
605
+ return lines;
606
+ }
607
+ function formatOutlineItem(item) {
608
+ const parts = [
609
+ `- ${item.kind} ${quote(compactText(item.label, OUTLINE_PREVIEW_CHARS))}`,
610
+ `selector=${quote(item.selector)}`,
611
+ ];
612
+ if (item.preview) {
613
+ parts.push(`preview=${quote(compactText(item.preview, OUTLINE_PREVIEW_CHARS))}`);
614
+ }
615
+ if (item.stable === true) {
616
+ parts.push("stable");
617
+ }
618
+ return parts.join(" ");
619
+ }
620
+ function formatGroupSection(groups) {
621
+ const boundedGroups = groups.slice(0, OUTLINE_MAX_GROUPS);
622
+ const lines = ["Repeated groups:"];
623
+ if (boundedGroups.length === 0) {
624
+ lines.push("- none");
625
+ return lines;
626
+ }
627
+ for (const group of boundedGroups) {
628
+ const parts = [
629
+ `- ${group.count} x ${group.kind}`,
630
+ `selector=${quote(group.selector)}`,
631
+ ];
632
+ if (group.sampleSelector) {
633
+ parts.push(`sampleSelector=${quote(group.sampleSelector)}`);
634
+ }
635
+ lines.push(parts.join(" "));
636
+ const samples = group.samples
637
+ .slice(0, OUTLINE_GROUP_SAMPLE_SIZE)
638
+ .map((sample) => quote(compactText(sample, OUTLINE_PREVIEW_CHARS)));
639
+ if (samples.length > 0) {
640
+ lines.push(` samples: ${samples.join(" | ")}`);
641
+ }
642
+ }
643
+ if (groups.length > OUTLINE_MAX_GROUPS) {
644
+ lines.push(`- ... ${groups.length - OUTLINE_MAX_GROUPS} more omitted`);
645
+ }
646
+ return lines;
647
+ }
648
+ function compactText(value, maxChars) {
649
+ const compacted = value.replace(/\s+/g, " ").trim();
650
+ return safeSliceHead(compacted, maxChars);
651
+ }
652
+ function quote(value) {
653
+ return JSON.stringify(value);
654
+ }
655
+ function pickDetailSelector(raw, opts) {
656
+ return (opts.selector ||
657
+ raw.landmarks[0]?.selector ||
658
+ raw.headings[0]?.selector ||
659
+ raw.groups[0]?.sampleSelector ||
660
+ raw.groups[0]?.selector ||
661
+ raw.controls[0]?.selector ||
662
+ "...");
663
+ }
664
+ function capWithTruncationHint(text, maxChars, detailSelector) {
665
+ if (text.length <= maxChars)
666
+ return text;
667
+ const hint = chooseTruncationHint(text, maxChars, detailSelector);
668
+ if (!hint) {
669
+ return safeSliceHead(text, maxChars);
670
+ }
671
+ const head = safeSliceHead(text, maxChars - hint.length);
672
+ return `${head}${hint}`;
673
+ }
674
+ function chooseTruncationHint(text, maxChars, detailSelector) {
675
+ const bodyReserve = Math.min(text.length, "Page outline".length);
676
+ const preferredHint = buildTruncationHint(detailSelector);
677
+ if (preferredHint.length <= maxChars - bodyReserve) {
678
+ return preferredHint;
679
+ }
680
+ const fallbackHint = buildTruncationHint("...");
681
+ if (fallbackHint.length <= maxChars - bodyReserve) {
682
+ return fallbackHint;
683
+ }
684
+ const semanticHint = buildSemanticTruncationHint();
685
+ if (semanticHint.length <= maxChars - bodyReserve) {
686
+ return semanticHint;
687
+ }
688
+ if (semanticHint.length <= maxChars) {
689
+ return semanticHint;
690
+ }
691
+ const minimalHint = "\n[TRUNCATED]";
692
+ if (minimalHint.length <= maxChars - bodyReserve) {
693
+ return minimalHint;
694
+ }
695
+ return undefined;
696
+ }
697
+ function buildTruncationHint(selector) {
698
+ return `\n[TRUNCATED snapshot({selector:${quote(selector)}, mode:"full", maxChars:10000}) or extractVisibleText({selector:${quote(selector)}})]`;
699
+ }
700
+ function buildSemanticTruncationHint() {
701
+ return '\n[TRUNCATED use snapshot full 10000 or extractVisibleText selector "..."]';
702
+ }
703
+ //# sourceMappingURL=page-outline.js.map