diff --git a/lib/gemini.ts b/lib/gemini.ts index 0e3110da..7a30697f 100644 --- a/lib/gemini.ts +++ b/lib/gemini.ts @@ -1,7 +1,16 @@ -import { GoogleGenerativeAI } from "@google/generative-ai"; +import { GoogleGenAI } from "@google/genai"; import { getConfigValue } from "@/lib/config"; -const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY || ""); +let _ai: GoogleGenAI | null = null; + +/** Lazy-initialize the GoogleGenAI client (avoids crash at import time if GEMINI_API_KEY is missing). */ +function getAI(): GoogleGenAI { + if (!_ai) { + const apiKey = process.env.GEMINI_API_KEY || ""; + _ai = new GoogleGenAI({ apiKey }); + } + return _ai; +} /** * Generate text content using Gemini Flash. @@ -14,13 +23,13 @@ export async function generateWithGemini( systemInstruction?: string, ): Promise { const geminiModel = await getConfigValue("pipeline_config", "geminiModel", "gemini-2.0-flash"); - const model = genAI.getGenerativeModel({ + const ai = getAI(); + const response = await ai.models.generateContent({ model: geminiModel, - ...(systemInstruction && { systemInstruction }), + contents: prompt, + ...(systemInstruction && { config: { systemInstruction } }), }); - const result = await model.generateContent(prompt); - const response = result.response; - return response.text(); + return response.text ?? ""; } /** diff --git a/lib/sponsor/gemini-intent.ts b/lib/sponsor/gemini-intent.ts index 9a9024d4..8c816d8d 100644 --- a/lib/sponsor/gemini-intent.ts +++ b/lib/sponsor/gemini-intent.ts @@ -1,4 +1,4 @@ -import { GoogleGenerativeAI } from '@google/generative-ai' +import { GoogleGenAI } from '@google/genai' import { getConfigValue } from '@/lib/config' const SPONSORSHIP_TIERS = [ @@ -17,10 +17,6 @@ export interface SponsorIntent { urgency: 'low' | 'medium' | 'high' } -/** - * Uses Gemini Flash to parse inbound sponsor emails/messages - * and extract structured data for creating a sponsorLead. - */ export async function extractSponsorIntent(message: string): Promise { const apiKey = process.env.GEMINI_API_KEY if (!apiKey) { @@ -35,8 +31,7 @@ export async function extractSponsorIntent(message: string): Promise