Files
seal-use/src/domains/seal/reports/monthly.ts
T
gitea 2ee15bb0d6 Add one-shot monthly audit report command (report monthly)
Eats in the ai-audit-report-template as a module. The template is the mutable
skin: describeTemplate() infers per-section data demands from ##/### headings
(keyword rules), so editing/renaming/reordering sections does not require
touching collect or render code.

- src/domains/seal/reports/: describe (heading to demand), collect (performance/
  risk blocks/template groups, fetched only for declared demands), render (fills
  tables by section+column signature, rewrites narrative by structure not line
  number), monthly (orchestrator returning content + per-section completeness +
  notes).
- report-templates/: the template body, assets, example, and downstream
  validate.py/render.py (not bundled; next.validate/render in the result point
  at them).
- Capability sealReportMonthlyGet (read-only, allow-listed) + CLI
  "seal-use report monthly --month <YYYY-MM>" + help + KNOWN_FLAGS month +
  read-only report:monthly.
- SKILL.md: routing row + a section stating the two hard rules: the template is
  swappable, and metrics with no upstream source keep template sample values
  with a warning note instead of fabricated numbers.

Template-path resolution branches on isCompiledBinary() (dev: module-relative to
repo root; compiled: cwd), matching how rules lint --file resolves paths, since
templates are not embedded.

Verified: bun test 633 pass (6 new reports tests), tsc clean, oxlint 0 warnings.
Live smoke: 2026-08 report for a real corp fills real accuracy/counts/distribution
and marks human-accuracy + mismatch-classification + section-6 sample rows with
gap notes.
2026-09-30 17:55:37 +08:00

92 lines
3.1 KiB
TypeScript

import type { KyInstance } from "ky";
import { existsSync, readFileSync } from "node:fs";
import { join, resolve } from "node:path";
import { isCompiledBinary } from "../../../core/runtime.js";
import { describeTemplate } from "./describe.js";
import { collectReportData, type ReportData } from "./collect.js";
import { renderMonthlyReport, type RenderOptions } from "./render.js";
export type MonthlyReportOptions = {
month: string; // YYYY-MM
corp: { id: string; name: string };
/** 模板路径;缺省用仓内 report-templates/monthly.md */
templatePath?: string;
reportId?: string;
maxRuns?: number;
};
export type MonthlyReportResult = {
month: string;
corpId: string;
reportId: string;
templatePath: string;
/** 模板各章节声明的数据需求与取数结果,供 agent 判断哪些章节可直接用、哪些需人工补 */
sections: Array<{ heading: string; demands: string[]; fetched: string[] }>;
notes: ReportData["notes"];
/** 报告 Markdown 本体 */
content: string;
next: {
validate: string;
render: string;
};
};
function defaultTemplatePath(): string {
if (isCompiledBinary()) {
// 模板不进包;编译产物用 cwd(与 rules lint --file 的相对路径解析一致)
return join(process.cwd(), "report-templates", "monthly.md");
}
// dev: src/domains/seal/reports → 仓库根
return join(import.meta.dirname, "..", "..", "..", "..", "report-templates", "monthly.md");
}
/** 一次调用产出填充好的月度报告 Markdown。模板皮与数据骨解耦:见 describe.ts。 */
export async function buildMonthlyReport(
client: KyInstance,
options: MonthlyReportOptions
): Promise<MonthlyReportResult> {
const templatePath = options.templatePath
? resolve(options.templatePath)
: defaultTemplatePath();
if (!existsSync(templatePath)) {
throw new Error(
`未找到报告模板: ${templatePath}\n` +
`可用 --template <file> 指定路径,或在 report-templates/ 目录下运行。`
);
}
const template = readFileSync(templatePath, "utf8");
const plans = describeTemplate(template);
const demands = new Set(plans.flatMap((plan) => plan.demands));
const data = await collectReportData(client, demands, {
month: options.month,
corp: options.corp,
maxRuns: options.maxRuns
});
const renderOptions: RenderOptions = {
reportId: options.reportId,
corpName: options.corp.name
};
const content = renderMonthlyReport(template, data, plans, renderOptions);
const reportId = options.reportId ?? `RPT-${options.month.replace("-", "")}-XXXXXX`;
return {
month: options.month,
corpId: options.corp.id,
reportId,
templatePath,
sections: plans.map((plan) => ({
heading: plan.heading,
demands: plan.demands,
fetched: plan.demands.filter((demand) => data.fetched[demand])
})),
notes: data.notes,
content,
next: {
validate: `python3 tools/validate.py <报告文件.md>(校验器与渲染器见 ai-audit-report-template 仓库)`,
render: `python3 tools/render.py <报告文件.md> -o build/<报告文件>(产出 HTML / PNG / A4 PDF 三件套)`
}
};
}