Skip to main content

clawft_plugin/voice/
models.rs

1//! Model download and cache management.
2//!
3//! Downloads STT/TTS/VAD models, verifies SHA-256 integrity,
4//! and caches them in the local filesystem.
5
6use std::path::PathBuf;
7
8/// Manages voice model downloads, caching, and integrity verification.
9pub struct ModelDownloadManager {
10    cache_dir: PathBuf,
11}
12
13/// Information about a downloadable model.
14#[derive(Debug, Clone)]
15pub struct ModelInfo {
16    /// Model identifier (e.g., "sherpa-onnx-streaming-zipformer-en-20M").
17    pub id: String,
18    /// Download URL.
19    pub url: String,
20    /// Expected SHA-256 hash of the downloaded file.
21    pub sha256: String,
22    /// File size in bytes (for progress reporting).
23    pub size_bytes: u64,
24}
25
26impl ModelDownloadManager {
27    /// Create a new manager with the given cache directory.
28    pub fn new(cache_dir: PathBuf) -> Self {
29        Self { cache_dir }
30    }
31
32    /// Get the cache directory path.
33    pub fn cache_dir(&self) -> &PathBuf {
34        &self.cache_dir
35    }
36
37    /// Check if a model is already cached and valid.
38    pub fn is_cached(&self, model: &ModelInfo) -> bool {
39        let model_path = self.cache_dir.join(&model.id);
40        model_path.exists()
41    }
42
43    /// Get the local path where a model would be cached.
44    pub fn model_path(&self, model_id: &str) -> PathBuf {
45        self.cache_dir.join(model_id)
46    }
47
48    /// List available STT models.
49    pub fn available_stt_models() -> Vec<ModelInfo> {
50        vec![
51            ModelInfo {
52                id: "sherpa-onnx-streaming-zipformer-en-20M".into(),
53                url: "https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-en-20M.tar.bz2".into(),
54                sha256: "PLACEHOLDER_SHA256_STT".into(),
55                size_bytes: 20_000_000,
56            },
57        ]
58    }
59
60    /// List available TTS models.
61    pub fn available_tts_models() -> Vec<ModelInfo> {
62        vec![
63            ModelInfo {
64                id: "vits-piper-en_US-amy-medium".into(),
65                url: "https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-piper-en_US-amy-medium.tar.bz2".into(),
66                sha256: "PLACEHOLDER_SHA256_TTS".into(),
67                size_bytes: 40_000_000,
68            },
69        ]
70    }
71
72    /// List available VAD models.
73    pub fn available_vad_models() -> Vec<ModelInfo> {
74        vec![
75            ModelInfo {
76                id: "silero-vad-v5".into(),
77                url: "https://github.com/snakers4/silero-vad/raw/master/files/silero_vad.onnx".into(),
78                sha256: "PLACEHOLDER_SHA256_VAD".into(),
79                size_bytes: 2_000_000,
80            },
81        ]
82    }
83}