@getchomp/mcp 0.0.0-stage → 0.2.43
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/LICENSE +21 -0
- package/README.md +15 -2
- package/dist/conventionsReference.d.ts +84 -0
- package/dist/conventionsReference.js +313 -0
- package/dist/designSystem/astScanner.d.ts +153 -0
- package/dist/designSystem/astScanner.js +2014 -0
- package/dist/designSystem/baseline.d.ts +65 -0
- package/dist/designSystem/baseline.js +136 -0
- package/dist/designSystem/color.d.ts +35 -0
- package/dist/designSystem/color.js +265 -0
- package/dist/designSystem/config.d.ts +183 -0
- package/dist/designSystem/config.js +469 -0
- package/dist/designSystem/contrast.d.ts +55 -0
- package/dist/designSystem/contrast.js +377 -0
- package/dist/designSystem/cssParser.d.ts +63 -0
- package/dist/designSystem/cssParser.js +469 -0
- package/dist/designSystem/merges.d.ts +57 -0
- package/dist/designSystem/merges.js +94 -0
- package/dist/designSystem/preprocessors.d.ts +46 -0
- package/dist/designSystem/preprocessors.js +1108 -0
- package/dist/designSystem/presentation.d.ts +45 -0
- package/dist/designSystem/presentation.js +796 -0
- package/dist/designSystem/styleVectors.d.ts +27 -0
- package/dist/designSystem/styleVectors.js +484 -0
- package/dist/designSystem/tokens.d.ts +39 -0
- package/dist/designSystem/tokens.js +409 -0
- package/dist/designSystem/topology.d.ts +64 -0
- package/dist/designSystem/topology.js +254 -0
- package/dist/designSystem/types.d.ts +857 -0
- package/dist/designSystem/types.js +113 -0
- package/dist/designSystem/typography.d.ts +25 -0
- package/dist/designSystem/typography.js +244 -0
- package/dist/designSystem/uiShapes.d.ts +74 -0
- package/dist/designSystem/uiShapes.js +285 -0
- package/dist/designSystem/valueClusters.d.ts +76 -0
- package/dist/designSystem/valueClusters.js +281 -0
- package/dist/designSystemPlugin.d.ts +321 -0
- package/dist/designSystemPlugin.js +4730 -0
- package/dist/enrichNodesPlugin.d.ts +160 -0
- package/dist/enrichNodesPlugin.js +957 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.js +1298 -0
- package/dist/intelligence.d.ts +502 -0
- package/dist/intelligence.js +3058 -0
- package/dist/pluginRunner.d.ts +364 -0
- package/dist/pluginRunner.js +1096 -0
- package/dist/plugins/design-system/index.d.ts +14 -0
- package/dist/plugins/design-system/index.js +241 -0
- package/dist/plugins/enrich-nodes/index.d.ts +388 -0
- package/dist/plugins/enrich-nodes/index.js +697 -0
- package/dist/plugins/index.d.ts +15 -0
- package/dist/plugins/index.js +20 -0
- package/dist/plugins/local-shims/index.d.ts +121 -0
- package/dist/plugins/local-shims/index.js +237 -0
- package/dist/shimsPlugin.d.ts +134 -0
- package/dist/shimsPlugin.js +1392 -0
- package/package.json +39 -3
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* packages/mcp/src/designSystem/baseline.ts
|
|
3
|
+
*
|
|
4
|
+
* Diff baseline for the Closed-Loop Frontend Gate ("Query before write, diff after write").
|
|
5
|
+
*
|
|
6
|
+
* The baseline is a *pinned snapshot file* (`.chomp/design-system-baseline.json` by default,
|
|
7
|
+
* or any path in `designSystem.baseline.path`, so it can be committed). It is written only by
|
|
8
|
+
* an explicit snapshot action and is deliberately independent from the `entity_overlays`
|
|
9
|
+
* display layer, which is refreshed by every audit. A tool that re-audits the dirty working
|
|
10
|
+
* tree therefore can never silently move the goalposts of the diff gate.
|
|
11
|
+
*
|
|
12
|
+
* Findings are keyed by *identity* (`category|file|component|value`), not by line number, and
|
|
13
|
+
* compared by *occurrence count*, so a second use of an already-present value is still "new".
|
|
14
|
+
*/
|
|
15
|
+
export type FindingCategory = "undefinedToken" | "arbitraryTailwind" | "primitiveSkip" | "hardcodedLiteral" | "invariantViolation" | "rawPrimitive" | "callSiteOverride" | "composability";
|
|
16
|
+
export interface FindingRecord {
|
|
17
|
+
category: FindingCategory;
|
|
18
|
+
/** Stable identity (no line numbers). */
|
|
19
|
+
key: string;
|
|
20
|
+
filePath: string;
|
|
21
|
+
lineNumber: number;
|
|
22
|
+
label: string;
|
|
23
|
+
snippet?: string;
|
|
24
|
+
}
|
|
25
|
+
export interface DesignSystemBaselineSnapshot {
|
|
26
|
+
schemaVersion: 1;
|
|
27
|
+
createdAt: string;
|
|
28
|
+
pluginVersion: string;
|
|
29
|
+
commitSha?: string;
|
|
30
|
+
/** True when the snapshot was pinned from a working tree with uncommitted changes in scope (explicit override). */
|
|
31
|
+
workingTreeDirty?: boolean;
|
|
32
|
+
mode: string;
|
|
33
|
+
/** `FindingRecord.key` → occurrence count. */
|
|
34
|
+
counts: Record<string, number>;
|
|
35
|
+
/** `pairId::theme` identities failing contrast at snapshot time. */
|
|
36
|
+
failingContrast: string[];
|
|
37
|
+
/** Totals per category at snapshot time (for human review of the file). */
|
|
38
|
+
totals: Record<string, number>;
|
|
39
|
+
}
|
|
40
|
+
export declare function findingKey(category: FindingCategory, filePath: string, component: string, value: string): string;
|
|
41
|
+
export declare function countFindings(records: FindingRecord[]): Record<string, number>;
|
|
42
|
+
export declare function totalsByCategory(records: FindingRecord[]): Record<string, number>;
|
|
43
|
+
export declare const DEFAULT_DESIGN_SYSTEM_BASELINE_REL: ".chomp/plugins/design-system/baseline.json";
|
|
44
|
+
export declare const LEGACY_DESIGN_SYSTEM_BASELINE_REL: ".chomp/design-system-baseline.json";
|
|
45
|
+
export declare function resolveBaselinePath(repoPath: string, configured: string): string;
|
|
46
|
+
export declare function readBaselineSnapshot(path: string): DesignSystemBaselineSnapshot | null;
|
|
47
|
+
export declare function writeBaselineSnapshot(path: string, snapshot: DesignSystemBaselineSnapshot): void;
|
|
48
|
+
export interface FindingDiff {
|
|
49
|
+
/** Live records whose key now occurs more often than in the baseline. */
|
|
50
|
+
added: FindingRecord[];
|
|
51
|
+
/** Baseline keys that now occur less often (or not at all): `key` → occurrences removed. */
|
|
52
|
+
resolved: Array<{
|
|
53
|
+
key: string;
|
|
54
|
+
removed: number;
|
|
55
|
+
}>;
|
|
56
|
+
}
|
|
57
|
+
export declare function diffFindings(baseCounts: Record<string, number>, live: FindingRecord[]): FindingDiff;
|
|
58
|
+
export interface WorkingTreeCheck {
|
|
59
|
+
/** `null` when cleanliness cannot be determined (not a git repo, git unavailable). */
|
|
60
|
+
clean: boolean | null;
|
|
61
|
+
/** Uncommitted paths inside the checked directories (capped). */
|
|
62
|
+
dirtyFiles: string[];
|
|
63
|
+
commitSha?: string;
|
|
64
|
+
}
|
|
65
|
+
export declare function checkWorkingTreeClean(repoPath: string, dirs: string[]): WorkingTreeCheck;
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* packages/mcp/src/designSystem/baseline.ts
|
|
3
|
+
*
|
|
4
|
+
* Diff baseline for the Closed-Loop Frontend Gate ("Query before write, diff after write").
|
|
5
|
+
*
|
|
6
|
+
* The baseline is a *pinned snapshot file* (`.chomp/design-system-baseline.json` by default,
|
|
7
|
+
* or any path in `designSystem.baseline.path`, so it can be committed). It is written only by
|
|
8
|
+
* an explicit snapshot action and is deliberately independent from the `entity_overlays`
|
|
9
|
+
* display layer, which is refreshed by every audit. A tool that re-audits the dirty working
|
|
10
|
+
* tree therefore can never silently move the goalposts of the diff gate.
|
|
11
|
+
*
|
|
12
|
+
* Findings are keyed by *identity* (`category|file|component|value`), not by line number, and
|
|
13
|
+
* compared by *occurrence count*, so a second use of an already-present value is still "new".
|
|
14
|
+
*/
|
|
15
|
+
import { execFileSync } from "node:child_process";
|
|
16
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
17
|
+
import { dirname, resolve } from "node:path";
|
|
18
|
+
export function findingKey(category, filePath, component, value) {
|
|
19
|
+
return `${category}|${filePath}|${component}|${value}`;
|
|
20
|
+
}
|
|
21
|
+
export function countFindings(records) {
|
|
22
|
+
const counts = {};
|
|
23
|
+
for (const r of records)
|
|
24
|
+
counts[r.key] = (counts[r.key] ?? 0) + 1;
|
|
25
|
+
return counts;
|
|
26
|
+
}
|
|
27
|
+
export function totalsByCategory(records) {
|
|
28
|
+
const totals = {};
|
|
29
|
+
for (const r of records)
|
|
30
|
+
totals[r.category] = (totals[r.category] ?? 0) + 1;
|
|
31
|
+
return totals;
|
|
32
|
+
}
|
|
33
|
+
export const DEFAULT_DESIGN_SYSTEM_BASELINE_REL = ".chomp/plugins/design-system/baseline.json";
|
|
34
|
+
export const LEGACY_DESIGN_SYSTEM_BASELINE_REL = ".chomp/design-system-baseline.json";
|
|
35
|
+
export function resolveBaselinePath(repoPath, configured) {
|
|
36
|
+
const primary = resolve(repoPath, configured);
|
|
37
|
+
if (configured === DEFAULT_DESIGN_SYSTEM_BASELINE_REL &&
|
|
38
|
+
!existsSync(primary) &&
|
|
39
|
+
existsSync(resolve(repoPath, LEGACY_DESIGN_SYSTEM_BASELINE_REL))) {
|
|
40
|
+
return resolve(repoPath, LEGACY_DESIGN_SYSTEM_BASELINE_REL);
|
|
41
|
+
}
|
|
42
|
+
return primary;
|
|
43
|
+
}
|
|
44
|
+
export function readBaselineSnapshot(path) {
|
|
45
|
+
if (!existsSync(path))
|
|
46
|
+
return null;
|
|
47
|
+
try {
|
|
48
|
+
const parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
49
|
+
if (parsed &&
|
|
50
|
+
parsed.schemaVersion === 1 &&
|
|
51
|
+
parsed.counts &&
|
|
52
|
+
typeof parsed.counts === "object" &&
|
|
53
|
+
Array.isArray(parsed.failingContrast)) {
|
|
54
|
+
return parsed;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
/* fallthrough: unreadable baseline is treated as missing */
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
export function writeBaselineSnapshot(path, snapshot) {
|
|
63
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
64
|
+
writeFileSync(path, JSON.stringify(snapshot, null, 2) + "\n", "utf8");
|
|
65
|
+
}
|
|
66
|
+
export function diffFindings(baseCounts, live) {
|
|
67
|
+
const liveCounts = countFindings(live);
|
|
68
|
+
const added = [];
|
|
69
|
+
const seenForKey = new Map();
|
|
70
|
+
for (const r of live) {
|
|
71
|
+
const baseN = baseCounts[r.key] ?? 0;
|
|
72
|
+
const n = (seenForKey.get(r.key) ?? 0) + 1;
|
|
73
|
+
seenForKey.set(r.key, n);
|
|
74
|
+
if (n > baseN)
|
|
75
|
+
added.push(r);
|
|
76
|
+
}
|
|
77
|
+
const resolved = [];
|
|
78
|
+
for (const [key, baseN] of Object.entries(baseCounts)) {
|
|
79
|
+
const liveN = liveCounts[key] ?? 0;
|
|
80
|
+
if (liveN < baseN)
|
|
81
|
+
resolved.push({ key, removed: baseN - liveN });
|
|
82
|
+
}
|
|
83
|
+
resolved.sort((a, b) => a.key.localeCompare(b.key));
|
|
84
|
+
return { added, resolved };
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Is the in-scope part of the working tree clean? Only the given directories are checked, so an
|
|
88
|
+
* unrelated backend edit never blocks pinning a frontend baseline.
|
|
89
|
+
*/
|
|
90
|
+
function cleanGitEnv() {
|
|
91
|
+
const env = { ...process.env };
|
|
92
|
+
delete env.GIT_DIR;
|
|
93
|
+
delete env.GIT_WORK_TREE;
|
|
94
|
+
delete env.GIT_INDEX_FILE;
|
|
95
|
+
delete env.GIT_COMMON_DIR;
|
|
96
|
+
delete env.GIT_PREFIX;
|
|
97
|
+
delete env.GIT_OBJECT_DIRECTORY;
|
|
98
|
+
return env;
|
|
99
|
+
}
|
|
100
|
+
export function checkWorkingTreeClean(repoPath, dirs) {
|
|
101
|
+
const gitEnv = cleanGitEnv();
|
|
102
|
+
const run = (args) => execFileSync("git", ["-C", repoPath, ...args], {
|
|
103
|
+
encoding: "utf8",
|
|
104
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
105
|
+
env: gitEnv,
|
|
106
|
+
});
|
|
107
|
+
try {
|
|
108
|
+
run(["rev-parse", "--is-inside-work-tree"]);
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
return { clean: null, dirtyFiles: [] };
|
|
112
|
+
}
|
|
113
|
+
let commitSha;
|
|
114
|
+
try {
|
|
115
|
+
commitSha = run(["rev-parse", "HEAD"]).trim() || undefined;
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
/* repo without commits */
|
|
119
|
+
}
|
|
120
|
+
try {
|
|
121
|
+
const pathspec = dirs.length > 0 ? dirs : ["."];
|
|
122
|
+
const out = run(["status", "--porcelain", "--untracked-files=normal", "--", ...pathspec]);
|
|
123
|
+
const dirtyFiles = out
|
|
124
|
+
.split("\n")
|
|
125
|
+
.map((l) => l.slice(3).trim())
|
|
126
|
+
.filter(Boolean);
|
|
127
|
+
return {
|
|
128
|
+
clean: dirtyFiles.length === 0,
|
|
129
|
+
dirtyFiles: dirtyFiles.slice(0, 20),
|
|
130
|
+
...(commitSha ? { commitSha } : {}),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
return { clean: null, dirtyFiles: [], ...(commitSha ? { commitSha } : {}) };
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* packages/mcp/src/designSystem/color.ts
|
|
3
|
+
*
|
|
4
|
+
* Framework-agnostic color parsing & math for the `design-system` plugin.
|
|
5
|
+
* Supports `#hex`, `rgb()/rgba()` (comma & space syntax), `hsl()/hsla()`, `oklch()`,
|
|
6
|
+
* `oklab()` and a small set of named colors. Anything else (e.g. `color-mix()`,
|
|
7
|
+
* `calc()`, `currentColor`) returns `null` so callers can report an explicit
|
|
8
|
+
* "unresolved" state instead of silently dropping the value (Principle of Incomplete Truth).
|
|
9
|
+
*/
|
|
10
|
+
export interface RgbaColor {
|
|
11
|
+
r: number;
|
|
12
|
+
g: number;
|
|
13
|
+
b: number;
|
|
14
|
+
a: number;
|
|
15
|
+
}
|
|
16
|
+
export interface OklabColor {
|
|
17
|
+
L: number;
|
|
18
|
+
a: number;
|
|
19
|
+
b: number;
|
|
20
|
+
}
|
|
21
|
+
export declare function toOklab(c: RgbaColor): OklabColor;
|
|
22
|
+
/** Euclidean distance in OKLab (≈ perceptual ΔE; ~0.02 is barely noticeable). */
|
|
23
|
+
export declare function oklabDistance(x: OklabColor, y: OklabColor): number;
|
|
24
|
+
export declare function parseCssColorString(raw: string): RgbaColor | null;
|
|
25
|
+
/** True when the *entire* string is a single color literal (hex / rgb / hsl / oklch / oklab). */
|
|
26
|
+
export declare function looksLikeColorLiteral(raw: string): boolean;
|
|
27
|
+
export declare function compositeOver(fg: RgbaColor, bg: RgbaColor): RgbaColor;
|
|
28
|
+
export declare function rgbaToHex(c: RgbaColor): string;
|
|
29
|
+
export declare function computeRelativeLuminance(c: RgbaColor): number;
|
|
30
|
+
export declare function computeWcagContrastRatio(fg: RgbaColor, bg: RgbaColor): number;
|
|
31
|
+
/** Generic, deterministic hue family label used only for *suggested* token names. */
|
|
32
|
+
export declare function describeColor(c: RgbaColor): {
|
|
33
|
+
family: string;
|
|
34
|
+
step: number;
|
|
35
|
+
};
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* packages/mcp/src/designSystem/color.ts
|
|
3
|
+
*
|
|
4
|
+
* Framework-agnostic color parsing & math for the `design-system` plugin.
|
|
5
|
+
* Supports `#hex`, `rgb()/rgba()` (comma & space syntax), `hsl()/hsla()`, `oklch()`,
|
|
6
|
+
* `oklab()` and a small set of named colors. Anything else (e.g. `color-mix()`,
|
|
7
|
+
* `calc()`, `currentColor`) returns `null` so callers can report an explicit
|
|
8
|
+
* "unresolved" state instead of silently dropping the value (Principle of Incomplete Truth).
|
|
9
|
+
*/
|
|
10
|
+
const NAMED_COLORS = {
|
|
11
|
+
black: [0, 0, 0],
|
|
12
|
+
white: [255, 255, 255],
|
|
13
|
+
red: [255, 0, 0],
|
|
14
|
+
green: [0, 128, 0],
|
|
15
|
+
blue: [0, 0, 255],
|
|
16
|
+
yellow: [255, 255, 0],
|
|
17
|
+
orange: [255, 165, 0],
|
|
18
|
+
purple: [128, 0, 128],
|
|
19
|
+
gray: [128, 128, 128],
|
|
20
|
+
grey: [128, 128, 128],
|
|
21
|
+
silver: [192, 192, 192],
|
|
22
|
+
maroon: [128, 0, 0],
|
|
23
|
+
navy: [0, 0, 128],
|
|
24
|
+
teal: [0, 128, 128],
|
|
25
|
+
aqua: [0, 255, 255],
|
|
26
|
+
cyan: [0, 255, 255],
|
|
27
|
+
fuchsia: [255, 0, 255],
|
|
28
|
+
magenta: [255, 0, 255],
|
|
29
|
+
lime: [0, 255, 0],
|
|
30
|
+
olive: [128, 128, 0],
|
|
31
|
+
pink: [255, 192, 203],
|
|
32
|
+
brown: [165, 42, 42],
|
|
33
|
+
};
|
|
34
|
+
function clamp(n, lo, hi) {
|
|
35
|
+
return Math.min(hi, Math.max(lo, n));
|
|
36
|
+
}
|
|
37
|
+
function parseNumberToken(tok, percentScale) {
|
|
38
|
+
const t = tok.trim().toLowerCase();
|
|
39
|
+
if (t === "none")
|
|
40
|
+
return 0;
|
|
41
|
+
if (t.endsWith("%")) {
|
|
42
|
+
const n = Number(t.slice(0, -1));
|
|
43
|
+
return Number.isFinite(n) ? (n / 100) * percentScale : null;
|
|
44
|
+
}
|
|
45
|
+
const n = Number.parseFloat(t.replace(/(deg|rad|turn|grad)$/, ""));
|
|
46
|
+
if (!Number.isFinite(n))
|
|
47
|
+
return null;
|
|
48
|
+
if (t.endsWith("turn"))
|
|
49
|
+
return n * 360;
|
|
50
|
+
if (t.endsWith("rad"))
|
|
51
|
+
return (n * 180) / Math.PI;
|
|
52
|
+
if (t.endsWith("grad"))
|
|
53
|
+
return n * 0.9;
|
|
54
|
+
return n;
|
|
55
|
+
}
|
|
56
|
+
function hslToRgb(h, s, l) {
|
|
57
|
+
const hue = ((h % 360) + 360) % 360;
|
|
58
|
+
const sat = clamp(s, 0, 1);
|
|
59
|
+
const lig = clamp(l, 0, 1);
|
|
60
|
+
const c = (1 - Math.abs(2 * lig - 1)) * sat;
|
|
61
|
+
const x = c * (1 - Math.abs(((hue / 60) % 2) - 1));
|
|
62
|
+
const m = lig - c / 2;
|
|
63
|
+
let rgb;
|
|
64
|
+
if (hue < 60)
|
|
65
|
+
rgb = [c, x, 0];
|
|
66
|
+
else if (hue < 120)
|
|
67
|
+
rgb = [x, c, 0];
|
|
68
|
+
else if (hue < 180)
|
|
69
|
+
rgb = [0, c, x];
|
|
70
|
+
else if (hue < 240)
|
|
71
|
+
rgb = [0, x, c];
|
|
72
|
+
else if (hue < 300)
|
|
73
|
+
rgb = [x, 0, c];
|
|
74
|
+
else
|
|
75
|
+
rgb = [c, 0, x];
|
|
76
|
+
return [
|
|
77
|
+
Math.round((rgb[0] + m) * 255),
|
|
78
|
+
Math.round((rgb[1] + m) * 255),
|
|
79
|
+
Math.round((rgb[2] + m) * 255),
|
|
80
|
+
];
|
|
81
|
+
}
|
|
82
|
+
function srgbChannelToLinear(channel255) {
|
|
83
|
+
const s = channel255 / 255;
|
|
84
|
+
return s <= 0.04045 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4);
|
|
85
|
+
}
|
|
86
|
+
function linearToSrgbChannel(l) {
|
|
87
|
+
const v = l <= 0.0031308 ? 12.92 * l : 1.055 * Math.pow(l, 1 / 2.4) - 0.055;
|
|
88
|
+
return clamp(Math.round(v * 255), 0, 255);
|
|
89
|
+
}
|
|
90
|
+
export function toOklab(c) {
|
|
91
|
+
const r = srgbChannelToLinear(c.r);
|
|
92
|
+
const g = srgbChannelToLinear(c.g);
|
|
93
|
+
const b = srgbChannelToLinear(c.b);
|
|
94
|
+
const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b);
|
|
95
|
+
const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b);
|
|
96
|
+
const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b);
|
|
97
|
+
return {
|
|
98
|
+
L: 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s,
|
|
99
|
+
a: 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s,
|
|
100
|
+
b: 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function oklabToRgba(L, a, b, alpha) {
|
|
104
|
+
const l_ = L + 0.3963377774 * a + 0.2158037573 * b;
|
|
105
|
+
const m_ = L - 0.1055613458 * a - 0.0638541728 * b;
|
|
106
|
+
const s_ = L - 0.0894841775 * a - 1.291485548 * b;
|
|
107
|
+
const l = l_ ** 3;
|
|
108
|
+
const m = m_ ** 3;
|
|
109
|
+
const s = s_ ** 3;
|
|
110
|
+
return {
|
|
111
|
+
r: linearToSrgbChannel(4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s),
|
|
112
|
+
g: linearToSrgbChannel(-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s),
|
|
113
|
+
b: linearToSrgbChannel(-0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s),
|
|
114
|
+
a: clamp(alpha, 0, 1),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/** Euclidean distance in OKLab (≈ perceptual ΔE; ~0.02 is barely noticeable). */
|
|
118
|
+
export function oklabDistance(x, y) {
|
|
119
|
+
return Math.sqrt((x.L - y.L) ** 2 + (x.a - y.a) ** 2 + (x.b - y.b) ** 2);
|
|
120
|
+
}
|
|
121
|
+
export function parseCssColorString(raw) {
|
|
122
|
+
const str = raw.trim().toLowerCase();
|
|
123
|
+
if (!str)
|
|
124
|
+
return null;
|
|
125
|
+
if (str.startsWith("#")) {
|
|
126
|
+
const hex = str.slice(1);
|
|
127
|
+
if (!/^[0-9a-f]+$/.test(hex))
|
|
128
|
+
return null;
|
|
129
|
+
if (hex.length === 3 || hex.length === 4) {
|
|
130
|
+
const r = Number.parseInt(hex[0] + hex[0], 16);
|
|
131
|
+
const g = Number.parseInt(hex[1] + hex[1], 16);
|
|
132
|
+
const b = Number.parseInt(hex[2] + hex[2], 16);
|
|
133
|
+
const a = hex.length === 4 ? Number.parseInt(hex[3] + hex[3], 16) / 255 : 1;
|
|
134
|
+
return { r, g, b, a };
|
|
135
|
+
}
|
|
136
|
+
if (hex.length === 6 || hex.length === 8) {
|
|
137
|
+
const r = Number.parseInt(hex.slice(0, 2), 16);
|
|
138
|
+
const g = Number.parseInt(hex.slice(2, 4), 16);
|
|
139
|
+
const b = Number.parseInt(hex.slice(4, 6), 16);
|
|
140
|
+
const a = hex.length === 8 ? Number.parseInt(hex.slice(6, 8), 16) / 255 : 1;
|
|
141
|
+
return { r, g, b, a };
|
|
142
|
+
}
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
if (str === "transparent")
|
|
146
|
+
return { r: 0, g: 0, b: 0, a: 0 };
|
|
147
|
+
const named = NAMED_COLORS[str];
|
|
148
|
+
if (named)
|
|
149
|
+
return { r: named[0], g: named[1], b: named[2], a: 1 };
|
|
150
|
+
const fn = str.match(/^(rgba?|hsla?|oklch|oklab)\(\s*([^)]+?)\s*\)$/);
|
|
151
|
+
if (!fn)
|
|
152
|
+
return null;
|
|
153
|
+
const kind = fn[1];
|
|
154
|
+
const args = fn[2].replace(/\//g, " / ").split(/[\s,]+/).filter(Boolean);
|
|
155
|
+
const slashIdx = args.indexOf("/");
|
|
156
|
+
const colorArgs = slashIdx === -1 ? args.slice(0, 3) : args.slice(0, slashIdx);
|
|
157
|
+
let alphaTok = slashIdx !== -1 ? args[slashIdx + 1] : args.length > 3 ? args[3] : undefined;
|
|
158
|
+
if (colorArgs.length < 3)
|
|
159
|
+
return null;
|
|
160
|
+
const alpha = alphaTok !== undefined ? parseNumberToken(alphaTok, 1) : 1;
|
|
161
|
+
if (alpha === null)
|
|
162
|
+
return null;
|
|
163
|
+
const a = clamp(alpha, 0, 1);
|
|
164
|
+
if (kind === "rgb" || kind === "rgba") {
|
|
165
|
+
const rgb = colorArgs.map((t) => parseNumberToken(t, 255));
|
|
166
|
+
if (rgb.some((n) => n === null))
|
|
167
|
+
return null;
|
|
168
|
+
return {
|
|
169
|
+
r: clamp(rgb[0], 0, 255),
|
|
170
|
+
g: clamp(rgb[1], 0, 255),
|
|
171
|
+
b: clamp(rgb[2], 0, 255),
|
|
172
|
+
a,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
if (kind === "hsl" || kind === "hsla") {
|
|
176
|
+
const h = parseNumberToken(colorArgs[0], 360);
|
|
177
|
+
const s = parseNumberToken(colorArgs[1], 1);
|
|
178
|
+
const l = parseNumberToken(colorArgs[2], 1);
|
|
179
|
+
if (h === null || s === null || l === null)
|
|
180
|
+
return null;
|
|
181
|
+
// Unitless saturation/lightness in legacy syntax are percentages without `%` in bare triplets
|
|
182
|
+
const sat = colorArgs[1].includes("%") ? s : s > 1 ? s / 100 : s;
|
|
183
|
+
const lig = colorArgs[2].includes("%") ? l : l > 1 ? l / 100 : l;
|
|
184
|
+
const [r, g, b] = hslToRgb(h, sat, lig);
|
|
185
|
+
return { r, g, b, a };
|
|
186
|
+
}
|
|
187
|
+
// oklch / oklab
|
|
188
|
+
const L = parseNumberToken(colorArgs[0], 1);
|
|
189
|
+
if (L === null)
|
|
190
|
+
return null;
|
|
191
|
+
if (kind === "oklch") {
|
|
192
|
+
const C = parseNumberToken(colorArgs[1], 0.4);
|
|
193
|
+
const h = parseNumberToken(colorArgs[2], 360);
|
|
194
|
+
if (C === null || h === null)
|
|
195
|
+
return null;
|
|
196
|
+
const rad = (h * Math.PI) / 180;
|
|
197
|
+
return oklabToRgba(L, C * Math.cos(rad), C * Math.sin(rad), a);
|
|
198
|
+
}
|
|
199
|
+
const A = parseNumberToken(colorArgs[1], 0.4);
|
|
200
|
+
const B = parseNumberToken(colorArgs[2], 0.4);
|
|
201
|
+
if (A === null || B === null)
|
|
202
|
+
return null;
|
|
203
|
+
return oklabToRgba(L, A, B, a);
|
|
204
|
+
}
|
|
205
|
+
/** True when the *entire* string is a single color literal (hex / rgb / hsl / oklch / oklab). */
|
|
206
|
+
export function looksLikeColorLiteral(raw) {
|
|
207
|
+
const s = raw.trim().toLowerCase();
|
|
208
|
+
if (/^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/.test(s))
|
|
209
|
+
return true;
|
|
210
|
+
if (/^(?:rgba?|hsla?|oklch|oklab)\(\s*[^()]+\)$/.test(s)) {
|
|
211
|
+
return parseCssColorString(s) !== null;
|
|
212
|
+
}
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
export function compositeOver(fg, bg) {
|
|
216
|
+
if (fg.a >= 0.999)
|
|
217
|
+
return { r: fg.r, g: fg.g, b: fg.b, a: 1 };
|
|
218
|
+
const a = fg.a;
|
|
219
|
+
return {
|
|
220
|
+
r: Math.round(fg.r * a + bg.r * (1 - a)),
|
|
221
|
+
g: Math.round(fg.g * a + bg.g * (1 - a)),
|
|
222
|
+
b: Math.round(fg.b * a + bg.b * (1 - a)),
|
|
223
|
+
a: 1,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
export function rgbaToHex(c) {
|
|
227
|
+
const to2 = (n) => Math.min(255, Math.max(0, Math.round(n))).toString(16).padStart(2, "0");
|
|
228
|
+
return `#${to2(c.r)}${to2(c.g)}${to2(c.b)}`;
|
|
229
|
+
}
|
|
230
|
+
export function computeRelativeLuminance(c) {
|
|
231
|
+
return (0.2126 * srgbChannelToLinear(c.r) +
|
|
232
|
+
0.7152 * srgbChannelToLinear(c.g) +
|
|
233
|
+
0.0722 * srgbChannelToLinear(c.b));
|
|
234
|
+
}
|
|
235
|
+
export function computeWcagContrastRatio(fg, bg) {
|
|
236
|
+
const effBg = { r: bg.r, g: bg.g, b: bg.b, a: 1 };
|
|
237
|
+
const effFg = compositeOver(fg, effBg);
|
|
238
|
+
const l1 = computeRelativeLuminance(effFg);
|
|
239
|
+
const l2 = computeRelativeLuminance(effBg);
|
|
240
|
+
const lighter = Math.max(l1, l2);
|
|
241
|
+
const darker = Math.min(l1, l2);
|
|
242
|
+
return Math.round(((lighter + 0.05) / (darker + 0.05)) * 100) / 100;
|
|
243
|
+
}
|
|
244
|
+
/** Generic, deterministic hue family label used only for *suggested* token names. */
|
|
245
|
+
export function describeColor(c) {
|
|
246
|
+
const lab = toOklab(c);
|
|
247
|
+
const chroma = Math.sqrt(lab.a * lab.a + lab.b * lab.b);
|
|
248
|
+
const step = Math.round(lab.L * 10) * 10; // 0..100
|
|
249
|
+
if (chroma < 0.03)
|
|
250
|
+
return { family: "neutral", step };
|
|
251
|
+
const hue = ((Math.atan2(lab.b, lab.a) * 180) / Math.PI + 360) % 360;
|
|
252
|
+
const families = [
|
|
253
|
+
[25, "red"],
|
|
254
|
+
[70, "orange"],
|
|
255
|
+
[110, "yellow"],
|
|
256
|
+
[160, "green"],
|
|
257
|
+
[200, "teal"],
|
|
258
|
+
[250, "blue"],
|
|
259
|
+
[310, "purple"],
|
|
260
|
+
[350, "pink"],
|
|
261
|
+
[360, "red"],
|
|
262
|
+
];
|
|
263
|
+
const family = families.find(([max]) => hue < max)?.[1] ?? "red";
|
|
264
|
+
return { family, step };
|
|
265
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* packages/mcp/src/designSystem/config.ts
|
|
3
|
+
*
|
|
4
|
+
* Configuration for the `design-system` plugin. Nothing in the plugin core may assume a
|
|
5
|
+
* particular repo's naming (`--surface-*`, `sg-*`, `<Drawer>` …). Repo-specific knowledge lives
|
|
6
|
+
* here, supplied via the `designSystem` key of `chomp.config.json` at the repo root, and is
|
|
7
|
+
* otherwise *derived* from the code ("extract" mode).
|
|
8
|
+
*
|
|
9
|
+
* Modes
|
|
10
|
+
* - `extract` (default when no system is declared): tiers, theme contexts, atoms and
|
|
11
|
+
* conventions are derived from the code itself.
|
|
12
|
+
* - `adhere`: a declared system (`tokens.sources` and/or `atoms.definitions`) is the single
|
|
13
|
+
* source of truth; anything outside it is reported as drift.
|
|
14
|
+
* - `auto`: `adhere` if the config declares a system, else `extract`.
|
|
15
|
+
*/
|
|
16
|
+
export type DesignSystemMode = "auto" | "extract" | "adhere";
|
|
17
|
+
export type ResolvedDesignSystemMode = "extract" | "adhere";
|
|
18
|
+
export interface AtomMatcher {
|
|
19
|
+
/** Lowercase DOM tag(s), e.g. `"button"` or `["dialog", "aside"]`. */
|
|
20
|
+
tag?: string | string[];
|
|
21
|
+
/** ARIA role, e.g. `"switch"`. */
|
|
22
|
+
role?: string;
|
|
23
|
+
/** Regex source tested against the element's `className` string. */
|
|
24
|
+
classPattern?: string;
|
|
25
|
+
/** Required static attribute values, e.g. `{ "type": "checkbox" }`. */
|
|
26
|
+
attrs?: Record<string, string>;
|
|
27
|
+
}
|
|
28
|
+
export interface AtomDefinition {
|
|
29
|
+
/** Canonical shared component name, e.g. `Button`. */
|
|
30
|
+
name: string;
|
|
31
|
+
description?: string;
|
|
32
|
+
/** Any matcher hitting means a raw DOM element is an un-shared instance of this atom. */
|
|
33
|
+
matchers: AtomMatcher[];
|
|
34
|
+
/** Existing shared component names (case-insensitive) that satisfy this atom. Default: `[name, "Shared"+name]`. */
|
|
35
|
+
sharedComponentNames?: string[];
|
|
36
|
+
}
|
|
37
|
+
export interface PropFamily {
|
|
38
|
+
semanticRole: string;
|
|
39
|
+
propNames: string[];
|
|
40
|
+
canonicalRecommendation?: string;
|
|
41
|
+
}
|
|
42
|
+
export interface ContrastPairConfig {
|
|
43
|
+
fg: string;
|
|
44
|
+
bg: string;
|
|
45
|
+
label?: string;
|
|
46
|
+
minRatio?: number;
|
|
47
|
+
}
|
|
48
|
+
export interface DesignSystemConfig {
|
|
49
|
+
mode: DesignSystemMode;
|
|
50
|
+
scope: {
|
|
51
|
+
/** `"auto"` detects frontend packages from `package.json` dependencies; or explicit package names / repo-relative path prefixes (`*` globs allowed). */
|
|
52
|
+
packages: "auto" | string[];
|
|
53
|
+
/** Package names / path prefixes to always exclude. */
|
|
54
|
+
exclude: string[];
|
|
55
|
+
/** Dependency names that mark a package as frontend when `packages` is `"auto"`. */
|
|
56
|
+
frontendDependencies: string[];
|
|
57
|
+
/** Regex sources for repo-relative file paths to skip (tests, stories, mocks…). */
|
|
58
|
+
excludeFiles: string[];
|
|
59
|
+
};
|
|
60
|
+
tokens: {
|
|
61
|
+
/** Adhere mode: CSS files (repo-relative path or glob) that *define* the system. Tokens declared elsewhere are drift. */
|
|
62
|
+
sources: string[];
|
|
63
|
+
/** Explicit tier regexes (tested against the token name) that override derivation. */
|
|
64
|
+
tiers: {
|
|
65
|
+
primitive: string[];
|
|
66
|
+
semantic: string[];
|
|
67
|
+
component: string[];
|
|
68
|
+
};
|
|
69
|
+
/** Token-name prefixes owned by third parties; never reported as undefined (`--tw-`, `--radix-`). */
|
|
70
|
+
externalPrefixes: string[];
|
|
71
|
+
};
|
|
72
|
+
themes: {
|
|
73
|
+
/** Regex sources tested against `<at-rule> <selector>`. */
|
|
74
|
+
dark: string[];
|
|
75
|
+
light: string[];
|
|
76
|
+
};
|
|
77
|
+
contrast: {
|
|
78
|
+
minRatio: number;
|
|
79
|
+
/** Explicit fg/bg token pairs always evaluated. */
|
|
80
|
+
pairs: ContrastPairConfig[];
|
|
81
|
+
/** Evaluate `color: var(--x)` + `background: var(--y)` pairs found in the same CSS rule. */
|
|
82
|
+
discoverFromRules: boolean;
|
|
83
|
+
/** Evaluate `--x-foreground` on `--x` and `--foreground` on `--background` conventions. */
|
|
84
|
+
discoverFromNaming: boolean;
|
|
85
|
+
/** Token used as the opaque base when compositing translucent backgrounds (default: probes common names). */
|
|
86
|
+
canvasToken?: string;
|
|
87
|
+
};
|
|
88
|
+
styling: {
|
|
89
|
+
/** Extra class-name prefixes that always count as stylesheet classes (classes found in the repo's CSS are detected automatically). */
|
|
90
|
+
cssClassPrefixes: string[];
|
|
91
|
+
/** Canvas (PixiJS) provenance conventions. Empty by default: no repo's recorder API is assumed. */
|
|
92
|
+
canvas: {
|
|
93
|
+
slotRecorderFactories: string[];
|
|
94
|
+
cssValueHelpers: string[];
|
|
95
|
+
tokenReaderHelpers: string[];
|
|
96
|
+
};
|
|
97
|
+
tailwind: {
|
|
98
|
+
/** `allow`: Tailwind's default palette (`bg-red-500`) is not drift. `flag`: always drift. */
|
|
99
|
+
defaultPalette: "allow" | "flag";
|
|
100
|
+
/** Palette names always treated as raw primitives (in addition to palettes derived from the repo's own primitive tokens). */
|
|
101
|
+
rawPalettes: string[];
|
|
102
|
+
/** Palette names never treated as raw primitives. */
|
|
103
|
+
allowedPalettes: string[];
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
atoms: {
|
|
107
|
+
/** `defaults`: built-in generic HTML-semantic catalog; `extend`: defaults + `definitions`; `replace`: only `definitions`. */
|
|
108
|
+
catalog: "defaults" | "extend" | "replace";
|
|
109
|
+
definitions: AtomDefinition[];
|
|
110
|
+
/** In extract mode, un-shared raw instances are only reported when a shared component exists OR this many instances are found. */
|
|
111
|
+
minUnsharedInstances: number;
|
|
112
|
+
};
|
|
113
|
+
concepts: {
|
|
114
|
+
/** Name-suffix abbreviations normalised before grouping (`Btn` → `Button`). */
|
|
115
|
+
abbreviations: Record<string, string>;
|
|
116
|
+
minGroupSize: number;
|
|
117
|
+
/** Min Jaccard similarity of (props ∪ DOM tags ∪ tokens) for components to be called parallel twins. */
|
|
118
|
+
minSimilarity: number;
|
|
119
|
+
};
|
|
120
|
+
propFamilies: PropFamily[];
|
|
121
|
+
clustering: {
|
|
122
|
+
/** Max OKLab distance for two literal colors to be one cluster. */
|
|
123
|
+
colorThreshold: number;
|
|
124
|
+
/** Max OKLab distance for a cluster to be matched to an existing token. */
|
|
125
|
+
tokenMatchThreshold: number;
|
|
126
|
+
/** Max px distance (or 4%, whichever is larger) for two lengths to be one cluster. */
|
|
127
|
+
lengthTolerancePx: number;
|
|
128
|
+
minOccurrences: number;
|
|
129
|
+
maxClusters: number;
|
|
130
|
+
};
|
|
131
|
+
composability: {
|
|
132
|
+
/** Atoms must not import these (modules bind them to a data layer / router). */
|
|
133
|
+
forbiddenImports: string[];
|
|
134
|
+
flagRootMargin: boolean;
|
|
135
|
+
flagFixedWidth: boolean;
|
|
136
|
+
};
|
|
137
|
+
invariants: {
|
|
138
|
+
/** Regex source: JSX props matching this must not receive raw color literals. */
|
|
139
|
+
colorPropPattern: string;
|
|
140
|
+
};
|
|
141
|
+
baseline: {
|
|
142
|
+
/** Repo-relative path of the diff baseline snapshot. */
|
|
143
|
+
path: string;
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
export declare const DEFAULT_FRONTEND_DEPENDENCIES: readonly string[];
|
|
147
|
+
/** Generic, HTML-semantic atom catalog. Order matters: first matching definition wins per element. */
|
|
148
|
+
export declare const DEFAULT_ATOM_CATALOG: readonly AtomDefinition[];
|
|
149
|
+
export declare const DEFAULT_DARK_THEME_PATTERNS: readonly string[];
|
|
150
|
+
export declare const DEFAULT_LIGHT_THEME_PATTERNS: readonly string[];
|
|
151
|
+
export declare const DEFAULT_PROP_FAMILIES: readonly PropFamily[];
|
|
152
|
+
export declare const DEFAULT_DESIGN_SYSTEM_CONFIG: DesignSystemConfig;
|
|
153
|
+
export interface ResolvedDesignSystemConfig {
|
|
154
|
+
config: DesignSystemConfig;
|
|
155
|
+
/** Absolute path of the config file that supplied overrides, if any. */
|
|
156
|
+
configPath: string | null;
|
|
157
|
+
/** True when the user declared at least one system source / atom definition. */
|
|
158
|
+
declaresSystem: boolean;
|
|
159
|
+
mode: ResolvedDesignSystemMode;
|
|
160
|
+
modeReason: string;
|
|
161
|
+
/** Non-fatal problems found in the user config (invalid regexes, bad types). */
|
|
162
|
+
warnings: string[];
|
|
163
|
+
}
|
|
164
|
+
export declare function validateRegexSources(label: string, sources: readonly string[], warnings: string[]): string[];
|
|
165
|
+
export declare function compileRegexes(sources: readonly string[], flags?: string): RegExp[];
|
|
166
|
+
/** Converts a repo-relative glob (`*`, `**`) or plain prefix to a RegExp. */
|
|
167
|
+
export declare function globToRegExp(glob: string): RegExp;
|
|
168
|
+
export declare function resolveDesignSystemConfig(repoPath: string, overrides?: Partial<DesignSystemConfig> | Record<string, unknown>): ResolvedDesignSystemConfig;
|
|
169
|
+
export interface DetectedPackageScope {
|
|
170
|
+
/** Repo-relative POSIX directory ("." for the root). */
|
|
171
|
+
dir: string;
|
|
172
|
+
name?: string;
|
|
173
|
+
frontend: boolean;
|
|
174
|
+
/** Why the package was included / excluded. */
|
|
175
|
+
reason: string;
|
|
176
|
+
}
|
|
177
|
+
export interface FrontendScope {
|
|
178
|
+
packages: DetectedPackageScope[];
|
|
179
|
+
includeFile: (relPath: string) => boolean;
|
|
180
|
+
/** Repo-relative dirs of included packages. */
|
|
181
|
+
includedDirs: string[];
|
|
182
|
+
}
|
|
183
|
+
export declare function detectFrontendScope(files: string[], readFile: (relPath: string) => string | null, config: DesignSystemConfig): FrontendScope;
|