--- report.before.ts +++ report.after.ts @@ -1,10 +1,27 @@ export function createDraft(note: string): DraftReport { + const clean = note.trim() + const lower = clean.toLowerCase() + const unsupported = !/(?:boiler|radiator|filter|pump|valve|pressure.test)/i.test(clean) + const unclear = !clean || unsupported || /\[unclear\]|inaudible|cannot hear/.test(lower) + if (unclear) { + return { work: "", parts: [], safety: "No safety result recorded.", followUp: "Please record a clearer note or edit the transcript.", homeowner: "", needsClarification: true } + } + const sentences = clean.split(/(?<=[.!?])\s+/).map((line) => line.trim()).filter(Boolean) + const followUp = sentences.filter((line) => /return visit|arrange|follow.?up/i.test(line)).join(" ") + const work = sentences.filter((line) => !/safety|pressure.test|return visit|arrange|follow.?up/i.test(line)).join(" ") + const clauses = clean.split(/[.!?;]|\b(?:and|but)\b/i) + const negated = (clause: string) => /\b(?:not|never|no|without|didn['’]t|hadn['’]t|wasn['’]t)\b/i.test(clause) + const parts = PARTS.filter((part) => clauses.some((clause) => !negated(clause) && new RegExp(`(?:replaced|fitted|used)\\s+(?:(?:a|the)\\s+)?${part}\\b`, "i").test(clause))) + const testSentences = sentences.filter((line) => /pressure.test/i.test(line) && !/return visit|arrange|please|needs? to|should|will|could|would|might/i.test(line)) + const negativeTest = testSentences.some(negated) + const testRecord = testSentences.filter((line) => !negated(line)).join(" ") + const safety = negativeTest ? "Pressure test not carried out." : testRecord ? `Recorded test note: ${testRecord} No additional safety result inferred.` : "No safety result recorded." return { - work: note, - parts: note.toLowerCase().includes("valve") ? ["radiator valve"] : [], - safety: /pressure.test/i.test(note) ? "Pressure tested." : "Safety checks complete.", - followUp: "", - homeowner: note, + work: work || "Review the original note before completing this report.", + parts: [...parts], + safety, + followUp, + homeowner: [work, followUp].filter(Boolean).join(" "), needsClarification: false, } }