Skip to content

Commit 9eab941

Browse files
authored
Merge pull request RooCodeInc#1550 from Smartsheet-JB-Brown/jbbrown/aws_custom_arn_for_intelligent_prompt_routing
Users need the ability to use custom ARNs (Amazon Resource Names) with AWS Bedrock for intelligent prompt routing.
2 parents d6a9e0f + 77186e8 commit 9eab941

File tree

7 files changed

+690
-33
lines changed

7 files changed

+690
-33
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { AwsBedrockHandler } from "../bedrock"
2+
import { ApiHandlerOptions } from "../../../shared/api"
3+
4+
// Mock the AWS SDK
5+
jest.mock("@aws-sdk/client-bedrock-runtime", () => {
6+
const mockSend = jest.fn().mockImplementation(() => {
7+
return Promise.resolve({
8+
output: new TextEncoder().encode(JSON.stringify({ content: "Test response" })),
9+
})
10+
})
11+
12+
return {
13+
BedrockRuntimeClient: jest.fn().mockImplementation(() => ({
14+
send: mockSend,
15+
config: {
16+
region: "us-east-1",
17+
},
18+
})),
19+
ConverseCommand: jest.fn(),
20+
ConverseStreamCommand: jest.fn(),
21+
}
22+
})
23+
24+
describe("AwsBedrockHandler with custom ARN", () => {
25+
const mockOptions: ApiHandlerOptions = {
26+
apiModelId: "custom-arn",
27+
awsCustomArn: "arn:aws:bedrock:us-east-1:123456789012:foundation-model/anthropic.claude-3-sonnet-20240229-v1:0",
28+
awsRegion: "us-east-1",
29+
}
30+
31+
it("should use the custom ARN as the model ID", async () => {
32+
const handler = new AwsBedrockHandler(mockOptions)
33+
const model = handler.getModel()
34+
35+
expect(model.id).toBe(mockOptions.awsCustomArn)
36+
expect(model.info).toHaveProperty("maxTokens")
37+
expect(model.info).toHaveProperty("contextWindow")
38+
expect(model.info).toHaveProperty("supportsPromptCache")
39+
})
40+
41+
it("should extract region from ARN and use it for client configuration", () => {
42+
// Test with matching region
43+
const handler1 = new AwsBedrockHandler(mockOptions)
44+
expect((handler1 as any).client.config.region).toBe("us-east-1")
45+
46+
// Test with mismatched region
47+
const mismatchOptions = {
48+
...mockOptions,
49+
awsRegion: "us-west-2",
50+
}
51+
const handler2 = new AwsBedrockHandler(mismatchOptions)
52+
// Should use the ARN region, not the provided region
53+
expect((handler2 as any).client.config.region).toBe("us-east-1")
54+
})
55+
56+
it("should validate ARN format", async () => {
57+
// Invalid ARN format
58+
const invalidOptions = {
59+
...mockOptions,
60+
awsCustomArn: "invalid-arn-format",
61+
}
62+
63+
const handler = new AwsBedrockHandler(invalidOptions)
64+
65+
// completePrompt should throw an error for invalid ARN
66+
await expect(handler.completePrompt("test")).rejects.toThrow("Invalid ARN format")
67+
})
68+
69+
it("should complete a prompt successfully with valid ARN", async () => {
70+
const handler = new AwsBedrockHandler(mockOptions)
71+
const response = await handler.completePrompt("test prompt")
72+
73+
expect(response).toBe("Test response")
74+
})
75+
})

src/api/providers/__tests__/bedrock.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,5 +315,34 @@ describe("AwsBedrockHandler", () => {
315315
expect(modelInfo.info.maxTokens).toBe(5000)
316316
expect(modelInfo.info.contextWindow).toBe(128_000)
317317
})
318+
319+
it("should use custom ARN when provided", () => {
320+
const customArnHandler = new AwsBedrockHandler({
321+
apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
322+
awsAccessKey: "test-access-key",
323+
awsSecretKey: "test-secret-key",
324+
awsRegion: "us-east-1",
325+
awsCustomArn: "arn:aws:bedrock:us-east-1::foundation-model/custom-model",
326+
})
327+
const modelInfo = customArnHandler.getModel()
328+
expect(modelInfo.id).toBe("arn:aws:bedrock:us-east-1::foundation-model/custom-model")
329+
expect(modelInfo.info.maxTokens).toBe(4096)
330+
expect(modelInfo.info.contextWindow).toBe(128_000)
331+
expect(modelInfo.info.supportsPromptCache).toBe(false)
332+
})
333+
334+
it("should use default model when custom-arn is selected but no ARN is provided", () => {
335+
const customArnHandler = new AwsBedrockHandler({
336+
apiModelId: "custom-arn",
337+
awsAccessKey: "test-access-key",
338+
awsSecretKey: "test-secret-key",
339+
awsRegion: "us-east-1",
340+
// No awsCustomArn provided
341+
})
342+
const modelInfo = customArnHandler.getModel()
343+
// Should fall back to default model
344+
expect(modelInfo.id).not.toBe("custom-arn")
345+
expect(modelInfo.info).toBeDefined()
346+
})
318347
})
319348
})

0 commit comments

Comments
 (0)