| 321 | } |
| 322 | |
| 323 | function applyCaching(msgs: ModelMessage[], model: Provider.Model): ModelMessage[] { |
| 324 | const system = msgs.filter((msg) => msg.role === "system").slice(0, 2) |
| 325 | const final = msgs.filter((msg) => msg.role !== "system").slice(-2) |
| 326 | |
| 327 | const providerOptions = { |
| 328 | anthropic: { |
| 329 | cacheControl: { type: "ephemeral" }, |
| 330 | }, |
| 331 | openrouter: { |
| 332 | cacheControl: { type: "ephemeral" }, |
| 333 | }, |
| 334 | bedrock: { |
| 335 | cachePoint: { type: "default" }, |
| 336 | }, |
| 337 | openaiCompatible: { |
| 338 | cache_control: { type: "ephemeral" }, |
| 339 | }, |
| 340 | copilot: { |
| 341 | copilot_cache_control: { type: "ephemeral" }, |
| 342 | }, |
| 343 | alibaba: { |
| 344 | cacheControl: { type: "ephemeral" }, |
| 345 | }, |
| 346 | } |
| 347 | |
| 348 | for (const msg of unique([...system, ...final])) { |
| 349 | const useMessageLevelOptions = |
| 350 | model.providerID === "anthropic" || |
| 351 | model.providerID.includes("bedrock") || |
| 352 | model.api.npm === "@ai-sdk/amazon-bedrock" |
| 353 | const shouldUseContentOptions = !useMessageLevelOptions && Array.isArray(msg.content) && msg.content.length > 0 |
| 354 | |
| 355 | if (shouldUseContentOptions) { |
| 356 | const lastContent = msg.content[msg.content.length - 1] |
| 357 | if ( |
| 358 | lastContent && |
| 359 | typeof lastContent === "object" && |
| 360 | lastContent.type !== "tool-approval-request" && |
| 361 | lastContent.type !== "tool-approval-response" |
| 362 | ) { |
| 363 | lastContent.providerOptions = mergeDeep(lastContent.providerOptions ?? {}, providerOptions) |
| 364 | continue |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | msg.providerOptions = mergeDeep(msg.providerOptions ?? {}, providerOptions) |
| 369 | } |
| 370 | |
| 371 | return msgs |
| 372 | } |
| 373 | |
| 374 | function unsupportedParts(msgs: ModelMessage[], model: Provider.Model): ModelMessage[] { |
| 375 | return msgs.map((msg) => { |