async function getAiHint()

in src/gemini_95/index.ts [1501:1555]


    async function getAiHint() {
        if (minesweeperGameOver || minesweeperFirstClick) {
            commentaryElement.textContent = "Click a square first!";
            return;
        }

        hintButton.disabled = true;
        hintButton.textContent = '🤔';
        commentaryElement.textContent = 'Thinking...';

        // --- Initialize Gemini using the required pattern --- //
        if (!geminiInstance) {
            if (!await initializeGeminiIfNeeded('getAiHint')) {
                commentaryElement.textContent = 'AI Init Error: Could not connect to Gemini';
                hintButton.disabled = false;
                hintButton.textContent = '💡 Hint';
                return;
            }
        }
        // --- End Gemini Initialization ---

        try {
            const boardState = getBoardStateAsText();
            const prompt = `
You are a witty, slightly sarcastic Minesweeper expert playing along.
Based on the following Minesweeper board state, provide a short (1-2 sentence) hint or observation about a potentially safe move or a dangerous area. Don't give away exact mine locations unless it's logically certain from the revealed numbers. Format the hint as playful commentary.

${boardState}
Hint:`;

            // --- Call Gemini API using existing pattern --- //
            const result = await geminiInstance.models.generateContent({
                model: "gemini-1.5-flash", // Use a fast model
                contents: [{ role: "user", parts: [{ text: prompt }] }],
                config: {
                    temperature: 0.7, // Allow for some creativity
                }
            });

            const hintText = result?.candidates?.[0]?.content?.parts?.[0]?.text?.trim() || "My circuits are buzzing... maybe try clicking somewhere?";
            commentaryElement.textContent = hintText;
            console.log("Minesweeper Hint:", hintText);

        } catch (error) {
            console.error("Error getting Minesweeper hint:", error);
            let errorMessage = 'Unknown error';
             if (error instanceof Error) errorMessage = error.message;
             else try {errorMessage = JSON.stringify(error); } catch {} // Simple stringify
            commentaryElement.textContent = `Hint Error: ${errorMessage}`;
        } finally {
            hintButton.disabled = false;
            hintButton.textContent = '💡 Hint';
        }

    }