feat: add AI settings routes, storage, UI card, and migration
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
100
apps/Backend/src/ai/reminder-graph.ts
Normal file
100
apps/Backend/src/ai/reminder-graph.ts
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
import { StateGraph, END, START, Annotation } from "@langchain/langgraph";
|
||||||
|
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
|
||||||
|
|
||||||
|
const GraphState = Annotation.Root({
|
||||||
|
message: Annotation<string>(),
|
||||||
|
intent: Annotation<string>(),
|
||||||
|
reply: Annotation<string>(),
|
||||||
|
});
|
||||||
|
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
}
|
||||||
39
apps/Backend/src/routes/ai-settings.ts
Normal file
39
apps/Backend/src/routes/ai-settings.ts
Normal file
@@ -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<any> => {
|
||||||
|
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<any> => {
|
||||||
|
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;
|
||||||
15
apps/Backend/src/storage/ai-settings-storage.ts
Normal file
15
apps/Backend/src/storage/ai-settings-storage.ts
Normal file
@@ -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 },
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
131
apps/Frontend/src/components/settings/ai-settings-card.tsx
Normal file
131
apps/Frontend/src/components/settings/ai-settings-card.tsx
Normal file
@@ -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<AiSettings | null>({
|
||||||
|
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 (
|
||||||
|
<Card>
|
||||||
|
<CardContent className="space-y-4 py-6">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<h3 className="text-lg font-semibold">Google AI Settings</h3>
|
||||||
|
{isConfigured && (
|
||||||
|
<span className="flex items-center gap-1 text-xs text-green-600 font-medium">
|
||||||
|
<CheckCircle className="h-3.5 w-3.5" /> Configured
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<p className="text-sm text-gray-500">
|
||||||
|
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.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
{isLoading ? (
|
||||||
|
<p className="text-sm text-gray-400">Loading...</p>
|
||||||
|
) : (
|
||||||
|
<form className="space-y-4" onSubmit={handleSubmit}>
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium">Google AI Studio API Key</label>
|
||||||
|
<div className="relative mt-1">
|
||||||
|
<input
|
||||||
|
type={showKey ? "text" : "password"}
|
||||||
|
value={apiKey}
|
||||||
|
onChange={(e) => setApiKey(e.target.value)}
|
||||||
|
className="p-2 border rounded w-full pr-10 font-mono text-sm"
|
||||||
|
placeholder="AIza••••••••••••••••••••••••••••••••••••••"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setShowKey((v) => !v)}
|
||||||
|
className="absolute inset-y-0 right-2 flex items-center text-gray-500 hover:text-gray-700"
|
||||||
|
tabIndex={-1}
|
||||||
|
>
|
||||||
|
{showKey ? <EyeOff size={16} /> : <Eye size={16} />}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<p className="text-xs text-gray-500 mt-1">
|
||||||
|
Get your free key from{" "}
|
||||||
|
<span className="font-medium">Google AI Studio</span> → Get API Key.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex items-center gap-3 pt-1">
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
className="bg-teal-600 text-white px-4 py-2 rounded hover:bg-teal-700 text-sm"
|
||||||
|
disabled={saveMutation.isPending}
|
||||||
|
>
|
||||||
|
{saveMutation.isPending ? "Saving..." : "Save AI Settings"}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{isConfigured && (
|
||||||
|
<div className="mt-2 p-3 bg-gray-50 rounded border text-xs text-gray-600 space-y-1">
|
||||||
|
<p className="font-medium text-gray-700">AI Auto-Reply Rules</p>
|
||||||
|
<p>
|
||||||
|
<span className="text-green-600 font-medium">Patient replies "Yes"</span> → AI sends a thank-you confirmation
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<span className="text-red-500 font-medium">Patient replies "No" / "Not available"</span> → AI notifies them an assistant will follow up
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</form>
|
||||||
|
)}
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -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");
|
||||||
Reference in New Issue
Block a user