Files
QuizMaster/services/geminiService.ts
T
Philip 77cc74a7c3 fix: Use type for type imports and literal types
Refactor `types.ts` to use `as const` for `GamePhase` and explicitly define the `GamePhase` type. This improves type safety and clarity. Additionally, update imports in other files to use `type` for type-only imports, further enhancing type safety and preventing potential runtime issues.
2026-01-28 18:07:31 -08:00

55 lines
1.7 KiB
TypeScript

import { GoogleGenAI, Type } from "@google/genai";
import type { 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 };