@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 318 lines 11.4 KB
Raw
1 import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2 import {
3 isActiveSubagentStatus,
4 type RuntimeSubagent,
5 type SubagentWorkflowGroup,
6 } from "@t3tools/client-runtime/state/subagentRuntime";
7 import {
8 ThreadId,
9 type EnvironmentId,
10 type OrchestrationV2Subagent,
11 type ServerProvider,
12 } from "@t3tools/contracts";
13 import { deriveSubagentElapsedMs } from "@t3tools/shared/orchestrationTiming";
14 import { formatDuration } from "@t3tools/shared/orchestrationTimingCopy";
15 import { T, useGT, useMessages } from "gt-react-native";
16 import { useState, type ReactNode } from "react";
17 import { ScrollView, View } from "react-native";
18
19 import { AppText as Text } from "../../components/ui/AppText";
20 import { SymbolView, type AppSymbolName } from "../../components/ui/AppSymbol";
21 import { Chip } from "../../components/ui/Chip";
22 import { ListRowLayout } from "../../components/ui/ListRowLayout";
23 import { ListSection } from "../../components/ui/ListSection";
24 import { RowPressable } from "../../components/ui/RowPressable";
25 import { cn } from "../../lib/cn";
26 import { useEnvironmentServerConfig, useThreadShell } from "../../state/entities";
27 import { SUBAGENT_TONE_TEXT_CLASS, SubagentStatusDot } from "./SubagentStatusDot";
28 import { SubagentStopButton } from "./SubagentStopButton";
29 import {
30 nextWorkflowSelection,
31 resolveSubagentRowPresentation,
32 workflowMemberDetail,
33 workflowMemberMetadata,
34 workflowRail,
35 type WorkflowRailTone,
36 } from "./threadAgentsPresentation";
37 import { useVisibleSecondClock } from "./use-visible-second-clock";
38
39 const 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
47 export 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}", { label, 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(
119 "{title} phase, {tone, select, working {working} completed {completed} failed {failed} stopped {stopped} other {queued}}, {count} completed",
120 {
121 title: item.title,
122 tone: item.tone,
123 count,
124 },
125 )
126 : gt(
127 "{title} phase, {tone, select, working {working} completed {completed} failed {failed} stopped {stopped} other {queued}}",
128 {
129 title: item.title,
130 tone: item.tone,
131 },
132 )
133 }
134 onPress={() => setSelected(nextWorkflowSelection(rail, selected, item.key))}
135 />
136 );
137 })}
138 </ScrollView>
139 ) : null}
140 {open === undefined ? null : open.members.length === 0 ? (
141 <Text
142 textRole="footnote"
143 className="border-t border-border-subtle px-4 py-3 text-foreground-muted"
144 >
145 <T>No agents yet</T>
146 </Text>
147 ) : (
148 open.members.map((member) => (
149 <WorkflowMemberRow
150 key={member.id}
151 environmentId={props.environmentId}
152 member={member}
153 provider={provider}
154 onOpen={props.onOpen}
155 />
156 ))
157 )}
158 </ListSection>
159 </View>
160 );
161 }
162
163 function WorkflowMemberRow(props: {
164 readonly environmentId: EnvironmentId;
165 readonly member: RuntimeSubagent;
166 readonly provider: ServerProvider | undefined;
167 readonly onOpen: (threadId: ThreadId) => void;
168 }) {
169 const { member } = props;
170 const gt = useGT();
171 const m = useMessages();
172 const threadId = member.childThreadId === null ? null : ThreadId.make(member.childThreadId);
173 const shell = useThreadShell(
174 threadId === null ? null : scopeThreadRef(props.environmentId, threadId),
175 );
176 const canOpen = threadId !== null && shell !== null;
177 const presentation = resolveSubagentRowPresentation(
178 {
179 title: member.title,
180 prompt: "",
181 status: member.status,
182 result: null,
183 childThreadId: threadId,
184 },
185 false,
186 m,
187 );
188 const { detail, error } = workflowMemberDetail(member);
189 const metadata = workflowMemberMetadata(member, props.provider, m);
190 const labels = [metadata.model, metadata.effort].filter((value) => value !== null);
191 return (
192 <RowPressable
193 accessibilityRole={canOpen ? "link" : undefined}
194 accessibilityLabel={[
195 presentation.title,
196 presentation.statusLabel,
197 ...labels,
198 ...(metadata.speed === null ? [] : [metadata.speed]),
199 ...(error === null ? [] : [error]),
200 ].join(", ")}
201 accessibilityHint={
202 canOpen ? gt("Opens this agent's thread") : gt("Its thread is not available yet")
203 }
204 disabled={!canOpen}
205 onPress={() => threadId !== null && props.onOpen(threadId)}
206 className="min-h-11 flex-row gap-3 border-t border-border-subtle px-4 py-3 active:opacity-70"
207 >
208 <View className="h-5 justify-center">
209 <SubagentStatusDot tone={presentation.tone} placement="sheet" />
210 </View>
211 <View className="min-w-0 flex-1 gap-1">
212 <View className="min-h-5 flex-row items-center gap-2">
213 <View className="min-w-0 flex-1 flex-row items-baseline gap-1.5">
214 <Text
215 textRole="footnote"
216 numberOfLines={1}
217 className="min-w-0 shrink font-t3-medium text-foreground"
218 >
219 {presentation.title}
220 </Text>
221 <Text
222 accessibilityElementsHidden
223 importantForAccessibility="no"
224 textRole="label"
225 className="shrink-0 text-foreground-muted"
226 >
227 ·
228 </Text>
229 <Text
230 textRole="label"
231 className={cn("shrink-0 font-t3-medium", SUBAGENT_TONE_TEXT_CLASS[presentation.tone])}
232 >
233 {presentation.statusLabel}
234 </Text>
235 </View>
236 <WorkflowElapsed timing={member} />
237 {canOpen ? (
238 <SymbolView name="chevron.right" size={12} tintColorClassName="accent-icon-subtle" />
239 ) : null}
240 </View>
241 {labels.length > 0 || metadata.speed !== null ? (
242 <View className="min-w-0 flex-row items-center">
243 <Text
244 textRole="label"
245 numberOfLines={1}
246 className="min-w-0 shrink text-foreground-muted"
247 >
248 {labels.join(" · ")}
249 {labels.length > 0 && metadata.speed !== null ? " · " : ""}
250 </Text>
251 {metadata.fast ? (
252 <View className="shrink-0 flex-row items-center gap-0.5">
253 <SymbolView
254 name={{ ios: "bolt.fill", android: "bolt" }}
255 size={11}
256 tintColorClassName="accent-adaptive-amber-700-300"
257 />
258 <Text textRole="label" className="text-adaptive-amber-700-300">
259 {metadata.speed}
260 </Text>
261 </View>
262 ) : metadata.speed !== null ? (
263 <Text textRole="label" className="shrink-0 text-foreground-muted">
264 {metadata.speed}
265 </Text>
266 ) : null}
267 </View>
268 ) : null}
269 {detail ? (
270 <Text textRole="label" numberOfLines={1} className="text-foreground-muted">
271 {detail}
272 </Text>
273 ) : null}
274 {error ? (
275 <Text textRole="label" numberOfLines={1} className={SUBAGENT_TONE_TEXT_CLASS.failed}>
276 {error}
277 </Text>
278 ) : null}
279 </View>
280 </RowPressable>
281 );
282 }
283
284 function WorkflowHeaderLayout(props: {
285 readonly label: string;
286 readonly statusLabel: string;
287 readonly timing: ElapsedTiming;
288 readonly accessory: ReactNode;
289 }) {
290 const elapsed = useElapsedLabel(props.timing);
291 return (
292 <ListRowLayout
293 icon="point.3.connected.trianglepath.dotted"
294 label={props.label}
295 description={props.statusLabel}
296 detail={elapsed ?? undefined}
297 accessory={props.accessory}
298 />
299 );
300 }
301
302 function WorkflowElapsed(props: { readonly timing: ElapsedTiming }) {
303 const elapsed = useElapsedLabel(props.timing);
304 return elapsed === null ? null : (
305 <Text textRole="label" className="shrink-0 tabular-nums text-foreground-muted">
306 {elapsed}
307 </Text>
308 );
309 }
310
311 type ElapsedTiming = Parameters<typeof deriveSubagentElapsedMs>[0];
312
313 function useElapsedLabel(timing: ElapsedTiming): string | null {
314 const nowMs = useVisibleSecondClock(isActiveSubagentStatus(timing.status));
315 const messages = useMessages();
316 const elapsedMs = deriveSubagentElapsedMs(timing, nowMs);
317 return elapsedMs === null || elapsedMs === 0 ? null : formatDuration(elapsedMs, messages);
318 }