|
| 1 | +use tauri::{AppHandle, State}; |
| 2 | +use tauri_plugin_shell::process::CommandEvent; |
| 3 | + |
| 4 | +use crate::owpenbot::manager::OwpenbotManager; |
| 5 | +use crate::owpenbot::spawn::spawn_owpenbot; |
| 6 | +use crate::types::OwpenbotInfo; |
| 7 | +use crate::utils::truncate_output; |
| 8 | + |
| 9 | +#[tauri::command] |
| 10 | +pub fn owpenbot_info(manager: State<OwpenbotManager>) -> OwpenbotInfo { |
| 11 | + let mut state = manager.inner.lock().expect("owpenbot mutex poisoned"); |
| 12 | + OwpenbotManager::snapshot_locked(&mut state) |
| 13 | +} |
| 14 | + |
| 15 | +#[tauri::command] |
| 16 | +pub fn owpenbot_start( |
| 17 | + app: AppHandle, |
| 18 | + manager: State<OwpenbotManager>, |
| 19 | + workspace_path: String, |
| 20 | + opencode_url: Option<String>, |
| 21 | +) -> Result<OwpenbotInfo, String> { |
| 22 | + let mut state = manager |
| 23 | + .inner |
| 24 | + .lock() |
| 25 | + .map_err(|_| "owpenbot mutex poisoned".to_string())?; |
| 26 | + OwpenbotManager::stop_locked(&mut state); |
| 27 | + |
| 28 | + let (mut rx, child) = spawn_owpenbot(&app, &workspace_path, opencode_url.as_deref())?; |
| 29 | + |
| 30 | + state.child = Some(child); |
| 31 | + state.child_exited = false; |
| 32 | + state.workspace_path = Some(workspace_path); |
| 33 | + state.opencode_url = opencode_url; |
| 34 | + state.last_stdout = None; |
| 35 | + state.last_stderr = None; |
| 36 | + |
| 37 | + let state_handle = manager.inner.clone(); |
| 38 | + |
| 39 | + tauri::async_runtime::spawn(async move { |
| 40 | + while let Some(event) = rx.recv().await { |
| 41 | + match event { |
| 42 | + CommandEvent::Stdout(line_bytes) => { |
| 43 | + let line = String::from_utf8_lossy(&line_bytes).to_string(); |
| 44 | + if let Ok(mut state) = state_handle.try_lock() { |
| 45 | + let next = state |
| 46 | + .last_stdout |
| 47 | + .as_deref() |
| 48 | + .unwrap_or_default() |
| 49 | + .to_string() |
| 50 | + + &line; |
| 51 | + state.last_stdout = Some(truncate_output(&next, 8000)); |
| 52 | + |
| 53 | + // Check for WhatsApp linked status in output |
| 54 | + if line.contains("WhatsApp linked") { |
| 55 | + state.whatsapp_linked = true; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + CommandEvent::Stderr(line_bytes) => { |
| 60 | + let line = String::from_utf8_lossy(&line_bytes).to_string(); |
| 61 | + if let Ok(mut state) = state_handle.try_lock() { |
| 62 | + let next = state |
| 63 | + .last_stderr |
| 64 | + .as_deref() |
| 65 | + .unwrap_or_default() |
| 66 | + .to_string() |
| 67 | + + &line; |
| 68 | + state.last_stderr = Some(truncate_output(&next, 8000)); |
| 69 | + } |
| 70 | + } |
| 71 | + CommandEvent::Terminated(payload) => { |
| 72 | + if let Ok(mut state) = state_handle.try_lock() { |
| 73 | + state.child_exited = true; |
| 74 | + if let Some(code) = payload.code { |
| 75 | + let next = format!("Owpenbot exited (code {code})."); |
| 76 | + state.last_stderr = Some(truncate_output(&next, 8000)); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + CommandEvent::Error(message) => { |
| 81 | + if let Ok(mut state) = state_handle.try_lock() { |
| 82 | + state.child_exited = true; |
| 83 | + let next = state |
| 84 | + .last_stderr |
| 85 | + .as_deref() |
| 86 | + .unwrap_or_default() |
| 87 | + .to_string() |
| 88 | + + &message; |
| 89 | + state.last_stderr = Some(truncate_output(&next, 8000)); |
| 90 | + } |
| 91 | + } |
| 92 | + _ => {} |
| 93 | + } |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + Ok(OwpenbotManager::snapshot_locked(&mut state)) |
| 98 | +} |
| 99 | + |
| 100 | +#[tauri::command] |
| 101 | +pub fn owpenbot_stop(manager: State<OwpenbotManager>) -> Result<OwpenbotInfo, String> { |
| 102 | + let mut state = manager |
| 103 | + .inner |
| 104 | + .lock() |
| 105 | + .map_err(|_| "owpenbot mutex poisoned".to_string())?; |
| 106 | + OwpenbotManager::stop_locked(&mut state); |
| 107 | + Ok(OwpenbotManager::snapshot_locked(&mut state)) |
| 108 | +} |
| 109 | + |
| 110 | +#[tauri::command] |
| 111 | +pub async fn owpenbot_qr(app: AppHandle) -> Result<String, String> { |
| 112 | + use tauri_plugin_shell::ShellExt; |
| 113 | + |
| 114 | + let command = match app.shell().sidecar("owpenbot") { |
| 115 | + Ok(command) => command, |
| 116 | + Err(_) => app.shell().command("owpenbot"), |
| 117 | + }; |
| 118 | + |
| 119 | + let output = command |
| 120 | + .args(["whatsapp", "qr", "--format", "base64", "--json"]) |
| 121 | + .output() |
| 122 | + .await |
| 123 | + .map_err(|e| format!("Failed to get QR code: {e}"))?; |
| 124 | + |
| 125 | + if !output.status.success() { |
| 126 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 127 | + return Err(format!("Failed to get QR code: {stderr}")); |
| 128 | + } |
| 129 | + |
| 130 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 131 | + |
| 132 | + // Parse JSON response |
| 133 | + #[derive(serde::Deserialize)] |
| 134 | + struct QrResponse { |
| 135 | + qr: Option<String>, |
| 136 | + error: Option<String>, |
| 137 | + } |
| 138 | + |
| 139 | + let response: QrResponse = |
| 140 | + serde_json::from_str(&stdout).map_err(|e| format!("Failed to parse QR response: {e}"))?; |
| 141 | + |
| 142 | + if let Some(error) = response.error { |
| 143 | + return Err(error); |
| 144 | + } |
| 145 | + |
| 146 | + response.qr.ok_or_else(|| "No QR code returned".to_string()) |
| 147 | +} |
0 commit comments