Open History to see the last commit
1/**
2 * Subagent status helpers shared by web and mobile, and the runtime shape the
3 * web agent rows render.
4 */
5import * as DateTime from "effect/DateTime";
6import type { OrchestrationV2Subagent } from "@t3tools/contracts";
7import { isOrchestrationV2WorkActive } from "@t3tools/contracts";
8
9export type RuntimeSubagentStatus =
10 | "pending"
11 | "running"
12 | "waiting"
13 | "idle"
14 | "completed"
15 | "failed"
16 | "cancelled"
17 | "interrupted";
18
19export interface SubagentUsage {
20 readonly totalTokens: number;
21 readonly inputTokens?: number;
22 readonly cachedInputTokens?: number;
23 readonly outputTokens?: number;
24 readonly reasoningOutputTokens?: number;
25 readonly toolUses?: number;
26 readonly durationMs?: number;
27}
28
29export interface SubagentActivityEntry {
30 readonly at: string;
31 readonly summary: string;
32}
33
34export interface SubagentWorkflowPhase {
35 readonly index: number;
36 readonly title: string;
37}
38
39export interface SubagentRunHandles {
40 readonly runId?: string;
41 readonly scriptPath?: string;
42 readonly transcriptDir?: string;
43 readonly sessionUrl?: string;
44}
45
46export interface RuntimeSubagent {
47 readonly id: string;
48 readonly kind: "subagent" | "subagent_batch" | "workflow" | "workflow_agent";
49 readonly title: string;
50 readonly role: string | null;
51 readonly model: string | null;
52 readonly effort: string | null;
53 readonly status: RuntimeSubagentStatus;
54 readonly activationCount: number;
55 readonly usage: SubagentUsage | null;
56 readonly progress: string | null;
57 readonly lastToolName: string | null;
58 readonly result: string | null;
59 readonly error: string | null;
60 readonly outputFile: string | null;
61 readonly parentAgentId: string | null;
62 readonly agentIndex: number | null;
63 readonly phaseIndex: number | null;
64 readonly phaseTitle: string | null;
65 readonly attempt: number | null;
66 readonly workflowName: string | null;
67 readonly phases: ReadonlyArray<SubagentWorkflowPhase>;
68 readonly runHandles: SubagentRunHandles | null;
69 readonly recentActivity: ReadonlyArray<SubagentActivityEntry>;
70 /** First retained observation, used as the roster's stable display order. */
71 readonly firstSeenAt: string;
72 readonly startedAt: string | null;
73 readonly completedAt: string | null;
74 readonly updatedAt: string;
75}
76
77const TERMINAL_STATUSES: ReadonlySet<RuntimeSubagentStatus> = new Set([
78 "completed",
79 "failed",
80 "cancelled",
81 "interrupted",
82]);
83
84export function isTerminalSubagentStatus(status: RuntimeSubagentStatus): boolean {
85 return TERMINAL_STATUSES.has(status);
86}
87
88/** Active = the user may still need to care while it runs. Idle is settled-ish
89 * but resumable; waiting counts as active because it needs the user. */
90export function isActiveSubagentStatus(status: RuntimeSubagentStatus): boolean {
91 return isOrchestrationV2WorkActive(status);
92}
93
94/**
95 * Projects orchestration-v2 subagent entities into the runtime shape the web
96 * and mobile agent rows render. A subagent carrying the provider-workflow
97 * block keeps its workflow identity: the coordinator becomes `kind: "workflow"`,
98 * each member `kind: "workflow_agent"` linked back through `parentNodeId`, and
99 * the run's engine id lands in `runHandles.runId` for the card's run suffix.
100 * Everything else projects exactly as before.
101 */
102export function projectedSubagentsToRuntime(
103 subagents: ReadonlyArray<{
104 readonly id: string;
105 readonly title: string | null;
106 readonly prompt: string;
107 readonly model: string | null;
108 readonly status: OrchestrationV2Subagent["status"];
109 readonly progress?: string | undefined;
110 readonly result: string | null;
111 readonly parentNodeId?: string | undefined;
112 readonly workflow?: OrchestrationV2Subagent["workflow"];
113 readonly startedAt: DateTime.Utc | null;
114 readonly completedAt: DateTime.Utc | null;
115 readonly updatedAt: DateTime.Utc;
116 }>,
117): ReadonlyArray<RuntimeSubagent> {
118 return subagents.map((subagent) => {
119 const updatedAt = DateTime.formatIso(subagent.updatedAt);
120 const startedAt = subagent.startedAt === null ? null : DateTime.formatIso(subagent.startedAt);
121 const workflow = subagent.workflow;
122 return {
123 id: subagent.id,
124 kind: workflow?.kind ?? "subagent",
125 title:
126 subagent.title ??
127 (subagent.prompt.length > 80 ? `${subagent.prompt.slice(0, 77)}...` : subagent.prompt),
128 role: null,
129 model: subagent.model,
130 effort: null,
131 status: subagent.status,
132 activationCount: 1,
133 usage: null,
134 progress: subagent.progress ?? null,
135 lastToolName: null,
136 result: subagent.result,
137 error: subagent.status === "failed" ? (subagent.result ?? null) : null,
138 outputFile: null,
139 // A workflow member hangs off its coordinator node; a coordinator and a
140 // plain subagent have no agent parent.
141 parentAgentId: workflow?.kind === "workflow_agent" ? (subagent.parentNodeId ?? null) : null,
142 agentIndex: workflow?.agentIndex ?? null,
143 phaseIndex: workflow?.phaseIndex ?? null,
144 phaseTitle: workflow?.phaseTitle ?? null,
145 attempt: null,
146 workflowName: workflow?.workflowName ?? null,
147 phases: workflow?.phases ?? [],
148 runHandles: workflow?.runId == null ? null : { runId: workflow.runId },
149 recentActivity: [],
150 firstSeenAt: startedAt ?? updatedAt,
151 startedAt,
152 completedAt: subagent.completedAt === null ? null : DateTime.formatIso(subagent.completedAt),
153 updatedAt,
154 } satisfies RuntimeSubagent;
155 });
156}