Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(local-api): LU Engine models back in /v1/models, loaded on demand (C57)

purple979ed08ffad6
diff --git a/src-tauri/src/commands/local_api.rs b/src-tauri/src/commands/local_api.rs
index 34a8449f..e0ab37b0 100644
--- a/src-tauri/src/commands/local_api.rs
+++ b/src-tauri/src/commands/local_api.rs
@@ -516,6 +516,11 @@ use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tokio::sync::RwLock;
+use super::local_api_engine::{
+ alte_engine_id_lesen, bereitstellen, engine_katalog, name_aus_pfad, EngineHost, EngineModell, LadeSchloss,
+ WARTEN_AUF_BEREIT, WARTEN_AUF_FREI,
+};
+
/// Wie eine Lane erreicht wird. `base` endet OHNE Schraegstrich und schliesst
/// `/v1` ein, weil genau das die Adresse ist, die alle drei Programme anbieten.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
@@ -695,6 +700,12 @@ pub struct LocalApiState {
/// Die laufenden Adressen der App. `None` in Tests und wenn der Aufrufer
/// eigene `upstreams` uebergeben hat: dann gelten genau die.
pub live: Option<LaneQuelle>,
+ /// The app's LU Engine: installed models, the loaded one, loading on
+ /// demand (local_api_engine.rs, C57). `None` in tests and with caller
+ /// upstreams: then the engine lane is what its `/v1/models` says.
+ pub engine: Option<Arc<dyn EngineHost>>,
+ /// One load decision at a time (`local_api_engine::bereitstellen`).
+ pub lade_schloss: LadeSchloss,
}
/// Die Upstreams, wie sie JETZT gelten: `cfg.upstreams`, wo die App fuer eine
@@ -799,6 +810,23 @@ fn fehler_mit(code: StatusCode, typ: &str, msg: &str, param: Option<&str>, fehle
antwort
}
+/// Was ein Client bei einer 401 liest.
+///
+/// GitHub Discussion #14: "API key not working even though I haven't set
+/// one". Die API verlangt IMMER einen Schluessel; LU erzeugt ihn, der Nutzer
+/// waehlt ihn nicht. Die alte Meldung sagte nur "Missing or wrong API key" und
+/// nicht, wo der Schluessel steht. Jetzt: ob keiner kam oder ein falscher, und
+/// wo der richtige zu finden ist.
+pub fn schluessel_fehlertext(erwartet: &str, gezeigt: &str) -> &'static str {
+ if erwartet.trim().is_empty() {
+ "The LU local API has no API key yet. In LU open Settings → Local API, press Generate, then paste that key into your app."
+ } else if gezeigt.trim().is_empty() {
+ "No API key was sent. The LU local API always needs one, also on this machine: copy it from LU → Settings → Local API (API Key) and paste it into your app's API key field. Raw HTTP: `Authorization: Bearer <key>` or `x-api-key: <key>`."
+ } else {
+ "Wrong API key. Copy the current one from LU → Settings → Local API (API Key) and paste it into your app. Pressing New in LU replaces the key, and the old one stops working."
+ }
+}
+
/// Token-Schranke. Kein Pfad hinter dem Router ist oeffentlich — auch
/// `/v1/models` nicht, denn die Modellliste verraet, was auf diesem Rechner
/// liegt, und das ist bei einer App namens Locally Uncensored eine Auskunft
@@ -811,17 +839,7 @@ async fn auth(
let erwartet = state.cfg.read().await.token.clone();
let gezeigt = presented_token(&header_map_to_btree(req.headers()));
if !token_matches(&erwartet, &gezeigt) {
- return fehler_mit(
- StatusCode::UNAUTHORIZED,
- "invalid_request_error",
- if erwartet.is_empty() {
- "The local API has no token yet. Open Settings and generate one — until then it answers nobody."
- } else {
- "Missing or wrong API key. Send it as `Authorization: Bearer <token>` or `x-api-key`."
- },
- None,
- Some("invalid_api_key"),
- );
+ return fehler_mit(StatusCode::UNAUTHORIZED, "invalid_request_error", schluessel_fehlertext(&erwartet, &gezeigt), None, Some("invalid_api_key"));
}
next.run(req).await
}
@@ -837,9 +855,20 @@ struct ModelZeile {
/// Fragt eine Lane nach ihren Modellen. Ein Fehler ist KEIN Abbruch: eine
/// nicht laufende Lane ist der Normalfall (wer nutzt schon alle drei
/// gleichzeitig), und sie darf die Liste der anderen nicht mitnehmen.
+/// Was eine Lane auf `/models` meldet.
+///
+/// C57 (3.1.0): die LU Engine bekommt ihren Schluessel auch hier. Seit
+/// llama.cpp b11960 (LU 3.0.9) ist `/v1/models` dort nicht mehr offen; ohne
+/// Schluessel kam eine 401, und die Lane galt als leer. Die Engine nennt ihr
+/// Modell beim vollen Pfad; nach aussen heisst es wie auf der Modellseite.
async fn lane_modelle(http: &reqwest::Client, up: &Upstream) -> Vec<String> {
let url = format!("{}/models", up.base.trim_end_matches('/'));
- let r = match http.get(&url).timeout(std::time::Duration::from_secs(4)).send().await {
+ let mut req = http.get(&url).timeout(std::time::Duration::from_secs(4));
+ let lane = up.lane();
+ if let Some(s) = lane.and_then(|l| engine_schluessel(l, &url)) {
+ req = req.header(header::AUTHORIZATION, s);
+ }
+ let r = match req.send().await {
Ok(r) if r.status().is_success() => r,
_ => return Vec::new(),
};
@@ -847,10 +876,23 @@ async fn lane_modelle(http: &reqwest::Client, up: &Upstream) -> Vec<String> {
Ok(v) => v,
Err(_) => return Vec::new(),
};
- v.get("data")
+ let ids: Vec<String> = v
+ .get("data")
.and_then(|d| d.as_array())
.map(|a| a.iter().filter_map(|m| m.get("id").and_then(|i| i.as_str()).map(str::to_string)).collect())
- .unwrap_or_default()
+ .unwrap_or_default();
+ if lane == Some(Lane::Engine) {
+ return ids.iter().map(|id| name_aus_pfad(id)).filter(|n| !n.is_empty()).collect();
+ }
+ ids
+}
+
+/// Die Modelle der LU Engine, wenn die App sie kennt: installiert und geladen.
+async fn engine_modelle(host: &Arc<dyn EngineHost>) -> Vec<EngineModell> {
+ let h = Arc::clone(host);
+ let installiert = tokio::task::spawn_blocking(move || h.installiert()).await.unwrap_or_default();
+ let geladen = host.geladen().map(|(p, _)| p);
+ engine_katalog(&installiert, geladen.as_deref())
}
/// Der aktuelle Katalog ueber alle Lanes, in Lane-Reihenfolge.
@@ -858,6 +900,12 @@ async fn katalog(state: &LocalApiState) -> Vec<(Lane, String)> {
let ups = upstreams_jetzt(state).await;
let mut raus: Vec<(Lane, String)> = Vec::new();
for lane in Lane::ALL {
+ if lane == Lane::Engine {
+ if let Some(host) = &state.engine {
+ raus.extend(engine_modelle(host).await.into_iter().map(|m| (lane, m.name)));
+ continue;
+ }
+ }
for up in ups.iter().filter(|u| u.lane() == Some(lane)) {
for id in lane_modelle(&state.http, up).await {
raus.push((lane, id));
@@ -933,7 +981,11 @@ async fn weiterleiten_mit(
);
}
- let gewuenscht = json.get("model").and_then(|m| m.as_str()).unwrap_or("").to_string();
+ let mut gewuenscht = json.get("model").and_then(|m| m.as_str()).unwrap_or("").to_string();
+ // Eine Engine-ID aus 3.0.8 (`lu/<voller Pfad>`) meint das Modell dieses Namens.
+ if let Some(neu) = alte_engine_id_lesen(&gewuenscht) {
+ gewuenscht = neu;
+ }
let k = katalog(&state).await;
let ziel = match resolve_model(&gewuenscht, &k) {
Ok(m) => m,
@@ -953,9 +1005,34 @@ async fn weiterleiten_mit(
let stream_gewuenscht = json.get("stream").and_then(|v| v.as_bool()).unwrap_or(false);
- let base = match upstreams_jetzt(&state).await.into_iter().find(|u| u.lane() == Some(ziel.lane)) {
- Some(u) => u.base.trim_end_matches('/').to_string(),
- None => return fehler(StatusCode::BAD_GATEWAY, "api_error", &format!("{} has no configured address.", ziel.lane.label())),
+ // C57: ein LU-Engine-Modell, das nicht geladen ist, wird jetzt geladen,
+ // wie Ollama es beim ersten Aufruf tut. Embeddings nicht: die laufen in
+ // der App ueber einen eigenen Engine-Prozess, nicht ueber den Chat.
+ let mut engine_port: Option<u16> = None;
+ if ziel.lane == Lane::Engine && pfad != "/embeddings" {
+ if let Some(host) = &state.engine {
+ let modelle = engine_modelle(host).await;
+ if let Some(m) = modelle.into_iter().find(|m| m.name == ziel.upstream_id) {
+ match bereitstellen(Arc::clone(host), &state.lade_schloss, &state.http, &m, WARTEN_AUF_FREI, WARTEN_AUF_BEREIT).await {
+ Ok(p) => engine_port = Some(p),
+ Err(e) => {
+ let code = match e {
+ super::local_api_engine::LadeFehler::Belegt { .. } => StatusCode::SERVICE_UNAVAILABLE,
+ super::local_api_engine::LadeFehler::Start { .. } => StatusCode::INTERNAL_SERVER_ERROR,
+ };
+ return fehler_mit(code, "api_error", &e.message(), Some("model"), Some(e.code()));
+ }
+ }
+ }
+ }
+ }
+
+ let base = match engine_port {
+ Some(p) => format!("http://127.0.0.1:{}/v1", p),
+ None => match upstreams_jetzt(&state).await.into_iter().find(|u| u.lane() == Some(ziel.lane)) {
+ Some(u) => u.base.trim_end_matches('/').to_string(),
+ None => return fehler(StatusCode::BAD_GATEWAY, "api_error", &format!("{} has no configured address.", ziel.lane.label())),
+ },
};
let ziel_url = format!("{}{}", base, pfad);
@@ -1144,14 +1221,19 @@ async fn handle_health(AxumState(state): AxumState<LocalApiState>) -> Response {
let k = katalog(&state).await;
let mut je_lane = serde_json::Map::new();
for lane in Lane::ALL {
- je_lane.insert(
- lane.prefix().to_string(),
- serde_json::json!({
- "label": lane.label(),
- "base": ups.iter().find(|u| u.lane() == Some(lane)).map(|u| u.base.clone()),
- "models": k.iter().filter(|(l, _)| *l == lane).count(),
- }),
- );
+ let mut zeile = serde_json::json!({
+ "label": lane.label(),
+ "base": ups.iter().find(|u| u.lane() == Some(lane)).map(|u| u.base.clone()),
+ "models": k.iter().filter(|(l, _)| *l == lane).count(),
+ });
+ // Die Engine haelt ein Modell zur Zeit; welches, steht hier.
+ if lane == Lane::Engine {
+ if let Some(host) = &state.engine {
+ let geladen = host.geladen().map(|(p, _)| qualified_id(lane, &name_aus_pfad(&p)));
+ zeile["loaded"] = serde_json::json!(geladen);
+ }
+ }
+ je_lane.insert(lane.prefix().to_string(), zeile);
}
Json(serde_json::json!({
"ok": true,
@@ -1262,7 +1344,7 @@ fn lu_doku() -> serde_json::Value {
"round_trip": "Responses echo the qualified id, so what comes back is always findable in GET /v1/models.",
},
"routes": [
- { "method": "GET", "path": "/v1/models", "note": "Every model across every running lane." },
+ { "method": "GET", "path": "/v1/models", "note": "Every model across every running lane. LU Engine models are listed whether loaded or not." },
{ "method": "POST", "path": "/v1/chat/completions", "note": "Requires `model` and `messages`. `stream: true` streams." },
{ "method": "POST", "path": "/v1/completions", "note": "Requires `model` and `prompt`." },
{ "method": "POST", "path": "/v1/embeddings", "note": "Requires `model` and `input`." },
@@ -1270,10 +1352,11 @@ fn lu_doku() -> serde_json::Value {
{ "method": "GET", "path": "/lu/v1/tools", "note": "Catalogue of LU agent tools. Not callable over HTTP yet." },
{ "method": "GET", "path": "/lu/v1/docs", "note": "This page." },
],
+ "loading": "The LU Engine holds one model at a time. A chat or completion for an LU Engine model that is not loaded loads it first, so that first answer takes as long as the load (like Ollama). A swap waits until the engine has finished the answer it is writing. GET /lu/v1/health names the loaded model.",
"passthrough": "Everything other than `model` goes to the backend untouched — temperature, tools, response_format, and whatever else that backend supports. What a lane cannot do, this API cannot add.",
"errors": {
"shape": "OpenAI's: { error: { message, type, param, code } }",
- "codes": ["invalid_api_key", "invalid_body", "missing_required_parameter", "model_not_found", "model_ambiguous", "lane_unavailable"],
+ "codes": ["invalid_api_key", "invalid_body", "missing_required_parameter", "model_not_found", "model_ambiguous", "lane_unavailable", "model_load_failed", "engine_busy"],
"lane_unavailable": "The name asked for a real lane that currently holds no models — that backend is most likely not running.",
},
"cors": "Off until you list an origin under Settings → Local API. No wildcard: name the origin you want to let read your local model traffic.",
@@ -1410,6 +1493,8 @@ pub fn build_local_api_router(state: LocalApiState) -> Router {
/// einzige Stelle, an der Regel 1 wirklich durchgesetzt werden kann: ein
/// laufender Server ohne Token waere auch mit `token_matches`-Schutz eine
/// offene Tuer, die nur zufaellig zu ist.
+// Tauri-Befehle nehmen ihre Argumente flach; jedes ist ein Feld der Einstellung.
+#[allow(clippy::too_many_arguments)]
#[tauri::command]
pub async fn start_local_api(
app: tauri::AppHandle,
@@ -1419,6 +1504,8 @@ pub async fn start_local_api(
token: String,
upstreams: Option<Vec<Upstream>>,
cors_origins: Option<Vec<String>>,
+ engine_dirs: Option<Vec<String>>,
+ engine_tuning: Option<crate::commands::engine::EngineTuning>,
) -> Result<serde_json::Value, String> {
if token.trim().is_empty() {
return Err("The local API needs a token before it can start. Generate one in Settings.".into());
@@ -1446,6 +1533,11 @@ pub async fn start_local_api(
// Wer eigene Upstreams uebergibt, bekommt genau die. Sonst gelten die
// Adressen, die die App selbst gerade benutzt, bei jeder Anfrage neu
// gelesen (siehe `LaneQuelle`).
+ let engine: Option<Arc<dyn EngineHost>> = if eigene_upstreams {
+ None
+ } else {
+ app_engine(&app, engine_dirs.unwrap_or_default(), engine_tuning)
+ };
let live: Option<LaneQuelle> = if eigene_upstreams {
None
} else {
@@ -1465,7 +1557,7 @@ pub async fn start_local_api(
.build()
.map_err(|e| e.to_string())?,
nutzung: Arc::new(Nutzung::neu()),
- live,
+ live, engine, lade_schloss: Default::default(),
};
let nutzung = Arc::clone(&api_state.nutzung);
@@ -1496,6 +1588,32 @@ pub async fn start_local_api(
}))
}
+/// Die Engine der App fuer die API: installierte Modelle in den Ordnern der
+/// App und denen unter Storage, gestartet mit der Abstimmung aus den
+/// Einstellungen (wie die Vorpruefung des Chats, api/builtin-ensure.ts).
+#[cfg(desktop)]
+fn app_engine(
+ app: &tauri::AppHandle,
+ ordner: Vec<String>,
+ tuning: Option<crate::commands::engine::EngineTuning>,
+) -> Option<Arc<dyn EngineHost>> {
+ Some(Arc::new(super::local_api_engine::AppEngine {
+ app: app.clone(),
+ vorgaben: std::sync::RwLock::new(super::local_api_engine::EngineVorgaben { ordner, tuning }),
+ }))
+}
+
+/// Auf dem Telefon laeuft die Engine im App-Prozess; die API sieht dort, was
+/// sie meldet.
+#[cfg(mobile)]
+fn app_engine(
+ _app: &tauri::AppHandle,
+ _ordner: Vec<String>,
+ _tuning: Option<crate::commands::engine::EngineTuning>,
+) -> Option<Arc<dyn EngineHost>> {
+ None
+}
+
fn stop_local_api_inner(state: &crate::state::AppState) {
if let Ok(mut slot) = state.local_api.lock() {
if let Some(mut s) = slot.take() {
@@ -1677,7 +1795,7 @@ mod local_api_echt {
upstreams: default_upstreams(),
cors_origins: cors,
};
- let state = LocalApiState { cfg: Arc::new(RwLock::new(cfg)), http, nutzung: Arc::new(Nutzung::neu()), live: None };
+ let state = LocalApiState { cfg: Arc::new(RwLock::new(cfg)), http, nutzung: Arc::new(Nutzung::neu()), live: None, engine: None, lade_schloss: Default::default() };
let l = tokio::net::TcpListener::bind(bind_addr(false, 0)).await.ok()?;
let addr = l.local_addr().ok()?;
let router = build_local_api_router(state);
@@ -1861,7 +1979,7 @@ mod local_api_echt {
http: reqwest::Client::builder()
.connect_timeout(std::time::Duration::from_secs(5)).build().unwrap(),
nutzung: Arc::new(Nutzung::neu()),
- live: None,
+ live: None, engine: None, lade_schloss: Default::default(),
};
let l = tokio::net::TcpListener::bind(bind_addr(false, port)).await
.unwrap_or_else(|e| panic!("Port {} nicht frei: {}", port, e));
@@ -2173,7 +2291,7 @@ mod tests {
})),
http: reqwest::Client::new(),
nutzung: Arc::clone(&nutzung),
- live: None,
+ live: None, engine: None, lade_schloss: Default::default(),
};
let l = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let api = l.local_addr().unwrap();
@@ -2226,7 +2344,7 @@ mod tests {
})),
http: reqwest::Client::new(),
nutzung: Arc::new(Nutzung::neu()),
- live,
+ live, engine: None, lade_schloss: Default::default(),
};
let l = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let a = l.local_addr().unwrap();
@@ -2732,3 +2850,266 @@ mod tests {
assert_eq!(lu_werkzeuge()["callable_here"], false);
}
}
+
+/// C57 (3.1.0): die LU-Engine-Lane der lokalen API.
+///
+/// Seit 3.0.9 (llama.cpp b11960) ist `/v1/models` der Engine nur noch mit
+/// Schluessel offen, und die API fragte ohne: SillyTavern sah "no models". Die
+/// Attrappe hier verhaelt sich wie b11960: ohne den Engine-Schluessel 401, und
+/// das Modell heisst beim vollen Pfad. Wer den Schluessel beim Auflisten
+/// wieder weglaesst, macht den ersten Test rot.
+#[cfg(test)]
+mod c57_engine_lane {
+ use super::*;
+ use super::super::local_api_engine::{EngineHost, EngineModell, LadeFehler};
+ use std::sync::atomic::{AtomicUsize, Ordering};
+ use std::sync::Mutex as StdMutex;
+
+ const T: &str = "t0k";
+
+ fn hat_engine_schluessel(h: &HeaderMap) -> bool {
+ h.get(header::AUTHORIZATION).and_then(|v| v.to_str().ok()) == Some(super::super::engine_key::bearer().as_str())
+ }
+
+ /// Eine Engine wie llama-server b11960 mit `LLAMA_API_KEY`: `/health`
+ /// offen, alles andere nur mit Schluessel. `/v1/models` nennt `pfad`.
+ async fn engine_attrappe(pfad: &'static str) -> std::net::SocketAddr {
+ let r = Router::new()
+ .route("/health", get(|| async { Json(serde_json::json!({ "status": "ok" })) }))
+ .route("/slots", get(|h: HeaderMap| async move {
+ if !hat_engine_schluessel(&h) { return StatusCode::UNAUTHORIZED.into_response() }
+ Json(serde_json::json!([{ "id": 0, "id_task": 3, "is_processing": false }])).into_response()
+ }))
+ .route("/v1/models", get(move |h: HeaderMap| async move {
+ if !hat_engine_schluessel(&h) {
+ return (StatusCode::UNAUTHORIZED, Json(serde_json::json!({ "error": { "message": "Invalid API Key", "code": 401 } }))).into_response();
+ }
+ Json(serde_json::json!({ "object": "list", "data": [{ "id": pfad, "object": "model" }] })).into_response()
+ }))
+ .route("/v1/chat/completions", post(move |h: HeaderMap, Json(b): Json<serde_json::Value>| async move {
+ if !hat_engine_schluessel(&h) { return StatusCode::UNAUTHORIZED.into_response() }
+ let tools = b.get("tools").and_then(|t| t.as_array()).map(|a| a.len()).unwrap_or(0);
+ if b["stream"].as_bool() == Some(true) {
+ let sse = format!(
+ "data: {{\"model\":\"{p}\",\"choices\":[{{\"delta\":{{\"content\":\"hi\"}}}}]}}\n\ndata: {{\"model\":\"{p}\",\"choices\":[{{\"delta\":{{}},\"finish_reason\":\"stop\"}}]}}\n\ndata: [DONE]\n\n",
+ p = pfad
+ );
+ return ([(header::CONTENT_TYPE, "text/event-stream")], sse).into_response();
+ }
+ Json(serde_json::json!({
+ "model": pfad,
+ "choices": [{ "message": { "role": "assistant", "content": "hi" }, "finish_reason": "stop" }],
+ "tools_seen": tools,
+ "usage": { "prompt_tokens": 3, "completion_tokens": 1 },
+ })).into_response()
+ }));
+ let l = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let a = l.local_addr().unwrap();
+ tokio::spawn(async move { let _ = axum::serve(l, r).await; });
+ a
+ }
+
+ async fn api(upstreams: Vec<Upstream>, engine: Option<Arc<dyn EngineHost>>) -> std::net::SocketAddr {
+ let state = LocalApiState {
+ cfg: Arc::new(RwLock::new(LocalApiConfig { port: 0, lan: false, token: T.into(), upstreams, cors_origins: Vec::new() })),
+ http: reqwest::Client::new(),
+ nutzung: Arc::new(Nutzung::neu()),
+ live: None,
+ engine,
+ lade_schloss: Default::default(),
+ };
+ let l = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let a = l.local_addr().unwrap();
+ let router = build_local_api_router(state);
+ tokio::spawn(async move { let _ = axum::serve(l, router).await; });
+ a
+ }
+
+ async fn ids(api: std::net::SocketAddr) -> Vec<String> {
+ let v: serde_json::Value = reqwest::Client::new()
+ .get(format!("http://{}/v1/models", api)).bearer_auth(T)
+ .send().await.unwrap().json().await.unwrap();
+ v["data"].as_array().unwrap().iter().map(|m| m["id"].as_str().unwrap().to_string()).collect()
+ }
+
+ fn chat(api: std::net::SocketAddr, modell: &str, stream: bool) -> reqwest::RequestBuilder {
+ reqwest::Client::new()
+ .post(format!("http://{}/v1/chat/completions", api)).bearer_auth(T)
+ .json(&serde_json::json!({
+ "model": modell, "stream": stream,
+ "messages": [{ "role": "user", "content": "hi" }],
+ "tools": [{ "type": "function", "function": { "name": "f", "parameters": { "type": "object" } } }],
+ }))
+ }
+
+ /// Genau der Fehler aus Discussion #225: eine laufende Engine mit
+ /// Schluessel, und `/v1/models` der API listet sie nicht.
+ #[tokio::test]
+ async fn die_laufende_engine_steht_in_v1_models() {
+ let e = engine_attrappe("/Users/x/Library/Application Support/Locally Uncensored/models/Qwen2.5-0.5B-Instruct-Q8_0.gguf").await;
+ let a = api(vec![Upstream { lane_prefix: "lu".into(), base: format!("http://{}/v1", e) }], None).await;
+ assert_eq!(ids(a).await, vec!["lu/Qwen2.5-0.5B-Instruct-Q8_0".to_string()], "the engine lane must not be empty (C57)");
+
+ // Chat ueber den Namen aus der Liste, und ueber die Pfad-ID von 3.0.8.
+ let r = chat(a, "lu/Qwen2.5-0.5B-Instruct-Q8_0", false).send().await.unwrap();
+ assert_eq!(r.status(), 200);
+ let v: serde_json::Value = r.json().await.unwrap();
+ assert_eq!(v["model"], "lu/Qwen2.5-0.5B-Instruct-Q8_0");
+ assert_eq!(v["tools_seen"], 1, "tools reach the engine untouched");
+ let r = chat(a, "lu//Users/x/models/Qwen2.5-0.5B-Instruct-Q8_0.gguf", false).send().await.unwrap();
+ assert_eq!(r.status(), 200, "a 3.0.8 path id still works");
+ }
+
+ /// Eine Engine, die laden kann, was installiert ist, und mitzaehlt.
+ struct FalscheEngine {
+ installiert: Vec<EngineModell>,
+ geladen: StdMutex<Option<(String, u16)>>,
+ port: u16,
+ ladungen: AtomicUsize,
+ scheitert: bool,
+ }
+
+ impl EngineHost for FalscheEngine {
+ fn installiert(&self) -> Vec<EngineModell> { self.installiert.clone() }
+ fn geladen(&self) -> Option<(String, u16)> { self.geladen.lock().unwrap().clone() }
+ fn laden(&self, path: &str) -> Result<u16, String> {
+ self.ladungen.fetch_add(1, Ordering::SeqCst);
+ if self.scheitert { return Err("Model file not found: /m/b.gguf".into()) }
+ *self.geladen.lock().unwrap() = Some((path.to_string(), self.port));
+ Ok(self.port)
+ }
+ }
+
+ fn falsche(port: u16, geladen: Option<&str>, scheitert: bool) -> Arc<FalscheEngine> {
+ Arc::new(FalscheEngine {
+ installiert: vec![
+ EngineModell { name: "a".into(), path: "/m/a.gguf".into() },
+ EngineModell { name: "b".into(), path: "/m/b.gguf".into() },
+ EngineModell { name: "nomic-embed-text-v1.5".into(), path: "/m/nomic-embed-text-v1.5.gguf".into() },
+ ],
+ geladen: StdMutex::new(geladen.map(|p| (p.to_string(), port))),
+ port,
+ ladungen: AtomicUsize::new(0),
+ scheitert,
+ })
+ }
+
+ /// Wie Ollama: alles Installierte steht in der Liste, auch ohne laufende
+ /// Engine, und der erste Chat laedt das Modell.
+ #[tokio::test]
+ async fn installierte_modelle_stehen_in_der_liste_und_laden_beim_ersten_chat() {
+ let e = engine_attrappe("/m/b.gguf").await;
+ let f = falsche(e.port(), None, false);
+ let a = api(default_upstreams_ohne_fremde(), Some(f.clone() as Arc<dyn EngineHost>)).await;
+ assert_eq!(ids(a).await, vec!["lu/a".to_string(), "lu/b".to_string()], "installed chat models, no embedder");
+
+ let r = chat(a, "lu/b", false).send().await.unwrap();
+ assert_eq!(r.status(), 200);
+ assert_eq!(f.ladungen.load(Ordering::SeqCst), 1, "loaded on demand");
+ let v: serde_json::Value = r.json().await.unwrap();
+ assert_eq!(v["model"], "lu/b");
+
+ // Geladen und gesund: kein zweites Laden, auch im Stream.
+ let r = chat(a, "lu/b", true).send().await.unwrap();
+ assert_eq!(r.status(), 200);
+ let text = r.text().await.unwrap();
+ assert!(text.contains("\"model\":\"lu/b\"") && text.contains("[DONE]"), "{text}");
+ assert_eq!(f.ladungen.load(Ordering::SeqCst), 1);
+
+ // Ein anderes Modell: Wechsel.
+ let r = chat(a, "lu/a", false).send().await.unwrap();
+ assert_eq!(r.status(), 200);
+ assert_eq!(f.ladungen.load(Ordering::SeqCst), 2);
+
+ let h: serde_json::Value = reqwest::Client::new()
+ .get(format!("http://{}/lu/v1/health", a)).bearer_auth(T)
+ .send().await.unwrap().json().await.unwrap();
+ assert_eq!(h["lanes"]["lu"]["loaded"], "lu/a");
+ }
+
+ #[tokio::test]
+ async fn ein_scheiterndes_laden_ist_ein_klarer_fehler() {
+ let f = falsche(9, None, true);
+ let a = api(default_upstreams_ohne_fremde(), Some(f as Arc<dyn EngineHost>)).await;
+ let r = chat(a, "lu/b", false).send().await.unwrap();
+ assert_eq!(r.status(), 500);
+ let v: serde_json::Value = r.json().await.unwrap();
+ assert_eq!(v["error"]["code"], "model_load_failed");
+ let msg = v["error"]["message"].as_str().unwrap();
+ assert!(msg.contains("lu/b") && msg.contains("Model file not found"), "{msg}");
+ }
+
+ /// Ein Wechsel wartet, bis die Engine fertig geschrieben hat, und gibt
+ /// nach der Frist mit `engine_busy` auf, statt einer Antwort den Boden
+ /// wegzuziehen.
+ #[tokio::test]
+ async fn ein_wechsel_wartet_auf_eine_beschaeftigte_engine() {
+ let r = Router::new().route("/slots", get(|| async {
+ Json(serde_json::json!([{ "id": 0, "id_task": 9, "is_processing": true }]))
+ }));
+ let l = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let port = l.local_addr().unwrap().port();
+ tokio::spawn(async move { let _ = axum::serve(l, r).await; });
+ let f = falsche(port, Some("/m/a.gguf"), false);
+ let ziel = EngineModell { name: "b".into(), path: "/m/b.gguf".into() };
+ let e = super::super::local_api_engine::bereitstellen(
+ f.clone(), &Default::default(), &reqwest::Client::new(), &ziel,
+ std::time::Duration::from_millis(1200), std::time::Duration::from_secs(1),
+ ).await;
+ assert!(matches!(e, Err(LadeFehler::Belegt { .. })), "{e:?}");
+ assert_eq!(f.ladungen.load(Ordering::SeqCst), 0, "the busy engine was left alone");
+ }
+
+ /// Ohne Ollama und LM Studio, damit der Test kein echtes Programm auf
+ /// 11434/1234 fragt.
+ fn default_upstreams_ohne_fremde() -> Vec<Upstream> {
+ vec![Upstream { lane_prefix: "lu".into(), base: "http://127.0.0.1:9/v1".into() }]
+ }
+
+ #[test]
+ fn die_401_sagt_wo_der_schluessel_steht() {
+ let kein = schluessel_fehlertext("abc", "");
+ assert!(kein.contains("No API key was sent") && kein.contains("Settings → Local API"));
+ let falsch = schluessel_fehlertext("abc", "xyz");
+ assert!(falsch.contains("Wrong API key") && falsch.contains("Settings → Local API"));
+ assert!(schluessel_fehlertext("", "").contains("Generate"));
+ }
+
+ /// Gegen die echte LU Engine (llama-server aus `bin/`, b11960) mit einem
+ /// echten GGUF: `LU_C57_GGUF=/pfad/modell.gguf cargo test c57 -- --nocapture`.
+ /// Ohne die Variable uebersprungen.
+ #[tokio::test]
+ async fn echt_gegen_die_lu_engine() {
+ let Ok(gguf) = std::env::var("LU_C57_GGUF") else { return };
+ let bin = concat!(env!("CARGO_MANIFEST_DIR"), "/bin/lu-llama-server-aarch64-apple-darwin");
+ let port = 18931u16;
+ let mut kind = std::process::Command::new(bin)
+ .args(["-m", &gguf, "--port", &port.to_string(), "-c", "2048"])
+ .env(super::super::engine_key::KEY_ENV, super::super::engine_key::engine_key())
+ .stdout(std::process::Stdio::null()).stderr(std::process::Stdio::null())
+ .spawn().expect("llama-server starts");
+ let http = reqwest::Client::new();
+ for _ in 0..120 {
+ if http.get(format!("http://127.0.0.1:{}/health", port)).send().await.map(|r| r.status() == 200).unwrap_or(false) { break }
+ tokio::time::sleep(std::time::Duration::from_millis(500)).await;
+ }
+ let ohne = http.get(format!("http://127.0.0.1:{}/v1/models", port)).send().await.unwrap().status();
+ eprintln!("[c57] engine /v1/models without key: {ohne}");
+ let a = api(vec![Upstream { lane_prefix: "lu".into(), base: format!("http://127.0.0.1:{}/v1", port) }], None).await;
+ let liste = ids(a).await;
+ let name = format!("lu/{}", name_aus_pfad(&gguf));
+ let r = chat(a, &name, false).send().await.unwrap();
+ let status = r.status();
+ let v: serde_json::Value = r.json().await.unwrap_or_default();
+ let s = chat(a, &name, true).send().await.unwrap();
+ let s_status = s.status();
+ let s_text = s.text().await.unwrap_or_default();
+ let _ = kind.kill();
+ let _ = kind.wait();
+ assert_eq!(liste, vec![name.clone()]);
+ assert_eq!(status, 200, "{v}");
+ assert_eq!(v["model"], name.as_str());
+ assert_eq!(s_status, 200);
+ assert!(s_text.contains("[DONE]"), "{s_text}");
+ }
+}
diff --git a/src-tauri/src/commands/local_api_engine.rs b/src-tauri/src/commands/local_api_engine.rs
new file mode 100644
index 00000000..178643bf
--- /dev/null
+++ b/src-tauri/src/commands/local_api_engine.rs
@@ -0,0 +1,327 @@
+//! The LU Engine lane of the local API: which models it offers, and loading
+//! one when a client asks for it.
+//!
+//! ── WHY THIS FILE EXISTS (C57, 3.1.0) ──────────────────────────────────────
+//!
+//! From 3.0.9 on, `GET /v1/models` of the local API listed Ollama and LM Studio
+//! but no LU Engine model (GitHub Discussion #225, Discord: SillyTavern shows
+//! "no model loaded"). The cause: 3.0.9 moved the engine to llama.cpp b11960,
+//! and b11960 took `/models` and `/v1/models` off its list of addresses that
+//! answer without a key (b9949 `server-http.cpp` had them public, b11960 keeps
+//! only `/health` and `/v1/health`). LU starts every engine with a key
+//! (engine_key.rs, since 3.0.7). The chat forwarding put that key on its
+//! requests, the model listing did not, got a 401, and read it as "no models".
+//!
+//! And even before that, the lane only knew what a running llama-server says
+//! about itself: one model, the loaded one, or nothing when the engine was
+//! stopped (idle unload, nothing chosen yet). Ollama lists everything that is
+//! installed and loads on the first request. That is the yardstick here:
+//!
+//! * `/v1/models` lists every installed chat GGUF of the LU Engine (the same
+//! scan the Models page shows) plus the loaded one, under `lu/<name>`.
+//! * A chat or completion for a model that is not loaded loads it first.
+//! The request waits for the load, like Ollama's first request. A load
+//! that fails ends in one clear error (`model_load_failed`), never in a
+//! dead port or a crash.
+//! * A model swap waits until the engine has finished what it is doing
+//! (`/slots`), so neither an API client nor the app's own chat loses an
+//! answer half way through. Ollama with one model slot behaves the same.
+
+use std::sync::Arc;
+use std::time::Duration;
+
+/// One installed LU Engine model.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct EngineModell {
+ /// The file name without `.gguf` (first part of a split set: the base).
+ /// The same name the Models page shows; the API offers it as `lu/<name>`.
+ pub name: String,
+ pub path: String,
+}
+
+/// What the local API needs from the app to serve the LU Engine lane.
+///
+/// A trait so the tests can stand in a fake engine; the app's implementation
+/// is `AppEngine` below.
+pub trait EngineHost: Send + Sync {
+ /// Every installed chat model. Blocking (walks folders).
+ fn installiert(&self) -> Vec<EngineModell>;
+ /// The chat model the engine holds right now: (path, port).
+ fn geladen(&self) -> Option<(String, u16)>;
+ /// Load `path` (stop what runs, start this). Blocking, can take a while.
+ /// `Ok(port)` when the engine is up and answers.
+ fn laden(&self, path: &str) -> Result<u16, String>;
+}
+
+/// Embedding GGUFs are no chat model. Same patterns as the chat picker's
+/// filter (`isEmbeddingGgufName` in src/api/engine.ts).
+pub fn ist_embedding_name(name: &str) -> bool {
+ let n = name.to_lowercase();
+ ["embed", "nomic-embed", "bge-", "e5-", "gte-", "sentence-"].iter().any(|p| n.contains(p))
+}
+
+/// The name an LU Engine model has in the API, from its path.
+pub fn name_aus_pfad(path: &str) -> String {
+ crate::commands::proxy::builtin_model_name_from_path(path)
+}
+
+/// The engine lane's catalogue: every installed chat model, the loaded one
+/// included even when it lies outside the scanned folders. Sorted by name,
+/// each name once.
+pub fn engine_katalog(installiert: &[EngineModell], geladen: Option<&str>) -> Vec<EngineModell> {
+ let mut raus: Vec<EngineModell> = installiert.iter().filter(|m| !ist_embedding_name(&m.name)).cloned().collect();
+ if let Some(p) = geladen {
+ let name = name_aus_pfad(p);
+ if !name.is_empty() && !raus.iter().any(|m| m.name == name) {
+ raus.push(EngineModell { name, path: p.to_string() });
+ }
+ }
+ raus.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()).then(a.name.cmp(&b.name)));
+ raus.dedup_by(|a, b| a.name == b.name);
+ raus
+}
+
+/// 3.0.8 offered the loaded model under the id llama-server gives it: the full
+/// path (`lu//Users/x/.../Qwen.gguf`). A client that saved that id keeps
+/// working: a path after `lu/` is read as the model it names.
+pub fn alte_engine_id_lesen(requested: &str) -> Option<String> {
+ let rest = requested.trim().strip_prefix("lu/")?;
+ let sieht_aus_wie_pfad = rest.contains('/') || rest.contains('\\') || rest.to_ascii_lowercase().ends_with(".gguf");
+ if !sieht_aus_wie_pfad {
+ return None;
+ }
+ let name = name_aus_pfad(rest);
+ (!name.is_empty()).then(|| format!("lu/{}", name))
+}
+
+/// Why a model could not be made ready.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum LadeFehler {
+ /// The engine stayed busy with another model's answer for too long.
+ Belegt { name: String, sekunden: u64 },
+ /// The start itself failed; the engine's own words.
+ Start { name: String, grund: String },
+}
+
+impl LadeFehler {
+ pub fn message(&self) -> String {
+ match self {
+ LadeFehler::Belegt { name, sekunden } => format!(
+ "The LU Engine is still answering with another model, so 'lu/{}' could not be loaded within {} s. Send the request again when that answer is done.",
+ name, sekunden
+ ),
+ LadeFehler::Start { name, grund } => format!(
+ "The LU Engine could not load 'lu/{}': {}. Open LU → Models to load it by hand and see the details.",
+ name,
+ grund.trim().trim_end_matches('.')
+ ),
+ }
+ }
+
+ pub fn code(&self) -> &'static str {
+ match self {
+ LadeFehler::Belegt { .. } => "engine_busy",
+ LadeFehler::Start { .. } => "model_load_failed",
+ }
+ }
+}
+
+/// How long a swap waits for the engine to finish the answer it is writing.
+pub const WARTEN_AUF_FREI: Duration = Duration::from_secs(300);
+/// How long an engine that is still loading the right model is waited for.
+pub const WARTEN_AUF_BEREIT: Duration = Duration::from_secs(180);
+
+/// One request at a time decides about loading. Two clients that ask for two
+/// models at once would otherwise stop each other's start.
+pub type LadeSchloss = Arc<tokio::sync::Mutex<()>>;
+
+/// The state of the loaded engine, asked over HTTP.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum Zustand {
+ Bereit,
+ LaedtNoch,
+ Weg,
+}
+
+async fn zustand(http: &reqwest::Client, port: u16) -> Zustand {
+ // `/health` answers without a key in every llama.cpp LU ships.
+ match http
+ .get(format!("http://127.0.0.1:{}/health", port))
+ .timeout(Duration::from_secs(3))
+ .send()
+ .await
+ {
+ Ok(r) if r.status().is_success() => Zustand::Bereit,
+ Ok(r) if r.status() == reqwest::StatusCode::SERVICE_UNAVAILABLE => Zustand::LaedtNoch,
+ Ok(_) => Zustand::Bereit,
+ Err(_) => Zustand::Weg,
+ }
+}
+
+/// Is a slot of the engine on `port` writing an answer? `/slots` wants the
+/// engine key. An engine that does not say counts as free: a swap must not
+/// hang on an engine that is gone.
+async fn beschaeftigt(http: &reqwest::Client, port: u16) -> bool {
+ let url = format!("http://127.0.0.1:{}/slots", port);
+ let r = http
+ .get(&url)
+ .header(reqwest::header::AUTHORIZATION, super::engine_key::bearer())
+ .timeout(Duration::from_secs(3))
+ .send()
+ .await;
+ let Ok(r) = r else { return false };
+ let Ok(v) = r.json::<serde_json::Value>().await else { return false };
+ super::engine_idle::slot_activity(&v).map(|a| a.busy).unwrap_or(false)
+}
+
+/// Makes `ziel` the model the engine holds, and gives back the port it answers
+/// on. Loads only when needed; the fast path (right model, healthy) asks the
+/// engine once.
+pub async fn bereitstellen(
+ host: Arc<dyn EngineHost>,
+ schloss: &LadeSchloss,
+ http: &reqwest::Client,
+ ziel: &EngineModell,
+ warten_frei: Duration,
+ warten_bereit: Duration,
+) -> Result<u16, LadeFehler> {
+ let _g = schloss.lock().await;
+
+ if let Some((pfad, port)) = host.geladen() {
+ if pfad == ziel.path || name_aus_pfad(&pfad) == ziel.name {
+ let bis = tokio::time::Instant::now() + warten_bereit;
+ loop {
+ match zustand(http, port).await {
+ Zustand::Bereit => return Ok(port),
+ Zustand::Weg => break, // dead process: start it below
+ Zustand::LaedtNoch if tokio::time::Instant::now() < bis => {
+ tokio::time::sleep(Duration::from_millis(500)).await
+ }
+ Zustand::LaedtNoch => break,
+ }
+ }
+ } else {
+ // Another model is loaded. Let it finish what it is writing.
+ let bis = tokio::time::Instant::now() + warten_frei;
+ while beschaeftigt(http, port).await {
+ if tokio::time::Instant::now() >= bis {
+ return Err(LadeFehler::Belegt { name: ziel.name.clone(), sekunden: warten_frei.as_secs() });
+ }
+ tokio::time::sleep(Duration::from_millis(500)).await;
+ }
+ }
+ }
+
+ let pfad = ziel.path.clone();
+ let h = Arc::clone(&host);
+ match tokio::task::spawn_blocking(move || h.laden(&pfad)).await {
+ Ok(Ok(port)) => Ok(port),
+ Ok(Err(grund)) => Err(LadeFehler::Start { name: ziel.name.clone(), grund }),
+ Err(e) => Err(LadeFehler::Start { name: ziel.name.clone(), grund: format!("the start task failed: {e}") }),
+ }
+}
+
+/// What the local API knows about how the app starts its engine: the folders
+/// named under Storage, and the user's engine tuning. Handed over by the
+/// frontend when it starts the API.
+#[derive(Debug, Clone, Default)]
+pub struct EngineVorgaben {
+ pub ordner: Vec<String>,
+ pub tuning: Option<crate::commands::engine::EngineTuning>,
+}
+
+/// The app's engine.
+#[cfg(desktop)]
+pub struct AppEngine {
+ pub app: tauri::AppHandle,
+ pub vorgaben: std::sync::RwLock<EngineVorgaben>,
+}
+
+#[cfg(desktop)]
+impl EngineHost for AppEngine {
+ fn installiert(&self) -> Vec<EngineModell> {
+ use crate::commands::engine::{bundled_scan_dirs, builtin_models_dir, scan_gguf_roots, ScanRoot, MAX_CUSTOM_SCAN_DEPTH, MAX_SCAN_DEPTH};
+ let Ok(dir) = builtin_models_dir() else { return Vec::new() };
+ let ordner = self.vorgaben.read().map(|v| v.ordner.clone()).unwrap_or_default();
+ let dirs = bundled_scan_dirs(&dir, &ordner);
+ let roots: Vec<ScanRoot> = dirs
+ .iter()
+ .enumerate()
+ .map(|(i, d)| ScanRoot { dir: d.as_path(), max_depth: if i == 0 { MAX_SCAN_DEPTH } else { MAX_CUSTOM_SCAN_DEPTH } })
+ .collect();
+ scan_gguf_roots(&roots)
+ .models
+ .into_iter()
+ .map(|m| EngineModell { name: m.name, path: m.path })
+ .collect()
+ }
+
+ fn geladen(&self) -> Option<(String, u16)> {
+ use tauri::Manager;
+ let st = self.app.state::<crate::state::AppState>();
+ let g = st.bundled_engine.lock().ok()?;
+ g.as_ref().map(|e| (e.model_path.clone(), e.port))
+ }
+
+ fn laden(&self, path: &str) -> Result<u16, String> {
+ use tauri::Manager;
+ let tuning = self.vorgaben.read().ok().and_then(|v| v.tuning.clone());
+ let st = self.app.state::<crate::state::AppState>();
+ let antwort = crate::commands::engine::start_bundled_engine_blocking(&self.app, &st, path.to_string(), tuning, None)?;
+ let port = antwort
+ .get("port")
+ .and_then(|p| p.as_u64())
+ .and_then(|p| u16::try_from(p).ok())
+ .or_else(|| self.geladen().map(|(_, p)| p))
+ .ok_or_else(|| "the engine started but did not say on which port".to_string())?;
+ // The app's own chat needs no word: its pre-send check
+ // (api/builtin-ensure.ts) asks the engine what it holds and swaps back
+ // when the user's chat wants another model.
+ Ok(port)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn m(name: &str, path: &str) -> EngineModell {
+ EngineModell { name: name.into(), path: path.into() }
+ }
+
+ #[test]
+ fn der_katalog_fuehrt_installierte_und_geladene_modelle_ohne_embedder() {
+ let inst = vec![
+ m("qwen3-8b", "/m/qwen3-8b.gguf"),
+ m("nomic-embed-text-v1.5.Q4_K_M", "/m/nomic-embed-text-v1.5.Q4_K_M.gguf"),
+ m("Gemma-3-4b", "/m/Gemma-3-4b.gguf"),
+ ];
+ let k = engine_katalog(&inst, Some("/elsewhere/Llama-3.2-1B-Q4_K_M.gguf"));
+ let namen: Vec<&str> = k.iter().map(|m| m.name.as_str()).collect();
+ assert_eq!(namen, vec!["Gemma-3-4b", "Llama-3.2-1B-Q4_K_M", "qwen3-8b"]);
+ // The loaded one is not listed twice when it is also installed.
+ let k = engine_katalog(&inst, Some("/m/qwen3-8b.gguf"));
+ assert_eq!(k.iter().filter(|m| m.name == "qwen3-8b").count(), 1);
+ // A split set is named after its base.
+ let k = engine_katalog(&[], Some("/m/big-00001-of-00003.gguf"));
+ assert_eq!(k[0].name, "big");
+ }
+
+ #[test]
+ fn eine_alte_id_mit_pfad_findet_ihr_modell() {
+ assert_eq!(alte_engine_id_lesen("lu//Users/x/models/Qwen2.5-0.5B-Instruct-Q8_0.gguf").as_deref(), Some("lu/Qwen2.5-0.5B-Instruct-Q8_0"));
+ assert_eq!(alte_engine_id_lesen("lu/C:\\AI\\m\\a.gguf").as_deref(), Some("lu/a"));
+ assert_eq!(alte_engine_id_lesen("lu/qwen3-8b"), None);
+ assert_eq!(alte_engine_id_lesen("ollama/huihui_ai/qwen3:4b"), None);
+ }
+
+ #[test]
+ fn die_fehler_sagen_was_zu_tun_ist() {
+ let e = LadeFehler::Start { name: "q".into(), grund: "Model file not found: /x.".into() };
+ assert_eq!(e.code(), "model_load_failed");
+ assert!(e.message().contains("'lu/q'") && e.message().contains("Model file not found: /x") && e.message().contains("Models"));
+ let b = LadeFehler::Belegt { name: "q".into(), sekunden: 300 };
+ assert_eq!(b.code(), "engine_busy");
+ assert!(b.message().contains("300 s"));
+ }
+}
diff --git a/src-tauri/src/commands/mod.rs b/src-tauri/src/commands/mod.rs
index 5e118fba..d0d31ca7 100644
--- a/src-tauri/src/commands/mod.rs
+++ b/src-tauri/src/commands/mod.rs
@@ -55,6 +55,7 @@ pub mod install_method_cmd;
pub mod self_migrate;
pub mod self_migrate_cmd;
pub mod local_api;
+pub mod local_api_engine;
pub mod logging;
pub mod media_cmds;
pub mod mlx;
diff --git a/src/components/settings/LocalApiSettings.tsx b/src/components/settings/LocalApiSettings.tsx
index 9824f8e7..8c65d1d6 100644
--- a/src/components/settings/LocalApiSettings.tsx
+++ b/src/components/settings/LocalApiSettings.tsx
@@ -1,9 +1,9 @@
import { useEffect, useState } from 'react'
-import { Copy, Check, KeyRound, Eye, EyeOff, Globe } from 'lucide-react'
+import { Copy, Check, KeyRound, Eye, EyeOff, Globe, ChevronDown, ChevronRight } from 'lucide-react'
import { useLocalApiStore } from '../../stores/localApiStore'
import {
localApiBaseUrl, curlBeispiel, clientFelder, reichweiteText, kannStarten, pruefePort,
- corsText, nutzungZeilen, ohneZahlenText, type LocalApiNutzung,
+ corsText, nutzungZeilen, ohneZahlenText, clientAnleitung, type LocalApiNutzung,
} from '../../lib/local-api'
import { backendCall } from '../../api/backend'
import { InlineToggle } from './InlineToggle'
@@ -28,6 +28,7 @@ export function LocalApiSettings() {
const { port, lan, token, corsOrigins, mitLuStarten, laeuft, fehler, setPort, setLan, setCors, setMitLuStarten, neuesToken, starten, stoppen, auffrischen } = useLocalApiStore()
const [zeigeToken, setZeigeToken] = useState(false)
const [kopiert, setKopiert] = useState<string | null>(null)
+ const [anleitungOffen, setAnleitungOffen] = useState(false)
// Der Rohtext bleibt lokal: gespeichert wird nur, was die Pruefung
// uebersteht. Sonst stuende in der Datei eine Freigabe, die nie greift.
const [corsRoh, setCorsRoh] = useState(corsOrigins.join(', '))
@@ -71,7 +72,8 @@ export function LocalApiSettings() {
<p className="t-micro text-gray-600">
One OpenAI-compatible address for every model on this machine: LU Engine, Ollama
and LM Studio under a single URL. Anything that can talk to OpenAI can talk to your
- local models through it.
+ local models through it. It always needs the API key LU generates below, also on
+ this machine.
</p>
<div className="flex items-center justify-between">
@@ -175,7 +177,7 @@ export function LocalApiSettings() {
<div className="space-y-1">
<div className="flex items-center gap-2">
<KeyRound className="w-3 h-3 text-gray-500" />
- <span className="t-micro text-gray-500">Token. Every request needs it, including from this machine.</span>
+ <span className="t-micro text-gray-500">API key. LU makes it, you do not choose one. Every request needs it, also from this machine: paste it into your app's API key field.</span>
</div>
<div className="flex items-center gap-2">
<input
@@ -218,6 +220,34 @@ export function LocalApiSettings() {
</button>
</div>
)}
+
+ {/* GH Discussion #14 und C57: wer SillyTavern oder eine andere App
+ anschliesst, soll hier lesen koennen, welches Feld welches ist. */}
+ <div className="pt-1">
+ <button
+ onClick={() => setAnleitungOffen((v) => !v)}
+ aria-expanded={anleitungOffen}
+ data-testid="local-api-howto"
+ className="flex items-center gap-1 t-micro text-gray-400 hover:text-gray-200"
+ >
+ {anleitungOffen ? <ChevronDown className="w-3 h-3" /> : <ChevronRight className="w-3 h-3" />}
+ Use with SillyTavern / other apps
+ </button>
+ {anleitungOffen && (
+ <div className="mt-2 space-y-2">
+ {clientAnleitung(base).map((a) => (
+ <div key={a.titel}>
+ <div className="t-micro text-gray-400">{a.titel}</div>
+ <ol className="list-decimal pl-4 space-y-0.5">
+ {a.schritte.map((s) => (
+ <li key={s} className="t-micro text-gray-500 break-words">{s}</li>
+ ))}
+ </ol>
+ </div>
+ ))}
+ </div>
+ )}
+ </div>
</div>
)
}
diff --git a/src/lib/__tests__/local-api-adresse-und-port.test.ts b/src/lib/__tests__/local-api-adresse-und-port.test.ts
index 864aa556..6ca27ebe 100644
--- a/src/lib/__tests__/local-api-adresse-und-port.test.ts
+++ b/src/lib/__tests__/local-api-adresse-und-port.test.ts
@@ -82,7 +82,7 @@ describe('Der Start', () => {
// ausgraut, statt in einen Fehler zu laufen.
const u = kannStarten(' ', LOCAL_API_DEFAULT_PORT)
expect(u.ok).toBe(false)
- expect(u.ok === false && u.grund).toContain('token')
+ expect(u.ok === false && u.grund).toContain('API key')
})
it('mit Token und freiem Port ja', () => {
diff --git a/src/lib/__tests__/local-api-anleitung.test.ts b/src/lib/__tests__/local-api-anleitung.test.ts
new file mode 100644
index 00000000..e8cb2927
--- /dev/null
+++ b/src/lib/__tests__/local-api-anleitung.test.ts
@@ -0,0 +1,50 @@
+/**
+ * GH Discussion #14 ("API key not working even though I haven't set one") und
+ * C57 (SillyTavern: "no model loaded"): die Anleitung in der Einstellung nennt
+ * die Felder, wie SillyTavern sie nennt, sagt, dass der Schluessel immer
+ * gebraucht wird, und dass LU-Engine-Modelle beim ersten Aufruf laden.
+ */
+import { describe, it, expect } from 'vitest'
+import { readFileSync } from 'node:fs'
+import { resolve, dirname } from 'node:path'
+import { fileURLToPath } from 'node:url'
+import { clientAnleitung, kannStarten } from '../local-api'
+
+const here = dirname(fileURLToPath(import.meta.url))
+
+describe('Use with SillyTavern / other apps', () => {
+ const a = clientAnleitung('http://127.0.0.1:8129/v1')
+ const text = a.flatMap((x) => [x.titel, ...x.schritte]).join('\n')
+
+ it('nennt SillyTaverns Felder und die echte Adresse', () => {
+ expect(a[0].titel).toBe('SillyTavern')
+ expect(text).toContain('Custom (OpenAI-compatible)')
+ expect(text).toContain('Custom Endpoint (Base URL): http://127.0.0.1:8129/v1')
+ expect(text).toContain('Custom API Key')
+ })
+
+ it('sagt, dass der Schluessel Pflicht ist und LU-Engine-Modelle beim ersten Aufruf laden', () => {
+ expect(text).toMatch(/key is required/i)
+ expect(text).toMatch(/lu\//)
+ expect(text).toMatch(/loaded by the first request/)
+ })
+
+ it('der Start ohne Schluessel nennt ihn API key, wie jede App ihn nennt', () => {
+ const u = kannStarten('', 8129)
+ expect(u.ok).toBe(false)
+ if (!u.ok) expect(u.grund).toMatch(/API key/)
+ })
+
+ it('die Einstellung zeigt die Anleitung und nennt das Feld API key', () => {
+ const tsx = readFileSync(resolve(here, '../../components/settings/LocalApiSettings.tsx'), 'utf8')
+ expect(tsx).toContain('Use with SillyTavern / other apps')
+ expect(tsx).toContain('clientAnleitung(base)')
+ expect(tsx).toContain("paste it into your app's API key field")
+ })
+
+ it('der Store gibt der API Engine-Ordner und Abstimmung mit (C57, Laden bei Bedarf)', () => {
+ const store = readFileSync(resolve(here, '../../stores/localApiStore.ts'), 'utf8')
+ expect(store).toMatch(/engineDirs: customModelDirs\(\)/)
+ expect(store).toMatch(/engineTuning:/)
+ })
+})
diff --git a/src/lib/local-api.ts b/src/lib/local-api.ts
index 79eb98df..bd4591fa 100644
--- a/src/lib/local-api.ts
+++ b/src/lib/local-api.ts
@@ -95,6 +95,43 @@ export function clientFelder(base: string, token: string): Array<{ feld: string;
]
}
+/**
+ * Die kurze Anleitung "Use with SillyTavern / other apps" in der Einstellung.
+ *
+ * GitHub Discussion #14 (micnorian14): "API key not working even though I
+ * haven't set one". Niemand setzt hier einen Schluessel selbst: LU erzeugt
+ * ihn, und jede Anfrage braucht ihn. Discord: SillyTavern zeigte "no model
+ * loaded", weil die Liste leer war (C57). Die Schritte nennen deshalb die
+ * Felder so, wie SillyTavern sie nennt, und sagen, dass LU-Engine-Modelle beim
+ * ersten Aufruf geladen werden.
+ */
+export interface AnleitungsAbschnitt {
+ titel: string
+ schritte: string[]
+}
+
+export function clientAnleitung(base: string): AnleitungsAbschnitt[] {
+ return [
+ {
+ titel: 'SillyTavern',
+ schritte: [
+ 'API Connections: API "Chat Completion", source "Custom (OpenAI-compatible)".',
+ `Custom Endpoint (Base URL): ${base}`,
+ 'Custom API Key: the API Key above (copy button).',
+ 'Press Connect, then pick a model. lu/… are LU Engine models, ollama/… and lmstudio/… the other apps.',
+ ],
+ },
+ {
+ titel: 'Any other OpenAI-compatible app',
+ schritte: [
+ 'Set the Base URL and the API Key from above. The key is required, also on this machine.',
+ 'Model: an id from GET /v1/models, for example lu/<model name>.',
+ 'An LU Engine model that is not loaded is loaded by the first request, so the first answer takes longer.',
+ ],
+ },
+ ]
+}
+
/**
* Was die Oberflaeche ueber die Reichweite sagt.
*
@@ -110,7 +147,7 @@ export function reichweiteText(lan: boolean): string {
/** Ob der aktuelle Stand ueberhaupt startbar ist. */
export function kannStarten(token: string, port: number): PortUrteil {
- if (!token.trim()) return { ok: false, grund: 'The API will not start without a token. Generate one first.' }
+ if (!token.trim()) return { ok: false, grund: 'The API will not start without an API key. Press Generate below first.' }
return pruefePort(port)
}
diff --git a/src/stores/__tests__/local-api-start-with-lu.test.ts b/src/stores/__tests__/local-api-start-with-lu.test.ts
index bfdead4c..86c8dee9 100644
--- a/src/stores/__tests__/local-api-start-with-lu.test.ts
+++ b/src/stores/__tests__/local-api-start-with-lu.test.ts
@@ -51,7 +51,9 @@ describe('startLocalApiAtLaunch', () => {
useLocalApiStore.setState({ mitLuStarten: true })
await startLocalApiAtLaunch()
const start = calls.find((c) => c.cmd === 'start_local_api')
- expect(start?.args).toEqual({ port: 8129, lan: true, token: TOKEN, corsOrigins: [] })
+ expect(start?.args).toMatchObject({ port: 8129, lan: true, token: TOKEN, corsOrigins: [], engineDirs: [] })
+ // C57: the API loads LU Engine models itself, with the tuning the chat uses.
+ expect(start?.args).toHaveProperty('engineTuning')
expect(useLocalApiStore.getState().laeuft).toBe(true)
})
diff --git a/src/stores/localApiStore.ts b/src/stores/localApiStore.ts
index 8bcd07d7..7243e7d3 100644
--- a/src/stores/localApiStore.ts
+++ b/src/stores/localApiStore.ts
@@ -24,6 +24,8 @@ import { create } from 'zustand'
import { persist } from 'zustand/middleware'
import { safeJSONStorage } from '../lib/storage-quota'
import { backendCall } from '../api/backend'
+import { customModelDirs } from '../lib/custom-model-dirs'
+import { useSettingsStore } from './settingsStore'
import { LOCAL_API_DEFAULT_PORT, kannStarten, parseCorsOrigins, sollBeimStartStarten } from '../lib/local-api'
interface LocalApiState {
@@ -85,8 +87,13 @@ export const useLocalApiStore = create<LocalApiState>()(
const urteil = kannStarten(token, port)
if (!urteil.ok) { set({ fehler: urteil.grund }); return }
try {
+ // C57: die API laedt LU-Engine-Modelle bei Bedarf selbst. Dafuer
+ // braucht sie, was auch die Vorpruefung des Chats nimmt: die Ordner
+ // unter Storage und die Engine-Abstimmung aus den Einstellungen.
const r = await backendCall<{ address?: string }>('start_local_api', {
port, lan, token, corsOrigins,
+ engineDirs: customModelDirs(),
+ engineTuning: useSettingsStore.getState().settings.builtinEngine ?? null,
})
set({ laeuft: true, adresse: r?.address ?? null, fehler: null })
} catch (e) {