|
| 1 | +//! The type's config, and the launch a session runs with: the desktop's |
| 2 | +//! `build_launch_block_for_policy` for what this type configures. |
| 3 | +
|
| 4 | +use buzz_host::api::{Launch, LaunchSpec}; |
| 5 | +use serde_json::{Value, json}; |
| 6 | +use std::collections::BTreeMap; |
| 7 | + |
| 8 | +/// The only provider whose sessions belong to threads. |
| 9 | +pub const PROVIDER: &str = "kubernetes"; |
| 10 | + |
| 11 | +/// A harness the agent's image runs, as the desktop's catalog has it. |
| 12 | +pub struct Runtime { |
| 13 | + pub id: &'static str, |
| 14 | + pub label: &'static str, |
| 15 | + /// Found on the image's `PATH`. |
| 16 | + command: &'static str, |
| 17 | + args: &'static [&'static str], |
| 18 | + default_env: &'static [(&'static str, &'static str)], |
| 19 | +} |
| 20 | + |
| 21 | +pub const RUNTIMES: &[Runtime] = &[ |
| 22 | + Runtime { |
| 23 | + id: "claude", |
| 24 | + label: "Claude Code", |
| 25 | + command: "claude-agent-acp", |
| 26 | + args: &[], |
| 27 | + default_env: &[], |
| 28 | + }, |
| 29 | + Runtime { |
| 30 | + id: "codex", |
| 31 | + label: "Codex", |
| 32 | + command: "codex-acp", |
| 33 | + args: &[], |
| 34 | + default_env: &[], |
| 35 | + }, |
| 36 | + Runtime { |
| 37 | + id: "goose", |
| 38 | + label: "Goose", |
| 39 | + command: "goose", |
| 40 | + args: &["acp"], |
| 41 | + default_env: &[("GOOSE_MODE", "auto")], |
| 42 | + }, |
| 43 | + Runtime { |
| 44 | + id: "buzz-agent", |
| 45 | + label: "Buzz agent", |
| 46 | + command: "buzz-agent", |
| 47 | + args: &[], |
| 48 | + default_env: &[("MCP_HOOK_SERVERS", "*")], |
| 49 | + }, |
| 50 | +]; |
| 51 | + |
| 52 | +/// What the type saves for each agent. |
| 53 | +#[derive(Clone, Debug, PartialEq)] |
| 54 | +pub struct Config { |
| 55 | + pub provider: String, |
| 56 | + /// The provider's public config; always a thread Sandbox. |
| 57 | + pub provider_config: Value, |
| 58 | + /// The user's environment for the agent, which wins over the launch's. |
| 59 | + pub env: BTreeMap<String, String>, |
| 60 | + /// One of [`RUNTIMES`]. |
| 61 | + pub runtime: String, |
| 62 | + /// Empty uses the harness's default. |
| 63 | + pub model: String, |
| 64 | + pub instructions: String, |
| 65 | + pub parallelism: u32, |
| 66 | +} |
| 67 | + |
| 68 | +impl Default for Config { |
| 69 | + fn default() -> Self { |
| 70 | + Self { |
| 71 | + provider: PROVIDER.into(), |
| 72 | + provider_config: json!({ "sandbox": true }), |
| 73 | + env: BTreeMap::new(), |
| 74 | + runtime: RUNTIMES[0].id.into(), |
| 75 | + model: String::new(), |
| 76 | + instructions: String::new(), |
| 77 | + parallelism: 1, |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl Config { |
| 83 | + /// The saved config; what is missing or malformed is the default. |
| 84 | + pub fn read(value: &Value) -> Self { |
| 85 | + let default = Self::default(); |
| 86 | + let text = |name: &str| value.get(name).and_then(Value::as_str).map(str::to_owned); |
| 87 | + let mut provider_config = value |
| 88 | + .get("provider_config") |
| 89 | + .filter(|config| config.is_object()) |
| 90 | + .cloned() |
| 91 | + .unwrap_or(default.provider_config); |
| 92 | + provider_config["sandbox"] = Value::Bool(true); |
| 93 | + let env = value |
| 94 | + .get("env") |
| 95 | + .and_then(Value::as_object) |
| 96 | + .map(|env| { |
| 97 | + env.iter() |
| 98 | + .filter_map(|(key, value)| Some((key.clone(), value.as_str()?.to_owned()))) |
| 99 | + .collect() |
| 100 | + }) |
| 101 | + .unwrap_or_default(); |
| 102 | + let runtime = text("runtime") |
| 103 | + .filter(|id| RUNTIMES.iter().any(|runtime| runtime.id == id)) |
| 104 | + .unwrap_or(default.runtime); |
| 105 | + Self { |
| 106 | + provider: text("provider").unwrap_or(default.provider), |
| 107 | + provider_config, |
| 108 | + env, |
| 109 | + runtime, |
| 110 | + model: text("model").unwrap_or_default(), |
| 111 | + instructions: text("instructions").unwrap_or_default(), |
| 112 | + parallelism: value |
| 113 | + .get("parallelism") |
| 114 | + .and_then(Value::as_u64) |
| 115 | + .and_then(|n| u32::try_from(n).ok()) |
| 116 | + .filter(|n| *n > 0) |
| 117 | + .unwrap_or(default.parallelism), |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + pub fn value(&self) -> Value { |
| 122 | + json!({ |
| 123 | + "provider": self.provider, |
| 124 | + "provider_config": self.provider_config, |
| 125 | + "env": self.env, |
| 126 | + "runtime": self.runtime, |
| 127 | + "model": self.model, |
| 128 | + "instructions": self.instructions, |
| 129 | + "parallelism": self.parallelism, |
| 130 | + }) |
| 131 | + } |
| 132 | + |
| 133 | + /// Whether the host will accept it, as far as the GUI can tell. |
| 134 | + pub fn validate(&self) -> Result<(), String> { |
| 135 | + if self.provider != PROVIDER { |
| 136 | + return Err(format!( |
| 137 | + "Remote workspaces run on the {PROVIDER} provider only." |
| 138 | + )); |
| 139 | + } |
| 140 | + buzz_remote_sessions::validate_provider_config(&self.provider_config)?; |
| 141 | + if let Some(key) = self.env.keys().find(|key| !env_name(key)) { |
| 142 | + return Err(format!("{key:?} is not an environment variable name.")); |
| 143 | + } |
| 144 | + Ok(()) |
| 145 | + } |
| 146 | + |
| 147 | + fn runtime(&self) -> &'static Runtime { |
| 148 | + RUNTIMES |
| 149 | + .iter() |
| 150 | + .find(|runtime| runtime.id == self.runtime) |
| 151 | + .unwrap_or(&RUNTIMES[0]) |
| 152 | + } |
| 153 | + |
| 154 | + /// What the host launches for agent `name`. `team` is the agent's |
| 155 | + /// team section, if it has one. |
| 156 | + pub fn spec(&self, name: &str, team: Option<&str>) -> LaunchSpec { |
| 157 | + let runtime = self.runtime(); |
| 158 | + let claude = runtime.id == "claude"; |
| 159 | + let mut policy_env: BTreeMap<String, String> = runtime |
| 160 | + .default_env |
| 161 | + .iter() |
| 162 | + .map(|(key, value)| (key.to_string(), value.to_string())) |
| 163 | + .collect(); |
| 164 | + let mut set = |key: &str, value: String| policy_env.insert(key.into(), value); |
| 165 | + set("BUZZ_ACP_RELAY_OBSERVER", "true".into()); |
| 166 | + set("BUZZ_ACP_LAZY_POOL", "true".into()); |
| 167 | + set("BUZZ_ACP_AGENTS", self.parallelism.to_string()); |
| 168 | + // Each Sandbox is one thread's. |
| 169 | + set(SESSION_POLICY, "thread".into()); |
| 170 | + if !self.instructions.trim().is_empty() { |
| 171 | + set("BUZZ_ACP_SYSTEM_PROMPT", self.instructions.clone()); |
| 172 | + } |
| 173 | + if !self.model.trim().is_empty() { |
| 174 | + // Claude reads its startup model from ANTHROPIC_MODEL. |
| 175 | + let key = if claude { |
| 176 | + "ANTHROPIC_MODEL" |
| 177 | + } else { |
| 178 | + "BUZZ_ACP_MODEL" |
| 179 | + }; |
| 180 | + set(key, self.model.trim().into()); |
| 181 | + } |
| 182 | + let title: String = name.chars().filter(|c| !c.is_control()).collect(); |
| 183 | + if !title.trim().is_empty() { |
| 184 | + set("BUZZ_ACP_SESSION_TITLE", title.trim().into()); |
| 185 | + set("BUZZ_ACP_DISPLAY_NAME", title.trim().into()); |
| 186 | + } |
| 187 | + if let Some(team) = team.filter(|team| !team.trim().is_empty()) { |
| 188 | + set("BUZZ_ACP_TEAM_INSTRUCTIONS", team.into()); |
| 189 | + } |
| 190 | + // The user's environment wins over policy_env, so it may not name |
| 191 | + // the session policy, nor for Claude a second startup model. |
| 192 | + let env = self |
| 193 | + .env |
| 194 | + .iter() |
| 195 | + .filter(|(key, _)| { |
| 196 | + !key.eq_ignore_ascii_case(SESSION_POLICY) |
| 197 | + && !(claude |
| 198 | + && (key.eq_ignore_ascii_case("BUZZ_ACP_MODEL") |
| 199 | + || key.eq_ignore_ascii_case("ANTHROPIC_MODEL"))) |
| 200 | + }) |
| 201 | + .map(|(key, value)| (key.clone(), value.clone())) |
| 202 | + .collect(); |
| 203 | + LaunchSpec { |
| 204 | + provider: self.provider.clone(), |
| 205 | + provider_config_json: self.provider_config.to_string(), |
| 206 | + env, |
| 207 | + launch: Launch { |
| 208 | + command: runtime.command.into(), |
| 209 | + args: runtime.args.iter().map(|arg| arg.to_string()).collect(), |
| 210 | + env: BTreeMap::new(), |
| 211 | + policy_env, |
| 212 | + }, |
| 213 | + name: name.into(), |
| 214 | + } |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +const SESSION_POLICY: &str = "BUZZ_ACP_SESSION_POLICY"; |
| 219 | + |
| 220 | +/// `[A-Za-z_][A-Za-z0-9_]*`, which the provider requires. |
| 221 | +fn env_name(key: &str) -> bool { |
| 222 | + let mut chars = key.chars(); |
| 223 | + chars |
| 224 | + .next() |
| 225 | + .is_some_and(|c| c == '_' || c.is_ascii_alphabetic()) |
| 226 | + && chars.all(|c| c == '_' || c.is_ascii_alphanumeric()) |
| 227 | +} |
0 commit comments