You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.3 KiB

/**
* Product rules:
* 1) Only latest instruction segment confirmable; history readonly
* 2) Soft keywords stay work_order
* 3) No-keyword mail (mail1) -> work_order fallback
*/
import fs from "fs";
const path = "src/services/parse/split-instructions.ts";
let src = fs.readFileSync(path, "utf8");
const marker = " // 2) ";
const idx = src.indexOf(marker);
if (idx < 0) throw new Error("section 2 marker missing");
const attMarker = " // 3) ";
const attIdx = src.indexOf(attMarker, idx);
if (attIdx < 0) throw new Error("section 3 marker missing");
const sortMarker = " // \u5c55\u793a\u987a\u5e8f\uff1a\u5f53\u524d\u6bb5\u4f18\u5148";
const sortIdx = src.indexOf(sortMarker);
if (sortIdx < 0) throw new Error("sort marker missing");
const section2 = ` // 2) \u6b63\u6587\u6309\u5bf9\u8bdd\u6bb5\u62c6\u5206\uff1b\u4ec5\u300c\u6700\u65b0\u4e00\u6761\u6709\u6307\u4ee4\u7684\u6bb5\u300d\u53ef\u786e\u8ba4\uff0c\u5386\u53f2\u53ea\u8bfb
const segments = splitBodySegments(body);
let currentSegIndex: number | null = null;
for (let i = 0; i < segments.length; i++) {
const seg = segments[i];
if (isNonInstructionSegment(seg)) continue;
const kinds = detectKindsForSegment(extractSegmentSubject(seg, ""), seg);
if (kinds.length) {
currentSegIndex = i;
break;
}
}
// \u65e0\u5173\u952e\u8bcd\u6bb5\uff1a\u9996\u4e2a\u975e\u58f3\u6bb5\u4f5c\u4e3a\u515c\u5e95\u5de5\u5355\u8f7d\u4f53
if (currentSegIndex === null) {
for (let i = 0; i < segments.length; i++) {
if (!isNonInstructionSegment(segments[i])) {
currentSegIndex = i;
break;
}
}
}
const kindsInCurrentBody = new Set<InstructionUiKind>();
segments.forEach((seg, i) => {
if (isNonInstructionSegment(seg)) return;
const segSubject = extractSegmentSubject(seg, "");
const kinds = detectKindsForSegment(segSubject, seg);
const isCurrent = currentSegIndex !== null && i === currentSegIndex;
if (!kinds.length) {
// \u90ae\u4ef61 \u7b49\uff1a\u65e0\u56db\u7c7b\u5173\u952e\u8bcd \u2192 \u6700\u65b0\u6bb5\u515c\u5e95\u4e3a\u5de5\u5355
if (isCurrent) {
kindsInCurrentBody.add("work_order");
push(
makeUnit({
uiKind: "work_order",
source: "body_segment",
segmentIndex: i,
isCurrent: true,
text: seg,
segmentSubject: segSubject || undefined,
extraKeywords: ["\u65e0\u5173\u952e\u8bcd\u515c\u5e95"],
}),
);
}
return;
}
for (const kind of kinds) {
if (isCurrent) kindsInCurrentBody.add(kind);
push(
makeUnit({
uiKind: kind,
source: "body_segment",
segmentIndex: i,
isCurrent,
text: seg,
segmentSubject: segSubject || undefined,
}),
);
}
});
`;
// Replace from section 2 through just before section 3
src = src.slice(0, idx) + section2 + src.slice(attIdx);
// Re-find markers after splice
const attIdx2 = src.indexOf(attMarker);
const pushAttRe =
/push\(\s*makeUnit\(\{\s*uiKind: kind,\s*source: "attachment",\s*segmentIndex: -2,\s*isCurrent: true,\s*text: f,\s*extraKeywords: \[f\.slice\(0, 40\)\],\s*\}\),\s*\);/;
if (!pushAttRe.test(src.slice(attIdx2, attIdx2 + 800))) {
throw new Error("attachment push block not found");
}
src = src.replace(
pushAttRe,
`const attachCurrent =
kindsInCurrentBody.size === 0 || !kindsInCurrentBody.has(kind);
push(
makeUnit({
uiKind: kind,
source: "attachment",
segmentIndex: -2,
isCurrent: Boolean(attachCurrent),
text: f,
extraKeywords: [f.slice(0, 40)],
}),
);`,
);
const sortIdx2 = src.indexOf(sortMarker);
if (sortIdx2 < 0) throw new Error("sort marker missing after edit");
const fallback = ` // \u6574\u5c01\u4ecd\u65e0\u5355\u5143 \u2192 \u5de5\u5355\u515c\u5e95
if (!units.length && (subject.trim() || body.trim())) {
push(
makeUnit({
uiKind: "work_order",
source: "record",
segmentIndex: -3,
isCurrent: true,
text: body.trim() || subject,
segmentSubject: subject || undefined,
extraKeywords: ["\u65e0\u5173\u952e\u8bcd\u515c\u5e95"],
}),
);
}
`;
src = src.slice(0, sortIdx2) + fallback + src.slice(sortIdx2);
fs.writeFileSync(path, src);
console.log("patched", path);