From 15d7a6ea3aff091c5ce93918403d1dc028dbdde2 Mon Sep 17 00:00:00 2001 From: "christian.vidal" Date: Fri, 27 Mar 2026 14:06:21 +0100 Subject: [PATCH] fix(gemini): broaden api key detection and improve error reporting --- src/services/gemini.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/services/gemini.ts b/src/services/gemini.ts index f6be644..c8c793d 100644 --- a/src/services/gemini.ts +++ b/src/services/gemini.ts @@ -1,7 +1,16 @@ export async function generateGemini(prompt: string, systemPrompt: string): Promise { - const apiKey = import.meta.env.GEMINI_API_KEY || localStorage.getItem('GEMINI_API_KEY') || ''; + // Try all possible ways to get the API key + const apiKey = + (window as any).GEMINI_API_KEY || + localStorage.getItem('GEMINI_API_KEY') || + import.meta.env.VITE_GEMINI_API_KEY || + import.meta.env.GEMINI_API_KEY || + (process.env as any).GEMINI_API_KEY || + ''; + if (!apiKey) { - throw new Error('Gemini API Key not found. Please set GEMINI_API_KEY in .env or GEMINI_API_KEY in localStorage.'); + console.error('No Gemini API Key found in env or storage'); + throw new Error('Gemini API Key not found. Please set VITE_GEMINI_API_KEY in .env or GEMINI_API_KEY in localStorage.'); } const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent?key=${apiKey}`, {