butterfly-bot 0.6.0

Butterfly Bot is an opinionated personal-ops AI assistant built for people who want results, not setup overhead.
Documentation
use std::path::{Path, PathBuf};

use crate::error::{ButterflyBotError, Result};

const BUNDLED_WASM_MODULES: [(&str, &[u8]); 11] = [
    (
        "coding_tool.wasm",
        include_bytes!("../wasm/coding_tool.wasm"),
    ),
    ("mcp_tool.wasm", include_bytes!("../wasm/mcp_tool.wasm")),
    (
        "http_call_tool.wasm",
        include_bytes!("../wasm/http_call_tool.wasm"),
    ),
    (
        "github_tool.wasm",
        include_bytes!("../wasm/github_tool.wasm"),
    ),
    (
        "zapier_tool.wasm",
        include_bytes!("../wasm/zapier_tool.wasm"),
    ),
    (
        "planning_tool.wasm",
        include_bytes!("../wasm/planning_tool.wasm"),
    ),
    (
        "reminders_tool.wasm",
        include_bytes!("../wasm/reminders_tool.wasm"),
    ),
    (
        "search_internet_tool.wasm",
        include_bytes!("../wasm/search_internet_tool.wasm"),
    ),
    ("tasks_tool.wasm", include_bytes!("../wasm/tasks_tool.wasm")),
    ("todo_tool.wasm", include_bytes!("../wasm/todo_tool.wasm")),
    (
        "wakeup_tool.wasm",
        include_bytes!("../wasm/wakeup_tool.wasm"),
    ),
];

fn env_wasm_dir() -> Option<PathBuf> {
    std::env::var("BUTTERFLY_BOT_WASM_DIR")
        .ok()
        .map(|value| value.trim().to_string())
        .filter(|value| !value.is_empty())
        .map(PathBuf::from)
}

fn write_module_if_needed(root: &Path, file_name: &str, content: &[u8]) -> Result<()> {
    let path = root.join(file_name);

    if path.exists() {
        let existing = std::fs::read(&path).map_err(|e| {
            ButterflyBotError::Runtime(format!(
                "Failed to read bundled WASM module {}: {}",
                path.to_string_lossy(),
                e
            ))
        })?;

        if existing.as_slice() == content {
            return Ok(());
        }
    }

    let tmp_path = path.with_extension("wasm.tmp");
    std::fs::write(&tmp_path, content).map_err(|e| {
        ButterflyBotError::Runtime(format!(
            "Failed to write bundled WASM module {}: {}",
            tmp_path.to_string_lossy(),
            e
        ))
    })?;

    std::fs::rename(&tmp_path, &path).map_err(|e| {
        ButterflyBotError::Runtime(format!(
            "Failed to replace bundled WASM module {}: {}",
            path.to_string_lossy(),
            e
        ))
    })
}

fn provision_into_dir(wasm_dir: &Path) -> Result<()> {
    std::fs::create_dir_all(wasm_dir).map_err(|e| {
        ButterflyBotError::Runtime(format!(
            "Failed to create WASM tools directory {}: {}",
            wasm_dir.to_string_lossy(),
            e
        ))
    })?;

    for (file_name, content) in BUNDLED_WASM_MODULES {
        write_module_if_needed(wasm_dir, file_name, content)?;
    }

    Ok(())
}

pub fn ensure_bundled_wasm_tools() -> Result<PathBuf> {
    if let Some(wasm_dir) = env_wasm_dir() {
        provision_into_dir(&wasm_dir)?;
        std::env::set_var("BUTTERFLY_BOT_WASM_DIR", &wasm_dir);
        return Ok(wasm_dir);
    }

    let mut tried = Vec::new();
    let mut last_err = None;
    let candidates = crate::runtime_paths::default_wasm_dir_candidates();

    for wasm_dir in candidates {
        tried.push(wasm_dir.to_string_lossy().to_string());
        match provision_into_dir(&wasm_dir) {
            Ok(()) => {
                std::env::set_var("BUTTERFLY_BOT_WASM_DIR", &wasm_dir);
                return Ok(wasm_dir);
            }
            Err(err) => {
                last_err = Some(err);
            }
        }
    }

    let detail = last_err
        .map(|e| e.to_string())
        .unwrap_or_else(|| "no candidate directories available".to_string());
    Err(ButterflyBotError::Runtime(format!(
        "Could not provision bundled WASM tool modules. Tried: [{}]. Last error: {}",
        tried.join(", "),
        detail
    )))
}