vibecode-anything 0.0.0-stage → 0.3.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 (62) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +235 -2
  3. package/dist/cli.d.ts +2 -0
  4. package/dist/cli.js +452 -0
  5. package/dist/core/adapters.d.ts +8 -0
  6. package/dist/core/adapters.js +92 -0
  7. package/dist/core/diff.d.ts +10 -0
  8. package/dist/core/diff.js +207 -0
  9. package/dist/core/doctor.d.ts +50 -0
  10. package/dist/core/doctor.js +312 -0
  11. package/dist/core/errors.d.ts +32 -0
  12. package/dist/core/errors.js +81 -0
  13. package/dist/core/exec.d.ts +14 -0
  14. package/dist/core/exec.js +109 -0
  15. package/dist/core/parity.d.ts +9 -0
  16. package/dist/core/parity.js +138 -0
  17. package/dist/core/png.d.ts +7 -0
  18. package/dist/core/png.js +303 -0
  19. package/dist/core/schema.d.ts +17 -0
  20. package/dist/core/schema.js +614 -0
  21. package/dist/core/slices.d.ts +24 -0
  22. package/dist/core/slices.js +294 -0
  23. package/dist/core/store.d.ts +17 -0
  24. package/dist/core/store.js +97 -0
  25. package/dist/core/types.d.ts +751 -0
  26. package/dist/core/types.js +4 -0
  27. package/dist/core/visual.d.ts +34 -0
  28. package/dist/core/visual.js +459 -0
  29. package/dist/core/watch.d.ts +53 -0
  30. package/dist/core/watch.js +285 -0
  31. package/dist/ingestors/api-repo.d.ts +5 -0
  32. package/dist/ingestors/api-repo.js +722 -0
  33. package/dist/ingestors/bridges.d.ts +3 -0
  34. package/dist/ingestors/bridges.js +562 -0
  35. package/dist/ingestors/cli.d.ts +9 -0
  36. package/dist/ingestors/cli.js +266 -0
  37. package/dist/ingestors/image.d.ts +2 -0
  38. package/dist/ingestors/image.js +147 -0
  39. package/dist/ingestors/index.d.ts +5 -0
  40. package/dist/ingestors/index.js +97 -0
  41. package/dist/ingestors/video.d.ts +47 -0
  42. package/dist/ingestors/video.js +605 -0
  43. package/dist/ingestors/web.d.ts +25 -0
  44. package/dist/ingestors/web.js +397 -0
  45. package/dist/mcp/server.d.ts +2 -0
  46. package/dist/mcp/server.js +504 -0
  47. package/dist/oracles/api-repo.d.ts +2 -0
  48. package/dist/oracles/api-repo.js +214 -0
  49. package/dist/oracles/cli.d.ts +2 -0
  50. package/dist/oracles/cli.js +157 -0
  51. package/dist/oracles/image.d.ts +2 -0
  52. package/dist/oracles/image.js +279 -0
  53. package/dist/oracles/index.d.ts +4 -0
  54. package/dist/oracles/index.js +91 -0
  55. package/dist/oracles/video.d.ts +21 -0
  56. package/dist/oracles/video.js +411 -0
  57. package/dist/oracles/web.d.ts +2 -0
  58. package/dist/oracles/web.js +480 -0
  59. package/dist/setup/index.d.ts +7 -0
  60. package/dist/setup/index.js +186 -0
  61. package/package.json +47 -6
  62. package/skills/vibecode-anything/SKILL.md +52 -0
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Shared TypeScript types and interfaces for vibecode-anything.
3
+ */
4
+ export {};
@@ -0,0 +1,34 @@
1
+ import type { BBox, DecodedImage, LayoutRegion, MatchedRegion, PaletteEntry, RgbaImage, SharpLike, TypographyBand } from "./types.js";
2
+ export declare function decodeImage(filePath: string, deps?: {
3
+ sharp?: SharpLike | null;
4
+ }): Promise<DecodedImage>;
5
+ export declare function resizeRgba(img: RgbaImage, targetW: number, targetH: number): RgbaImage;
6
+ export declare function toGray(img: RgbaImage): Uint8Array;
7
+ export declare function computeDHash(img: RgbaImage): string;
8
+ export declare function hammingDistanceHex(a: string, b: string): number;
9
+ export declare function computeSSIM(imgA: RgbaImage, imgB: RgbaImage): {
10
+ ssim: number;
11
+ dimensionMismatch: boolean;
12
+ };
13
+ export declare function computePixelDiff(imgA: RgbaImage, imgB: RgbaImage, { colorDelta }?: {
14
+ colorDelta?: number;
15
+ }): {
16
+ diffPixels: number;
17
+ totalPixels: number;
18
+ diffRatio: number;
19
+ matchScore: number;
20
+ dimensionMismatch: boolean;
21
+ diffMask: RgbaImage;
22
+ };
23
+ export declare function extractPalette(img: RgbaImage, k?: number): PaletteEntry[];
24
+ export declare function comparePalettes(palA: readonly PaletteEntry[] | null | undefined, palB: readonly PaletteEntry[] | null | undefined): number;
25
+ export declare function extractLayoutRegions(img: RgbaImage, { maxDepth }?: {
26
+ maxDepth?: number;
27
+ }): LayoutRegion[];
28
+ export declare function compareRegionsIoU(regA: readonly LayoutRegion[] | null | undefined, regB: readonly LayoutRegion[] | null | undefined): {
29
+ meanIoU: number;
30
+ matched: MatchedRegion[];
31
+ };
32
+ export declare function bboxIoU(a: BBox | null | undefined, b: BBox | null | undefined): number;
33
+ export declare function estimateTypographyBands(img: RgbaImage): TypographyBand[];
34
+ export declare function writeDiffPng(diffMask: RgbaImage, filePath: string): Buffer;
@@ -0,0 +1,459 @@
1
+ import fs from "node:fs";
2
+ import { VibecodeError } from "./errors.js";
3
+ import { decodePng, readImageDimensions, encodePng } from "./png.js";
4
+ import { loadSharp } from "./adapters.js";
5
+ const MAX_SSIM_EDGE = 512;
6
+ export async function decodeImage(filePath, deps = {}) {
7
+ const buf = fs.readFileSync(filePath);
8
+ const dims = readImageDimensions(buf);
9
+ let pngErr = null;
10
+ if (dims && dims.format === "png") {
11
+ try {
12
+ const decoded = decodePng(buf);
13
+ return { ...decoded, format: "png", backend: "png-native" };
14
+ }
15
+ catch (err) {
16
+ pngErr = err;
17
+ }
18
+ }
19
+ const explicitSharp = Object.prototype.hasOwnProperty.call(deps, "sharp") ? deps.sharp : undefined;
20
+ const sharp = explicitSharp === null ? null : (explicitSharp || (await loadSharp(deps)));
21
+ if (sharp) {
22
+ const { data, info } = await sharp(buf).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
23
+ return {
24
+ width: info.width,
25
+ height: info.height,
26
+ data: new Uint8Array(data),
27
+ format: dims?.format || "unknown",
28
+ backend: "sharp"
29
+ };
30
+ }
31
+ if (pngErr)
32
+ throw pngErr;
33
+ throw new VibecodeError("ADAPTER_MISSING", `Cannot decode ${dims?.format || "unknown"} image without the optional 'sharp' adapter`, {
34
+ adapter: "sharp",
35
+ installHint: "npm install sharp",
36
+ fallbackHint: "Convert the image to a non-interlaced 8-bit PNG to use the zero-dependency decoder.",
37
+ details: dims
38
+ });
39
+ }
40
+ export function resizeRgba(img, targetW, targetH) {
41
+ const w = Math.max(1, Math.round(targetW));
42
+ const h = Math.max(1, Math.round(targetH));
43
+ if (w === img.width && h === img.height) {
44
+ return { width: w, height: h, data: new Uint8Array(img.data) };
45
+ }
46
+ const out = new Uint8Array(w * h * 4);
47
+ const scaleX = img.width / w;
48
+ const scaleY = img.height / h;
49
+ for (let y = 0; y < h; y++) {
50
+ const fy = (y + 0.5) * scaleY - 0.5;
51
+ const y0 = Math.max(0, Math.min(img.height - 1, Math.floor(fy)));
52
+ const y1 = Math.max(0, Math.min(img.height - 1, y0 + 1));
53
+ const wy = Math.max(0, Math.min(1, fy - y0));
54
+ for (let x = 0; x < w; x++) {
55
+ const fx = (x + 0.5) * scaleX - 0.5;
56
+ const x0 = Math.max(0, Math.min(img.width - 1, Math.floor(fx)));
57
+ const x1 = Math.max(0, Math.min(img.width - 1, x0 + 1));
58
+ const wx = Math.max(0, Math.min(1, fx - x0));
59
+ const o = (y * w + x) * 4;
60
+ for (let c = 0; c < 4; c++) {
61
+ const p00 = img.data[(y0 * img.width + x0) * 4 + c];
62
+ const p10 = img.data[(y0 * img.width + x1) * 4 + c];
63
+ const p01 = img.data[(y1 * img.width + x0) * 4 + c];
64
+ const p11 = img.data[(y1 * img.width + x1) * 4 + c];
65
+ const top = p00 + (p10 - p00) * wx;
66
+ const bottom = p01 + (p11 - p01) * wx;
67
+ out[o + c] = Math.round(top + (bottom - top) * wy);
68
+ }
69
+ }
70
+ }
71
+ return { width: w, height: h, data: out };
72
+ }
73
+ export function toGray(img) {
74
+ const out = new Uint8Array(img.width * img.height);
75
+ for (let i = 0, p = 0; i < out.length; i++, p += 4) {
76
+ // Composite partially transparent pixels over white for stable luminance.
77
+ const a = img.data[p + 3] / 255;
78
+ const r = img.data[p] * a + 255 * (1 - a);
79
+ const g = img.data[p + 1] * a + 255 * (1 - a);
80
+ const b = img.data[p + 2] * a + 255 * (1 - a);
81
+ out[i] = Math.max(0, Math.min(255, Math.round(0.299 * r + 0.587 * g + 0.114 * b)));
82
+ }
83
+ return out;
84
+ }
85
+ export function computeDHash(img) {
86
+ const small = resizeRgba(img, 9, 8);
87
+ const gray = toGray(small);
88
+ let bits = 0n;
89
+ for (let y = 0; y < 8; y++) {
90
+ for (let x = 0; x < 8; x++) {
91
+ const left = gray[y * 9 + x];
92
+ const right = gray[y * 9 + x + 1];
93
+ bits = (bits << 1n) | (left > right ? 1n : 0n);
94
+ }
95
+ }
96
+ return bits.toString(16).padStart(16, "0");
97
+ }
98
+ export function hammingDistanceHex(a, b) {
99
+ try {
100
+ let x = BigInt("0x" + a) ^ BigInt("0x" + b);
101
+ let count = 0;
102
+ while (x > 0n) {
103
+ count += Number(x & 1n);
104
+ x >>= 1n;
105
+ }
106
+ return count;
107
+ }
108
+ catch {
109
+ return 64;
110
+ }
111
+ }
112
+ export function computeSSIM(imgA, imgB) {
113
+ const dimensionMismatch = imgA.width !== imgB.width || imgA.height !== imgB.height;
114
+ let a = imgA;
115
+ let b = dimensionMismatch ? resizeRgba(imgB, imgA.width, imgA.height) : imgB;
116
+ let w = a.width;
117
+ let h = a.height;
118
+ const maxEdge = Math.max(w, h);
119
+ if (maxEdge > MAX_SSIM_EDGE) {
120
+ const scale = MAX_SSIM_EDGE / maxEdge;
121
+ w = Math.max(1, Math.round(w * scale));
122
+ h = Math.max(1, Math.round(h * scale));
123
+ a = resizeRgba(a, w, h);
124
+ b = resizeRgba(b, w, h);
125
+ }
126
+ const ga = toGray(a);
127
+ const gb = toGray(b);
128
+ const C1 = 6.5025;
129
+ const C2 = 58.5225;
130
+ let total = 0;
131
+ let weight = 0;
132
+ for (let y = 0; y < h; y += 8) {
133
+ for (let x = 0; x < w; x += 8) {
134
+ const bw = Math.min(8, w - x);
135
+ const bh = Math.min(8, h - y);
136
+ const n = bw * bh;
137
+ let sa = 0, sb = 0, saa = 0, sbb = 0, sab = 0;
138
+ for (let yy = 0; yy < bh; yy++) {
139
+ for (let xx = 0; xx < bw; xx++) {
140
+ const va = ga[(y + yy) * w + x + xx];
141
+ const vb = gb[(y + yy) * w + x + xx];
142
+ sa += va;
143
+ sb += vb;
144
+ saa += va * va;
145
+ sbb += vb * vb;
146
+ sab += va * vb;
147
+ }
148
+ }
149
+ const ma = sa / n;
150
+ const mb = sb / n;
151
+ const va = Math.max(0, saa / n - ma * ma);
152
+ const vb = Math.max(0, sbb / n - mb * mb);
153
+ const cov = sab / n - ma * mb;
154
+ const ssim = ((2 * ma * mb + C1) * (2 * cov + C2)) / ((ma * ma + mb * mb + C1) * (va + vb + C2));
155
+ total += Math.max(0, Math.min(1, ssim)) * n;
156
+ weight += n;
157
+ }
158
+ }
159
+ return { ssim: Math.max(0, Math.min(1, weight > 0 ? total / weight : 1)), dimensionMismatch };
160
+ }
161
+ function rgbToYiq(r, g, b) {
162
+ return {
163
+ y: 0.299 * r + 0.587 * g + 0.114 * b,
164
+ i: 0.596 * r - 0.274 * g - 0.322 * b,
165
+ q: 0.211 * r - 0.523 * g + 0.312 * b
166
+ };
167
+ }
168
+ export function computePixelDiff(imgA, imgB, { colorDelta = 0.1 } = {}) {
169
+ const dimensionMismatch = imgA.width !== imgB.width || imgA.height !== imgB.height;
170
+ const b = dimensionMismatch ? resizeRgba(imgB, imgA.width, imgA.height) : imgB;
171
+ const totalPixels = imgA.width * imgA.height;
172
+ const mask = new Uint8Array(totalPixels * 4);
173
+ let diffPixels = 0;
174
+ for (let i = 0, p = 0; i < totalPixels; i++, p += 4) {
175
+ const ya = rgbToYiq(imgA.data[p], imgA.data[p + 1], imgA.data[p + 2]);
176
+ const yb = rgbToYiq(b.data[p], b.data[p + 1], b.data[p + 2]);
177
+ const dy = ya.y - yb.y;
178
+ const di = ya.i - yb.i;
179
+ const dq = ya.q - yb.q;
180
+ const delta = Math.sqrt(dy * dy + di * di + dq * dq) / 255;
181
+ if (delta > colorDelta) {
182
+ diffPixels++;
183
+ mask[p] = 255;
184
+ mask[p + 1] = 0;
185
+ mask[p + 2] = 0;
186
+ mask[p + 3] = 255;
187
+ }
188
+ else {
189
+ // lightweight grayscale of the reference for visual context
190
+ const g = Math.round(ya.y);
191
+ mask[p] = g;
192
+ mask[p + 1] = g;
193
+ mask[p + 2] = g;
194
+ mask[p + 3] = 255;
195
+ }
196
+ }
197
+ const diffRatio = totalPixels > 0 ? diffPixels / totalPixels : 0;
198
+ return {
199
+ diffPixels,
200
+ totalPixels,
201
+ diffRatio,
202
+ matchScore: 1 - diffRatio,
203
+ dimensionMismatch,
204
+ diffMask: { width: imgA.width, height: imgA.height, data: mask }
205
+ };
206
+ }
207
+ export function extractPalette(img, k = 6) {
208
+ const counts = new Map();
209
+ const total = img.width * img.height;
210
+ for (let i = 0, p = 0; i < total; i++, p += 4) {
211
+ if (img.data[p + 3] === 0)
212
+ continue;
213
+ const r = img.data[p], g = img.data[p + 1], b = img.data[p + 2];
214
+ const key = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);
215
+ let entry = counts.get(key);
216
+ if (!entry) {
217
+ entry = { count: 0, r: 0, g: 0, b: 0 };
218
+ counts.set(key, entry);
219
+ }
220
+ entry.count++;
221
+ entry.r += r;
222
+ entry.g += g;
223
+ entry.b += b;
224
+ }
225
+ const entries = [...counts.values()].sort((a, b) => b.count - a.count).slice(0, Math.max(1, k));
226
+ const denom = entries.reduce((sum, e) => sum + e.count, 0) || 1;
227
+ return entries.map((e) => {
228
+ const r = Math.round(e.r / e.count);
229
+ const g = Math.round(e.g / e.count);
230
+ const b = Math.round(e.b / e.count);
231
+ const hex = "#" + [r, g, b].map((v) => v.toString(16).padStart(2, "0")).join("");
232
+ return { hex, weight: e.count / denom };
233
+ });
234
+ }
235
+ function hexToRgb(hex) {
236
+ const m = /^#?([0-9a-f]{6})$/i.exec(hex || "");
237
+ if (!m)
238
+ return { r: 0, g: 0, b: 0 };
239
+ const v = parseInt(m[1], 16);
240
+ return { r: (v >> 16) & 255, g: (v >> 8) & 255, b: v & 255 };
241
+ }
242
+ const MAX_COLOR_DIST = Math.sqrt(3 * 255 * 255);
243
+ export function comparePalettes(palA, palB) {
244
+ if (!palA?.length && !palB?.length)
245
+ return 1;
246
+ if (!palA?.length || !palB?.length)
247
+ return 0;
248
+ const oneWay = (from, to) => {
249
+ let score = 0;
250
+ for (const a of from) {
251
+ const ca = hexToRgb(a.hex);
252
+ let best = 0;
253
+ for (const b of to) {
254
+ const cb = hexToRgb(b.hex);
255
+ const d = Math.sqrt((ca.r - cb.r) ** 2 + (ca.g - cb.g) ** 2 + (ca.b - cb.b) ** 2);
256
+ best = Math.max(best, 1 - d / MAX_COLOR_DIST);
257
+ }
258
+ score += a.weight * best;
259
+ }
260
+ return score;
261
+ };
262
+ return Math.max(0, Math.min(1, (oneWay(palA, palB) + oneWay(palB, palA)) / 2));
263
+ }
264
+ function edgeProfiles(gray, W, _H, x0, y0, w, h) {
265
+ const v = new Float64Array(w);
266
+ const hprof = new Float64Array(h);
267
+ for (let y = y0; y < y0 + h; y++) {
268
+ for (let x = x0; x < x0 + w; x++) {
269
+ const dx = x + 1 < x0 + w ? Math.abs(gray[y * W + x] - gray[y * W + x + 1]) : 0;
270
+ const dy = y + 1 < y0 + h ? Math.abs(gray[y * W + x] - gray[(y + 1) * W + x]) : 0;
271
+ const e = dx + dy;
272
+ v[x - x0] += e;
273
+ hprof[y - y0] += e;
274
+ }
275
+ }
276
+ return { v, h: hprof };
277
+ }
278
+ function splitByGaps(profile, minSegFrac = 0.06) {
279
+ const n = profile.length;
280
+ if (n < 4)
281
+ return [[0, n]];
282
+ let max = 0;
283
+ for (const v of profile)
284
+ if (v > max)
285
+ max = v;
286
+ if (max <= 0)
287
+ return [[0, n]];
288
+ const threshold = max * 0.08;
289
+ const minGap = Math.max(2, Math.round(n * 0.02));
290
+ const minSeg = Math.max(4, Math.round(n * minSegFrac));
291
+ const segs = [];
292
+ let start = 0;
293
+ let i = 0;
294
+ while (i < n) {
295
+ if (profile[i] < threshold) {
296
+ let j = i;
297
+ while (j < n && profile[j] < threshold)
298
+ j++;
299
+ if (j - i >= minGap) {
300
+ const end = i;
301
+ if (end - start >= minSeg)
302
+ segs.push([start, end]);
303
+ start = j;
304
+ }
305
+ i = j;
306
+ }
307
+ else {
308
+ i++;
309
+ }
310
+ }
311
+ if (n - start >= minSeg)
312
+ segs.push([start, n]);
313
+ return segs.length > 1 ? segs : [[0, n]];
314
+ }
315
+ export function extractLayoutRegions(img, { maxDepth = 3 } = {}) {
316
+ const maxEdge = 96;
317
+ const scale = Math.min(1, maxEdge / Math.max(img.width, img.height));
318
+ const small = scale < 1 ? resizeRgba(img, Math.round(img.width * scale), Math.round(img.height * scale)) : img;
319
+ const gray = toGray(small);
320
+ const regions = [];
321
+ const W = small.width;
322
+ const H = small.height;
323
+ const push = (x, y, w, h) => {
324
+ if (regions.length >= 12)
325
+ return;
326
+ regions.push({ bbox: { x: x / W, y: y / H, w: w / W, h: h / H } });
327
+ };
328
+ const recurse = (x, y, w, h, depth) => {
329
+ if (regions.length >= 12)
330
+ return;
331
+ if (depth >= maxDepth || w < W * 0.08 || h < H * 0.08) {
332
+ push(x, y, w, h);
333
+ return;
334
+ }
335
+ const prof = edgeProfiles(gray, W, H, x, y, w, h);
336
+ const hSegs = splitByGaps(prof.h);
337
+ const vSegs = splitByGaps(prof.v);
338
+ if (hSegs.length > 1) {
339
+ for (const [sy, ey] of hSegs)
340
+ recurse(x, y + sy, w, ey - sy, depth + 1);
341
+ return;
342
+ }
343
+ if (vSegs.length > 1) {
344
+ for (const [sx, ex] of vSegs)
345
+ recurse(x + sx, y, ex - sx, h, depth + 1);
346
+ return;
347
+ }
348
+ push(x, y, w, h);
349
+ };
350
+ recurse(0, 0, W, H, 0);
351
+ if (regions.length === 0)
352
+ regions.push({ bbox: { x: 0, y: 0, w: 1, h: 1 } });
353
+ return regions.map((r, i) => ({ id: `r-${i}`, bbox: r.bbox }));
354
+ }
355
+ export function compareRegionsIoU(regA, regB) {
356
+ const a = regA || [];
357
+ const b = regB || [];
358
+ if (a.length === 0 && b.length === 0)
359
+ return { meanIoU: 1, matched: [] };
360
+ const pairs = [];
361
+ for (const ra of a) {
362
+ for (const rb of b)
363
+ pairs.push({ ra, rb, iou: bboxIoU(ra.bbox, rb.bbox) });
364
+ }
365
+ pairs.sort((x, y) => y.iou - x.iou);
366
+ const usedA = new Set();
367
+ const usedB = new Set();
368
+ const matched = [];
369
+ let sum = 0;
370
+ for (const p of pairs) {
371
+ if (usedA.has(p.ra.id) || usedB.has(p.rb.id))
372
+ continue;
373
+ usedA.add(p.ra.id);
374
+ usedB.add(p.rb.id);
375
+ matched.push({ refId: p.ra.id, candId: p.rb.id, iou: p.iou });
376
+ sum += p.iou;
377
+ }
378
+ const denom = Math.max(a.length, b.length);
379
+ return { meanIoU: denom > 0 ? sum / denom : 1, matched };
380
+ }
381
+ export function bboxIoU(a, b) {
382
+ if (!a || !b)
383
+ return 0;
384
+ const ax2 = a.x + a.w, ay2 = a.y + a.h;
385
+ const bx2 = b.x + b.w, by2 = b.y + b.h;
386
+ const ix = Math.max(0, Math.min(ax2, bx2) - Math.max(a.x, b.x));
387
+ const iy = Math.max(0, Math.min(ay2, by2) - Math.max(a.y, b.y));
388
+ const inter = ix * iy;
389
+ const union = a.w * a.h + b.w * b.h - inter;
390
+ return union > 0 ? inter / union : 0;
391
+ }
392
+ export function estimateTypographyBands(img) {
393
+ const maxW = 240;
394
+ const scale = Math.min(1, maxW / img.width);
395
+ const small = scale < 1 ? resizeRgba(img, Math.round(img.width * scale), img.height ? Math.round(img.height * scale) : img.height) : img;
396
+ const gray = toGray(small);
397
+ const W = small.width, H = small.height;
398
+ const energy = new Float64Array(H);
399
+ for (let y = 0; y < H; y++) {
400
+ let sum = 0;
401
+ for (let x = 0; x < W - 1; x++)
402
+ sum += Math.abs(gray[y * W + x] - gray[y * W + x + 1]);
403
+ energy[y] = sum;
404
+ }
405
+ let mean = 0;
406
+ for (const e of energy)
407
+ mean += e;
408
+ mean /= H || 1;
409
+ let variance = 0;
410
+ for (const e of energy)
411
+ variance += (e - mean) ** 2;
412
+ variance /= H || 1;
413
+ const std = Math.sqrt(variance);
414
+ const threshold = mean + std * 0.25;
415
+ const bands = [];
416
+ let start = -1;
417
+ for (let y = 0; y < H; y++) {
418
+ const active = energy[y] > threshold;
419
+ if (active && start < 0)
420
+ start = y;
421
+ if (!active && start >= 0) {
422
+ if (y - start >= 2)
423
+ bands.push([start, y]);
424
+ start = -1;
425
+ }
426
+ }
427
+ if (start >= 0 && H - start >= 2)
428
+ bands.push([start, H]);
429
+ const groups = new Map();
430
+ const pxScale = scale < 1 ? 1 / scale : 1;
431
+ for (const [s, e] of bands) {
432
+ const heightPx = Math.round((e - s) * pxScale);
433
+ let level;
434
+ if (heightPx >= 36)
435
+ level = "display";
436
+ else if (heightPx >= 20)
437
+ level = "heading";
438
+ else if (heightPx >= 11)
439
+ level = "body";
440
+ else
441
+ level = "caption";
442
+ const g = groups.get(level) || { total: 0, count: 0 };
443
+ g.total += heightPx;
444
+ g.count++;
445
+ groups.set(level, g);
446
+ }
447
+ const order = ["display", "heading", "body", "caption"];
448
+ return order
449
+ .filter((level) => groups.has(level))
450
+ .map((level) => {
451
+ const g = groups.get(level);
452
+ return { level, estimatedHeightPx: Math.round(g.total / g.count), bandCount: g.count };
453
+ });
454
+ }
455
+ export function writeDiffPng(diffMask, filePath) {
456
+ const png = encodePng(diffMask);
457
+ fs.writeFileSync(filePath, png);
458
+ return png;
459
+ }
@@ -0,0 +1,53 @@
1
+ import type { ContractV02, OracleContext, ParityReport, SliceSummary, VerifyDeps } from "./types.js";
2
+ export interface WatchFsWatcher {
3
+ close(): void;
4
+ }
5
+ export type WatchRunReason = "initial" | "change" | "manual";
6
+ export interface WatchRunEvent {
7
+ runIndex: number;
8
+ reason: WatchRunReason;
9
+ changedFiles: string[];
10
+ timestamp: string;
11
+ durationMs: number;
12
+ report: ParityReport;
13
+ slices?: SliceSummary;
14
+ newlyFixed: string[];
15
+ newlyBroken: string[];
16
+ }
17
+ export interface WatchOptions {
18
+ target: string;
19
+ cwd?: string;
20
+ watchPaths?: string[];
21
+ debounceMs?: number;
22
+ strict?: boolean;
23
+ sliceIds?: string[];
24
+ invariantIds?: string[];
25
+ ignore?: string[];
26
+ maxRuns?: number;
27
+ runOnStart?: boolean;
28
+ updateSlices?: boolean;
29
+ onRun?: (event: WatchRunEvent) => void;
30
+ onError?: (error: unknown) => void;
31
+ signal?: AbortSignal;
32
+ }
33
+ export interface WatchDeps {
34
+ verifyContract?: (contract: ContractV02, ctx: OracleContext, deps?: VerifyDeps) => Promise<ParityReport>;
35
+ readContract?: (cwd?: string) => ContractV02;
36
+ writeContract?: (contract: unknown, cwd?: string, options?: {
37
+ force?: boolean;
38
+ }) => string;
39
+ writeLastReport?: (report: ParityReport, cwd?: string) => string;
40
+ fsWatch?: (filename: string, options: {
41
+ recursive: boolean;
42
+ }, listener: (eventType: string, filename: string | Buffer | null) => void) => WatchFsWatcher;
43
+ }
44
+ export interface WatchHandle {
45
+ close(): Promise<void>;
46
+ trigger(changedFiles?: string[]): void;
47
+ done: Promise<void>;
48
+ runCount(): number;
49
+ }
50
+ /** Start a zero-dependency continuous verification watcher. */
51
+ export declare function startWatch(options: WatchOptions, deps?: WatchDeps): Promise<WatchHandle>;
52
+ /** Concise one-line summary of a watch run, with follow-up details when relevant. */
53
+ export declare function formatWatchEvent(event: WatchRunEvent): string;