|
| 1 | +// npx vitest run src/api/providers/__tests__/bedrock-global-inference.spec.ts |
| 2 | + |
| 3 | +import { AwsBedrockHandler } from "../bedrock" |
| 4 | +import { BedrockRuntimeClient, ConverseStreamCommand } from "@aws-sdk/client-bedrock-runtime" |
| 5 | +import { logger } from "../../../utils/logging" |
| 6 | +import type { ProviderSettings } from "@roo-code/types" |
| 7 | + |
| 8 | +// Mock AWS SDK modules |
| 9 | +vitest.mock("@aws-sdk/client-bedrock-runtime", () => { |
| 10 | + const mockSend = vi.fn().mockResolvedValue({ |
| 11 | + stream: (async function* () { |
| 12 | + yield { |
| 13 | + contentBlockStart: { |
| 14 | + start: { text: "Test response" }, |
| 15 | + }, |
| 16 | + } |
| 17 | + yield { |
| 18 | + contentBlockDelta: { |
| 19 | + delta: { text: " from Claude" }, |
| 20 | + }, |
| 21 | + } |
| 22 | + yield { |
| 23 | + messageStop: {}, |
| 24 | + } |
| 25 | + })(), |
| 26 | + }) |
| 27 | + |
| 28 | + return { |
| 29 | + BedrockRuntimeClient: vi.fn().mockImplementation(() => ({ |
| 30 | + send: mockSend, |
| 31 | + })), |
| 32 | + ConverseStreamCommand: vi.fn(), |
| 33 | + ConverseCommand: vi.fn(), |
| 34 | + } |
| 35 | +}) |
| 36 | + |
| 37 | +vitest.mock("../../../utils/logging") |
| 38 | + |
| 39 | +describe("AwsBedrockHandler - Global Inference Profile Support", () => { |
| 40 | + let handler: AwsBedrockHandler |
| 41 | + let mockSend: any |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + vi.clearAllMocks() |
| 45 | + mockSend = vi.fn().mockResolvedValue({ |
| 46 | + stream: (async function* () { |
| 47 | + yield { |
| 48 | + contentBlockStart: { |
| 49 | + start: { text: "Test response" }, |
| 50 | + }, |
| 51 | + } |
| 52 | + yield { |
| 53 | + contentBlockDelta: { |
| 54 | + delta: { text: " from Claude" }, |
| 55 | + }, |
| 56 | + } |
| 57 | + yield { |
| 58 | + messageStop: {}, |
| 59 | + } |
| 60 | + })(), |
| 61 | + }) |
| 62 | + ;(BedrockRuntimeClient as any).mockImplementation(() => ({ |
| 63 | + send: mockSend, |
| 64 | + })) |
| 65 | + }) |
| 66 | + |
| 67 | + describe("Global Inference Profile ARN Support", () => { |
| 68 | + it("should detect Claude Sonnet 4.5 global inference profile ARN", () => { |
| 69 | + const options: ProviderSettings = { |
| 70 | + apiProvider: "bedrock", |
| 71 | + awsRegion: "us-east-1", |
| 72 | + awsCustomArn: |
| 73 | + "arn:aws:bedrock:us-east-1:148761681080:inference-profile/global.anthropic.claude-sonnet-4-5-20250929-v1:0", |
| 74 | + awsAccessKey: "test-key", |
| 75 | + awsSecretKey: "test-secret", |
| 76 | + } |
| 77 | + |
| 78 | + handler = new AwsBedrockHandler(options) |
| 79 | + const model = handler.getModel() |
| 80 | + |
| 81 | + // Should recognize the ARN and provide appropriate model info |
| 82 | + expect(model.id).toBe( |
| 83 | + "arn:aws:bedrock:us-east-1:148761681080:inference-profile/global.anthropic.claude-sonnet-4-5-20250929-v1:0", |
| 84 | + ) |
| 85 | + expect(model.info).toBeDefined() |
| 86 | + expect(model.info.supportsReasoningBudget).toBe(true) |
| 87 | + expect(model.info.supportsPromptCache).toBe(true) |
| 88 | + expect(model.info.supportsImages).toBe(true) |
| 89 | + }) |
| 90 | + |
| 91 | + it("should enable 1M context for global inference profile when awsBedrock1MContext is true", async () => { |
| 92 | + const options: ProviderSettings = { |
| 93 | + apiProvider: "bedrock", |
| 94 | + awsRegion: "us-east-1", |
| 95 | + awsCustomArn: |
| 96 | + "arn:aws:bedrock:us-east-1:148761681080:inference-profile/global.anthropic.claude-sonnet-4-5-20250929-v1:0", |
| 97 | + awsBedrock1MContext: true, |
| 98 | + awsAccessKey: "test-key", |
| 99 | + awsSecretKey: "test-secret", |
| 100 | + } |
| 101 | + |
| 102 | + handler = new AwsBedrockHandler(options) |
| 103 | + |
| 104 | + const messages = [{ role: "user" as const, content: "Test message" }] |
| 105 | + const stream = handler.createMessage("System prompt", messages) |
| 106 | + |
| 107 | + // Consume the stream |
| 108 | + const chunks = [] |
| 109 | + for await (const chunk of stream) { |
| 110 | + chunks.push(chunk) |
| 111 | + } |
| 112 | + |
| 113 | + // Check that the command was called |
| 114 | + expect(mockSend).toHaveBeenCalled() |
| 115 | + expect(ConverseStreamCommand).toHaveBeenCalled() |
| 116 | + |
| 117 | + // Get the payload from the ConverseStreamCommand constructor |
| 118 | + const commandPayload = (ConverseStreamCommand as any).mock.calls[0][0] |
| 119 | + expect(commandPayload).toBeDefined() |
| 120 | + expect(commandPayload.additionalModelRequestFields).toBeDefined() |
| 121 | + expect(commandPayload.additionalModelRequestFields.anthropic_beta).toContain("context-1m-2025-08-07") |
| 122 | + }) |
| 123 | + |
| 124 | + it("should enable thinking/reasoning for global inference profile", async () => { |
| 125 | + const options: ProviderSettings = { |
| 126 | + apiProvider: "bedrock", |
| 127 | + awsRegion: "us-east-1", |
| 128 | + awsCustomArn: |
| 129 | + "arn:aws:bedrock:us-east-1:148761681080:inference-profile/global.anthropic.claude-sonnet-4-5-20250929-v1:0", |
| 130 | + enableReasoningEffort: true, |
| 131 | + awsAccessKey: "test-key", |
| 132 | + awsSecretKey: "test-secret", |
| 133 | + } |
| 134 | + |
| 135 | + handler = new AwsBedrockHandler(options) |
| 136 | + |
| 137 | + const messages = [{ role: "user" as const, content: "Test message" }] |
| 138 | + const metadata = { |
| 139 | + taskId: "test-task-id", |
| 140 | + thinking: { |
| 141 | + enabled: true, |
| 142 | + maxThinkingTokens: 8192, |
| 143 | + }, |
| 144 | + } |
| 145 | + |
| 146 | + const stream = handler.createMessage("System prompt", messages, metadata) |
| 147 | + |
| 148 | + // Consume the stream |
| 149 | + const chunks = [] |
| 150 | + for await (const chunk of stream) { |
| 151 | + chunks.push(chunk) |
| 152 | + } |
| 153 | + |
| 154 | + // Check that thinking was enabled |
| 155 | + expect(logger.info).toHaveBeenCalledWith( |
| 156 | + expect.stringContaining("Extended thinking enabled"), |
| 157 | + expect.objectContaining({ |
| 158 | + ctx: "bedrock", |
| 159 | + thinking: expect.objectContaining({ |
| 160 | + type: "enabled", |
| 161 | + budget_tokens: 8192, |
| 162 | + }), |
| 163 | + }), |
| 164 | + ) |
| 165 | + }) |
| 166 | + |
| 167 | + it("should handle various Claude 4.5 ARN patterns", () => { |
| 168 | + const testCases = [ |
| 169 | + "arn:aws:bedrock:us-east-1:148761681080:inference-profile/global.anthropic.claude-sonnet-4-5-20250929-v1:0", |
| 170 | + "arn:aws:bedrock:eu-west-1:123456789012:inference-profile/anthropic.claude-sonnet-4-5-20250929-v1:0", |
| 171 | + "arn:aws:bedrock:ap-southeast-1:987654321098:foundation-model/anthropic.claude-sonnet-4.5-v1:0", |
| 172 | + ] |
| 173 | + |
| 174 | + testCases.forEach((arn) => { |
| 175 | + const options: ProviderSettings = { |
| 176 | + apiProvider: "bedrock", |
| 177 | + awsRegion: "us-east-1", |
| 178 | + awsCustomArn: arn, |
| 179 | + awsAccessKey: "test-key", |
| 180 | + awsSecretKey: "test-secret", |
| 181 | + } |
| 182 | + |
| 183 | + handler = new AwsBedrockHandler(options) |
| 184 | + const model = handler.getModel() |
| 185 | + |
| 186 | + expect(model.info.supportsReasoningBudget).toBe(true) |
| 187 | + expect(model.info.supportsPromptCache).toBe(true) |
| 188 | + }) |
| 189 | + }) |
| 190 | + |
| 191 | + it("should not enable thinking for non-Claude-4.5 custom ARNs", () => { |
| 192 | + const options: ProviderSettings = { |
| 193 | + apiProvider: "bedrock", |
| 194 | + awsRegion: "us-east-1", |
| 195 | + awsCustomArn: |
| 196 | + "arn:aws:bedrock:us-east-1:123456789012:foundation-model/anthropic.claude-3-haiku-20240307-v1:0", |
| 197 | + awsAccessKey: "test-key", |
| 198 | + awsSecretKey: "test-secret", |
| 199 | + } |
| 200 | + |
| 201 | + handler = new AwsBedrockHandler(options) |
| 202 | + const model = handler.getModel() |
| 203 | + |
| 204 | + // Should not have reasoning budget support for non-Claude-4.5 models |
| 205 | + expect(model.info.supportsReasoningBudget).toBeFalsy() |
| 206 | + }) |
| 207 | + }) |
| 208 | + |
| 209 | + describe("ARN Parsing with Global Inference Profile", () => { |
| 210 | + it("should correctly parse global inference profile ARN", () => { |
| 211 | + const handler = new AwsBedrockHandler({ |
| 212 | + apiProvider: "bedrock", |
| 213 | + awsRegion: "us-east-1", |
| 214 | + awsAccessKey: "test-key", |
| 215 | + awsSecretKey: "test-secret", |
| 216 | + }) |
| 217 | + |
| 218 | + const parseArn = (handler as any).parseArn.bind(handler) |
| 219 | + const result = parseArn( |
| 220 | + "arn:aws:bedrock:us-east-1:148761681080:inference-profile/global.anthropic.claude-sonnet-4-5-20250929-v1:0", |
| 221 | + ) |
| 222 | + |
| 223 | + expect(result.isValid).toBe(true) |
| 224 | + expect(result.region).toBe("us-east-1") |
| 225 | + expect(result.modelType).toBe("inference-profile") |
| 226 | + expect(result.modelId).toContain("anthropic.claude-sonnet-4-5-20250929") |
| 227 | + }) |
| 228 | + }) |
| 229 | +}) |
0 commit comments