feat: persist AI conversation state in DB and fix LangGraph flow bugs

- Replace in-memory Maps in aiHandoffStore with DB-backed async functions
  using new patient_conversation table (stage + aiHandoff per patient)
- Add afterHoursEnabled to ai_settings table (persists across restarts)
- Fix runtime crash in reschedule-graph: mon/tue/wed variables were out
  of scope in the next-week fallback branch (ReferenceError)
- Wire rescheduleGreeting and generalFallback chat templates through to
  LangGraph nodes so user-configured messages take effect
- Add otherNode to reminder-graph to handle unclassified patient replies
  (e.g. "I want another appointment") and route to booking flow
- Fetch chatTemplates once per webhook request instead of per stage

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Gitead
2026-05-09 15:23:55 -04:00
parent e9296c68f9
commit 112529155c
321 changed files with 5096 additions and 446 deletions

View File

@@ -208,7 +208,7 @@ async function runMassHealthCheckAndNotify(
// Persist and advance stage
await saveOutbound(patient.id, resultText);
setStage(patient.userId, patient.id, nextStage);
await setStage(patient.userId, patient.id, nextStage);
} catch {
// Silent — don't crash the main request
@@ -241,7 +241,7 @@ router.post("/webhook/sms", async (req: Request, res: Response): Promise<any> =>
});
// Per-patient handoff toggle must be ON
if (!getHandoff(patient.userId, patient.id)) {
if (!await getHandoff(patient.userId, patient.id)) {
res.set("Content-Type", "text/xml");
return res.send(empty());
}
@@ -252,23 +252,22 @@ router.post("/webhook/sms", async (req: Request, res: Response): Promise<any> =>
return res.send(empty());
}
const language = patient.preferredLanguage || "English";
const stage = getStage(patient.userId, patient.id);
const language = patient.preferredLanguage || "English";
const stage = await getStage(patient.userId, patient.id);
const chatTemplates = await storage.getAiChatTemplates(patient.userId);
const officeContact = await storage.getOfficeContact(patient.userId);
const officeName = (officeContact as any)?.officeName?.trim() || "";
// ── Helper: send reply + set stage ─────────────────────────────────────
const reply = async (text: string, nextStage: ConversationStage) => {
await saveOutbound(patient.id, text);
setStage(patient.userId, patient.id, nextStage);
await setStage(patient.userId, patient.id, nextStage);
res.set("Content-Type", "text/xml");
return res.send(twimlReply(text));
};
// ── Stage: reminder_initial → send reminder greeting ─────────────────
if (stage === "reminder_initial") {
const chatTemplates = await storage.getAiChatTemplates(patient.userId);
const officeContact = await storage.getOfficeContact(patient.userId);
const officeName = (officeContact as any)?.officeName?.trim() || "";
const rawGreeting = chatTemplates.reminderGreeting ||
`Hi! My name is Lisa, the dedicated AI assistant at {officeName}. I can confirm or reschedule your appointment and answer general questions 24/7. I will reply your message at any time you need.`;
@@ -278,10 +277,15 @@ router.post("/webhook/sms", async (req: Request, res: Response): Promise<any> =>
// ── Stage: greeted → classify yes/no for appointment reminder ────────
if (stage === "greeted") {
const apptDatetime = await getAppointmentDatetime(patient.id);
const { reply: aiReply, intent } = await runReminderGraph(Body, aiSettings.apiKey, language, apptDatetime);
const { reply: aiReply, intent } = await runReminderGraph(
Body, aiSettings.apiKey, language, apptDatetime,
chatTemplates.rescheduleGreeting, chatTemplates.generalFallback
);
if (aiReply) {
// YES → done; NO → start rescheduling flow
const nextStage: ConversationStage = intent === "no" ? "asked_reschedule_confirm" : "done";
let nextStage: ConversationStage;
if (intent === "no") nextStage = "asked_reschedule_confirm";
else if (intent === "wants_appointment") nextStage = "asked_new_or_existing";
else nextStage = "done";
return reply(aiReply, nextStage);
}
}
@@ -332,7 +336,7 @@ router.post("/webhook/sms", async (req: Request, res: Response): Promise<any> =>
// Reply now — Selenium runs in the background
await saveOutbound(patient.id, checkingMsg);
setStage(patient.userId, patient.id, "done");
await setStage(patient.userId, patient.id, "done");
res.set("Content-Type", "text/xml");
res.send(twimlReply(checkingMsg));
@@ -380,7 +384,7 @@ router.post("/webhook/sms", async (req: Request, res: Response): Promise<any> =>
const checkingMsg = checkingMessages[language] ?? checkingMessages["English"]!;
await saveOutbound(patient.id, checkingMsg);
setStage(patient.userId, patient.id, "done");
await setStage(patient.userId, patient.id, "done");
res.set("Content-Type", "text/xml");
res.send(twimlReply(checkingMsg));
@@ -403,7 +407,7 @@ router.post("/webhook/sms", async (req: Request, res: Response): Promise<any> =>
];
if (newPatientStages.includes(stage)) {
const { reply: aiReply, nextStage } = await runNewPatientStep(
Body, stage, language, aiSettings.apiKey
Body, stage, language, aiSettings.apiKey, chatTemplates.generalFallback
);
return reply(aiReply, nextStage);
}
@@ -411,14 +415,10 @@ router.post("/webhook/sms", async (req: Request, res: Response): Promise<any> =>
// ── Stage: initial (no active conversation) ───────────────────────────
// Check after-hours: if enabled and currently outside office hours → start new-patient flow
if (stage === "initial" || stage === "done") {
const afterHoursEnabled = getAfterHoursHandoff(patient.userId);
const afterHoursEnabled = await getAfterHoursHandoff(patient.userId);
const outsideHours = await isAfterHours(patient.userId);
if (afterHoursEnabled && outsideHours) {
const chatTemplates = await storage.getAiChatTemplates(patient.userId);
const officeContact = await storage.getOfficeContact(patient.userId);
const officeName = (officeContact as any)?.officeName?.trim() || "";
const rawGreeting = chatTemplates.newPatientGreeting ||
`Hi! My name is Lisa, the dedicated AI assistant at {officeName}. I can help you schedule an appointment, check your insurance, and answer general questions 24/7. How can I help you today?`;

View File

@@ -94,11 +94,11 @@ router.post("/send-sms", async (req: Request, res: Response): Promise<any> => {
});
// Set conversation stage based on which flow was started
if (startFlow === "new_patient") {
startNewPatientConversation(userId, pid);
await startNewPatientConversation(userId, pid);
} else if (startFlow === "reschedule") {
startRescheduleConversation(userId, pid);
await startRescheduleConversation(userId, pid);
} else {
resetConversation(userId, pid);
await resetConversation(userId, pid);
}
}
@@ -140,7 +140,7 @@ router.get("/after-hours-handoff", async (req: Request, res: Response): Promise<
try {
const userId = req.user?.id;
if (!userId) return res.status(401).json({ message: "Unauthorized" });
return res.status(200).json({ enabled: getAfterHoursHandoff(userId) });
return res.status(200).json({ enabled: await getAfterHoursHandoff(userId) });
} catch (err) {
return res.status(500).json({ error: "Failed to get after-hours handoff state" });
}
@@ -153,7 +153,7 @@ router.put("/after-hours-handoff", async (req: Request, res: Response): Promise<
if (!userId) return res.status(401).json({ message: "Unauthorized" });
const { enabled } = req.body;
if (typeof enabled !== "boolean") return res.status(400).json({ message: "enabled must be a boolean" });
setAfterHoursHandoff(userId, enabled);
await setAfterHoursHandoff(userId, enabled);
return res.status(200).json({ enabled });
} catch (err) {
return res.status(500).json({ error: "Failed to set after-hours handoff state" });
@@ -167,7 +167,7 @@ router.get("/ai-handoff/:patientId", async (req: Request, res: Response): Promis
if (!userId) return res.status(401).json({ message: "Unauthorized" });
const patientId = parseInt(req.params.patientId);
if (isNaN(patientId)) return res.status(400).json({ message: "Invalid patientId" });
return res.status(200).json({ enabled: getHandoff(userId, patientId) });
return res.status(200).json({ enabled: await getHandoff(userId, patientId) });
} catch (err) {
return res.status(500).json({ error: "Failed to get AI handoff state" });
}
@@ -182,7 +182,7 @@ router.put("/ai-handoff/:patientId", async (req: Request, res: Response): Promis
if (isNaN(patientId)) return res.status(400).json({ message: "Invalid patientId" });
const { enabled } = req.body;
if (typeof enabled !== "boolean") return res.status(400).json({ message: "enabled must be a boolean" });
setHandoff(userId, patientId, enabled);
await setHandoff(userId, patientId, enabled);
return res.status(200).json({ enabled });
} catch (err) {
return res.status(500).json({ error: "Failed to set AI handoff state" });