@mizchi/vlmkit-core 0.7.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 (65) 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/terminal-colors.ts +0 -13
  65. package/src/text-rows.ts +0 -412
package/src/text-rows.ts DELETED
@@ -1,412 +0,0 @@
1
- /**
2
- * Text-row y-position extraction.
3
- *
4
- * Subagent F's wish-list item: "I need 'the `$24` text row is at y=420
5
- * in the reference but y=380 in mine' as a first-class signal — without
6
- * needing DOM correspondence."
7
- *
8
- * Compute the per-row mean luminance of an RGBA image. Text rows are
9
- * darker than the surrounding whitespace (assuming light background;
10
- * the dark-bg case is handled by symmetry: we just look for *dips*
11
- * relative to the local average, regardless of polarity). Group
12
- * consecutive dark rows into "text bands" and report each band's
13
- * y-center.
14
- *
15
- * Pair baseline rows with variant rows by order (band #N in baseline ↔
16
- * band #N in variant) and emit Δy. When the agent's rendering has the
17
- * right *number* of text rows but wrong y-positions, this surfaces the
18
- * shift directly.
19
- */
20
- import { PNG } from "pngjs";
21
- import { readFile } from "node:fs/promises";
22
-
23
- export interface TextRow {
24
- /** Optional OCR/debug text when a caller can supply it. Pixel extraction does not infer this. */
25
- text?: string;
26
- /** Y-coordinate of the row's vertical center. */
27
- yCenter: number;
28
- /** Top y of the run of dark rows (inclusive). */
29
- yStart: number;
30
- /** Bottom y of the run of dark rows (inclusive). */
31
- yEnd: number;
32
- /** Mean luminance across the band (0-255). */
33
- meanLuma: number;
34
- /** Approximate height in pixels. */
35
- height: number;
36
- /**
37
- * Estimated font-size in px, derived from band height. Typical UI
38
- * fonts have a band height ≈ 0.75 × fontSize (the band spans
39
- * baseline to ascender; descenders push it a bit further). Rounded
40
- * to the nearest common UI size (12, 14, 16, 18, 20, 24, …, 72).
41
- */
42
- estimatedFontSize?: number;
43
- /**
44
- * Ink density inside the text's horizontal bounding box. Computed
45
- * as (dark pixels with luma < 100) / (pixels in text-bbox). Larger
46
- * values = thicker strokes = bolder weight. Empirical buckets:
47
- * - < 0.10 → regular / light
48
- * - 0.10-0.16 → medium / semibold
49
- * - > 0.16 → bold / heavy
50
- * Noisy on short text — compare same-content baseline vs variant
51
- * for high-confidence weight-mismatch detection.
52
- */
53
- inkDensity?: number;
54
- /** Bucketed weight class — heuristic, see `inkDensity` doc. */
55
- weightBucket?: "light" | "regular" | "medium" | "bold";
56
- /** Horizontal extent of ink within the band (px from left edge of image). */
57
- inkLeft?: number;
58
- inkRight?: number;
59
- }
60
-
61
- export interface MatchedTextRow {
62
- /** Position in the ordered list of detected rows (0 = topmost). */
63
- rank: number;
64
- baseline: TextRow;
65
- variant: TextRow;
66
- /** variant.yCenter − baseline.yCenter (px). */
67
- deltaY: number;
68
- }
69
-
70
- export interface RowGapDelta {
71
- /** Rank of the row above the gap. */
72
- aboveRank: number;
73
- /** Rank of the row below the gap. */
74
- belowRank: number;
75
- /** Baseline gap (yCenter[below] − yCenter[above]). */
76
- baselineGap: number;
77
- /** Variant gap. */
78
- variantGap: number;
79
- /** variantGap − baselineGap; positive means the gap grew. */
80
- delta: number;
81
- }
82
-
83
- export interface ExtractTextRowsOptions {
84
- /**
85
- * Minimum dip in mean luminance (vs. the median row luminance) for a
86
- * row to be considered "text". Default 12 — picks up body text but
87
- * ignores subpixel-rendering noise.
88
- */
89
- minLumaDip?: number;
90
- /** Minimum band height in px. Default 4. */
91
- minBandHeight?: number;
92
- /** Maximum bands to return (top-N by yCenter, i.e. all of them). */
93
- maxBands?: number;
94
- }
95
-
96
- const DEFAULT_MIN_LUMA_DIP = 12;
97
- const DEFAULT_MIN_BAND_HEIGHT = 4;
98
- const DEFAULT_MAX_BANDS = 64;
99
-
100
- interface RowStats {
101
- mean: Float32Array;
102
- range: Float32Array; // max − min per row; high for text rows (dark on light bg).
103
- }
104
-
105
- function rowStats(data: Uint8Array, width: number, height: number): RowStats {
106
- const mean = new Float32Array(height);
107
- const range = new Float32Array(height);
108
- for (let y = 0; y < height; y++) {
109
- let sum = 0, mn = 255, mx = 0;
110
- const rowStart = y * width * 4;
111
- for (let x = 0; x < width; x++) {
112
- const idx = rowStart + x * 4;
113
- // Rec.601 luma.
114
- const L = 0.299 * data[idx]! + 0.587 * data[idx + 1]! + 0.114 * data[idx + 2]!;
115
- sum += L;
116
- if (L < mn) mn = L;
117
- if (L > mx) mx = L;
118
- }
119
- mean[y] = sum / width;
120
- range[y] = mx - mn;
121
- }
122
- return { mean, range };
123
- }
124
-
125
- function median(values: Float32Array): number {
126
- if (values.length === 0) return 0;
127
- const sorted = Float32Array.from(values).sort();
128
- const mid = sorted.length >> 1;
129
- return sorted.length % 2 ? sorted[mid]! : (sorted[mid - 1]! + sorted[mid]!) / 2;
130
- }
131
-
132
- /** Snap a raw px height to the nearest common UI font-size bucket. */
133
- function snapFontSize(rawPx: number): number {
134
- const buckets = [10, 11, 12, 13, 14, 16, 18, 20, 22, 24, 28, 32, 36, 40, 48, 56, 64, 72];
135
- let best = buckets[0]!;
136
- let bestDist = Math.abs(rawPx - best);
137
- for (const b of buckets) {
138
- const d = Math.abs(rawPx - b);
139
- if (d < bestDist) { best = b; bestDist = d; }
140
- }
141
- return best;
142
- }
143
-
144
- function bucketWeight(density: number): TextRow["weightBucket"] {
145
- // Empirical calibration against system-ui at 12-48 px:
146
- // regular text: density 0.15-0.25
147
- // medium (500-600): 0.25-0.35
148
- // bold (700+): > 0.35
149
- // Small text has slightly elevated density due to proportional AA
150
- // blur — the buckets accept that noise.
151
- if (density < 0.15) return "light";
152
- if (density < 0.27) return "regular";
153
- if (density < 0.36) return "medium";
154
- return "bold";
155
- }
156
-
157
- /**
158
- * Compute typography hints (font-size estimate, ink density, weight
159
- * bucket) for a band by sampling its pixels. Mutates the band in
160
- * place. From subagent G v3 dogfood: "next blocker is sub-pixel
161
- * vertical spacing tuning — and a font-size / weight estimate per
162
- * heatmap region would push this to < 1% in one more round."
163
- */
164
- function annotateTypography(
165
- band: TextRow,
166
- data: Uint8Array,
167
- width: number,
168
- height: number,
169
- ): void {
170
- // 1. Find the horizontal extent of ink within the band. Use an
171
- // *adaptive* threshold so muted-gray text (e.g. #9ca3af on white,
172
- // luma ≈ 165) is still detected. A pixel is "ink" if its luma is
173
- // at least 50 below the band's background luma — `meanLuma` is a
174
- // good proxy for the band's background since text occupies a
175
- // small fraction of the band's area.
176
- const inkThreshold = Math.max(40, band.meanLuma - 50);
177
- let inkLeft = width, inkRight = -1;
178
- let inkPixels = 0;
179
- for (let y = band.yStart; y <= band.yEnd && y < height; y++) {
180
- for (let x = 0; x < width; x++) {
181
- const i = (y * width + x) * 4;
182
- const L = 0.299 * data[i]! + 0.587 * data[i + 1]! + 0.114 * data[i + 2]!;
183
- if (L < inkThreshold) {
184
- if (x < inkLeft) inkLeft = x;
185
- if (x > inkRight) inkRight = x;
186
- inkPixels++;
187
- }
188
- }
189
- }
190
- if (inkRight < inkLeft) return;
191
-
192
- // 2. Ink density = ink pixels / area of the text bbox.
193
- const inkWidth = inkRight - inkLeft + 1;
194
- const inkArea = inkWidth * (band.yEnd - band.yStart + 1);
195
- const density = inkArea > 0 ? inkPixels / inkArea : 0;
196
-
197
- // 3. Estimate font-size from band height. Calibrated empirically
198
- // against system-ui at 12-48px: band height ≈ 0.92× fontSize
199
- // (includes ascender + descender + AA blur). Divide to recover.
200
- const bandH = band.height;
201
- const rawFontSize = bandH / 0.92;
202
- band.estimatedFontSize = snapFontSize(rawFontSize);
203
- band.inkDensity = density;
204
- band.weightBucket = bucketWeight(density);
205
- band.inkLeft = inkLeft;
206
- band.inkRight = inkRight;
207
- }
208
-
209
- export function extractTextRowsFromRgba(
210
- data: Uint8Array,
211
- width: number,
212
- height: number,
213
- options: ExtractTextRowsOptions = {},
214
- ): TextRow[] {
215
- if (width <= 0 || height <= 0) return [];
216
-
217
- const minDip = options.minLumaDip ?? DEFAULT_MIN_LUMA_DIP;
218
- const minBandH = options.minBandHeight ?? DEFAULT_MIN_BAND_HEIGHT;
219
- const maxBands = options.maxBands ?? DEFAULT_MAX_BANDS;
220
- // Minimum max-min range for a row to count as "ink-bearing". Text on
221
- // a light bg has a row range of ≥ ~150 even when the text covers a
222
- // small horizontal slice (the row contains both bg-luma ≈ 245 pixels
223
- // and text-luma ≈ 30 pixels). Solid backgrounds have near-zero range.
224
- // Set conservatively to skip subpixel AA noise (range < 30 happens
225
- // on bands with thin horizontal lines).
226
- const MIN_INK_RANGE = 80;
227
-
228
- const { mean: rowMeans, range: rowRanges } = rowStats(data, width, height);
229
- const med = median(rowMeans);
230
- // A row is content if EITHER:
231
- // (a) the row mean drops at least `minDip` below the median row
232
- // mean — catches solid-fill bands (buttons, banners) that span
233
- // the full width and pull the average down.
234
- // (b) the row's max-min range exceeds `MIN_INK_RANGE` — catches
235
- // thin text-on-bg bands where bg luma dominates the mean but
236
- // a few dark text pixels pull the row min way down.
237
- // Subagent G dogfood: prior version used (a) only and missed
238
- // heading / price / feature rows on pricing-card targets because
239
- // they don't pull the full-width mean below threshold.
240
- const bands: TextRow[] = [];
241
- let runStart = -1;
242
- let runSum = 0;
243
- for (let y = 0; y < height; y++) {
244
- const meanDip = med - rowMeans[y]! >= minDip;
245
- const hasInk = rowRanges[y]! >= MIN_INK_RANGE;
246
- const isContent = meanDip || hasInk;
247
- if (isContent) {
248
- if (runStart < 0) {
249
- runStart = y;
250
- runSum = 0;
251
- }
252
- runSum += rowMeans[y]!;
253
- } else if (runStart >= 0) {
254
- const bandH = y - runStart;
255
- if (bandH >= minBandH) {
256
- bands.push({
257
- yStart: runStart,
258
- yEnd: y - 1,
259
- yCenter: Math.round((runStart + y - 1) / 2),
260
- height: bandH,
261
- meanLuma: runSum / bandH,
262
- });
263
- }
264
- runStart = -1;
265
- }
266
- }
267
- if (runStart >= 0) {
268
- const bandH = height - runStart;
269
- if (bandH >= minBandH) {
270
- bands.push({
271
- yStart: runStart,
272
- yEnd: height - 1,
273
- yCenter: Math.round((runStart + height - 1) / 2),
274
- height: bandH,
275
- meanLuma: runSum / bandH,
276
- });
277
- }
278
- }
279
- const capped = bands.slice(0, maxBands);
280
- for (const band of capped) annotateTypography(band, data, width, height);
281
- return capped;
282
- }
283
-
284
- export async function extractTextRowsFromFile(
285
- path: string,
286
- options: ExtractTextRowsOptions = {},
287
- ): Promise<TextRow[]> {
288
- const buf = await readFile(path);
289
- const png = PNG.sync.read(buf);
290
- return extractTextRowsFromRgba(png.data, png.width, png.height, options);
291
- }
292
-
293
- /**
294
- * Pair text rows by ordered index. When baseline has 6 rows and variant
295
- * has 6 rows, this maps row #0 → row #0, #1 → #1, etc. When the counts
296
- * differ, only the prefix common to both is matched.
297
- *
298
- * Reports rows with |Δy| above `minDeltaY` to keep the table focused on
299
- * actionable shifts (small subpixel deltas are excluded by default).
300
- */
301
- export function matchTextRows(
302
- baseline: TextRow[],
303
- variant: TextRow[],
304
- minDeltaY = 2,
305
- ): MatchedTextRow[] {
306
- const out: MatchedTextRow[] = [];
307
- const n = Math.min(baseline.length, variant.length);
308
- for (let i = 0; i < n; i++) {
309
- const dy = variant[i]!.yCenter - baseline[i]!.yCenter;
310
- if (Math.abs(dy) >= minDeltaY) {
311
- out.push({
312
- rank: i,
313
- baseline: baseline[i]!,
314
- variant: variant[i]!,
315
- deltaY: dy,
316
- });
317
- }
318
- }
319
- return out;
320
- }
321
-
322
- /**
323
- * Compute per-pair gap deltas from baseline ↔ variant row matches.
324
- * The gap above row N+1 = `row[N+1].yCenter − row[N].yCenter` — the
325
- * vertical distance between consecutive text bands. If the gap
326
- * differs between baseline and variant, the spacing between those
327
- * two pieces of content is wrong, and the fix is on the preceding
328
- * element's `margin-bottom` / `padding-bottom`.
329
- *
330
- * Subagent G v3 dogfood: "once IoU > 0.98, the remaining ~1.7%
331
- * diff is text baseline Δy in the 2-19px range, and the report
332
- * doesn't yet suggest which CSS knob to turn." Gap deltas tell
333
- * the agent exactly which margin to adjust.
334
- */
335
- export interface TypographyMismatch {
336
- rank: number;
337
- baselineFontSize?: number;
338
- variantFontSize?: number;
339
- baselineWeight?: TextRow["weightBucket"];
340
- variantWeight?: TextRow["weightBucket"];
341
- baselineDensity?: number;
342
- variantDensity?: number;
343
- /** "size" | "weight" | "both" */
344
- kind: "size" | "weight" | "both";
345
- }
346
-
347
- /**
348
- * Compare typography hints across ordered baseline ↔ variant pairs.
349
- * Reports rows where:
350
- * - estimated font-size buckets differ (different snapped px), OR
351
- * - weight buckets differ ("regular" vs "bold").
352
- *
353
- * Same-content assumption: when baseline and variant render the same
354
- * text content, ink density is directly comparable. When content
355
- * differs, density can vary by character makeup ("iii" vs "WWW")
356
- * and the comparison is noisier — only large bucket jumps are
357
- * surfaced.
358
- */
359
- export function compareRowTypography(
360
- baseline: TextRow[],
361
- variant: TextRow[],
362
- minDensityDelta = 0.04,
363
- ): TypographyMismatch[] {
364
- const n = Math.min(baseline.length, variant.length);
365
- const out: TypographyMismatch[] = [];
366
- for (let i = 0; i < n; i++) {
367
- const b = baseline[i]!, v = variant[i]!;
368
- if (b.estimatedFontSize === undefined || v.estimatedFontSize === undefined) continue;
369
- const sizeDiffers = b.estimatedFontSize !== v.estimatedFontSize;
370
- // Weight differs if buckets differ AND the underlying density
371
- // delta is larger than the threshold (avoids flagging
372
- // "regular vs medium" on a marginal-density pair).
373
- const densityDelta = Math.abs((b.inkDensity ?? 0) - (v.inkDensity ?? 0));
374
- const weightDiffers = b.weightBucket !== v.weightBucket && densityDelta >= minDensityDelta;
375
- if (!sizeDiffers && !weightDiffers) continue;
376
- out.push({
377
- rank: i,
378
- baselineFontSize: b.estimatedFontSize,
379
- variantFontSize: v.estimatedFontSize,
380
- baselineWeight: b.weightBucket,
381
- variantWeight: v.weightBucket,
382
- baselineDensity: b.inkDensity,
383
- variantDensity: v.inkDensity,
384
- kind: sizeDiffers && weightDiffers ? "both" : sizeDiffers ? "size" : "weight",
385
- });
386
- }
387
- return out;
388
- }
389
-
390
- export function computeRowGapDeltas(
391
- baseline: TextRow[],
392
- variant: TextRow[],
393
- minAbsoluteDelta = 2,
394
- ): RowGapDelta[] {
395
- const n = Math.min(baseline.length, variant.length);
396
- const out: RowGapDelta[] = [];
397
- for (let i = 0; i < n - 1; i++) {
398
- const baseGap = baseline[i + 1]!.yCenter - baseline[i]!.yCenter;
399
- const varGap = variant[i + 1]!.yCenter - variant[i]!.yCenter;
400
- const delta = varGap - baseGap;
401
- if (Math.abs(delta) >= minAbsoluteDelta) {
402
- out.push({
403
- aboveRank: i,
404
- belowRank: i + 1,
405
- baselineGap: baseGap,
406
- variantGap: varGap,
407
- delta,
408
- });
409
- }
410
- }
411
- return out;
412
- }