@@ -1327,4 +1327,99 @@ describe("AwsBedrockHandler", () => {
13271327 expect ( hasCachePoint ) . toBe ( false )
13281328 } )
13291329 } )
1330+
1331+ describe ( "prompt caching with custom ARN" , ( ) => {
1332+ beforeEach ( ( ) => {
1333+ mockConverseStreamCommand . mockReset ( )
1334+ } )
1335+
1336+ // System prompt must exceed minTokensPerCachePoint (1024) for cache points to be placed
1337+ const longSystemPrompt = "You are a helpful assistant. " . repeat ( 200 )
1338+ const messages : Anthropic . Messages . MessageParam [ ] = [ { role : "user" , content : "Hello" } ]
1339+
1340+ it ( "should enable prompt caching for custom ARN with recognized Claude model ID" , async ( ) => {
1341+ // Custom ARN containing a Claude model ID that matches the guess pattern
1342+ const customArnHandler = new AwsBedrockHandler ( {
1343+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
1344+ awsAccessKey : "test-access-key" ,
1345+ awsSecretKey : "test-secret-key" ,
1346+ awsRegion : "us-east-1" ,
1347+ awsCustomArn : "arn:aws:bedrock:us-east-1:123456789012:inference-profile/claude-3-5-sonnet-custom" ,
1348+ } )
1349+
1350+ const generator = customArnHandler . createMessage ( longSystemPrompt , messages )
1351+ await generator . next ( )
1352+
1353+ expect ( mockConverseStreamCommand ) . toHaveBeenCalled ( )
1354+ const commandArg = mockConverseStreamCommand . mock . calls [ 0 ] [ 0 ] as any
1355+
1356+ // System content should include a cachePoint since prompt caching should work
1357+ const systemBlocks = commandArg . system
1358+ const hasCachePoint = systemBlocks ?. some ( ( block : any ) => block . cachePoint !== undefined )
1359+ expect ( hasCachePoint ) . toBe ( true )
1360+ } )
1361+
1362+ it ( "should enable prompt caching for custom ARN with unrecognized model ID when user opts in" , async ( ) => {
1363+ // Custom ARN with an opaque model ID that doesn't match any pattern
1364+ const customArnHandler = new AwsBedrockHandler ( {
1365+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
1366+ awsAccessKey : "test-access-key" ,
1367+ awsSecretKey : "test-secret-key" ,
1368+ awsRegion : "us-east-1" ,
1369+ awsUsePromptCache : true ,
1370+ awsCustomArn : "arn:aws:bedrock:us-east-1:123456789012:provisioned-model/my-custom-model-xyz" ,
1371+ } )
1372+
1373+ const generator = customArnHandler . createMessage ( longSystemPrompt , messages )
1374+ await generator . next ( )
1375+
1376+ expect ( mockConverseStreamCommand ) . toHaveBeenCalled ( )
1377+ const commandArg = mockConverseStreamCommand . mock . calls [ 0 ] [ 0 ] as any
1378+
1379+ // System content should include a cachePoint since user explicitly enabled caching
1380+ const systemBlocks = commandArg . system
1381+ const hasCachePoint = systemBlocks ?. some ( ( block : any ) => block . cachePoint !== undefined )
1382+ expect ( hasCachePoint ) . toBe ( true )
1383+ } )
1384+
1385+ it ( "should disable prompt caching for custom ARN when user explicitly disables it" , async ( ) => {
1386+ const customArnHandler = new AwsBedrockHandler ( {
1387+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
1388+ awsAccessKey : "test-access-key" ,
1389+ awsSecretKey : "test-secret-key" ,
1390+ awsRegion : "us-east-1" ,
1391+ awsUsePromptCache : false ,
1392+ awsCustomArn : "arn:aws:bedrock:us-east-1:123456789012:inference-profile/claude-3-5-sonnet-custom" ,
1393+ } )
1394+
1395+ const generator = customArnHandler . createMessage ( longSystemPrompt , messages )
1396+ await generator . next ( )
1397+
1398+ expect ( mockConverseStreamCommand ) . toHaveBeenCalled ( )
1399+ const commandArg = mockConverseStreamCommand . mock . calls [ 0 ] [ 0 ] as any
1400+
1401+ // System content should NOT include cachePoint since user explicitly disabled caching
1402+ const systemBlocks = commandArg . system
1403+ const hasCachePoint = systemBlocks ?. some ( ( block : any ) => block . cachePoint !== undefined )
1404+ expect ( hasCachePoint ) . toBe ( false )
1405+ } )
1406+
1407+ it ( "should include cachableFields in guessModelInfoFromId for Claude patterns" , ( ) => {
1408+ // Test with a custom ARN that has a Claude model ID in it
1409+ const customArnHandler = new AwsBedrockHandler ( {
1410+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
1411+ awsAccessKey : "test-access-key" ,
1412+ awsSecretKey : "test-secret-key" ,
1413+ awsRegion : "us-east-1" ,
1414+ awsCustomArn : "arn:aws:bedrock:us-east-1:123456789012:inference-profile/claude-3-5-sonnet-custom" ,
1415+ } )
1416+
1417+ const modelConfig = customArnHandler . getModel ( )
1418+ expect ( modelConfig . info . supportsPromptCache ) . toBe ( true )
1419+ expect ( ( modelConfig . info as any ) . cachableFields ) . toBeDefined ( )
1420+ expect ( ( modelConfig . info as any ) . cachableFields ) . toContain ( "system" )
1421+ expect ( ( modelConfig . info as any ) . cachableFields ) . toContain ( "messages" )
1422+ expect ( ( modelConfig . info as any ) . cachableFields ) . toContain ( "tools" )
1423+ } )
1424+ } )
13301425} )
0 commit comments