feat: add internal AI chat assistant and AI Input Agent page

- Add Gemini-powered internal staff chatbot (free-text input in the
  upper-right bot panel): type "check MARIA GONZALES" to search patient
  and pre-fill eligibility, or "open claims" to navigate directly
- Add /api/ai/internal-chat endpoint with LangGraph + Google Gemini
  classifier (intent: check_eligibility, find_patient, navigate_*)
- Add Users AI Chat settings section in Settings > Advanced > AI Chat
  to configure a custom system prompt for the internal assistant
- Store internal chat system prompt in existing twilioSettings JSON
  blob (no DB migration needed)
- Add AI Input Agent sidebar entry and placeholder page describing
  planned keyboard-automation typing into Open Dental / Eaglesoft

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
ff
2026-06-02 00:44:05 -04:00
parent 3ac185b0ec
commit be90966f6e
8 changed files with 707 additions and 101 deletions

View File

@@ -0,0 +1,69 @@
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
export type InternalChatIntent =
| "check_eligibility"
| "find_patient"
| "navigate_claims"
| "navigate_schedule"
| "general";
export interface ChatClassification {
intent: InternalChatIntent;
patientName?: string;
fallbackReply: string;
}
const BASE_SYSTEM_PROMPT = `You are an internal assistant for a dental office management system.
Staff members type natural language commands and you classify what they want.
Respond ONLY with valid JSON (no markdown, no code fences) in this exact format:
{
"intent": "<one of the intents below>",
"patientName": "<full name if patient is mentioned, otherwise omit>",
"fallbackReply": "<a short, helpful reply to show the user>"
}
Intents:
- check_eligibility: user wants to check insurance eligibility for a patient (e.g. "check MARIA", "verify insurance for John Smith", "eligibility GONZALES")
- find_patient: user wants to look up a patient record only (e.g. "find patient John", "look up Smith", "show me GONZALES record")
- navigate_claims: user wants to open the claims page
- navigate_schedule: user wants to open the appointments/schedule page
- general: anything else — answer helpfully based on dental office context
Rules:
- Extract the full patient name as-is from the message for check_eligibility and find_patient
- Keep fallbackReply to 1-2 sentences max
- For navigate intents, fallbackReply should say "Opening the [page] page..."`;
export async function classifyInternalChat(
message: string,
apiKey: string,
extraSystemPrompt?: string
): Promise<ChatClassification> {
const fallback: ChatClassification = {
intent: "general",
fallbackReply: "I can help you search for a patient, check eligibility, or navigate to claims or appointments.",
};
if (!apiKey) return fallback;
const systemPrompt = extraSystemPrompt
? `${BASE_SYSTEM_PROMPT}\n\nAdditional office context:\n${extraSystemPrompt}`
: BASE_SYSTEM_PROMPT;
try {
const llm = new ChatGoogleGenerativeAI({ model: "gemini-1.5-flash", apiKey });
const response = await llm.invoke([
{ role: "system", content: systemPrompt },
{ role: "user", content: message },
]);
const raw = String(response.content).trim();
const jsonStr = raw.replace(/^```json\s*/i, "").replace(/```\s*$/, "").trim();
const parsed = JSON.parse(jsonStr) as ChatClassification;
if (!parsed.intent || !parsed.fallbackReply) return fallback;
return parsed;
} catch {
return fallback;
}
}

View File

@@ -1,5 +1,6 @@
import express, { Request, Response } from "express";
import { storage } from "../storage";
import { classifyInternalChat } from "../ai/internal-chat-graph";
const router = express.Router();
@@ -97,4 +98,152 @@ router.put("/chat-templates", async (req: Request, res: Response): Promise<any>
}
});
// GET /api/ai/internal-chat-settings
router.get("/internal-chat-settings", async (req: Request, res: Response): Promise<any> => {
try {
const userId = req.user?.id;
if (!userId) return res.status(401).json({ message: "Unauthorized" });
const systemPrompt = await storage.getInternalChatSystemPrompt(userId);
return res.status(200).json({ systemPrompt });
} catch (err) {
return res.status(500).json({ error: "Failed to fetch internal chat settings", details: String(err) });
}
});
// PUT /api/ai/internal-chat-settings
router.put("/internal-chat-settings", async (req: Request, res: Response): Promise<any> => {
try {
const userId = req.user?.id;
if (!userId) return res.status(401).json({ message: "Unauthorized" });
const { systemPrompt } = req.body;
if (typeof systemPrompt !== "string") return res.status(400).json({ message: "systemPrompt must be a string" });
await storage.saveInternalChatSystemPrompt(userId, systemPrompt.trim());
return res.status(200).json({ systemPrompt: systemPrompt.trim() });
} catch (err) {
return res.status(500).json({ error: "Failed to save internal chat settings", details: String(err) });
}
});
// POST /api/ai/internal-chat
router.post("/internal-chat", async (req: Request, res: Response): Promise<any> => {
try {
const userId = req.user?.id;
if (!userId) return res.status(401).json({ message: "Unauthorized" });
const { message } = req.body;
if (!message?.trim()) return res.status(400).json({ message: "message is required" });
const aiSettings = await storage.getAiSettings(userId);
if (!aiSettings?.apiKey) {
return res.status(200).json({
reply: "AI is not configured. Please add a Google AI API key in Settings.",
});
}
const extraSystemPrompt = await storage.getInternalChatSystemPrompt(userId);
const classification = await classifyInternalChat(message.trim(), aiSettings.apiKey, extraSystemPrompt || undefined);
// 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" } });
}
// 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 });
} catch (err) {
return res.status(500).json({ error: "Internal chat error", details: String(err) });
}
});
export default router;

View File

@@ -111,6 +111,23 @@ export const twilioStorage = {
});
},
async getInternalChatSystemPrompt(userId: number): Promise<string> {
const settings = await db.twilioSettings.findUnique({ where: { userId } });
const all = (settings?.templates as Record<string, string>) || {};
return all["_internal_chat_system_prompt"] ?? "";
},
async saveInternalChatSystemPrompt(userId: number, prompt: string): Promise<void> {
const settings = await db.twilioSettings.findUnique({ where: { userId } });
const existing = (settings?.templates as Record<string, string>) || {};
const updated = { ...existing, "_internal_chat_system_prompt": prompt };
await db.twilioSettings.upsert({
where: { userId },
update: { templates: updated },
create: { userId, accountSid: "", authToken: "", phoneNumber: "", templates: updated },
});
},
async getRecentCommunicationsByUser(userId: number, limit = 20) {
return db.communication.findMany({
where: { patient: { userId } },