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

275 lines
10 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“.
//!
//! 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::<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();
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" => {
"<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\"/>"
}
// 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",
_ => "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 "<td class=\"faehig\"><span class=\"muted\">–</span></td>".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!(
"<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),
)
})
.collect();
format!("<td class=\"faehig\">{icons}</td>")
}
#[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"));
}
}