/** * Stop pipeline from overwriting bodyText with HTML-normalized / OCR-merged text. * Parse still uses a local normalized body; OCR stays in ocrText only. */ import fs from "fs"; const path = "src/services/parse/pipeline.ts"; let src = fs.readFileSync(path, "utf8"); const oldNorm = ` const subject = mail.subject?.trim() ? mail.subject : "(无主�?"; // 历史库可能存了原�?HTML;解析前先归一成纯文本 let body = plainTextFromMaybeHtml(mail.bodyText || ""); if (body && body !== (mail.bodyText || "").trim()) { await prisma.mailMessage.update({ where: { id: mailId }, data: { bodyText: body }, }); }`; const neuNorm = ` const subject = mail.subject?.trim() ? mail.subject : "(无主�?"; // 解析用归一化正文;绝不回写 bodyText(排查用原文一字不落) const bodyStored = mail.bodyText || ""; let body = plainTextFromMaybeHtml(bodyStored) || bodyStored;`; if (!src.includes(oldNorm)) { const oldCrlf = oldNorm.replace(/\n/g, "\r\n"); if (!src.includes(oldCrlf)) { console.error("norm block missing"); process.exit(1); } src = src.replace(oldCrlf, neuNorm.replace(/\n/g, "\r\n")); } else { src = src.replace(oldNorm, neuNorm); } const oldOcr = ` body = body.trim() ? \`\${body}\\n\\n\${ocr.text}\` : ocr.text; await prisma.mailMessage.update({ where: { id: mailId }, data: { ocrText: ocr.text, bodyText: body }, });`; const neuOcr = ` // OCR 只写�?ocrText,不污染 bodyText if (!body.trim()) body = ocr.text; else body = \`\${body}\\n\\n\${ocr.text}\`; await prisma.mailMessage.update({ where: { id: mailId }, data: { ocrText: ocr.text }, });`; if (!src.includes("ocrText: ocr.text, bodyText: body")) { // try already patched if (!src.includes("OCR 只写�?ocrText")) { console.error("ocr block missing", src.includes("bodyText: body")); process.exit(1); } } else { src = src.replace( /body = body\.trim\(\)\s*\?\s*`\$\{body\}\\n\\n\$\{ocr\.text\}`\s*:\s*ocr\.text;\s*await prisma\.mailMessage\.update\(\{\s*where: \{ id: mailId \},\s*data: \{ ocrText: ocr\.text, bodyText: body \},\s*\}\);/s, `// OCR only in ocrText �?never overwrite bodyText if (!body.trim()) body = ocr.text; else body = \`\${body}\\n\\n\${ocr.text}\`; await prisma.mailMessage.update({ where: { id: mailId }, data: { ocrText: ocr.text }, });`, ); } fs.writeFileSync(path, src); console.log("pipeline body preserve ok", { hasStored: src.includes("bodyStored"), ocrOnly: src.includes("ocrText: ocr.text }") || src.includes("data: { ocrText: ocr.text }"), });