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

subagentRuntime.ts

156 lines4.9 KB

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, OrchestrationV2TurnItem } 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;
28​
29export interface SubagentActivityEntry {
30 readonly at: string;
31 readonly summary: string;
33​
34export interface SubagentWorkflowPhase {
35 readonly index: number;
36 readonly title: string;
38​
39export interface SubagentRunHandles {
40 readonly runId?: string;
41 readonly scriptPath?: string;
42 readonly transcriptDir?: string;
43 readonly sessionUrl?: string;
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;
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);
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);
93​
94/**
95 * A subagent card whose child is still working. The card outlives its
96 * launching turn: once that turn settles, the waiting footer counts the child,
97 * so the card stays out of the turn's fold until the child finishes.
98 */
99export function isLiveSubagentTurnItem(item: OrchestrationV2TurnItem): boolean {
100 return item.type === "subagent" && isOrchestrationV2WorkActive(item.status);
102​
103/**
104 * Projects orchestration-v2 subagent entities into the runtime shape the web
105 * agent rows render.
106 */
107export function projectedSubagentsToRuntime(
108 subagents: ReadonlyArray<{
109 readonly id: string;
110 readonly title: string | null;
111 readonly prompt: string;
112 readonly model: string | null;
113 readonly status: OrchestrationV2Subagent["status"];
114 readonly progress?: string | undefined;
115 readonly result: string | null;
116 readonly startedAt: DateTime.Utc | null;
117 readonly completedAt: DateTime.Utc | null;
118 readonly updatedAt: DateTime.Utc;
119 }>,
120): ReadonlyArray<RuntimeSubagent> {
121 return subagents.map((subagent) => {
122 const updatedAt = DateTime.formatIso(subagent.updatedAt);
123 const startedAt = subagent.startedAt === null ? null : DateTime.formatIso(subagent.startedAt);
124 return {
125 id: subagent.id,
126 kind: "subagent" as const,
127 title:
128 subagent.title ??
129 (subagent.prompt.length > 80 ? `${subagent.prompt.slice(0, 77)}...` : subagent.prompt),
130 role: null,
131 model: subagent.model,
132 effort: null,
133 status: subagent.status,
134 activationCount: 1,
135 usage: null,
136 progress: subagent.progress ?? null,
137 lastToolName: null,
138 result: subagent.result,
139 error: subagent.status === "failed" ? (subagent.result ?? null) : null,
140 outputFile: null,
141 parentAgentId: null,
142 agentIndex: null,
143 phaseIndex: null,
144 phaseTitle: null,
145 attempt: null,
146 workflowName: null,
147 phases: [],
148 runHandles: null,
149 recentActivity: [],
150 firstSeenAt: startedAt ?? updatedAt,
151 startedAt,
152 completedAt: subagent.completedAt === null ? null : DateTime.formatIso(subagent.completedAt),
153 updatedAt,
154 } satisfies RuntimeSubagent;
155 });