Skip to content

Commit 01f83b4

Browse files
committed
Add credentials auth for Google vertex
1 parent 2e41376 commit 01f83b4

File tree

6 files changed

+88
-9
lines changed

6 files changed

+88
-9
lines changed

.changeset/tame-carpets-bake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"roo-cline": patch
3+
---
4+
5+
Add credentials auth for Google vertex

src/api/providers/vertex.ts

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { BaseProvider } from "./base-provider"
1111

1212
import { ANTHROPIC_DEFAULT_MAX_TOKENS } from "./constants"
1313
import { getModelParams, SingleCompletionHandler } from "../"
14+
import { GoogleAuth } from "google-auth-library"
1415

1516
// Types for Vertex SDK
1617

@@ -120,16 +121,56 @@ export class VertexHandler extends BaseProvider implements SingleCompletionHandl
120121
throw new Error(`Unknown model ID: ${this.options.apiModelId}`)
121122
}
122123

123-
this.anthropicClient = new AnthropicVertex({
124-
projectId: this.options.vertexProjectId ?? "not-provided",
125-
// https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude#regions
126-
region: this.options.vertexRegion ?? "us-east5",
127-
})
124+
if (this.options.vertexJsonCredentials) {
125+
this.anthropicClient = new AnthropicVertex({
126+
projectId: this.options.vertexProjectId ?? "not-provided",
127+
// https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude#regions
128+
region: this.options.vertexRegion ?? "us-east5",
129+
googleAuth: new GoogleAuth({
130+
scopes: ["https://www.googleapis.com/auth/cloud-platform"],
131+
credentials: JSON.parse(this.options.vertexJsonCredentials),
132+
}),
133+
})
134+
} else if (this.options.vertexKeyFile) {
135+
this.anthropicClient = new AnthropicVertex({
136+
projectId: this.options.vertexProjectId ?? "not-provided",
137+
// https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude#regions
138+
region: this.options.vertexRegion ?? "us-east5",
139+
googleAuth: new GoogleAuth({
140+
scopes: ["https://www.googleapis.com/auth/cloud-platform"],
141+
keyFile: this.options.vertexKeyFile,
142+
}),
143+
})
144+
} else {
145+
this.anthropicClient = new AnthropicVertex({
146+
projectId: this.options.vertexProjectId ?? "not-provided",
147+
// https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude#regions
148+
region: this.options.vertexRegion ?? "us-east5",
149+
})
150+
}
128151

129-
this.geminiClient = new VertexAI({
130-
project: this.options.vertexProjectId ?? "not-provided",
131-
location: this.options.vertexRegion ?? "us-east5",
132-
})
152+
if (this.options.vertexJsonCredentials) {
153+
this.geminiClient = new VertexAI({
154+
project: this.options.vertexProjectId ?? "not-provided",
155+
location: this.options.vertexRegion ?? "us-east5",
156+
googleAuthOptions: {
157+
credentials: JSON.parse(this.options.vertexJsonCredentials),
158+
},
159+
})
160+
} else if (this.options.vertexKeyFile) {
161+
this.geminiClient = new VertexAI({
162+
project: this.options.vertexProjectId ?? "not-provided",
163+
location: this.options.vertexRegion ?? "us-east5",
164+
googleAuthOptions: {
165+
keyFile: this.options.vertexKeyFile,
166+
},
167+
})
168+
} else {
169+
this.geminiClient = new VertexAI({
170+
project: this.options.vertexProjectId ?? "not-provided",
171+
location: this.options.vertexRegion ?? "us-east5",
172+
})
173+
}
133174
}
134175

135176
private formatMessageForCache(message: Anthropic.Messages.MessageParam, shouldCache: boolean): VertexMessage {

src/core/webview/ClineProvider.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,6 +1659,8 @@ export class ClineProvider implements vscode.WebviewViewProvider {
16591659
awsUseCrossRegionInference,
16601660
awsProfile,
16611661
awsUseProfile,
1662+
vertexKeyFile,
1663+
vertexJsonCredentials,
16621664
vertexProjectId,
16631665
vertexRegion,
16641666
openAiBaseUrl,
@@ -1710,6 +1712,8 @@ export class ClineProvider implements vscode.WebviewViewProvider {
17101712
this.updateGlobalState("awsUseCrossRegionInference", awsUseCrossRegionInference),
17111713
this.updateGlobalState("awsProfile", awsProfile),
17121714
this.updateGlobalState("awsUseProfile", awsUseProfile),
1715+
this.updateGlobalState("vertexKeyFile", vertexKeyFile),
1716+
this.updateGlobalState("vertexJsonCredentials", vertexJsonCredentials),
17131717
this.updateGlobalState("vertexProjectId", vertexProjectId),
17141718
this.updateGlobalState("vertexRegion", vertexRegion),
17151719
this.updateGlobalState("openAiBaseUrl", openAiBaseUrl),
@@ -2160,6 +2164,8 @@ export class ClineProvider implements vscode.WebviewViewProvider {
21602164
awsUseCrossRegionInference,
21612165
awsProfile,
21622166
awsUseProfile,
2167+
vertexKeyFile,
2168+
vertexJsonCredentials,
21632169
vertexProjectId,
21642170
vertexRegion,
21652171
openAiBaseUrl,
@@ -2248,6 +2254,8 @@ export class ClineProvider implements vscode.WebviewViewProvider {
22482254
this.getGlobalState("awsUseCrossRegionInference") as Promise<boolean | undefined>,
22492255
this.getGlobalState("awsProfile") as Promise<string | undefined>,
22502256
this.getGlobalState("awsUseProfile") as Promise<boolean | undefined>,
2257+
this.getGlobalState("vertexKeyFile") as Promise<string | undefined>,
2258+
this.getGlobalState("vertexJsonCredentials") as Promise<string | undefined>,
22512259
this.getGlobalState("vertexProjectId") as Promise<string | undefined>,
22522260
this.getGlobalState("vertexRegion") as Promise<string | undefined>,
22532261
this.getGlobalState("openAiBaseUrl") as Promise<string | undefined>,
@@ -2353,6 +2361,8 @@ export class ClineProvider implements vscode.WebviewViewProvider {
23532361
awsUseCrossRegionInference,
23542362
awsProfile,
23552363
awsUseProfile,
2364+
vertexKeyFile,
2365+
vertexJsonCredentials,
23562366
vertexProjectId,
23572367
vertexRegion,
23582368
openAiBaseUrl,

src/shared/api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export interface ApiHandlerOptions {
4040
awsUseProfile?: boolean
4141
vertexProjectId?: string
4242
vertexRegion?: string
43+
vertexKeyFile?: string
44+
vertexJsonCredentials?: string
4345
openAiBaseUrl?: string
4446
openAiApiKey?: string
4547
openAiModelId?: string

src/shared/globalState.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export type GlobalStateKey =
2222
| "awsUseCrossRegionInference"
2323
| "awsProfile"
2424
| "awsUseProfile"
25+
| "vertexKeyFile"
26+
| "vertexJsonCredentials"
2527
| "vertexProjectId"
2628
| "vertexRegion"
2729
| "lastShownAnnouncementId"

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,20 @@ const ApiOptions = ({
604604

605605
{selectedProvider === "vertex" && (
606606
<div style={{ display: "flex", flexDirection: "column", gap: 5 }}>
607+
<VSCodeTextField
608+
value={apiConfiguration?.vertexJsonCredentials || ""}
609+
style={{ width: "100%" }}
610+
onInput={handleInputChange("vertexJsonCredentials")}
611+
placeholder="Enter Credentials JSON...">
612+
<span className="font-medium">Google Cloud Credentials</span>
613+
</VSCodeTextField>
614+
<VSCodeTextField
615+
value={apiConfiguration?.vertexKeyFile || ""}
616+
style={{ width: "100%" }}
617+
onInput={handleInputChange("vertexKeyFile")}
618+
placeholder="Enter Key File Path...">
619+
<span className="font-medium">Google Cloud Key File Path</span>
620+
</VSCodeTextField>
607621
<VSCodeTextField
608622
value={apiConfiguration?.vertexProjectId || ""}
609623
style={{ width: "100%" }}
@@ -649,6 +663,11 @@ const ApiOptions = ({
649663
style={{ display: "inline", fontSize: "inherit" }}>
650664
{"2) install the Google Cloud CLI › configure Application Default Credentials."}
651665
</VSCodeLink>
666+
<VSCodeLink
667+
href="https://developers.google.com/workspace/guides/create-credentials?hl=en#service-account"
668+
style={{ display: "inline", fontSize: "inherit" }}>
669+
{"3) or create a service account with credentials."}
670+
</VSCodeLink>
652671
</p>
653672
</div>
654673
)}

0 commit comments

Comments
 (0)