Skip to main content

everruns_local/
profile.rs

1// Named local environment configuration for embedded hosts.
2//
3// A `LocalProfile` bundles the on-disk locations and identity defaults an
4// embedder needs to stand up the local stores: where the SQLite database and
5// workspace files live, the UI base URL used for tool result links, and the
6// local org/principal identity stamped onto created records.
7
8use std::path::{Path, PathBuf};
9
10use everruns_core::typed_id::PrincipalId;
11
12/// Local environment configuration.
13#[derive(Debug, Clone)]
14pub struct LocalProfile {
15    /// Directory holding the local SQLite database(s).
16    pub data_dir: PathBuf,
17    /// Root directory for the session workspace filesystem.
18    pub workspace_root: PathBuf,
19    /// Base URL used to build UI links in tool results.
20    pub base_url: String,
21    /// Public organization id for created records.
22    pub org_public_id: String,
23    /// Owning principal stamped on schedules/sessions created locally.
24    pub owner_principal_id: PrincipalId,
25}
26
27impl Default for LocalProfile {
28    fn default() -> Self {
29        let data_dir = std::env::temp_dir().join("everruns-local");
30        Self {
31            workspace_root: data_dir.join("workspace"),
32            data_dir,
33            base_url: "http://localhost:9300".to_string(),
34            org_public_id: everruns_core::DEFAULT_ORG_PUBLIC_ID.to_string(),
35            owner_principal_id: PrincipalId::from_seed(1),
36        }
37    }
38}
39
40impl LocalProfile {
41    /// Start from defaults rooted at `data_dir`. The workspace defaults to
42    /// `data_dir/workspace`.
43    pub fn new(data_dir: impl Into<PathBuf>) -> Self {
44        let data_dir = data_dir.into();
45        Self {
46            workspace_root: data_dir.join("workspace"),
47            data_dir,
48            ..Self::default()
49        }
50    }
51
52    /// Path to the local stores database file under `data_dir`.
53    pub fn db_path(&self) -> PathBuf {
54        self.data_dir.join("local.db")
55    }
56
57    pub fn with_workspace_root(mut self, root: impl Into<PathBuf>) -> Self {
58        self.workspace_root = root.into();
59        self
60    }
61
62    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
63        self.base_url = base_url.into();
64        self
65    }
66
67    pub fn with_org_public_id(mut self, org_public_id: impl Into<String>) -> Self {
68        self.org_public_id = org_public_id.into();
69        self
70    }
71
72    pub fn with_owner_principal_id(mut self, owner_principal_id: PrincipalId) -> Self {
73        self.owner_principal_id = owner_principal_id;
74        self
75    }
76
77    /// Ensure `data_dir` and `workspace_root` exist on disk.
78    pub fn ensure_dirs(&self) -> std::io::Result<()> {
79        std::fs::create_dir_all(&self.data_dir)?;
80        std::fs::create_dir_all(&self.workspace_root)?;
81        Ok(())
82    }
83
84    pub fn data_dir(&self) -> &Path {
85        &self.data_dir
86    }
87}