feat(A.2 #13): replace SpeechBackend enum with Transcriber trait

New crates/transcription/src/transcriber.rs defines a Transcriber
trait (Send supertrait for spawn_blocking travel) with
TranscriberCapabilities (sample_rate, channels, supports_initial_prompt).
TranscriberCapabilities.sample_rate is load-bearing for the upcoming
progressive WAV writer (#19).

Concrete impls:
- SpeechModelAdapter wraps Box<dyn transcribe_rs::SpeechModel + Send>
  for Parakeet (and any future transcribe-rs-backed engine).
- WhisperRsBackend moves its transcribe_sync body into the impl,
  widening the signature from &self to &mut self so per-call
  WhisperState can be created cleanly through the trait object.

LocalEngine now holds Box<dyn Transcriber + Send>; dispatch in
transcribe_sync collapses from a match to a direct call. Adds
LocalEngine::capabilities() for the WAV-writer.

Cargo feature flag "whisper" (default on) makes whisper-rs, num_cpus,
and the whole whisper_rs_backend module optional. cargo check
--no-default-features -p kon-transcription now builds without pulling
whisper-rs-sys — the escape hatch brief item #6 / #13 called for on
Windows / non-AVX2 / cloud-only builds. load_whisper is cfg-gated
behind the same feature.

src-tauri/src/commands/models.rs load_model_from_disk returns
Box<dyn Transcriber + Send> instead of SpeechBackend; caller chain
(ensure_model_loaded, prewarm_default_model) is unchanged.

transcriber_trait_is_object_safe test lands alongside the trait as a
compile-time witness against future Self-returning / generic-method
additions.
This commit is contained in:
2026-04-22 04:33:23 +01:00
parent dd98cb7994
commit 8b49d0fe9c
6 changed files with 174 additions and 59 deletions

View File

@@ -10,8 +10,11 @@ use std::path::Path;
use whisper_rs::{FullParams, SamplingStrategy, WhisperContext, WhisperContextParameters};
use kon_core::error::{KonError, Result};
use kon_core::types::{Segment, TranscriptionOptions};
use crate::transcriber::{Transcriber, TranscriberCapabilities};
#[derive(Debug, thiserror::Error)]
pub enum WhisperBackendError {
#[error("whisper-rs load failed: {0}")]
@@ -27,20 +30,32 @@ pub struct WhisperRsBackend {
}
impl WhisperRsBackend {
pub fn load(model_path: &Path) -> Result<Self, WhisperBackendError> {
pub fn load(model_path: &Path) -> std::result::Result<Self, WhisperBackendError> {
let ctx = WhisperContext::new_with_params(model_path, WhisperContextParameters::default())
.map_err(|e| WhisperBackendError::Load(e.to_string()))?;
Ok(Self { ctx })
}
}
impl Transcriber for WhisperRsBackend {
fn capabilities(&self) -> TranscriberCapabilities {
TranscriberCapabilities {
sample_rate: kon_core::constants::WHISPER_SAMPLE_RATE,
channels: 1,
supports_initial_prompt: true,
}
}
/// Synchronously transcribe 16 kHz mono f32 PCM.
///
/// `options.initial_prompt` is piped directly to whisper-rs.
pub fn transcribe_sync(
&self,
/// `options.initial_prompt` is piped directly to whisper-rs — this
/// is the only backend path that honours it; `SpeechModelAdapter`
/// discards it (Parakeet has no equivalent).
fn transcribe_sync(
&mut self,
samples: &[f32],
options: &TranscriptionOptions,
) -> Result<Vec<Segment>, WhisperBackendError> {
) -> Result<Vec<Segment>> {
tracing::info!(
language = ?options.language,
has_initial_prompt = options.initial_prompt.as_deref().map(|p| !p.is_empty()).unwrap_or(false),
@@ -50,7 +65,7 @@ impl WhisperRsBackend {
let mut state = self
.ctx
.create_state()
.map_err(|e| WhisperBackendError::State(e.to_string()))?;
.map_err(|e| KonError::TranscriptionFailed(WhisperBackendError::State(e.to_string()).to_string()))?;
let mut params = FullParams::new(SamplingStrategy::Greedy { best_of: 1 });
if let Some(lang) = options.language.as_deref() {
@@ -70,7 +85,7 @@ impl WhisperRsBackend {
state
.full(params, samples)
.map_err(|e| WhisperBackendError::Transcribe(e.to_string()))?;
.map_err(|e| KonError::TranscriptionFailed(WhisperBackendError::Transcribe(e.to_string()).to_string()))?;
let n = state.full_n_segments();
@@ -81,7 +96,7 @@ impl WhisperRsBackend {
};
let text = seg
.to_str()
.map_err(|e| WhisperBackendError::Transcribe(e.to_string()))?
.map_err(|e| KonError::TranscriptionFailed(WhisperBackendError::Transcribe(e.to_string()).to_string()))?
.to_string();
// whisper-rs timestamps are centiseconds (10ms units). Convert to seconds (f64).
let start = seg.start_timestamp() as f64 * 0.01;