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 progressive_session_maintenance;
34mod prompt_file;
35mod protection;
36mod providers;
37mod rendering;
38mod runtime_context;
39mod security;
40mod session_titles;
41pub mod sessions;
42mod shell;
43mod skills;
44mod subagents;
45mod summarizer;
46mod thinking;
47mod thread_join;
48mod tool_output_measurement;
49mod tools;
50mod tui;
51mod typesafe;
52mod updates;
53
54// Intentional public API surface for the CLI crate. Keep additions and removals explicit:
55// - `run_from_env()` and `run_from_env_with_exit_code()` are the supported binary entrypoints.
56// - `config` exposes durable paths, settings, hooks, and custom-provider data.
57// - `context` exposes context-budget and token-estimation helpers.
58// - `sessions` exposes session managers, session IDs, and latest-title lookup.
59// - `ChatMessage`, `MessageRole`, `Usage`, and `ThinkingLevel` are shared data contracts.
60// Provider, auth, command, instruction, skill, agent, shell, subagent, TUI, rendering, and
61// persistence internals stay private to the crate.
62
63pub use providers::{ChatMessage, MessageRole, Usage};
64pub use thinking::ThinkingLevel;
65
66use anyhow::Result;
67use clap::Parser;
68
69pub fn run_from_env() -> Result<()> {
70    run_from_env_inner().map_err(crate::cli::AppError::into_error)
71}
72
73pub fn run_from_env_with_exit_code() -> std::process::ExitCode {
74    match run_from_env_inner() {
75        Ok(()) => std::process::ExitCode::SUCCESS,
76        Err(error) => {
77            if !error.as_error().is::<local_agent::TransportStopped>() {
78                eprintln!("Error: {:?}", error.as_error());
79            }
80            error.exit_code()
81        }
82    }
83}
84
85fn run_from_env_inner() -> crate::cli::AppResult<()> {
86    // Claude CLI launches this binary as its inventory-only MCP server; dispatch before CLI parsing.
87    if let Some(path) = std::env::var_os("MAGI_CLAUDE_INERT_MCP_INVENTORY") {
88        return providers::claude_inert::serve_inventory(std::path::Path::new(&path))
89            .map_err(crate::cli::AppError::runtime);
90    }
91    let args = cli::CliArgs::parse();
92    cli::run(args)
93}