@@ -41,6 +41,13 @@ const hasCachePoint = (block: ContentBlock | SystemContentBlock): boolean => {
4141 )
4242}
4343
44+ const getCachePoint = ( block : ContentBlock | SystemContentBlock ) : { type ?: string ; ttl ?: string } | undefined => {
45+ if ( ! ( "cachePoint" in block ) || typeof block . cachePoint !== "object" || block . cachePoint === null ) {
46+ return undefined
47+ }
48+ return block . cachePoint as { type ?: string ; ttl ?: string }
49+ }
50+
4451// Create a mock object to store the last config passed to convertToBedrockConverseMessages
4552interface CacheConfig {
4653 modelInfo : any
@@ -176,6 +183,25 @@ describe("Cache Strategy", () => {
176183 expect ( hasCachePoint ( result . system [ 1 ] ) ) . toBe ( true )
177184 } )
178185
186+ it ( "adds configured ttl to system cache blocks" , ( ) => {
187+ const shortSystemPrompt = "You are a helpful assistant"
188+
189+ const config = createConfig ( {
190+ messages : [ { role : "user" , content : "Hello" } ] ,
191+ systemPrompt : shortSystemPrompt ,
192+ modelInfo : {
193+ ...defaultModelInfo ,
194+ minTokensPerCachePoint : 1 ,
195+ promptCacheTtl : "1h" ,
196+ } ,
197+ } )
198+
199+ const strategy = new MultiPointStrategy ( config )
200+ const result = strategy . determineOptimalCachePoints ( )
201+
202+ expect ( getCachePoint ( result . system [ 1 ] ) ) . toMatchObject ( { type : "default" , ttl : "1h" } )
203+ } )
204+
179205 it ( "does not add system cache block when system prompt is too short" , ( ) => {
180206 const shortSystemPrompt = "You are a helpful assistant"
181207
@@ -660,6 +686,11 @@ describe("Cache Strategy", () => {
660686 }
661687 }
662688
689+ const createWordMessage = ( role : "user" | "assistant" , wordCount : number ) => ( {
690+ role,
691+ content : Array . from ( { length : wordCount } , ( _ , index ) => `word${ index } ` ) . join ( " " ) ,
692+ } )
693+
663694 // Helper to log cache point placements for debugging
664695 const logPlacements = ( placements : any [ ] ) => {
665696 console . log (
@@ -669,6 +700,70 @@ describe("Cache Strategy", () => {
669700 }
670701
671702 describe ( "Example 1: Initial Cache Point Placement" , ( ) => {
703+ it ( "should place a cache point after a single first-turn user message that meets the threshold" , ( ) => {
704+ const messages = [ createWordMessage ( "user" , 60 ) ]
705+
706+ const config = createConfig ( {
707+ modelInfo : multiPointModelInfo ,
708+ systemPrompt : "" ,
709+ messages,
710+ usePromptCache : true ,
711+ } )
712+
713+ const strategy = new MultiPointStrategy ( config )
714+ const result = strategy . determineOptimalCachePoints ( )
715+
716+ expect ( result . messageCachePointPlacements ) . toEqual ( [
717+ expect . objectContaining ( {
718+ index : 0 ,
719+ type : "message" ,
720+ } ) ,
721+ ] )
722+ expect ( result . messageCachePointPlacements ?. [ 0 ] . tokensCovered ) . toBeGreaterThanOrEqual (
723+ multiPointModelInfo . minTokensPerCachePoint ,
724+ )
725+ expect ( result . messages [ 0 ] . content ?. some ( ( block ) => hasCachePoint ( block ) ) ) . toBe ( true )
726+ } )
727+
728+ it ( "should add a cache point for a single new user message after a previous placement" , ( ) => {
729+ const messages = [
730+ createWordMessage ( "user" , 60 ) ,
731+ createWordMessage ( "assistant" , 5 ) ,
732+ createWordMessage ( "user" , 60 ) ,
733+ ]
734+
735+ const previousCachePointPlacements : CachePointPlacement [ ] = [
736+ {
737+ index : 0 ,
738+ type : "message" ,
739+ tokensCovered : 88 ,
740+ } ,
741+ ]
742+
743+ const config = createConfig ( {
744+ modelInfo : multiPointModelInfo ,
745+ systemPrompt : "" ,
746+ messages,
747+ usePromptCache : true ,
748+ previousCachePointPlacements,
749+ } )
750+
751+ const strategy = new MultiPointStrategy ( config )
752+ const result = strategy . determineOptimalCachePoints ( )
753+
754+ expect ( result . messageCachePointPlacements ) . toEqual ( [
755+ expect . objectContaining ( {
756+ index : 0 ,
757+ type : "message" ,
758+ } ) ,
759+ expect . objectContaining ( {
760+ index : 2 ,
761+ type : "message" ,
762+ } ) ,
763+ ] )
764+ expect ( result . messages [ 2 ] . content ?. some ( ( block ) => hasCachePoint ( block ) ) ) . toBe ( true )
765+ } )
766+
672767 it ( "should place a cache point after the second user message" , ( ) => {
673768 // Create messages matching Example 1 from documentation
674769 const messages = [
0 commit comments