@tbox.cn/app-template-agent 1.0.0-alpha.7 → 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 +11 -8
- package/apps/client/index.html +0 -43
- package/apps/client/package.json +1 -0
- package/apps/client/scripts/acceptance-changes.test.mjs +97 -0
- package/apps/client/scripts/check-evidence.mjs +625 -108
- 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 +337 -0
- package/apps/client/scripts/evidence-gate.test.mjs +135 -0
- 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 +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 +95 -126
- 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 +54 -0
- package/apps/client/scripts/theme-kernel.mjs +236 -0
- package/apps/client/scripts/theme-kernel.test.mjs +136 -0
- package/apps/client/src/app.config.ts +4 -1
- package/apps/client/src/styles/brand.css +13 -14
- package/apps/client/src/styles/tailwind.css +20 -21
- package/apps/client/vite.config.ts +2 -1
- package/apps/server/scripts/probe.mjs +6 -1
- package/apps/server/scripts/scenario-lib.mjs +56 -8
- package/apps/server/scripts/scenario.mjs +41 -7
- package/config/AGENTS.md +1 -0
- package/config/chat.json +21 -9
- package/docs/agent/agent-prompt.md +1 -1
- package/docs/agent/card-action.md +5 -0
- package/docs/agent/card.md +45 -2
- package/docs/agent/kit.md +3 -3
- package/docs/agent/shell.md +1 -0
- package/docs/agent/style.md +11 -3
- package/docs/agent/tool-emits-card.md +7 -2
- package/docs/agent/tool.md +2 -0
- package/docs/agent/verify.md +59 -16
- package/package.json +21 -7
- package/packages/agent-ui-kit/AGENTS.md +1 -1
- package/packages/agent-ui-kit/README.md +21 -1
- package/packages/agent-ui-kit/catalog.json +8 -4
- package/packages/agent-ui-kit/package.json +9 -1
- package/packages/agent-ui-kit/src/._surface.tsx +0 -0
- package/packages/agent-ui-kit/src/ChatPage.tsx +70 -17
- package/packages/agent-ui-kit/src/card/._action-list-pending.test.tsx +0 -0
- package/packages/agent-ui-kit/src/card/._define-card.test.ts +0 -0
- package/packages/agent-ui-kit/src/card/._view-card-inline.test.tsx +0 -0
- package/packages/agent-ui-kit/src/card/._view-card.test.tsx +0 -0
- package/packages/agent-ui-kit/src/card/._view-card.tsx +0 -0
- package/packages/agent-ui-kit/src/card/action-list-pending.test.tsx +91 -0
- package/packages/agent-ui-kit/src/card/define-card.test.ts +25 -0
- package/packages/agent-ui-kit/src/card/define-card.ts +18 -5
- package/packages/agent-ui-kit/src/card/primitives.test.tsx +10 -0
- package/packages/agent-ui-kit/src/card/primitives.tsx +136 -37
- package/packages/agent-ui-kit/src/card/view-card-inline.test.tsx +42 -0
- package/packages/agent-ui-kit/src/card/view-card.test.tsx +79 -0
- package/packages/agent-ui-kit/src/card/view-card.tsx +250 -0
- 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 +22 -17
- package/packages/agent-ui-kit/src/components/HistoryEntry/index.tsx +3 -2
- package/packages/agent-ui-kit/src/components/MessageBubble/index.tsx +11 -5
- package/packages/agent-ui-kit/src/components/QuickCommands/index.test.tsx +3 -1
- package/packages/agent-ui-kit/src/components/QuickCommands/index.tsx +31 -8
- 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 +8 -0
- package/packages/agent-ui-kit/src/preview-provider.tsx +47 -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/surface.test.tsx +35 -0
- package/packages/agent-ui-kit/src/surface.tsx +44 -0
- package/packages/agent-ui-kit/src/theme/base.css +9 -11
- package/packages/agent-ui-kit/src/theme/contract.json +123 -2
- package/packages/agent-ui-kit/src/theme/theme-contract.test.ts +16 -0
- package/packages/agent-ui-kit/src/theme/tokens.css +9 -12
- 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/._assert-pack.mjs +0 -0
- package/scripts/._deliver.mjs +0 -0
- package/scripts/._shell-probe.mjs +0 -0
- package/scripts/._storyboard.mjs +0 -0
- package/scripts/assert-pack.mjs +48 -0
- package/scripts/check-css-target.mjs +6 -3
- package/scripts/check-fast.mjs +1 -0
- package/scripts/checks.json +400 -26
- 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/.tbox/card-render/baseline/demo.png +0 -0
- package/.tbox/card-render/demo.html +0 -342
- package/.tbox/card-render/demo.png +0 -0
- package/.tbox/card-render/scale-demo.audit.html +0 -984
- package/.tbox/card-render/scale-demo.html +0 -939
- package/.tbox/evidence/cards.txt +0 -1
- package/.tbox/evidence/gate.json +0 -1
- package/.tbox/evidence/last-gate.json +0 -1
- package/.tbox/evidence/scenarios.json +0 -1
- package/apps/client/.tbox-gen/app-registry.ts +0 -3
- package/apps/client/.tbox-gen/documents/_tbox/card-preview.html +0 -62
- package/apps/client/.tbox-gen/documents/index.html +0 -63
- package/apps/client/.tbox-gen/entries/card-preview.tsx +0 -6
- package/apps/client/.tbox-gen/entries/index.tsx +0 -11
- package/apps/client/.tbox-gen/manifest.json +0 -17
- package/apps/client/.tbox-gen/registry.client.ts +0 -6
- package/apps/client/.tbox-gen/registry.server.ts +0 -15
- package/apps/client/STYLE_GUIDE.md +0 -48
- package/apps/client/VISUAL_RESTORE.md +0 -90
- package/profiles/mall/config/llm_provider.json +0 -5
- package/profiles/mall/config/plugins.json +0 -3
- package/scripts/compat-probe.mjs +0 -108
- package/scripts/gen-tailwind-sources.mjs +0 -55
- /package/.tbox/{card-render/._demo.html → factory/._chat.json} +0 -0
- /package/{.tbox/card-render/._demo.png → apps/client/scripts/._acceptance-changes.test.mjs} +0 -0
- /package/{.tbox/card-render/._scale-demo.audit.html → apps/client/scripts/._color-scan.mjs} +0 -0
- /package/{.tbox/card-render/._scale-demo.html → apps/client/scripts/._color-scan.test.mjs} +0 -0
- /package/{.tbox/card-render/baseline/._demo.png → apps/client/scripts/._evidence-gate.mjs} +0 -0
- /package/{.tbox/evidence/._cards.txt → apps/client/scripts/._evidence-gate.test.mjs} +0 -0
- /package/{.tbox/evidence/._gate.json → apps/client/scripts/._experience-checks.mjs} +0 -0
- /package/{.tbox/evidence/._last-gate.json → apps/client/scripts/._geometry-kernel.test.mjs} +0 -0
- /package/{.tbox/evidence/._scenarios.json → apps/client/scripts/._in-process-checks.mjs} +0 -0
- /package/apps/client/{._STYLE_GUIDE.md → scripts/._probe-checks.mjs} +0 -0
- /package/apps/client/{._VISUAL_RESTORE.md → scripts/._sample-entries.test.mjs} +0 -0
- /package/apps/client/{.tbox-gen/._app-registry.ts → scripts/._storyboard.test.mjs} +0 -0
- /package/apps/client/{.tbox-gen/._manifest.json → scripts/._surface-css.mjs} +0 -0
- /package/apps/client/{.tbox-gen/._registry.client.ts → scripts/._theme-kernel.mjs} +0 -0
- /package/apps/client/{.tbox-gen/._registry.server.ts → scripts/._theme-kernel.test.mjs} +0 -0
- /package/apps/client/{.tbox-gen/documents/._index.html → scripts/lib/._card-registry.mjs} +0 -0
- /package/apps/client/{.tbox-gen/documents/_tbox/._card-preview.html → scripts/lib/._card-registry.test.mjs} +0 -0
- /package/apps/client/{.tbox-gen/entries/._card-preview.tsx → scripts/lib/._evidence-inputs.mjs} +0 -0
- /package/apps/client/{.tbox-gen/entries/._index.tsx → scripts/lib/._evidence-inputs.test.mjs} +0 -0
- /package/{profiles/mall/config/._llm_provider.json → apps/client/scripts/lib/._static-render.mjs} +0 -0
- /package/{profiles/mall/config/._plugins.json → apps/client/scripts/lib/._static-render.test.mjs} +0 -0
- /package/{scripts/._compat-probe.mjs → packages/agent-ui-kit/src/._preview-provider.tsx} +0 -0
- /package/{scripts/._gen-tailwind-sources.mjs → packages/agent-ui-kit/src/._surface.test.tsx} +0 -0
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* storyboard —— 一页一图看清「壳 + 每张卡」在不同宽度下长什么样,并同时量几何。
|
|
4
|
+
*
|
|
5
|
+
* 为什么不逐帧起浏览器:一帧一次要 1–2 秒,一张卡三个样例两个宽度就是六次。这里一次 vite、
|
|
6
|
+
* 一次 Chrome、一张长图:帧是同源 iframe,几何内核注入到每帧自己的 window 里,按帧宽测量。
|
|
7
|
+
*
|
|
8
|
+
* 输入:scripts/checks.json 的 frames 段(帧来源与宽度登记)。
|
|
9
|
+
* 输出:.tbox/evidence/storyboard.png(超高分页)、storyboard.json(逐帧几何与哈希)。
|
|
10
|
+
* 无 Chrome 记 not-run;超过预算/页数上限时截断并记 ok + truncated + planned(已渲染的帧全部通过);
|
|
11
|
+
* 预算/页数来自 scripts/checks.json 的 storyboard 段(TBOX_STORYBOARD_BUDGET_MS 可覆盖预算)。
|
|
12
|
+
*/
|
|
13
|
+
import { spawnSync } from 'node:child_process';
|
|
14
|
+
import { createHash } from 'node:crypto';
|
|
15
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
16
|
+
import path from 'node:path';
|
|
17
|
+
import { fileURLToPath } from 'node:url';
|
|
18
|
+
import { duplicateSampleLabels, loadVite, loadRegistry, buildStyleSheet, sampleEntries, renderSamples, writeSample } from '../apps/client/scripts/render-card.mjs';
|
|
19
|
+
import { collectCards } from '../apps/client/scripts/lib/card-registry.mjs';
|
|
20
|
+
import { evidenceInputFiles } from '../apps/client/scripts/lib/evidence-inputs.mjs';
|
|
21
|
+
import { findChrome, openChromeSession } from './lib/chrome-pipe.mjs';
|
|
22
|
+
import { geometryFunctionSource, loadThresholds } from './lib/geometry-kernel.mjs';
|
|
23
|
+
|
|
24
|
+
const appDir = path.resolve(import.meta.dirname, '..');
|
|
25
|
+
const outDir = path.join(appDir, '.tbox/evidence');
|
|
26
|
+
const KERNEL_VERSION = '4';
|
|
27
|
+
const checksPath = path.join(appDir, 'scripts/checks.json');
|
|
28
|
+
const checksJson = existsSync(checksPath) ? JSON.parse(readFileSync(checksPath, 'utf8')) : {};
|
|
29
|
+
const storyboardConfig = checksJson.storyboard ?? {};
|
|
30
|
+
|
|
31
|
+
let configError = '';
|
|
32
|
+
/** 正整数配置:缺席取缺省;非法记 not-run 原因(不静默 fallback,也不在模块顶层崩掉)。 */
|
|
33
|
+
function positiveInt(value, fallback, label) {
|
|
34
|
+
if (value === undefined) return fallback;
|
|
35
|
+
if (!Number.isInteger(value) || value <= 0) {
|
|
36
|
+
configError ||= `checks.json storyboard.${label} 必须是正整数(当前 ${JSON.stringify(value)})`;
|
|
37
|
+
return fallback;
|
|
38
|
+
}
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const envBudget = process.env.TBOX_STORYBOARD_BUDGET_MS;
|
|
43
|
+
const BUDGET_MS = envBudget === undefined ? positiveInt(storyboardConfig.budgetMs, 10000, 'budgetMs') : Number(envBudget);
|
|
44
|
+
if (!Number.isFinite(BUDGET_MS) || BUDGET_MS <= 0) {
|
|
45
|
+
configError ||= `TBOX_STORYBOARD_BUDGET_MS 必须是正数(当前 ${JSON.stringify(envBudget)})`;
|
|
46
|
+
}
|
|
47
|
+
const PAGE_LIMIT = positiveInt(storyboardConfig.pageLimit, 12000, 'pageLimit');
|
|
48
|
+
const MAX_PAGES = positiveInt(storyboardConfig.maxPages, 3, 'maxPages');
|
|
49
|
+
const frameSpecs = Array.isArray(checksJson.frames) ? checksJson.frames : [];
|
|
50
|
+
const thresholds = loadThresholds(checksJson, ['card.overflow-x', 'card.tap-target', 'card.min-font']);
|
|
51
|
+
|
|
52
|
+
const escapeAttr = (value) =>
|
|
53
|
+
value.replaceAll('&', '&').replaceAll('"', '"').replaceAll('<', '<').replaceAll('>', '>');
|
|
54
|
+
|
|
55
|
+
/** 一帧的几何问题(检查 id + detail);门禁按 id 判级别。纯函数,供单测。 */
|
|
56
|
+
export function geometryProblems(frame, limits = thresholds) {
|
|
57
|
+
const geometry = frame.geometry ?? {};
|
|
58
|
+
const problems = [];
|
|
59
|
+
// 壳帧的问题由壳探针(shell.*)负责;这里只判卡片帧,避免同一个毛病报两遍。
|
|
60
|
+
if (frame.source !== 'card-samples') return problems;
|
|
61
|
+
if ((geometry.overflowX ?? 0) > 1) {
|
|
62
|
+
problems.push({ id: 'card.overflow-x', detail: frame.id + ' 横向溢出 ' + geometry.overflowX + 'px' });
|
|
63
|
+
}
|
|
64
|
+
const clipped = (geometry.clipped ?? []).filter((item) => !item.ellipsis);
|
|
65
|
+
if (clipped.length > 0) {
|
|
66
|
+
problems.push({ id: 'card.text-clip', detail: frame.id + ' 文本截断 ' + clipped.length + ' 处:' + clipped.slice(0, 3).map((item) => item.text).join('、') });
|
|
67
|
+
}
|
|
68
|
+
const small = geometry.smallTargets ?? [];
|
|
69
|
+
if (small.length > 0) {
|
|
70
|
+
problems.push({ id: 'card.tap-target', detail: frame.id + ' 点击区不足 ' + limits.minTarget + 'px:' + small.slice(0, 3).map((item) => (item.text || item.tag) + ' ' + item.w + '×' + item.h).join('、') });
|
|
71
|
+
}
|
|
72
|
+
if (typeof geometry.minFontPx === 'number' && geometry.minFontPx < limits.minFontPx) {
|
|
73
|
+
problems.push({ id: 'card.min-font', detail: frame.id + ' 最小字号 ' + geometry.minFontPx + 'px' });
|
|
74
|
+
}
|
|
75
|
+
return problems;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** 帧展开:宽度笛卡尔积;卡片帧按 maxSamplesPerCard 截断。纯函数,供单测。 */
|
|
79
|
+
export function expandFrames(specs, cardSamples) {
|
|
80
|
+
const frames = [];
|
|
81
|
+
for (const spec of specs) {
|
|
82
|
+
for (const width of spec.widths ?? [390]) {
|
|
83
|
+
if (spec.source === 'shell') {
|
|
84
|
+
frames.push({ id: spec.id + '@' + width, source: 'shell', width });
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (spec.source === 'card-samples') {
|
|
88
|
+
// 最宽的那档看全部样例;更窄的档只看第一个样例——窄屏判的是「会不会溢出」,
|
|
89
|
+
// 不是每一份数据都要再看一遍,帧数直接决定渲染耗时。
|
|
90
|
+
const widest = Math.max(...(spec.widths ?? [390]));
|
|
91
|
+
for (const card of cardSamples) {
|
|
92
|
+
// hidden 卡是「预览策展/降噪」面:只验基准态(sampleData)两档,状态样例交给可见卡。
|
|
93
|
+
const max = card.hidden === true ? 1 : (spec.maxSamplesPerCard ?? 3);
|
|
94
|
+
const samples = width === widest ? card.samples.slice(0, max) : card.samples.slice(0, 1);
|
|
95
|
+
for (const sample of samples) {
|
|
96
|
+
frames.push({ id: 'card:' + card.cardType + ':' + sample.label + '@' + width, source: 'card-samples', width, cardType: card.cardType, sample });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return frames;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** 分页:每页高度上限 limit,最多 maxPages 页。纯函数,供单测。 */
|
|
106
|
+
export function chunkPages(frames, limit = PAGE_LIMIT, maxPages = 3) {
|
|
107
|
+
const pages = [];
|
|
108
|
+
let current = [];
|
|
109
|
+
let height = 0;
|
|
110
|
+
for (const frame of frames) {
|
|
111
|
+
const frameHeight = (frame.geometry?.height ?? 600) + 60;
|
|
112
|
+
if (current.length > 0 && height + frameHeight > limit) {
|
|
113
|
+
pages.push(current);
|
|
114
|
+
current = [];
|
|
115
|
+
height = 0;
|
|
116
|
+
if (pages.length === maxPages) return pages;
|
|
117
|
+
}
|
|
118
|
+
current.push(frame);
|
|
119
|
+
height += frameHeight;
|
|
120
|
+
}
|
|
121
|
+
if (current.length > 0 && pages.length < maxPages) pages.push(current);
|
|
122
|
+
return pages;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const frameKey = (frame) => (frame.source === 'shell' ? 'shell' : 'card:' + frame.cardType + ':' + frame.sample.label);
|
|
126
|
+
|
|
127
|
+
/** 缓存键只读源文件(渲染前就能算),命中则整轮跳过。 */
|
|
128
|
+
export function sourceCacheKey() {
|
|
129
|
+
const parts = [];
|
|
130
|
+
const push = (file) => {
|
|
131
|
+
try {
|
|
132
|
+
parts.push(path.relative(appDir, file) + ':' + createHash('sha256').update(readFileSync(file)).digest('hex').slice(0, 16));
|
|
133
|
+
} catch { /* 缺文件不影响缓存键 */ }
|
|
134
|
+
};
|
|
135
|
+
// 单一真源:与门禁 readStoryboardResult 的过期判定共用同一份输入清单(含模块卡/主题接线)。
|
|
136
|
+
// 不把 shell.html 自身算进键:缓存未命中时探针会重写它,纳入会导致键每次都不一致(永不命中)。
|
|
137
|
+
for (const file of evidenceInputFiles(appDir)) push(file);
|
|
138
|
+
return createHash('sha256').update(parts.join('|') + '|' + KERNEL_VERSION + '|' + JSON.stringify(thresholds)).digest('hex').slice(0, 16);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function frameHtml(frame, htmlByKey) {
|
|
142
|
+
const html = htmlByKey[frameKey(frame)];
|
|
143
|
+
if (!html) return null;
|
|
144
|
+
const script = '<script>' + geometryFunctionSource() + '<\/script>';
|
|
145
|
+
return html.includes('</body>') ? html.replace('</body>', script + '</body>') : html + script;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function storyboardPage(frames, htmlByKey) {
|
|
149
|
+
const blocks = frames.map((frame, index) => {
|
|
150
|
+
const html = frameHtml(frame, htmlByKey) ?? '<!doctype html><body style="font:14px sans-serif;padding:16px">该帧没有渲染结果</body>';
|
|
151
|
+
return '<section class="frame"><h2><span class="idx">' + (index + 1) + '</span>' + frame.id.replaceAll('<', '<') + '</h2>' +
|
|
152
|
+
'<iframe class="canvas" data-source="' + frame.source + '" style="width:' + frame.width + 'px" srcdoc="' + escapeAttr(html) + '"></iframe></section>';
|
|
153
|
+
});
|
|
154
|
+
return '<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><title>storyboard</title><style>' +
|
|
155
|
+
'body{margin:0;background:#eef0f2;font:12px/1.4 -apple-system,system-ui,sans-serif;color:#222}' +
|
|
156
|
+
'.grid{display:flex;flex-wrap:wrap;gap:16px;padding:16px;align-items:flex-start}' +
|
|
157
|
+
'.frame{background:#fff;border-radius:10px;box-shadow:0 1px 3px rgba(0,0,0,.12);padding:8px;margin:0}' +
|
|
158
|
+
'.frame h2{font-size:11px;font-weight:500;color:#555;margin:0 0 6px;display:flex;gap:6px;align-items:center;max-width:390px;overflow:hidden;white-space:nowrap}' +
|
|
159
|
+
'.idx{display:inline-flex;align-items:center;justify-content:center;min-width:16px;height:16px;border-radius:8px;background:#2f6b4c;color:#fff;font-size:10px}' +
|
|
160
|
+
'.canvas{border:0;display:block;background:#fff;min-height:200px}' +
|
|
161
|
+
'</style></head><body><div class="grid">' + blocks.join('') + '</div></body></html>';
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const MEASURE = [
|
|
165
|
+
'(() => {',
|
|
166
|
+
' const out = [];',
|
|
167
|
+
' for (const el of document.querySelectorAll("iframe.canvas")) {',
|
|
168
|
+
' try {',
|
|
169
|
+
' const win = el.contentWindow;',
|
|
170
|
+
' if (!win || typeof win.tboxGeometry !== "function") { out.push({ ok: false, error: "帧未加载几何内核" }); continue; }',
|
|
171
|
+
' const geometry = win.tboxGeometry({ root: "body", thresholds: ' + JSON.stringify(thresholds) + ' });',
|
|
172
|
+
' // 壳帧是整页应用(高度按视口撑满),卡片帧按内容高度裁切。',
|
|
173
|
+
' el.style.height = (el.getAttribute("data-source") === "shell" ? 844 : Math.round(geometry.height)) + "px";',
|
|
174
|
+
' out.push({ ok: true, geometry });',
|
|
175
|
+
' } catch (error) { out.push({ ok: false, error: String(error && error.message) }); }',
|
|
176
|
+
' }',
|
|
177
|
+
' return JSON.stringify(out);',
|
|
178
|
+
'})()',
|
|
179
|
+
].join('\n');
|
|
180
|
+
|
|
181
|
+
/** 上一轮可能分了更多页:残留的 storyboard-N.png/html 会被当成这一轮的证据。 */
|
|
182
|
+
function clearStalePages() {
|
|
183
|
+
if (!existsSync(outDir)) return;
|
|
184
|
+
for (const entry of readdirSync(outDir)) {
|
|
185
|
+
if (/^storyboard(-\d+)?\.(png|html)$/.test(entry)) unlinkSync(path.join(outDir, entry));
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function write(record) {
|
|
190
|
+
mkdirSync(outDir, { recursive: true });
|
|
191
|
+
writeFileSync(path.join(outDir, 'storyboard.json'), JSON.stringify(record, null, 2) + '\n', 'utf8');
|
|
192
|
+
console.log(summary(record));
|
|
193
|
+
return record;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function summary(record) {
|
|
197
|
+
if (record.status === 'not-run') return 'storyboard: not-run(' + record.reason + ')';
|
|
198
|
+
const problems = (record.frames ?? []).reduce((total, frame) => total + geometryProblems(frame).length, 0);
|
|
199
|
+
const images = record.images ?? [];
|
|
200
|
+
return 'storyboard: ' + record.status + ',' + (record.frames ?? []).length + ' 帧,几何问题 ' + problems + ' 个,' +
|
|
201
|
+
Math.round(record.durationMs ?? 0) + 'ms' + (record.cached ? '(缓存)' : '') +
|
|
202
|
+
(images.length > 0 ? ' → ' + images.map((image) => image.file).join('、') : '');
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async function main() {
|
|
206
|
+
if (configError) return write({ protocolVersion: 1, status: 'not-run', reason: configError, frames: [], images: [] });
|
|
207
|
+
if (process.env.TBOX_STORYBOARD === '0') return write({ protocolVersion: 1, status: 'not-run', reason: 'TBOX_STORYBOARD=0,已关闭', frames: [], images: [] });
|
|
208
|
+
if (frameSpecs.length === 0) return write({ protocolVersion: 1, status: 'not-run', reason: 'checks.json 没有登记 frames', frames: [], images: [] });
|
|
209
|
+
const chrome = findChrome();
|
|
210
|
+
if (!chrome) return write({ protocolVersion: 1, status: 'not-run', reason: '未找到无头浏览器(可设 TBOX_CHROME)', frames: [], images: [] });
|
|
211
|
+
|
|
212
|
+
const started = Date.now();
|
|
213
|
+
const cacheKey = sourceCacheKey();
|
|
214
|
+
const previousPath = path.join(outDir, 'storyboard.json');
|
|
215
|
+
if (existsSync(previousPath)) {
|
|
216
|
+
try {
|
|
217
|
+
const previous = JSON.parse(readFileSync(previousPath, 'utf8'));
|
|
218
|
+
if (previous.cacheKey === cacheKey && previous.status === 'ok' && (previous.images ?? []).every((image) => existsSync(path.join(appDir, image.file)))) {
|
|
219
|
+
// 命中缓存时也清掉比这一轮多出来的旧页(它们没有对应记录,纯属误导)。
|
|
220
|
+
const keep = new Set((previous.images ?? []).map((image) => path.basename(image.file)));
|
|
221
|
+
for (const entry of (existsSync(outDir) ? readdirSync(outDir) : [])) {
|
|
222
|
+
if (/^storyboard(-\d+)?\.(png|html)$/.test(entry) && !keep.has(entry)) unlinkSync(path.join(outDir, entry));
|
|
223
|
+
}
|
|
224
|
+
return write({ ...previous, cached: true, generatedAt: new Date().toISOString(), durationMs: Date.now() - started });
|
|
225
|
+
}
|
|
226
|
+
} catch { /* 坏缓存不当缓存 */ }
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const htmlByKey = {};
|
|
230
|
+
let vite;
|
|
231
|
+
try {
|
|
232
|
+
vite = await loadVite();
|
|
233
|
+
const registries = await loadRegistry(vite);
|
|
234
|
+
const view = collectCards({ server: registries.server, client: registries.client });
|
|
235
|
+
const mergedRegistries = {
|
|
236
|
+
...registries,
|
|
237
|
+
server: { ...registries.server, cards: view.metas },
|
|
238
|
+
client: { ...registries.client, cards: view.components },
|
|
239
|
+
};
|
|
240
|
+
const cardSamples = Object.entries(view.metas)
|
|
241
|
+
.map(([cardType, meta]) => {
|
|
242
|
+
const duplicates = duplicateSampleLabels(meta, { includeHidden: false });
|
|
243
|
+
if (duplicates.length > 0) throw new Error(`卡片 ${cardType} 的样例 label 重复:${duplicates.join('、')}(帧键会互相覆盖,先修 samples[].label)`);
|
|
244
|
+
return { cardType, hidden: meta?.hidden === true, samples: sampleEntries(meta, { includeHidden: false }) };
|
|
245
|
+
})
|
|
246
|
+
.filter((card) => card.samples.length > 0);
|
|
247
|
+
const planned = expandFrames(frameSpecs, cardSamples);
|
|
248
|
+
|
|
249
|
+
// 壳帧:缓存未命中时总是重跑探针——壳源/主题/kit 变更后 10 分钟内的旧 shell.html 不能冒充当前。
|
|
250
|
+
const shellHtml = path.join(outDir, 'shell.html');
|
|
251
|
+
const shellReady = process.env.TBOX_SHELL_FRESH === '1' && existsSync(shellHtml);
|
|
252
|
+
if (!shellReady && planned.some((frame) => frame.source === 'shell')) {
|
|
253
|
+
spawnSync(process.execPath, [path.join(appDir, 'scripts/shell-probe.mjs')], { cwd: appDir, encoding: 'utf8', timeout: 120000 });
|
|
254
|
+
}
|
|
255
|
+
if (existsSync(shellHtml)) htmlByKey.shell = readFileSync(shellHtml, 'utf8');
|
|
256
|
+
|
|
257
|
+
const cardFrames = planned.filter((frame) => frame.source === 'card-samples');
|
|
258
|
+
const cardTypes = [...new Set(cardFrames.map((frame) => frame.cardType))];
|
|
259
|
+
if (cardTypes.length > 0) {
|
|
260
|
+
const surface = await buildStyleSheet(vite);
|
|
261
|
+
const renderDir = path.join(appDir, '.tbox/card-render');
|
|
262
|
+
for (const cardType of cardTypes) {
|
|
263
|
+
const needed = new Set(cardFrames.filter((frame) => frame.cardType === cardType).map((frame) => frame.sample.label));
|
|
264
|
+
const selected = sampleEntries(view.metas[cardType], { includeHidden: false }).filter((sample) => needed.has(sample.label));
|
|
265
|
+
const renderedSamples = await renderSamples(vite, mergedRegistries, cardType, selected);
|
|
266
|
+
for (const sample of renderedSamples) {
|
|
267
|
+
const { file } = writeSample(renderDir, cardType, sample, surface.css);
|
|
268
|
+
htmlByKey['card:' + cardType + ':' + sample.label] = readFileSync(file, 'utf8');
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const usable = planned.filter((frame) => htmlByKey[frameKey(frame)]);
|
|
274
|
+
if (usable.length === 0) {
|
|
275
|
+
return write({ protocolVersion: 1, status: 'not-run', reason: '没有可渲染的帧(缺壳产物也没有卡片样例)', frames: [], images: [], cacheKey, durationMs: Date.now() - started });
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const pages = chunkPages(usable, PAGE_LIMIT, MAX_PAGES);
|
|
279
|
+
clearStalePages();
|
|
280
|
+
const images = [];
|
|
281
|
+
const collected = new Map();
|
|
282
|
+
for (const [index, pageFrames] of pages.entries()) {
|
|
283
|
+
const pagePath = path.join(outDir, pages.length === 1 ? 'storyboard.html' : 'storyboard-' + (index + 1) + '.html');
|
|
284
|
+
writeFileSync(pagePath, storyboardPage(pageFrames, htmlByKey), 'utf8');
|
|
285
|
+
const session = await openChromeSession({ chrome, url: 'file://' + pagePath, width: Math.max(...pageFrames.map((frame) => frame.width)) + 40, height: 1400, deviceScaleFactor: 2, mobile: false, settleMs: 900 });
|
|
286
|
+
if (!session) {
|
|
287
|
+
// 已经截出来的页留在盘上:报 partial 并带上它们,别写成「没跑过」。
|
|
288
|
+
return write({
|
|
289
|
+
protocolVersion: 1,
|
|
290
|
+
status: images.length > 0 ? 'partial' : 'not-run',
|
|
291
|
+
reason: images.length > 0 ? '第 ' + (index + 1) + ' 页起无头浏览器未启动' : '无头浏览器未启动',
|
|
292
|
+
frames: [...collected.values()],
|
|
293
|
+
images,
|
|
294
|
+
cacheKey,
|
|
295
|
+
planned: usable.length,
|
|
296
|
+
truncated: images.length > 0 ? usable.length - collected.size : 0,
|
|
297
|
+
durationMs: Date.now() - started,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
try {
|
|
301
|
+
const measured = JSON.parse(await session.evaluate(MEASURE));
|
|
302
|
+
pageFrames.forEach((frame, frameIndex) => {
|
|
303
|
+
const item = measured[frameIndex] ?? { ok: false, error: '未回传测量结果' };
|
|
304
|
+
const entry = {
|
|
305
|
+
id: frame.id,
|
|
306
|
+
source: frame.source,
|
|
307
|
+
width: frame.width,
|
|
308
|
+
...(item.ok ? { geometry: item.geometry } : { error: item.error }),
|
|
309
|
+
};
|
|
310
|
+
entry.problems = entry.error ? [{ id: 'storyboard.render', detail: frame.id + ':' + entry.error }] : geometryProblems(entry);
|
|
311
|
+
collected.set(frame.id, entry);
|
|
312
|
+
});
|
|
313
|
+
const png = await session.screenshot();
|
|
314
|
+
const file = path.join(outDir, pages.length === 1 ? 'storyboard.png' : 'storyboard-' + (index + 1) + '.png');
|
|
315
|
+
writeFileSync(file, png);
|
|
316
|
+
images.push({ file: path.relative(appDir, file), sha256: createHash('sha256').update(png).digest('hex').slice(0, 16) });
|
|
317
|
+
} finally {
|
|
318
|
+
session.close();
|
|
319
|
+
}
|
|
320
|
+
if (Date.now() > started + BUDGET_MS) break;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const frames = [...collected.values()];
|
|
324
|
+
const failures = frames.filter((frame) => frame.error);
|
|
325
|
+
const truncated = frames.length < usable.length;
|
|
326
|
+
return write({
|
|
327
|
+
protocolVersion: 1,
|
|
328
|
+
status: failures.length > 0 ? 'partial' : 'ok',
|
|
329
|
+
...(truncated ? { truncated: true } : {}),
|
|
330
|
+
...(failures.length > 0 ? { reason: failures.length + ' 帧未完成测量:' + failures.slice(0, 2).map((frame) => frame.id + '(' + frame.error + ')').join(';') } : truncated ? { reason: '超过渲染预算 ' + BUDGET_MS + 'ms,只渲染了 ' + frames.length + '/' + usable.length + ' 帧(已渲染的帧全部通过)' } : {}),
|
|
331
|
+
cacheKey,
|
|
332
|
+
planned: usable.length,
|
|
333
|
+
durationMs: Date.now() - started,
|
|
334
|
+
images,
|
|
335
|
+
frames,
|
|
336
|
+
});
|
|
337
|
+
} finally {
|
|
338
|
+
if (vite) await vite.close().catch(() => undefined);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const invokedDirectly = process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url);
|
|
343
|
+
if (invokedDirectly) {
|
|
344
|
+
try {
|
|
345
|
+
await main();
|
|
346
|
+
} catch (error) {
|
|
347
|
+
write({ protocolVersion: 1, status: 'not-run', reason: 'storyboard 失败:' + (error?.message ?? String(error)), frames: [], images: [] });
|
|
348
|
+
process.exitCode = 1;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
Binary file
|