whisper-nvidia-cli 0.1.0

One-shot agent-native local NVIDIA GPU speech-to-text CLI powered by whisper.cpp
Documentation
//! Verb implementations. Each mutating verb echoes target_resolved.
//!
//! The implementations themselves now live in `transcribe` and `verbs`; what
//! stays here is the invocation context they all share and the one function that
//! routes argv to them. The sentence above is kept because the contract it
//! states did not move with the code: it still binds every verb this module
//! dispatches to.

use uuid::Uuid;

use crate::cli::Commands;
use crate::config::{self, Store};
use crate::error::Error;
use crate::i18n::UiLang;
use crate::paths::AppPaths;
use crate::surface::Surface;

/// Shared invocation context.
#[derive(Debug)]
pub struct Ctx<'a> {
    /// Resolved config, cache, and marker locations.
    pub paths: &'a AppPaths,
    /// Persisted configuration already loaded from disk.
    pub store: Store,
    /// Payload reduction knobs parsed from argv.
    pub surface: &'a Surface,
    /// Identifier echoed on every structured line of this invocation.
    pub correlation_id: &'a str,
    /// Language the stderr messages are rendered in.
    pub ui: UiLang,
    /// True when the process must never prompt on stdin.
    pub no_input: bool,
    /// Global refusal of every network call.
    pub offline: bool,
    /// Whole-invocation deadline in seconds, from argv (FR-034).
    ///
    /// `None` means argv said nothing and the persisted ceiling decides.
    pub timeout: Option<u64>,
    /// Set when the verb already wrote the envelope for the error it returns.
    ///
    /// A batch reports each target on its own line and then returns the fatal
    /// condition so the exit code carries it. Without this flag the top level
    /// wrote the same failure a second time, after the summary, so an agent
    /// reading the last NDJSON line found an error where FR-003 promises the
    /// tally. The error still reaches the exit code; only the duplicate line
    /// is suppressed.
    pub error_reported: bool,
}

/// Dispatch a parsed verb.
///
/// Every verb except `transcribe` gets its deadline armed here. Transcription
/// arms its own, because it pads the watchdog with a grace period so the NDJSON
/// summary still reaches stdout before the process is torn down; re-arming it
/// from this level would start a second, unpadded watchdog and make FR-034 fire
/// early on exactly the batch the grace exists to protect.
///
/// Without this, promoting `--timeout` to a global flag would have accepted the
/// argument on `models download` and then ignored it, which is worse than not
/// offering it: the caller believes the invocation is bounded when it is not.
pub fn dispatch(cmd: Commands, ctx: &mut Ctx<'_>) -> Result<(), Error> {
    if !matches!(cmd, Commands::Transcribe(_)) {
        let seconds = ctx
            .timeout
            .unwrap_or(ctx.store.effective().timeout.unwrap_or(0));
        crate::signal::publish_deadline(seconds);
        crate::signal::arm_deadline(seconds, ctx.correlation_id, None);
    }
    match cmd {
        Commands::Transcribe(args) => crate::transcribe::transcribe(ctx, args),
        Commands::Inspect(args) => crate::verbs::inspect(ctx, args),
        Commands::Models { action } => crate::verbs::models(ctx, action),
        Commands::Doctor { device } => crate::verbs::doctor(ctx, device),
        Commands::Config { action } => crate::verbs::config_cmd(ctx, action),
        Commands::Schema { name } => crate::verbs::schema(ctx, name.as_deref()),
        Commands::Commands => crate::verbs::commands_tree(ctx),
        Commands::Version => crate::verbs::version(ctx),
        Commands::Completions { shell } => crate::verbs::completions(shell),
        Commands::Licenses => crate::verbs::licenses(ctx),
        Commands::Locale { action } => crate::verbs::locale_cmd(ctx, action),
    }
}

/// Fresh correlation id (UUIDv7).
pub fn new_correlation_id() -> String {
    Uuid::now_v7().to_string()
}

/// Load store for the path jail.
pub fn load_store(paths: &AppPaths) -> Result<Store, Error> {
    config::load(paths)
}