Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions apps/api/src/utils/genai.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
import { ConfigService } from '@nestjs/config';

let genaiModule: typeof import('@google/genai') | null = null;

async function loadGenAI() {
if (!genaiModule) {
genaiModule = await import('@google/genai');
}
return genaiModule;
export interface GoogleAIInstance {
files: {
get(params: { name: string }): Promise<{ state: string; uri?: string; stateDescription?: string }>;
upload(params: { file: string; config: { mimeType: string } }): Promise<{ name: string; uri: string; mimeType: string }>;
};
models: {
generateContent(params: {
model: string;
contents: Array<{ role: string; parts: Array<{ text?: string; fileData?: { fileUri: string; mimeType: string } }> }>;
config?: { responseMimeType?: string };
}): Promise<{ text: string }>;
};
}

export async function createGoogleAI(configService: ConfigService) {
const { GoogleGenAI } = await loadGenAI();
let cachedGoogleAI: GoogleAIInstance | null = null;
let cachedApiKey: string | null = null;

export async function createGoogleAI(configService: ConfigService): Promise<GoogleAIInstance> {
const apiKey = configService.get<string>('GOOGLE_GENERATIVE_AI_API_KEY');
if (!apiKey) {
throw new Error('GOOGLE_GENERATIVE_AI_API_KEY is not configured');
}
return new GoogleGenAI({ apiKey });
}

export type GoogleAIInstance = Awaited<ReturnType<typeof createGoogleAI>>;
if (cachedGoogleAI && cachedApiKey === apiKey) {
return cachedGoogleAI;
}

const mod = await (Function('return import("@google/genai")')() as Promise<{ GoogleGenAI: new (opts: { apiKey: string }) => GoogleAIInstance }>);
cachedGoogleAI = new mod.GoogleGenAI({ apiKey });
cachedApiKey = apiKey;
return cachedGoogleAI;
}
Loading