//! Agent integrations: hook extensions that let agents report their own //! state (herdr's integration model), so detection does not rely on screen //! scraping for the agents that support it. use std::path::PathBuf; use detect_core::Agent; /// The embedded pi extension, adapted from herdr's `herdr-agent-state.ts` /// (Apache-2.0, Copyright herdr contributors). const PI_EXTENSION: &str = include_str!("../assets/pi/pi-agent-state.ts"); fn pi_extension_dir() -> Result { let base = std::env::var_os("PI_CODING_AGENT_DIR") .map(PathBuf::from) .or_else(|| std::env::var_os("HOME").map(|home| PathBuf::from(home).join(".pi").join("agent"))) .ok_or("cannot determine pi agent dir (no HOME)")?; Ok(base.join("extensions")) } /// Install the integration for one agent. Only pi has an integration today. pub fn install(agent: &str) -> Result<(), String> { let parsed = detect_core::parse_agent_label(agent) .ok_or_else(|| format!("unknown agent: {agent}"))?; match parsed { Agent::Pi => install_pi(), other => Err(format!( "no hook integration for {} yet — screen detection is used", detect_core::agent_label(other) )), } } fn install_pi() -> Result<(), String> { let dir = pi_extension_dir()?; std::fs::create_dir_all(&dir).map_err(|err| format!("mkdir failed: {err}"))?; let path = dir.join("dingger-agent-state.ts"); std::fs::write(&path, PI_EXTENSION).map_err(|err| format!("write failed: {err}"))?; tracing::info!(path = %path.display(), "installed pi extension"); println!( "installed {} — new pi sessions report their state directly;\nrestart running pi agents (or reload them) to pick it up", path.display() ); Ok(()) }