feat: AI-powered eligibility detection from Copy Agent screenshots

Adds an "AI Detect & Check Eligibility" action to the Copy Agent page that
sends captured screenshots to Claude to extract Member ID/DOB, then opens
the chatbot's eligibility confirm step pre-filled with the result.

- POST /api/ai/detect-eligibility-info: Claude vision extraction of
  Member ID/DOB from uploaded screenshots.
- POST /api/patients/create-temp: auto-creates a placeholder "PID-<id>"
  patient (matched by insuranceId+dob) when no patient is selected, so
  screenshots can be saved immediately; the real eligibility check later
  rewrites the patient's name and Cloud Storage folder automatically via
  the existing createOrUpdatePatientByInsuranceId matching logic.
- Chatbot: Member ID/DOB on the confirm card are now editable (MM/DD/YYYY),
  with edits synced back to the patient record before the check runs.
- Fixed a useEffect ordering bug on the insurance-status page that could
  silently clear a chatbot-prefilled Member ID/DOB before the auto-trigger
  effect ran, preventing the eligibility check from ever starting.
- Added a local-disk-only screenshot backup (apps/Backend/uploads/
  screenshot-backups/) for debugging, not exposed anywhere in the UI.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-16 21:20:33 -04:00
co-authored by Claude Sonnet 5
parent 34312b62bc
commit c95d08c951
9 changed files with 748 additions and 57 deletions
+123 -3
View File
@@ -14,6 +14,7 @@ import {
Image as ImageIcon,
} from "lucide-react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { useLocation } from "wouter";
import { cn } from "@/lib/utils";
@@ -77,6 +78,20 @@ function makeMsg(role: "bot" | "user", text: string, isLoading = false): Message
return { id: ++msgCounter, role, text, isLoading };
}
// eligibilityIdData.dob is stored/sent as ISO ("YYYY-MM-DD") everywhere downstream (patient
// sync, sessionStorage prefill, matching) — these only convert for display/editing.
function isoToMDY(iso: string): string {
const m = iso.match(/^(\d{4})-(\d{2})-(\d{2})$/);
if (!m) return iso;
return `${m[2]}/${m[3]}/${m[1]}`;
}
function mdyToISO(mdy: string): string {
const m = mdy.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/);
if (!m) return mdy;
return `${m[3]}-${m[1]!.padStart(2, "0")}-${m[2]!.padStart(2, "0")}`;
}
function getAutoCheck(dobISO: string): "mh" | "cmsp" {
const [y, m, d] = dobISO.split("-").map(Number);
const today = new Date();
@@ -212,6 +227,9 @@ export function ChatbotButton() {
const pasteRef = useRef<HTMLTextAreaElement>(null);
const freeTextRef = useRef<HTMLTextAreaElement>(null);
const fileInputRef = useRef<HTMLInputElement>(null);
// Set by the Copy Agent page's "AI Detect & Check Eligibility" flow when the screenshots were
// saved under a placeholder folder name; renamed to the real patient name once the user confirms.
const pendingFolderRenameRef = useRef<{ patientId: number; patientName: string } | null>(null);
// Load chat history from server on first mount (server is source of truth)
const serverLoaded = useRef(false);
@@ -237,6 +255,59 @@ export function ChatbotButton() {
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
}, [messages, step]);
// Opened externally by the Copy Agent page after it saves screenshots and runs AI
// Member ID/DOB extraction — lands the chatbot directly on the confirm step.
useEffect(() => {
function handleOpenEligibilityIdReady(e: Event) {
const detail = (e as CustomEvent).detail as {
memberId: string | null;
dob: string | null;
autoCheck: string;
patient: PatientResult | null;
patientId?: number;
pendingFolderPatientName?: string;
};
if (detail.patientId && detail.pendingFolderPatientName) {
pendingFolderRenameRef.current = {
patientId: detail.patientId,
patientName: detail.pendingFolderPatientName,
};
}
if (shouldAutoReset()) resetStep();
setOpen(true);
if (detail.memberId && detail.dob) {
setEligibilityIdData({
memberId: detail.memberId,
dob: detail.dob,
siteKey: "",
autoCheck: detail.autoCheck || "mh",
patient: detail.patient,
appointmentDate: null,
});
addMsg(
"bot",
`I read this from your screenshots — ready to check ${detail.autoCheck === "mh" ? "MassHealth" : "eligibility"} for ${
detail.patient ? `${detail.patient.firstName} ${detail.patient.lastName}` : detail.memberId
}. Confirm to proceed.`
);
setStep("eligibility-id-ready");
} else {
setPasteInput([detail.memberId, detail.dob].filter(Boolean).join(" "));
addMsg(
"bot",
"I saved your screenshots but couldn't fully read the Member ID/DOB. Please review and complete the fields below:"
);
setStep("eligibility-input");
}
}
window.addEventListener("chatbot:open-eligibility-id-ready", handleOpenEligibilityIdReady);
return () => window.removeEventListener("chatbot:open-eligibility-id-ready", handleOpenEligibilityIdReady);
}, []);
// Persist messages to sessionStorage AND server
useEffect(() => {
try {
@@ -284,6 +355,7 @@ export function ChatbotButton() {
setBatchClaimData(null);
setBatchCheckAndClaimData(null);
setPreauthReadyData(null);
pendingFolderRenameRef.current = null;
};
// Full reset including message history and stored session
@@ -377,13 +449,39 @@ export function ChatbotButton() {
sessionStorage.setItem("chatbot_eligibility", JSON.stringify({ memberId, dob: dobISO, autoCheck }));
window.dispatchEvent(new CustomEvent("chatbot:eligibility-prefill"));
markJobStarted();
if (pendingFolderRenameRef.current) {
const { patientId, patientName } = pendingFolderRenameRef.current;
pendingFolderRenameRef.current = null;
apiRequest("PATCH", `/api/claims/patient-folder/${patientId}/confirm-name`, { patientName }).catch(() => {});
}
setTimeout(() => { setLocation("/insurance-status"); setOpen(false); resetStep(); }, 600);
};
const handleEligibilityIdRun = () => {
// If the user hand-edited the Member ID/DOB on the "eligibility-id-ready" card (e.g. fixing a
// misread digit), push the correction to the patient record too — otherwise the real eligibility
// check (which matches by insuranceId+dob) would create a second, disconnected patient instead
// of updating this one. Surface a failure in the chat rather than swallowing it, since a silent
// failure here means the check proceeds against a stale record and silently creates a duplicate.
const syncEditedPatientFields = async (memberId: string, dob: string, patientId?: number): Promise<boolean> => {
if (!patientId) return true;
try {
await apiRequest("PUT", `/api/patients/${patientId}`, { insuranceId: memberId, dateOfBirth: dob });
return true;
} catch (err: any) {
console.error("Failed to sync edited Member ID/DOB to patient record:", err);
addMsg(
"bot",
`⚠️ Couldn't save the edited Member ID/DOB to the patient record (${err?.message ?? "unknown error"}). The eligibility check may create a duplicate patient — check the Patients page after.`
);
return false;
}
};
const handleEligibilityIdRun = async () => {
if (!eligibilityIdData) return;
addMsg("user", "Check eligibility now");
addMsg("bot", "Opening the eligibility check page...");
await syncEditedPatientFields(eligibilityIdData.memberId, eligibilityIdData.dob, eligibilityIdData.patient?.id);
prefillAndNavigate(eligibilityIdData.memberId, eligibilityIdData.dob, eligibilityIdData.autoCheck);
};
@@ -416,6 +514,7 @@ export function ChatbotButton() {
? new Date(targetDate + "T00:00:00").toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric" })
: "today";
addMsg("user", `Check eligibility & add to schedule (${dateLabel})`);
await syncEditedPatientFields(eligibilityIdData.memberId, eligibilityIdData.dob, eligibilityIdData.patient?.id);
if (!eligibilityIdData.patient) {
addMsg("bot", `Running eligibility check — will add patient and create appointment for ${dateLabel} after...`);
@@ -859,8 +958,29 @@ export function ChatbotButton() {
{eligibilityIdData.patient.firstName} {eligibilityIdData.patient.lastName}
</p>
)}
<p className="text-xs text-blue-600">ID: {eligibilityIdData.memberId}</p>
<p className="text-xs text-gray-500">DOB: {eligibilityIdData.dob}</p>
<div className="space-y-1">
<Label className="text-[10px] text-gray-500">Member ID</Label>
<Input
value={eligibilityIdData.memberId}
onChange={(e) =>
setEligibilityIdData((prev) => (prev ? { ...prev, memberId: e.target.value } : prev))
}
className="h-7 text-xs bg-white"
/>
</div>
<div className="space-y-1">
<Label className="text-[10px] text-gray-500">Date of Birth (MM/DD/YYYY)</Label>
<Input
value={isoToMDY(eligibilityIdData.dob)}
onChange={(e) =>
setEligibilityIdData((prev) =>
prev ? { ...prev, dob: mdyToISO(e.target.value) } : prev
)
}
placeholder="MM/DD/YYYY"
className="h-7 text-xs bg-white"
/>
</div>
{eligibilityIdData.patient?.insuranceProvider && (
<p className="text-xs text-gray-500">{eligibilityIdData.patient.insuranceProvider}</p>
)}