1use serde::{Deserialize, Serialize};
4
5pub const MUSE_API_BASE: &str = "https://api.meta.ai/v1";
7
8pub const MUSE_MIN_MAX_TOKENS: u32 = 1024;
12
13#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
30#[serde(rename_all = "snake_case")]
31#[non_exhaustive]
32pub enum MuseApi {
33 #[default]
35 ChatCompletions,
36 Responses,
38}
39
40#[derive(Clone, Serialize, Deserialize)]
47pub struct MuseConfig {
48 #[serde(skip_serializing, default)]
50 pub api_key: String,
51 pub model: String,
53 #[serde(skip_serializing_if = "Option::is_none")]
55 pub base_url: Option<String>,
56 #[serde(skip_serializing_if = "Option::is_none")]
58 pub max_tokens: Option<u32>,
59 #[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 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 pub fn spark_1_3(api_key: impl Into<String>) -> Self {
96 Self::new(api_key, crate::catalog::MUSE_DEFAULT)
97 }
98
99 pub fn spark_1_2(api_key: impl Into<String>) -> Self {
110 Self::new(api_key, "muse-spark-1.2")
111 }
112
113 pub fn spark_1_1(api_key: impl Into<String>) -> Self {
115 Self::new(api_key, "muse-spark-1.1")
116 }
117
118 pub fn wonder(api_key: impl Into<String>) -> Self {
120 Self::new(api_key, "muse-wonder-1.0")
121 }
122
123 pub fn with_max_tokens(mut self, max_tokens: u32) -> Self {
125 self.max_tokens = Some(max_tokens);
126 self
127 }
128
129 pub fn with_api(mut self, api: MuseApi) -> Self {
131 self.api = api;
132 self
133 }
134
135 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 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}