//! The app side of the tools: speech, pointer, Accessibility, plan/progress, asking, jobs, //! the browser and connectors. use crate::{browser, emit, jobs, overlay, speech, AppState}; use openclicky_core::prompt::Mode; use openclicky_core::Event; use openclicky_llm::ToolDef; use openclicky_tools::{Host, ToolCtx, ToolOutput}; use serde_json::{json, Value}; use std::time::Duration; use tauri::{AppHandle, Manager}; pub struct AppHost { pub app: AppHandle, } fn s<'a>(v: &'a Value, k: &str) -> &'a str { v.get(k).and_then(Value::as_str).unwrap_or("") } #[async_trait::async_trait] impl Host for AppHost { async fn call(&self, ctx: &ToolCtx, name: &str, input: &Value) -> Option { let app = &self.app; let st = app.state::(); let job = ctx.job_id.as_deref(); let out: ToolOutput = match name { "highlight" => overlay::highlight(app, input).into(), "draw" => overlay::draw(app, input).into(), "say" => { let text = s(input, "text"); let id = format!("say-{}", rand::random::()); emit(app, Event::new("speech.segment", json!({ "id": id, "text": text, "job": job }))); if st.settings.lock().map(|s| s.speak_answers).unwrap_or(true) { speech::speak(app, &id, text); } ToolOutput::ok("Said it.") } "mac_step" if matches!(s(input, "action"), "click" | "double_click" | "right_click" | "drag") => { if job.is_some() { return Some(ToolOutput::err("Background jobs don't click around in their apps; ask them, or do it in voice.")); } crate::buddy::act(app, s(input, "action"), input).await.into() } "mac_step" => { let action = s(input, "action"); if job.is_some() && !matches!(action, "open") { return Some(ToolOutput::err("Background jobs don't click around in their apps; ask them, or do it in voice.")); } match st.helper.call("mac_step", input.clone(), Duration::from_secs(10)).await { Ok(v) => { // Show where it happened. if let Some(f) = v.get("frame") { let _ = overlay::highlight(app, &json!({ "x": f["x"], "y": f["y"], "w": f["w"], "h": f["h"], "label": v.get("pressed").or(v.get("label")).cloned().unwrap_or(Value::Null) })); } ToolOutput::ok(v.to_string()) } Err(e) => ToolOutput::err(e), } } "start_job" => match jobs::start(app, s(input, "title"), s(input, "instructions")) { Ok(id) => ToolOutput::ok(format!("Started job {id}. It's on a card at the right edge of the screen; tell them in a few words.")), Err(e) => ToolOutput::err(e), }, "update_plan" => match job { Some(id) => jobs::set_plan(app, id, input).into(), None => ToolOutput::err("update_plan only works inside a job."), }, "progress" => match job { Some(id) => jobs::set_progress(app, id, s(input, "note")).into(), None => ToolOutput::ok("ok"), }, "ask_user" => match job { Some(id) => { let choices = input .get("choices") .and_then(Value::as_array) .map(|a| a.iter().filter_map(Value::as_str).map(str::to_string).collect()) .unwrap_or_default(); jobs::ask(app, id, s(input, "question"), choices).await.into() } None => ToolOutput::err("In voice, just ask them out loud."), }, "browser" => browser::call(app, input).await.into(), other if other.starts_with("mcp__") => crate::connectors::call(app, other, input).await.into(), _ => return None, }; Some(out) } fn note_changed(&self, ctx: &ToolCtx) { let src = std::fs::read_to_string(&ctx.note_path).unwrap_or_default(); let doc = openclicky_core::note::parse(&src); match &ctx.job_id { Some(id) => { emit(&self.app, Event::with_job("job.note", id, json!({ "doc": doc }))); self.app.state::().play_quiet("pen-flick"); } None => { emit(&self.app, Event::new("session.note", json!({ "doc": doc }))); } } } fn extra_tools(&self, mode: Mode) -> Vec { if mode == Mode::Job || mode == Mode::Voice { crate::connectors::tools(&self.app) } else { vec![] } } }