|
| 1 | +// npx vitest run src/api/providers/__tests__/bedrock-manual-prompt-cache.spec.ts |
| 2 | + |
| 3 | +import { AwsBedrockHandler } from "../bedrock" |
| 4 | +import { ProviderSettings } from "@roo-code/types" |
| 5 | + |
| 6 | +// Mock AWS SDK |
| 7 | +vi.mock("@aws-sdk/client-bedrock-runtime") |
| 8 | +vi.mock("@aws-sdk/client-bedrock") |
| 9 | +vi.mock("../../../utils/logging") |
| 10 | + |
| 11 | +describe("AwsBedrockHandler - Manual Prompt Caching", () => { |
| 12 | + let handler: AwsBedrockHandler |
| 13 | + let mockOptions: ProviderSettings |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + vi.clearAllMocks() |
| 17 | + |
| 18 | + mockOptions = { |
| 19 | + apiProvider: "bedrock", |
| 20 | + apiModelId: "anthropic.claude-3-5-sonnet-20241022-v2:0", |
| 21 | + awsAccessKey: "test-access-key", |
| 22 | + awsSecretKey: "test-secret-key", |
| 23 | + awsRegion: "us-east-1", |
| 24 | + } as ProviderSettings |
| 25 | + }) |
| 26 | + |
| 27 | + describe("Manual Prompt Cache Configuration", () => { |
| 28 | + it("should enable prompt caching when awsManualPromptCacheEnabled is true", () => { |
| 29 | + const options = { |
| 30 | + ...mockOptions, |
| 31 | + awsManualPromptCacheEnabled: true, |
| 32 | + } |
| 33 | + |
| 34 | + handler = new AwsBedrockHandler(options) |
| 35 | + const modelConfig = handler.getModel() |
| 36 | + |
| 37 | + // Access private method for testing |
| 38 | + const supportsCache = (handler as any).supportsAwsPromptCache(modelConfig) |
| 39 | + expect(supportsCache).toBe(true) |
| 40 | + }) |
| 41 | + |
| 42 | + it("should use default manual cache configuration", () => { |
| 43 | + const options = { |
| 44 | + ...mockOptions, |
| 45 | + awsManualPromptCacheEnabled: true, |
| 46 | + } |
| 47 | + |
| 48 | + handler = new AwsBedrockHandler(options) |
| 49 | + |
| 50 | + // Access private method for testing |
| 51 | + const cacheConfig = (handler as any).getManualCacheConfig() |
| 52 | + |
| 53 | + expect(cacheConfig).toEqual({ |
| 54 | + maxCachePoints: 1, |
| 55 | + minTokensPerCachePoint: 1024, |
| 56 | + cachableFields: ["system"], |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + it("should use custom manual cache configuration", () => { |
| 61 | + const options = { |
| 62 | + ...mockOptions, |
| 63 | + awsManualPromptCacheEnabled: true, |
| 64 | + awsManualMaxCachePoints: 4, |
| 65 | + awsManualMinTokensPerCachePoint: 2048, |
| 66 | + awsManualCachableFields: ["system", "messages", "tools"] as ("system" | "messages" | "tools")[], |
| 67 | + } |
| 68 | + |
| 69 | + handler = new AwsBedrockHandler(options) |
| 70 | + |
| 71 | + // Access private method for testing |
| 72 | + const cacheConfig = (handler as any).getManualCacheConfig() |
| 73 | + |
| 74 | + expect(cacheConfig).toEqual({ |
| 75 | + maxCachePoints: 4, |
| 76 | + minTokensPerCachePoint: 2048, |
| 77 | + cachableFields: ["system", "messages", "tools"], |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + it("should fall back to automatic detection when manual caching is disabled", () => { |
| 82 | + const options = { |
| 83 | + ...mockOptions, |
| 84 | + awsManualPromptCacheEnabled: false, |
| 85 | + } |
| 86 | + |
| 87 | + handler = new AwsBedrockHandler(options) |
| 88 | + const modelConfig = handler.getModel() |
| 89 | + |
| 90 | + // Access private method for testing |
| 91 | + const supportsCache = (handler as any).supportsAwsPromptCache(modelConfig) |
| 92 | + |
| 93 | + // Should use automatic detection based on model capabilities |
| 94 | + expect(supportsCache).toBe(true) // Claude 3.5 Sonnet supports prompt cache |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + describe("Cache Configuration in Message Conversion", () => { |
| 99 | + it("should use manual cache configuration when enabled", async () => { |
| 100 | + const options = { |
| 101 | + ...mockOptions, |
| 102 | + awsManualPromptCacheEnabled: true, |
| 103 | + awsManualMaxCachePoints: 2, |
| 104 | + awsManualMinTokensPerCachePoint: 512, |
| 105 | + awsManualCachableFields: ["system", "messages"] as ("system" | "messages" | "tools")[], |
| 106 | + } |
| 107 | + |
| 108 | + handler = new AwsBedrockHandler(options) |
| 109 | + |
| 110 | + // Test the convertToBedrockConverseMessages method |
| 111 | + const messages = [{ role: "user", content: "Hello" }] |
| 112 | + |
| 113 | + // Access private method for testing |
| 114 | + const result = (handler as any).convertToBedrockConverseMessages( |
| 115 | + messages, |
| 116 | + "You are a helpful assistant", |
| 117 | + true, // usePromptCache |
| 118 | + { maxTokens: 8192, contextWindow: 200000 }, |
| 119 | + "test-conversation", |
| 120 | + ) |
| 121 | + |
| 122 | + expect(result).toBeDefined() |
| 123 | + expect(result.system).toBeDefined() |
| 124 | + expect(result.messages).toBeDefined() |
| 125 | + }) |
| 126 | + |
| 127 | + it("should use automatic model configuration when manual caching is disabled", async () => { |
| 128 | + const options = { |
| 129 | + ...mockOptions, |
| 130 | + awsManualPromptCacheEnabled: false, |
| 131 | + } |
| 132 | + |
| 133 | + handler = new AwsBedrockHandler(options) |
| 134 | + |
| 135 | + // Test the convertToBedrockConverseMessages method |
| 136 | + const messages = [{ role: "user", content: "Hello" }] |
| 137 | + |
| 138 | + // Access private method for testing |
| 139 | + const result = (handler as any).convertToBedrockConverseMessages( |
| 140 | + messages, |
| 141 | + "You are a helpful assistant", |
| 142 | + true, // usePromptCache |
| 143 | + { |
| 144 | + maxTokens: 8192, |
| 145 | + contextWindow: 200000, |
| 146 | + supportsPromptCache: true, |
| 147 | + maxCachePoints: 4, |
| 148 | + minTokensPerCachePoint: 1024, |
| 149 | + cachableFields: ["system", "messages", "tools"], |
| 150 | + }, |
| 151 | + "test-conversation", |
| 152 | + ) |
| 153 | + |
| 154 | + expect(result).toBeDefined() |
| 155 | + expect(result.system).toBeDefined() |
| 156 | + expect(result.messages).toBeDefined() |
| 157 | + }) |
| 158 | + }) |
| 159 | + |
| 160 | + describe("Application Inference Profile Integration", () => { |
| 161 | + it("should enable manual caching for Application Inference Profiles", () => { |
| 162 | + const options = { |
| 163 | + ...mockOptions, |
| 164 | + awsCustomArn: "arn:aws:bedrock:us-east-1:123456789012:inference-profile/my-custom-profile", |
| 165 | + awsManualPromptCacheEnabled: true, |
| 166 | + awsManualMaxCachePoints: 3, |
| 167 | + } |
| 168 | + |
| 169 | + handler = new AwsBedrockHandler(options) |
| 170 | + const modelConfig = handler.getModel() |
| 171 | + |
| 172 | + // Should support caching even if the underlying model detection fails |
| 173 | + const supportsCache = (handler as any).supportsAwsPromptCache(modelConfig) |
| 174 | + expect(supportsCache).toBe(true) |
| 175 | + }) |
| 176 | + |
| 177 | + it("should work with both automatic and manual configuration", () => { |
| 178 | + const options = { |
| 179 | + ...mockOptions, |
| 180 | + awsCustomArn: "arn:aws:bedrock:us-east-1:123456789012:inference-profile/claude-profile", |
| 181 | + awsManualPromptCacheEnabled: true, |
| 182 | + } |
| 183 | + |
| 184 | + handler = new AwsBedrockHandler(options) |
| 185 | + |
| 186 | + // Manual configuration should take precedence |
| 187 | + const cacheConfig = (handler as any).getManualCacheConfig() |
| 188 | + expect(cacheConfig.maxCachePoints).toBe(1) // default manual value |
| 189 | + }) |
| 190 | + }) |
| 191 | + |
| 192 | + describe("Edge Cases", () => { |
| 193 | + it("should handle missing manual cache configuration gracefully", () => { |
| 194 | + const options = { |
| 195 | + ...mockOptions, |
| 196 | + awsManualPromptCacheEnabled: true, |
| 197 | + // No manual cache settings provided |
| 198 | + } |
| 199 | + |
| 200 | + handler = new AwsBedrockHandler(options) |
| 201 | + |
| 202 | + const cacheConfig = (handler as any).getManualCacheConfig() |
| 203 | + |
| 204 | + // Should use defaults |
| 205 | + expect(cacheConfig).toEqual({ |
| 206 | + maxCachePoints: 1, |
| 207 | + minTokensPerCachePoint: 1024, |
| 208 | + cachableFields: ["system"], |
| 209 | + }) |
| 210 | + }) |
| 211 | + |
| 212 | + it("should validate cache points within bounds", () => { |
| 213 | + const options = { |
| 214 | + ...mockOptions, |
| 215 | + awsManualPromptCacheEnabled: true, |
| 216 | + awsManualMaxCachePoints: 10, // Above max of 4 |
| 217 | + } |
| 218 | + |
| 219 | + handler = new AwsBedrockHandler(options) |
| 220 | + |
| 221 | + const cacheConfig = (handler as any).getManualCacheConfig() |
| 222 | + |
| 223 | + // Should be clamped to maximum allowed |
| 224 | + expect(cacheConfig.maxCachePoints).toBe(10) // Note: validation happens in UI, not here |
| 225 | + }) |
| 226 | + |
| 227 | + it("should handle empty cachable fields array", () => { |
| 228 | + const options = { |
| 229 | + ...mockOptions, |
| 230 | + awsManualPromptCacheEnabled: true, |
| 231 | + awsManualCachableFields: [], |
| 232 | + } |
| 233 | + |
| 234 | + handler = new AwsBedrockHandler(options) |
| 235 | + |
| 236 | + const cacheConfig = (handler as any).getManualCacheConfig() |
| 237 | + |
| 238 | + // Should fall back to default |
| 239 | + expect(cacheConfig.cachableFields).toEqual(["system"]) |
| 240 | + }) |
| 241 | + }) |
| 242 | +}) |
0 commit comments