Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

9.8 KB
Raw Blame
//! Config-backed operations for the user-facing agent registry.

use std::collections::HashMap;

use crate::agent::OpenHumanSessionHost;
use crate::agent_registry::host_definition::AgentDefinitionRegistry;
use crate::config::Config;
use crate::core::runtime::DispatchCtx;

use super::defaults::default_agents;
use super::types::{AgentRegistryEntry, AgentRegistryPatch, AgentRegistrySource, AgentToolInfo};

const ORCHESTRATOR_AGENT_ID: &str = "orchestrator";

pub async fn list_agents(
    ctx: &DispatchCtx,
    include_disabled: bool,
) -> Result<Vec<AgentRegistryEntry>, String> {
    let config = ctx.load_config().await?;
    Ok(merge_entries(
        &config.agent_registry.entries,
        include_disabled,
    ))
}

pub async fn get_agent(ctx: &DispatchCtx, id: &str) -> Result<Option<AgentRegistryEntry>, String> {
    let id = id.trim();
    Ok(list_agents(ctx, true)
        .await?
        .into_iter()
        .find(|agent| agent.id == id))
}

pub async fn upsert_custom_agent(
    ctx: &DispatchCtx,
    mut entry: AgentRegistryEntry,
) -> Result<AgentRegistryEntry, String> {
    entry.source = AgentRegistrySource::Custom;
    entry.validate()?;

    if default_agents().iter().any(|agent| agent.id == entry.id) {
        return Err(format!(
            "agent '{}' is a default agent; use update_agent to override it",
            entry.id
        ));
    }

    let mut config = ctx.load_config().await?;
    match config
        .agent_registry
        .entries
        .iter_mut()
        .find(|agent| agent.id == entry.id)
    {
        Some(existing) => *existing = entry.clone(),
        None => config.agent_registry.entries.push(entry.clone()),
    }
    config.save().await.map_err(|error| {
        crate::config::ops::config_save_error_with_full_context(
            error,
            "failed to save config",
            true,
        )
    })?;
    Ok(entry)
}

pub async fn update_agent(
    ctx: &DispatchCtx,
    id: &str,
    patch: AgentRegistryPatch,
) -> Result<AgentRegistryEntry, String> {
    let id = id.trim();
    if id.is_empty() {
        return Err("id is required".to_string());
    }

    let defaults = default_agents();
    let mut config = ctx.load_config().await?;
    let entry = match config
        .agent_registry
        .entries
        .iter_mut()
        .find(|agent| agent.id == id)
    {
        Some(existing) => existing,
        None => {
            let base = defaults
                .iter()
                .find(|agent| agent.id == id)
                .cloned()
                .ok_or_else(|| format!("agent '{id}' not found"))?;
            config.agent_registry.entries.push(base);
            config
                .agent_registry
                .entries
                .last_mut()
                .expect("pushed entry")
        }
    };

    apply_patch(entry, patch);
    entry.validate()?;
    ensure_orchestrator_enabled(entry)?;
    let updated = entry.clone();
    config.save().await.map_err(|error| {
        crate::config::ops::config_save_error_with_full_context(
            error,
            "failed to save config",
            true,
        )
    })?;
    Ok(updated)
}

pub async fn set_agent_enabled(
    ctx: &DispatchCtx,
    id: &str,
    enabled: bool,
) -> Result<AgentRegistryEntry, String> {
    update_agent(
        ctx,
        id,
        AgentRegistryPatch {
            enabled: Some(enabled),
            ..AgentRegistryPatch::default()
        },
    )
    .await
}

pub async fn remove_agent(ctx: &DispatchCtx, id: &str) -> Result<bool, String> {
    let id = id.trim();
    if id.is_empty() {
        return Err("id is required".to_string());
    }

    let mut config = ctx.load_config().await?;
    let before = config.agent_registry.entries.len();
    config.agent_registry.entries.retain(|agent| agent.id != id);
    let removed = config.agent_registry.entries.len() < before;
    if removed {
        config.save().await.map_err(|error| {
            crate::config::ops::config_save_error_with_full_context(
                error,
                "failed to save config",
                true,
            )
        })?;
    }
    Ok(removed)
}

/// List every assignable agent tool, with descriptions, for the editor's
/// tool picker.
///
/// Built from the orchestrator session's *durable* registry
/// (`durable_tool_specs_arc`), not its advertised belt: every session is built
/// over the complete built-in tool catalog and narrows only what it
/// advertises, so the durable specs are exactly the identifiers a
/// `tool_allowlist` is matched against, including `Deferred` tools the
/// orchestrator reaches through `tool_search`. Synthesised delegation tools
/// live in a separate set and are intentionally not included — the picker
/// surfaces the stable built-in surface only. Sorted + deduped by name for a
/// stable picker UI.
pub async fn available_tools(ctx: &DispatchCtx) -> Result<Vec<AgentToolInfo>, String> {
    let config = ctx.load_config().await?;
    AgentDefinitionRegistry::init_global(&config.workspace_dir)
        .map_err(|e| format!("failed to initialise AgentDefinitionRegistry: {e}"))?;
    let agent = OpenHumanSessionHost::from_config_for_agent(ctx, &config, ORCHESTRATOR_AGENT_ID)
        .map_err(|e| format!("failed to build tools-catalog agent: {e}"))?;

    let mut tools: Vec<AgentToolInfo> = agent
        .durable_tool_specs_arc()
        .iter()
        .map(|spec| AgentToolInfo {
            name: spec.name.clone(),
            description: spec.description.clone(),
        })
        .collect();
    tools.sort_by(|a, b| a.name.cmp(&b.name));
    tools.dedup_by(|a, b| a.name == b.name);
    Ok(tools)
}

pub fn merge_entries(
    configured: &[AgentRegistryEntry],
    include_disabled: bool,
) -> Vec<AgentRegistryEntry> {
    let mut default_order = Vec::new();
    let mut merged: HashMap<String, AgentRegistryEntry> = HashMap::new();
    for agent in default_agents() {
        default_order.push(agent.id.clone());
        merged.insert(agent.id.clone(), agent);
    }

    let mut custom_order = Vec::new();
    for entry in configured {
        if matches!(entry.source, AgentRegistrySource::Custom) && !merged.contains_key(&entry.id) {
            custom_order.push(entry.id.clone());
        }
        merged.insert(entry.id.clone(), entry.clone());
    }

    let mut result = Vec::new();
    for id in default_order.into_iter().chain(custom_order) {
        if let Some(agent) = merged.remove(&id) {
            if include_disabled || agent.enabled {
                result.push(agent);
            }
        }
    }
    result
}

/// Synchronous, config-only lookup for a user-authored (`Custom`-source),
/// **enabled** agent registry entry by id.
///
/// Used by the agent factory (`OpenHumanSessionHost::from_config_for_agent` family, see
/// `agent::session_host::builder::factory`) on a harness-registry lookup
/// miss so a custom agent can be synthesized into a real
/// `AgentDefinition` (via `definition_from_registry_entry`) and run with its
/// real tool belt, instead of erroring in chat or degrading to
/// a persona-only completion (flows). Deliberately sync — unlike
/// [`get_agent`]/[`list_agents`] — because the factory already holds a
/// `&Config` in scope and must not spawn an async config reload mid-build.
///
/// Only `AgentRegistrySource::Custom` entries match: a `Default`-sourced
/// override (a user edit to a shipped agent, e.g. via `update_agent`) is
/// already resolvable through the harness `AgentDefinitionRegistry` by id —
/// that agent ships an `agent.toml`/builtin definition — so it never reaches
/// this fallback path.
///
/// A **disabled** custom entry is deliberately treated as a miss (`None`),
/// same as an unknown id — never synthesized into a runnable definition here.
/// Every caller of this function (chat and flows' registry
/// routing) resolves an agent id directly to "runnable or not"; without this
/// filter a disabled custom agent referenced by an existing profile or a
/// direct caller could still run through the harness path, silently
/// bypassing the disabled flag the flows path already enforces explicitly.
pub fn find_custom_in_config(config: &Config, id: &str) -> Option<AgentRegistryEntry> {
    let id = id.trim();
    config
        .agent_registry
        .entries
        .iter()
        .find(|entry| {
            entry.id == id && entry.enabled && matches!(entry.source, AgentRegistrySource::Custom)
        })
        .cloned()
}

fn apply_patch(entry: &mut AgentRegistryEntry, patch: AgentRegistryPatch) {
    if let Some(name) = patch.name {
        entry.name = name;
    }
    if let Some(description) = patch.description {
        entry.description = description;
    }
    if let Some(enabled) = patch.enabled {
        entry.enabled = enabled;
    }
    if let Some(model) = patch.model {
        entry.model = Some(model);
    }
    if let Some(system_prompt) = patch.system_prompt {
        entry.system_prompt = Some(system_prompt);
    }
    if let Some(tool_allowlist) = patch.tool_allowlist {
        entry.tool_allowlist = tool_allowlist;
    }
    if let Some(tool_denylist) = patch.tool_denylist {
        entry.tool_denylist = tool_denylist;
    }
    if let Some(tool_rules) = patch.tool_rules {
        // An empty rule set clears the field rather than storing a no-op.
        entry.tool_rules = (!tool_rules.is_permissive()).then_some(tool_rules);
    }
    if let Some(subagents) = patch.subagents {
        entry.subagents = subagents;
    }
    if let Some(tags) = patch.tags {
        entry.tags = tags;
    }
    if let Some(metadata) = patch.metadata {
        entry.metadata = metadata;
    }
}

fn ensure_orchestrator_enabled(entry: &AgentRegistryEntry) -> Result<(), String> {
    if entry.id == ORCHESTRATOR_AGENT_ID && !entry.enabled {
        return Err("orchestrator agent cannot be disabled".to_string());
    }
    Ok(())
}

#[cfg(test)]
#[path = "ops_tests.rs"]
mod tests;