feat #14740: transcription pipeline

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hauke
2026-11-05 16:41:53 +01:00
co-authored by Claude Opus 5.5
parent 1420e56e77
commit e2828791da
13 changed files with 1066 additions and 17 deletions
+109 -5
View File
@@ -1,22 +1,24 @@
//! Application state shared by commands, the tray and background threads.
//!
//! Holds the store, the settings, the secret store, the recording that is
//! currently running, the call detector and the call awaiting a decision in
//! the end-of-call prompt. Everything here is independent of windows and
//! Holds the store, the settings, the secret store, the HTTP transport, the
//! recording that is currently running, the call detector, the calls
//! awaiting a decision in the end-of-call prompt and the calls being
//! processed. Everything here is independent of windows and
//! events; the command layer turns results into UI updates.
use std::collections::VecDeque;
use std::collections::{HashSet, VecDeque};
use std::fs;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Mutex, MutexGuard};
use std::sync::{Arc, Mutex, MutexGuard};
use std::thread;
use std::time::{Duration, Instant};
use chrono::{DateTime, Local};
use recorder_core::capture::{CaptureSource, OutputTarget};
use recorder_core::detect::{CallDetector, DetectorEvent, MicSession};
use recorder_core::http::{HttpTransport, ReqwestTransport};
use recorder_core::model::{Call, CallId, CallStatus};
use recorder_core::recorder::Recorder;
use recorder_core::secrets::SecretStore;
@@ -31,6 +33,7 @@ const MANUAL_APP: &str = "Manuell";
/// How long [`AppState::test_devices`] records.
const TEST_DURATION: Duration = Duration::from_secs(5);
const TEST_POLL: Duration = Duration::from_millis(50);
const ALREADY_PROCESSING: &str = "Wird bereits verarbeitet";
/// Payload of the `recording-state` event.
#[derive(Serialize, Clone, Debug)]
@@ -111,6 +114,8 @@ struct CurrentRecording {
pub struct AppState {
pub store: Store,
pub secrets: Box<dyn SecretStore>,
/// The one HTTP client all API calls share.
pub transport: Arc<dyn HttpTransport>,
settings: Mutex<Settings>,
settings_path: PathBuf,
recordings_dir: PathBuf,
@@ -120,6 +125,8 @@ pub struct AppState {
/// first. A call leaves the queue when it is decided (discarded or sent
/// to transcription).
pending_prompts: Mutex<VecDeque<CallId>>,
/// Calls a pipeline run is under way for; at most one run per call.
processing: Mutex<HashSet<CallId>>,
/// Set while a recording starts or the devices are tested, so neither
/// runs twice and `current` is never locked for longer than a moment.
devices_busy: AtomicBool,
@@ -146,12 +153,14 @@ impl AppState {
Self {
store,
secrets,
transport: Arc::new(ReqwestTransport::new()),
settings: Mutex::new(settings),
settings_path,
recordings_dir,
current: Mutex::new(None),
detector: Mutex::new(detector),
pending_prompts: Mutex::new(VecDeque::new()),
processing: Mutex::new(HashSet::new()),
devices_busy: AtomicBool::new(false),
}
}
@@ -432,6 +441,40 @@ impl AppState {
Ok(self.prompts_waiting())
}
/// Reserves `id` for one pipeline run; release it with
/// [`AppState::release_processing`] when the run ends. A call that waits
/// for the decision can be processed; a failed one only on `retry`, or
/// while the prompt still shows it (its first attempt failed before
/// anything started, e.g. for lack of an API key).
pub fn claim_processing(&self, id: CallId, retry: bool) -> Result<(), String> {
let mut processing = lock(&self.processing);
if processing.contains(&id) {
return Err(ALREADY_PROCESSING.to_string());
}
let call = self
.store
.get_call(id)
.map_err(|e| e.to_string())?
.ok_or_else(|| "Gespräch nicht gefunden".to_string())?;
match call.status {
CallStatus::AwaitingDecision => {}
CallStatus::Failed if retry || lock(&self.pending_prompts).contains(&id) => {}
CallStatus::Processing => return Err(ALREADY_PROCESSING.to_string()),
CallStatus::Recording => return Err("Das Gespräch wird gerade aufgenommen".to_string()),
CallStatus::Done => return Err("Das Gespräch ist bereits transkribiert".to_string()),
CallStatus::Failed => {
return Err("Das Gespräch wartet nicht auf eine Entscheidung".to_string())
}
}
processing.insert(id);
Ok(())
}
/// Ends the pipeline run claimed by [`AppState::claim_processing`].
pub fn release_processing(&self, id: CallId) {
lock(&self.processing).remove(&id);
}
/// Records both configured devices into a temporary directory for five
/// seconds and returns the peak level of each track.
pub fn test_devices(&self) -> Result<DeviceLevels, String> {
@@ -664,6 +707,67 @@ mod tests {
assert_eq!(f.state.prompts_waiting(), 0);
}
#[test]
fn a_call_is_processed_once_at_a_time() {
let f = fixture();
let (id, _, _) = finished_call(&f, "a");
assert_eq!(f.state.claim_processing(id, false), Ok(()));
assert_eq!(
f.state.claim_processing(id, true),
Err("Wird bereits verarbeitet".to_string())
);
f.state.release_processing(id);
assert_eq!(f.state.claim_processing(id, false), Ok(()));
}
#[test]
fn a_processing_call_is_not_claimed_again() {
let f = fixture();
let (id, _, _) = finished_call(&f, "a");
f.state
.store
.set_status(id, CallStatus::Processing, None)
.unwrap();
assert_eq!(
f.state.claim_processing(id, true),
Err("Wird bereits verarbeitet".to_string())
);
}
#[test]
fn a_failed_call_is_processed_again_on_retry_or_from_the_prompt() {
let f = fixture();
let (id, _, _) = finished_call(&f, "a");
f.state
.store
.set_status(id, CallStatus::Failed, Some("Fehler"))
.unwrap();
assert!(f.state.claim_processing(id, false).is_err());
assert_eq!(f.state.claim_processing(id, true), Ok(()));
f.state.release_processing(id);
f.state.queue_prompt(id);
assert_eq!(f.state.claim_processing(id, false), Ok(()));
}
#[test]
fn done_and_recording_calls_are_not_processed() {
let f = fixture();
let (done, _, _) = finished_call(&f, "a");
f.state
.store
.set_status(done, CallStatus::Done, None)
.unwrap();
assert!(f.state.claim_processing(done, true).is_err());
let recording = f
.state
.store
.create_call("Manuell", at(9, 0, 0), Path::new(""), Path::new(""))
.unwrap();
assert!(f.state.claim_processing(recording, true).is_err());
assert!(f.state.claim_processing(recording + 1, true).is_err());
}
#[test]
fn invalid_detection_delays_are_not_saved() {
let f = fixture();