async function critiquePaintDrawing()

in src/gemini_95/index.ts [909:985]


async function critiquePaintDrawing(): Promise<void> {
    const paintWindow = document.getElementById('paint') as HTMLDivElement | null;
    if (!paintWindow || paintWindow.style.display === 'none') {
        return; // Paint window not open or visible
    }

    // Get our own canvas element
    const canvas = paintWindow.querySelector('#paint-canvas') as HTMLCanvasElement | null;
    if (!canvas) {
        console.error("Paint canvas element not found.");
        if (assistantBubble) assistantBubble.textContent = 'Error: Cannot find the canvas!';
        return;
    }

    if (!geminiInstance) {
        // Attempt to initialize Gemini if not already done (same logic as before)
        if (!await initializeGeminiIfNeeded('critiquePaintDrawing')) {
            if (assistantBubble) assistantBubble.textContent = 'Error: AI initialization failed!';
            return; // Stop if AI cannot be initialized
        }
    }

    try {
        if (assistantBubble) assistantBubble.textContent = 'Analyzing masterpiece...';

        // Get image data directly from canvas
        const ctx = canvas.getContext('2d');
        if (!ctx) {
            console.warn("Could not get canvas context for critique.");
            if (assistantBubble) assistantBubble.textContent = 'Error: Cannot read canvas!';
            return;
        }

        // Convert canvas to base64 JPEG
        const imageDataUrl = canvas.toDataURL('image/jpeg', 0.8);
        const base64Data = imageDataUrl.split(',')[1];

        if (!base64Data) {
            throw new Error("Failed to get base64 data from canvas.");
        }

        // Prepare the prompt and image data for Gemini
        const prompt = "Comment on the drawing you see and give your critique. Be a witty and slightly sarcastic artist. Keep your response to 1-2 sentences.";
        const imagePart = {
            inlineData: {
                data: base64Data,
                mimeType: "image/jpeg",
            },
        };

        // API Call
        const result = await geminiInstance.models.generateContent({
             model: "gemini-1.5-pro-latest",
             contents: [{ role: "user", parts: [ { text: prompt }, imagePart] }]
        });

        const critique = result?.candidates?.[0]?.content?.parts?.[0]?.text?.trim() || "Looks like... modern art? Or maybe my circuits are crossed.";
        if (assistantBubble) assistantBubble.textContent = critique;
        console.log("Paint Critique:", critique);

    } catch (error) {
        console.error("Error during paint critique:", error);
        if (assistantBubble) {
             let errorMessage = 'Unknown error';
             if (error instanceof Error) {
                 errorMessage = error.message;
             } else if (typeof error === 'string') {
                 errorMessage = error;
             } else {
                 try {
                     errorMessage = JSON.stringify(error);
                 } catch { /* ignore stringify errors */ }
             }
            assistantBubble.textContent = `Critique Error: ${errorMessage}`;
        }
    }
}