Files
capri-core-rust/crates/capri-core/src/paths.rs
T
2026-11-11 20:20:47 +01:00

70 lines
2.1 KiB
Rust

use std::path::PathBuf;
use anyhow::{Context, Result};
/// Where Capri keeps its files. `CAPRI_HOME` puts everything under one folder (tests, portable use);
/// otherwise the XDG base directories are used.
#[derive(Clone, Debug)]
pub struct Paths {
pub config: PathBuf,
pub data: PathBuf,
pub cache: PathBuf,
}
impl Paths {
pub fn discover() -> Result<Self> {
if let Some(home) = std::env::var_os("CAPRI_HOME") {
return Ok(Self::under(PathBuf::from(home)));
}
let dirs = directories::ProjectDirs::from("", "", "capri").context("No home directory")?;
Ok(Self {
config: dirs.config_dir().to_path_buf(),
data: dirs.data_dir().to_path_buf(),
cache: dirs.cache_dir().to_path_buf(),
})
}
pub fn under(root: PathBuf) -> Self {
Self { config: root.join("config"), data: root.join("data"), cache: root.join("cache") }
}
/// Workspace: PERSONALITY.md, USER.md, MEMORY.md, memory/, .capri/.
pub fn workspace(&self) -> PathBuf {
self.data.join("workspace")
}
/// Desktop state: transcript.json and other UI state.
pub fn state(&self) -> PathBuf {
self.data.join("desktop")
}
pub fn agent_db(&self) -> PathBuf {
self.data.join("agent").join("session.sqlite")
}
pub fn settings_file(&self) -> PathBuf {
self.config.join("settings.json")
}
pub fn logs(&self) -> PathBuf {
self.data.join("logs")
}
/// Creates the private directories (mode 0700).
pub fn ensure(&self) -> Result<()> {
use std::os::unix::fs::DirBuilderExt;
for dir in [
self.config.clone(),
self.data.clone(),
self.cache.clone(),
self.workspace(),
self.state(),
self.data.join("agent"),
self.logs(),
] {
std::fs::DirBuilder::new()
.recursive(true)
.mode(0o700)
.create(&dir)
.with_context(|| format!("Cannot create {}", dir.display()))?;
}
Ok(())
}
}