use anyhow::Context as _;
use clap::Parser;
use std::path::PathBuf;
use std::time::Duration;
use agent_block_core::{run, BlockConfig};
use agent_block_mcp::DEFAULT_RPC_TIMEOUT;
#[derive(Parser, Debug)]
#[command(
name = "agent-block",
about = "Single-purpose agent building block with built-in mesh communication"
)]
struct Cli {
#[arg(short = 's', long)]
script: PathBuf,
#[arg(short = 'r', long)]
relay: Option<String>,
#[arg(long, env = "AGENT_BLOCK_MESH_SECRET_KEY")]
secret_key: Option<String>,
#[arg(short = 'p', long, default_value = ".")]
project: PathBuf,
#[arg(long, value_name = "SECS", value_parser = clap::value_parser!(u64).range(1..))]
mcp_timeout_secs: Option<u64>,
#[arg(long, env = "AGENT_BLOCK_PROMPT")]
prompt: Option<String>,
#[arg(short = 'c', long, env = "AGENT_BLOCK_CONTEXT")]
context: Option<String>,
#[arg(long, value_name = "FILE", conflicts_with = "prompt")]
prompt_file: Option<PathBuf>,
#[arg(long, value_name = "FILE", conflicts_with = "context")]
context_file: Option<PathBuf>,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let _ = rustls::crypto::ring::default_provider().install_default();
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
)
.init();
let cli = Cli::parse();
let mcp_rpc_timeout = cli
.mcp_timeout_secs
.map(Duration::from_secs)
.unwrap_or(DEFAULT_RPC_TIMEOUT);
let prompt = match cli.prompt_file {
Some(ref path) => {
let content = std::fs::read_to_string(path)
.with_context(|| format!("failed to read --prompt-file '{}'", path.display()))?;
Some(content)
}
None => cli.prompt,
};
let context = match cli.context_file {
Some(ref path) => {
let content = std::fs::read_to_string(path)
.with_context(|| format!("failed to read --context-file '{}'", path.display()))?;
Some(content)
}
None => cli.context,
};
let config = BlockConfig {
script_path: cli.script,
project_root: cli.project,
relay_url: cli.relay,
secret_key: cli.secret_key,
mcp_rpc_timeout,
prompt,
context,
host_handlers: std::collections::HashMap::new(),
};
Ok(run(config).await?)
}