| 1 | /** |
| 2 | * The subagent roster for one turn, and the compact label the mobile composer |
| 3 | * pill renders for it. |
| 4 | * |
| 5 | * Scoped to a run rather than the whole thread: a thread accumulates every |
| 6 | * subagent it ever spawned, while the pill and its sheet answer "what is this |
| 7 | * turn doing right now". Statuses come from the v2 projection directly, so |
| 8 | * this stays a pure fold over `runs` and `subagents`. |
| 9 | */ |
| 10 | import * as DateTime from "effect/DateTime"; |
| 11 | import type { OrchestrationV2Subagent, OrchestrationV2ThreadProjection } from "@t3tools/contracts"; |
| 12 | import { copySorted } from "@t3tools/shared/Array"; |
| 13 | |
| 14 | import { isActiveSubagentStatus, isTerminalSubagentStatus } from "./subagentRuntime.ts"; |
| 15 | import { resolveActiveThreadRun } from "./threadWorkflows.ts"; |
| 16 | |
| 17 | type Projection = OrchestrationV2ThreadProjection; |
| 18 | type Subagent = OrchestrationV2Subagent; |
| 19 | type RunId = Projection["runs"][number]["id"]; |
| 20 | |
| 21 | export interface ThreadTurnSubagents { |
| 22 | /** The run the roster belongs to: the active one, else the most recent one that spawned agents. */ |
| 23 | readonly runId: RunId | null; |
| 24 | readonly turnActive: boolean; |
| 25 | /** Spawn order: startedAt, falling back to updatedAt for agents that never started. */ |
| 26 | readonly subagents: ReadonlyArray<Subagent>; |
| 27 | readonly liveCount: number; |
| 28 | readonly settledCount: number; |
| 29 | } |
| 30 | |
| 31 | export interface SubagentPillSegment { |
| 32 | readonly label: string; |
| 33 | readonly accessibilityLabel: string; |
| 34 | } |
| 35 | |
| 36 | function orderKey(subagent: Subagent): number { |
| 37 | return DateTime.toEpochMillis(subagent.startedAt ?? subagent.updatedAt); |
| 38 | } |
| 39 | |
| 40 | /** null when the thread has never spawned a subagent. */ |
| 41 | export function deriveThreadTurnSubagents( |
| 42 | projection: Pick<Projection, "runs" | "subagents">, |
| 43 | ): ThreadTurnSubagents | null { |
| 44 | if (projection.subagents.length === 0) return null; |
| 45 | const activeRun = resolveActiveThreadRun(projection); |
| 46 | // With no live run the newest roster is still worth showing: a turn that |
| 47 | // just finished leaves results the user has not read yet. |
| 48 | const latestUpdated = projection.subagents.reduce((latest, subagent) => |
| 49 | DateTime.toEpochMillis(subagent.updatedAt) > DateTime.toEpochMillis(latest.updatedAt) |
| 50 | ? subagent |
| 51 | : latest, |
| 52 | ); |
| 53 | const runId = activeRun?.id ?? latestUpdated.runId; |
| 54 | const subagents = copySorted( |
| 55 | projection.subagents.filter((subagent) => subagent.runId === runId), |
| 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 | |
| 69 | /** |
| 70 | * What the pill's agents segment says, or null while it should stay hidden. |
| 71 | * The segment is for work in flight: once the turn settles it goes away |
| 72 | * rather than leaving a stale count above the composer. |
| 73 | */ |
| 74 | export function resolveSubagentPillSegment( |
| 75 | turn: ThreadTurnSubagents | null, |
| 76 | ): SubagentPillSegment | null { |
| 77 | if (turn === null) return null; |
| 78 | if (!turn.turnActive && turn.liveCount === 0) return null; |
| 79 | const total = turn.subagents.length; |
| 80 | if (turn.liveCount > 0) { |
| 81 | return { |
| 82 | label: `${turn.liveCount}/${total}`, |
| 83 | accessibilityLabel: `${turn.liveCount} of ${total} agents working`, |
| 84 | }; |
| 85 | } |
| 86 | return { |
| 87 | label: `${total} done`, |
| 88 | accessibilityLabel: `${total} ${total === 1 ? "agent" : "agents"} done`, |
| 89 | }; |
| 90 | } |