brok-cli 0.4.1

Brokspace: local-first knowledge index and event-driven agents for your workspace
//! `brok`: the Brokspace command line.

mod cmd;
mod daemon;
mod integrations;
mod local_llm;
mod metrics;
mod ui;

use std::path::PathBuf;
use std::process::ExitCode;

use clap::{Parser, Subcommand};
use tracing_subscriber::EnvFilter;

/// Local-first knowledge index and event-driven agents for your workspace.
#[derive(Debug, Parser)]
#[command(name = "brok", version, about, long_about = None, propagate_version = true)]
struct Cli {
    /// Run as if brok was started in this directory.
    #[arg(short = 'C', long, global = true, value_name = "DIR")]
    dir: Option<PathBuf>,

    /// More logging (-v info, -vv debug, -vvv trace). RUST_LOG overrides.
    #[arg(short, long, global = true, action = clap::ArgAction::Count)]
    verbose: u8,

    /// Only print errors.
    #[arg(short, long, global = true, conflicts_with = "verbose")]
    quiet: bool,

    #[command(subcommand)]
    command: Command,
}

#[derive(Debug, Subcommand)]
enum Command {
    /// Set up Brokspace here: workspace, agent, search model, local LLM, index, coding agents.
    Init(cmd::init::Args),
    /// Index the workspace (incremental; only changed files are processed).
    Index(cmd::index::Args),
    /// Hybrid (BM25 + semantic) search over the index.
    Search(cmd::search::Args),
    /// Ask a question about the workspace (starts brok in the background if needed).
    Ask(cmd::ask::Args),
    /// Chat about the workspace, with follow-up questions.
    Chat(cmd::chat::Args),
    /// AI code review of your changes (uncommitted, staged or a branch).
    Review(cmd::git_assist::ReviewArgs),
    /// Write a commit message for the staged changes, then commit.
    Commit(cmd::git_assist::CommitArgs),
    /// Start brok in the background (bus, live indexing, agent).
    Start,
    /// Stop the background brok.
    Stop,
    /// Restart the background brok (e.g. after changing brok.yml).
    Restart,
    /// Show the background brok's log.
    Logs(cmd::service::LogsArgs),
    /// Run brok in the foreground (bus, index service, shards); `brok start` runs it in the background.
    Run(cmd::run::Args),
    /// Show workspace, index, daemon and shard status.
    Status(cmd::status::Args),
    /// Configure the local LLM, coding agent integrations or index mirroring.
    Setup(cmd::setup::Args),
    /// Read and change brok.yml (`brok config get search`, `brok config set search.limit 12`).
    #[command(subcommand)]
    Config(cmd::config::Command),
    /// What Brokspace remembers between sessions: decisions, facts, notes, chat sessions.
    #[command(subcommand)]
    Memory(cmd::memory::Command),
    /// Workspaces this one depends on (other services, libraries): searched with it.
    #[command(subcommand)]
    Link(cmd::link::Command),
    /// Local metrics: how searches, answers and indexing perform, and what failed.
    Stats(cmd::stats::Args),
    /// Report a bug: gathers diagnostics (secrets masked), lets you review them, opens a prefilled issue.
    Bug(cmd::bug::Args),
    /// Check the installation and explain how to fix what is missing.
    Doctor(cmd::doctor::Args),
    /// Upgrade brok and brok-agent to the latest release and show what changed.
    Upgrade(cmd::upgrade::Args),
    /// Agent skills: list, run on the local LLM, create, export to coding agents.
    #[command(subcommand)]
    Skill(cmd::skill::Command),
    /// Serve the Brokspace tools to coding agents over MCP (stdio).
    Mcp(cmd::mcp::Args),
    /// Print shell completions (bash, zsh, fish, powershell, elvish).
    Completions {
        /// Target shell.
        shell: clap_complete::Shell,
    },
    /// Manage shards.
    #[command(subcommand)]
    Shard(cmd::shard::Command),
    /// Manage the local embedding model.
    #[command(subcommand)]
    Model(cmd::model::Command),
    /// Inspect and drive the event bus (debugging and shard development).
    #[command(subcommand)]
    Bus(cmd::bus::Command),
}

fn main() -> ExitCode {
    // Before parsing: `brok --version` exits inside `parse`.
    cmd::upgrade::remove_leftovers();
    let cli = Cli::parse();
    init_tracing(&cli);

    let runtime = match tokio::runtime::Builder::new_multi_thread().enable_all().build() {
        Ok(runtime) => runtime,
        Err(e) => {
            ui::error(&format!("cannot start the async runtime: {e}"));
            return ExitCode::FAILURE;
        }
    };
    let command = command_name(&cli.command);
    match runtime.block_on(run(cli)) {
        Ok(()) => ExitCode::SUCCESS,
        Err(e) => {
            let message = format!("{e:#}");
            metrics::recorder().record_error(command, &message);
            ui::error(&message);
            ExitCode::FAILURE
        }
    }
}

async fn run(cli: Cli) -> anyhow::Result<()> {
    let cwd = match cli.dir {
        Some(dir) => dir,
        None => std::env::current_dir()?,
    };
    metrics::init(&cwd);
    match cli.command {
        Command::Init(args) => cmd::init::run(&cwd, args).await,
        Command::Index(args) => cmd::index::run(&cwd, args).await,
        Command::Search(args) => cmd::search::run(&cwd, args).await,
        Command::Ask(args) => cmd::ask::run(&cwd, args).await,
        Command::Chat(args) => cmd::chat::run(&cwd, args).await,
        Command::Review(args) => cmd::git_assist::review(&cwd, args).await,
        Command::Commit(args) => cmd::git_assist::commit(&cwd, args).await,
        Command::Start => cmd::service::start(&cwd).await,
        Command::Stop => cmd::service::stop(&cwd).await,
        Command::Restart => cmd::service::restart(&cwd).await,
        Command::Logs(args) => cmd::service::logs(&cwd, args).await,
        Command::Run(args) => cmd::run::run(&cwd, args).await,
        Command::Status(args) => cmd::status::run(&cwd, args).await,
        Command::Setup(args) => cmd::setup::run(&cwd, args).await,
        Command::Config(command) => cmd::config::run(&cwd, command).await,
        Command::Memory(command) => cmd::memory::run(&cwd, command).await,
        Command::Link(command) => cmd::link::run(&cwd, command).await,
        Command::Stats(args) => cmd::stats::run(&cwd, args).await,
        Command::Bug(args) => cmd::bug::run(&cwd, args).await,
        Command::Doctor(args) => cmd::doctor::run(&cwd, args).await,
        Command::Upgrade(args) => cmd::upgrade::run(&cwd, args).await,
        Command::Skill(command) => cmd::skill::run(&cwd, command).await,
        Command::Mcp(args) => cmd::mcp::run(&cwd, args).await,
        Command::Completions { shell } => {
            use clap::CommandFactory;
            let mut script = Vec::new();
            clap_complete::generate(shell, &mut Cli::command(), "brok", &mut script);
            // A closed pipe (`brok completions bash | head`) is not an error.
            let _ = std::io::Write::write_all(&mut std::io::stdout(), &script);
            Ok(())
        }
        Command::Shard(command) => cmd::shard::run(&cwd, command).await,
        Command::Model(command) => cmd::model::run(&cwd, command).await,
        Command::Bus(command) => cmd::bus::run(&cwd, command).await,
    }
}

/// The subcommand's name, for metrics and reports.
fn command_name(command: &Command) -> &'static str {
    match command {
        Command::Init(_) => "init",
        Command::Index(_) => "index",
        Command::Search(_) => "search",
        Command::Ask(_) => "ask",
        Command::Chat(_) => "chat",
        Command::Review(_) => "review",
        Command::Commit(_) => "commit",
        Command::Start => "start",
        Command::Stop => "stop",
        Command::Restart => "restart",
        Command::Logs(_) => "logs",
        Command::Run(_) => "run",
        Command::Status(_) => "status",
        Command::Setup(_) => "setup",
        Command::Config(_) => "config",
        Command::Memory(_) => "memory",
        Command::Link(_) => "link",
        Command::Stats(_) => "stats",
        Command::Bug(_) => "bug",
        Command::Doctor(_) => "doctor",
        Command::Upgrade(_) => "upgrade",
        Command::Skill(_) => "skill",
        Command::Mcp(_) => "mcp",
        Command::Completions { .. } => "completions",
        Command::Shard(_) => "shard",
        Command::Model(_) => "model",
        Command::Bus(_) => "bus",
    }
}

fn init_tracing(cli: &Cli) {
    // `brok run` is a long-running service: show what it does by default.
    let base = match (&cli.command, cli.quiet, cli.verbose) {
        (_, true, _) => "error",
        (Command::Run(_), false, 0) => "info",
        (_, false, 0) => "warn",
        (_, false, 1) => "info",
        (_, false, 2) => "debug",
        _ => "trace",
    };
    let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
        // Dependencies stay quiet unless asked for explicitly.
        EnvFilter::new(format!("warn,brok={base},brok_cli={base},brok_core={base},brok_bus={base},brok_index={base},brok_shard={base},brok_skills={base},brok_sdk={base},shard=info"))
    });
    tracing_subscriber::fmt()
        .with_env_filter(filter)
        .with_writer(std::io::stderr)
        .with_ansi(std::io::IsTerminal::is_terminal(&std::io::stderr()))
        .with_target(false)
        .init();
}

#[cfg(test)]
mod tests {
    use clap::CommandFactory;

    #[test]
    fn cli_definition_is_valid() {
        super::Cli::command().debug_assert();
    }
}