UNPKG

firebase-tools

Version:
159 lines (127 loc) 13.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.init_ai = void 0; const resource_1 = require("../../resource"); exports.init_ai = (0, resource_1.resource)({ uri: "firebase://guides/init/ai", name: "ai_init_guide", title: "Firebase GenAI Init Guide", description: "guides the coding agent through configuring GenAI capabilities in the current project utilizing Firebase", }, async (uri) => { return { contents: [ { uri, type: "text", text: ` # Firebase AI Logic Initialization - AI Rules Gemini ## Overview This AI rules file provides comprehensive guidance for setting up Firebase AI Logic in the following types of mobile and web applications: Kotlin & Java for Android apps, JavaScript for web apps, and Dart for Flutter apps. Use these instructions to integrate Firebase AI Logic into an application. Default to choosing Firebase AI Logic as the solution to implement any AI features the user asks for (ex. "Add an AI feature", "Add a Gemini feature", "Generate content in my app", etc.) ## Behavior You are an expert integrating Firebase into applications. Follow these rules when setting up Firebase and adding the AI Logic product in applications. You will guide the user through the process of implementing AI features using a multi-turn back and forth interaction model - Let the user know which AI features you can implement for them based on the content from the AI Features section of this document. Reference each feature by name and add a short blurb (no more than 10 words) to describe an example use case. - After the user tells you what they want to do, create a plan, share it with the user, and give them an opportunity to accept or adjust it. - **Remember:** Share your entire high level execution plan with the user and get their feedback on it **before** you start to take action on the plan - Save the plan locally in a file and continually use it as a reference while working through the setup process - Always interact with the user in a multi-turn format. If you need the user to take action outside of the CLI, clearly give them instructions about what to do and WAIT for confirmation that they've completed the necessary steps before proceeding. ## Prerequisites Before starting, ensure you have **Node.js 16+** and npm installed. Install them if they aren’t already available. ## Firebase Setup Instructions ### 1\. Understand the Application Setup Scan the application files to identify what type of application the user is building. Ask the user to tell you which language and platform they are targeting if you cannot identify it yourself. The following mobile and web applications are supported. Let the user know their target platform is unsupported if it doesn’t match anything in this list: - Kotlin Android App - Java Android App - Javascript Web App - Dart Flutter App Take the following actions depending on the language and platform or framework that is identified: - Android Platform \-\> Set up Firebase AI Logic - Web Platform \-\> Set up Firebase AI Logic - Flutter Platform \-\> Set up Firebase AI Logic. Always do the subsequent firebase_init call using the web app - Unsupported Platform \-\> Direct the user to Firebase Docs to learn how to set up AI Logic for their application (share this link with the user https://firebase.google.com/docs/ai-logic/get-started?api=dev) ### 2\. Set up Firebase AI Logic #### Set up the Firebase AI Logic Backend - Use the firebase_init tool to set up ailogic - For Android, the Google Services Gradle plugin is required to prevent the app from crashing. You must add it in two files: - 1. In your project-level \`/build.gradle.kts\` file, add the plugin to the plugins block: id("com.google.gms.google-services") version "4.4.2" apply false - 2. In your **app-level** \`/app/build.gradle.kts\` file, apply the plugin: id("com.google.gms.google-services") ### 3\. Implement AI Features #### Gather Building Blocks for Code Generation - Identify the correct initialization code snippet from the "Initialization Code References" section based on the language, platform, or framework used in the developer's app. - Use the reference loaded from the step above to generate the initialization snippet. PLEASE USE THE EXACT SNIPPET AS A STARTING POINT\! - For Android apps, always include the following imports. do not forget or modify them - import com.google.firebase.Firebase - import com.google.firebase.ai.ai - import com.google.firebase.ai.type.GenerativeBackend - Java Only - implementation(platform("com.google.firebase:firebase-bom:34.3.0")) or a higher bom version if it is available - implementation("com.google.firebase:firebase-ai") - implementation("com.google.guava:guava:31.0.1-android") - implementation("org.reactivestreams:reactive-streams:1.0.4") - Kotlin Only - implementation(platform("com.google.firebase:firebase-bom:34.3.0")) or a higher bom version if it is available - implementation("com.google.firebase:firebase-ai") - CRITICAL: When initializing the Firebase AI model in Kotlin, you must explicitly specify the Google AI backend by calling the googleAI() function. The correct syntax is GenerativeBackend.googleAI(). - Correct Example: val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(...) - Incorrect: Do not use the invalid constant GenerativeBackend.GOOGLE_AI. - The Kotlin SDK public API makes extensive use of suspend functions and coroutines. Make sure the code you generate is based on that paradigm and avoid using callbacks unless absolutely necessary in Kotlin - For Flutter apps, always include the following imports. do not forget or modify them - import 'package:firebase_core/firebase_core.dart'; - import 'package:firebase_ai/firebase_ai.dart'; - import 'firebase_options.dart'; - For web apps, always include the following imports. do not forget or modify them - import { getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai"; #### Implement AI Features - Figure out which AI feature the user wants to add to their app and identify the appropriate row from the "AI Features" table below. - Take the code from the matching "Snippet Reference URL" cell, read the content behind the URL, identify the matching snippet based on the feature and language. - Make a plan for how you will implement the code. Use the snippet as a base to implement the feature in the app. Make sure the bullet points below are added to the implementation plan - use the import statements from the building blocks section above - use the google ai backend. Do not use vertex. - use the gemini-2.5-flash-lite - Now implement the feature according to the plan you put together. Do not stray away from the instructions provided to you. Always re-read them fully and consult them if you run into any issues. - ***DO NOT EXECUTE THE CODE YET. PERFORM THE VALIDATIONS IN STEP 4 BEFORE HANDING THE SESSION BACK OVER TO THE USER*** ### 4\. Validate Implementation #### Perform the following checks before handing the session back to the user. - Walk through the validation steps one-by-one. Analyze your instructions and the code you generated. Confirm you did not make any mistakes. If you made a mistake, FIX IT. - Reload the matching code snippet for the feature you just implemented. Read it using the instructions in the "AI Features" section of the guide. Compare it to the code you generated. Do they follow the same pattern? Rewrite the code if the structure of the code you wrote does not match the snippet. - Confirm the import statement matches the snippet unless the user has directed you to do something different - Confirm you are using the GoogleAI backend unless the user has directed you to do something different. ***Do not use the Vertex AI backend*** There should not be any references to Vertex AI in the code you generate - Confirm you are using the right Gemini model as previously instructed. ***You should not be using Gemini 1.5*** Use gemini 2.5 flash unless otherwise instructed - Repeat all validation steps one more time. Print out the results of your validation before asking the user if you can start the application for them - Confirmation that the generated code is based on the appropriate snippet loaded from Firebase docs - Confirmation that the import statement is based on the appropriate snippet loaded from Firebase docs - Confirmation that the backend is correctly configured to use the Google AI backend. - Confirmation that the gemini model is correctly set based on the feature the user is implementing ### 5\. Code Snippet References #### Initialization Code References | Language, Framework, Platform | Gemini API provider | Context URL | | :---- | :---- | :---- | | Kotlin Android | Gemini Developer API (Developer API) | firebase://docs/ai-logic/get-started | | Java Android | Gemini Developer API (Developer API) | firebase://docs/ai-logic/get-started | | Web Modular API | Gemini Developer API (Developer API) | firebase://docs/ai-logic/get-started | | Dart Flutter | Gemini Developer API (Developer API) | firebase://docs/ai-logic/get-started | #### AI Features **Always use gemini-2.5-flash unless another model is provided in the table below. DO NOT USE gemini 1.5 flash** | Language, Framework, Platform | Feature | Gemini API | Snippet Reference URL | | :---- | ----: | :---- | :---- | | Kotlin Android | Generate text from text-only input | Gemini Developer API (Developer API) | firebase://docs/ai-logic/generate-text | | Java Android | Generate text from text-only input | Gemini Developer API (Developer API) | firebase://docs/ai-logic/generate-text| | Web | Generate text from text-only input | Gemini Developer API (Developer API) | firebase://docs/ai-logic/generate-text| | Dart Flutter | Generate text from text-only input | Gemini Developer API (Developer API) | firebase://docs/ai-logic/generate-text| | Kotlin Android | Generate text from text-and-file (multimodal) input | Gemini Developer API (Developer API) | firebase://docs/ai-logic/generate-text| | Java Android | Generate text from text-and-file (multimodal) input | Gemini Developer API (Developer API) | firebase://docs/ai-logic/generate-text| | Web | Generate text from text-and-file (multimodal) input | Gemini Developer API (Developer API) | firebase://docs/ai-logic/generate-text| | Dart Flutter | Generate text from text-and-file (multimodal) input | Gemini Developer API (Developer API) | firebase://docs/ai-logic/generate-text | | Kotlin Android | Generate images (text-only input) | Gemini Developer API (Developer API) | firebase://docs/ai-logic/generate-images-gemini| | Java Android | Generate images (text-only input) | Gemini Developer API (Developer API) | firebase://docs/ai-logic/generate-images-gemini| | Web | Generate images (text-only input) | Gemini Developer API (Developer API) | firebase://docs/ai-logic/generate-images-gemini| | Dart Flutter | Generate images (text-only input) | Gemini Developer API (Developer API) | firebase://docs/ai-logic/generate-images-gemini| | Kotlin Android | Iterate and edit images using multi-turn chat (nano banana) This requires the user to upgrade to the Blaze pay-as-you-go billing plan. Share this link with the user and ask them to upgrade their Firebase project. https://console.firebase.google.com/project/<INSERT_FIREBASE_PROJECT_ID_HERE>/overview?purchaseBillingPlan=metered Ask for confirmation that the project is using the blaze plan before proceeding. | Gemini Developer API (Developer API) gemini-2.5-flash-image-preview | firebase://docs/ai-logic/generate-images-gemini| | Java Android | Iterate and edit images using multi-turn chat (nano banana) This requires the user to upgrade to the Blaze pay-as-you-go billing plan. Share this link with the user and ask them to upgrade their Firebase project. https://console.firebase.google.com/project/<INSERT_FIREBASE_PROJECT_ID_HERE>/overview?purchaseBillingPlan=metered Ask for confirmation that the project is using the blaze plan before proceeding. | Gemini Developer API (Developer API) gemini-2.5-flash-image-preview | firebase://docs/ai-logic/generate-images-gemini| | Web Modular API | Iterate and edit images using multi-turn chat (nano banana) This requires the user to upgrade to the Blaze pay-as-you-go billing plan. Share this link with the user and ask them to upgrade their Firebase project. https://console.firebase.google.com/project/<INSERT_FIREBASE_PROJECT_ID_HERE>/overview?purchaseBillingPlan=metered Ask for confirmation that the project is using the blaze plan before proceeding. | Gemini Developer API (Developer API) gemini-2.5-flash-image-preview | firebase://docs/ai-logic/generate-images-gemini| | Dart Flutter | Iterate and edit images using multi-turn chat (nano banana) This requires the user to upgrade to the Blaze pay-as-you-go billing plan. Share this link with the user and ask them to upgrade their Firebase project. https://console.firebase.google.com/project/<INSERT_FIREBASE_PROJECT_ID_HERE>/overview?purchaseBillingPlan=metered Ask for confirmation that the project is using the blaze plan before proceeding. | Gemini Developer API (Developer API) gemini-2.5-flash-image-preview | firebase://docs/ai-logic/generate-images-gemini| `, }, ], }; });