Skip to main content

gopher_mcp_rust/
config.rs

1//! Configuration types for creating agents.
2
3/// Configuration for creating a GopherAgent.
4#[derive(Debug, Clone, Default)]
5pub struct Config {
6    provider: String,
7    model: String,
8    api_key: Option<String>,
9    server_config: Option<String>,
10}
11
12impl Config {
13    /// Get the provider name.
14    pub fn provider(&self) -> &str {
15        &self.provider
16    }
17
18    /// Get the model name.
19    pub fn model(&self) -> &str {
20        &self.model
21    }
22
23    /// Get the API key (if set).
24    pub fn api_key(&self) -> Option<&str> {
25        self.api_key.as_deref()
26    }
27
28    /// Get the server config JSON (if set).
29    pub fn server_config(&self) -> Option<&str> {
30        self.server_config.as_deref()
31    }
32
33    /// Check if an API key is configured.
34    pub fn has_api_key(&self) -> bool {
35        self.api_key.as_ref().map_or(false, |k| !k.is_empty())
36    }
37
38    /// Check if a server config is configured.
39    pub fn has_server_config(&self) -> bool {
40        self.server_config.as_ref().map_or(false, |c| !c.is_empty())
41    }
42}
43
44/// Builder for creating Config instances.
45#[derive(Debug, Default)]
46pub struct ConfigBuilder {
47    config: Config,
48}
49
50impl ConfigBuilder {
51    /// Create a new ConfigBuilder.
52    pub fn new() -> Self {
53        Self::default()
54    }
55
56    /// Set the provider name.
57    pub fn with_provider<S: Into<String>>(mut self, provider: S) -> Self {
58        self.config.provider = provider.into();
59        self
60    }
61
62    /// Set the model name.
63    pub fn with_model<S: Into<String>>(mut self, model: S) -> Self {
64        self.config.model = model.into();
65        self
66    }
67
68    /// Set the API key.
69    pub fn with_api_key<S: Into<String>>(mut self, api_key: S) -> Self {
70        self.config.api_key = Some(api_key.into());
71        self
72    }
73
74    /// Set the server config JSON.
75    pub fn with_server_config<S: Into<String>>(mut self, server_config: S) -> Self {
76        self.config.server_config = Some(server_config.into());
77        self
78    }
79
80    /// Build the Config.
81    pub fn build(self) -> Config {
82        self.config
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn test_builder_with_api_key() {
92        let config = ConfigBuilder::new()
93            .with_provider("AnthropicProvider")
94            .with_model("claude-3-haiku-20240307")
95            .with_api_key("test-api-key")
96            .build();
97
98        assert_eq!(config.provider(), "AnthropicProvider");
99        assert_eq!(config.model(), "claude-3-haiku-20240307");
100        assert!(config.has_api_key());
101        assert!(!config.has_server_config());
102        assert_eq!(config.api_key(), Some("test-api-key"));
103    }
104
105    #[test]
106    fn test_builder_with_server_config() {
107        let server_config = r#"{"succeeded": true}"#;
108        let config = ConfigBuilder::new()
109            .with_provider("AnthropicProvider")
110            .with_model("claude-3-haiku-20240307")
111            .with_server_config(server_config)
112            .build();
113
114        assert_eq!(config.provider(), "AnthropicProvider");
115        assert_eq!(config.model(), "claude-3-haiku-20240307");
116        assert!(!config.has_api_key());
117        assert!(config.has_server_config());
118        assert_eq!(config.server_config(), Some(server_config));
119    }
120
121    #[test]
122    fn test_empty_config() {
123        let config = ConfigBuilder::new().build();
124
125        assert!(!config.has_api_key());
126        assert!(!config.has_server_config());
127        assert!(config.provider().is_empty());
128        assert!(config.model().is_empty());
129    }
130}