From 01f83b4d17d4ca1de5988479e136b682aa4558e2 Mon Sep 17 00:00:00 2001 From: eong Date: Thu, 6 Mar 2025 00:51:38 +1300 Subject: [PATCH] Add credentials auth for Google vertex --- .changeset/tame-carpets-bake.md | 5 ++ src/api/providers/vertex.ts | 59 ++++++++++++++++--- src/core/webview/ClineProvider.ts | 10 ++++ src/shared/api.ts | 2 + src/shared/globalState.ts | 2 + .../src/components/settings/ApiOptions.tsx | 19 ++++++ 6 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 .changeset/tame-carpets-bake.md diff --git a/.changeset/tame-carpets-bake.md b/.changeset/tame-carpets-bake.md new file mode 100644 index 00000000000..69141ac4ca8 --- /dev/null +++ b/.changeset/tame-carpets-bake.md @@ -0,0 +1,5 @@ +--- +"roo-cline": patch +--- + +Add credentials auth for Google vertex diff --git a/src/api/providers/vertex.ts b/src/api/providers/vertex.ts index 7f764a9076a..bc888e04599 100644 --- a/src/api/providers/vertex.ts +++ b/src/api/providers/vertex.ts @@ -11,6 +11,7 @@ import { BaseProvider } from "./base-provider" import { ANTHROPIC_DEFAULT_MAX_TOKENS } from "./constants" import { getModelParams, SingleCompletionHandler } from "../" +import { GoogleAuth } from "google-auth-library" // Types for Vertex SDK @@ -120,16 +121,56 @@ export class VertexHandler extends BaseProvider implements SingleCompletionHandl throw new Error(`Unknown model ID: ${this.options.apiModelId}`) } - this.anthropicClient = new AnthropicVertex({ - projectId: this.options.vertexProjectId ?? "not-provided", - // https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude#regions - region: this.options.vertexRegion ?? "us-east5", - }) + if (this.options.vertexJsonCredentials) { + this.anthropicClient = new AnthropicVertex({ + projectId: this.options.vertexProjectId ?? "not-provided", + // https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude#regions + region: this.options.vertexRegion ?? "us-east5", + googleAuth: new GoogleAuth({ + scopes: ["https://www.googleapis.com/auth/cloud-platform"], + credentials: JSON.parse(this.options.vertexJsonCredentials), + }), + }) + } else if (this.options.vertexKeyFile) { + this.anthropicClient = new AnthropicVertex({ + projectId: this.options.vertexProjectId ?? "not-provided", + // https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude#regions + region: this.options.vertexRegion ?? "us-east5", + googleAuth: new GoogleAuth({ + scopes: ["https://www.googleapis.com/auth/cloud-platform"], + keyFile: this.options.vertexKeyFile, + }), + }) + } else { + this.anthropicClient = new AnthropicVertex({ + projectId: this.options.vertexProjectId ?? "not-provided", + // https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude#regions + region: this.options.vertexRegion ?? "us-east5", + }) + } - this.geminiClient = new VertexAI({ - project: this.options.vertexProjectId ?? "not-provided", - location: this.options.vertexRegion ?? "us-east5", - }) + if (this.options.vertexJsonCredentials) { + this.geminiClient = new VertexAI({ + project: this.options.vertexProjectId ?? "not-provided", + location: this.options.vertexRegion ?? "us-east5", + googleAuthOptions: { + credentials: JSON.parse(this.options.vertexJsonCredentials), + }, + }) + } else if (this.options.vertexKeyFile) { + this.geminiClient = new VertexAI({ + project: this.options.vertexProjectId ?? "not-provided", + location: this.options.vertexRegion ?? "us-east5", + googleAuthOptions: { + keyFile: this.options.vertexKeyFile, + }, + }) + } else { + this.geminiClient = new VertexAI({ + project: this.options.vertexProjectId ?? "not-provided", + location: this.options.vertexRegion ?? "us-east5", + }) + } } private formatMessageForCache(message: Anthropic.Messages.MessageParam, shouldCache: boolean): VertexMessage { diff --git a/src/core/webview/ClineProvider.ts b/src/core/webview/ClineProvider.ts index ccb50fe4400..8d3515e426f 100644 --- a/src/core/webview/ClineProvider.ts +++ b/src/core/webview/ClineProvider.ts @@ -1659,6 +1659,8 @@ export class ClineProvider implements vscode.WebviewViewProvider { awsUseCrossRegionInference, awsProfile, awsUseProfile, + vertexKeyFile, + vertexJsonCredentials, vertexProjectId, vertexRegion, openAiBaseUrl, @@ -1710,6 +1712,8 @@ export class ClineProvider implements vscode.WebviewViewProvider { this.updateGlobalState("awsUseCrossRegionInference", awsUseCrossRegionInference), this.updateGlobalState("awsProfile", awsProfile), this.updateGlobalState("awsUseProfile", awsUseProfile), + this.updateGlobalState("vertexKeyFile", vertexKeyFile), + this.updateGlobalState("vertexJsonCredentials", vertexJsonCredentials), this.updateGlobalState("vertexProjectId", vertexProjectId), this.updateGlobalState("vertexRegion", vertexRegion), this.updateGlobalState("openAiBaseUrl", openAiBaseUrl), @@ -2160,6 +2164,8 @@ export class ClineProvider implements vscode.WebviewViewProvider { awsUseCrossRegionInference, awsProfile, awsUseProfile, + vertexKeyFile, + vertexJsonCredentials, vertexProjectId, vertexRegion, openAiBaseUrl, @@ -2248,6 +2254,8 @@ export class ClineProvider implements vscode.WebviewViewProvider { this.getGlobalState("awsUseCrossRegionInference") as Promise, this.getGlobalState("awsProfile") as Promise, this.getGlobalState("awsUseProfile") as Promise, + this.getGlobalState("vertexKeyFile") as Promise, + this.getGlobalState("vertexJsonCredentials") as Promise, this.getGlobalState("vertexProjectId") as Promise, this.getGlobalState("vertexRegion") as Promise, this.getGlobalState("openAiBaseUrl") as Promise, @@ -2353,6 +2361,8 @@ export class ClineProvider implements vscode.WebviewViewProvider { awsUseCrossRegionInference, awsProfile, awsUseProfile, + vertexKeyFile, + vertexJsonCredentials, vertexProjectId, vertexRegion, openAiBaseUrl, diff --git a/src/shared/api.ts b/src/shared/api.ts index 2ce7162640a..b670d924979 100644 --- a/src/shared/api.ts +++ b/src/shared/api.ts @@ -40,6 +40,8 @@ export interface ApiHandlerOptions { awsUseProfile?: boolean vertexProjectId?: string vertexRegion?: string + vertexKeyFile?: string + vertexJsonCredentials?: string openAiBaseUrl?: string openAiApiKey?: string openAiModelId?: string diff --git a/src/shared/globalState.ts b/src/shared/globalState.ts index ccb87bd315b..dbe6b3c7ff5 100644 --- a/src/shared/globalState.ts +++ b/src/shared/globalState.ts @@ -22,6 +22,8 @@ export type GlobalStateKey = | "awsUseCrossRegionInference" | "awsProfile" | "awsUseProfile" + | "vertexKeyFile" + | "vertexJsonCredentials" | "vertexProjectId" | "vertexRegion" | "lastShownAnnouncementId" diff --git a/webview-ui/src/components/settings/ApiOptions.tsx b/webview-ui/src/components/settings/ApiOptions.tsx index 4466d12aaa8..2f557c294bc 100644 --- a/webview-ui/src/components/settings/ApiOptions.tsx +++ b/webview-ui/src/components/settings/ApiOptions.tsx @@ -604,6 +604,20 @@ const ApiOptions = ({ {selectedProvider === "vertex" && (
+ + Google Cloud Credentials + + + Google Cloud Key File Path + {"2) install the Google Cloud CLI › configure Application Default Credentials."} + + {"3) or create a service account with credentials."} +

)}