feat #14740: transcription pipeline
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
//! 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 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<Self, String> {
|
||||
app.state::<AppState>().claim_processing(id, retry)?;
|
||||
Ok(Self {
|
||||
app: app.clone(),
|
||||
id,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Claim {
|
||||
fn drop(&mut self) {
|
||||
self.app.state::<AppState>().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::<AppState>();
|
||||
|
||||
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 = app
|
||||
.path()
|
||||
.app_cache_dir()
|
||||
.map_err(|e| format!("Cache-Ordner nicht gefunden: {e}"))?
|
||||
.join("upload");
|
||||
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::<AppState>();
|
||||
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(())
|
||||
}
|
||||
|
||||
/// Best-effort deletes the DeepScript jobs of calls whose processing the
|
||||
/// last run of the app did not finish ([`RecoveryReport::failed`]), in the
|
||||
/// background. Skipped without a key.
|
||||
///
|
||||
/// [`RecoveryReport::failed`]: recorder_core::store::RecoveryReport::failed
|
||||
pub fn delete_leftover_jobs(app: &AppHandle, calls: Vec<CallId>) {
|
||||
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::<AppState>();
|
||||
let transcriber = DeepScript::new(state.transport.clone(), key);
|
||||
for id in calls {
|
||||
// A retry the user started meanwhile cleans up itself.
|
||||
let Ok(_claim) = Claim::take(&app, id, true) else {
|
||||
continue;
|
||||
};
|
||||
pipeline::delete_remote_jobs(&state.store, &transcriber, id).await;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn deepscript_key(state: &AppState) -> Option<String> {
|
||||
state
|
||||
.secrets
|
||||
.get(Service::DeepScript)
|
||||
.filter(|key| !key.trim().is_empty())
|
||||
}
|
||||
|
||||
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 "<app> · <N> min", or the failure's message.
|
||||
fn notify(app: &AppHandle, id: CallId, error: Option<String>) {
|
||||
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::<AppState>().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()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user