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

threadSubagents.ts

87 lines3.1 KB

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;
19​
20export interface SubagentPillSegment {
21 readonly label: string;
22 readonly accessibilityLabel: string;
24​
25function orderKey(subagent: Subagent): number {
26 return DateTime.toEpochMillis(subagent.startedAt ?? subagent.updatedAt);
28​
29/**
30 * The pill and sheet include the active run's agents, or the most recently updated agent's run when idle.
31 * Include agents still working from other runs so background work stays counted and reachable.
32 * Exclude rolled-back runs, matching the Waiting strip.
33 */
34export function deriveThreadTurnSubagents(
35 projection: Pick<Projection, "runs" | "subagents">,
36): ThreadTurnSubagents | null {
37 const rolledBackRunIds = new Set(
38 projection.runs.filter((run) => run.status === "rolled_back").map((run) => run.id),
39 );
40 const eligible = projection.subagents.filter(
41 (subagent) => subagent.runId === null || !rolledBackRunIds.has(subagent.runId),
42 );
43 const [first] = eligible;
44 if (first === undefined) return null;
45 const activeRun = resolveActiveThreadRun(projection);
46 const latestUpdated = eligible.reduce(
47 (latest, subagent) =>
48 DateTime.toEpochMillis(subagent.updatedAt) > DateTime.toEpochMillis(latest.updatedAt)
49 ? subagent
50 : latest,
51 first,
52 );
53 const runId = activeRun?.id ?? latestUpdated.runId;
54 const subagents = copySorted(
55 eligible.filter(
56 (subagent) => subagent.runId === runId || isActiveSubagentStatus(subagent.status),
57 ),
58 (left, right) => orderKey(left) - orderKey(right) || left.id.localeCompare(right.id),
59 );
60 if (subagents.length === 0) return null;
61​
62 let liveCount = 0;
63 let settledCount = 0;
64 for (const subagent of subagents) {
65 if (isActiveSubagentStatus(subagent.status)) liveCount += 1;
66 else if (isTerminalSubagentStatus(subagent.status)) settledCount += 1;
67 }
68 return { runId, turnActive: activeRun !== null, subagents, liveCount, settledCount };
70​
71export function resolveSubagentPillSegment(
72 turn: ThreadTurnSubagents | null,
73): SubagentPillSegment | null {
74 if (turn === null) return null;
75 if (!turn.turnActive && turn.liveCount === 0) return null;
76 const total = turn.subagents.length;
77 if (turn.liveCount > 0) {
78 return {
79 label: `${turn.liveCount}/${total}`,
80 accessibilityLabel: `${turn.liveCount} of ${total} agents working`,
81 };
82 }
83 return {
84 label: `${total} done`,
85 accessibilityLabel: `${total} ${total === 1 ? "agent" : "agents"} done`,
86 };