mirror of
https://github.com/m4rcel-lol/custom-discord-ai-chatbot-site.git
synced 2025-12-06 10:53:58 +05:30
Adds a loading animation fade-in and reduces initial loading time. Suppresses Tailwind CDN production warnings in the console for a cleaner developer experience. Enhances error messages for missing API keys and API communication failures, providing more specific guidance. Removes redundant content from README.md and updates the banner image reference. Sets body overflow to hidden to prevent unwanted scrolling and ensures the application handles its own scroll.
41 lines
1.4 KiB
TypeScript
41 lines
1.4 KiB
TypeScript
import { GoogleGenAI } from "@google/genai";
|
|
import { Message } from '../types';
|
|
|
|
let chatSession: any = null;
|
|
|
|
const getClient = () => {
|
|
// Ensure process.env.API_KEY is available.
|
|
// If running in a browser without a bundler that polyfills process, this might fail unless configured.
|
|
const apiKey = process.env.API_KEY;
|
|
|
|
if (!apiKey) {
|
|
console.error("Gemini API Key is missing. Ensure process.env.API_KEY is set.");
|
|
return null;
|
|
}
|
|
return new GoogleGenAI({ apiKey });
|
|
};
|
|
|
|
export const sendMessageToGemini = async (
|
|
newMessage: string,
|
|
previousMessages: Message[]
|
|
): Promise<string> => {
|
|
const ai = getClient();
|
|
if (!ai) return "Error: API Key is missing. Check the console for details.";
|
|
|
|
try {
|
|
if (!chatSession) {
|
|
chatSession = ai.chats.create({
|
|
model: 'gemini-2.5-flash',
|
|
config: {
|
|
systemInstruction: "You are a casual, friendly AI chat companion in a Discord-like interface. Keep your responses concise, conversational, and use markdown where appropriate (like bolding or code blocks). Do not be overly formal.",
|
|
},
|
|
});
|
|
}
|
|
|
|
const response = await chatSession.sendMessage({ message: newMessage });
|
|
return response.text;
|
|
} catch (error) {
|
|
console.error("Gemini API Error:", error);
|
|
return "I'm having trouble connecting to the servers right now. Please check your network or API key configuration.";
|
|
}
|
|
}; |