//! The agents a session may run, set on the page (Settings ▸ Agents). //! //! Claude Code is always there, as `claude`, through `midas acp` (or the //! runner's `MIDAS_AGENT`). Each other agent is a name and a shell command //! that speaks the Agent Client Protocol on its stdin and stdout, which the //! sessions runner starts in the session's folder, on its own machine, with //! that machine's login for it. A session keeps the id it was started with //! (`sessions::start`), so it is reopened on the same agent. //! //! Setting a command is running it on the runner's machine, so it takes the //! admin role (every `settings/` path does, `hub.rs`); the page starting a //! session sends only the id. use std::collections::BTreeMap; use std::path::PathBuf; use anyhow::{bail, Result}; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; use crate::{events, registry, store}; /// Claude Code's id: the agent a session runs unless another is chosen. pub const CLAUDE: &str = "claude"; #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] pub struct Agent { /// What the page calls it, e.g. `Codex`. #[serde(default)] pub name: String, /// A shell command on the runner's machine, e.g. `bunx @zed-industries/codex-acp`. #[serde(default)] pub command: String, } type Agents = BTreeMap; fn path() -> PathBuf { registry::root().join("agents.json") } fn read() -> Agents { store::read(&path()).unwrap_or_default() } /// Every agent, Claude Code first; with `commands`, what the runner starts for each /// (none for Claude Code, whose command is the runner's own). Only Settings sees /// those: a command may carry a key in its environment. pub fn list(commands: bool) -> Value { let mut all = vec![json!({"id": CLAUDE, "name": "Claude Code"})]; all.extend(read().into_iter().map(|(id, a)| { let mut v = json!({"id": id, "name": a.name}); if commands { v["command"] = json!(a.command); } v })); json!(all) } /// The command the runner starts for agent `id`. pub fn command(id: &str) -> Result { match read().remove(id) { Some(a) => Ok(a.command), None => bail!("the hub has no agent {id} any more (Settings ▸ Agents)"), } } /// `id` as one a session may be started with: none for Claude Code, so a /// session from before there was a choice and one that names it read alike. pub fn chosen(id: Option<&str>) -> Result> { let Some(id) = id.map(str::trim).filter(|a| !a.is_empty() && *a != CLAUDE) else { return Ok(None) }; if !read().contains_key(id) { bail!("no agent {id} on this hub: add it in Settings ▸ Agents"); } Ok(Some(id.to_string())) } fn id_ok(id: &str) -> bool { !id.is_empty() && id.len() <= 32 && id.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-') } /// Add an agent or change one: `name` and `command`, both one line. pub fn set(id: &str, name: &str, command: &str, by: &str) -> Result<()> { let (id, name, command) = (id.trim(), name.trim(), command.trim()); if id == CLAUDE { bail!("Claude Code is built in; a runner's MIDAS_AGENT gives it another command"); } if !id_ok(id) { bail!("an agent's id is lowercase letters, digits and - (at most 32), not {id:?}"); } if name.is_empty() || name.len() > 40 || name.contains('\n') { bail!("an agent's name is one short line"); } if command.is_empty() || command.len() > 1000 || command.contains('\n') { bail!("an agent's command is one line: the program that speaks ACP, with its arguments"); } let mut s = store::Store::::open(path())?.private(); s.data.insert(id.to_string(), Agent { name: name.to_string(), command: command.to_string() }); s.save()?; events::emit("agent_config", json!({"agent": id, "actor": by})); Ok(()) } pub fn remove(id: &str, by: &str) -> Result { let mut s = store::Store::::open(path())?.private(); let gone = s.data.remove(id).is_some(); s.save()?; if gone { events::emit("agent_config", json!({"agent": id, "removed": true, "actor": by})); } Ok(gone) }