137 lines
3.7 KiB
Rust
137 lines
3.7 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use tauri::State;
|
|
|
|
use crate::app_state::{AppSnapshot, ManagedAppState};
|
|
use crate::injection::{FallbackInjector, InsertMethod, InsertRequest, InsertResult, TextInjector};
|
|
use crate::models::{model_statuses, ModelWithStatus};
|
|
use crate::settings::AppSettings;
|
|
use crate::transcription::crispasr::{CrispAsrConfig, CrispAsrTranscriber};
|
|
use crate::transcription::{Transcriber, TranscriptionInput, TranscriptionOptions};
|
|
|
|
#[tauri::command]
|
|
pub(crate) fn get_app_snapshot(state: State<'_, ManagedAppState>) -> Result<AppSnapshot, String> {
|
|
state.snapshot()
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub(crate) fn start_recording(state: State<'_, ManagedAppState>) -> Result<AppSnapshot, String> {
|
|
state
|
|
.recording
|
|
.lock()
|
|
.map_err(|_| "falha ao acessar estado de gravação".to_string())?
|
|
.start()
|
|
.map_err(String::from)?;
|
|
|
|
state.snapshot()
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub(crate) fn stop_recording_and_transcribe(
|
|
state: State<'_, ManagedAppState>,
|
|
) -> Result<AppSnapshot, String> {
|
|
let sequence = state
|
|
.recording
|
|
.lock()
|
|
.map_err(|_| "falha ao acessar estado de gravação".to_string())?
|
|
.stop()
|
|
.map_err(String::from)?;
|
|
|
|
let settings = state
|
|
.settings
|
|
.lock()
|
|
.map_err(|_| "falha ao acessar configurações".to_string())?
|
|
.clone();
|
|
|
|
let transcriber = CrispAsrTranscriber::new(CrispAsrConfig {
|
|
sidecar_name: None,
|
|
model_path: None,
|
|
});
|
|
|
|
let result = transcriber.transcribe(TranscriptionInput {
|
|
audio_path: PathBuf::from(format!("recording-{sequence}.wav")),
|
|
options: TranscriptionOptions {
|
|
model_id: settings.transcription.model_id,
|
|
language: settings.transcription.language,
|
|
timestamps: false,
|
|
},
|
|
});
|
|
|
|
let mut transcript = state
|
|
.transcript
|
|
.lock()
|
|
.map_err(|_| "falha ao acessar transcrição".to_string())?;
|
|
|
|
match result {
|
|
Ok(result) => {
|
|
transcript.text = result.text;
|
|
transcript.last_message = Some("Transcrição concluída.".to_string());
|
|
}
|
|
Err(error) => {
|
|
transcript.text = "".to_string();
|
|
transcript.last_message = Some(error.to_string());
|
|
}
|
|
}
|
|
|
|
drop(transcript);
|
|
state.snapshot()
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub(crate) fn copy_ready_text(state: State<'_, ManagedAppState>) -> Result<String, String> {
|
|
let transcript = state
|
|
.transcript
|
|
.lock()
|
|
.map_err(|_| "falha ao acessar transcrição".to_string())?;
|
|
|
|
Ok(transcript.text.clone())
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub(crate) fn request_insert_text(
|
|
text: String,
|
|
state: State<'_, ManagedAppState>,
|
|
) -> Result<InsertResult, String> {
|
|
let injector = FallbackInjector;
|
|
let result = injector.insert_text(InsertRequest {
|
|
text,
|
|
target: None,
|
|
method: InsertMethod::ClipboardPaste,
|
|
});
|
|
|
|
state
|
|
.transcript
|
|
.lock()
|
|
.map_err(|_| "falha ao acessar transcrição".to_string())?
|
|
.last_message = Some(result.message.clone());
|
|
|
|
Ok(result)
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub(crate) fn list_models() -> Vec<ModelWithStatus> {
|
|
model_statuses()
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub(crate) fn get_settings(state: State<'_, ManagedAppState>) -> Result<AppSettings, String> {
|
|
state
|
|
.settings
|
|
.lock()
|
|
.map_err(|_| "falha ao acessar configurações".to_string())
|
|
.map(|settings| settings.clone())
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub(crate) fn save_settings(
|
|
settings: AppSettings,
|
|
state: State<'_, ManagedAppState>,
|
|
) -> Result<AppSettings, String> {
|
|
*state
|
|
.settings
|
|
.lock()
|
|
.map_err(|_| "falha ao acessar configurações".to_string())? = settings.clone();
|
|
|
|
Ok(settings)
|
|
}
|