Nests: team rooms with core crows and borrowing
- Server: nests API, persistence, core-first picking, borrowed flag (built by the backend agent against EVENTS.md; tests pass with -race) - UI: nests in the sidebar, one feed per nest, header shows the core team, New nest dialog, borrowed crows labeled, per-nest waiting count - start.command: one-click local server in controlled mode (mock crows, localhost only, state in .crows/) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UQnSw4RRWp8uqpj3nsnnXF
This commit is contained in:
14 files changed
+782
-62
No files matched your search
+151
-32
@@ -1,11 +1,12 @@
|
||||
import "./style.css";
|
||||
import { getTrust, listEvents, postList, postVerdict, type TrustLevels } from "./api";
|
||||
import { createNest, getNests, getTrust, listEvents, postList, postVerdict, type Nest, type TrustLevels } from "./api";
|
||||
import { crowAvatar, svgCrow, type CrowView, type Role } from "./crow";
|
||||
|
||||
// ---------- page ----------
|
||||
const $ = <T extends HTMLElement>(id: string) => document.getElementById(id) as T;
|
||||
const roster = $<HTMLDivElement>("roster");
|
||||
const feed = $<HTMLOListElement>("feed");
|
||||
const feedsBox = $<HTMLDivElement>("feeds");
|
||||
const nestNav = $<HTMLElement>("nests");
|
||||
const typing = $<HTMLParagraphElement>("typing");
|
||||
const form = $<HTMLFormElement>("composer");
|
||||
const listInput = $<HTMLTextAreaElement>("list");
|
||||
@@ -110,16 +111,70 @@ function fmtTime(ts?: string) {
|
||||
return isNaN(+d) ? "" : d.toLocaleTimeString([], { hour: "numeric", minute: "2-digit" });
|
||||
}
|
||||
|
||||
function append(li: HTMLLIElement) {
|
||||
// ---------- nests: one room (and one feed) per team ----------
|
||||
type NestRoom = Nest & { feed: HTMLOListElement; link: HTMLButtonElement; count: HTMLSpanElement };
|
||||
const nests = new Map<string, NestRoom>();
|
||||
let flockNames: string[] = [];
|
||||
let current = "todo";
|
||||
const roomTitle = $<HTMLHeadingElement>("room-title");
|
||||
const roomSub = $<HTMLParagraphElement>("room-sub");
|
||||
const roomTab = $<HTMLButtonElement>("tab-room");
|
||||
|
||||
function addNest(n: Nest): NestRoom {
|
||||
const feed = el("ol", "feed");
|
||||
feed.setAttribute("aria-live", "polite");
|
||||
feed.hidden = true;
|
||||
feedsBox.append(feed);
|
||||
const link = el("button", "nest-link");
|
||||
link.type = "button";
|
||||
const count = el("span", "nest-count");
|
||||
link.append(el("span", "nest-name", `# ${n.name}`), count);
|
||||
link.addEventListener("click", () => { selectNest(n.nestId); showView("room"); });
|
||||
nestNav.append(link);
|
||||
const room = { ...n, feed, link, count };
|
||||
nests.set(n.nestId, room);
|
||||
return room;
|
||||
}
|
||||
|
||||
function nest(id: string): NestRoom {
|
||||
return nests.get(id) ?? nests.get("todo")!;
|
||||
}
|
||||
|
||||
function selectNest(id: string) {
|
||||
current = nest(id).nestId;
|
||||
for (const r of nests.values()) {
|
||||
r.feed.hidden = r.nestId !== current;
|
||||
r.link.setAttribute("aria-current", String(r.nestId === current));
|
||||
}
|
||||
const r = nest(current);
|
||||
roomTitle.textContent = `# ${r.name}`;
|
||||
roomSub.textContent = r.crows.length
|
||||
? `Core crows: ${r.crows.join(", ")}. The Sentry borrows others when a job needs more hands.`
|
||||
: "The whole flock roosts here. Hand it a list: the Sentry sorts it, crows fly, you judge.";
|
||||
roomTab.textContent = `# ${r.name}`;
|
||||
r.feed.scrollTop = r.feed.scrollHeight;
|
||||
renderTyping();
|
||||
}
|
||||
|
||||
// Red count on each nest: questions waiting on you in that room.
|
||||
function renderNestCounts() {
|
||||
for (const r of nests.values()) {
|
||||
const n = r.feed.querySelectorAll(".msg.asking").length;
|
||||
r.count.textContent = n ? String(n) : "";
|
||||
}
|
||||
}
|
||||
|
||||
function append(li: HTMLLIElement, nestId: string) {
|
||||
const feed = nest(nestId).feed;
|
||||
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 {
|
||||
function post(author: string, parts: Node[], ts: string | undefined, nestId: string): Msg {
|
||||
const li = el("li", "msg");
|
||||
li.dataset.author = author;
|
||||
const last = feed.lastElementChild as HTMLElement | null;
|
||||
const last = nest(nestId).feed.lastElementChild as HTMLElement | null;
|
||||
if (last?.dataset.author === author) li.classList.add("cont");
|
||||
let avatar: Element;
|
||||
if (author === "you") {
|
||||
@@ -141,14 +196,14 @@ function post(author: string, parts: Node[], ts?: string): Msg {
|
||||
const main = el("div", "msg-main");
|
||||
main.append(head, body, actions, reactions);
|
||||
li.append(avatar, main);
|
||||
append(li);
|
||||
append(li, nestId);
|
||||
return { li, body, actions, reactions };
|
||||
}
|
||||
|
||||
function note(text: string, kind = "") {
|
||||
function note(text: string, nestId: string, kind = "") {
|
||||
const li = el("li", `msg system ${kind}`.trim());
|
||||
li.append(el("p", "", text));
|
||||
append(li);
|
||||
append(li, nestId);
|
||||
}
|
||||
|
||||
function re(text: string) {
|
||||
@@ -160,7 +215,7 @@ function react(m: Msg, text: string, kind: string) {
|
||||
}
|
||||
|
||||
// ---------- per-item state ----------
|
||||
type Item = { text: string; crows: string[]; taskType: string; triage?: Msg; answer?: Msg };
|
||||
type Item = { text: string; nest: string; crows: string[]; borrowed: Set<string>; taskType: string; triage?: Msg; answer?: Msg };
|
||||
const items = new Map<string, Item>();
|
||||
|
||||
function ask(m: Msg, itemId: string, target: "triage" | "output") {
|
||||
@@ -183,12 +238,14 @@ function ask(m: Msg, itemId: string, target: "triage" | "output") {
|
||||
});
|
||||
}
|
||||
m.actions.append(yes, no, err);
|
||||
renderNestCounts();
|
||||
}
|
||||
|
||||
// ---------- who's flying ----------
|
||||
const flying = new Set<string>();
|
||||
let flyingNest = "";
|
||||
function renderTyping() {
|
||||
const names = [...flying];
|
||||
const names = flyingNest === current ? [...flying] : [];
|
||||
if (names.length === 0) { typing.textContent = ""; return; }
|
||||
const who = names.length === 1 ? names[0]
|
||||
: names.length === 2 ? `${names[0]} and ${names[1]}`
|
||||
@@ -203,12 +260,14 @@ const LEVELS = ["stranger", "regular", "trusted"];
|
||||
|
||||
const handlers: Record<string, (d: Payload) => void> = {
|
||||
"list.started": (d) => {
|
||||
const nestId = nests.has(d.nest_id) ? d.nest_id : sendingNest;
|
||||
flyingNest = nestId;
|
||||
const ol = el("ol", "list");
|
||||
for (const it of d.items ?? []) {
|
||||
items.set(it.item_id, { text: it.text, crows: [], taskType: "other" });
|
||||
items.set(it.item_id, { text: it.text, nest: nestId, crows: [], borrowed: new Set(), taskType: "other" });
|
||||
ol.append(el("li", "", it.text));
|
||||
}
|
||||
post("you", [ol], d.ts);
|
||||
post("you", [ol], d.ts, nestId);
|
||||
crow("Sentry").setPose("perched");
|
||||
const n = (d.items ?? []).length;
|
||||
update("Sentry", `Got a list of ${n} item${n === 1 ? "" : "s"}`, d.ts);
|
||||
@@ -225,7 +284,7 @@ const handlers: Record<string, (d: Payload) => void> = {
|
||||
for (const s of d.subtasks) ul.append(el("li", "", s));
|
||||
parts.push(ul);
|
||||
}
|
||||
it.triage = post("Sentry", parts, d.ts);
|
||||
it.triage = post("Sentry", parts, d.ts, it.nest);
|
||||
showTrust("Sentry", d.task_type);
|
||||
if (d.needs_approval) {
|
||||
crow("Sentry").setPose("waiting");
|
||||
@@ -242,8 +301,10 @@ const handlers: Record<string, (d: Payload) => void> = {
|
||||
if (it && d.crow !== "Sentry") showTrust(d.crow, it.taskType);
|
||||
crow(d.crow).setPose("flying");
|
||||
if (it) {
|
||||
if (d.borrowed) it.borrowed.add(d.crow);
|
||||
const doing = d.crow === "Sentry" ? "Sorting" : d.crow === "Elder" ? "Merging reports on" : "Flying";
|
||||
update(d.crow, `${doing}${doing === "Flying" ? ":" : ""} ${q(it.text)}`, d.ts);
|
||||
const where = d.borrowed ? ` (borrowed for # ${nest(it.nest).name})` : "";
|
||||
update(d.crow, `${doing}${where}${doing === "Flying" ? ":" : ""} ${q(it.text)}`, d.ts);
|
||||
}
|
||||
flying.add(d.crow);
|
||||
renderTyping();
|
||||
@@ -265,8 +326,9 @@ const handlers: Record<string, (d: Payload) => void> = {
|
||||
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);
|
||||
const named = helpers.map((n) => (it.borrowed.has(n) ? `${n} (borrowed)` : n));
|
||||
if (author === "Elder" && helpers.length) parts.push(el("p", "with", `from ${named.join(", ")}`));
|
||||
it.answer = post(author, parts, d.ts, it.nest);
|
||||
if (d.needs_approval) {
|
||||
for (const n of it.crows) {
|
||||
crow(n).setPose("waiting");
|
||||
@@ -292,6 +354,7 @@ const handlers: Record<string, (d: Payload) => void> = {
|
||||
};
|
||||
const [text, kind] = label[d.verdict] ?? [d.verdict, ""];
|
||||
react(m, text, kind);
|
||||
renderNestCounts();
|
||||
}
|
||||
if (d.target === "triage" && d.verdict === "auto") return; // a trusted Sentry just keeps going
|
||||
const word: Record<string, string> = { approved: "Approved", rejected: "Rejected", auto: "Accepted (trusted)", timeout: "Timed out" };
|
||||
@@ -308,19 +371,20 @@ const handlers: Record<string, (d: Payload) => void> = {
|
||||
const what = String(d.task_type).replace("triage/", "sorting ");
|
||||
const up = d.to > d.from;
|
||||
if (d.holder === "flock") {
|
||||
note(`The flock ${up ? "earned" : "dropped to"} “${LEVELS[d.to]}” on ${what}.`, up ? "sparkle" : "");
|
||||
note(`The flock ${up ? "earned" : "dropped to"} “${LEVELS[d.to]}” on ${what}.`, flyingNest || current, up ? "sparkle" : "");
|
||||
return;
|
||||
}
|
||||
const c = crow(d.holder);
|
||||
c.setTrust(d.to);
|
||||
c.fire(up ? "levelUp" : "levelDown");
|
||||
update(d.holder, `${up ? "Leveled up to" : "Dropped to"} ${LEVELS[d.to]} on ${what}`, d.ts, up ? "good" : "bad");
|
||||
note(`${d.holder} ${up ? "leveled up to" : "dropped to"} “${LEVELS[d.to]}” on ${what}.`, up ? "sparkle" : "");
|
||||
note(`${d.holder} ${up ? "leveled up to" : "dropped to"} “${LEVELS[d.to]}” on ${what}.`, flyingNest || current, up ? "sparkle" : "");
|
||||
},
|
||||
|
||||
"item.skipped": (d) => {
|
||||
const it = items.get(d.item_id);
|
||||
note(`Skipped “${it?.text ?? d.item_id}”: ${d.reason}.`);
|
||||
note(`Skipped “${it?.text ?? d.item_id}”: ${d.reason}.`, it?.nest ?? flyingNest ?? current);
|
||||
renderNestCounts();
|
||||
for (const n of it?.crows ?? []) crow(n).setPose("perched");
|
||||
update("Sentry", `Skipped ${q(it?.text ?? d.item_id)}`, d.ts, "bad");
|
||||
},
|
||||
@@ -331,7 +395,8 @@ const handlers: Record<string, (d: Payload) => void> = {
|
||||
for (const t of tiles.values()) { t.view.setPose("perched"); t.view.fire("cheer"); }
|
||||
flying.clear();
|
||||
renderTyping();
|
||||
note(`List done. The flock used ${Number(d.tokens_used).toLocaleString()} of ${Number(d.token_budget).toLocaleString()} tokens.`);
|
||||
note(`List done. The flock used ${Number(d.tokens_used).toLocaleString()} of ${Number(d.token_budget).toLocaleString()} tokens.`, flyingNest || current);
|
||||
flyingNest = "";
|
||||
source?.close();
|
||||
source = null;
|
||||
sendBtn.disabled = false;
|
||||
@@ -343,7 +408,7 @@ let cooldownTimer = 0;
|
||||
const sendLabel = sendBtn.textContent ?? "Send";
|
||||
function prank(message: string, cooldownS: number) {
|
||||
crow("Sentry").fire("prank");
|
||||
post("Sentry", [el("p", "", message)]);
|
||||
post("Sentry", [el("p", "", message)], undefined, current);
|
||||
update("Sentry", "Left a gift on someone's windshield", undefined, "bad");
|
||||
let left = Math.max(0, Math.round(cooldownS));
|
||||
clearInterval(cooldownTimer);
|
||||
@@ -362,6 +427,7 @@ function prank(message: string, cooldownS: number) {
|
||||
}
|
||||
|
||||
// ---------- send a list ----------
|
||||
let sendingNest = "todo";
|
||||
form.addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
const text = listInput.value.trim();
|
||||
@@ -369,13 +435,14 @@ form.addEventListener("submit", async (e) => {
|
||||
const requester = requesterInput.value.trim() || "anonymous";
|
||||
store.set("requester", requester);
|
||||
sendBtn.disabled = true;
|
||||
sendingNest = current;
|
||||
|
||||
const res = await postList(text, requester).catch(() => ({ ok: false as const, pranked: false as const, message: "Can't reach the flock. Is `crows serve` running?" }));
|
||||
const res = await postList(text, requester, current).catch(() => ({ ok: false as const, pranked: false as const, message: "Can't reach the flock. Is `crows serve` running?" }));
|
||||
if (!res.ok) {
|
||||
if (res.pranked) {
|
||||
prank(res.message, res.cooldownS); // re-enables the button when the cooldown ends
|
||||
} else {
|
||||
note(res.message);
|
||||
note(res.message, current);
|
||||
sendBtn.disabled = false;
|
||||
}
|
||||
return;
|
||||
@@ -392,16 +459,68 @@ form.addEventListener("submit", async (e) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Phone tabs: the channel or the flock.
|
||||
// ---------- phone tabs: the room or the roost (nests + flock) ----------
|
||||
const app = document.getElementById("app")!;
|
||||
for (const [id, view] of [["tab-room", "room"], ["tab-flock", "flock"]] as const) {
|
||||
document.getElementById(id)!.addEventListener("click", () => {
|
||||
app.dataset.view = view;
|
||||
for (const t of document.querySelectorAll<HTMLButtonElement>(".tab")) t.setAttribute("aria-pressed", String(t.id === id));
|
||||
if (view === "room") feed.scrollTop = feed.scrollHeight;
|
||||
});
|
||||
function showView(view: "room" | "flock") {
|
||||
app.dataset.view = view;
|
||||
for (const t of document.querySelectorAll<HTMLButtonElement>(".tab")) {
|
||||
t.setAttribute("aria-pressed", String(t.id === (view === "room" ? "tab-room" : "tab-flock")));
|
||||
}
|
||||
if (view === "room") nest(current).feed.scrollTop = nest(current).feed.scrollHeight;
|
||||
}
|
||||
roomTab.addEventListener("click", () => showView("room"));
|
||||
$<HTMLButtonElement>("tab-flock").addEventListener("click", () => showView("flock"));
|
||||
|
||||
// The Sentry is always on watch, and says hello.
|
||||
// ---------- new nest dialog ----------
|
||||
const dialog = $<HTMLDialogElement>("nest-dialog");
|
||||
const nestForm = $<HTMLFormElement>("nest-form");
|
||||
const nestName = $<HTMLInputElement>("nest-name");
|
||||
const nestCrows = $<HTMLDivElement>("nest-crows");
|
||||
const nestErr = $<HTMLParagraphElement>("nest-err");
|
||||
|
||||
$<HTMLButtonElement>("new-nest").addEventListener("click", () => {
|
||||
nestErr.textContent = "";
|
||||
nestName.value = "";
|
||||
nestCrows.replaceChildren();
|
||||
for (const name of flockNames) {
|
||||
const label = el("label", "pick-crow");
|
||||
const box = el("input");
|
||||
box.type = "checkbox";
|
||||
box.value = name;
|
||||
label.append(box, crowAvatar("crow"), el("span", "", name));
|
||||
nestCrows.append(label);
|
||||
}
|
||||
dialog.showModal();
|
||||
nestName.focus();
|
||||
});
|
||||
|
||||
nestForm.addEventListener("submit", async (e) => {
|
||||
const submitter = (e as SubmitEvent).submitter as HTMLButtonElement | null;
|
||||
if (submitter?.value !== "create") return; // Cancel closes the dialog
|
||||
e.preventDefault();
|
||||
const name = nestName.value.trim();
|
||||
const crows = [...nestCrows.querySelectorAll<HTMLInputElement>("input:checked")].map((b) => b.value);
|
||||
if (!name) { nestErr.textContent = "Give the nest a name."; return; }
|
||||
if (!crows.length) { nestErr.textContent = "Pick at least one crow for the core team."; return; }
|
||||
try {
|
||||
const n = await createNest(name, crows);
|
||||
addNest(n);
|
||||
dialog.close();
|
||||
selectNest(n.nestId);
|
||||
showView("room");
|
||||
post("Sentry", [el("p", "", `New nest. Core crows: ${n.crows.join(", ")}. I'll borrow others when a job needs more hands.`)], undefined, n.nestId);
|
||||
} catch (err) {
|
||||
nestErr.textContent = (err as Error).message;
|
||||
}
|
||||
});
|
||||
|
||||
// ---------- start: load the nests, open # todo, the Sentry 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.")]);
|
||||
(async () => {
|
||||
const data = await getNests();
|
||||
flockNames = data.roster;
|
||||
if (!data.nests.some((n) => n.nestId === "todo")) data.nests.unshift({ nestId: "todo", name: "todo", crows: [] });
|
||||
for (const n of data.nests) addNest(n);
|
||||
selectNest("todo");
|
||||
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.")], undefined, "todo");
|
||||
})();
|
||||
Reference in new issue
Block a user