use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Blob {
pub url: String,
pub filename: String,
pub digest: Option<String>,
pub size_bytes: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FetchPlan {
pub key: String,
pub display_name: String,
pub blobs: Vec<Blob>,
pub quant: Option<String>,
pub quant_was_chosen_for_you: bool,
}
impl FetchPlan {
pub fn total_bytes(&self) -> Option<u64> {
self.blobs.iter().map(|b| b.size_bytes).sum()
}
pub fn is_multipart(&self) -> bool {
self.blobs.len() > 1
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolveError(pub String);
impl std::fmt::Display for ResolveError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::error::Error for ResolveError {}
fn err(m: impl Into<String>) -> ResolveError {
ResolveError(m.into())
}
pub const OLLAMA_MODEL_MEDIA_TYPE: &str = "application/vnd.ollama.image.model";
pub fn plan_from_ollama_manifest(
manifest: &Value,
base: &str,
namespace: &str,
name: &str,
key: String,
display_name: String,
) -> Result<FetchPlan, ResolveError> {
let layers = manifest
.get("layers")
.and_then(|l| l.as_array())
.ok_or_else(|| err("manifest has no layers array"))?;
let model_layers: Vec<&Value> = layers
.iter()
.filter(|l| l.get("mediaType").and_then(|m| m.as_str()) == Some(OLLAMA_MODEL_MEDIA_TYPE))
.collect();
if model_layers.is_empty() {
let seen: Vec<&str> = layers
.iter()
.filter_map(|l| l.get("mediaType").and_then(|m| m.as_str()))
.collect();
return Err(err(format!(
"no {OLLAMA_MODEL_MEDIA_TYPE} layer in this manifest — saw: {}",
if seen.is_empty() {
"nothing".to_string()
} else {
seen.join(", ")
}
)));
}
let mut blobs = Vec::with_capacity(model_layers.len());
for (i, layer) in model_layers.iter().enumerate() {
let digest = layer
.get("digest")
.and_then(|d| d.as_str())
.ok_or_else(|| err("a model layer has no digest"))?;
let size = layer.get("size").and_then(|s| s.as_u64());
blobs.push(Blob {
url: format!(
"{}/v2/{namespace}/{name}/blobs/{digest}",
base.trim_end_matches('/')
),
filename: if model_layers.len() == 1 {
"model.gguf".to_string()
} else {
format!("model-{:05}.gguf", i + 1)
},
digest: Some(digest.to_string()),
size_bytes: size,
});
}
Ok(FetchPlan {
key,
display_name,
blobs,
quant: None,
quant_was_chosen_for_you: false,
})
}
pub const QUANT_PREFERENCE: &[&str] = &[
"Q4_K_M", "Q4_K_S", "Q5_K_M", "Q5_K_S", "Q6_K", "Q8_0", "Q4_0", "Q3_K_M", "F16",
];
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RepoFile {
pub path: String,
pub size_bytes: Option<u64>,
}
pub fn pick_gguf(
files: &[RepoFile],
wanted: Option<&str>,
) -> Result<(Vec<RepoFile>, String, bool), ResolveError> {
let ggufs: Vec<&RepoFile> = files
.iter()
.filter(|f| f.path.to_ascii_lowercase().ends_with(".gguf"))
.collect();
if ggufs.is_empty() {
return Err(err(
"no .gguf files in this repo — hearth serves GGUF, so a repo of \
safetensors needs converting first",
));
}
let available = available_quants(&ggufs);
let (chosen, was_chosen_for_you) = match wanted {
Some(w) => {
let up = w.to_ascii_uppercase();
if !available.iter().any(|q| q == &up) {
return Err(err(format!(
"no {up} in this repo — available: {}",
if available.is_empty() {
"none detected".into()
} else {
available.join(", ")
}
)));
}
(up, false)
}
None => {
let pick = QUANT_PREFERENCE
.iter()
.find(|p| available.iter().any(|q| q == *p))
.map(|p| p.to_string())
.or_else(|| (ggufs.len() == 1).then(|| "unknown".to_string()))
.ok_or_else(|| {
err(format!(
"could not choose between: {} — pin one with @QUANT",
available.join(", ")
))
})?;
(pick, true)
}
};
let mut parts: Vec<RepoFile> = if chosen == "unknown" {
ggufs.iter().map(|f| (*f).clone()).collect()
} else {
ggufs
.iter()
.filter(|f| quant_of(&f.path).as_deref() == Some(chosen.as_str()))
.map(|f| (*f).clone())
.collect()
};
parts.sort_by(|a, b| a.path.cmp(&b.path));
if let Some(expected) = parts.iter().find_map(|f| total_parts(&f.path)) {
if parts.len() as u32 != expected {
return Err(err(format!(
"{chosen} is split into {expected} parts but only {} are present — \
an incomplete split GGUF will download fine and then fail to load",
parts.len()
)));
}
}
Ok((parts, chosen, was_chosen_for_you))
}
pub fn available_quants(files: &[&RepoFile]) -> Vec<String> {
let mut out: Vec<String> = files.iter().filter_map(|f| quant_of(&f.path)).collect();
out.sort();
out.dedup();
out
}
pub fn quant_of(path: &str) -> Option<String> {
let stem = path.rsplit('/').next().unwrap_or(path);
let stem = stem
.strip_suffix(".gguf")
.or_else(|| stem.strip_suffix(".GGUF"))?;
stem.split(['.', '-', '_'])
.collect::<Vec<_>>()
.windows(1)
.filter_map(|w| w.first().copied())
.next()
.and_then(|_| {
let upper = stem.to_ascii_uppercase();
KNOWN_QUANTS
.iter()
.filter(|q| is_token(&upper, q))
.max_by_key(|q| q.len())
.map(|q| q.to_string())
})
}
const KNOWN_QUANTS: &[&str] = &[
"Q2_K", "Q3_K_S", "Q3_K_M", "Q3_K_L", "Q3_K", "Q4_0", "Q4_1", "Q4_K_S", "Q4_K_M", "Q4_K",
"Q5_0", "Q5_1", "Q5_K_S", "Q5_K_M", "Q5_K", "Q6_K", "Q8_0", "IQ1_S", "IQ2_XXS", "IQ2_XS",
"IQ3_XXS", "IQ3_XS", "IQ4_XS", "IQ4_NL", "F16", "FP16", "BF16", "F32",
];
fn is_token(hay: &str, needle: &str) -> bool {
let bytes = hay.as_bytes();
let n = needle.as_bytes();
let sep = |b: u8| !(b.is_ascii_alphanumeric());
let mut i = 0;
while let Some(pos) = hay[i..].find(needle) {
let start = i + pos;
let end = start + n.len();
let before_ok = start == 0 || sep(bytes[start - 1]);
let after_ok = end == bytes.len() || sep(bytes[end]);
if before_ok && after_ok {
return true;
}
i = start + 1;
if i >= hay.len() {
break;
}
}
false
}
pub fn total_parts(path: &str) -> Option<u32> {
let stem = path.rsplit('/').next()?;
let idx = stem.to_ascii_lowercase().find("-of-")?;
let after = &stem[idx + 4..];
let digits: String = after.chars().take_while(|c| c.is_ascii_digit()).collect();
digits.parse().ok()
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HfSource<'a> {
pub owner: &'a str,
pub repo: &'a str,
pub revision: &'a str,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct QuantChoice {
pub quant: String,
pub chosen_for_you: bool,
}
pub fn plan_from_hf_files(
src: HfSource<'_>,
files: Vec<RepoFile>,
choice: QuantChoice,
key: String,
display_name: String,
) -> FetchPlan {
let (owner, repo, revision) = (src.owner, src.repo, src.revision);
let QuantChoice {
quant,
chosen_for_you,
} = choice;
let blobs = files
.into_iter()
.map(|f| Blob {
url: format!(
"https://huggingface.co/{owner}/{repo}/resolve/{revision}/{}",
f.path
),
filename: f.path.rsplit('/').next().unwrap_or(&f.path).to_string(),
digest: None,
size_bytes: f.size_bytes,
})
.collect();
FetchPlan {
key,
display_name,
blobs,
quant: Some(quant),
quant_was_chosen_for_you: chosen_for_you,
}
}