Skip to main content

synaptic_config/
lib.rs

1mod format;
2mod mcp;
3mod model;
4mod paths;
5mod source;
6mod tools;
7
8pub use format::{parse_config, ConfigFormat};
9pub use mcp::McpServerConfig;
10pub use model::ModelConfig;
11pub use paths::PathsConfig;
12pub use source::{
13    discover_and_load, load_from_file, load_from_source, ConfigSource, FileConfigSource,
14    StringConfigSource,
15};
16pub use tools::ToolsConfig;
17
18use std::path::Path;
19
20use serde::Deserialize;
21use synaptic_core::SynapticError;
22
23/// Top-level agent configuration, loaded from TOML / JSON / YAML.
24#[derive(Debug, Clone, Deserialize)]
25pub struct SynapticAgentConfig {
26    pub model: ModelConfig,
27    #[serde(default)]
28    pub agent: AgentConfig,
29    #[serde(default)]
30    pub paths: PathsConfig,
31    #[serde(default)]
32    pub mcp: Option<Vec<McpServerConfig>>,
33}
34
35/// Agent behavior configuration.
36#[derive(Debug, Clone, Deserialize, Default)]
37pub struct AgentConfig {
38    pub system_prompt: Option<String>,
39    pub max_turns: Option<usize>,
40    #[serde(default)]
41    pub tools: ToolsConfig,
42}
43
44impl SynapticAgentConfig {
45    /// Load configuration from a file (TOML, JSON, or YAML).
46    ///
47    /// Search order:
48    /// 1. Explicit path (if provided)
49    /// 2. `./synaptic.{toml,json,yaml,yml}`
50    /// 3. `~/.synaptic/config.{toml,json,yaml,yml}`
51    pub fn load(path: Option<&Path>) -> Result<Self, SynapticError> {
52        discover_and_load(path)
53    }
54
55    /// Load from any [`ConfigSource`].
56    pub fn load_from(source: &dyn ConfigSource) -> Result<Self, SynapticError> {
57        load_from_source(source)
58    }
59
60    /// Parse from a string in the given format.
61    pub fn parse(content: &str, format: ConfigFormat) -> Result<Self, SynapticError> {
62        parse_config(content, format)
63    }
64
65    /// Resolve the API key from the environment variable specified in `model.api_key_env`.
66    pub fn resolve_api_key(&self) -> Result<String, SynapticError> {
67        std::env::var(&self.model.api_key_env).map_err(|_| {
68            SynapticError::Config(format!(
69                "environment variable '{}' not set",
70                self.model.api_key_env
71            ))
72        })
73    }
74}