Skip to main content

cc_switch/omp/
types.rs

1use serde::{Deserialize, Serialize};
2
3/// OmpConfiguration represents an OMP (omp.sh) configuration.
4///
5/// omp is configured as an OpenAI-standard-protocol provider: the key is written
6/// as `OPENAI_API_KEY` and custom endpoints are registered in `models.yml` with
7/// `api: openai-completions`.
8#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
9pub struct OmpConfiguration {
10    /// Unique alias name for this configuration
11    pub alias_name: String,
12    /// OpenAI API key
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub openai_api_key: Option<String>,
15    /// Base URL for API endpoint (optional, custom OpenAI-compatible gateway)
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub base_url: Option<String>,
18    /// PI_SLOW_MODEL - slow/reasoning model
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub slow_model: Option<String>,
21    /// PI_SMOL_MODEL - fast/smaller model
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub smol_model: Option<String>,
24    /// PI_PLAN_MODEL - planning model
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub plan_model: Option<String>,
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn test_serialize_openai() {
35        let config = OmpConfiguration {
36            alias_name: "test".to_string(),
37            openai_api_key: Some("sk-test".to_string()),
38            base_url: None,
39            slow_model: Some("openai/gpt-5.3-codex:high".to_string()),
40            smol_model: Some("openai/gpt-4o-mini".to_string()),
41            plan_model: None,
42        };
43
44        let json = serde_json::to_string(&config).expect("Should serialize");
45        assert!(json.contains("\"openai_api_key\":\"sk-test\""));
46        assert!(json.contains("\"slow_model\":\"openai/gpt-5.3-codex:high\""));
47        assert!(!json.contains("plan_model"));
48        assert!(!json.contains("auth_mode"));
49        assert!(!json.contains("anthropic"));
50    }
51
52    #[test]
53    fn test_deserialize_openai() {
54        let json = r#"{
55            "alias_name": "test",
56            "openai_api_key": "sk-test",
57            "slow_model": "openai/gpt-5.3-codex:high"
58        }"#;
59
60        let config: OmpConfiguration = serde_json::from_str(json).expect("Should deserialize");
61        assert_eq!(config.openai_api_key, Some("sk-test".to_string()));
62        assert_eq!(
63            config.slow_model,
64            Some("openai/gpt-5.3-codex:high".to_string())
65        );
66        assert_eq!(config.base_url, None);
67    }
68
69    #[test]
70    fn test_deserialize_ignores_legacy_anthropic_fields() {
71        // Configs stored before Anthropic support was removed still deserialize;
72        // the obsolete fields are simply ignored.
73        let json = r#"{
74            "alias_name": "legacy",
75            "auth_mode": "anthropic",
76            "anthropic_api_key": "sk-ant-old",
77            "anthropic_oauth_token": "oauth-old"
78        }"#;
79
80        let config: OmpConfiguration = serde_json::from_str(json).expect("Should deserialize");
81        assert_eq!(config.alias_name, "legacy");
82        assert_eq!(config.openai_api_key, None);
83    }
84
85    #[test]
86    fn test_roundtrip_serialization() {
87        let original = OmpConfiguration {
88            alias_name: "work".to_string(),
89            openai_api_key: Some("sk-work".to_string()),
90            base_url: Some("https://api.example.com/v1".to_string()),
91            slow_model: Some("gpt-4o".to_string()),
92            smol_model: Some("gpt-4o-mini".to_string()),
93            plan_model: Some("gpt-4o".to_string()),
94        };
95
96        let json = serde_json::to_string(&original).expect("Should serialize");
97        let deserialized: OmpConfiguration =
98            serde_json::from_str(&json).expect("Should deserialize");
99        assert_eq!(original, deserialized);
100    }
101}