use arabica_cli::auth::AuthAction;
use arabica_cli::config::{ResolvedCliConfig, resolve_cli_runtime_config};
use arabica_cli::host::{
HostConfigArgs, LocalRunnerPolicy, build_host_runtime, process_environment,
};
use arabica_cli::interactive::{self, InteractiveOptions};
use arabica_cli::print::{self, OutputFormat, PrintOptions, Resume};
use arabica_cli::sessions::SessionsAction;
use arabica_runner::LocalTool;
use clap::{Args, Parser, Subcommand};
use std::io::IsTerminal;
#[derive(Parser, Debug)]
#[command(name = "arabica", about = "Arabica coding agent host")]
struct Cli {
#[command(flatten)]
config: HostConfigArgs,
#[command(subcommand)]
command: Option<Commands>,
#[command(flatten)]
print_args: PrintArgs,
}
#[derive(Args, Debug)]
struct PrintArgs {
#[arg(short = 'p', long = "print", value_name = "TASK")]
print: Option<String>,
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
output_format: OutputFormat,
#[arg(long)]
allow_shell: bool,
#[arg(long)]
read_only: bool,
#[arg(long)]
plain: bool,
#[arg(long = "continue", conflicts_with = "resume")]
resume_last: bool,
#[arg(long, value_name = "ID", conflicts_with = "resume_last")]
resume: Option<String>,
}
impl PrintArgs {
fn resume_mode(&self) -> Resume {
if self.resume_last {
Resume::Continue
} else if let Some(id) = &self.resume {
Resume::Id(id.clone())
} else {
Resume::None
}
}
}
#[derive(Subcommand, Debug)]
enum Commands {
Acp,
Chat,
Config,
Auth {
#[command(subcommand)]
action: AuthAction,
},
Evaluations {
#[command(subcommand)]
action: arabica_cli::evaluation::EvaluationAction,
},
Sessions {
#[command(subcommand)]
action: SessionsAction,
},
}
#[tokio::main]
async fn main() {
let cli = Cli::parse();
std::process::exit(run(cli).await);
}
async fn run(cli: Cli) -> i32 {
let Cli {
config,
command,
print_args,
} = cli;
if let Some(Commands::Evaluations { action }) = command {
return if print_args.print.is_some() {
eprintln!("error: -p cannot be combined with the evaluations subcommand");
2
} else {
arabica_cli::evaluation::run(action).await
};
}
if let Some(Commands::Sessions { action }) = command {
return if print_args.print.is_some() {
eprintln!("error: -p cannot be combined with the sessions subcommand");
2
} else {
arabica_cli::sessions::run(action)
};
}
if let Some(Commands::Auth { action }) = command {
return if print_args.print.is_some() {
eprintln!("error: -p cannot be combined with the auth subcommand");
2
} else {
arabica_cli::auth::run(action).await
};
}
let resolved = match (|| {
let home = arabica_adapters::default_arabica_home()?;
let cwd = std::env::current_dir()?;
if matches!(command, Some(Commands::Acp)) {
arabica_cli::config::resolve_cli_acp_config(&config, &home, &cwd, process_environment)
} else {
resolve_cli_runtime_config(&config, &home, &cwd, process_environment)
}
})() {
Ok(config) => config,
Err(error) => {
eprintln!("error: {error}");
return 2;
}
};
match (command, &print_args.print) {
(Some(Commands::Acp), Some(_)) => {
eprintln!("error: -p cannot be combined with the acp subcommand");
2
}
(Some(Commands::Acp), None) => run_acp(resolved).await,
(Some(Commands::Chat | Commands::Config), Some(_)) => {
eprintln!("error: -p cannot be combined with this subcommand");
2
}
(Some(Commands::Chat), None) => run_chat(resolved, print_args).await,
(Some(Commands::Config), None) => match describe_configuration(resolved) {
Ok(()) => 0,
Err(error) => {
eprintln!("error: {error}");
1
}
},
(Some(Commands::Evaluations { .. }), _) => unreachable!("evaluations returns early"),
(Some(Commands::Sessions { .. }), _) => {
unreachable!("Commands::Sessions returns early above")
}
(Some(Commands::Auth { .. }), _) => {
unreachable!("Commands::Auth returns early above")
}
(None, Some(task)) => run_print(resolved, task.clone(), print_args).await,
(None, None) => run_chat(resolved, print_args).await,
}
}
async fn run_chat(resolved: ResolvedCliConfig, args: PrintArgs) -> i32 {
if args.output_format != OutputFormat::Text {
eprintln!("error: --output-format is only available with -p");
return 2;
}
let options = InteractiveOptions {
allow_shell: args.allow_shell,
read_only: args.read_only,
resume: args.resume_mode(),
};
if !args.plain && std::io::stdin().is_terminal() && std::io::stdout().is_terminal() {
match resolved.build_model() {
Ok(model) => {
arabica_cli::tui::run_with_model_configs(
resolved.provider,
model,
resolved.blend_policy,
resolved.model_configs,
options,
)
.await
}
Err(error) => {
eprintln!("error: {error}");
2
}
}
} else {
match resolved.build_model() {
Ok(model) => {
interactive::run_with_model_configs(
resolved.provider,
model,
resolved.blend_policy,
resolved.model_configs,
options,
)
.await
}
Err(error) => {
eprintln!("error: {error}");
2
}
}
}
}
async fn run_acp(resolved: ResolvedCliConfig) -> i32 {
let tool_policy = LocalRunnerPolicy::coding().with_tool(LocalTool::Shell);
match arabica_cli::acp::run(resolved, tool_policy).await {
Ok(()) => 0,
Err(error) => {
eprintln!("error: {error}");
1
}
}
}
async fn run_print(resolved: ResolvedCliConfig, task: String, args: PrintArgs) -> i32 {
let task = match print::resolve_task(&task) {
Ok(task) => task,
Err(error) => {
eprintln!("error: reading task: {error}");
return 2;
}
};
let resume = args.resume_mode();
let model = match resolved.build_model() {
Ok(model) => model,
Err(error) => {
eprintln!("error: {error}");
return 2;
}
};
print::run_with_model(
model,
resolved.blend_policy,
PrintOptions {
task,
output_format: args.output_format,
allow_shell: args.allow_shell,
read_only: args.read_only,
resume,
},
)
.await
}
fn describe_configuration(resolved: ResolvedCliConfig) -> Result<(), Box<dyn std::error::Error>> {
let model = resolved.build_model()?;
let aliases = resolved.models.clone();
let providers = resolved.model_providers.clone();
let model_configs = resolved.model_configs.clone();
let blend_policy = resolved.blend_policy.clone();
let provider_config = resolved.provider;
let api_type = provider_config.api_type;
let base_url = provider_config.base_url.clone();
let model_name = provider_config.model.clone();
let thinking = arabica_cli::config::thinking_level(&provider_config).to_owned();
let runner_root = std::env::current_dir()?;
let arabica_home = arabica_adapters::default_arabica_home()?;
let runtime = build_host_runtime(
model,
&runner_root,
LocalRunnerPolicy::coding(),
&arabica_home,
);
println!("structure: configuration resolved");
println!(" api_type: {api_type}");
println!(" base_url: {base_url}");
println!(" model: {model_name}");
if !aliases.is_empty() {
println!(" models:");
for (alias, model_id) in aliases {
let provider_name = providers
.get(&alias)
.map(String::as_str)
.unwrap_or("unknown");
let api_type = model_configs
.get(&alias)
.map(|config| config.api_type.to_string())
.unwrap_or_else(|| "unknown".to_owned());
println!(" {alias}: {provider_name}/{model_id} ({api_type})");
}
if let Some(policy) = blend_policy {
println!(
" blend: {} v{} (default {})",
policy.policy_id, policy.version, policy.default_model
);
}
}
println!(" thinking: {thinking}");
println!(" runner_root: {}", runner_root.display());
let home = arabica_adapters::default_arabica_home()?;
println!(
" user config: {}",
arabica_cli::config::user_config_path(&home).display()
);
println!(
" workspace: {}",
arabica_cli::config::workspace_config_path(&home, &runner_root).display()
);
println!(
" tools: {}",
runtime
.tools()
.iter()
.map(|tool| tool.name.as_str())
.collect::<Vec<_>>()
.join(", ")
);
println!(" next: run `arabica` for interactive chat or `arabica -p \"task\"` for scripting");
Ok(())
}