//! Tauri commands called from the UI. Permission commands live in `permissions.rs`. use crate::config::Settings; use crate::jobs::{self, JobView}; use crate::macos::Rect; use crate::panel::{self, PanelMode}; use crate::{hit, session, windows, AppState, PANEL, SHELF}; use openclicky_core::resources::{MissingFile, ResourceKind}; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; use std::time::Duration; use tauri::{AppHandle, Manager, State}; #[tauri::command] pub fn panel_mode(app: AppHandle) -> PanelMode { panel::mode(&app) } #[tauri::command] pub fn panel_screen(app: AppHandle) -> crate::macos::ScreenInfo { panel::screen(&app) } #[tauri::command] pub fn hide_panel(app: AppHandle) { session::escape(&app); } #[derive(Deserialize)] pub struct HitRect { x: f64, y: f64, w: f64, h: f64, } /// The UI reports where it actually draws, so the rest of the window lets clicks through. #[tauri::command] pub fn hit_rects(app: AppHandle, window: String, rects: Vec) { let label = if window == SHELF { SHELF } else { PANEL }; hit::set_rects(&app, label, rects.into_iter().map(|r| Rect { x: r.x, y: r.y, w: r.w, h: r.h }).collect()); } #[tauri::command] pub fn panel_hover(state: State<'_, AppState>, hovered: bool) { state.session.set_hovered(hovered); } /// The running app bundle (what to drag into a Privacy list). fn app_bundle() -> std::path::PathBuf { let exe = std::env::current_exe().unwrap_or_default(); exe.ancestors() .find(|p| p.extension().map(|e| e == "app").unwrap_or(false)) .map(|p| p.to_path_buf()) .unwrap_or(exe) } /// HeyClicky-style helper: a card under System Settings with a draggable app tile. #[tauri::command] pub fn guide_show(app: AppHandle, id: String) { let state = app.state::(); let icon = state.store.resolve(ResourceKind::Icon, "app-icon-1024").map(|p| p.display().to_string()).unwrap_or_default(); let bundle = app_bundle().display().to_string(); let h = app.clone(); let _ = app.run_on_main_thread(move || { if let Some(w) = h.get_webview_window(crate::GUIDE) { let s = crate::macos::screens().into_iter().next().unwrap_or_default(); let (gw, gh) = (440.0, 130.0); let frame = Rect { x: s.frame.x + (s.frame.w - gw) / 2.0, y: s.frame.y + s.frame.h - gh - 70.0, w: gw, h: gh }; crate::macos::configure_overlay(&w, crate::macos::PANEL_LEVEL); crate::macos::set_frame(&w, frame); crate::macos::order_front_without_focus(&w); } }); crate::emit(&app, openclicky_core::Event::new("guide.show", json!({ "perm": id, "app": bundle, "icon": icon }))); } #[tauri::command] pub fn guide_hide(app: AppHandle) { crate::emit(&app, openclicky_core::Event::new("guide.hide", json!({}))); let h = app.clone(); let _ = app.run_on_main_thread(move || { if let Some(w) = h.get_webview_window(crate::GUIDE) { crate::macos::order_out(&w); } }); } #[tauri::command] pub fn board_close(app: AppHandle) { crate::overlay::board_close(&app); } /// Hovering the resting notch opens the peek; leaving closes it. #[tauri::command] pub fn panel_peek(app: AppHandle, on: bool) { match (on, panel::mode(&app)) { (true, PanelMode::Hidden) => panel::set_mode(&app, PanelMode::Peek), (false, PanelMode::Peek) => panel::set_mode(&app, PanelMode::Hidden), _ => {} } } /// Click-to-talk from the peek (same as holding ⌃⌥). #[tauri::command] pub fn talk_start(app: AppHandle) { session::talk_down(&app); } #[tauri::command] pub fn talk_stop(app: AppHandle) { session::talk_up(&app, false, true); } /// Open the text box (the panel becomes key so it can take typing). #[tauri::command] pub fn panel_type(app: AppHandle) { panel::set_mode(&app, PanelMode::Typing); } #[tauri::command] pub fn ask(app: AppHandle, text: String) -> Result<(), String> { let text = text.trim().to_string(); if text.is_empty() { return Err("Type something first.".into()); } session::ask_text(&app, text); Ok(()) } #[tauri::command] pub fn session_note(state: State<'_, AppState>) -> Value { let src = std::fs::read_to_string(state.data_dir.join("session/note.md")).unwrap_or_default(); if src.trim().is_empty() { return Value::Null; } serde_json::to_value(openclicky_core::note::parse(&src)).unwrap_or(Value::Null) } /// Play a UI sound by slot. Only slots in the manifest can play. #[tauri::command] pub fn play_sound(state: State<'_, AppState>, slot: String) -> Result<(), String> { if state.store.entry(ResourceKind::Sound, &slot).is_none() { return Err(format!("unknown sound slot `{slot}`")); } state.play(&slot); Ok(()) } #[tauri::command] pub fn stop_bed(state: State<'_, AppState>) { if let Ok(mut bus) = state.audio.lock() { bus.stop_bed(); } } #[tauri::command] pub fn show_settings(app: AppHandle, tab: Option) { windows::open_settings(&app, tab.as_deref().unwrap_or("")); } #[derive(Serialize)] pub struct ResourcesReport { root: String, pack: crate::resources::PackSource, sounds: usize, missing: Vec, } #[tauri::command] pub fn resources_report(state: State<'_, AppState>) -> ResourcesReport { ResourcesReport { root: state.store.root().display().to_string(), pack: state.pack, sounds: state.store.manifest().sounds.len(), missing: state.store.missing(), } } /// Design tokens (`theme.json`) for the UI to apply as CSS custom properties. #[tauri::command] pub fn theme(state: State<'_, AppState>) -> Value { state.theme.clone() } // ---- jobs ------------------------------------------------------------------------------ #[tauri::command] pub fn jobs_list(state: State<'_, AppState>) -> Vec { state.jobs.list() } #[tauri::command] pub fn job_get(state: State<'_, AppState>, id: String) -> Option { state.jobs.get(&id) } #[tauri::command] pub fn job_note(app: AppHandle, id: String) -> Option { jobs::note_doc(&app, &id) } #[tauri::command] pub fn job_start(app: AppHandle, title: String, instructions: String) -> Result { jobs::start(&app, &title, &instructions) } #[tauri::command] pub fn job_answer(app: AppHandle, id: String, text: String) -> Result<(), String> { jobs::answer(&app, &id, &text) } #[tauri::command] pub fn job_cancel(app: AppHandle, id: String) { jobs::cancel(&app, &id); } #[tauri::command] pub fn job_dismiss(app: AppHandle, id: String) { jobs::dismiss(&app, &id); } #[tauri::command] pub fn job_delete(app: AppHandle, id: String) { jobs::delete(&app, &id); } #[tauri::command] pub fn open_note(app: AppHandle, id: String) { jobs::dismiss(&app, &id); windows::open_note(&app, &id); } /// Open an artifact (path or URL) the way Finder would. #[tauri::command] pub fn open_target(target: String, reveal: Option) -> Result<(), String> { let mut cmd = std::process::Command::new("/usr/bin/open"); if reveal.unwrap_or(false) { cmd.arg("-R"); } let status = cmd.arg(&target).status().map_err(|e| e.to_string())?; if status.success() { Ok(()) } else { Err(format!("Couldn't open {target}")) } } // ---- settings -------------------------------------------------------------------------- #[derive(Serialize)] pub struct SettingsView { settings: Settings, provider: Option, /// Where the key came from (masked), for the UI. key_hint: String, } fn mask(k: &str) -> String { if k.len() <= 8 { return "••••".into(); } format!("{}••••{}", &k[..4], &k[k.len() - 4..]) } #[tauri::command] pub fn settings_get(state: State<'_, AppState>) -> SettingsView { let settings = state.settings.lock().unwrap().clone(); let cfg = crate::config::provider_config(&state.data_dir, &settings); SettingsView { provider: state.provider().map(|p| p.label()), key_hint: cfg.map(|c| mask(&c.api_key)).unwrap_or_default(), settings: Settings { deepseek_key: mask_if(&settings.deepseek_key), anthropic_key: mask_if(&settings.anthropic_key), openai_key: mask_if(&settings.openai_key), ..settings }, } } fn mask_if(k: &str) -> String { if k.is_empty() { String::new() } else { mask(k) } } /// Merge a partial settings object. Masked keys coming back unchanged are ignored. #[tauri::command] pub fn settings_set(app: AppHandle, patch: Value) -> Result { let state = app.state::(); { let mut s = state.settings.lock().unwrap(); let mut cur = serde_json::to_value(&*s).map_err(|e| e.to_string())?; if let (Some(obj), Some(p)) = (cur.as_object_mut(), patch.as_object()) { for (k, v) in p { if (k == "deepseek_key" || k == "anthropic_key" || k == "openai_key") && v.as_str().map(|x| x.contains('•')).unwrap_or(false) { continue; } obj.insert(k.clone(), v.clone()); } } *s = serde_json::from_value(cur).map_err(|e| e.to_string())?; s.save(&state.data_dir); if let Ok(mut bus) = state.audio.lock() { bus.set_muted(s.muted); } } state.reload_provider(); Ok(settings_get(state)) } /// Live model list for a provider (uses that provider's key). #[tauri::command] pub async fn models_available(app: AppHandle, provider: String) -> Result { let state = app.state::(); let settings = state.settings.lock().unwrap().clone(); let key = crate::config::key_for(&state.data_dir, &settings, &provider).ok_or("No key for this provider yet.")?; let http = reqwest::Client::builder().timeout(Duration::from_secs(15)).build().map_err(|e| e.to_string())?; let req = match provider.as_str() { "claude" => http.get("https://api.anthropic.com/v1/models?limit=100").header("x-api-key", &key).header("anthropic-version", "2023-06-01"), "openai" => http.get("https://api.openai.com/v1/models").bearer_auth(&key), "deepseek" => http.get("https://api.deepseek.com/models").bearer_auth(&key), _ => return Err("Unknown provider.".into()), }; let resp = req.send().await.map_err(|e| e.to_string())?; if !resp.status().is_success() { return Err(format!("{} answered {}", provider, resp.status())); } let v: Value = resp.json().await.map_err(|e| e.to_string())?; let mut ids: Vec = v["data"] .as_array() .cloned() .unwrap_or_default() .into_iter() .filter_map(|m| { let id = m["id"].as_str()?.to_string(); // Chat-capable models only for OpenAI. if provider == "openai" && !(id.starts_with("gpt-") || id.starts_with('o') || id.starts_with("chatgpt")) { return None; } if provider == "openai" && (id.contains("audio") || id.contains("realtime") || id.contains("tts") || id.contains("transcribe") || id.contains("image")) { return None; } Some(json!({ "id": id, "name": m["display_name"].as_str().or(m["name"].as_str()).unwrap_or(""), "vision": m["input_modalities"].as_array().map(|a| a.iter().any(|x| x == "image")) })) }) .collect(); ids.sort_by(|a, b| a["id"].as_str().cmp(&b["id"].as_str())); Ok(json!({ "models": ids })) } #[tauri::command] pub fn usage_summary(app: AppHandle, days: Option) -> Value { let state = app.state::(); let settings = state.settings.lock().unwrap().clone(); let lines = crate::usage::read(&state.data_dir); crate::usage::summary(&lines, &settings, days.unwrap_or(30), chrono::Utc::now().timestamp_millis()) } #[tauri::command] pub fn coders_status() -> Value { let found = openclicky_tools::coders::detect(); let agents: Vec = ["codex", "claude", "opencode"] .iter() .map(|name| { let c = found.iter().find(|c| c.name == *name); json!({ "id": name, "label": match *name { "codex" => "Codex CLI", "claude" => "Claude Code", _ => "opencode" }, "installed": c.is_some(), "path": c.map(|c| c.path.display().to_string()), "version": c.map(openclicky_tools::coders::version), }) }) .collect(); let t3 = std::path::Path::new("/Applications/T3 Code (Nightly).app").exists() || std::path::Path::new("/Applications/T3 Code.app").exists(); json!({ "agents": agents, "t3code": t3 }) } /// One tiny request to prove the key works. #[tauri::command] pub async fn provider_test(app: AppHandle) -> Result { let state = app.state::(); let p = state.provider().ok_or("No key yet.")?; let mut req = openclicky_llm::Request::new("Reply with exactly: ready", vec![openclicky_llm::Message::user_text("ping")]); req.max_tokens = 20; let started = std::time::Instant::now(); let r = tokio::time::timeout(Duration::from_secs(20), p.complete(&req, &mut |_| {})) .await .map_err(|_| "No answer in 20 s.".to_string())? .map_err(|e| e.to_string())?; Ok(format!("{} answered in {} ms: “{}”", p.label(), started.elapsed().as_millis(), r.text().trim())) } #[tauri::command] pub async fn voices(app: AppHandle) -> Result { app.state::().helper.call("voices", json!({}), Duration::from_secs(5)).await } #[tauri::command] pub fn voice_test(app: AppHandle, text: Option) { let line = text.unwrap_or_else(|| "Hi! Hold control and option, and ask me anything about your screen.".into()); crate::speech::speak(&app, "test", &line); } #[tauri::command] pub fn voice_status(app: AppHandle) -> crate::kokoro::Status { crate::kokoro::status(&app) } #[tauri::command] pub fn voice_install(app: AppHandle) -> Result<(), String> { crate::kokoro::install(app) } #[tauri::command] pub fn onboarding_done(app: AppHandle) { let state = app.state::(); let mut s = state.settings.lock().unwrap(); s.onboarded = true; s.save(&state.data_dir); } #[tauri::command] pub fn status(app: AppHandle) -> Value { let st = app.state::(); json!({ "helper": st.helper.available(), "provider": st.provider().map(|p| p.label()), "browser": crate::browser::connected(&app), "connectors": st.connected_apps(), "data_dir": st.data_dir, "version": env!("CARGO_PKG_VERSION"), }) } /// The wizard is a normal window: the user asked for it, so it may take focus. pub fn open_permissions_window(app: &AppHandle) { windows::open_settings(app, "setup"); }