//! Fähigkeiten je Modell als Symbole auf der Modelle-Seite (#1081). //! //! Gezeigt wird nur, woher das Wissen stammt: **Engine** (die Engine kann es grundsätzlich, ob das //! Modell es kann, ist nicht geprüft) und **Datei** (aus den Dateien des Modells erkannt, zum Beispiel //! ein `mmproj` neben der Modelldatei). Ein Test am Modell selbst gibt es noch nicht; bis dahin //! steht in keinem Symbol „geprüft“. //! //! Darstellung: je Fähigkeit eine eigene Farbe (Klasse `k-…`); gefüllte Kachel = aus den Dateien //! erkannt, gestrichelter Umriss = nur Fähigkeit der Engine. use super::esc; use nexus_adapter::Capabilities; use std::{fs, path::Path}; /// Woher eine Angabe stammt. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub(super) enum Herkunft { Engine, Datei, } /// Symbole in fester Reihenfolge: Chat, Bilder (Vision), Werkzeuge, Embeddings, Streaming, Completions. pub(super) fn capability_icons(caps: &Capabilities) -> Vec<&'static str> { let mut icons = Vec::new(); if caps.chat { icons.push("chat"); } if caps.images { icons.push("vision"); } if caps.tool_calling { icons.push("tools"); } if caps.embeddings { icons.push("embed"); } if caps.streaming { icons.push("stream"); } if caps.completions { icons.push("completion"); } icons } const MAX_ENTRIES: usize = 512; const MAX_JSON_BYTES: u64 = 64 * 1024; /// Hat das Verzeichnis eine `mmproj*.gguf`? (Begrenzte Liste, keine Unterordner, keine Symlinks.) fn hat_mmproj(dir: &Path) -> bool { let Ok(entries) = fs::read_dir(dir) else { return false; }; entries.take(MAX_ENTRIES).flatten().any(|e| { let name = e.file_name(); let name = name.to_string_lossy().to_ascii_lowercase(); name.starts_with("mmproj") && name.ends_with(".gguf") && e.file_type().is_ok_and(|t| t.is_file()) }) } /// Was die Dateien eines Modells verraten. Nur Dinge, die sich sicher aus dem Dateisystem ergeben: /// llama.cpp: `mmproj` im Ordner der Modelldatei oder im Ordner darüber (dort liegt sie bei geteilten /// Modellordnern); Strata: der Eintrag `vision` in der Laufkonfiguration. pub(super) fn aus_dateien(engine: &str, path: &str) -> Vec<&'static str> { let mut found = Vec::new(); let p = Path::new(path); match engine { "llama.cpp" => { let dir = p.parent(); if dir.is_some_and(hat_mmproj) || dir.and_then(Path::parent).is_some_and(hat_mmproj) { found.push("vision"); } } "strata" => { let meta = fs::symlink_metadata(p); if meta.is_ok_and(|m| m.is_file() && m.len() <= MAX_JSON_BYTES) && let Ok(text) = fs::read_to_string(p) && let Ok(json) = serde_json::from_str::(&text) && json .get("vision") .is_some_and(|v| !v.is_null() && *v != serde_json::Value::Bool(false)) { found.push("vision"); } } _ => {} } found } /// Alle Symbole eines Modells mit Herkunft: erst aus den Dateien, dann die der Engine (ohne Doppelte). pub(super) fn symbole( caps: Option<&Capabilities>, engine: &str, path: &str, ) -> Vec<(&'static str, Herkunft)> { let mut out: Vec<(&'static str, Herkunft)> = Vec::new(); let datei = aus_dateien(engine, path); let engine_icons = caps.map(capability_icons).unwrap_or_default(); for key in ["chat", "vision", "tools", "embed", "stream", "completion"] { if datei.contains(&key) { out.push((key, Herkunft::Datei)); } else if engine_icons.contains(&key) { out.push((key, Herkunft::Engine)); } } out } /// Symbole aus Lucide (`lucide-static` 1.74.0, ISC-Lizenz, Copyright (c) 2026 Lucide Icons and /// Contributors; Text in `docs/lizenzen/lucide-ISC.txt`). Je Fähigkeit nur die Pfade im 24×24-Raster. fn svg(key: &str) -> &'static str { match key { // message-square "chat" => { "" } // eye "vision" => { "" } // wrench "tools" => { "" } // network "embed" => { "" } // audio-lines "stream" => { "" } // braces _ => { "" } } } fn titel(key: &str) -> &'static str { match key { "chat" => "Chat", "vision" => "Bilder verstehen (Vision)", "tools" => "Werkzeuge aufrufen (Tool Calling)", "embed" => "Embeddings", "stream" => "Streaming", _ => "Completions", } } /// Die Zelle für die Tabelle: je Symbol ein Bild mit Hinweistext zur Herkunft. pub(super) fn zelle(symbole: &[(&'static str, Herkunft)]) -> String { if symbole.is_empty() { return "–".to_string(); } let icons: String = symbole .iter() .map(|(key, herkunft)| { let (klasse, hinweis) = match herkunft { Herkunft::Datei => ("datei", "aus den Dateien des Modells erkannt"), Herkunft::Engine => ( "engine", "Fähigkeit der Engine, für dieses Modell nicht geprüft", ), }; format!( "{t}: {h}{p}", t = esc(titel(key)), h = esc(hinweis), p = svg(key), ) }) .collect(); format!("{icons}") } #[cfg(test)] mod tests { use super::*; fn caps( chat: bool, images: bool, tools: bool, embed: bool, stream: bool, compl: bool, ) -> Capabilities { Capabilities { chat, completions: compl, embeddings: embed, streaming: stream, tool_calling: tools, images, } } #[test] fn icons_in_fester_reihenfolge() { assert!(capability_icons(&caps(false, false, false, false, false, false)).is_empty()); assert_eq!( capability_icons(&caps(true, false, false, false, false, false)), ["chat"] ); assert_eq!( capability_icons(&caps(true, true, false, false, false, false)), ["chat", "vision"] ); assert_eq!( capability_icons(&caps(true, true, true, true, true, true)), ["chat", "vision", "tools", "embed", "stream", "completion"] ); } #[test] fn mmproj_neben_oder_ueber_der_modelldatei_zeigt_vision() { let base = std::env::temp_dir().join(format!("nexus-faehig-{}", std::process::id())); let _ = fs::remove_dir_all(&base); let sub = base.join("modell"); fs::create_dir_all(&sub).unwrap(); let datei = sub.join("m.gguf"); fs::write(&datei, b"x").unwrap(); assert!(aus_dateien("llama.cpp", datei.to_str().unwrap()).is_empty()); fs::write(base.join("mmproj-F16.gguf"), b"x").unwrap(); assert_eq!( aus_dateien("llama.cpp", datei.to_str().unwrap()), ["vision"] ); fs::write(sub.join("mmproj-x.gguf"), b"x").unwrap(); assert_eq!( aus_dateien("llama.cpp", datei.to_str().unwrap()), ["vision"] ); // Andere Engines lesen kein mmproj. assert!(aus_dateien("ollama", datei.to_str().unwrap()).is_empty()); let _ = fs::remove_dir_all(&base); } #[test] fn strata_vision_steht_in_der_laufkonfiguration() { let base = std::env::temp_dir().join(format!("nexus-faehig-s-{}", std::process::id())); fs::create_dir_all(&base).unwrap(); let mit = base.join("mit.json"); let ohne = base.join("ohne.json"); let aus = base.join("aus.json"); fs::write(&mit, br#"{"vision": {"mmproj": "x"}}"#).unwrap(); fs::write(&ohne, br#"{"gpu": [0]}"#).unwrap(); fs::write(&aus, br#"{"vision": false}"#).unwrap(); assert_eq!(aus_dateien("strata", mit.to_str().unwrap()), ["vision"]); assert!(aus_dateien("strata", ohne.to_str().unwrap()).is_empty()); assert!(aus_dateien("strata", aus.to_str().unwrap()).is_empty()); assert!(aus_dateien("strata", "/gibt/es/nicht.json").is_empty()); let _ = fs::remove_dir_all(&base); } #[test] fn herkunft_datei_geht_vor_engine_ohne_doppelte() { let c = caps(true, false, false, false, true, false); let s = symbole(Some(&c), "ollama", "qwen3:0.6b"); assert_eq!( s, vec![("chat", Herkunft::Engine), ("stream", Herkunft::Engine)] ); let leer = symbole(None, "unbekannt", ""); assert!(leer.is_empty()); assert!(zelle(&leer).contains("–")); let html = zelle(&s); assert!(html.contains("fsym engine k-chat") && html.contains("nicht geprüft")); } }