Skip to main content

adk_model/muse/
config.rs

1//! Configuration types for the Meta Muse provider.
2
3use serde::{Deserialize, Serialize};
4
5/// Default Meta Model API base URL.
6pub const MUSE_API_BASE: &str = "https://api.meta.ai/v1";
7
8/// Minimum output cap the client sends. Spark reasoning counts toward `max_tokens` —
9/// a trivial greeting was observed spending over 500 reasoning tokens — so smaller
10/// caps are raised to this instead of returning empty `length` responses.
11pub const MUSE_MIN_MAX_TOKENS: u32 = 1024;
12
13/// The Meta Model API endpoint a [`MuseClient`](crate::muse::MuseClient) calls.
14///
15/// | API | Endpoint | Carries |
16/// |-----|----------|---------|
17/// | [`ChatCompletions`](Self::ChatCompletions) (default) | `/chat/completions` | Stateless turns |
18/// | [`Responses`](Self::Responses) | `/responses` | Reasoning across turns (`previous_response_id`), stored responses, streamed events; needs the `muse-responses` feature |
19///
20/// # Example
21///
22/// ```
23/// use adk_model::muse::{MuseApi, MuseConfig};
24///
25/// let config = MuseConfig::spark_1_3("LLM-key").with_api(MuseApi::Responses);
26/// assert_eq!(config.api, MuseApi::Responses);
27/// assert_eq!(MuseConfig::spark_1_3("LLM-key").api, MuseApi::ChatCompletions);
28/// ```
29#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
30#[serde(rename_all = "snake_case")]
31#[non_exhaustive]
32pub enum MuseApi {
33    /// Chat Completions, drop-in compatible with OpenAI's.
34    #[default]
35    ChatCompletions,
36    /// The Responses API, through the shared Open Responses engine.
37    Responses,
38}
39
40/// Configuration for the Meta Model API.
41///
42/// The key is read from `META_API_KEY` (Meta also documents `MODEL_API_KEY`); see the
43/// [module documentation](crate::muse) for an example.
44///
45/// The API key is redacted from `Debug` output and omitted when serializing.
46#[derive(Clone, Serialize, Deserialize)]
47pub struct MuseConfig {
48    /// Meta Model API key. Never serialized; deserializing a config without it yields an empty key.
49    #[serde(skip_serializing, default)]
50    pub api_key: String,
51    /// Model name.
52    pub model: String,
53    /// Optional custom base URL.
54    #[serde(skip_serializing_if = "Option::is_none")]
55    pub base_url: Option<String>,
56    /// Maximum tokens for output.
57    #[serde(skip_serializing_if = "Option::is_none")]
58    pub max_tokens: Option<u32>,
59    /// The endpoint the client calls.
60    #[serde(default)]
61    pub api: MuseApi,
62}
63
64impl std::fmt::Debug for MuseConfig {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        f.debug_struct("MuseConfig")
67            .field("api_key", &"[REDACTED]")
68            .field("model", &self.model)
69            .field("base_url", &self.base_url)
70            .field("max_tokens", &self.max_tokens)
71            .field("api", &self.api)
72            .finish()
73    }
74}
75
76impl Default for MuseConfig {
77    fn default() -> Self {
78        Self {
79            api_key: String::new(),
80            model: crate::catalog::MUSE_DEFAULT.to_string(),
81            base_url: None,
82            max_tokens: None,
83            api: MuseApi::ChatCompletions,
84        }
85    }
86}
87
88impl MuseConfig {
89    /// Create a new Muse config with the given API key and model.
90    pub fn new(api_key: impl Into<String>, model: impl Into<String>) -> Self {
91        Self { api_key: api_key.into(), model: model.into(), ..Default::default() }
92    }
93
94    /// Create a config for `muse-spark-1.3`, the recommended default.
95    pub fn spark_1_3(api_key: impl Into<String>) -> Self {
96        Self::new(api_key, crate::catalog::MUSE_DEFAULT)
97    }
98
99    /// Create a config for `muse-spark-1.2`, the previous version.
100    ///
101    /// # Example
102    ///
103    /// ```
104    /// use adk_model::muse::MuseConfig;
105    ///
106    /// let config = MuseConfig::spark_1_2("LLM-key");
107    /// assert_eq!(config.model, "muse-spark-1.2");
108    /// ```
109    pub fn spark_1_2(api_key: impl Into<String>) -> Self {
110        Self::new(api_key, "muse-spark-1.2")
111    }
112
113    /// Create a config for `muse-spark-1.1`, the original version.
114    pub fn spark_1_1(api_key: impl Into<String>) -> Self {
115        Self::new(api_key, "muse-spark-1.1")
116    }
117
118    /// Create a config for `muse-wonder-1.0`, the flagship (preview) model.
119    pub fn wonder(api_key: impl Into<String>) -> Self {
120        Self::new(api_key, "muse-wonder-1.0")
121    }
122
123    /// Set max tokens for output.
124    pub fn with_max_tokens(mut self, max_tokens: u32) -> Self {
125        self.max_tokens = Some(max_tokens);
126        self
127    }
128
129    /// Selects the endpoint the client calls.
130    pub fn with_api(mut self, api: MuseApi) -> Self {
131        self.api = api;
132        self
133    }
134
135    /// Set custom base URL.
136    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
137        self.base_url = Some(base_url.into());
138        self
139    }
140
141    /// Get the effective base URL.
142    pub fn effective_base_url(&self) -> &str {
143        self.base_url.as_deref().unwrap_or(MUSE_API_BASE)
144    }
145}
146
147#[cfg(test)]
148mod tests {
149    use super::MuseConfig;
150    use crate::catalog::{ModelLifecycle, lookup_model};
151
152    #[test]
153    fn presets_select_active_catalog_models() {
154        for config in [
155            MuseConfig::spark_1_3("LLM-key"),
156            MuseConfig::spark_1_2("LLM-key"),
157            MuseConfig::spark_1_1("LLM-key"),
158        ] {
159            let entry = lookup_model("muse", &config.model)
160                .unwrap_or_else(|| panic!("{} is not in the catalog", config.model));
161            assert_eq!(entry.lifecycle, ModelLifecycle::Active, "{}", config.model);
162        }
163    }
164
165    #[test]
166    fn debug_redacts_the_api_key() {
167        let rendered = format!("{:?}", MuseConfig::spark_1_3("LLM-secret"));
168        assert!(rendered.contains("[REDACTED]"));
169        assert!(!rendered.contains("LLM-secret"));
170    }
171}