Skip to main content

magi_code/
lib.rs

1mod agent;
2mod appearance;
3mod auth;
4mod bash_mode;
5mod browser;
6mod cancellation;
7mod checkpoints;
8mod cli;
9mod code_mode;
10mod commands;
11mod compaction;
12pub mod config;
13pub mod context;
14mod diff_review;
15mod fast;
16mod hash;
17mod herdr;
18mod hex;
19mod hooks;
20mod http_body;
21mod instructions;
22mod local_agent;
23mod lsp;
24mod mcp;
25mod model_catalog;
26mod output;
27mod path_utils;
28mod persistence;
29mod primary_agents;
30mod process_environment;
31#[cfg(test)]
32mod profiling;
33mod prompt_file;
34mod protection;
35mod providers;
36mod rendering;
37mod runtime_context;
38mod security;
39mod session_titles;
40pub mod sessions;
41mod shell;
42mod skills;
43mod subagents;
44mod summarizer;
45mod thinking;
46mod thread_join;
47mod tool_output_measurement;
48mod tools;
49mod tui;
50mod typesafe;
51mod updates;
52
53// Intentional public API surface for the CLI crate. Keep additions and removals explicit:
54// - `run_from_env()` and `run_from_env_with_exit_code()` are the supported binary entrypoints.
55// - `config` exposes durable paths, settings, hooks, and custom-provider data.
56// - `context` exposes context-budget and token-estimation helpers.
57// - `sessions` exposes session managers, session IDs, and latest-title lookup.
58// - `ChatMessage`, `MessageRole`, `Usage`, and `ThinkingLevel` are shared data contracts.
59// Provider, auth, command, instruction, skill, agent, shell, subagent, TUI, rendering, and
60// persistence internals stay private to the crate.
61
62pub use providers::{ChatMessage, MessageRole, Usage};
63pub use thinking::ThinkingLevel;
64
65use anyhow::Result;
66use clap::Parser;
67
68pub fn run_from_env() -> Result<()> {
69    run_from_env_inner().map_err(crate::cli::AppError::into_error)
70}
71
72pub fn run_from_env_with_exit_code() -> std::process::ExitCode {
73    match run_from_env_inner() {
74        Ok(()) => std::process::ExitCode::SUCCESS,
75        Err(error) => {
76            if !error.as_error().is::<local_agent::TransportStopped>() {
77                eprintln!("Error: {:?}", error.as_error());
78            }
79            error.exit_code()
80        }
81    }
82}
83
84fn run_from_env_inner() -> crate::cli::AppResult<()> {
85    // Claude CLI launches this binary as its inventory-only MCP server; dispatch before CLI parsing.
86    if let Some(path) = std::env::var_os("MAGI_CLAUDE_INERT_MCP_INVENTORY") {
87        return providers::claude_inert::serve_inventory(std::path::Path::new(&path))
88            .map_err(crate::cli::AppError::runtime);
89    }
90    let args = cli::CliArgs::parse();
91    cli::run(args)
92}