mod channels;
mod completion;
mod config;
mod doctor;
pub mod fmt;
mod hooks;
#[cfg(feature = "import")]
pub mod import;
mod logs;
mod mcp;
pub mod memory;
mod models;
pub mod oauth_defaults;
mod pairing;
mod registry;
mod routines;
mod service;
mod skills;
pub mod status;
mod tool;
pub use channels::{ChannelsCommand, run_channels_command};
pub use completion::Completion;
pub use config::{ConfigCommand, run_config_command};
pub use doctor::run_doctor_command;
pub use hooks::{HooksCommand, run_hooks_command};
#[cfg(feature = "import")]
pub use import::{ImportCommand, run_import_command};
pub use logs::{LogsCommand, run_logs_command};
pub use mcp::{McpCommand, run_mcp_command};
pub use memory::MemoryCommand;
pub use memory::run_memory_command_with_db;
pub use models::{ModelsCommand, run_models_command};
pub use pairing::{PairingCommand, run_pairing_command, run_pairing_command_with_store};
pub use registry::{RegistryCommand, run_registry_command};
pub use routines::{RoutinesCommand, run_routines_command};
pub use service::{ServiceCommand, run_service_command};
pub use skills::{SkillsCommand, run_skills_command};
pub use status::run_status_command;
pub use tool::{ToolCommand, run_tool_command};
use std::sync::Arc;
use clap::{ColorChoice, Parser, Subcommand};
#[derive(Parser, Debug)]
#[command(name = "ironclaw")]
#[command(
about = "Secure personal AI assistant that protects your data and expands its capabilities"
)]
#[command(
long_about = "IronClaw is a secure AI assistant. Use 'ironclaw <subcommand> --help' for details.\nExamples:\n ironclaw run # Start the agent\n ironclaw config list # List configs"
)]
#[command(version)]
#[command(color = ColorChoice::Auto)] pub struct Cli {
#[command(subcommand)]
pub command: Option<Command>,
#[arg(long, global = true)]
pub cli_only: bool,
#[arg(long, global = true)]
pub no_db: bool,
#[arg(short, long, global = true)]
pub message: Option<String>,
#[arg(short, long, global = true)]
pub config: Option<std::path::PathBuf>,
#[arg(long, global = true)]
pub no_onboard: bool,
}
#[derive(Subcommand, Debug)]
pub enum Command {
#[command(
about = "Run the AI agent",
long_about = "Starts the IronClaw agent in default mode.\nExample: ironclaw run"
)]
Run,
#[command(
about = "Run interactive setup wizard",
long_about = "Guides through initial configuration.\nExamples:\n ironclaw onboard --skip-auth # Skip auth step\n ironclaw onboard --channels-only # Reconfigure channels\n ironclaw onboard --provider-only # Change LLM provider and model"
)]
Onboard {
#[arg(long)]
skip_auth: bool,
#[arg(long, conflicts_with_all = ["provider_only", "quick", "step"], help = "Deprecated: use --step channels")]
channels_only: bool,
#[arg(long, conflicts_with_all = ["channels_only", "quick", "step"], help = "Deprecated: use --step provider")]
provider_only: bool,
#[arg(long, conflicts_with_all = ["channels_only", "provider_only", "step"])]
quick: bool,
#[arg(long, value_delimiter = ',', conflicts_with_all = ["channels_only", "provider_only", "quick"])]
step: Vec<String>,
},
#[command(
subcommand,
about = "Manage app configs",
long_about = "Commands for listing, getting, and setting configurations.\nExample: ironclaw config list"
)]
Config(ConfigCommand),
#[command(
subcommand,
about = "Manage WASM tools",
long_about = "Install, list, or remove WASM-based tools.\nExample: ironclaw tool install mytool.wasm"
)]
Tool(ToolCommand),
#[command(
subcommand,
about = "Browse/install extensions",
long_about = "Interact with extension registry.\nExample: ironclaw registry list"
)]
Registry(RegistryCommand),
#[command(
subcommand,
about = "Manage channels",
long_about = "List configured messaging channels.\nExamples:\n ironclaw channels list\n ironclaw channels list --verbose\n ironclaw channels list --json"
)]
Channels(ChannelsCommand),
#[command(
subcommand,
alias = "cron",
about = "Manage routines",
long_about = "List, create, edit, enable/disable, delete, and view history of routines.\nExamples:\n ironclaw routines list\n ironclaw routines create --name daily-digest --schedule '0 0 9 * * *' --prompt 'Summarize today'"
)]
Routines(RoutinesCommand),
#[command(
subcommand,
about = "Manage MCP servers",
long_about = "Add, auth, list, or test MCP servers.\nExample: ironclaw mcp add notion https://mcp.notion.com"
)]
Mcp(Box<McpCommand>),
#[command(
subcommand,
about = "Manage workspace memory",
long_about = "Search, read, or write to memory.\nExample: ironclaw memory search 'query'"
)]
Memory(MemoryCommand),
#[command(
subcommand,
about = "Manage DM pairing",
long_about = "Approve or manage pairing requests.\nExamples:\n ironclaw pairing list telegram\n ironclaw pairing approve telegram ABC12345"
)]
Pairing(PairingCommand),
#[command(
subcommand,
about = "Manage OS service",
long_about = "Install, start, or stop service.\nExample: ironclaw service install"
)]
Service(ServiceCommand),
#[command(
subcommand,
about = "Manage skills",
long_about = "List, search, and inspect SKILL.md-based skills.\nExamples:\n ironclaw skills list\n ironclaw skills search 'writing'\n ironclaw skills info my-skill"
)]
Skills(SkillsCommand),
#[command(
subcommand,
about = "Manage lifecycle hooks",
long_about = "List and inspect lifecycle hooks (bundled, plugin, workspace).\nExamples:\n ironclaw hooks list\n ironclaw hooks list --verbose\n ironclaw hooks list --json"
)]
Hooks(HooksCommand),
#[command(
subcommand,
about = "Manage LLM providers and models",
long_about = "List providers, view current configuration, and set active provider/model.\nExamples:\n ironclaw models list\n ironclaw models list openai --verbose\n ironclaw models status\n ironclaw models set gpt-4o\n ironclaw models set-provider anthropic --model claude-sonnet-4-6-20250514"
)]
Models(ModelsCommand),
#[command(
about = "Run diagnostics",
long_about = "Checks dependencies and config validity.\nExample: ironclaw doctor"
)]
Doctor,
#[command(
about = "View and manage gateway logs",
long_about = "Tail gateway logs, stream live output, or adjust log level.\nExamples:\n ironclaw logs # Show last 200 lines from gateway.log\n ironclaw logs --follow # Stream live logs via SSE\n ironclaw logs --level # Show current log level\n ironclaw logs --level debug # Set log level to debug"
)]
Logs(LogsCommand),
#[command(
about = "Show system status",
long_about = "Displays health and diagnostics info.\nExample: ironclaw status"
)]
Status,
#[command(
about = "Generate completions",
long_about = "Generates shell completion scripts.\nExample: ironclaw completion --shell bash > ironclaw.bash"
)]
Completion(Completion),
#[cfg(feature = "import")]
#[command(
subcommand,
about = "Import from other AI systems",
long_about = "Migrate data from other AI assistants like OpenClaw.\nExample: ironclaw import openclaw"
)]
Import(ImportCommand),
#[command(
about = "Authenticate with a provider",
long_about = "Re-authenticate with an LLM provider.\nExample: ironclaw login --openai-codex"
)]
Login {
#[arg(long)]
openai_codex: bool,
},
#[command(hide = true)]
Worker {
#[arg(long)]
job_id: uuid::Uuid,
#[arg(long, default_value = "http://host.docker.internal:50051")]
orchestrator_url: String,
#[arg(long, default_value = "50")]
max_iterations: u32,
},
#[command(hide = true)]
ClaudeBridge {
#[arg(long)]
job_id: uuid::Uuid,
#[arg(long, default_value = "http://host.docker.internal:50051")]
orchestrator_url: String,
#[arg(long, default_value = "50")]
max_turns: u32,
#[arg(long, default_value = "sonnet")]
model: String,
},
}
impl Cli {
pub fn should_run_agent(&self) -> bool {
matches!(self.command, None | Some(Command::Run))
}
}
pub async fn init_secrets_store()
-> anyhow::Result<Arc<dyn crate::secrets::SecretsStore + Send + Sync>> {
let config = crate::config::Config::from_env().await?;
let master_key = config.secrets.master_key().ok_or_else(|| {
anyhow::anyhow!(
"SECRETS_MASTER_KEY not set. Run 'ironclaw onboard' first or set it in .env"
)
})?;
let crypto = Arc::new(crate::secrets::SecretsCrypto::new(master_key.clone())?);
Ok(crate::db::create_secrets_store(&config.database, crypto).await?)
}
pub async fn run_routines_cli(
routines_cmd: &RoutinesCommand,
config_path: Option<&std::path::Path>,
) -> anyhow::Result<()> {
let config = crate::config::Config::from_env_with_toml(config_path)
.await
.map_err(|e| anyhow::anyhow!("{e:#}"))?;
let db: Arc<dyn crate::db::Database> = crate::db::connect_from_config(&config.database)
.await
.map_err(|e| anyhow::anyhow!("{e:#}"))?;
let user_id = std::env::var("IRONCLAW_OWNER_ID").unwrap_or_else(|_| "default".to_string());
run_routines_command(routines_cmd.clone(), db, &user_id).await
}
pub async fn run_memory_command(mem_cmd: &MemoryCommand) -> anyhow::Result<()> {
let config = crate::config::Config::from_env()
.await
.map_err(|e| anyhow::anyhow!("{}", e))?;
let session = crate::llm::create_session_manager(config.llm.session.clone()).await;
let embeddings = config
.embeddings
.create_provider(&config.llm.nearai.base_url, session);
let db: Arc<dyn crate::db::Database> = crate::db::connect_from_config(&config.database)
.await
.map_err(|e| anyhow::anyhow!("{}", e))?;
let cache_config = crate::workspace::EmbeddingCacheConfig {
max_entries: config.embeddings.cache_size,
};
run_memory_command_with_db(mem_cmd.clone(), db, embeddings, cache_config).await
}
#[cfg(test)]
mod tests {
use super::*;
use clap::CommandFactory;
use insta::assert_snapshot;
#[test]
fn test_version() {
let cmd = Cli::command();
assert_eq!(
cmd.get_version().unwrap_or("unknown"),
env!("CARGO_PKG_VERSION")
);
}
#[test]
#[cfg(feature = "import")]
fn test_help_output() {
let mut cmd = Cli::command();
let help = cmd.render_help().to_string();
assert_snapshot!(help);
}
#[test]
#[cfg(not(feature = "import"))]
fn test_help_output_without_import() {
let mut cmd = Cli::command();
let help = cmd.render_help().to_string();
assert_snapshot!(help);
}
#[test]
#[cfg(feature = "import")]
fn test_long_help_output() {
let mut cmd = Cli::command();
let help = cmd.render_long_help().to_string();
assert_snapshot!(help);
}
#[test]
#[cfg(not(feature = "import"))]
fn test_long_help_output_without_import() {
let mut cmd = Cli::command();
let help = cmd.render_long_help().to_string();
assert_snapshot!(help);
}
}