Mega Mind v0.5.4

lib.rs

Rust · 58 lines · 1.6 KB
Symbols
//! Mega Mind: shared, searchable memory for AI coding agents. The `mind` command calls [`main`].

mod ai;
mod cli;
mod commands;
mod hooks;
mod index;
mod librarian;
mod links;
mod maintain;
mod privacy;
mod report;
mod setup;
mod store;
mod ui;
mod upgrade;
#[cfg(test)]
mod tests;

use clap::Parser;
use cli::{Cli, Cmd};
use store::Mind;

/// The whole program: parse the command line, run a hook or a command, exit 1 on error.
pub fn main() {
    // Exit quietly when piped into `head` instead of panicking on a closed stdout.
    #[cfg(unix)]
    unsafe {
        libc::signal(libc::SIGPIPE, libc::SIG_DFL);
    }
    let mut cli = Cli::parse();
    let Some(cmd) = cli.cmd.take() else {
        // Plain `mind`: the overview, or a pointer to setup on a fresh machine.
        match Mind::open() {
            Ok(b) => {
                if let Err(e) = report::stats(&b) {
                    eprintln!("{} {e:#}", ui::err_red("error:"));
                }
            }
            Err(_) => println!("No mind yet. Run `mind setup` to create one (`mind --help` for commands)."),
        }
        return;
    };
    let cli = Cli { cmd: Some(cmd), ..cli };
    if let Some(Cmd::Hook { name }) = &cli.cmd {
        match name.as_str() {
            "session-start" => hooks::session_start(),
            "stop" => hooks::stop(),
            "prompt" => hooks::prompt(),
            _ => {}
        }
        return;
    }
    if let Err(e) = commands::run(cli) {
        eprintln!("{} {}", ui::err_red("error:"), crate::store::strip_controls(&format!("{e:#}")));
        std::process::exit(1);
    }
}