Skip to main content

cookie_agent_config/
cache.rs

1use serde::{Deserialize, Serialize};
2
3pub use cookie_agent_models::{
4    AnthropicCacheConfig, BedrockCacheConfig, CacheTtl, OpenAiCacheConfig, OpenAiCacheMode,
5    OpenAiPromptCacheRetention, OpenAiPromptCacheTtl, RollingCacheTtl,
6};
7
8#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize)]
9pub struct CacheConfig {
10    pub anthropic: Option<AnthropicCacheConfig>,
11    pub bedrock: Option<BedrockCacheConfig>,
12    pub openai: Option<OpenAiCacheConfig>,
13}
14
15impl CacheConfig {
16    pub fn validate(&self) -> Result<(), &'static str> {
17        if let Some(config) = &self.anthropic {
18            config.validate()?;
19        }
20        if let Some(config) = &self.bedrock {
21            config.validate()?;
22        }
23        Ok(())
24    }
25}
26
27impl<'de> Deserialize<'de> for CacheConfig {
28    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
29    where
30        D: serde::Deserializer<'de>,
31    {
32        #[derive(Deserialize)]
33        #[serde(deny_unknown_fields)]
34        struct Raw {
35            anthropic: Option<AnthropicCacheConfig>,
36            bedrock: Option<BedrockCacheConfig>,
37            openai: Option<OpenAiCacheConfig>,
38        }
39
40        let raw = Raw::deserialize(deserializer)?;
41        let config = Self {
42            anthropic: raw.anthropic,
43            bedrock: raw.bedrock,
44            openai: raw.openai,
45        };
46        config.validate().map_err(serde::de::Error::custom)?;
47        Ok(config)
48    }
49}