From bb039af55a89816dfa089857af716ce9752515f3 Mon Sep 17 00:00:00 2001 From: Vibhor Phalke Date: Fri, 26 Sep 2025 06:56:18 +0530 Subject: [PATCH 1/2] feat: add support for WIF --- .env | 6 +++++- src/App.tsx | 34 +++++++++++++++++++++++++++++----- src/types.ts | 2 +- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/.env b/.env index c1ffb2c11..7a428b737 100644 --- a/.env +++ b/.env @@ -1,2 +1,6 @@ # create your own API KEY at https://aistudio.google.com/apikey -#REACT_APP_GEMINI_API_KEY='' +REACT_APP_GEMINI_API_KEY=your_api_key_here +# Vertex AI (for WIP (Workload Identity Federation)) +REACT_APP_USE_VERTEX_AI=false # set to true to use Vertex AI +REACT_APP_GOOGLE_CLOUD_PROJECT=your-project-id +REACT_APP_GOOGLE_CLOUD_LOCATION=us-central1 \ No newline at end of file diff --git a/src/App.tsx b/src/App.tsx index 31f00b16b..8044ae4d0 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -24,13 +24,37 @@ import cn from "classnames"; import { LiveClientOptions } from "./types"; const API_KEY = process.env.REACT_APP_GEMINI_API_KEY as string; -if (typeof API_KEY !== "string") { - throw new Error("set REACT_APP_GEMINI_API_KEY in .env"); +const USE_VERTEX_AI = process.env.REACT_APP_USE_VERTEX_AI === 'false'; +const PROJECT_ID = process.env.REACT_APP_GOOGLE_CLOUD_PROJECT as string; +const LOCATION = process.env.REACT_APP_GOOGLE_CLOUD_LOCATION as string; + + +function createApiOptions(): LiveClientOptions { + if (USE_VERTEX_AI) { + // Vertex AI configuration (supports WIF) + if (!PROJECT_ID || !LOCATION) { + throw new Error( + "When using Vertex AI, set REACT_APP_GOOGLE_CLOUD_PROJECT and REACT_APP_GOOGLE_CLOUD_LOCATION in .env" + ); + } + + return { + vertexai: true, + project: PROJECT_ID, + location: LOCATION, + }; + } else { + if (typeof API_KEY !== "string") { + throw new Error("set REACT_APP_GEMINI_API_KEY in .env"); + } + + return { + apiKey: API_KEY, + }; + } } -const apiOptions: LiveClientOptions = { - apiKey: API_KEY, -}; +const apiOptions: LiveClientOptions = createApiOptions(); function App() { // this video reference is used for displaying the active stream, whether that is the webcam or screen capture diff --git a/src/types.ts b/src/types.ts index 9f5b2b26e..2366a45d8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -24,7 +24,7 @@ import { /** * the options to initiate the client, ensure apiKey is required */ -export type LiveClientOptions = GoogleGenAIOptions & { apiKey: string }; +export type LiveClientOptions = GoogleGenAIOptions; /** log types */ export type StreamingLog = { From 0f64fc64872989a163f7bc41932ee8058685ba04 Mon Sep 17 00:00:00 2001 From: Vibhor Phalke Date: Fri, 26 Sep 2025 14:48:47 +0530 Subject: [PATCH 2/2] address code-assit review --- src/App.tsx | 4 ++-- src/types.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 8044ae4d0..1cc786bb5 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -24,7 +24,7 @@ import cn from "classnames"; import { LiveClientOptions } from "./types"; const API_KEY = process.env.REACT_APP_GEMINI_API_KEY as string; -const USE_VERTEX_AI = process.env.REACT_APP_USE_VERTEX_AI === 'false'; +const USE_VERTEX_AI = process.env.REACT_APP_USE_VERTEX_AI === 'true'; const PROJECT_ID = process.env.REACT_APP_GOOGLE_CLOUD_PROJECT as string; const LOCATION = process.env.REACT_APP_GOOGLE_CLOUD_LOCATION as string; @@ -44,7 +44,7 @@ function createApiOptions(): LiveClientOptions { location: LOCATION, }; } else { - if (typeof API_KEY !== "string") { + if (!API_KEY) { throw new Error("set REACT_APP_GEMINI_API_KEY in .env"); } diff --git a/src/types.ts b/src/types.ts index 2366a45d8..3a44f22e0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -22,7 +22,7 @@ import { } from "@google/genai"; /** - * the options to initiate the client, ensure apiKey is required + * The options to initiate the client, supporting either API key or Vertex AI. */ export type LiveClientOptions = GoogleGenAIOptions;