From 98673a7fc742f30545fe71af099bfa394c8a4026 Mon Sep 17 00:00:00 2001 From: wlkns-dev Date: Fri, 20 Nov 2026 19:59:54 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20Freigeben=20hielt=20die=20Datenbanksperr?= =?UTF-8?q?e=20und=20legte=20den=20Dienst=20lahm;=20Stimmen-Seite=20als=20?= =?UTF-8?q?Tabelle=20mit=20passendem=20Modell;=20Probeh=C3=B6ren=20mit=20g?= =?UTF-8?q?emeinsamem=20Ton-Ausgang;=20Version=200.1.15-rc12?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01GmHhg6k5Hxh8GpVW7od4Di --- Cargo.lock | 12 ++-- Cargo.toml | 2 +- crates/nexus-api/assets/stimmen.js | 36 ++++++----- crates/nexus-api/src/ui/stimmen.rs | 96 ++++++++++++++++++++++++------ crates/nexus-api/tests/api.rs | 20 +++++++ 5 files changed, 128 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 445348c..cc1fd2c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -700,7 +700,7 @@ dependencies = [ [[package]] name = "nexus" -version = "0.1.15-rc10" +version = "0.1.15-rc12" dependencies = [ "axum", "nexus-adapter", @@ -715,7 +715,7 @@ dependencies = [ [[package]] name = "nexus-adapter" -version = "0.1.15-rc10" +version = "0.1.15-rc12" dependencies = [ "async-trait", "libc", @@ -730,7 +730,7 @@ dependencies = [ [[package]] name = "nexus-api" -version = "0.1.15-rc10" +version = "0.1.15-rc12" dependencies = [ "async-trait", "axum", @@ -750,7 +750,7 @@ dependencies = [ [[package]] name = "nexus-core" -version = "0.1.15-rc10" +version = "0.1.15-rc12" dependencies = [ "argon2", "serde", @@ -761,14 +761,14 @@ dependencies = [ [[package]] name = "nexus-scheduler" -version = "0.1.15-rc10" +version = "0.1.15-rc12" dependencies = [ "nexus-core", ] [[package]] name = "nexus-store" -version = "0.1.15-rc10" +version = "0.1.15-rc12" dependencies = [ "nexus-core", "rusqlite", diff --git a/Cargo.toml b/Cargo.toml index cb67611..9bb4d17 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ resolver = "3" members = ["crates/nexus-core", "crates/nexus-scheduler", "crates/nexus-store", "crates/nexus-adapter", "crates/nexus-api", "crates/nexus"] [workspace.package] -version = "0.1.15-rc11" +version = "0.1.15-rc12" edition = "2024" license = "MIT" repository = "ssh://git@git.wlkns.org:2222/wlkns-dev/nexus.git" diff --git a/crates/nexus-api/assets/stimmen.js b/crates/nexus-api/assets/stimmen.js index 654e918..6e8204d 100644 --- a/crates/nexus-api/assets/stimmen.js +++ b/crates/nexus-api/assets/stimmen.js @@ -2,27 +2,35 @@ (() => { const status = document.getElementById('probe-status'); const csrf = document.querySelector('input[name=csrf]')?.value; + // Ein einziger Ton-Ausgang für alle Proben, gleich beim ersten Klick geweckt (sonst geht der Anfang verloren + // oder die zweite Probe bleibt stumm). Vor dem Ton liegt eine kurze Stille. + let ctx = null; + const ausgang = () => { + if (!ctx || ctx.state === 'closed') ctx = new AudioContext({sampleRate: 24000}); + if (ctx.state !== 'running') ctx.resume(); + return ctx; + }; document.querySelectorAll('button.probe').forEach(button => button.addEventListener('click', async () => { - const select = button.parentElement.querySelector('.stimmen-modell'); - const saved = button.dataset.gespeichert === '1'; - const matches = o => o.dataset.art === (saved ? 'gespeichert' : 'eingebaut') || o.dataset.art === 'alle'; - const model = matches(select.selectedOptions[0]) ? select.selectedOptions[0] : Array.from(select.options).find(matches); - if (!model) { status.textContent = 'Kein passendes installiertes Sprachmodell verfügbar.'; return; } + const select = button.closest('tr')?.querySelector('.stimmen-modell'); + const modell = select ? select.value : button.dataset.modell; + if (!modell) { status.textContent = 'Kein passendes installiertes Sprachmodell verfügbar.'; return; } + const ton = ausgang(); button.disabled = true; - status.textContent = 'Probehören wird geladen …'; + status.textContent = 'Probehören wird geladen (beim ersten Mal kann das einige Sekunden dauern) …'; try { - const response = await fetch('/ui/modelle/probe', {method: 'POST', headers: {'Content-Type': 'application/json', 'X-CSRF-Token': csrf}, credentials: 'same-origin', body: JSON.stringify({model: model.value, voice: button.dataset.stimme})}); + const response = await fetch('/ui/modelle/probe', {method: 'POST', headers: {'Content-Type': 'application/json', 'X-CSRF-Token': csrf}, credentials: 'same-origin', body: JSON.stringify({model: modell, voice: button.dataset.stimme})}); if (!response.ok) throw new Error('Die Stimme konnte nicht probegehört werden.'); // PCM 24 kHz / 16 Bit / Mono, wie bei der bestehenden Vorlese-Route. const pcm = await response.arrayBuffer(); - const samples = new Int16Array(pcm); - const ctx = new AudioContext({sampleRate: 24000}); - await ctx.resume(); - const buffer = ctx.createBuffer(1, samples.length, 24000); + const samples = new Int16Array(pcm, 0, Math.floor(pcm.byteLength / 2)); + const vorlauf = Math.round(0.3 * 24000); + const buffer = ton.createBuffer(1, vorlauf + samples.length, 24000); const channel = buffer.getChannelData(0); - for (let i = 0; i < samples.length; i++) channel[i] = samples[i] / 32768; - const source = ctx.createBufferSource(); source.buffer = buffer; source.connect(ctx.destination); - source.onended = () => ctx.close(); source.start(); + for (let i = 0; i < samples.length; i++) channel[vorlauf + i] = samples[i] / 32768; + await ton.resume(); + const source = ton.createBufferSource(); source.buffer = buffer; source.connect(ton.destination); + source.onended = () => { status.textContent = ''; }; + source.start(ton.currentTime + 0.05); status.textContent = 'Probehören läuft.'; } catch (_) { status.textContent = 'Die Stimme konnte nicht probegehört werden.'; } finally { button.disabled = false; } diff --git a/crates/nexus-api/src/ui/stimmen.rs b/crates/nexus-api/src/ui/stimmen.rs index 21b5670..b3cccbd 100644 --- a/crates/nexus-api/src/ui/stimmen.rs +++ b/crates/nexus-api/src/ui/stimmen.rs @@ -30,23 +30,19 @@ pub(super) async fn page(state: &AppState, user: &super::Viewer) -> String { let voices = state.runtime.engine_voices("tts").await; let studio_erreichbar = state.runtime.engine_voices_reachable("tts").await; let freigegeben = state.store().list_models(true).unwrap_or_default(); - let options: String = freigegeben + // Installierte, freigegebene Sprachmodelle mit der Art ihrer Stimmen. + let modelle: Vec<(String, &'static str)> = freigegeben .into_iter() .filter_map(|m| { let d = state.store().model_details(&m.name).ok().flatten()?; if d.engine != "tts" || !crate::ui::chat::speech_offered(&state.runtime, &d.path) { return None; } - Some(format!( - "", - esc(&m.name), - super::chat::voice_art(&d.path), - esc(&m.name) - )) + Some((m.name, super::chat::voice_art(&d.path))) }) .collect(); - body.push_str("

Stimmen

Eine Stimme darf ohne Freigabe nur probegehört werden. Die erste Freigabe beansprucht die Stimme für diesen Benutzer.

"); - if options.is_empty() { + body.push_str("

Stimmen

Eine Stimme darf ohne Freigabe nur probegehört werden. Die erste Freigabe beansprucht die Stimme für diesen Benutzer. Gespeicherte Stimmen klont ein Base-Modell, eingebaute Stimmen spricht ein CustomVoice-Modell.

"); + if modelle.is_empty() { body.push_str("

Kein installiertes Sprachmodell verfügbar.

"); } if !studio_erreichbar { @@ -57,15 +53,79 @@ pub(super) async fn page(state: &AppState, user: &super::Viewer) -> String { } else if voices.is_empty() { body.push_str("

Das Studio meldet keine Stimmen.

"); } - for v in voices { + let (gespeichert, eingebaut): (Vec<_>, Vec<_>) = voices.into_iter().partition(|v| v.saved); + let zeile = |v: &nexus_adapter::Voice, passend: &[&(String, &'static str)]| -> String { let approved = state .store() .voice_approved(&v.id, v.saved, &user.user) .unwrap_or(false); - let art = if v.saved { "gespeichert" } else { "eingebaut" }; - body.push_str(&format!("
{}({art})
{}
", - esc(&v.name), esc(&v.name), esc(&v.id), if v.saved { "1" } else { "0" }, csrf_field(user), esc(&v.id), if v.saved { "1" } else { "0" }, if approved { "0" } else { "1" }, if approved { "Freigabe zurücknehmen" } else { "Freigeben" })); - } + let modell = match passend { + [] => "kein passendes Modell".to_string(), + [(name, _)] => esc(name), + many => format!( + "", + esc(&v.name), + many.iter() + .map(|(n, _)| format!("", esc(n))) + .collect::() + ), + }; + let erstes = passend.first().map(|(n, _)| esc(n)).unwrap_or_default(); + let probe = if passend.is_empty() { + String::new() + } else { + format!( + " ", + esc(&v.id) + ) + }; + format!( + "{name}{modell}{status}{probe}
{csrf}
", + name = esc(&v.name), + status = if approved { + "Freigegeben" + } else { + "Nicht freigegeben" + }, + csrf = csrf_field(user), + id = esc(&v.id), + saved = if v.saved { "1" } else { "0" }, + neu = if approved { "0" } else { "1" }, + text = if approved { + "Freigabe zurücknehmen" + } else { + "Freigeben" + }, + ) + }; + let tabelle = |titel: &str, liste: &[nexus_adapter::Voice], art: &str| -> String { + let passend: Vec<_> = modelle + .iter() + .filter(|(_, a)| *a == art || *a == "alle") + .collect(); + if liste.is_empty() { + return String::new(); + } + let zeilen: String = liste.iter().map(|v| zeile(v, &passend)).collect(); + if passend.is_empty() { + // Ohne passendes Modell gibt es nichts zu tun: nur die Namen, zugeklappt. + let namen: Vec = liste.iter().map(|v| esc(&v.name)).collect(); + return format!( + "
{titel} ({}) – kein passendes Sprachmodell installiert

{}

", + liste.len(), + namen.join(", ") + ); + } + format!( + "

{titel}

{zeilen}
StimmeModellFreigabeAktionen
" + ) + }; + body.push_str(&tabelle( + "Gespeicherte Stimmen", + &gespeichert, + "gespeichert", + )); + body.push_str(&tabelle("Eingebaute Stimmen", &eingebaut, "eingebaut")); body.push_str("

"); body } @@ -97,10 +157,12 @@ pub(super) async fn save( { return super::refused(StatusCode::BAD_REQUEST, "Die Stimme ist nicht verfügbar."); } - match state + // Ergebnis zuerst holen: Die Sperre der Datenbank darf im `match` nicht mehr gehalten werden, denn + // `audit` sperrt sie erneut (sonst steht der ganze Dienst still). + let ergebnis = state .store() - .set_voice_approval(&voice, saved, &user.user, approved) - { + .set_voice_approval(&voice, saved, &user.user, approved); + match ergebnis { Ok(true) => { audit(&state, &user, "voice.approval", &voice); redirect("/ui/modelle?art=sprache") diff --git a/crates/nexus-api/tests/api.rs b/crates/nexus-api/tests/api.rs index d420e1f..6c49878 100644 --- a/crates/nexus-api/tests/api.rs +++ b/crates/nexus-api/tests/api.rs @@ -4625,3 +4625,23 @@ async fn studio_starten_braucht_admin_und_csrf() { let (status, _, _) = ui_post(&gw, "/ui/modelle/studio-start", &admin_cookie, "").await; assert_eq!(status, 403); } + +#[tokio::test] +async fn stimme_ueber_die_oberflaeche_freigeben_haengt_nicht() { + // Fehler srv04 (rc11): Der Knopf „Freigeben“ hielt die Datenbanksperre und schrieb dann ins Audit; + // der ganze Dienst stand still. Bei einem Rückfall hängt dieser Test, statt zu scheitern. + let gw = gateway(0, false).await; + let (cookie, csrf) = ui_session(&gw, &gw.key_ui).await; + let body = format!("csrf={csrf}&stimme=Ryan&gespeichert=1&freigabe=1"); + let (status, _, _) = ui_post(&gw, "/ui/modelle/stimme", &cookie, &body).await; + assert_eq!(status, 303); + assert!( + gw.state + .store() + .voice_approved("Ryan", true, TEST_NUTZER) + .unwrap() + ); + // Und die Seite danach antwortet noch. + let (status, _, _) = ui_get(&gw, "/ui/modelle?art=sprache", Some(&cookie)).await; + assert_eq!(status, 200); +}