184 lines
8.1 KiB
TypeScript
184 lines
8.1 KiB
TypeScript
import { writeFileSync } from "node:fs";
|
|
import { resolve } from "node:path";
|
|
import type { CorpConfig } from "../../core/config/types.js";
|
|
import type { createSealClient } from "../../core/http/factory.js";
|
|
import { runCapabilityResult } from "../helpers.js";
|
|
import { booleanOption, numberOption, stringOption } from "../parser.js";
|
|
import { outputOptions, printJson } from "../printer.js";
|
|
import { CliError, usageError } from "../../core/errors.js";
|
|
import {
|
|
BUILTIN_MONTHLY_TEMPLATE,
|
|
exportReportPackage,
|
|
renderReportFile,
|
|
renderVennAccuracySvg,
|
|
saveReportMarkdown,
|
|
validateReportFile,
|
|
loadReviews,
|
|
reviewFilePath,
|
|
writeReview,
|
|
type MonthlyReportResult
|
|
} from "../../domains/seal/reports/index.js";
|
|
import type { ParsedArgs } from "../types.js";
|
|
|
|
/** Tenant-free commands: dispatch these before resolving enterprise configuration or authentication. */
|
|
export async function handleLocalReportCommand(action: "validate" | "render" | "export" | "template" | "chart", args: ParsedArgs) {
|
|
if (action === "template") {
|
|
const out = stringOption(args.options.out) ?? stringOption(args.options.o);
|
|
if (out) {
|
|
writeFileSync(resolve(out), BUILTIN_MONTHLY_TEMPLATE, "utf8");
|
|
printJson({ file: resolve(out), size: Buffer.byteLength(BUILTIN_MONTHLY_TEMPLATE) }, outputOptions(args.options));
|
|
} else {
|
|
printJson({ content: BUILTIN_MONTHLY_TEMPLATE, size: Buffer.byteLength(BUILTIN_MONTHLY_TEMPLATE) }, outputOptions(args.options));
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (action === "chart") {
|
|
const sub = args.command[2];
|
|
if (sub !== "venn") throw usageError("Usage: seal-use report chart venn --ai <accuracy> --human <accuracy> [--both <accuracy>] [--out <file.svg>]");
|
|
const ai = numberOption(args.options.ai) ?? numberOption(args.options["ai-accuracy"]) ?? 92.08;
|
|
const human = numberOption(args.options.human) ?? numberOption(args.options["human-accuracy"]) ?? 83.33;
|
|
const both = numberOption(args.options.both) ?? numberOption(args.options["both-accurate"]) ?? 75.42;
|
|
const svg = renderVennAccuracySvg({ aiAccuracy: ai, humanAccuracy: human, bothAccurate: both });
|
|
const out = stringOption(args.options.out) ?? stringOption(args.options.o);
|
|
if (out) {
|
|
writeFileSync(resolve(out), svg, "utf8");
|
|
printJson({ file: resolve(out), format: "svg", aiAccuracy: ai, humanAccuracy: human, bothAccurate: both, size: Buffer.byteLength(svg) }, outputOptions(args.options));
|
|
} else {
|
|
printJson({ format: "svg", aiAccuracy: ai, humanAccuracy: human, bothAccurate: both, svg }, outputOptions(args.options));
|
|
}
|
|
return;
|
|
}
|
|
|
|
const file = args.command[2] ?? stringOption(args.options.file);
|
|
if (!file) throw usageError(`Usage: seal-use report ${action} <报告文件.md>${action === "render" ? " --out <产物路径词干>" : action === "export" ? " --out <目标文件夹>" : ""}`);
|
|
if ("python" in args.options) throw usageError("报告工具已移除 Python 依赖,请去掉 --python 参数。");
|
|
if (action === "validate") {
|
|
const result = await validateReportFile(file);
|
|
printJson(result, outputOptions(args.options));
|
|
if (!result.valid) throw new CliError("报告校验未通过,请按 issues 修正后重试。", 1);
|
|
return;
|
|
}
|
|
const out = stringOption(args.options.out) ?? stringOption(args.options.o);
|
|
if (!out) throw usageError(`Usage: seal-use report ${action} <报告文件.md> --out <${action === "export" ? "目标文件夹" : "产物路径词干"}>`);
|
|
if (action === "export") {
|
|
const result = await exportReportPackage(file, {
|
|
out, format: stringOption(args.options.format), width: numberOption(args.options.width),
|
|
chrome: stringOption(args.options.chrome), overwrite: booleanOption(args.options.overwrite)
|
|
});
|
|
printJson(result, outputOptions(args.options));
|
|
if (!result.validation.valid) throw new CliError("报告校验未通过,未生成完整导出包。请按 validation.issues 修正后重试。", 1);
|
|
return;
|
|
}
|
|
const result = await renderReportFile(file, {
|
|
out, format: stringOption(args.options.format), width: numberOption(args.options.width),
|
|
chrome: stringOption(args.options.chrome), overwrite: booleanOption(args.options.overwrite)
|
|
});
|
|
printJson(result, outputOptions(args.options));
|
|
if (!result.validation.valid) throw new CliError("报告校验未通过,未生成渲染产物。请按 validation.issues 修正后重试。", 1);
|
|
}
|
|
|
|
/** `seal-use report monthly` / `data` — 月度 AI 审核报告。 */
|
|
export async function handleReportCommand(
|
|
action: string | undefined,
|
|
client: Awaited<ReturnType<typeof createSealClient>>,
|
|
corp: CorpConfig,
|
|
args: ParsedArgs
|
|
) {
|
|
if (action === "data") {
|
|
const month = stringOption(args.options.month);
|
|
if (!month) {
|
|
throw new Error("Usage: seal-use report data --month <YYYY-MM> [--max-runs <n>] [--corp <corpId>]");
|
|
}
|
|
const result = await runCapabilityResult(
|
|
"sealReportDataGet",
|
|
client,
|
|
corp,
|
|
{
|
|
month,
|
|
maxRuns: numberOption(args.options["max-runs"]) ?? numberOption(args.options.maxRuns)
|
|
}
|
|
);
|
|
printJson(result, outputOptions(args.options));
|
|
return;
|
|
}
|
|
|
|
if (action === "monthly" || action === "month") {
|
|
const month = stringOption(args.options.month);
|
|
if (!month) {
|
|
throw new Error("Usage: seal-use report monthly --month <YYYY-MM> [--template <file>] [--report-id <RPT-...>] [--corp <corpId>]");
|
|
}
|
|
const result = await runCapabilityResult(
|
|
"sealReportMonthlyGet",
|
|
client,
|
|
corp,
|
|
{
|
|
month,
|
|
templatePath: stringOption(args.options.template),
|
|
reportId: stringOption(args.options["report-id"]) ?? stringOption(args.options.reportId),
|
|
maxRuns: numberOption(args.options["max-runs"]) ?? numberOption(args.options.maxRuns)
|
|
}
|
|
) as MonthlyReportResult;
|
|
const out = stringOption(args.options.out) ?? stringOption(args.options.o);
|
|
if (out) {
|
|
const saved = saveReportMarkdown(
|
|
result.content,
|
|
result.templatePath,
|
|
out,
|
|
booleanOption(args.options.overwrite),
|
|
result.dynamicAssets,
|
|
result.companionFiles
|
|
);
|
|
const quotedFile = `'${saved.file.replace(/'/g, "'\\''")}'`;
|
|
printJson({ ...result, content: undefined, dynamicAssets: undefined, companionFiles: undefined, ...saved, next: {
|
|
validate: `seal-use report validate ${quotedFile}`,
|
|
render: `seal-use report render ${quotedFile} --out build/report`
|
|
} }, outputOptions(args.options));
|
|
} else printJson({ ...result, dynamicAssets: undefined, companionFiles: undefined }, outputOptions(args.options));
|
|
return;
|
|
}
|
|
|
|
if (action === "review") {
|
|
const sub = args.command[2]; // set | list
|
|
const month = stringOption(args.options.month);
|
|
if (!month) {
|
|
throw new Error("Usage: seal-use report review <set|list> --month <YYYY-MM> [--sn <SN> --verdict <v>] [--corp <corpId>]");
|
|
}
|
|
|
|
if (sub === "list") {
|
|
const reviews = loadReviews(corp.id, month);
|
|
printJson({
|
|
corpId: corp.id,
|
|
month,
|
|
file: reviewFilePath(corp.id, month),
|
|
count: reviews.size,
|
|
reviews: [...reviews.values()]
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (sub === "set") {
|
|
const sn = stringOption(args.options.sn) ?? stringOption(args.options["document-sn"]);
|
|
const verdict = stringOption(args.options.verdict);
|
|
if (!sn || !verdict) {
|
|
throw new Error(
|
|
"Usage: seal-use report review set --month <YYYY-MM> --sn <SN> --verdict <ai_correct|human_correct|both_correct|both_wrong|undecided> [--note <text>] [--reviewed-by <who>] [--corp <corpId>]"
|
|
);
|
|
}
|
|
const record = writeReview(corp.id, {
|
|
sn,
|
|
month,
|
|
verdict,
|
|
note: stringOption(args.options.note),
|
|
reviewedBy: stringOption(args.options["reviewed-by"]) ?? stringOption(args.options.reviewedBy)
|
|
});
|
|
printJson({ corpId: corp.id, file: reviewFilePath(corp.id, month), record });
|
|
return;
|
|
}
|
|
|
|
throw new Error(`Unknown report review subcommand: ${sub ?? ""}. Use 'set' or 'list'.`);
|
|
}
|
|
|
|
throw new Error(`Unknown report command: ${action ?? ""}. Run 'seal-use report --help' for available commands.`);
|
|
}
|