Open History to see the last commit
1import * as DateTime from "effect/DateTime";
2import type { OrchestrationV2Subagent, OrchestrationV2ThreadProjection } from "@t3tools/contracts";
3import { copySorted } from "@t3tools/shared/Array";
4
5import { isActiveSubagentStatus, isTerminalSubagentStatus } from "./subagentRuntime.ts";
6import { resolveActiveThreadRun } from "./threadWorkflows.ts";
7
8type Projection = OrchestrationV2ThreadProjection;
9type Subagent = OrchestrationV2Subagent;
10type RunId = Projection["runs"][number]["id"];
11
12export interface ThreadTurnSubagents {
13 readonly runId: RunId | null;
14 readonly turnActive: boolean;
15 readonly subagents: ReadonlyArray<Subagent>;
16 readonly liveCount: number;
17 readonly settledCount: number;
18}
19
20export interface SubagentPillSegment {
21 readonly label: string;
22 readonly accessibilityLabel: string;
23}
24
25function orderKey(subagent: Subagent): number {
26 return DateTime.toEpochMillis(subagent.startedAt ?? subagent.updatedAt);
27}
28
29/**
30 * The agents behind the thread's agents pill and sheet: every agent of the active run (or, once
31 * the thread is idle, of the run the most recently updated agent belongs to), plus any agent
32 * still working from another run. A background agent outlives the turn that launched it, and the
33 * Waiting strip counts it, so the pill has to count it and the sheet has to reach it too. Agents
34 * of rolled-back runs stay out, as they do from the strip.
35 */
36export function deriveThreadTurnSubagents(
37 projection: Pick<Projection, "runs" | "subagents">,
38): ThreadTurnSubagents | null {
39 const rolledBackRunIds = new Set(
40 projection.runs.filter((run) => run.status === "rolled_back").map((run) => run.id),
41 );
42 const eligible = projection.subagents.filter(
43 (subagent) => subagent.runId === null || !rolledBackRunIds.has(subagent.runId),
44 );
45 const [first] = eligible;
46 if (first === undefined) return null;
47 const activeRun = resolveActiveThreadRun(projection);
48 const latestUpdated = eligible.reduce(
49 (latest, subagent) =>
50 DateTime.toEpochMillis(subagent.updatedAt) > DateTime.toEpochMillis(latest.updatedAt)
51 ? subagent
52 : latest,
53 first,
54 );
55 const runId = activeRun?.id ?? latestUpdated.runId;
56 const subagents = copySorted(
57 eligible.filter(
58 (subagent) => subagent.runId === runId || isActiveSubagentStatus(subagent.status),
59 ),
60 (left, right) => orderKey(left) - orderKey(right) || left.id.localeCompare(right.id),
61 );
62 if (subagents.length === 0) return null;
63
64 let liveCount = 0;
65 let settledCount = 0;
66 for (const subagent of subagents) {
67 if (isActiveSubagentStatus(subagent.status)) liveCount += 1;
68 else if (isTerminalSubagentStatus(subagent.status)) settledCount += 1;
69 }
70 return { runId, turnActive: activeRun !== null, subagents, liveCount, settledCount };
71}
72
73export function resolveSubagentPillSegment(
74 turn: ThreadTurnSubagents | null,
75): SubagentPillSegment | null {
76 if (turn === null) return null;
77 if (!turn.turnActive && turn.liveCount === 0) return null;
78 const total = turn.subagents.length;
79 if (turn.liveCount > 0) {
80 return {
81 label: `${turn.liveCount}/${total}`,
82 accessibilityLabel: `${turn.liveCount} of ${total} agents working`,
83 };
84 }
85 return {
86 label: `${total} done`,
87 accessibilityLabel: `${total} ${total === 1 ? "agent" : "agents"} done`,
88 };
89}