@tbox.cn/app-template-agent 1.0.0-alpha.8 → 1.0.0-alpha.9
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/.gitignore +7 -0
- package/.tbox/app.json +1 -1
- package/.tbox/default-modules.json +1 -1
- package/.tbox/factory/chat.json +9 -0
- package/.tbox/miniapp-containers.json +1 -1
- package/AGENTS.md +4 -2
- package/apps/client/package.json +1 -0
- package/apps/client/scripts/._color-scan.test.mjs +0 -0
- package/apps/client/scripts/._experience-checks.mjs +0 -0
- package/apps/client/scripts/._geometry-kernel.test.mjs +0 -0
- package/apps/client/scripts/._in-process-checks.mjs +0 -0
- package/apps/client/scripts/._probe-checks.mjs +0 -0
- package/apps/client/scripts/._sample-entries.test.mjs +0 -0
- package/apps/client/scripts/._storyboard.test.mjs +0 -0
- package/apps/client/scripts/._theme-kernel.mjs +0 -0
- package/apps/client/scripts/._theme-kernel.test.mjs +0 -0
- package/apps/client/scripts/acceptance-changes.test.mjs +97 -0
- package/apps/client/scripts/check-evidence.mjs +361 -94
- package/apps/client/scripts/color-scan.mjs +66 -0
- package/apps/client/scripts/color-scan.test.mjs +25 -0
- package/apps/client/scripts/evidence-gate.mjs +248 -46
- package/apps/client/scripts/evidence-gate.test.mjs +63 -5
- package/apps/client/scripts/experience-checks.mjs +163 -0
- package/apps/client/scripts/geometry-kernel.test.mjs +92 -0
- package/apps/client/scripts/in-process-checks.mjs +51 -0
- package/apps/client/scripts/lib/._card-registry.mjs +0 -0
- package/apps/client/scripts/lib/._card-registry.test.mjs +0 -0
- package/apps/client/scripts/lib/._evidence-inputs.mjs +0 -0
- package/apps/client/scripts/lib/._evidence-inputs.test.mjs +0 -0
- package/apps/client/scripts/lib/._static-render.mjs +0 -0
- package/apps/client/scripts/lib/._static-render.test.mjs +0 -0
- package/apps/client/scripts/lib/card-registry.mjs +81 -0
- package/apps/client/scripts/lib/card-registry.test.mjs +59 -0
- package/apps/client/scripts/lib/evidence-inputs.mjs +91 -0
- package/apps/client/scripts/lib/evidence-inputs.test.mjs +27 -0
- package/apps/client/scripts/lib/static-render.mjs +35 -0
- package/apps/client/scripts/lib/static-render.test.mjs +21 -0
- package/apps/client/scripts/probe-checks.mjs +85 -0
- package/apps/client/scripts/render-card.mjs +76 -60
- package/apps/client/scripts/sample-entries.test.mjs +31 -0
- package/apps/client/scripts/storyboard.test.mjs +149 -0
- package/apps/client/scripts/surface-css.mjs +5 -2
- package/apps/client/scripts/theme-kernel.mjs +236 -0
- package/apps/client/scripts/theme-kernel.test.mjs +136 -0
- package/apps/client/src/styles/brand.css +11 -11
- package/apps/client/src/styles/tailwind.css +3 -4
- package/apps/client/vite.config.ts +2 -1
- package/apps/server/scripts/probe.mjs +6 -1
- package/apps/server/scripts/scenario-lib.mjs +54 -7
- package/apps/server/scripts/scenario.mjs +41 -7
- package/config/AGENTS.md +1 -0
- package/config/chat.json +21 -9
- package/docs/agent/card.md +1 -0
- package/docs/agent/kit.md +1 -1
- package/docs/agent/shell.md +1 -0
- package/docs/agent/style.md +3 -1
- package/docs/agent/verify.md +37 -8
- package/package.json +9 -6
- package/packages/agent-ui-kit/README.md +21 -1
- package/packages/agent-ui-kit/catalog.json +1 -1
- package/packages/agent-ui-kit/package.json +9 -1
- package/packages/agent-ui-kit/src/ChatPage.tsx +30 -1
- package/packages/agent-ui-kit/src/card/primitives.test.tsx +10 -0
- package/packages/agent-ui-kit/src/card/primitives.tsx +14 -10
- package/packages/agent-ui-kit/src/card/view-card.tsx +6 -1
- package/packages/agent-ui-kit/src/chat-config.ts +11 -7
- package/packages/agent-ui-kit/src/components/ActionBar/DebugButton.test.tsx +3 -0
- package/packages/agent-ui-kit/src/components/ActionBar/DebugButton.tsx +2 -7
- package/packages/agent-ui-kit/src/components/BottomSheet/._index.test.tsx +0 -0
- package/packages/agent-ui-kit/src/components/BottomSheet/._index.tsx +0 -0
- package/packages/agent-ui-kit/src/components/BottomSheet/index.test.tsx +90 -0
- package/packages/agent-ui-kit/src/components/BottomSheet/index.tsx +129 -0
- package/packages/agent-ui-kit/src/components/ChatInput/index.test.tsx +2 -2
- package/packages/agent-ui-kit/src/components/ChatInput/index.tsx +21 -16
- package/packages/agent-ui-kit/src/components/HistoryEntry/index.tsx +1 -0
- package/packages/agent-ui-kit/src/components/QuickCommands/index.tsx +2 -2
- package/packages/agent-ui-kit/src/components/ScrollToBottomButton/index.tsx +7 -3
- package/packages/agent-ui-kit/src/components/ToolCallBlock/index.tsx +4 -2
- package/packages/agent-ui-kit/src/index.ts +4 -0
- package/packages/agent-ui-kit/src/shell.contract.json +9 -0
- package/packages/agent-ui-kit/src/shell.test.ts +10 -0
- package/packages/agent-ui-kit/src/shell.ts +15 -0
- package/packages/agent-ui-kit/src/theme/contract.json +78 -1
- package/packages/agent-ui-kit/src/theme/theme-contract.test.ts +16 -0
- package/packages/agent-ui-kit/src/theme/tokens.css +1 -1
- package/pnpm-lock.template.yaml +97 -91
- package/pnpm-workspace.template.yaml +10 -10
- package/profiles/mall/._chat.overrides.json +0 -0
- package/profiles/mall/chat.overrides.json +12 -0
- package/profiles/mall/config/chat.json +14 -6
- package/profiles/mall.json +1 -1
- package/scripts/._deliver.mjs +0 -0
- package/scripts/._shell-probe.mjs +0 -0
- package/scripts/._storyboard.mjs +0 -0
- package/scripts/check-fast.mjs +1 -0
- package/scripts/checks.json +294 -17
- package/scripts/deliver.mjs +183 -0
- package/scripts/lib/._chrome-pipe.mjs +0 -0
- package/scripts/lib/._geometry-kernel.mjs +0 -0
- package/scripts/lib/chrome-pipe.mjs +95 -0
- package/scripts/lib/geometry-kernel.mjs +137 -0
- package/scripts/shell-probe.mjs +283 -0
- package/scripts/storyboard.mjs +350 -0
- package/profiles/mall/config/llm_provider.json +0 -5
- package/profiles/mall/config/plugins.json +0 -3
- package/scripts/gen-tailwind-sources.mjs +0 -55
- /package/{profiles/mall/config/._llm_provider.json → .tbox/factory/._chat.json} +0 -0
- /package/{profiles/mall/config/._plugins.json → apps/client/scripts/._acceptance-changes.test.mjs} +0 -0
- /package/{scripts/._gen-tailwind-sources.mjs → apps/client/scripts/._color-scan.mjs} +0 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 色彩内核:令牌解析、对比度、OKLCH 派生与最小改动调校。
|
|
3
|
+
*
|
|
4
|
+
* 门禁(shell.contrast)、pnpm exec tbox-app theme 三个子命令与最近令牌建议共用这一份实现:
|
|
5
|
+
* 判据(阈值与对比度对)在 packages/agent-ui-kit/src/theme/contract.json 的 pairs 段,
|
|
6
|
+
* 这里只做数学——不认识「品牌」「商圈」,也不写文件。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const clamp = (value, low, high) => Math.min(high, Math.max(low, value));
|
|
10
|
+
|
|
11
|
+
/** #rgb / #rrggbb / rgb() / rgba() / oklch() → {r,g,b,a}(0–255,a 0–1);不认识返回 null。 */
|
|
12
|
+
export function parseColor(value) {
|
|
13
|
+
if (!value) return null;
|
|
14
|
+
const text = String(value).trim();
|
|
15
|
+
const oklch = /^oklch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(?:deg|rad|grad|turn)?(?:\s*\/\s*([\d.]+)(%?))?\s*\)$/i.exec(text);
|
|
16
|
+
if (oklch) {
|
|
17
|
+
const l = oklch[2] === '%' ? Number(oklch[1]) / 100 : Number(oklch[1]);
|
|
18
|
+
const alpha = oklch[5] === undefined ? 1 : oklch[6] === '%' ? Number(oklch[5]) / 100 : Number(oklch[5]);
|
|
19
|
+
return oklchToRgb({ l, c: Number(oklch[3]), h: Number(oklch[4]), a: alpha });
|
|
20
|
+
}
|
|
21
|
+
const rgb = /^rgba?\(\s*([\d.]+)[\s,]+([\d.]+)[\s,]+([\d.]+)(?:[\s,/]+([\d.]+)(%?))?\s*\)$/i.exec(text);
|
|
22
|
+
if (rgb) {
|
|
23
|
+
const alpha = rgb[4] === undefined ? 1 : rgb[5] === '%' ? Number(rgb[4]) / 100 : Number(rgb[4]);
|
|
24
|
+
return { r: Number(rgb[1]), g: Number(rgb[2]), b: Number(rgb[3]), a: alpha };
|
|
25
|
+
}
|
|
26
|
+
const hex = /^#([0-9a-f]{3,8})$/i.exec(text);
|
|
27
|
+
if (!hex) return null;
|
|
28
|
+
const raw = hex[1];
|
|
29
|
+
const expand = (part) => parseInt(part.length === 1 ? part + part : part, 16);
|
|
30
|
+
if (raw.length === 3 || raw.length === 4) {
|
|
31
|
+
return { r: expand(raw[0]), g: expand(raw[1]), b: expand(raw[2]), a: raw.length === 4 ? expand(raw[3]) / 255 : 1 };
|
|
32
|
+
}
|
|
33
|
+
if (raw.length === 6 || raw.length === 8) {
|
|
34
|
+
return {
|
|
35
|
+
r: parseInt(raw.slice(0, 2), 16),
|
|
36
|
+
g: parseInt(raw.slice(2, 4), 16),
|
|
37
|
+
b: parseInt(raw.slice(4, 6), 16),
|
|
38
|
+
a: raw.length === 8 ? parseInt(raw.slice(6, 8), 16) / 255 : 1,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function toHex({ r, g, b }) {
|
|
45
|
+
const part = (value) => Math.round(clamp(value, 0, 255)).toString(16).padStart(2, '0');
|
|
46
|
+
return '#' + part(r) + part(g) + part(b);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const toLinear = (channel) => {
|
|
50
|
+
const c = channel / 255;
|
|
51
|
+
return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const fromLinear = (value) => {
|
|
55
|
+
const c = value <= 0.0031308 ? value * 12.92 : 1.055 * value ** (1 / 2.4) - 0.055;
|
|
56
|
+
return clamp(Math.round(c * 255), 0, 255);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/** sRGB → OKLab → OKLCH(h 单位度)。 */
|
|
60
|
+
export function toOklch(rgb) {
|
|
61
|
+
const r = toLinear(rgb.r);
|
|
62
|
+
const g = toLinear(rgb.g);
|
|
63
|
+
const b = toLinear(rgb.b);
|
|
64
|
+
const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b);
|
|
65
|
+
const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b);
|
|
66
|
+
const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b);
|
|
67
|
+
const L = 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s;
|
|
68
|
+
const A = 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s;
|
|
69
|
+
const B = 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s;
|
|
70
|
+
const c = Math.sqrt(A * A + B * B);
|
|
71
|
+
let h = (Math.atan2(B, A) * 180) / Math.PI;
|
|
72
|
+
if (h < 0) h += 360;
|
|
73
|
+
return { l: L, c, h };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** OKLCH → sRGB;超色域时逐级降 C,直到落回 0–255。 */
|
|
77
|
+
export function oklchToRgb({ l, c, h, a = 1 }) {
|
|
78
|
+
const convert = (chroma) => {
|
|
79
|
+
const hr = (h * Math.PI) / 180;
|
|
80
|
+
const A = Math.cos(hr) * chroma;
|
|
81
|
+
const B = Math.sin(hr) * chroma;
|
|
82
|
+
const l_ = (l + 0.3963377774 * A + 0.2158037573 * B) ** 3;
|
|
83
|
+
const m_ = (l - 0.1055613458 * A - 0.0638541728 * B) ** 3;
|
|
84
|
+
const s_ = (l - 0.0894841775 * A - 1.291485548 * B) ** 3;
|
|
85
|
+
return {
|
|
86
|
+
r: 4.0767416621 * l_ - 3.3077115913 * m_ + 0.2309699292 * s_,
|
|
87
|
+
g: -1.2684380046 * l_ + 2.6097574011 * m_ - 0.3413193965 * s_,
|
|
88
|
+
b: -0.0041960863 * l_ - 0.7034186147 * m_ + 1.707614701 * s_,
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
let chroma = c;
|
|
92
|
+
let linear = convert(chroma);
|
|
93
|
+
for (let i = 0; i < 24 && (linear.r < -0.0001 || linear.g < -0.0001 || linear.b < -0.0001 || linear.r > 1.0001 || linear.g > 1.0001 || linear.b > 1.0001); i += 1) {
|
|
94
|
+
chroma *= 0.92;
|
|
95
|
+
linear = convert(chroma);
|
|
96
|
+
}
|
|
97
|
+
return { r: fromLinear(linear.r), g: fromLinear(linear.g), b: fromLinear(linear.b), a };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const luminance = ({ r, g, b }) => 0.2126 * toLinear(r) + 0.7152 * toLinear(g) + 0.0722 * toLinear(b);
|
|
101
|
+
|
|
102
|
+
/** WCAG 2.x 对比度(1–21)。 */
|
|
103
|
+
export function contrastRatio(fg, bg) {
|
|
104
|
+
const a = luminance(fg);
|
|
105
|
+
const b = luminance(bg);
|
|
106
|
+
const high = Math.max(a, b);
|
|
107
|
+
const low = Math.min(a, b);
|
|
108
|
+
return (high + 0.05) / (low + 0.05);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** 一组 CSS 文件里的 --tbox-* 声明;后读覆盖先读(与门禁的 parseCssTokens 同语义)。 */
|
|
112
|
+
export function readTokens(cssTexts) {
|
|
113
|
+
const map = new Map();
|
|
114
|
+
for (const text of cssTexts) {
|
|
115
|
+
if (typeof text !== 'string') continue;
|
|
116
|
+
for (const match of text.matchAll(/(--tbox-[a-z0-9-]+)\s*:\s*([^;}\n]+)/gi)) {
|
|
117
|
+
map.set(match[1], match[2].trim());
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return map;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** 逐对判定:pairs 来自 contract.json,颜色解析不出来记 skipped。 */
|
|
124
|
+
export function checkPairs(tokens, pairs) {
|
|
125
|
+
const rows = [];
|
|
126
|
+
for (const pair of pairs ?? []) {
|
|
127
|
+
const fgRaw = tokens.get(pair.fg);
|
|
128
|
+
const bgRaw = tokens.get(pair.bg);
|
|
129
|
+
const fg = parseColor(fgRaw);
|
|
130
|
+
const bg = parseColor(bgRaw);
|
|
131
|
+
if (!fg || !bg) {
|
|
132
|
+
rows.push({ fg: pair.fg, bg: pair.bg, min: pair.min ?? 4.5, usage: pair.usage, ratio: null, ok: false, skipped: true });
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
const ratio = contrastRatio(fg, bg);
|
|
136
|
+
rows.push({ fg: pair.fg, bg: pair.bg, min: pair.min ?? 4.5, usage: pair.usage, ratio: Math.round(ratio * 100) / 100, ok: ratio >= (pair.min ?? 4.5) - 1e-9 });
|
|
137
|
+
}
|
|
138
|
+
return rows;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** 最近令牌(OKLab 欧氏距离);用于「这个硬编码色值最像哪个令牌」。 */
|
|
142
|
+
export function nearestToken(color, tokens) {
|
|
143
|
+
const target = parseColor(color);
|
|
144
|
+
if (!target) return undefined;
|
|
145
|
+
const lab = toOklch(target);
|
|
146
|
+
let best;
|
|
147
|
+
for (const [name, raw] of tokens) {
|
|
148
|
+
const parsed = parseColor(raw);
|
|
149
|
+
if (!parsed) continue;
|
|
150
|
+
const candidate = toOklch(parsed);
|
|
151
|
+
const dh = ((candidate.h - lab.h + 540) % 360) - 180;
|
|
152
|
+
const distance = Math.sqrt((candidate.l - lab.l) ** 2 + (candidate.c - lab.c) ** 2 + (dh / 360) ** 2);
|
|
153
|
+
if (!best || distance < best.distance) best = { name, distance };
|
|
154
|
+
}
|
|
155
|
+
return best ? { token: best.name, delta: Math.round(best.distance * 1000) / 10 } : undefined;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 最小改动调校:只动未达标对的前景(文字类),必要时才动背景(bg-*)。
|
|
160
|
+
* 固定色相与彩度,只调 L;同一令牌出现在多对里取最严目标。返回改动表与无解项。
|
|
161
|
+
*/
|
|
162
|
+
export function tune(tokens, pairs, options = {}) {
|
|
163
|
+
const stepLimit = options.stepLimit ?? 60;
|
|
164
|
+
const working = new Map(tokens);
|
|
165
|
+
const changes = new Map();
|
|
166
|
+
const cycle = 4;
|
|
167
|
+
for (let pass = 0; pass < cycle; pass += 1) {
|
|
168
|
+
let touched = false;
|
|
169
|
+
for (const row of checkPairs(working, pairs)) {
|
|
170
|
+
if (row.ok) continue;
|
|
171
|
+
const target = row.fg.startsWith('--tbox-bg-') ? row.bg : row.fg;
|
|
172
|
+
const raw = working.get(target);
|
|
173
|
+
const parsed = parseColor(raw);
|
|
174
|
+
if (!parsed) continue;
|
|
175
|
+
const base = toOklch(parsed);
|
|
176
|
+
const other = parseColor(working.get(target === row.fg ? row.bg : row.fg));
|
|
177
|
+
if (!other) continue;
|
|
178
|
+
const otherL = toOklch(other).l;
|
|
179
|
+
const direction = base.l > otherL ? 1 : -1;
|
|
180
|
+
let applied;
|
|
181
|
+
for (let step = 1; step <= stepLimit; step += 1) {
|
|
182
|
+
const l = clamp(base.l + direction * 0.004 * step, 0, 1);
|
|
183
|
+
const candidate = oklchToRgb({ l, c: base.c, h: base.h });
|
|
184
|
+
const ratio = target === row.fg ? contrastRatio(candidate, other) : contrastRatio(other, candidate);
|
|
185
|
+
if (ratio >= (row.min ?? 4.5) + 0.05) {
|
|
186
|
+
working.set(target, toHex(candidate));
|
|
187
|
+
applied = { token: target, from: raw, to: toHex(candidate), deltaL: Math.round((l - base.l) * 1000) / 1000, deltaH: 0 };
|
|
188
|
+
touched = true;
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (!applied) changes.set(target + '#unresolved', { token: target, unresolved: true, reason: row.fg + ' / ' + row.bg + ' 在保持色相与彩度的前提下无解' });
|
|
193
|
+
else changes.set(target, applied);
|
|
194
|
+
}
|
|
195
|
+
if (!touched) break;
|
|
196
|
+
}
|
|
197
|
+
const resolved = [...changes.values()];
|
|
198
|
+
const report = checkPairs(working, pairs);
|
|
199
|
+
// 终局校验:多轮调校可能让某项在别处又掉下去;仍不达标的对必须进 unresolved,
|
|
200
|
+
// 否则调用方会以为「已修好」并把不达标的颜色写进 brand.css。
|
|
201
|
+
const unresolved = resolved.filter((item) => item.unresolved);
|
|
202
|
+
for (const row of report.filter((entry) => !entry.ok)) {
|
|
203
|
+
const token = row.fg.startsWith('--tbox-bg-') ? row.bg : row.fg;
|
|
204
|
+
if (unresolved.some((item) => item.token === token)) continue;
|
|
205
|
+
unresolved.push({ token, reason: row.fg + ' / ' + row.bg + ' 调校后仍为 ' + row.ratio + '(需 ≥' + row.min + ')' });
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
tokens: working,
|
|
209
|
+
changes: resolved.filter((item) => !item.unresolved),
|
|
210
|
+
unresolved,
|
|
211
|
+
report,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* 从种子色派生整套语义令牌。规则在 contract.json 的 derive 段:
|
|
217
|
+
* \`from\`(seed / accent)+ 绝对目标 \`L\`/\`C\`,或相对种子的 \`l\` 增量。
|
|
218
|
+
*/
|
|
219
|
+
export function derive({ seed, accent, mood = 'neutral' }, rules, pairs) {
|
|
220
|
+
const seedRgb = parseColor(seed);
|
|
221
|
+
if (!seedRgb) throw new Error('种子色无法解析:' + seed);
|
|
222
|
+
const seedLch = toOklch(seedRgb);
|
|
223
|
+
const accentRgb = accent ? parseColor(accent) : null;
|
|
224
|
+
const accentLch = accentRgb ? toOklch(accentRgb) : { l: seedLch.l, c: seedLch.c, h: (seedLch.h + 60) % 360 };
|
|
225
|
+
const tokens = new Map();
|
|
226
|
+
for (const [name, rule] of Object.entries(rules ?? {})) {
|
|
227
|
+
if (name.startsWith('$')) continue;
|
|
228
|
+
const base = rule.from === 'accent' ? accentLch : seedLch;
|
|
229
|
+
const hue = rule.mood && rule.mood[mood] ? rule.mood[mood].H : base.h;
|
|
230
|
+
const l = typeof rule.L === 'number' ? rule.L : clamp(base.l + (rule.l ?? 0), 0, 1);
|
|
231
|
+
const c = typeof rule.C === 'number' ? rule.C : base.c;
|
|
232
|
+
tokens.set(name, toHex(oklchToRgb({ l: clamp(l, 0, 1), c, h: hue })));
|
|
233
|
+
}
|
|
234
|
+
const report = checkPairs(tokens, pairs);
|
|
235
|
+
return { tokens, report, unresolved: report.filter((row) => !row.ok).map((row) => ({ fg: row.fg, bg: row.bg, ratio: row.ratio, min: row.min })) };
|
|
236
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { checkPairs, contrastRatio, derive, nearestToken, oklchToRgb, parseColor, readTokens, toHex, toOklch, tune } from './theme-kernel.mjs';
|
|
3
|
+
|
|
4
|
+
const PAIRS = [
|
|
5
|
+
{ fg: '--tbox-text-primary', bg: '--tbox-bg-card', min: 4.5 },
|
|
6
|
+
{ fg: '--tbox-text-muted', bg: '--tbox-bg-card', min: 4.5 },
|
|
7
|
+
{ fg: '--tbox-text-muted', bg: '--tbox-bg-page', min: 4.5 },
|
|
8
|
+
];
|
|
9
|
+
// 三案当时的 brand.css 片段:这些数字是门禁实际报出来的,复算必须一致。
|
|
10
|
+
const CASE_A = 'html[data-theme=\'tbox\']{--tbox-bg-card:#fff;--tbox-bg-page:#fbf7f2;--tbox-text-muted:#9a9a9a;--tbox-text-primary:#1b1b1b;}';
|
|
11
|
+
|
|
12
|
+
describe('颜色解析与对比度', () => {
|
|
13
|
+
it('hex / rgb / oklch 三种写法都认,非法值返回 null', () => {
|
|
14
|
+
expect(parseColor('#fff')).toMatchObject({ r: 255, g: 255, b: 255, a: 1 });
|
|
15
|
+
expect(parseColor('#1f5a45')).toMatchObject({ r: 31, g: 90, b: 69 });
|
|
16
|
+
expect(parseColor('rgb(31 90 69 / 50%)').a).toBeCloseTo(0.5, 2);
|
|
17
|
+
expect(parseColor('oklch(0.5 0.1 200)')).not.toBeNull();
|
|
18
|
+
expect(parseColor('not-a-color')).toBeNull();
|
|
19
|
+
expect(parseColor('')).toBeNull();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('OKLCH 往返误差小于 1/255', () => {
|
|
23
|
+
for (const hex of ['#1f5a45', '#c59a55', '#fffcf6', '#173f33', '#000000']) {
|
|
24
|
+
const rgb = parseColor(hex);
|
|
25
|
+
const back = oklchToRgb(toOklch(rgb));
|
|
26
|
+
expect(Math.abs(back.r - rgb.r)).toBeLessThanOrEqual(1);
|
|
27
|
+
expect(Math.abs(back.g - rgb.g)).toBeLessThanOrEqual(1);
|
|
28
|
+
expect(Math.abs(back.b - rgb.b)).toBeLessThanOrEqual(1);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('对比度:黑白 21、同色 1、灰底低于阈值', () => {
|
|
33
|
+
expect(contrastRatio(parseColor('#000'), parseColor('#fff'))).toBeCloseTo(21, 0);
|
|
34
|
+
expect(contrastRatio(parseColor('#777'), parseColor('#777'))).toBeCloseTo(1, 5);
|
|
35
|
+
const rows = checkPairs(readTokens([CASE_A]), PAIRS);
|
|
36
|
+
const muted = rows.find((row) => row.fg === '--tbox-text-muted' && row.bg === '--tbox-bg-card');
|
|
37
|
+
expect(muted.ok).toBe(false);
|
|
38
|
+
expect(muted.ratio).toBeLessThan(4.5);
|
|
39
|
+
expect(rows.find((row) => row.fg === '--tbox-text-primary').ok).toBe(true);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('令牌后读覆盖先读;解析不出来的对记 skipped', () => {
|
|
43
|
+
const tokens = readTokens(['--tbox-text-muted:#111;', '--tbox-text-muted:#626d66;']);
|
|
44
|
+
expect(tokens.get('--tbox-text-muted')).toBe('#626d66');
|
|
45
|
+
const rows = checkPairs(new Map([['--tbox-text-primary', 'var(--x)']]), PAIRS);
|
|
46
|
+
expect(rows[0].skipped).toBe(true);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('最小改动调校', () => {
|
|
51
|
+
it('把不达标的说明文字调到达标,且色相不动', () => {
|
|
52
|
+
const tokens = readTokens([CASE_A]);
|
|
53
|
+
const result = tune(tokens, PAIRS);
|
|
54
|
+
expect(result.unresolved).toEqual([]);
|
|
55
|
+
expect(result.report.every((row) => row.ok)).toBe(true);
|
|
56
|
+
const change = result.changes.find((item) => item.token === '--tbox-text-muted');
|
|
57
|
+
expect(change).toBeTruthy();
|
|
58
|
+
const before = toOklch(parseColor(tokens.get('--tbox-text-muted')));
|
|
59
|
+
const after = toOklch(parseColor(change.to));
|
|
60
|
+
expect(Math.abs(after.h - before.h)).toBeLessThan(5);
|
|
61
|
+
// 最小改动:亮度变化不超过 0.25(灰色 #9a9a9a 到刚好 4.5:1)
|
|
62
|
+
expect(Math.abs(after.l - before.l)).toBeLessThan(0.25);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('全部达标时不产生改动', () => {
|
|
66
|
+
const tokens = new Map([['--tbox-text-primary', '#111111'], ['--tbox-bg-card', '#ffffff'], ['--tbox-text-muted', '#555555'], ['--tbox-bg-page', '#ffffff']]);
|
|
67
|
+
const result = tune(tokens, PAIRS);
|
|
68
|
+
expect(result.changes).toEqual([]);
|
|
69
|
+
expect(result.report.every((row) => row.ok)).toBe(true);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe('调校的终局校验', () => {
|
|
74
|
+
it('互相拉扯的令牌不会假装修好:仍不达标的对进 unresolved', () => {
|
|
75
|
+
// 同一令牌同时出现在两对里,且两个方向互相矛盾 → 不可能同时达标
|
|
76
|
+
const tokens = new Map([
|
|
77
|
+
['--tbox-text-muted', '#808080'],
|
|
78
|
+
['--tbox-bg-card', '#ffffff'],
|
|
79
|
+
['--tbox-bg-page', '#000000'],
|
|
80
|
+
]);
|
|
81
|
+
const pairs = [
|
|
82
|
+
{ fg: '--tbox-text-muted', bg: '--tbox-bg-card', min: 7 },
|
|
83
|
+
{ fg: '--tbox-text-muted', bg: '--tbox-bg-page', min: 7 },
|
|
84
|
+
];
|
|
85
|
+
const result = tune(tokens, pairs);
|
|
86
|
+
const failing = result.report.filter((row) => !row.ok);
|
|
87
|
+
if (failing.length > 0) {
|
|
88
|
+
expect(result.unresolved.length).toBeGreaterThan(0);
|
|
89
|
+
}
|
|
90
|
+
// 无论能否闭合,只要 report 里还有不达标项,unresolved 就不能为空
|
|
91
|
+
expect(failing.length === 0 || result.unresolved.length > 0).toBe(true);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('派生', () => {
|
|
96
|
+
const rules = {
|
|
97
|
+
'--tbox-primary': { from: 'seed' },
|
|
98
|
+
'--tbox-primary-dark': { from: 'seed', l: -0.12 },
|
|
99
|
+
'--tbox-bg-card': { from: 'seed', L: 0.99, C: 0.004 },
|
|
100
|
+
'--tbox-bg-page': { from: 'seed', L: 0.975, C: 0.008 },
|
|
101
|
+
'--tbox-text-primary': { from: 'seed', L: 0.25, C: 0.02 },
|
|
102
|
+
'--tbox-text-muted': { from: 'seed', L: 0.52, C: 0.015 },
|
|
103
|
+
};
|
|
104
|
+
const pairs = [
|
|
105
|
+
{ fg: '--tbox-text-primary', bg: '--tbox-bg-card', min: 4.5 },
|
|
106
|
+
{ fg: '--tbox-text-muted', bg: '--tbox-bg-card', min: 4.5 },
|
|
107
|
+
];
|
|
108
|
+
|
|
109
|
+
it('深绿种子派生的令牌能闭合常用对比度对', () => {
|
|
110
|
+
const result = derive({ seed: '#1f5a45' }, rules, pairs);
|
|
111
|
+
expect(result.unresolved).toEqual([]);
|
|
112
|
+
expect(result.tokens.get('--tbox-primary')).toMatch(/^#[0-9a-f]{6}$/);
|
|
113
|
+
expect(result.tokens.get('--tbox-primary-dark')).not.toBe(result.tokens.get('--tbox-primary'));
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('极浅与极深种子都不抛错,且给出未闭合清单', () => {
|
|
117
|
+
for (const seed of ['#ffffff', '#000000', '#ffff00']) {
|
|
118
|
+
const result = derive({ seed }, rules, pairs);
|
|
119
|
+
expect(result.tokens.size).toBe(Object.keys(rules).length);
|
|
120
|
+
expect(Array.isArray(result.unresolved)).toBe(true);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('非法种子直接报错', () => {
|
|
125
|
+
expect(() => derive({ seed: 'nope' }, rules, pairs)).toThrow(/种子色无法解析/);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe('最近令牌', () => {
|
|
130
|
+
it('硬编码色值给出最像的令牌与距离', () => {
|
|
131
|
+
const tokens = new Map([['--tbox-bg-card', '#fffcf6'], ['--tbox-primary-dark', '#143f30'], ['--tbox-text-primary', '#1b2a24']]);
|
|
132
|
+
const near = nearestToken('#173f33', tokens);
|
|
133
|
+
expect(near.token).toBe('--tbox-primary-dark');
|
|
134
|
+
expect(near.delta).toBeLessThan(10);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
@@ -11,25 +11,25 @@
|
|
|
11
11
|
*/
|
|
12
12
|
html[data-theme='tbox'] {
|
|
13
13
|
/* 主色与强调色 */
|
|
14
|
-
/* --tbox-primary: #
|
|
15
|
-
/* --tbox-primary-dark: #
|
|
16
|
-
/* --tbox-primary-light: #
|
|
14
|
+
/* --tbox-primary: #c5401f; */
|
|
15
|
+
/* --tbox-primary-dark: #a83418; */
|
|
16
|
+
/* --tbox-primary-light: #fdeee8; */
|
|
17
17
|
/* --tbox-accent: #e9a13b; */
|
|
18
18
|
/* --tbox-accent-light: #fff5df; */
|
|
19
19
|
|
|
20
20
|
/* 页面与卡片底色 */
|
|
21
|
-
/* --tbox-bg-page: #
|
|
21
|
+
/* --tbox-bg-page: #fbf7f2; */
|
|
22
22
|
/* --tbox-bg-card: #fffdfa; */
|
|
23
|
-
/* --tbox-bg-subtle: #
|
|
24
|
-
/* --tbox-bg-warm: #
|
|
23
|
+
/* --tbox-bg-subtle: #f6eee6; */
|
|
24
|
+
/* --tbox-bg-warm: #fff6ee; */
|
|
25
25
|
|
|
26
26
|
/* 文字三级与描边 */
|
|
27
|
-
/* --tbox-ink: #
|
|
28
|
-
/* --tbox-text-primary: #
|
|
27
|
+
/* --tbox-ink: #2a211b; */
|
|
28
|
+
/* --tbox-text-primary: #2a211b; */
|
|
29
29
|
/* --tbox-text-secondary: #65564b; */
|
|
30
|
-
/* --tbox-text-muted: #
|
|
31
|
-
/* --tbox-border: #
|
|
32
|
-
/* --tbox-border-strong: #
|
|
30
|
+
/* --tbox-text-muted: #756457; */
|
|
31
|
+
/* --tbox-border: #ede0d2; */
|
|
32
|
+
/* --tbox-border-strong: #dcc9b8; */
|
|
33
33
|
|
|
34
34
|
/* 圆角与阴影 */
|
|
35
35
|
/* --tbox-radius-lg: 44px; */
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
* Tailwind 入口(S1):只引 theme 与 utilities,不引 preflight——宿主与三方样式不受重置影响。
|
|
3
3
|
*
|
|
4
4
|
* - 主题值经 `@theme inline` 直接投影到 `--tbox-*`:工具类产物是 `var(--tbox-*)`,子树覆写照常生效;
|
|
5
|
-
* - `source(none)` + 显式 @source:只扫应用源码与 kit
|
|
5
|
+
* - `source(none)` + 显式 @source:只扫应用源码与 kit;模块源码由客户端 Vite 插件
|
|
6
|
+
* `@tbox.cn/app-build` 的 tboxModuleSourcesPlugin 在编译期追加,证据包走同一发现函数;
|
|
6
7
|
* - 组件样式全部走工具类与语义令牌,没有 Less,也没有 rem 换算。
|
|
7
8
|
*/
|
|
8
9
|
/* 显式层序:kit 的 base.css 与模块基座进 base 层,工具类恒在最后 */
|
|
@@ -13,9 +14,6 @@
|
|
|
13
14
|
|
|
14
15
|
@source "../../src";
|
|
15
16
|
@source "../../../../packages/agent-ui-kit/src";
|
|
16
|
-
/* @tbox:module-sources:begin */
|
|
17
|
-
/* 未安装客户端模块 */
|
|
18
|
-
/* @tbox:module-sources:end */
|
|
19
17
|
|
|
20
18
|
@theme inline {
|
|
21
19
|
/* 颜色:清空默认调色板,只留契约里的语义名 */
|
|
@@ -27,6 +25,7 @@
|
|
|
27
25
|
--color-muted-foreground: var(--tbox-text-muted);
|
|
28
26
|
--color-secondary-foreground: var(--tbox-text-secondary);
|
|
29
27
|
--color-primary: var(--tbox-primary);
|
|
28
|
+
--color-primary-dark: var(--tbox-primary-dark);
|
|
30
29
|
--color-primary-foreground: var(--tbox-primary-light);
|
|
31
30
|
--color-accent: var(--tbox-accent);
|
|
32
31
|
--color-border: var(--tbox-border);
|
|
@@ -8,7 +8,7 @@ import { pathToFileURL } from 'node:url';
|
|
|
8
8
|
// 平台微调配套 babel plugin(随 @tbox.cn/app-sdk 分发):给每个 JSX 注入 data-code-inspect-*
|
|
9
9
|
// 源码位置属性,供 inspector runtime(/__vs_inspector__.js) 点选元素→定位源码。仅 dev 启用(见下)。
|
|
10
10
|
import codeInspectPlugin from '@tbox.cn/app-agent-sdk-client/platform/code-inspect';
|
|
11
|
-
import { tboxAppPlugin } from '@tbox.cn/app-build';
|
|
11
|
+
import { tboxAppPlugin, tboxModuleSourcesPlugin } from '@tbox.cn/app-build';
|
|
12
12
|
|
|
13
13
|
export default defineConfig(async ({ command }) => {
|
|
14
14
|
// tbox-app-dev dev 探测行(dev 探针常量;构件区在位时启用原地装配改指,缺席零行为变化。
|
|
@@ -31,6 +31,7 @@ export default defineConfig(async ({ command }) => {
|
|
|
31
31
|
}
|
|
32
32
|
return {
|
|
33
33
|
plugins: [
|
|
34
|
+
tboxModuleSourcesPlugin({ appRoot: resolve(__dirname, '../..') }),
|
|
34
35
|
tboxAppPlugin({ root: resolve(__dirname, '../..') }),
|
|
35
36
|
tailwindcss(),
|
|
36
37
|
react({
|
|
@@ -81,6 +81,7 @@ function readArgs(argv) {
|
|
|
81
81
|
else if (arg === '--record') out.record = take();
|
|
82
82
|
else if (arg === '--steps') out.steps = take();
|
|
83
83
|
else if (arg === '--expect') out.expect = take();
|
|
84
|
+
else if (arg === '--keep') out.keep = take();
|
|
84
85
|
else if (arg.startsWith('-')) throw new Error(`未知参数:${arg}`);
|
|
85
86
|
else out._.push(arg);
|
|
86
87
|
}
|
|
@@ -327,7 +328,11 @@ async function runRecord(args) {
|
|
|
327
328
|
|
|
328
329
|
let scenario;
|
|
329
330
|
try {
|
|
330
|
-
|
|
331
|
+
const keep = (args.keep ?? '').split(',').map((key) => key.trim()).filter(Boolean);
|
|
332
|
+
scenario = await recordScenario({ registry, name: args.record, steps, keep });
|
|
333
|
+
if (scenario.volatile?.length) {
|
|
334
|
+
console.warn('⚠️ 已从期望里剔除易变值(--keep 可保留):' + scenario.volatile.join('、'));
|
|
335
|
+
}
|
|
331
336
|
} catch (error) {
|
|
332
337
|
throw new Error(`录制执行失败:${error?.message ?? error}`);
|
|
333
338
|
}
|
|
@@ -186,7 +186,7 @@ export async function runScenarios({ appDir, registry, files }) {
|
|
|
186
186
|
* 录制:顺序执行每一步并把实际出参写成期望(只保留 `keys` 指定的键,缺省全部)。
|
|
187
187
|
* 返回可直接写入 `scenarios/<name>.json` 的对象。
|
|
188
188
|
*/
|
|
189
|
-
export async function recordScenario({ registry, name, steps, user }) {
|
|
189
|
+
export async function recordScenario({ registry, name, steps, user, keep = [] }) {
|
|
190
190
|
const { createToolHarness } = await import('@tbox.cn/app-agent-sdk-server/testing');
|
|
191
191
|
const harness = createToolHarness({ registry });
|
|
192
192
|
const scenarioUser = typeof user === 'string' ? user : `scenario:${name}`;
|
|
@@ -203,30 +203,77 @@ export async function recordScenario({ registry, name, steps, user }) {
|
|
|
203
203
|
if (kind === 'action') {
|
|
204
204
|
const result = await harness.act(id, step.input ?? {}, ctx);
|
|
205
205
|
const state = changedLeaves(before, mergedState(harness, stepUser));
|
|
206
|
+
const expected = stripVolatile({ result: pick(result, step.keys), ...(Object.keys(state).length > 0 ? { state } : {}) }, { keep });
|
|
207
|
+
if (expected.removed.length > 0) out.volatile = [...(out.volatile ?? []), ...expected.removed];
|
|
206
208
|
out.steps.push({
|
|
207
209
|
action: id,
|
|
208
210
|
...(step.input ? { input: step.input } : {}),
|
|
209
211
|
...(step.user ? { user: step.user } : {}),
|
|
210
|
-
expect:
|
|
212
|
+
expect: expected.value,
|
|
211
213
|
});
|
|
212
214
|
continue;
|
|
213
215
|
}
|
|
214
216
|
const result = await harness.call(id, step.input ?? {}, ctx);
|
|
215
217
|
const state = changedLeaves(before, mergedState(harness, stepUser));
|
|
218
|
+
const expected = stripVolatile({
|
|
219
|
+
output: pick(result.output, step.keys),
|
|
220
|
+
...(result.cards.length > 0 ? { cards: result.cards.map((card) => card.cardType) } : {}),
|
|
221
|
+
...(Object.keys(state).length > 0 ? { state } : {}),
|
|
222
|
+
}, { keep });
|
|
223
|
+
if (expected.removed.length > 0) out.volatile = [...(out.volatile ?? []), ...expected.removed];
|
|
216
224
|
out.steps.push({
|
|
217
225
|
tool: id,
|
|
218
226
|
...(step.input ? { input: step.input } : {}),
|
|
219
227
|
...(step.user ? { user: step.user } : {}),
|
|
220
|
-
expect:
|
|
221
|
-
output: pick(result.output, step.keys),
|
|
222
|
-
...(result.cards.length > 0 ? { cards: result.cards.map((card) => card.cardType) } : {}),
|
|
223
|
-
...(Object.keys(state).length > 0 ? { state } : {}),
|
|
224
|
-
},
|
|
228
|
+
expect: expected.value,
|
|
225
229
|
});
|
|
226
230
|
}
|
|
227
231
|
return out;
|
|
228
232
|
}
|
|
229
233
|
|
|
234
|
+
/** 易变字段:时间戳/UUID/透明 id。录制期望时剔除,避免「今天过、明天挂」。 */
|
|
235
|
+
const VOLATILE_KEY = /(^|_)(id|timestamp|time|createdat|updatedat|uuid|guid|nonce|token|sessionid|traceid|requestid|messageid)$/i;
|
|
236
|
+
const ISO_TIME = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
237
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
238
|
+
const HEX32 = /^[0-9a-f]{32}$/i;
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* 剔除期望里的易变值(录制稳定化):键名命中时间/id 词,或值形如 ISO 时间/UUID/32 位哈希。
|
|
242
|
+
* 数组不拆元素(拆了会改变长度语义),整体命中才剔除;--keep 可逐项豁免。
|
|
243
|
+
*/
|
|
244
|
+
export function stripVolatile(value, options = {}) {
|
|
245
|
+
const keep = new Set(options.keep ?? []);
|
|
246
|
+
const removed = [];
|
|
247
|
+
const walk = (node, path) => {
|
|
248
|
+
if (Array.isArray(node)) {
|
|
249
|
+
if (node.some((item) => typeof item === 'string' && (ISO_TIME.test(item) || UUID.test(item)))) {
|
|
250
|
+
removed.push(path);
|
|
251
|
+
return undefined;
|
|
252
|
+
}
|
|
253
|
+
return node.map((item, index) => walk(item, path + '[' + index + ']'));
|
|
254
|
+
}
|
|
255
|
+
if (node !== null && typeof node === 'object') {
|
|
256
|
+
const out = {};
|
|
257
|
+
for (const [key, child] of Object.entries(node)) {
|
|
258
|
+
const childPath = path ? path + '.' + key : key;
|
|
259
|
+
if (!keep.has(key) && VOLATILE_KEY.test(key)) {
|
|
260
|
+
removed.push(childPath);
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
const next = walk(child, childPath);
|
|
264
|
+
if (next !== undefined) out[key] = next;
|
|
265
|
+
}
|
|
266
|
+
return out;
|
|
267
|
+
}
|
|
268
|
+
if (typeof node === 'string' && (ISO_TIME.test(node) || UUID.test(node) || HEX32.test(node))) {
|
|
269
|
+
removed.push(path);
|
|
270
|
+
return undefined;
|
|
271
|
+
}
|
|
272
|
+
return node;
|
|
273
|
+
};
|
|
274
|
+
return { value: walk(value, ''), removed };
|
|
275
|
+
}
|
|
276
|
+
|
|
230
277
|
/**
|
|
231
278
|
* 前后状态的顶层差异:录制 expect.state 用。
|
|
232
279
|
* 取整段顶层值(对象/数组原样),与 `diffPath` 的子集语义兼容——不写点号键。
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* 运行:`pnpm scenario [文件或目录…]`(根脚本转发到本文件;SDK 从 apps/server 依赖解析)。
|
|
9
9
|
*/
|
|
10
10
|
import path from 'node:path';
|
|
11
|
+
import { mkdirSync, writeFileSync } from 'node:fs';
|
|
11
12
|
import process from 'node:process';
|
|
12
13
|
import { resolveScenarioFiles, runScenarios } from './scenario-lib.mjs';
|
|
13
14
|
|
|
@@ -22,12 +23,45 @@ if (files.length === 0) {
|
|
|
22
23
|
const { registry } = await import(path.join(appDir, 'apps/client/.tbox-gen/registry.server.ts'));
|
|
23
24
|
const result = await runScenarios({ appDir, registry, files });
|
|
24
25
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
// 人读输出有预算:失败优先、整体 ≤2000 字符;完整结果落 .tbox/logs/scenario.log 并给出路径。
|
|
27
|
+
// --json 只出摘要 JSON(机器消费),--full 直出全部失败行。
|
|
28
|
+
const full = process.argv.includes('--full');
|
|
29
|
+
const jsonOnly = process.argv.includes('--json');
|
|
30
|
+
const MAX_CHARS = 2000;
|
|
31
|
+
const summary = { ok: result.ok, scenarios: result.scenarios, failed: result.failed, tools: result.tools, cards: result.cards };
|
|
32
|
+
|
|
33
|
+
const failedResults = result.results.filter((item) => !item.ok);
|
|
34
|
+
const failureLines = [];
|
|
35
|
+
for (const item of failedResults) {
|
|
36
|
+
failureLines.push(`✗ ${item.scenario}(${item.steps} 步)`);
|
|
37
|
+
for (const line of item.failures) failureLines.push(` - ${line}`);
|
|
38
|
+
}
|
|
39
|
+
const allFailures = result.results.flatMap((item) => item.failures.map((line) => `${item.scenario}: ${line}`));
|
|
40
|
+
let logPath;
|
|
41
|
+
if (failureLines.length > 0 || !jsonOnly) {
|
|
42
|
+
try {
|
|
43
|
+
const dir = path.join(appDir, '.tbox/logs');
|
|
44
|
+
mkdirSync(dir, { recursive: true });
|
|
45
|
+
logPath = path.join(dir, 'scenario.log');
|
|
46
|
+
writeFileSync(logPath, JSON.stringify({ summary, results: result.results }, null, 2) + '\n', 'utf8');
|
|
47
|
+
} catch { /* 只读环境不阻断 */ }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (!jsonOnly && failureLines.length > 0) {
|
|
51
|
+
const bounded = [];
|
|
52
|
+
let used = 0;
|
|
53
|
+
for (const line of failureLines) {
|
|
54
|
+
if (!full && used + line.length + 1 > MAX_CHARS - 120) {
|
|
55
|
+
bounded.push(' …(还有 ' + (failureLines.length - bounded.length) + ' 行,全文见 ' + (logPath ? path.relative(appDir, logPath) : '上文') + ')');
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
bounded.push(line);
|
|
59
|
+
used += line.length + 1;
|
|
60
|
+
}
|
|
61
|
+
console.error(bounded.join('\n'));
|
|
62
|
+
}
|
|
63
|
+
console.log(JSON.stringify(summary));
|
|
64
|
+
if (!jsonOnly && !full && logPath && failureLines.length > 0) {
|
|
65
|
+
console.log('全文:' + path.relative(appDir, logPath) + '(--full 直出失败行)');
|
|
29
66
|
}
|
|
30
|
-
console.log(
|
|
31
|
-
JSON.stringify({ ok: result.ok, scenarios: result.scenarios, failed: result.failed, tools: result.tools, cards: result.cards }),
|
|
32
|
-
);
|
|
33
67
|
if (!result.ok) process.exitCode = 1;
|
package/config/AGENTS.md
CHANGED