Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

agentSpawnSummary.ts

62 lines2.2 KB

1import {
2 isActiveSubagentStatus,
3 isTerminalSubagentStatus,
4 type RuntimeSubagent,
5} from "@t3tools/client-runtime/state/subagentRuntime";
6​
7export function deriveAgentSpawnSummary({
8 agents,
9 agentCount,
10 coordinatorStatus,
11}: {
12 agents: ReadonlyArray<Pick<RuntimeSubagent, "kind" | "status">>;
13 agentCount: number;
14 coordinatorStatus?: RuntimeSubagent["status"] | undefined;
15}) {
16 const working = agents.filter((agent) => isActiveSubagentStatus(agent.status)).length;
17 const failed = agents.filter((agent) => agent.status === "failed").length;
18 const idle = agents.filter((agent) => agent.status === "idle").length;
19 const stopped = agents.filter(
20 (agent) => agent.status === "cancelled" || agent.status === "interrupted",
21 ).length;
22 const batches = agents.filter((agent) => agent.kind === "subagent_batch").length;
23 const individuals = agentCount - batches;
24 const live =
25 coordinatorStatus !== undefined ? !isTerminalSubagentStatus(coordinatorStatus) : working > 0;
26 const subjects = [
27 individuals > 0 ? `${individuals} subagent${individuals === 1 ? "" : "s"}` : null,
28 batches > 0
29 ? `${batches} ${individuals > 0 ? "" : "subagent "}batch${batches === 1 ? "" : "es"}`
30 : null,
31 ]
32 .filter(Boolean)
33 .join(" and ");
34 const lead = `${batches > 0 ? "Launched" : live ? "Kicked off" : "Ran"} ${subjects || "subagents"}`;
35​
36 const status = live
37 ? working > 0
38 ? `${working} working`
39 : "working"
40 : coordinatorStatus === "failed"
41 ? "Workflow failed"
42 : coordinatorStatus === "cancelled" || coordinatorStatus === "interrupted"
43 ? "Workflow stopped"
44 : failed > 0
45 ? `${failed} failed`
46 : stopped > 0
47 ? `${stopped} stopped`
48 : idle > 0
49 ? `${idle} idle`
50 : coordinatorStatus !== "completed" &&
51 (agents.length === 0 || agents.length < agentCount)
52 ? "Status unavailable"
53 : "✓ completed";
54 const tone = live
55 ? "working"
56 : failed > 0 || coordinatorStatus === "failed"
57 ? "failed"
58 : status === "✓ completed"
59 ? "completed"
60 : "inactive";
61 return { live, lead, status, tone };