diff --git a/apps/Backend/src/ai/reminder-graph.ts b/apps/Backend/src/ai/reminder-graph.ts new file mode 100644 index 00000000..a4ce4951 --- /dev/null +++ b/apps/Backend/src/ai/reminder-graph.ts @@ -0,0 +1,100 @@ +import { StateGraph, END, START, Annotation } from "@langchain/langgraph"; +import { ChatGoogleGenerativeAI } from "@langchain/google-genai"; + +const GraphState = Annotation.Root({ + message: Annotation(), + intent: Annotation(), + reply: Annotation(), +}); + +type GraphStateType = typeof GraphState.State; + +// Keyword-based intent classifier — fast and deterministic for yes/no +function classifyNode(state: GraphStateType) { + const text = state.message.toLowerCase().trim(); + + const yesPatterns = /\b(yes|yeah|yep|yup|sure|ok|okay|confirmed|confirm|will be there|sounds good|see you|great|perfect|absolutely|definitely)\b/; + const noPatterns = /\b(no|nope|can't|cannot|won't|not available|unavailable|cancel|reschedule|busy|sorry|unable|not coming|not going)\b/; + + if (yesPatterns.test(text)) return { intent: "yes" }; + if (noPatterns.test(text)) return { intent: "no" }; + return { intent: "other" }; +} + +function routeByIntent(state: GraphStateType): string { + if (state.intent === "yes") return "thank_you"; + if (state.intent === "no") return "reschedule"; + return END; +} + +async function thankYouNode(state: GraphStateType, config: any) { + const apiKey: string | undefined = config?.configurable?.apiKey; + const fallback = "Thank you for confirming your appointment! We look forward to seeing you."; + + if (!apiKey) return { reply: fallback }; + + try { + const llm = new ChatGoogleGenerativeAI({ model: "gemini-1.5-flash", apiKey }); + const response = await llm.invoke([ + { + role: "system", + content: + "You are a friendly dental office assistant. Write a short, warm SMS reply (1-2 sentences max) thanking a patient who just confirmed their appointment. Do not add any formatting or extra text.", + }, + { role: "user", content: `Patient replied: "${state.message}"` }, + ]); + return { reply: String(response.content) || fallback }; + } catch { + return { reply: fallback }; + } +} + +async function rescheduleNode(state: GraphStateType, config: any) { + const apiKey: string | undefined = config?.configurable?.apiKey; + const fallback = "We understand! Our assistant will contact you shortly to help reschedule."; + + if (!apiKey) return { reply: fallback }; + + try { + const llm = new ChatGoogleGenerativeAI({ model: "gemini-1.5-flash", apiKey }); + const response = await llm.invoke([ + { + role: "system", + content: + "You are a friendly dental office assistant. Write a short, empathetic SMS reply (1-2 sentences max) to a patient who can't make their appointment. Tell them an assistant will contact them soon to reschedule. Do not add any formatting or extra text.", + }, + { role: "user", content: `Patient replied: "${state.message}"` }, + ]); + return { reply: String(response.content) || fallback }; + } catch { + return { reply: fallback }; + } +} + +const graph = new StateGraph(GraphState) + .addNode("classify", classifyNode) + .addNode("thank_you", thankYouNode) + .addNode("reschedule", rescheduleNode) + .addEdge(START, "classify") + .addConditionalEdges("classify", routeByIntent, { + thank_you: "thank_you", + reschedule: "reschedule", + [END]: END, + }) + .addEdge("thank_you", END) + .addEdge("reschedule", END) + .compile(); + +export async function runReminderGraph( + patientMessage: string, + apiKey: string +): Promise<{ reply: string | null; intent: string | null }> { + const result = await graph.invoke( + { message: patientMessage, intent: "", reply: "" }, + { configurable: { apiKey } } + ); + return { + reply: result.reply || null, + intent: result.intent || null, + }; +} diff --git a/apps/Backend/src/routes/ai-settings.ts b/apps/Backend/src/routes/ai-settings.ts new file mode 100644 index 00000000..9e1a4c2b --- /dev/null +++ b/apps/Backend/src/routes/ai-settings.ts @@ -0,0 +1,39 @@ +import express, { Request, Response } from "express"; +import { storage } from "../storage"; + +const router = express.Router(); + +// GET /api/ai/settings +router.get("/settings", async (req: Request, res: Response): Promise => { + try { + const userId = req.user?.id; + if (!userId) return res.status(401).json({ message: "Unauthorized" }); + + const settings = await storage.getAiSettings(userId); + if (!settings) return res.status(200).json(null); + + return res.status(200).json({ id: settings.id, apiKey: settings.apiKey }); + } catch (err) { + return res.status(500).json({ error: "Failed to fetch AI settings", details: String(err) }); + } +}); + +// PUT /api/ai/settings +router.put("/settings", async (req: Request, res: Response): Promise => { + try { + const userId = req.user?.id; + if (!userId) return res.status(401).json({ message: "Unauthorized" }); + + const { apiKey } = req.body; + if (!apiKey?.trim()) { + return res.status(400).json({ message: "apiKey is required" }); + } + + const settings = await storage.upsertAiSettings(userId, apiKey.trim()); + return res.status(200).json({ id: settings.id, apiKey: settings.apiKey }); + } catch (err) { + return res.status(500).json({ error: "Failed to save AI settings", details: String(err) }); + } +}); + +export default router; diff --git a/apps/Backend/src/storage/ai-settings-storage.ts b/apps/Backend/src/storage/ai-settings-storage.ts new file mode 100644 index 00000000..e7a6d134 --- /dev/null +++ b/apps/Backend/src/storage/ai-settings-storage.ts @@ -0,0 +1,15 @@ +import { prisma as db } from "@repo/db/client"; + +export const aiSettingsStorage = { + async getAiSettings(userId: number) { + return db.aiSettings.findUnique({ where: { userId } }); + }, + + async upsertAiSettings(userId: number, apiKey: string) { + return db.aiSettings.upsert({ + where: { userId }, + update: { apiKey }, + create: { userId, apiKey }, + }); + }, +}; diff --git a/apps/Frontend/src/components/settings/ai-settings-card.tsx b/apps/Frontend/src/components/settings/ai-settings-card.tsx new file mode 100644 index 00000000..d01f3c09 --- /dev/null +++ b/apps/Frontend/src/components/settings/ai-settings-card.tsx @@ -0,0 +1,131 @@ +import { useState, useEffect } from "react"; +import { useQuery, useMutation } from "@tanstack/react-query"; +import { Card, CardContent } from "@/components/ui/card"; +import { Eye, EyeOff, CheckCircle } from "lucide-react"; +import { useToast } from "@/hooks/use-toast"; +import { apiRequest, queryClient } from "@/lib/queryClient"; + +type AiSettings = { + id?: number; + apiKey: string; +}; + +export function AiSettingsCard() { + const { toast } = useToast(); + const [apiKey, setApiKey] = useState(""); + const [showKey, setShowKey] = useState(false); + + const { data: settings, isLoading } = useQuery({ + queryKey: ["/api/ai/settings"], + queryFn: async () => { + const res = await apiRequest("GET", "/api/ai/settings"); + if (!res.ok) return null; + return res.json(); + }, + }); + + useEffect(() => { + if (settings) { + setApiKey(settings.apiKey ?? ""); + } + }, [settings]); + + const saveMutation = useMutation({ + mutationFn: async (data: { apiKey: string }) => { + const res = await apiRequest("PUT", "/api/ai/settings", data); + if (!res.ok) { + const err = await res.json().catch(() => null); + throw new Error(err?.message || "Failed to save AI settings"); + } + return res.json(); + }, + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ["/api/ai/settings"] }); + toast({ title: "AI Settings Saved", description: "Your Google AI API key has been saved." }); + }, + onError: (err: any) => { + toast({ title: "Error", description: err?.message || "Failed to save AI settings", variant: "destructive" }); + }, + }); + + const handleSubmit = (e: React.FormEvent) => { + e.preventDefault(); + if (!apiKey.trim()) return; + saveMutation.mutate({ apiKey: apiKey.trim() }); + }; + + const isConfigured = !!settings?.apiKey; + + return ( + + +
+

Google AI Settings

+ {isConfigured && ( + + Configured + + )} +
+

+ Enter your Google AI Studio API key to enable AI-powered SMS replies. When a patient responds to an + appointment reminder, the AI will automatically reply based on their answer. +

+ + {isLoading ? ( +

Loading...

+ ) : ( +
+
+ +
+ setApiKey(e.target.value)} + className="p-2 border rounded w-full pr-10 font-mono text-sm" + placeholder="AIza••••••••••••••••••••••••••••••••••••••" + required + /> + +
+

+ Get your free key from{" "} + Google AI Studio → Get API Key. +

+
+ +
+ +
+ + {isConfigured && ( +
+

AI Auto-Reply Rules

+

+ Patient replies "Yes" → AI sends a thank-you confirmation +

+

+ Patient replies "No" / "Not available" → AI notifies them an assistant will follow up +

+
+ )} +
+ )} +
+
+ ); +} diff --git a/packages/db/prisma/migrations/20260502000003_add_ai_settings/migration.sql b/packages/db/prisma/migrations/20260502000003_add_ai_settings/migration.sql new file mode 100644 index 00000000..2810c326 --- /dev/null +++ b/packages/db/prisma/migrations/20260502000003_add_ai_settings/migration.sql @@ -0,0 +1,10 @@ +CREATE TABLE "ai_settings" ( + "id" SERIAL NOT NULL, + "userId" INTEGER NOT NULL, + "apiKey" TEXT NOT NULL, + CONSTRAINT "ai_settings_pkey" PRIMARY KEY ("id") +); + +ALTER TABLE "ai_settings" ADD CONSTRAINT "ai_settings_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +CREATE UNIQUE INDEX "ai_settings_userId_key" ON "ai_settings"("userId");