Skip to content

feat: add file watching for Whisper models and settings #4

@wilcorrea

Description

@wilcorrea

Context

During PR #2 review, CodeRabbit suggested adding notify-based file watching for the Whisper models directory and settings file. This would enable the app to reactively refresh when external changes occur.

Current State

  • ✅ The app already uses notify crate for watching markdown files (lib.rs:88-109)
  • model_manager.rs owns the paths and I/O logic for settings and models
  • ❌ No auto-refresh when models are added/deleted externally
  • ❌ No auto-refresh when settings change externally

Proposed Implementation

Add two watcher helper functions to whisper/model_manager.rs:

1. Watch Models Directory

pub fn watch_models_dir(
    models_dir: &PathBuf, 
    tx: std::sync::mpsc::Sender<notify::Event>
) -> notify::Result<notify::RecommendedWatcher> {
    let mut watcher = notify::recommended_watcher(move |res: Result<Event, notify::Error>| {
        if let Ok(event) = res {
            let _ = tx.send(event);
        }
    })?;
    
    watcher.watch(models_dir, RecursiveMode::Recursive)?;
    Ok(watcher)
}

2. Watch Settings File

pub fn watch_settings_file(
    settings_path: &PathBuf,
    tx: std::sync::mpsc::Sender<notify::Event>
) -> notify::Result<notify::RecommendedWatcher> {
    let mut watcher = notify::recommended_watcher(move |res: Result<Event, notify::Error>| {
        if let Ok(event) = res {
            let _ = tx.send(event);
        }
    })?;
    
    watcher.watch(settings_path, RecursiveMode::NonRecursive)?;
    Ok(watcher)
}

Integration Points

Update lib.rs to:

  1. Create watchers in setup() similar to markdown file watching
  2. Store watchers in app state (e.g., WhisperWatcherState)
  3. Emit events to frontend: whisper:models-changed, whisper:settings-changed

Frontend Reactions

When events are received:

  • models-changed: Refresh model list UI (list_models())
  • settings-changed: Reload settings, update active model/shortcut

Use Cases

  1. Model Management: User downloads model via CLI/script → UI auto-updates
  2. Settings Sync: Settings changed by another app instance → current instance reflects changes
  3. External Tools: Models managed by external tools → app stays in sync

Optional Enhancements

  • Debounce rapid events using notify-debouncer-mini
  • Add #[tauri::command] to manually trigger refresh
  • Add toast notification when external changes detected

Testing Checklist

  • Add model file manually → UI refreshes
  • Delete model file → UI updates list
  • Edit settings file externally → app reflects changes
  • Verify no memory leaks with long-running watchers
  • Test on macOS and Linux (different notify backends)

References

Notes

This aligns with the project's coding guidelines to use notify for file watching and improves UX by making the app reactive to external changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions