( messages: ModelMessage[], modelString: string, cacheTtl?: AnthropicCacheTtl | null )
| 90 | * We add cache_control to the last content part of the last message. |
| 91 | */ |
| 92 | export function applyCacheControl( |
| 93 | messages: ModelMessage[], |
| 94 | modelString: string, |
| 95 | cacheTtl?: AnthropicCacheTtl | null |
| 96 | ): ModelMessage[] { |
| 97 | // Only apply cache control for Anthropic models |
| 98 | if (!supportsAnthropicCache(modelString)) { |
| 99 | return messages; |
| 100 | } |
| 101 | |
| 102 | // Need at least 1 message to add a cache breakpoint |
| 103 | if (messages.length < 1) { |
| 104 | return messages; |
| 105 | } |
| 106 | |
| 107 | // Add cache breakpoint at the last message |
| 108 | const cacheIndex = messages.length - 1; |
| 109 | |
| 110 | return messages.map((msg, index) => { |
| 111 | if (index === cacheIndex) { |
| 112 | return addCacheControlToLastContentPart(msg, cacheTtl); |
| 113 | } |
| 114 | return msg; |
| 115 | }); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Create a system message with cache control for Anthropic models. |
no test coverage detected