Files
QuizMaster/services/geminiService.ts
T
Philip ee4f3766cd feat(gemini): Add API key validation and error handling
Includes a check for the presence of the API key before initializing the Gemini AI client. Logs an error and returns an empty array if the API key is not found, preventing runtime errors and providing clearer feedback.
2026-01-28 17:49:18 -08:00

55 lines
1.7 KiB
TypeScript

import { GoogleGenAI, Type } from "@google/genai";
import { Question } from "../types";
const generateQuestions = async (topic: string, count: number = 5): Promise<Question[]> => {
// Use process.env.API_KEY as per guidelines.
const apiKey = process.env.API_KEY;
if (!apiKey) {
console.error("API Key is missing. Check your .env file.");
return [];
}
const ai = new GoogleGenAI({ apiKey });
try {
const response = await ai.models.generateContent({
model: "gemini-3-flash-preview",
contents: `Generate ${count} trivia questions about "${topic}". The questions should be suitable for a pub quiz.`,
config: {
responseMimeType: "application/json",
responseSchema: {
type: Type.ARRAY,
items: {
type: Type.OBJECT,
properties: {
text: { type: Type.STRING, description: "The question text" },
answer: { type: Type.STRING, description: "The correct answer" },
points: { type: Type.INTEGER, description: "Points value, usually 10, 20, or 30" },
category: { type: Type.STRING, description: "Short category name" }
},
required: ["text", "answer", "points", "category"]
}
}
}
});
if (response.text) {
const rawData = JSON.parse(response.text);
// Map to our internal ID structure
return rawData.map((q: any) => ({
id: crypto.randomUUID(),
text: q.text,
answer: q.answer,
points: q.points,
category: q.category
}));
}
return [];
} catch (error) {
console.error("Gemini generation error:", error);
return [];
}
};
export { generateQuestions };