Files
nexus/crates/nexus-api/src/ui/faehigkeiten.rs

444 lines
18 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! 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“.
//!
//! Je Modell nur, was vom Modell abhängt (Bilder, Werkzeuge, Embeddings, Sprachausgabe). Chat, Streaming und
//! Completions sind Fähigkeiten der Engine und stehen auf der Seite „Engines“.
//!
//! 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,
/// Am Modell selbst getestet (Selbsttest, #1157).
Test,
}
/// Symbole in fester Reihenfolge: Chat, Bilder (Vision), Werkzeuge, Embeddings, Streaming, Completions, Sprachausgabe.
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");
}
if caps.speech {
icons.push("speech");
}
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::<serde_json::Value>(&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();
// Chat, Streaming und Completions hängen an der Engine, nicht am Modell: Sie stehen auf der
// Seite „Engines“ (`engine_faehigkeiten`), nicht in der Spalte „Kann“ je Modell.
for key in ["vision", "tools", "embed", "speech"] {
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" => {
"<path d=\"M22 17a2 2 0 0 1-2 2H6.828a2 2 0 0 0-1.414.586l-2.202 2.202A.71.71 0 0 1 2 21.286V5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2z\"/>"
}
// eye
"vision" => {
"<path d=\"M2.062 12.348a1 1 0 0 1 0-.696 10.75 10.75 0 0 1 19.876 0 1 1 0 0 1 0 .696 10.75 10.75 0 0 1-19.876 0\"/><circle cx=\"12\" cy=\"12\" r=\"3\"/>"
}
// wrench
"tools" => {
"<path d=\"M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.106-3.105c.32-.322.863-.22.983.218a6 6 0 0 1-8.259 7.057l-7.91 7.91a1 1 0 0 1-2.999-3l7.91-7.91a6 6 0 0 1 7.057-8.259c.438.12.54.662.219.984z\"/>"
}
// network
"embed" => {
"<rect x=\"16\" y=\"16\" width=\"6\" height=\"6\" rx=\"1\"/><rect x=\"2\" y=\"16\" width=\"6\" height=\"6\" rx=\"1\"/><rect x=\"9\" y=\"2\" width=\"6\" height=\"6\" rx=\"1\"/><path d=\"M5 16v-3a1 1 0 0 1 1-1h12a1 1 0 0 1 1 1v3\"/><path d=\"M12 12V8\"/>"
}
// audio-lines
"stream" => {
"<path d=\"M2 10v3\"/><path d=\"M6 6v11\"/><path d=\"M10 3v18\"/><path d=\"M14 8v7\"/><path d=\"M18 5v13\"/><path d=\"M22 10v3\"/>"
}
// volume-2
"speech" => {
"<path d=\"M11 4.702a.705.705 0 0 0-1.203-.498L6.413 7.587A1.4 1.4 0 0 1 5.416 8H3a1 1 0 0 0-1 1v6a1 1 0 0 0 1 1h2.416a1.4 1.4 0 0 1 .997.413l3.383 3.384A.705.705 0 0 0 11 19.298z\"/><path d=\"M16 9a5 5 0 0 1 0 6\"/><path d=\"M19.364 18.364a9 9 0 0 0 0-12.728\"/>"
}
// lightbulb
"think" => {
"<path d=\"M15 14c.2-1 .7-1.7 1.5-2.5 1-.9 1.5-2.2 1.5-3.5A6 6 0 0 0 6 8c0 1 .2 2.2 1.5 3.5.7.7 1.3 1.5 1.5 2.5\"/><path d=\"M9 18h6\"/><path d=\"M10 22h4\"/>"
}
// braces
_ => {
"<path d=\"M8 3H7a2 2 0 0 0-2 2v5a2 2 0 0 1-2 2 2 2 0 0 1 2 2v5c0 1.1.9 2 2 2h1\"/><path d=\"M16 21h1a2 2 0 0 0 2-2v-5c0-1.1.9-2 2-2a2 2 0 0 1-2-2V5a2 2 0 0 0-2-2h-1\"/>"
}
}
}
fn titel(key: &str) -> &'static str {
match key {
"chat" => "Chat",
"vision" => "Bilder verstehen (Vision)",
"tools" => "Werkzeuge aufrufen (Tool Calling)",
"embed" => "Embeddings",
"stream" => "Streaming",
"speech" => "Sprachausgabe",
"think" => "Denkstufe",
_ => "Completions",
}
}
/// Symbol „Engine bringt eine Oberfläche mit“ (Lucide `app-window`): mit Adresse als Link für Admins,
/// sonst nur als Hinweis (die Engine hat eine Oberfläche, der Zugang fehlt oder ist nicht erlaubt).
pub(super) fn oberflaeche(frontend: &nexus_adapter::Frontend, admin: bool) -> String {
use nexus_adapter::Frontend;
let (href, hinweis) = match frontend {
Frontend::None => return String::new(),
Frontend::Url(url) if admin => (
Some(url.as_str()),
"Oberfläche der Engine öffnen (eigene Adresse, ohne Anmeldung des Gateways)",
),
Frontend::Url(_) => (
None,
"Die Engine hat eine eigene Oberfläche (Zugang nur für Admins)",
),
Frontend::Managed => (
None,
"Die Engine hat eine eigene Oberfläche; der Zugang über das Gateway ist noch nicht gebaut",
),
};
let svg = format!(
"<svg class=\"fsym {} k-chat\" viewBox=\"0 0 24 24\" role=\"img\" aria-label=\"{h}\"><title>{h}</title>\
<rect x=\"2\" y=\"4\" width=\"20\" height=\"16\" rx=\"2\"/><path d=\"M10 4v4\"/><path d=\"M2 8h20\"/><path d=\"M6 4v4\"/></svg>",
if href.is_some() { "gemeldet" } else { "engine" },
h = esc(hinweis),
);
match href {
Some(url) => format!(
"<a class=\"fenster\" href=\"{}\" target=\"_blank\" rel=\"noopener noreferrer\">{svg}</a>",
esc(url)
),
None => format!("<span class=\"fenster\">{svg}</span>"),
}
}
/// Zustand eines Modells als Symbol (Lucide): `geladen`, `laedt`, `entlaedt`, `wartet`, sonst nicht geladen.
/// `text` steht im Hinweis und für Screenreader; der Tabellenfilter liest ihn aus `data-sort`.
pub(super) fn zustand(art: &str, text: &str) -> String {
let (klasse, farbe, pfade) = match art {
"geladen" => (
"gemeldet",
"z-ok",
"<circle cx=\"12\" cy=\"12\" r=\"10\"/><path d=\"m16 9-5.5 5.5L8 12\"/>",
),
"laedt" | "entlaedt" => (
"gemeldet",
"z-warn",
"<path d=\"M21 12a9 9 0 1 1-6.219-8.56\"/>",
),
"wartet" => (
"gemeldet",
"z-warn",
"<path d=\"M5 22h14\"/><path d=\"M5 2h14\"/><path d=\"M17 22v-4.172a2 2 0 0 0-.586-1.414L12 12l-4.414 4.414A2 2 0 0 0 7 17.828V22\"/><path d=\"M7 2v4.172a2 2 0 0 0 .586 1.414L12 12l4.414-4.414A2 2 0 0 0 17 6.172V2\"/>",
),
_ => (
"engine",
"z-aus",
"<path d=\"M10.1 2.182a10 10 0 0 1 3.8 0\"/><path d=\"M13.9 21.818a10 10 0 0 1-3.8 0\"/><path d=\"M17.609 3.721a10 10 0 0 1 2.69 2.7\"/><path d=\"M2.182 13.9a10 10 0 0 1 0-3.8\"/><path d=\"M20.279 17.609a10 10 0 0 1-2.7 2.69\"/><path d=\"M21.818 10.1a10 10 0 0 1 0 3.8\"/><path d=\"M3.721 6.391a10 10 0 0 1 2.7-2.69\"/><path d=\"M6.391 20.279a10 10 0 0 1-2.69-2.7\"/>",
),
};
format!(
"<svg class=\"fsym {klasse} {farbe}\" viewBox=\"0 0 24 24\" role=\"img\" aria-label=\"{t}\"><title>{t}</title>{pfade}</svg>",
t = esc(text)
)
}
/// Freigabe eines Modells als Symbol (Lucide `circle-check` und `lock`): freigegeben (grün) oder gesperrt (rot).
/// Der Text steht im Hinweis und für Screenreader; der Tabellenfilter liest ihn aus `data-sort`.
pub(super) fn freigabe(enabled: bool) -> String {
let (text, farbe, pfade) = if enabled {
(
"freigegeben",
"z-ok",
"<circle cx=\"12\" cy=\"12\" r=\"10\"/><path d=\"m9 12 2 2 4-4\"/>",
)
} else {
(
"gesperrt",
"z-fehler",
"<rect width=\"18\" height=\"11\" x=\"3\" y=\"11\" rx=\"2\" ry=\"2\"/><path d=\"M7 11V7a5 5 0 0 1 10 0v4\"/>",
)
};
format!(
"<svg class=\"fsym gemeldet {farbe}\" viewBox=\"0 0 24 24\" role=\"img\" aria-label=\"{text}\"><title>{text}</title>{pfade}</svg>"
)
}
/// GPU-Nummern („0, 1“) als Chip-Symbole mit Nummer.
pub(super) fn gpu_symbole(gpus: &str) -> String {
gpus.split(',')
.map(str::trim)
.filter(|n| !n.is_empty())
.map(|n| {
format!(
"<span class=\"fkann\" title=\"GPU {n}\"><svg class=\"fsym engine k-embed\" viewBox=\"0 0 24 24\" role=\"img\" aria-label=\"GPU {n}\"><path d=\"M12 20v2\"/><path d=\"M12 2v2\"/><path d=\"M17 20v2\"/><path d=\"M17 2v2\"/><path d=\"M2 12h2\"/><path d=\"M2 17h2\"/><path d=\"M2 7h2\"/><path d=\"M20 12h2\"/><path d=\"M20 17h2\"/><path d=\"M20 7h2\"/><path d=\"M7 20v2\"/><path d=\"M7 2v2\"/><rect x=\"4\" y=\"4\" width=\"16\" height=\"16\" rx=\"2\"/><rect x=\"8\" y=\"8\" width=\"8\" height=\"8\" rx=\"1\"/></svg>{n}</span>",
n = esc(n)
)
})
.collect()
}
fn symbol(key: &str, klasse: &str, hinweis: &str) -> String {
format!(
"<svg class=\"fsym {klasse} k-{key}\" viewBox=\"0 0 24 24\" role=\"img\" aria-label=\"{t}: {h}\"><title>{t}: {h}</title>{p}</svg>",
t = esc(titel(key)),
h = esc(hinweis),
p = svg(key),
)
}
/// 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 "<td class=\"faehig\"><span class=\"muted\">–</span></td>".to_string();
}
let icons: String = symbole
.iter()
.map(|(key, herkunft)| match herkunft {
Herkunft::Datei => symbol(key, "datei", "aus den Dateien des Modells erkannt"),
Herkunft::Test => symbol(key, "datei", "am Modell getestet (Selbsttest)"),
Herkunft::Engine => symbol(
key,
"engine",
"Fähigkeit der Engine, für dieses Modell nicht geprüft",
),
})
.collect();
format!("<td class=\"faehig\">{icons}</td>")
}
/// Die Fähigkeiten einer Engine für die Seite „Engines“: Symbol mit Namen, so wie die Engine sie meldet.
pub(super) fn engine_faehigkeiten(caps: &Capabilities) -> String {
capability_icons(caps)
.into_iter()
.map(|key| {
format!(
"<span class=\"fkann\">{}{}</span>",
symbol(key, "gemeldet", "von der Engine gemeldet"),
esc(titel(key))
)
})
.collect()
}
#[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,
speech: false,
}
}
#[test]
fn sprachausgabe_ist_ein_eigenes_symbol_je_modell() {
let c = Capabilities {
speech: true,
..Capabilities::default()
};
assert_eq!(capability_icons(&c), ["speech"]);
let s = symbole(Some(&c), "tts", "Qwen/Qwen3-TTS-12Hz-1.7B-Base");
assert_eq!(s, vec![("speech", Herkunft::Engine)]);
assert!(zelle(&s).contains("k-speech") && zelle(&s).contains("Sprachausgabe"));
assert!(engine_faehigkeiten(&c).contains("Sprachausgabe"));
}
#[test]
fn oberflaechen_symbol_ist_nur_fuer_admins_ein_link() {
use nexus_adapter::Frontend;
assert!(oberflaeche(&Frontend::None, true).is_empty());
let url = Frontend::Url("http://192.168.12.4:8862".to_string());
let admin = oberflaeche(&url, true);
assert!(admin.contains("<a class=\"fenster\" href=\"http://192.168.12.4:8862\""));
assert!(admin.contains("noopener") && admin.contains("_blank"));
let nutzer = oberflaeche(&url, false);
assert!(!nutzer.contains("href=") && nutzer.contains("nur für Admins"));
// Verwaltete Engine: Symbol ja, Link nein (Weiterleitung fehlt, #1101).
let verwaltet = oberflaeche(&Frontend::Managed, true);
assert!(!verwaltet.contains("href=") && verwaltet.contains("noch nicht gebaut"));
}
#[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, true, false, true, false);
let s = symbole(Some(&c), "ollama", "qwen3:0.6b");
assert_eq!(s, vec![("tools", Herkunft::Engine)]);
let leer = symbole(None, "unbekannt", "");
assert!(leer.is_empty());
assert!(zelle(&leer).contains("–"));
let html = zelle(&s);
// Chat und Streaming gehören zur Engine, nicht zum Modell.
assert!(!html.contains("k-chat") && !html.contains("k-stream"));
let eng = engine_faehigkeiten(&c);
assert!(
eng.contains("k-chat") && eng.contains("Streaming") && eng.contains("fsym gemeldet")
);
assert!(html.contains("fsym engine k-tools") && html.contains("nicht geprüft"));
}
}