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

threadSubagents.ts

99 lines4.0 KB

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 * Workflow rows are excluded: a provider workflow coordinator and its members
11 * carry the `workflow` block (and usually `runId: null`, since a run outlives
12 * the turn that launched it), so the settled-turn fallback below would
13 * otherwise adopt every workflow row as "the turn". Workflows render on their
14 * own card, not in this roster. A plain subagent may also have no orchestration
15 * `runId`; it stays in the roster and is scoped like any other row, so the
16 * `workflow` block (not a null `runId`) is the discriminant.
17 */
18import * as DateTime from "effect/DateTime";
19import type { OrchestrationV2Subagent, OrchestrationV2ThreadProjection } from "@t3tools/contracts";
20import { copySorted } from "@t3tools/shared/Array";
21​
22import { isActiveSubagentStatus, isTerminalSubagentStatus } from "./subagentRuntime.ts";
23import { resolveActiveThreadRun } from "./threadWorkflows.ts";
24​
25type Projection = OrchestrationV2ThreadProjection;
26type Subagent = OrchestrationV2Subagent;
27type RunId = Projection["runs"][number]["id"];
28​
29export interface ThreadTurnSubagents {
30 /** The run the roster belongs to: the active one, else the most recent one that spawned agents. */
31 readonly runId: RunId | null;
32 readonly turnActive: boolean;
33 /** Spawn order: startedAt, falling back to updatedAt for agents that never started. */
34 readonly subagents: ReadonlyArray<Subagent>;
35 readonly liveCount: number;
36 readonly settledCount: number;
38​
39export interface SubagentPillSegment {
40 readonly label: string;
41 readonly accessibilityLabel: string;
43​
44function orderKey(subagent: Subagent): number {
45 return DateTime.toEpochMillis(subagent.startedAt ?? subagent.updatedAt);
47​
48/** null when the thread has never spawned a turn subagent. */
49export function deriveThreadTurnSubagents(
50 projection: Pick<Projection, "runs" | "subagents">,
51): ThreadTurnSubagents | null {
52 const roster = projection.subagents.filter((subagent) => subagent.workflow === undefined);
53 if (roster.length === 0) return null;
54 const activeRun = resolveActiveThreadRun(projection);
55 // With no live run the newest roster is still worth showing: a turn that
56 // just finished leaves results the user has not read yet.
57 const latestUpdated = roster.reduce((latest, subagent) =>
58 DateTime.toEpochMillis(subagent.updatedAt) > DateTime.toEpochMillis(latest.updatedAt)
59 ? subagent
60 : latest,
61 );
62 const runId = activeRun?.id ?? latestUpdated.runId;
63 const subagents = copySorted(
64 roster.filter((subagent) => subagent.runId === runId),
65 (left, right) => orderKey(left) - orderKey(right) || left.id.localeCompare(right.id),
66 );
67 if (subagents.length === 0) return null;
68​
69 let liveCount = 0;
70 let settledCount = 0;
71 for (const subagent of subagents) {
72 if (isActiveSubagentStatus(subagent.status)) liveCount += 1;
73 else if (isTerminalSubagentStatus(subagent.status)) settledCount += 1;
74 }
75 return { runId, turnActive: activeRun !== null, subagents, liveCount, settledCount };
77​
78/**
79 * What the pill's agents segment says, or null while it should stay hidden.
80 * The segment is for work in flight: once the turn settles it goes away
81 * rather than leaving a stale count above the composer.
82 */
83export function resolveSubagentPillSegment(
84 turn: ThreadTurnSubagents | null,
85): SubagentPillSegment | null {
86 if (turn === null) return null;
87 if (!turn.turnActive && turn.liveCount === 0) return null;
88 const total = turn.subagents.length;
89 if (turn.liveCount > 0) {
90 return {
91 label: `${turn.liveCount}/${total}`,
92 accessibilityLabel: `${turn.liveCount} of ${total} agents working`,
93 };
94 }
95 return {
96 label: `${total} done`,
97 accessibilityLabel: `${total} ${total === 1 ? "agent" : "agents"} done`,
98 };