fix: handle global region correctly for Vertex AI Claude models

- Add custom baseURL for global region to use aiplatform.googleapis.com
- Prevents incorrect hostname construction (global-aiplatform.googleapis.com)
- Add comprehensive test coverage for global region scenarios

Fixes #8571
This commit is contained in:
Roo Code 2025-10-08 22:37:58 +00:00
parent eeaafef786
commit e0fd25ae42
2 changed files with 58 additions and 1 deletions

View file

@ -62,8 +62,58 @@ describe("VertexHandler", () => {
expect(AnthropicVertex).toHaveBeenCalledWith({
projectId: "test-project",
region: "us-central1",
baseURL: undefined,
})
})
it("should use custom baseURL for global region", () => {
handler = new AnthropicVertexHandler({
apiModelId: "claude-3-5-sonnet-v2@20241022",
vertexProjectId: "test-project",
vertexRegion: "global",
})
expect(AnthropicVertex).toHaveBeenCalledWith({
projectId: "test-project",
region: "global",
baseURL: "https://aiplatform.googleapis.com/v1",
})
})
it("should use custom baseURL for global region with JSON credentials", () => {
const mockCredentials = JSON.stringify({ type: "service_account", project_id: "test" })
handler = new AnthropicVertexHandler({
apiModelId: "claude-3-5-sonnet-v2@20241022",
vertexProjectId: "test-project",
vertexRegion: "global",
vertexJsonCredentials: mockCredentials,
})
expect(AnthropicVertex).toHaveBeenCalledWith(
expect.objectContaining({
projectId: "test-project",
region: "global",
baseURL: "https://aiplatform.googleapis.com/v1",
}),
)
})
it("should use custom baseURL for global region with key file", () => {
handler = new AnthropicVertexHandler({
apiModelId: "claude-3-5-sonnet-v2@20241022",
vertexProjectId: "test-project",
vertexRegion: "global",
vertexKeyFile: "/path/to/keyfile.json",
})
expect(AnthropicVertex).toHaveBeenCalledWith(
expect.objectContaining({
projectId: "test-project",
region: "global",
baseURL: "https://aiplatform.googleapis.com/v1",
}),
)
})
})
describe("createMessage", () => {

View file

@ -34,10 +34,16 @@ export class AnthropicVertexHandler extends BaseProvider implements SingleComple
const projectId = this.options.vertexProjectId ?? "not-provided"
const region = this.options.vertexRegion ?? "us-east5"
// For the "global" region, we need to use a custom base URL
// The default would be "global-aiplatform.googleapis.com" which is incorrect
// The correct endpoint for global is "aiplatform.googleapis.com"
const baseURL = region === "global" ? `https://aiplatform.googleapis.com/v1` : undefined // Let the SDK use its default for other regions
if (this.options.vertexJsonCredentials) {
this.client = new AnthropicVertex({
projectId,
region,
baseURL,
googleAuth: new GoogleAuth({
scopes: ["https://www.googleapis.com/auth/cloud-platform"],
credentials: safeJsonParse<JWTInput>(this.options.vertexJsonCredentials, undefined),
@ -47,13 +53,14 @@ export class AnthropicVertexHandler extends BaseProvider implements SingleComple
this.client = new AnthropicVertex({
projectId,
region,
baseURL,
googleAuth: new GoogleAuth({
scopes: ["https://www.googleapis.com/auth/cloud-platform"],
keyFile: this.options.vertexKeyFile,
}),
})
} else {
this.client = new AnthropicVertex({ projectId, region })
this.client = new AnthropicVertex({ projectId, region, baseURL })
}
}