agent-hub-runtime 0.6.0

AgentHub desktop and companion CLI runtime for Tauri applications
Documentation
//! Read-only projection of the official desktop app's versioned Core document.
use serde::Deserialize;
use serde_json::{json, Value};

pub(super) const DOCUMENT_NAME: &str = "herdr-app";
pub(super) const SPACE_PREFIX: &str = "hc:space/";

pub(super) fn terminal_tab(tab: &str, pane: &str, name: &str, terminal: &str) -> Value {
    json!({
        "id": tab, "name": name,
        "root": {"id": tab, "type": "row", "children": [
            {"id": pane, "type": "block", "content": {"terminal": terminal}}
        ]}
    })
}

#[derive(Default, Deserialize)]
pub(super) struct DesktopLayout {
    #[serde(default)]
    pub spaces: Vec<Space>,
}

#[derive(Deserialize)]
pub(super) struct Space {
    pub id: String,
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub folder: String,
    #[serde(default)]
    pub tabs: Vec<Tab>,
}

#[derive(Deserialize)]
pub(super) struct Tab {
    pub id: String,
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub root: Value,
}

pub(super) fn valid_id(id: &str) -> bool {
    !id.is_empty()
        && id.len() <= 128
        && id
            .bytes()
            .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_'))
}

pub(super) fn terminal_addresses(node: &Value, addresses: &mut Vec<String>) {
    if let Some(address) = node["content"]["terminal"].as_str() {
        addresses.push(address.to_owned());
    }
    if let Some(children) = node["children"].as_array() {
        for child in children {
            terminal_addresses(child, addresses);
        }
    }
}

pub(super) fn terminal_layout(node: &Value, depth: usize) -> Option<Value> {
    if depth >= crate::core::terminal::TERMINAL_PANE_LIMIT {
        return None;
    }
    if let Some(address) = node["content"]["terminal"].as_str() {
        return Some(json!({"pane": format!("hc:{address}")}));
    }
    let mut children: Vec<_> = node["children"]
        .as_array()?
        .iter()
        .filter_map(|child| terminal_layout(child, depth + 1))
        .collect();
    match children.len() {
        0 => None,
        1 => children.pop(),
        _ => Some(json!({
            "direction": if node["type"] == "column" { "vertical" } else { "horizontal" },
            "children": children,
        })),
    }
}

impl DesktopLayout {
    pub(super) fn contains_terminal(&self, workspace: &str, address: &str) -> bool {
        let Some(id) = workspace.strip_prefix(SPACE_PREFIX) else {
            return false;
        };
        self.spaces
            .iter()
            .find(|space| space.id == id)
            .is_some_and(|space| {
                let mut addresses = Vec::new();
                for tab in space.tabs.iter().filter(|tab| valid_id(&tab.id)) {
                    terminal_addresses(&tab.root, &mut addresses);
                }
                addresses.iter().any(|candidate| candidate == address)
            })
    }
}