Per mara's review call on this PR: "the view should be filled by a single backend call." AgentsPage.tsx was doing three fetches (/api/agents, /api/config-prs, /api/agents/status) and joining them client-side by name. Moves the config-PR join server-side instead: AgentStatusRow gains a config_pr field, populated by get_agents_status's handler from AppState::config_prs after agent_status::AgentStatusReader::view() returns - not inside that module, which has no forge client and stays that way (see the field's doc comment for why the handler is the right layer for this merge, not the reader). AgentsPage.tsx now does exactly one fetch and no client-side joining at all - the wire row is the table row. Dropped the separate AgentStatusRow TS interface (folded into AgentRow, which now mirrors the backend type field-for-field) and the /api/agents + /api/config-prs fetches entirely; neither is needed once /api/agents/status already returns every roster agent with its config PR attached. ConfigPrStatus gained Deserialize (previously Serialize-only) since AgentStatusRow derives both and a struct's derive requires every field to support it.
188 lines
6.9 KiB
TypeScript
188 lines
6.9 KiB
TypeScript
// <AgentsPage> — the swarm's agent roster, merged with each agent's open
|
|
// config-PR status and swarm-wide health status in the same table. Three
|
|
// separate asks (a roster listing, a per-agent config-PR indicator, and
|
|
// per-agent status/liveness) that turned out to be one page: a roster with
|
|
// no per-row detail is thin, and neither a config-PR panel nor a status
|
|
// panel has anything to render against without a roster to embed in.
|
|
//
|
|
// One fetch, on a refresh-interval cadence like HivesPage: `GET
|
|
// /api/agents/status` — one row per roster agent (identity store is the
|
|
// roster, so a never-reported agent still gets a row), each already
|
|
// carrying its freshness/snapshot *and* its open config PR if any. That
|
|
// merge used to be three separate fetches (`/api/agents`, `/api/config-prs`,
|
|
// `/api/agents/status`) joined client-side by name; per operator review
|
|
// feedback ("the view should be filled by a single backend call") the
|
|
// join moved server-side instead — see `swarm-controller/src/main.rs`'s
|
|
// `get_agents_status` handler for where `config_pr` gets merged in and why
|
|
// that's the handler's job rather than `agent_status::AgentStatusReader`'s.
|
|
// No more per-page joining left to do here: the wire row *is* the table
|
|
// row.
|
|
//
|
|
// Owns the "+ agent" trigger too — the roster this populates is the
|
|
// natural home for the action that populates it; a separate top-level
|
|
// nav entry would be one click of indirection for no benefit. The form
|
|
// itself (`CreateAgentForm`) mounts inside a `Dialog` here rather than
|
|
// its own route.
|
|
import { useState } from 'preact/hooks';
|
|
import { ApiErrorPanel } from '@hive/shared/api-error-panel.js';
|
|
import { readApiError, type ProblemDetails } from '@hive/shared/api-error.js';
|
|
import { Badge, type BadgeTone } from '@hive/shared/badge.js';
|
|
import { Button } from '../ui/button/Button.js';
|
|
import { Dialog } from '../ui/dialog/Dialog.js';
|
|
import { Panel } from '../ui/panel/Panel.js';
|
|
import { RelativeTime } from '../ui/relative-time/RelativeTime.js';
|
|
import {
|
|
RefreshIntervalPicker,
|
|
useRefreshInterval,
|
|
type RefreshIntervalMs,
|
|
} from '../ui/refresh-interval/RefreshInterval.js';
|
|
import { Table, type TableColumn } from '../ui/table/Table.js';
|
|
import { CreateAgentForm } from './CreateAgentForm.js';
|
|
|
|
interface ConfigPrStatus {
|
|
pr_number: number;
|
|
html_url: string | null;
|
|
}
|
|
|
|
type Freshness = 'fresh' | 'stale' | 'never_reported' | 'unknown';
|
|
|
|
interface AgentStatusSnapshot {
|
|
status_text: string | null;
|
|
status_set_at: number | null;
|
|
running: boolean;
|
|
}
|
|
|
|
// Mirrors `agent_status::AgentStatusRow` field-for-field — this *is* the
|
|
// table row now, not a shape assembled from it, so there's no separate
|
|
// join-result type to keep in sync with the wire contract by hand.
|
|
interface AgentRow {
|
|
name: string;
|
|
hive: string | null;
|
|
freshness: Freshness;
|
|
last_seen_unix: number | null;
|
|
// `age_seconds` deliberately unused here, same reason as HivesPage:
|
|
// `RelativeTime` recomputes age client-side from `last_seen_unix`
|
|
// rather than rendering a once-computed-at-fetch value.
|
|
snapshot: AgentStatusSnapshot | null;
|
|
config_pr: ConfigPrStatus | null;
|
|
}
|
|
|
|
// Same tone/label pairing as HivesPage — one freshness enum shared by both
|
|
// endpoints, so the same rendering rule applies to both pages.
|
|
const FRESHNESS: Record<Freshness, { tone: BadgeTone; label: string }> = {
|
|
fresh: { tone: 'positive', label: 'fresh' },
|
|
stale: { tone: 'warning', label: 'stale' },
|
|
never_reported: { tone: 'neutral', label: 'never reported' },
|
|
unknown: { tone: 'negative', label: 'unknown' },
|
|
};
|
|
|
|
const COLUMNS: TableColumn<AgentRow>[] = [
|
|
{ key: 'name', header: 'name', render: (a) => a.name },
|
|
{ key: 'hive', header: 'hive', render: (a) => a.hive ?? '—' },
|
|
{
|
|
key: 'status',
|
|
header: 'status',
|
|
render: (a) => {
|
|
const { tone, label } = FRESHNESS[a.freshness];
|
|
const text = a.snapshot?.status_text;
|
|
// A `running: false` snapshot always carries `status_text: null`
|
|
// (the wire contract's own rule, not something this page derives),
|
|
// so an agent that reported recently but isn't running still shows
|
|
// a bare freshness badge rather than a stale status string.
|
|
return (
|
|
<Badge
|
|
tone={tone}
|
|
value={
|
|
<>
|
|
{text ? `${text} — ` : ''}
|
|
{label}
|
|
{a.last_seen_unix !== null ? (
|
|
<>
|
|
{' '}
|
|
(<RelativeTime epochMs={a.last_seen_unix * 1000} />)
|
|
</>
|
|
) : null}
|
|
</>
|
|
}
|
|
/>
|
|
);
|
|
},
|
|
},
|
|
{
|
|
key: 'config-pr',
|
|
header: 'config PR',
|
|
render: (a) =>
|
|
a.config_pr ? (
|
|
<Badge
|
|
tone="warning"
|
|
value={
|
|
a.config_pr.html_url ? (
|
|
<a href={a.config_pr.html_url} target="_blank" rel="noreferrer">
|
|
#{a.config_pr.pr_number}
|
|
</a>
|
|
) : (
|
|
`#${a.config_pr.pr_number}`
|
|
)
|
|
}
|
|
/>
|
|
) : (
|
|
'—'
|
|
),
|
|
},
|
|
];
|
|
|
|
// Same 30s default + same reasoning as HivesPage: no inputs on this page
|
|
// for a refresh to clobber, so the out-of-the-box behaviour should just
|
|
// solve staleness rather than require an opt-in every visit.
|
|
const DEFAULT_INTERVAL_MS: RefreshIntervalMs = 30_000;
|
|
|
|
export function AgentsPage() {
|
|
const [rows, setRows] = useState<AgentRow[] | null>(null);
|
|
const [error, setError] = useState<ProblemDetails | null>(null);
|
|
const [intervalMs, setIntervalMs] = useState<RefreshIntervalMs>(DEFAULT_INTERVAL_MS);
|
|
const [createOpen, setCreateOpen] = useState(false);
|
|
|
|
useRefreshInterval(intervalMs, () => {
|
|
(async () => {
|
|
const res = await fetch('/api/agents/status');
|
|
if (!res.ok) {
|
|
setError(await readApiError(res));
|
|
return;
|
|
}
|
|
setRows((await res.json()) as AgentRow[]);
|
|
// A refresh that succeeds clears a previous failure — otherwise a
|
|
// transient error would sit on screen forever after the data itself
|
|
// has recovered.
|
|
setError(null);
|
|
})().catch((e: unknown) => setError({ detail: String(e) }));
|
|
});
|
|
|
|
return (
|
|
<Panel
|
|
title="agents"
|
|
icon="👥"
|
|
actions={
|
|
<>
|
|
<Button variant="primary" onClick={() => setCreateOpen(true)}>
|
|
+ agent
|
|
</Button>
|
|
<RefreshIntervalPicker id="agents-refresh" value={intervalMs} onChange={setIntervalMs} />
|
|
</>
|
|
}
|
|
>
|
|
{error ? <ApiErrorPanel context="failed to load the agent roster" problem={error} /> : null}
|
|
{!error && rows === null ? <p>loading…</p> : null}
|
|
{rows ? (
|
|
<Table
|
|
columns={COLUMNS}
|
|
rows={rows}
|
|
rowKey={(a) => a.name}
|
|
emptyMessage="no agents yet — the swarm-wide identity store has no agents registered on any hive"
|
|
/>
|
|
) : null}
|
|
<Dialog open={createOpen} onClose={() => setCreateOpen(false)} label="create agent">
|
|
<CreateAgentForm />
|
|
</Dialog>
|
|
</Panel>
|
|
);
|
|
}
|