Chat layout: roster, channel feed, composer
- The flock is a member list with live status (perched, flying, waiting on you, grounded); a scrolling strip on phones - The Sentry and crows post into one channel; approvals are inline buttons on their messages; verdicts show as reactions - "Who's flying" line above the composer; Ctrl/Cmd+Enter to send - Consecutive messages from one author group under one name - RIVE_CONTRACT.md: roster size and still message avatars Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UQnSw4RRWp8uqpj3nsnnXF
This commit is contained in:
5 files changed
+346
-258
No files matched your search
+161
-115
@@ -1,18 +1,16 @@
|
||||
import "./style.css";
|
||||
import { getTrust, listEvents, postList, postVerdict, type TrustLevels } from "./api";
|
||||
import { svgCrow, type CrowView, type Role } from "./crow";
|
||||
import { crowAvatar, svgCrow, type CrowView, type Role } from "./crow";
|
||||
|
||||
// ---------- page ----------
|
||||
const $ = <T extends HTMLElement>(id: string) => document.getElementById(id) as T;
|
||||
const perch = $<HTMLElement>("perch");
|
||||
const cards = $<HTMLOListElement>("cards");
|
||||
const roster = $<HTMLDivElement>("roster");
|
||||
const feed = $<HTMLOListElement>("feed");
|
||||
const typing = $<HTMLParagraphElement>("typing");
|
||||
const form = $<HTMLFormElement>("composer");
|
||||
const listInput = $<HTMLTextAreaElement>("list");
|
||||
const sendBtn = $<HTMLButtonElement>("send");
|
||||
const requesterInput = $<HTMLInputElement>("requester");
|
||||
const banner = $<HTMLParagraphElement>("banner");
|
||||
const toast = $<HTMLDivElement>("toast");
|
||||
const meter = $<HTMLParagraphElement>("meter");
|
||||
|
||||
const store = {
|
||||
get(k: string) { try { return localStorage.getItem(k); } catch { return null; } },
|
||||
@@ -20,8 +18,18 @@ const store = {
|
||||
};
|
||||
requesterInput.value = store.get("requester") ?? "";
|
||||
requesterInput.addEventListener("change", () => store.set("requester", requesterInput.value.trim()));
|
||||
listInput.addEventListener("keydown", (e) => {
|
||||
if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) { e.preventDefault(); form.requestSubmit(); }
|
||||
});
|
||||
|
||||
// ---------- the flock on screen ----------
|
||||
function el<K extends keyof HTMLElementTagNameMap>(tag: K, cls?: string, text?: string) {
|
||||
const e = document.createElement(tag);
|
||||
if (cls) e.className = cls;
|
||||
if (text !== undefined) e.textContent = text; // server text always goes in as text, never HTML
|
||||
return e;
|
||||
}
|
||||
|
||||
// ---------- the roster: live, animated crows ----------
|
||||
const crows = new Map<string, CrowView>();
|
||||
let trust: TrustLevels = new Map();
|
||||
|
||||
@@ -34,7 +42,7 @@ function crow(name: string): CrowView {
|
||||
if (!c) {
|
||||
c = svgCrow(name, roleOf(name));
|
||||
crows.set(name, c);
|
||||
perch.append(c.el);
|
||||
roster.append(c.el);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
@@ -45,36 +53,72 @@ function showTrust(name: string, taskType: string) {
|
||||
crow(name).setTrust(trust.get(key) ?? 0);
|
||||
}
|
||||
|
||||
// ---------- item cards ----------
|
||||
type Card = { el: HTMLLIElement; crows: string[]; taskType: string; actions: HTMLDivElement };
|
||||
const items = new Map<string, Card>();
|
||||
// ---------- the feed: messages from you, the Sentry, the crows ----------
|
||||
type Msg = { li: HTMLLIElement; body: HTMLDivElement; actions: HTMLDivElement; reactions: HTMLDivElement };
|
||||
|
||||
function el<K extends keyof HTMLElementTagNameMap>(tag: K, cls?: string, text?: string) {
|
||||
const e = document.createElement(tag);
|
||||
if (cls) e.className = cls;
|
||||
if (text !== undefined) e.textContent = text; // server text always goes in as text, never HTML
|
||||
return e;
|
||||
const ROLE_TAG: Partial<Record<Role, string>> = { sentry: "sorts the list", elder: "merges reports" };
|
||||
|
||||
function fmtTime(ts?: string) {
|
||||
const d = ts ? new Date(ts) : new Date();
|
||||
return isNaN(+d) ? "" : d.toLocaleTimeString([], { hour: "numeric", minute: "2-digit" });
|
||||
}
|
||||
|
||||
function addCard(itemId: string, text: string) {
|
||||
const li = el("li", "card inked");
|
||||
li.dataset.state = "waiting";
|
||||
const head = el("div", "card-head");
|
||||
head.append(el("span", "dot"), el("p", "text", text));
|
||||
function append(li: HTMLLIElement) {
|
||||
const stick = feed.scrollHeight - feed.scrollTop - feed.clientHeight < 120;
|
||||
feed.append(li);
|
||||
if (stick) feed.scrollTop = feed.scrollHeight;
|
||||
}
|
||||
|
||||
function post(author: string, parts: Node[], ts?: string): Msg {
|
||||
const li = el("li", "msg");
|
||||
li.dataset.author = author;
|
||||
const last = feed.lastElementChild as HTMLElement | null;
|
||||
if (last?.dataset.author === author) li.classList.add("cont");
|
||||
let avatar: Element;
|
||||
if (author === "you") {
|
||||
const who = requesterInput.value.trim() || "you";
|
||||
avatar = el("span", "avatar you", who.charAt(0).toUpperCase());
|
||||
} else {
|
||||
avatar = crowAvatar(roleOf(author));
|
||||
}
|
||||
const head = el("div", "msg-head");
|
||||
head.append(el("span", "author", author === "you" ? requesterInput.value.trim() || "You" : author));
|
||||
const tag = ROLE_TAG[roleOf(author)];
|
||||
if (author !== "you" && tag) head.append(el("span", "tag", tag));
|
||||
const time = el("time", "", fmtTime(ts));
|
||||
head.append(time);
|
||||
const body = el("div", "msg-body");
|
||||
body.append(...parts);
|
||||
const actions = el("div", "actions");
|
||||
li.append(head, el("div", "chips"), el("p", "reason"), el("ul", "subtasks"), el("blockquote", "answer"), actions);
|
||||
li.querySelector<HTMLElement>(".reason")!.hidden = true;
|
||||
li.querySelector<HTMLElement>(".answer")!.hidden = true;
|
||||
cards.append(li);
|
||||
items.set(itemId, { el: li, crows: [], taskType: "other", actions });
|
||||
const reactions = el("div", "reactions");
|
||||
const main = el("div", "msg-main");
|
||||
main.append(head, body, actions, reactions);
|
||||
li.append(avatar, main);
|
||||
append(li);
|
||||
return { li, body, actions, reactions };
|
||||
}
|
||||
|
||||
function ask(itemId: string, target: "triage" | "output") {
|
||||
const card = items.get(itemId);
|
||||
if (!card) return;
|
||||
card.el.dataset.state = "asking";
|
||||
card.actions.replaceChildren();
|
||||
const q = el("p", "question", target === "triage" ? "Approve the Sentry's plan?" : "Accept this answer?");
|
||||
function note(text: string, kind = "") {
|
||||
const li = el("li", `msg system ${kind}`.trim());
|
||||
li.append(el("p", "", text));
|
||||
append(li);
|
||||
}
|
||||
|
||||
function re(text: string) {
|
||||
return el("p", "re", `re: ${text}`);
|
||||
}
|
||||
|
||||
function react(m: Msg, text: string, kind: string) {
|
||||
m.reactions.append(el("span", `reaction ${kind}`, text));
|
||||
}
|
||||
|
||||
// ---------- per-item state ----------
|
||||
type Item = { text: string; crows: string[]; taskType: string; triage?: Msg; answer?: Msg };
|
||||
const items = new Map<string, Item>();
|
||||
|
||||
function ask(m: Msg, itemId: string, target: "triage" | "output") {
|
||||
m.li.classList.add("asking");
|
||||
m.actions.replaceChildren();
|
||||
const yes = el("button", "btn btn-go inked", target === "triage" ? "Fly it" : "Looks good");
|
||||
const no = el("button", "btn btn-no inked", target === "triage" ? "Not like that" : "Not right");
|
||||
const err = el("p", "err");
|
||||
@@ -91,141 +135,145 @@ function ask(itemId: string, target: "triage" | "output") {
|
||||
}
|
||||
});
|
||||
}
|
||||
const row = el("div", "buttons");
|
||||
row.append(no, yes);
|
||||
card.actions.append(q, row, err);
|
||||
yes.focus({ preventScroll: true });
|
||||
m.actions.append(yes, no, err);
|
||||
}
|
||||
|
||||
// ---------- who's flying ----------
|
||||
const flying = new Set<string>();
|
||||
function renderTyping() {
|
||||
const names = [...flying];
|
||||
if (names.length === 0) { typing.textContent = ""; return; }
|
||||
const who = names.length === 1 ? names[0]
|
||||
: names.length === 2 ? `${names[0]} and ${names[1]}`
|
||||
: `${names.slice(0, 2).join(", ")} and ${names.length - 2} more`;
|
||||
typing.textContent = `${who} ${names.length === 1 ? "is" : "are"} flying…`;
|
||||
}
|
||||
|
||||
// ---------- events (mapping from RIVE_CONTRACT.md section 3) ----------
|
||||
type Payload = Record<string, any>;
|
||||
let source: EventSource | null = null;
|
||||
const LEVELS = ["stranger", "regular", "trusted"];
|
||||
|
||||
const handlers: Record<string, (d: Payload) => void> = {
|
||||
"list.started": (d) => {
|
||||
const ol = el("ol", "list");
|
||||
for (const it of d.items ?? []) {
|
||||
items.set(it.item_id, { text: it.text, crows: [], taskType: "other" });
|
||||
ol.append(el("li", "", it.text));
|
||||
}
|
||||
post("you", [ol], d.ts);
|
||||
crow("Sentry").setPose("perched");
|
||||
for (const it of d.items ?? []) addCard(it.item_id, it.text);
|
||||
},
|
||||
|
||||
"item.triaged": (d) => {
|
||||
const card = items.get(d.item_id);
|
||||
if (!card) return;
|
||||
card.taskType = d.task_type;
|
||||
const chips = card.el.querySelector(".chips")!;
|
||||
chips.replaceChildren(el("span", `chip mode-${d.mode}`, d.mode), el("span", "chip", d.task_type));
|
||||
const reason = card.el.querySelector<HTMLElement>(".reason")!;
|
||||
reason.textContent = d.reason;
|
||||
reason.hidden = !d.reason;
|
||||
card.el.querySelector(".subtasks")!.replaceChildren(...(d.subtasks ?? []).map((s: string) => el("li", "", s)));
|
||||
const it = items.get(d.item_id);
|
||||
if (!it) return;
|
||||
it.taskType = d.task_type;
|
||||
const parts: Node[] = [re(it.text), el("p", "plan", `${d.mode} · ${d.task_type}`)];
|
||||
if (d.reason) parts.push(el("p", "reason", d.reason));
|
||||
if (d.subtasks?.length) {
|
||||
const ul = el("ul");
|
||||
for (const s of d.subtasks) ul.append(el("li", "", s));
|
||||
parts.push(ul);
|
||||
}
|
||||
it.triage = post("Sentry", parts, d.ts);
|
||||
showTrust("Sentry", d.task_type);
|
||||
if (d.needs_approval) {
|
||||
crow("Sentry").setPose("waiting");
|
||||
ask(d.item_id, "triage");
|
||||
ask(it.triage, d.item_id, "triage");
|
||||
}
|
||||
},
|
||||
|
||||
"crow.flying": (d) => {
|
||||
const card = items.get(d.item_id);
|
||||
if (card) {
|
||||
card.el.dataset.state = "flying";
|
||||
if (d.crow !== "Sentry") showTrust(d.crow, card.taskType);
|
||||
}
|
||||
const it = items.get(d.item_id);
|
||||
if (it && d.crow !== "Sentry") showTrust(d.crow, it.taskType);
|
||||
crow(d.crow).setPose("flying");
|
||||
flying.add(d.crow);
|
||||
renderTyping();
|
||||
},
|
||||
|
||||
"crow.landed": (d) => crow(d.crow).setPose(d.ok ? "perched" : "grounded"),
|
||||
"crow.landed": (d) => {
|
||||
crow(d.crow).setPose(d.ok ? "perched" : "grounded");
|
||||
flying.delete(d.crow);
|
||||
renderTyping();
|
||||
},
|
||||
|
||||
"item.answered": (d) => {
|
||||
const card = items.get(d.item_id);
|
||||
if (!card) return;
|
||||
card.crows = d.crows ?? [];
|
||||
const answer = card.el.querySelector<HTMLElement>(".answer")!;
|
||||
answer.textContent = d.answer;
|
||||
answer.hidden = false;
|
||||
const chips = card.el.querySelector(".chips")!;
|
||||
chips.append(el("span", "chip crows", card.crows.join(", ")));
|
||||
const it = items.get(d.item_id);
|
||||
if (!it) return;
|
||||
it.crows = d.crows ?? [];
|
||||
const author = it.crows.includes("Elder") ? "Elder" : it.crows[0] ?? "Elder";
|
||||
const parts: Node[] = [re(it.text), el("p", "answer", d.answer)];
|
||||
const helpers = it.crows.filter((n) => n !== "Elder");
|
||||
if (author === "Elder" && helpers.length) parts.push(el("p", "with", `from ${helpers.join(", ")}`));
|
||||
it.answer = post(author, parts, d.ts);
|
||||
if (d.needs_approval) {
|
||||
for (const n of card.crows) crow(n).setPose("waiting");
|
||||
ask(d.item_id, "output");
|
||||
for (const n of it.crows) crow(n).setPose("waiting");
|
||||
ask(it.answer, d.item_id, "output");
|
||||
}
|
||||
},
|
||||
|
||||
verdict: (d) => {
|
||||
const card = items.get(d.item_id);
|
||||
if (!card) return;
|
||||
card.actions.replaceChildren();
|
||||
const it = items.get(d.item_id);
|
||||
if (!it) return;
|
||||
const m = d.target === "triage" ? it.triage : it.answer;
|
||||
const good = d.verdict === "approved" || d.verdict === "auto";
|
||||
const who = d.target === "triage" ? ["Sentry"] : card.crows;
|
||||
if (m) {
|
||||
m.li.classList.remove("asking");
|
||||
m.actions.replaceChildren();
|
||||
const label: Record<string, [string, string]> = {
|
||||
approved: ["✓ Approved", "good"], rejected: ["✕ Rejected", "bad"],
|
||||
auto: ["✓ Trusted, no review", "auto"], timeout: ["⌛ Timed out", "bad"],
|
||||
};
|
||||
const [text, kind] = label[d.verdict] ?? [d.verdict, ""];
|
||||
react(m, text, kind);
|
||||
}
|
||||
if (d.target === "triage" && d.verdict === "auto") return; // a trusted Sentry just keeps going
|
||||
for (const n of who) {
|
||||
for (const n of d.target === "triage" ? ["Sentry"] : it.crows) {
|
||||
crow(n).fire(good ? "cheer" : "sulk");
|
||||
crow(n).setPose("perched");
|
||||
}
|
||||
if (d.target === "output") {
|
||||
card.el.dataset.state = good ? "done" : "rejected";
|
||||
if (d.verdict === "auto") card.actions.append(el("p", "note", "Trusted crows: accepted without asking."));
|
||||
} else if (!good) {
|
||||
card.el.dataset.state = "rejected";
|
||||
} else {
|
||||
card.el.dataset.state = "flying";
|
||||
}
|
||||
},
|
||||
|
||||
"trust.changed": (d) => {
|
||||
trust.set(d.holder === "Sentry" ? `Sentry|${d.task_type}` : `${d.holder}|${d.task_type}`, d.to);
|
||||
const names = ["stranger", "regular", "trusted"];
|
||||
trust.set(`${d.holder}|${d.task_type}`, d.to);
|
||||
const what = String(d.task_type).replace("triage/", "sorting ");
|
||||
const up = d.to > d.from;
|
||||
if (d.holder === "flock") {
|
||||
showBanner(d.to > d.from
|
||||
? `The flock earned "${names[d.to]}" on ${d.task_type}.`
|
||||
: `The flock dropped to "${names[d.to]}" on ${d.task_type}.`);
|
||||
note(`The flock ${up ? "earned" : "dropped to"} “${LEVELS[d.to]}” on ${what}.`, up ? "sparkle" : "");
|
||||
return;
|
||||
}
|
||||
const c = crow(d.holder);
|
||||
c.setTrust(d.to);
|
||||
c.fire(d.to > d.from ? "levelUp" : "levelDown");
|
||||
if (d.to > d.from) showBanner(`${d.holder} leveled up: ${names[d.to]} on ${String(d.task_type).replace("triage/", "triage of ")}.`);
|
||||
c.fire(up ? "levelUp" : "levelDown");
|
||||
note(`${d.holder} ${up ? "leveled up to" : "dropped to"} “${LEVELS[d.to]}” on ${what}.`, up ? "sparkle" : "");
|
||||
},
|
||||
|
||||
"item.skipped": (d) => {
|
||||
const card = items.get(d.item_id);
|
||||
if (!card) return;
|
||||
card.el.dataset.state = "skipped";
|
||||
card.actions.replaceChildren(el("p", "note", `Skipped: ${d.reason}`));
|
||||
for (const n of card.crows) crow(n).setPose("perched");
|
||||
const it = items.get(d.item_id);
|
||||
note(`Skipped “${it?.text ?? d.item_id}”: ${d.reason}.`);
|
||||
for (const n of it?.crows ?? []) crow(n).setPose("perched");
|
||||
},
|
||||
|
||||
"requester.pranked": (d) => prank(d.message, d.cooldown_s),
|
||||
|
||||
"list.finished": (d) => {
|
||||
for (const c of crows.values()) { c.setPose("perched"); c.fire("cheer"); }
|
||||
meter.textContent = `The flock used ${Number(d.tokens_used).toLocaleString()} of ${Number(d.token_budget).toLocaleString()} tokens.`;
|
||||
meter.hidden = false;
|
||||
flying.clear();
|
||||
renderTyping();
|
||||
note(`List done. The flock used ${Number(d.tokens_used).toLocaleString()} of ${Number(d.token_budget).toLocaleString()} tokens.`);
|
||||
source?.close();
|
||||
source = null;
|
||||
sendBtn.disabled = false;
|
||||
},
|
||||
};
|
||||
|
||||
// ---------- toasts ----------
|
||||
let bannerTimer = 0;
|
||||
function showBanner(text: string) {
|
||||
banner.textContent = text;
|
||||
banner.hidden = false;
|
||||
clearTimeout(bannerTimer);
|
||||
bannerTimer = window.setTimeout(() => (banner.hidden = true), 5000);
|
||||
}
|
||||
|
||||
// The prank: toast for a few seconds, and the send button counts down the cooldown.
|
||||
let toastTimer = 0;
|
||||
// ---------- pranks: the Sentry says something, the send button counts down ----------
|
||||
let cooldownTimer = 0;
|
||||
const sendLabel = sendBtn.textContent ?? "Send the flock";
|
||||
const sendLabel = sendBtn.textContent ?? "Send";
|
||||
function prank(message: string, cooldownS: number) {
|
||||
crow("Sentry").fire("prank");
|
||||
toast.textContent = message;
|
||||
toast.hidden = false;
|
||||
clearTimeout(toastTimer);
|
||||
toastTimer = window.setTimeout(() => (toast.hidden = true), 5000);
|
||||
|
||||
post("Sentry", [el("p", "", message)]);
|
||||
let left = Math.max(0, Math.round(cooldownS));
|
||||
clearInterval(cooldownTimer);
|
||||
const tick = () => {
|
||||
@@ -236,7 +284,7 @@ function prank(message: string, cooldownS: number) {
|
||||
return;
|
||||
}
|
||||
sendBtn.disabled = true;
|
||||
sendBtn.textContent = `The flock is ignoring you · ${left--}s`;
|
||||
sendBtn.textContent = `${left--}s`;
|
||||
};
|
||||
tick();
|
||||
cooldownTimer = window.setInterval(tick, 1000);
|
||||
@@ -246,7 +294,7 @@ function prank(message: string, cooldownS: number) {
|
||||
form.addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
const text = listInput.value.trim();
|
||||
if (!text) { listInput.focus(); return; }
|
||||
if (!text || sendBtn.disabled) { listInput.focus(); return; }
|
||||
const requester = requesterInput.value.trim() || "anonymous";
|
||||
store.set("requester", requester);
|
||||
sendBtn.disabled = true;
|
||||
@@ -256,16 +304,13 @@ form.addEventListener("submit", async (e) => {
|
||||
if (res.pranked) {
|
||||
prank(res.message, res.cooldownS); // re-enables the button when the cooldown ends
|
||||
} else {
|
||||
showBanner(res.message);
|
||||
note(res.message);
|
||||
sendBtn.disabled = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Fresh list: clear the old cards, keep the crows (they remember you).
|
||||
cards.replaceChildren();
|
||||
items.clear();
|
||||
meter.hidden = true;
|
||||
listInput.value = "";
|
||||
trust = await getTrust();
|
||||
source?.close();
|
||||
source = listEvents(res.listId);
|
||||
@@ -276,5 +321,6 @@ form.addEventListener("submit", async (e) => {
|
||||
}
|
||||
});
|
||||
|
||||
// The Sentry is always on watch.
|
||||
// The Sentry is always on watch, and says hello.
|
||||
crow("Sentry");
|
||||
post("Sentry", [el("p", "", "Caw. Send me a todo list, one item per line. I'll sort it, the flock will fly it, and you judge the results.")]);
|
||||
Reference in new issue
Block a user