use std::path::PathBuf;
pub mod bundled {
pub const HEADS: &[u8] = include_bytes!("../assets/heads.safetensors");
pub fn have_heads() -> bool {
HEADS.len() > 1024
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Source {
Bundled,
Ours { repo: &'static str, rev: &'static str },
Upstream { repo: &'static str, license: &'static str, rev: &'static str },
Mirrored {
ours: &'static str,
upstream: &'static str,
license: &'static str,
rev: &'static str,
},
}
impl Source {
pub fn license(&self) -> Option<&'static str> {
match self {
Source::Bundled | Source::Ours { .. } => None,
Source::Upstream { license, .. } | Source::Mirrored { license, .. } => Some(license),
}
}
pub fn attribution(&self) -> Option<&'static str> {
match self {
Source::Bundled | Source::Ours { .. } => None,
Source::Upstream { repo, .. } => Some(repo),
Source::Mirrored { upstream, .. } => Some(upstream),
}
}
pub fn revision(&self) -> &'static str {
match self {
Source::Bundled => "bundled",
Source::Ours { rev, .. } | Source::Upstream { rev, .. } | Source::Mirrored { rev, .. } => rev,
}
}
pub fn fetch_repo(&self) -> Option<&'static str> {
match self {
Source::Bundled => None,
Source::Ours { repo, .. } => Some(repo),
Source::Upstream { repo, .. } => Some(repo),
Source::Mirrored { ours, .. } => Some(ours),
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct Artifact {
pub name: &'static str,
pub env: &'static str,
pub dir: &'static str,
pub marker: &'static str,
pub source: Source,
pub approx_mb: u32,
pub purpose: &'static str,
}
pub const ARTIFACTS: &[Artifact] = &[
Artifact {
name: "embeddings",
env: "STEELDB_EMBEDDINGS",
dir: "bert-tiny",
marker: "model.safetensors",
source: Source::Mirrored {
ours: "cp500/steeldb-models",
upstream: "google/bert_uncased_L-2_H-128_A-2",
license: "Apache-2.0",
rev: "main",
},
approx_mb: 16,
purpose: "the embedding table the bundled heads sit on; 15.2 MB of it is the vocabulary alone",
},
Artifact {
name: "spo-tagger",
env: "STEELDB_ML_BUNDLE",
dir: "step0_bundle_ml",
marker: "spo.onnx",
source: Source::Ours { repo: "cp500/steeldb-models", rev: "main" },
approx_mb: 168,
purpose: "typed span tagger: text becomes ENT/REL/GEO/TIME/QTY spans",
},
Artifact {
name: "splade",
env: "STEELDB_SPLADE_DIR",
dir: "splade",
marker: "splade.onnx",
source: Source::Ours { repo: "cp500/steeldb-models", rev: "main" },
approx_mb: 107,
purpose: "learned facet heads: the projection that builds the bitmap",
},
Artifact {
name: "model2vec",
env: "STEELDB_MODEL2VEC",
dir: "model2vec",
marker: "potion.f32",
source: Source::Mirrored {
ours: "cp500/steeldb-models",
upstream: "minishlab/potion-base-4M",
license: "MIT",
rev: "main",
},
approx_mb: 15,
purpose: "static embeddings for optimal-transport ontology discovery",
},
Artifact {
name: "needle3",
env: "STEELDB_NEEDLE_DIR",
dir: "needle3",
marker: "needle3.cact",
source: Source::Mirrored {
ours: "cp500/steeldb-models",
upstream: "Cactus-Compute/needle3",
license: "Apache-2.0",
rev: "b274efcb211a9eef48c9a88da4b43bd569696a39",
},
approx_mb: 242,
purpose: "121M tool-calling model used as the query planner",
},
];
#[derive(Debug, Clone)]
pub enum ModelError {
Unknown(String),
NotFound { name: String, searched: Vec<PathBuf>, hint: String },
}
impl std::fmt::Display for ModelError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ModelError::Unknown(n) => write!(f, "unknown model '{n}'"),
ModelError::NotFound { name, searched, hint } => {
writeln!(f, "model '{name}' not found. Looked in:")?;
for p in searched {
writeln!(f, " {}", p.display())?;
}
write!(f, "{hint}")
}
}
}
}
impl std::error::Error for ModelError {}
pub fn artifact(name: &str) -> Option<&'static Artifact> {
ARTIFACTS.iter().find(|a| a.name == name)
}
pub fn search_roots() -> Vec<PathBuf> {
let mut roots = Vec::new();
if let Ok(home) = std::env::var("HOME") {
roots.push(PathBuf::from(home).join(".steeldb").join("models"));
}
if let Ok(dir) = std::env::var("STEELDB_MODELS") {
roots.insert(0, PathBuf::from(dir));
}
roots.push(PathBuf::from("models"));
roots
}
pub fn resolve(name: &str) -> Result<PathBuf, ModelError> {
let art = artifact(name).ok_or_else(|| ModelError::Unknown(name.to_string()))?;
if let Ok(dir) = std::env::var(art.env) {
let p = PathBuf::from(dir);
if p.join(art.marker).exists() {
return Ok(p);
}
}
let mut searched = Vec::new();
for root in search_roots() {
let cand = root.join(art.dir);
if cand.join(art.marker).exists() {
return Ok(cand);
}
searched.push(cand);
}
Err(ModelError::NotFound { name: name.to_string(), searched, hint: fetch_hint(art) })
}
pub fn fetch_hint(art: &Artifact) -> String {
let root = search_roots().first().cloned().unwrap_or_else(|| PathBuf::from("models"));
match art.source.fetch_repo() {
None => "this artifact ships with the crate; the build is incomplete".to_string(),
Some(repo) => {
let credit = match art.source.attribution() {
Some(up) => format!("\n {up} — {}", art.source.license().unwrap_or("see model card")),
None => String::new(),
};
format!(
"Fetch it ({} MB) with:\n \
huggingface-cli download {repo} --revision {} --include '{}/*' --local-dir {}\n\
or set {}=/path/to/{}{credit}",
art.approx_mb,
art.source.revision(),
art.dir,
root.display(),
art.env,
art.dir
)
}
}
}
pub fn status() -> Vec<(&'static str, Option<PathBuf>)> {
ARTIFACTS.iter().map(|a| (a.name, resolve(a.name).ok())).collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn third_party_weights_keep_their_licence_and_credit() {
for a in ARTIFACTS {
match a.source {
Source::Ours { repo, .. } => {
assert!(repo.starts_with("cp500/"), "{} claims ours but points at {repo}", a.name);
assert!(a.source.license().is_none(), "our own work needs no third-party licence");
}
Source::Upstream { license, .. } | Source::Mirrored { license, .. } => {
assert!(!license.is_empty(), "{} must record a licence", a.name);
let credit = a.source.attribution().expect("third-party work needs attribution");
assert!(
!credit.starts_with("cp500/"),
"{} credits us for someone else's work",
a.name
);
}
Source::Bundled => {}
}
}
}
#[test]
fn every_fetched_artifact_pins_a_revision() {
for a in ARTIFACTS {
if a.source.fetch_repo().is_some() {
assert!(!a.source.revision().is_empty(), "{} must pin a revision", a.name);
}
}
let n = artifact("needle3").unwrap();
assert_eq!(n.source.revision().len(), 40, "needle3 should pin an exact commit");
}
#[test]
fn the_fetch_hint_downloads_into_the_models_ROOT_not_the_artifact_directory() {
std::env::set_var("STEELDB_MODELS", "/models-root");
let hint = fetch_hint(artifact("spo-tagger").unwrap());
assert!(hint.contains("--include 'step0_bundle_ml/*'"), "must select the subdirectory: {hint}");
assert!(hint.contains("--local-dir /models-root"), "must target the ROOT: {hint}");
assert!(
!hint.contains("--local-dir /models-root/step0_bundle_ml"),
"must not nest the artifact directory inside itself: {hint}"
);
std::env::remove_var("STEELDB_MODELS");
}
#[test]
fn a_missing_model_explains_how_to_get_it() {
std::env::set_var("STEELDB_MODELS", "/nonexistent-steeldb-test-root");
let err = resolve("needle3").unwrap_err();
let msg = err.to_string();
assert!(msg.contains("not found"), "{msg}");
assert!(msg.contains("Cactus-Compute/needle3"), "must credit the author: {msg}");
assert!(msg.contains("Apache-2.0"), "must state the licence: {msg}");
assert!(msg.contains("--revision"), "must pin a revision: {msg}");
assert!(msg.contains("242 MB"), "must state the size: {msg}");
std::env::remove_var("STEELDB_MODELS");
}
#[test]
fn an_unknown_name_is_an_error_not_a_panic() {
assert!(matches!(resolve("no-such-model"), Err(ModelError::Unknown(_))));
}
#[test]
fn every_artifact_is_uniquely_named_and_documented() {
let mut seen = std::collections::HashSet::new();
for a in ARTIFACTS {
assert!(seen.insert(a.name), "duplicate artifact name {}", a.name);
assert!(!a.purpose.is_empty(), "{} needs a purpose", a.name);
assert!(a.env.starts_with("STEELDB_"), "{} env var should be namespaced", a.name);
}
}
}