|
| 1 | +import { writeFileSync } from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import type { CaseResult, RunnerResult, SuiteRunResult } from "../domain/result.js"; |
| 4 | +import type { Case } from "../domain/case.js"; |
| 5 | +import type { SessionEvent } from "../domain/session-report.js"; |
| 6 | +import type { BenchmarkReporter, CaseFinishEvent } from "./contract.js"; |
| 7 | + |
| 8 | +function esc(str: string): string { |
| 9 | + return str |
| 10 | + .replace(/&/g, "&") |
| 11 | + .replace(/</g, "<") |
| 12 | + .replace(/>/g, ">") |
| 13 | + .replace(/"/g, """); |
| 14 | +} |
| 15 | + |
| 16 | +function ms(n: number | undefined): string { |
| 17 | + if (n == null) return "—"; |
| 18 | + return n < 1000 ? `${n}ms` : `${(n / 1000).toFixed(1)}s`; |
| 19 | +} |
| 20 | + |
| 21 | +function tokens(n: number | undefined): string { |
| 22 | + if (n == null) return "—"; |
| 23 | + return n >= 1000 ? `${(n / 1000).toFixed(1)}k` : String(n); |
| 24 | +} |
| 25 | + |
| 26 | +function renderEvent(e: SessionEvent): string { |
| 27 | + if (e.type === "message" && e.role === "assistant") { |
| 28 | + return `<details><summary>assistant message</summary><pre>${esc(e.text)}</pre></details>`; |
| 29 | + } |
| 30 | + if (e.type === "toolCall") { |
| 31 | + const args = e.args ? JSON.stringify(e.args, null, 2) : ""; |
| 32 | + return `<details><summary>tool: ${esc(e.tool)}</summary><pre>${esc(args)}</pre></details>`; |
| 33 | + } |
| 34 | + if (e.type === "fileRead") { |
| 35 | + return `<p>read: <code>${esc(e.path)}</code></p>`; |
| 36 | + } |
| 37 | + if (e.type === "command") { |
| 38 | + return `<p>cmd: <code>${esc(e.command)}</code></p>`; |
| 39 | + } |
| 40 | + return ""; |
| 41 | +} |
| 42 | + |
| 43 | +function renderRunnerBlock(rr: RunnerResult): string { |
| 44 | + const report = rr.report; |
| 45 | + const statusLabel = rr.passed ? "PASS" : "FAIL"; |
| 46 | + const runnerId = rr.runner?.id ?? "unknown"; |
| 47 | + const eventsHtml = (report?.events ?? []).map(renderEvent).join("\n"); |
| 48 | + const errorHtml = rr.error |
| 49 | + ? `<pre>${esc(rr.error.message)}\n${esc(rr.error.stack ?? "")}</pre>` |
| 50 | + : ""; |
| 51 | + const finalOutput = report?.finalOutput |
| 52 | + ? `<details><summary>final output</summary><pre>${esc(report.finalOutput)}</pre></details>` |
| 53 | + : ""; |
| 54 | + |
| 55 | + return ` |
| 56 | + <section> |
| 57 | + <h3>${esc(runnerId)} — ${statusLabel} — ${ms(rr.durationMs)} |
| 58 | + in: ${tokens(report?.usage?.inputTokens)} / out: ${tokens(report?.usage?.outputTokens)} |
| 59 | + </h3> |
| 60 | + ${errorHtml} |
| 61 | + ${finalOutput} |
| 62 | + ${eventsHtml} |
| 63 | + </section>`; |
| 64 | +} |
| 65 | + |
| 66 | +function renderCaseSection(testCase: Case, caseResult: CaseResult): string { |
| 67 | + const status = caseResult.passed ? "✓" : "✗"; |
| 68 | + const runnerBlocks = (caseResult.runnerResults ?? []) |
| 69 | + .filter(Boolean) |
| 70 | + .map(renderRunnerBlock) |
| 71 | + .join("\n"); |
| 72 | + |
| 73 | + return ` |
| 74 | + <details> |
| 75 | + <summary><strong>${status} ${esc(testCase.id)}</strong></summary> |
| 76 | + <blockquote><pre>${esc(testCase.prompt)}</pre></blockquote> |
| 77 | + ${runnerBlocks} |
| 78 | + </details>`; |
| 79 | +} |
| 80 | + |
| 81 | +function renderPage(result: SuiteRunResult, caseSections: string): string { |
| 82 | + const passed = result.cases.filter((c) => c.passed).length; |
| 83 | + const total = result.cases.length; |
| 84 | + |
| 85 | + return ` |
| 86 | + <!DOCTYPE html> |
| 87 | + <html lang="en"> |
| 88 | + <head> |
| 89 | + <meta charset="UTF-8"> |
| 90 | + <title>skillgym — ${esc(result.suitePath)}</title> |
| 91 | + <style> |
| 92 | + body { font-family: monospace; margin: 2em; } |
| 93 | + pre { white-space: pre-wrap; word-break: break-word; background: #f4f4f4; padding: 0.5em; } |
| 94 | + summary { cursor: pointer; } |
| 95 | + blockquote { border-left: 3px solid #ccc; margin: 0; padding-left: 1em; } |
| 96 | + h3 { margin: 0.5em 0; font-size: 1em; } |
| 97 | + section { margin-left: 1em; border-left: 2px solid #eee; padding-left: 1em; } |
| 98 | + </style> |
| 99 | + </head> |
| 100 | + <body> |
| 101 | + <h1>skillgym report</h1> |
| 102 | + <p>${esc(result.suitePath)}</p> |
| 103 | + <p>${passed}/${total} passed ${ms(result.durationMs)}</p> |
| 104 | + <hr> |
| 105 | + ${caseSections} |
| 106 | + </body> |
| 107 | + </html>`; |
| 108 | +} |
| 109 | + |
| 110 | +export function createHtmlReporter(): BenchmarkReporter { |
| 111 | + const caseResults: CaseFinishEvent[] = []; |
| 112 | + |
| 113 | + return { |
| 114 | + onCaseFinish(event) { |
| 115 | + caseResults.push(event); |
| 116 | + }, |
| 117 | + |
| 118 | + onSuiteFinish(event) { |
| 119 | + try { |
| 120 | + const { result } = event; |
| 121 | + const caseSections = caseResults |
| 122 | + .map((ev) => renderCaseSection(ev.case, ev.result)) |
| 123 | + .join("\n"); |
| 124 | + const html = renderPage(result, caseSections); |
| 125 | + const outPath = join(result.suiteRunArtifactDir, "report.html"); |
| 126 | + writeFileSync(outPath, html, "utf-8"); |
| 127 | + console.log(`\nHTML report: ${outPath}\n`); |
| 128 | + } catch (err) { |
| 129 | + console.error("html-reporter error:", err); |
| 130 | + } |
| 131 | + }, |
| 132 | + }; |
| 133 | +} |
0 commit comments