forked from vhaudiquet/rhema-together
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.
This commit is contained in:
1 parent
04dd63c4ca
commit
0035f4e031
69 files changed
+22501
-48
No files matched your search
@@ -0,0 +1,82 @@
|
||||
//! The room turn runner. Filled in below.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use crate::state::Shared;
|
||||
|
||||
/// A wake signal for a room's turn task.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum Wake {
|
||||
/// A message was posted without mentioning the agent.
|
||||
Chat,
|
||||
/// A message mentioned the agent.
|
||||
Mention,
|
||||
}
|
||||
|
||||
struct Handle {
|
||||
tx: mpsc::UnboundedSender<Wake>,
|
||||
}
|
||||
|
||||
/// The live turn tasks, one per room.
|
||||
#[derive(Default)]
|
||||
pub struct RoomRunner {
|
||||
handles: Mutex<HashMap<String, Handle>>,
|
||||
current: Mutex<HashMap<String, Arc<AtomicBool>>>,
|
||||
}
|
||||
|
||||
impl RoomRunner {
|
||||
/// Notifies the room's task that new work may be pending, spawning
|
||||
/// the task on first use.
|
||||
pub fn wake(&self, state: &Shared, room: &str, wake: Wake) {
|
||||
let mut handles = self.handles.lock().unwrap_or_else(|err| err.into_inner());
|
||||
if let Some(handle) = handles.get(room) {
|
||||
let _ = handle.tx.send(wake);
|
||||
return;
|
||||
}
|
||||
let (tx, rx) = mpsc::unbounded_channel();
|
||||
let _ = tx.send(wake);
|
||||
handles.insert(room.to_owned(), Handle { tx });
|
||||
let room = room.to_owned();
|
||||
let weak = std::sync::Arc::downgrade(state);
|
||||
tokio::spawn(async move {
|
||||
let Some(state) = weak.upgrade() else { return };
|
||||
crate::room::runner::run(state, room, rx).await;
|
||||
});
|
||||
}
|
||||
|
||||
/// Records the cancel flag of the turn now running in `room`.
|
||||
pub fn set_current(&self, room: &str, cancel: Arc<AtomicBool>) {
|
||||
self.current
|
||||
.lock()
|
||||
.unwrap_or_else(|err| err.into_inner())
|
||||
.insert(room.to_owned(), cancel);
|
||||
}
|
||||
|
||||
/// Forgets the running turn.
|
||||
pub fn clear_current(&self, room: &str) {
|
||||
self.current
|
||||
.lock()
|
||||
.unwrap_or_else(|err| err.into_inner())
|
||||
.remove(room);
|
||||
}
|
||||
|
||||
/// Flips the running turn's cancel flag. False when nothing runs.
|
||||
pub fn interrupt(&self, room: &str) -> bool {
|
||||
let current = self.current.lock().unwrap_or_else(|err| err.into_inner());
|
||||
match current.get(room) {
|
||||
Some(flag) => {
|
||||
flag.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||
true
|
||||
}
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod agent;
|
||||
pub mod message_tool;
|
||||
pub mod runner;
|
||||
Reference in new issue
Block a user