Skip to main content

systemprompt_models/profile/
paths.rs

1//! Profile `paths:` block resolving system/services locations.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use serde::{Deserialize, Serialize};
7use std::path::{Path, PathBuf};
8
9#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
10#[serde(deny_unknown_fields)]
11pub struct PathsConfig {
12    pub system: String,
13    pub services: String,
14    pub bin: String,
15
16    #[serde(default)]
17    pub web_path: Option<String>,
18
19    #[serde(default)]
20    pub storage: Option<String>,
21
22    #[serde(default)]
23    pub geoip_database: Option<String>,
24}
25
26impl PathsConfig {
27    pub fn resolve_relative_to(&mut self, base: &Path) {
28        self.system = resolve_path(base, &self.system);
29        self.services = resolve_path(base, &self.services);
30        self.bin = resolve_path(base, &self.bin);
31        self.storage = self.storage.as_ref().map(|p| resolve_path(base, p));
32        self.geoip_database = self.geoip_database.as_ref().map(|p| resolve_path(base, p));
33        self.web_path = self.web_path.as_ref().map(|p| resolve_path(base, p));
34    }
35
36    pub fn skills(&self) -> String {
37        format!("{}/skills", self.services)
38    }
39
40    pub fn config(&self) -> String {
41        format!("{}/config/config.yaml", self.services)
42    }
43
44    pub fn ai_config(&self) -> String {
45        format!("{}/ai/config.yaml", self.services)
46    }
47
48    pub fn content_config(&self) -> String {
49        format!("{}/content/config.yaml", self.services)
50    }
51
52    pub fn web_config(&self) -> String {
53        format!("{}/web/config.yaml", self.services)
54    }
55
56    pub fn web_metadata(&self) -> String {
57        format!("{}/web/metadata.yaml", self.services)
58    }
59
60    pub fn plugins(&self) -> String {
61        format!("{}/plugins", self.services)
62    }
63
64    pub fn marketplaces(&self) -> String {
65        format!("{}/marketplaces", self.services)
66    }
67
68    pub fn hooks(&self) -> String {
69        format!("{}/hooks", self.services)
70    }
71
72    pub fn agents(&self) -> String {
73        format!("{}/agents", self.services)
74    }
75
76    pub fn logs(&self) -> String {
77        format!("{}/logs", self.system)
78    }
79
80    pub fn web_path_resolved(&self) -> String {
81        self.web_path
82            .clone()
83            .unwrap_or_else(|| format!("{}/web", self.system))
84    }
85
86    pub fn storage_resolved(&self) -> Option<&str> {
87        self.storage.as_deref()
88    }
89
90    pub fn geoip_database_resolved(&self) -> Option<&str> {
91        self.geoip_database.as_deref()
92    }
93}
94
95pub fn resolve_path(base: &Path, path: &str) -> String {
96    let p = Path::new(path);
97    if p.is_absolute() {
98        path.to_owned()
99    } else {
100        let resolved = base.join(p);
101        resolved.canonicalize().map_or_else(
102            |_| resolved.to_string_lossy().to_string(),
103            |canonical| canonical.to_string_lossy().to_string(),
104        )
105    }
106}
107
108pub fn expand_home(path_str: &str) -> PathBuf {
109    path_str.strip_prefix("~/").map_or_else(
110        || PathBuf::from(path_str),
111        |stripped| {
112            let home = std::env::var("HOME")
113                .or_else(|_| std::env::var("USERPROFILE"))
114                .unwrap_or_else(|_| {
115                    tracing::warn!(
116                        path = %path_str,
117                        "Cannot expand ~/ path: neither HOME nor USERPROFILE is set"
118                    );
119                    String::new()
120                });
121            PathBuf::from(home).join(stripped)
122        },
123    )
124}
125
126pub fn resolve_with_home(base: &Path, path_str: &str) -> PathBuf {
127    let path = expand_home(path_str);
128
129    if path.is_absolute() {
130        path
131    } else {
132        base.join(path)
133    }
134}