huskies: merge 626_refactor_introduce_services_bundle_and_migrate_appcontext_matrix_transport

This commit is contained in:
dave
2026-04-25 15:04:37 +00:00
parent aeff0b55be
commit 4b089c1ed8
21 changed files with 403 additions and 339 deletions
+18 -28
View File
@@ -1,14 +1,13 @@
//! Matrix bot run loop — connects to the homeserver and processes sync events.
use crate::agents::AgentPool;
use crate::services::Services;
use crate::slog;
use matrix_sdk::ruma::OwnedRoomId;
use matrix_sdk::{Client, LoopCtrl, config::SyncSettings};
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use tokio::sync::Mutex as TokioMutex;
use tokio::sync::{RwLock, mpsc, watch};
use tokio::sync::{RwLock, watch};
use super::context::BotContext;
use super::format::{format_startup_announcement, markdown_to_html};
@@ -22,16 +21,15 @@ use super::verification::{on_room_verification_request, on_to_device_verificatio
#[allow(clippy::too_many_arguments)]
pub async fn run_bot(
config: super::super::config::BotConfig,
project_root: PathBuf,
services: Arc<Services>,
watcher_rx: tokio::sync::broadcast::Receiver<crate::io::watcher::WatcherEvent>,
watcher_rx_auto: tokio::sync::broadcast::Receiver<crate::io::watcher::WatcherEvent>,
perm_rx: Arc<TokioMutex<mpsc::UnboundedReceiver<crate::http::context::PermissionForward>>>,
agents: Arc<AgentPool>,
shutdown_rx: watch::Receiver<Option<crate::rebuild::ShutdownReason>>,
gateway_active_project: Option<Arc<RwLock<String>>>,
gateway_projects: Vec<String>,
gateway_project_urls: std::collections::BTreeMap<String, String>,
) -> Result<(), String> {
let project_root = &services.project_root;
let store_path = project_root.join(".huskies").join("matrix_store");
let client = Client::builder()
.homeserver_url(config.homeserver.as_deref().unwrap_or_default())
@@ -174,20 +172,22 @@ pub async fn run_bot(
let poller_poll_interval = config.aggregated_notifications_poll_interval_secs;
let poller_enabled = config.aggregated_notifications_enabled;
let persisted = load_history(&project_root);
let persisted = load_history(project_root);
slog!(
"[matrix-bot] Loaded persisted conversation history for {} room(s)",
persisted.len()
);
// Restore persisted ambient rooms from config.
let persisted_ambient: HashSet<String> = config.ambient_rooms.iter().cloned().collect();
if !persisted_ambient.is_empty() {
slog!(
"[matrix-bot] Restored ambient mode for {} room(s): {:?}",
persisted_ambient.len(),
persisted_ambient
);
// Ambient rooms are already restored in Services from bot.toml config.
{
let ambient = services.ambient_rooms.lock().unwrap();
if !ambient.is_empty() {
slog!(
"[matrix-bot] Restored ambient mode for {} room(s): {:?}",
ambient.len(),
*ambient
);
}
}
// Create the transport abstraction based on the configured transport type.
@@ -222,11 +222,7 @@ pub async fn run_bot(
}
};
let bot_name = config
.display_name
.clone()
.unwrap_or_else(|| "Assistant".to_string());
let announce_bot_name = bot_name.clone();
let announce_bot_name = services.bot_name.clone();
let timer_store = Arc::new(crate::service::timer::TimerStore::load(
project_root.join(".huskies").join("timers.json"),
@@ -238,19 +234,13 @@ pub async fn run_bot(
);
let ctx = BotContext {
bot_user_id,
services,
matrix_user_id: bot_user_id,
target_room_ids,
project_root,
allowed_users: config.allowed_users,
history: Arc::new(TokioMutex::new(persisted)),
history_size: config.history_size,
bot_sent_event_ids: Arc::new(TokioMutex::new(HashSet::new())),
perm_rx,
pending_perm_replies: Arc::new(TokioMutex::new(HashMap::new())),
permission_timeout_secs: config.permission_timeout_secs,
bot_name,
ambient_rooms: Arc::new(std::sync::Mutex::new(persisted_ambient)),
agents,
htop_sessions: Arc::new(TokioMutex::new(HashMap::new())),
transport: Arc::clone(&transport),
timer_store,