@mizchi/vlmkit-core 0.8.0 → 0.8.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 (66) hide show
  1. package/dist/a11y-semantic.d.mts +34 -0
  2. package/dist/a11y-semantic.mjs +261 -0
  3. package/dist/authored-style-capture.d.mts +42 -0
  4. package/dist/authored-style-capture.mjs +117 -0
  5. package/dist/authored-style-diff.d.mts +15 -0
  6. package/dist/authored-style-diff.mjs +16 -0
  7. package/dist/cli-args.d.mts +11 -0
  8. package/dist/cli-args.mjs +21 -0
  9. package/dist/cli-error.d.mts +4 -0
  10. package/dist/cli-error.mjs +59 -0
  11. package/dist/computed-style-capture.d.mts +41 -0
  12. package/dist/computed-style-capture.mjs +589 -0
  13. package/dist/computed-style-diff.d.mts +83 -0
  14. package/dist/computed-style-diff.mjs +123 -0
  15. package/dist/diff-regions.d.mts +8 -0
  16. package/dist/diff-regions.mjs +52 -0
  17. package/dist/dom-equivalence.d.mts +51 -0
  18. package/dist/dom-equivalence.mjs +91 -0
  19. package/dist/dom-position-styles.d.mts +166 -0
  20. package/dist/dom-position-styles.mjs +296 -0
  21. package/dist/element-compare.d.mts +71 -0
  22. package/dist/element-compare.mjs +269 -0
  23. package/dist/heatmap-regions.d.mts +57 -0
  24. package/dist/heatmap-regions.mjs +158 -0
  25. package/dist/heatmap.d.mts +86 -0
  26. package/dist/heatmap.mjs +510 -0
  27. package/dist/image-resize.d.mts +34 -0
  28. package/dist/image-resize.mjs +92 -0
  29. package/dist/index.d.mts +15 -0
  30. package/dist/index.mjs +14 -0
  31. package/dist/mask.d.mts +18 -0
  32. package/dist/mask.mjs +27 -0
  33. package/dist/png-utils.d.mts +30 -0
  34. package/dist/png-utils.mjs +80 -0
  35. package/dist/region-shift.d.mts +20 -0
  36. package/dist/region-shift.mjs +128 -0
  37. package/dist/run-ledger.d.mts +14 -0
  38. package/dist/run-ledger.mjs +31 -0
  39. package/dist/terminal-colors.d.mts +12 -0
  40. package/dist/terminal-colors.mjs +14 -0
  41. package/dist/text-rows.d.mts +121 -0
  42. package/dist/text-rows.mjs +246 -0
  43. package/{src/types.ts → dist/types.d.mts} +88 -206
  44. package/dist/types.mjs +1 -0
  45. package/package.json +17 -5
  46. package/src/a11y-semantic.ts +0 -384
  47. package/src/authored-style-capture.ts +0 -185
  48. package/src/authored-style-diff.ts +0 -32
  49. package/src/cli-args.ts +0 -29
  50. package/src/cli-error.ts +0 -85
  51. package/src/computed-style-capture.ts +0 -717
  52. package/src/computed-style-diff.ts +0 -192
  53. package/src/diff-regions.ts +0 -70
  54. package/src/dom-equivalence.ts +0 -158
  55. package/src/dom-position-styles.ts +0 -457
  56. package/src/element-compare.ts +0 -354
  57. package/src/heatmap-regions.ts +0 -245
  58. package/src/heatmap.ts +0 -670
  59. package/src/image-resize.ts +0 -112
  60. package/src/index.ts +0 -105
  61. package/src/mask.ts +0 -41
  62. package/src/png-utils.ts +0 -86
  63. package/src/region-shift.ts +0 -173
  64. package/src/run-ledger.ts +0 -39
  65. package/src/terminal-colors.ts +0 -13
  66. package/src/text-rows.ts +0 -412
@@ -1,192 +0,0 @@
1
- /**
2
- * Computed-style diff between two captured snapshots.
3
- *
4
- * Closes the dogfood gap from `docs/reports/2026-05-12-dogfood-shadcn-luna.md`
5
- * point 4: "Pixel-matching can't catch semantically-different CSS that
6
- * produces identical layout (margin-sibling vs flex gap)." Layered on top
7
- * of the existing `computed-style-capture` snapshot infrastructure.
8
- *
9
- * The diff lists (selector, property, before, after) tuples, plus a
10
- * by-property tally so the agent can see "12 selectors changed
11
- * `gap`, 8 changed `padding`" at a glance.
12
- */
13
-
14
- export type { ComputedStyleSnapshot } from "./computed-style-capture.ts";
15
- import type { ComputedStyleSnapshot } from "./computed-style-capture.ts";
16
-
17
- export interface CsdEntry {
18
- selector: string;
19
- property: string;
20
- baseline: string;
21
- variant: string;
22
- }
23
-
24
- export interface CsdResult {
25
- /** Per-(selector, property) differences. */
26
- entries: CsdEntry[];
27
- /** Total differing (selector, property) pairs. */
28
- totalDiffs: number;
29
- /** Sorted property → count map (descending). */
30
- byProperty: Array<{ property: string; count: number }>;
31
- /** Sorted selector → count map (descending). */
32
- bySelector: Array<{ selector: string; count: number }>;
33
- /** Selectors present only in one side. */
34
- selectorsOnlyInBaseline: string[];
35
- selectorsOnlyInVariant: string[];
36
- }
37
-
38
- export interface CsdPerViewportSample {
39
- viewport: string;
40
- baseline: string;
41
- variant: string;
42
- }
43
-
44
- export interface CsdPerViewportEntry {
45
- selector: string;
46
- property: string;
47
- /** Viewports on which this (selector, property) pair differs. */
48
- viewports: string[];
49
- samples: CsdPerViewportSample[];
50
- }
51
-
52
- export interface CsdPerViewportResult {
53
- /** Total (selector, property, viewport) tuples — i.e. Σ per-viewport counts. */
54
- totalDiffs: number;
55
- byViewport: Array<{ viewport: string; count: number }>;
56
- /**
57
- * One row per unique (selector, property) pair, listing which viewports
58
- * it differs on and the actual baseline/variant samples per viewport.
59
- */
60
- bySelectorProperty: CsdPerViewportEntry[];
61
- /**
62
- * "selector|property" tokens for pairs that differ on every viewport.
63
- * A consumer rendering "fix the base rule" tables wants this subset.
64
- */
65
- universalPairs: string[];
66
- /**
67
- * "selector|property" tokens for pairs that differ on a strict subset
68
- * of viewports. Almost always a `@media` rule is missing or wrong.
69
- */
70
- breakpointGatedPairs: string[];
71
- }
72
-
73
- /**
74
- * Roll up N per-viewport CSD results into a single per-viewport report,
75
- * surfacing which pairs apply universally vs. which are breakpoint-gated.
76
- *
77
- * Stable iteration order: results are taken in the input array's order,
78
- * which is the same order migration-compare captures viewports (small →
79
- * large or whatever the caller chose).
80
- */
81
- export function aggregateCsdByViewport(
82
- perViewport: ReadonlyArray<{ viewport: string; result: CsdResult }>,
83
- ): CsdPerViewportResult {
84
- const totalViewports = perViewport.length;
85
- const pairToEntry = new Map<string, CsdPerViewportEntry>();
86
- const byViewport: Array<{ viewport: string; count: number }> = [];
87
-
88
- for (const { viewport, result } of perViewport) {
89
- byViewport.push({ viewport, count: result.entries.length });
90
- for (const entry of result.entries) {
91
- const key = `${entry.selector}|${entry.property}`;
92
- const existing = pairToEntry.get(key);
93
- if (existing) {
94
- existing.viewports.push(viewport);
95
- existing.samples.push({ viewport, baseline: entry.baseline, variant: entry.variant });
96
- } else {
97
- pairToEntry.set(key, {
98
- selector: entry.selector,
99
- property: entry.property,
100
- viewports: [viewport],
101
- samples: [{ viewport, baseline: entry.baseline, variant: entry.variant }],
102
- });
103
- }
104
- }
105
- }
106
-
107
- const bySelectorProperty = [...pairToEntry.values()].sort((a, b) => {
108
- if (b.viewports.length !== a.viewports.length) return b.viewports.length - a.viewports.length;
109
- if (a.selector !== b.selector) return a.selector.localeCompare(b.selector);
110
- return a.property.localeCompare(b.property);
111
- });
112
-
113
- const universalPairs: string[] = [];
114
- const breakpointGatedPairs: string[] = [];
115
- for (const entry of bySelectorProperty) {
116
- const token = `${entry.selector}|${entry.property}`;
117
- if (totalViewports > 0 && entry.viewports.length === totalViewports) {
118
- universalPairs.push(token);
119
- } else {
120
- breakpointGatedPairs.push(token);
121
- }
122
- }
123
-
124
- return {
125
- totalDiffs: byViewport.reduce((sum, e) => sum + e.count, 0),
126
- byViewport,
127
- bySelectorProperty,
128
- universalPairs,
129
- breakpointGatedPairs,
130
- };
131
- }
132
-
133
- const EMPTY: CsdResult = {
134
- entries: [],
135
- totalDiffs: 0,
136
- byProperty: [],
137
- bySelector: [],
138
- selectorsOnlyInBaseline: [],
139
- selectorsOnlyInVariant: [],
140
- };
141
-
142
- /**
143
- * Diff two computed-style snapshots. Both inputs are
144
- * `Record<selector, Record<property, value>>`; produced by
145
- * `captureComputedStyleSnapshotInDom`.
146
- */
147
- export function diffComputedStyles(
148
- baseline: ComputedStyleSnapshot,
149
- variant: ComputedStyleSnapshot,
150
- ): CsdResult {
151
- if (!baseline || !variant) return EMPTY;
152
-
153
- const entries: CsdEntry[] = [];
154
- const byProperty = new Map<string, number>();
155
- const bySelector = new Map<string, number>();
156
-
157
- const baselineSelectors = new Set(Object.keys(baseline));
158
- const variantSelectors = new Set(Object.keys(variant));
159
- const shared = [...baselineSelectors].filter((s) => variantSelectors.has(s));
160
-
161
- for (const selector of shared) {
162
- const b = baseline[selector] ?? {};
163
- const v = variant[selector] ?? {};
164
- const props = new Set([...Object.keys(b), ...Object.keys(v)]);
165
- for (const property of props) {
166
- const bv = b[property] ?? "";
167
- const vv = v[property] ?? "";
168
- if (bv !== vv) {
169
- entries.push({ selector, property, baseline: bv, variant: vv });
170
- byProperty.set(property, (byProperty.get(property) ?? 0) + 1);
171
- bySelector.set(selector, (bySelector.get(selector) ?? 0) + 1);
172
- }
173
- }
174
- }
175
-
176
- const selectorsOnlyInBaseline = [...baselineSelectors].filter((s) => !variantSelectors.has(s));
177
- const selectorsOnlyInVariant = [...variantSelectors].filter((s) => !baselineSelectors.has(s));
178
-
179
- const sortMap = (m: Map<string, number>) =>
180
- [...m.entries()]
181
- .map(([k, count]) => ({ key: k, count }))
182
- .sort((a, b) => b.count - a.count || a.key.localeCompare(b.key));
183
-
184
- return {
185
- entries,
186
- totalDiffs: entries.length,
187
- byProperty: sortMap(byProperty).map(({ key, count }) => ({ property: key, count })),
188
- bySelector: sortMap(bySelector).map(({ key, count }) => ({ selector: key, count })),
189
- selectorsOnlyInBaseline,
190
- selectorsOnlyInVariant,
191
- };
192
- }
@@ -1,70 +0,0 @@
1
- import type { DiffRegion, VrtDiff } from "./types.ts";
2
-
3
- export function normalizeVrtDiffRegions(diff: VrtDiff): DiffRegion[] {
4
- const normalizedPixels = normalizeVrtDiffRegionPixels(diff);
5
- return diff.regions.map((region, index) => ({
6
- ...region,
7
- diffPixelCount: normalizedPixels[index] ?? 0,
8
- }));
9
- }
10
-
11
- export function normalizeVrtDiffRegionPixels(diff: VrtDiff): number[] {
12
- if (diff.regions.length === 0) return [];
13
-
14
- const target = Math.max(0, Math.round(diff.diffPixels));
15
- const rawWeights = diff.regions.map((region) => Math.max(0, Math.round(region.diffPixelCount)));
16
- const totalWeight = rawWeights.reduce((sum, weight) => sum + weight, 0);
17
-
18
- if (target === 0) {
19
- return rawWeights.map(() => 0);
20
- }
21
-
22
- if (totalWeight === target) {
23
- return rawWeights;
24
- }
25
-
26
- if (totalWeight === 0) {
27
- return distributeEvenly(target, diff.regions.length);
28
- }
29
-
30
- const exactShares = rawWeights.map((weight) => (target * weight) / totalWeight);
31
- const normalized = exactShares.map((share) => Math.floor(share));
32
- let remainder = target - normalized.reduce((sum, value) => sum + value, 0);
33
-
34
- const byFraction = exactShares
35
- .map((share, index) => ({ index, fraction: share - Math.floor(share) }))
36
- .sort((left, right) => right.fraction - left.fraction);
37
-
38
- for (const entry of byFraction) {
39
- if (remainder <= 0) break;
40
- normalized[entry.index]++;
41
- remainder--;
42
- }
43
-
44
- return normalized;
45
- }
46
-
47
- export function createScopedVrtDiff(
48
- diff: VrtDiff,
49
- region: DiffRegion,
50
- ): VrtDiff {
51
- return {
52
- ...diff,
53
- diffPixels: region.diffPixelCount,
54
- diffRatio: region.diffPixelCount / Math.max(diff.totalPixels, 1),
55
- regions: [region],
56
- heatmapPath: undefined,
57
- };
58
- }
59
-
60
- function distributeEvenly(total: number, count: number): number[] {
61
- const base = Math.floor(total / count);
62
- let remainder = total - base * count;
63
- return Array.from({ length: count }, () => {
64
- if (remainder > 0) {
65
- remainder--;
66
- return base + 1;
67
- }
68
- return base;
69
- });
70
- }
@@ -1,158 +0,0 @@
1
- /**
2
- * DOM equivalence preflight for `migration-compare`.
3
- *
4
- * Catches the failure mode from `docs/reports/2026-05-12-dogfood-shadcn-luna.md`:
5
- * an agent edits the *body* of the variant HTML (rename a heading, drop a
6
- * button, change input values) and the run blames `layout-shift` because
7
- * the renders diverge structurally rather than stylistically.
8
- *
9
- * The preflight captures three lightweight fingerprints per document:
10
- * - heading texts in document order (h1..h6)
11
- * - button texts in document order
12
- * - input/textarea/select value sequence
13
- *
14
- * Both pure helpers (sequence diff) and a browser-side capture script live
15
- * here; migration-compare evaluates the script with `page.evaluate()`
16
- * and feeds the results into `evaluateDomEquivalence`.
17
- */
18
-
19
- export interface DomFingerprint {
20
- headingTexts: string[];
21
- buttonTexts: string[];
22
- inputValues: string[];
23
- elementCount: number;
24
- }
25
-
26
- export interface DomEquivalenceWarning {
27
- code:
28
- | "heading-mismatch"
29
- | "button-mismatch"
30
- | "input-mismatch"
31
- | "element-count-mismatch";
32
- message: string;
33
- baseline: string[];
34
- variant: string[];
35
- }
36
-
37
- export interface DomEquivalenceResult {
38
- ok: boolean;
39
- warnings: DomEquivalenceWarning[];
40
- }
41
-
42
- function normalize(s: string): string {
43
- return s.replace(/\s+/g, " ").trim();
44
- }
45
-
46
- function diffSequence(a: string[], b: string[]): { added: string[]; removed: string[]; reordered: boolean } {
47
- const aNorm = a.map(normalize);
48
- const bNorm = b.map(normalize);
49
- const aSet = new Set(aNorm);
50
- const bSet = new Set(bNorm);
51
- const added = bNorm.filter((x) => !aSet.has(x));
52
- const removed = aNorm.filter((x) => !bSet.has(x));
53
- const sameElements = added.length === 0 && removed.length === 0;
54
- const reordered = sameElements && aNorm.join("|") !== bNorm.join("|");
55
- return { added, removed, reordered };
56
- }
57
-
58
- function makeMismatchMessage(
59
- kind: "headings" | "buttons" | "inputs",
60
- diff: { added: string[]; removed: string[]; reordered: boolean },
61
- ): string {
62
- if (diff.reordered) {
63
- return `${kind} reordered between baseline and variant`;
64
- }
65
- const parts: string[] = [];
66
- if (diff.removed.length > 0) {
67
- parts.push(`missing in variant: [${diff.removed.map((s) => `"${s}"`).join(", ")}]`);
68
- }
69
- if (diff.added.length > 0) {
70
- parts.push(`extra in variant: [${diff.added.map((s) => `"${s}"`).join(", ")}]`);
71
- }
72
- return `${kind} differ — ${parts.join("; ")}`;
73
- }
74
-
75
- /**
76
- * Threshold: 5% element-count drift before flagging. Single-element drift on
77
- * small fixtures is noise; large additions/removals are usually a real
78
- * DOM edit.
79
- */
80
- const ELEMENT_COUNT_DRIFT_THRESHOLD = 0.05;
81
-
82
- /**
83
- * Verify structural DOM equivalence between two captured fingerprints.
84
- *
85
- * @since 0.5.0 — replaces `evaluateDomEquivalence`.
86
- */
87
- export function verifyDomEquivalence(
88
- baseline: DomFingerprint,
89
- variant: DomFingerprint,
90
- ): DomEquivalenceResult {
91
- const warnings: DomEquivalenceWarning[] = [];
92
-
93
- const headingDiff = diffSequence(baseline.headingTexts, variant.headingTexts);
94
- if (headingDiff.added.length > 0 || headingDiff.removed.length > 0 || headingDiff.reordered) {
95
- warnings.push({
96
- code: "heading-mismatch",
97
- message: makeMismatchMessage("headings", headingDiff),
98
- baseline: baseline.headingTexts,
99
- variant: variant.headingTexts,
100
- });
101
- }
102
-
103
- const buttonDiff = diffSequence(baseline.buttonTexts, variant.buttonTexts);
104
- if (buttonDiff.added.length > 0 || buttonDiff.removed.length > 0 || buttonDiff.reordered) {
105
- warnings.push({
106
- code: "button-mismatch",
107
- message: makeMismatchMessage("buttons", buttonDiff),
108
- baseline: baseline.buttonTexts,
109
- variant: variant.buttonTexts,
110
- });
111
- }
112
-
113
- const inputDiff = diffSequence(baseline.inputValues, variant.inputValues);
114
- if (inputDiff.added.length > 0 || inputDiff.removed.length > 0 || inputDiff.reordered) {
115
- warnings.push({
116
- code: "input-mismatch",
117
- message: makeMismatchMessage("inputs", inputDiff),
118
- baseline: baseline.inputValues,
119
- variant: variant.inputValues,
120
- });
121
- }
122
-
123
- const drift = baseline.elementCount === 0
124
- ? 0
125
- : Math.abs(baseline.elementCount - variant.elementCount) / baseline.elementCount;
126
- if (drift > ELEMENT_COUNT_DRIFT_THRESHOLD) {
127
- warnings.push({
128
- code: "element-count-mismatch",
129
- message: `total element count differs by ${(drift * 100).toFixed(1)}% ` +
130
- `(baseline: ${baseline.elementCount}, variant: ${variant.elementCount})`,
131
- baseline: [String(baseline.elementCount)],
132
- variant: [String(variant.elementCount)],
133
- });
134
- }
135
-
136
- return { ok: warnings.length === 0, warnings };
137
- }
138
-
139
- /**
140
- * Browser-side capture script. Designed to be passed verbatim to
141
- * `page.evaluate()`; produces a {@link DomFingerprint}.
142
- */
143
- export const DOM_FINGERPRINT_BROWSER_SCRIPT = `(() => {
144
- const norm = (s) => (s || "").replace(/\\s+/g, " ").trim();
145
- const headingTexts = Array.from(document.querySelectorAll("h1, h2, h3, h4, h5, h6")).map((el) => norm(el.textContent));
146
- const buttonTexts = Array.from(document.querySelectorAll("button")).map((el) => norm(el.textContent));
147
- const inputValues = Array.from(document.querySelectorAll("input, textarea, select")).map((el) => {
148
- const v = el.value != null ? el.value : el.getAttribute("value") || "";
149
- return norm(v);
150
- });
151
- const elementCount = document.querySelectorAll("*").length;
152
- return { headingTexts, buttonTexts, inputValues, elementCount };
153
- })()`;
154
-
155
- /**
156
- * @deprecated since 0.5.0 — use `verifyDomEquivalence` instead. Removed in 1.0.0.
157
- */
158
- export const evaluateDomEquivalence = verifyDomEquivalence;