|
| 1 | +import { useAtomValue } from "@effect/atom-react"; |
| 2 | +import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 3 | +import { |
| 4 | + isActiveSubagentStatus, |
| 5 | + workflowPhaseElapsed, |
| 6 | + workflowPhaseStatus, |
| 7 | + type RuntimeSubagent, |
| 8 | + type SubagentWorkflowGroup, |
| 9 | +} from "@t3tools/client-runtime/state/subagentRuntime"; |
| 10 | +import { |
| 11 | + NodeId, |
| 12 | + ThreadId, |
| 13 | + type EnvironmentId, |
| 14 | + type OrchestrationV2Subagent, |
| 15 | +} from "@t3tools/contracts"; |
| 16 | +import { deriveSubagentElapsedMs, formatDuration } from "@t3tools/shared/orchestrationTiming"; |
| 17 | +import { useState, type ReactNode } from "react"; |
| 18 | +import { Alert, Pressable, View } from "react-native"; |
| 19 | + |
| 20 | +import { AppActivityIndicator } from "../../components/AppActivityIndicator"; |
| 21 | +import { AppText as Text } from "../../components/AppText"; |
| 22 | +import { SymbolView } from "../../components/AppSymbol"; |
| 23 | +import { cn } from "../../lib/cn"; |
| 24 | +import { useThreadShell } from "../../state/entities"; |
| 25 | +import { orchestrationEnvironment } from "../../state/orchestration"; |
| 26 | +import { useAtomCommand } from "../../state/use-atom-command"; |
| 27 | +import { subagentCardDetail } from "./subagent-card-presentation"; |
| 28 | +import { SUBAGENT_TONE_TEXT_CLASS, SubagentStatusDot } from "./SubagentStatusDot"; |
| 29 | +import { resolveSubagentRowPresentation, workflowGroupSummary } from "./threadAgentsPresentation"; |
| 30 | +import { useVisibleSecondClock } from "./use-visible-second-clock"; |
| 31 | + |
| 32 | +export function ThreadWorkflowGroup(props: { |
| 33 | + readonly environmentId: EnvironmentId; |
| 34 | + readonly coordinator: OrchestrationV2Subagent; |
| 35 | + readonly group: SubagentWorkflowGroup; |
| 36 | + readonly header: ReactNode; |
| 37 | + readonly onOpen: (threadId: ThreadId) => void; |
| 38 | +}) { |
| 39 | + const { coordinator, group } = props; |
| 40 | + const [expanded, setExpanded] = useState(() => isActiveSubagentStatus(group.workflow.status)); |
| 41 | + const [phaseOpen, setPhaseOpen] = useState<ReadonlyMap<number, boolean>>(() => new Map()); |
| 42 | + const label = group.workflow.workflowName ?? group.workflow.title; |
| 43 | + const summary = workflowGroupSummary(group); |
| 44 | + const phases = group.phases.filter((phase) => phase.members.length > 0); |
| 45 | + |
| 46 | + const canOperate = useAtomValue( |
| 47 | + orchestrationEnvironment.stopWorkflow.permissionAtom(props.environmentId), |
| 48 | + ); |
| 49 | + const stopWorkflow = useAtomCommand(orchestrationEnvironment.stopWorkflow); |
| 50 | + const [stopping, setStopping] = useState(false); |
| 51 | + const canStop = |
| 52 | + canOperate && |
| 53 | + group.workflow.status === "running" && |
| 54 | + coordinator.origin === "provider_native" && |
| 55 | + coordinator.driver === "claudeAgent"; |
| 56 | + const confirmStop = () => |
| 57 | + Alert.alert("Stop workflow?", `Every running agent in ${label} stops.`, [ |
| 58 | + { text: "Cancel", style: "cancel" }, |
| 59 | + { |
| 60 | + text: "Stop", |
| 61 | + style: "destructive", |
| 62 | + onPress: async () => { |
| 63 | + setStopping(true); |
| 64 | + const result = await stopWorkflow({ |
| 65 | + environmentId: props.environmentId, |
| 66 | + input: { threadId: coordinator.threadId, subagentId: NodeId.make(coordinator.id) }, |
| 67 | + }); |
| 68 | + setStopping(false); |
| 69 | + if (result._tag === "Failure") Alert.alert("Could not stop workflow"); |
| 70 | + }, |
| 71 | + }, |
| 72 | + ]); |
| 73 | + |
| 74 | + return ( |
| 75 | + <View className="border-b border-border pb-2"> |
| 76 | + {props.header} |
| 77 | + <View className="-mt-2 flex-row items-center gap-2"> |
| 78 | + <Pressable |
| 79 | + accessibilityRole="button" |
| 80 | + accessibilityLabel={`${expanded ? "Collapse" : "Expand"} ${label}, ${summary}`} |
| 81 | + accessibilityState={{ expanded }} |
| 82 | + onPress={() => setExpanded(!expanded)} |
| 83 | + className="min-h-11 min-w-0 flex-1 flex-row items-center gap-3 rounded-lg active:opacity-70" |
| 84 | + > |
| 85 | + <View className="w-2 items-center"> |
| 86 | + <SymbolView |
| 87 | + name={expanded ? "chevron.down" : "chevron.right"} |
| 88 | + size={11} |
| 89 | + tintColorClassName="accent-icon-subtle" |
| 90 | + /> |
| 91 | + </View> |
| 92 | + <Text numberOfLines={1} className="min-w-0 shrink text-xs text-foreground-muted"> |
| 93 | + {summary} |
| 94 | + </Text> |
| 95 | + </Pressable> |
| 96 | + {canStop ? ( |
| 97 | + <Pressable |
| 98 | + accessibilityRole="button" |
| 99 | + accessibilityLabel={`Stop workflow ${label}`} |
| 100 | + accessibilityState={{ disabled: stopping, busy: stopping }} |
| 101 | + disabled={stopping} |
| 102 | + hitSlop={8} |
| 103 | + onPress={confirmStop} |
| 104 | + className="min-h-9 flex-row items-center gap-1.5 rounded-full bg-subtle px-3 active:opacity-70 disabled:opacity-50" |
| 105 | + > |
| 106 | + {stopping ? ( |
| 107 | + <AppActivityIndicator size="small" /> |
| 108 | + ) : ( |
| 109 | + <SymbolView |
| 110 | + name="stop.fill" |
| 111 | + size={10} |
| 112 | + tintColorClassName="accent-danger-foreground" |
| 113 | + /> |
| 114 | + )} |
| 115 | + <Text className="font-t3-medium text-xs text-danger-foreground">Stop</Text> |
| 116 | + </Pressable> |
| 117 | + ) : null} |
| 118 | + </View> |
| 119 | + {expanded ? ( |
| 120 | + <View className="ml-[3px] border-l border-border pl-3.5"> |
| 121 | + {phases.map((phase) => { |
| 122 | + const open = phaseOpen.get(phase.index) ?? phase.state === "running"; |
| 123 | + const status = workflowPhaseStatus(phase); |
| 124 | + const count = `${phase.settledCount}/${phase.members.length} ${phase.members.length === 1 ? "agent" : "agents"}`; |
| 125 | + return ( |
| 126 | + <View key={phase.index}> |
| 127 | + <Pressable |
| 128 | + accessibilityRole="button" |
| 129 | + accessibilityLabel={`${phase.title} phase, ${status}, ${count} done`} |
| 130 | + accessibilityState={{ expanded: open }} |
| 131 | + onPress={() => |
| 132 | + setPhaseOpen((current) => new Map(current).set(phase.index, !open)) |
| 133 | + } |
| 134 | + className="min-h-11 flex-row items-center gap-2 active:opacity-70" |
| 135 | + > |
| 136 | + <SymbolView |
| 137 | + name={open ? "chevron.down" : "chevron.right"} |
| 138 | + size={10} |
| 139 | + tintColorClassName="accent-icon-subtle" |
| 140 | + /> |
| 141 | + <Text |
| 142 | + numberOfLines={1} |
| 143 | + className={cn( |
| 144 | + "min-w-0 shrink font-t3-medium text-xs", |
| 145 | + phase.state === "running" ? "text-foreground" : "text-foreground-muted", |
| 146 | + )} |
| 147 | + > |
| 148 | + {phase.title} |
| 149 | + </Text> |
| 150 | + {status === "failed" ? ( |
| 151 | + <Text |
| 152 | + className={cn("text-2xs font-t3-medium", SUBAGENT_TONE_TEXT_CLASS.failed)} |
| 153 | + > |
| 154 | + Failed |
| 155 | + </Text> |
| 156 | + ) : null} |
| 157 | + <View className="flex-1" /> |
| 158 | + <Text className="text-2xs tabular-nums text-foreground-muted">{count}</Text> |
| 159 | + <WorkflowElapsed timing={workflowPhaseElapsed(phase)} /> |
| 160 | + </Pressable> |
| 161 | + {open |
| 162 | + ? phase.members.map((member) => ( |
| 163 | + <WorkflowMemberRow |
| 164 | + key={member.id} |
| 165 | + environmentId={props.environmentId} |
| 166 | + member={member} |
| 167 | + onOpen={props.onOpen} |
| 168 | + /> |
| 169 | + )) |
| 170 | + : null} |
| 171 | + </View> |
| 172 | + ); |
| 173 | + })} |
| 174 | + {group.unphasedMembers.map((member) => ( |
| 175 | + <WorkflowMemberRow |
| 176 | + key={member.id} |
| 177 | + environmentId={props.environmentId} |
| 178 | + member={member} |
| 179 | + onOpen={props.onOpen} |
| 180 | + /> |
| 181 | + ))} |
| 182 | + </View> |
| 183 | + ) : null} |
| 184 | + </View> |
| 185 | + ); |
| 186 | +} |
| 187 | + |
| 188 | +function WorkflowMemberRow(props: { |
| 189 | + readonly environmentId: EnvironmentId; |
| 190 | + readonly member: RuntimeSubagent; |
| 191 | + readonly onOpen: (threadId: ThreadId) => void; |
| 192 | +}) { |
| 193 | + const { member } = props; |
| 194 | + const threadId = member.childThreadId === null ? null : ThreadId.make(member.childThreadId); |
| 195 | + const shell = useThreadShell( |
| 196 | + threadId === null ? null : scopeThreadRef(props.environmentId, threadId), |
| 197 | + ); |
| 198 | + const canOpen = threadId !== null && shell !== null; |
| 199 | + const presentation = resolveSubagentRowPresentation({ |
| 200 | + title: member.title, |
| 201 | + prompt: "", |
| 202 | + status: member.status, |
| 203 | + progress: member.progress ?? undefined, |
| 204 | + result: member.result ?? member.error, |
| 205 | + childThreadId: threadId, |
| 206 | + }); |
| 207 | + const detail = subagentCardDetail(presentation.detail); |
| 208 | + return ( |
| 209 | + <Pressable |
| 210 | + accessibilityRole={canOpen ? "link" : undefined} |
| 211 | + accessibilityLabel={`${presentation.title}, ${presentation.statusLabel}`} |
| 212 | + accessibilityHint={canOpen ? "Opens this agent's thread" : "Its thread is not available yet"} |
| 213 | + disabled={!canOpen} |
| 214 | + onPress={() => threadId !== null && props.onOpen(threadId)} |
| 215 | + className="min-h-11 flex-row gap-2.5 py-2 active:opacity-70" |
| 216 | + > |
| 217 | + <View className="h-5 justify-center"> |
| 218 | + <SubagentStatusDot tone={presentation.tone} /> |
| 219 | + </View> |
| 220 | + <View className="min-w-0 flex-1 gap-0.5"> |
| 221 | + <View className="min-h-5 flex-row items-center gap-2"> |
| 222 | + <Text numberOfLines={1} className="min-w-0 flex-1 text-sm text-foreground"> |
| 223 | + {presentation.title} |
| 224 | + </Text> |
| 225 | + <Text className={cn("shrink-0 text-xs", SUBAGENT_TONE_TEXT_CLASS[presentation.tone])}> |
| 226 | + {presentation.statusLabel} |
| 227 | + </Text> |
| 228 | + <WorkflowElapsed timing={member} /> |
| 229 | + {canOpen ? ( |
| 230 | + <SymbolView name="chevron.right" size={12} tintColorClassName="accent-icon-subtle" /> |
| 231 | + ) : null} |
| 232 | + </View> |
| 233 | + {detail ? ( |
| 234 | + <Text numberOfLines={2} className="text-xs text-foreground-muted"> |
| 235 | + {detail} |
| 236 | + </Text> |
| 237 | + ) : null} |
| 238 | + </View> |
| 239 | + </Pressable> |
| 240 | + ); |
| 241 | +} |
| 242 | + |
| 243 | +function WorkflowElapsed({ |
| 244 | + timing, |
| 245 | +}: { |
| 246 | + readonly timing: Parameters<typeof deriveSubagentElapsedMs>[0]; |
| 247 | +}) { |
| 248 | + const nowMs = useVisibleSecondClock(isActiveSubagentStatus(timing.status)); |
| 249 | + const elapsedMs = deriveSubagentElapsedMs(timing, nowMs); |
| 250 | + return elapsedMs === null || elapsedMs === 0 ? null : ( |
| 251 | + <Text className="shrink-0 text-xs tabular-nums text-foreground-muted"> |
| 252 | + {formatDuration(elapsedMs)} |
| 253 | + </Text> |
| 254 | + ); |
| 255 | +} |
0 commit comments