From de504fbcb4baa741afcc2f6d40e70b9ba5adfb19 Mon Sep 17 00:00:00 2001 From: "michael.borak" Date: Sun, 25 Jan 2026 16:37:58 +0100 Subject: [PATCH] feat: improve audio exclusion with smart matching and reliability fixes --- README.md | 23 +- package-lock.json | 4 +- package.json | 2 +- src-tauri/Cargo.lock | 32 ++ src-tauri/Cargo.toml | 3 + src-tauri/src/apps.rs | 65 +++ src-tauri/src/lib.rs | 806 ++++++++++++++++++-------- src-tauri/src/lib.rs.bak | 984 ++++++++++++++++++++++++++++++++ src-tauri/src/sc_audio.rs | 138 +++-- src-tauri/src/sc_audio.rs.bak | 103 ++++ src-tauri/tauri.conf.json | 2 +- src/App.tsx | 12 +- src/components/Recorder.tsx | 6 +- src/components/Settings.tsx | 130 ++++- src/components/Settings.tsx.bak | 716 +++++++++++++++++++++++ 15 files changed, 2720 insertions(+), 306 deletions(-) create mode 100644 src-tauri/src/apps.rs create mode 100644 src-tauri/src/lib.rs.bak create mode 100644 src-tauri/src/sc_audio.rs.bak create mode 100644 src/components/Settings.tsx.bak diff --git a/README.md b/README.md index 9e6f40f..d62f9e0 100644 --- a/README.md +++ b/README.md @@ -4,16 +4,21 @@ ![App Icon](src-tauri/icons/128x128@2x.png) -## ✨ New in v1.2.0 +## ✨ New in v1.2.x -* **🎧 Native System Audio**: No more BlackHole driver needed! Captures Teams, Zoom, 3CX, and Talk audio directly and securely via macOS ScreenCaptureKit. -* **πŸ” Auto-Loop (Standby Mode)**: The app automatically "re-arms" after a call finishes. Just leave it runningβ€”it will wake up, record your next call, and go back to sleep. -* **⚑ Smart VAD & Pre-Roll**: - * **3-Second Pre-Roll**: Catches the start of the sentence even if you speak before the trigger. - * **Noise Filtering**: Ignores typing and background noise. -* **πŸ›‘οΈ Empty Audio Guard**: Automatically discards silent recordings (e.g., false triggers) to save API costs and prevent errors. -* **✨ 48kHz Crystal Clear Audio**: Optimized audio pipeline prevents "robot voice" distortion. -* **πŸ’Ύ Daily Security Backups**: Automatically saves your entire history as a standard JSON file every 24 hours (unencrypted for easy recovery). +### v1.2.3: App Blocking & Selective Recording 🚫🎧 +* **πŸ”‡ App Blocking (Selective Recording)**: You can now exclude specific applications from being captured by the system audio recording. +* **🧠 Smart Matching**: Uses prefix-based matching (e.g., `com.apple.Safari`) to reliably block main apps AND their background/helper processes (e.g., "Safari Web Content"). +* **πŸš€ Quick Select Presets**: Quickly block common background apps like **Spotify, Apple Music, VLC, Safari, or Chrome** with one click. +* **πŸ” Live App List**: Filter any currently running application directly from the new **Apps** tab in Settings. +* **πŸ”„ Robust Auto-Refresh**: The capture filter automatically updates every 5 seconds to account for newly opened browsers or media players. + +### v1.2.0: Native Audio & Auto-Loop πŸ¦‰ +* **🎧 Native System Audio**: No more BlackHole driver needed! Captures Teams, Zoom, 3CX, and Talk audio directly via macOS ScreenCaptureKit. +* **πŸ” Auto-Loop (Standby Mode)**: Automatically "re-arms" after a call finishes. +* **⚑ Smart VAD & Pre-Roll**: 3-second pre-roll and noise filtering. +* **πŸ›‘οΈ Empty Audio Guard**: Automatically discards silent recordings. +* **πŸ’Ύ Daily Security Backups**: Automatic JSON history backups every 24 hours. ## πŸš€ Key Features diff --git a/package-lock.json b/package-lock.json index 302e076..0c1b2ee 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "hearbit-ai", - "version": "1.1.1", + "version": "1.2.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "hearbit-ai", - "version": "1.1.1", + "version": "1.2.2", "dependencies": { "@tailwindcss/postcss": "^4.1.18", "@tauri-apps/api": "^2", diff --git a/package.json b/package.json index add2c47..e952d7c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "hearbit-ai", "private": true, - "version": "1.2.1", + "version": "1.2.3", "type": "module", "scripts": { "dev": "vite", diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 77817d9..891962a 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -629,6 +629,35 @@ dependencies = [ "stacker", ] +[[package]] +name = "cocoa" +version = "0.26.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad36507aeb7e16159dfe68db81ccc27571c3ccd4b76fb2fb72fc59e7a4b1b64c" +dependencies = [ + "bitflags 2.10.0", + "block", + "cocoa-foundation", + "core-foundation 0.10.1", + "core-graphics", + "foreign-types 0.5.0", + "libc", + "objc", +] + +[[package]] +name = "cocoa-foundation" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81411967c50ee9a1fc11365f8c585f863a22a9697c89239c452292c40ba79b0d" +dependencies = [ + "bitflags 2.10.0", + "block", + "core-foundation 0.10.1", + "core-graphics-types", + "objc", +] + [[package]] name = "combine" version = "4.6.7" @@ -1749,10 +1778,13 @@ version = "1.2.1" dependencies = [ "base64 0.22.1", "chrono", + "cocoa", + "core-foundation 0.10.1", "cpal", "hound", "lettre", "oauth2", + "objc", "reqwest 0.11.27", "rubato", "screencapturekit", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 2366d92..68d8892 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -40,3 +40,6 @@ tauri-plugin-shell = "2.3.4" base64 = "0.22" screencapturekit = "0.2.0" screencapturekit-sys = "0.2.8" +cocoa = "0.26.1" +objc = "0.2.7" +core-foundation = "0.10.1" diff --git a/src-tauri/src/apps.rs b/src-tauri/src/apps.rs new file mode 100644 index 0000000..b0eba39 --- /dev/null +++ b/src-tauri/src/apps.rs @@ -0,0 +1,65 @@ +use cocoa::base::{id, nil}; +use cocoa::foundation::{NSArray, NSAutoreleasePool, NSString}; +use objc::{msg_send, sel, sel_impl}; +use serde::Serialize; + +#[derive(Serialize, Debug)] +pub struct RunningApp { + pub name: String, + pub bundle_id: String, +} + +pub fn get_running_applications() -> Vec { + let mut apps = Vec::new(); + + unsafe { + let pool = NSAutoreleasePool::new(nil); + + // [NSWorkspace sharedWorkspace] + let workspace_class = objc::runtime::Class::get("NSWorkspace").unwrap(); + let shared_workspace: id = msg_send![workspace_class, sharedWorkspace]; + + // [sharedWorkspace runningApplications] + let running_apps: id = msg_send![shared_workspace, runningApplications]; + let count: usize = msg_send![running_apps, count]; + + for i in 0..count { + let app: id = msg_send![running_apps, objectAtIndex: i]; + + // Check if it's a regular GUI application + // activationPolicy: 0 = regular, 1 = accessory, 2 = prohibited + let policy: isize = msg_send![app, activationPolicy]; + if policy != 0 { + continue; + } + + let bundle_id_ns: id = msg_send![app, bundleIdentifier]; + let name_ns: id = msg_send![app, localizedName]; + + if bundle_id_ns != nil && name_ns != nil { + let bundle_id = nsstring_to_string(bundle_id_ns); + let name = nsstring_to_string(name_ns); + + apps.push(RunningApp { name, bundle_id }); + } + } + + // We don't drain the pool here as it might be handled by Tauri's main loop if we are called frequently, + // but for a one-off command it's safer. + let _: () = msg_send![pool, release]; + } + + // Sort by name for better UI experience + apps.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase())); + apps +} + +unsafe fn nsstring_to_string(ns_string: id) -> String { + let char_ptr: *const std::os::raw::c_char = msg_send![ns_string, UTF8String]; + if char_ptr.is_null() { + return String::new(); + } + std::ffi::CStr::from_ptr(char_ptr) + .to_string_lossy() + .into_owned() +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index e3f1c7b..3ed3ccc 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,18 +1,18 @@ +use base64::Engine; +use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; +use std::process::Command; +use std::sync::{Arc, Mutex}; +use std::time::Duration; use tauri::{ - AppHandle, Manager, State, Emitter, menu::{Menu, MenuItem}, tray::{TrayIconBuilder, TrayIconEvent}, - WindowEvent + AppHandle, Emitter, Manager, State, WindowEvent, }; -use std::sync::{Arc, Mutex}; -use std::process::Command; -use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; -use std::time::Duration; use tokio::time::sleep; -use base64::Engine; mod audio_processor; use audio_processor::AudioProcessor; +mod apps; mod auth; mod email; mod sc_audio; @@ -21,7 +21,7 @@ mod sc_audio; struct AppState { recording_stream: Mutex>, recording_file_path: Mutex>, - system_capture: Mutex>, + system_capture: tokio::sync::Mutex>, } #[derive(serde::Serialize)] @@ -55,7 +55,7 @@ fn greet(name: &str) -> String { fn get_input_devices() -> Result, String> { let host = cpal::default_host(); let devices = host.input_devices().map_err(|e| e.to_string())?; - + let mut result = Vec::new(); for device in devices { #[allow(deprecated)] @@ -70,16 +70,31 @@ fn get_input_devices() -> Result, String> { Ok(result) } - - #[tauri::command] -async fn start_recording(app: AppHandle, state: State<'_, AppState>, device_id: String, save_path: Option, custom_filename: Option, wait_for_speech: Option, mode: String) -> Result<(), String> { - emit_log(&app, "INFO", &format!("Starting recording [Mode: {}] on device: {}", mode, device_id)); +async fn start_recording( + app: AppHandle, + state: State<'_, AppState>, + device_id: String, + save_path: Option, + custom_filename: Option, + wait_for_speech: Option, + mode: String, + excluded_apps: Option>, +) -> Result<(), String> { + emit_log( + &app, + "INFO", + &format!( + "Starting recording [Mode: {}] on device: {}", + mode, device_id + ), + ); let host = cpal::default_host(); - + // Find device by name (using name as ID) #[allow(deprecated)] - let device = host.input_devices() + let device = host + .input_devices() .map_err(|e| e.to_string())? .find(|d| d.name().map(|n| n == device_id).unwrap_or(false)) .or_else(|| host.default_input_device()) @@ -89,22 +104,35 @@ async fn start_recording(app: AppHandle, state: State<'_, AppState>, device_id: // This is crucial for "Hearbit Audio" (Aggregate) which lists 3 channels but might default to 2. // Select Audio Configuration // We prioritize 48kHz because System Audio (ScreenCaptureKit) acts best at 48k. - let supported_configs: Vec<_> = device.supported_input_configs().map_err(|e| e.to_string())?.collect(); - + let supported_configs: Vec<_> = device + .supported_input_configs() + .map_err(|e| e.to_string())? + .collect(); + // Try to find 48kHz specifically // Note: cpal::SampleRate is likely a type alias for u32 here, so we pass 48000 directly. - let config = supported_configs.iter() + let config = supported_configs + .iter() .find(|c| c.min_sample_rate() <= 48000 && c.max_sample_rate() >= 48000) .map(|c| c.with_sample_rate(48000)) .or_else(|| { // Fallback: Max sample rate - supported_configs.iter() + supported_configs + .iter() .max_by_key(|c| c.channels()) .map(|c| c.with_max_sample_rate()) }) .ok_or("No supported input configurations found")?; - - emit_log(&app, "INFO", &format!("Selected Audio Config: {} Channels, {} Hz", config.channels(), config.sample_rate())); + + emit_log( + &app, + "INFO", + &format!( + "Selected Audio Config: {} Channels, {} Hz", + config.channels(), + config.sample_rate() + ), + ); let spec = hound::WavSpec { channels: config.channels(), @@ -116,39 +144,76 @@ async fn start_recording(app: AppHandle, state: State<'_, AppState>, device_id: // Determine file path: User provided or Temp let filename = if let Some(name) = custom_filename { // Sanitize filename - let safe_name: String = name.chars().map(|x| if x.is_alphanumeric() || x == ' ' || x == '-' || x == '_' { x } else { '_' }).collect(); + let safe_name: String = name + .chars() + .map(|x| { + if x.is_alphanumeric() || x == ' ' || x == '-' || x == '_' { + x + } else { + '_' + } + }) + .collect(); format!("{}.wav", safe_name) } else { - format!("recording_{}.wav", std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs()) + format!( + "recording_{}.wav", + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs() + ) }; let file_path = if let Some(path) = save_path { if path.trim().is_empty() { - std::env::temp_dir().join(&filename) + std::env::temp_dir().join(&filename) } else { - std::path::PathBuf::from(path).join(&filename) + std::path::PathBuf::from(path).join(&filename) } } else { - std::env::temp_dir().join(&filename) + std::env::temp_dir().join(&filename) }; - + let file_path_str = file_path.to_string_lossy().to_string(); - emit_log(&app, "INFO", &format!("Saving recording to: {}", file_path_str)); + emit_log( + &app, + "INFO", + &format!("Saving recording to: {}", file_path_str), + ); let writer = hound::WavWriter::create(&file_path, spec).map_err(|e| e.to_string())?; let writer = Arc::new(Mutex::new(writer)); let writer_clone = writer.clone(); - + // Initialize AudioProcessor (VAD) // We pass the writer to it. let should_wait = wait_for_speech.unwrap_or(false); if should_wait { - emit_log(&app, "INFO", &format!("Recording started in WAITING mode (Trigger: {}).", if mode == "voice" { "Speech" } else { "System Audio" })); + emit_log( + &app, + "INFO", + &format!( + "Recording started in WAITING mode (Trigger: {}).", + if mode == "voice" { + "Speech" + } else { + "System Audio" + } + ), + ); } - let processor = AudioProcessor::new(config.sample_rate(), config.channels(), writer.clone(), app.clone(), should_wait, mode) - .map_err(|e| format!("Failed to create AudioProcessor: {}", e))?; - + let processor = AudioProcessor::new( + config.sample_rate(), + config.channels(), + writer.clone(), + app.clone(), + should_wait, + mode, + ) + .map_err(|e| format!("Failed to create AudioProcessor: {}", e))?; + // Wrap processor in Arc so we can share/move it into callback // Actually, cpal callback takes ownership of its closure state usually if 'move'. // Since stream is on another thread, we need Send. AudioProcessor should be Send. @@ -158,15 +223,16 @@ async fn start_recording(app: AppHandle, state: State<'_, AppState>, device_id: let processor_clone = processor.clone(); // --- SYSTEM AUDIO CAPTURE START --- - // Prevent Doubling: If user selected an aggregate device (Hearbit Audio/BlackHole), + // Prevent Doubling: If user selected an aggregate device (Hearbit Audio/BlackHole), // it ALREADY contains system audio. In that case, we don't need internal SCK capture. let is_aggregate = device_id.contains("Hearbit") || device_id.contains("BlackHole"); - + if is_aggregate { emit_log(&app, "INFO", "Aggregate device detected. Disabling internal System Audio Capture to prevent doubling."); } else { - let mut sys_capture = sc_audio::SystemAudioCapture::new(config.sample_rate()); - + let excluded = excluded_apps.unwrap_or_default(); + let mut sys_capture = sc_audio::SystemAudioCapture::new(config.sample_rate(), excluded); + // Get the queue to share with the capture callback let queue_clone = { let p = processor.lock().unwrap(); @@ -177,20 +243,45 @@ async fn start_recording(app: AppHandle, state: State<'_, AppState>, device_id: // Push to queue if let Ok(mut q) = queue_clone.lock() { q.extend(data.iter()); - + // Limit queue size to avoid memory leaks if main process loop is slow - while q.len() > 48000 * 5 { // 5 seconds buffer - q.pop_front(); + while q.len() > 48000 * 5 { + // 5 seconds buffer + q.pop_front(); } } }; match sys_capture.start(sys_callback).await { Ok(_) => emit_log(&app, "INFO", "System Audio Capture started."), - Err(e) => emit_log(&app, "WARN", &format!("System Audio Capture failed (Permissions?): {}", e)), + Err(e) => emit_log( + &app, + "WARN", + &format!("System Audio Capture failed (Permissions?): {}", e), + ), } - - *state.system_capture.lock().unwrap() = Some(sys_capture); + + *state.system_capture.lock().await = Some(sys_capture); + + // Spawn background task to periodically refresh the filter (handles apps opened after recording starts) + let app_handle = app.clone(); + tauri::async_runtime::spawn(async move { + loop { + tokio::time::sleep(tokio::time::Duration::from_secs(5)).await; + let state = app_handle.state::(); + let mut guard = state.system_capture.lock().await; + if let Some(sys) = guard.as_mut() { + // Try to refresh filter + if let Err(e) = sys.refresh_filter().await { + // Just log as debug/warn, not critical if it fails once + eprintln!("Failed to refresh audio filter: {}", e); + } + } else { + // Recording stopped + break; + } + } + }); } // --- SYSTEM AUDIO CAPTURE END --- @@ -209,35 +300,39 @@ async fn start_recording(app: AppHandle, state: State<'_, AppState>, device_id: } }, err_fn, - None + None, ), // For I16 and U16 we need to convert to F32 for our processor cpal::SampleFormat::I16 => device.build_input_stream( &config.into(), move |data: &[i16], _: &_| { - // Convert i16 to f32 - let f32_data: Vec = data.iter().map(|&s| s as f32 / i16::MAX as f32).collect(); - if let Ok(mut p) = processor_clone.lock() { - p.process(&f32_data); - } - }, - err_fn, - None - ), - cpal::SampleFormat::U16 => device.build_input_stream( - &config.into(), - move |data: &[u16], _: &_| { - // Convert u16 to f32 - let f32_data: Vec = data.iter().map(|&s| (s as i32 - 32768) as f32 / 32768.0).collect(); + // Convert i16 to f32 + let f32_data: Vec = data.iter().map(|&s| s as f32 / i16::MAX as f32).collect(); if let Ok(mut p) = processor_clone.lock() { p.process(&f32_data); } }, err_fn, - None + None, + ), + cpal::SampleFormat::U16 => device.build_input_stream( + &config.into(), + move |data: &[u16], _: &_| { + // Convert u16 to f32 + let f32_data: Vec = data + .iter() + .map(|&s| (s as i32 - 32768) as f32 / 32768.0) + .collect(); + if let Ok(mut p) = processor_clone.lock() { + p.process(&f32_data); + } + }, + err_fn, + None, ), _ => return Err("Unsupported sample format".to_string()), - }.map_err(|e| e.to_string())?; + } + .map_err(|e| e.to_string())?; stream.play().map_err(|e| e.to_string())?; @@ -245,33 +340,43 @@ async fn start_recording(app: AppHandle, state: State<'_, AppState>, device_id: *state.recording_stream.lock().unwrap() = Some(stream); *state.recording_file_path.lock().unwrap() = Some(file_path_str.clone()); - emit_log(&app, "SUCCESS", &format!("Recording started. File: {}", file_path_str)); + emit_log( + &app, + "SUCCESS", + &format!("Recording started. File: {}", file_path_str), + ); Ok(()) } #[tauri::command] -fn stop_recording(app: AppHandle, state: State<'_, AppState>) -> Result { +async fn stop_recording(app: AppHandle, state: State<'_, AppState>) -> Result { emit_log(&app, "INFO", "Stopping recording..."); // Drop stream to stop recording { - let mut stream_guard = state.recording_stream.lock().unwrap(); // Also stop System Capture - let mut sys_guard = state.system_capture.lock().unwrap(); - if let Some(sys) = sys_guard.as_mut() { - sys.stop(); + let mut sys_guard = state.system_capture.lock().await; + if let Some(mut sys) = sys_guard.take() { + sys.stop(); } - *sys_guard = None; + + let mut stream_guard = state.recording_stream.lock().unwrap(); if stream_guard.is_none() { return Err("Not recording".to_string()); } *stream_guard = None; // This drops the stream and stops recording } - + // Return file path let mut path_guard = state.recording_file_path.lock().unwrap(); - let path = path_guard.take().ok_or("No recording path found".to_string())?; - emit_log(&app, "SUCCESS", &format!("Recording stopped. Saved to: {}", path)); + let path = path_guard + .take() + .ok_or("No recording path found".to_string())?; + emit_log( + &app, + "SUCCESS", + &format!("Recording stopped. Saved to: {}", path), + ); Ok(path) } @@ -341,15 +446,23 @@ struct ModelInfo { } #[tauri::command] -async fn get_available_models(app: AppHandle, api_key: String, product_id: String) -> Result, String> { +async fn get_available_models( + app: AppHandle, + api_key: String, + product_id: String, +) -> Result, String> { emit_log(&app, "INFO", "Fetching available models from Infomaniak..."); let client = reqwest::Client::new(); // Use the v2/openai compliant endpoint as per docs - let url = format!("https://api.infomaniak.com/2/ai/{}/openai/v1/models", product_id); + let url = format!( + "https://api.infomaniak.com/2/ai/{}/openai/v1/models", + product_id + ); emit_log(&app, "DEBUG", &format!("GET {}", url)); - let res = client.get(&url) + let res = client + .get(&url) .header("Authorization", format!("Bearer {}", api_key)) .send() .await @@ -365,23 +478,29 @@ async fn get_available_models(app: AppHandle, api_key: String, product_id: Strin let list: ModelListResponse = serde_json::from_str(&raw_body) .map_err(|e| format!("Failed to parse models: {}. Body: {}", e, raw_body))?; - let models = list.data.into_iter() - .filter(|m| !m.id.to_lowercase().contains("mini_lm") && !m.id.to_lowercase().contains("bert") && !m.id.to_lowercase().contains("embedding")) + let models = list + .data + .into_iter() + .filter(|m| { + !m.id.to_lowercase().contains("mini_lm") + && !m.id.to_lowercase().contains("bert") + && !m.id.to_lowercase().contains("embedding") + }) .map(|m| ModelInfo { - id: m.id.clone(), - name: m.id, // Use ID as name for now, or fetch more details if available - }).collect::>(); - + id: m.id.clone(), + name: m.id, // Use ID as name for now, or fetch more details if available + }) + .collect::>(); + emit_log(&app, "SUCCESS", &format!("Loaded {} models.", models.len())); Ok(models) } else { - let err = res.text().await.unwrap_or_default(); - emit_log(&app, "ERROR", &format!("Failed to fetch models: {}", err)); - Err(format!("Failed to fetch models: {}", err)) + let err = res.text().await.unwrap_or_default(); + emit_log(&app, "ERROR", &format!("Failed to fetch models: {}", err)); + Err(format!("Failed to fetch models: {}", err)) } } - #[derive(serde::Deserialize)] struct WhisperVerboseResponse { text: Option, @@ -396,15 +515,20 @@ struct Segment { } #[tauri::command] -async fn transcribe_audio(app: AppHandle, file_path: String, api_key: String, product_id: String) -> Result { +async fn transcribe_audio( + app: AppHandle, + file_path: String, + api_key: String, + product_id: String, +) -> Result { emit_log(&app, "INFO", "Starting transcription with timestamps..."); let client = reqwest::Client::new(); - + // Prepare file part let file_bytes = std::fs::read(&file_path).map_err(|e| e.to_string())?; // We must use a known file name for the part, Infomaniak might care, or not. let file_part = reqwest::multipart::Part::bytes(file_bytes) - .file_name("recording.wav") + .file_name("recording.wav") .mime_str("audio/wav") .map_err(|e| e.to_string())?; @@ -414,11 +538,15 @@ async fn transcribe_audio(app: AppHandle, file_path: String, api_key: String, pr .text("response_format", "verbose_json") .text("timestamp_granularities[]", "segment"); // Crucial for accurate segments - let url = format!("https://api.infomaniak.com/1/ai/{}/openai/audio/transcriptions", product_id); - + let url = format!( + "https://api.infomaniak.com/1/ai/{}/openai/audio/transcriptions", + product_id + ); + emit_log(&app, "DEBUG", &format!("POST {}", url)); - let res = client.post(&url) + let res = client + .post(&url) .header("Authorization", format!("Bearer {}", api_key)) .multipart(form) .send() @@ -431,31 +559,43 @@ async fn transcribe_audio(app: AppHandle, file_path: String, api_key: String, pr if res.status().is_success() { let raw_body = res.text().await.map_err(|e| e.to_string())?; - + // Check if we got a batch ID #[derive(serde::Deserialize)] struct BatchResponse { batch_id: Option, } - + // Try parsing as batch response first (Infomaniak specific behavior) if let Ok(batch_res) = serde_json::from_str::(&raw_body) { - if let Some(batch_id) = batch_res.batch_id { - emit_log(&app, "INFO", &format!("Transcription queued. Batch ID: {}", batch_id)); - return poll_transcription(&app, &client, &api_key, &product_id, &batch_id).await; - } + if let Some(batch_id) = batch_res.batch_id { + emit_log( + &app, + "INFO", + &format!("Transcription queued. Batch ID: {}", batch_id), + ); + return poll_transcription(&app, &client, &api_key, &product_id, &batch_id).await; + } } // If not batch, try parsing verbose response directly // Log the raw body so we can see why it fails - emit_log(&app, "DEBUG", &format!("Direct Response (first 500 chars): {:.500}", raw_body)); + emit_log( + &app, + "DEBUG", + &format!("Direct Response (first 500 chars): {:.500}", raw_body), + ); let response: WhisperVerboseResponse = serde_json::from_str(&raw_body) .map_err(|e| format!("Failed to decode JSON: {}. Body: {}", e, raw_body))?; if let Some(segments) = response.segments { - emit_log(&app, "INFO", &format!("Found {} segments (Direct).", segments.len())); - for (i, seg) in segments.iter().take(3).enumerate() { + emit_log( + &app, + "INFO", + &format!("Found {} segments (Direct).", segments.len()), + ); + for (i, seg) in segments.iter().take(3).enumerate() { emit_log(&app, "DEBUG", &format!("Seg {}: start={}", i, seg.start)); } @@ -464,9 +604,14 @@ async fn transcribe_audio(app: AppHandle, file_path: String, api_key: String, pr for segment in segments { let start_mins = (segment.start / 60.0).floor() as u64; let start_secs = (segment.start % 60.0).floor() as u64; - formatted_transcript.push_str(&format!("[{:02}:{:02}] {}\n", start_mins, start_secs, segment.text.trim())); + formatted_transcript.push_str(&format!( + "[{:02}:{:02}] {}\n", + start_mins, + start_secs, + segment.text.trim() + )); } - + // Fallback to raw text if segments empty if formatted_transcript.trim().is_empty() { if let Some(text) = response.text { @@ -478,93 +623,153 @@ async fn transcribe_audio(app: AppHandle, file_path: String, api_key: String, pr return Ok(formatted_transcript); } } else if let Some(text) = response.text { - emit_log(&app, "SUCCESS", "Segments missing, using raw text."); - return Ok(text); + emit_log(&app, "SUCCESS", "Segments missing, using raw text."); + return Ok(text); } emit_log(&app, "ERROR", "Response contained no recognized content."); - Err(format!("Response contained no recognized content. Body: {}", raw_body)) + Err(format!( + "Response contained no recognized content. Body: {}", + raw_body + )) } else { let error_text = res.text().await.unwrap_or_default(); - emit_log(&app, "ERROR", &format!("Transcription failed: {}", error_text)); + emit_log( + &app, + "ERROR", + &format!("Transcription failed: {}", error_text), + ); Err(format!("Transcription failed: {}", error_text)) } } -async fn poll_transcription(app: &AppHandle, client: &reqwest::Client, api_key: &str, product_id: &str, batch_id: &str) -> Result { - let status_url = format!("https://api.infomaniak.com/1/ai/{}/results/{}", product_id, batch_id); - +async fn poll_transcription( + app: &AppHandle, + client: &reqwest::Client, + api_key: &str, + product_id: &str, + batch_id: &str, +) -> Result { + let status_url = format!( + "https://api.infomaniak.com/1/ai/{}/results/{}", + product_id, batch_id + ); + let mut attempts = 0; - while attempts < 40 { // 40 * 2s = 80s timeout + while attempts < 40 { + // 40 * 2s = 80s timeout attempts += 1; sleep(Duration::from_secs(2)).await; - - emit_log(app, "DEBUG", &format!("Polling status... Attempt {}", attempts)); - let res = client.get(&status_url) - .header("Authorization", format!("Bearer {}", api_key)) - .send() - .await - .map_err(|e| format!("Polling error: {}", e))?; - + + emit_log( + app, + "DEBUG", + &format!("Polling status... Attempt {}", attempts), + ); + let res = client + .get(&status_url) + .header("Authorization", format!("Bearer {}", api_key)) + .send() + .await + .map_err(|e| format!("Polling error: {}", e))?; + if res.status().is_success() { let json: serde_json::Value = res.json().await.map_err(|e| e.to_string())?; // Check 'status' if let Some(status) = json.get("status").and_then(|s| s.as_str()) { if status == "success" { // Download the result - let download_url = format!("https://api.infomaniak.com/1/ai/{}/results/{}/download", product_id, batch_id); - let dl_res = client.get(&download_url) - .header("Authorization", format!("Bearer {}", api_key)) - .send() - .await - .map_err(|e| e.to_string())?; - - if dl_res.status().is_success() { - let content = dl_res.text().await.map_err(|e| e.to_string())?; - emit_log(app, "DEBUG", &format!("Poll Raw Content (first 500 chars): {:.500}", content)); + let download_url = format!( + "https://api.infomaniak.com/1/ai/{}/results/{}/download", + product_id, batch_id + ); + let dl_res = client + .get(&download_url) + .header("Authorization", format!("Bearer {}", api_key)) + .send() + .await + .map_err(|e| e.to_string())?; - // Try to parse as Verbose JSON to get timestamps - if let Ok(response) = serde_json::from_str::(&content) { + if dl_res.status().is_success() { + let content = dl_res.text().await.map_err(|e| e.to_string())?; + emit_log( + app, + "DEBUG", + &format!("Poll Raw Content (first 500 chars): {:.500}", content), + ); + + // Try to parse as Verbose JSON to get timestamps + if let Ok(response) = + serde_json::from_str::(&content) + { if let Some(segments) = response.segments { - emit_log(app, "INFO", &format!("Found {} segments.", segments.len())); + emit_log( + app, + "INFO", + &format!("Found {} segments.", segments.len()), + ); // Log first 3 segments start times for (i, seg) in segments.iter().take(3).enumerate() { - emit_log(app, "DEBUG", &format!("Seg {}: start={}", i, seg.start)); + emit_log( + app, + "DEBUG", + &format!("Seg {}: start={}", i, seg.start), + ); } let mut formatted_transcript = String::new(); for segment in segments { let start_mins = (segment.start / 60.0).floor() as u64; let start_secs = (segment.start % 60.0).floor() as u64; - formatted_transcript.push_str(&format!("[{:02}:{:02}] {}\n", start_mins, start_secs, segment.text.trim())); + formatted_transcript.push_str(&format!( + "[{:02}:{:02}] {}\n", + start_mins, + start_secs, + segment.text.trim() + )); } if !formatted_transcript.trim().is_empty() { - emit_log(app, "SUCCESS", "Transcription completed (async) with timestamps."); + emit_log( + app, + "SUCCESS", + "Transcription completed (async) with timestamps.", + ); return Ok(formatted_transcript); } else { - emit_log(app, "WARN", "Segments found but empty content."); + emit_log(app, "WARN", "Segments found but empty content."); } } else { emit_log(app, "WARN", "Verbose parsed but no segments found."); } - - if let Some(text) = response.text { - emit_log(app, "SUCCESS", "Transcription completed (async) - raw text (segments missing)."); - return Ok(text); - } - } else { - emit_log(app, "WARN", "Failed to parse poll content as WhisperVerboseResponse"); - } - emit_log(app, "SUCCESS", "Transcription completed - returning raw content."); - // If not JSON or no text field, return raw content - return Ok(content); - } else { - emit_log(app, "ERROR", "Failed to download transcription results."); - return Err(format!("Download failed: {}", dl_res.status())); - } + if let Some(text) = response.text { + emit_log(app, "SUCCESS", "Transcription completed (async) - raw text (segments missing)."); + return Ok(text); + } + } else { + emit_log( + app, + "WARN", + "Failed to parse poll content as WhisperVerboseResponse", + ); + } + + emit_log( + app, + "SUCCESS", + "Transcription completed - returning raw content.", + ); + // If not JSON or no text field, return raw content + return Ok(content); + } else { + emit_log(app, "ERROR", "Failed to download transcription results."); + return Err(format!("Download failed: {}", dl_res.status())); + } } else if status == "failed" || status == "error" { - let err_msg = format!("Batch processing failed [Status: {}]. Full Response: {:?}", status, json); + let err_msg = format!( + "Batch processing failed [Status: {}]. Full Response: {:?}", + status, json + ); emit_log(app, "ERROR", &err_msg); return Err(err_msg); } @@ -577,26 +782,41 @@ async fn poll_transcription(app: &AppHandle, client: &reqwest::Client, api_key: } #[tauri::command] -async fn summarize_text(app: AppHandle, text: String, api_key: String, product_id: String, prompt: String, model: String) -> Result { +async fn summarize_text( + app: AppHandle, + text: String, + api_key: String, + product_id: String, + prompt: String, + model: String, +) -> Result { emit_log(&app, "INFO", "Starting summarization..."); let client = reqwest::Client::new(); - let url = format!("https://api.infomaniak.com/2/ai/{}/openai/v1/chat/completions", product_id); + let url = format!( + "https://api.infomaniak.com/2/ai/{}/openai/v1/chat/completions", + product_id + ); let messages = serde_json::json!([ { "role": "system", "content": prompt }, { "role": "user", "content": text } ]); - let model_to_use = if model.is_empty() { "mixtral".to_string() } else { model }; + let model_to_use = if model.is_empty() { + "mixtral".to_string() + } else { + model + }; let body = serde_json::json!({ - "model": model_to_use, + "model": model_to_use, "messages": messages }); emit_log(&app, "DEBUG", &format!("POST {}", url)); - let res = client.post(&url) + let res = client + .post(&url) .header("Authorization", format!("Bearer {}", api_key)) .header("Content-Type", "application/json") .json(&body) @@ -614,7 +834,7 @@ async fn summarize_text(app: AppHandle, text: String, api_key: String, product_i let response_body: ChatCompletionResponse = serde_json::from_str(&raw_body) .map_err(|e| format!("Failed to decode JSON: {}. Body: {}", e, raw_body))?; - + if let Some(choice) = response_body.choices.first() { emit_log(&app, "SUCCESS", "Summarization received."); Ok(choice.message.content.clone()) @@ -624,7 +844,11 @@ async fn summarize_text(app: AppHandle, text: String, api_key: String, product_i } } else { let error_text = res.text().await.unwrap_or_default(); - emit_log(&app, "ERROR", &format!("Summarization failed: {}", error_text)); + emit_log( + &app, + "ERROR", + &format!("Summarization failed: {}", error_text), + ); Err(format!("Summarization failed: {}", error_text)) } } @@ -649,15 +873,19 @@ fn resolve_binary_path(binary_name: &str) -> String { return path.clone(); } } - + // Fallback to expecting it in PATH binary_name.to_string() } #[tauri::command] fn get_audio_metadata(app: AppHandle, file_path: String) -> Result { - emit_log(&app, "INFO", &format!("Getting metadata for: {}", file_path)); - + emit_log( + &app, + "INFO", + &format!("Getting metadata for: {}", file_path), + ); + let path = std::path::Path::new(&file_path); if !path.exists() { return Err(format!("File not found: {}", file_path)); @@ -670,22 +898,26 @@ fn get_audio_metadata(app: AppHandle, file_path: String) -> Result Result Result { emit_log(&app, "INFO", &format!("Converting to MP3: {}", wav_path)); - + let mp3_path = wav_path.replace(".wav", ".mp3"); let ffmpeg_cmd = resolve_binary_path("ffmpeg"); - + let output = Command::new(&ffmpeg_cmd) .args([ - "-i", &wav_path, - "-codec:a", "libmp3lame", - "-b:a", "64k", + "-i", + &wav_path, + "-codec:a", + "libmp3lame", + "-b:a", + "64k", "-y", // overwrite - &mp3_path + &mp3_path, ]) .output() .map_err(|e| format!("Failed to execute ffmpeg at '{}': {}", ffmpeg_cmd, e))?; - + if output.status.success() { emit_log(&app, "SUCCESS", &format!("MP3 created: {}", mp3_path)); Ok(mp3_path) @@ -726,92 +961,136 @@ fn convert_to_mp3(app: AppHandle, wav_path: String) -> Result { } #[tauri::command] -fn chunk_audio(app: AppHandle, file_path: String, chunk_minutes: u32) -> Result, String> { - emit_log(&app, "INFO", &format!("Chunking audio: {} ({}min chunks)", file_path, chunk_minutes)); - +fn chunk_audio( + app: AppHandle, + file_path: String, + chunk_minutes: u32, +) -> Result, String> { + emit_log( + &app, + "INFO", + &format!( + "Chunking audio: {} ({}min chunks)", + file_path, chunk_minutes + ), + ); + let chunk_seconds = chunk_minutes * 60; let ffprobe_cmd = resolve_binary_path("ffprobe"); let ffmpeg_cmd = resolve_binary_path("ffmpeg"); - + // Get total duration using ffprobe let duration_output = Command::new(&ffprobe_cmd) .args([ - "-v", "error", - "-show_entries", "format=duration", - "-of", "default=noprint_wrappers=1:nokey=1", - &file_path + "-v", + "error", + "-show_entries", + "format=duration", + "-of", + "default=noprint_wrappers=1:nokey=1", + &file_path, ]) .output() .map_err(|e| format!("Failed to get duration with '{}': {}", ffprobe_cmd, e))?; - + let duration_str = String::from_utf8_lossy(&duration_output.stdout); - let duration: f64 = duration_str.trim().parse() + let duration: f64 = duration_str + .trim() + .parse() .map_err(|_| "Failed to parse duration".to_string())?; - + let num_chunks = (duration / chunk_seconds as f64).ceil() as usize; - emit_log(&app, "INFO", &format!("Total duration: {}s, creating {} chunks", duration, num_chunks)); - + emit_log( + &app, + "INFO", + &format!( + "Total duration: {}s, creating {} chunks", + duration, num_chunks + ), + ); + let mut chunk_paths = Vec::new(); let base_path = file_path.replace(".mp3", ""); - + for i in 0..num_chunks { let start_time = i as u32 * chunk_seconds; let chunk_path = format!("{}_chunk_{}.mp3", base_path, i); - + let output = Command::new(&ffmpeg_cmd) .args([ - "-i", &file_path, - "-ss", &start_time.to_string(), - "-t", &chunk_seconds.to_string(), - "-c", "copy", + "-i", + &file_path, + "-ss", + &start_time.to_string(), + "-t", + &chunk_seconds.to_string(), + "-c", + "copy", "-y", - &chunk_path + &chunk_path, ]) .output() .map_err(|e| format!("Failed to create chunk {} with '{}': {}", i, ffmpeg_cmd, e))?; - + if !output.status.success() { let error = String::from_utf8_lossy(&output.stderr); return Err(format!("Chunk {} failed: {}", i, error)); } - + chunk_paths.push(chunk_path); } - - emit_log(&app, "SUCCESS", &format!("Created {} chunks", chunk_paths.len())); + + emit_log( + &app, + "SUCCESS", + &format!("Created {} chunks", chunk_paths.len()), + ); Ok(chunk_paths) } #[tauri::command] fn read_image_as_base64(app: AppHandle, file_path: String) -> Result { - emit_log(&app, "INFO", &format!("Reading image as base64: {}", file_path)); - - let bytes = std::fs::read(&file_path) - .map_err(|e| format!("Failed to read file: {}", e))?; - + emit_log( + &app, + "INFO", + &format!("Reading image as base64: {}", file_path), + ); + + let bytes = std::fs::read(&file_path).map_err(|e| format!("Failed to read file: {}", e))?; + // Detect image type from extension let extension = std::path::Path::new(&file_path) .extension() .and_then(|e| e.to_str()) .unwrap_or("png") .to_lowercase(); - + let mime_type = match extension.as_str() { "jpg" | "jpeg" => "image/jpeg", "png" => "image/png", "svg" => "image/svg+xml", "gif" => "image/gif", - _ => "image/png" + _ => "image/png", }; - + // Use base64 encoding let base64_str = base64::prelude::BASE64_STANDARD.encode(&bytes); let data_url = format!("data:{};base64,{}", mime_type, base64_str); - - emit_log(&app, "SUCCESS", &format!("Image converted to base64 ({} bytes)", base64_str.len())); + + emit_log( + &app, + "SUCCESS", + &format!("Image converted to base64 ({} bytes)", base64_str.len()), + ); Ok(data_url) } +#[tauri::command] +fn get_running_apps(app: AppHandle) -> Result, String> { + emit_log(&app, "INFO", "Fetching running applications..."); + Ok(apps::get_running_applications()) +} + #[tauri::command] fn open_audio_midi_setup() -> Result<(), String> { Command::new("open") @@ -825,35 +1104,48 @@ fn open_audio_midi_setup() -> Result<(), String> { #[tauri::command] fn create_hearbit_audio_device(app: AppHandle) -> Result { emit_log(&app, "INFO", "Attempting to create Hearbit Audio device..."); - + // Resolve resource path - let resource_path = app.path().resource_dir() + let resource_path = app + .path() + .resource_dir() .map_err(|e| e.to_string())? .join("resources/create_hearbit_audio.swift"); - + if !resource_path.exists() { - // Fallback for dev environment where resources might not be bundled yet or different path - emit_log(&app, "WARN", &format!("Resource script not found at {:?}. Trying local src-tauri path.", resource_path)); + // Fallback for dev environment where resources might not be bundled yet or different path + emit_log( + &app, + "WARN", + &format!( + "Resource script not found at {:?}. Trying local src-tauri path.", + resource_path + ), + ); } - + // For now, in dev mode, we might need to point to the source location if bundle isn't active // But let's try running it. - + let output = Command::new("swift") .arg(resource_path) .output() .map_err(|e| e.to_string())?; - + let stdout = String::from_utf8_lossy(&output.stdout).to_string(); let stderr = String::from_utf8_lossy(&output.stderr).to_string(); - + emit_log(&app, "DEBUG", &format!("Script Output: {}", stdout)); if !stderr.is_empty() { emit_log(&app, "WARN", &format!("Script Stderr: {}", stderr)); } if output.status.success() { - emit_log(&app, "SUCCESS", "Hearbit Audio device created successfully."); + emit_log( + &app, + "SUCCESS", + "Hearbit Audio device created successfully.", + ); Ok("Device created successfully".to_string()) } else { emit_log(&app, "ERROR", "Failed to create device."); @@ -866,7 +1158,6 @@ async fn check_screen_recording_permission() -> bool { sc_audio::check_permissions().await } - #[tauri::command] async fn save_text_file(app: AppHandle, path: String, content: String) -> Result<(), String> { emit_log(&app, "INFO", &format!("Saving text file to: {}", path)); @@ -874,7 +1165,7 @@ async fn save_text_file(app: AppHandle, path: String, content: String) -> Result Ok(_) => { emit_log(&app, "SUCCESS", "File saved successfully."); Ok(()) - }, + } Err(e) => { emit_log(&app, "ERROR", &format!("Failed to save file: {}", e)); Err(e.to_string()) @@ -882,11 +1173,13 @@ async fn save_text_file(app: AppHandle, path: String, content: String) -> Result } } - - #[tauri::command] async fn read_log_file(app: AppHandle) -> Result { - let log_path = app.path().app_log_dir().map_err(|e| e.to_string())?.join("hearbit-ai.log"); + let log_path = app + .path() + .app_log_dir() + .map_err(|e| e.to_string())? + .join("hearbit-ai.log"); if log_path.exists() { let content = std::fs::read_to_string(&log_path).map_err(|e| e.to_string())?; Ok(content) @@ -900,34 +1193,33 @@ pub fn run() { tauri::Builder::default() .setup(|app| { // Setup Tray Icon - let quit_i = MenuItem::with_id(app, "quit", "Quit Hearbit AI", true, None::<&str>).unwrap(); + let quit_i = + MenuItem::with_id(app, "quit", "Quit Hearbit AI", true, None::<&str>).unwrap(); let show_i = MenuItem::with_id(app, "show", "Show Window", true, None::<&str>).unwrap(); let menu = Menu::with_items(app, &[&show_i, &quit_i]).unwrap(); let _tray = TrayIconBuilder::new() .icon(app.default_window_icon().unwrap().clone()) .menu(&menu) - .show_menu_on_left_click(true) - .on_menu_event(|app, event| { - match event.id.as_ref() { - "quit" => app.exit(0), - "show" => { - if let Some(window) = app.get_webview_window("main") { - let _ = window.show(); - let _ = window.set_focus(); - } + .show_menu_on_left_click(true) + .on_menu_event(|app, event| match event.id.as_ref() { + "quit" => app.exit(0), + "show" => { + if let Some(window) = app.get_webview_window("main") { + let _ = window.show(); + let _ = window.set_focus(); } - _ => {} } + _ => {} }) .on_tray_icon_event(|tray, event| { - if let TrayIconEvent::Click { .. } = event { - let app = tray.app_handle(); - if let Some(window) = app.get_webview_window("main") { - let _ = window.show(); - let _ = window.set_focus(); - } - } + if let TrayIconEvent::Click { .. } = event { + let app = tray.app_handle(); + if let Some(window) = app.get_webview_window("main") { + let _ = window.show(); + let _ = window.set_focus(); + } + } }) .build(app)?; @@ -941,12 +1233,16 @@ pub fn run() { } }) .plugin(tauri_plugin_shell::init()) - .plugin(tauri_plugin_log::Builder::default() - .targets([ - tauri_plugin_log::Target::new(tauri_plugin_log::TargetKind::Stdout), - tauri_plugin_log::Target::new(tauri_plugin_log::TargetKind::LogDir { file_name: Some("hearbit-ai.log".to_string()) }), - ]) - .build()) + .plugin( + tauri_plugin_log::Builder::default() + .targets([ + tauri_plugin_log::Target::new(tauri_plugin_log::TargetKind::Stdout), + tauri_plugin_log::Target::new(tauri_plugin_log::TargetKind::LogDir { + file_name: Some("hearbit-ai.log".to_string()), + }), + ]) + .build(), + ) .plugin(tauri_plugin_opener::init()) .plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_fs::init()) @@ -954,7 +1250,7 @@ pub fn run() { .manage(AppState { recording_stream: Mutex::new(None), recording_file_path: Mutex::new(None), - system_capture: Mutex::new(None), + system_capture: tokio::sync::Mutex::new(None), }) .invoke_handler(tauri::generate_handler![ greet, @@ -977,8 +1273,18 @@ pub fn run() { convert_to_mp3, chunk_audio, read_image_as_base64, - email::send_smtp_email + email::send_smtp_email, + get_running_apps ]) - .run(tauri::generate_context!()) - .expect("error while running tauri application"); + .build(tauri::generate_context!()) + .expect("error while building tauri application") + .run(|app_handle, event| match event { + tauri::RunEvent::Reopen { .. } => { + if let Some(window) = app_handle.get_webview_window("main") { + let _ = window.show(); + let _ = window.set_focus(); + } + } + _ => {} + }); } diff --git a/src-tauri/src/lib.rs.bak b/src-tauri/src/lib.rs.bak new file mode 100644 index 0000000..e3f1c7b --- /dev/null +++ b/src-tauri/src/lib.rs.bak @@ -0,0 +1,984 @@ +use tauri::{ + AppHandle, Manager, State, Emitter, + menu::{Menu, MenuItem}, + tray::{TrayIconBuilder, TrayIconEvent}, + WindowEvent +}; +use std::sync::{Arc, Mutex}; +use std::process::Command; +use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; +use std::time::Duration; +use tokio::time::sleep; +use base64::Engine; + +mod audio_processor; +use audio_processor::AudioProcessor; +mod auth; +mod email; +mod sc_audio; + +// State to hold the active recording stream +struct AppState { + recording_stream: Mutex>, + recording_file_path: Mutex>, + system_capture: Mutex>, +} + +#[derive(serde::Serialize)] +struct AudioDevice { + id: String, + name: String, +} + +#[derive(serde::Serialize, Clone)] +struct LogEvent { + level: String, + message: String, + timestamp: String, +} + +pub(crate) fn emit_log(app: &AppHandle, level: &str, message: &str) { + let log = LogEvent { + level: level.to_string(), + message: message.to_string(), + timestamp: chrono::Local::now().format("%H:%M:%S").to_string(), + }; + let _ = app.emit("log-event", log); +} + +#[tauri::command] +fn greet(name: &str) -> String { + format!("Hello, {}! You've been greeted from Rust!", name) +} + +#[tauri::command] +fn get_input_devices() -> Result, String> { + let host = cpal::default_host(); + let devices = host.input_devices().map_err(|e| e.to_string())?; + + let mut result = Vec::new(); + for device in devices { + #[allow(deprecated)] + if let Ok(name) = device.name() { + // macOS often produces weird names, but let's just use what we get + result.push(AudioDevice { + id: name.clone(), // Using name as ID for simplicity in this MVP + name, + }); + } + } + Ok(result) +} + + + +#[tauri::command] +async fn start_recording(app: AppHandle, state: State<'_, AppState>, device_id: String, save_path: Option, custom_filename: Option, wait_for_speech: Option, mode: String) -> Result<(), String> { + emit_log(&app, "INFO", &format!("Starting recording [Mode: {}] on device: {}", mode, device_id)); + let host = cpal::default_host(); + + // Find device by name (using name as ID) + #[allow(deprecated)] + let device = host.input_devices() + .map_err(|e| e.to_string())? + .find(|d| d.name().map(|n| n == device_id).unwrap_or(false)) + .or_else(|| host.default_input_device()) + .ok_or("No input device found")?; + + // Select the configuration with the MAXIMUM number of channels + // This is crucial for "Hearbit Audio" (Aggregate) which lists 3 channels but might default to 2. + // Select Audio Configuration + // We prioritize 48kHz because System Audio (ScreenCaptureKit) acts best at 48k. + let supported_configs: Vec<_> = device.supported_input_configs().map_err(|e| e.to_string())?.collect(); + + // Try to find 48kHz specifically + // Note: cpal::SampleRate is likely a type alias for u32 here, so we pass 48000 directly. + let config = supported_configs.iter() + .find(|c| c.min_sample_rate() <= 48000 && c.max_sample_rate() >= 48000) + .map(|c| c.with_sample_rate(48000)) + .or_else(|| { + // Fallback: Max sample rate + supported_configs.iter() + .max_by_key(|c| c.channels()) + .map(|c| c.with_max_sample_rate()) + }) + .ok_or("No supported input configurations found")?; + + emit_log(&app, "INFO", &format!("Selected Audio Config: {} Channels, {} Hz", config.channels(), config.sample_rate())); + + let spec = hound::WavSpec { + channels: config.channels(), + sample_rate: config.sample_rate(), + bits_per_sample: 16, + sample_format: hound::SampleFormat::Int, + }; + + // Determine file path: User provided or Temp + let filename = if let Some(name) = custom_filename { + // Sanitize filename + let safe_name: String = name.chars().map(|x| if x.is_alphanumeric() || x == ' ' || x == '-' || x == '_' { x } else { '_' }).collect(); + format!("{}.wav", safe_name) + } else { + format!("recording_{}.wav", std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs()) + }; + + let file_path = if let Some(path) = save_path { + if path.trim().is_empty() { + std::env::temp_dir().join(&filename) + } else { + std::path::PathBuf::from(path).join(&filename) + } + } else { + std::env::temp_dir().join(&filename) + }; + + let file_path_str = file_path.to_string_lossy().to_string(); + emit_log(&app, "INFO", &format!("Saving recording to: {}", file_path_str)); + + let writer = hound::WavWriter::create(&file_path, spec).map_err(|e| e.to_string())?; + let writer = Arc::new(Mutex::new(writer)); + let writer_clone = writer.clone(); + + // Initialize AudioProcessor (VAD) + // We pass the writer to it. + let should_wait = wait_for_speech.unwrap_or(false); + if should_wait { + emit_log(&app, "INFO", &format!("Recording started in WAITING mode (Trigger: {}).", if mode == "voice" { "Speech" } else { "System Audio" })); + } + + let processor = AudioProcessor::new(config.sample_rate(), config.channels(), writer.clone(), app.clone(), should_wait, mode) + .map_err(|e| format!("Failed to create AudioProcessor: {}", e))?; + + // Wrap processor in Arc so we can share/move it into callback + // Actually, cpal callback takes ownership of its closure state usually if 'move'. + // Since stream is on another thread, we need Send. AudioProcessor should be Send. + // However, the callback is called repeatedly. We need to keep state. + // The workaround is to wrap it in a Mutex. + let processor = Arc::new(Mutex::new(processor)); + let processor_clone = processor.clone(); + + // --- SYSTEM AUDIO CAPTURE START --- + // Prevent Doubling: If user selected an aggregate device (Hearbit Audio/BlackHole), + // it ALREADY contains system audio. In that case, we don't need internal SCK capture. + let is_aggregate = device_id.contains("Hearbit") || device_id.contains("BlackHole"); + + if is_aggregate { + emit_log(&app, "INFO", "Aggregate device detected. Disabling internal System Audio Capture to prevent doubling."); + } else { + let mut sys_capture = sc_audio::SystemAudioCapture::new(config.sample_rate()); + + // Get the queue to share with the capture callback + let queue_clone = { + let p = processor.lock().unwrap(); + p.system_queue.clone() // Access the pub field we added + }; + + let sys_callback = move |data: &[f32]| { + // Push to queue + if let Ok(mut q) = queue_clone.lock() { + q.extend(data.iter()); + + // Limit queue size to avoid memory leaks if main process loop is slow + while q.len() > 48000 * 5 { // 5 seconds buffer + q.pop_front(); + } + } + }; + + match sys_capture.start(sys_callback).await { + Ok(_) => emit_log(&app, "INFO", "System Audio Capture started."), + Err(e) => emit_log(&app, "WARN", &format!("System Audio Capture failed (Permissions?): {}", e)), + } + + *state.system_capture.lock().unwrap() = Some(sys_capture); + } + // --- SYSTEM AUDIO CAPTURE END --- + + let app_handle = app.clone(); + let err_fn = move |err| { + eprintln!("an error occurred on stream: {}", err); + emit_log(&app_handle, "ERROR", &format!("Stream error: {}", err)); + }; + + let stream = match config.sample_format() { + cpal::SampleFormat::F32 => device.build_input_stream( + &config.into(), + move |data: &[f32], _: &_| { + if let Ok(mut p) = processor_clone.lock() { + p.process(data); + } + }, + err_fn, + None + ), + // For I16 and U16 we need to convert to F32 for our processor + cpal::SampleFormat::I16 => device.build_input_stream( + &config.into(), + move |data: &[i16], _: &_| { + // Convert i16 to f32 + let f32_data: Vec = data.iter().map(|&s| s as f32 / i16::MAX as f32).collect(); + if let Ok(mut p) = processor_clone.lock() { + p.process(&f32_data); + } + }, + err_fn, + None + ), + cpal::SampleFormat::U16 => device.build_input_stream( + &config.into(), + move |data: &[u16], _: &_| { + // Convert u16 to f32 + let f32_data: Vec = data.iter().map(|&s| (s as i32 - 32768) as f32 / 32768.0).collect(); + if let Ok(mut p) = processor_clone.lock() { + p.process(&f32_data); + } + }, + err_fn, + None + ), + _ => return Err("Unsupported sample format".to_string()), + }.map_err(|e| e.to_string())?; + + stream.play().map_err(|e| e.to_string())?; + + // Store state + *state.recording_stream.lock().unwrap() = Some(stream); + *state.recording_file_path.lock().unwrap() = Some(file_path_str.clone()); + + emit_log(&app, "SUCCESS", &format!("Recording started. File: {}", file_path_str)); + Ok(()) +} + +#[tauri::command] +fn stop_recording(app: AppHandle, state: State<'_, AppState>) -> Result { + emit_log(&app, "INFO", "Stopping recording..."); + // Drop stream to stop recording + { + let mut stream_guard = state.recording_stream.lock().unwrap(); + // Also stop System Capture + let mut sys_guard = state.system_capture.lock().unwrap(); + if let Some(sys) = sys_guard.as_mut() { + sys.stop(); + } + *sys_guard = None; + + if stream_guard.is_none() { + return Err("Not recording".to_string()); + } + *stream_guard = None; // This drops the stream and stops recording + } + + // Return file path + let mut path_guard = state.recording_file_path.lock().unwrap(); + let path = path_guard.take().ok_or("No recording path found".to_string())?; + emit_log(&app, "SUCCESS", &format!("Recording stopped. Saved to: {}", path)); + Ok(path) +} + +#[tauri::command] +fn pause_recording(app: AppHandle, state: State<'_, AppState>) -> Result<(), String> { + emit_log(&app, "INFO", "Pausing recording..."); + let stream_guard = state.recording_stream.lock().unwrap(); + if let Some(stream) = stream_guard.as_ref() { + stream.pause().map_err(|e| e.to_string())?; + emit_log(&app, "SUCCESS", "Recording paused."); + Ok(()) + } else { + Err("Not recording".to_string()) + } +} + +#[tauri::command] +fn resume_recording(app: AppHandle, state: State<'_, AppState>) -> Result<(), String> { + emit_log(&app, "INFO", "Resuming recording..."); + let stream_guard = state.recording_stream.lock().unwrap(); + if let Some(stream) = stream_guard.as_ref() { + stream.play().map_err(|e| e.to_string())?; + emit_log(&app, "SUCCESS", "Recording resumed."); + Ok(()) + } else { + Err("Not recording".to_string()) + } +} + +#[derive(serde::Deserialize)] +struct ModelListResponse { + data: Vec, +} + +#[derive(serde::Deserialize)] +struct ModelData { + id: String, + #[allow(dead_code)] + owned_by: Option, +} + +// Structs for Infomaniak API responses +#[derive(serde::Deserialize)] +struct WhisperResponse { + text: Option, + batch_id: Option, +} + +#[derive(serde::Deserialize)] +struct ChatCompletionResponse { + choices: Vec, +} +#[derive(serde::Deserialize)] +struct Choice { + message: Message, +} +#[derive(serde::Deserialize)] +struct Message { + #[allow(dead_code)] + content: String, +} + +#[derive(serde::Serialize)] +struct ModelInfo { + id: String, + name: String, +} + +#[tauri::command] +async fn get_available_models(app: AppHandle, api_key: String, product_id: String) -> Result, String> { + emit_log(&app, "INFO", "Fetching available models from Infomaniak..."); + let client = reqwest::Client::new(); + // Use the v2/openai compliant endpoint as per docs + let url = format!("https://api.infomaniak.com/2/ai/{}/openai/v1/models", product_id); + + emit_log(&app, "DEBUG", &format!("GET {}", url)); + + let res = client.get(&url) + .header("Authorization", format!("Bearer {}", api_key)) + .send() + .await + .map_err(|e| { + let msg = format!("Network error fetching models: {}", e); + emit_log(&app, "ERROR", &msg); + msg + })?; + + if res.status().is_success() { + let raw_body = res.text().await.map_err(|e| e.to_string())?; + // println!("Models Raw Response: {}", raw_body); + let list: ModelListResponse = serde_json::from_str(&raw_body) + .map_err(|e| format!("Failed to parse models: {}. Body: {}", e, raw_body))?; + + let models = list.data.into_iter() + .filter(|m| !m.id.to_lowercase().contains("mini_lm") && !m.id.to_lowercase().contains("bert") && !m.id.to_lowercase().contains("embedding")) + .map(|m| ModelInfo { + id: m.id.clone(), + name: m.id, // Use ID as name for now, or fetch more details if available + }).collect::>(); + + emit_log(&app, "SUCCESS", &format!("Loaded {} models.", models.len())); + Ok(models) + } else { + let err = res.text().await.unwrap_or_default(); + emit_log(&app, "ERROR", &format!("Failed to fetch models: {}", err)); + Err(format!("Failed to fetch models: {}", err)) + } +} + + +#[derive(serde::Deserialize)] +struct WhisperVerboseResponse { + text: Option, + segments: Option>, +} + +#[derive(serde::Deserialize)] +struct Segment { + start: f64, + end: f64, + text: String, +} + +#[tauri::command] +async fn transcribe_audio(app: AppHandle, file_path: String, api_key: String, product_id: String) -> Result { + emit_log(&app, "INFO", "Starting transcription with timestamps..."); + let client = reqwest::Client::new(); + + // Prepare file part + let file_bytes = std::fs::read(&file_path).map_err(|e| e.to_string())?; + // We must use a known file name for the part, Infomaniak might care, or not. + let file_part = reqwest::multipart::Part::bytes(file_bytes) + .file_name("recording.wav") + .mime_str("audio/wav") + .map_err(|e| e.to_string())?; + + let form = reqwest::multipart::Form::new() + .part("file", file_part) + .text("model", "whisper") + .text("response_format", "verbose_json") + .text("timestamp_granularities[]", "segment"); // Crucial for accurate segments + + let url = format!("https://api.infomaniak.com/1/ai/{}/openai/audio/transcriptions", product_id); + + emit_log(&app, "DEBUG", &format!("POST {}", url)); + + let res = client.post(&url) + .header("Authorization", format!("Bearer {}", api_key)) + .multipart(form) + .send() + .await + .map_err(|e| { + let msg = format!("Network error during transcription: {}", e); + emit_log(&app, "ERROR", &msg); + msg + })?; + + if res.status().is_success() { + let raw_body = res.text().await.map_err(|e| e.to_string())?; + + // Check if we got a batch ID + #[derive(serde::Deserialize)] + struct BatchResponse { + batch_id: Option, + } + + // Try parsing as batch response first (Infomaniak specific behavior) + if let Ok(batch_res) = serde_json::from_str::(&raw_body) { + if let Some(batch_id) = batch_res.batch_id { + emit_log(&app, "INFO", &format!("Transcription queued. Batch ID: {}", batch_id)); + return poll_transcription(&app, &client, &api_key, &product_id, &batch_id).await; + } + } + + // If not batch, try parsing verbose response directly + // Log the raw body so we can see why it fails + emit_log(&app, "DEBUG", &format!("Direct Response (first 500 chars): {:.500}", raw_body)); + + let response: WhisperVerboseResponse = serde_json::from_str(&raw_body) + .map_err(|e| format!("Failed to decode JSON: {}. Body: {}", e, raw_body))?; + + if let Some(segments) = response.segments { + emit_log(&app, "INFO", &format!("Found {} segments (Direct).", segments.len())); + for (i, seg) in segments.iter().take(3).enumerate() { + emit_log(&app, "DEBUG", &format!("Seg {}: start={}", i, seg.start)); + } + + // Format timestamps: [MM:SS] Text + let mut formatted_transcript = String::new(); + for segment in segments { + let start_mins = (segment.start / 60.0).floor() as u64; + let start_secs = (segment.start % 60.0).floor() as u64; + formatted_transcript.push_str(&format!("[{:02}:{:02}] {}\n", start_mins, start_secs, segment.text.trim())); + } + + // Fallback to raw text if segments empty + if formatted_transcript.trim().is_empty() { + if let Some(text) = response.text { + emit_log(&app, "SUCCESS", "Segments missing, using raw text."); + return Ok(text); + } + } else { + emit_log(&app, "SUCCESS", "Transcription received with timestamps."); + return Ok(formatted_transcript); + } + } else if let Some(text) = response.text { + emit_log(&app, "SUCCESS", "Segments missing, using raw text."); + return Ok(text); + } + + emit_log(&app, "ERROR", "Response contained no recognized content."); + Err(format!("Response contained no recognized content. Body: {}", raw_body)) + } else { + let error_text = res.text().await.unwrap_or_default(); + emit_log(&app, "ERROR", &format!("Transcription failed: {}", error_text)); + Err(format!("Transcription failed: {}", error_text)) + } +} + +async fn poll_transcription(app: &AppHandle, client: &reqwest::Client, api_key: &str, product_id: &str, batch_id: &str) -> Result { + let status_url = format!("https://api.infomaniak.com/1/ai/{}/results/{}", product_id, batch_id); + + let mut attempts = 0; + while attempts < 40 { // 40 * 2s = 80s timeout + attempts += 1; + sleep(Duration::from_secs(2)).await; + + emit_log(app, "DEBUG", &format!("Polling status... Attempt {}", attempts)); + let res = client.get(&status_url) + .header("Authorization", format!("Bearer {}", api_key)) + .send() + .await + .map_err(|e| format!("Polling error: {}", e))?; + + if res.status().is_success() { + let json: serde_json::Value = res.json().await.map_err(|e| e.to_string())?; + // Check 'status' + if let Some(status) = json.get("status").and_then(|s| s.as_str()) { + if status == "success" { + // Download the result + let download_url = format!("https://api.infomaniak.com/1/ai/{}/results/{}/download", product_id, batch_id); + let dl_res = client.get(&download_url) + .header("Authorization", format!("Bearer {}", api_key)) + .send() + .await + .map_err(|e| e.to_string())?; + + if dl_res.status().is_success() { + let content = dl_res.text().await.map_err(|e| e.to_string())?; + emit_log(app, "DEBUG", &format!("Poll Raw Content (first 500 chars): {:.500}", content)); + + // Try to parse as Verbose JSON to get timestamps + if let Ok(response) = serde_json::from_str::(&content) { + if let Some(segments) = response.segments { + emit_log(app, "INFO", &format!("Found {} segments.", segments.len())); + // Log first 3 segments start times + for (i, seg) in segments.iter().take(3).enumerate() { + emit_log(app, "DEBUG", &format!("Seg {}: start={}", i, seg.start)); + } + + let mut formatted_transcript = String::new(); + for segment in segments { + let start_mins = (segment.start / 60.0).floor() as u64; + let start_secs = (segment.start % 60.0).floor() as u64; + formatted_transcript.push_str(&format!("[{:02}:{:02}] {}\n", start_mins, start_secs, segment.text.trim())); + } + if !formatted_transcript.trim().is_empty() { + emit_log(app, "SUCCESS", "Transcription completed (async) with timestamps."); + return Ok(formatted_transcript); + } else { + emit_log(app, "WARN", "Segments found but empty content."); + } + } else { + emit_log(app, "WARN", "Verbose parsed but no segments found."); + } + + if let Some(text) = response.text { + emit_log(app, "SUCCESS", "Transcription completed (async) - raw text (segments missing)."); + return Ok(text); + } + } else { + emit_log(app, "WARN", "Failed to parse poll content as WhisperVerboseResponse"); + } + + emit_log(app, "SUCCESS", "Transcription completed - returning raw content."); + // If not JSON or no text field, return raw content + return Ok(content); + } else { + emit_log(app, "ERROR", "Failed to download transcription results."); + return Err(format!("Download failed: {}", dl_res.status())); + } + } else if status == "failed" || status == "error" { + let err_msg = format!("Batch processing failed [Status: {}]. Full Response: {:?}", status, json); + emit_log(app, "ERROR", &err_msg); + return Err(err_msg); + } + // If 'processing' or 'pending', continue loop + } + } + } + emit_log(app, "ERROR", "Transcription timed out after 80s."); + Err("Transcription timed out".to_string()) +} + +#[tauri::command] +async fn summarize_text(app: AppHandle, text: String, api_key: String, product_id: String, prompt: String, model: String) -> Result { + emit_log(&app, "INFO", "Starting summarization..."); + let client = reqwest::Client::new(); + let url = format!("https://api.infomaniak.com/2/ai/{}/openai/v1/chat/completions", product_id); + + let messages = serde_json::json!([ + { "role": "system", "content": prompt }, + { "role": "user", "content": text } + ]); + + let model_to_use = if model.is_empty() { "mixtral".to_string() } else { model }; + + let body = serde_json::json!({ + "model": model_to_use, + "messages": messages + }); + + emit_log(&app, "DEBUG", &format!("POST {}", url)); + + let res = client.post(&url) + .header("Authorization", format!("Bearer {}", api_key)) + .header("Content-Type", "application/json") + .json(&body) + .send() + .await + .map_err(|e| { + let msg = format!("Network error during summarization: {}", e); + emit_log(&app, "ERROR", &msg); + msg + })?; + + if res.status().is_success() { + let raw_body = res.text().await.map_err(|e| e.to_string())?; + // println!("Summarization Raw Response: {}", raw_body); + + let response_body: ChatCompletionResponse = serde_json::from_str(&raw_body) + .map_err(|e| format!("Failed to decode JSON: {}. Body: {}", e, raw_body))?; + + if let Some(choice) = response_body.choices.first() { + emit_log(&app, "SUCCESS", "Summarization received."); + Ok(choice.message.content.clone()) + } else { + emit_log(&app, "WARN", "No summary generated in response."); + Err("No summary generated".to_string()) + } + } else { + let error_text = res.text().await.unwrap_or_default(); + emit_log(&app, "ERROR", &format!("Summarization failed: {}", error_text)); + Err(format!("Summarization failed: {}", error_text)) + } +} + +#[derive(serde::Serialize)] +struct AudioMetadata { + duration: f64, + size: u64, + format: String, +} + +// Helper to find ffmpeg/ffprobe in common paths +fn resolve_binary_path(binary_name: &str) -> String { + let common_paths = [ + format!("/opt/homebrew/bin/{}", binary_name), + format!("/usr/local/bin/{}", binary_name), + format!("/usr/bin/{}", binary_name), + ]; + + for path in common_paths.iter() { + if std::path::Path::new(path).exists() { + return path.clone(); + } + } + + // Fallback to expecting it in PATH + binary_name.to_string() +} + +#[tauri::command] +fn get_audio_metadata(app: AppHandle, file_path: String) -> Result { + emit_log(&app, "INFO", &format!("Getting metadata for: {}", file_path)); + + let path = std::path::Path::new(&file_path); + if !path.exists() { + return Err(format!("File not found: {}", file_path)); + } + + let size = std::fs::metadata(&file_path) + .map_err(|e| e.to_string())? + .len(); + + // Use ffprobe to get duration + // Try resolved path first + let ffprobe_cmd = resolve_binary_path("ffprobe"); + + let output = Command::new(&ffprobe_cmd) + .args([ + "-v", "error", + "-show_entries", "format=duration", + "-of", "default=noprint_wrappers=1:nokey=1", + &file_path + ]) + .output() + .map_err(|e| format!("Failed to execute ffprobe at '{}': {}", ffprobe_cmd, e))?; + + let duration_str = String::from_utf8_lossy(&output.stdout); + let duration: f64 = duration_str.trim().parse().unwrap_or(0.0); + + // Extension as format + let format = path.extension() + .and_then(|e| e.to_str()) + .unwrap_or("unknown") + .to_string(); + + Ok(AudioMetadata { + duration, + size, + format, + }) +} + +#[tauri::command] +fn convert_to_mp3(app: AppHandle, wav_path: String) -> Result { + emit_log(&app, "INFO", &format!("Converting to MP3: {}", wav_path)); + + let mp3_path = wav_path.replace(".wav", ".mp3"); + let ffmpeg_cmd = resolve_binary_path("ffmpeg"); + + let output = Command::new(&ffmpeg_cmd) + .args([ + "-i", &wav_path, + "-codec:a", "libmp3lame", + "-b:a", "64k", + "-y", // overwrite + &mp3_path + ]) + .output() + .map_err(|e| format!("Failed to execute ffmpeg at '{}': {}", ffmpeg_cmd, e))?; + + if output.status.success() { + emit_log(&app, "SUCCESS", &format!("MP3 created: {}", mp3_path)); + Ok(mp3_path) + } else { + let error = String::from_utf8_lossy(&output.stderr); + emit_log(&app, "ERROR", &format!("MP3 conversion failed: {}", error)); + Err(format!("MP3 conversion failed: {}", error)) + } +} + +#[tauri::command] +fn chunk_audio(app: AppHandle, file_path: String, chunk_minutes: u32) -> Result, String> { + emit_log(&app, "INFO", &format!("Chunking audio: {} ({}min chunks)", file_path, chunk_minutes)); + + let chunk_seconds = chunk_minutes * 60; + let ffprobe_cmd = resolve_binary_path("ffprobe"); + let ffmpeg_cmd = resolve_binary_path("ffmpeg"); + + // Get total duration using ffprobe + let duration_output = Command::new(&ffprobe_cmd) + .args([ + "-v", "error", + "-show_entries", "format=duration", + "-of", "default=noprint_wrappers=1:nokey=1", + &file_path + ]) + .output() + .map_err(|e| format!("Failed to get duration with '{}': {}", ffprobe_cmd, e))?; + + let duration_str = String::from_utf8_lossy(&duration_output.stdout); + let duration: f64 = duration_str.trim().parse() + .map_err(|_| "Failed to parse duration".to_string())?; + + let num_chunks = (duration / chunk_seconds as f64).ceil() as usize; + emit_log(&app, "INFO", &format!("Total duration: {}s, creating {} chunks", duration, num_chunks)); + + let mut chunk_paths = Vec::new(); + let base_path = file_path.replace(".mp3", ""); + + for i in 0..num_chunks { + let start_time = i as u32 * chunk_seconds; + let chunk_path = format!("{}_chunk_{}.mp3", base_path, i); + + let output = Command::new(&ffmpeg_cmd) + .args([ + "-i", &file_path, + "-ss", &start_time.to_string(), + "-t", &chunk_seconds.to_string(), + "-c", "copy", + "-y", + &chunk_path + ]) + .output() + .map_err(|e| format!("Failed to create chunk {} with '{}': {}", i, ffmpeg_cmd, e))?; + + if !output.status.success() { + let error = String::from_utf8_lossy(&output.stderr); + return Err(format!("Chunk {} failed: {}", i, error)); + } + + chunk_paths.push(chunk_path); + } + + emit_log(&app, "SUCCESS", &format!("Created {} chunks", chunk_paths.len())); + Ok(chunk_paths) +} + +#[tauri::command] +fn read_image_as_base64(app: AppHandle, file_path: String) -> Result { + emit_log(&app, "INFO", &format!("Reading image as base64: {}", file_path)); + + let bytes = std::fs::read(&file_path) + .map_err(|e| format!("Failed to read file: {}", e))?; + + // Detect image type from extension + let extension = std::path::Path::new(&file_path) + .extension() + .and_then(|e| e.to_str()) + .unwrap_or("png") + .to_lowercase(); + + let mime_type = match extension.as_str() { + "jpg" | "jpeg" => "image/jpeg", + "png" => "image/png", + "svg" => "image/svg+xml", + "gif" => "image/gif", + _ => "image/png" + }; + + // Use base64 encoding + let base64_str = base64::prelude::BASE64_STANDARD.encode(&bytes); + let data_url = format!("data:{};base64,{}", mime_type, base64_str); + + emit_log(&app, "SUCCESS", &format!("Image converted to base64 ({} bytes)", base64_str.len())); + Ok(data_url) +} + +#[tauri::command] +fn open_audio_midi_setup() -> Result<(), String> { + Command::new("open") + .arg("-a") + .arg("Audio MIDI Setup") + .spawn() + .map_err(|e| e.to_string())?; + Ok(()) +} + +#[tauri::command] +fn create_hearbit_audio_device(app: AppHandle) -> Result { + emit_log(&app, "INFO", "Attempting to create Hearbit Audio device..."); + + // Resolve resource path + let resource_path = app.path().resource_dir() + .map_err(|e| e.to_string())? + .join("resources/create_hearbit_audio.swift"); + + if !resource_path.exists() { + // Fallback for dev environment where resources might not be bundled yet or different path + emit_log(&app, "WARN", &format!("Resource script not found at {:?}. Trying local src-tauri path.", resource_path)); + } + + // For now, in dev mode, we might need to point to the source location if bundle isn't active + // But let's try running it. + + let output = Command::new("swift") + .arg(resource_path) + .output() + .map_err(|e| e.to_string())?; + + let stdout = String::from_utf8_lossy(&output.stdout).to_string(); + let stderr = String::from_utf8_lossy(&output.stderr).to_string(); + + emit_log(&app, "DEBUG", &format!("Script Output: {}", stdout)); + if !stderr.is_empty() { + emit_log(&app, "WARN", &format!("Script Stderr: {}", stderr)); + } + + if output.status.success() { + emit_log(&app, "SUCCESS", "Hearbit Audio device created successfully."); + Ok("Device created successfully".to_string()) + } else { + emit_log(&app, "ERROR", "Failed to create device."); + Err(format!("Failed to create device: {} {}", stdout, stderr)) + } +} + +#[tauri::command] +async fn check_screen_recording_permission() -> bool { + sc_audio::check_permissions().await +} + + +#[tauri::command] +async fn save_text_file(app: AppHandle, path: String, content: String) -> Result<(), String> { + emit_log(&app, "INFO", &format!("Saving text file to: {}", path)); + match std::fs::write(&path, content) { + Ok(_) => { + emit_log(&app, "SUCCESS", "File saved successfully."); + Ok(()) + }, + Err(e) => { + emit_log(&app, "ERROR", &format!("Failed to save file: {}", e)); + Err(e.to_string()) + } + } +} + + + +#[tauri::command] +async fn read_log_file(app: AppHandle) -> Result { + let log_path = app.path().app_log_dir().map_err(|e| e.to_string())?.join("hearbit-ai.log"); + if log_path.exists() { + let content = std::fs::read_to_string(&log_path).map_err(|e| e.to_string())?; + Ok(content) + } else { + Ok("No log file found yet.".to_string()) + } +} + +#[cfg_attr(mobile, tauri::mobile_entry_point)] +pub fn run() { + tauri::Builder::default() + .setup(|app| { + // Setup Tray Icon + let quit_i = MenuItem::with_id(app, "quit", "Quit Hearbit AI", true, None::<&str>).unwrap(); + let show_i = MenuItem::with_id(app, "show", "Show Window", true, None::<&str>).unwrap(); + let menu = Menu::with_items(app, &[&show_i, &quit_i]).unwrap(); + + let _tray = TrayIconBuilder::new() + .icon(app.default_window_icon().unwrap().clone()) + .menu(&menu) + .show_menu_on_left_click(true) + .on_menu_event(|app, event| { + match event.id.as_ref() { + "quit" => app.exit(0), + "show" => { + if let Some(window) = app.get_webview_window("main") { + let _ = window.show(); + let _ = window.set_focus(); + } + } + _ => {} + } + }) + .on_tray_icon_event(|tray, event| { + if let TrayIconEvent::Click { .. } = event { + let app = tray.app_handle(); + if let Some(window) = app.get_webview_window("main") { + let _ = window.show(); + let _ = window.set_focus(); + } + } + }) + .build(app)?; + + Ok(()) + }) + .on_window_event(|window, event| { + if let WindowEvent::CloseRequested { api, .. } = event { + // Prevent window from closing, just hide it + window.hide().unwrap(); + api.prevent_close(); + } + }) + .plugin(tauri_plugin_shell::init()) + .plugin(tauri_plugin_log::Builder::default() + .targets([ + tauri_plugin_log::Target::new(tauri_plugin_log::TargetKind::Stdout), + tauri_plugin_log::Target::new(tauri_plugin_log::TargetKind::LogDir { file_name: Some("hearbit-ai.log".to_string()) }), + ]) + .build()) + .plugin(tauri_plugin_opener::init()) + .plugin(tauri_plugin_dialog::init()) + .plugin(tauri_plugin_fs::init()) + .plugin(tauri_plugin_oauth::init()) + .manage(AppState { + recording_stream: Mutex::new(None), + recording_file_path: Mutex::new(None), + system_capture: Mutex::new(None), + }) + .invoke_handler(tauri::generate_handler![ + greet, + get_input_devices, + start_recording, + stop_recording, + pause_recording, + resume_recording, + transcribe_audio, + summarize_text, + get_available_models, + open_audio_midi_setup, + create_hearbit_audio_device, + check_screen_recording_permission, + auth::start_auth_flow, + auth::get_calendar_events, + save_text_file, + read_log_file, + get_audio_metadata, + convert_to_mp3, + chunk_audio, + read_image_as_base64, + email::send_smtp_email + ]) + .run(tauri::generate_context!()) + .expect("error while running tauri application"); +} diff --git a/src-tauri/src/sc_audio.rs b/src-tauri/src/sc_audio.rs index f07f0a2..0cf2939 100644 --- a/src-tauri/src/sc_audio.rs +++ b/src-tauri/src/sc_audio.rs @@ -1,17 +1,20 @@ +use cocoa::base::nil; +use objc::{msg_send, sel, sel_impl}; use screencapturekit_sys::{ - os_types::rc::Id, - shareable_content::UnsafeSCShareableContent, + cm_sample_buffer_ref::CMSampleBufferRef, content_filter::{UnsafeContentFilter, UnsafeInitParams}, - stream_configuration::UnsafeStreamConfiguration, + os_types::rc::Id, + shareable_content::{UnsafeSCRunningApplication, UnsafeSCShareableContent}, stream::UnsafeSCStream, + stream_configuration::UnsafeStreamConfiguration, stream_error_handler::UnsafeSCStreamError, stream_output_handler::UnsafeSCStreamOutput, - cm_sample_buffer_ref::CMSampleBufferRef, }; pub struct SystemAudioCapture { stream: Option>, sample_rate: u32, + excluded_apps: Vec, } struct AudioOutputWrapper { @@ -20,20 +23,15 @@ struct AudioOutputWrapper { impl UnsafeSCStreamOutput for AudioOutputWrapper { fn did_output_sample_buffer(&self, sample: Id, of_type: u8) { - if of_type == 1 { // Audio + if of_type == 1 { + // Audio let buffers = sample.get_av_audio_buffer_list(); for buffer in buffers { - // Buffer data is u8, we usually get F32 from SCK if configured. - // Assuming f32 (Floating Point) based on our config. - // We need to convert [u8] to [f32]. let data_u8 = buffer.data; let data_f32: &[f32] = unsafe { - std::slice::from_raw_parts( - data_u8.as_ptr() as *const f32, - data_u8.len() / 4, - ) + std::slice::from_raw_parts(data_u8.as_ptr() as *const f32, data_u8.len() / 4) }; - + (self.callback)(data_f32); } } @@ -52,52 +50,122 @@ pub async fn check_permissions() -> bool { } impl SystemAudioCapture { - pub fn new(sample_rate: u32) -> Self { - Self { stream: None, sample_rate } + pub fn new(sample_rate: u32, excluded_apps: Vec) -> Self { + Self { + stream: None, + sample_rate, + excluded_apps, + } } - pub async fn start(&mut self, callback: F) -> Result<(), String> - where F: Fn(&[f32]) + Send + Sync + 'static { - - let content = UnsafeSCShareableContent::get().map_err(|e| format!("Failed to get content"))?; + async fn build_filter(&self) -> Result, String> { + let content = + UnsafeSCShareableContent::get().map_err(|_| format!("Failed to get content"))?; let displays = content.displays(); let display = displays.first().ok_or("No display found")?; - let filter_init = UnsafeInitParams::Display(display.clone()); - let filter = UnsafeContentFilter::init(filter_init); + if self.excluded_apps.is_empty() { + return Ok(UnsafeContentFilter::init(UnsafeInitParams::Display( + display.clone(), + ))); + } + + let mut apps_to_exclude = Vec::new(); + let all_apps = content.applications(); + + // Prepare lowercase excluded list for case-insensitive matching + let excluded_lower: Vec = self + .excluded_apps + .iter() + .map(|s| s.to_lowercase()) + .collect(); + + for app in all_apps { + if let Some(bid) = app.get_bundle_identifier() { + let bid_lower = bid.to_lowercase(); + // Smart match: check if the running app's ID starts with any blocked ID + // e.g., "com.apple.Safari.WebContent" starts with "com.apple.Safari" + if excluded_lower + .iter() + .any(|excluded| bid_lower.starts_with(excluded)) + { + apps_to_exclude.push(app); + } + } + } + + // NOTE: In screencapturekit-sys 0.2.8, DisplayIncludingApplicationsExceptingWindows + // actually calls initWithDisplay:excludingApplications: (it's a bug in the crate) + let filter_init = UnsafeInitParams::DisplayIncludingApplicationsExceptingWindows( + display.clone(), + apps_to_exclude, + Vec::new(), + ); + + Ok(UnsafeContentFilter::init(filter_init)) + } + + pub async fn start(&mut self, callback: F) -> Result<(), String> + where + F: Fn(&[f32]) + Send + Sync + 'static, + { + let filter = self.build_filter().await?; - // Wait, 'pixel_format' is OSType. b"BGRA" is &[u8;4]. - // FourCharCode::from_chars exists in crate::os_types::four_char_code but we didn't import it. - // Actually, we can just use the Default and overwrite fields. - // But better: use Default and only set what we need. - let mut config = UnsafeStreamConfiguration::default(); config.width = 100; config.height = 100; config.captures_audio = 1; config.sample_rate = self.sample_rate; config.channel_count = 2; - config.excludes_current_process_audio = 0; + config.excludes_current_process_audio = 1; let output_wrapper = AudioOutputWrapper { - callback: Box::new(callback), + callback: Box::new(callback), }; - // Convert config to Id using Into let stream = UnsafeSCStream::init(filter, config.into(), ErrorHandler); - stream.add_stream_output(output_wrapper, 1); // 1 = Audio - - stream.start_capture().map_err(|e| "Failed to start capture".to_string())?; - + + stream + .start_capture() + .map_err(|_| "Failed to start capture".to_string())?; + self.stream = Some(stream); Ok(()) } - + pub fn stop(&mut self) { if let Some(stream) = &self.stream { - stream.stop_capture(); + let _ = stream.stop_capture(); } self.stream = None; } + + pub async fn refresh_filter(&mut self) -> Result<(), String> { + let stream = match &self.stream { + Some(s) => s, + None => return Ok(()), + }; + + let filter = self.build_filter().await?; + + // Call updateContentFilter:completionHandler: + // screencapturekit-sys 0.2.8 does not have this method exposed yet in UnsafeSCStream. + // We use msg_send! to call it directly on the underlying object. + unsafe { + // Get raw pointer to the underlying Objective-C objects. + // Since we don't have easy access to the inner pointer of Id via methods, + // we cast the pointer to the Id wrapper itself to a pointer to a pointer. + // This assumes Id is a transparent wrapper around a pointer. + let stream_ptr = + *(stream as *const Id as *const *mut objc::runtime::Object); + let filter_ptr = + *(&filter as *const Id as *const *mut objc::runtime::Object); + + let _: () = + msg_send![stream_ptr, updateContentFilter: filter_ptr completionHandler: nil]; + } + + Ok(()) + } } diff --git a/src-tauri/src/sc_audio.rs.bak b/src-tauri/src/sc_audio.rs.bak new file mode 100644 index 0000000..f07f0a2 --- /dev/null +++ b/src-tauri/src/sc_audio.rs.bak @@ -0,0 +1,103 @@ +use screencapturekit_sys::{ + os_types::rc::Id, + shareable_content::UnsafeSCShareableContent, + content_filter::{UnsafeContentFilter, UnsafeInitParams}, + stream_configuration::UnsafeStreamConfiguration, + stream::UnsafeSCStream, + stream_error_handler::UnsafeSCStreamError, + stream_output_handler::UnsafeSCStreamOutput, + cm_sample_buffer_ref::CMSampleBufferRef, +}; + +pub struct SystemAudioCapture { + stream: Option>, + sample_rate: u32, +} + +struct AudioOutputWrapper { + callback: Box, +} + +impl UnsafeSCStreamOutput for AudioOutputWrapper { + fn did_output_sample_buffer(&self, sample: Id, of_type: u8) { + if of_type == 1 { // Audio + let buffers = sample.get_av_audio_buffer_list(); + for buffer in buffers { + // Buffer data is u8, we usually get F32 from SCK if configured. + // Assuming f32 (Floating Point) based on our config. + // We need to convert [u8] to [f32]. + let data_u8 = buffer.data; + let data_f32: &[f32] = unsafe { + std::slice::from_raw_parts( + data_u8.as_ptr() as *const f32, + data_u8.len() / 4, + ) + }; + + (self.callback)(data_f32); + } + } + } +} + +struct ErrorHandler; +impl UnsafeSCStreamError for ErrorHandler { + fn handle_error(&self) { + // eprintln!("Stream Error"); + } +} + +pub async fn check_permissions() -> bool { + UnsafeSCShareableContent::get().is_ok() +} + +impl SystemAudioCapture { + pub fn new(sample_rate: u32) -> Self { + Self { stream: None, sample_rate } + } + + pub async fn start(&mut self, callback: F) -> Result<(), String> + where F: Fn(&[f32]) + Send + Sync + 'static { + + let content = UnsafeSCShareableContent::get().map_err(|e| format!("Failed to get content"))?; + let displays = content.displays(); + let display = displays.first().ok_or("No display found")?; + + let filter_init = UnsafeInitParams::Display(display.clone()); + let filter = UnsafeContentFilter::init(filter_init); + + // Wait, 'pixel_format' is OSType. b"BGRA" is &[u8;4]. + // FourCharCode::from_chars exists in crate::os_types::four_char_code but we didn't import it. + // Actually, we can just use the Default and overwrite fields. + // But better: use Default and only set what we need. + + let mut config = UnsafeStreamConfiguration::default(); + config.width = 100; + config.height = 100; + config.captures_audio = 1; + config.sample_rate = self.sample_rate; + config.channel_count = 2; + config.excludes_current_process_audio = 0; + + let output_wrapper = AudioOutputWrapper { + callback: Box::new(callback), + }; + + // Convert config to Id using Into + let stream = UnsafeSCStream::init(filter, config.into(), ErrorHandler); + + stream.add_stream_output(output_wrapper, 1); // 1 = Audio + + stream.start_capture().map_err(|e| "Failed to start capture".to_string())?; + + self.stream = Some(stream); + Ok(()) + } + + pub fn stop(&mut self) { + if let Some(stream) = &self.stream { + stream.stop_capture(); + } + self.stream = None; + } +} diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 0f503e3..bcf12f6 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "https://schema.tauri.app/config/2", "productName": "Hearbit AI", - "version": "1.2.1", + "version": "1.2.3", "identifier": "com.hearbit-ai.desktop", "build": { "beforeDevCommand": "npm run dev", diff --git a/src/App.tsx b/src/App.tsx index a415071..2365481 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -65,6 +65,10 @@ function App() { const [dailyBackupEnabled, setDailyBackupEnabled] = useState(() => localStorage.getItem('hearbit_daily_backup_enabled') === 'true'); const [dailyBackupPath, setDailyBackupPath] = useState(() => localStorage.getItem('hearbit_daily_backup_path') || ''); const [lastBackupDate, setLastBackupDate] = useState(() => localStorage.getItem('hearbit_last_backup_date') || ''); + const [blockedApps, setBlockedApps] = useState(() => { + const saved = localStorage.getItem('hearbit_blocked_apps'); + return saved ? JSON.parse(saved) : []; + }); const handleModelChange = (model: string) => { setSelectedModel(model); @@ -233,7 +237,6 @@ Thanks!` return saved ? JSON.parse(saved) : defaultEmailTemplates; }); - const handleSaveSettings = ( newApiKey: string, newProductId: string, @@ -243,7 +246,8 @@ Thanks!` newAzure: AzureConfig, newEmailTemplates: EmailTemplate[], newDailyBackupEnabled: boolean, - newDailyBackupPath: string + newDailyBackupPath: string, + newBlockedApps: string[] ) => { setApiKey(newApiKey); setProductId(newProductId); @@ -255,6 +259,7 @@ Thanks!` setDailyBackupEnabled(newDailyBackupEnabled); setDailyBackupPath(newDailyBackupPath); + setBlockedApps(newBlockedApps); localStorage.setItem('hearbit_api_key', newApiKey); localStorage.setItem('hearbit_product_id', newProductId); @@ -266,6 +271,7 @@ Thanks!` localStorage.setItem('hearbit_daily_backup_enabled', String(newDailyBackupEnabled)); localStorage.setItem('hearbit_daily_backup_path', newDailyBackupPath); + localStorage.setItem('hearbit_blocked_apps', JSON.stringify(newBlockedApps)); setView(lastTab); }; @@ -477,6 +483,7 @@ Thanks!` selectedModel={selectedModel} onModelChange={handleModelChange} isVisible={view === 'recorder'} + blockedApps={blockedApps} /> @@ -575,6 +582,7 @@ Thanks!` setHistory(newHistory); localStorage.setItem('infomaniak_history', JSON.stringify(newHistory)); }} + blockedApps={blockedApps} /> )} diff --git a/src/components/Recorder.tsx b/src/components/Recorder.tsx index 5216507..4cd93f2 100644 --- a/src/components/Recorder.tsx +++ b/src/components/Recorder.tsx @@ -43,6 +43,7 @@ interface RecorderProps { selectedModel: string; onModelChange: (model: string) => void; isVisible: boolean; + blockedApps: string[]; } interface AudioDevice { @@ -54,7 +55,7 @@ const Recorder: React.FC = ({ apiKey, productId, prompts, setTranscription, setSummary, onSaveToHistory, savePath, onRecordingComplete, - onOpenSettings, addToast, selectedModel, onModelChange, ...props + onOpenSettings, addToast, selectedModel, onModelChange, blockedApps, ...props }) => { const [isRecording, setIsRecording] = useState(false); const [isStopping, setIsStopping] = useState(false); // New lock state @@ -164,7 +165,8 @@ const Recorder: React.FC = ({ savePath: savePath || null, customFilename: props.recordingSubject || null, waitForSpeech: autoStartEnabled, // Pass the toggle state - mode: recordingMode + mode: recordingMode, + excludedApps: blockedApps }); setIsRecording(true); diff --git a/src/components/Settings.tsx b/src/components/Settings.tsx index fa281b1..f1d2df7 100644 --- a/src/components/Settings.tsx +++ b/src/components/Settings.tsx @@ -1,5 +1,5 @@ import React, { useState, useEffect } from 'react'; -import { Save, FolderOpen, Lock, Upload, Download, Mail, FileText, ScrollText } from 'lucide-react'; +import { Save, FolderOpen, Lock, Upload, Download, Mail, FileText, ScrollText, Headphones } from 'lucide-react'; import { save, open } from '@tauri-apps/plugin-dialog'; import { invoke } from '@tauri-apps/api/core'; import EmailTemplateEditor from './EmailTemplateEditor'; @@ -28,10 +28,12 @@ interface SettingsProps { azure: AzureConfig, emailTemplates: EmailTemplate[], dailyBackupEnabled: boolean, - dailyBackupPath: string + dailyBackupPath: string, + blockedApps: string[] ) => void; onHistoryUpdate: (history: any[]) => void; onClose: () => void; + blockedApps: string[]; } export interface SmtpConfig { @@ -48,6 +50,30 @@ export interface AzureConfig { tenantId: string; } +const APP_PRESETS = [ + { + category: 'Browsers', + apps: [ + { name: 'Safari', id: 'com.apple.Safari' }, + { name: 'Chrome', id: 'com.google.Chrome' }, + { name: 'Firefox', id: 'org.mozilla.firefox' }, + { name: 'Arc', id: 'company.thebrowser.Browser' }, + { name: 'Edge', id: 'com.microsoft.edgemac' }, + { name: 'Brave', id: 'com.brave.Browser' }, + ] + }, + { + category: 'Music & Media', + apps: [ + { name: 'Apple Music', id: 'com.apple.Music' }, + { name: 'Spotify', id: 'com.spotify.client' }, + { name: 'Tidal', id: 'com.aspiro.Tidal' }, + { name: 'VLC', id: 'org.videolan.vlc' }, + { name: 'Podcasts', id: 'com.apple.podcasts' }, + ] + } +]; + const Settings: React.FC = ({ apiKey, productId, prompts, savePath, onSave, onClose, ...props }) => { const [localApiKey, setLocalApiKey] = useState(apiKey); const [localProductId, setLocalProductId] = useState(productId); @@ -59,21 +85,34 @@ const Settings: React.FC = ({ apiKey, productId, prompts, savePat const [localAzure, setLocalAzure] = useState(props.azureConfig); const [localDailyBackupEnabled, setLocalDailyBackupEnabled] = useState(props.dailyBackupEnabled); const [localDailyBackupPath, setLocalDailyBackupPath] = useState(props.dailyBackupPath); + const [localBlockedApps, setLocalBlockedApps] = useState(props.blockedApps); + const [runningApps, setRunningApps] = useState<{ name: string, bundle_id: string }[]>([]); const [statusIdx, setStatusIdx] = useState(null); // Email Template Editor State const [editingTemplate, setEditingTemplate] = useState(null); const [isEmailEditorOpen, setIsEmailEditorOpen] = useState(false); - const [activeTab, setActiveTab] = useState<'general' | 'prompts' | 'email' | 'backup' | 'logs'>('general'); + const [activeTab, setActiveTab] = useState<'general' | 'prompts' | 'email' | 'backup' | 'logs' | 'apps'>('general'); const [logs, setLogs] = useState('Loading logs...'); useEffect(() => { if (activeTab === 'logs') { loadLogs(); + } else if (activeTab === 'general') { + loadRunningApps(); } }, [activeTab]); + const loadRunningApps = async () => { + try { + const apps = await invoke<{ name: string, bundle_id: string }[]>('get_running_apps'); + setRunningApps(apps); + } catch (e) { + console.error("Failed to load running apps:", e); + } + }; + const loadLogs = async () => { try { const content = await invoke('read_log_file'); @@ -143,7 +182,8 @@ const Settings: React.FC = ({ apiKey, productId, prompts, savePat localAzure, localEmailTemplates, localDailyBackupEnabled, - localDailyBackupPath + localDailyBackupPath, + localBlockedApps ); onClose(); }; @@ -229,6 +269,7 @@ const Settings: React.FC = ({ apiKey, productId, prompts, savePat if (data.dailyBackup.enabled !== undefined) setLocalDailyBackupEnabled(data.dailyBackup.enabled); if (data.dailyBackup.path) setLocalDailyBackupPath(data.dailyBackup.path); } + if (data.blockedApps) setLocalBlockedApps(data.blockedApps); // Import history! if (data.history && Array.isArray(data.history)) { props.onHistoryUpdate(data.history); @@ -263,6 +304,7 @@ const Settings: React.FC = ({ apiKey, productId, prompts, savePat { id: 'prompts', label: 'Prompts', icon: }, { id: 'email', label: 'Email', icon: }, { id: 'backup', label: 'Backup', icon: }, + { id: 'apps', label: 'Apps', icon: }, { id: 'logs', label: 'Logs', icon: }, ] as const; @@ -676,6 +718,86 @@ const Settings: React.FC = ({ apiKey, productId, prompts, savePat )} + {activeTab === 'apps' && ( +
+
+

+ Recording Filters +

+

+ Block specific apps to keep their audio out of your recordings (e.g., music or browser sounds). +

+
+ +
+ {/* App Presets */} +
+

Quick Select (Classic Apps)

+
+ {APP_PRESETS.map(group => ( +
+
{group.category}
+
+ {group.apps.map(app => ( + + ))} +
+
+ ))} +
+
+ + {/* Running Apps */} +
+

Currently Running

+
+ {runningApps.length > 0 ? ( + runningApps.map(app => ( + + )) + ) : ( +
+ No running applications detected. +
+ )} +
+
+
+
+ )} + {activeTab === 'logs' && (
diff --git a/src/components/Settings.tsx.bak b/src/components/Settings.tsx.bak new file mode 100644 index 0000000..fa281b1 --- /dev/null +++ b/src/components/Settings.tsx.bak @@ -0,0 +1,716 @@ +import React, { useState, useEffect } from 'react'; +import { Save, FolderOpen, Lock, Upload, Download, Mail, FileText, ScrollText } from 'lucide-react'; +import { save, open } from '@tauri-apps/plugin-dialog'; +import { invoke } from '@tauri-apps/api/core'; +import EmailTemplateEditor from './EmailTemplateEditor'; +import logo from '../assets/logo.png'; + +import { PromptTemplate, EmailTemplate } from '../App'; + +interface SettingsProps { + apiKey: string; + productId: string; + savePath: string; + prompts: PromptTemplate[]; + emailTemplates: EmailTemplate[]; + smtpConfig: SmtpConfig; + azureConfig: AzureConfig; + dailyBackupEnabled: boolean; + dailyBackupPath: string; + lastBackupDate: string; + history: any[]; + onSave: ( + apiKey: string, + productId: string, + prompts: PromptTemplate[], + savePath: string, + smtp: SmtpConfig, + azure: AzureConfig, + emailTemplates: EmailTemplate[], + dailyBackupEnabled: boolean, + dailyBackupPath: string + ) => void; + onHistoryUpdate: (history: any[]) => void; + onClose: () => void; +} + +export interface SmtpConfig { + host: string; + port: string; + user: string; + pass: string; + sender: string; + senderName: string; +} + +export interface AzureConfig { + clientId: string; + tenantId: string; +} + +const Settings: React.FC = ({ apiKey, productId, prompts, savePath, onSave, onClose, ...props }) => { + const [localApiKey, setLocalApiKey] = useState(apiKey); + const [localProductId, setLocalProductId] = useState(productId); + const [localSavePath, setLocalSavePath] = useState(savePath); + + const [localPrompts, setLocalPrompts] = useState(prompts); + const [localEmailTemplates, setLocalEmailTemplates] = useState(props.emailTemplates); // New state + const [localSmtp, setLocalSmtp] = useState(props.smtpConfig); + const [localAzure, setLocalAzure] = useState(props.azureConfig); + const [localDailyBackupEnabled, setLocalDailyBackupEnabled] = useState(props.dailyBackupEnabled); + const [localDailyBackupPath, setLocalDailyBackupPath] = useState(props.dailyBackupPath); + const [statusIdx, setStatusIdx] = useState(null); + + // Email Template Editor State + const [editingTemplate, setEditingTemplate] = useState(null); + const [isEmailEditorOpen, setIsEmailEditorOpen] = useState(false); + + const [activeTab, setActiveTab] = useState<'general' | 'prompts' | 'email' | 'backup' | 'logs'>('general'); + const [logs, setLogs] = useState('Loading logs...'); + + useEffect(() => { + if (activeTab === 'logs') { + loadLogs(); + } + }, [activeTab]); + + const loadLogs = async () => { + try { + const content = await invoke('read_log_file'); + setLogs(content); + } catch (e) { + setLogs(`Failed to load logs: ${e}`); + } + }; + + const handleSaveLogs = async () => { + try { + const filePath = await save({ + defaultPath: `hearbit_logs_${new Date().toISOString().slice(0, 10)}.log`, + filters: [{ + name: 'Log File', + extensions: ['log', 'txt'] + }] + }); + + if (filePath) { + await invoke('save_text_file', { path: filePath, content: logs }); + setStatusIdx(`Logs exported to: ${filePath}`); + } + } catch (e) { + console.error(e); + setStatusIdx(`Failed to export logs: ${e}`); + } + }; + + const handlePromptChange = (id: string, field: 'name' | 'content', value: string) => { + setLocalPrompts(localPrompts.map(p => p.id === id ? { ...p, [field]: value } : p)); + }; + + const addPrompt = () => { + setLocalPrompts([...localPrompts, { id: Date.now().toString(), name: 'New Prompt', content: '' }]); + }; + + const removePrompt = (id: string) => { + setLocalPrompts(localPrompts.filter(p => p.id !== id)); + }; + + const handleSaveEmailTemplate = (template: EmailTemplate) => { + const exists = localEmailTemplates.find(t => t.id === template.id); + if (exists) { + setLocalEmailTemplates(localEmailTemplates.map(t => t.id === template.id ? template : t)); + } else { + setLocalEmailTemplates([...localEmailTemplates, template]); + } + }; + + const openEmailEditor = (template: EmailTemplate | null) => { + setEditingTemplate(template); + setIsEmailEditorOpen(true); + }; + + const removeEmailTemplate = (id: string) => { + setLocalEmailTemplates(localEmailTemplates.filter(t => t.id !== id)); + }; + + const handleSave = () => { + onSave( + localApiKey, + localProductId, + localPrompts, + localSavePath, + localSmtp, + localAzure, + localEmailTemplates, + localDailyBackupEnabled, + localDailyBackupPath + ); + onClose(); + }; + + const handleSelectFolder = async () => { + try { + const selected = await open({ + directory: true, + multiple: false, + defaultPath: localSavePath || undefined, + }); + if (selected && typeof selected === 'string') { + setLocalSavePath(selected); + } + } catch (e) { + console.error("Failed to open directory picker", e); + setStatusIdx('Error: Failed to open directory picker.'); + } + }; + + const handleExport = async () => { + try { + const data = { + apiKey: localApiKey, + productId: localProductId, + prompts: localPrompts, + savePath: localSavePath, + smtp: localSmtp, + azure: localAzure, + emailTemplates: localEmailTemplates, + history: props.history, + dailyBackup: { + enabled: localDailyBackupEnabled, + path: localDailyBackupPath, + } + }; + + // Always save as JSON (no encryption) + const content = JSON.stringify(data, null, 2); + + const filePath = await save({ + defaultPath: `hearbit_backup_${new Date().toISOString().slice(0, 10)}.json`, + filters: [{ + name: 'Hearbit Config', + extensions: ['json'] + }] + }); + + if (filePath) { + await invoke('save_text_file', { path: filePath, content }); + setStatusIdx(`Configuration exported to: ${filePath}`); + } + } catch (e) { + console.error(e); + setStatusIdx(`Export failed: ${e}`); + } + }; + + const triggerImport = () => { + document.getElementById('import-file-input')?.click(); + }; + + const handleFileSelect = (e: React.ChangeEvent) => { + const file = e.target.files?.[0]; + if (!file) return; + + const reader = new FileReader(); + reader.onload = (event) => { + if (event.target?.result) { + const content = event.target.result as string; + // Directly import without password modal since we don't use encryption + try { + const data = JSON.parse(content); + + if (data.apiKey) setLocalApiKey(data.apiKey); + if (data.productId) setLocalProductId(data.productId); + if (data.prompts) setLocalPrompts(data.prompts); + if (data.emailTemplates) setLocalEmailTemplates(data.emailTemplates); + if (data.savePath) setLocalSavePath(data.savePath); + if (data.smtp) setLocalSmtp(data.smtp); + if (data.azure) setLocalAzure(data.azure); + if (data.dailyBackup) { + if (data.dailyBackup.enabled !== undefined) setLocalDailyBackupEnabled(data.dailyBackup.enabled); + if (data.dailyBackup.path) setLocalDailyBackupPath(data.dailyBackup.path); + } + // Import history! + if (data.history && Array.isArray(data.history)) { + props.onHistoryUpdate(data.history); + } + + setStatusIdx('Configuration imported! Click Save to apply.'); + } catch (error) { + console.error(error); + setStatusIdx(`Import failed: ${error}`); + } + } + }; + reader.readAsText(file); + e.target.value = ''; + }; + + + + const handleCreateDevice = async () => { + try { + setStatusIdx('Creating Hearbit Audio device...'); + await invoke('create_hearbit_audio_device'); + setStatusIdx('Success! "Hearbit Audio" device created.'); + } catch (e) { + console.error(e); + setStatusIdx(`Error: ${e}`); + } + }; + + const tabs = [ + { id: 'general', label: 'General', icon: }, + { id: 'prompts', label: 'Prompts', icon: }, + { id: 'email', label: 'Email', icon: }, + { id: 'backup', label: 'Backup', icon: }, + { id: 'logs', label: 'Logs', icon: }, + ] as const; + + return ( +
+ {/* Import Password Modal */} + {/* Email Template Editor Modal */} + setIsEmailEditorOpen(false)} + template={editingTemplate} + onSave={handleSaveEmailTemplate} + smtpConfig={localSmtp} + addToast={(msg, type) => setStatusIdx(`${type === 'error' ? 'Error' : 'Success'}: ${msg}`)} + /> + +
+
+

Settings

+ +
+ + {/* Tabs */} +
+ {tabs.map(tab => ( + + ))} +
+
+ +
+ {activeTab === 'general' && ( +
+
+

Application Keys

+
+ + setLocalApiKey(e.target.value)} + className="w-full p-2 rounded border border-border bg-secondary text-foreground focus:ring-2 focus:ring-primary outline-none font-mono text-sm" + /> +
+
+ + setLocalProductId(e.target.value)} + className="w-full p-2 rounded border border-border bg-secondary text-foreground focus:ring-2 focus:ring-primary outline-none font-mono text-sm" + /> +
+
+ +
+

Storage

+
+ +
+ setLocalSavePath(e.target.value)} + placeholder="/Users/username/Desktop/Recordings" + className="flex-1 p-2 rounded border border-border bg-secondary text-foreground focus:ring-2 focus:ring-primary outline-none font-mono text-sm" + /> + +
+
+
+ +
+

πŸ“Έ Branding

+
+
+
Custom Logo
+
Upload your company logo to replace the default Livtec branding throughout the app.
+
+ + {/* Logo Preview */} +
+
+ Logo Preview +
+
+ + +
+
+

Supported: PNG, JPG, SVG. Recommended: Square format, transparent background.

+
+
+ +
+

System Intergration

+
+
+
Hearbit Audio Device
+
Required for recording system audio (Teams, Zoom, etc.)
+
+ +
+
+
+ )} + + {activeTab === 'prompts' && ( +
+
+

AI Prompts

+ +
+
+ {localPrompts.map((prompt) => ( +
+ + handlePromptChange(prompt.id, 'name', e.target.value)} + className="w-full p-1 bg-transparent border-b border-border/50 focus:border-primary outline-none font-semibold text-sm" + placeholder="Prompt Name" + /> +