add list_accounts daemon op to hive-matrix-mcp

This commit is contained in:
damocles 2026-06-18 12:19:59 +02:00 committed by mara
commit 68b0894f5b
3 changed files with 90 additions and 1 deletions

View file

@ -20,7 +20,7 @@ use std::path::PathBuf;
use std::sync::Arc;
use matrix_sdk::Client;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use crate::paths;
@ -97,6 +97,28 @@ pub fn configured() -> anyhow::Result<Vec<AccountCfg>> {
Ok(accounts)
}
/// Live status of one matrix account, as reported by [`Registry::list`]
/// (the `list_accounts` daemon op). Only accounts that successfully
/// restored a session appear, so `live` is always `true` today; the
/// field is kept so a future "configured but down" entry can report
/// `false` without a wire-shape change.
#[derive(Debug, Serialize)]
pub struct AccountStatus {
/// Logical account name (the `account` arg on the MCP tools).
pub name: String,
/// Effective homeserver URL the restored client is talking to.
pub homeserver: String,
/// The account's own matrix user id (`@user:server`), when known.
pub user_id: Option<String>,
/// Whether the account has a live, restored client. Always `true`
/// for registry entries today (the registry only holds restored
/// accounts); reserved for future configured-but-down reporting.
pub live: bool,
/// Whether this is the primary account (selected when a tool call
/// omits `account`).
pub is_primary: bool,
}
/// Account name → live `Client` map plus the primary-account name used
/// when a request omits `account`. Built once at daemon startup from
/// the accounts that successfully restored a session.
@ -127,6 +149,32 @@ impl Registry {
self.by_name.is_empty()
}
/// Snapshot every restored account: name, homeserver, user id, and
/// primary flag. Registry membership == a session restored, so every
/// entry is reported `live`. Sorted primary-first then by name for a
/// stable order in the dashboard. Account-agnostic — the caller does
/// not resolve a single client (see `socket::dispatch`).
#[must_use]
pub fn list(&self) -> Vec<AccountStatus> {
let mut out: Vec<AccountStatus> = self
.by_name
.iter()
.map(|(name, client)| AccountStatus {
name: name.clone(),
homeserver: client.homeserver().to_string(),
user_id: client.user_id().map(ToString::to_string),
live: true,
is_primary: *name == self.primary,
})
.collect();
out.sort_by(|a, b| {
b.is_primary
.cmp(&a.is_primary)
.then_with(|| a.name.cmp(&b.name))
});
out
}
/// Resolve a request's `account` to a client. `None` → the primary
/// account. Returns a human-readable error (listing known accounts)
/// when the name is unknown — surfaced to the agent as a tool error.