@mizchi/vlmkit-core 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/README.md +1 -1
  2. package/dist/a11y-semantic.d.mts +34 -0
  3. package/dist/a11y-semantic.mjs +261 -0
  4. package/dist/arg-reader.d.mts +52 -0
  5. package/dist/arg-reader.mjs +129 -0
  6. package/dist/auth-state.d.mts +30 -0
  7. package/dist/auth-state.mjs +82 -0
  8. package/dist/authored-style-capture.d.mts +42 -0
  9. package/dist/authored-style-capture.mjs +117 -0
  10. package/dist/authored-style-diff.d.mts +15 -0
  11. package/dist/authored-style-diff.mjs +16 -0
  12. package/dist/cli-args.d.mts +35 -0
  13. package/dist/cli-args.mjs +63 -0
  14. package/dist/cli-error.d.mts +12 -0
  15. package/dist/cli-error.mjs +71 -0
  16. package/dist/computed-style-capture.d.mts +41 -0
  17. package/dist/computed-style-capture.mjs +589 -0
  18. package/dist/computed-style-diff.d.mts +83 -0
  19. package/dist/computed-style-diff.mjs +123 -0
  20. package/dist/diff-regions.d.mts +8 -0
  21. package/dist/diff-regions.mjs +52 -0
  22. package/dist/dom-equivalence.d.mts +51 -0
  23. package/dist/dom-equivalence.mjs +91 -0
  24. package/dist/dom-position-styles.d.mts +166 -0
  25. package/dist/dom-position-styles.mjs +296 -0
  26. package/dist/element-compare.d.mts +71 -0
  27. package/dist/element-compare.mjs +269 -0
  28. package/dist/gate-config.d.mts +116 -0
  29. package/dist/gate-config.mjs +169 -0
  30. package/dist/gate-exit.d.mts +45 -0
  31. package/dist/gate-exit.mjs +25 -0
  32. package/dist/heatmap-regions.d.mts +57 -0
  33. package/dist/heatmap-regions.mjs +158 -0
  34. package/dist/heatmap.d.mts +86 -0
  35. package/dist/heatmap.mjs +510 -0
  36. package/dist/image-resize.d.mts +34 -0
  37. package/dist/image-resize.mjs +92 -0
  38. package/dist/index.d.mts +15 -0
  39. package/dist/index.mjs +14 -0
  40. package/dist/legacy-names.d.mts +48 -0
  41. package/dist/legacy-names.mjs +134 -0
  42. package/dist/mask.d.mts +18 -0
  43. package/dist/mask.mjs +27 -0
  44. package/dist/navigation-redirect.d.mts +28 -0
  45. package/dist/navigation-redirect.mjs +47 -0
  46. package/dist/page-open.d.mts +102 -0
  47. package/dist/page-open.mjs +133 -0
  48. package/dist/png-utils.d.mts +30 -0
  49. package/dist/png-utils.mjs +80 -0
  50. package/dist/region-shift.d.mts +20 -0
  51. package/dist/region-shift.mjs +128 -0
  52. package/dist/run-ledger.d.mts +14 -0
  53. package/dist/run-ledger.mjs +31 -0
  54. package/dist/terminal-colors.d.mts +12 -0
  55. package/dist/terminal-colors.mjs +14 -0
  56. package/dist/text-rows.d.mts +121 -0
  57. package/dist/text-rows.mjs +246 -0
  58. package/{src/types.ts → dist/types.d.mts} +88 -206
  59. package/dist/types.mjs +1 -0
  60. package/package.json +17 -5
  61. package/src/a11y-semantic.ts +0 -384
  62. package/src/authored-style-capture.ts +0 -185
  63. package/src/authored-style-diff.ts +0 -32
  64. package/src/cli-args.ts +0 -29
  65. package/src/cli-error.ts +0 -85
  66. package/src/computed-style-capture.ts +0 -717
  67. package/src/computed-style-diff.ts +0 -192
  68. package/src/diff-regions.ts +0 -70
  69. package/src/dom-equivalence.ts +0 -158
  70. package/src/dom-position-styles.ts +0 -457
  71. package/src/element-compare.ts +0 -354
  72. package/src/heatmap-regions.ts +0 -245
  73. package/src/heatmap.ts +0 -670
  74. package/src/image-resize.ts +0 -112
  75. package/src/index.ts +0 -105
  76. package/src/mask.ts +0 -41
  77. package/src/png-utils.ts +0 -86
  78. package/src/region-shift.ts +0 -173
  79. package/src/run-ledger.ts +0 -39
  80. package/src/terminal-colors.ts +0 -13
  81. package/src/text-rows.ts +0 -412
@@ -1,457 +0,0 @@
1
- /**
2
- * DOM-position-based selector alignment for migration diffs.
3
- *
4
- * Addresses the killer finding from `docs/reports/2026-05-12-subagent-eval.md`:
5
- * the migration-VRT scenario is class renames (`.card` → `.luna-panel`),
6
- * but `diffComputedStyles` matches selectors by literal string and so
7
- * produces zero verified entries for the entire rename. The subagent
8
- * eval showed this blind spot lets a fresh agent plateau at 10–24%
9
- * diff because it has to guess values from PNGs.
10
- *
11
- * This module captures `{ path, tag, classes, styles }` per
12
- * "interesting" element on each page, then matches across baseline and
13
- * variant by the position `path` (e.g. `main[0]>section[1]>article[0]>div[0]`),
14
- * which is invariant under class renames. Diff output names both
15
- * class strings + the property delta so the agent can rewrite the
16
- * variant's CSS class against the baseline's rendered values.
17
- *
18
- * Pure module. The browser-side capture script ships as a string fed
19
- * to `page.evaluate()`.
20
- */
21
-
22
- import { TRACKED_PROPERTIES } from "./computed-style-capture.ts";
23
-
24
- export interface PositionedElement {
25
- /** DOM path: `tag[childIndex]>tag[childIndex]>...` rooted at body. */
26
- path: string;
27
- /** Tag name (lower-case). */
28
- tag: string;
29
- /** Class string verbatim (or "" when absent). */
30
- classes: string;
31
- /** Tracked computed-style values. */
32
- styles: Record<string, string>;
33
- /**
34
- * Parent element's computed `display`. Used to annotate `display`-property
35
- * diffs where the value looks different but is actually flex/grid-item
36
- * coercion (CSS blockifies inline children of flex/grid containers).
37
- */
38
- parentDisplay?: string;
39
- }
40
-
41
- export interface DpEntry {
42
- path: string;
43
- tag: string;
44
- baselineClasses: string;
45
- variantClasses: string;
46
- property: string;
47
- baseline: string;
48
- variant: string;
49
- /**
50
- * For em-relative properties (`letter-spacing`, `line-height` etc.),
51
- * the value divided by the element's own font-size — exposes when a
52
- * single `em` rule produces several different px values across
53
- * elements with different font sizes.
54
- */
55
- baselineEm?: number;
56
- variantEm?: number;
57
- /**
58
- * Set on `display`-property rows when the parent on either side is a
59
- * flex/grid container. Helps the agent realize the difference may be
60
- * flex-item coercion (`inline-flex` → `flex`) rather than a real
61
- * source-CSS mismatch.
62
- */
63
- parentDisplayContext?: {
64
- baselineParent: string;
65
- variantParent: string;
66
- /** True when at least one parent is flex/grid/inline-flex/inline-grid. */
67
- isFlexOrGridItem: boolean;
68
- };
69
- }
70
-
71
- /** Properties for which em-normalization is informative. */
72
- const EM_RELATIVE = new Set([
73
- "letter-spacing",
74
- "word-spacing",
75
- "line-height",
76
- ]);
77
-
78
- /** Parent `display` values that coerce inline children to block-ish layouts. */
79
- const FLEX_OR_GRID_DISPLAYS = new Set([
80
- "flex",
81
- "inline-flex",
82
- "grid",
83
- "inline-grid",
84
- ]);
85
-
86
- function emEquivalent(value: string, fontSizePx: number): number | undefined {
87
- if (!value || value === "normal" || value === "auto" || fontSizePx <= 0) return undefined;
88
- // Already em? Trust the source.
89
- const emMatch = value.match(/^(-?\d+(?:\.\d+)?)em$/);
90
- if (emMatch) return Number(emMatch[1]);
91
- const pxMatch = value.match(/^(-?\d+(?:\.\d+)?)px$/);
92
- if (!pxMatch) return undefined;
93
- return Number(pxMatch[1]) / fontSizePx;
94
- }
95
-
96
- function parseFontSizePx(value: string | undefined): number {
97
- if (!value) return 0;
98
- const m = value.match(/^(\d+(?:\.\d+)?)px$/);
99
- return m ? Number(m[1]) : 0;
100
- }
101
-
102
- export interface DpResult {
103
- entries: DpEntry[];
104
- totalDiffs: number;
105
- /** path strings that appear only in one side (DOM structure drift). */
106
- pathsOnlyInBaseline: string[];
107
- pathsOnlyInVariant: string[];
108
- /** by-property + by-path aggregates, sorted desc. */
109
- byProperty: Array<{ property: string; count: number }>;
110
- byPath: Array<{ path: string; baselineClasses: string; variantClasses: string; count: number }>;
111
- }
112
-
113
- const EMPTY: DpResult = {
114
- entries: [],
115
- totalDiffs: 0,
116
- pathsOnlyInBaseline: [],
117
- pathsOnlyInVariant: [],
118
- byProperty: [],
119
- byPath: [],
120
- };
121
-
122
- export function diffDomPositionStyles(
123
- baseline: PositionedElement[],
124
- variant: PositionedElement[],
125
- ): DpResult {
126
- if (!baseline?.length || !variant?.length) return EMPTY;
127
-
128
- const baselineMap = new Map<string, PositionedElement>();
129
- const variantMap = new Map<string, PositionedElement>();
130
- for (const el of baseline) baselineMap.set(el.path, el);
131
- for (const el of variant) variantMap.set(el.path, el);
132
-
133
- const entries: DpEntry[] = [];
134
- const byProperty = new Map<string, number>();
135
- const byPath = new Map<string, { baselineClasses: string; variantClasses: string; count: number }>();
136
-
137
- const sharedPaths: string[] = [];
138
- for (const path of baselineMap.keys()) {
139
- if (variantMap.has(path)) sharedPaths.push(path);
140
- }
141
-
142
- for (const path of sharedPaths) {
143
- const b = baselineMap.get(path)!;
144
- const v = variantMap.get(path)!;
145
- if (b.tag !== v.tag) continue; // structural mismatch — skip
146
-
147
- const props = new Set<string>([...Object.keys(b.styles), ...Object.keys(v.styles)]);
148
- const bFontSize = parseFontSizePx(b.styles["font-size"]);
149
- const vFontSize = parseFontSizePx(v.styles["font-size"]);
150
- for (const property of props) {
151
- const bv = b.styles[property] ?? "";
152
- const vv = v.styles[property] ?? "";
153
- if (bv === vv) continue;
154
- const diffEntry: DpEntry = {
155
- path,
156
- tag: b.tag,
157
- baselineClasses: b.classes,
158
- variantClasses: v.classes,
159
- property,
160
- baseline: bv,
161
- variant: vv,
162
- };
163
- if (EM_RELATIVE.has(property)) {
164
- const baselineEm = emEquivalent(bv, bFontSize);
165
- const variantEm = emEquivalent(vv, vFontSize);
166
- if (baselineEm !== undefined) diffEntry.baselineEm = Math.round(baselineEm * 10000) / 10000;
167
- if (variantEm !== undefined) diffEntry.variantEm = Math.round(variantEm * 10000) / 10000;
168
- }
169
- if (property === "display") {
170
- const bp = b.parentDisplay ?? "";
171
- const vp = v.parentDisplay ?? "";
172
- if (bp || vp) {
173
- diffEntry.parentDisplayContext = {
174
- baselineParent: bp,
175
- variantParent: vp,
176
- isFlexOrGridItem: FLEX_OR_GRID_DISPLAYS.has(bp) || FLEX_OR_GRID_DISPLAYS.has(vp),
177
- };
178
- }
179
- }
180
- entries.push(diffEntry);
181
- byProperty.set(property, (byProperty.get(property) ?? 0) + 1);
182
- const entry = byPath.get(path) ?? { baselineClasses: b.classes, variantClasses: v.classes, count: 0 };
183
- entry.count += 1;
184
- byPath.set(path, entry);
185
- }
186
- }
187
-
188
- const pathsOnlyInBaseline = [...baselineMap.keys()].filter((p) => !variantMap.has(p));
189
- const pathsOnlyInVariant = [...variantMap.keys()].filter((p) => !baselineMap.has(p));
190
-
191
- const sortedByProperty = [...byProperty.entries()]
192
- .map(([property, count]) => ({ property, count }))
193
- .sort((a, b) => b.count - a.count || a.property.localeCompare(b.property));
194
-
195
- const sortedByPath = [...byPath.entries()]
196
- .map(([path, v]) => ({ path, baselineClasses: v.baselineClasses, variantClasses: v.variantClasses, count: v.count }))
197
- .sort((a, b) => b.count - a.count || a.path.localeCompare(b.path));
198
-
199
- return {
200
- entries,
201
- totalDiffs: entries.length,
202
- pathsOnlyInBaseline,
203
- pathsOnlyInVariant,
204
- byProperty: sortedByProperty,
205
- byPath: sortedByPath,
206
- };
207
- }
208
-
209
- /**
210
- * Browser-side capture script: walks the DOM from `<body>`, emits
211
- * `{path, tag, classes, styles}` for every element that has a class
212
- * attribute or is one of the semantic / interactive tags listed below.
213
- * `path` uses 0-based child indices among ALL children, so it's stable
214
- * across class renames as long as the tree shape is the same.
215
- */
216
- export const DOM_POSITION_STYLES_BROWSER_SCRIPT = `JSON.stringify((function(){
217
- var TRACKED = ${JSON.stringify(TRACKED_PROPERTIES)};
218
- var SEMANTIC = new Set([
219
- "main","header","nav","footer","aside","article","section",
220
- "h1","h2","h3","h4","h5","h6","p","a","button","input","select","textarea",
221
- "label","blockquote","pre","code","table","thead","tbody","tr","th","td",
222
- "ul","ol","li","img","span"
223
- ]);
224
- var out = [];
225
- function walk(el, parentPath, indexAmongChildren) {
226
- var tag = el.tagName.toLowerCase();
227
- var path = parentPath === "" ? tag + "[0]" : parentPath + ">" + tag + "[" + indexAmongChildren + "]";
228
- var hasClass = el.hasAttribute && el.hasAttribute("class");
229
- var interesting = hasClass || SEMANTIC.has(tag);
230
- if (interesting) {
231
- var cs = getComputedStyle(el);
232
- var styles = {};
233
- for (var i = 0; i < TRACKED.length; i++) {
234
- var p = TRACKED[i];
235
- styles[p] = cs.getPropertyValue(p);
236
- }
237
- // Capture parent display so the diff layer can flag flex/grid-item coercion.
238
- var parentDisplay = "";
239
- if (el.parentElement) {
240
- try { parentDisplay = getComputedStyle(el.parentElement).getPropertyValue("display"); } catch (e) { parentDisplay = ""; }
241
- }
242
- out.push({
243
- path: path,
244
- tag: tag,
245
- classes: (el.getAttribute("class") || "").trim(),
246
- styles: styles,
247
- parentDisplay: parentDisplay
248
- });
249
- }
250
- var children = el.children;
251
- for (var j = 0; j < children.length; j++) {
252
- walk(children[j], path, j);
253
- }
254
- }
255
- if (document.body) walk(document.body, "", 0);
256
- return out;
257
- })())`;
258
-
259
- export function parseDomPositionStyles(value: unknown): PositionedElement[] {
260
- if (Array.isArray(value)) return value as PositionedElement[];
261
- if (typeof value !== "string") return [];
262
- try {
263
- const parsed = JSON.parse(value);
264
- return Array.isArray(parsed) ? parsed as PositionedElement[] : [];
265
- } catch {
266
- return [];
267
- }
268
- }
269
-
270
- // ----------------------------------------------------------------------
271
- // Per-viewport variant
272
- // ----------------------------------------------------------------------
273
-
274
- export interface DpEntryWithViewport extends DpEntry {
275
- viewport: string;
276
- }
277
-
278
- export interface DpSample {
279
- viewport: string;
280
- baseline: string;
281
- variant: string;
282
- /** Em-normalized values when the property is em-relative. */
283
- baselineEm?: number;
284
- variantEm?: number;
285
- }
286
-
287
- export interface DpPerViewportResult {
288
- /** All per-viewport diff tuples (entries[i] carries its viewport). */
289
- entries: DpEntryWithViewport[];
290
- /** Total tuples across all viewports (before any caller-side cap). */
291
- totalDiffs: number;
292
- /**
293
- * Compact verification index: `${variantClassToken}::${property}` strings
294
- * for every real delta. Used by `diff-for-agent` to gate heuristic
295
- * fix-candidate ✓/✗ marks. Stored unconditionally (cheap; tens of KB
296
- * worst-case) so verification stays accurate even when `entries` and
297
- * `byPathProperty` are capped for report-size reasons.
298
- */
299
- verifiedPairs: string[];
300
- /**
301
- * (path, property) → viewport-list. Same property at same position can
302
- * differ on some viewports and not others (media-query-gated rules).
303
- * Sorted by number of viewports descending.
304
- */
305
- byPathProperty: Array<{
306
- path: string;
307
- property: string;
308
- baselineClasses: string;
309
- variantClasses: string;
310
- viewports: string[];
311
- samples: DpSample[];
312
- /** Set on `display`-property pairs when at least one parent is flex/grid. */
313
- parentDisplayContext?: {
314
- baselineParent: string;
315
- variantParent: string;
316
- isFlexOrGridItem: boolean;
317
- };
318
- }>;
319
- /** Per-viewport tally so the caller can see which viewport is worst. */
320
- byViewport: Array<{ viewport: string; count: number }>;
321
- /** Property → total viewport-occurrences, sorted desc. */
322
- byProperty: Array<{ property: string; count: number }>;
323
- /** Path → total viewport-occurrences + class names, sorted desc. */
324
- byPath: Array<{
325
- path: string;
326
- baselineClasses: string;
327
- variantClasses: string;
328
- count: number;
329
- }>;
330
- }
331
-
332
- const EMPTY_PV: DpPerViewportResult = {
333
- entries: [],
334
- totalDiffs: 0,
335
- verifiedPairs: [],
336
- byPathProperty: [],
337
- byViewport: [],
338
- byProperty: [],
339
- byPath: [],
340
- };
341
-
342
- /**
343
- * Diff DOM-position style captures across N viewports.
344
- *
345
- * Inputs are maps of viewport-label → captured element list. Each viewport
346
- * is diffed independently (same `diffDomPositionStyles` per-viewport
347
- * semantics), then results are merged with a viewport label per tuple and
348
- * a (path, property) cross-viewport aggregate so the agent can spot
349
- * "this delta appears at every viewport" vs "this delta only appears at
350
- * mobile" (the latter usually means a missing or wrong media query).
351
- */
352
- export function diffPositionStylesAcrossViewports(
353
- baselineByVp: Map<string, PositionedElement[]> | Record<string, PositionedElement[]>,
354
- variantByVp: Map<string, PositionedElement[]> | Record<string, PositionedElement[]>,
355
- ): DpPerViewportResult {
356
- const baselineMap = baselineByVp instanceof Map ? baselineByVp : new Map(Object.entries(baselineByVp));
357
- const variantMap = variantByVp instanceof Map ? variantByVp : new Map(Object.entries(variantByVp));
358
- if (baselineMap.size === 0 || variantMap.size === 0) return EMPTY_PV;
359
-
360
- const entries: DpEntryWithViewport[] = [];
361
- const byViewport = new Map<string, number>();
362
-
363
- for (const [viewport, baselineList] of baselineMap) {
364
- const variantList = variantMap.get(viewport);
365
- if (!variantList) continue;
366
- const perVp = diffDomPositionStyles(baselineList, variantList);
367
- for (const e of perVp.entries) entries.push({ ...e, viewport });
368
- byViewport.set(viewport, (byViewport.get(viewport) ?? 0) + perVp.entries.length);
369
- }
370
-
371
- // Aggregate per (path, property) — same delta on multiple viewports is
372
- // typically one source rule; differing-viewport deltas are media-query
373
- // gated.
374
- const ppKey = (path: string, property: string) => `${path} ${property}`;
375
- const aggregated = new Map<string, {
376
- path: string;
377
- property: string;
378
- baselineClasses: string;
379
- variantClasses: string;
380
- viewports: string[];
381
- samples: DpSample[];
382
- parentDisplayContext?: { baselineParent: string; variantParent: string; isFlexOrGridItem: boolean };
383
- }>();
384
- for (const e of entries) {
385
- const k = ppKey(e.path, e.property);
386
- const sample: DpSample = {
387
- viewport: e.viewport,
388
- baseline: e.baseline,
389
- variant: e.variant,
390
- };
391
- if (e.baselineEm !== undefined) sample.baselineEm = e.baselineEm;
392
- if (e.variantEm !== undefined) sample.variantEm = e.variantEm;
393
- const existing = aggregated.get(k);
394
- if (existing) {
395
- existing.viewports.push(e.viewport);
396
- // De-dupe samples by (baseline, variant) — most deltas are identical
397
- // across affected viewports (e.g. `height: 48px → 33px` everywhere it
398
- // applies). Keep at most one sample per unique value-pair so the
399
- // report stays compact when shipped as JSON.
400
- const alreadySeen = existing.samples.some(
401
- (s) => s.baseline === e.baseline && s.variant === e.variant,
402
- );
403
- if (!alreadySeen) {
404
- existing.samples.push(sample);
405
- }
406
- } else {
407
- aggregated.set(k, {
408
- path: e.path,
409
- property: e.property,
410
- baselineClasses: e.baselineClasses,
411
- variantClasses: e.variantClasses,
412
- viewports: [e.viewport],
413
- samples: [sample],
414
- parentDisplayContext: e.parentDisplayContext,
415
- });
416
- }
417
- }
418
- const byPathProperty = [...aggregated.values()].sort((a, b) =>
419
- b.viewports.length - a.viewports.length
420
- || a.path.localeCompare(b.path)
421
- || a.property.localeCompare(b.property),
422
- );
423
-
424
- const byProperty = new Map<string, number>();
425
- for (const e of entries) byProperty.set(e.property, (byProperty.get(e.property) ?? 0) + 1);
426
- const byPath = new Map<string, { baselineClasses: string; variantClasses: string; count: number }>();
427
- for (const e of entries) {
428
- const cur = byPath.get(e.path) ?? { baselineClasses: e.baselineClasses, variantClasses: e.variantClasses, count: 0 };
429
- cur.count += 1;
430
- byPath.set(e.path, cur);
431
- }
432
-
433
- // Compact verification set: `${variantClassToken}::${property}` plus a
434
- // bare `::property` fallback used by tag-only candidates. Unbounded
435
- // because each entry is short and downstream consumers gate on it for
436
- // ✓/✗ accuracy even when entries/byPathProperty are capped.
437
- const verifiedPairsSet = new Set<string>();
438
- for (const e of entries) {
439
- const tokens = e.variantClasses.split(/\s+/).filter(Boolean);
440
- for (const cls of tokens) verifiedPairsSet.add(`.${cls}::${e.property}`);
441
- verifiedPairsSet.add(`::${e.property}`);
442
- }
443
- const verifiedPairs = [...verifiedPairsSet].sort();
444
-
445
- return {
446
- entries,
447
- totalDiffs: entries.length,
448
- verifiedPairs,
449
- byPathProperty,
450
- byViewport: [...byViewport.entries()].map(([viewport, count]) => ({ viewport, count }))
451
- .sort((a, b) => b.count - a.count || a.viewport.localeCompare(b.viewport)),
452
- byProperty: [...byProperty.entries()].map(([property, count]) => ({ property, count }))
453
- .sort((a, b) => b.count - a.count || a.property.localeCompare(b.property)),
454
- byPath: [...byPath.entries()].map(([path, v]) => ({ path, baselineClasses: v.baselineClasses, variantClasses: v.variantClasses, count: v.count }))
455
- .sort((a, b) => b.count - a.count || a.path.localeCompare(b.path)),
456
- };
457
- }