1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//! What a platform bot (the Telegram, Slack and Discord bots in their
//! adapter crates) runs on: the agent, sessions, the task registry and file
//! settings, as one bundle, and who counts as the owner.
use std::path::PathBuf;
use std::sync::Arc;
use aidaemon_tools::semantics::UserRole;
use crate::hub::{SessionChannelStore, SessionMap};
use crate::ports::ChannelAgentRuntime;
use crate::tasks::TaskRegistry;
use crate::turns::{TurnDeps, TurnWatch};
/// The services and settings every bot takes, so adding one does not
/// change each bot's constructor.
#[derive(Clone)]
pub struct BotDeps {
pub agent: Arc<dyn ChannelAgentRuntime>,
/// Which bot each session belongs to, for outbound routing.
pub session_map: SessionMap,
/// Where that mapping is persisted.
pub sessions: Arc<dyn SessionChannelStore>,
pub task_registry: Arc<TaskRegistry>,
/// Whether inbound files are accepted.
pub files_enabled: bool,
/// Where inbound files are saved.
pub inbox_dir: PathBuf,
/// Largest inbound file accepted, in MB.
pub max_file_size_mb: u64,
/// Heartbeat silence (seconds) after which a turn is cancelled as hung;
/// 0 never cancels.
pub watchdog_stale_threshold_secs: u64,
/// Who sees every turn (empty by default).
pub watch: TurnWatch,
}
impl BotDeps {
/// What the shared turn runner needs from these.
pub fn turn_deps(&self) -> TurnDeps {
TurnDeps {
agent: self.agent.clone(),
session_map: self.session_map.clone(),
sessions: self.sessions.clone(),
task_registry: self.task_registry.clone(),
watchdog_stale_threshold_secs: self.watchdog_stale_threshold_secs,
watch: self.watch.clone(),
}
}
}
/// A sender's role: Owner when `owner_ids` is empty (every allowed user is
/// an owner) or lists them; Guest otherwise.
pub fn determine_role(owner_ids: &[u64], user_id: u64) -> UserRole {
if owner_ids.is_empty() || owner_ids.contains(&user_id) {
UserRole::Owner
} else {
UserRole::Guest
}
}
/// The shared slash commands as the application runs them (its stores,
/// its extra commands).
#[async_trait::async_trait]
pub trait SharedCommandDispatch: Send + Sync {
/// The reply to `cmd`; `None` when it is not a shared command.
async fn dispatch(
&self,
cmd: &str,
args: &str,
session_id: &str,
sender_id: Option<&str>,
user_role: UserRole,
) -> Option<String>;
}
/// Keep a bot running: run `start` again whenever it returns, after a
/// backoff (5 s doubling to 60 s, reset by a run of a minute or more, 10%
/// jitter). `label` names the bot in the log ("Slack client").
pub async fn supervise<F, Fut>(label: &str, mut start: F)
where
F: FnMut() -> Fut,
Fut: std::future::Future<Output = ()>,
{
use std::time::Duration;
let mut backoff = aidaemon_tools::backoff::ChannelReconnectBackoff::new(
Duration::from_secs(5),
Duration::from_secs(60),
Duration::from_secs(60),
);
loop {
tracing::info!(label, "Starting");
let started = tokio::time::Instant::now();
start().await;
let ran_for = started.elapsed();
let delay = backoff.next_jittered_delay(ran_for, 0.10);
tracing::warn!(
label,
backoff_secs = delay.as_secs(),
ran_for_secs = ran_for.as_secs(),
"Stopped, restarting"
);
tokio::time::sleep(delay).await;
}
}