pub mod detect;
pub mod notify;
pub mod stop;
use std::path::Path;
use anyhow::Context as _;
pub fn run(owner_dir: &Path) -> anyhow::Result<()> {
let input = detect::parse_stdin().context("failed to parse hook stdin JSON")?;
let sid = match &input.session_id {
Some(s) if !s.is_empty() => s.clone(),
_ => {
return Ok(());
}
};
let decision = detect::classify(&input, owner_dir)?;
match decision {
detect::Decision::Skip => {
}
detect::Decision::NotifyOnly { ref message } => {
let log_msg = format!(
"notify-only sid={} msg={}",
&sid[..sid.len().min(8)],
message
);
notify::emit_osc777(message).unwrap_or(()); let _ = notify::append_log(&sid, &log_msg, owner_dir); }
detect::Decision::LimitSwitch {
ref message,
ref target_profile,
ref handoff,
ref cwd,
born,
} => {
notify::emit_osc777(message).unwrap_or(());
let log_msg = format!(
"limit-switch sid={} to={} cwd={} hop={}",
&sid[..sid.len().min(8)],
target_profile,
cwd,
born,
);
let _ = notify::append_log(&sid, &log_msg, owner_dir);
stop::commit_and_stop(sid.as_str(), target_profile, handoff, cwd, born, owner_dir)
.with_context(|| format!("commit_and_stop failed for session {sid}"))?;
}
}
Ok(())
}