import { printJson } from "../../printer.js"; import { VERSION } from "../../../version.js"; import { checkAiProLicense } from "../../aipro-license.js"; import type { AiProCommandResult, AiProExecutionContext } from "./types.js"; /** * hose-aipro report --json:本地 Web 控制台的聚合数据源。 * 只读、不联网;单据快照等大字段不返回,避免撑爆浏览器。 */ export function handleAiProReport(ctx: AiProExecutionContext): AiProCommandResult { const { repo, corp, corpId, operator } = ctx; const scopes = repo.listScopes("active"); const rules = repo.listRules(); const runs = repo.listRuns({ corpId, limit: 500 }); const logs = repo.listAiProLogs(undefined, 200); const license = checkAiProLicense(); const runRows = runs.map((run) => ({ id: run.id, shortId: run.shortId, documentSn: run.documentSn, corpId: run.corpId, decision: run.decision, createdAt: run.createdAt, updatedAt: run.updatedAt, findingsCount: repo.getFindingsByRunId(run.id).length, })); const findings = runs.flatMap((run) => repo.getFindingsByRunId(run.id).map((finding) => ({ runId: run.shortId || run.id, documentSn: run.documentSn, ruleCode: finding.ruleCode, status: finding.checkStatus, strictness: finding.strictness, content: finding.clauseText, evidence: finding.evidence, reasoning: finding.reasoning, })) ); const payload = { version: VERSION, generatedAt: new Date().toISOString(), corp: corp ? { id: corp.id, name: corp.name, staffId: corp.credentials?.staffId || corp.staffId || null, baseUrl: corp.baseUrl || null, source: corp.source || null, } : null, operator, license: license.ok ? { activated: true, subject: license.payload.sub, expiresAt: new Date(license.payload.exp * 1000).toISOString(), maxVersion: license.payload.maxVersion, } : { activated: false, reason: license.reason, message: license.message }, summary: { scopes: scopes.length, rules: rules.length, runs: runRows.length, pending: runRows.filter((run) => run.decision === "PENDING").length, findings: findings.length, logs: logs.length, }, scopes, rules, runs: runRows, findings, logs, }; return { stdout: printJson(payload) + "\n", stderr: "", exitCode: 0 }; }