MCPcopy Index your code
hub / github.com/ChrisFeldmeier/OpenCodeRust / apply_caching

Function apply_caching

crates/opencode-provider/src/transform.rs:87–115  ·  view source on GitHub ↗
(messages: &mut [Message], provider_type: ProviderType)

Source from the content-addressed store, hash-verified

85// ---------------------------------------------------------------------------
86
87pub 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
117fn apply_cache_to_message(message: &mut Message, provider_type: ProviderType) {
118 // TS applyCaching uses providerOptions with multiple provider keys merged via mergeDeep.

Callers 3

transform_messagesFunction · 0.85
loop_innerMethod · 0.85

Calls 6

newFunction · 0.85
apply_cache_to_messageFunction · 0.85
supports_cachingMethod · 0.80
containsMethod · 0.80
filterMethod · 0.45
get_mutMethod · 0.45

Tested by 1