swarm ui: select several agents on the agents page and broadcast to them
Select mode picks agents from the list: the select button in the filter card, a press on an agent's icon, or a long press on a card starts it. In it a card toggles a tick instead of opening its details, and the card's own controls stop answering. A bar at the foot of the list has select all shown, clear, broadcast and done; Escape leaves the mode. Broadcast opens a dialog with the picked agents and a message box, sending through the existing broadcast endpoint; the composer's send and result parts are now shared with the swarm page's broadcast card. A full send shows a toast and ends select mode; a partial one keeps the result with a retry for the agents that failed.
This commit is contained in:
parent
7bdaea7954
commit
56ed2e07da
3 changed files with 793 additions and 88 deletions
|
|
@ -14,8 +14,6 @@
|
|||
// controller takes the sender from the gateway, as for the single-agent box).
|
||||
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 { AgentIcon } from "../../agent/AgentIcon.js";
|
||||
import { AgentPicker } from "../../agent/picker/AgentPicker.js";
|
||||
import { Button } from "../../ui/button/Button.js";
|
||||
import {
|
||||
|
|
@ -31,7 +29,6 @@ import {
|
|||
failedAgents,
|
||||
filterShowing,
|
||||
isRunning,
|
||||
parseResults,
|
||||
selectedVisible,
|
||||
MAX_BROADCAST,
|
||||
sendLabel,
|
||||
|
|
@ -42,17 +39,13 @@ import {
|
|||
type BroadcastAgent,
|
||||
type BroadcastResult,
|
||||
} from "./broadcast-types.js";
|
||||
import { postBroadcast, type SendState } from "./broadcast-send.js";
|
||||
import { SendResults } from "./SendResults.js";
|
||||
import "./BroadcastComposer.css";
|
||||
|
||||
const DEFAULT_INTERVAL_MS: RefreshIntervalMs = 30_000;
|
||||
const FILTER_KEY = "swarm-ui:broadcast:filter";
|
||||
|
||||
type SendState =
|
||||
| { status: "idle" }
|
||||
| { status: "sending" }
|
||||
| { status: "done"; results: BroadcastResult[] }
|
||||
| { status: "error"; problem: ProblemDetails };
|
||||
|
||||
function storedFilter(): AgentFilter {
|
||||
try {
|
||||
return localStorage.getItem(FILTER_KEY) === "all" ? "all" : "running";
|
||||
|
|
@ -122,30 +115,12 @@ export function BroadcastComposer() {
|
|||
e.preventDefault();
|
||||
if (!canSend) return;
|
||||
setSend({ status: "sending" });
|
||||
try {
|
||||
const res = await fetch("/api/agents/broadcast", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ agents: targets, body }),
|
||||
});
|
||||
if (!res.ok) {
|
||||
setSend({ status: "error", problem: await readApiError(res) });
|
||||
return;
|
||||
}
|
||||
const results = parseResults(await res.json());
|
||||
if (results === null) {
|
||||
setSend({
|
||||
status: "error",
|
||||
problem: { detail: "the controller's answer was not understood" },
|
||||
});
|
||||
return;
|
||||
}
|
||||
setSend({ status: "done", results });
|
||||
// Everyone got it: the text has served its purpose. A partial send keeps
|
||||
// it so the retry to the rest sends the same words.
|
||||
if (failedAgents(results).length === 0) setBody("");
|
||||
} catch (err) {
|
||||
setSend({ status: "error", problem: { detail: String(err) } });
|
||||
const answer = await postBroadcast(targets, body);
|
||||
setSend(answer);
|
||||
// Everyone got it: the text has served its purpose. A partial send keeps
|
||||
// it so the retry to the rest sends the same words.
|
||||
if (answer.status === "done" && failedAgents(answer.results).length === 0) {
|
||||
setBody("");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -261,46 +236,3 @@ export function BroadcastComposer() {
|
|||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function SendResults({
|
||||
results,
|
||||
onRetry,
|
||||
}: {
|
||||
results: BroadcastResult[];
|
||||
onRetry: (results: BroadcastResult[]) => void;
|
||||
}) {
|
||||
const failed = failedAgents(results);
|
||||
const sent = results.length - failed.length;
|
||||
return (
|
||||
<section class="ui-broadcast-results" aria-label="send result">
|
||||
<p class="ui-broadcast-summary">
|
||||
sent to {sent} of {results.length}
|
||||
{failed.length > 0 && (
|
||||
<button
|
||||
type="button"
|
||||
class="ui-broadcast-link"
|
||||
onClick={() => onRetry(results)}
|
||||
>
|
||||
select the {failed.length} that failed
|
||||
</button>
|
||||
)}
|
||||
</p>
|
||||
<ul class="ui-broadcast-outcomes">
|
||||
{results.map((r) => (
|
||||
<li
|
||||
key={r.agent}
|
||||
class={r.sent ? "ui-broadcast-ok" : "ui-broadcast-fail"}
|
||||
>
|
||||
<AgentIcon name={r.agent} small />
|
||||
<span class="ui-broadcast-outcome-name">{r.agent}</span>
|
||||
<span>
|
||||
{r.sent
|
||||
? "✓ sent"
|
||||
: `✗ not sent${r.reason ? `: ${r.reason}` : ""}`}
|
||||
</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
// <SendResults> — what a broadcast came back with: how many agents got it, one
|
||||
// line per agent, and a way to select just the ones that failed for a retry.
|
||||
import { AgentIcon } from "../../agent/AgentIcon.js";
|
||||
import { failedAgents, type BroadcastResult } from "./broadcast-types.js";
|
||||
import "./BroadcastComposer.css";
|
||||
|
||||
export function SendResults({
|
||||
results,
|
||||
onRetry,
|
||||
}: {
|
||||
results: BroadcastResult[];
|
||||
onRetry: (results: BroadcastResult[]) => void;
|
||||
}) {
|
||||
const failed = failedAgents(results);
|
||||
const sent = results.length - failed.length;
|
||||
return (
|
||||
<section class="ui-broadcast-results" aria-label="send result">
|
||||
<p class="ui-broadcast-summary">
|
||||
sent to {sent} of {results.length}
|
||||
{failed.length > 0 && (
|
||||
<button
|
||||
type="button"
|
||||
class="ui-broadcast-link"
|
||||
onClick={() => onRetry(results)}
|
||||
>
|
||||
select the {failed.length} that failed
|
||||
</button>
|
||||
)}
|
||||
</p>
|
||||
<ul class="ui-broadcast-outcomes">
|
||||
{results.map((r) => (
|
||||
<li
|
||||
key={r.agent}
|
||||
class={r.sent ? "ui-broadcast-ok" : "ui-broadcast-fail"}
|
||||
>
|
||||
<AgentIcon name={r.agent} small />
|
||||
<span class="ui-broadcast-outcome-name">{r.agent}</span>
|
||||
<span>
|
||||
{r.sent
|
||||
? "✓ sent"
|
||||
: `✗ not sent${r.reason ? `: ${r.reason}` : ""}`}
|
||||
</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
// Sending one message to several agents: `POST /api/agents/broadcast` answers
|
||||
// one outcome per agent, so one that is down does not stop the rest. The
|
||||
// composer on the swarm page and the agents page's broadcast dialog both send
|
||||
// through this, and show the answer with `SendResults`.
|
||||
import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js";
|
||||
import { parseResults, type BroadcastResult } from "./broadcast-types.js";
|
||||
|
||||
export type SendState =
|
||||
| { status: "idle" }
|
||||
| { status: "sending" }
|
||||
| { status: "done"; results: BroadcastResult[] }
|
||||
| { status: "error"; problem: ProblemDetails };
|
||||
|
||||
/** Sends `body` to `agents`, as typed; settles on `done` or `error`. */
|
||||
export async function postBroadcast(
|
||||
agents: string[],
|
||||
body: string,
|
||||
): Promise<SendState> {
|
||||
try {
|
||||
const res = await fetch("/api/agents/broadcast", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ agents, body }),
|
||||
});
|
||||
if (!res.ok) return { status: "error", problem: await readApiError(res) };
|
||||
const results = parseResults(await res.json());
|
||||
if (results === null) {
|
||||
return {
|
||||
status: "error",
|
||||
problem: { detail: "the controller's answer was not understood" },
|
||||
};
|
||||
}
|
||||
return { status: "done", results };
|
||||
} catch (err) {
|
||||
return { status: "error", problem: { detail: String(err) } };
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue