use arabica_cli::auth::AuthAction;
use arabica_cli::config::resolve_cli_config;
use arabica_cli::host::{
HostConfigArgs, HostModel, LocalRunnerPolicy, build_host_runtime, process_environment,
resolve_provider_config,
};
use arabica_cli::interactive::{self, InteractiveOptions};
use arabica_cli::print::{self, OutputFormat, PrintOptions, Resume};
use arabica_cli::sessions::SessionsAction;
use arabica_provider::{ApiModelProvider, ApiProviderConfig};
use arabica_runner::LocalTool;
use clap::{Args, Parser, Subcommand};
use std::io::IsTerminal;
#[derive(Parser, Debug)]
#[command(name = "arabica", about = "Structure 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,
},
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::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)
};
}
let provider_config = match if matches!(command, Some(Commands::Acp)) {
resolve_provider_config(&config, process_environment)
.map_err(|error| Box::new(error) as Box<dyn std::error::Error>)
} else {
(|| {
let home = arabica_adapters::default_arabica_home()?;
let cwd = std::env::current_dir()?;
resolve_cli_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(provider_config).await,
(Some(Commands::Chat | Commands::Config), Some(_)) => {
eprintln!("error: -p cannot be combined with this subcommand");
2
}
(Some(Commands::Chat), None) => run_chat(provider_config, print_args).await,
(Some(Commands::Config), None) => match describe_configuration(provider_config) {
Ok(()) => 0,
Err(error) => {
eprintln!("error: {error}");
1
}
},
(Some(Commands::Sessions { .. }), _) => {
unreachable!("Commands::Sessions returns early above")
}
(Some(Commands::Auth { .. }), _) => {
unreachable!("Commands::Auth returns early above")
}
(None, Some(task)) => run_print(provider_config, task.clone(), print_args).await,
(None, None) => run_chat(provider_config, print_args).await,
}
}
async fn run_chat(provider_config: ApiProviderConfig, 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() {
arabica_cli::tui::run(provider_config, options).await
} else {
interactive::run(provider_config, options).await
}
}
async fn run_acp(provider_config: ApiProviderConfig) -> i32 {
let tool_policy = LocalRunnerPolicy::coding().with_tool(LocalTool::Shell);
match arabica_cli::acp::run(provider_config, tool_policy).await {
Ok(()) => 0,
Err(error) => {
eprintln!("error: {error}");
1
}
}
}
async fn run_print(provider_config: ApiProviderConfig, 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();
print::run(
provider_config,
PrintOptions {
task,
output_format: args.output_format,
allow_shell: args.allow_shell,
read_only: args.read_only,
resume,
},
)
.await
}
fn describe_configuration(
provider_config: ApiProviderConfig,
) -> Result<(), Box<dyn std::error::Error>> {
let api_type = provider_config.api_type;
let base_url = provider_config.base_url.clone();
let model_name = provider_config.model.clone();
let thinking = if !provider_config.thinking_enabled {
"off".to_owned()
} else {
provider_config
.reasoning_effort
.clone()
.unwrap_or_else(|| "on".to_owned())
};
let model = HostModel::Api(ApiModelProvider::new(provider_config)?);
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}");
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 `structure` for interactive chat or `structure -p \"task\"` for scripting"
);
Ok(())
}