feat: Users AI Chat multi-step workflows with CDT lookup and alias management
- Add eligibility_by_id and check_and_claim intents to internal chat - New cdt-lookup.ts: keyword search against fee schedule JSON (no LLM) - New internal-chat-workflow.ts: deterministic orchestration — patient resolution, insurance siteKey derivation, CDT code mapping - Custom CDT aliases stored per-user in DB (TwilioSettings JSON blob) with GET/PUT /api/ai/cdt-aliases endpoints - Chatbot UI: new steps for eligibility-id-ready, check-and-claim-ready, and need-insurance-clarification with insurance picker - Settings UI: CDT Aliases CRUD table with built-in alias reference Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import express, { Request, Response } from "express";
|
||||
import { storage } from "../storage";
|
||||
import { classifyInternalChat } from "../ai/internal-chat-graph";
|
||||
import { runInternalChatWorkflow } from "../ai/internal-chat-workflow";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -124,6 +125,40 @@ router.put("/internal-chat-settings", async (req: Request, res: Response): Promi
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/ai/cdt-aliases
|
||||
router.get("/cdt-aliases", async (req: Request, res: Response): Promise<any> => {
|
||||
try {
|
||||
const userId = req.user?.id;
|
||||
if (!userId) return res.status(401).json({ message: "Unauthorized" });
|
||||
const aliases = await storage.getCdtAliases(userId);
|
||||
return res.status(200).json(aliases);
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: "Failed to fetch CDT aliases", details: String(err) });
|
||||
}
|
||||
});
|
||||
|
||||
// PUT /api/ai/cdt-aliases
|
||||
router.put("/cdt-aliases", async (req: Request, res: Response): Promise<any> => {
|
||||
try {
|
||||
const userId = req.user?.id;
|
||||
if (!userId) return res.status(401).json({ message: "Unauthorized" });
|
||||
const aliases = req.body;
|
||||
if (!Array.isArray(aliases)) {
|
||||
return res.status(400).json({ message: "Body must be an array of { phrase, cdtCode }" });
|
||||
}
|
||||
const cleaned = aliases
|
||||
.filter((a: any) => typeof a?.phrase === "string" && typeof a?.cdtCode === "string")
|
||||
.map((a: any) => ({
|
||||
phrase: a.phrase.trim().toLowerCase(),
|
||||
cdtCode: a.cdtCode.trim().toUpperCase(),
|
||||
}));
|
||||
await storage.saveCdtAliases(userId, cleaned);
|
||||
return res.status(200).json(cleaned);
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: "Failed to save CDT aliases", details: String(err) });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/ai/internal-chat
|
||||
router.post("/internal-chat", async (req: Request, res: Response): Promise<any> => {
|
||||
try {
|
||||
@@ -140,107 +175,19 @@ router.post("/internal-chat", async (req: Request, res: Response): Promise<any>
|
||||
});
|
||||
}
|
||||
|
||||
const extraSystemPrompt = await storage.getInternalChatSystemPrompt(userId);
|
||||
const classification = await classifyInternalChat(message.trim(), aiSettings.apiKey, extraSystemPrompt || undefined);
|
||||
const [extraSystemPrompt, customAliases] = await Promise.all([
|
||||
storage.getInternalChatSystemPrompt(userId),
|
||||
storage.getCdtAliases(userId),
|
||||
]);
|
||||
|
||||
// Handle navigation intents immediately
|
||||
if (classification.intent === "navigate_claims") {
|
||||
return res.status(200).json({ reply: classification.fallbackReply, action: "navigate", actionData: { url: "/claims" } });
|
||||
}
|
||||
if (classification.intent === "navigate_schedule") {
|
||||
return res.status(200).json({ reply: classification.fallbackReply, action: "navigate", actionData: { url: "/appointments" } });
|
||||
}
|
||||
const classification = await classifyInternalChat(
|
||||
message.trim(),
|
||||
aiSettings.apiKey,
|
||||
extraSystemPrompt || undefined
|
||||
);
|
||||
|
||||
// Handle patient intents — search DB
|
||||
if (classification.intent === "check_eligibility" || classification.intent === "find_patient") {
|
||||
const name = classification.patientName?.trim();
|
||||
if (!name) {
|
||||
return res.status(200).json({ reply: "Please include the patient's name in your message." });
|
||||
}
|
||||
|
||||
const patients = await storage.searchPatients({
|
||||
filters: {
|
||||
OR: [
|
||||
{ firstName: { contains: name, mode: "insensitive" } },
|
||||
{ lastName: { contains: name, mode: "insensitive" } },
|
||||
{
|
||||
AND: name.split(/\s+/).map((part: string) => ({
|
||||
OR: [
|
||||
{ firstName: { contains: part, mode: "insensitive" } },
|
||||
{ lastName: { contains: part, mode: "insensitive" } },
|
||||
],
|
||||
})),
|
||||
},
|
||||
],
|
||||
},
|
||||
limit: 5,
|
||||
offset: 0,
|
||||
});
|
||||
|
||||
if (!patients || patients.length === 0) {
|
||||
return res.status(200).json({
|
||||
reply: `No patient found matching "${name}". Try a different spelling or search on the Patients page.`,
|
||||
});
|
||||
}
|
||||
|
||||
const patient = patients[0]!;
|
||||
const fullName = `${patient.firstName ?? ""} ${patient.lastName ?? ""}`.trim();
|
||||
|
||||
if (classification.intent === "find_patient") {
|
||||
const ins = patient.insuranceProvider ? ` · ${patient.insuranceProvider}` : "";
|
||||
const id = patient.insuranceId ? ` (ID: ${patient.insuranceId})` : "";
|
||||
return res.status(200).json({
|
||||
reply: `Found: ${fullName}${ins}${id}`,
|
||||
action: "show_patient",
|
||||
actionData: {
|
||||
patient: {
|
||||
id: patient.id,
|
||||
firstName: patient.firstName,
|
||||
lastName: patient.lastName,
|
||||
insuranceId: patient.insuranceId ?? null,
|
||||
insuranceProvider: patient.insuranceProvider ?? null,
|
||||
dateOfBirth: patient.dateOfBirth ? patient.dateOfBirth.toISOString().split("T")[0] : null,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// check_eligibility
|
||||
if (!patient.insuranceId) {
|
||||
return res.status(200).json({
|
||||
reply: `Found ${fullName} but no Member ID is on file. Please add their insurance info first.`,
|
||||
action: "show_patient",
|
||||
actionData: {
|
||||
patient: {
|
||||
id: patient.id,
|
||||
firstName: patient.firstName,
|
||||
lastName: patient.lastName,
|
||||
insuranceId: null,
|
||||
insuranceProvider: patient.insuranceProvider ?? null,
|
||||
dateOfBirth: patient.dateOfBirth ? patient.dateOfBirth.toISOString().split("T")[0] : null,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
reply: `Found ${fullName}. Ready to check eligibility.`,
|
||||
action: "check_eligibility_prefill",
|
||||
actionData: {
|
||||
patient: {
|
||||
id: patient.id,
|
||||
firstName: patient.firstName,
|
||||
lastName: patient.lastName,
|
||||
insuranceId: patient.insuranceId,
|
||||
insuranceProvider: patient.insuranceProvider ?? null,
|
||||
dateOfBirth: patient.dateOfBirth ? patient.dateOfBirth.toISOString().split("T")[0] : null,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// General intent — return Gemini's reply
|
||||
return res.status(200).json({ reply: classification.fallbackReply });
|
||||
const response = await runInternalChatWorkflow(classification, userId, storage, customAliases);
|
||||
return res.status(200).json(response);
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: "Internal chat error", details: String(err) });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user