|
| 1 | +import { resolveProviderRequestCapabilities } from "./provider-attribution.js"; |
| 2 | + |
| 3 | +export type AnthropicServiceTier = "auto" | "standard_only"; |
| 4 | + |
| 5 | +export type AnthropicEphemeralCacheControl = { |
| 6 | + type: "ephemeral"; |
| 7 | + ttl?: "1h"; |
| 8 | +}; |
| 9 | + |
| 10 | +type AnthropicPayloadPolicyInput = { |
| 11 | + api?: string; |
| 12 | + baseUrl?: string; |
| 13 | + cacheRetention?: "short" | "long" | "none"; |
| 14 | + enableCacheControl?: boolean; |
| 15 | + provider?: string; |
| 16 | + serviceTier?: AnthropicServiceTier; |
| 17 | +}; |
| 18 | + |
| 19 | +export type AnthropicPayloadPolicy = { |
| 20 | + allowsServiceTier: boolean; |
| 21 | + cacheControl: AnthropicEphemeralCacheControl | undefined; |
| 22 | + serviceTier: AnthropicServiceTier | undefined; |
| 23 | +}; |
| 24 | + |
| 25 | +function resolveAnthropicEphemeralCacheControl( |
| 26 | + baseUrl: string | undefined, |
| 27 | + cacheRetention: AnthropicPayloadPolicyInput["cacheRetention"], |
| 28 | +): AnthropicEphemeralCacheControl | undefined { |
| 29 | + const retention = |
| 30 | + cacheRetention ?? (process.env.PI_CACHE_RETENTION === "long" ? "long" : "short"); |
| 31 | + if (retention === "none") { |
| 32 | + return undefined; |
| 33 | + } |
| 34 | + const ttl = |
| 35 | + retention === "long" && typeof baseUrl === "string" && baseUrl.includes("api.anthropic.com") |
| 36 | + ? "1h" |
| 37 | + : undefined; |
| 38 | + return { type: "ephemeral", ...(ttl ? { ttl } : {}) }; |
| 39 | +} |
| 40 | + |
| 41 | +function applyAnthropicCacheControlToSystem( |
| 42 | + system: unknown, |
| 43 | + cacheControl: AnthropicEphemeralCacheControl, |
| 44 | +): void { |
| 45 | + if (!Array.isArray(system)) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + for (const block of system) { |
| 50 | + if (!block || typeof block !== "object") { |
| 51 | + continue; |
| 52 | + } |
| 53 | + const record = block as Record<string, unknown>; |
| 54 | + if (record.type === "text" && record.cache_control === undefined) { |
| 55 | + record.cache_control = cacheControl; |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +function applyAnthropicCacheControlToMessages( |
| 61 | + messages: unknown, |
| 62 | + cacheControl: AnthropicEphemeralCacheControl, |
| 63 | +): void { |
| 64 | + if (!Array.isArray(messages) || messages.length === 0) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + const lastMessage = messages[messages.length - 1]; |
| 69 | + if (!lastMessage || typeof lastMessage !== "object") { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + const record = lastMessage as Record<string, unknown>; |
| 74 | + if (record.role !== "user") { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + const content = record.content; |
| 79 | + if (Array.isArray(content)) { |
| 80 | + const lastBlock = content[content.length - 1]; |
| 81 | + if (!lastBlock || typeof lastBlock !== "object") { |
| 82 | + return; |
| 83 | + } |
| 84 | + const lastBlockRecord = lastBlock as Record<string, unknown>; |
| 85 | + if ( |
| 86 | + lastBlockRecord.type === "text" || |
| 87 | + lastBlockRecord.type === "image" || |
| 88 | + lastBlockRecord.type === "tool_result" |
| 89 | + ) { |
| 90 | + lastBlockRecord.cache_control = cacheControl; |
| 91 | + } |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + if (typeof content === "string") { |
| 96 | + record.content = [ |
| 97 | + { |
| 98 | + type: "text", |
| 99 | + text: content, |
| 100 | + cache_control: cacheControl, |
| 101 | + }, |
| 102 | + ]; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +export function resolveAnthropicPayloadPolicy( |
| 107 | + input: AnthropicPayloadPolicyInput, |
| 108 | +): AnthropicPayloadPolicy { |
| 109 | + const capabilities = resolveProviderRequestCapabilities({ |
| 110 | + provider: input.provider, |
| 111 | + api: input.api, |
| 112 | + baseUrl: input.baseUrl, |
| 113 | + capability: "llm", |
| 114 | + transport: "stream", |
| 115 | + }); |
| 116 | + |
| 117 | + return { |
| 118 | + allowsServiceTier: capabilities.allowsAnthropicServiceTier, |
| 119 | + cacheControl: |
| 120 | + input.enableCacheControl === true |
| 121 | + ? resolveAnthropicEphemeralCacheControl(input.baseUrl, input.cacheRetention) |
| 122 | + : undefined, |
| 123 | + serviceTier: input.serviceTier, |
| 124 | + }; |
| 125 | +} |
| 126 | + |
| 127 | +export function applyAnthropicPayloadPolicyToParams( |
| 128 | + payloadObj: Record<string, unknown>, |
| 129 | + policy: AnthropicPayloadPolicy, |
| 130 | +): void { |
| 131 | + if ( |
| 132 | + policy.allowsServiceTier && |
| 133 | + policy.serviceTier !== undefined && |
| 134 | + payloadObj.service_tier === undefined |
| 135 | + ) { |
| 136 | + payloadObj.service_tier = policy.serviceTier; |
| 137 | + } |
| 138 | + |
| 139 | + if (!policy.cacheControl) { |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + applyAnthropicCacheControlToSystem(payloadObj.system, policy.cacheControl); |
| 144 | + // Preserve Anthropic cache-write scope by only tagging the trailing user turn. |
| 145 | + applyAnthropicCacheControlToMessages(payloadObj.messages, policy.cacheControl); |
| 146 | +} |
| 147 | + |
| 148 | +export function applyAnthropicEphemeralCacheControlMarkers( |
| 149 | + payloadObj: Record<string, unknown>, |
| 150 | +): void { |
| 151 | + const messages = payloadObj.messages; |
| 152 | + if (!Array.isArray(messages)) { |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + for (const message of messages as Array<{ role?: string; content?: unknown }>) { |
| 157 | + if (message.role === "system" || message.role === "developer") { |
| 158 | + if (typeof message.content === "string") { |
| 159 | + message.content = [ |
| 160 | + { type: "text", text: message.content, cache_control: { type: "ephemeral" } }, |
| 161 | + ]; |
| 162 | + continue; |
| 163 | + } |
| 164 | + if (Array.isArray(message.content) && message.content.length > 0) { |
| 165 | + const last = message.content[message.content.length - 1]; |
| 166 | + if (last && typeof last === "object") { |
| 167 | + const record = last as Record<string, unknown>; |
| 168 | + if (record.type !== "thinking" && record.type !== "redacted_thinking") { |
| 169 | + record.cache_control = { type: "ephemeral" }; |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + continue; |
| 174 | + } |
| 175 | + |
| 176 | + if (message.role === "assistant" && Array.isArray(message.content)) { |
| 177 | + for (const block of message.content) { |
| 178 | + if (!block || typeof block !== "object") { |
| 179 | + continue; |
| 180 | + } |
| 181 | + const record = block as Record<string, unknown>; |
| 182 | + if (record.type === "thinking" || record.type === "redacted_thinking") { |
| 183 | + delete record.cache_control; |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments