Skip to main content

cc_switch/grok/
types.rs

1use serde::{Deserialize, Serialize};
2use std::collections::BTreeMap;
3
4/// Map of HTTP header name to value.
5pub type HeaderMap = BTreeMap<String, String>;
6
7/// GrokConfiguration represents a custom third-party model definition for the
8/// grok CLI (xAI), stored under `[model.<alias_name>]` in `~/.grok/config.toml`.
9///
10/// The env-based auth path (`env_key`) is the primary way to supply credentials;
11/// `api_key` is an alternative that writes the key directly to disk.
12#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
13pub struct GrokConfiguration {
14    /// Unique alias name; also used as the `[model.<alias_name>]` id.
15    pub alias_name: String,
16    /// Underlying model id (e.g. "claude-opus-4-6").
17    pub model: String,
18    /// API endpoint base URL.
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub base_url: Option<String>,
21    /// Human-friendly display name.
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub name: Option<String>,
24    /// API backend: "chat_completions" | "responses" | "messages".
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub api_backend: Option<String>,
27    /// Context window size.
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub context_window: Option<u32>,
30    /// API key written directly into config (secondary to env_key).
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub api_key: Option<String>,
33    /// Environment variable name(s) to read the key from (primary path).
34    #[serde(skip_serializing_if = "Option::is_none")]
35    pub env_key: Option<Vec<String>>,
36    /// Extra HTTP headers (e.g. x-api-key, anthropic-version) for the model.
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub extra_headers: Option<HeaderMap>,
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    fn sample() -> GrokConfiguration {
46        let mut headers = BTreeMap::new();
47        headers.insert("x-api-key".to_string(), "sk-ant-xxx".to_string());
48        headers.insert("anthropic-version".to_string(), "2023-06-01".to_string());
49        GrokConfiguration {
50            alias_name: "claude-opus".to_string(),
51            model: "claude-opus-4-6".to_string(),
52            base_url: Some("https://api.anthropic.com/v1".to_string()),
53            name: Some("Claude Opus 4.6".to_string()),
54            api_backend: Some("messages".to_string()),
55            context_window: Some(200000),
56            api_key: None,
57            env_key: Some(vec!["ANTHROPIC_API_KEY".to_string()]),
58            extra_headers: Some(headers),
59        }
60    }
61
62    #[test]
63    fn test_serialize_skips_none() {
64        let config = GrokConfiguration {
65            alias_name: "gpt".to_string(),
66            model: "gpt-4o".to_string(),
67            base_url: None,
68            name: None,
69            api_backend: None,
70            context_window: None,
71            api_key: None,
72            env_key: Some(vec!["OPENAI_API_KEY".to_string()]),
73            extra_headers: None,
74        };
75        let json = serde_json::to_string(&config).expect("Should serialize");
76        assert!(json.contains("\"model\":\"gpt-4o\""));
77        assert!(json.contains("\"env_key\":[\"OPENAI_API_KEY\"]"));
78        assert!(!json.contains("base_url"));
79        assert!(!json.contains("api_key"));
80        assert!(!json.contains("extra_headers"));
81    }
82
83    #[test]
84    fn test_roundtrip_serialization() {
85        let original = sample();
86        let json = serde_json::to_string(&original).expect("Should serialize");
87        let deserialized: GrokConfiguration =
88            serde_json::from_str(&json).expect("Should deserialize");
89        assert_eq!(original, deserialized);
90    }
91}