|
| 1 | +use acp_nats::{AcpPrefix, AcpPrefixError, Config, NatsConfig}; |
| 2 | +use clap::Parser; |
| 3 | +use std::time::Duration; |
| 4 | +use tracing::warn; |
| 5 | +use trogon_std::env::ReadEnv; |
| 6 | + |
| 7 | +const MIN_TIMEOUT_SECS: u64 = 1; |
| 8 | +const DEFAULT_CONNECT_TIMEOUT_SECS: u64 = 10; |
| 9 | + |
| 10 | +const ENV_ACP_PREFIX: &str = "ACP_PREFIX"; |
| 11 | +const ENV_OPERATION_TIMEOUT_SECS: &str = "ACP_OPERATION_TIMEOUT_SECS"; |
| 12 | +const ENV_PROMPT_TIMEOUT_SECS: &str = "ACP_PROMPT_TIMEOUT_SECS"; |
| 13 | +const ENV_CONNECT_TIMEOUT_SECS: &str = "ACP_NATS_CONNECT_TIMEOUT_SECS"; |
| 14 | +const DEFAULT_ACP_PREFIX: &str = "acp"; |
| 15 | + |
| 16 | +#[derive(Parser, Debug)] |
| 17 | +#[command(name = "acp-nats-stdio")] |
| 18 | +#[command(about = "ACP stdio to NATS bridge for agent-client protocol", long_about = None)] |
| 19 | +pub struct Args { |
| 20 | + #[arg(long = "acp-prefix")] |
| 21 | + pub acp_prefix: Option<String>, |
| 22 | +} |
| 23 | + |
| 24 | +pub fn from_env_with_provider<E: ReadEnv>( |
| 25 | + env_provider: &E, |
| 26 | +) -> Result<Config, AcpPrefixError> { |
| 27 | + let args = Args::parse(); |
| 28 | + from_args(args, env_provider) |
| 29 | +} |
| 30 | + |
| 31 | +pub fn from_args<E: ReadEnv>( |
| 32 | + args: Args, |
| 33 | + env_provider: &E, |
| 34 | +) -> Result<Config, AcpPrefixError> { |
| 35 | + let raw_prefix = args |
| 36 | + .acp_prefix |
| 37 | + .or_else(|| env_provider.var(ENV_ACP_PREFIX).ok()) |
| 38 | + .unwrap_or_else(|| DEFAULT_ACP_PREFIX.to_string()); |
| 39 | + let prefix = AcpPrefix::new(raw_prefix)?; |
| 40 | + let mut config = Config::with_prefix(prefix, NatsConfig::from_env(env_provider)); |
| 41 | + |
| 42 | + if let Ok(raw) = env_provider.var(ENV_OPERATION_TIMEOUT_SECS) { |
| 43 | + match raw.parse::<u64>() { |
| 44 | + Ok(secs) if secs >= MIN_TIMEOUT_SECS => { |
| 45 | + config = config.with_operation_timeout(Duration::from_secs(secs)); |
| 46 | + } |
| 47 | + Ok(secs) => { |
| 48 | + warn!( |
| 49 | + "{ENV_OPERATION_TIMEOUT_SECS}={secs} is below minimum ({MIN_TIMEOUT_SECS}), using default" |
| 50 | + ); |
| 51 | + } |
| 52 | + Err(_) => { |
| 53 | + warn!("{ENV_OPERATION_TIMEOUT_SECS}={raw:?} is not a valid integer, using default"); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if let Ok(raw) = env_provider.var(ENV_PROMPT_TIMEOUT_SECS) { |
| 59 | + match raw.parse::<u64>() { |
| 60 | + Ok(secs) if secs >= MIN_TIMEOUT_SECS => { |
| 61 | + config = config.with_prompt_timeout(Duration::from_secs(secs)); |
| 62 | + } |
| 63 | + Ok(secs) => { |
| 64 | + warn!( |
| 65 | + "{ENV_PROMPT_TIMEOUT_SECS}={secs} is below minimum ({MIN_TIMEOUT_SECS}), using default" |
| 66 | + ); |
| 67 | + } |
| 68 | + Err(_) => { |
| 69 | + warn!("{ENV_PROMPT_TIMEOUT_SECS}={raw:?} is not a valid integer, using default"); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + Ok(config) |
| 75 | +} |
| 76 | + |
| 77 | +pub fn nats_connect_timeout<E: ReadEnv>(env_provider: &E) -> Duration { |
| 78 | + let default = Duration::from_secs(DEFAULT_CONNECT_TIMEOUT_SECS); |
| 79 | + |
| 80 | + match env_provider.var(ENV_CONNECT_TIMEOUT_SECS) { |
| 81 | + Ok(raw) => match raw.parse::<u64>() { |
| 82 | + Ok(secs) if secs >= MIN_TIMEOUT_SECS => Duration::from_secs(secs), |
| 83 | + Ok(secs) => { |
| 84 | + warn!( |
| 85 | + "{ENV_CONNECT_TIMEOUT_SECS}={secs} is below minimum ({MIN_TIMEOUT_SECS}), using default" |
| 86 | + ); |
| 87 | + default |
| 88 | + } |
| 89 | + Err(_) => { |
| 90 | + warn!("{ENV_CONNECT_TIMEOUT_SECS}={raw:?} is not a valid integer, using default"); |
| 91 | + default |
| 92 | + } |
| 93 | + }, |
| 94 | + Err(_) => default, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(test)] |
| 99 | +mod tests { |
| 100 | + use super::*; |
| 101 | + use trogon_std::env::InMemoryEnv; |
| 102 | + |
| 103 | + fn config_from_env(env: &InMemoryEnv) -> Config { |
| 104 | + let args = Args { acp_prefix: None }; |
| 105 | + from_args(args, env).unwrap() |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn test_default_config() { |
| 110 | + let env = InMemoryEnv::new(); |
| 111 | + let config = config_from_env(&env); |
| 112 | + assert_eq!(config.acp_prefix(), DEFAULT_ACP_PREFIX); |
| 113 | + assert_eq!(config.nats().servers, vec!["localhost:4222"]); |
| 114 | + assert!(matches!(&config.nats().auth, acp_nats::NatsAuth::None)); |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn test_acp_prefix_from_env_provider() { |
| 119 | + let env = InMemoryEnv::new(); |
| 120 | + env.set("ACP_PREFIX", "custom-prefix"); |
| 121 | + let config = config_from_env(&env); |
| 122 | + assert_eq!(config.acp_prefix(), "custom-prefix"); |
| 123 | + } |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn test_acp_prefix_from_args() { |
| 127 | + let env = InMemoryEnv::new(); |
| 128 | + let args = Args { |
| 129 | + acp_prefix: Some("cli-prefix".to_string()), |
| 130 | + }; |
| 131 | + let config = from_args(args, &env).unwrap(); |
| 132 | + assert_eq!(config.acp_prefix(), "cli-prefix"); |
| 133 | + } |
| 134 | + |
| 135 | + #[test] |
| 136 | + fn test_args_override_env() { |
| 137 | + let env = InMemoryEnv::new(); |
| 138 | + env.set("ACP_PREFIX", "env-prefix"); |
| 139 | + let args = Args { |
| 140 | + acp_prefix: Some("cli-prefix".to_string()), |
| 141 | + }; |
| 142 | + let config = from_args(args, &env).unwrap(); |
| 143 | + assert_eq!(config.acp_prefix(), "cli-prefix"); |
| 144 | + } |
| 145 | + |
| 146 | + #[test] |
| 147 | + fn test_nats_config_from_env() { |
| 148 | + let env = InMemoryEnv::new(); |
| 149 | + env.set("NATS_URL", "host1:4222,host2:4222"); |
| 150 | + env.set("NATS_TOKEN", "my-token"); |
| 151 | + let config = config_from_env(&env); |
| 152 | + assert_eq!(config.nats().servers, vec!["host1:4222", "host2:4222"]); |
| 153 | + assert!(matches!(&config.nats().auth, acp_nats::NatsAuth::Token(t) if t == "my-token")); |
| 154 | + } |
| 155 | + |
| 156 | + #[test] |
| 157 | + fn test_nats_connect_timeout_from_env() { |
| 158 | + let env = InMemoryEnv::new(); |
| 159 | + assert_eq!(nats_connect_timeout(&env), Duration::from_secs(DEFAULT_CONNECT_TIMEOUT_SECS)); |
| 160 | + |
| 161 | + env.set(ENV_CONNECT_TIMEOUT_SECS, "15"); |
| 162 | + assert_eq!(nats_connect_timeout(&env), Duration::from_secs(15)); |
| 163 | + env.set(ENV_CONNECT_TIMEOUT_SECS, "0"); |
| 164 | + assert_eq!(nats_connect_timeout(&env), Duration::from_secs(DEFAULT_CONNECT_TIMEOUT_SECS)); |
| 165 | + env.set(ENV_CONNECT_TIMEOUT_SECS, "not-a-number"); |
| 166 | + assert_eq!(nats_connect_timeout(&env), Duration::from_secs(DEFAULT_CONNECT_TIMEOUT_SECS)); |
| 167 | + } |
| 168 | + |
| 169 | + #[test] |
| 170 | + fn test_operation_timeout_invalid_env_is_ignored() { |
| 171 | + let env = InMemoryEnv::new(); |
| 172 | + let default = config_from_env(&env); |
| 173 | + let default_timeout = default.operation_timeout(); |
| 174 | + |
| 175 | + env.set("ACP_OPERATION_TIMEOUT_SECS", "not-a-number"); |
| 176 | + let invalid = config_from_env(&env); |
| 177 | + assert_eq!(invalid.operation_timeout(), default_timeout); |
| 178 | + } |
| 179 | + |
| 180 | + #[test] |
| 181 | + fn test_operation_timeout_under_min_is_ignored() { |
| 182 | + let env = InMemoryEnv::new(); |
| 183 | + let default = config_from_env(&env); |
| 184 | + let default_timeout = default.operation_timeout(); |
| 185 | + |
| 186 | + env.set("ACP_OPERATION_TIMEOUT_SECS", "0"); |
| 187 | + let ignored = config_from_env(&env); |
| 188 | + assert_eq!(ignored.operation_timeout(), default_timeout); |
| 189 | + } |
| 190 | + |
| 191 | + #[test] |
| 192 | + fn test_prompt_timeout_invalid_env_is_ignored() { |
| 193 | + let env = InMemoryEnv::new(); |
| 194 | + let default = config_from_env(&env); |
| 195 | + let default_timeout = default.prompt_timeout(); |
| 196 | + |
| 197 | + env.set("ACP_PROMPT_TIMEOUT_SECS", "not-a-number"); |
| 198 | + let invalid = config_from_env(&env); |
| 199 | + assert_eq!(invalid.prompt_timeout(), default_timeout); |
| 200 | + } |
| 201 | + |
| 202 | + #[test] |
| 203 | + fn test_prompt_timeout_under_min_is_ignored() { |
| 204 | + let env = InMemoryEnv::new(); |
| 205 | + let default = config_from_env(&env); |
| 206 | + let default_timeout = default.prompt_timeout(); |
| 207 | + |
| 208 | + env.set("ACP_PROMPT_TIMEOUT_SECS", "0"); |
| 209 | + let ignored = config_from_env(&env); |
| 210 | + assert_eq!(ignored.prompt_timeout(), default_timeout); |
| 211 | + } |
| 212 | +} |
0 commit comments