| 85 | // --------------------------------------------------------------------------- |
| 86 | |
| 87 | pub fn apply_caching(messages: &mut [Message], provider_type: ProviderType) { |
| 88 | if !provider_type.supports_caching() { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | let system_indices: Vec<usize> = messages |
| 93 | .iter() |
| 94 | .enumerate() |
| 95 | .filter(|(_, m)| matches!(m.role, crate::Role::System)) |
| 96 | .map(|(i, _)| i) |
| 97 | .take(2) |
| 98 | .collect(); |
| 99 | |
| 100 | let total = messages.len(); |
| 101 | let final_indices: Vec<usize> = (total.saturating_sub(2)..total).collect(); |
| 102 | |
| 103 | let mut indices_to_cache: Vec<usize> = Vec::new(); |
| 104 | for idx in system_indices.into_iter().chain(final_indices.into_iter()) { |
| 105 | if !indices_to_cache.contains(&idx) { |
| 106 | indices_to_cache.push(idx); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | for idx in indices_to_cache { |
| 111 | if let Some(msg) = messages.get_mut(idx) { |
| 112 | apply_cache_to_message(msg, provider_type); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | fn apply_cache_to_message(message: &mut Message, provider_type: ProviderType) { |
| 118 | // TS applyCaching uses providerOptions with multiple provider keys merged via mergeDeep. |