From 4fcda7c56a3c4b2495673f29e310d0fde4f168fe Mon Sep 17 00:00:00 2001 From: sirpiglr <49359077-sirpiglr@users.noreply.replit.com> Date: Sat, 6 Dec 2025 04:00:55 +0000 Subject: [PATCH] Add AI chat functionality with fallback for unconfigured services Update AIChat component to handle AI service configuration errors and register new API routes for AI chat and title generation. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 9203795e-937a-4306-b81d-b4d5c78c240e Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: 33138a67-a89c-47ac-a551-6b983481ce56 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7c94b7a0-29c7-4f2e-94ef-44b2153872b7/9203795e-937a-4306-b81d-b4d5c78c240e/BDxFKG1 Replit-Helium-Checkpoint-Created: true --- client/components/ai/AIChat.tsx | 22 ++++++++++++++++------ server/index.ts | 6 ++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/client/components/ai/AIChat.tsx b/client/components/ai/AIChat.tsx index 898523aa..bcc2563e 100644 --- a/client/components/ai/AIChat.tsx +++ b/client/components/ai/AIChat.tsx @@ -101,12 +101,22 @@ export const AIChat: React.FC = ({ try { const history = messages.filter(m => m.role !== 'model' || messages.indexOf(m) > 0); - const response = await runChat( - content, - history, - currentPersona.systemInstruction, - currentPersona.tools - ); + let response: string; + try { + response = await runChat( + content, + history, + currentPersona.systemInstruction, + currentPersona.tools + ); + } catch (err) { + const errorMessage = err instanceof Error ? err.message : 'Unknown error'; + if (errorMessage.includes('AI service not configured') || errorMessage.includes('not configured')) { + response = "The AI service is currently being set up. Please check back soon, or contact the administrator to configure the Gemini API key."; + } else { + throw err; + } + } const modelMessage: ChatMessageType = { role: 'model', diff --git a/server/index.ts b/server/index.ts index acc43283..e746ff25 100644 --- a/server/index.ts +++ b/server/index.ts @@ -7,6 +7,8 @@ import { emailService } from "./email"; import { randomUUID, createHash, createVerify, randomBytes } from "crypto"; import blogIndexHandler from "../api/blog/index"; import blogSlugHandler from "../api/blog/[slug]"; +import aiChatHandler from "../api/ai/chat"; +import aiTitleHandler from "../api/ai/title"; // Discord Interactions Handler const handleDiscordInteractions = async ( @@ -7104,5 +7106,9 @@ export function createServer() { return blogSlugHandler(req, res); }); + // AI Chat API routes + app.post("/api/ai/chat", aiChatHandler); + app.post("/api/ai/title", aiTitleHandler); + return app; }