| 124 | } |
| 125 | |
| 126 | function applyCaching(msgs: ModelMessage[], providerID: string): ModelMessage[] { |
| 127 | const system = msgs.filter((msg) => msg.role === "system").slice(0, 2) |
| 128 | const final = msgs.filter((msg) => msg.role !== "system").slice(-2) |
| 129 | |
| 130 | const providerOptions = { |
| 131 | anthropic: { |
| 132 | cacheControl: { type: "ephemeral" }, |
| 133 | }, |
| 134 | openrouter: { |
| 135 | cache_control: { type: "ephemeral" }, |
| 136 | }, |
| 137 | bedrock: { |
| 138 | cachePoint: { type: "ephemeral" }, |
| 139 | }, |
| 140 | openaiCompatible: { |
| 141 | cache_control: { type: "ephemeral" }, |
| 142 | }, |
| 143 | } |
| 144 | |
| 145 | for (const msg of unique([...system, ...final])) { |
| 146 | const shouldUseContentOptions = providerID !== "anthropic" && Array.isArray(msg.content) && msg.content.length > 0 |
| 147 | |
| 148 | if (shouldUseContentOptions) { |
| 149 | const lastContent = msg.content[msg.content.length - 1] |
| 150 | if (lastContent && typeof lastContent === "object") { |
| 151 | lastContent.providerOptions = { |
| 152 | ...lastContent.providerOptions, |
| 153 | ...providerOptions, |
| 154 | } |
| 155 | continue |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | msg.providerOptions = { |
| 160 | ...msg.providerOptions, |
| 161 | ...providerOptions, |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return msgs |
| 166 | } |
| 167 | |
| 168 | function unsupportedParts(msgs: ModelMessage[], model: Provider.Model): ModelMessage[] { |
| 169 | return msgs.map((msg) => { |