@@ -3,7 +3,7 @@ import { OAuth2Client } from "google-auth-library"
33import * as fs from "fs/promises"
44import * as path from "path"
55import * as os from "os"
6- import axios from "axios "
6+ import dotenvx from "@dotenvx/dotenvx "
77
88import { type ModelInfo , type GeminiCliModelId , geminiCliDefaultModelId , geminiCliModels } from "@roo-code/types"
99
@@ -139,8 +139,31 @@ export class GeminiCliHandler extends BaseProvider implements SingleCompletionHa
139139 return this . projectId
140140 }
141141
142- // Start with a default project ID (can be anything for personal OAuth)
143- const initialProjectId = "default"
142+ // Construct the path to the .env file for Gemini CLI configuration
143+ // Uses the custom OAuth path if provided, otherwise defaults to ~/.gemini/.env
144+ const envPath = path . join (
145+ this . options . geminiCliOAuthPath
146+ ? path . dirname ( this . options . geminiCliOAuthPath )
147+ : path . join ( os . homedir ( ) , ".gemini" ) ,
148+ ".env" ,
149+ )
150+
151+ // Load environment variables from the .env file with override enabled
152+ const { parsed, error } = dotenvx . config ( { path : envPath , override : true } )
153+
154+ // Handle case where .env file is missing or has invalid format
155+ if ( error ) {
156+ console . warn ( "[GeminiCLI] .env file not found or invalid format, proceeding with default project ID" )
157+ }
158+
159+ // Check if GOOGLE_CLOUD_PROJECT is defined in the parsed .env file
160+ if ( parsed ?. GOOGLE_CLOUD_PROJECT ) {
161+ this . projectId = parsed . GOOGLE_CLOUD_PROJECT
162+ return this . projectId
163+ }
164+
165+ // Fallback to environment variable or default project ID if not found in .env
166+ const initialProjectId = process . env . GOOGLE_CLOUD_PROJECT || "default"
144167
145168 // Prepare client metadata
146169 const clientMetadata = {
@@ -389,8 +412,9 @@ export class GeminiCliHandler extends BaseProvider implements SingleCompletionHa
389412 data : JSON . stringify ( requestBody ) ,
390413 } )
391414
392- // Extract text from response
393- const responseData = response . data as any
415+ const rawData = response . data as any
416+ const responseData = rawData . response || rawData
417+
394418 if ( responseData . candidates && responseData . candidates . length > 0 ) {
395419 const candidate = responseData . candidates [ 0 ]
396420 if ( candidate . content && candidate . content . parts ) {
0 commit comments