use std::path::{Path, PathBuf};
use std::time::Duration;
use serde::{Deserialize, Serialize};
use tokio::process::Command;
use weirdnet_agent::Modalities;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum EngineKind {
Llamacpp,
Vllm,
Mlx,
}
impl EngineKind {
pub fn name(self) -> &'static str {
match self {
EngineKind::Llamacpp => "llamacpp",
EngineKind::Vllm => "vllm",
EngineKind::Mlx => "mlx",
}
}
pub fn capabilities(self) -> Capabilities {
match self {
EngineKind::Llamacpp | EngineKind::Vllm => Capabilities { chat: true, embeddings: true, vision: true, audio: true },
EngineKind::Mlx => Capabilities::text(),
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Capabilities {
pub chat: bool,
pub embeddings: bool,
pub vision: bool,
pub audio: bool,
}
impl Capabilities {
pub fn text() -> Self {
Self { chat: true, ..Self::default() }
}
pub fn embeddings() -> Self {
Self { embeddings: true, ..Self::default() }
}
pub fn with_vision(mut self, on: bool) -> Self {
self.vision = on;
self
}
pub fn with_audio(mut self, on: bool) -> Self {
self.audio = on;
self
}
pub fn inputs(&self) -> Modalities {
Modalities::new(self.vision, self.audio)
}
pub fn lacking(&self, needed: Modalities) -> Option<&'static str> {
self.inputs().lacking(needed)
}
pub fn intersect(self, other: Self) -> Self {
Self {
chat: self.chat && other.chat,
embeddings: self.embeddings && other.embeddings,
vision: self.vision && other.vision,
audio: self.audio && other.audio,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EngineConfig {
pub kind: EngineKind,
pub program: PathBuf,
pub extra_args: Vec<String>,
pub ready_timeout: Duration,
}
impl EngineConfig {
pub fn llamacpp(program: impl Into<PathBuf>) -> Self {
Self { kind: EngineKind::Llamacpp, program: program.into(), extra_args: Vec::new(), ready_timeout: Duration::from_secs(300) }
}
pub fn vllm(program: impl Into<PathBuf>) -> Self {
Self { kind: EngineKind::Vllm, program: program.into(), extra_args: Vec::new(), ready_timeout: Duration::from_secs(1200) }
}
pub fn mlx(program: impl Into<PathBuf>) -> Self {
Self { kind: EngineKind::Mlx, program: program.into(), extra_args: Vec::new(), ready_timeout: Duration::from_secs(1200) }
}
pub fn command(&self, model: &ModelLaunch, port: u16, api_key: &str) -> Command {
let mut cmd = Command::new(&self.program);
match self.kind {
EngineKind::Llamacpp => {
cmd.args(["--model", &model.source, "--host", "127.0.0.1", "--port", &port.to_string(), "--alias", &model.id, "--no-webui"]);
if let Some(ctx) = model.context {
cmd.args(["--ctx-size", &ctx.to_string()]);
}
if let Some(slots) = model.slots {
cmd.args(["--parallel", &slots.to_string()]);
}
if let Some(ngl) = model.gpu_layers {
cmd.args(["--n-gpu-layers", &ngl.to_string()]);
}
if model.embedding {
cmd.arg("--embedding");
}
if let Some(mmproj) = &model.mmproj {
cmd.args(["--mmproj", mmproj]);
}
for a in &model.adapters {
cmd.args(["--lora", &a.path]);
}
if !model.adapters.is_empty() {
cmd.arg("--lora-init-without-apply");
}
cmd.env("LLAMA_API_KEY", api_key);
}
EngineKind::Vllm => {
cmd.args(["serve", &model.source, "--host", "127.0.0.1", "--port", &port.to_string(), "--served-model-name", &model.id]);
if let Some(ctx) = model.context {
cmd.args(["--max-model-len", &ctx.to_string()]);
}
if let Some(slots) = model.slots {
cmd.args(["--max-num-seqs", &slots.to_string()]);
}
if model.runtime_lora() {
cmd.arg("--enable-lora");
if !model.adapters.is_empty() {
cmd.arg("--lora-modules");
for a in &model.adapters {
cmd.arg(serde_json::json!({"name": a.id, "path": a.path, "base_model_name": model.id}).to_string());
}
}
let max = (model.adapters.len() as u32).max(model.lora_slots.unwrap_or(0)).max(1);
cmd.args(["--max-loras", &max.to_string()]);
cmd.env("VLLM_ALLOW_RUNTIME_LORA_UPDATING", "True");
}
cmd.env("VLLM_API_KEY", api_key);
}
EngineKind::Mlx => {
let max = model.context.unwrap_or(crate::mlx::DEFAULT_MAX_TOKENS).to_string();
cmd.args(["--model", &model.source, "--host", "127.0.0.1", "--port", &port.to_string(), "--max-tokens", &max]);
}
}
cmd.args(&self.extra_args).args(&model.extra_args);
cmd
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct LoraAdapter {
pub id: String,
pub path: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub scale: Option<f32>,
}
impl Eq for LoraAdapter {}
impl LoraAdapter {
pub fn new(id: impl Into<String>, path: impl Into<String>) -> Self {
Self { id: id.into(), path: path.into(), scale: None }
}
pub fn with_scale(mut self, scale: f32) -> Self {
self.scale = Some(scale);
self
}
pub fn effective_scale(&self) -> f32 {
self.scale.unwrap_or(1.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ModelLaunch {
pub id: String,
pub engine: EngineKind,
pub source: String,
pub context: Option<u32>,
pub slots: Option<u32>,
pub gpu_layers: Option<i32>,
pub swap_group: Option<String>,
pub embedding: bool,
pub extra_args: Vec<String>,
#[serde(default, with = "opt_secs")]
pub idle_ttl: Option<Duration>,
#[serde(default)]
pub wrap: Option<Wrap>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub adapters: Vec<LoraAdapter>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub lora_slots: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub mmproj: Option<String>,
}
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Wrap {
pub url: String,
#[serde(default, skip_serializing)]
pub api_key: Option<String>,
}
impl std::fmt::Debug for Wrap {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Wrap").field("url", &self.url).field("api_key", &self.api_key.as_ref().map(|_| "<redacted>")).finish()
}
}
impl Wrap {
pub fn check(&self) -> Result<(), String> {
let rest = self.url.strip_prefix("http://").ok_or_else(|| format!("wrap url {} must start with http:// (loopback only)", self.url))?;
let host = match rest.strip_prefix('[') {
Some(v6) => v6.split(']').next().unwrap_or(""),
None => rest.split(['/', ':']).next().unwrap_or(""),
};
if matches!(host, "127.0.0.1" | "localhost" | "::1") { Ok(()) } else { Err(format!("wrap url {} is not loopback", self.url)) }
}
pub fn base(&self) -> String {
self.url.trim_end_matches('/').trim_end_matches("/v1").to_string()
}
}
mod opt_secs {
use std::time::Duration;
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S: Serializer>(d: &Option<Duration>, s: S) -> Result<S::Ok, S::Error> {
match d {
Some(d) => s.serialize_some(&d.as_secs()),
None => s.serialize_none(),
}
}
pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Option<Duration>, D::Error> {
Ok(Option::<u64>::deserialize(d)?.map(Duration::from_secs))
}
}
impl ModelLaunch {
pub fn new(id: impl Into<String>, engine: EngineKind, source: impl Into<String>) -> Self {
Self {
id: id.into(),
engine,
source: source.into(),
context: None,
slots: None,
gpu_layers: None,
swap_group: None,
embedding: false,
extra_args: Vec::new(),
idle_ttl: None,
wrap: None,
adapters: Vec::new(),
lora_slots: None,
mmproj: None,
}
}
pub fn with_mmproj(mut self, path: impl Into<String>) -> Self {
self.mmproj = Some(path.into());
self
}
pub fn with_lora_slots(mut self, slots: u32) -> Self {
self.lora_slots = Some(slots);
self
}
pub fn runtime_lora(&self) -> bool {
self.engine == EngineKind::Vllm && self.wrap.is_none() && (!self.adapters.is_empty() || self.lora_slots.is_some_and(|n| n > 0))
}
pub fn with_adapters(mut self, adapters: Vec<LoraAdapter>) -> Self {
self.adapters = adapters;
self
}
pub fn served_ids(&self) -> Vec<String> {
std::iter::once(self.id.clone()).chain(self.adapters.iter().map(|a| a.id.clone())).collect()
}
pub fn check_adapters(&self) -> Result<(), String> {
if self.adapters.is_empty() {
return Ok(());
}
let id = &self.id;
if self.wrap.is_some() {
return Err(format!("{id}: a wrapped server can't take adapters from this node"));
}
if self.embedding {
return Err(format!("{id}: embedding models take no adapters"));
}
if !matches!(self.engine, EngineKind::Llamacpp | EngineKind::Vllm) {
return Err(format!("{id}: {} serves no adapters here; use llama.cpp or vLLM", self.engine.name()));
}
let mut seen = std::collections::HashSet::new();
for a in &self.adapters {
if a.id.trim().is_empty() {
return Err(format!("{id}: an adapter needs an id"));
}
if a.id == *id {
return Err(format!("{id}: adapter id {} is the base model's", a.id));
}
if !seen.insert(a.id.as_str()) {
return Err(format!("{id}: adapter id {} is used twice", a.id));
}
if a.scale.is_some_and(|s| !s.is_finite()) {
return Err(format!("{}: the scale must be a finite number", a.id));
}
}
match self.engine {
EngineKind::Llamacpp => {
let header = |path: &str| -> Result<weirdnet_model::gguf::GgufHeader, String> {
let f = std::fs::File::open(path).map_err(|e| format!("{path}: {e}"))?;
weirdnet_model::gguf::read_header(std::io::BufReader::new(f)).map_err(|e| format!("{path}: {e}"))
};
let base = header(&self.source)?;
if base.kind.is_adapter() {
return Err(format!("{id}: {} is an adapter, not a base model", self.source));
}
for a in &self.adapters {
if a.path.contains(',') {
return Err(format!("{}: llama.cpp can't take a path with a comma ({})", a.id, a.path));
}
let h = header(&a.path)?;
if !h.kind.is_lora() {
return Err(format!("{}: {} is not a LoRA adapter GGUF", a.id, a.path));
}
if h.architecture != base.architecture {
return Err(format!("{}: adapter architecture {} doesn't match the base's {}", a.id, h.architecture, base.architecture));
}
}
}
_ => {
for a in &self.adapters {
let p = Path::new(&a.path);
if p.is_dir() {
let config: serde_json::Value = std::fs::read(p.join("adapter_config.json"))
.ok()
.and_then(|b| serde_json::from_slice(&b).ok())
.ok_or_else(|| format!("{}: {} has no readable adapter_config.json", a.id, a.path))?;
if !config["peft_type"].as_str().unwrap_or("LORA").eq_ignore_ascii_case("lora") {
return Err(format!("{}: {} is not a LoRA adapter", a.id, a.path));
}
} else if p.exists() {
return Err(format!("{}: vLLM takes a PEFT directory, not the file {}", a.id, a.path));
} else if weirdnet_model::hf::check_repo(&a.path).is_err() {
return Err(format!("{}: {} is neither a directory nor a Hub id", a.id, a.path));
}
}
}
}
Ok(())
}
pub fn capabilities(&self) -> Capabilities {
let own = if self.embedding {
Capabilities::embeddings()
} else {
let media = self.engine == EngineKind::Llamacpp && self.has_projector();
Capabilities::text().with_vision(media).with_audio(media)
};
own.intersect(self.engine.capabilities())
}
fn has_projector(&self) -> bool {
self.mmproj.is_some() || self.extra_args.iter().any(|a| matches!(a.split('=').next(), Some("--mmproj" | "-mm" | "--mmproj-url" | "-mmu")))
}
}
pub fn find_in_path(name: &str) -> Option<PathBuf> {
let path = std::env::var_os("PATH")?;
std::env::split_paths(&path).map(|d| d.join(name)).find(|p| is_executable(p))
}
fn is_executable(p: &Path) -> bool {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::metadata(p).is_ok_and(|m| m.is_file() && m.permissions().mode() & 0o111 != 0)
}
#[cfg(not(unix))]
{
p.is_file()
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
fn args(cmd: &Command) -> Vec<String> {
cmd.as_std().get_args().map(|a| a.to_string_lossy().into_owned()).collect()
}
#[test]
fn llama_args() {
let mut m = ModelLaunch::new("qwen3-8b", EngineKind::Llamacpp, "/m/q.gguf");
m.context = Some(8192);
m.slots = Some(2);
let cmd = EngineConfig::llamacpp("/bin/llama-server").command(&m, 4321, "k");
let a = args(&cmd);
assert!(a.windows(2).any(|w| w == ["--host", "127.0.0.1"]));
assert!(a.windows(2).any(|w| w == ["--port", "4321"]));
assert!(a.windows(2).any(|w| w == ["--alias", "qwen3-8b"]));
assert!(a.windows(2).any(|w| w == ["--parallel", "2"]));
assert!(!a.iter().any(|x| x == "k"), "the key stays out of argv");
}
#[test]
fn a_projector_goes_on_the_command_and_into_capabilities() {
let m = ModelLaunch::new("v", EngineKind::Llamacpp, "/m/v.gguf");
assert!(!m.capabilities().vision);
let m = m.with_mmproj("/m/mmproj.gguf");
let a = args(&EngineConfig::llamacpp("/bin/llama-server").command(&m, 4321, "k"));
assert!(a.windows(2).any(|w| w == ["--mmproj", "/m/mmproj.gguf"]));
assert!(m.capabilities().vision);
let back: ModelLaunch = serde_json::from_value(serde_json::to_value(&m).unwrap()).unwrap();
assert_eq!(back.mmproj.as_deref(), Some("/m/mmproj.gguf"));
}
pub(crate) fn gguf(kv: &[(&str, serde_json::Value)]) -> Vec<u8> {
let mut out = b"GGUF".to_vec();
out.extend(3u32.to_le_bytes());
out.extend(0u64.to_le_bytes());
out.extend((kv.len() as u64).to_le_bytes());
let s = |out: &mut Vec<u8>, s: &str| {
out.extend((s.len() as u64).to_le_bytes());
out.extend(s.as_bytes());
};
for (k, v) in kv {
s(&mut out, k);
match v {
serde_json::Value::String(t) => {
out.extend(8u32.to_le_bytes());
s(&mut out, t);
}
serde_json::Value::Number(n) if n.is_u64() => {
out.extend(4u32.to_le_bytes());
out.extend((n.as_u64().unwrap() as u32).to_le_bytes());
}
serde_json::Value::Number(n) => {
out.extend(6u32.to_le_bytes());
out.extend((n.as_f64().unwrap() as f32).to_le_bytes());
}
_ => unreachable!(),
}
}
out
}
pub(crate) fn base_gguf(arch: &str) -> Vec<u8> {
gguf(&[("general.architecture", arch.into()), (format!("{arch}.block_count").as_str(), 1.into())])
}
pub(crate) fn lora_gguf(arch: &str) -> Vec<u8> {
gguf(&[
("general.architecture", arch.into()),
("general.type", "adapter".into()),
("adapter.type", "lora".into()),
("adapter.lora.alpha", 16.5.into()),
])
}
#[test]
fn llama_adapter_args_and_checks() {
let tmp = tempfile::tempdir().unwrap();
let w = |name: &str, b: Vec<u8>| {
let p = tmp.path().join(name);
std::fs::write(&p, b).unwrap();
p.display().to_string()
};
let base = w("base.gguf", base_gguf("qwen3"));
let (a, b) = (w("a.gguf", lora_gguf("qwen3")), w("b.gguf", lora_gguf("qwen3")));
let m = ModelLaunch::new("qwen", EngineKind::Llamacpp, &base)
.with_adapters(vec![LoraAdapter::new("tune-a", &a).with_scale(0.5), LoraAdapter::new("tune-b", &b)]);
assert_eq!(m.served_ids(), ["qwen", "tune-a", "tune-b"]);
m.check_adapters().unwrap();
let argv = args(&EngineConfig::llamacpp("llama-server").command(&m, 1, "k"));
let loras: Vec<&String> = argv.windows(2).filter(|w| w[0] == "--lora").map(|w| &w[1]).collect();
assert_eq!(loras, [&a, &b], "one --lora per adapter, in order");
assert!(argv.iter().any(|x| x == "--lora-init-without-apply"));
assert!(
!args(&EngineConfig::llamacpp("l").command(&ModelLaunch::new("x", EngineKind::Llamacpp, &base), 1, "k"))
.iter()
.any(|x| x.starts_with("--lora"))
);
let bad =
|adapters: Vec<LoraAdapter>| ModelLaunch::new("qwen", EngineKind::Llamacpp, &base).with_adapters(adapters).check_adapters().unwrap_err();
assert!(bad(vec![LoraAdapter::new("qwen", &a)]).contains("base model's"));
assert!(bad(vec![LoraAdapter::new("t", &a), LoraAdapter::new("t", &b)]).contains("twice"));
assert!(bad(vec![LoraAdapter::new("t", &a).with_scale(f32::NAN)]).contains("finite"));
assert!(bad(vec![LoraAdapter::new("t", &base)]).contains("not a LoRA"));
assert!(bad(vec![LoraAdapter::new("t", w("llama.gguf", lora_gguf("llama")))]).contains("architecture"));
assert!(bad(vec![LoraAdapter::new("t", tmp.path().join("missing.gguf").display().to_string())]).contains("missing.gguf"));
assert!(bad(vec![LoraAdapter::new("t", w("a,b.gguf", lora_gguf("qwen3")))]).contains("comma"));
let mut emb = m.clone();
emb.embedding = true;
assert!(emb.check_adapters().unwrap_err().contains("embedding"));
let mut wrapped = m.clone();
wrapped.wrap = Some(Wrap { url: "http://127.0.0.1:1".into(), api_key: None });
assert!(wrapped.check_adapters().unwrap_err().contains("wrapped"));
let mlx = ModelLaunch::new("m", EngineKind::Mlx, &base).with_adapters(vec![LoraAdapter::new("t", &a)]);
assert!(mlx.check_adapters().unwrap_err().contains("mlx"));
let json = serde_json::to_string(&ModelLaunch::new("x", EngineKind::Llamacpp, "/m")).unwrap();
assert!(!json.contains("adapters"), "no adapters, same JSON as before");
}
#[test]
fn vllm_adapter_args_and_checks() {
let tmp = tempfile::tempdir().unwrap();
let peft = tmp.path().join("tune");
std::fs::create_dir(&peft).unwrap();
std::fs::write(peft.join("adapter_config.json"), br#"{"peft_type":"LORA","r":8}"#).unwrap();
let peft = peft.display().to_string();
let m = ModelLaunch::new("big", EngineKind::Vllm, "Qwen/Qwen3-8B")
.with_adapters(vec![LoraAdapter::new("tune", &peft), LoraAdapter::new("hub", "owner/some-lora")]);
m.check_adapters().unwrap();
let a = args(&EngineConfig::vllm("vllm").command(&m, 9000, "k"));
assert_eq!(a.iter().filter(|x| *x == "--lora-modules").count(), 1, "vLLM keeps only the last --lora-modules");
let at = a.iter().position(|x| x == "--lora-modules").unwrap();
let mods: Vec<serde_json::Value> = a[at + 1..at + 3].iter().map(|j| serde_json::from_str(j).unwrap()).collect();
assert_eq!(mods[0], serde_json::json!({"name": "tune", "path": peft, "base_model_name": "big"}));
assert_eq!(mods[1]["name"], "hub");
assert!(a.iter().any(|x| x == "--enable-lora"));
assert!(a.windows(2).any(|w| w == ["--max-loras", "2"]));
let cmd = EngineConfig::vllm("vllm").command(&m, 9000, "k");
let env: Vec<_> = cmd.as_std().get_envs().map(|(k, v)| (k.to_owned(), v.map(|v| v.to_owned()))).collect();
assert!(env.iter().any(|(k, v)| k == "VLLM_ALLOW_RUNTIME_LORA_UPDATING" && v.as_deref() == Some("True".as_ref())));
let empty = ModelLaunch::new("big", EngineKind::Vllm, "Qwen/Qwen3-8B").with_lora_slots(4);
assert!(empty.runtime_lora());
let a = args(&EngineConfig::vllm("vllm").command(&empty, 9000, "k"));
assert!(a.iter().any(|x| x == "--enable-lora") && !a.iter().any(|x| x == "--lora-modules"));
assert!(a.windows(2).any(|w| w == ["--max-loras", "4"]));
let plain = args(&EngineConfig::vllm("vllm").command(&ModelLaunch::new("big", EngineKind::Vllm, "Q/Q"), 9000, "k"));
assert!(!plain.iter().any(|x| x == "--enable-lora"));
assert!(!ModelLaunch::new("q", EngineKind::Llamacpp, "/q.gguf").with_lora_slots(2).runtime_lora(), "llama.cpp ignores it");
let file = tmp.path().join("x.gguf");
std::fs::write(&file, lora_gguf("qwen3")).unwrap();
let bad = |path: String| {
ModelLaunch::new("big", EngineKind::Vllm, "Q/Q").with_adapters(vec![LoraAdapter::new("t", path)]).check_adapters().unwrap_err()
};
assert!(bad(file.display().to_string()).contains("PEFT directory"));
assert!(bad("/no/such/dir".into()).contains("Hub id"));
}
#[test]
fn wrap_is_loopback_only() {
let w = |u: &str| Wrap { url: u.into(), api_key: None }.check();
assert!(w("http://127.0.0.1:8000").is_ok());
assert!(w("http://localhost:8000/").is_ok());
assert!(w("http://[::1]:8000").is_ok());
assert!(w("https://127.0.0.1:8000").is_err());
assert!(w("http://10.0.0.2:8000").is_err());
assert!(w("http://127.0.0.1.evil:8000").is_err());
assert_eq!(Wrap { url: "http://127.0.0.1:8000/v1/".into(), api_key: None }.base(), "http://127.0.0.1:8000");
}
#[test]
fn vllm_args() {
let m = ModelLaunch::new("big", EngineKind::Vllm, "Qwen/Qwen3-8B");
let a = args(&EngineConfig::vllm("vllm").command(&m, 9000, "k"));
assert_eq!(&a[..2], ["serve", "Qwen/Qwen3-8B"]);
assert!(a.windows(2).any(|w| w == ["--served-model-name", "big"]));
}
#[test]
fn mlx_args() {
let mut m = ModelLaunch::new("qwen3-8b", EngineKind::Mlx, "/m/mlx/Qwen3-8B-4bit");
m.context = Some(4096);
let cmd = EngineConfig::mlx("/v/bin/mlx_lm.server").command(&m, 9, "k");
assert_eq!(args(&cmd), ["--model", "/m/mlx/Qwen3-8B-4bit", "--host", "127.0.0.1", "--port", "9", "--max-tokens", "4096"]);
assert!(cmd.as_std().get_envs().all(|(_, v)| v != Some(std::ffi::OsStr::new("k"))), "MLX-LM gets no key");
m.context = None;
let a = args(&EngineConfig::mlx("mlx_lm.server").command(&m, 9, "k"));
assert_eq!(a.last().map(String::as_str), Some("32768"));
assert_eq!(serde_json::to_string(&EngineKind::Mlx).unwrap(), "\"mlx\"");
}
#[test]
fn capabilities_as_modalities() {
let media = Capabilities::text().with_vision(true).with_audio(true);
assert_eq!(media.inputs(), Modalities::new(true, true));
assert_eq!(Capabilities::text().lacking(Modalities::VISION), Some("images"));
assert_eq!(Capabilities::text().with_vision(true).lacking(Modalities::new(true, true)), Some("audio"));
assert_eq!(media.lacking(Modalities::AUDIO), None);
assert_eq!(
ModelLaunch::new("x", EngineKind::Mlx, "/m/x").capabilities().lacking(Modalities::VISION),
Some("images"),
"MLX never takes images"
);
}
#[test]
fn capabilities() {
let mut m = ModelLaunch::new("x", EngineKind::Mlx, "/m/x");
assert_eq!(m.capabilities(), Capabilities::text());
m.extra_args = vec!["--mmproj".into(), "/p".into()];
assert!(!m.capabilities().vision, "MLX-LM never takes images");
m.embedding = true;
assert_eq!(m.capabilities(), Capabilities::default(), "MLX-LM serves no embeddings");
let mut l = ModelLaunch::new("g", EngineKind::Llamacpp, "/m/g.gguf");
assert_eq!(l.capabilities(), Capabilities::text());
l.extra_args = vec!["--mmproj=/p/mm.gguf".into()];
let c = l.capabilities();
assert!(c.chat && c.vision && c.audio && !c.embeddings);
l.embedding = true;
assert_eq!(l.capabilities(), Capabilities::embeddings());
let v = ModelLaunch::new("v", EngineKind::Vllm, "Qwen/Qwen3-8B");
assert_eq!(v.capabilities(), Capabilities::text());
assert!(!EngineKind::Mlx.capabilities().embeddings && EngineKind::Llamacpp.capabilities().vision);
}
}