fix: schedule AI claim keeps per-tooth procedure groups on one patient

Notes like "#6 post, crown, #7 rct, post, crown" now give each procedure the
tooth number it follows, and claim groups the AI splits per tooth for the same
patient are merged back into a single claim.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 12:51:59 -05:00
co-authored by Claude Opus 5.5
parent b951524fcc
commit 3410a2a0c4
@@ -275,6 +275,44 @@ export function restoreCodeToothLists(message: string, names: string[]): string[
return kept;
}
// "#6 post, crown, #7 rct, post, crown" — every procedure after a #tooth belongs to
// that tooth until the next #tooth. The LLM can leave the later items bare or put
// them on the wrong tooth, so rebuild those entries from the message itself.
export function restoreToothGroups(message: string, names: string[]): string[] {
// Several patients ("... for A and ... for B") are split per patient in claimGroups
if ((message.match(/\bfor\b/gi) ?? []).length > 1) return names;
const groups: { tooth: string; procs: string[] }[] = [];
const matches = [...message.matchAll(/#\s*(\d{1,2})\b/g)];
for (let i = 0; i < matches.length; i++) {
const m = matches[i]!;
const end = i + 1 < matches.length ? matches[i + 1]!.index! : message.length;
const raw = message.slice(m.index! + m[0].length, end);
const parts = raw.split(/[,;\n]/).map((p) => p.trim());
// Drop the trailing empty part left by "crown, #7"; cut off "... for <patient> today"
if (parts.length > 1 && parts[parts.length - 1] === "") parts.pop();
const procs = parts.map((p) => p.replace(/\s+(for|today|this|on)\b.*$/i, "").trim().toLowerCase());
// Only plain procedure words; anything else (tooth lists, surfaces, PA counts, codes) is left to the LLM
if (procs.length === 0 || procs.some((p) => !/^[a-z][a-z\/\- ]*$/.test(p) || /\band\b/.test(p))) return names;
if (/^[mdlfbovi]{1,5}$/.test(procs[0]!)) return names; // composite surfaces, e.g. "#8 MO"
groups.push({ tooth: m[1]!, procs });
}
if (groups.length < 2 && !groups.some((g) => g.procs.length > 1)) return names;
const teeth = new Set(groups.map((g) => g.tooth));
const procs = new Set(groups.flatMap((g) => g.procs));
const isCovered = (n: string) => {
const s = n.trim().toLowerCase();
const t = s.match(/#\s*(\d{1,2})\b/);
if (t) return teeth.has(t[1]!);
return procs.has(s);
};
const expanded = groups.flatMap((g) => g.procs.map((p) => `#${g.tooth} ${p}`));
const first = names.findIndex(isCovered);
const kept = names.filter((n) => !isCovered(n));
kept.splice(first === -1 ? kept.length : first, 0, ...expanded);
return kept;
}
// Field labels copied along with the values, e.g. "Date of Birth 05/03/1952\nMember ID S01057721"
// or "Subscriber #: 700002459574\nPatient date of birth: Jul 31, 1982 Age: 44".
const ID_DOB_LABEL_RE =
@@ -385,8 +423,27 @@ export async function classifyInternalChat(
const jsonStr = raw.replace(/^```json\s*/i, "").replace(/```\s*$/, "").trim();
const parsed = JSON.parse(jsonStr) as ChatClassification;
if (!parsed.intent || !parsed.fallbackReply) return fallback;
// "#6 post, crown, #7 rct, post, crown for A" can come back as one claim group per
// tooth for the same patient — merge groups that name the same patient.
if (parsed.claimGroups && parsed.claimGroups.length > 1) {
const merged = new Map<string, { patientName: string; procedureNames: string[] }>();
for (const g of parsed.claimGroups) {
const key = (g.patientName ?? "").trim().replace(/\s+/g, " ").toLowerCase();
const existing = merged.get(key);
if (existing) existing.procedureNames.push(...(g.procedureNames ?? []));
else merged.set(key, { patientName: g.patientName, procedureNames: [...(g.procedureNames ?? [])] });
}
parsed.claimGroups = [...merged.values()];
if (parsed.claimGroups.length === 1 && parsed.intent === "multi_claim") {
parsed.intent = "claim_only";
parsed.patientName = parsed.claimGroups[0]!.patientName;
parsed.procedureNames = parsed.claimGroups[0]!.procedureNames;
parsed.claimGroups = undefined;
}
}
if (parsed.procedureNames) parsed.procedureNames = restoreBarePaCount(message, parsed.procedureNames);
if (parsed.procedureNames) parsed.procedureNames = restoreCodeToothLists(message, parsed.procedureNames);
if (parsed.procedureNames) parsed.procedureNames = restoreToothGroups(message, parsed.procedureNames);
return parsed;
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);