Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

2.7 KB
Raw Blame
//! Windows' service runner: starts `codync-host.exe` beside it with the given
//! arguments and no console window, and starts it again when it exits (what
//! launchd's `KeepAlive` and systemd's `Restart=always` do elsewhere). It is a
//! GUI-subsystem program, so signing in doesn't flash a console either.
#![cfg_attr(windows, windows_subsystem = "windows")]

#[cfg(windows)]
#[path = "../environment.rs"]
#[allow(dead_code)]
mod environment;

#[cfg(windows)]
fn main() {
    use std::os::windows::process::CommandExt as _;
    use std::time::Duration;
    /// The host gets a hidden console, which the agents it starts share.
    const CREATE_NO_WINDOW: u32 = 0x0800_0000;

    let Ok(exe) = std::env::current_exe() else { return };
    let host = exe.with_file_name("codync-host.exe");
    let args: Vec<_> = std::env::args_os().skip(1).collect();
    let data = std::env::var_os("CODYNC_HOME")
        .map(std::path::PathBuf::from)
        .or_else(|| dirs::home_dir().map(|h| h.join(environment::Environment::current().data_folder())));
    // One supervisor per data folder: the Run key and the app can both start one, and the second's
    // host could only fail on the data folder's lock, forever. It waits a little first, since
    // `install` starts the new supervisor right after stopping the old one.
    let lock = data.as_ref().and_then(|d| {
        std::fs::create_dir_all(d).ok()?;
        std::fs::OpenOptions::new().create(true).truncate(false).write(true).open(d.join("hostw.lock")).ok()
    });
    if let Some(lock) = &lock {
        let mut tries = 0;
        while fs2::FileExt::try_lock_exclusive(lock).is_err() {
            tries += 1;
            if tries == 20 {
                return;
            }
            std::thread::sleep(Duration::from_millis(500));
        }
    }
    loop {
        let mut command = std::process::Command::new(&host);
        // Tells the host it runs as the service (`service::started_by_service`).
        command.args(&args).env("CODYNC_SERVICE", "1").creation_flags(CREATE_NO_WINDOW);
        let log = data.as_ref().and_then(|d| {
            std::fs::create_dir_all(d).ok()?;
            std::fs::OpenOptions::new().create(true).append(true).open(d.join("host.log")).ok()
        });
        if let Some(log) = log
            && let Ok(err) = log.try_clone()
        {
            command.stdout(log).stderr(err);
        }
        // Whatever the exit (a crash, a host that can't start yet), try again shortly.
        let _ = command.status();
        std::thread::sleep(Duration::from_secs(3));
    }
}

#[cfg(not(windows))]
fn main() {
    eprintln!("codync-hostw only runs the host on Windows; use codync-host here.");
    std::process::exit(2);
}