vibecode-anything 0.3.0 → 0.4.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 +239 -67
- package/dist/cli.js +2140 -73
- package/dist/core/adapters.d.ts +26 -3
- package/dist/core/adapters.js +411 -25
- package/dist/core/agent-adapter.d.ts +67 -0
- package/dist/core/agent-adapter.js +312 -0
- package/dist/core/anti-cheat.d.ts +6 -0
- package/dist/core/anti-cheat.js +372 -0
- package/dist/core/badge.d.ts +18 -0
- package/dist/core/badge.js +360 -0
- package/dist/core/cache.d.ts +47 -0
- package/dist/core/cache.js +403 -0
- package/dist/core/ci-export.d.ts +35 -0
- package/dist/core/ci-export.js +796 -0
- package/dist/core/ci-formats.d.ts +21 -0
- package/dist/core/ci-formats.js +388 -0
- package/dist/core/client-codegen.d.ts +36 -0
- package/dist/core/client-codegen.js +2538 -0
- package/dist/core/compose.d.ts +71 -0
- package/dist/core/compose.js +1088 -0
- package/dist/core/docs-export.d.ts +37 -0
- package/dist/core/docs-export.js +2706 -0
- package/dist/core/doctor.d.ts +21 -1
- package/dist/core/doctor.js +234 -21
- package/dist/core/exec.d.ts +26 -2
- package/dist/core/exec.js +235 -8
- package/dist/core/fixtures.d.ts +153 -0
- package/dist/core/fixtures.js +2245 -0
- package/dist/core/format.d.ts +77 -0
- package/dist/core/format.js +632 -0
- package/dist/core/fs-snapshot.d.ts +37 -0
- package/dist/core/fs-snapshot.js +216 -0
- package/dist/core/graphql-export.d.ts +34 -0
- package/dist/core/graphql-export.js +2482 -0
- package/dist/core/har-export.d.ts +29 -0
- package/dist/core/har-export.js +988 -0
- package/dist/core/html-report.d.ts +27 -0
- package/dist/core/html-report.js +1535 -0
- package/dist/core/http-export.d.ts +31 -0
- package/dist/core/http-export.js +726 -0
- package/dist/core/impact.d.ts +80 -0
- package/dist/core/impact.js +1412 -0
- package/dist/core/lint.d.ts +38 -0
- package/dist/core/lint.js +1111 -0
- package/dist/core/load-export.d.ts +37 -0
- package/dist/core/load-export.js +1813 -0
- package/dist/core/mcp-export.d.ts +35 -0
- package/dist/core/mcp-export.js +1736 -0
- package/dist/core/mock-server.d.ts +47 -0
- package/dist/core/mock-server.js +1722 -0
- package/dist/core/openapi-export.d.ts +44 -0
- package/dist/core/openapi-export.js +1074 -0
- package/dist/core/parity.d.ts +6 -1
- package/dist/core/parity.js +36 -1
- package/dist/core/playwright-exporter.d.ts +23 -0
- package/dist/core/playwright-exporter.js +1343 -0
- package/dist/core/png.js +45 -9
- package/dist/core/regression.d.ts +54 -0
- package/dist/core/regression.js +529 -0
- package/dist/core/responsive.d.ts +86 -0
- package/dist/core/responsive.js +606 -0
- package/dist/core/run.d.ts +74 -0
- package/dist/core/run.js +696 -0
- package/dist/core/scheduler.d.ts +24 -0
- package/dist/core/scheduler.js +162 -0
- package/dist/core/schema-export.d.ts +29 -0
- package/dist/core/schema-export.js +2215 -0
- package/dist/core/schema.d.ts +3 -2
- package/dist/core/schema.js +460 -17
- package/dist/core/security.d.ts +233 -0
- package/dist/core/security.js +1839 -0
- package/dist/core/server-export.d.ts +38 -0
- package/dist/core/server-export.js +3198 -0
- package/dist/core/spec-writer.d.ts +9 -0
- package/dist/core/spec-writer.js +1202 -0
- package/dist/core/store.d.ts +11 -2
- package/dist/core/store.js +128 -8
- package/dist/core/target-map.d.ts +34 -0
- package/dist/core/target-map.js +262 -0
- package/dist/core/test-exporter.d.ts +25 -0
- package/dist/core/test-exporter.js +1918 -0
- package/dist/core/types.d.ts +182 -14
- package/dist/core/visual-semantic.d.ts +67 -0
- package/dist/core/visual-semantic.js +673 -0
- package/dist/core/visual.js +65 -16
- package/dist/core/watch.d.ts +28 -3
- package/dist/core/watch.js +222 -12
- package/dist/core/yaml.d.ts +5 -0
- package/dist/core/yaml.js +1009 -0
- package/dist/diagnostics/diagnose.d.ts +53 -0
- package/dist/diagnostics/diagnose.js +1083 -0
- package/dist/diagnostics/flake.d.ts +71 -0
- package/dist/diagnostics/flake.js +608 -0
- package/dist/diagnostics/mutation-audit.d.ts +69 -0
- package/dist/diagnostics/mutation-audit.js +769 -0
- package/dist/diagnostics/spatial.d.ts +42 -0
- package/dist/diagnostics/spatial.js +346 -0
- package/dist/diagnostics/trends.d.ts +122 -0
- package/dist/diagnostics/trends.js +1190 -0
- package/dist/discovery/discover.d.ts +111 -0
- package/dist/discovery/discover.js +2017 -0
- package/dist/discovery/lock.d.ts +116 -0
- package/dist/discovery/lock.js +1505 -0
- package/dist/discovery/routes.d.ts +36 -0
- package/dist/discovery/routes.js +1494 -0
- package/dist/discovery/test-importer.d.ts +92 -0
- package/dist/discovery/test-importer.js +4952 -0
- package/dist/index.d.ts +123 -0
- package/dist/index.js +99 -0
- package/dist/ingestors/api-collections.d.ts +28 -0
- package/dist/ingestors/api-collections.js +2335 -0
- package/dist/ingestors/api-repo.d.ts +2 -1
- package/dist/ingestors/api-repo.js +483 -85
- package/dist/ingestors/asyncapi.d.ts +26 -0
- package/dist/ingestors/asyncapi.js +2105 -0
- package/dist/ingestors/bridges.d.ts +2 -0
- package/dist/ingestors/bridges.js +702 -0
- package/dist/ingestors/cli.d.ts +2 -1
- package/dist/ingestors/cli.js +234 -46
- package/dist/ingestors/curl.d.ts +14 -0
- package/dist/ingestors/curl.js +1024 -0
- package/dist/ingestors/graphql.d.ts +56 -0
- package/dist/ingestors/graphql.js +1852 -0
- package/dist/ingestors/http-file.d.ts +20 -0
- package/dist/ingestors/http-file.js +923 -0
- package/dist/ingestors/index.js +486 -6
- package/dist/ingestors/openrpc.d.ts +29 -0
- package/dist/ingestors/openrpc.js +1807 -0
- package/dist/ingestors/proto.d.ts +47 -0
- package/dist/ingestors/proto.js +2690 -0
- package/dist/ingestors/trace.d.ts +10 -0
- package/dist/ingestors/trace.js +599 -0
- package/dist/ingestors/video.d.ts +4 -1
- package/dist/ingestors/video.js +86 -36
- package/dist/ingestors/web.d.ts +16 -1
- package/dist/ingestors/web.js +474 -32
- package/dist/mcp/server.js +3532 -67
- package/dist/oracles/api-repo.js +102 -30
- package/dist/oracles/cli.js +65 -17
- package/dist/oracles/http-workflow.d.ts +68 -0
- package/dist/oracles/http-workflow.js +828 -0
- package/dist/oracles/image.js +178 -28
- package/dist/oracles/index.d.ts +3 -1
- package/dist/oracles/index.js +243 -14
- package/dist/oracles/perf-budget.d.ts +90 -0
- package/dist/oracles/perf-budget.js +1329 -0
- package/dist/oracles/stream.d.ts +94 -0
- package/dist/oracles/stream.js +1268 -0
- package/dist/oracles/ui-states.d.ts +49 -0
- package/dist/oracles/ui-states.js +894 -0
- package/dist/oracles/video.d.ts +1 -1
- package/dist/oracles/video.js +232 -16
- package/dist/oracles/web.js +464 -73
- package/dist/plugins/builtins.d.ts +12 -0
- package/dist/plugins/builtins.js +1052 -0
- package/dist/plugins/registry.d.ts +86 -0
- package/dist/plugins/registry.js +554 -0
- package/dist/setup/index.js +145 -33
- package/package.json +56 -47
- package/skills/vibecode-anything/SKILL.md +75 -52
|
@@ -0,0 +1,673 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure-Node WCAG 2.1 AA/AAA contrast audit, design-token extractor,
|
|
3
|
+
* and semantic verification modes (`exact`, `structural`, `functional`, `reskin`).
|
|
4
|
+
*
|
|
5
|
+
* Zero runtime dependencies; reuses `extractLayoutRegions` from `./visual.js`.
|
|
6
|
+
*/
|
|
7
|
+
import { VibecodeError } from "./errors.js";
|
|
8
|
+
import { isForbiddenObjectKey, stripTerminalEscapes } from "./security.js";
|
|
9
|
+
import { extractLayoutRegions } from "./visual.js";
|
|
10
|
+
export const VALID_VERIFY_MODES = new Set(["exact", "structural", "functional", "reskin"]);
|
|
11
|
+
const MAX_IMAGE_DIM = 16_384;
|
|
12
|
+
const MAX_IMAGE_PIXELS = 33_554_432;
|
|
13
|
+
const DEFAULT_MAX_REGIONS = 32;
|
|
14
|
+
const MAX_REGIONS_CAP = 128;
|
|
15
|
+
const MAX_CLONE_DEPTH = 32;
|
|
16
|
+
const PERCEPTUAL_TOLERANCE_KEYS = [
|
|
17
|
+
"minSSIM", "maxPixelDiffRatio", "colorDelta", "minPaletteSimilarity", "maxHammingDistance", "minKeyframeSSIM"
|
|
18
|
+
];
|
|
19
|
+
const ANSI = {
|
|
20
|
+
reset: "\x1b[0m", bold: "\x1b[1m", dim: "\x1b[2m",
|
|
21
|
+
red: "\x1b[31m", green: "\x1b[32m", yellow: "\x1b[33m", cyan: "\x1b[36m"
|
|
22
|
+
};
|
|
23
|
+
function cleanTerminal(value) {
|
|
24
|
+
return typeof value === "string" ? stripTerminalEscapes(value).replace(/[\r\n]+/g, " ").trim() : "";
|
|
25
|
+
}
|
|
26
|
+
function safeDescribe(value) {
|
|
27
|
+
if (typeof value === "string") {
|
|
28
|
+
const clean = cleanTerminal(value);
|
|
29
|
+
return clean.length > 64 ? `${clean.slice(0, 64)}...` : clean;
|
|
30
|
+
}
|
|
31
|
+
if (value === null || value === undefined || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
|
|
32
|
+
return String(value);
|
|
33
|
+
}
|
|
34
|
+
if (typeof value === "symbol")
|
|
35
|
+
return value.description ? `Symbol(${value.description})` : "Symbol()";
|
|
36
|
+
return typeof value;
|
|
37
|
+
}
|
|
38
|
+
function safeFixed(value, digits) {
|
|
39
|
+
return typeof value === "number" && Number.isFinite(value) ? value.toFixed(digits) : (0).toFixed(digits);
|
|
40
|
+
}
|
|
41
|
+
function safeInt(value) {
|
|
42
|
+
return typeof value === "number" && Number.isFinite(value) ? Math.round(value) : 0;
|
|
43
|
+
}
|
|
44
|
+
function clampChannel(v) {
|
|
45
|
+
return Number.isFinite(v) ? Math.max(0, Math.min(255, Math.round(v))) : 0;
|
|
46
|
+
}
|
|
47
|
+
export function parseHexColor(hex) {
|
|
48
|
+
if (typeof hex !== "string" || (hex.length !== 4 && hex.length !== 7))
|
|
49
|
+
return null;
|
|
50
|
+
const match = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(hex);
|
|
51
|
+
if (!match)
|
|
52
|
+
return null;
|
|
53
|
+
const d = match[1];
|
|
54
|
+
if (d.length === 3) {
|
|
55
|
+
return { r: parseInt(d[0], 16) * 17, g: parseInt(d[1], 16) * 17, b: parseInt(d[2], 16) * 17 };
|
|
56
|
+
}
|
|
57
|
+
const v = parseInt(d, 16);
|
|
58
|
+
return { r: (v >> 16) & 255, g: (v >> 8) & 255, b: v & 255 };
|
|
59
|
+
}
|
|
60
|
+
export function rgbToHex(rgb) {
|
|
61
|
+
const hex = (c) => clampChannel(c).toString(16).padStart(2, "0");
|
|
62
|
+
return `#${hex(rgb?.r)}${hex(rgb?.g)}${hex(rgb?.b)}`;
|
|
63
|
+
}
|
|
64
|
+
function linearizeChannel(channel) {
|
|
65
|
+
const srgb = (Number.isFinite(channel) ? Math.max(0, Math.min(255, channel)) : 0) / 255;
|
|
66
|
+
return srgb <= 0.04045 ? srgb / 12.92 : Math.pow((srgb + 0.055) / 1.055, 2.4);
|
|
67
|
+
}
|
|
68
|
+
export function computeRelativeLuminance(rgb) {
|
|
69
|
+
if (!rgb || typeof rgb !== "object")
|
|
70
|
+
return 0;
|
|
71
|
+
return 0.2126 * linearizeChannel(rgb.r) + 0.7152 * linearizeChannel(rgb.g) + 0.0722 * linearizeChannel(rgb.b);
|
|
72
|
+
}
|
|
73
|
+
function resolveColorInput(color, label) {
|
|
74
|
+
if (typeof color === "string") {
|
|
75
|
+
const parsed = parseHexColor(color);
|
|
76
|
+
if (!parsed)
|
|
77
|
+
throw new VibecodeError("CONTRACT_INVALID", `Invalid ${label} hex color: ${JSON.stringify(safeDescribe(color))}`);
|
|
78
|
+
return parsed;
|
|
79
|
+
}
|
|
80
|
+
if (!color || typeof color !== "object" || Array.isArray(color) || !Number.isFinite(color.r) || !Number.isFinite(color.g) || !Number.isFinite(color.b)) {
|
|
81
|
+
throw new VibecodeError("CONTRACT_INVALID", `Invalid ${label} RGB color: channels must be finite numbers`);
|
|
82
|
+
}
|
|
83
|
+
return { r: Math.max(0, Math.min(255, color.r)), g: Math.max(0, Math.min(255, color.g)), b: Math.max(0, Math.min(255, color.b)) };
|
|
84
|
+
}
|
|
85
|
+
export function computeContrastRatio(fg, bg) {
|
|
86
|
+
const l1 = computeRelativeLuminance(resolveColorInput(fg, "foreground"));
|
|
87
|
+
const l2 = computeRelativeLuminance(resolveColorInput(bg, "background"));
|
|
88
|
+
return Math.max(1, Math.min(21, (Math.max(l1, l2) + 0.05) / (Math.min(l1, l2) + 0.05)));
|
|
89
|
+
}
|
|
90
|
+
function rgbDist(a, b) {
|
|
91
|
+
return Math.hypot(a.r - b.r, a.g - b.g, a.b - b.b);
|
|
92
|
+
}
|
|
93
|
+
function computeChromaAndSat(rgb) {
|
|
94
|
+
const r = rgb.r / 255, g = rgb.g / 255, b = rgb.b / 255;
|
|
95
|
+
const max = Math.max(r, g, b), min = Math.min(r, g, b), chroma = max - min;
|
|
96
|
+
if (chroma === 0)
|
|
97
|
+
return { chroma: 0, saturation: 0 };
|
|
98
|
+
const lum = (max + min) / 2;
|
|
99
|
+
return { chroma, saturation: lum > 0.5 ? chroma / (2 - max - min) : chroma / (max + min) };
|
|
100
|
+
}
|
|
101
|
+
function isChromatic(c) {
|
|
102
|
+
return c.chroma >= 0.22 && c.saturation >= 0.35;
|
|
103
|
+
}
|
|
104
|
+
function assertValidRgbaImage(img) {
|
|
105
|
+
if (!img || typeof img !== "object")
|
|
106
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", "Invalid RGBA image: expected an object");
|
|
107
|
+
if (!Number.isSafeInteger(img.width) || !Number.isSafeInteger(img.height) || img.width <= 0 || img.height <= 0) {
|
|
108
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", `Invalid RGBA image dimensions: ${safeDescribe(img.width)}x${safeDescribe(img.height)}`);
|
|
109
|
+
}
|
|
110
|
+
if (img.width > MAX_IMAGE_DIM || img.height > MAX_IMAGE_DIM || img.width * img.height > MAX_IMAGE_PIXELS) {
|
|
111
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", `RGBA image dimensions exceed safe cap: ${img.width}x${img.height}`);
|
|
112
|
+
}
|
|
113
|
+
if (!(img.data instanceof Uint8Array))
|
|
114
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", "Invalid RGBA image: data must be a Uint8Array");
|
|
115
|
+
const requiredBytes = img.width * img.height * 4;
|
|
116
|
+
if (img.data.length < requiredBytes) {
|
|
117
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", `Truncated RGBA pixel buffer: expected ${requiredBytes} bytes, got ${img.data.length}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function readCompositedRgb(data, p) {
|
|
121
|
+
const a = data[p + 3];
|
|
122
|
+
if (a >= 255)
|
|
123
|
+
return { r: data[p], g: data[p + 1], b: data[p + 2] };
|
|
124
|
+
if (a <= 0)
|
|
125
|
+
return { r: 255, g: 255, b: 255 };
|
|
126
|
+
const alpha = a / 255, inv = 255 * (1 - alpha);
|
|
127
|
+
return { r: Math.round(data[p] * alpha + inv), g: Math.round(data[p + 1] * alpha + inv), b: Math.round(data[p + 2] * alpha + inv) };
|
|
128
|
+
}
|
|
129
|
+
function addSampleRgb(buckets, r, g, b, isBorder = false) {
|
|
130
|
+
const key = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);
|
|
131
|
+
let entry = buckets.get(key);
|
|
132
|
+
if (!entry) {
|
|
133
|
+
entry = { count: 0, borderCount: 0, r: 0, g: 0, b: 0 };
|
|
134
|
+
buckets.set(key, entry);
|
|
135
|
+
}
|
|
136
|
+
entry.count++;
|
|
137
|
+
entry.r += r;
|
|
138
|
+
entry.g += g;
|
|
139
|
+
entry.b += b;
|
|
140
|
+
if (isBorder)
|
|
141
|
+
entry.borderCount++;
|
|
142
|
+
}
|
|
143
|
+
function addCompositedSample(buckets, data, p, isBorder = false) {
|
|
144
|
+
const a = data[p + 3];
|
|
145
|
+
if (a >= 255) {
|
|
146
|
+
addSampleRgb(buckets, data[p], data[p + 1], data[p + 2], isBorder);
|
|
147
|
+
}
|
|
148
|
+
else if (a <= 0) {
|
|
149
|
+
addSampleRgb(buckets, 255, 255, 255, isBorder);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
const alpha = a / 255, inv = 255 * (1 - alpha);
|
|
153
|
+
addSampleRgb(buckets, Math.round(data[p] * alpha + inv), Math.round(data[p + 1] * alpha + inv), Math.round(data[p + 2] * alpha + inv), isBorder);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function bucketToRgb(e) {
|
|
157
|
+
return { r: Math.round(e.r / e.count), g: Math.round(e.g / e.count), b: Math.round(e.b / e.count) };
|
|
158
|
+
}
|
|
159
|
+
function bucketToCluster(e, totalSamples) {
|
|
160
|
+
const rgb = bucketToRgb(e);
|
|
161
|
+
const { chroma, saturation } = computeChromaAndSat(rgb);
|
|
162
|
+
return {
|
|
163
|
+
rgb, hex: rgbToHex(rgb), count: e.count, borderCount: e.borderCount,
|
|
164
|
+
weight: e.count / (totalSamples || 1), luminance: computeRelativeLuminance(rgb), chroma, saturation
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
export function extractDesignTokens(img) {
|
|
168
|
+
assertValidRgbaImage(img);
|
|
169
|
+
const { width, height, data } = img;
|
|
170
|
+
const totalPixels = width * height;
|
|
171
|
+
const stride = totalPixels <= 120_000 ? 1 : Math.ceil(Math.sqrt(totalPixels / 80_000));
|
|
172
|
+
const borderBand = Math.max(1, Math.min(8, Math.round(Math.min(width, height) * 0.04)));
|
|
173
|
+
const buckets = new Map();
|
|
174
|
+
let totalSamples = 0, totalBorder = 0;
|
|
175
|
+
for (let y = 0; y < height; y += stride) {
|
|
176
|
+
const isBorderY = y < borderBand || y >= height - borderBand;
|
|
177
|
+
const rowOff = y * width * 4;
|
|
178
|
+
for (let x = 0; x < width; x += stride) {
|
|
179
|
+
const isBorder = isBorderY || x < borderBand || x >= width - borderBand;
|
|
180
|
+
addCompositedSample(buckets, data, rowOff + x * 4, isBorder);
|
|
181
|
+
totalSamples++;
|
|
182
|
+
if (isBorder)
|
|
183
|
+
totalBorder++;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
const sortedBuckets = [...buckets.values()].sort((a, b) => b.count - a.count);
|
|
187
|
+
let topBorderBucket = sortedBuckets[0];
|
|
188
|
+
for (let i = 1; i < sortedBuckets.length; i++) {
|
|
189
|
+
if (sortedBuckets[i].borderCount > topBorderBucket.borderCount)
|
|
190
|
+
topBorderBucket = sortedBuckets[i];
|
|
191
|
+
}
|
|
192
|
+
const minSig = totalSamples < 400 ? 1 : Math.max(2, Math.floor(totalSamples * 0.0005));
|
|
193
|
+
let sigEnd = 0;
|
|
194
|
+
while (sigEnd < sortedBuckets.length && sortedBuckets[sigEnd].count >= minSig)
|
|
195
|
+
sigEnd++;
|
|
196
|
+
const keepCount = Math.min(sortedBuckets.length, Math.max(256, sigEnd));
|
|
197
|
+
const clusters = new Array(keepCount);
|
|
198
|
+
let topBorder = null;
|
|
199
|
+
for (let i = 0; i < keepCount; i++) {
|
|
200
|
+
const c = bucketToCluster(sortedBuckets[i], totalSamples);
|
|
201
|
+
clusters[i] = c;
|
|
202
|
+
if (sortedBuckets[i] === topBorderBucket)
|
|
203
|
+
topBorder = c;
|
|
204
|
+
}
|
|
205
|
+
if (!topBorder)
|
|
206
|
+
topBorder = bucketToCluster(topBorderBucket, totalSamples);
|
|
207
|
+
const bg = topBorder && totalBorder > 0 && topBorder.borderCount / totalBorder >= 0.4 && topBorder.weight >= 0.12
|
|
208
|
+
? topBorder : clusters[0];
|
|
209
|
+
const theme = bg.luminance < 0.35 ? "dark" : "light";
|
|
210
|
+
const significant = sigEnd > 0 ? clusters.slice(0, sigEnd) : [];
|
|
211
|
+
const pool = significant.length > 1 ? significant : clusters;
|
|
212
|
+
const distinctFromBg = pool.filter((c) => rgbDist(c.rgb, bg.rgb) >= 16);
|
|
213
|
+
let fg = bg;
|
|
214
|
+
if (distinctFromBg.length > 0) {
|
|
215
|
+
const scored = distinctFromBg
|
|
216
|
+
.map((cluster) => ({ cluster, contrast: computeContrastRatio(cluster.rgb, bg.rgb) }))
|
|
217
|
+
.sort((a, b) => b.contrast - a.contrast || b.cluster.count - a.cluster.count);
|
|
218
|
+
const bestNeutral = scored.find((s) => !isChromatic(s.cluster));
|
|
219
|
+
fg = bestNeutral && bestNeutral.contrast >= 3.0 && bestNeutral.contrast >= scored[0].contrast * 0.85
|
|
220
|
+
? bestNeutral.cluster : scored[0].cluster;
|
|
221
|
+
}
|
|
222
|
+
const surfaceCluster = pool.find((c) => !isChromatic(c) && rgbDist(c.rgb, bg.rgb) >= 8 && rgbDist(c.rgb, fg.rgb) >= 24 && c.weight >= 0.02 && computeContrastRatio(c.rgb, bg.rgb) < 2.2) ?? bg;
|
|
223
|
+
const minAccent = totalSamples < 400 ? 1 : Math.max(2, Math.floor(totalSamples * 0.002));
|
|
224
|
+
const chromaticAll = pool.filter((c) => isChromatic(c) && rgbDist(c.rgb, bg.rgb) >= 24 && rgbDist(c.rgb, fg.rgb) >= 24);
|
|
225
|
+
const chromaticStrong = chromaticAll.filter((c) => c.count >= minAccent);
|
|
226
|
+
const accentCluster = (chromaticStrong.length > 0 ? chromaticStrong : chromaticAll)
|
|
227
|
+
.sort((a, b) => b.saturation - a.saturation || b.chroma - a.chroma || b.count - a.count)[0] ?? null;
|
|
228
|
+
const fgContrast = computeContrastRatio(fg.rgb, bg.rgb);
|
|
229
|
+
const mutedCluster = pool
|
|
230
|
+
.filter((c) => !isChromatic(c) && rgbDist(c.rgb, bg.rgb) >= 16 && rgbDist(c.rgb, fg.rgb) >= 16 && rgbDist(c.rgb, surfaceCluster.rgb) >= 12)
|
|
231
|
+
.map((cluster) => ({ cluster, contrast: computeContrastRatio(cluster.rgb, bg.rgb) }))
|
|
232
|
+
.filter((x) => x.contrast >= 1.5 && x.contrast < fgContrast)
|
|
233
|
+
.sort((a, b) => b.cluster.count - a.cluster.count || b.contrast - a.contrast)[0]?.cluster ?? null;
|
|
234
|
+
const topPalette = clusters.slice(0, 6);
|
|
235
|
+
const paletteTotal = topPalette.reduce((sum, c) => sum + c.count, 0) || 1;
|
|
236
|
+
return {
|
|
237
|
+
background: bg.hex, surface: surfaceCluster.hex, foreground: fg.hex,
|
|
238
|
+
accent: accentCluster ? accentCluster.hex : null, muted: mutedCluster ? mutedCluster.hex : null,
|
|
239
|
+
theme, foregroundContrast: fgContrast,
|
|
240
|
+
accentContrast: accentCluster ? computeContrastRatio(accentCluster.rgb, bg.rgb) : null,
|
|
241
|
+
palette: topPalette.map((c) => ({ hex: c.hex, weight: c.count / paletteTotal }))
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
function getRequiredContrast(level, isLargeText) {
|
|
245
|
+
return level === "AAA" ? (isLargeText ? 4.5 : 7.0) : (isLargeText ? 3.0 : 4.5);
|
|
246
|
+
}
|
|
247
|
+
function sampleRectClusters(img, x0, y0, x1, y1, ringOnly) {
|
|
248
|
+
const { width, data } = img;
|
|
249
|
+
const rw = Math.max(1, x1 - x0), rh = Math.max(1, y1 - y0);
|
|
250
|
+
const step = ringOnly ? Math.max(1, Math.floor((rw + rh) / 400)) : Math.max(1, Math.floor(Math.sqrt((rw * rh) / 16_000)));
|
|
251
|
+
const buckets = new Map();
|
|
252
|
+
let total = 0;
|
|
253
|
+
const add = (x, y) => { addCompositedSample(buckets, data, (y * width + x) * 4); total++; };
|
|
254
|
+
if (ringOnly) {
|
|
255
|
+
for (let x = x0; x < x1; x += step) {
|
|
256
|
+
add(x, y0);
|
|
257
|
+
if (y1 - 1 > y0)
|
|
258
|
+
add(x, y1 - 1);
|
|
259
|
+
}
|
|
260
|
+
for (let y = y0 + 1; y < y1 - 1; y += step) {
|
|
261
|
+
add(x0, y);
|
|
262
|
+
if (x1 - 1 > x0)
|
|
263
|
+
add(x1 - 1, y);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
else {
|
|
267
|
+
for (let y = y0; y < y1; y += step)
|
|
268
|
+
for (let x = x0; x < x1; x += step)
|
|
269
|
+
add(x, y);
|
|
270
|
+
}
|
|
271
|
+
const clusters = [...buckets.values()].sort((a, b) => b.count - a.count).map((e) => ({ rgb: bucketToRgb(e), count: e.count }));
|
|
272
|
+
return {
|
|
273
|
+
dominant: clusters[0]?.rgb ?? { r: 255, g: 255, b: 255 },
|
|
274
|
+
dominance: total > 0 && clusters[0] ? clusters[0].count / total : 0,
|
|
275
|
+
clusters, total
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
function sampleOuterRingBg(img, box) {
|
|
279
|
+
const { width, height, data } = img;
|
|
280
|
+
const x0 = box.x, y0 = box.y, x1 = box.x + box.w, y1 = box.y + box.h;
|
|
281
|
+
const step = Math.max(1, Math.floor((box.w + box.h) / 300));
|
|
282
|
+
const buckets = new Map();
|
|
283
|
+
const add = (x, y) => {
|
|
284
|
+
if (x >= 0 && x < width && y >= 0 && y < height)
|
|
285
|
+
addCompositedSample(buckets, data, (y * width + x) * 4);
|
|
286
|
+
};
|
|
287
|
+
for (let d = 1; d <= 2; d++) {
|
|
288
|
+
for (let x = x0 - d; x < x1 + d; x += step) {
|
|
289
|
+
add(x, y0 - d);
|
|
290
|
+
add(x, y1 - 1 + d);
|
|
291
|
+
}
|
|
292
|
+
for (let y = y0; y < y1; y += step) {
|
|
293
|
+
add(x0 - d, y);
|
|
294
|
+
add(x1 - 1 + d, y);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
const best = [...buckets.values()].sort((a, b) => b.count - a.count)[0];
|
|
298
|
+
return best ? bucketToRgb(best) : sampleRectClusters(img, x0, y0, x1, y1, true).dominant;
|
|
299
|
+
}
|
|
300
|
+
function findActiveRuns(active, minGap) {
|
|
301
|
+
const runs = [];
|
|
302
|
+
let i = 0;
|
|
303
|
+
while (i < active.length) {
|
|
304
|
+
while (i < active.length && !active[i])
|
|
305
|
+
i++;
|
|
306
|
+
if (i >= active.length)
|
|
307
|
+
break;
|
|
308
|
+
const start = i;
|
|
309
|
+
let end = i;
|
|
310
|
+
while (i < active.length) {
|
|
311
|
+
if (active[i]) {
|
|
312
|
+
end = i + 1;
|
|
313
|
+
i++;
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
let gapEnd = i;
|
|
317
|
+
while (gapEnd < active.length && !active[gapEnd])
|
|
318
|
+
gapEnd++;
|
|
319
|
+
if (gapEnd - i >= minGap || gapEnd >= active.length) {
|
|
320
|
+
i = gapEnd;
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
i = gapEnd;
|
|
324
|
+
}
|
|
325
|
+
if (end - start >= 4)
|
|
326
|
+
runs.push([start, end]);
|
|
327
|
+
}
|
|
328
|
+
return runs;
|
|
329
|
+
}
|
|
330
|
+
function scanNonBg(img, box, bg) {
|
|
331
|
+
if (box.w <= 0 || box.h <= 0)
|
|
332
|
+
return null;
|
|
333
|
+
const { width, data } = img;
|
|
334
|
+
const x0 = box.x, y0 = box.y, x1 = box.x + box.w, y1 = box.y + box.h;
|
|
335
|
+
const step = Math.max(1, Math.floor(Math.sqrt((box.w * box.h) / 32_000)));
|
|
336
|
+
const rows = new Uint8Array(box.h);
|
|
337
|
+
const cols = new Uint8Array(box.w);
|
|
338
|
+
let minX = x1, minY = y1, maxX = -1, maxY = -1;
|
|
339
|
+
for (let y = y0; y < y1; y += step) {
|
|
340
|
+
for (let x = x0; x < x1; x += step) {
|
|
341
|
+
if (rgbDist(readCompositedRgb(data, (y * width + x) * 4), bg) >= 12) {
|
|
342
|
+
rows[y - y0] = 1;
|
|
343
|
+
cols[x - x0] = 1;
|
|
344
|
+
if (x < minX)
|
|
345
|
+
minX = x;
|
|
346
|
+
if (x > maxX)
|
|
347
|
+
maxX = x;
|
|
348
|
+
if (y < minY)
|
|
349
|
+
minY = y;
|
|
350
|
+
if (y > maxY)
|
|
351
|
+
maxY = y;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
if (maxX < minX || maxY < minY)
|
|
356
|
+
return null;
|
|
357
|
+
const nx = Math.max(x0, minX - (step - 1)), ny = Math.max(y0, minY - (step - 1));
|
|
358
|
+
const nx1 = Math.min(x1, maxX + step), ny1 = Math.min(y1, maxY + step);
|
|
359
|
+
if (nx1 - nx < 4 || ny1 - ny < 4)
|
|
360
|
+
return null;
|
|
361
|
+
return {
|
|
362
|
+
tight: { x: nx, y: ny, w: nx1 - nx, h: ny1 - ny },
|
|
363
|
+
rowActive: rows.subarray(ny - y0, ny1 - y0),
|
|
364
|
+
colActive: cols.subarray(nx - x0, nx1 - x0),
|
|
365
|
+
step
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
function splitSubRegions(img, box, outerBg, fallbackKind, depth, out, limit, budget) {
|
|
369
|
+
if (out.length >= limit || budget.remaining <= 0 || box.w < 4 || box.h < 4)
|
|
370
|
+
return;
|
|
371
|
+
budget.remaining--;
|
|
372
|
+
const push = (b) => {
|
|
373
|
+
if (out.length >= limit)
|
|
374
|
+
return;
|
|
375
|
+
const kind = fallbackKind === "media_block" && b.w / b.h >= 2.2 && b.h / img.height <= 0.45 ? "text_band" : fallbackKind;
|
|
376
|
+
out.push({ bbox: b, kind });
|
|
377
|
+
};
|
|
378
|
+
if (depth >= 4) {
|
|
379
|
+
push(box);
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
const x0 = box.x, y0 = box.y, x1 = box.x + box.w, y1 = box.y + box.h;
|
|
383
|
+
const ring = sampleRectClusters(img, x0, y0, x1, y1, true);
|
|
384
|
+
if (ring.dominance < (depth === 0 ? 0.5 : 0.8)) {
|
|
385
|
+
push(box);
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
const bg = ring.dominant;
|
|
389
|
+
const scan = scanNonBg(img, box, bg);
|
|
390
|
+
if (!scan) {
|
|
391
|
+
if (outerBg)
|
|
392
|
+
push(box);
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
const { tight, rowActive, colActive, step } = scan;
|
|
396
|
+
const isThinBorder = tight.x - x0 <= 2 && x1 - (tight.x + tight.w) <= 2 && tight.y - y0 <= 2 && y1 - (tight.y + tight.h) <= 2;
|
|
397
|
+
if (outerBg && isThinBorder) {
|
|
398
|
+
const inner = sampleRectClusters(img, tight.x, tight.y, tight.x + tight.w, tight.y + tight.h, false);
|
|
399
|
+
if (inner.clusters.every((c) => rgbDist(c.rgb, inner.dominant) < 12) && rgbDist(inner.dominant, outerBg) < 12) {
|
|
400
|
+
push(box);
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
const hRuns = findActiveRuns(rowActive, Math.max(2, step + 1, Math.round(tight.h * 0.02)));
|
|
405
|
+
if (hRuns.length > 1) {
|
|
406
|
+
for (const [sy, ey] of hRuns) {
|
|
407
|
+
if (out.length >= limit || budget.remaining <= 0)
|
|
408
|
+
break;
|
|
409
|
+
const sub = scanNonBg(img, { x: tight.x, y: tight.y + sy, w: tight.w, h: ey - sy }, bg);
|
|
410
|
+
if (sub)
|
|
411
|
+
splitSubRegions(img, sub.tight, bg, fallbackKind, depth + 1, out, limit, budget);
|
|
412
|
+
}
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
const vRuns = findActiveRuns(colActive, Math.max(6, step + 1, Math.round(tight.w * 0.04)));
|
|
416
|
+
if (vRuns.length > 1) {
|
|
417
|
+
for (const [sx, ex] of vRuns) {
|
|
418
|
+
if (out.length >= limit || budget.remaining <= 0)
|
|
419
|
+
break;
|
|
420
|
+
const sub = scanNonBg(img, { x: tight.x + sx, y: tight.y, w: ex - sx, h: tight.h }, bg);
|
|
421
|
+
if (sub)
|
|
422
|
+
splitSubRegions(img, sub.tight, bg, fallbackKind, depth + 1, out, limit, budget);
|
|
423
|
+
}
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
if (tight.w === box.w && tight.h === box.h) {
|
|
427
|
+
push(box);
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
splitSubRegions(img, tight, bg, fallbackKind, depth + 1, out, limit, budget);
|
|
431
|
+
}
|
|
432
|
+
function downsampleForLayout(img, maxEdge = 640) {
|
|
433
|
+
const longest = Math.max(img.width, img.height);
|
|
434
|
+
if (longest <= maxEdge)
|
|
435
|
+
return img;
|
|
436
|
+
const scale = Math.ceil(longest / maxEdge);
|
|
437
|
+
const w = Math.max(1, Math.floor(img.width / scale));
|
|
438
|
+
const h = Math.max(1, Math.floor(img.height / scale));
|
|
439
|
+
const out = new Uint8Array(w * h * 4);
|
|
440
|
+
const srcW = img.width;
|
|
441
|
+
const src = img.data;
|
|
442
|
+
for (let y = 0; y < h; y++) {
|
|
443
|
+
const sy = y * scale * srcW;
|
|
444
|
+
const dy = y * w;
|
|
445
|
+
for (let x = 0; x < w; x++) {
|
|
446
|
+
const sp = (sy + x * scale) * 4;
|
|
447
|
+
const dp = (dy + x) * 4;
|
|
448
|
+
out[dp] = src[sp];
|
|
449
|
+
out[dp + 1] = src[sp + 1];
|
|
450
|
+
out[dp + 2] = src[sp + 2];
|
|
451
|
+
out[dp + 3] = src[sp + 3];
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
return { width: w, height: h, data: out };
|
|
455
|
+
}
|
|
456
|
+
export function auditWcagContrast(img, options) {
|
|
457
|
+
assertValidRgbaImage(img);
|
|
458
|
+
if (options?.level !== undefined && options.level !== "AA" && options.level !== "AAA") {
|
|
459
|
+
throw new VibecodeError("CONFIG_INVALID", `Invalid WCAG level: ${safeDescribe(options.level)} (expected "AA" or "AAA")`);
|
|
460
|
+
}
|
|
461
|
+
const designTokens = extractDesignTokens(img);
|
|
462
|
+
const level = options?.level ?? "AA";
|
|
463
|
+
const forceLarge = options?.largeText === true;
|
|
464
|
+
const maxRegions = typeof options?.maxRegions === "number" && Number.isFinite(options.maxRegions)
|
|
465
|
+
? Math.max(1, Math.min(MAX_REGIONS_CAP, Math.floor(options.maxRegions)))
|
|
466
|
+
: DEFAULT_MAX_REGIONS;
|
|
467
|
+
const pad = Math.min(3, Math.max(1, Math.ceil(Math.max(img.width, img.height) / 192)));
|
|
468
|
+
const seenBoxes = new Set();
|
|
469
|
+
const detected = [];
|
|
470
|
+
const splitBudget = { remaining: Math.max(128, maxRegions * 8) };
|
|
471
|
+
for (const region of extractLayoutRegions(downsampleForLayout(img), { maxDepth: 3 })) {
|
|
472
|
+
if (detected.length >= maxRegions || splitBudget.remaining <= 0)
|
|
473
|
+
break;
|
|
474
|
+
const ex0 = Math.max(0, Math.min(img.width - 1, Math.round(region.bbox.x * img.width)));
|
|
475
|
+
const ey0 = Math.max(0, Math.min(img.height - 1, Math.round(region.bbox.y * img.height)));
|
|
476
|
+
const ex1 = Math.max(ex0, Math.min(img.width, Math.round((region.bbox.x + region.bbox.w) * img.width)));
|
|
477
|
+
const ey1 = Math.max(ey0, Math.min(img.height, Math.round((region.bbox.y + region.bbox.h) * img.height)));
|
|
478
|
+
const useExact = ex1 - ex0 >= 4 && ey1 - ey0 >= 4 && sampleRectClusters(img, ex0, ey0, ex1, ey1, true).dominance >= 0.8;
|
|
479
|
+
const startBox = useExact
|
|
480
|
+
? { x: ex0, y: ey0, w: ex1 - ex0, h: ey1 - ey0 }
|
|
481
|
+
: { x: Math.max(0, ex0 - pad), y: Math.max(0, ey0 - pad), w: Math.min(img.width, ex1 + pad) - Math.max(0, ex0 - pad), h: Math.min(img.height, ey1 + pad) - Math.max(0, ey0 - pad) };
|
|
482
|
+
const expanded = [];
|
|
483
|
+
splitSubRegions(img, startBox, null, region.kind ?? "media_block", 0, expanded, maxRegions - detected.length, splitBudget);
|
|
484
|
+
for (let i = 0; i < expanded.length; i++) {
|
|
485
|
+
const b = expanded[i].bbox;
|
|
486
|
+
const key = `${b.x},${b.y},${b.w},${b.h}`;
|
|
487
|
+
if (b.w < 4 || b.h < 4 || seenBoxes.has(key))
|
|
488
|
+
continue;
|
|
489
|
+
seenBoxes.add(key);
|
|
490
|
+
detected.push({ id: expanded.length === 1 ? region.id : `${region.id}.${i}`, bbox: b, kind: expanded[i].kind });
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
const focused = detected.filter((r) => r.kind === "text_band" || r.kind === "ui_chrome");
|
|
494
|
+
const candidates = detected.length > 8 && focused.length > 0 ? focused : detected;
|
|
495
|
+
const violations = [];
|
|
496
|
+
let checkedRegions = 0;
|
|
497
|
+
let worstContrastRatio = 21;
|
|
498
|
+
for (const region of candidates) {
|
|
499
|
+
const { bbox } = region;
|
|
500
|
+
const localBg = sampleOuterRingBg(img, bbox);
|
|
501
|
+
const interior = sampleRectClusters(img, bbox.x, bbox.y, bbox.x + bbox.w, bbox.y + bbox.h, false);
|
|
502
|
+
const minInkCount = Math.max(1, Math.floor(interior.total * 0.01));
|
|
503
|
+
const distinctInk = interior.clusters.filter((c) => c.count >= minInkCount && rgbDist(c.rgb, localBg) >= 12);
|
|
504
|
+
if (distinctInk.length === 0)
|
|
505
|
+
continue;
|
|
506
|
+
let bestInk = distinctInk[0].rgb, bestContrast = computeContrastRatio(bestInk, localBg);
|
|
507
|
+
for (let i = 1; i < distinctInk.length; i++) {
|
|
508
|
+
const ratio = computeContrastRatio(distinctInk[i].rgb, localBg);
|
|
509
|
+
if (ratio > bestContrast) {
|
|
510
|
+
bestContrast = ratio;
|
|
511
|
+
bestInk = distinctInk[i].rgb;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
checkedRegions++;
|
|
515
|
+
if (bestContrast < worstContrastRatio)
|
|
516
|
+
worstContrastRatio = bestContrast;
|
|
517
|
+
const isLargeText = forceLarge || (region.kind === "text_band" && bbox.h >= 24);
|
|
518
|
+
const requiredRatio = getRequiredContrast(level, isLargeText);
|
|
519
|
+
if (bestContrast < requiredRatio) {
|
|
520
|
+
violations.push({
|
|
521
|
+
regionId: region.id, bbox, foregroundHex: rgbToHex(bestInk), backgroundHex: rgbToHex(localBg),
|
|
522
|
+
contrastRatio: bestContrast, requiredRatio, level, isLargeText
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
if (checkedRegions === 0 && designTokens.palette.length >= 2 && designTokens.foreground !== designTokens.background) {
|
|
527
|
+
checkedRegions = 1;
|
|
528
|
+
worstContrastRatio = designTokens.foregroundContrast;
|
|
529
|
+
const requiredRatio = getRequiredContrast(level, forceLarge);
|
|
530
|
+
if (worstContrastRatio < requiredRatio) {
|
|
531
|
+
violations.push({
|
|
532
|
+
regionId: "canvas", bbox: { x: 0, y: 0, w: img.width, h: img.height },
|
|
533
|
+
foregroundHex: designTokens.foreground, backgroundHex: designTokens.background,
|
|
534
|
+
contrastRatio: worstContrastRatio, requiredRatio, level, isLargeText: forceLarge
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
return {
|
|
539
|
+
passed: violations.length === 0, level, minRequiredRatio: getRequiredContrast(level, forceLarge),
|
|
540
|
+
checkedRegions, worstContrastRatio, designTokens, violations
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
function deepCloneSafe(value, seen = new WeakMap(), depth = 0) {
|
|
544
|
+
if (value === null || typeof value !== "object")
|
|
545
|
+
return value;
|
|
546
|
+
if (depth >= MAX_CLONE_DEPTH)
|
|
547
|
+
return (Array.isArray(value) ? [] : {});
|
|
548
|
+
const cached = seen.get(value);
|
|
549
|
+
if (cached !== undefined)
|
|
550
|
+
return cached;
|
|
551
|
+
if (value instanceof Date)
|
|
552
|
+
return new Date(value.getTime());
|
|
553
|
+
if (value instanceof RegExp)
|
|
554
|
+
return new RegExp(value.source, value.flags);
|
|
555
|
+
if (value instanceof Uint8Array)
|
|
556
|
+
return new Uint8Array(value);
|
|
557
|
+
if (Array.isArray(value)) {
|
|
558
|
+
const arr = [];
|
|
559
|
+
seen.set(value, arr);
|
|
560
|
+
for (let i = 0; i < value.length; i++)
|
|
561
|
+
arr.push(deepCloneSafe(value[i], seen, depth + 1));
|
|
562
|
+
return arr;
|
|
563
|
+
}
|
|
564
|
+
const out = {};
|
|
565
|
+
seen.set(value, out);
|
|
566
|
+
for (const key of Object.keys(value)) {
|
|
567
|
+
if (isForbiddenObjectKey(key))
|
|
568
|
+
continue;
|
|
569
|
+
const cloned = deepCloneSafe(value[key], seen, depth + 1);
|
|
570
|
+
Object.defineProperty(out, key, { value: cloned, enumerable: true, writable: true, configurable: true });
|
|
571
|
+
}
|
|
572
|
+
return out;
|
|
573
|
+
}
|
|
574
|
+
function isPlainRecord(value) {
|
|
575
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
576
|
+
return false;
|
|
577
|
+
const proto = Object.getPrototypeOf(value);
|
|
578
|
+
return proto === Object.prototype || proto === null;
|
|
579
|
+
}
|
|
580
|
+
function stripPerceptualTolerances(tolerance) {
|
|
581
|
+
const next = isPlainRecord(tolerance) ? deepCloneSafe(tolerance) : {};
|
|
582
|
+
for (const key of PERCEPTUAL_TOLERANCE_KEYS)
|
|
583
|
+
delete next[key];
|
|
584
|
+
return next;
|
|
585
|
+
}
|
|
586
|
+
export function applyVerifyModeToInvariant(invariant, mode) {
|
|
587
|
+
if (typeof mode !== "string" || !VALID_VERIFY_MODES.has(mode)) {
|
|
588
|
+
throw new VibecodeError("CONFIG_INVALID", `Invalid verify mode: ${safeDescribe(mode)} (expected one of ${[...VALID_VERIFY_MODES].join(", ")})`);
|
|
589
|
+
}
|
|
590
|
+
if (!invariant || typeof invariant !== "object" || Array.isArray(invariant)) {
|
|
591
|
+
throw new VibecodeError("CONTRACT_INVALID", "Invalid invariant: expected a non-null object");
|
|
592
|
+
}
|
|
593
|
+
const base = deepCloneSafe(invariant);
|
|
594
|
+
const adjusted = {
|
|
595
|
+
...base,
|
|
596
|
+
tags: Array.isArray(base.tags) ? base.tags : [],
|
|
597
|
+
input: isPlainRecord(base.input) ? base.input : {},
|
|
598
|
+
expected: isPlainRecord(base.expected) ? base.expected : {},
|
|
599
|
+
tolerance: isPlainRecord(base.tolerance) ? base.tolerance : {}
|
|
600
|
+
};
|
|
601
|
+
const decide = (shouldRun, enforceWcagAudit, relaxColorChecks, skipReason) => ({
|
|
602
|
+
mode, shouldRun, ...(skipReason ? { skipReason } : {}), enforceWcagAudit, relaxColorChecks, adjustedInvariant: adjusted
|
|
603
|
+
});
|
|
604
|
+
if (mode === "exact")
|
|
605
|
+
return decide(true, false, false);
|
|
606
|
+
if (mode === "functional") {
|
|
607
|
+
if (invariant.type === "image_layout" || invariant.type === "image_perceptual" || invariant.type === "video_timeline") {
|
|
608
|
+
return decide(false, false, false, "Skipped in functional verification mode");
|
|
609
|
+
}
|
|
610
|
+
if (invariant.type === "web_route") {
|
|
611
|
+
adjusted.tolerance = stripPerceptualTolerances(adjusted.tolerance);
|
|
612
|
+
return decide(true, false, true);
|
|
613
|
+
}
|
|
614
|
+
return decide(true, false, false);
|
|
615
|
+
}
|
|
616
|
+
const enforceWcag = mode === "reskin";
|
|
617
|
+
if (invariant.type === "image_perceptual") {
|
|
618
|
+
if (!enforceWcag)
|
|
619
|
+
return decide(false, false, true, "Skipped raw pixel/color check in structural verification mode");
|
|
620
|
+
adjusted.tolerance = { ...adjusted.tolerance, minSSIM: 0, maxPixelDiffRatio: 1, minPaletteSimilarity: 0 };
|
|
621
|
+
return decide(true, true, true);
|
|
622
|
+
}
|
|
623
|
+
if (invariant.type === "image_layout" || invariant.type === "video_timeline") {
|
|
624
|
+
adjusted.tolerance = { ...adjusted.tolerance, minPaletteSimilarity: 0 };
|
|
625
|
+
return decide(true, enforceWcag, true);
|
|
626
|
+
}
|
|
627
|
+
if (invariant.type === "web_route") {
|
|
628
|
+
adjusted.tolerance = stripPerceptualTolerances(adjusted.tolerance);
|
|
629
|
+
return decide(true, enforceWcag, true);
|
|
630
|
+
}
|
|
631
|
+
return decide(true, false, false);
|
|
632
|
+
}
|
|
633
|
+
function style(text, codes, useColor) {
|
|
634
|
+
return useColor ? `${codes}${text}${ANSI.reset}` : text;
|
|
635
|
+
}
|
|
636
|
+
export function formatPrettyWcagAudit(report, options) {
|
|
637
|
+
const color = Boolean(options?.color);
|
|
638
|
+
const badge = report?.passed ? style("PASS", `${ANSI.bold}${ANSI.green}`, color) : style("FAIL", `${ANSI.bold}${ANSI.red}`, color);
|
|
639
|
+
const levelLabel = cleanTerminal(report?.level) || "AA";
|
|
640
|
+
const checked = safeInt(report?.checkedRegions);
|
|
641
|
+
const tokens = report?.designTokens;
|
|
642
|
+
const violations = Array.isArray(report?.violations) ? report.violations : [];
|
|
643
|
+
const lines = [
|
|
644
|
+
`${style(`WCAG 2.1 ${levelLabel} Contrast Audit`, ANSI.bold, color)}: ${badge} (min ${safeFixed(report?.minRequiredRatio, 1)}:1, worst ${safeFixed(report?.worstContrastRatio, 2)}:1, checked ${checked} ${checked === 1 ? "region" : "regions"})`
|
|
645
|
+
];
|
|
646
|
+
const bgHex = cleanTerminal(tokens?.background) || "#000000";
|
|
647
|
+
const surfaceHex = cleanTerminal(tokens?.surface) || bgHex;
|
|
648
|
+
const fgHex = cleanTerminal(tokens?.foreground) || bgHex;
|
|
649
|
+
const themeLabel = cleanTerminal(tokens?.theme) || "light";
|
|
650
|
+
const tokenParts = [`bg=${bgHex}`, `surface=${surfaceHex}`, `fg=${fgHex} (${safeFixed(tokens?.foregroundContrast, 2)}:1)`];
|
|
651
|
+
const accentHex = cleanTerminal(tokens?.accent);
|
|
652
|
+
if (accentHex)
|
|
653
|
+
tokenParts.push(`accent=${accentHex}${tokens?.accentContrast !== null && tokens?.accentContrast !== undefined ? ` (${safeFixed(tokens.accentContrast, 2)}:1)` : ""}`);
|
|
654
|
+
const mutedHex = cleanTerminal(tokens?.muted);
|
|
655
|
+
if (mutedHex)
|
|
656
|
+
tokenParts.push(`muted=${mutedHex}`);
|
|
657
|
+
lines.push(` ${style(`Design Tokens [${themeLabel}]`, ANSI.cyan, color)}: ${tokenParts.join(" ")}`);
|
|
658
|
+
if (violations.length === 0) {
|
|
659
|
+
lines.push(` ${style("No contrast violations detected.", ANSI.dim, color)}`);
|
|
660
|
+
}
|
|
661
|
+
else {
|
|
662
|
+
lines.push(` ${style(`Violations (${violations.length})`, ANSI.yellow, color)}:`);
|
|
663
|
+
for (const v of violations) {
|
|
664
|
+
const regionId = cleanTerminal(v?.regionId) || "region";
|
|
665
|
+
const fg = cleanTerminal(v?.foregroundHex) || "#000000";
|
|
666
|
+
const bg = cleanTerminal(v?.backgroundHex) || "#000000";
|
|
667
|
+
const vLevel = cleanTerminal(v?.level) || levelLabel;
|
|
668
|
+
const bx = safeInt(v?.bbox?.x), by = safeInt(v?.bbox?.y), bw = safeInt(v?.bbox?.w), bh = safeInt(v?.bbox?.h);
|
|
669
|
+
lines.push(` - [${regionId}] ${fg} on ${bg} -> ${style(`${safeFixed(v?.contrastRatio, 2)}:1`, ANSI.red, color)} < ${safeFixed(v?.requiredRatio, 1)}:1 (${vLevel} ${v?.isLargeText ? "large" : "normal"}) @ (${bx},${by} ${bw}x${bh})`);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
return lines.join("\n");
|
|
673
|
+
}
|