Skip to main content

acorn/io/api/
huggingface.rs

1//! Hugging Face Hub API helpers.
2use super::{Endpoint, IntoHeaders, Params, RemoteResource, ResponseContent, TreeEntry};
3use crate::io::api::{Param, RepositoryFileMetadata};
4use crate::io::config::FilterSet;
5use crate::io::download::{DownloadItem, DownloadItems};
6use crate::io::sync::Shard;
7use crate::io::{first_env_var, http, ApiResult, Source};
8use crate::prelude::{HashMap, Path};
9use crate::schema::agent::{ModelDetails, ModelSelector, Quantization, Weight, Weights, FALLBACK_MODEL_SUFFIXES};
10use crate::util::constants::app::{
11    DEFAULT_HUGGINGFACE_DOMAIN, DEFAULT_HUGGINGFACE_MINIMUM_DOWNLOAD_COUNT, DEFAULT_HUGGINGFACE_MODEL_REVISION, DEFAULT_HUGGINGFACE_SEARCH_LIMIT,
12    DEFAULT_HUGGINGFACE_SEARCH_TERM,
13};
14use crate::util::constants::env::HUGGINGFACE_TOKEN_VARIABLE_NAMES;
15use crate::util::{glob_matches, regex_to_glob, strip_suffixes, to_ascii_alphanumeric, Label};
16use crate::{Location, Repository};
17use alloc::boxed::Box;
18use alloc::collections::BTreeSet;
19use async_trait::async_trait;
20use axum::http::HeaderMap;
21use bon::Builder;
22use color_eyre::eyre::{eyre, Report};
23use core::{cmp::Reverse, fmt, ops::Deref};
24use futures::{future::join_all, TryStreamExt};
25use hf_hub::{repository::ModelInfo, HFClient, HFError};
26use owo_colors::OwoColorize;
27use serde::{Deserialize, Serialize};
28use serde_json::Value;
29use tracing::{info, warn};
30
31/// Candidate repositories returned by model search.
32pub type Candidates = Vec<Candidate>;
33/// Function used to select from multiple interactive candidates.
34pub type CandidateSelector = fn(Candidates, &Options) -> ApiResult<String>;
35/// Select a repository from model search candidates.
36pub trait CandidateSelection {
37    /// Build eligible fallback candidates from Hugging Face model metadata.
38    fn fallback(models: Vec<ModelInfo>, options: &SearchOptions) -> Self;
39    /// Select a candidate using the selector configured in `options` when interaction is required.
40    fn select(self, options: &Options) -> ApiResult<String>;
41    /// Fallback selector used when the interactive picker is unavailable.
42    ///
43    /// Picks the most-downloaded candidate and warns the user.
44    fn select_interactively(self, options: &Options) -> ApiResult<String>;
45}
46/// Trait for selecting and filtering files from a Hugging Face repository.
47#[async_trait]
48pub trait HuggingFaceRepository {
49    /// Resolve SHA-256 checksums for repository files from `.sha256` sidecar files
50    async fn checksums(&self, identifier: &str, revision: &str) -> ApiResult<HashMap<String, String>>;
51    /// Download files and return the selected repository metadata
52    async fn download(&self, options: &Options) -> ApiResult<Downloaded>;
53    /// Filter repository files using glob patterns for include/exclude.
54    fn filter(&self, filter: Option<&str>, ignore: Option<&str>) -> ApiResult<Self>
55    where
56        Self: Sized;
57    /// Select preferred files based on a marker pattern, erroring on ambiguity.
58    fn select(&self, policy: &FileSelectionPolicy<'_>) -> ApiResult<Self>
59    where
60        Self: Sized;
61    /// Determine whether a failed download should trigger GGUF repository fallback discovery.
62    fn should_use_fallback(&self, options: &Options) -> bool;
63    /// Try to filter repository files using glob patterns, returning `None` when regex fallback is required.
64    fn try_glob(&self, filter: Option<&str>, ignore: Option<&str>) -> Option<Self>
65    where
66        Self: Sized;
67}
68/// Extension trait for Hugging Face model metadata
69pub trait ModelInfoExtension {
70    /// Determine whether a Hugging Face model repository contains GGUF files
71    fn has_gguf_files(&self) -> bool;
72    /// Determine whether a Hugging Face model is an eligible fallback for an identifier.
73    fn is_fallback_for(&self, identifier: &str) -> bool;
74    /// Determine whether a Hugging Face model declares a given model as its exact base model.
75    ///
76    /// For example, a GGUF repository declaring `openai/gpt-oss-20b` is a derivative of
77    /// `openai/gpt-oss-20b`.
78    fn is_declared_derivative_of(&self, identifier: &str) -> bool;
79    /// Determine whether a Hugging Face model declares a decorated variant of a given base model.
80    ///
81    /// For example, a GGUF repository declaring `nvidia/NVIDIA-Nemotron-3-Super-120B-A12B-BF16`
82    /// is a variant of `nvidia/nemotron-3-super-120b-a12b`.
83    fn is_declared_variant_of(&self, identifier: &str) -> bool;
84}
85/// Errors produced by Hugging Face repository selection and download helpers
86#[derive(Clone, Debug, Eq, PartialEq)]
87pub enum HuggingFaceError {
88    /// No GGUF model files were present in the repository tree
89    NoGgufModelFiles,
90    /// No GGUF quantization repository declares the requested base model
91    NoGgufQuantizationRepository {
92        /// Requested Hugging Face base-model identifier
93        identifier: Box<str>,
94    },
95    /// The requested Hugging Face base-model identifier is invalid
96    InvalidBaseModelIdentifier {
97        /// Invalid Hugging Face base-model identifier
98        identifier: Box<str>,
99    },
100    /// The Hugging Face client could not be initialized
101    ClientInitializationFailed {
102        /// Underlying client initialization error
103        reason: Box<str>,
104    },
105    /// The Hugging Face model search could not be configured
106    ModelSearchConfigurationFailed {
107        /// Underlying search configuration error
108        reason: Box<str>,
109    },
110    /// The Hugging Face model search failed while reading results
111    ModelSearchFailed {
112        /// Underlying model search error
113        reason: Box<str>,
114    },
115}
116/// GGUF repository metadata returned by fallback discovery.
117#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
118pub struct Candidate {
119    /// Hugging Face repository identifier
120    pub id: String,
121    /// Downloads during the repository's reported period
122    pub downloads: u64,
123    /// Repository like count
124    pub likes: Option<u64>,
125    /// Quantization formats detected in repository filenames
126    pub quantizations: Vec<String>,
127}
128/// A repository-backed value together with its requested and resolved identifiers.
129#[derive(Clone, Debug, Eq, PartialEq)]
130pub struct RepositoryResolution<T> {
131    requested: String,
132    resolved: String,
133    value: T,
134}
135/// Hugging Face repository files selected and downloaded for a model
136#[derive(Builder, Clone, Debug, Eq, PartialEq)]
137#[builder(start_fn = init, on(String, into))]
138pub struct Downloaded {
139    /// Hugging Face repository identifier
140    pub identifier: String,
141    /// Repository revision used for the download
142    pub revision: String,
143    /// Repository-relative paths downloaded from the repository
144    pub files: Vec<String>,
145}
146/// Policy for selecting a preferred file when multiple candidates exist.
147#[derive(Clone, Debug)]
148pub struct FileSelectionPolicy<'a> {
149    /// Substring to match in the filename (case-insensitive) to identify the preferred file.
150    pub preferred_marker: &'a str,
151    /// Error message to display when no files match.
152    pub no_match_message: &'a str,
153}
154/// Options for Hugging Face model fallback searches.
155#[derive(Builder, Clone, Debug)]
156#[builder(start_fn = init, on(String, into))]
157pub struct SearchOptions {
158    /// Base model identifier used to find related GGUF repositories.
159    pub identifier: String,
160    /// Hugging Face search filter.
161    #[builder(default = DEFAULT_HUGGINGFACE_SEARCH_TERM.to_string())]
162    pub term: String,
163    /// Maximum number of repositories considered during fallback discovery.
164    #[builder(default = DEFAULT_HUGGINGFACE_SEARCH_LIMIT)]
165    pub limit: usize,
166    /// Minimum number of downloads required for a fallback repository.
167    #[builder(default = DEFAULT_HUGGINGFACE_MINIMUM_DOWNLOAD_COUNT)]
168    pub minimum_download_count: u64,
169    /// Whether fallback candidates should be selected interactively.
170    #[builder(default)]
171    pub interactive: bool,
172}
173/// Options for HuggingFace API requests
174#[derive(Builder, Clone, Debug)]
175#[builder(start_fn = init, on(String, into))]
176pub struct Options {
177    /// Authentication token
178    pub token: Option<String>,
179    /// Model registry domain (default: "huggingface.co")
180    #[builder(default = String::from("huggingface.co"))]
181    pub domain: String,
182    /// Model repository identifier
183    pub identifier: Option<String>,
184    /// Model repository revision (branch, tag, or commit)
185    #[builder(default = String::from(DEFAULT_HUGGINGFACE_MODEL_REVISION))]
186    pub revision: String,
187    /// Repository path used for tree requests
188    pub path: Option<String>,
189    /// Regex pattern of files to include at a given path desginated by `path`
190    pub filter: Option<String>,
191    /// Regex pattern of files to ignore at a given path desginated by `path`
192    pub ignore: Option<String>,
193    /// Flag used to suppress output
194    #[builder(default)]
195    pub quiet: bool,
196    /// Whether network-backed fallback discovery is disabled by offline mode
197    #[builder(default)]
198    pub offline: bool,
199    /// Whether automatic GGUF quantization repository discovery is disabled
200    #[builder(default)]
201    pub no_fallback: bool,
202    /// Maximum number of repositories considered during GGUF fallback discovery
203    #[builder(default = DEFAULT_HUGGINGFACE_SEARCH_LIMIT)]
204    pub search_limit: usize,
205    /// Minimum number of downloads required for a GGUF fallback repository
206    #[builder(default = DEFAULT_HUGGINGFACE_MINIMUM_DOWNLOAD_COUNT)]
207    pub minimum_download_count: u64,
208    /// Whether multiple GGUF fallback repositories should be selected interactively
209    #[builder(default)]
210    pub interactive: bool,
211    /// Selector used when multiple repositories require interactive selection
212    #[builder(default = select_first)]
213    pub selector: CandidateSelector,
214    /// Custom API parameters to include in every request
215    #[builder(default = vec![])]
216    pub custom_params: Vec<Param>,
217    /// Skip SHA-256 checksum verification after download
218    #[builder(default)]
219    pub skip_verify_checksum: bool,
220    /// Output directory for downloaded files
221    pub output: Option<String>,
222}
223/// File metadata returned by the Hugging Face repository tree API
224#[derive(Clone, Debug, PartialEq, Eq)]
225pub struct HuggingFaceRepositoryFile {
226    /// Repository-relative path
227    pub path: String,
228    /// File size in bytes, when provided by Hugging Face
229    pub size: Option<u64>,
230}
231/// Repository identity and files returned by the Hugging Face tree API.
232#[derive(Clone, Debug, PartialEq, Eq)]
233pub struct HuggingFaceRepositoryFiles {
234    /// Repository-relative file metadata.
235    pub files: Vec<HuggingFaceRepositoryFile>,
236    /// Hugging Face repository identifier.
237    pub identifier: String,
238    /// Repository revision used for the tree request.
239    pub revision: String,
240}
241impl RepositoryFileMetadata for HuggingFaceRepositoryFile {
242    fn path(&self) -> &str {
243        &self.path
244    }
245    fn size(&self) -> Option<u64> {
246        self.size
247    }
248}
249impl HuggingFaceRepositoryFiles {
250    /// Create repository file metadata with its repository identity.
251    pub fn new(identifier: impl Into<String>, revision: impl Into<String>, files: Vec<HuggingFaceRepositoryFile>) -> Self {
252        Self {
253            files,
254            identifier: identifier.into(),
255            revision: revision.into(),
256        }
257    }
258    /// Returns a complete split GGUF shard set as one logical candidate.
259    /// Repository listings expose each shard as a separate file, which can otherwise look like multiple candidates.
260    fn complete_shard_set(&self) -> Option<Self> {
261        let shards = self
262            .iter()
263            .filter_map(|file| Shard::parts(&file.path.to_ascii_lowercase()))
264            .collect::<Vec<_>>();
265        shards.first().and_then(|(key, _, count)| {
266            let indexes = shards
267                .iter()
268                .filter(|(candidate_key, _, candidate_count)| candidate_key == key && candidate_count == count)
269                .map(|(_, index, _)| *index)
270                .collect::<BTreeSet<_>>();
271            let expected = (1..=*count).collect::<BTreeSet<_>>();
272            match indexes == expected && usize::try_from(*count).ok() == Some(shards.len()) {
273                | true => Some(self.clone()),
274                | false => None,
275            }
276        })
277    }
278    fn candidate_report(&self) -> String {
279        match self.len() {
280            | 0 => "no GGUF files".to_string(),
281            | 1 | 2 => self.iter().map(|file| file.path.as_str()).collect::<Vec<_>>().join(", "),
282            | count => format!("{count} GGUF files"),
283        }
284    }
285    fn parse(content: &str, options: &Options) -> ApiResult<Self> {
286        #[derive(Deserialize)]
287        struct ApiError {
288            #[serde(alias = "message")]
289            error: String,
290        }
291        match options.identifier.as_deref() {
292            | Some(identifier) => serde_json::from_str::<Vec<TreeEntry>>(content)
293                .map(|entries| {
294                    let files = entries
295                        .into_iter()
296                        .filter(TreeEntry::is_file)
297                        .map(HuggingFaceRepositoryFile::from)
298                        .collect();
299                    Self::new(identifier, &options.revision, files)
300                })
301                .map_err(|why| {
302                    serde_json::from_str::<ApiError>(content).map_or_else(
303                        |_| eyre!("Failed to parse Hugging Face repository file list for '{identifier}' — {why}"),
304                        |response| eyre!("Hugging Face API rejected repository '{identifier}' — {}", response.error),
305                    )
306                }),
307            | None => Err(eyre!("Missing Hugging Face repository identifier")),
308        }
309    }
310    /// Resolve the configured repository or a selected GGUF fallback repository.
311    pub async fn resolve(options: &Options) -> ApiResult<RepositoryResolution<Self>> {
312        let Options {
313            identifier,
314            no_fallback,
315            revision,
316            search_limit,
317            minimum_download_count,
318            interactive,
319            ..
320        } = options;
321        match identifier.as_deref() {
322            | Some(identifier) => match repository_tree(identifier, revision).await {
323                | Ok(repository) if repository.files.iter().any(|file| Quantization::from_gguf_filename(&file.path).is_some()) => {
324                    Ok(RepositoryResolution::new(identifier, identifier, repository))
325                }
326                | Ok(_) if *no_fallback => Err(eyre!("No GGUF model files found for '{identifier}'")),
327                | Err(why) if *no_fallback => Err(why),
328                | Ok(_) | Err(_) => {
329                    let search_options = SearchOptions::init()
330                        .identifier(identifier)
331                        .limit(*search_limit)
332                        .minimum_download_count(*minimum_download_count)
333                        .interactive(*interactive)
334                        .build();
335                    match search(&search_options).await {
336                        | Ok(candidates) => match candidates.select(options) {
337                            | Ok(resolved) => match repository_tree(&resolved, DEFAULT_HUGGINGFACE_MODEL_REVISION).await {
338                                | Ok(repository) => Ok(RepositoryResolution::new(identifier, resolved, repository)),
339                                | Err(why) => Err(why),
340                            },
341                            | Err(why) => Err(why),
342                        },
343                        | Err(why) => Err(why),
344                    }
345                }
346            },
347            | None => Err(eyre!("Missing Hugging Face repository identifier")),
348        }
349    }
350}
351impl Deref for HuggingFaceRepositoryFiles {
352    type Target = [HuggingFaceRepositoryFile];
353    fn deref(&self) -> &Self::Target {
354        &self.files
355    }
356}
357impl IntoIterator for HuggingFaceRepositoryFiles {
358    type Item = HuggingFaceRepositoryFile;
359    type IntoIter = alloc::vec::IntoIter<HuggingFaceRepositoryFile>;
360    fn into_iter(self) -> Self::IntoIter {
361        self.files.into_iter()
362    }
363}
364impl From<HuggingFaceRepositoryFiles> for Weights {
365    fn from(repository: HuggingFaceRepositoryFiles) -> Self {
366        Weights(
367            repository
368                .files
369                .into_iter()
370                .filter_map(|file| {
371                    Quantization::from_gguf_filename(&file.path).map(|quantization| Weight {
372                        label: file.path.clone(),
373                        url: format!(
374                            "https://{DEFAULT_HUGGINGFACE_DOMAIN}/{}/resolve/{}/{}",
375                            repository.identifier, repository.revision, file.path
376                        ),
377                        is_open: None,
378                        quantization: Some(quantization),
379                        size: file.size,
380                    })
381                })
382                .collect(),
383        )
384    }
385}
386impl fmt::Display for Candidate {
387    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
388        formatter.write_str(&self.id)
389    }
390}
391impl From<ModelInfo> for Candidate {
392    fn from(model: ModelInfo) -> Self {
393        let quantizations = model
394            .siblings
395            .unwrap_or_default()
396            .iter()
397            .filter_map(|sibling| Quantization::from_gguf_filename(&sibling.rfilename).map(|quantization| quantization.to_string()))
398            .collect::<BTreeSet<_>>()
399            .into_iter()
400            .collect();
401        Self {
402            id: model.id,
403            downloads: model.downloads.unwrap_or_default(),
404            likes: model.likes,
405            quantizations,
406        }
407    }
408}
409impl CandidateSelection for Candidates {
410    fn fallback(models: Vec<ModelInfo>, options: &SearchOptions) -> Self {
411        let SearchOptions {
412            identifier,
413            minimum_download_count,
414            interactive,
415            ..
416        } = options;
417        let mut candidates = models
418            .into_iter()
419            .filter(|model| model.is_fallback_for(identifier))
420            .map(Candidate::from)
421            .filter(|candidate| !candidate.quantizations.is_empty())
422            .collect::<Vec<_>>();
423        candidates.sort_by_key(|candidate| Reverse(candidate.downloads));
424        let rejected = |candidate: &&Candidate| candidate.downloads < *minimum_download_count;
425        let report = |candidate: &Candidate| {
426            let Candidate { id, downloads, .. } = candidate;
427            let context = format!("{} {identifier}", "fallback from".italic());
428            let reason = format!("({downloads} below minimum {minimum_download_count} popularity)");
429            warn!("=> {}{} {} {}", Label::rejected(), id.yellow(), context.dimmed(), reason.dimmed(),);
430        };
431        match interactive {
432            | true => candidates.iter().filter(rejected).for_each(report),
433            | false => candidates.first().filter(rejected).into_iter().for_each(report),
434        }
435        candidates
436            .into_iter()
437            .filter(|candidate| candidate.downloads >= *minimum_download_count)
438            .collect()
439    }
440    fn select(self, options: &Options) -> ApiResult<String> {
441        let base_model = options.identifier.as_deref().unwrap_or_default();
442        match self.as_slice() {
443            | [] => Err(eyre!(HuggingFaceError::NoGgufQuantizationRepository {
444                identifier: base_model.into()
445            })),
446            | [candidate] => Ok(candidate.to_string()),
447            | [candidate, ..] if !options.interactive => {
448                if !options.quiet {
449                    info!(
450                        "=> {} {} {} {}",
451                        Label::using(),
452                        candidate.green(),
453                        format!("{} {base_model}", "fallback for".italic().dimmed()),
454                        "(using most popular)".dimmed(),
455                    );
456                }
457                Ok(candidate.to_string())
458            }
459            | _ => (options.selector)(self, options),
460        }
461    }
462    fn select_interactively(self, options: &Options) -> ApiResult<String> {
463        let candidate = self.first().ok_or_else(|| eyre!("GGUF search returned no candidates"))?;
464        if !options.quiet {
465            let reason = "(using most popular)";
466            warn!("=> {} {} {}", Label::using(), candidate.green(), reason.dimmed());
467        }
468        Ok(candidate.to_string())
469    }
470}
471impl Downloaded {
472    /// Wrap this download as a direct repository resolution.
473    pub fn into_resolution(self, identifier: impl Into<String>) -> RepositoryResolution<Self> {
474        RepositoryResolution::direct(identifier, self)
475    }
476    /// Merge downloaded GGUF quantizations into existing model weights.
477    pub fn merge_weights(&self, existing: Weights) -> Weights {
478        let downloaded = self
479            .files
480            .iter()
481            .filter_map(|path| {
482                let Self { identifier, revision, .. } = self;
483                Quantization::from_gguf_filename(path).map(|quantization| Weight {
484                    label: quantization.to_string(),
485                    url: format!("https://{DEFAULT_HUGGINGFACE_DOMAIN}/{identifier}/resolve/{revision}/{path}"),
486                    is_open: None,
487                    quantization: Some(quantization),
488                    size: None,
489                })
490            })
491            .filter(|candidate| {
492                !existing
493                    .0
494                    .iter()
495                    .any(|weight| weight.url == candidate.url && weight.quantization == candidate.quantization)
496            })
497            .collect::<Vec<_>>();
498        Weights(existing.0.into_iter().chain(downloaded).collect())
499    }
500}
501impl fmt::Display for HuggingFaceError {
502    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
503        match self {
504            | Self::NoGgufModelFiles => {
505                write!(
506                    formatter,
507                    "No GGUF model files found; use a GGUF repository or provide --filter for another format"
508                )
509            }
510            | Self::NoGgufQuantizationRepository { identifier } => {
511                write!(formatter, "no GGUF quantization repo found for {identifier}")
512            }
513            | Self::InvalidBaseModelIdentifier { identifier } => {
514                write!(formatter, "invalid Hugging Face base model identifier: {identifier}")
515            }
516            | Self::ClientInitializationFailed { reason } => {
517                write!(formatter, "failed to initialize Hugging Face client: {reason}")
518            }
519            | Self::ModelSearchConfigurationFailed { reason } => {
520                write!(formatter, "failed to configure Hugging Face model search: {reason}")
521            }
522            | Self::ModelSearchFailed { reason } => {
523                write!(formatter, "failed to search Hugging Face models: {reason}")
524            }
525        }
526    }
527}
528impl core::error::Error for HuggingFaceError {}
529impl From<TreeEntry> for HuggingFaceRepositoryFile {
530    fn from(entry: TreeEntry) -> Self {
531        HuggingFaceRepositoryFile {
532            path: entry.path,
533            size: entry.size,
534        }
535    }
536}
537#[async_trait]
538impl HuggingFaceRepository for HuggingFaceRepositoryFiles {
539    /// Resolve SHA-256 checksums for repository files by fetching `.sha256` sidecar files.
540    async fn checksums(&self, identifier: &str, revision: &str) -> ApiResult<HashMap<String, String>> {
541        let candidates: Vec<_> = self
542            .iter()
543            .filter_map(|file| target_path_from_sidecar(&file.path).map(|target| (file.path.clone(), target)))
544            .collect();
545        let digests = join_all(candidates.into_iter().map(|(path, target)| async move {
546            let url = format!("https://huggingface.co/{identifier}/resolve/{revision}/{path}");
547            let content = match http::get(url).headers(auth_headers()).send().await {
548                | Ok(response) => response.text().await.ok(),
549                | Err(_) => None,
550            };
551            (target, content.as_deref().and_then(extract_sha256))
552        }))
553        .await;
554        Ok(digests.into_iter().filter_map(|(target, digest)| digest.map(|d| (target, d))).collect())
555    }
556    async fn download(&self, options: &Options) -> ApiResult<Downloaded> {
557        let Options {
558            identifier,
559            revision,
560            filter,
561            ignore,
562            quiet,
563            skip_verify_checksum,
564            output,
565            ..
566        } = options;
567        match (identifier.as_deref(), output.as_deref()) {
568            | (Some(identifier), Some(output)) => match self.checksums(identifier, revision).await {
569                | Ok(lookup) => match self.filter(filter.as_deref(), ignore.as_deref()) {
570                    | Ok(files) => {
571                        let selected_files = files.iter().map(|file| file.path.clone()).collect();
572                        let items = files
573                            .into_iter()
574                            .map(|HuggingFaceRepositoryFile { path, size, .. }| {
575                                let sha = lookup.get(&path).cloned();
576                                let url = format!("https://huggingface.co/{identifier}/resolve/{revision}/{path}");
577                                DownloadItem { path, sha, size, url }
578                            })
579                            .collect::<Vec<_>>();
580                        DownloadItems::new(Path::new(output), items, *quiet, *skip_verify_checksum)
581                            .download()
582                            .await
583                            .map(|()| {
584                                Downloaded::init()
585                                    .identifier(identifier)
586                                    .revision(revision.as_str())
587                                    .files(selected_files)
588                                    .build()
589                            })
590                    }
591                    | Err(why) => Err(why),
592                },
593                | Err(why) => Err(why),
594            },
595            | (None, _) => Err(eyre!("Missing Hugging Face repository identifier")),
596            | (_, None) => Err(eyre!("Missing output directory")),
597        }
598    }
599    fn filter(&self, filter: Option<&str>, ignore: Option<&str>) -> ApiResult<Self> {
600        let filter_vec = filter.into_iter().map(String::from).collect::<Vec<_>>();
601        let ignore_vec = ignore.into_iter().map(String::from).collect::<Vec<_>>();
602        let filtered = match self.try_glob(filter, ignore) {
603            | Some(result) => Ok(result),
604            | None => FilterSet::filter(
605                self.files.clone(),
606                &filter_vec,
607                &ignore_vec,
608                |file: &HuggingFaceRepositoryFile| file.path.clone(),
609                |_| true,
610            )
611            .map(|files| Self::new(&self.identifier, &self.revision, files)),
612        };
613        filtered.and_then(|files| {
614            if filter.is_some() {
615                match files.is_empty() {
616                    | true => Err(eyre!("No model files matched --filter/--ignore")),
617                    | false => Ok(files),
618                }
619            } else {
620                let policy = FileSelectionPolicy {
621                    preferred_marker: "Q4_K_M",
622                    no_match_message: "No GGUF model files found; use a GGUF repository or provide --filter for another format",
623                };
624                files.select(&policy)
625            }
626        })
627    }
628    fn select(&self, policy: &FileSelectionPolicy<'_>) -> ApiResult<Self> {
629        let Self { identifier, revision, .. } = self;
630        let gguf_files = self
631            .iter()
632            .filter(|file| file.path.to_ascii_lowercase().ends_with(".gguf"))
633            .cloned()
634            .collect();
635        let files = Self::new(identifier, revision, gguf_files);
636        match files.len() {
637            | 0 => Err(eyre!(HuggingFaceError::NoGgufModelFiles)),
638            | 1 => Ok(files),
639            | _ => {
640                let preferred_files = files
641                    .iter()
642                    .filter(|file| file.path.to_ascii_uppercase().contains(policy.preferred_marker))
643                    .cloned()
644                    .collect();
645                let preferred = Self::new(identifier, revision, preferred_files);
646                match preferred.len() {
647                    | 1 => Ok(preferred),
648                    | _ => preferred.complete_shard_set().ok_or_else(|| {
649                        eyre!(
650                            "Multiple model file candidates found ({}); use --filter to choose one",
651                            files.candidate_report()
652                        )
653                    }),
654                }
655            }
656        }
657    }
658    fn should_use_fallback(&self, options: &Options) -> bool {
659        let Options {
660            identifier,
661            offline,
662            no_fallback,
663            filter,
664            ..
665        } = options;
666        let is_huggingface_repo_id = identifier
667            .as_deref()
668            .and_then(|value| value.split_once('/'))
669            .is_some_and(|(owner, name)| !owner.is_empty() && !name.is_empty() && !name.contains('/'));
670        let contains_gguf_files = self.iter().any(|file| file.path.to_ascii_lowercase().ends_with(".gguf"));
671        is_huggingface_repo_id && filter.is_none() && !(contains_gguf_files || *offline || *no_fallback)
672    }
673    fn try_glob(&self, filter: Option<&str>, ignore: Option<&str>) -> Option<Self> {
674        match (filter.map(regex_to_glob), ignore.map(regex_to_glob)) {
675            | (Some(None), _) | (_, Some(None)) => None,
676            | (filter_opt, ignore_opt) => {
677                let filter_glob = filter_opt.flatten();
678                let ignore_glob = ignore_opt.flatten();
679                let filtered = self
680                    .iter()
681                    .filter(|file| {
682                        let ignored = ignore_glob.as_ref().is_some_and(|pattern| glob_matches(&file.path, pattern));
683                        let kept = filter_glob.as_ref().is_none_or(|pattern| glob_matches(&file.path, pattern));
684                        !ignored && kept
685                    })
686                    .cloned()
687                    .collect();
688                Some(Self::new(&self.identifier, &self.revision, filtered))
689            }
690        }
691    }
692}
693impl ModelInfoExtension for ModelInfo {
694    fn has_gguf_files(&self) -> bool {
695        self.siblings
696            .as_ref()
697            .is_some_and(|siblings| siblings.iter().any(|file| file.rfilename.to_ascii_lowercase().ends_with(".gguf")))
698    }
699    fn is_fallback_for(&self, identifier: &str) -> bool {
700        self.id.eq_ignore_ascii_case(identifier) || self.is_declared_derivative_of(identifier) || self.is_declared_variant_of(identifier)
701    }
702    fn is_declared_derivative_of(&self, identifier: &str) -> bool {
703        let declares_base_model = self
704            .base_models
705            .as_ref()
706            .is_some_and(|models| models.iter().any(|value| value.eq_ignore_ascii_case(identifier)));
707        let declares_card_base_model = self
708            .card_data
709            .as_ref()
710            .and_then(|value| value.get("base_model"))
711            .is_some_and(|value| match value {
712                | Value::String(value) => value.eq_ignore_ascii_case(identifier),
713                | Value::Array(values) => values
714                    .iter()
715                    .filter_map(Value::as_str)
716                    .any(|value| value.eq_ignore_ascii_case(identifier)),
717                | _ => false,
718            });
719        let has_quantized_base_model_tag = self
720            .tags
721            .as_ref()
722            .is_some_and(|tags| tags.iter().any(|tag| is_quantized_base_model_tag(tag, identifier)));
723        declares_base_model || declares_card_base_model || has_quantized_base_model_tag
724    }
725    fn is_declared_variant_of(&self, identifier: &str) -> bool {
726        let matches = |value: &str| variant_matches(value, identifier);
727        let declares_base_model = self.base_models.as_ref().is_some_and(|models| models.iter().any(|value| matches(value)));
728        let declares_card_base_model = self
729            .card_data
730            .as_ref()
731            .and_then(|value| value.get("base_model"))
732            .is_some_and(|value| match value {
733                | Value::String(value) => matches(value),
734                | Value::Array(values) => values.iter().filter_map(Value::as_str).any(matches),
735                | _ => false,
736            });
737        let has_quantized_base_model_tag = self.tags.as_ref().is_some_and(|tags| {
738            tags.iter().any(|tag| {
739                let mut parts = tag.splitn(3, ':');
740                match (parts.next(), parts.next(), parts.next()) {
741                    | (Some(kind), Some(relation), Some(value)) => {
742                        kind.eq_ignore_ascii_case("base_model") && relation.eq_ignore_ascii_case("quantized") && matches(value)
743                    }
744                    | _ => false,
745                }
746            })
747        });
748        declares_base_model || declares_card_base_model || has_quantized_base_model_tag
749    }
750}
751impl ModelDetails {
752    /// Resolve this model to a GGUF fallback repository.
753    pub async fn resolve_fallback(self, search_options: &SearchOptions, offline: bool) -> ApiResult<RepositoryResolution<Self>> {
754        match search(search_options).await {
755            | Ok(candidates) => {
756                let options = Options::init()
757                    .identifier(&search_options.identifier)
758                    .offline(offline)
759                    .search_limit(search_options.limit)
760                    .minimum_download_count(search_options.minimum_download_count)
761                    .interactive(search_options.interactive)
762                    .quiet(true)
763                    .build();
764                candidates
765                    .select(&options)
766                    .map(|resolved| RepositoryResolution::new(&search_options.identifier, resolved, self))
767            }
768            | Err(why) => Err(why),
769        }
770    }
771}
772impl From<RepositoryResolution<ModelDetails>> for ModelDetails {
773    fn from(resolution: RepositoryResolution<ModelDetails>) -> Self {
774        let (requested, resolved, details) = resolution.into_parts();
775        let details = details.with_id(&resolved);
776        match requested == resolved {
777            | true => details,
778            | false => details.with_fallback(&requested),
779        }
780    }
781}
782impl<T> RepositoryResolution<T> {
783    /// Create a repository resolution from requested and resolved identifiers.
784    pub fn new(requested: impl Into<String>, resolved: impl Into<String>, value: T) -> Self {
785        Self {
786            requested: requested.into(),
787            resolved: resolved.into(),
788            value,
789        }
790    }
791    /// Create a direct repository resolution from one identifier.
792    pub fn direct(identifier: impl Into<String>, value: T) -> Self {
793        let requested = identifier.into();
794        let resolved = requested.clone();
795        Self::new(requested, resolved, value)
796    }
797    /// Return whether fallback discovery selected a different repository.
798    pub fn is_fallback(&self) -> bool {
799        self.requested != self.resolved
800    }
801    /// Return the originally requested repository identifier.
802    pub fn requested(&self) -> &str {
803        &self.requested
804    }
805    /// Return the repository identifier that supplied the resolved value.
806    pub fn resolved(&self) -> &str {
807        &self.resolved
808    }
809    /// Return the resolved value.
810    pub fn value(&self) -> &T {
811        &self.value
812    }
813    /// Transform the resolved value while preserving repository identity.
814    pub fn map<U>(self, transform: impl FnOnce(T) -> U) -> RepositoryResolution<U> {
815        let (requested, resolved, value) = self.into_parts();
816        RepositoryResolution::new(requested, resolved, transform(value))
817    }
818    /// Try to transform the resolved value while preserving repository identity.
819    pub fn try_map<U, E>(self, transform: impl FnOnce(T) -> Result<U, E>) -> Result<RepositoryResolution<U>, E> {
820        let (requested, resolved, value) = self.into_parts();
821        transform(value).map(|value| RepositoryResolution::new(requested, resolved, value))
822    }
823    /// Consume the resolution into its identifiers and value.
824    pub fn into_parts(self) -> (String, String, T) {
825        (self.requested, self.resolved, self.value)
826    }
827}
828impl From<&Source> for Weights {
829    fn from(source: &Source) -> Self {
830        match source {
831            | Source::Remote { identifier, .. } => Self(vec![Weight {
832                label: "Hugging Face".to_string(),
833                url: format!("https://{DEFAULT_HUGGINGFACE_DOMAIN}/{identifier}"),
834                is_open: None,
835                quantization: None,
836                size: None,
837            }]),
838            | Source::Local { path, .. } => Self(vec![Weight {
839                label: "Local".to_string(),
840                url: path.display().to_string(),
841                is_open: Some(true),
842                quantization: None,
843                size: None,
844            }]),
845            | Source::Unsupported(_) => Self::default(),
846        }
847    }
848}
849impl Weights {
850    /// Set the open-weight flag for the primary source.
851    pub fn open(mut self, is_open: Option<bool>) -> Self {
852        if let Some(weight) = self.0.first_mut() {
853            weight.is_open = is_open;
854        }
855        self
856    }
857    /// Construct weights from a source location with explicit open-weight control.
858    ///
859    /// For remote Hugging Face sources, `is_open` controls the open-weight flag.
860    /// For local sources, `is_open` is always `Some(true)`, regardless of the parameter.
861    pub fn from_source(source: &Source, is_open: Option<bool>) -> Self {
862        let weights = Self::from(source);
863        // Only override is_open for Remote sources; Local sources are always Some(true)
864        match source {
865            | Source::Remote { .. } => weights.open(is_open),
866            | _ => weights,
867        }
868    }
869    /// Resolve the first non-empty model weight URL into a downloadable source.
870    pub fn to_source(self, name: Option<String>) -> Option<Source> {
871        self.0.into_iter().find(|weight| !weight.url.trim().is_empty()).map(|weight| {
872            let location = Location::from(weight.url.as_str());
873            let is_repository = location.host().is_some_and(|host| host.eq_ignore_ascii_case(DEFAULT_HUGGINGFACE_DOMAIN))
874                && location
875                    .path()
876                    .is_some_and(|path| path.split('/').filter(|segment| !segment.is_empty()).count() == 2);
877            let source = match is_repository {
878                | true => Source::from(&Repository::HuggingFace { location }),
879                | false => Source::from(weight.url.as_str()),
880            };
881            source.with_name(name.unwrap_or(weight.label))
882        })
883    }
884}
885/// Build Hugging Face authorization headers using configured environment token values.
886pub fn auth_headers() -> HeaderMap {
887    Params::new()
888        .with_auth(first_env_var(&HUGGINGFACE_TOKEN_VARIABLE_NAMES).unwrap_or_default().as_str(), None)
889        .build()
890        .into_headers()
891}
892/// Fetch model metadata from Hugging Face API with base model and tag expansion
893pub async fn fetch_model_info(provider: &str, name: &str) -> ApiResult<ModelInfo> {
894    let client = HFClient::new().map_err(|why| {
895        eyre!(HuggingFaceError::ClientInitializationFailed {
896            reason: why.to_string().into()
897        })
898    });
899    match client {
900        | Ok(client) => client
901            .model(provider, name)
902            .info()
903            .expand(vec![
904                "baseModels".to_string(),
905                "cardData".to_string(),
906                "siblings".to_string(),
907                "tags".to_string(),
908            ])
909            .send()
910            .await
911            .map_err(|why| eyre!(why).wrap_err(format!("Failed to read Hugging Face metadata for '{provider}/{name}'"))),
912        | Err(e) => Err(e),
913    }
914}
915/// Return whether a non-empty Hugging Face token is configured
916pub fn has_auth_token() -> bool {
917    first_env_var(&HUGGINGFACE_TOKEN_VARIABLE_NAMES).is_some()
918}
919/// Return whether a Hugging Face error means a model is unavailable to the caller
920pub fn model_is_unavailable(error: &Report) -> bool {
921    error.downcast_ref::<HFError>().is_some_and(|source| match source {
922        | HFError::RepoNotFound { .. } | HFError::AuthRequired { .. } | HFError::Forbidden { .. } => true,
923        | HFError::Http { context } => matches!(context.status.as_u16(), 401 | 403 | 404),
924        | _ => false,
925    })
926}
927/// Validate and parse a Hugging Face model identifier into (owner, name)
928pub fn parse_identifier(identifier: &str) -> ApiResult<(&str, &str)> {
929    match identifier.split_once('/') {
930        | Some((owner, name)) if !owner.is_empty() && !name.is_empty() && !name.contains('/') => Ok((owner, name)),
931        | _ => Err(eyre!("Invalid Hugging Face model identifier — {identifier}")),
932    }
933}
934/// List files in a Hugging Face model repository at `revision`
935pub async fn repository_tree(identifier: &str, revision: &str) -> ApiResult<HuggingFaceRepositoryFiles> {
936    let template = "huggingface::api";
937    let action = "tree";
938    let options = Options::init().identifier(identifier).revision(revision).build();
939    let params = Params::new()
940        .with_auth(first_env_var(&HUGGINGFACE_TOKEN_VARIABLE_NAMES).unwrap_or_default().as_str(), None)
941        .with_template("identifier", options.identifier.as_deref())
942        .with_template("revision", Some(&options.revision))
943        .with_keyvalue("recursive", Some("1"))
944        .build();
945    match Endpoint::from_template(template) {
946        | Ok(endpoint) => match endpoint.invoke(action, Some(params)).await {
947            | Ok(ResponseContent::Json(content)) => HuggingFaceRepositoryFiles::parse(&content, &options),
948            | Ok(_) => Err(eyre!("Failed to list Hugging Face model files — response was not JSON")),
949            | Err(why) => Err(eyre!("Failed to list Hugging Face model files — {why}")),
950        },
951        | Err(why) => Err(eyre!("Failed to configure Hugging Face API — {why}")),
952    }
953}
954/// Find repositories matching the search options, sorted by downloads.
955pub async fn search(options: &SearchOptions) -> ApiResult<Candidates> {
956    let basename = ModelSelector::new(&options.identifier)
957        .map(|selector| selector.fallback_search_name())
958        .filter(|value| !value.is_empty());
959    match (basename, HFClient::new()) {
960        | (Some(basename), Ok(client)) => {
961            let response = client
962                .list_models()
963                .search(&basename)
964                .filter(&options.term)
965                .sort("downloads")
966                .full(true)
967                .card_data(true)
968                .limit(options.limit)
969                .send();
970            match response {
971                | Ok(stream) => match stream.try_collect::<Vec<ModelInfo>>().await {
972                    | Ok(models) => {
973                        let candidates = Candidates::fallback(models, options);
974                        match candidates.is_empty() {
975                            | true => Err(eyre!(HuggingFaceError::NoGgufQuantizationRepository {
976                                identifier: options.identifier.clone().into()
977                            })),
978                            | false => Ok(candidates),
979                        }
980                    }
981                    | Err(why) => Err(eyre!(HuggingFaceError::ModelSearchFailed {
982                        reason: why.to_string().into()
983                    })),
984                },
985                | Err(why) => Err(eyre!(HuggingFaceError::ModelSearchConfigurationFailed {
986                    reason: why.to_string().into()
987                })),
988            }
989        }
990        | (None, _) => Err(eyre!(HuggingFaceError::InvalidBaseModelIdentifier {
991            identifier: options.identifier.clone().into()
992        })),
993        | (_, Err(why)) => Err(eyre!(HuggingFaceError::ClientInitializationFailed {
994            reason: why.to_string().into()
995        })),
996    }
997}
998fn extract_sha256(content: &str) -> Option<String> {
999    content
1000        .split_whitespace()
1001        .find(|token| token.len() == 64 && token.chars().all(|character| character.is_ascii_hexdigit()))
1002        .map(|value| value.to_ascii_lowercase())
1003}
1004fn is_quantized_base_model_tag(tag: &str, identifier: &str) -> bool {
1005    let mut parts = tag.splitn(3, ':');
1006    match (parts.next(), parts.next(), parts.next()) {
1007        | (Some(kind), Some(relation), Some(value)) => {
1008            kind.eq_ignore_ascii_case("base_model") && relation.eq_ignore_ascii_case("quantized") && value.eq_ignore_ascii_case(identifier)
1009        }
1010        | _ => false,
1011    }
1012}
1013fn select_first(candidates: Candidates, _options: &Options) -> ApiResult<String> {
1014    candidates
1015        .into_iter()
1016        .next()
1017        .map(|candidate| candidate.to_string())
1018        .ok_or_else(|| eyre!("GGUF search returned no candidates"))
1019}
1020fn target_path_from_sidecar(path: &str) -> Option<String> {
1021    path.strip_suffix(".sha256")
1022        .or_else(|| path.strip_suffix(".sha256sum"))
1023        .map(ToString::to_string)
1024}
1025fn variant_matches(declared: &str, requested: &str) -> bool {
1026    match (declared.split_once('/'), requested.split_once('/')) {
1027        | (Some((declared_owner, declared_name)), Some((requested_owner, requested_name))) => {
1028            let same_owner = declared_owner.eq_ignore_ascii_case(requested_owner);
1029            let owner_is_meta = requested_owner.eq_ignore_ascii_case("meta") && declared_owner.eq_ignore_ascii_case("meta-llama");
1030            let requested_name = to_ascii_alphanumeric(strip_suffixes(FALLBACK_MODEL_SUFFIXES, requested_name));
1031            let name_matches = to_ascii_alphanumeric(declared_name).contains(&requested_name);
1032            (same_owner || owner_is_meta) && !requested_name.is_empty() && name_matches
1033        }
1034        | _ => false,
1035    }
1036}
1037
1038#[cfg(test)]
1039mod tests {
1040    use super::{
1041        extract_sha256, target_path_from_sidecar, Candidate, CandidateSelection, Candidates, Downloaded, HuggingFaceRepositoryFiles, ModelInfo,
1042        ModelInfoExtension, RepositoryResolution, SearchOptions, Value,
1043    };
1044    use crate::schema::agent::{ModelDetails, Weight, Weights};
1045    use serde_json::json;
1046
1047    fn model(value: Value) -> ModelInfo {
1048        serde_json::from_value(value).unwrap()
1049    }
1050
1051    #[test]
1052    fn test_has_gguf_files() {
1053        let gguf = model(json!({"id": "mozilla/test-llama", "siblings": [{"rfilename": "tiny-llama.gguf"}]}));
1054        assert!(gguf.has_gguf_files());
1055        let non_gguf = model(json!({"id": "openai/gpt-oss-20b", "siblings": [{"rfilename": "model.safetensors"}]}));
1056        assert!(!non_gguf.has_gguf_files());
1057    }
1058    #[test]
1059    fn test_is_declared_derivative() {
1060        let candidate = model(json!({"id": "community/quantized", "baseModels": ["OpenAI/GPT-OSS-2B"]}));
1061        assert!(candidate.is_declared_derivative_of("openai/gpt-oss-2b"));
1062        let candidate = model(json!({"id": "community/quantized", "tags": ["base_model:quantized:openai/gpt-oss-2b"]}));
1063        assert!(candidate.is_declared_derivative_of("openai/gpt-oss-2b"));
1064        let candidate = model(json!({
1065            "id": "community/quantized",
1066            "baseModels": ["other/model"],
1067            "tags": ["base_model:quantized:other/model"]
1068        }));
1069        assert!(!candidate.is_declared_derivative_of("openai/gpt-oss-2b"));
1070    }
1071    #[test]
1072    fn test_is_declared_variant_accepts_decorated_base_model_names() {
1073        let candidate = model(json!({
1074            "id": "unsloth/NVIDIA-Nemotron-3-Super-120B-A12B-GGUF",
1075            "cardData": {"base_model": ["nvidia/NVIDIA-Nemotron-3-Super-120B-A12B-BF16"]},
1076            "tags": ["base_model:quantized:nvidia/NVIDIA-Nemotron-3-Super-120B-A12B-BF16"]
1077        }));
1078        assert!(candidate.is_declared_variant_of("nvidia/nemotron-3-super-120b-a12b"));
1079        assert!(!candidate.is_declared_variant_of("other/nemotron-3-super-120b-a12b"));
1080        assert!(!candidate.is_declared_variant_of("nvidia/different-model"));
1081    }
1082    #[test]
1083    fn test_is_declared_variant_accepts_meta_publisher_alias_and_catalog_suffixes() {
1084        let candidate = model(json!({
1085            "id": "unsloth/Llama-4-Maverick-17B-128E-Instruct-GGUF",
1086            "tags": ["base_model:quantized:meta-llama/Llama-4-Maverick-17B-128E-Instruct"]
1087        }));
1088        assert!(candidate.is_declared_variant_of("meta/llama-4-maverick-17b-128e-instruct"));
1089        assert!(candidate.is_declared_variant_of("meta/llama-4-maverick-17b-128e-instruct-fp8"));
1090        assert!(candidate.is_declared_variant_of("meta/llama-4-maverick-17b-128e-instruct-maas"));
1091        assert!(!candidate.is_declared_variant_of("other/llama-4-maverick-17b-128e-instruct"));
1092    }
1093    #[test]
1094    fn test_is_declared_variant_accepts_nvidia_version_separator_aliases() {
1095        let ultra = model(json!({
1096            "id": "bartowski/nvidia_Llama-3_1-Nemotron-Ultra-253B-v1-GGUF",
1097            "tags": ["base_model:quantized:nvidia/Llama-3_1-Nemotron-Ultra-253B-v1"]
1098        }));
1099        let super_model = model(json!({
1100            "id": "bartowski/nvidia_Llama-3_3-Nemotron-Super-49B-v1_5-GGUF",
1101            "tags": ["base_model:quantized:nvidia/Llama-3_3-Nemotron-Super-49B-v1_5"]
1102        }));
1103        assert!(ultra.is_declared_variant_of("nvidia/llama-3.1-nemotron-ultra-253b"));
1104        assert!(super_model.is_declared_variant_of("nvidia/llama-3.3-nemotron-super-49b-v1.5"));
1105        assert!(!super_model.is_declared_variant_of("nvidia/llama-nemotron-rerank-vl-1b-v2"));
1106    }
1107    #[test]
1108    fn test_gguf_candidate_includes_sorted_unique_quantizations() {
1109        let candidate = Candidate::from(model(json!({
1110            "id": "community/quantized",
1111            "downloads": 42,
1112            "likes": 7,
1113            "siblings": [
1114                {"rfilename": "model-Q5_K_M.gguf"},
1115                {"rfilename": "model-Q4_K_M.gguf"},
1116                {"rfilename": "model-Q4_K_M-00001-of-00002.gguf"}
1117            ]
1118        })));
1119        assert_eq!(candidate.downloads, 42);
1120        assert_eq!(candidate.likes, Some(7));
1121        assert_eq!(candidate.quantizations, vec!["Q4_K_M", "Q5_K_M"]);
1122        assert_eq!(candidate.to_string(), "community/quantized");
1123    }
1124    #[test]
1125    fn test_gguf_candidate_excludes_unrecognized_quantizations() {
1126        let candidate = Candidate::from(model(json!({
1127            "id": "community/unsupported",
1128            "siblings": [{"rfilename": "model-tq1_0.gguf"}]
1129        })));
1130        assert!(candidate.quantizations.is_empty());
1131    }
1132    #[test]
1133    fn test_fallback_candidates_apply_inclusive_minimum_download_count() {
1134        let candidate = |id: &str, downloads: Option<u64>| {
1135            model(json!({
1136                "id": id,
1137                "downloads": downloads,
1138                "tags": ["base_model:quantized:acme/base"],
1139                "siblings": [{"rfilename": "model-Q4_K_M.gguf"}]
1140            }))
1141        };
1142        let options = SearchOptions::init().identifier("acme/base").minimum_download_count(100).build();
1143        let candidates = Candidates::fallback(
1144            vec![
1145                candidate("acme/above-GGUF", Some(101)),
1146                candidate("acme/boundary-GGUF", Some(100)),
1147                candidate("acme/below-GGUF", Some(99)),
1148                candidate("acme/missing-GGUF", None),
1149            ],
1150            &options,
1151        );
1152        assert_eq!(
1153            candidates.iter().map(|candidate| candidate.id.as_str()).collect::<Vec<_>>(),
1154            vec!["acme/above-GGUF", "acme/boundary-GGUF"]
1155        );
1156    }
1157    #[test]
1158    fn test_weights_to_source_uses_hugging_face_repository_identifier() {
1159        let source = Weights(vec![Weight {
1160            label: "Hugging Face".to_string(),
1161            url: "https://huggingface.co/openai/gpt-oss-20b".to_string(),
1162            is_open: Some(true),
1163            quantization: None,
1164            size: None,
1165        }])
1166        .to_source(Some("GPT OSS 20B".to_string()))
1167        .unwrap();
1168        assert_eq!(source.identifier(), "openai/gpt-oss-20b");
1169        assert_eq!(source.name(), "GPT OSS 20B");
1170    }
1171    #[test]
1172    fn test_weights_to_source_keeps_direct_hugging_face_file_url() {
1173        let url = "https://huggingface.co/openai/gpt-oss-20b/resolve/main/model.gguf".to_string();
1174        let source = Weights(vec![Weight {
1175            label: "GGUF".to_string(),
1176            url: url.to_string(),
1177            is_open: Some(true),
1178            quantization: None,
1179            size: None,
1180        }])
1181        .to_source(None)
1182        .unwrap();
1183        assert_eq!(source.identifier(), url);
1184        assert_eq!(source.name(), "GGUF");
1185    }
1186    #[test]
1187    fn test_repository_tree_reports_api_error_message() {
1188        let options = super::Options::init().identifier("missing/model").revision("main").build();
1189        let error = HuggingFaceRepositoryFiles::parse(r#"{"error":"Repository not found"}"#, &options).unwrap_err();
1190        assert_eq!(
1191            error.to_string(),
1192            "Hugging Face API rejected repository 'missing/model' — Repository not found"
1193        );
1194    }
1195    #[test]
1196    fn test_repository_tree_parse_error_identifies_repository() {
1197        let options = super::Options::init().identifier("broken/model").revision("main").build();
1198        let error = HuggingFaceRepositoryFiles::parse(r#"{"unexpected":true}"#, &options).unwrap_err();
1199        assert!(error
1200            .to_string()
1201            .starts_with("Failed to parse Hugging Face repository file list for 'broken/model'"));
1202    }
1203    #[test]
1204    fn test_repository_resolution_identifies_direct_and_fallback_values() {
1205        let direct = RepositoryResolution::direct("acme/model", 1);
1206        assert!(!direct.is_fallback());
1207        assert_eq!(direct.requested(), "acme/model");
1208        assert_eq!(direct.resolved(), "acme/model");
1209        assert_eq!(*direct.value(), 1);
1210        let fallback = RepositoryResolution::new("acme/model", "community/model-GGUF", 2);
1211        assert!(fallback.is_fallback());
1212        assert_eq!(fallback.requested(), "acme/model");
1213        assert_eq!(fallback.resolved(), "community/model-GGUF");
1214        assert_eq!(fallback.into_parts(), ("acme/model".to_string(), "community/model-GGUF".to_string(), 2));
1215    }
1216    #[test]
1217    fn test_repository_resolution_transforms_values_without_losing_identity() {
1218        let mapped = RepositoryResolution::new("acme/model", "community/model-GGUF", 2).map(|value| value.to_string());
1219        assert_eq!(mapped.requested(), "acme/model");
1220        assert_eq!(mapped.resolved(), "community/model-GGUF");
1221        assert_eq!(mapped.value(), "2");
1222        let mapped = RepositoryResolution::direct("acme/model", 2).try_map(|value| if value > 0 { Ok(value * 2) } else { Err("invalid") });
1223        assert_eq!(
1224            mapped.map(RepositoryResolution::into_parts),
1225            Ok(("acme/model".to_string(), "acme/model".to_string(), 4))
1226        );
1227    }
1228    #[test]
1229    fn test_downloaded_into_resolution_uses_one_identifier() {
1230        let downloaded = Downloaded::init().identifier("acme/model").revision("main").files(Vec::new()).build();
1231        let resolution = downloaded.into_resolution("acme/model");
1232        assert!(!resolution.is_fallback());
1233        assert_eq!(resolution.requested(), "acme/model");
1234        assert_eq!(resolution.resolved(), "acme/model");
1235    }
1236    #[test]
1237    fn test_model_details_from_repository_resolution_preserves_fallback() {
1238        let resolution = RepositoryResolution::new("acme/model", "community/model-GGUF", ModelDetails::default());
1239        let details = ModelDetails::from(resolution);
1240        assert_eq!(details.id.as_deref(), Some("community/model-GGUF"));
1241        assert_eq!(details.fallback.as_deref(), Some("acme/model"));
1242    }
1243    #[test]
1244    fn test_target_path_from_sidecar() {
1245        assert_eq!(target_path_from_sidecar("model.gguf.sha256"), Some("model.gguf".to_string()));
1246        assert_eq!(
1247            target_path_from_sidecar("nested/model.gguf.sha256sum"),
1248            Some("nested/model.gguf".to_string())
1249        );
1250    }
1251    #[test]
1252    fn test_extract_sha256() {
1253        let digest = "ABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCD";
1254        assert_eq!(
1255            extract_sha256(format!("{digest}  model.gguf").as_str()).as_deref(),
1256            Some("abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd"),
1257        );
1258        let digest = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
1259        assert_eq!(
1260            extract_sha256(format!("SHA256 ({digest}) = {digest} model.gguf").as_str()).as_deref(),
1261            Some(digest),
1262        );
1263        assert_eq!(extract_sha256("not-a-sha model.gguf"), None);
1264        assert_eq!(extract_sha256("0123456789abcdef"), None);
1265    }
1266}