pub mod admin;
pub mod codemap;
#[cfg(all(feature = "comms", any(unix, windows)))]
pub mod comms;
#[cfg(all(feature = "comms", any(unix, windows)))]
pub mod comms_daemon;
pub mod context;
pub mod git;
pub mod governance;
pub mod init;
pub mod init_rules;
pub mod memory;
#[cfg(all(feature = "comms", any(unix, windows)))]
pub mod registry;
pub mod render;
#[cfg(all(feature = "shells", any(unix, windows)))]
pub mod shells;
pub mod web;
use std::io::Write;
use std::path::Path;
use std::time::Instant;
use anyhow::{Context, Result};
use clap::Subcommand;
use rmcp::ErrorData as McpError;
use rmcp::model::CallToolResult;
use crate::cli::render::Emit;
use crate::config::DocumentsCliOverrides;
#[derive(Subcommand, Debug)]
pub enum ToolCmd {
#[command(subcommand)]
Query(codemap::QueryCmd),
#[command(subcommand)]
Git(git::GitCmd),
#[command(subcommand)]
Memory(memory::MemoryCmd),
#[command(subcommand)]
Governance(governance::GovernanceCmd),
#[command(subcommand)]
Web(web::WebCmd),
#[cfg(all(feature = "shells", any(unix, windows)))]
#[command(subcommand)]
Shells(shells::ShellsCmd),
Telemetry {
#[arg(long)]
window: Option<String>,
#[arg(long)]
tool: Option<String>,
},
}
pub fn run_tool(tool: &str, result: Result<CallToolResult, McpError>) -> Result<CallToolResult> {
result.map_err(|e| anyhow::anyhow!("{tool}: {e}"))
}
pub fn run(
root: &Path,
view: &str,
documents: DocumentsCliOverrides,
json: bool,
process_started: Instant,
cmd: ToolCmd,
) -> Result<()> {
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.context("build tokio runtime")?;
runtime.block_on(async move {
let server = context::build_server(root, view, documents)?;
let stdout = std::io::stdout();
let mut out = stdout.lock();
let opts = Emit {
json,
startup_us: u64::try_from(process_started.elapsed().as_micros()).unwrap_or(u64::MAX),
};
match cmd {
ToolCmd::Query(q) => codemap::run(&server, q, &opts, &mut out).await?,
ToolCmd::Git(g) => git::run(&server, g, &opts, &mut out).await?,
ToolCmd::Memory(m) => memory::run(&server, m, &opts, &mut out).await?,
ToolCmd::Governance(g) => governance::run(&server, g, &opts, &mut out).await?,
ToolCmd::Web(w) => web::run(&server, w, &opts, &mut out).await?,
#[cfg(all(feature = "shells", any(unix, windows)))]
ToolCmd::Shells(s) => shells::run(&server, s, &opts, &mut out).await?,
ToolCmd::Telemetry { window, tool } => admin::run_telemetry(&server, window, tool, &opts, &mut out).await?,
}
out.flush().context("flush stdout")?;
Ok(())
})
}
pub fn run_cache(root: &Path, cmd: admin::CacheCmd, json: bool) -> Result<()> {
let stdout = std::io::stdout();
let mut out = stdout.lock();
admin::run_cache(root, cmd, json, &mut out)?;
out.flush().context("flush stdout")?;
Ok(())
}