Skip to main content

rigspark_runtime/
local_catalog.rs

1//! Models the user added from Hugging Face, kept beside the signed catalog in
2//! `~/.rigspark/catalog/local.json`. Entries stay pinned to a commit and SHA-256, so `up`
3//! verifies them exactly like catalog models.
4
5use crate::secure_fs::Directory;
6use rigspark_core::catalog::{Catalog, CatalogModel, EntryProvenance};
7use std::{
8    io::ErrorKind,
9    path::{Path, PathBuf},
10};
11
12const FILE: &str = "catalog/local.json";
13const MAX_BYTES: u64 = 4 * 1024 * 1024;
14const MAX_MODELS: usize = 256;
15
16#[derive(Debug, thiserror::Error)]
17pub enum LocalCatalogError {
18    #[error("your added models could not be read; fix or delete {0}")]
19    Corrupt(String),
20    #[error("{0}")]
21    Invalid(String),
22    #[error("{0} is already in the RigSpark catalog")]
23    Conflict(String),
24    #[error("you can add at most {MAX_MODELS} models")]
25    Full,
26    #[error(transparent)]
27    Io(#[from] std::io::Error),
28}
29
30pub struct LocalCatalog {
31    home: PathBuf,
32}
33
34impl LocalCatalog {
35    pub fn new(home: impl Into<PathBuf>) -> Self {
36        Self { home: home.into() }
37    }
38
39    pub fn path(&self) -> PathBuf {
40        self.home.join(FILE)
41    }
42
43    /// Added models; none when nothing was added yet.
44    pub fn models(&self) -> Result<Vec<CatalogModel>, LocalCatalogError> {
45        let bytes = match Directory::open(&self.home)
46            .and_then(|directory| directory.read(Path::new(FILE), MAX_BYTES, false))
47        {
48            Ok(bytes) => bytes,
49            Err(error) if error.kind() == ErrorKind::NotFound => return Ok(Vec::new()),
50            Err(error) => return Err(error.into()),
51        };
52        let text = std::str::from_utf8(&bytes)
53            .map_err(|_| LocalCatalogError::Corrupt(self.path().display().to_string()))?;
54        Catalog::parse_local(text)
55            .map(|catalog| catalog.models)
56            .map_err(|_| LocalCatalogError::Corrupt(self.path().display().to_string()))
57    }
58
59    /// Adds or replaces `entry`; ids already in the signed or bundled catalog are refused.
60    pub fn add(&self, entry: CatalogModel) -> Result<(), LocalCatalogError> {
61        if entry.provenance != EntryProvenance::Local {
62            return Err(LocalCatalogError::Invalid(
63                "only models added from Hugging Face can be stored locally".into(),
64            ));
65        }
66        let published = crate::catalog_update::CatalogStore::official(&self.home)
67            .load_published()
68            .map_err(|error| LocalCatalogError::Invalid(error.to_string()))?;
69        if published.models.iter().any(|model| model.id == entry.id) {
70            return Err(LocalCatalogError::Conflict(entry.id));
71        }
72        let mut models = self.models()?;
73        models.retain(|model| model.id != entry.id);
74        if models.len() >= MAX_MODELS {
75            return Err(LocalCatalogError::Full);
76        }
77        models.push(entry);
78        models.sort_by(|left, right| left.id.cmp(&right.id));
79        self.write(models)
80    }
81
82    /// Removes an added model; `false` when it was not there.
83    pub fn remove(&self, id: &str) -> Result<bool, LocalCatalogError> {
84        let mut models = self.models()?;
85        let before = models.len();
86        models.retain(|model| model.id != id);
87        if models.len() == before {
88            return Ok(false);
89        }
90        self.write(models)?;
91        Ok(true)
92    }
93
94    fn write(&self, models: Vec<CatalogModel>) -> Result<(), LocalCatalogError> {
95        let document = serde_json::json!({
96            "schemaVersion": rigspark_core::catalog::SCHEMA_VERSION,
97            "generatedAt": now(),
98            "models": models,
99        });
100        let text = serde_json::to_string_pretty(&document)
101            .map_err(|error| LocalCatalogError::Invalid(error.to_string()))?;
102        Catalog::parse_local(&text).map_err(|error| LocalCatalogError::Invalid(error.0))?;
103        std::fs::create_dir_all(&self.home)?;
104        let directory = Directory::open(&self.home)?;
105        directory.ensure_dir(Path::new("catalog"))?;
106        let exists = self.path().exists();
107        directory.write(Path::new(FILE), text.as_bytes(), !exists, false)?;
108        Ok(())
109    }
110}
111
112fn now() -> String {
113    time::OffsetDateTime::now_utc()
114        .format(time::macros::format_description!(
115            "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3]Z"
116        ))
117        .unwrap_or_else(|_| "1970-01-01T00:00:00.000Z".into())
118}