// Pure fleet model: turns central's per-project `meta` into one Robot shape. // No DOM, no network — keep it that way so it runs under `node --test`. // Mirrors central's robot_kind_of() (app.py), plus kinds central does not // know yet. Keep in sync with KNOWN_ROBOT_KINDS there. export const KINDS = { reachy_mini: { label: "Reachy Mini" }, microduck: { label: "Micro Duck" }, grabette: { label: "Grabette" }, other: { label: "Other" }, }; const DEFAULT_KIND = "reachy_mini"; const KIND_BY_COMPACT = Object.fromEntries( Object.keys(KINDS).filter((k) => k !== "other").map((k) => [k.replace(/_/g, ""), k]), ); // Peers that register as producers but are not robots. const PSEUDO_PEER_NAMES = new Set(["central-relay"]); const usable = (v) => typeof v === "string" && v.trim() !== ""; export function kindOf(meta) { if (!meta || typeof meta !== "object") return DEFAULT_KIND; let raw = meta.kind; if (!usable(raw)) raw = meta.robot_type; if (!usable(raw)) return DEFAULT_KIND; if (raw.length > 64) return "other"; return KIND_BY_COMPACT[raw.toLowerCase().replace(/[^a-z0-9]/g, "")] ?? "other"; } export function isPseudoPeer(entry) { return PSEUDO_PEER_NAMES.has(entry?.meta?.name ?? entry?.robotName); } // One /api/robot-status entry -> Robot. `now` is wall-clock ms. export function normalizeRobot(entry, now = Date.now()) { const meta = entry.meta && typeof entry.meta === "object" ? entry.meta : {}; const kind = kindOf(meta); const peerId = entry.peerId ?? entry.peer_id ?? entry.id; // peerId changes on every reconnect; hardware_id / install_id are stable. // Hardware ids are not comparable across kinds, so namespace them. const stableId = meta.hardware_id ?? meta.install_id; const age = Number(entry.last_seen_age_seconds); return { key: stableId ? `${kind}:${stableId}` : `${kind}:peer:${peerId}`, kind, name: meta.name ?? entry.robotName ?? entry.name ?? "(unnamed)", peerId, hardwareId: stableId ?? null, version: meta.release ?? meta.version ?? null, apiVersion: meta.api_version ?? null, transport: meta.transport ?? null, busy: Boolean(entry.busy), activeApp: entry.activeApp ?? null, lastSeenAt: Number.isFinite(age) ? now - age * 1000 : now, online: true, meta, }; } export function normalizeFleet(entries, now = Date.now()) { const byKey = new Map(); for (const entry of entries ?? []) { if (isPseudoPeer(entry)) continue; const robot = normalizeRobot(entry, now); const prev = byKey.get(robot.key); // Central already dedupes on hardware_id, but keep the freshest just in case. if (!prev || robot.lastSeenAt > prev.lastSeenAt) byKey.set(robot.key, robot); } return [...byKey.values()]; } // Merge live robots with robots this browser saw before (central forgets // offline robots). Returns { robots, history } where history is the new // value to persist. export function mergeWithHistory(live, history = {}, now = Date.now(), maxAgeMs = 30 * 864e5) { const next = {}; for (const [key, r] of Object.entries(history)) { if (now - r.lastSeenAt < maxAgeMs) next[key] = r; } for (const r of live) { const { meta, online, busy, activeApp, peerId, ...persisted } = r; next[r.key] = { ...next[r.key], ...persisted }; } const liveKeys = new Set(live.map((r) => r.key)); const offline = Object.values(next) .filter((r) => !liveKeys.has(r.key)) .map((r) => ({ ...r, online: false, busy: false, activeApp: null, peerId: null, meta: {} })); return { robots: [...live, ...offline], history: next }; } export function countByKind(robots) { const counts = {}; for (const r of robots) counts[r.kind] = (counts[r.kind] ?? 0) + 1; return counts; } export function formatAge(ms) { const s = Math.max(0, Math.round(ms / 1000)); if (s < 60) return `${s}s`; const m = Math.round(s / 60); if (m < 60) return `${m} min`; const h = Math.round(m / 60); if (h < 48) return `${h} h`; return `${Math.round(h / 24)} d`; }