eclipse-web 0.13.3

Local web dashboard for eclipse
Documentation
//! `#509`: the bots this process declared, and the one agent each of them runs on.
//!
//! A **bot** is a declaration (`[bots.<name>]` in `config.toml`) plus one long-lived session
//! whose `origin` is `bot:<name>`. Nothing here is a second runtime and nothing here is a second
//! conversation store: the agent is the same [`Agent`] the human's own sessions run on, built from
//! the same config with three things overridden — the instructions named by the declaration are
//! added to the system prompt, the memory file is the bot's own, and the skills it may see are the
//! ones it named.
//!
//! Built once, at startup, by [`Bots::build`]. A bot whose agent could not be built keeps the
//! sentence that says why instead of vanishing: a declaration that does not work is a thing the
//! Bots tab has to be able to show, because the alternative is a bot that looks reachable and
//! answers nothing.

use eclipse_core::config::{BotConfig, Config};
use eclipse_core::runtime::Agent;
use std::collections::BTreeMap;
use std::sync::Arc;

/// One bot's agent, or the reason it does not have one.
pub type BotAgent = std::result::Result<Arc<Agent>, String>;

pub struct Bots {
    /// In declaration order — `BTreeMap` in the config, kept that way here so the tab lists the
    /// same order twice.
    agents: BTreeMap<String, BotAgent>,
    /// What each one declares, for the tab: the lane, the memory path, the skills.
    declared: BTreeMap<String, BotConfig>,
}

impl Default for Bots {
    fn default() -> Self {
        Self::empty()
    }
}

impl Bots {
    /// No bots: the state of every config that does not carry `[bots]`, and of every test that
    /// builds [`crate::app::AppState`] by hand. The page then renders exactly what it did before
    /// there were bots (`#509`).
    pub fn empty() -> Self {
        Self {
            agents: BTreeMap::new(),
            declared: BTreeMap::new(),
        }
    }

    /// Build one agent per declared bot.
    ///
    /// A failure is kept, not fatal: a bot with a missing instructions file or a memory path that
    /// cannot be resolved must not be able to take the human's dashboard down with it. The tab
    /// names the failure and the bot answers nothing, which is the honest pair.
    pub fn build(cfg: &Config, env: eclipse_core::config::EnvFn) -> Self {
        let mut agents = BTreeMap::new();
        let mut declared = BTreeMap::new();
        for (name, bot) in &cfg.bots {
            let built = Agent::for_bot(cfg, env, name)
                .map(Arc::new)
                .map_err(|e| e.to_string());
            agents.insert(name.clone(), built);
            declared.insert(name.clone(), bot.clone());
        }
        Self { agents, declared }
    }

    pub fn is_empty(&self) -> bool {
        self.declared.is_empty()
    }

    /// The bot's name and declaration, in the order the config declared them.
    pub fn declared(&self) -> impl Iterator<Item = (&str, &BotConfig)> {
        self.declared.iter().map(|(name, bot)| (name.as_str(), bot))
    }

    /// The agent one bot's conversation runs on, or why it cannot run.
    pub fn agent(&self, name: &str) -> Option<&BotAgent> {
        self.agents.get(name)
    }

    /// Whether `name` is declared at all — the difference between "no such bot" and "this bot is
    /// declared and broken", which are a 404 and a 503 in [`crate::app`].
    pub fn declares(&self, name: &str) -> bool {
        self.declared.contains_key(name)
    }
}