Skip to main content

infinity_daemon/
config.rs

1use infinity_agent_core::tools::config::ToolsConfig;
2use std::path::PathBuf;
3
4type BoxError = Box<dyn std::error::Error + Send + Sync>;
5
6pub fn user_config_path() -> Result<PathBuf, BoxError> {
7    let home = dirs::home_dir().ok_or("could not determine home directory")?;
8    Ok(home.join(".infinity").join("rap.json"))
9}
10
11/// Path to the model providers config: `~/.infinity/providers.json`.
12pub fn providers_config_path() -> Result<PathBuf, BoxError> {
13    let home = dirs::home_dir().ok_or("could not determine home directory")?;
14    Ok(home.join(".infinity").join("providers.json"))
15}
16
17pub fn load_config(path: &std::path::Path) -> Result<ToolsConfig, BoxError> {
18    ToolsConfig::from_file(path)
19}
20
21/// Load the merged RAP config for `cwd`: the cwd-local `.infinity/rap.json`
22/// merged with the optional user-level config. Returns a human-readable line
23/// describing which config source(s) were used (for display to clients) and
24/// the merged config — `None` when neither source exists.
25///
26/// This is the single place the local/user merge semantics live; both the
27/// lazily managed per-session servers and the migration flows go through it.
28pub fn load_merged_rap_config(
29    cwd: &std::path::Path,
30    user_config_path: Option<&std::path::Path>,
31) -> Result<(String, Option<ToolsConfig>), BoxError> {
32    let cwd_rap = cwd.join(".infinity").join("rap.json");
33    let local_config = cwd_rap
34        .exists()
35        .then(|| load_config(&cwd_rap))
36        .transpose()?;
37    let user_config = user_config_path
38        .and_then(|p| p.exists().then(|| load_config(p)))
39        .transpose()?;
40
41    Ok(match (local_config, user_config) {
42        (None, None) => (
43            "Neither local nor user RAP configs exist, using empty config".into(),
44            None,
45        ),
46        (None, Some(c)) => ("Using user config".into(), Some(c)),
47        (Some(c), None) => ("Using local config".into(), Some(c)),
48        (Some(mut l), Some(u)) => {
49            l.merge(u);
50            (
51                "Both local and user RAP configs exist, merging".into(),
52                Some(l),
53            )
54        }
55    })
56}