feat(transcribe): route dictionary_terms + initial_prompt through active profile

This commit is contained in:
2026-04-19 20:51:41 +01:00
parent c8952df591
commit 6544bcbaa0
2 changed files with 115 additions and 28 deletions

View File

@@ -36,12 +36,39 @@ pub async fn transcribe_pcm(
british_english: bool,
anti_hallucination: bool,
format_mode: String,
profile_id: Option<String>,
) -> Result<(), String> {
let resolved_profile_id = profile_id
.unwrap_or_else(|| kon_storage::DEFAULT_PROFILE_ID.to_string());
let profile = kon_storage::database::get_profile(&state.db, &resolved_profile_id)
.await
.map_err(|e| e.to_string())?
.ok_or_else(|| format!("Profile {resolved_profile_id} not found"))?;
let profile_terms: Vec<String> =
kon_storage::database::list_profile_terms(&state.db, &resolved_profile_id)
.await
.map_err(|e| e.to_string())?
.into_iter()
.map(|t| t.term)
.collect();
let effective_prompt = if !initial_prompt.is_empty() {
initial_prompt
} else {
profile.initial_prompt.clone()
};
let engine = state.whisper_engine.clone();
let audio = kon_core::AudioSamples::mono_16khz(samples);
let options = TranscriptionOptions {
language: Some(language),
initial_prompt: Some(initial_prompt),
initial_prompt: if effective_prompt.is_empty() {
None
} else {
Some(effective_prompt)
},
};
let timed = tokio::task::spawn_blocking(move || {
@@ -50,12 +77,7 @@ pub async fn transcribe_pcm(
.await
.map_err(|e| e.to_string())??;
let dictionary_terms: Vec<String> = kon_storage::database::list_dictionary(&state.db)
.await
.unwrap_or_default()
.into_iter()
.map(|e| e.term)
.collect();
let dictionary_terms = profile_terms.clone();
let mut segments: Vec<Segment> = timed.transcript.segments().to_vec();
post_process_segments(
@@ -98,7 +120,30 @@ pub async fn transcribe_file(
british_english: bool,
anti_hallucination: bool,
format_mode: String,
profile_id: Option<String>,
) -> Result<serde_json::Value, String> {
let resolved_profile_id = profile_id
.unwrap_or_else(|| kon_storage::DEFAULT_PROFILE_ID.to_string());
let profile = kon_storage::database::get_profile(&state.db, &resolved_profile_id)
.await
.map_err(|e| e.to_string())?
.ok_or_else(|| format!("Profile {resolved_profile_id} not found"))?;
let profile_terms: Vec<String> =
kon_storage::database::list_profile_terms(&state.db, &resolved_profile_id)
.await
.map_err(|e| e.to_string())?
.into_iter()
.map(|t| t.term)
.collect();
let effective_prompt = if !initial_prompt.is_empty() {
initial_prompt
} else {
profile.initial_prompt.clone()
};
let engine_name = engine.unwrap_or_else(|| "whisper".to_string());
let model_id = model_id
.unwrap_or_else(|| default_model_id_for_engine(&engine_name).to_string());
@@ -107,7 +152,11 @@ pub async fn transcribe_file(
let engine = pick_engine(&state, &engine_name)?;
let options = TranscriptionOptions {
language: Some(language),
initial_prompt: Some(initial_prompt),
initial_prompt: if effective_prompt.is_empty() {
None
} else {
Some(effective_prompt)
},
};
let timed = tokio::task::spawn_blocking(move || {
@@ -122,12 +171,7 @@ pub async fn transcribe_file(
.await
.map_err(|e| e.to_string())??;
let dictionary_terms: Vec<String> = kon_storage::database::list_dictionary(&state.db)
.await
.unwrap_or_default()
.into_iter()
.map(|e| e.term)
.collect();
let dictionary_terms = profile_terms.clone();
let mut segments: Vec<Segment> = timed.transcript.segments().to_vec();
post_process_segments(
@@ -162,7 +206,26 @@ pub async fn transcribe_pcm_parakeet(
british_english: bool,
anti_hallucination: bool,
format_mode: String,
profile_id: Option<String>,
) -> Result<(), String> {
let resolved_profile_id = profile_id
.unwrap_or_else(|| kon_storage::DEFAULT_PROFILE_ID.to_string());
// Validate the profile exists so parakeet and whisper behave identically
// when a bogus id slips through from the frontend.
kon_storage::database::get_profile(&state.db, &resolved_profile_id)
.await
.map_err(|e| e.to_string())?
.ok_or_else(|| format!("Profile {resolved_profile_id} not found"))?;
let profile_terms: Vec<String> =
kon_storage::database::list_profile_terms(&state.db, &resolved_profile_id)
.await
.map_err(|e| e.to_string())?
.into_iter()
.map(|t| t.term)
.collect();
let engine = state.parakeet_engine.clone();
let audio = kon_core::AudioSamples::mono_16khz(samples);
let options = TranscriptionOptions::default();
@@ -173,12 +236,7 @@ pub async fn transcribe_pcm_parakeet(
.await
.map_err(|e| e.to_string())??;
let dictionary_terms: Vec<String> = kon_storage::database::list_dictionary(&state.db)
.await
.unwrap_or_default()
.into_iter()
.map(|e| e.term)
.collect();
let dictionary_terms = profile_terms.clone();
let mut segments: Vec<Segment> = timed.transcript.segments().to_vec();
post_process_segments(