@mizchi/vlmkit-core 0.6.0 → 0.7.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.
- package/README.md +9 -6
- package/package.json +3 -3
- package/src/authored-style-capture.ts +185 -0
- package/src/authored-style-diff.ts +32 -0
- package/src/computed-style-capture.ts +143 -22
- package/src/element-compare.ts +25 -10
- package/src/heatmap-regions.ts +5 -6
- package/src/heatmap.ts +155 -1
- package/src/image-resize.ts +1 -1
- package/src/index.ts +5 -8
- package/src/png-utils.ts +30 -0
- package/src/region-shift.ts +173 -0
- package/src/text-rows.ts +2 -0
- package/src/types.ts +65 -0
- package/src/a11y-contrast.ts +0 -387
- package/src/a11y-focus-order.ts +0 -397
- package/src/a11y-touch.ts +0 -340
- package/src/grid-ratio.ts +0 -251
- package/src/png-diff.ts +0 -165
- package/src/quality.ts +0 -209
- package/src/region-classify.ts +0 -189
- package/src/shift-origin.ts +0 -204
- package/src/visual-semantic.ts +0 -185
package/src/shift-origin.ts
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Vertical-shift origin diagnostic.
|
|
3
|
-
*
|
|
4
|
-
* Closes the residual gap from `docs/reports/2026-05-12-subagent-eval.md`:
|
|
5
|
-
* after per-viewport DOM-position diff lands, layout-shift bands can
|
|
6
|
-
* still appear with no DOM-position delta attributing them. Subagent D
|
|
7
|
-
* plateaued at 4 viewports × ~3% diff because of one such band
|
|
8
|
-
* (`[0..1047]: +152px`) at viewports ≥ 1024.
|
|
9
|
-
*
|
|
10
|
-
* This module captures per-element bounding boxes on each page and,
|
|
11
|
-
* given the existing per-band shift report, names the *first* element
|
|
12
|
-
* whose y-coordinate diverges between baseline and variant. The agent
|
|
13
|
-
* gets a "shift origin" pointer instead of just "everything below row
|
|
14
|
-
* N moved +152px."
|
|
15
|
-
*
|
|
16
|
-
* Pure module. Browser-side capture script is a string for
|
|
17
|
-
* `page.evaluate()`; matching algorithm runs in Node.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
import type { ShiftRegion } from "./types.ts";
|
|
21
|
-
|
|
22
|
-
export interface BboxElement {
|
|
23
|
-
/** DOM path: `tag[childIndex]>...` rooted at body (same scheme as dom-position-styles). */
|
|
24
|
-
path: string;
|
|
25
|
-
tag: string;
|
|
26
|
-
classes: string;
|
|
27
|
-
top: number;
|
|
28
|
-
left: number;
|
|
29
|
-
width: number;
|
|
30
|
-
height: number;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface ShiftOrigin {
|
|
34
|
-
/** Band y-range this origin explains. */
|
|
35
|
-
bandStart: number;
|
|
36
|
-
bandEnd: number;
|
|
37
|
-
bandShift: number;
|
|
38
|
-
originPath: string;
|
|
39
|
-
originTag: string;
|
|
40
|
-
originBaselineTop: number;
|
|
41
|
-
originVariantTop: number;
|
|
42
|
-
originDeltaY: number;
|
|
43
|
-
originBaselineClasses: string;
|
|
44
|
-
originVariantClasses: string;
|
|
45
|
-
/** Heuristic guess at the responsible CSS axis based on bbox deltas. */
|
|
46
|
-
suspectedAxis?: "height" | "margin/padding-above" | "y-position" | "unknown";
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export interface FindShiftOriginsOptions {
|
|
50
|
-
/**
|
|
51
|
-
* Δy magnitude (px) above which an element is considered "shifted." Defaults
|
|
52
|
-
* to 5 — below this is usually subpixel rounding from font metrics.
|
|
53
|
-
*/
|
|
54
|
-
minDeltaPx?: number;
|
|
55
|
-
/**
|
|
56
|
-
* Number of candidate origin elements to emit per band (most useful is the
|
|
57
|
-
* first; subsequent ones can be downstream propagation, but giving the
|
|
58
|
-
* agent 2–3 helps when the first guess is wrong).
|
|
59
|
-
*/
|
|
60
|
-
perBandLimit?: number;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const DEFAULT_MIN_DELTA = 5;
|
|
64
|
-
const DEFAULT_PER_BAND_LIMIT = 3;
|
|
65
|
-
|
|
66
|
-
function classifySuspect(
|
|
67
|
-
baseline: BboxElement,
|
|
68
|
-
variant: BboxElement,
|
|
69
|
-
): ShiftOrigin["suspectedAxis"] {
|
|
70
|
-
// Element itself has different height → its `height`/`padding`/`line-height` is the cause.
|
|
71
|
-
const heightDelta = Math.abs(baseline.height - variant.height);
|
|
72
|
-
// Element's top moved but its left didn't → vertical-axis cause.
|
|
73
|
-
const topDelta = Math.abs(baseline.top - variant.top);
|
|
74
|
-
if (heightDelta >= 5) return "height";
|
|
75
|
-
if (topDelta >= 5) return "margin/padding-above";
|
|
76
|
-
return "y-position";
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* For each shift band, walk baseline elements in document order (sorted by
|
|
81
|
-
* baseline.top) and find the first one whose matching-by-path variant
|
|
82
|
-
* counterpart has a Δy comparable to the band's shift magnitude.
|
|
83
|
-
*
|
|
84
|
-
* The "first such element" is the most actionable origin — everything below
|
|
85
|
-
* it just inherits the shift. We emit up to `perBandLimit` candidates so
|
|
86
|
-
* the agent gets fallback options.
|
|
87
|
-
*/
|
|
88
|
-
export function findShiftOrigins(
|
|
89
|
-
baseline: BboxElement[],
|
|
90
|
-
variant: BboxElement[],
|
|
91
|
-
shiftRegions: ShiftRegion[],
|
|
92
|
-
options: FindShiftOriginsOptions = {},
|
|
93
|
-
): ShiftOrigin[] {
|
|
94
|
-
const minDelta = options.minDeltaPx ?? DEFAULT_MIN_DELTA;
|
|
95
|
-
const perBandLimit = options.perBandLimit ?? DEFAULT_PER_BAND_LIMIT;
|
|
96
|
-
if (baseline.length === 0 || variant.length === 0 || shiftRegions.length === 0) return [];
|
|
97
|
-
|
|
98
|
-
const variantByPath = new Map<string, BboxElement>();
|
|
99
|
-
for (const v of variant) variantByPath.set(v.path, v);
|
|
100
|
-
|
|
101
|
-
const sorted = [...baseline].sort((a, b) => a.top - b.top || a.left - b.left);
|
|
102
|
-
const origins: ShiftOrigin[] = [];
|
|
103
|
-
|
|
104
|
-
for (const band of shiftRegions) {
|
|
105
|
-
const bandShift = band.shift;
|
|
106
|
-
if (Math.abs(bandShift) < minDelta) continue;
|
|
107
|
-
|
|
108
|
-
// Look at elements whose baseline.top is at or just above the band's
|
|
109
|
-
// start — typical origin is the element just before the visible shift
|
|
110
|
-
// boundary (extra height/padding pushes everything below).
|
|
111
|
-
const candidates: ShiftOrigin[] = [];
|
|
112
|
-
for (const b of sorted) {
|
|
113
|
-
// Skip elements far above the band (they wouldn't be the local cause).
|
|
114
|
-
if (b.top < band.yStart - 200) continue;
|
|
115
|
-
// Stop scanning well past the band end — origin is always at or above.
|
|
116
|
-
if (b.top > band.yEnd) break;
|
|
117
|
-
|
|
118
|
-
const v = variantByPath.get(b.path);
|
|
119
|
-
if (!v) continue;
|
|
120
|
-
const deltaY = v.top - b.top;
|
|
121
|
-
if (Math.abs(deltaY) < minDelta) continue;
|
|
122
|
-
// Note: do NOT require Math.sign(deltaY) === Math.sign(bandShift).
|
|
123
|
-
// The pixelmatch-derived band shift uses cross-correlation which can
|
|
124
|
-
// report a sign opposite to the bbox Δy when the variant has *less*
|
|
125
|
-
// content than the baseline (or vice versa). We still want to flag
|
|
126
|
-
// the element so the agent can interpret the direction itself.
|
|
127
|
-
|
|
128
|
-
candidates.push({
|
|
129
|
-
bandStart: band.yStart,
|
|
130
|
-
bandEnd: band.yEnd,
|
|
131
|
-
bandShift,
|
|
132
|
-
originPath: b.path,
|
|
133
|
-
originTag: b.tag,
|
|
134
|
-
originBaselineTop: b.top,
|
|
135
|
-
originVariantTop: v.top,
|
|
136
|
-
originDeltaY: deltaY,
|
|
137
|
-
originBaselineClasses: b.classes,
|
|
138
|
-
originVariantClasses: v.classes,
|
|
139
|
-
suspectedAxis: classifySuspect(b, v),
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// Rank: closer to |bandShift| first (best explanatory match), then
|
|
144
|
-
// smallest baseline.top (earliest divergence wins ties).
|
|
145
|
-
candidates.sort((a, b) =>
|
|
146
|
-
Math.abs(Math.abs(a.originDeltaY) - Math.abs(bandShift)) -
|
|
147
|
-
Math.abs(Math.abs(b.originDeltaY) - Math.abs(bandShift))
|
|
148
|
-
|| a.originBaselineTop - b.originBaselineTop,
|
|
149
|
-
);
|
|
150
|
-
origins.push(...candidates.slice(0, perBandLimit));
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return origins;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Browser-side capture: same path scheme as
|
|
158
|
-
* `dom-position-styles.DOM_POSITION_STYLES_BROWSER_SCRIPT`, returns bbox
|
|
159
|
-
* (top/left/width/height in document-coords) instead of computed styles.
|
|
160
|
-
*/
|
|
161
|
-
export const DOM_BBOX_BROWSER_SCRIPT = `JSON.stringify((function(){
|
|
162
|
-
var SEMANTIC = new Set([
|
|
163
|
-
"main","header","nav","footer","aside","article","section",
|
|
164
|
-
"h1","h2","h3","h4","h5","h6","p","a","button","input","select","textarea",
|
|
165
|
-
"label","blockquote","pre","code","table","thead","tbody","tr","th","td",
|
|
166
|
-
"ul","ol","li","img","span"
|
|
167
|
-
]);
|
|
168
|
-
var out = [];
|
|
169
|
-
function walk(el, parentPath, indexAmongChildren) {
|
|
170
|
-
var tag = el.tagName.toLowerCase();
|
|
171
|
-
var path = parentPath === "" ? tag + "[0]" : parentPath + ">" + tag + "[" + indexAmongChildren + "]";
|
|
172
|
-
var hasClass = el.hasAttribute && el.hasAttribute("class");
|
|
173
|
-
var interesting = hasClass || SEMANTIC.has(tag);
|
|
174
|
-
if (interesting) {
|
|
175
|
-
var r = el.getBoundingClientRect();
|
|
176
|
-
out.push({
|
|
177
|
-
path: path,
|
|
178
|
-
tag: tag,
|
|
179
|
-
classes: (el.getAttribute("class") || "").trim(),
|
|
180
|
-
top: r.top + window.scrollY,
|
|
181
|
-
left: r.left + window.scrollX,
|
|
182
|
-
width: r.width,
|
|
183
|
-
height: r.height
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
var children = el.children;
|
|
187
|
-
for (var j = 0; j < children.length; j++) {
|
|
188
|
-
walk(children[j], path, j);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
if (document.body) walk(document.body, "", 0);
|
|
192
|
-
return out;
|
|
193
|
-
})())`;
|
|
194
|
-
|
|
195
|
-
export function parseBboxes(value: unknown): BboxElement[] {
|
|
196
|
-
if (Array.isArray(value)) return value as BboxElement[];
|
|
197
|
-
if (typeof value !== "string") return [];
|
|
198
|
-
try {
|
|
199
|
-
const parsed = JSON.parse(value);
|
|
200
|
-
return Array.isArray(parsed) ? parsed as BboxElement[] : [];
|
|
201
|
-
} catch {
|
|
202
|
-
return [];
|
|
203
|
-
}
|
|
204
|
-
}
|
package/src/visual-semantic.ts
DELETED
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
VrtDiff,
|
|
3
|
-
DiffRegion,
|
|
4
|
-
VisualSemanticChange,
|
|
5
|
-
VisualSemanticDiff,
|
|
6
|
-
VisualChangeType,
|
|
7
|
-
} from "./types.ts";
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Convert VRT pixel diff into visual semantics.
|
|
11
|
-
*
|
|
12
|
-
* Analyzes each diff region to infer what changed:
|
|
13
|
-
* - text-change: text area change (wide, small region)
|
|
14
|
-
* - color-change: color-only change (same shape)
|
|
15
|
-
* - layout-shift: element repositioning (large region, correlated diffs)
|
|
16
|
-
* - element-added/removed: new/removed element
|
|
17
|
-
* - icon-change: small square region
|
|
18
|
-
*/
|
|
19
|
-
export function classifyVisualDiff(diff: VrtDiff): VisualSemanticDiff {
|
|
20
|
-
const changes: VisualSemanticChange[] = [];
|
|
21
|
-
|
|
22
|
-
for (const region of diff.regions) {
|
|
23
|
-
const classified = classifyRegion(region, diff);
|
|
24
|
-
changes.push(classified);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Group correlated layout-shifts
|
|
28
|
-
const grouped = groupLayoutShifts(changes);
|
|
29
|
-
|
|
30
|
-
return {
|
|
31
|
-
testId: diff.snapshot.testId,
|
|
32
|
-
changes: grouped,
|
|
33
|
-
summary: summarizeChanges(grouped),
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function classifyRegion(
|
|
38
|
-
region: DiffRegion,
|
|
39
|
-
diff: VrtDiff
|
|
40
|
-
): VisualSemanticChange {
|
|
41
|
-
const area = region.width * region.height;
|
|
42
|
-
const aspectRatio = region.width / Math.max(region.height, 1);
|
|
43
|
-
const density = region.diffPixelCount / area;
|
|
44
|
-
const globalRatio = region.diffPixelCount / diff.totalPixels;
|
|
45
|
-
|
|
46
|
-
// Small square -> icon change
|
|
47
|
-
if (
|
|
48
|
-
area < 4096 && // <= 64x64
|
|
49
|
-
aspectRatio > 0.5 &&
|
|
50
|
-
aspectRatio < 2.0
|
|
51
|
-
) {
|
|
52
|
-
return {
|
|
53
|
-
type: "icon-change",
|
|
54
|
-
region,
|
|
55
|
-
confidence: 0.7,
|
|
56
|
-
description: `Small square region changed at (${region.x}, ${region.y})`,
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// Wide and sparse -> text change
|
|
61
|
-
if (aspectRatio > 3.0 && density < 0.5) {
|
|
62
|
-
return {
|
|
63
|
-
type: "text-change",
|
|
64
|
-
region,
|
|
65
|
-
confidence: 0.6,
|
|
66
|
-
description: `Text-like region changed at (${region.x}, ${region.y}), ${region.width}x${region.height}`,
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// High density, small-medium area -> color change
|
|
71
|
-
if (density > 0.7 && area < diff.totalPixels * 0.3) {
|
|
72
|
-
return {
|
|
73
|
-
type: "color-change",
|
|
74
|
-
region,
|
|
75
|
-
confidence: 0.65,
|
|
76
|
-
description: `Color change in region (${region.x}, ${region.y}), ${region.width}x${region.height}, ${(density * 100).toFixed(0)}% density`,
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Large area -> layout shift
|
|
81
|
-
if (globalRatio > 0.05) {
|
|
82
|
-
return {
|
|
83
|
-
type: "layout-shift",
|
|
84
|
-
region,
|
|
85
|
-
confidence: 0.5,
|
|
86
|
-
description: `Layout shift at (${region.x}, ${region.y}), ${region.width}x${region.height}, ${(globalRatio * 100).toFixed(1)}% of total`,
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// New element (high density + medium size)
|
|
91
|
-
if (density > 0.5 && area > 1024) {
|
|
92
|
-
return {
|
|
93
|
-
type: "element-added",
|
|
94
|
-
region,
|
|
95
|
-
confidence: 0.4,
|
|
96
|
-
description: `New element appeared at (${region.x}, ${region.y}), ${region.width}x${region.height}`,
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Default: element-added
|
|
101
|
-
return {
|
|
102
|
-
type: "element-added",
|
|
103
|
-
region,
|
|
104
|
-
confidence: 0.3,
|
|
105
|
-
description: `Change at (${region.x}, ${region.y}), ${region.width}x${region.height}`,
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Group layout-shifts with close Y coordinates.
|
|
111
|
-
* Multiple regions shifting on the same row = one layout shift.
|
|
112
|
-
*/
|
|
113
|
-
function groupLayoutShifts(
|
|
114
|
-
changes: VisualSemanticChange[]
|
|
115
|
-
): VisualSemanticChange[] {
|
|
116
|
-
const layoutShifts = changes.filter((c) => c.type === "layout-shift");
|
|
117
|
-
const others = changes.filter((c) => c.type !== "layout-shift");
|
|
118
|
-
|
|
119
|
-
if (layoutShifts.length <= 1) return changes;
|
|
120
|
-
|
|
121
|
-
// Sort by Y coordinate and group nearby ones
|
|
122
|
-
layoutShifts.sort((a, b) => a.region.y - b.region.y);
|
|
123
|
-
|
|
124
|
-
const groups: VisualSemanticChange[][] = [];
|
|
125
|
-
let currentGroup: VisualSemanticChange[] = [layoutShifts[0]];
|
|
126
|
-
|
|
127
|
-
for (let i = 1; i < layoutShifts.length; i++) {
|
|
128
|
-
const prev = currentGroup[currentGroup.length - 1];
|
|
129
|
-
const curr = layoutShifts[i];
|
|
130
|
-
// Within 64px Y distance -> same group
|
|
131
|
-
if (Math.abs(curr.region.y - prev.region.y) < 64) {
|
|
132
|
-
currentGroup.push(curr);
|
|
133
|
-
} else {
|
|
134
|
-
groups.push(currentGroup);
|
|
135
|
-
currentGroup = [curr];
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
groups.push(currentGroup);
|
|
139
|
-
|
|
140
|
-
// Merge each group into a single change
|
|
141
|
-
const merged = groups.map((group): VisualSemanticChange => {
|
|
142
|
-
if (group.length === 1) return group[0];
|
|
143
|
-
|
|
144
|
-
const minX = Math.min(...group.map((c) => c.region.x));
|
|
145
|
-
const minY = Math.min(...group.map((c) => c.region.y));
|
|
146
|
-
const maxX = Math.max(
|
|
147
|
-
...group.map((c) => c.region.x + c.region.width)
|
|
148
|
-
);
|
|
149
|
-
const maxY = Math.max(
|
|
150
|
-
...group.map((c) => c.region.y + c.region.height)
|
|
151
|
-
);
|
|
152
|
-
const totalDiff = group.reduce(
|
|
153
|
-
(sum, c) => sum + c.region.diffPixelCount,
|
|
154
|
-
0
|
|
155
|
-
);
|
|
156
|
-
|
|
157
|
-
return {
|
|
158
|
-
type: "layout-shift",
|
|
159
|
-
region: {
|
|
160
|
-
x: minX,
|
|
161
|
-
y: minY,
|
|
162
|
-
width: maxX - minX,
|
|
163
|
-
height: maxY - minY,
|
|
164
|
-
diffPixelCount: totalDiff,
|
|
165
|
-
},
|
|
166
|
-
confidence: Math.max(...group.map((c) => c.confidence)),
|
|
167
|
-
description: `Layout shift spanning ${group.length} regions at y=${minY}-${maxY}`,
|
|
168
|
-
};
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
return [...others, ...merged];
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
function summarizeChanges(changes: VisualSemanticChange[]): string {
|
|
175
|
-
const byType = new Map<VisualChangeType, number>();
|
|
176
|
-
for (const c of changes) {
|
|
177
|
-
byType.set(c.type, (byType.get(c.type) ?? 0) + 1);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const parts: string[] = [];
|
|
181
|
-
for (const [type, count] of byType) {
|
|
182
|
-
parts.push(`${count} ${type}`);
|
|
183
|
-
}
|
|
184
|
-
return parts.join(", ") || "no changes";
|
|
185
|
-
}
|