MCPcopy
hub / github.com/coder/mux / applyCacheControl

Function applyCacheControl

src/common/utils/ai/cacheStrategy.ts:92–116  ·  view source on GitHub ↗
(
  messages: ModelMessage[],
  modelString: string,
  cacheTtl?: AnthropicCacheTtl | null
)

Source from the content-addressed store, hash-verified

90 * We add cache_control to the last content part of the last message.
91 */
92export 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.

Callers 3

Calls 2

supportsAnthropicCacheFunction · 0.85

Tested by

no test coverage detected