Files
Lumotia/src-tauri/src/commands/models.rs
Jake 62ea58d95a diagnostics: panic hook + frontend error capture + Settings → About diagnostic report
THE BRIEF:
For the friends beta we need verbal feedback AND technical feedback —
some bugs the user cannot describe but a stack trace can. Built it in
two layers, with Layer 3 deferred until there is real volume.

PRIVACY POSTURE (matches Kon's local-first positioning):
- All capture is to disk only. Nothing is transmitted.
- The manual report bundler shows the user exactly what would be
  shared and lets them choose to copy / save it.
- No remote endpoint, no Sentry, no opt-out telemetry.

LAYER 1 — Always-on local capture:
- Rust panic hook (commands/diagnostics.rs::install_panic_hook) writes
  each panic to ~/.kon/crashes/<unix-ts>-<short-id>.crash. Captures
  thread name, OS/arch, RUST_BACKTRACE state, and the panic info.
  Installed before tauri::Builder so it catches setup-time panics too.
- Frontend window.onerror + window.unhandledrejection in
  src/routes/+layout.svelte forward to the new log_frontend_error
  Tauri command, which inserts into the existing error_log SQLite
  table. Best-effort: errors in the error handler itself are swallowed
  so logging can never crash the app.
- Existing error_log table (migration v1) is now actually used —
  closes the TODO from crates/storage/src/database.rs:409.

LAYER 2 — Manual diagnostic report:
- Settings → About → Diagnostics section: Generate report → Preview →
  Copy / Save.
- Report is plain markdown so it pastes cleanly into email, Discord,
  GitHub issues. Sections: app version + OS, sanitised settings JSON,
  recent error_log rows, last 5 crash dumps with previews, log file
  tail (8KB).
- Preview is shown in a <details> with the full text the user can
  inspect before deciding to share.
- Save writes to ~/.kon/diagnostic-reports/kon-diagnostic-<ts>.md.

NEW FILES:
- src-tauri/src/commands/diagnostics.rs (panic hook + 5 Tauri commands)

CHANGES:
- crates/storage/src/file_storage.rs: crashes_dir() + logs_dir() helpers
- crates/storage/src/database.rs: ErrorLogRow + list_recent_errors()
- crates/storage/src/lib.rs: re-exports
- src-tauri/src/commands/mod.rs: + diagnostics module
- src-tauri/src/lib.rs: install_panic_hook() before Builder; 5 new
  Tauri commands registered (log_frontend_error,
  list_recent_errors_command, list_crash_files,
  generate_diagnostic_report, save_diagnostic_report)
- src/routes/+layout.svelte: installGlobalErrorCapture() at mount
- src/lib/pages/SettingsPage.svelte: diagnostic state + buttons +
  preview block at the end of the About section

cargo check -p kon-storage clean. Settings.svelte if/each balanced.

LAYER 3 (deferred):
- Optional opt-in remote reporting (self-hosted Sentry on Tartarus or
  similar). Not for friends beta. Open up after volume justifies it.
2026-04-17 13:47:20 +01:00

344 lines
10 KiB
Rust

use std::sync::Arc;
use serde::Serialize;
use tauri::Emitter;
use crate::AppState;
use kon_core::model_registry::{
self, Engine, LanguageSupport, ModelEntry,
};
use kon_core::types::ModelId;
use kon_transcription::model_manager;
use kon_transcription::{load_parakeet, load_whisper, LocalEngine};
/// Map legacy size strings to ModelId.
fn whisper_model_id(size: &str) -> ModelId {
match size.to_lowercase().as_str() {
"tiny" => ModelId::new("whisper-tiny-en"),
"base" => ModelId::new("whisper-base-en"),
"small" => ModelId::new("whisper-small-en"),
"medium" => ModelId::new("whisper-medium-en"),
other => ModelId::new(other),
}
}
fn parakeet_model_id(name: &str) -> ModelId {
match name {
"ctc-int8" => ModelId::new("parakeet-ctc-0.6b-int8"),
other => ModelId::new(other),
}
}
fn engine_for_name(
state: &AppState,
engine_name: &str,
) -> Result<Arc<LocalEngine>, String> {
match engine_name {
"whisper" => Ok(state.whisper_engine.clone()),
"parakeet" => Ok(state.parakeet_engine.clone()),
other => Err(format!("Unknown engine: {other}")),
}
}
fn language_support_info(
language_support: LanguageSupport,
) -> LanguageSupportInfo {
match language_support {
LanguageSupport::EnglishOnly => LanguageSupportInfo {
kind: "english-only".into(),
language_count: 1,
},
LanguageSupport::Multilingual(count) => LanguageSupportInfo {
kind: "multilingual".into(),
language_count: count,
},
}
}
fn model_capability(
entry: &'static ModelEntry,
engine: &Arc<LocalEngine>,
) -> ModelRuntimeCapabilities {
let loaded_model_id = engine.loaded_model_id();
ModelRuntimeCapabilities {
id: entry.id.to_string(),
display_name: entry.display_name.to_string(),
downloaded: model_manager::is_downloaded(&entry.id),
loaded: loaded_model_id
.as_ref()
.map(|id| id == &entry.id)
.unwrap_or(false),
language_support: language_support_info(entry.languages),
}
}
fn load_model_from_disk(model_id: &ModelId) -> Result<kon_transcription::LoadedModel, String> {
let entry = model_registry::find_model(model_id)
.ok_or_else(|| format!("Unknown model: {model_id}"))?;
match entry.engine {
Engine::Whisper => {
let dir = model_manager::model_dir(model_id);
let model_file = entry
.files
.first()
.map(|file| dir.join(file.filename))
.ok_or_else(|| format!("No files registered for model: {model_id}"))?;
if !model_file.exists() {
return Err(format!("Model not downloaded: {model_id}"));
}
load_whisper(&model_file).map_err(|e| e.to_string())
}
Engine::Parakeet => {
let dir = model_manager::model_dir(model_id);
if !dir.exists() {
return Err(format!("Model not downloaded: {model_id}"));
}
load_parakeet(&dir).map_err(|e| e.to_string())
}
Engine::Moonshine => Err("Moonshine models are not yet supported in this build".into()),
}
}
pub fn default_model_id_for_engine(engine: &str) -> ModelId {
match engine {
"parakeet" => ModelId::new("parakeet-ctc-0.6b-int8"),
_ => ModelId::new("whisper-base-en"),
}
}
pub async fn ensure_model_loaded(
state: &AppState,
engine_name: &str,
model_id: &str,
) -> Result<(), String> {
let model_id = ModelId::new(model_id);
let entry = model_registry::find_model(&model_id)
.ok_or_else(|| format!("Unknown model: {model_id}"))?;
let expected_engine = match entry.engine {
Engine::Whisper => "whisper",
Engine::Parakeet => "parakeet",
Engine::Moonshine => "moonshine",
};
if expected_engine != engine_name {
return Err(format!(
"Model {} belongs to {}, not {}",
model_id, expected_engine, engine_name
));
}
if !model_manager::is_downloaded(&model_id) {
return Err(format!("Model not downloaded: {model_id}"));
}
let engine = engine_for_name(state, engine_name)?;
if engine
.loaded_model_id()
.as_ref()
.map(|loaded| loaded == &model_id)
.unwrap_or(false)
{
return Ok(());
}
let engine_clone = engine.clone();
let model_id_clone = model_id.clone();
tokio::task::spawn_blocking(move || {
let model = load_model_from_disk(&model_id_clone)?;
engine_clone.load(model, model_id_clone);
Ok::<_, String>(())
})
.await
.map_err(|e| e.to_string())??;
Ok(())
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RuntimeCapabilities {
pub accelerators: Vec<String>,
pub engines: Vec<EngineRuntimeCapabilities>,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EngineRuntimeCapabilities {
pub id: String,
pub default_model_id: String,
pub loaded_model_id: Option<String>,
pub supports_gpu: bool,
pub models: Vec<ModelRuntimeCapabilities>,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ModelRuntimeCapabilities {
pub id: String,
pub display_name: String,
pub downloaded: bool,
pub loaded: bool,
pub language_support: LanguageSupportInfo,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LanguageSupportInfo {
pub kind: String,
pub language_count: u16,
}
#[tauri::command]
pub fn get_runtime_capabilities(
state: tauri::State<'_, AppState>,
) -> Result<RuntimeCapabilities, String> {
let whisper = state.whisper_engine.clone();
let parakeet = state.parakeet_engine.clone();
let whisper_models = model_registry::all_models()
.iter()
.filter(|entry| entry.engine == Engine::Whisper)
.map(|entry| model_capability(entry, &whisper))
.collect();
let parakeet_models = model_registry::all_models()
.iter()
.filter(|entry| entry.engine == Engine::Parakeet)
.map(|entry| model_capability(entry, &parakeet))
.collect();
Ok(RuntimeCapabilities {
// Current desktop build ships CPU-only inference backends.
accelerators: vec!["cpu".into()],
engines: vec![
EngineRuntimeCapabilities {
id: "whisper".into(),
default_model_id: default_model_id_for_engine("whisper")
.to_string(),
loaded_model_id: whisper
.loaded_model_id()
.map(|model_id| model_id.to_string()),
supports_gpu: false,
models: whisper_models,
},
EngineRuntimeCapabilities {
id: "parakeet".into(),
default_model_id: default_model_id_for_engine("parakeet")
.to_string(),
loaded_model_id: parakeet
.loaded_model_id()
.map(|model_id| model_id.to_string()),
supports_gpu: false,
models: parakeet_models,
},
],
})
}
// --- Whisper model commands ---
#[tauri::command]
pub async fn download_model(
app: tauri::AppHandle,
size: String,
) -> Result<String, String> {
let id = whisper_model_id(&size);
let app_clone = app.clone();
model_manager::download(&id, move |progress| {
let _ = app_clone.emit("model-download-progress", &progress);
})
.await
.map_err(|e| e.to_string())?;
Ok(format!("Model {} downloaded", size))
}
#[tauri::command]
pub fn check_model(size: String) -> Result<bool, String> {
let id = whisper_model_id(&size);
Ok(model_manager::is_downloaded(&id))
}
#[tauri::command]
pub fn list_models() -> Result<Vec<String>, String> {
let ids = model_manager::list_downloaded();
Ok(ids
.into_iter()
.filter(|id| id.as_str().starts_with("whisper-"))
.map(|id| match id.as_str() {
"whisper-tiny-en" => "Tiny".to_string(),
"whisper-base-en" => "Base".to_string(),
"whisper-small-en" => "Small".to_string(),
"whisper-medium-en" => "Medium".to_string(),
other => other.to_string(),
})
.collect())
}
#[tauri::command]
pub async fn load_model(
state: tauri::State<'_, AppState>,
size: String,
) -> Result<String, String> {
let id = whisper_model_id(&size);
ensure_model_loaded(&state, "whisper", id.as_str()).await?;
Ok(format!("Model {} loaded", size))
}
#[tauri::command]
pub fn check_engine(state: tauri::State<AppState>) -> Result<bool, String> {
Ok(state.whisper_engine.is_loaded())
}
// --- Parakeet model commands ---
#[tauri::command]
pub async fn download_parakeet_model(
app: tauri::AppHandle,
name: String,
) -> Result<String, String> {
let id = parakeet_model_id(&name);
let app_clone = app.clone();
model_manager::download(&id, move |progress| {
let _ = app_clone.emit("parakeet-download-progress", &progress);
})
.await
.map_err(|e| e.to_string())?;
Ok(format!("Parakeet model {} downloaded", name))
}
#[tauri::command]
pub fn check_parakeet_model(name: String) -> Result<bool, String> {
let id = parakeet_model_id(&name);
Ok(model_manager::is_downloaded(&id))
}
#[tauri::command]
pub fn list_parakeet_models() -> Result<Vec<String>, String> {
let ids = model_manager::list_downloaded();
Ok(ids
.into_iter()
.filter(|id| id.as_str().starts_with("parakeet-"))
.map(|id| match id.as_str() {
"parakeet-ctc-0.6b-int8" => "ctc-int8".to_string(),
other => other.to_string(),
})
.collect())
}
#[tauri::command]
pub async fn load_parakeet_model(
state: tauri::State<'_, AppState>,
name: String,
) -> Result<String, String> {
let id = parakeet_model_id(&name);
ensure_model_loaded(&state, "parakeet", id.as_str()).await?;
Ok(format!("Parakeet model {} loaded", name))
}
#[tauri::command]
pub fn check_parakeet_engine(
state: tauri::State<'_, AppState>,
) -> Result<bool, String> {
Ok(state.parakeet_engine.is_loaded())
}