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

ThreadWorkflowGroup.tsx

323 lines11.5 KB

1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import {
3 isActiveSubagentStatus,
4 type RuntimeSubagent,
5 type SubagentWorkflowGroup,
6} from "@t3tools/client-runtime/state/subagentRuntime";
7import {
8 ThreadId,
9 type EnvironmentId,
10 type OrchestrationV2Subagent,
11 type ServerProvider,
12} from "@t3tools/contracts";
13import { deriveSubagentElapsedMs } from "@t3tools/shared/orchestrationTiming";
14import { formatDuration } from "@t3tools/shared/orchestrationTimingCopy";
15import { T, useGT, useMessages, select } from "gt-react-native";
16import { useState, type ReactNode } from "react";
17import { ScrollView, View } from "react-native";
18​
19import { AppText as Text } from "../../components/ui/AppText";
20import { SymbolView, type AppSymbolName } from "../../components/ui/AppSymbol";
21import { Chip } from "../../components/ui/Chip";
22import { ListRowLayout } from "../../components/ui/ListRowLayout";
23import { ListSection } from "../../components/ui/ListSection";
24import { RowPressable } from "../../components/ui/RowPressable";
25import { cn } from "../../lib/cn";
26import { useEnvironmentServerConfig, useThreadShell } from "../../state/entities";
27import { SUBAGENT_TONE_TEXT_CLASS, SubagentStatusDot } from "./SubagentStatusDot";
28import { SubagentStopButton } from "./SubagentStopButton";
29import {
30 nextWorkflowSelection,
31 resolveSubagentRowPresentation,
32 workflowMemberDetail,
33 workflowMemberMetadata,
34 workflowRail,
35 type WorkflowRailTone,
36} from "./threadAgentsPresentation";
37import { useVisibleSecondClock } from "./use-visible-second-clock";
38​
39const RAIL_ICON = {
40 working: "circle.inset.filled",
41 completed: "checkmark.circle.fill",
42 failed: "xmark.circle.fill",
43 stopped: "circle",
44 queued: "circle.dashed",
45} as const satisfies Record<WorkflowRailTone, AppSymbolName>;
46​
47export function ThreadWorkflowGroup(props: {
48 readonly environmentId: EnvironmentId;
49 readonly subagent: OrchestrationV2Subagent;
50 readonly group: SubagentWorkflowGroup;
51 readonly onOpen: (threadId: ThreadId) => void;
52}) {
53 const { group } = props;
54 const gt = useGT();
55 const m = useMessages();
56 const provider = useEnvironmentServerConfig(props.environmentId)?.providers.find(
57 (candidate) => candidate.instanceId === props.subagent.providerInstanceId,
58 );
59 const [selected, setSelected] = useState<string | null>(null);
60 const rail = workflowRail(group, m);
61 const open = rail.find((item) => item.key === selected);
62 const label = group.workflow.workflowName ?? group.workflow.title;
63 const statusLabel = resolveSubagentRowPresentation(props.subagent, false, m).statusLabel;
64​
65 return (
66 <View className="my-2">
67 <ListSection>
68 <View className="flex-row items-center">
69 <RowPressable
70 accessibilityRole="button"
71 accessibilityLabel={gt`${{ label }} workflow, ${{ status: statusLabel }}`}
72 accessibilityHint={open ? gt`Collapses the workflow` : gt`Opens the current phase`}
73 accessibilityState={{ expanded: open !== undefined }}
74 disabled={rail.length === 0}
75 onPress={() => setSelected(nextWorkflowSelection(rail, selected))}
76 className="min-w-0 flex-1 active:opacity-70"
77 >
78 <WorkflowHeaderLayout
79 label={label}
80 statusLabel={statusLabel}
81 timing={group.workflow}
82 accessory={
83 rail.length > 0 ? (
84 <SymbolView
85 name={open ? "chevron.up" : "chevron.down"}
86 size={12}
87 tintColorClassName="accent-icon-subtle"
88 />
89 ) : null
90 }
91 />
92 </RowPressable>
93 <SubagentStopButton
94 environmentId={props.environmentId}
95 subagent={props.subagent}
96 className="mr-2"
97 />
98 </View>
99 {rail.length > 0 ? (
100 <ScrollView
101 horizontal
102 showsHorizontalScrollIndicator={false}
103 contentContainerClassName="gap-2 px-4 pb-3"
104 >
105 {rail.map((item) => {
106 const count =
107 item.tone === "working" || item.tone === "failed"
108 ? `${item.completedCount}/${item.members.length}`
109 : null;
110 return (
111 <Chip
112 key={item.key}
113 icon={RAIL_ICON[item.tone]}
114 label={count ? `${count} ${item.title}` : item.title}
115 selected={item.key === selected}
116 accessibilityLabel={
117 count
118 ? gt`${{ title: item.title }} phase, ${select(
119 { tone: item.tone },
120 {
121 working: "working",
122 completed: "completed",
123 failed: "failed",
124 stopped: "stopped",
125 other: "queued",
126 },
127 )}, ${{ count }} completed`
128 : gt`${{ title: item.title }} phase, ${select(
129 { tone: item.tone },
130 {
131 working: "working",
132 completed: "completed",
133 failed: "failed",
134 stopped: "stopped",
135 other: "queued",
136 },
137 )}`
138 }
139 onPress={() => setSelected(nextWorkflowSelection(rail, selected, item.key))}
140 />
141 );
142 })}
143 </ScrollView>
144 ) : null}
145 {open === undefined ? null : open.members.length === 0 ? (
146 <Text
147 textRole="footnote"
148 className="border-t border-border-subtle px-4 py-3 text-foreground-muted"
149 >
150 <T>No agents yet</T>
151 </Text>
152 ) : (
153 open.members.map((member) => (
154 <WorkflowMemberRow
155 key={member.id}
156 environmentId={props.environmentId}
157 member={member}
158 provider={provider}
159 onOpen={props.onOpen}
160 />
161 ))
162 )}
163 </ListSection>
164 </View>
165 );
167​
168function WorkflowMemberRow(props: {
169 readonly environmentId: EnvironmentId;
170 readonly member: RuntimeSubagent;
171 readonly provider: ServerProvider | undefined;
172 readonly onOpen: (threadId: ThreadId) => void;
173}) {
174 const { member } = props;
175 const gt = useGT();
176 const m = useMessages();
177 const threadId = member.childThreadId === null ? null : ThreadId.make(member.childThreadId);
178 const shell = useThreadShell(
179 threadId === null ? null : scopeThreadRef(props.environmentId, threadId),
180 );
181 const canOpen = threadId !== null && shell !== null;
182 const presentation = resolveSubagentRowPresentation(
183 {
184 title: member.title,
185 prompt: "",
186 status: member.status,
187 result: null,
188 childThreadId: threadId,
189 },
190 false,
191 m,
192 );
193 const { detail, error } = workflowMemberDetail(member);
194 const metadata = workflowMemberMetadata(member, props.provider, m);
195 const labels = [metadata.model, metadata.effort].filter((value) => value !== null);
196 return (
197 <RowPressable
198 accessibilityRole={canOpen ? "link" : undefined}
199 accessibilityLabel={[
200 presentation.title,
201 presentation.statusLabel,
202 ...labels,
203 ...(metadata.speed === null ? [] : [metadata.speed]),
204 ...(error === null ? [] : [error]),
205 ].join(", ")}
206 accessibilityHint={
207 canOpen ? gt`Opens this agent's thread` : gt`Its thread is not available yet`
208 }
209 disabled={!canOpen}
210 onPress={() => threadId !== null && props.onOpen(threadId)}
211 className="min-h-11 flex-row gap-3 border-t border-border-subtle px-4 py-3 active:opacity-70"
212 >
213 <View className="h-5 justify-center">
214 <SubagentStatusDot tone={presentation.tone} placement="sheet" />
215 </View>
216 <View className="min-w-0 flex-1 gap-1">
217 <View className="min-h-5 flex-row items-center gap-2">
218 <View className="min-w-0 flex-1 flex-row items-baseline gap-1.5">
219 <Text
220 textRole="footnote"
221 numberOfLines={1}
222 className="min-w-0 shrink font-t3-medium text-foreground"
223 >
224 {presentation.title}
225 </Text>
226 <Text
227 accessibilityElementsHidden
228 importantForAccessibility="no"
229 textRole="label"
230 className="shrink-0 text-foreground-muted"
231 >
232 ·
233 </Text>
234 <Text
235 textRole="label"
236 className={cn("shrink-0 font-t3-medium", SUBAGENT_TONE_TEXT_CLASS[presentation.tone])}
237 >
238 {presentation.statusLabel}
239 </Text>
240 </View>
241 <WorkflowElapsed timing={member} />
242 {canOpen ? (
243 <SymbolView name="chevron.right" size={12} tintColorClassName="accent-icon-subtle" />
244 ) : null}
245 </View>
246 {labels.length > 0 || metadata.speed !== null ? (
247 <View className="min-w-0 flex-row items-center">
248 <Text
249 textRole="label"
250 numberOfLines={1}
251 className="min-w-0 shrink text-foreground-muted"
252 >
253 {labels.join(" · ")}
254 {labels.length > 0 && metadata.speed !== null ? " · " : ""}
255 </Text>
256 {metadata.fast ? (
257 <View className="shrink-0 flex-row items-center gap-0.5">
258 <SymbolView
259 name={{ ios: "bolt.fill", android: "bolt" }}
260 size={11}
261 tintColorClassName="accent-adaptive-amber-700-300"
262 />
263 <Text textRole="label" className="text-adaptive-amber-700-300">
264 {metadata.speed}
265 </Text>
266 </View>
267 ) : metadata.speed !== null ? (
268 <Text textRole="label" className="shrink-0 text-foreground-muted">
269 {metadata.speed}
270 </Text>
271 ) : null}
272 </View>
273 ) : null}
274 {detail ? (
275 <Text textRole="label" numberOfLines={1} className="text-foreground-muted">
276 {detail}
277 </Text>
278 ) : null}
279 {error ? (
280 <Text textRole="label" numberOfLines={1} className={SUBAGENT_TONE_TEXT_CLASS.failed}>
281 {error}
282 </Text>
283 ) : null}
284 </View>
285 </RowPressable>
286 );
288​
289function WorkflowHeaderLayout(props: {
290 readonly label: string;
291 readonly statusLabel: string;
292 readonly timing: ElapsedTiming;
293 readonly accessory: ReactNode;
294}) {
295 const elapsed = useElapsedLabel(props.timing);
296 return (
297 <ListRowLayout
298 icon="point.3.connected.trianglepath.dotted"
299 label={props.label}
300 description={props.statusLabel}
301 detail={elapsed ?? undefined}
302 accessory={props.accessory}
303 />
304 );
306​
307function WorkflowElapsed(props: { readonly timing: ElapsedTiming }) {
308 const elapsed = useElapsedLabel(props.timing);
309 return elapsed === null ? null : (
310 <Text textRole="label" className="shrink-0 tabular-nums text-foreground-muted">
311 {elapsed}
312 </Text>
313 );
315​
316type ElapsedTiming = Parameters<typeof deriveSubagentElapsedMs>[0];
317​
318function useElapsedLabel(timing: ElapsedTiming): string | null {
319 const nowMs = useVisibleSecondClock(isActiveSubagentStatus(timing.status));
320 const messages = useMessages();
321 const elapsedMs = deriveSubagentElapsedMs(timing, nowMs);
322 return elapsedMs === null || elapsedMs === 0 ? null : formatDuration(elapsedMs, messages);