swarm ui: an open todos section on the agent card
This commit is contained in:
parent
b239500f88
commit
fa2a75d499
8 changed files with 200 additions and 12 deletions
|
|
@ -43,6 +43,7 @@ import {
|
|||
startsOpen,
|
||||
type DetailSection,
|
||||
} from "./sections.js";
|
||||
import { AgentTodos } from "../terminal/AgentTodos.js";
|
||||
import { BashTasks } from "../terminal/BashTasks.js";
|
||||
import { SubagentTerms } from "../terminal/SubagentTerms.js";
|
||||
import { WantedMenu } from "../roster/WantedMenu.js";
|
||||
|
|
@ -317,6 +318,8 @@ function SectionBody({
|
|||
case "terminal":
|
||||
// The agent's live terminal and its input box.
|
||||
return <AgentTermPreview agentName={row.name} />;
|
||||
case "todos":
|
||||
return <AgentTodos key={row.name} agentName={row.name} />;
|
||||
case "subagents":
|
||||
return <SubagentTerms key={row.name} agentName={row.name} />;
|
||||
case "tasks":
|
||||
|
|
@ -332,7 +335,7 @@ export function AgentDetail({
|
|||
}: {
|
||||
row: AgentRow;
|
||||
actions: AgentActions;
|
||||
/** Add the live terminal, subagents and background tasks sections. */
|
||||
/** Add the live terminal, todos, subagents and background tasks sections. */
|
||||
withTerminal?: boolean;
|
||||
/**
|
||||
* An opened card, whose header already shows the state rows. Without it
|
||||
|
|
|
|||
|
|
@ -4,29 +4,26 @@
|
|||
|
||||
/** One folded block of an opened agent card. */
|
||||
export type DetailSection =
|
||||
| "status"
|
||||
| "terminal"
|
||||
| "subagents"
|
||||
| "tasks"
|
||||
| "settings";
|
||||
"status" | "terminal" | "todos" | "subagents" | "tasks" | "settings";
|
||||
|
||||
/** What each section's header says. */
|
||||
export const SECTION_TITLE: Record<DetailSection, string> = {
|
||||
status: "status",
|
||||
terminal: "terminal",
|
||||
todos: "open todos",
|
||||
subagents: "subagents",
|
||||
tasks: "background tasks",
|
||||
settings: "settings",
|
||||
};
|
||||
|
||||
/**
|
||||
* The sections a card shows, top to bottom. The terminal, subagents and tasks are
|
||||
* The sections a card shows, top to bottom. The terminal, todos, subagents and tasks are
|
||||
* left out where the page is already beside a terminal (the terminal page's
|
||||
* details popover).
|
||||
*/
|
||||
export function sectionsFor(withTerminal: boolean): DetailSection[] {
|
||||
return withTerminal
|
||||
? ["status", "terminal", "subagents", "tasks", "settings"]
|
||||
? ["status", "terminal", "todos", "subagents", "tasks", "settings"]
|
||||
: ["status", "settings"];
|
||||
}
|
||||
|
||||
|
|
|
|||
42
frontend/packages/swarm-ui/src/agent/terminal/AgentTodos.css
Normal file
42
frontend/packages/swarm-ui/src/agent/terminal/AgentTodos.css
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
/* <AgentTodos> — the rows of an agent's open todos. */
|
||||
.ui-agent-todos-count {
|
||||
margin: 0 0 0.5em;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.ui-agent-todos-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.35em;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
.ui-agent-todos-item {
|
||||
display: grid;
|
||||
grid-template-columns: 6em minmax(0, 1fr) auto;
|
||||
align-items: baseline;
|
||||
gap: 0.75em;
|
||||
}
|
||||
|
||||
.ui-agent-todos-subsystem {
|
||||
overflow: hidden;
|
||||
color: var(--blue);
|
||||
font-family: var(--mono, monospace);
|
||||
font-size: 0.85em;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.ui-agent-todos-summary {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.ui-agent-todos-age {
|
||||
color: var(--muted);
|
||||
font-size: 0.85em;
|
||||
white-space: nowrap;
|
||||
}
|
||||
50
frontend/packages/swarm-ui/src/agent/terminal/AgentTodos.tsx
Normal file
50
frontend/packages/swarm-ui/src/agent/terminal/AgentTodos.tsx
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
// <AgentTodos> — an agent's open todos, in its card's "todos" section: what each
|
||||
// says, which subsystem raised it and since when. The list is
|
||||
// `GET /api/agents/{name}/todos` (`useAgentTodos`), which swarm-controller reads
|
||||
// from what the agent's harness published. Read-only.
|
||||
//
|
||||
// Mount it keyed by agent name: the refresh timer is armed once per mount.
|
||||
import { ApiErrorPanel } from "@hive/shared/api-error-panel.js";
|
||||
import { Hint } from "../../ui/hint/Hint.js";
|
||||
import { RelativeTime } from "../../ui/relative-time/RelativeTime.js";
|
||||
import { todosLabel } from "./agent-todos.js";
|
||||
import { useAgentTodos } from "./useAgentTodos.js";
|
||||
import "./AgentTodos.css";
|
||||
|
||||
export function AgentTodos({ agentName }: { agentName: string }) {
|
||||
const { todos, error } = useAgentTodos(agentName);
|
||||
return (
|
||||
<section class="ui-agent-todos" aria-label={`${agentName}'s open todos`}>
|
||||
{error && (
|
||||
<ApiErrorPanel
|
||||
problem={error}
|
||||
context={`failed to list ${agentName}'s todos`}
|
||||
/>
|
||||
)}
|
||||
{todos === null ? (
|
||||
error ? null : (
|
||||
<Hint as="span">…</Hint>
|
||||
)
|
||||
) : todos.length === 0 ? (
|
||||
<Hint as="span">{todosLabel(todos)}</Hint>
|
||||
) : (
|
||||
<>
|
||||
<p class="ui-agent-todos-count">{todosLabel(todos)}</p>
|
||||
<ul class="ui-agent-todos-list">
|
||||
{todos.map((t) => (
|
||||
<li key={t.id} class="ui-agent-todos-item">
|
||||
<span class="ui-agent-todos-subsystem">{t.subsystem}</span>
|
||||
<span class="ui-agent-todos-summary" title={t.summary}>
|
||||
{t.summary}
|
||||
</span>
|
||||
<span class="ui-agent-todos-age">
|
||||
<RelativeTime epochMs={t.updated_unix * 1000} />
|
||||
</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</>
|
||||
)}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
29
frontend/packages/swarm-ui/src/agent/terminal/agent-todos.ts
Normal file
29
frontend/packages/swarm-ui/src/agent/terminal/agent-todos.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// An agent's open todos as `GET /api/agents/{name}/todos` serves them: what the
|
||||
// agent's harness last published, newest first.
|
||||
|
||||
/** One row of `GET /api/agents/{name}/todos`. */
|
||||
export interface AgentTodo {
|
||||
/** The harness's own id for it. */
|
||||
id: number;
|
||||
/** What produced it: `matrix`, `forge`, `bash`, … */
|
||||
subsystem: string;
|
||||
/** What it says, as one line. */
|
||||
summary: string;
|
||||
/** Where it came from, when the producer says. */
|
||||
source?: string;
|
||||
/** Unix seconds when it was last updated. */
|
||||
updated_unix: number;
|
||||
}
|
||||
|
||||
/** Newest first; ties by id, the higher one first. */
|
||||
export function sortTodos(todos: AgentTodo[]): AgentTodo[] {
|
||||
return [...todos].sort(
|
||||
(a, b) => b.updated_unix - a.updated_unix || b.id - a.id,
|
||||
);
|
||||
}
|
||||
|
||||
/** The count in words: `no open todos`, `1 open todo`, `3 open todos`. */
|
||||
export function todosLabel(todos: AgentTodo[]): string {
|
||||
if (todos.length === 0) return "no open todos";
|
||||
return todos.length === 1 ? "1 open todo" : `${todos.length} open todos`;
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
// An agent's open todos, refetched on a timer: `GET /api/agents/{name}/todos`.
|
||||
// `null` until the first answer; a failed fetch sets `error` and keeps the last
|
||||
// list. Mount per agent name: the poll belongs to one agent.
|
||||
import type { ProblemDetails } from "@hive/shared/api-error.js";
|
||||
import { fetchJson, usePolled } from "../../ui/polled/usePolled.js";
|
||||
import { sortTodos, type AgentTodo } from "./agent-todos.js";
|
||||
|
||||
const REFRESH_MS = 10_000;
|
||||
|
||||
export function useAgentTodos(agentName: string): {
|
||||
todos: AgentTodo[] | null;
|
||||
error: ProblemDetails | null;
|
||||
} {
|
||||
const { data, error } = usePolled(REFRESH_MS, async (signal) =>
|
||||
sortTodos(
|
||||
await fetchJson<AgentTodo[]>(
|
||||
`/api/agents/${encodeURIComponent(agentName)}/todos`,
|
||||
signal,
|
||||
),
|
||||
),
|
||||
);
|
||||
return { todos: data, error };
|
||||
}
|
||||
36
frontend/packages/swarm-ui/test/agent-todos.test.mjs
Normal file
36
frontend/packages/swarm-ui/test/agent-todos.test.mjs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
// An agent's open todos: their order and the count in words. Run by `npm test`.
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { sortTodos, todosLabel } from "../src/agent/terminal/agent-todos.ts";
|
||||
|
||||
const todo = (id, updated_unix) => ({
|
||||
id,
|
||||
subsystem: "forge",
|
||||
summary: `todo ${id}`,
|
||||
updated_unix,
|
||||
});
|
||||
|
||||
test("the newest todo comes first and a tie goes to the higher id", () => {
|
||||
const ids = sortTodos([
|
||||
todo(1, 100),
|
||||
todo(2, 300),
|
||||
todo(3, 300),
|
||||
todo(4, 200),
|
||||
]).map((t) => t.id);
|
||||
assert.deepEqual(ids, [3, 2, 4, 1]);
|
||||
});
|
||||
|
||||
test("sorting leaves the list it was given alone", () => {
|
||||
const list = [todo(1, 100), todo(2, 200)];
|
||||
sortTodos(list);
|
||||
assert.deepEqual(
|
||||
list.map((t) => t.id),
|
||||
[1, 2],
|
||||
);
|
||||
});
|
||||
|
||||
test("the count reads in words", () => {
|
||||
assert.equal(todosLabel([]), "no open todos");
|
||||
assert.equal(todosLabel([todo(1, 1)]), "1 open todo");
|
||||
assert.equal(todosLabel([todo(1, 1), todo(2, 2)]), "2 open todos");
|
||||
});
|
||||
|
|
@ -8,17 +8,18 @@ import {
|
|||
startsOpen,
|
||||
} from "../src/agent/detail/sections.ts";
|
||||
|
||||
test("a card holds status, terminal, subagents, background tasks, then settings", () => {
|
||||
test("a card holds status, terminal, open todos, subagents, background tasks, then settings", () => {
|
||||
assert.deepEqual(sectionsFor(true), [
|
||||
"status",
|
||||
"terminal",
|
||||
"todos",
|
||||
"subagents",
|
||||
"tasks",
|
||||
"settings",
|
||||
]);
|
||||
});
|
||||
|
||||
test("a page beside a terminal leaves the terminal, subagents and tasks out", () => {
|
||||
test("a page beside a terminal leaves the terminal, todos, subagents and tasks out", () => {
|
||||
assert.deepEqual(sectionsFor(false), ["status", "settings"]);
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue