diff --git a/lib/integrations-gemini-ai/src/client.ts b/lib/integrations-gemini-ai/src/client.ts index 2d4be6f..7c76a61 100644 --- a/lib/integrations-gemini-ai/src/client.ts +++ b/lib/integrations-gemini-ai/src/client.ts @@ -1,21 +1,38 @@ import { GoogleGenAI } from "@google/genai"; -if (!process.env.AI_INTEGRATIONS_GEMINI_BASE_URL) { - throw new Error( - "AI_INTEGRATIONS_GEMINI_BASE_URL must be set. Did you forget to provision the Gemini AI integration?", - ); +function createClient(): GoogleGenAI { + if (!process.env.AI_INTEGRATIONS_GEMINI_BASE_URL) { + throw new Error( + "AI_INTEGRATIONS_GEMINI_BASE_URL must be set. Did you forget to provision the Gemini AI integration?", + ); + } + + if (!process.env.AI_INTEGRATIONS_GEMINI_API_KEY) { + throw new Error( + "AI_INTEGRATIONS_GEMINI_API_KEY must be set. Did you forget to provision the Gemini AI integration?", + ); + } + + return new GoogleGenAI({ + apiKey: process.env.AI_INTEGRATIONS_GEMINI_API_KEY, + httpOptions: { + apiVersion: "", + baseUrl: process.env.AI_INTEGRATIONS_GEMINI_BASE_URL, + }, + }); } -if (!process.env.AI_INTEGRATIONS_GEMINI_API_KEY) { - throw new Error( - "AI_INTEGRATIONS_GEMINI_API_KEY must be set. Did you forget to provision the Gemini AI integration?", - ); +let _ai: GoogleGenAI | null = null; + +export function getAi(): GoogleGenAI { + if (!_ai) { + _ai = createClient(); + } + return _ai; } -export const ai = new GoogleGenAI({ - apiKey: process.env.AI_INTEGRATIONS_GEMINI_API_KEY, - httpOptions: { - apiVersion: "", - baseUrl: process.env.AI_INTEGRATIONS_GEMINI_BASE_URL, +export const ai: GoogleGenAI = new Proxy({} as GoogleGenAI, { + get(_target, prop: keyof GoogleGenAI) { + return getAi()[prop]; }, }); diff --git a/lib/integrations-gemini-ai/src/image/client.ts b/lib/integrations-gemini-ai/src/image/client.ts index 2cf58e3..d769b6a 100644 --- a/lib/integrations-gemini-ai/src/image/client.ts +++ b/lib/integrations-gemini-ai/src/image/client.ts @@ -1,28 +1,10 @@ -import { GoogleGenAI, Modality } from "@google/genai"; - -if (!process.env.AI_INTEGRATIONS_GEMINI_BASE_URL) { - throw new Error( - "AI_INTEGRATIONS_GEMINI_BASE_URL must be set. Did you forget to provision the Gemini AI integration?", - ); -} - -if (!process.env.AI_INTEGRATIONS_GEMINI_API_KEY) { - throw new Error( - "AI_INTEGRATIONS_GEMINI_API_KEY must be set. Did you forget to provision the Gemini AI integration?", - ); -} - -export const ai = new GoogleGenAI({ - apiKey: process.env.AI_INTEGRATIONS_GEMINI_API_KEY, - httpOptions: { - apiVersion: "", - baseUrl: process.env.AI_INTEGRATIONS_GEMINI_BASE_URL, - }, -}); +import { Modality } from "@google/genai"; +import { getAi } from "../client.js"; export async function generateImage( prompt: string ): Promise<{ b64_json: string; mimeType: string }> { + const ai = getAi(); const response = await ai.models.generateContent({ model: "gemini-2.5-flash-image", contents: [{ role: "user", parts: [{ text: prompt }] }],