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

ThreadWorkflowGroup.tsx

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