eclipse_web/bots.rs
1//! `#509`: the bots this process declared, and the one agent each of them runs on.
2//!
3//! A **bot** is a declaration (`[bots.<name>]` in `config.toml`) plus one long-lived session
4//! whose `origin` is `bot:<name>`. Nothing here is a second runtime and nothing here is a second
5//! conversation store: the agent is the same [`Agent`] the human's own sessions run on, built from
6//! the same config with three things overridden — the instructions named by the declaration are
7//! added to the system prompt, the memory file is the bot's own, and the skills it may see are the
8//! ones it named.
9//!
10//! Built once, at startup, by [`Bots::build`]. A bot whose agent could not be built keeps the
11//! sentence that says why instead of vanishing: a declaration that does not work is a thing the
12//! Bots tab has to be able to show, because the alternative is a bot that looks reachable and
13//! answers nothing.
14
15use eclipse_core::config::{BotConfig, Config};
16use eclipse_core::runtime::Agent;
17use std::collections::BTreeMap;
18use std::sync::Arc;
19
20/// One bot's agent, or the reason it does not have one.
21pub type BotAgent = std::result::Result<Arc<Agent>, String>;
22
23pub struct Bots {
24 /// In declaration order — `BTreeMap` in the config, kept that way here so the tab lists the
25 /// same order twice.
26 agents: BTreeMap<String, BotAgent>,
27 /// What each one declares, for the tab: the lane, the memory path, the skills.
28 declared: BTreeMap<String, BotConfig>,
29}
30
31impl Default for Bots {
32 fn default() -> Self {
33 Self::empty()
34 }
35}
36
37impl Bots {
38 /// No bots: the state of every config that does not carry `[bots]`, and of every test that
39 /// builds [`crate::app::AppState`] by hand. The page then renders exactly what it did before
40 /// there were bots (`#509`).
41 pub fn empty() -> Self {
42 Self {
43 agents: BTreeMap::new(),
44 declared: BTreeMap::new(),
45 }
46 }
47
48 /// Build one agent per declared bot.
49 ///
50 /// A failure is kept, not fatal: a bot with a missing instructions file or a memory path that
51 /// cannot be resolved must not be able to take the human's dashboard down with it. The tab
52 /// names the failure and the bot answers nothing, which is the honest pair.
53 pub fn build(cfg: &Config, env: eclipse_core::config::EnvFn) -> Self {
54 let mut agents = BTreeMap::new();
55 let mut declared = BTreeMap::new();
56 for (name, bot) in &cfg.bots {
57 let built = Agent::for_bot(cfg, env, name)
58 .map(Arc::new)
59 .map_err(|e| e.to_string());
60 agents.insert(name.clone(), built);
61 declared.insert(name.clone(), bot.clone());
62 }
63 Self { agents, declared }
64 }
65
66 pub fn is_empty(&self) -> bool {
67 self.declared.is_empty()
68 }
69
70 /// The bot's name and declaration, in the order the config declared them.
71 pub fn declared(&self) -> impl Iterator<Item = (&str, &BotConfig)> {
72 self.declared.iter().map(|(name, bot)| (name.as_str(), bot))
73 }
74
75 /// The agent one bot's conversation runs on, or why it cannot run.
76 pub fn agent(&self, name: &str) -> Option<&BotAgent> {
77 self.agents.get(name)
78 }
79
80 /// Whether `name` is declared at all — the difference between "no such bot" and "this bot is
81 /// declared and broken", which are a 404 and a 503 in [`crate::app`].
82 pub fn declares(&self, name: &str) -> bool {
83 self.declared.contains_key(name)
84 }
85}