@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,112 +0,0 @@
1
- /**
2
- * Image resize + VLM optimization
3
- *
4
- * Downscale images before sending to VLM to reduce token cost.
5
- * Supports auto-escalation when resolution is insufficient.
6
- */
7
- import { PNG } from "pngjs";
8
-
9
- export type ResolutionPreset = "low" | "medium" | "high" | "full";
10
-
11
- export const RESOLUTION_PRESETS: Record<ResolutionPreset, { maxWidth: number; maxHeight: number }> = {
12
- low: { maxWidth: 375, maxHeight: 320 }, // mobile viewport width. ~130 tokens
13
- medium: { maxWidth: 640, maxHeight: 480 }, // breakpoint boundary. ~200 tokens
14
- high: { maxWidth: 1280, maxHeight: 900 }, // desktop viewport. ~500 tokens
15
- full: { maxWidth: 4096, maxHeight: 4096 }, // original size
16
- };
17
-
18
- /**
19
- * Select the optimal resolution preset for a given viewport width.
20
- * Returns the smallest preset with at least half the viewport width.
21
- */
22
- export function resolveResolutionForViewport(
23
- viewportWidth: number,
24
- maxPreset: ResolutionPreset = "high",
25
- ): ResolutionPreset {
26
- const order: ResolutionPreset[] = ["low", "medium", "high", "full"];
27
- const maxIdx = order.indexOf(maxPreset);
28
-
29
- for (let i = 0; i <= maxIdx; i++) {
30
- const preset = RESOLUTION_PRESETS[order[i]];
31
- // Sufficient if image width >= half of viewport
32
- if (preset.maxWidth >= viewportWidth / 2) return order[i];
33
- }
34
-
35
- return maxPreset;
36
- }
37
-
38
- export interface ResizeOptions {
39
- /** Preset or custom size */
40
- resolution?: ResolutionPreset | { maxWidth: number; maxHeight: number };
41
- }
42
-
43
- /** Resize PNG buffer to fit within the given size. Preserves aspect ratio. */
44
- export function resizePngBuffer(pngBuffer: Buffer, options: ResizeOptions = {}): Buffer {
45
- const preset = typeof options.resolution === "string"
46
- ? RESOLUTION_PRESETS[options.resolution]
47
- : options.resolution ?? RESOLUTION_PRESETS.medium;
48
-
49
- const src = PNG.sync.read(pngBuffer);
50
-
51
- if (src.width <= preset.maxWidth && src.height <= preset.maxHeight) {
52
- return pngBuffer; // already small enough
53
- }
54
-
55
- const scale = Math.min(preset.maxWidth / src.width, preset.maxHeight / src.height);
56
- const targetW = Math.round(src.width * scale);
57
- const targetH = Math.round(src.height * scale);
58
-
59
- const dst = new PNG({ width: targetW, height: targetH });
60
- const xRatio = src.width / targetW;
61
- const yRatio = src.height / targetH;
62
-
63
- for (let y = 0; y < targetH; y++) {
64
- for (let x = 0; x < targetW; x++) {
65
- const srcX = Math.min(Math.floor(x * xRatio), src.width - 1);
66
- const srcY = Math.min(Math.floor(y * yRatio), src.height - 1);
67
- const si = (srcY * src.width + srcX) * 4;
68
- const di = (y * targetW + x) * 4;
69
- dst.data[di] = src.data[si];
70
- dst.data[di + 1] = src.data[si + 1];
71
- dst.data[di + 2] = src.data[si + 2];
72
- dst.data[di + 3] = src.data[si + 3];
73
- }
74
- }
75
-
76
- return Buffer.from(PNG.sync.write(dst));
77
- }
78
-
79
- /** Read dimensions from PNG IHDR chunk (no full decode needed) */
80
- export function readPngDimensions(buf: Buffer): { width: number; height: number } {
81
- // PNG header: 8-byte signature, then IHDR chunk: 4-byte length, 4-byte type, 4-byte width, 4-byte height
82
- if (buf.length >= 24 && buf[0] === 0x89 && buf[1] === 0x50) {
83
- return { width: buf.readUInt32BE(16), height: buf.readUInt32BE(20) };
84
- }
85
- // Fallback: full decode
86
- const png = PNG.sync.read(buf);
87
- return { width: png.width, height: png.height };
88
- }
89
-
90
- /** Resize a base64 PNG to the specified resolution. Returns base64. */
91
- export function resizeBase64Png(base64: string, options: ResizeOptions = {}): string {
92
- const preset = typeof options.resolution === "string"
93
- ? RESOLUTION_PRESETS[options.resolution]
94
- : options.resolution ?? RESOLUTION_PRESETS.medium;
95
-
96
- const buf = Buffer.from(base64, "base64");
97
- const { width, height } = readPngDimensions(buf);
98
-
99
- // Skip round-trip if already small enough
100
- if (width <= preset.maxWidth && height <= preset.maxHeight) {
101
- return base64;
102
- }
103
-
104
- const resized = resizePngBuffer(buf, options);
105
- return resized.toString("base64");
106
- }
107
-
108
- /** Get image dimensions */
109
- export function getImageDimensions(base64: string): { width: number; height: number } {
110
- const buf = Buffer.from(base64, "base64");
111
- return readPngDimensions(buf);
112
- }
package/src/index.ts DELETED
@@ -1,105 +0,0 @@
1
- /**
2
- * @mizchi/vlmkit-core — VRT diff engine + shared types
3
- *
4
- * This barrel is the curated public API. Anything not re-exported here
5
- * is internal — either a browser-context helper (designed to run inside
6
- * `page.evaluate(...)`), a CLI helper, or a low-level parser. Those
7
- * remain accessible via deep import (`@mizchi/vlmkit-core/<module>.ts`)
8
- * but aren't part of the stable surface.
9
- *
10
- * Naming groups (informal):
11
- *
12
- * - `compareScreenshots`, `runPngDiff`, `generateDiffReport`
13
- * → top-level diff entry points.
14
- * - `find{HeatmapRegions,ShiftOrigins,GridSuggestions}`,
15
- * `detect{BandShifts,Whiteout,EmptyContent}`
16
- * → detectors that return structured findings.
17
- * - `diff{A11yTrees,ComputedStyles,DomPositionStyles}`,
18
- * `classify{Region,VisualDiff}`, `evaluateDomEquivalence`
19
- * → structural comparisons.
20
- * - `extractTextRows*`, `matchTextRows`, `compareRowTypography`
21
- * → typography / text-row primitives.
22
- * - `{decode,encode,crop,resize}Png*`, `getImageDimensions`
23
- * → image-IO helpers.
24
- * - `INTERACTIVE_ROLES`, `LANDMARK_ROLES`, `RESOLUTION_PRESETS`,
25
- * `TRACKED_PROPERTIES` → public config constants.
26
- * - `runQualityChecks`
27
- * → end-to-end quality gate.
28
- *
29
- * Modules NOT in the barrel (deep import only):
30
- * - terminal-colors, cli-args, cli-error — CLI helpers
31
- * - element-compare, mask — require Playwright at module load
32
- * - a11y-contrast, a11y-touch, a11y-focus-order — CLI entries
33
- * - browser-script string constants & `*InDom` helpers
34
- */
35
-
36
- // ---- Types (open re-export — types don't pollute the value namespace) ----
37
- export * from "./types.ts";
38
-
39
- // ---- Image diff ----
40
- export {
41
- compareScreenshots,
42
- detectBandShifts,
43
- generateDiffReport,
44
- detectWhiteout,
45
- detectEmptyContent,
46
- } from "./heatmap.ts";
47
- export { findHeatmapRegionsFromFile, findHeatmapRegionsFromRgba } from "./heatmap-regions.ts";
48
- export {
49
- createScopedVrtDiff,
50
- normalizeVrtDiffRegions,
51
- normalizeVrtDiffRegionPixels,
52
- } from "./diff-regions.ts";
53
-
54
- // ---- Layout / typography ----
55
- export {
56
- extractTextRowsFromFile,
57
- extractTextRowsFromRgba,
58
- matchTextRows,
59
- compareRowTypography,
60
- computeRowGapDeltas,
61
- } from "./text-rows.ts";
62
-
63
- // ---- PNG IO ----
64
- export {
65
- cropImage,
66
- decodePng,
67
- encodePng,
68
- } from "./png-utils.ts";
69
- export {
70
- resizePngBuffer,
71
- resolveResolutionForViewport,
72
- getImageDimensions,
73
- } from "./image-resize.ts";
74
-
75
- // ---- DOM diff ----
76
- export {
77
- verifyDomEquivalence,
78
- /** @deprecated since 0.5.0 — use `verifyDomEquivalence`. */
79
- evaluateDomEquivalence,
80
- } from "./dom-equivalence.ts";
81
- export {
82
- diffDomPositionStyles,
83
- diffPositionStylesAcrossViewports,
84
- } from "./dom-position-styles.ts";
85
- export { diffComputedStyles } from "./computed-style-diff.ts";
86
- export {
87
- diffAuthoredStyles,
88
- aggregateAuthoredStyleByViewport,
89
- } from "./authored-style-diff.ts";
90
-
91
- // ---- A11y diff ----
92
- export {
93
- diffA11yTrees,
94
- parsePlaywrightA11ySnapshot,
95
- verifyA11yTree,
96
- /** @deprecated since 0.5.0 — use `verifyA11yTree`. */
97
- checkA11yTree,
98
- INTERACTIVE_ROLES,
99
- LANDMARK_ROLES,
100
- } from "./a11y-semantic.ts";
101
-
102
- // ---- Public config constants ----
103
- export { RESOLUTION_PRESETS } from "./image-resize.ts";
104
- export { TRACKED_PROPERTIES } from "./computed-style-capture.ts";
105
- export { AUTHORED_PROPERTIES } from "./authored-style-capture.ts";
package/src/mask.ts DELETED
@@ -1,41 +0,0 @@
1
- /**
2
- * VRT Mask -- hide specific selectors before screenshotting.
3
- *
4
- * Uses visibility: hidden to preserve layout while hiding rendering.
5
- * Prevents false positives from dynamic content (counters, animations, ads).
6
- */
7
- import type { Page } from "playwright";
8
-
9
- /**
10
- * Inject mask styles into the page.
11
- * Sets visibility: hidden on target selectors including descendants.
12
- */
13
- export async function applyMask(page: Page, selectors: string[]): Promise<void> {
14
- if (selectors.length === 0) return;
15
-
16
- const css = selectors
17
- .map((s) => `${s} { visibility: hidden !important; }`)
18
- .join("\n");
19
-
20
- await page.addStyleTag({ content: css });
21
- }
22
-
23
- /**
24
- * Parse selector array from CLI --mask flags.
25
- * Supports comma-separated or multiple --mask flags.
26
- *
27
- * --mask ".stars,.carousel"
28
- * --mask ".stars" --mask ".carousel"
29
- */
30
- export function parseMaskSelectors(args: string[]): string[] {
31
- const selectors: string[] = [];
32
- for (let i = 0; i < args.length; i++) {
33
- if (args[i] === "--mask" && args[i + 1]) {
34
- for (const s of args[i + 1].split(",")) {
35
- const trimmed = s.trim();
36
- if (trimmed) selectors.push(trimmed);
37
- }
38
- }
39
- }
40
- return selectors;
41
- }
package/src/png-utils.ts DELETED
@@ -1,86 +0,0 @@
1
- /**
2
- * PNG I/O utilities
3
- *
4
- * Low-level PNG decode/encode using pngjs.
5
- * Used by heatmap.ts and other modules that need raw pixel data.
6
- */
7
- import { readFile, writeFile } from "node:fs/promises";
8
-
9
- export interface PngData {
10
- width: number;
11
- height: number;
12
- data: Uint8Array;
13
- }
14
-
15
- /**
16
- * Crop or pad an image to the target dimensions.
17
- * Only handles the common region; overflow is zero-filled.
18
- */
19
- export function cropImage(img: PngData, w: number, h: number): PngData {
20
- if (img.width === w && img.height === h) return img;
21
- const data = new Uint8Array(w * h * 4);
22
- for (let y = 0; y < h; y++) {
23
- const srcOffset = y * img.width * 4;
24
- const dstOffset = y * w * 4;
25
- data.set(img.data.subarray(srcOffset, srcOffset + w * 4), dstOffset);
26
- }
27
- return { width: w, height: h, data };
28
- }
29
-
30
- /**
31
- * Crop an arbitrary rectangle out of an image.
32
- *
33
- * Unlike {@link cropImage} (which only clips the top-left common region),
34
- * this extracts a region at an arbitrary (x, y) offset. The rectangle is
35
- * clamped to the image bounds, so the returned crop may be smaller than
36
- * requested when the region overruns an edge (or off the top-left).
37
- */
38
- export function cropRegion(
39
- img: PngData,
40
- x: number,
41
- y: number,
42
- w: number,
43
- h: number,
44
- ): PngData {
45
- const x0 = Math.max(0, Math.min(Math.floor(x), img.width));
46
- const y0 = Math.max(0, Math.min(Math.floor(y), img.height));
47
- const x1 = Math.max(x0, Math.min(Math.floor(x + w), img.width));
48
- const y1 = Math.max(y0, Math.min(Math.floor(y + h), img.height));
49
- const cw = x1 - x0;
50
- const ch = y1 - y0;
51
- const data = new Uint8Array(cw * ch * 4);
52
- for (let row = 0; row < ch; row++) {
53
- const srcOffset = ((y0 + row) * img.width + x0) * 4;
54
- const dstOffset = row * cw * 4;
55
- data.set(img.data.subarray(srcOffset, srcOffset + cw * 4), dstOffset);
56
- }
57
- return { width: cw, height: ch, data };
58
- }
59
-
60
- /**
61
- * Read a PNG file and return RGBA pixel data.
62
- */
63
- export async function decodePng(path: string): Promise<PngData> {
64
- const { PNG } = await import("pngjs");
65
- const buffer = await readFile(path);
66
- const png = PNG.sync.read(buffer);
67
- return {
68
- width: png.width,
69
- height: png.height,
70
- data: new Uint8Array(png.data.buffer, png.data.byteOffset, png.data.byteLength),
71
- };
72
- }
73
-
74
- /**
75
- * Write RGBA pixel data to a PNG file.
76
- */
77
- export async function encodePng(
78
- path: string,
79
- data: PngData
80
- ): Promise<void> {
81
- const { PNG } = await import("pngjs");
82
- const png = new PNG({ width: data.width, height: data.height });
83
- Buffer.from(data.data.buffer, data.data.byteOffset, data.data.byteLength).copy(png.data);
84
- const buffer = PNG.sync.write(png);
85
- await writeFile(path, buffer);
86
- }
@@ -1,173 +0,0 @@
1
- /**
2
- * Per-region translation detection.
3
- *
4
- * Both A/B validation runs (2026-06-05 / 06-06) showed agents
5
- * misreading position shifts as color changes: the diff region for a
6
- * moved element samples near-identical colors on both sides
7
- * ("#ffffff -> #ffffff") and the semantic classifier falls back to
8
- * `element-added`. This module estimates whether a diff region is
9
- * better explained by a translation, using mean-subtracted normalized
10
- * cross-correlation of luminance profiles per axis — the same idea as
11
- * `detectBandShifts`, applied to a single region.
12
- */
13
- import type { PngData } from "./png-utils.ts";
14
-
15
- export interface RegionShiftEstimate {
16
- /** Content moved by +dx px horizontally in current (vs baseline). */
17
- dx: number;
18
- /** Content moved by +dy px vertically in current (vs baseline). */
19
- dy: number;
20
- /** 0..1; correlation peak strength of the dominant axis. */
21
- confidence: number;
22
- }
23
-
24
- interface RegionRect {
25
- x: number;
26
- y: number;
27
- width: number;
28
- height: number;
29
- }
30
-
31
- const MIN_REGION_EDGE = 8;
32
- const MIN_PEAK = 0.6;
33
- /** The shifted-lag peak must beat the zero-lag correlation by this much. */
34
- const MIN_PEAK_ADVANTAGE = 0.15;
35
-
36
- export function estimateRegionShift(
37
- baseline: PngData,
38
- current: PngData,
39
- region: RegionRect,
40
- maxOffset = 64,
41
- ): RegionShiftEstimate | null {
42
- if (region.width < MIN_REGION_EDGE || region.height < MIN_REGION_EDGE) return null;
43
- const width = Math.min(baseline.width, current.width);
44
- const height = Math.min(baseline.height, current.height);
45
-
46
- const x0 = clamp(region.x - maxOffset, 0, width);
47
- const x1 = clamp(region.x + region.width + maxOffset, 0, width);
48
- const y0 = clamp(region.y, 0, height);
49
- const y1 = clamp(region.y + region.height, 0, height);
50
- const yw0 = clamp(region.y - maxOffset, 0, height);
51
- const yw1 = clamp(region.y + region.height + maxOffset, 0, height);
52
- const xr0 = clamp(region.x, 0, width);
53
- const xr1 = clamp(region.x + region.width, 0, width);
54
- if (x1 <= x0 || y1 <= y0 || yw1 <= yw0 || xr1 <= xr0) return null;
55
-
56
- const horizontal = bestLag(
57
- columnProfile(baseline, x0, x1, y0, y1),
58
- columnProfile(current, x0, x1, y0, y1),
59
- maxOffset,
60
- );
61
- const vertical = bestLag(
62
- rowProfile(baseline, xr0, xr1, yw0, yw1),
63
- rowProfile(current, xr0, xr1, yw0, yw1),
64
- maxOffset,
65
- );
66
-
67
- const dx = acceptAxis(horizontal) ? horizontal.lag : 0;
68
- const dy = acceptAxis(vertical) ? vertical.lag : 0;
69
- if (dx === 0 && dy === 0) return null;
70
-
71
- const confidences: number[] = [];
72
- if (dx !== 0) confidences.push(horizontal.peak);
73
- if (dy !== 0) confidences.push(vertical.peak);
74
- return {
75
- dx,
76
- dy,
77
- confidence: Number(Math.max(...confidences).toFixed(3)),
78
- };
79
- }
80
-
81
- interface LagResult {
82
- lag: number;
83
- peak: number;
84
- zeroLag: number;
85
- flat: boolean;
86
- }
87
-
88
- function acceptAxis(result: LagResult): boolean {
89
- return !result.flat
90
- && result.lag !== 0
91
- && result.peak >= MIN_PEAK
92
- && result.peak - result.zeroLag >= MIN_PEAK_ADVANTAGE;
93
- }
94
-
95
- /**
96
- * Find the lag maximizing normalized cross-correlation between two
97
- * mean-subtracted profiles. Positive lag = current content sits at a
98
- * higher coordinate than baseline (moved right / down).
99
- */
100
- function bestLag(base: Float64Array, curr: Float64Array, maxOffset: number): LagResult {
101
- const a = meanSubtract(base);
102
- const b = meanSubtract(curr);
103
- if (a.flat || b.flat) return { lag: 0, peak: 0, zeroLag: 0, flat: true };
104
-
105
- let bestLag = 0;
106
- let bestVal = -Infinity;
107
- let zeroVal = 0;
108
- const n = Math.min(a.values.length, b.values.length);
109
- const limit = Math.min(maxOffset, n - 1);
110
- for (let lag = -limit; lag <= limit; lag++) {
111
- let dot = 0;
112
- let normA = 0;
113
- let normB = 0;
114
- for (let i = 0; i < n; i++) {
115
- const j = i + lag;
116
- if (j < 0 || j >= n) continue;
117
- dot += a.values[i]! * b.values[j]!;
118
- normA += a.values[i]! * a.values[i]!;
119
- normB += b.values[j]! * b.values[j]!;
120
- }
121
- if (normA === 0 || normB === 0) continue;
122
- const val = dot / Math.sqrt(normA * normB);
123
- if (lag === 0) zeroVal = val;
124
- if (val > bestVal) {
125
- bestVal = val;
126
- bestLag = lag;
127
- }
128
- }
129
- if (!Number.isFinite(bestVal)) return { lag: 0, peak: 0, zeroLag: 0, flat: true };
130
- return { lag: bestLag, peak: bestVal, zeroLag: zeroVal, flat: false };
131
- }
132
-
133
- function meanSubtract(profile: Float64Array): { values: Float64Array; flat: boolean } {
134
- let sum = 0;
135
- for (const v of profile) sum += v;
136
- const mean = sum / profile.length;
137
- const values = new Float64Array(profile.length);
138
- let variance = 0;
139
- for (let i = 0; i < profile.length; i++) {
140
- values[i] = profile[i]! - mean;
141
- variance += values[i]! * values[i]!;
142
- }
143
- return { values, flat: variance / profile.length < 1 };
144
- }
145
-
146
- function columnProfile(img: PngData, x0: number, x1: number, y0: number, y1: number): Float64Array {
147
- const profile = new Float64Array(x1 - x0);
148
- for (let x = x0; x < x1; x++) {
149
- let sum = 0;
150
- for (let y = y0; y < y1; y++) sum += lumaAt(img, x, y);
151
- profile[x - x0] = sum / (y1 - y0);
152
- }
153
- return profile;
154
- }
155
-
156
- function rowProfile(img: PngData, x0: number, x1: number, y0: number, y1: number): Float64Array {
157
- const profile = new Float64Array(y1 - y0);
158
- for (let y = y0; y < y1; y++) {
159
- let sum = 0;
160
- for (let x = x0; x < x1; x++) sum += lumaAt(img, x, y);
161
- profile[y - y0] = sum / (x1 - x0);
162
- }
163
- return profile;
164
- }
165
-
166
- function lumaAt(img: PngData, x: number, y: number): number {
167
- const i = (y * img.width + x) * 4;
168
- return 0.299 * img.data[i]! + 0.587 * img.data[i + 1]! + 0.114 * img.data[i + 2]!;
169
- }
170
-
171
- function clamp(value: number, min: number, max: number): number {
172
- return Math.max(min, Math.min(max, value));
173
- }
package/src/run-ledger.ts DELETED
@@ -1,39 +0,0 @@
1
- /**
2
- * Append-only run ledger for agent-loop tools.
3
- *
4
- * Every measurement invocation (`build page`, the dynamic gates,
5
- * `verify markup`) appends one JSONL line to `.vlmkit/run-ledger.jsonl`
6
- * under the working directory. This turns the "rounds" KPI from an agent
7
- * self-report into an auditable record: a verifier can check that the
8
- * agent actually re-measured after each fix, and how the headline numbers
9
- * moved round by round (see docs/knowledge.md "Markup Agent KPI" — round
10
- * counts were self-reported and unreliable in 5/5 S5 runs).
11
- *
12
- * Writes are best-effort: a ledger failure must never break the tool run.
13
- * Opt out with VLMKIT_NO_LEDGER=1 (e.g. CI bulk runs).
14
- */
15
- import { appendFileSync, mkdirSync } from "node:fs";
16
- import { join } from "node:path";
17
-
18
- export interface RunLedgerEntry {
19
- /** Tool name, e.g. "build-page", "check-breakpoints", "verify-markup". */
20
- tool: string;
21
- /** Primary input (attempt HTML or URL). */
22
- source: string;
23
- /** Secondary input (target PNG, manifest, ...) when applicable. */
24
- target?: string;
25
- /** Headline numbers, tool-specific but flat (matched/missing/extra/status/diffRatio...). */
26
- headline: Record<string, string | number | boolean | null>;
27
- }
28
-
29
- export function appendRunLedger(entry: RunLedgerEntry, cwd = process.cwd()): void {
30
- if (process.env.VLMKIT_NO_LEDGER === "1") return;
31
- try {
32
- const dir = join(cwd, ".vlmkit");
33
- mkdirSync(dir, { recursive: true });
34
- const line = JSON.stringify({ ts: new Date().toISOString(), ...entry });
35
- appendFileSync(join(dir, "run-ledger.jsonl"), line + "\n");
36
- } catch {
37
- // Best-effort by design.
38
- }
39
- }
@@ -1,13 +0,0 @@
1
- /** Shared ANSI terminal color codes and helpers */
2
-
3
- export const DIM = "\x1b[2m";
4
- export const RESET = "\x1b[0m";
5
- export const GREEN = "\x1b[32m";
6
- export const RED = "\x1b[31m";
7
- export const YELLOW = "\x1b[33m";
8
- export const CYAN = "\x1b[36m";
9
- export const BOLD = "\x1b[1m";
10
-
11
- export function hr(width = 72) {
12
- console.log(`${DIM}${"─".repeat(width)}${RESET}`);
13
- }