//! Runs the processing pipeline for calls in the background and tells the //! user about it: a `call-updated` event on every status change and a //! system notification when a run ends. Notifications never contain //! transcript text, and the API key is never logged. use std::io::ErrorKind; use std::path::PathBuf; use recorder_core::model::{CallId, CallStatus}; use recorder_core::pipeline::{self, PipelineOptions}; use recorder_core::secrets::Service; use recorder_core::transcribe::deepscript::DeepScript; use recorder_core::transcribe::TranscribeError; use serde::Serialize; use tauri::{AppHandle, Emitter, Manager}; use tauri_plugin_notification::NotificationExt; use crate::commands; use crate::state::AppState; /// Payload of the `call-updated` event: the call's status changed. #[derive(Serialize, Clone)] struct CallUpdated { id: CallId, } /// A call reserved with [`AppState::claim_processing`]; released when /// dropped, also if the run panics. struct Claim { app: AppHandle, id: CallId, } impl Claim { fn take(app: &AppHandle, id: CallId, retry: bool) -> Result { app.state::().claim_processing(id, retry)?; Ok(Self { app: app.clone(), id, }) } /// Reserves the call for a cleanup ([`AppState::reserve_processing`]). fn for_cleanup(app: &AppHandle, id: CallId) -> Option { app.state::() .reserve_processing(id) .then(|| Self { app: app.clone(), id, }) } } impl Drop for Claim { fn drop(&mut self) { self.app.state::().release_processing(self.id); } } /// Starts processing call `id` in the background and returns at once; the /// call leaves the end-of-call prompt. `retry` also allows failed calls. /// Without a DeepScript key nothing starts: the call is marked failed and /// the error returned. Reads the keyring, so call it off the UI thread. pub fn start(app: &AppHandle, id: CallId, retry: bool) -> Result<(), String> { let claim = Claim::take(app, id, retry)?; let state = app.state::(); let Some(key) = deepscript_key(&state) else { let msg = TranscribeError::MissingKey.user_message(); if let Err(e) = state.store.set_status(id, CallStatus::Failed, Some(&msg)) { log::error!("call {id}: could not mark as failed: {e}"); } emit_updated(app, id); return Err(msg); }; let work_dir = upload_dir(app)?; let settings = state.settings(); let opts = PipelineOptions { language: settings.language, deepscript_model: settings.deepscript_model, }; // A fresh client per run, so a key changed in the settings applies to // the next run. let transcriber = DeepScript::new(state.transport.clone(), key); // Shown as processing right away, before the run gets going. state .store .set_status(id, CallStatus::Processing, None) .map_err(|e| format!("Interner Fehler: {e}"))?; emit_updated(app, id); if state.remove_queued_prompt(id).is_some() { commands::prompt_queue_changed(app); } log::info!("call {id}: processing started"); tauri::async_runtime::spawn(async move { let app = &claim.app; let state = app.state::(); let result = pipeline::process_call(&state.store, &transcriber, id, &opts, &work_dir).await; emit_updated(app, id); notify(app, id, result.err().map(|e| e.user_message())); }); Ok(()) } /// Where the pipeline puts the FLAC copies it uploads. fn upload_dir(app: &AppHandle) -> Result { app.path() .app_cache_dir() .map(|dir| dir.join("upload")) .map_err(|e| format!("Cache-Ordner nicht gefunden: {e}")) } /// Removes FLAC copies left in the upload directory by a run the app did /// not finish. Call before any run can start. pub fn clear_upload_dir(app: &AppHandle) { let dir = match upload_dir(app) { Ok(dir) => dir, Err(e) => { log::warn!("{e}"); return; } }; match std::fs::remove_dir_all(&dir) { Ok(()) => log::info!("removed leftover uploads in {}", dir.display()), Err(e) if e.kind() == ErrorKind::NotFound => {} Err(e) => log::warn!("could not empty {}: {e}", dir.display()), } } /// Best-effort deletes the DeepScript jobs still on record for `calls` /// (from runs the app did not finish, or whose delete failed), in the /// background. Skipped without a key. pub fn delete_leftover_jobs(app: &AppHandle, calls: Vec) { if calls.is_empty() { return; } let app = app.clone(); tauri::async_runtime::spawn(async move { let key = { let app = app.clone(); tauri::async_runtime::spawn_blocking(move || deepscript_key(&app.state())).await }; let Ok(Some(key)) = key else { log::info!("no DeepScript key: remote jobs of calls {calls:?} stay for now"); return; }; let state = app.state::(); let transcriber = DeepScript::new(state.transport.clone(), key); for id in calls { // A run the user started meanwhile cleans up itself. let Some(_claim) = Claim::for_cleanup(&app, id) else { continue; }; pipeline::delete_remote_jobs(&state.store, &transcriber, id).await; } }); } fn deepscript_key(state: &AppState) -> Option { state .secrets .get(Service::DeepScript) .filter(|key| !key.trim().is_empty()) } /// Tells the UI that call `id` changed (`call-updated`). pub fn emit_updated(app: &AppHandle, id: CallId) { if let Err(e) = app.emit("call-updated", CallUpdated { id }) { log::error!("could not emit call-updated: {e}"); } } /// "Transkript bereit" with " · min", or the failure's message. fn notify(app: &AppHandle, id: CallId, error: Option) { let (title, body) = match error { None => ("Transkript bereit", call_summary_line(app, id)), Some(msg) => ("Transkription fehlgeschlagen", msg), }; let result = app.notification().builder().title(title).body(body).show(); if let Err(e) = result { log::warn!("call {id}: could not show the notification: {e}"); } } /// "Microsoft Teams · 32 min" (whole minutes, at least 1, like the UI). fn call_summary_line(app: &AppHandle, id: CallId) -> String { match app.state::().call_brief(id) { Ok(brief) => format!( "{} · {} min", brief.app, ((brief.duration_secs as f64 / 60.0).round() as i64).max(1) ), Err(e) => { log::error!("call {id}: could not load it for the notification: {e}"); String::new() } } }