import {HttpError, api, callerKey} from '@/lib/api' import {local} from '@/lib/reactive' import type {Runners} from '@/lib/api' /** The page's destinations: Home, Sessions and Workspace, and what More holds. */ export type Page = 'home' | 'sessions' | 'workspace' | 'activity' | 'memory' | 'settings' | 'more' | 'design' | 'autopilot' | 'focus' | 'research' import type {AdminState, Build, Decision, Deploys, WorkspaceEvent, WorkspaceState, Landing, PoolSample, Reservations, Session, SessionName, Take, Tally, Todo} from '@/lib/types' /** * The page's one store. The live part (`workspace`, `deploys`) is polled while the * page is open; the history is fetched for the chosen range when the History * tab is opened or the range changes. */ export const $s = local({ tab: 'home' as Page, hubVersion: '', hubRecipe: '', workspace: null as WorkspaceState | null, deploys: null as Deploys | null, names: {} as Record, reservations: {} as Reservations, todos: [] as Todo[], decisions: [] as Decision[], /** Admin windows (`/v1/admin`): null from a hub without them. */ admin: null as AdminState | null, error: '', /** Seconds since the last good poll, for "updated 4s ago". */ polledAt: 0, rangeDays: 30, /** When `history()` last changed (ms), 0 before it is read. */ historyAt: 0, historyLoading: false, logHours: 6, events: [] as WorkspaceEvent[], hasKey: callerKey() !== '', /** The logged-in account and its role (viewer, operator, steer, admin), if any. */ account: null as string | null, role: null as string | null, /** The hub wants a login before it shows anything (the page is behind the web proxy). */ needLogin: false, /** The hub already counts this page as a caller: a proxy adds the key. */ keyFromProxy: false, /** The node whose details are open. */ selected: null as string | null, /** The deploys' details are open. */ deploysOpen: false, }) export async function checkCaller() { const who = await api.caller().catch(() => ({caller: null, account: null, role: null})) $s.account = who.account $s.role = who.role $s.keyFromProxy = who.caller != null && who.account == null && callerKey() === '' // The page's controls change the workspace: operator and up. $s.hasKey = who.role != null && ['operator', 'steer', 'admin'].includes(who.role) } export async function poll() { try { const [workspace, deploys, health, names, reservations, todos, decisions, admin] = await Promise.all([ api.state(), api.deploys(), api.health(), api.names(), api.reservations(), api.todos(), // A hub from before decisions has none to show. api.decisions().catch(() => [] as Decision[]), api.admin().catch(() => null), ]) // Only what changed: each answer is a new object, and setting it redrew every view // that reads it (the session page reads the decisions) every ten seconds for nothing. const changed = (was: T, now: T) => JSON.stringify(was) !== JSON.stringify(now) if (changed($s.workspace, workspace)) $s.workspace = workspace if (changed($s.deploys, deploys)) $s.deploys = deploys if (changed($s.names, names)) $s.names = names if (changed($s.reservations, reservations)) $s.reservations = reservations if (changed($s.todos, todos)) $s.todos = todos if (changed($s.decisions, decisions)) $s.decisions = decisions if (changed($s.admin, admin)) $s.admin = admin $s.hubVersion = health.version $s.hubRecipe = health.recipe ?? '' $s.error = '' $s.polledAt = Date.now() $s.needLogin = false } catch (e) { if (e instanceof HttpError && e.status === 401) { $s.needLogin = true $s.error = '' } else { $s.error = `the hub did not answer: ${(e as Error).message}` } } } /** The history of the chosen range, as the hub answered it. */ export interface History { days: number builds: Build[] takes: Take[] pool: PoolSample[] sessions: Session[] landings: Landing[] conversations: Tally[] /** The sessions runners' stops and absences; null from a hub before runners.rs. */ runners: Runners | null } /* * Kept out of `$s`: the store wraps every object it holds in a proxy of signals, * and a year of builds is tens of thousands of them, wrapped as the view first * reads each one (0.4 s of a range switch). `$s.historyAt` says when it changed. */ let historyData: History | null = null /** The history of the chosen range, or null before it is read; a view reading it redraws when it changes. */ export function history(): History | null { return $s.historyAt ? historyData : null } export async function loadHistory() { const days = $s.rangeDays $s.historyLoading = true try { const since = days * 86400 const [builds, hist, sessions, landings, conversations, runners] = await Promise.all([ api.builds(since), api.history(since), api.sessions(since), api.landings(since), // A hub from before the tally has none: the rest of the history all the same. api.conversations(since).catch(() => [] as Tally[]), api.runners(since).catch(() => null), ]) if (days === $s.rangeDays) { historyData = {days, builds, takes: hist.takes, pool: hist.pool, sessions, landings, conversations, runners} $s.historyAt = Date.now() } } catch (e) { $s.error = `history: ${(e as Error).message}` } finally { $s.historyLoading = false } } export async function loadLog() { try { const events = await api.events($s.logHours * 3600) $s.events = events.reverse() } catch (e) { $s.error = `log: ${(e as Error).message}` } } /** Talking to agents — a message, an answer, a permission — takes the steer role. */ export const steers = () => $s.role === 'steer' || $s.role === 'admin'