use std::path::PathBuf;
use anyhow::Context;
use clap::Args;
use serde::Serialize;
use crate::modelpack::resolve_model;
use crate::storage::OciStore;
#[derive(Args, Debug)]
pub struct ResolveArgs {
#[arg(value_name = "REFERENCE")]
pub reference: String,
#[arg(long)]
pub cache: Option<PathBuf>,
#[arg(long)]
pub no_pull: bool,
}
#[derive(Serialize)]
struct ResolveOutput<'a> {
reference: &'a str,
path: String,
format: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
mmproj: Option<String>,
}
pub fn run(args: &ResolveArgs) -> anyhow::Result<()> {
let reference = crate::shortnames::resolve_ollama_api(&args.reference)?;
let store_path = crate::default_store()?;
let cache_path = args
.cache
.clone()
.map(Ok)
.unwrap_or_else(crate::default_cache)?;
std::fs::create_dir_all(&store_path)
.with_context(|| format!("creating store dir {}", store_path.display()))?;
std::fs::create_dir_all(&cache_path)
.with_context(|| format!("creating cache dir {}", cache_path.display()))?;
let already_present = OciStore::open(&store_path)
.and_then(|s| s.find(&reference))
.is_ok();
if !already_present {
if args.no_pull {
anyhow::bail!(
"{reference} not found in local store ({}) and --no-pull was set",
store_path.display()
);
}
let layout_dir = store_path
.to_str()
.context("store path is not valid UTF-8")?;
tokio::runtime::Runtime::new()
.context("start tokio runtime")?
.block_on(async {
match crate::hf::classify(&reference).await {
crate::hf::ClassifiedRef::Hf(hf_ref) => {
crate::hf::pull::pull(&hf_ref, &store_path, &reference).await
}
crate::hf::ClassifiedRef::Source(src_ref) => {
crate::sources::pull(&src_ref, &store_path, &reference).await
}
crate::hf::ClassifiedRef::Other(normalized) => {
crate::ffi::pull(&normalized, layout_dir)
}
}
})
.with_context(|| format!("pulling {reference}"))?;
}
let resolved = resolve_model(&store_path, &cache_path, &reference)
.with_context(|| format!("resolving {reference}"))?;
let out = ResolveOutput {
reference: &reference,
path: resolved.path().to_string_lossy().into_owned(),
format: resolved.format(),
mmproj: resolved.mmproj().map(|p| p.to_string_lossy().into_owned()),
};
println!("{}", serde_json::to_string(&out)?);
Ok(())
}