The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as DateTime from "effect/DateTime"; |
| 2 | import type { OrchestrationV2Subagent, OrchestrationV2ThreadProjection } from "@t3tools/contracts"; |
| 3 | import { copySorted } from "@t3tools/shared/Array"; |
| 4 | |
| 5 | import { isActiveSubagentStatus, isTerminalSubagentStatus } from "./subagentRuntime.ts"; |
| 6 | import { resolveActiveThreadRun } from "./threadWorkflows.ts"; |
| 7 | import { mFallback, msg } from "gt-i18n"; |
| 8 | import type { MFunctionType } from "gt-i18n/types"; |
| 9 | |
| 10 | type Projection = OrchestrationV2ThreadProjection; |
| 11 | type Subagent = OrchestrationV2Subagent; |
| 12 | type RunId = Projection["runs"][number]["id"]; |
| 13 | |
| 14 | export interface ThreadTurnSubagents { |
| 15 | readonly runId: RunId | null; |
| 16 | readonly turnActive: boolean; |
| 17 | /** No later turn has run since these agents' turn. */ |
| 18 | readonly latestTurn: boolean; |
| 19 | readonly subagents: ReadonlyArray<Subagent>; |
| 20 | readonly liveCount: number; |
| 21 | readonly settledCount: number; |
| 22 | } |
| 23 | |
| 24 | export interface SubagentPillSegment { |
| 25 | readonly label: string; |
| 26 | readonly accessibilityLabel: string; |
| 27 | } |
| 28 | |
| 29 | function orderKey(subagent: Subagent): number { |
| 30 | return DateTime.toEpochMillis(subagent.startedAt ?? subagent.updatedAt); |
| 31 | } |
| 32 | |
| 33 | export function deriveThreadTurnSubagents( |
| 34 | projection: Pick<Projection, "runs" | "subagents">, |
| 35 | ): ThreadTurnSubagents | null { |
| 36 | const rolledBackRunIds = new Set( |
| 37 | projection.runs.filter((run) => run.status === "rolled_back").map((run) => run.id), |
| 38 | ); |
| 39 | const eligible = projection.subagents.filter( |
| 40 | (subagent) => subagent.runId === null || !rolledBackRunIds.has(subagent.runId), |
| 41 | ); |
| 42 | const [first] = eligible; |
| 43 | if (first === undefined) return null; |
| 44 | const activeRun = resolveActiveThreadRun(projection); |
| 45 | const latestUpdated = eligible.reduce( |
| 46 | (latest, subagent) => |
| 47 | DateTime.toEpochMillis(subagent.updatedAt) > DateTime.toEpochMillis(latest.updatedAt) |
| 48 | ? subagent |
| 49 | : latest, |
| 50 | first, |
| 51 | ); |
| 52 | // Ordinal, not array position: a reordered queue can leave the projection's runs out of order. |
| 53 | // Queued and never-started runs have not replaced anything yet. |
| 54 | const latestStartedRun = projection.runs.reduce<Projection["runs"][number] | null>( |
| 55 | (latest, run) => |
| 56 | run.startedAt !== null && |
| 57 | !rolledBackRunIds.has(run.id) && |
| 58 | (latest === null || run.ordinal > latest.ordinal) |
| 59 | ? run |
| 60 | : latest, |
| 61 | null, |
| 62 | ); |
| 63 | const latestStartedRunHasAgents = |
| 64 | latestStartedRun !== null && |
| 65 | eligible.some((subagent) => subagent.runId === latestStartedRun.id); |
| 66 | const runId = |
| 67 | activeRun?.id ?? (latestStartedRunHasAgents ? latestStartedRun.id : latestUpdated.runId); |
| 68 | const subagents = copySorted( |
| 69 | eligible.filter( |
| 70 | (subagent) => |
| 71 | subagent.runId === runId || |
| 72 | isActiveSubagentStatus(subagent.status) || |
| 73 | subagent.workflow !== undefined, |
| 74 | ), |
| 75 | (left, right) => orderKey(left) - orderKey(right) || left.id.localeCompare(right.id), |
| 76 | ); |
| 77 | if (subagents.length === 0) return null; |
| 78 | |
| 79 | let liveCount = 0; |
| 80 | let settledCount = 0; |
| 81 | for (const subagent of subagents) { |
| 82 | if (isActiveSubagentStatus(subagent.status)) liveCount += 1; |
| 83 | else if (isTerminalSubagentStatus(subagent.status)) settledCount += 1; |
| 84 | } |
| 85 | return { |
| 86 | runId, |
| 87 | turnActive: activeRun !== null, |
| 88 | latestTurn: runId === null || latestStartedRun === null || runId === latestStartedRun.id, |
| 89 | subagents, |
| 90 | liveCount, |
| 91 | settledCount, |
| 92 | }; |
| 93 | } |
| 94 | |
| 95 | export function resolveSubagentPillSegment( |
| 96 | turn: ThreadTurnSubagents | null, |
| 97 | resolve: MFunctionType = mFallback, |
| 98 | ): SubagentPillSegment | null { |
| 99 | if (turn === null) return null; |
| 100 | // Finished agents stay reachable until a later turn replaces them; workflows always stay reachable. |
| 101 | if ( |
| 102 | !turn.turnActive && |
| 103 | !turn.latestTurn && |
| 104 | turn.liveCount === 0 && |
| 105 | !turn.subagents.some((subagent) => subagent.workflow !== undefined) |
| 106 | ) { |
| 107 | return null; |
| 108 | } |
| 109 | const total = turn.subagents.length; |
| 110 | if (turn.liveCount > 0) { |
| 111 | return { |
| 112 | label: `${turn.liveCount}/${total}`, |
| 113 | accessibilityLabel: resolve( |
| 114 | msg("{live} of {total} agents working", { live: turn.liveCount, total }), |
| 115 | ), |
| 116 | }; |
| 117 | } |
| 118 | return { |
| 119 | label: resolve(msg("{count} done", { count: turn.settledCount })), |
| 120 | accessibilityLabel: resolve( |
| 121 | msg("{count, plural, one {{count} agent done} other {{count} agents done}}", { |
| 122 | count: turn.settledCount, |
| 123 | }), |
| 124 | ), |
| 125 | }; |
| 126 | } |