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
+78
@@ -0,0 +1,78 @@
|
||||
//! Small helpers shared by the API and the turn runner.
|
||||
|
||||
use rand::RngCore;
|
||||
|
||||
/// Milliseconds since the Unix epoch.
|
||||
pub fn now_ms() -> i64 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_millis() as i64)
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
/// A URL-safe random token of `bytes` entropy, no padding.
|
||||
pub fn random_token(bytes: usize) -> String {
|
||||
use base64::Engine;
|
||||
let mut buf = vec![0u8; bytes];
|
||||
rand::thread_rng().fill_bytes(&mut buf);
|
||||
base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(buf)
|
||||
}
|
||||
|
||||
/// A short, lowercase, unguessable id: 12 characters over a 32-symbol
|
||||
/// alphabet (60 bits).
|
||||
pub fn random_id(len: usize) -> String {
|
||||
const ALPHABET: &[u8] = b"0123456789abcdefghjkmnpqrstvwxyz";
|
||||
let mut rng = rand::thread_rng();
|
||||
(0..len)
|
||||
.map(|_| ALPHABET[(rng.next_u32() % ALPHABET.len() as u32) as usize] as char)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// SHA-256 in lowercase hex.
|
||||
pub fn sha256_hex(input: &[u8]) -> String {
|
||||
use sha2::{Digest, Sha256};
|
||||
hex::encode(Sha256::digest(input))
|
||||
}
|
||||
|
||||
/// One of the member colors assigned when a member joins.
|
||||
pub fn member_color(index: usize) -> &'static str {
|
||||
const COLORS: &[&str] = &[
|
||||
"#3f6f5a", "#8a5a2b", "#4f5f8f", "#8f4f5a", "#6a5a8f", "#2f6f6f", "#7a6a2f", "#5a6a3f",
|
||||
];
|
||||
COLORS[index % COLORS.len()]
|
||||
}
|
||||
|
||||
/// `@rhema` in any case, as a whole word.
|
||||
pub fn mentions_agent(text: &str) -> bool {
|
||||
let lower = text.to_lowercase();
|
||||
let needle = "@rhema";
|
||||
let mut start = 0;
|
||||
while let Some(pos) = lower[start..].find(needle) {
|
||||
let at = start + pos;
|
||||
let after = at + needle.len();
|
||||
let boundary = lower[after..]
|
||||
.chars()
|
||||
.next()
|
||||
.map(|c| !c.is_alphanumeric() && c != '_')
|
||||
.unwrap_or(true);
|
||||
if boundary {
|
||||
return true;
|
||||
}
|
||||
start = after;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn mentions_need_a_word_boundary() {
|
||||
assert!(mentions_agent("@rhema fix this"));
|
||||
assert!(mentions_agent("hey @Rhema, look"));
|
||||
assert!(mentions_agent("ping @rhema."));
|
||||
assert!(!mentions_agent("@rhema2 go"));
|
||||
assert!(!mentions_agent("rhema go"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user