Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src-tauri/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ struct LanguageGuardEvent {
preview: String,
}

#[derive(Clone, serde::Serialize)]
struct TransformSelectionCaptureBlockedEvent {
reason_code: String,
}

/// Drop guard that notifies the [`TranscriptionCoordinator`] when the
/// transcription pipeline finishes — whether it completes normally or panics.
struct FinishGuard(AppHandle, u64);
Expand Down Expand Up @@ -1019,6 +1024,7 @@ fn force_ltr_input_direction_before_paste(
pub(crate) async fn process_adaptive_transcription_output(
settings: &AppSettings,
transcription: &str,
effective_language: Option<&str>,
context: crate::adaptive::types::CapturedContext,
shortcut: crate::adaptive::types::ShortcutIntent,
) -> crate::adaptive::types::AdaptiveProcessResult {
Expand Down Expand Up @@ -1048,7 +1054,11 @@ pub(crate) async fn process_adaptive_transcription_output(
let final_text = if settings.formatting_level == crate::settings::FormattingLevel::None {
transcription.to_string()
} else {
crate::adaptive::processor::deterministic_process(transcription, profile)
crate::adaptive::processor::deterministic_process(
transcription,
profile,
effective_language,
)
};
let final_text = crate::adaptive::smart_formatting::format_transcript(
&final_text,
Expand Down Expand Up @@ -1982,7 +1992,7 @@ impl ShortcutAction for TranscribeAction {

// Transcribe concurrently with WAV save
let transcription_time = Instant::now();
let transcription_result = tm.transcribe_with_cancellation(
let transcription_result = tm.transcribe_with_cancellation_context(
samples,
operation_token
.as_ref()
Expand Down Expand Up @@ -2025,7 +2035,8 @@ impl ShortcutAction for TranscribeAction {
}

match transcription_result {
Ok(transcription) => {
Ok(transcription_output) => {
let transcription = transcription_output.text;
debug!(
"{}",
transcription_completed_log_message(
Expand Down Expand Up @@ -2066,6 +2077,7 @@ impl ShortcutAction for TranscribeAction {
let processed = process_adaptive_transcription_output(
&settings,
&transcription,
transcription_output.effective_language.as_deref(),
context.clone(),
crate::adaptive::types::ShortcutIntent::Default,
)
Expand Down Expand Up @@ -2394,6 +2406,18 @@ impl ShortcutAction for TransformShortcutAction {
);
}
Err(err) => {
if matches!(
err.as_str(),
crate::selection::SECURE_FIELD_REASON_CODE
| crate::selection::SECURE_CHECK_ERROR_REASON_CODE
) {
let _ = app.emit(
"transform-selection-capture-blocked",
TransformSelectionCaptureBlockedEvent {
reason_code: err.clone(),
},
);
}
warn!("Transform shortcut '{}' failed: {}", binding_id, err);
}
}
Expand Down
38 changes: 34 additions & 4 deletions src-tauri/src/adaptive/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ use crate::adaptive::language::analyze_language;
use crate::adaptive::profile::{AdaptiveProfile, RewriteMode};
use crate::adaptive::types::LanguageClass;

pub fn deterministic_process(raw: &str, profile: &AdaptiveProfile) -> String {
pub fn deterministic_process(
raw: &str,
profile: &AdaptiveProfile,
effective_language: Option<&str>,
) -> String {
if profile.rewrite.mode == RewriteMode::Disabled {
return raw.to_string();
}

let mut tokens = Vec::new();
for token in raw.split_whitespace() {
if profile.cleanup.remove_fillers && is_simple_filler(token) {
if profile.cleanup.remove_fillers
&& effective_language.is_some_and(is_english_language)
&& is_simple_filler(token)
{
continue;
}
tokens.push(token);
Expand All @@ -25,6 +32,13 @@ pub fn deterministic_process(raw: &str, profile: &AdaptiveProfile) -> String {
output
}

fn is_english_language(language: &str) -> bool {
language
.split(['-', '_'])
.next()
.is_some_and(|base| base.eq_ignore_ascii_case("en"))
}

pub fn validate_output(raw: &str, output: &str, profile: &AdaptiveProfile) -> Result<(), String> {
if output.trim().is_empty() && !raw.trim().is_empty() {
return Err("processed output is empty".to_string());
Expand Down Expand Up @@ -213,21 +227,35 @@ mod tests {

#[test]
fn raw_profile_returns_input_unchanged() {
let result = deterministic_process("um hello hello", &profile("raw"));
let result = deterministic_process("um hello hello", &profile("raw"), None);
assert_eq!(result, "um hello hello");
}

#[test]
fn clean_profile_removes_simple_english_fillers() {
let result = deterministic_process("um hello, uh we should go", &profile("default_clean"));
let result = deterministic_process(
"um hello, uh we should go",
&profile("default_clean"),
Some("en"),
);
assert_eq!(result, "hello, we should go");
}

#[test]
fn clean_profile_preserves_english_fillers_without_locked_english() {
let auto = deterministic_process("um hello", &profile("default_clean"), None);
let portuguese = deterministic_process("um hello", &profile("default_clean"), Some("pt"));

assert_eq!(auto, "um hello");
assert_eq!(portuguese, "um hello");
}

#[test]
fn technical_profile_preserves_identifiers() {
let result = deterministic_process(
"uh run cargo_test in src-tauri/src/actions.rs",
&profile("technical"),
Some("en"),
);
assert!(result.contains("cargo_test"));
assert!(result.contains("src-tauri/src/actions.rs"));
Expand All @@ -238,6 +266,7 @@ mod tests {
let result = deterministic_process(
"Dear James, I have received your Excel file. Sincerely, Abdullah Al-Khalid.",
&profile("email"),
Some("en"),
);

assert_eq!(
Expand All @@ -251,6 +280,7 @@ mod tests {
let result = deterministic_process(
"Hello Dana, The report is attached. Best regards, Abdullah.",
&profile("email"),
Some("en"),
);

assert_eq!(
Expand Down
Loading
Loading