|
| 1 | +import { initializeApp, getApp, getApps, FirebaseApp } from 'firebase/app'; |
| 2 | +import { getFirestore, Firestore } from 'firebase/firestore'; |
| 3 | +import { getAI, getGenerativeModel, VertexAIBackend } from "firebase/ai"; |
| 4 | +// import { getAuth } from 'firebase/auth'; // Example: if you need Firebase Auth |
| 5 | +// import { getStorage } from 'firebase/storage'; // Example: if you need Firebase Storage |
| 6 | +// import { getAnalytics } from 'firebase/analytics'; // Example: if you need Firebase Analytics |
| 7 | + |
| 8 | +const firebaseConfig = { |
| 9 | + apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY, |
| 10 | + authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN, |
| 11 | + projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID, |
| 12 | + storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET, |
| 13 | + messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID, |
| 14 | + appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID, |
| 15 | + measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID, |
| 16 | +}; |
| 17 | + |
| 18 | +const firebaseApp = initializeApp(firebaseConfig); |
| 19 | +const generationConfig = { |
| 20 | + max_output_tokens: 200, |
| 21 | + stop_sequences: ["red"], |
| 22 | + temperature: 0.9, |
| 23 | + top_p: 0.1, |
| 24 | + top_k: 16, |
| 25 | +}; |
| 26 | + |
| 27 | +const ai = getAI(firebaseApp, { backend: new VertexAIBackend() }); |
| 28 | + |
| 29 | +const model = getGenerativeModel(ai, { model: "gemini-2.5-flash", generationConfig }); |
| 30 | + |
| 31 | + |
| 32 | +let app: FirebaseApp; |
| 33 | +let db: Firestore; |
| 34 | +// let auth; // Example |
| 35 | +// let storage; // Example |
| 36 | +// let analytics; // Example |
| 37 | + |
| 38 | +const requiredEnvVars = [ |
| 39 | + process.env.NEXT_PUBLIC_FIREBASE_API_KEY, |
| 40 | + process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN, |
| 41 | + process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID, |
| 42 | + process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET, |
| 43 | + process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID, |
| 44 | + process.env.NEXT_PUBLIC_FIREBASE_APP_ID, |
| 45 | +]; |
| 46 | + |
| 47 | +const allVarsPresent = requiredEnvVars.every(Boolean); |
| 48 | + |
| 49 | +if (allVarsPresent) { |
| 50 | + if (!getApps().length) { |
| 51 | + app = initializeApp(firebaseConfig); |
| 52 | + } else { |
| 53 | + app = getApp(); |
| 54 | + } |
| 55 | + db = getFirestore(app); |
| 56 | + // auth = getAuth(app); // Example |
| 57 | + // storage = getStorage(app); // Example |
| 58 | + // if (firebaseConfig.measurementId) { // Example for Analytics |
| 59 | + // analytics = getAnalytics(app); |
| 60 | + // } |
| 61 | +} else { |
| 62 | + console.warn( |
| 63 | + 'Firebase environment variables are not fully configured. Firebase services will not be available.', |
| 64 | + ); |
| 65 | + // Provide dummy/undefined exports or throw an error if Firebase is critical |
| 66 | + // For now, db will be undefined if not configured. |
| 67 | +} |
| 68 | + |
| 69 | +export { app, model, db }; |
0 commit comments