1pub(crate) mod agent;
2pub(crate) mod agent_key;
3pub(crate) mod agent_runtime;
4pub(crate) mod config_setting;
5pub(crate) mod error;
6pub(crate) mod fake_prompt_mcp;
7pub(crate) mod model_config;
8pub(crate) mod prompt_history_index;
9pub(crate) mod protocol;
10pub(crate) mod session_actor;
11pub(crate) mod session_config_state;
12pub(crate) mod session_factory;
13pub(crate) mod session_store;
14pub(crate) mod slash_commands;
15pub(crate) mod state;
16pub(crate) mod stdio;
17pub mod testing;
18
19pub use protocol::map_mcp_prompt_to_available_command;
20
21use crate::acp::agent::acp_agent_builder;
22use crate::acp::state::{AcpState, AcpStateConfig};
23use crate::acp::stdio::Stdio;
24use crate::provider_connection_args::ProviderConnectionArgs;
25use crate::settings_args::SettingsSourceArgs;
26use agent_client_protocol as acp;
27use llm::ReasoningEffort;
28use std::sync::Arc;
29use std::{fs::create_dir_all, path::PathBuf};
30use thiserror::Error;
31use tracing::info;
32use tracing_appender::rolling::daily;
33use tracing_subscriber::EnvFilter;
34
35use aether_auth::OAuthCredentialStorage;
36use session_factory::InitialSessionSelection;
37use session_store::SessionStore;
38
39#[derive(clap::Args, Debug)]
40pub struct AcpArgs {
41 #[clap(long, default_value = "/tmp/aether-acp-logs")]
43 pub log_dir: PathBuf,
44
45 #[clap(long, conflicts_with_all = ["model", "reasoning_effort"])]
47 pub agent: Option<String>,
48
49 #[clap(long, conflicts_with = "agent")]
52 pub model: Option<String>,
53
54 #[clap(long, value_name = "low|medium|high|xhigh", requires = "model", conflicts_with = "agent")]
56 pub reasoning_effort: Option<ReasoningEffort>,
57
58 #[command(flatten)]
59 pub provider_connection: ProviderConnectionArgs,
60
61 #[command(flatten)]
62 pub settings_source: SettingsSourceArgs,
63}
64
65#[derive(Debug)]
67pub enum AcpRunOutcome {
68 CleanDisconnect,
70}
71
72#[derive(Debug, Error)]
74pub enum AcpRunError {
75 #[error("ACP protocol error: {0}")]
76 Protocol(#[from] acp::Error),
77}
78
79pub async fn run_acp(args: AcpArgs) -> Result<AcpRunOutcome, AcpRunError> {
80 info!("Starting Aether ACP server");
81
82 setup_logging(&args);
83
84 let initial_selection = if let Some(agent) = args.agent.clone() {
85 InitialSessionSelection::agent(agent)
86 } else if let Some(model) = args.model.clone() {
87 InitialSessionSelection::model(model, args.reasoning_effort)
88 } else {
89 InitialSessionSelection::default()
90 };
91 let session_store =
92 SessionStore::new().map_or_else(|e| panic!("Failed to initialize session store: {e}"), Arc::new);
93 let oauth_credential_store: Arc<dyn OAuthCredentialStorage> =
94 Arc::new(aether_auth::OsKeyringStore::with_platform_store());
95 let provider_connections = args.provider_connection.into_overrides();
96 let state = Arc::new(AcpState::new(AcpStateConfig {
97 session_store,
98 oauth_credential_store,
99 initial_selection,
100 settings_source: args.settings_source,
101 provider_connections,
102 }));
103
104 let connect_result = acp_agent_builder(state.clone()).connect_to(Stdio::new()).await;
105 state.shutdown_all().await;
106
107 match connect_result {
108 Ok(()) => Ok(AcpRunOutcome::CleanDisconnect),
109 Err(err) => Err(AcpRunError::Protocol(err)),
110 }
111}
112
113fn setup_logging(args: &AcpArgs) {
114 create_dir_all(&args.log_dir).ok();
115 tracing_subscriber::fmt()
116 .with_writer(daily(&args.log_dir, "aether-acp.log"))
117 .with_ansi(false) .with_env_filter(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("warn")))
119 .pretty()
120 .init();
121}
122
123#[cfg(test)]
124mod tests {
125 use super::*;
126 use clap::Parser;
127
128 #[derive(Debug, Parser)]
129 struct TestCli {
130 #[command(flatten)]
131 args: AcpArgs,
132 }
133
134 #[test]
135 fn agent_conflicts_with_model() {
136 let err = TestCli::try_parse_from(["test", "--agent", "planner", "--model", "anthropic:claude-sonnet-4-5"])
137 .expect_err("agent and model should conflict");
138 assert_eq!(err.kind(), clap::error::ErrorKind::ArgumentConflict);
139 }
140
141 #[test]
142 fn agent_conflicts_with_reasoning_effort() {
143 let err = TestCli::try_parse_from(["test", "--agent", "planner", "--reasoning-effort", "high"])
144 .expect_err("agent and reasoning effort should conflict");
145 assert_eq!(err.kind(), clap::error::ErrorKind::ArgumentConflict);
146 }
147
148 #[test]
149 fn reasoning_effort_requires_model() {
150 let err = TestCli::try_parse_from(["test", "--reasoning-effort", "high"])
151 .expect_err("reasoning effort should require model");
152 assert_eq!(err.kind(), clap::error::ErrorKind::MissingRequiredArgument);
153 }
154
155 #[test]
156 fn reasoning_effort_with_model_is_allowed() {
157 let cli =
158 TestCli::try_parse_from(["test", "--model", "anthropic:claude-sonnet-4-5", "--reasoning-effort", "high"])
159 .expect("reasoning effort can configure an explicit model session");
160 assert_eq!(cli.args.reasoning_effort, Some(ReasoningEffort::High));
161 }
162}