@@ -18,30 +18,42 @@ export function convertToOpenAIChatMessages({
1818 const messages : OpenAIChatPrompt = [ ]
1919
2020 // Helper function to add a message with cacheControl if needed
21- const addMessage = ( message : any , cacheControl : boolean ) => {
22- if ( cacheControl ) {
21+ const addMessage = (
22+ message : any ,
23+ cacheEnabled : boolean ,
24+ cacheTtl ?: string ,
25+ ) => {
26+ if ( cacheEnabled ) {
2327 message . cache_enabled = true
2428 }
29+ if ( cacheTtl ) {
30+ message . cache_ttl = cacheTtl
31+ }
2532
2633 messages . push ( message )
2734 }
2835
2936 for ( const { role, content, providerMetadata } of prompt ) {
30- const anthropicCacheControl = Boolean (
31- providerMetadata ?. anthropic ?. cacheControl ,
32- )
37+ const anthropicCacheControl = providerMetadata ?. anthropic ?. cacheControl
38+ const cacheEnabled = Boolean ( anthropicCacheControl )
39+ const cacheTtl =
40+ typeof anthropicCacheControl === "object" &&
41+ anthropicCacheControl !== null
42+ ? ( anthropicCacheControl as { ttl ?: string } ) . ttl
43+ : undefined
3344
3445 switch ( role ) {
3546 case "system" : {
36- addMessage ( { role : "system" , content } , anthropicCacheControl )
47+ addMessage ( { role : "system" , content } , cacheEnabled , cacheTtl )
3748 break
3849 }
3950
4051 case "user" : {
4152 if ( content . length === 1 && content [ 0 ] . type === "text" ) {
4253 addMessage (
4354 { role : "user" , content : content [ 0 ] . text } ,
44- anthropicCacheControl ,
55+ cacheEnabled ,
56+ cacheTtl ,
4557 )
4658 break
4759 }
@@ -78,7 +90,8 @@ export function convertToOpenAIChatMessages({
7890 }
7991 } ) ,
8092 } ,
81- anthropicCacheControl ,
93+ cacheEnabled ,
94+ cacheTtl ,
8295 )
8396 break
8497 }
@@ -150,7 +163,8 @@ export function convertToOpenAIChatMessages({
150163 reasoning_details : reasoningDetails ,
151164 tool_calls : toolCalls . length > 0 ? toolCalls : undefined ,
152165 } ,
153- anthropicCacheControl ,
166+ cacheEnabled ,
167+ cacheTtl ,
154168 )
155169 break
156170 }
@@ -203,7 +217,8 @@ export function convertToOpenAIChatMessages({
203217 tool_call_id : toolResponse . toolCallId ,
204218 content : toolContent ,
205219 } ,
206- anthropicCacheControl ,
220+ cacheEnabled ,
221+ cacheTtl ,
207222 )
208223 }
209224 break
0 commit comments