use blyte_cli::cli::{apply_converse_defaults, Cli, SurfaceArgs};
use clap::{CommandFactory, FromArgMatches};
fn restore_sigpipe() {
unsafe {
libc::signal(libc::SIGPIPE, libc::SIG_DFL);
}
}
fn main() {
restore_sigpipe();
if let Some(path) = blyte_cli::env_file::default_path() {
if let Err(e) = blyte_cli::env_file::load(&path) {
eprintln!("warning: {e}");
}
}
let matches = Cli::command().get_matches();
let mut cli = Cli::from_arg_matches(&matches).unwrap_or_else(|e| e.exit());
if std::env::var(blyte_agent::shell::MODEL_SHELL_ENV).as_deref() == Ok("1") {
let refused = match &cli.command {
Some(blyte_cli::cli::Command::Combine(_)) => Some("combine"),
Some(blyte_cli::cli::Command::Review(args)) if args.model_review => {
Some("review --model-review")
}
Some(blyte_cli::cli::Command::Review(args)) => match args.verdict {
Some(blyte_cli::cli::ReviewVerdict::Accepted) => Some("review --verdict accepted"),
Some(blyte_cli::cli::ReviewVerdict::Fixed) => Some("review --verdict fixed"),
Some(blyte_cli::cli::ReviewVerdict::Redone) | None => None,
},
_ => None,
};
if let Some(cmd) = refused {
eprintln!("{}", blyte_agent::shell::model_shell_refusal_message(cmd));
std::process::exit(2);
}
if let Some(blyte_cli::cli::Command::Dispatch(args)) = &cli.command {
let dispatch_matches = matches.subcommand_matches("dispatch");
let refused_opt = if args.sandbox != "on"
|| dispatch_matches.and_then(|m| m.value_source("sandbox"))
== Some(clap::parser::ValueSource::CommandLine)
{
Some("--sandbox")
} else if args.network.is_some() {
Some("--network")
} else if !args.allow_read.is_empty() {
Some("--allow-read")
} else if !args.pass_env.is_empty() {
Some("--pass-env")
} else if args.gate.is_some() {
Some("--gate")
} else if args.no_path_check {
Some("--no-path-check")
} else {
None
};
if let Some(opt) = refused_opt {
eprintln!(
"{}",
blyte_agent::tools::refused_dispatch_option_message(opt)
);
std::process::exit(2);
}
}
}
let has_surface_flags = cli.port.is_some()
|| cli.no_open
|| cli.open_in.is_some()
|| cli.days.is_some()
|| cli.name.is_some()
|| cli.resume.is_some();
if has_surface_flags && (cli.prompt.is_some() || cli.command.is_some()) {
eprintln!(
"error: --port, --no-open, --open-in, --days, --name, and --resume cannot be used with a prompt or a subcommand"
);
std::process::exit(2);
}
if cli.terminal && cli.open_in.is_some() {
eprintln!("error: --open-in cannot be used with --terminal");
std::process::exit(2);
}
apply_converse_defaults(&mut cli, &matches);
if let Some(cmd) = cli.command {
match cmd {
blyte_cli::cli::Command::Init(args) => {
std::process::exit(blyte_cli::init::run(&args));
}
blyte_cli::cli::Command::Dashboard(args) => {
std::process::exit(blyte_cli::dashboard::run(&args));
}
blyte_cli::cli::Command::Combine(args) => {
std::process::exit(blyte_cli::combine::run(&args));
}
blyte_cli::cli::Command::Dispatch(args) => {
blyte_cli::update_notice::print_notice();
std::process::exit(blyte_cli::dispatch::run_dispatch(&args));
}
blyte_cli::cli::Command::EditStart(args) => {
std::process::exit(blyte_cli::direct_edit::run_edit_start(&args));
}
blyte_cli::cli::Command::EditFinish(args) => {
std::process::exit(blyte_cli::direct_edit::run_edit_finish(&args));
}
blyte_cli::cli::Command::Review(args) => {
blyte_cli::update_notice::print_notice();
std::process::exit(blyte_cli::dispatch::run_review(&args));
}
blyte_cli::cli::Command::Undo(args) => {
std::process::exit(blyte_cli::in_place_review::undo(&args));
}
blyte_cli::cli::Command::Clean(args) => {
std::process::exit(blyte_cli::clean::run(&args));
}
blyte_cli::cli::Command::Status { id, json } => {
std::process::exit(blyte_cli::dispatch::run_status(&id, json));
}
blyte_cli::cli::Command::Wait { ids, any, timeout } => {
std::process::exit(blyte_cli::wait::run(&ids, any, timeout));
}
blyte_cli::cli::Command::Update { check, rollback } => {
std::process::exit(blyte_cli::update::run(check, rollback));
}
blyte_cli::cli::Command::Cost(args) => {
std::process::exit(blyte_cli::cost::run(&args));
}
blyte_cli::cli::Command::Statusline(args) => {
std::process::exit(blyte_cli::statusline::run(&args));
}
blyte_cli::cli::Command::Plan(args) => {
std::process::exit(blyte_cli::plan_cmd::run(&args.plan, args.json));
}
blyte_cli::cli::Command::Surfaces { json, stopped } => {
let mut out = std::io::stdout();
let mut err = std::io::stderr();
if stopped {
let state_dir = match blyte_cli::surface_registry::state_dir() {
Ok(dir) => dir,
Err(e) => {
eprintln!("blyte: {e}");
std::process::exit(1);
}
};
let cwd = match std::env::current_dir() {
Ok(dir) => dir,
Err(e) => {
eprintln!("blyte: cannot read current directory: {e}");
std::process::exit(1);
}
};
std::process::exit(blyte_cli::surfaces_cmd::run_stopped_surfaces(
&cwd, &state_dir, json, &mut out, &mut err,
));
}
let dir = match blyte_cli::surface_registry::registry_dir() {
Ok(dir) => dir,
Err(e) => {
eprintln!("blyte: {e}");
std::process::exit(1);
}
};
std::process::exit(blyte_cli::surfaces_cmd::run_surfaces(
&dir, json, &mut out, &mut err,
));
}
blyte_cli::cli::Command::Open { name, open_in } => {
let open_in =
match blyte_cli::open_window::resolve_open_in_from_env(open_in.as_deref()) {
Ok(v) => v,
Err(e) => {
eprintln!("error: {e}");
std::process::exit(2);
}
};
let dir = match blyte_cli::surface_registry::registry_dir() {
Ok(dir) => dir,
Err(e) => {
eprintln!("blyte: {e}");
std::process::exit(1);
}
};
let mut out = std::io::stdout();
let mut err = std::io::stderr();
let opener = |url: &str| blyte_cli::serve::open_browser_in(open_in, url);
std::process::exit(blyte_cli::surfaces_cmd::run_open(
&dir, &name, &opener, &mut out, &mut err,
));
}
blyte_cli::cli::Command::Reload(args) => {
let dir = match blyte_cli::surface_registry::registry_dir() {
Ok(dir) => dir,
Err(e) => {
eprintln!("blyte: {e}");
std::process::exit(1);
}
};
let cwd = match std::env::current_dir() {
Ok(dir) => dir,
Err(e) => {
eprintln!("blyte: cannot read current directory: {e}");
std::process::exit(1);
}
};
let mut out = std::io::stdout();
let mut err = std::io::stderr();
std::process::exit(blyte_cli::surfaces_cmd::run_reload(
&dir,
&cwd,
args.target.as_deref(),
args.all,
&mut out,
&mut err,
));
}
blyte_cli::cli::Command::Tell {
name,
message,
from,
} => {
let dir = match blyte_cli::surface_registry::registry_dir() {
Ok(dir) => dir,
Err(e) => {
eprintln!("blyte: {e}");
std::process::exit(1);
}
};
let mut out = std::io::stdout();
let mut err = std::io::stderr();
std::process::exit(blyte_cli::tell::run_tell(
&dir, &name, &message, &from, &mut out, &mut err,
));
}
blyte_cli::cli::Command::Comments(args) => {
let dir = match std::env::current_dir() {
Ok(dir) => dir,
Err(e) => {
eprintln!("blyte comments: cannot read the current directory: {e}");
std::process::exit(2);
}
};
std::process::exit(blyte_cli::comments_cmd::run(&args.base, &dir));
}
blyte_cli::cli::Command::Instructions(args) => {
if args.default_process {
print!("{}", blyte_cli::conversation_text::DEFAULT_PROCESS);
std::process::exit(0);
}
let root = match blyte_cli::instructions_cmd::repo_root() {
Ok(root) => root,
Err(e) => {
eprintln!("error: {e}");
std::process::exit(1);
}
};
let answers = if args.one_shot {
blyte_agent::conversation::Answers::Nobody
} else {
blyte_agent::conversation::Answers::Person
};
let sandbox_on = args.sandbox.as_deref().unwrap_or(&cli.sandbox) == "on";
match blyte_cli::instructions_cmd::render_with_sandbox(
args.dispatch_tool,
answers,
&root,
sandbox_on,
) {
Ok(text) => {
print!("{text}");
std::process::exit(0);
}
Err(e) => {
eprintln!("error: {e}");
std::process::exit(1);
}
}
}
blyte_cli::cli::Command::Skill(args) => match args.action {
blyte_cli::cli::SkillAction::Install {
project,
force,
default: _,
no_default,
no_statusline,
no_autocompact,
harness,
model,
} => {
let targets = match blyte_cli::skill::Harness::parse_all(&harness) {
Ok(t) => t,
Err(e) => {
eprintln!("{e}");
std::process::exit(1);
}
};
for &target in &targets {
let written = match blyte_cli::skill::install_for(
target,
project,
force,
no_default,
no_statusline,
no_autocompact,
) {
Ok(w) => w,
Err(e) => {
eprintln!("{e}");
std::process::exit(1);
}
};
for path in written {
if path.extension().is_some_and(|ext| ext == "rules") {
println!("wrote {}", path.display());
} else if path.file_name().is_some_and(|name| name == "SKILL.md") {
println!("installed {}", path.display());
} else {
println!("updated {}", path.display());
}
}
}
for target in targets {
match target {
blyte_cli::skill::Harness::Claude => {
if no_default {
println!(
"to use it, ask Claude Code: \"Plan this change, then use blyte to implement each task and review the results.\""
);
} else {
println!(
"Claude Code now writes the plan and delegates implementation to blyte, in parallel, by default"
);
}
}
blyte_cli::skill::Harness::Codex => {
if no_default {
println!(
"to use it, ask Codex: \"use blyte to implement this\""
);
} else {
println!(
"Codex now writes the plan and delegates implementation to blyte, in parallel, by default"
);
}
}
}
}
if let Some(env_path) = blyte_cli::env_file::default_path() {
use std::io::IsTerminal;
let is_tty =
std::io::stdin().is_terminal() && std::io::stdout().is_terminal();
let stdin = std::io::stdin();
let mut reader = stdin.lock();
let mut stdout = std::io::stdout();
if let Err(e) = blyte_cli::skill::configure_model(
&env_path,
model.as_deref(),
is_tty,
&mut reader,
&mut stdout,
) {
eprintln!("{e}");
std::process::exit(1);
}
}
if let Ok(home) = blyte_cli::skill::home_dir() {
blyte_cli::skill::warn_superseded(&home);
}
std::process::exit(0);
}
blyte_cli::cli::SkillAction::Uninstall { project, harness } => {
let targets = match blyte_cli::skill::Harness::parse_all(&harness) {
Ok(t) => t,
Err(e) => {
eprintln!("{e}");
std::process::exit(1);
}
};
let mut all_paths = Vec::new();
for target in targets {
match blyte_cli::skill::uninstall_for(target, project) {
Ok(paths) => all_paths.extend(paths),
Err(e) => {
eprintln!("{e}");
std::process::exit(1);
}
}
}
if all_paths.is_empty() {
println!("nothing to remove");
} else {
for path in all_paths {
println!("removed {}", path.display());
}
}
std::process::exit(0);
}
blyte_cli::cli::SkillAction::Print => {
print!("{}", blyte_cli::skill::SKILL_MD);
std::process::exit(0);
}
},
}
}
if cli.converse
&& std::env::var("BLYTE_SESSION")
.map(|v| v.is_empty())
.unwrap_or(true)
{
let now_secs = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
let id = blyte_cli::repl::new_session_id(now_secs, std::process::id());
std::env::set_var("BLYTE_SESSION", &id);
}
if !cli.no_gate_slot {
blyte_agent::gate_slots::enable();
}
if let Some(ref prompt) = cli.prompt {
let trimmed = prompt.trim();
if trimmed.is_empty() {
eprintln!("Error: prompt cannot be empty");
std::process::exit(1);
}
if !trimmed.contains(char::is_whitespace) {
let cmd = Cli::command();
let version = cmd.get_version().unwrap_or(env!("CARGO_PKG_VERSION"));
eprintln!(
"error: `{trimmed}` is not a blyte command in this build (blyte {version}); a prompt needs more than one word"
);
std::process::exit(2);
}
let config = blyte_cli::task_runner::TaskConfig::from(&cli);
match blyte_cli::task_runner::run_task(config) {
Ok((blyte_agent::run::Outcome::Verified, report)) => {
println!(
"Task verified in {} turns (usage: {:?})",
report.outcome_turns, report.usage
);
std::process::exit(0);
}
Ok((blyte_agent::run::Outcome::Finished, report)) => {
println!(
"Task finished in {} turns (usage: {:?})",
report.outcome_turns, report.usage
);
std::process::exit(0);
}
Ok((outcome, report)) => {
eprintln!(
"Task finished with outcome: {outcome:?} in {} turns",
report.outcome_turns
);
std::process::exit(1);
}
Err(err) => {
eprintln!("Error executing task: {err}");
std::process::exit(1);
}
}
}
let open_in = if cli.terminal {
blyte_cli::open_window::OpenIn::App
} else {
match blyte_cli::open_window::resolve_open_in_from_env(cli.open_in.as_deref()) {
Ok(v) => v,
Err(e) => {
eprintln!("error: {e}");
std::process::exit(2);
}
}
};
cli.converse = true;
apply_converse_defaults(&mut cli, &matches);
let surface_args = SurfaceArgs {
port: cli.port.unwrap_or(0),
no_open: if cli.terminal { true } else { cli.no_open },
open_in,
days: cli.days.unwrap_or(14),
name: cli.name,
resume: cli.resume,
};
let cwd = match std::env::current_dir() {
Ok(d) => d,
Err(e) => {
eprintln!("blyte: cannot determine current directory: {e}");
std::process::exit(1);
}
};
let repl_config = blyte_cli::repl::ReplConfig {
repo_root: cwd,
model: cli.model,
gate: cli.gate,
context_strategy: cli.context,
container: cli.container,
max_turns: cli.max_turns,
max_gates: cli.max_gates,
rollback: cli.rollback,
converse: cli.converse,
dispatch_cmd: cli.dispatch_cmd,
fold_budget: cli.fold_budget,
sandbox: cli.sandbox,
network: cli.network,
allow_read: cli.allow_read,
pass_env: cli.pass_env,
};
let code = if cli.terminal {
blyte_cli::serve::run_terminal(&surface_args, repl_config)
} else {
blyte_cli::serve::run(&surface_args, repl_config)
};
std::process::exit(code);
}