Skip to main content

cc_switch/omp/
types.rs

1use serde::{Deserialize, Serialize};
2
3/// OmpConfiguration represents an OMP (omp.sh) configuration
4///
5/// Supports two authentication modes:
6/// - "anthropic": Uses ANTHROPIC_API_KEY or ANTHROPIC_OAUTH_TOKEN
7/// - "openai": Uses OPENAI_API_KEY
8#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
9pub struct OmpConfiguration {
10    /// Unique alias name for this configuration
11    pub alias_name: String,
12    /// Authentication mode: "anthropic" or "openai"
13    pub auth_mode: String,
14    /// Anthropic API key (anthropic mode)
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub anthropic_api_key: Option<String>,
17    /// Anthropic OAuth token (anthropic mode, takes precedence over API key)
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub anthropic_oauth_token: Option<String>,
20    /// OpenAI API key (openai mode)
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub openai_api_key: Option<String>,
23    /// Base URL for API endpoint (optional, e.g., ANTHROPIC_BASE_URL)
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub base_url: Option<String>,
26    /// PI_SLOW_MODEL - slow/reasoning model
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub slow_model: Option<String>,
29    /// PI_SMOL_MODEL - fast/smaller model
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub smol_model: Option<String>,
32    /// PI_PLAN_MODEL - planning model
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub plan_model: Option<String>,
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn test_serialize_anthropic_mode() {
43        let config = OmpConfiguration {
44            alias_name: "test".to_string(),
45            auth_mode: "anthropic".to_string(),
46            anthropic_api_key: Some("sk-ant-test".to_string()),
47            anthropic_oauth_token: None,
48            openai_api_key: None,
49            base_url: None,
50            slow_model: Some("claude-3-5-sonnet-20241022".to_string()),
51            smol_model: None,
52            plan_model: None,
53        };
54
55        let json = serde_json::to_string(&config).expect("Should serialize");
56        assert!(json.contains("\"auth_mode\":\"anthropic\""));
57        assert!(json.contains("\"anthropic_api_key\":\"sk-ant-test\""));
58        assert!(!json.contains("openai_api_key"));
59    }
60
61    #[test]
62    fn test_deserialize_anthropic_mode() {
63        let json = r#"{
64            "alias_name": "test",
65            "auth_mode": "anthropic",
66            "anthropic_api_key": "sk-ant-test",
67            "slow_model": "claude-3-5-sonnet-20241022"
68        }"#;
69
70        let config: OmpConfiguration = serde_json::from_str(json).expect("Should deserialize");
71        assert_eq!(config.auth_mode, "anthropic");
72        assert_eq!(config.anthropic_api_key, Some("sk-ant-test".to_string()));
73        assert_eq!(
74            config.slow_model,
75            Some("claude-3-5-sonnet-20241022".to_string())
76        );
77        assert_eq!(config.openai_api_key, None);
78    }
79
80    #[test]
81    fn test_serialize_openai_mode() {
82        let config = OmpConfiguration {
83            alias_name: "test".to_string(),
84            auth_mode: "openai".to_string(),
85            anthropic_api_key: None,
86            anthropic_oauth_token: None,
87            openai_api_key: Some("sk-test".to_string()),
88            base_url: None,
89            slow_model: Some("openai/gpt-5.3-codex:high".to_string()),
90            smol_model: Some("openai/gpt-4o-mini".to_string()),
91            plan_model: None,
92        };
93
94        let json = serde_json::to_string(&config).expect("Should serialize");
95        assert!(json.contains("\"auth_mode\":\"openai\""));
96        assert!(json.contains("\"openai_api_key\":\"sk-test\""));
97        assert!(!json.contains("anthropic_api_key"));
98    }
99
100    #[test]
101    fn test_deserialize_openai_mode() {
102        let json = r#"{
103            "alias_name": "test",
104            "auth_mode": "openai",
105            "openai_api_key": "sk-test",
106            "slow_model": "openai/gpt-5.3-codex:high"
107        }"#;
108
109        let config: OmpConfiguration = serde_json::from_str(json).expect("Should deserialize");
110        assert_eq!(config.auth_mode, "openai");
111        assert_eq!(config.openai_api_key, Some("sk-test".to_string()));
112        assert_eq!(
113            config.slow_model,
114            Some("openai/gpt-5.3-codex:high".to_string())
115        );
116        assert_eq!(config.anthropic_api_key, None);
117    }
118
119    #[test]
120    fn test_roundtrip_serialization() {
121        let original = OmpConfiguration {
122            alias_name: "work".to_string(),
123            auth_mode: "anthropic".to_string(),
124            anthropic_api_key: Some("sk-ant-work".to_string()),
125            anthropic_oauth_token: None,
126            openai_api_key: None,
127            base_url: None,
128            slow_model: Some("claude-3-5-sonnet-20241022".to_string()),
129            smol_model: Some("claude-3-haiku-20240307".to_string()),
130            plan_model: Some("claude-3-5-sonnet-20241022".to_string()),
131        };
132
133        let json = serde_json::to_string(&original).expect("Should serialize");
134        let deserialized: OmpConfiguration =
135            serde_json::from_str(&json).expect("Should deserialize");
136        assert_eq!(original, deserialized);
137    }
138}