Skip to content

Commit e8b2169

Browse files
owengoolivierhub
andauthored
Add baseUrl configuration for gemini api requests (RooCodeInc#2843)
* Add baseUrl configuration for gemini api requests * Add changeset --------- Co-authored-by: Olivier Schiavo <[email protected]>
1 parent 3ef81cd commit e8b2169

File tree

6 files changed

+44
-2
lines changed

6 files changed

+44
-2
lines changed

.changeset/rich-bees-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"claude-dev": patch
3+
---
4+
5+
Support baseURL for Google Gemini provider

src/api/providers/gemini.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ export class GeminiHandler implements ApiHandler {
2020

2121
@withRetry()
2222
async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream {
23-
const model = this.client.getGenerativeModel({
23+
const modelOptions = {
2424
model: this.getModel().id,
2525
systemInstruction: systemPrompt,
26-
})
26+
}
27+
28+
const clientOptions = this.options.geminiBaseUrl ? { baseUrl: this.options.geminiBaseUrl } : undefined
29+
const model = this.client.getGenerativeModel(modelOptions, clientOptions)
2730
const result = await model.generateContentStream({
2831
contents: messages.map(convertAnthropicMessageToGemini),
2932
generationConfig: {

src/core/storage/state-keys.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export type GlobalStateKey =
4444
| "lmStudioModelId"
4545
| "lmStudioBaseUrl"
4646
| "anthropicBaseUrl"
47+
| "geminiBaseUrl"
4748
| "azureApiVersion"
4849
| "openRouterModelId"
4950
| "openRouterModelInfo"

src/core/storage/state.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export async function getAllExtensionState(context: vscode.ExtensionContext) {
8181
lmStudioBaseUrl,
8282
anthropicBaseUrl,
8383
geminiApiKey,
84+
geminiBaseUrl,
8485
openAiNativeApiKey,
8586
deepSeekApiKey,
8687
requestyApiKey,
@@ -153,6 +154,7 @@ export async function getAllExtensionState(context: vscode.ExtensionContext) {
153154
getGlobalState(context, "lmStudioBaseUrl") as Promise<string | undefined>,
154155
getGlobalState(context, "anthropicBaseUrl") as Promise<string | undefined>,
155156
getSecret(context, "geminiApiKey") as Promise<string | undefined>,
157+
getGlobalState(context, "geminiBaseUrl") as Promise<string | undefined>,
156158
getSecret(context, "openAiNativeApiKey") as Promise<string | undefined>,
157159
getSecret(context, "deepSeekApiKey") as Promise<string | undefined>,
158160
getSecret(context, "requestyApiKey") as Promise<string | undefined>,
@@ -266,6 +268,7 @@ export async function getAllExtensionState(context: vscode.ExtensionContext) {
266268
lmStudioBaseUrl,
267269
anthropicBaseUrl,
268270
geminiApiKey,
271+
geminiBaseUrl,
269272
openAiNativeApiKey,
270273
deepSeekApiKey,
271274
requestyApiKey,
@@ -345,6 +348,7 @@ export async function updateApiConfiguration(context: vscode.ExtensionContext, a
345348
lmStudioBaseUrl,
346349
anthropicBaseUrl,
347350
geminiApiKey,
351+
geminiBaseUrl,
348352
openAiNativeApiKey,
349353
deepSeekApiKey,
350354
requestyApiKey,
@@ -401,6 +405,7 @@ export async function updateApiConfiguration(context: vscode.ExtensionContext, a
401405
await updateGlobalState(context, "lmStudioBaseUrl", lmStudioBaseUrl)
402406
await updateGlobalState(context, "anthropicBaseUrl", anthropicBaseUrl)
403407
await storeSecret(context, "geminiApiKey", geminiApiKey)
408+
await updateGlobalState(context, "geminiBaseUrl", geminiBaseUrl)
404409
await storeSecret(context, "openAiNativeApiKey", openAiNativeApiKey)
405410
await storeSecret(context, "deepSeekApiKey", deepSeekApiKey)
406411
await storeSecret(context, "requestyApiKey", requestyApiKey)

src/shared/api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export interface ApiHandlerOptions {
5959
lmStudioModelId?: string
6060
lmStudioBaseUrl?: string
6161
geminiApiKey?: string
62+
geminiBaseUrl?: string
6263
openAiNativeApiKey?: string
6364
deepSeekApiKey?: string
6465
requestyApiKey?: string

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const ApiOptions = ({ showModelOptions, apiErrorMessage, modelIdErrorMessage, is
9898
const [lmStudioModels, setLmStudioModels] = useState<string[]>([])
9999
const [vsCodeLmModels, setVsCodeLmModels] = useState<vscodemodels.LanguageModelChatSelector[]>([])
100100
const [anthropicBaseUrlSelected, setAnthropicBaseUrlSelected] = useState(!!apiConfiguration?.anthropicBaseUrl)
101+
const [geminiBaseUrlSelected, setGeminiBaseUrlSelected] = useState(!!apiConfiguration?.geminiBaseUrl)
101102
const [azureApiVersionSelected, setAzureApiVersionSelected] = useState(!!apiConfiguration?.azureApiVersion)
102103
const [awsEndpointSelected, setAwsEndpointSelected] = useState(!!apiConfiguration?.awsBedrockEndpoint)
103104
const [modelConfigurationSelected, setModelConfigurationSelected] = useState(false)
@@ -779,6 +780,32 @@ const ApiOptions = ({ showModelOptions, apiErrorMessage, modelIdErrorMessage, is
779780
placeholder="Enter API Key...">
780781
<span style={{ fontWeight: 500 }}>Gemini API Key</span>
781782
</VSCodeTextField>
783+
784+
<VSCodeCheckbox
785+
checked={geminiBaseUrlSelected}
786+
onChange={(e: any) => {
787+
const isChecked = e.target.checked === true
788+
setGeminiBaseUrlSelected(isChecked)
789+
if (!isChecked) {
790+
setApiConfiguration({
791+
...apiConfiguration,
792+
geminiBaseUrl: "",
793+
})
794+
}
795+
}}>
796+
Use custom base URL
797+
</VSCodeCheckbox>
798+
799+
{geminiBaseUrlSelected && (
800+
<VSCodeTextField
801+
value={apiConfiguration?.geminiBaseUrl || ""}
802+
style={{ width: "100%", marginTop: 3 }}
803+
type="url"
804+
onInput={handleInputChange("geminiBaseUrl")}
805+
placeholder="Default: https://generativelanguage.googleapis.com"
806+
/>
807+
)}
808+
782809
<p
783810
style={{
784811
fontSize: "12px",

0 commit comments

Comments
 (0)