agent-hub-runtime 0.6.0

AgentHub desktop and companion CLI runtime for Tauri applications
Documentation
//! Read-only identity shared by the desktop host and its companion CLI.

use std::path::{Path, PathBuf};
use std::sync::OnceLock;

use anyhow::{ensure, Result};

include!(concat!(env!("OUT_DIR"), "/features.rs"));

/// Product configuration supplied by the desktop or CLI executable.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Identity {
    pub product_id: &'static str,
    pub data_id: &'static str,
    pub version: &'static str,
    pub cli_name: &'static str,
    pub config_hash: &'static str,
}

const DEFAULT: Identity = Identity {
    product_id: env!("AGENT_HUB_PRODUCT_ID"),
    data_id: env!("AGENT_HUB_DATA_ID"),
    version: env!("AGENT_HUB_PRODUCT_VERSION"),
    cli_name: env!("AGENT_HUB_PRODUCT_CLI_NAME"),
    config_hash: env!("AGENT_HUB_PRODUCT_HASH"),
};

static IDENTITY: OnceLock<Identity> = OnceLock::new();

fn valid_id(id: &str) -> bool {
    id.as_bytes().first().is_some_and(u8::is_ascii_lowercase)
        && id
            .bytes()
            .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-')
}

/// Configure the process before constructing the desktop builder or parsing CLI arguments.
pub fn initialize(identity: Identity) -> Result<()> {
    ensure!(valid_id(identity.product_id), "Invalid product ID");
    ensure!(valid_id(identity.data_id), "Invalid product data ID");
    ensure!(
        identity.cli_name.strip_suffix("-cli").is_some_and(valid_id),
        "Invalid product CLI name"
    );
    ensure!(
        !identity.version.is_empty() && !identity.version.chars().any(char::is_control),
        "Invalid product version"
    );
    ensure!(
        !identity.config_hash.is_empty() && !identity.config_hash.chars().any(char::is_control),
        "Invalid product configuration hash"
    );
    ensure!(
        *IDENTITY.get_or_init(|| identity) == identity,
        "AgentHub product identity has already been initialized"
    );
    Ok(())
}

pub(crate) fn identity() -> &'static Identity {
    IDENTITY.get_or_init(|| DEFAULT)
}

pub fn version() -> &'static str {
    identity().version
}

pub fn cli_name() -> &'static str {
    identity().cli_name
}

#[derive(Debug, serde::Serialize)]
pub struct BuildInfo {
    pub product_id: &'static str,
    pub data_id: &'static str,
    pub config_hash: &'static str,
    pub base_commit: &'static str,
    pub features: Vec<&'static str>,
    pub version: &'static str,
}

/// Describe compiled features, independently of runtime environment variables.
pub fn current() -> BuildInfo {
    BuildInfo {
        product_id: identity().product_id,
        data_id: identity().data_id,
        config_hash: identity().config_hash,
        base_commit: env!("AGENT_HUB_BASE_COMMIT"),
        features: COMPILED_FEATURES.to_vec(),
        version: version(),
    }
}

/// Stable identity for application-owned data and platform schedules.
pub fn application_id() -> String {
    if identity().data_id == "standard" {
        "com.jtstudio.agenthub".into()
    } else {
        format!("com.jtstudio.agenthub.{}", identity().data_id)
    }
}

pub fn home_directory(home: &Path) -> PathBuf {
    home_for_id(home, identity().data_id)
}

pub(crate) fn product_home_directory(home: &Path) -> PathBuf {
    home_for_id(home, identity().product_id)
}

fn home_for_id(home: &Path, id: &str) -> PathBuf {
    if id == "standard" {
        home.join(".agent-hub")
    } else {
        home.join(format!(".agent-hub-{id}"))
    }
}

pub(crate) fn credential_service() -> String {
    if identity().data_id == "standard" {
        "agent-hub-git-backup".into()
    } else {
        format!("agent-hub-git-backup-{}", identity().data_id)
    }
}