The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { useGT, useMessages } from "gt-react-native"; |
| 3 | import { StackActions, useNavigation } from "@react-navigation/native"; |
| 4 | import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 5 | import { |
| 6 | resolveProviderInstanceDisplayName, |
| 7 | shouldShowInstanceBadge, |
| 8 | } from "@t3tools/client-runtime/state/provider-instance-display"; |
| 9 | import { summarizeSubagentStatuses } from "@t3tools/client-runtime/state/subagent-display"; |
| 10 | import { isActiveSubagentStatus } from "@t3tools/client-runtime/state/subagentRuntime"; |
| 11 | import type { |
| 12 | EnvironmentId, |
| 13 | OrchestrationV2Subagent, |
| 14 | OrchestrationV2TurnItem, |
| 15 | ServerConfig, |
| 16 | } from "@t3tools/contracts"; |
| 17 | import { Pressable, View, type ColorValue } from "react-native"; |
| 18 | |
| 19 | import { AppText as Text } from "../../components/ui/AppText"; |
| 20 | import { SymbolView } from "../../components/ui/AppSymbol"; |
| 21 | import { ProviderIcon, ProviderInstanceIcon } from "../../components/ProviderIcon"; |
| 22 | import { cn } from "../../lib/cn"; |
| 23 | import type { ThreadFeedActivity } from "../../lib/threadActivity"; |
| 24 | import { serverEnvironment } from "../../state/server"; |
| 25 | import { environmentThreadDetails } from "../../state/threads"; |
| 26 | import { subagentCardElapsed } from "./subagent-card-presentation"; |
| 27 | import { useServerConfigs } from "../../state/entities"; |
| 28 | import { SubagentRow } from "./SubagentRow"; |
| 29 | import { useVisibleSecondClock } from "./use-visible-second-clock"; |
| 30 | import { subagentThreadTarget } from "./threadAgentsPresentation"; |
| 31 | import { useLinkedThreadReach } from "../../state/linkedThread"; |
| 32 | import { WorkLogBlock } from "./work-log-layout"; |
| 33 | |
| 34 | import { useOpenThreadInspector } from "./thread-inspector-navigation"; |
| 35 | import { useOpenLinkedThreadRead } from "./useOpenLinkedThread"; |
| 36 | |
| 37 | type SubagentItem = Extract<OrchestrationV2TurnItem, { type: "subagent" }>; |
| 38 | type AgentTiming = Pick<OrchestrationV2Subagent, "status" | "startedAt" | "completedAt">; |
| 39 | |
| 40 | function SubagentElapsed({ agents }: { readonly agents: ReadonlyArray<AgentTiming> }) { |
| 41 | const live = agents.some((agent) => isActiveSubagentStatus(agent.status)); |
| 42 | const nowMs = useVisibleSecondClock(live); |
| 43 | const messages = useMessages(); |
| 44 | const elapsed = subagentCardElapsed(agents, nowMs, messages); |
| 45 | return elapsed ? ( |
| 46 | <Text className="shrink-0 text-xs tabular-nums text-foreground-muted">{elapsed}</Text> |
| 47 | ) : null; |
| 48 | } |
| 49 | |
| 50 | function SubagentAvatar(props: { |
| 51 | readonly item: SubagentItem; |
| 52 | readonly config: ServerConfig | null | undefined; |
| 53 | }) { |
| 54 | const providers = props.config?.providers ?? []; |
| 55 | const provider = providers.find( |
| 56 | (candidate) => candidate.instanceId === props.item.providerInstanceId, |
| 57 | ); |
| 58 | // A remote task's own driver is only a "remote" label. |
| 59 | const driver = |
| 60 | provider?.driver ?? (props.item.remoteChild === undefined ? props.item.driver : undefined); |
| 61 | const badgeEntries = providers.map((candidate) => { |
| 62 | const rawConfig = props.config?.settings.providerInstances[candidate.instanceId]?.config; |
| 63 | const instanceConfig = |
| 64 | rawConfig !== null && typeof rawConfig === "object" |
| 65 | ? (rawConfig as Readonly<Record<string, unknown>>) |
| 66 | : null; |
| 67 | const agentId = instanceConfig?.source === "local" ? undefined : instanceConfig?.agentId; |
| 68 | return { |
| 69 | ...candidate, |
| 70 | driverKind: candidate.driver, |
| 71 | acpRegistryAgentId: typeof agentId === "string" ? agentId.trim() || undefined : undefined, |
| 72 | }; |
| 73 | }); |
| 74 | const badgeEntry = badgeEntries.find( |
| 75 | (candidate) => candidate.instanceId === props.item.providerInstanceId, |
| 76 | ); |
| 77 | const showBadge = |
| 78 | provider !== undefined && |
| 79 | badgeEntry !== undefined && |
| 80 | shouldShowInstanceBadge(badgeEntry, badgeEntries); |
| 81 | return ( |
| 82 | <View |
| 83 | accessible={false} |
| 84 | className="h-7 w-7 shrink-0 items-center justify-center rounded-full border border-border bg-card" |
| 85 | > |
| 86 | {showBadge ? ( |
| 87 | <ProviderInstanceIcon |
| 88 | provider={provider.driver} |
| 89 | iconUrl={provider.iconUrl} |
| 90 | size={15} |
| 91 | displayName={resolveProviderInstanceDisplayName(provider)} |
| 92 | accentColor={provider.accentColor} |
| 93 | showBadge |
| 94 | /> |
| 95 | ) : driver === undefined ? ( |
| 96 | <SymbolView name="robot" size={13} tintColorClassName="accent-icon-muted" /> |
| 97 | ) : ( |
| 98 | <ProviderIcon provider={driver} iconUrl={provider?.iconUrl} size={15} /> |
| 99 | )} |
| 100 | </View> |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | export function ThreadSubagentGroup(props: { |
| 105 | readonly activities: ReadonlyArray<ThreadFeedActivity>; |
| 106 | readonly environmentId: EnvironmentId; |
| 107 | readonly anchorKey: string; |
| 108 | readonly expandedRows: Readonly<Record<string, boolean>>; |
| 109 | readonly iconSubtleColor: ColorValue; |
| 110 | readonly onToggleRow: (rowId: string, anchorKey: string) => void; |
| 111 | }) { |
| 112 | const gt = useGT(); |
| 113 | const messages = useMessages(); |
| 114 | const config = useAtomValue(serverEnvironment.configValueAtom(props.environmentId)); |
| 115 | const navigation = useNavigation(); |
| 116 | const openInspector = useOpenThreadInspector(); |
| 117 | const openLinkedRead = useOpenLinkedThreadRead(); |
| 118 | const members = props.activities.flatMap(({ projectedItem }) => |
| 119 | projectedItem.item.type === "subagent" ? [projectedItem.item] : [], |
| 120 | ); |
| 121 | const liveAgents = useAtomValue( |
| 122 | environmentThreadDetails.threadAtom(scopeThreadRef(props.environmentId, members[0]!.threadId)), |
| 123 | (thread) => thread?.projection.subagents, |
| 124 | ); |
| 125 | const agents = members.map((item) => { |
| 126 | const live = liveAgents?.find((agent) => agent.id === item.subagentId); |
| 127 | return { |
| 128 | ...item, |
| 129 | item, |
| 130 | status: live?.status ?? item.status, |
| 131 | startedAt: live?.startedAt ?? item.startedAt, |
| 132 | completedAt: live?.completedAt ?? item.completedAt, |
| 133 | result: live?.result ?? item.result, |
| 134 | progress: live?.progress ?? item.progress, |
| 135 | model: live?.model ?? null, |
| 136 | modelSelection: live?.modelSelection, |
| 137 | }; |
| 138 | }); |
| 139 | const reach = useLinkedThreadReach(props.environmentId, agents); |
| 140 | const grouped = agents.length > 1; |
| 141 | const label = gt("{count, plural, one {{count} subagent} other {{count} subagents}}", { |
| 142 | count: agents.length, |
| 143 | }); |
| 144 | const summary = summarizeSubagentStatuses( |
| 145 | agents.map((agent) => agent.status), |
| 146 | messages, |
| 147 | ); |
| 148 | const expanded = props.expandedRows[props.anchorKey] ?? false; |
| 149 | const serverConfigs = useServerConfigs(); |
| 150 | // A task in a linked environment runs a provider instance of that environment. |
| 151 | const configOf = (item: SubagentItem) => |
| 152 | item.remoteChild === undefined ? config : serverConfigs.get(item.remoteChild.environmentId); |
| 153 | return ( |
| 154 | <WorkLogBlock> |
| 155 | {grouped ? ( |
| 156 | <Pressable |
| 157 | accessibilityRole="button" |
| 158 | accessibilityLabel={gt("{label}, {summary}", { label, summary })} |
| 159 | accessibilityState={{ expanded }} |
| 160 | onPress={() => props.onToggleRow(props.anchorKey, props.anchorKey)} |
| 161 | className="min-h-14 flex-row items-center gap-3 rounded-lg py-2 active:bg-subtle" |
| 162 | > |
| 163 | <View className="flex-row items-center"> |
| 164 | {agents.slice(0, 3).map((agent, index) => ( |
| 165 | <View key={agent.item.id} style={{ marginLeft: index === 0 ? 0 : -7 }}> |
| 166 | <SubagentAvatar item={agent.item} config={configOf(agent.item)} /> |
| 167 | </View> |
| 168 | ))} |
| 169 | {agents.length > 3 ? ( |
| 170 | <View className="-ml-2 h-7 w-7 items-center justify-center rounded-full border border-border bg-card"> |
| 171 | <Text className="text-2xs text-foreground-muted">+{agents.length - 3}</Text> |
| 172 | </View> |
| 173 | ) : null} |
| 174 | </View> |
| 175 | <View className="min-w-0 flex-1 gap-0.5"> |
| 176 | <Text numberOfLines={1} className="font-t3-medium text-sm text-foreground"> |
| 177 | {label} |
| 178 | </Text> |
| 179 | <Text |
| 180 | numberOfLines={1} |
| 181 | className={cn( |
| 182 | "text-2xs text-foreground-muted", |
| 183 | agents.some((agent) => isActiveSubagentStatus(agent.status)) |
| 184 | ? "text-adaptive-sky-600-400" |
| 185 | : agents.some((agent) => agent.status === "failed") && |
| 186 | "text-adaptive-rose-600-400", |
| 187 | )} |
| 188 | > |
| 189 | {summary} |
| 190 | </Text> |
| 191 | </View> |
| 192 | <SubagentElapsed agents={agents} /> |
| 193 | <SymbolView |
| 194 | name={expanded ? "chevron.up" : "chevron.down"} |
| 195 | size={11} |
| 196 | tintColor={props.iconSubtleColor} |
| 197 | /> |
| 198 | </Pressable> |
| 199 | ) : null} |
| 200 | {!grouped || expanded ? ( |
| 201 | <View className="mb-1 gap-px rounded-xl border border-border bg-card/30 p-1"> |
| 202 | {agents.map((agent) => { |
| 203 | // A task in a linked environment opens there, if this app is connected to it. |
| 204 | const target = subagentThreadTarget( |
| 205 | agent, |
| 206 | props.environmentId, |
| 207 | reach.ownConnection, |
| 208 | reach.isLinked, |
| 209 | ); |
| 210 | return ( |
| 211 | <Pressable |
| 212 | key={agent.item.id} |
| 213 | accessible |
| 214 | accessibilityRole={target === null ? undefined : "link"} |
| 215 | accessibilityHint={ |
| 216 | target === null |
| 217 | ? gt("Provider-managed agent") |
| 218 | : target.linked === undefined |
| 219 | ? gt("Opens this agent's thread") |
| 220 | : gt("Opens a read-only view of this agent's thread on {label}", { |
| 221 | label: target.linked.label, |
| 222 | }) |
| 223 | } |
| 224 | disabled={target === null} |
| 225 | onPress={() => { |
| 226 | if (target === null) return; |
| 227 | if (target.linked !== undefined) { |
| 228 | openLinkedRead(target, target.linked, agent.title, { |
| 229 | source: { |
| 230 | environmentId: props.environmentId, |
| 231 | threadId: members[0]!.threadId, |
| 232 | }, |
| 233 | }); |
| 234 | return; |
| 235 | } |
| 236 | if ( |
| 237 | openInspector( |
| 238 | { environmentId: props.environmentId, threadId: members[0]!.threadId }, |
| 239 | { |
| 240 | type: "thread", |
| 241 | environmentId: target.environmentId, |
| 242 | threadId: target.threadId, |
| 243 | }, |
| 244 | ) |
| 245 | ) |
| 246 | return; |
| 247 | navigation.dispatch( |
| 248 | StackActions.push("Thread", { |
| 249 | environmentId: String(target.environmentId), |
| 250 | threadId: String(target.threadId), |
| 251 | }), |
| 252 | ); |
| 253 | }} |
| 254 | className="rounded-lg px-3 py-3 active:bg-subtle" |
| 255 | > |
| 256 | <SubagentRow |
| 257 | environmentId={props.environmentId} |
| 258 | subagent={agent} |
| 259 | canOpenRemote={target !== null} |
| 260 | elapsed={<SubagentElapsed agents={[agent]} />} |
| 261 | /> |
| 262 | </Pressable> |
| 263 | ); |
| 264 | })} |
| 265 | </View> |
| 266 | ) : null} |
| 267 | </WorkLogBlock> |
| 268 | ); |
| 269 | } |