use bunsen::errors::{
BunsenError,
LookupError,
LookupProblem,
Namespace,
};
const MOST: usize = 3;
const SHOWN: usize = 16;
pub fn nearest(lookup: &LookupError) -> Vec<String> {
let key = lookup.key.to_lowercase();
let bare = !key.contains(':');
let grouped = key.contains('/');
let most = (key.chars().count() / 3).max(1);
let name_of = |candidate: &str| -> String {
let name = match candidate.split_once(':') {
Some((_, name)) if bare => name,
_ => candidate,
};
name.to_string()
};
let short_of = |name: &str| -> Option<String> {
(bare && !grouped)
.then(|| name.split_once('/').map(|(_, row)| row.to_string()))
.flatten()
};
let names: Vec<String> = lookup.candidates.iter().map(|c| name_of(c)).collect();
let shorts: Vec<Option<String>> = names.iter().map(|n| short_of(n)).collect();
let mut ranked: Vec<(bool, usize, String)> = Vec::new();
for (name, short) in names.iter().zip(&shorts) {
let offered = match short {
Some(row) if shorts.iter().filter(|s| s.as_ref() == Some(row)).count() == 1 => row,
_ => name,
};
let compared = short.as_ref().unwrap_or(name).to_lowercase();
let extends = key
.strip_prefix(compared.as_str())
.and_then(|rest| rest.chars().next())
.is_some_and(|c| matches!(c, '-' | '_' | '.' | '/'));
let distance = strsim::damerau_levenshtein(&key, &compared);
if (extends || distance <= most) && !ranked.iter().any(|(_, _, n)| n == offered) {
ranked.push((!extends, distance, offered.clone()));
}
}
ranked.sort();
ranked.into_iter().take(MOST).map(|(_, _, n)| n).collect()
}
pub fn hint(error: &BunsenError) -> Option<String> {
let lookup = error.find::<LookupError>()?;
if lookup.problem != LookupProblem::Missing
|| !matches!(lookup.namespace, Namespace::Table(_))
|| lookup.candidates.is_empty()
{
return None;
}
let mut lines = Vec::new();
let nearest = nearest(lookup);
if let Some((last, rest)) = nearest.split_last() {
let quoted = |n: &String| format!("`{n}`");
lines.push(match rest {
[] => format!("did you mean {}?", quoted(last)),
rest => format!(
"did you mean {}, or {}?",
rest.iter().map(quoted).collect::<Vec<_>>().join(", "),
quoted(last)
),
});
}
if lookup.candidates.len() > SHOWN {
lines.push(format!("the {} names there are:", lookup.candidates.len()));
lines.extend(lookup.candidates.iter().map(|c| format!(" {c}")));
}
(!lines.is_empty()).then(|| lines.join("\n"))
}
#[cfg(test)]
mod tests {
use super::*;
fn missing(
key: &str,
candidates: &[&str],
) -> LookupError {
LookupError::missing("pretrained", key).with_candidates(candidates.iter().copied())
}
#[test]
fn test_nearest() {
let names = [
"catalog:openai/tiny.en",
"catalog:openai/tiny",
"catalog:openai/small",
"other:openai/tiny",
];
assert_eq!(
nearest(&missing("openai/tinny", &names)),
["openai/tiny", "openai/tiny.en"]
);
assert_eq!(nearest(&missing("OpenAI/Small", &names)), ["openai/small"]);
assert_eq!(
nearest(&missing("other:openai/tini", &names)),
["other:openai/tiny"]
);
assert!(nearest(&missing("qwen/qwen3-8b", &names)).is_empty());
assert_eq!(
nearest(&missing("mult", &["en", "multi", "translate-en"])),
["multi"]
);
}
#[test]
fn test_nearest_ungrouped() {
let qwen3 = [
"catalog:qwen/qwen3-0.6b",
"catalog:qwen/qwen3-0.6b-base",
"catalog:qwen/qwen3-1.7b",
"catalog:qwen/qwen3-1.7b-base",
"catalog:qwen/qwen3-4b",
"catalog:qwen/qwen3-4b-base",
"catalog:qwen/qwen3-4b-instruct-2507",
"catalog:qwen/qwen3-4b-thinking-2507",
"catalog:qwen/qwen3-8b",
"catalog:qwen/qwen3-8b-base",
"catalog:qwen/qwen3-4b-saferl",
"catalog:qwen/qwen3guard-gen-0.6b",
"catalog:qwen/qwen3guard-gen-4b",
"catalog:qwen/qwen3guard-gen-8b",
];
assert_eq!(
nearest(&missing("qwen3-1.7b-turbo", &qwen3)),
["qwen3-1.7b", "qwen3-1.7b-base"]
);
assert_eq!(
nearest(&missing("qwen3-8bb", &qwen3)),
["qwen3-8b", "qwen3-4b", "qwen3-0.6b"]
);
assert_eq!(
nearest(&missing("qwen/qwen3-8bb", &qwen3)),
["qwen/qwen3-8b", "qwen/qwen3-4b", "qwen/qwen3-0.6b"]
);
let shared = ["catalog:openai/tiny", "catalog:other/tiny"];
assert_eq!(
nearest(&missing("tinny", &shared)),
["openai/tiny", "other/tiny"]
);
}
#[test]
fn test_hint() {
let error = BunsenError::lookup(missing("translte", &["transcribe", "translate"]))
.context("the presets of \"openai/small\"");
assert_eq!(hint(&error).as_deref(), Some("did you mean `translate`?"));
let three = BunsenError::lookup(missing(
"qwen3-0.6",
&["qwen3-0.6b", "qwen3-1.7b", "qwen3-8b", "qwen3-32b-awq"],
));
assert_eq!(
hint(&three).as_deref(),
Some("did you mean `qwen3-0.6b`, `qwen3-1.7b`, or `qwen3-8b`?")
);
let many: Vec<String> = (0..20).map(|i| format!("model-{i}")).collect();
let error = BunsenError::lookup(
LookupError::missing("pretrained", "zzz").with_candidates(many.iter().cloned()),
);
let text = hint(&error).unwrap();
assert!(
text.starts_with("the 20 names there are:\n model-0\n"),
"{text}"
);
assert_eq!(hint(&BunsenError::policy("no")), None);
let file = BunsenError::lookup(LookupError::path("/nope", LookupProblem::Missing));
assert_eq!(hint(&file), None);
}
}