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

subagentRuntime.ts

147 lines4.5 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 } 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 * Projects orchestration-v2 subagent entities into the runtime shape the web
96 * agent rows render.
97 */
98export function projectedSubagentsToRuntime(
99 subagents: ReadonlyArray<{
100 readonly id: string;
101 readonly title: string | null;
102 readonly prompt: string;
103 readonly model: string | null;
104 readonly status: OrchestrationV2Subagent["status"];
105 readonly progress?: string | undefined;
106 readonly result: string | null;
107 readonly startedAt: DateTime.Utc | null;
108 readonly completedAt: DateTime.Utc | null;
109 readonly updatedAt: DateTime.Utc;
110 }>,
111): ReadonlyArray<RuntimeSubagent> {
112 return subagents.map((subagent) => {
113 const updatedAt = DateTime.formatIso(subagent.updatedAt);
114 const startedAt = subagent.startedAt === null ? null : DateTime.formatIso(subagent.startedAt);
115 return {
116 id: subagent.id,
117 kind: "subagent" as const,
118 title:
119 subagent.title ??
120 (subagent.prompt.length > 80 ? `${subagent.prompt.slice(0, 77)}...` : subagent.prompt),
121 role: null,
122 model: subagent.model,
123 effort: null,
124 status: subagent.status,
125 activationCount: 1,
126 usage: null,
127 progress: subagent.progress ?? null,
128 lastToolName: null,
129 result: subagent.result,
130 error: subagent.status === "failed" ? (subagent.result ?? null) : null,
131 outputFile: null,
132 parentAgentId: null,
133 agentIndex: null,
134 phaseIndex: null,
135 phaseTitle: null,
136 attempt: null,
137 workflowName: null,
138 phases: [],
139 runHandles: null,
140 recentActivity: [],
141 firstSeenAt: startedAt ?? updatedAt,
142 startedAt,
143 completedAt: subagent.completedAt === null ? null : DateTime.formatIso(subagent.completedAt),
144 updatedAt,
145 } satisfies RuntimeSubagent;
146 });