Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 103 additions & 1 deletion src/api/providers/__tests__/bedrock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ vi.mock("@aws-sdk/client-bedrock-runtime", () => {

import { AwsBedrockHandler } from "../bedrock"
import { ConverseStreamCommand, BedrockRuntimeClient, ConverseCommand } from "@aws-sdk/client-bedrock-runtime"
import { BEDROCK_1M_CONTEXT_MODEL_IDS, BEDROCK_SERVICE_TIER_MODEL_IDS, bedrockModels, ApiProviderError } from "@roo-code/types"
import {
BEDROCK_1M_CONTEXT_MODEL_IDS,
BEDROCK_SERVICE_TIER_MODEL_IDS,
bedrockModels,
ApiProviderError,
} from "@roo-code/types"

import type { Anthropic } from "@anthropic-ai/sdk"

Expand Down Expand Up @@ -371,6 +376,103 @@ describe("AwsBedrockHandler", () => {
expect(result.modelId).toBe("ap.anthropic.claude-3-5-sonnet-20241022-v2:0") // Should be preserved as-is
})
})

describe("AWS GovCloud and China partition support", () => {
it("should parse AWS GovCloud ARNs (arn:aws-us-gov:bedrock:...)", () => {
const handler = new AwsBedrockHandler({
apiModelId: "test",
awsAccessKey: "test",
awsSecretKey: "test",
awsRegion: "us-gov-west-1",
})

const parseArn = (handler as any).parseArn.bind(handler)

const result = parseArn(
"arn:aws-us-gov:bedrock:us-gov-west-1:123456789012:inference-profile/us-gov.anthropic.claude-sonnet-4-5-20250929-v1:0",
)

expect(result.isValid).toBe(true)
expect(result.region).toBe("us-gov-west-1")
expect(result.modelType).toBe("inference-profile")
})

it("should parse AWS China ARNs (arn:aws-cn:bedrock:...)", () => {
const handler = new AwsBedrockHandler({
apiModelId: "test",
awsAccessKey: "test",
awsSecretKey: "test",
awsRegion: "cn-north-1",
})

const parseArn = (handler as any).parseArn.bind(handler)

const result = parseArn(
"arn:aws-cn:bedrock:cn-north-1:123456789012:inference-profile/anthropic.claude-3-sonnet-20240229-v1:0",
)

expect(result.isValid).toBe(true)
expect(result.region).toBe("cn-north-1")
expect(result.modelType).toBe("inference-profile")
})

it("should accept GovCloud custom ARN in handler constructor", () => {
const handler = new AwsBedrockHandler({
apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
awsAccessKey: "test-access-key",
awsSecretKey: "test-secret-key",
awsRegion: "us-gov-west-1",
awsCustomArn:
"arn:aws-us-gov:bedrock:us-gov-west-1:123456789012:inference-profile/us-gov.anthropic.claude-sonnet-4-5-20250929-v1:0",
})

// Should not throw and should return valid model info
const modelInfo = handler.getModel()
expect(modelInfo.id).toBe(
"arn:aws-us-gov:bedrock:us-gov-west-1:123456789012:inference-profile/us-gov.anthropic.claude-sonnet-4-5-20250929-v1:0",
)
expect(modelInfo.info).toBeDefined()
})

it("should accept China region custom ARN in handler constructor", () => {
const handler = new AwsBedrockHandler({
apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
awsAccessKey: "test-access-key",
awsSecretKey: "test-secret-key",
awsRegion: "cn-north-1",
awsCustomArn:
"arn:aws-cn:bedrock:cn-north-1:123456789012:inference-profile/anthropic.claude-3-sonnet-20240229-v1:0",
})

// Should not throw and should return valid model info
const modelInfo = handler.getModel()
expect(modelInfo.id).toBe(
"arn:aws-cn:bedrock:cn-north-1:123456789012:inference-profile/anthropic.claude-3-sonnet-20240229-v1:0",
)
expect(modelInfo.info).toBeDefined()
})

it("should detect region mismatch in GovCloud ARN", () => {
const handler = new AwsBedrockHandler({
apiModelId: "test",
awsAccessKey: "test",
awsSecretKey: "test",
awsRegion: "us-east-1",
})

const parseArn = (handler as any).parseArn.bind(handler)

// Region in ARN (us-gov-west-1) doesn't match provided region (us-east-1)
const result = parseArn(
"arn:aws-us-gov:bedrock:us-gov-west-1:123456789012:inference-profile/us-gov.anthropic.claude-sonnet-4-5-20250929-v1:0",
"us-east-1",
)

expect(result.isValid).toBe(true)
expect(result.region).toBe("us-gov-west-1")
expect(result.errorMessage).toContain("Region mismatch")
})
})
})

describe("image handling", () => {
Expand Down
8 changes: 6 additions & 2 deletions src/api/providers/bedrock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -928,22 +928,26 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
* represent literal characters in the AWS ARN format, not filesystem paths. This regex will function consistently across Windows,
* macOS, Linux, and any other operating system where JavaScript runs.
*
* Supports any AWS partition (aws, aws-us-gov, aws-cn, or future partitions).
* The partition is not captured since we don't need to use it.
*
* This matches ARNs like:
* - Foundation Model: arn:aws:bedrock:us-west-2::foundation-model/anthropic.claude-v2
* - GovCloud Inference Profile: arn:aws-us-gov:bedrock:us-gov-west-1:123456789012:inference-profile/us-gov.anthropic.claude-sonnet-4-5-20250929-v1:0
* - Prompt Router: arn:aws:bedrock:us-west-2:123456789012:prompt-router/anthropic-claude
* - Inference Profile: arn:aws:bedrock:us-west-2:123456789012:inference-profile/anthropic.claude-v2
* - Cross Region Inference Profile: arn:aws:bedrock:us-west-2:123456789012:inference-profile/us.anthropic.claude-3-5-sonnet-20241022-v2:0
* - Custom Model (Provisioned Throughput): arn:aws:bedrock:us-west-2:123456789012:provisioned-model/my-custom-model
* - Imported Model: arn:aws:bedrock:us-west-2:123456789012:imported-model/my-imported-model
*
* match[0] - The entire matched string
* match[1] - The region (e.g., "us-east-1")
* match[1] - The region (e.g., "us-east-1", "us-gov-west-1")
* match[2] - The account ID (can be empty string for AWS-managed resources)
* match[3] - The resource type (e.g., "foundation-model")
* match[4] - The resource ID (e.g., "anthropic.claude-3-sonnet-20240229-v1:0")
*/

const arnRegex = /^arn:aws:(?:bedrock|sagemaker):([^:]+):([^:]*):(?:([^\/]+)\/([\w\.\-:]+)|([^\/]+))$/
const arnRegex = /^arn:[^:]+:(?:bedrock|sagemaker):([^:]+):([^:]*):(?:([^\/]+)\/([\w\.\-:]+)|([^\/]+))$/
let match = arn.match(arnRegex)

if (match && match[1] && match[3] && match[4]) {
Expand Down
Loading