Files
rhema-together/src/presence.rs
T
vhaudiquet 0035f4e031 feat: add the multiplayer room server and client
A room is one conversation, one ssh sandbox, and one agent. Accounts
create rooms; friends join through a single-use invite link and a
display name; the agent speaks only through the message tool.

The server is Axum and SQLite. It serves the built Nuxt client, streams
room events over SSE, and runs one serialized turn loop per room with
turn-level steering copied from rhema-code. The client has the chat,
preview iframe, read-only file pane, xterm terminal, and presence.

Preview URLs are signed wildcard hosts, and the proxy tunnels HTTP over
ssh direct-tcpip. HostAgent::step remains deferred in the framework.
2026-11-28 01:09:37 +01:00

93 lines
2.9 KiB
Rust

//! Who has a room open right now. Presence is ephemeral: it never enters
//! the room log or the model's history.
use std::collections::HashMap;
use std::sync::Mutex;
use serde::{Deserialize, Serialize};
use tokio::sync::broadcast;
/// What one connection reports. The server relays it to the others.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Peer {
pub member_id: String,
pub display_name: String,
pub color: String,
/// Free-form client state: typing, open file, active pane.
#[serde(default)]
pub state: serde_json::Value,
}
struct Room {
next: u64,
peers: HashMap<u64, Peer>,
tx: broadcast::Sender<Vec<Peer>>,
}
/// The per-room presence registry.
#[derive(Default)]
pub struct Presence {
rooms: Mutex<HashMap<String, Room>>,
}
impl Presence {
/// Registers a connection and returns its id, the current peer list,
/// and a subscription to changes.
pub fn join(&self, room: &str, peer: Peer) -> (u64, Vec<Peer>, broadcast::Receiver<Vec<Peer>>) {
let mut rooms = self.rooms.lock().unwrap_or_else(|err| err.into_inner());
let entry = rooms.entry(room.to_owned()).or_insert_with(|| {
let (tx, _) = broadcast::channel(128);
Room {
next: 0,
peers: HashMap::new(),
tx,
}
});
entry.next += 1;
let id = entry.next;
entry.peers.insert(id, peer);
let snapshot = entry.peers.values().cloned().collect();
let rx = entry.tx.subscribe();
(id, snapshot, rx)
}
/// Replaces a connection's state and broadcasts the new list.
pub fn update(&self, room: &str, id: u64, state: serde_json::Value) {
let mut rooms = self.rooms.lock().unwrap_or_else(|err| err.into_inner());
let Some(entry) = rooms.get_mut(room) else {
return;
};
if let Some(peer) = entry.peers.get_mut(&id) {
peer.state = state;
}
let snapshot: Vec<Peer> = entry.peers.values().cloned().collect();
let _ = entry.tx.send(snapshot);
}
/// Removes a connection and broadcasts the new list.
pub fn leave(&self, room: &str, id: u64) {
let mut rooms = self.rooms.lock().unwrap_or_else(|err| err.into_inner());
let Some(entry) = rooms.get_mut(room) else {
return;
};
entry.peers.remove(&id);
let snapshot: Vec<Peer> = entry.peers.values().cloned().collect();
let _ = entry.tx.send(snapshot);
}
/// The online member ids in a room.
pub fn online(&self, room: &str) -> Vec<String> {
let rooms = self.rooms.lock().unwrap_or_else(|err| err.into_inner());
rooms
.get(room)
.map(|entry| {
entry
.peers
.values()
.map(|peer| peer.member_id.clone())
.collect()
})
.unwrap_or_default()
}
}