Decouple chapter numbers from validation and pipeline rendering to allow arbitrary chapter order

This commit is contained in:
gitea committed 2026-10-15 16:39:33 +08:00
1 parent a52b6ac117
commit f9f72fa918
3 files changed
+86 -35

No files matched your search

+35 -32
View File
@@ -210,28 +210,29 @@ function fillInline(text: string, data: ReportData, ctx: FillContext): string {
const p1 = wrong > 0 ? Number((100 - p2 - p3).toFixed(1)) : 0;
result = result.replace(
/###\s*2\.2\s*TOP\s*1[^(\n]*(\d+\s*张[^)\n]*)/,
`### 2.2 TOP 1 · 规则执行与判定偏差(${c1} 张 / ${p1.toFixed(1)}%)`
/###\s*(\d+\.\d+)\s*TOP\s*1[^(\n]*(\d+\s*张[^)\n]*)/,
`### $1 TOP 1 · 规则执行与判定偏差(${c1} 张 / ${p1.toFixed(1)}%)`
);
result = result.replace(
/###\s*2\.3\s*TOP\s*2[^(\n]*(\d+\s*张[^)\n]*)/,
`### 2.3 TOP 2 · 数据与附件识别问题(${c2} 张 / ${p2.toFixed(1)}%)`
/###\s*(\d+\.\d+)\s*TOP\s*2[^(\n]*(\d+\s*张[^)\n]*)/,
`### $1 TOP 2 · 数据与附件识别问题(${c2} 张 / ${p2.toFixed(1)}%)`
);
result = result.replace(
/###\s*2\.4\s*TOP\s*3[^(\n]*(\d+\s*张[^)\n]*)/,
`### 2.4 TOP 3 · 费用明细查重冲突(${c3} 张 / ${p3.toFixed(1)}%)`
/###\s*(\d+\.\d+)\s*TOP\s*3[^(\n]*(\d+\s*张[^)\n]*)/,
`### $1 TOP 3 · 费用明细查重冲突(${c3} 张 / ${p3.toFixed(1)}%)`
);
const sec3Idx = result.indexOf("## 3.");
if (sec3Idx > 0) {
let beforeSec3 = result.slice(0, sec3Idx);
const riskSecMatch = result.match(/##\s*\d+\.\s*[^#\n]*风险/);
const secRiskIdx = riskSecMatch ? result.indexOf(riskSecMatch[0]) : -1;
if (secRiskIdx > 0) {
let beforeSecRisk = result.slice(0, secRiskIdx);
let fnIdx = 0;
const fnCounts = [c1, c2, c3];
beforeSec3 = beforeSec3.replace(/(节选 3 例,全量 \d+ 张)/g, (m) => {
beforeSecRisk = beforeSecRisk.replace(/(节选 3 例,全量 \d+ 张)/g, (m) => {
const val = fnCounts[fnIdx++];
return val !== undefined ? `(节选 3 例,全量 ${val} 张)` : m;
});
result = beforeSec3 + result.slice(sec3Idx);
result = beforeSecRisk + result.slice(secRiskIdx);
}
}
}
@@ -240,7 +241,7 @@ function fillInline(text: string, data: ReportData, ctx: FillContext): string {
result = result.replace(/\*\*83\.33%\*\*/g, `**${pct(data.humanAccuracy)}**`);
}
// §3 小节标题与脚注单数同步:将总览表中的真实风险类型与张数同步至小节标题(解决 NM002 勾稽校验)
// 小节标题与脚注单数同步:将总览表中的真实风险类型与张数同步至小节标题(解决 NM002 勾稽校验)
if (data.fetched.riskStats && data.riskStats?.tags.length) {
const tags = data.riskStats.tags.filter((t) => t.count !== undefined).slice(0, 3);
const totalRisk = tags.reduce((sum, t) => sum + (t.count ?? 0), 0) || 1;
@@ -249,16 +250,17 @@ function fillInline(text: string, data: ReportData, ctx: FillContext): string {
const count = tag.count ?? 0;
const share = ((count / totalRisk) * 100).toFixed(1);
const name = tag.name ?? `TOP ${topNum} 风险`;
const reg = new RegExp(`###\\s*3\\.${topNum + 1}\\s*TOP\\s*${topNum}[^(\n]*(\\d+\\s*张[^)\n]*)`, "g");
result = result.replace(reg, `### 3.${topNum + 1} TOP ${topNum} · ${name}(${count} 张 / ${share}%)`);
const reg = new RegExp(`###\\s*(\\d+\\.\\d+)\\s*TOP\\s*${topNum}[^(\n]*(\\d+\\s*张[^)\n]*)`, "g");
result = result.replace(reg, `### $1 TOP ${topNum} · ${name}(${count} 张 / ${share}%)`);
});
const sec3Idx = result.indexOf("## 3.");
if (sec3Idx >= 0) {
let fromSec3 = result.slice(sec3Idx);
if (tags[0]?.count) fromSec3 = fromSec3.replace(/(节选 3 例,全量 23 张)/, `(节选 3 例,全量 ${tags[0].count} 张)`);
if (tags[1]?.count) fromSec3 = fromSec3.replace(/(节选 3 例,全量 9 张)/, `(节选 3 例,全量 ${tags[1].count} 张)`);
if (tags[2]?.count) fromSec3 = fromSec3.replace(/(节选 3 条,全量 8 张)/, `(节选 3 例,全量 ${tags[2].count} 张)`);
result = result.slice(0, sec3Idx) + fromSec3;
const riskSecMatch = result.match(/##\s*\d+\.\s*[^#\n]*风险/);
const secRiskIdx = riskSecMatch ? result.indexOf(riskSecMatch[0]) : -1;
if (secRiskIdx >= 0) {
let fromSecRisk = result.slice(secRiskIdx);
if (tags[0]?.count) fromSecRisk = fromSecRisk.replace(/(节选 3 例,全量 23 张)/, `(节选 3 例,全量 ${tags[0].count} 张)`);
if (tags[1]?.count) fromSecRisk = fromSecRisk.replace(/(节选 3 例,全量 9 张)/, `(节选 3 例,全量 ${tags[1].count} 张)`);
if (tags[2]?.count) fromSecRisk = fromSecRisk.replace(/(节选 3 条,全量 8 张)/, `(节选 3 例,全量 ${tags[2].count} 张)`);
result = result.slice(0, secRiskIdx) + fromSecRisk;
}
}
@@ -363,7 +365,7 @@ function transformTable(
if (!columns.length) return block;
const notes = noteAfter(columns, heading, lastH2, data);
const rows = replacementRows(columns, sampleRows, data, heading);
const rows = replacementRows(columns, sampleRows, data, heading, lastH2);
// 无论是否替换数据行,缺口径说明都要追加(模板保留时同样需要知会)
if (!rows) return [...block, ...notes];
return [header, separator, ...rows.map((row) => `| ${row.map(escapeCell).join(" | ")} |`), ...notes];
@@ -400,7 +402,8 @@ function replacementRows(
columns: string[],
sampleRows: string[],
data: ReportData,
heading: string
heading: string,
lastH2: string
): Array<Array<string | number>> | null {
const perf = data.performance;
@@ -667,17 +670,17 @@ function replacementRows(
});
}
// §3 潜在风险明细表(支持 2 列:单号 | 问题描述 或 3 列:单号 | 问题描述 | 审核详情)
if (columns.includes("单号") && columns.includes("问题描述") && heading.includes("3.")) {
// 潜在风险明细表(支持 2 列:单号 | 问题描述 或 3 列:单号 | 问题描述 | 审核详情)
if (columns.includes("单号") && columns.includes("问题描述") && (heading.includes("风险") || lastH2.includes("风险"))) {
if (!data.blocks.length) return null;
let picks: ReportBlock[] = [];
if (heading.includes("3.2") || heading.includes("TOP 1")) {
if (heading.includes("TOP 1") || heading.includes("3.2")) {
const matches = data.blocks.filter((b) => /招待|餐费|标准|超标|人均|酒水/i.test(b.aiSummary || ""));
picks = matches.length >= 3 ? matches.slice(0, 3) : data.blocks.filter((b) => b.agreement === "diverged").slice(0, 3);
} else if (heading.includes("3.3") || heading.includes("TOP 2")) {
} else if (heading.includes("TOP 2") || heading.includes("3.3")) {
const matches = data.blocks.filter((b) => /比价|询价|合同|采购|订单|对账单|签章|未签署/i.test(b.aiSummary || ""));
picks = matches.length >= 3 ? matches.slice(0, 3) : data.blocks.filter((b) => b.agreement === "diverged").slice(3, 6);
} else if (heading.includes("3.4") || heading.includes("TOP 3")) {
} else if (heading.includes("TOP 3") || heading.includes("3.4")) {
const matches = data.blocks.filter((b) => /超期|跨季度|时效|发票日期|过期|出差申请/i.test(b.aiSummary || ""));
picks = matches.length >= 3 ? matches.slice(0, 3) : data.blocks.filter((b) => b.agreement === "diverged").slice(6, 9);
}
@@ -698,8 +701,8 @@ function replacementRows(
});
}
// §2 失准与复核分析明细表(支持 2 列:单号 | 问题描述 或 3 列:单号 | 问题描述 | 审核详情)
if (columns.includes("单号") && columns.includes("问题描述") && heading.includes("2.")) {
// 失准与复核分析明细表(支持 2 列:单号 | 问题描述 或 3 列:单号 | 问题描述 | 审核详情)
if (columns.includes("单号") && columns.includes("问题描述") && (heading.includes("失准") || lastH2.includes("失准"))) {
if (!data.blocks.length) return null;
const humanCorrectBlocks = data.blocks.filter((b) => b.verdict === "human_correct");
const candidates = humanCorrectBlocks.length >= 3
@@ -708,8 +711,8 @@ function replacementRows(
if (!candidates.length) return null;
let sliceIndex = 0;
if (heading.includes("2.3") || heading.includes("TOP 2")) sliceIndex = 1;
else if (heading.includes("2.4") || heading.includes("TOP 3")) sliceIndex = 2;
if (heading.includes("TOP 2") || heading.includes("2.3")) sliceIndex = 1;
else if (heading.includes("TOP 3") || heading.includes("2.4")) sliceIndex = 2;
const start = sliceIndex * 3;
const picks = candidates.slice(start, start + 3);
@@ -449,6 +449,47 @@ describe("getMonthlyReportData", () => {
});
expect(draft.content).toBeDefined();
});
test("supports custom chapters inserted in the middle and arbitrary section ordering", () => {
const template = [
"# 【测试企业】8月AI审核准确率报告",
"## 1. 审核测试结果",
"| 审核方式 | 准确率 |",
"| --- | --- |",
"| AI 审核 | 90% |",
"| 人工审核 | 80% |",
"",
"## 2. 营销线专项业务洞察(新插入章节)",
"本章节为针对营销业务线的专项分析。",
"| 业务条线 | 抽检单数 | 合规率 |",
"| --- | --- | --- |",
"| 华东大区 | 50 | 98% |",
"| 华南大区 | 45 | 96% |",
"",
"## 3. AI 发现的潜在风险",
"### 3.1 风险构成总览",
"| 维度 | 风险类型 | 占比 | 单数 |",
"| --- | --- | --- | --- |",
"| TOP 1 | 缺失要求凭证 | 100% | 23 张 |",
"| 合计 | — | 100% | 23 张 |",
"",
"### 3.2 TOP 1 · 缺失要求凭证(23 张 / 100.0%)",
"| 单号 | 问题描述 |",
"| --- | --- |",
"| FP260011 | 缺失附件 |"
].join("\n");
const plans = describeTemplate(template);
expect(plans).toHaveLength(3);
expect(plans[1]!.demands).toEqual([]); // 纯叙述新章节无需数据依赖
expect(plans[2]!.demands).toContain("riskStats");
const out = renderMonthlyReport(template, sampleData(), plans);
expect(out).toContain("营销线专项业务洞察(新插入章节)");
expect(out).toContain("华东大区");
expect(out).toContain("华南大区");
expect(out).toContain("缺失要求凭证");
});
});
+10 -3
View File
@@ -133,11 +133,18 @@ export function validateReportDocument(file: string): ReportValidation {
error("NM003", `${keyword}「${row[1]}」占比 ${decimal(share)}% ≠ ${count.toFixed(0)}/${base.toFixed(0)} = ${(count / base * 100).toFixed(1)}%`)
}
}
// 小节标题里的张数与总览表一致
let sectionNum = keyword === "失准类型" ? "2" : "3"
for (let i = table.line - 1; i >= 0; i--) {
const h2Match = lines[i]?.match(/^##\s*(\d+)\./)
if (h2Match) {
sectionNum = h2Match[1]!
break
}
}
// 小节标题里的张数与总览表一致(动态定位所属章节序号,支持章节任意重排)
for (const top of tops) {
const section = keyword === "失准类型" ? "2" : "3"
const escaped = top.name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
const match = text.match(new RegExp(`###\\s*${section}\\.\\d+\\s*${escaped}[^(]*((\\d+)\\s*张`))
const match = text.match(new RegExp(`###\\s*${sectionNum}\\.\\d+\\s*${escaped}[^(]*((\\d+)\\s*张`))
if (match && top.count !== null && Number(match[1]) !== top.count) error(tag, `${top.name} 小节标题写 ${match[1]} 张,总览表写 ${top.count.toFixed(0)} 张`)
}
}