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

@@ -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" });