Skip to main content

everruns_core/capabilities/
prompt_caching.rs

1// Prompt Caching Capability
2//
3// When added to an agent, enables provider-specific prompt caching behavior for
4// drivers that support it. This capability does not add tools or prompt text;
5// it only configures the outbound LLM request.
6
7use super::{Capability, CapabilityLocalization, CapabilityStatus, SystemPromptContext};
8use crate::driver_registry::{PromptCacheConfig, PromptCacheStrategy};
9use async_trait::async_trait;
10
11/// Capability ID for provider prompt caching.
12pub const PROMPT_CACHING_CAPABILITY_ID: &str = "prompt_caching";
13
14/// Prompt caching capability.
15///
16/// Drivers translate this generic config into provider-specific request
17/// controls when possible. Unsupported providers or models ignore it.
18pub struct PromptCachingCapability {
19    strategy: PromptCacheStrategy,
20    gemini_cached_content: Option<String>,
21}
22
23impl PromptCachingCapability {
24    pub fn new() -> Self {
25        Self {
26            strategy: PromptCacheStrategy::Auto,
27            gemini_cached_content: None,
28        }
29    }
30
31    pub fn with_strategy(strategy: PromptCacheStrategy) -> Self {
32        Self {
33            strategy,
34            gemini_cached_content: None,
35        }
36    }
37
38    pub fn with_gemini_cached_content(
39        strategy: PromptCacheStrategy,
40        gemini_cached_content: impl Into<String>,
41    ) -> Self {
42        Self {
43            strategy,
44            gemini_cached_content: Some(gemini_cached_content.into()),
45        }
46    }
47
48    /// Returns the PromptCacheConfig for this capability.
49    pub fn prompt_cache_config(&self) -> PromptCacheConfig {
50        PromptCacheConfig {
51            enabled: true,
52            strategy: self.strategy,
53            gemini_cached_content: self.gemini_cached_content.clone(),
54        }
55    }
56}
57
58impl Default for PromptCachingCapability {
59    fn default() -> Self {
60        Self::new()
61    }
62}
63
64#[async_trait]
65impl Capability for PromptCachingCapability {
66    fn id(&self) -> &str {
67        PROMPT_CACHING_CAPABILITY_ID
68    }
69
70    fn name(&self) -> &str {
71        "Prompt Caching"
72    }
73
74    fn description(&self) -> &str {
75        "Enables provider-specific prompt caching where supported and records \
76         that request intent in llm.generation metadata."
77    }
78
79    fn localizations(&self) -> Vec<CapabilityLocalization> {
80        vec![CapabilityLocalization::text(
81            "uk",
82            "Кешування промптів",
83            "Вмикає кешування промптів, специфічне для провайдера, там, де воно підтримується, і фіксує цей намір запиту в метаданих llm.generation.",
84        )]
85    }
86
87    fn status(&self) -> CapabilityStatus {
88        CapabilityStatus::Available
89    }
90
91    fn category(&self) -> Option<&str> {
92        Some("Optimization")
93    }
94
95    async fn system_prompt_contribution(&self, _ctx: &SystemPromptContext) -> Option<String> {
96        None
97    }
98}
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103
104    // Metadata/tool-list constants covered by builtin_capabilities_satisfy_registry_invariants.
105
106    #[test]
107    fn test_default_strategy() {
108        let cap = PromptCachingCapability::new();
109        let config = cap.prompt_cache_config();
110        assert!(config.enabled);
111        assert_eq!(config.strategy, PromptCacheStrategy::Auto);
112        assert!(config.gemini_cached_content.is_none());
113    }
114
115    #[test]
116    fn test_capability_with_gemini_cached_content() {
117        let cap = PromptCachingCapability::with_gemini_cached_content(
118            PromptCacheStrategy::Auto,
119            "cachedContents/example",
120        );
121        let config = cap.prompt_cache_config();
122        assert_eq!(
123            config.gemini_cached_content.as_deref(),
124            Some("cachedContents/example")
125        );
126    }
127}