5a12341d4c
Introduces a new authentication flow for the host, requiring a password to access host-specific features. This commit also adds a dedicated settings section within the host view, allowing administrators to configure essential application parameters such as the API key for AI question generation and the join URL for players. The backend has been updated to include a new `settings` table in the database to persist these configurations. The Gemini service is refactored to accept the API key as a parameter, enhancing flexibility and security. UI components like `HostView` and `App.tsx` are modified to integrate the new authentication and settings management functionalities. Key changes include: - Password-based authentication for host access. - A new settings interface for API key and join URL management. - Database schema update with a `settings` table. - Refactoring `geminiService` to accept API key. - UI adjustments for login and settings screens.
52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
import { GoogleGenAI, Type } from "@google/genai";
|
|
import type { Question } from "../types";
|
|
|
|
const generateQuestions = async (topic: string, apiKey: string, count: number = 5): Promise<Question[]> => {
|
|
if (!apiKey) {
|
|
console.error("API Key is missing. Please configure it in Host Settings.");
|
|
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 }; |