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
29export function deriveThreadTurnSubagents(
30 projection: Pick<Projection, "runs" | "subagents">,
31): ThreadTurnSubagents | null {
32 const rolledBackRunIds = new Set(
33 projection.runs.filter((run) => run.status === "rolled_back").map((run) => run.id),
34 );
35 const eligible = projection.subagents.filter(
36 (subagent) => subagent.runId === null || !rolledBackRunIds.has(subagent.runId),
37 );
38 const [first] = eligible;
39 if (first === undefined) return null;
40 const activeRun = resolveActiveThreadRun(projection);
41 const latestUpdated = eligible.reduce(
42 (latest, subagent) =>
43 DateTime.toEpochMillis(subagent.updatedAt) > DateTime.toEpochMillis(latest.updatedAt)
44 ? subagent
45 : latest,
46 first,
47 );
48 const runId = activeRun?.id ?? latestUpdated.runId;
49 const subagents = copySorted(
50 eligible.filter(
51 (subagent) =>
52 subagent.runId === runId ||
53 isActiveSubagentStatus(subagent.status) ||
54 subagent.workflow !== undefined,
55 ),
56 (left, right) => orderKey(left) - orderKey(right) || left.id.localeCompare(right.id),
57 );
58 if (subagents.length === 0) return null;
59
60 let liveCount = 0;
61 let settledCount = 0;
62 for (const subagent of subagents) {
63 if (isActiveSubagentStatus(subagent.status)) liveCount += 1;
64 else if (isTerminalSubagentStatus(subagent.status)) settledCount += 1;
65 }
66 return { runId, turnActive: activeRun !== null, subagents, liveCount, settledCount };
67}
68
69export function resolveSubagentPillSegment(
70 turn: ThreadTurnSubagents | null,
71): SubagentPillSegment | null {
72 if (turn === null) return null;
73 if (
74 !turn.turnActive &&
75 turn.liveCount === 0 &&
76 !turn.subagents.some((subagent) => subagent.workflow !== undefined)
77 ) {
78 return null;
79 }
80 const total = turn.subagents.length;
81 if (turn.liveCount > 0) {
82 return {
83 label: `${turn.liveCount}/${total}`,
84 accessibilityLabel: `${turn.liveCount} of ${total} agents working`,
85 };
86 }
87 return {
88 label: `${turn.settledCount} done`,
89 accessibilityLabel: `${turn.settledCount} ${turn.settledCount === 1 ? "agent" : "agents"} done`,
90 };
91}