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
22 changes: 22 additions & 0 deletions crates/voxctrl-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,16 @@ pub struct FeaturesConfig {
pub show_notification: Option<bool>,
/// Map of trigger → expansion, e.g. {"addr" → "123 Main St"}
pub snippets: std::collections::HashMap<String, String>,
/// Transcribe the opening seconds of a recording while it is still going,
/// so a spoken voice command shows its overlay and starts loading the TTS
/// model before the user releases the hotkey. Costs some extra
/// transcription work at the start of each recording.
#[serde(default = "default_early_command_detection")]
pub early_command_detection: bool,
}

fn default_early_command_detection() -> bool {
true
}

impl Default for FeaturesConfig {
Expand All @@ -297,6 +307,7 @@ impl Default for FeaturesConfig {
auto_format_lists: true,
show_notification: None,
snippets: std::collections::HashMap::new(),
early_command_detection: true,
}
}
}
Expand Down Expand Up @@ -1647,4 +1658,15 @@ mod tests {

serde_json::from_str::<AppConfig>(&with_leftover).expect("older configs must still load");
}

#[test]
fn early_command_detection_defaults_on_for_older_configs() {
use super::FeaturesConfig;
let features: FeaturesConfig = serde_json::from_str(
r#"{"remove_fillers": true, "custom_vocabulary": [], "spoken_punctuation": true,
"auto_format_lists": true, "snippets": {}}"#,
)
.unwrap();
assert!(features.early_command_detection);
}
}
40 changes: 38 additions & 2 deletions crates/voxctrl-inference/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ pub struct InferenceRequest {
pub target_id: String,
/// Hotkey binding ID (if triggered by a hotkey)
pub binding_id: Option<String>,
/// Whether this is an intermediate/live transcription during active speech
pub is_interim: bool,
/// Unique session identifier for the recording turn
pub session_id: u64,
}

/// Final output after transcription + post-processing.
Expand All @@ -239,6 +243,8 @@ pub struct InferenceOutput {
/// Set when transcription failed (model missing, backend error, ...). The
/// UI layer surfaces this to the user; `text` is empty in that case.
pub error: Option<String>,
pub is_interim: bool,
pub session_id: u64,
}

// ── Engine ────────────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -293,6 +299,9 @@ impl InferenceEngine {

/// Transcribe and post-process. Returns the final text.
pub fn process(&self, req: InferenceRequest) -> Result<InferenceOutput> {
let is_interim = req.is_interim;
let session_id = req.session_id;

if req.audio.is_empty() {
return Ok(InferenceOutput {
text: String::new(),
Expand All @@ -302,6 +311,8 @@ impl InferenceEngine {
inference_ms: 0,
language: "en".into(),
error: None,
is_interim,
session_id,
});
}

Expand Down Expand Up @@ -345,6 +356,8 @@ impl InferenceEngine {
inference_ms: 0,
language: "en".into(),
error: None,
is_interim,
session_id,
});
}

Expand Down Expand Up @@ -400,7 +413,10 @@ impl InferenceEngine {
.and_then(|b| b.openai_enabled)
.unwrap_or(false);

if binding_wants_openai && !processed.is_empty() {
// Interim passes only feed the voice-command trigger check and are
// thrown away; running the hotkey's (billed, slow) OpenAI rewrite on
// every one of them would cost a request per pass for nothing.
if binding_wants_openai && !is_interim && !processed.is_empty() {
// Re-read the OpenAI settings from disk so changes made in the
// Settings UI (model, endpoint, API key, prompts) take effect without
// restarting the app. Targets and bindings above are already hot-read
Expand Down Expand Up @@ -477,6 +493,8 @@ impl InferenceEngine {
inference_ms: result.inference_ms,
language: result.language,
error: None,
is_interim,
session_id,
})
}

Expand Down Expand Up @@ -616,11 +634,25 @@ pub fn run_worker_with_config(
loop {
crossbeam_channel::select! {
recv(rx) -> req_res => {
let req = match req_res {
let mut req = match req_res {
Ok(r) => r,
Err(_) => break,
};

// If an interim request arrived, drain any newer requests already in the queue
// to prioritize fresher audio or the final release request.
if req.is_interim {
while let Ok(newer) = rx.try_recv() {
req = newer;
if !req.is_interim {
break;
}
}
}

let is_interim = req.is_interim;
let session_id = req.session_id;

if !loaded {
match engine.load() {
Ok(()) => {
Expand All @@ -637,6 +669,8 @@ pub fn run_worker_with_config(
inference_ms: 0,
language: String::new(),
error: Some(format!("{e:#}")),
is_interim,
session_id,
});
continue;
}
Expand All @@ -657,6 +691,8 @@ pub fn run_worker_with_config(
inference_ms: 0,
language: "".to_string(),
error: Some(format!("{e:#}")),
is_interim,
session_id,
});
}
}
Expand Down
15 changes: 15 additions & 0 deletions crates/voxctrl-routing/src/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ pub fn notify_command_trigger(command_name: &str, text_summary: &str) {
}
}

pub type CommandWithdrawnCallback = Arc<dyn Fn() + Send + Sync + 'static>;
static COMMAND_WITHDRAWN_CALLBACK: OnceLock<CommandWithdrawnCallback> = OnceLock::new();

pub fn set_command_withdrawn_callback(callback: CommandWithdrawnCallback) {
let _ = COMMAND_WITHDRAWN_CALLBACK.set(callback);
}

/// Take back a command announced early (from a partial transcript) that the
/// final transcript did not confirm, so its overlay does not linger.
pub fn notify_command_withdrawn() {
if let Some(cb) = COMMAND_WITHDRAWN_CALLBACK.get() {
cb();
}
}

// Shared HTTP client — built once, reused for connection pooling.
fn http_client() -> &'static reqwest::Client {
static CLIENT: std::sync::OnceLock<reqwest::Client> = std::sync::OnceLock::new();
Expand Down
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ interface FeaturesConfig {
spoken_punctuation: boolean;
auto_format_lists: boolean;
snippets: Record<string, string>;
early_command_detection: boolean;
}

interface OpenAiConfig {
Expand Down
4 changes: 3 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ Full schema with defaults:
"custom_vocabulary": [],
"spoken_punctuation": true,
"auto_format_lists": true,
"snippets": {}
"snippets": {},
"early_command_detection": true
},
"openai": {
"enabled": false,
Expand Down Expand Up @@ -237,6 +238,7 @@ The `.en` variants are English-only but slightly faster. `large-v3-turbo` is a d
| `auto_format_lists` | bool | `true` | Detect "first/second/third" patterns and reformat as a numbered list |
| `custom_vocabulary` | string[] | `[]` | Custom words; VoxCtrl uses fuzzy Levenshtein matching to correct near-matches post-transcription |
| `snippets` | object | `{}` | Short code → expansion map |
| `early_command_detection` | bool | `true` | Transcribe the first 6 seconds of a recording while it is still going, so a voice command shows its overlay and starts loading the TTS model before the hotkey is released. Routing is still decided by the final transcript. Skipped for the remote backend and CPU-only medium/large Whisper models |

Example with snippets:
```json
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ pub fn run() {
router: router.clone(),
recording: Arc::new(AtomicBool::new(false)),
processing: Arc::new(AtomicBool::new(false)),
interim_in_flight: Arc::new(AtomicBool::new(false)),
speaking: Arc::new(AtomicBool::new(false)),
overlay_enabled: Arc::new(AtomicBool::new(cfg_data.ui.show_overlay)),
mcp_recording: Arc::new(AtomicBool::new(false)),
Expand Down
Loading
Loading