Skip to content

Commit 2be53fe

Browse files
authored
feat(gemini-cli): load project ID from .env and enhance API response parsing (#516)
1 parent 57df9be commit 2be53fe

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

src/api/providers/gemini-cli.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { OAuth2Client } from "google-auth-library"
33
import * as fs from "fs/promises"
44
import * as path from "path"
55
import * as os from "os"
6-
import axios from "axios"
6+
import dotenvx from "@dotenvx/dotenvx"
77

88
import { 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) {

webview-ui/src/components/settings/ApiOptions.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ import {
9999
Featherless,
100100
VercelAiGateway,
101101
DeepInfra,
102+
GeminiCli,
102103
} from "./providers"
103104

104105
import { MODELS_BY_PROVIDER, PROVIDERS } from "./constants"
@@ -114,7 +115,6 @@ import { RateLimitSecondsControl } from "./RateLimitSecondsControl"
114115
import { ConsecutiveMistakeLimitControl } from "./ConsecutiveMistakeLimitControl"
115116
import { BedrockCustomArn } from "./providers/BedrockCustomArn"
116117
// import { buildDocLink } from "@src/utils/docLinks"
117-
import { GeminiCli } from "./providers/GeminiCli"
118118
import { SetCachedStateField } from "./types"
119119

120120
export interface ApiOptionsProps {

webview-ui/src/components/settings/providers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export { Unbound } from "./Unbound"
2424
export { Vertex } from "./Vertex"
2525
export { VSCodeLM } from "./VSCodeLM"
2626
export { XAI } from "./XAI"
27+
export { GeminiCli } from "./GeminiCli"
2728
export { ZAi } from "./ZAi"
2829
export { LiteLLM } from "./LiteLLM"
2930
export { ZgsmAI } from "./ZgsmAI"

0 commit comments

Comments
 (0)