index.ts
15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/**
* Report generation tool for validation results.
* Provides pure visualization and formatting.
*/
import * as fs from 'fs';
import * as fsPromises from 'fs/promises';
import * as path from 'path';
import { fileURLToPath } from 'url';
import { tools } from 'evoltagent';
import { logger } from '../../utils/logger';
import { VisualizationTool } from '../visualization-tool';
import type { MisalignedComponentData } from '../visualization-tool/types';
import type { UserReport } from '../../types/validation-types';
import type { ReportGenerationRequest, ReportGenerationResult, ErrorReportOptions, GenerateHtmlResult } from './types';
export type { ReportGenerationRequest, ReportGenerationResult, ErrorReportOptions, GenerateHtmlResult } from './types';
@tools({
generateReport: {
description:
'Generate visual validation report structure from pre-computed validation results. Creates annotated screenshots, heatmap, and userReport structure.',
params: [
{
name: 'request',
type: 'object',
description: 'ReportGenerationRequest with validationResult, figmaThumbnailUrl, serverUrl, outputDir, etc.',
},
],
returns: { type: 'object', description: 'ReportGenerationResult with userReport and misalignedCount' },
},
createMinimalReport: {
description: 'Create a minimal user report for error scenarios without screenshots',
params: [{ name: 'options', type: 'object', description: 'ErrorReportOptions with serverUrl, figmaThumbnailUrl, mae, sae' }],
returns: { type: 'object', description: 'Minimal UserReport structure' },
},
generateHtml: {
description: 'Generate standalone HTML report file from userReport with embedded data and inlined assets',
params: [
{ name: 'userReport', type: 'object', description: 'UserReport data from validation with screenshots and metrics' },
{ name: 'outputDir', type: 'string', description: 'Output directory path for HTML file' },
],
returns: { type: 'object', description: 'GenerateHtmlResult with success/htmlPath/error' },
},
})
export class ReportTool {
/**
* Generate visual validation report from validation results.
*
* This method ONLY does visualization and formatting:
* - Loads saved annotated screenshots from last validation iteration (avoids port mismatch)
* - Generates pixel difference heatmap
* - Builds userReport structure
*
*/
async generateReport(request: ReportGenerationRequest): Promise<ReportGenerationResult> {
logger.printInfoLog('\nGenerating validation report...');
const visualizationTool = new VisualizationTool();
const { validationResult } = request;
// Prepare output directory
const comparisonDir = path.join(request.outputDir, 'comparison_screenshots');
if (!fs.existsSync(comparisonDir)) {
fs.mkdirSync(comparisonDir, { recursive: true });
}
// Transform validation data to visualization format
const misalignedData = visualizationTool['formatForVisualization'](validationResult.misalignedComponents);
logger.printInfoLog(`最终对齐异常组件数:${misalignedData.length}`);
// Load saved annotated screenshots from last validation iteration
const renderMarkedBuffer = await fsPromises.readFile(request.savedRenderMarkedPath);
const targetMarkedBuffer = await fsPromises.readFile(request.savedTargetMarkedPath);
const renderMarked = `data:image/webp;base64,${renderMarkedBuffer.toString('base64')}`;
const targetMarked = `data:image/webp;base64,${targetMarkedBuffer.toString('base64')}`;
const screenshots = {
renderSnap: validationResult.screenshots?.renderSnap || renderMarked,
renderMarked,
targetMarked,
};
// Generate combined comparison screenshot
const finalScreenshotPath = path.join(comparisonDir, 'final.webp');
await visualizationTool.combine(screenshots.renderMarked, screenshots.targetMarked, finalScreenshotPath);
logger.printSuccessLog(`已保存最终对比截图:${path.basename(finalScreenshotPath)}`);
// Generate pixel difference heatmap
let heatmap = '';
try {
heatmap = await visualizationTool.diffHeatmap(screenshots.renderSnap, request.figmaThumbnailUrl);
if (heatmap) {
const heatmapPath = path.join(comparisonDir, 'heatmap.webp');
const base64Data = heatmap.split(',')[1];
if (!base64Data) {
throw new Error('Invalid heatmap data URI format');
}
const buffer = Buffer.from(base64Data, 'base64');
await fs.promises.writeFile(heatmapPath, buffer);
logger.printSuccessLog(`已保存像素差异热力图:${path.basename(heatmapPath)}`);
}
} catch (heatmapError) {
const errorMsg = heatmapError instanceof Error ? heatmapError.message : 'Unknown error';
logger.printWarnLog(`生成像素差异热力图失败:${errorMsg},将继续但不含热力图。`);
}
// Build userReport structure (pure data formatting)
const userReport = this.buildUserReportStructure(
validationResult.mae,
validationResult.sae,
misalignedData.length,
screenshots,
heatmap,
request.serverUrl,
request.figmaThumbnailUrl,
misalignedData
);
logger.printSuccessLog('校验报告已生成');
return {
userReport,
misalignedCount: misalignedData.length,
};
}
/**
* Creates a minimal user report for error scenarios.
*/
createMinimalReport(options: ErrorReportOptions): UserReport {
return {
design: {
snap: options.figmaThumbnailUrl || '',
markedSnap: '',
},
page: {
url: options.serverUrl,
snap: '',
markedSnap: '',
},
report: {
heatmap: '',
detail: {
metrics: {
mae: options.mae ?? -1,
sae: options.sae ?? -1,
misalignedCount: 0,
},
components: [],
},
},
};
}
/**
* Generate standalone HTML report file from userReport.
* Inlines all assets (JS/CSS) for single-file distribution.
*/
async generateHtml(userReport: UserReport, outputDir: string): Promise<GenerateHtmlResult> {
const reportDistDir = this.getReportDistDir();
const reportIndexHtml = path.join(reportDistDir, 'index.html');
logger.printInfoLog(`[ReportTool] Using template: ${reportIndexHtml}`);
logger.printInfoLog(`[ReportTool] Output directory: ${outputDir}`);
try {
// Check if template exists
try {
await fsPromises.access(reportIndexHtml);
} catch {
const errorMsg = `Template not found at ${reportIndexHtml}`;
logger.printErrorLog(`[ReportTool] ${errorMsg}. Skipping report generation.`);
return { success: false, error: errorMsg };
}
// Ensure output directory exists
await fsPromises.mkdir(outputDir, { recursive: true });
// Transform data if it exists
let reportData = userReport;
if (reportData) {
reportData = this.transformReportData(reportData);
}
logger.printTestLog(`Report data: ${JSON.stringify(reportData)}`);
let htmlContent = await fsPromises.readFile(reportIndexHtml, 'utf-8');
// 1. Inject Data
// Serialize report data as JSON and inject directly into the HTML
// Only escape characters that could break the HTML context
const jsonString = JSON.stringify(reportData)
.replace(/<\/script>/gi, '<\\/script>') // Prevent script injection
.replace(/\u2028/g, '\\u2028') // Line separator (breaks JS string literals)
.replace(/\u2029/g, '\\u2029'); // Paragraph separator (breaks JS string literals)
// Assign JSON object directly - no JSON.parse needed
const scriptTag = `<script>window.__REPORT_DATA__ = ${jsonString};</script>`;
htmlContent = htmlContent.replace(/<script>\s*window\.__REPORT_DATA__\s*=\s*null;?\s*<\/script>/, scriptTag);
// 2. Inline Assets (JS) to make it single-file
// Find ALL script tags with src="/assets/..." and pre-read files
const scriptRegexGlobal = /<script\s+type="module"\s+crossorigin\s+src="\/assets\/([^"]+)"><\/script>/g;
const jsMatches = [...htmlContent.matchAll(scriptRegexGlobal)];
const jsContentsMap = new Map<string, string>();
for (const match of jsMatches) {
const fileName = match[1];
if (fileName) {
const jsFilePath = path.join(reportDistDir, 'assets', fileName);
try {
let jsContent = await fsPromises.readFile(jsFilePath, 'utf-8');
// Prevent </script> inside JS from breaking HTML
jsContent = jsContent.replace(/<\/script>/g, '\\u003c/script>');
jsContentsMap.set(fileName, jsContent);
} catch {
logger.printWarnLog(`[ReportTool] JS asset not found: ${jsFilePath}`);
}
}
}
htmlContent = htmlContent.replace(scriptRegexGlobal, (match, fileName: string) => {
const jsContent = jsContentsMap.get(fileName);
if (jsContent) {
return `<script type="module">${jsContent}</script>`;
}
return match; // Keep original if file not found
});
// 3. Inline CSS if exists (Vite might produce css in assets)
const cssRegex = /<link\s+rel="stylesheet"\s+crossorigin\s+href="\/assets\/(.*?)">/;
const cssMatch = htmlContent.match(cssRegex);
if (cssMatch) {
const cssFileName = cssMatch[1];
if (!cssFileName) {
logger.printWarnLog('[ReportTool] CSS filename not found in match');
} else {
const cssFilePath = path.join(reportDistDir, 'assets', cssFileName);
try {
const cssContent = await fsPromises.readFile(cssFilePath, 'utf-8');
htmlContent = htmlContent.replace(cssMatch[0], `<style>${cssContent}</style>`);
} catch {
// CSS file not found, skip inlining
}
}
}
// Remove /index.css reference if it doesn't exist
htmlContent = htmlContent.replace('<link rel="stylesheet" href="/index.css">', '');
const absoluteOutputPath = path.join(outputDir, 'index.html');
await fsPromises.writeFile(absoluteOutputPath, htmlContent);
return { success: true, htmlPath: absoluteOutputPath };
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error);
logger.printErrorLog(`[ReportTool] Failed to generate report: ${errorMsg}`);
return { success: false, error: errorMsg };
}
}
// ==================== PRIVATE HELPER METHODS ====================
/**
* Build userReport structure from validation results
*/
private buildUserReportStructure(
mae: number,
sae: number,
misalignedCount: number,
screenshots: { renderSnap: string; renderMarked: string; targetMarked: string },
heatmap: string,
serverUrl: string,
figmaThumbnailUrl: string,
misalignedData: MisalignedComponentData[]
): UserReport {
return {
design: {
snap: figmaThumbnailUrl,
markedSnap: screenshots.targetMarked,
},
page: {
url: serverUrl,
snap: screenshots.renderSnap,
markedSnap: screenshots.renderMarked,
},
report: {
heatmap: heatmap,
detail: {
metrics: {
mae,
sae,
misalignedCount,
},
components: misalignedData.map(c => ({
componentId: c.componentId,
componentPath: c.componentPath,
elementId: c.elementId,
validationInfo: {
x: c.xDelta,
y: c.yDelta,
},
})),
},
},
};
}
/**
* Transform report data by grouping components by componentId
*/
private transformReportData(reportData: UserReport): UserReport {
const components = reportData?.report?.detail?.components || [];
if (!components?.length) return reportData;
// Group components by componentId
const groupedComponentsMap = new Map<string, UserReport['report']['detail']['components'][number]>();
components.forEach((component: UserReport['report']['detail']['components'][number], index: number) => {
const { componentId, componentPath, elementId, validationInfo } = component;
const elementIndex = index + 1;
if (!groupedComponentsMap.has(componentId)) {
groupedComponentsMap.set(componentId, {
componentId,
componentPath,
elements: [],
});
}
groupedComponentsMap.get(componentId)?.elements?.push({
elementId: elementId || '',
elementIndex,
validationInfo: validationInfo || { x: 0, y: 0 },
});
});
const groupedComponents = Array.from(groupedComponentsMap.values());
return {
...reportData,
report: {
...reportData.report,
detail: {
...reportData.report.detail,
components: groupedComponents,
},
},
};
}
/**
* Determine the root directory of the package by walking up to find package.json
*/
private getPackageRoot(): string {
// In ES modules, __dirname is not available, construct it from import.meta.url
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Walk up the directory tree to find package.json
let currentDir = __dirname;
while (currentDir !== path.parse(currentDir).root) {
if (fs.existsSync(path.join(currentDir, 'package.json'))) {
return currentDir;
}
currentDir = path.dirname(currentDir);
}
return process.cwd(); // Fallback
}
/**
* Get the report dist directory containing built HTML/assets
*/
private getReportDistDir(): string {
const root = this.getPackageRoot();
// Check standard locations for report template
const paths = [
path.join(root, 'dist/tools/report-tool/template'), // Production layout (if copied)
path.join(root, 'src/tools/report-tool/template/dist'), // Development layout
];
for (const p of paths) {
if (fs.existsSync(path.join(p, 'index.html'))) {
return p;
}
}
// Fallback to development layout
return path.join(root, 'src/tools/report-tool/template/dist');
}
}