|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2024 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { initializeApp } from "firebase/app"; |
| 19 | +import { firebaseConfig, RECAPTCHA_ENTERPRISE_SITE_KEY } from "./config"; |
| 20 | +import { |
| 21 | + initializeAppCheck, |
| 22 | + ReCaptchaEnterpriseProvider, |
| 23 | +} from "firebase/app-check"; |
| 24 | +import { |
| 25 | + getFunctions, |
| 26 | + httpsCallable, |
| 27 | + connectFunctionsEmulator, |
| 28 | +} from "firebase/functions"; |
| 29 | + |
| 30 | +// Set to true to test in emulator. |
| 31 | +const testMode = false; |
| 32 | + |
| 33 | +// Use showdown to convert Gemini-provided Markdown to HTML |
| 34 | +import { Converter } from "showdown"; |
| 35 | +const converter = new Converter(); |
| 36 | + |
| 37 | +// Set up output elements. |
| 38 | +const outputDiv = document.createElement("div"); |
| 39 | +document.body.appendChild(outputDiv); |
| 40 | + |
| 41 | +// Initialize Firebase app. |
| 42 | +const app = initializeApp(firebaseConfig); |
| 43 | + |
| 44 | +// Initialize App Check. |
| 45 | +initializeAppCheck(app, { |
| 46 | + provider: new ReCaptchaEnterpriseProvider(RECAPTCHA_ENTERPRISE_SITE_KEY), |
| 47 | +}); |
| 48 | + |
| 49 | +// Define generateWithVertex as a call to the generateWithVertex function. |
| 50 | +const generateWithVertex = httpsCallable(getFunctions(), "generateWithVertex", { |
| 51 | + limitedUseAppCheckTokens: true, |
| 52 | +}); |
| 53 | + |
| 54 | +// Enable emulator so that it can be used in test mode. |
| 55 | +const functions = getFunctions(app, "us-central1"); // Replace with your region |
| 56 | + |
| 57 | +if (testMode) { |
| 58 | + connectFunctionsEmulator(functions, "localhost", 5001); |
| 59 | +} |
| 60 | + |
| 61 | +// Generate body for index.html. |
| 62 | +document.body.innerHTML += ` |
| 63 | + <form id="promptForm"> |
| 64 | + <label for="promptInput">Prompt:</label><br> |
| 65 | + <input type="text" id="promptInput" name="prompt"><br><br> |
| 66 | + <input type="submit" value="Submit"> |
| 67 | + </form> |
| 68 | + <br/> |
| 69 | + <p id="generatedText"></p> |
| 70 | + |
| 71 | +`; |
| 72 | + |
| 73 | +const promptForm = document.getElementById("promptForm") as HTMLFormElement; |
| 74 | + |
| 75 | +promptForm.addEventListener("submit", async (event) => { |
| 76 | + event.preventDefault(); |
| 77 | + const promptInput = document.getElementById( |
| 78 | + "promptInput" |
| 79 | + ) as HTMLInputElement; |
| 80 | + const prompt = promptInput.value; |
| 81 | + |
| 82 | + try { |
| 83 | + const { data } = await generateWithVertex({ prompt }); |
| 84 | + const generatedTextElement = document.getElementById("generatedText"); // Access the element |
| 85 | + const htmlContent = converter.makeHtml(data); |
| 86 | + if (!generatedTextElement){ |
| 87 | + throw new Error("Missing generated text."); |
| 88 | + } |
| 89 | + generatedTextElement.innerHTML = htmlContent; // Set the element's content |
| 90 | + } catch (error) { |
| 91 | + console.error("Error calling generateWithVertex:", error); |
| 92 | + } |
| 93 | +}); |
0 commit comments