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:
Gitead
2026-05-06 08:58:58 -04:00
parent 8c162d7040
commit 4989201c62
5 changed files with 295 additions and 0 deletions

View 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,
};
}

View 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;

View 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 },
});
},
};